xref: /rk3399_rockchip-uboot/cmd/crypto.c (revision 827e2ae92e2103f82dab5b54228ad24e40db6263)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
4  */
5 
6 #include <common.h>
7 #include <command.h>
8 #include <crypto.h>
9 #include <dm.h>
10 #include <u-boot/md5.h>
11 #include <u-boot/sha1.h>
12 #include <u-boot/sha256.h>
13 #include <u-boot/sha512.h>
14 
15 #define HASH_PERF_EVAL(dev, algo)	hash_perf_eval(dev, algo, #algo)
16 
17 __cacheline_aligned static u8 foo_data[] = {
18 	0x52, 0x53, 0x41, 0x4b, 0x00, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00,
19 	0xda, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd5, 0xf2, 0xfc, 0xbb,
21 	0x1a, 0x39, 0x61, 0xf5, 0x63, 0x7f, 0xa6, 0xeb, 0x5d, 0xc5, 0x22, 0xe2,
22 	0x65, 0x03, 0xcc, 0x61, 0x92, 0x60, 0x4c, 0x5f, 0x4e, 0x81, 0x96, 0x3d,
23 	0x89, 0x1d, 0xa3, 0x5c, 0xb7, 0x88, 0xc3, 0x75, 0xcd, 0x71, 0xf0, 0x16,
24 	0x04, 0x5e, 0xef, 0x85, 0xdf, 0x30, 0x57, 0x7e, 0x06, 0x0d, 0x44, 0x3b,
25 	0x80, 0x22, 0x41, 0xcd, 0xba, 0x40, 0xb5, 0xfa, 0xbe, 0x8f, 0xa9, 0x61,
26 	0x4d, 0xc6, 0x2a, 0xd9, 0x96, 0x57, 0x57, 0xc6, 0xc0, 0x99, 0xc0, 0x94,
27 	0x57, 0x21, 0x0a, 0xad, 0xde, 0x4c, 0xe1, 0xaa, 0x6e, 0x11, 0xc6, 0xeb,
28 	0xa2, 0x09, 0x65, 0xd9, 0xd5,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x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63 	0x00, 0x00, 0x00, 0x00, 0xa5, 0x0d, 0xc2, 0xc6, 0xf1, 0x08, 0x95, 0x55,
64 	0x1d, 0xb3, 0xf9, 0x43, 0xb0, 0x7b, 0x5b, 0x96, 0xa0, 0x72, 0xe8, 0xd6,
65 	0x95, 0xd6, 0x98, 0xec, 0x53, 0x6b, 0x85, 0x77, 0x74, 0x21, 0xf1, 0x22,
66 	0x5c, 0x4d, 0x56, 0x55, 0x89, 0x0d, 0x60, 0xa5, 0x2b, 0x68, 0xf8, 0x71,
67 	0x6d, 0x72, 0xed, 0x67, 0x55, 0x3e, 0x5f, 0xd1, 0x9e, 0x21, 0xa7, 0x6e,
68 	0x5c, 0xf2, 0xac, 0xe8, 0x66, 0x1d, 0x4e, 0xe5, 0x58, 0x76, 0xed, 0x2b,
69 	0xfe, 0x2b, 0xc9, 0xf6, 0x7f, 0xb7, 0x02, 0x27, 0xfd, 0x25, 0x3f, 0x2d,
70 	0x7c, 0x1c, 0x37, 0x80, 0x29, 0x7f, 0x12, 0xd2, 0xd5, 0xae, 0x27, 0xc6,
71 	0x68, 0x24, 0x83, 0xc5, 0x61, 0x8b, 0xea, 0x1a, 0xbd, 0x07, 0x96, 0xe4,
72 	0xd1, 0xc5, 0xd4, 0x21, 0x39, 0x4a, 0xaa, 0x66, 0xd2, 0x3f, 0xd2, 0x67,
73 	0x55, 0x39, 0xbf, 0x40, 0x66, 0x66, 0x5a, 0x92, 0xdd, 0x8d, 0xda, 0x5a,
74 	0x89, 0x7e, 0x78, 0xd9, 0x5f, 0x42, 0xe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, 0x46, 0x93, 0x24, 0x89, 0x1b, 0x74, 0x3c, 0xa0,
90 	0x20, 0x3b, 0x4e, 0x64, 0xff, 0xff, 0xff, 0xff,
91 };
92 
93 static u8 rsa2048_n[] = {
94 	0xd5, 0xf2, 0xfc, 0xbb, 0x1a, 0x39, 0x61, 0xf5, 0x63, 0x7f, 0xa6, 0xeb,
95 	0x5d, 0xc5, 0x22, 0xe2, 0x65, 0x03, 0xcc, 0x61, 0x92, 0x60, 0x4c, 0x5f,
96 	0x4e, 0x81, 0x96, 0x3d, 0x89, 0x1d, 0xa3, 0x5c, 0xb7, 0x88, 0xc3, 0x75,
97 	0xcd, 0x71, 0xf0, 0x16, 0x04, 0x5e, 0xef, 0x85, 0xdf, 0x30, 0x57, 0x7e,
98 	0x06, 0x0d, 0x44, 0x3b, 0x80, 0x22, 0x41, 0xcd, 0xba, 0x40, 0xb5, 0xfa,
99 	0xbe, 0x8f, 0xa9, 0x61, 0x4d, 0xc6, 0x2a, 0xd9, 0x96, 0x57, 0x57, 0xc6,
100 	0xc0, 0x99, 0xc0, 0x94, 0x57, 0x21, 0x0a, 0xad, 0xde, 0x4c, 0xe1, 0xaa,
101 	0x6e, 0x11, 0xc6, 0xeb, 0xa2, 0x09, 0x65, 0xd9, 0xd5, 0xc6, 0x8f, 0x8d,
102 	0xb6, 0xec, 0xb3, 0xca, 0x29, 0xa4, 0x9f, 0xab, 0x90, 0x7e, 0xf2, 0x86,
103 	0x92, 0x25, 0x71, 0xf4, 0x17, 0x70, 0x92, 0xe0, 0xd0, 0xe2, 0xe1, 0x57,
104 	0xc4, 0xca, 0x44, 0xbb, 0x14, 0xdc, 0xf8, 0x1a, 0x27, 0x00, 0x8f, 0x30,
105 	0xcd, 0x0a, 0x4a, 0x26, 0x42, 0x07, 0xa1, 0x1e, 0x69, 0x7e, 0xfd, 0xc4,
106 	0x38, 0xf8, 0x53, 0xfe, 0xcd, 0xa7, 0xfc, 0x7d, 0xa1, 0x87, 0xe8, 0xec,
107 	0x52, 0x01, 0x37, 0x43, 0x0c, 0xf2, 0x2e, 0x89, 0x3d, 0xaa, 0xed, 0x9d,
108 	0x83, 0x13, 0x8f, 0x79, 0xec, 0xb3, 0x7f, 0xbe, 0xf9, 0x36, 0x99, 0x8d,
109 	0x93, 0xc2, 0x4d, 0x8f, 0x8f, 0x18, 0x1e, 0x94, 0x25, 0xe7, 0x0b, 0xdc,
110 	0xd9, 0x85, 0x88, 0x77, 0x7e, 0x25, 0x80, 0x10, 0xde, 0xb6, 0xdd, 0xe6,
111 	0xb6, 0xba, 0xe9, 0x1d, 0xac, 0xd2, 0xd7, 0x1c, 0x8e, 0x86, 0x5e, 0xd1,
112 	0xc0, 0x97, 0x59, 0x79, 0x7e, 0x1a, 0x96, 0x6a, 0xa1, 0x09, 0xc0, 0x45,
113 	0xb0, 0x71, 0x2d, 0xd1, 0x87, 0x6f, 0xbe, 0x31, 0x21, 0xcb, 0x52, 0xde,
114 	0x48, 0x41, 0x73, 0x7e, 0x4a, 0x2f, 0xf8, 0x95, 0x0c, 0x2d, 0xb4, 0x9d,
115 	0x53, 0x70, 0xcd, 0xa5,
116 };
117 
118 static u8 rsa2048_e[] = {
119 	0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
120 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
121 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
122 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
124 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
125 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
133 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
137 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
138 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
139 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
140 	0x00, 0x00, 0x00, 0x00,
141 };
142 
143 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
144 static u8 rsa2048_c[] = {
145 	0xa5, 0x0d, 0xc2, 0xc6, 0xf1, 0x08, 0x95, 0x55, 0x1d, 0xb3, 0xf9, 0x43,
146 	0xb0, 0x7b, 0x5b, 0x96, 0xa0, 0x72, 0xe8, 0xd6, 0x95, 0xd6, 0x98, 0xec,
147 	0x53, 0x6b, 0x85, 0x77, 0x74, 0x21, 0xf1, 0x22, 0x5c, 0x4d, 0x56, 0x55,
148 	0x89, 0x0d, 0x60, 0xa5, 0x2b, 0x68, 0xf8, 0x71, 0x6d, 0x72, 0xed, 0x67,
149 	0x55, 0x3e, 0x5f, 0xd1, 0x9e, 0x21, 0xa7, 0x6e, 0x5c, 0xf2, 0xac, 0xe8,
150 	0x66, 0x1d, 0x4e, 0xe5, 0x58, 0x76, 0xed, 0x2b, 0xfe, 0x2b, 0xc9, 0xf6,
151 	0x7f, 0xb7, 0x02, 0x27, 0xfd, 0x25, 0x3f, 0x2d, 0x7c, 0x1c, 0x37, 0x80,
152 	0x29, 0x7f, 0x12, 0xd2, 0xd5, 0xae, 0x27, 0xc6, 0x68, 0x24, 0x83, 0xc5,
153 	0x61, 0x8b, 0xea, 0x1a, 0xbd, 0x07, 0x96, 0xe4, 0xd1, 0xc5, 0xd4, 0x21,
154 	0x39, 0x4a, 0xaa, 0x66, 0xd2, 0x3f, 0xd2, 0x67, 0x55, 0x39, 0xbf, 0x40,
155 	0x66, 0x66, 0x5a, 0x92, 0xdd, 0x8d, 0xda, 0x5a, 0x89, 0x7e, 0x78, 0xd9,
156 	0x5f, 0x42, 0xe4, 0x16, 0xdb, 0x76, 0x49, 0xe0, 0x71, 0x98, 0x8e, 0xed,
157 	0x61, 0xd9, 0x63, 0x0a, 0x11, 0xf6, 0x24, 0x7f, 0x9b, 0xf2, 0xee, 0xfc,
158 	0xa4, 0x4c, 0x98, 0x81, 0xfb, 0x2d, 0xd3, 0x8f, 0x29, 0x77, 0x63, 0xd9,
159 	0x51, 0xfc, 0xe1, 0x17, 0x3f, 0xc3, 0xce, 0xca, 0xbe, 0xec, 0xf2, 0xc9,
160 	0x07, 0x48, 0x74, 0x54, 0xbb, 0x1d, 0x02, 0xe7, 0xaf, 0x18, 0x27, 0x39,
161 	0xb8, 0xc4, 0x90, 0x05, 0x20, 0x5b, 0x38, 0x7b, 0x3a, 0x15, 0x94, 0x7e,
162 	0xe8, 0xc1, 0xa5, 0x8c, 0xc0, 0x6c, 0x86, 0x96, 0xeb, 0x46, 0xfa, 0xad,
163 	0x2e, 0x69, 0x12, 0x56, 0xb9, 0x51, 0x91, 0x9c, 0xea, 0x83, 0x4b, 0xd3,
164 	0x5f, 0xb7, 0xdd, 0x29, 0x99, 0xb9, 0x66, 0x65, 0x5e, 0xe2, 0x59, 0x16,
165 	0x20, 0xa2, 0xda, 0x0e, 0x8e, 0xd4, 0x41, 0xdc, 0x1e, 0x75, 0x32, 0xf8,
166 	0x73, 0xfa, 0x22, 0x7b,
167 };
168 #endif
169 
170 /* RSA2048-SHA256 sign data of foo_data[] */
171 static u8 rsa2048_sha256_sign[] = {
172 	0xd4, 0x05, 0x21, 0x34, 0x2c, 0x92, 0xaf, 0x73, 0x72, 0x13, 0x54, 0x92,
173 	0x9b, 0x57, 0x63, 0x82, 0xd4, 0x7c, 0x41, 0xba, 0x58, 0x3e, 0xa6, 0x82,
174 	0xe8, 0x56, 0x9f, 0x1c, 0xc2, 0x6d, 0x07, 0x6d, 0xe0, 0xca, 0x4c, 0x93,
175 	0xb4, 0xa4, 0x82, 0x53, 0x41, 0x58, 0xeb, 0x77, 0xd5, 0x2c, 0xf5, 0x3f,
176 	0x3d, 0x1b, 0x54, 0x1b, 0x6a, 0x18, 0x30, 0xc1, 0x93, 0x14, 0x8f, 0xdd,
177 	0x77, 0xe4, 0xed, 0xb8, 0xd3, 0xfc, 0x63, 0xed, 0x88, 0x2f, 0xf4, 0x35,
178 	0xc4, 0xdb, 0x89, 0x94, 0x4a, 0xcd, 0x88, 0x88, 0x4f, 0xc3, 0xeb, 0x14,
179 	0x03, 0xd9, 0x85, 0x2e, 0x7a, 0x27, 0xbd, 0xc9, 0x19, 0xbf, 0x55, 0x50,
180 	0x2a, 0x29, 0x23, 0x02, 0xe4, 0xaf, 0x16, 0x5c, 0x17, 0x81, 0x38, 0x79,
181 	0xf1, 0x2b, 0x9a, 0xc7, 0xd2, 0x9d, 0x66, 0xca, 0xf7, 0x6b, 0xf0, 0x83,
182 	0x81, 0x57, 0xfc, 0x56, 0xae, 0x6d, 0x98, 0xae, 0xe2, 0xec, 0x00, 0x19,
183 	0x6b, 0x75, 0x06, 0xdc, 0x1c, 0x6e, 0xbd, 0x23, 0x15, 0xb1, 0x3a, 0x43,
184 	0x7e, 0x3c, 0x8b, 0x32, 0x9c, 0x4d, 0xa2, 0xcf, 0x14, 0xee, 0xd8, 0x7a,
185 	0x12, 0x90, 0x70, 0xd2, 0x9f, 0xd5, 0x6d, 0x50, 0x67, 0x35, 0x93, 0x97,
186 	0x2e, 0xff, 0x8f, 0xbf, 0x34, 0x4c, 0xa9, 0xee, 0x6a, 0xaa, 0x87, 0x09,
187 	0x1f, 0x6b, 0xcc, 0x3d, 0xeb, 0x88, 0xca, 0x8f, 0x45, 0x86, 0xb7, 0xb2,
188 	0xd6, 0xd8, 0xe5, 0x3e, 0x94, 0x02, 0x4b, 0xa7, 0x74, 0xb8, 0x5f, 0x1f,
189 	0xe3, 0xbc, 0x95, 0x72, 0x3b, 0x51, 0xd4, 0xec, 0x8d, 0x87, 0xca, 0xb0,
190 	0x69, 0xca, 0xa9, 0x68, 0xfc, 0x34, 0xf9, 0xb5, 0x67, 0xef, 0xac, 0xc2,
191 	0x10, 0x1a, 0x39, 0x56, 0xcb, 0x38, 0x45, 0x92, 0x74, 0x2e, 0x24, 0x1c,
192 	0x4e, 0x31, 0x80, 0xd9, 0x2d, 0x5f, 0x74, 0x48, 0xfe, 0x46, 0x4e, 0xf6,
193 	0x19, 0x9a, 0x1d, 0x32,
194 };
195 
196 static void dump_hash(const char *title, void *hard_d, void *soft_d, u32 nbits)
197 {
198 	int i, same;
199 	char *buf;
200 
201 	same = !memcmp(hard_d, soft_d, BITS2BYTE(nbits));
202 	printf("[%s]: %s\n", title, same ? "OK" : "FAIL");
203 
204 	printf("    hard: ");
205 	buf = hard_d;
206 	for (i = 0; i < BITS2BYTE(nbits); i++)
207 		printf("%02x ", buf[i]);
208 
209 	printf("\n    soft: ");
210 	buf = soft_d;
211 	for (i = 0; i < BITS2BYTE(nbits); i++)
212 		printf("%02x ", buf[i]);
213 
214 	printf("\n\n");
215 }
216 
217 static void dump_hex(const char *name, const u8 *array, u32 len)
218 {
219 	int i;
220 
221 	printf("[%s]: %uByte", name, len);
222 	for (i = 0; i < len; i++) {
223 		if (i % 32 == 0)
224 			printf("\n");
225 		printf("%02x ", array[i]);
226 	}
227 	printf("\n");
228 }
229 
230 static int hash_perf_eval(struct udevice *dev, u32 algo, char *algo_name)
231 {
232 	sha_context ctx;
233 	u32 data_size = 8 * 1024;
234 	u8 *data = NULL;
235 	u8 hash_out[64];
236 	int ret;
237 
238 	ctx.algo = algo;
239 	ctx.length = 100 * 1024 * 1024;
240 
241 	data = (u8 *)memalign(CONFIG_SYS_CACHELINE_SIZE, data_size);
242 	if (!data) {
243 		printf("%s, %d: memalign %u error!\n",
244 		       __func__, __LINE__, data_size);
245 		return -EINVAL;
246 	}
247 
248 	memset(data, 0xab, data_size);
249 
250 	ulong start = get_timer(0);
251 
252 	ret = crypto_sha_init(dev, &ctx);
253 	if (ret)
254 		goto exit;
255 
256 	for (u32 i = 0; i < ctx.length / data_size; i++) {
257 		ret = crypto_sha_update(dev, (u32 *)data, data_size);
258 		if (ret)
259 			goto exit;
260 	}
261 
262 	ret = crypto_sha_final(dev, &ctx, hash_out);
263 
264 	ulong time_cost = get_timer(start);
265 
266 	printf("%s, hash performance = %luMBps\n",
267 	       algo_name, (100 * 1000) / time_cost);
268 exit:
269 	free(data);
270 
271 	return ret;
272 }
273 
274 static int do_crypto(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
275 {
276 	struct udevice *dev;
277 	sha_context csha_ctx;
278 	sha1_context sha1_ctx;
279 	sha256_context sha256_ctx;
280 	sha512_context sha512_ctx;
281 	rsa_key rsa_key;
282 	u8 hard_out[256];
283 	u8 soft_out[64];
284 	u32 cap;
285 
286 	/* CRYPTO_V1 TODO: SHA512 is not available */
287 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
288 	cap = CRYPTO_MD5 | CRYPTO_SHA1 | CRYPTO_SHA256 |
289 	      CRYPTO_RSA2048;
290 #else
291 	cap = CRYPTO_MD5 | CRYPTO_SHA1 | CRYPTO_SHA256 |
292 #if !defined(CONFIG_ROCKCHIP_RK1808)
293 	      CRYPTO_SHA512 |
294 #endif
295 	      CRYPTO_RSA2048 | CRYPTO_TRNG;
296 #endif
297 	dev = crypto_get_device(cap);
298 	if (!dev) {
299 		printf("Can't find crypto device for expected capability\n");
300 		return -ENODEV;
301 	}
302 
303 	/* MD5 */
304 	if (cap & CRYPTO_MD5) {
305 		HASH_PERF_EVAL(dev, CRYPTO_MD5);
306 		csha_ctx.algo = CRYPTO_MD5;
307 		csha_ctx.length = sizeof(foo_data);
308 		memset(hard_out, 0x00, sizeof(hard_out));
309 		crypto_sha_csum(dev, &csha_ctx, (char *)foo_data,
310 				sizeof(foo_data), hard_out);
311 		md5(foo_data, sizeof(foo_data), soft_out);
312 		dump_hash("MD5", hard_out, soft_out,
313 			  crypto_algo_nbits(csha_ctx.algo));
314 	}
315 
316 	/* SHA1 */
317 	if (cap & CRYPTO_SHA1) {
318 		HASH_PERF_EVAL(dev, CRYPTO_SHA1);
319 		csha_ctx.algo = CRYPTO_SHA1;
320 		csha_ctx.length = sizeof(foo_data);
321 		memset(hard_out, 0x00, sizeof(hard_out));
322 		crypto_sha_csum(dev, &csha_ctx, (char *)foo_data,
323 				sizeof(foo_data), hard_out);
324 		sha1_starts(&sha1_ctx);
325 		sha1_update(&sha1_ctx, (const u8 *)foo_data, sizeof(foo_data));
326 		sha1_finish(&sha1_ctx, soft_out);
327 		dump_hash("SHA1", hard_out, soft_out,
328 			  crypto_algo_nbits(csha_ctx.algo));
329 	}
330 
331 	/* SHA512 */
332 	if (cap & CRYPTO_SHA512) {
333 		HASH_PERF_EVAL(dev, CRYPTO_SHA512);
334 		csha_ctx.algo = CRYPTO_SHA512;
335 		csha_ctx.length = sizeof(foo_data);
336 		memset(hard_out, 0x00, sizeof(hard_out));
337 		crypto_sha_csum(dev, &csha_ctx, (char *)foo_data,
338 				sizeof(foo_data), hard_out);
339 		sha512_starts(&sha512_ctx);
340 		sha512_update(&sha512_ctx, (const u8 *)foo_data,
341 			      sizeof(foo_data));
342 		sha512_finish(&sha512_ctx, soft_out);
343 		dump_hash("SHA512", hard_out, soft_out,
344 			  crypto_algo_nbits(csha_ctx.algo));
345 	}
346 
347 	/* SHA256 */
348 	if (cap & CRYPTO_SHA256) {
349 		HASH_PERF_EVAL(dev, CRYPTO_SHA256);
350 		csha_ctx.algo = CRYPTO_SHA256;
351 		csha_ctx.length = sizeof(foo_data);
352 		memset(hard_out, 0x00, sizeof(hard_out));
353 		crypto_sha_csum(dev, &csha_ctx, (char *)foo_data,
354 				sizeof(foo_data), hard_out);
355 		sha256_starts(&sha256_ctx);
356 		sha256_update(&sha256_ctx, (const u8 *)foo_data,
357 			      sizeof(foo_data));
358 		sha256_finish(&sha256_ctx, soft_out);
359 		dump_hash("SHA256", hard_out, soft_out,
360 			  crypto_algo_nbits(csha_ctx.algo));
361 	}
362 
363 	/* RSA2048-SHA256 */
364 	if (cap & CRYPTO_RSA2048) {
365 		memset(&rsa_key, 0x00, sizeof(rsa_key));
366 		rsa_key.algo = CRYPTO_RSA2048;
367 		rsa_key.n = (u32 *)&rsa2048_n;
368 		rsa_key.e = (u32 *)&rsa2048_e;
369 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
370 		rsa_key.c = (u32 *)&rsa2048_c;
371 #endif
372 		memset(hard_out, 0x00, sizeof(hard_out));
373 		crypto_rsa_verify(dev, &rsa_key, rsa2048_sha256_sign, hard_out);
374 		sha256_starts(&sha256_ctx);
375 		sha256_update(&sha256_ctx, (const u8 *)foo_data,
376 			      sizeof(foo_data));
377 		sha256_finish(&sha256_ctx, soft_out);
378 		dump_hash("RSA2048-SHA256", hard_out,
379 			  soft_out, crypto_algo_nbits(csha_ctx.algo));
380 	}
381 
382 	/* TRNG */
383 	if (cap & CRYPTO_TRNG) {
384 		memset(hard_out, 0x00, sizeof(hard_out));
385 		crypto_get_trng(dev, hard_out, sizeof(hard_out));
386 		dump_hex("TRNG", hard_out, sizeof(hard_out));
387 	}
388 
389 	return 0;
390 }
391 
392 U_BOOT_CMD(
393 	crypto, 1, 1, do_crypto,
394 	"crypto test",
395 	""
396 );
397