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