xref: /rk3399_rockchip-uboot/cmd/crypto.c (revision 9c5e2f1dbb99eaf369ad4db5eb753d1bd4ae53d8)
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, 0xd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, 0x00, 0x00, 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, 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e6, 0x6a, 0x46, 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 int hash_perf_eval(struct udevice *dev, u32 algo, char *algo_name)
222 {
223 	sha_context ctx;
224 	u32 data_size = 8 * 1024;
225 	u8 *data = NULL;
226 	u8 hash_out[64];
227 	int ret;
228 
229 	ctx.algo = algo;
230 	ctx.length = 100 * 1024 * 1024;
231 
232 	data = (u8 *)memalign(CONFIG_SYS_CACHELINE_SIZE, data_size);
233 	if (!data) {
234 		printf("%s, %d: memalign %u error!\n",
235 		       __func__, __LINE__, data_size);
236 		return -EINVAL;
237 	}
238 
239 	memset(data, 0xab, data_size);
240 
241 	ulong start = get_timer(0);
242 
243 	ret = crypto_sha_init(dev, &ctx);
244 	if (ret)
245 		goto exit;
246 
247 	for (u32 i = 0; i < ctx.length / data_size; i++) {
248 		ret = crypto_sha_update(dev, (u32 *)data, data_size);
249 		if (ret)
250 			goto exit;
251 	}
252 
253 	ret = crypto_sha_final(dev, &ctx, hash_out);
254 
255 	ulong time_cost = get_timer(start);
256 
257 	printf("%s, hash performance = %luMBps\n",
258 	       algo_name, (100 * 1000) / time_cost);
259 exit:
260 	free(data);
261 
262 	return ret;
263 }
264 
265 static int do_crypto(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
266 {
267 	struct udevice *dev;
268 	sha_context csha_ctx;
269 	sha1_context sha1_ctx;
270 	sha256_context sha256_ctx;
271 	sha512_context sha512_ctx;
272 	u8 hard_out[256];
273 	u8 soft_out[64];
274 	u32 cap;
275 
276 	/* CRYPTO_V1 TODO: SHA512 is not available */
277 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
278 	cap = CRYPTO_MD5 | CRYPTO_SHA1 | CRYPTO_SHA256 |
279 	      CRYPTO_RSA2048;
280 #else
281 	cap = CRYPTO_MD5 | CRYPTO_SHA1 | CRYPTO_SHA256 |
282 #if !defined(CONFIG_ROCKCHIP_RK1808)
283 	      CRYPTO_SHA512 |
284 #endif
285 	      CRYPTO_RSA2048;
286 #endif
287 	dev = crypto_get_device(cap);
288 	if (!dev) {
289 		printf("Can't find crypto device for expected capability\n");
290 		return -ENODEV;
291 	}
292 
293 	/* MD5 */
294 	if (cap & CRYPTO_MD5) {
295 		HASH_PERF_EVAL(dev, CRYPTO_MD5);
296 		csha_ctx.algo = CRYPTO_MD5;
297 		csha_ctx.length = sizeof(foo_data);
298 		memset(hard_out, 0x00, sizeof(hard_out));
299 		crypto_sha_csum(dev, &csha_ctx, (char *)foo_data,
300 				sizeof(foo_data), hard_out);
301 		md5(foo_data, sizeof(foo_data), soft_out);
302 		dump_hash("MD5", hard_out, soft_out,
303 			  crypto_algo_nbits(csha_ctx.algo));
304 	}
305 
306 	/* SHA1 */
307 	if (cap & CRYPTO_SHA1) {
308 		HASH_PERF_EVAL(dev, CRYPTO_SHA1);
309 		csha_ctx.algo = CRYPTO_SHA1;
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 		sha1_starts(&sha1_ctx);
315 		sha1_update(&sha1_ctx, (const u8 *)foo_data, sizeof(foo_data));
316 		sha1_finish(&sha1_ctx, soft_out);
317 		dump_hash("SHA1", hard_out, soft_out,
318 			  crypto_algo_nbits(csha_ctx.algo));
319 	}
320 
321 	/* SHA512 */
322 	if (cap & CRYPTO_SHA512) {
323 		HASH_PERF_EVAL(dev, CRYPTO_SHA512);
324 		csha_ctx.algo = CRYPTO_SHA512;
325 		csha_ctx.length = sizeof(foo_data);
326 		memset(hard_out, 0x00, sizeof(hard_out));
327 		crypto_sha_csum(dev, &csha_ctx, (char *)foo_data,
328 				sizeof(foo_data), hard_out);
329 		sha512_starts(&sha512_ctx);
330 		sha512_update(&sha512_ctx, (const u8 *)foo_data,
331 			      sizeof(foo_data));
332 		sha512_finish(&sha512_ctx, soft_out);
333 		dump_hash("SHA512", hard_out, soft_out,
334 			  crypto_algo_nbits(csha_ctx.algo));
335 	}
336 
337 	/* SHA256 */
338 	if (cap & CRYPTO_SHA256) {
339 		HASH_PERF_EVAL(dev, CRYPTO_SHA256);
340 		csha_ctx.algo = CRYPTO_SHA256;
341 		csha_ctx.length = sizeof(foo_data);
342 		memset(hard_out, 0x00, sizeof(hard_out));
343 		crypto_sha_csum(dev, &csha_ctx, (char *)foo_data,
344 				sizeof(foo_data), hard_out);
345 		sha256_starts(&sha256_ctx);
346 		sha256_update(&sha256_ctx, (const u8 *)foo_data,
347 			      sizeof(foo_data));
348 		sha256_finish(&sha256_ctx, soft_out);
349 		dump_hash("SHA256", hard_out, soft_out,
350 			  crypto_algo_nbits(csha_ctx.algo));
351 	}
352 
353 #if CONFIG_IS_ENABLED(ROCKCHIP_RSA)
354 	/* RSA2048-SHA256 */
355 	if (cap & CRYPTO_RSA2048) {
356 		rsa_key rsa_key;
357 
358 		memset(&rsa_key, 0x00, sizeof(rsa_key));
359 		rsa_key.algo = CRYPTO_RSA2048;
360 		rsa_key.n = (u32 *)&rsa2048_n;
361 		rsa_key.e = (u32 *)&rsa2048_e;
362 #ifdef CONFIG_ROCKCHIP_CRYPTO_V1
363 		rsa_key.c = (u32 *)&rsa2048_c;
364 #endif
365 		memset(hard_out, 0x00, sizeof(hard_out));
366 		crypto_rsa_verify(dev, &rsa_key, rsa2048_sha256_sign, hard_out);
367 		sha256_starts(&sha256_ctx);
368 		sha256_update(&sha256_ctx, (const u8 *)foo_data,
369 			      sizeof(foo_data));
370 		sha256_finish(&sha256_ctx, soft_out);
371 		dump_hash("RSA2048-SHA256", hard_out,
372 			  soft_out, crypto_algo_nbits(csha_ctx.algo));
373 	}
374 #endif
375 
376 	return 0;
377 }
378 
379 U_BOOT_CMD(
380 	crypto, 1, 1, do_crypto,
381 	"crypto test",
382 	""
383 );
384