hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
 */
 
#include <crypto.h>
 
static const u8 null_hash_sha1_value[] = {
   0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
   0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
   0xaf, 0xd8, 0x07, 0x09
};
 
static const u8 null_hash_md5_value[] = {
   0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
   0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e
};
 
static const u8 null_hash_sha256_value[] = {
   0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
   0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
   0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
   0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
};
 
static const u8 null_hash_sha512_value[] = {
   0xcf, 0x83, 0xe1, 0x35, 0x7e, 0xef, 0xb8, 0xbd,
   0xf1, 0x54, 0x28, 0x50, 0xd6, 0x6d, 0x80, 0x07,
   0xd6, 0x20, 0xe4, 0x05, 0x0b, 0x57, 0x15, 0xdc,
   0x83, 0xf4, 0xa9, 0x21, 0xd3, 0x6c, 0xe9, 0xce,
   0x47, 0xd0, 0xd1, 0x3c, 0x5d, 0x85, 0xf2, 0xb0,
   0xff, 0x83, 0x18, 0xd2, 0x87, 0x7e, 0xec, 0x2f,
   0x63, 0xb9, 0x31, 0xbd, 0x47, 0x41, 0x7a, 0x81,
   0xa5, 0x38, 0x32, 0x7a, 0xf9, 0x27, 0xda, 0x3e
};
 
const static u8 null_hash_sm3_value[] = {
   0x1a, 0xb2, 0x1d, 0x83, 0x55, 0xcf, 0xa1, 0x7f,
   0x8e, 0x61, 0x19, 0x48, 0x31, 0xe8, 0x1a, 0x8f,
   0x22, 0xbe, 0xc8, 0xc7, 0x28, 0xfe, 0xfb, 0x74,
   0x7e, 0xd0, 0x35, 0xeb, 0x50, 0x82, 0xaa, 0x2b
};
 
u32 crypto_algo_nbits(u32 algo)
{
   switch (algo) {
   case CRYPTO_MD5:
   case CRYPTO_HMAC_MD5:
       return 128;
   case CRYPTO_SHA1:
   case CRYPTO_HMAC_SHA1:
       return 160;
   case CRYPTO_SHA256:
   case CRYPTO_HMAC_SHA256:
       return 256;
   case CRYPTO_SHA512:
   case CRYPTO_HMAC_SHA512:
       return 512;
   case CRYPTO_SM3:
   case CRYPTO_HMAC_SM3:
       return 256;
   case CRYPTO_RSA512:
       return 512;
   case CRYPTO_RSA1024:
       return 1024;
   case CRYPTO_RSA2048:
       return 2048;
   case CRYPTO_RSA3072:
       return 3072;
   case CRYPTO_RSA4096:
       return 4096;
   }
 
   printf("Unknown crypto algorithm: 0x%x\n", algo);
 
   return 0;
}
 
struct udevice *crypto_get_device(u32 capability)
{
   const struct dm_crypto_ops *ops;
   struct udevice *dev;
   struct uclass *uc;
   int ret;
   u32 cap;
 
   ret = uclass_get(UCLASS_CRYPTO, &uc);
   if (ret)
       return NULL;
 
   for (uclass_first_device(UCLASS_CRYPTO, &dev);
        dev;
        uclass_next_device(&dev)) {
       ops = device_get_ops(dev);
       if (!ops || !ops->capability)
           continue;
 
       cap = ops->capability(dev);
       if ((cap & capability) == capability)
           return dev;
   }
 
   return NULL;
}
 
int crypto_sha_init(struct udevice *dev, sha_context *ctx)
{
   const struct dm_crypto_ops *ops = device_get_ops(dev);
 
   if (ctx && !ctx->length)
       return 0;
 
   if (!ops || !ops->sha_init)
       return -ENOSYS;
 
   return ops->sha_init(dev, ctx);
}
 
int crypto_sha_update(struct udevice *dev, u32 *input, u32 len)
{
   const struct dm_crypto_ops *ops = device_get_ops(dev);
 
   if (!len)
       return 0;
 
   if (!ops || !ops->sha_update)
       return -ENOSYS;
 
   return ops->sha_update(dev, input, len);
}
 
int crypto_sha_final(struct udevice *dev, sha_context *ctx, u8 *output)
{
   const struct dm_crypto_ops *ops = device_get_ops(dev);
   const u8 *null_hash = NULL;
   u32 hash_size = 0;
 
   if (ctx && !ctx->length && output) {
       switch (ctx->algo) {
       case CRYPTO_MD5:
           null_hash = null_hash_md5_value;
           hash_size = sizeof(null_hash_md5_value);
           break;
       case CRYPTO_SHA1:
           null_hash = null_hash_sha1_value;
           hash_size = sizeof(null_hash_sha1_value);
           break;
       case CRYPTO_SHA256:
           null_hash = null_hash_sha256_value;
           hash_size = sizeof(null_hash_sha256_value);
           break;
       case CRYPTO_SHA512:
           null_hash = null_hash_sha512_value;
           hash_size = sizeof(null_hash_sha512_value);
           break;
       case CRYPTO_SM3:
           null_hash = null_hash_sm3_value;
           hash_size = sizeof(null_hash_sm3_value);
           break;
       default:
           return -EINVAL;
       }
 
       memcpy(output, null_hash, hash_size);
 
       return 0;
   }
 
   if (!ops || !ops->sha_final)
       return -ENOSYS;
 
   return ops->sha_final(dev, ctx, output);
}
 
int crypto_hmac_init(struct udevice *dev, sha_context *ctx,
            u8 *key, u32 key_len)
{
   const struct dm_crypto_ops *ops = device_get_ops(dev);
 
   if (ctx && !ctx->length)
       return -EINVAL;
 
   if (!ops || !ops->hmac_init)
       return -ENOSYS;
 
   return ops->hmac_init(dev, ctx, key, key_len);
}
 
int crypto_hmac_update(struct udevice *dev, u32 *input, u32 len)
{
   const struct dm_crypto_ops *ops = device_get_ops(dev);
 
   if (!len)
       return 0;
 
   if (!ops || !ops->hmac_update)
       return -ENOSYS;
 
   return ops->hmac_update(dev, input, len);
}
 
int crypto_hmac_final(struct udevice *dev, sha_context *ctx, u8 *output)
{
   const struct dm_crypto_ops *ops = device_get_ops(dev);
 
   if (!ops || !ops->hmac_final)
       return -ENOSYS;
 
   return ops->hmac_final(dev, ctx, output);
}
 
int crypto_sha_csum(struct udevice *dev, sha_context *ctx,
           char *input, u32 input_len, u8 *output)
{
   int ret;
 
   ret = crypto_sha_init(dev, ctx);
   if (ret)
       return ret;
 
   ret = crypto_sha_update(dev, (u32 *)input, input_len);
   if (ret)
       return ret;
 
   ret = crypto_sha_final(dev, ctx, output);
 
   return ret;
}
 
int crypto_sha_regions_csum(struct udevice *dev, sha_context *ctx,
               const struct image_region region[],
               int region_count, u8 *output)
{
   int i, ret;
 
   ctx->length = 0;
   for (i = 0; i < region_count; i++)
       ctx->length += region[i].size;
 
   ret = crypto_sha_init(dev, ctx);
   if (ret)
       return ret;
 
   for (i = 0; i < region_count; i++) {
       ret = crypto_sha_update(dev, (void *)region[i].data,
                   region[i].size);
       if (ret)
           return ret;
   }
 
   return crypto_sha_final(dev, ctx, output);
}
 
int crypto_rsa_verify(struct udevice *dev, rsa_key *ctx, u8 *sign, u8 *output)
{
   const struct dm_crypto_ops *ops = device_get_ops(dev);
 
   if (!ops || !ops->rsa_verify)
       return -ENOSYS;
 
   if (!ctx || !ctx->n || !ctx->e || !sign || !output)
       return -EINVAL;
 
   return ops->rsa_verify(dev, ctx, sign, output);
}
 
int crypto_cipher(struct udevice *dev, cipher_context *ctx,
         const u8 *in, u8 *out, u32 len, bool enc)
{
   const struct dm_crypto_ops *ops = device_get_ops(dev);
 
   if (!ops || !ops->cipher_crypt)
       return -ENOSYS;
 
   return ops->cipher_crypt(dev, ctx, in, out, len, enc);
}
 
int crypto_mac(struct udevice *dev, cipher_context *ctx,
          const u8 *in, u32 len, u8 *tag)
{
   const struct dm_crypto_ops *ops = device_get_ops(dev);
 
   if (!ops || !ops->cipher_mac)
       return -ENOSYS;
 
   return ops->cipher_mac(dev, ctx, in, len, tag);
}
 
int crypto_ae(struct udevice *dev, cipher_context *ctx,
         const u8 *in, u32 len, const u8 *aad, u32 aad_len,
         u8 *out, u8 *tag)
{
   const struct dm_crypto_ops *ops = device_get_ops(dev);
 
   if (!ops || !ops->cipher_ae)
       return -ENOSYS;
 
   return ops->cipher_ae(dev, ctx, in, len, aad, aad_len, out, tag);
}
 
UCLASS_DRIVER(crypto) = {
   .id    = UCLASS_CRYPTO,
   .name    = "crypto",
};