/src/wolfssl-disable-fastmath/wolfcrypt/src/cryptocb.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* cryptocb.c |
2 | | * |
3 | | * Copyright (C) 2006-2022 wolfSSL Inc. |
4 | | * |
5 | | * This file is part of wolfSSL. |
6 | | * |
7 | | * wolfSSL is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the GNU General Public License as published by |
9 | | * the Free Software Foundation; either version 2 of the License, or |
10 | | * (at your option) any later version. |
11 | | * |
12 | | * wolfSSL is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU General Public License |
18 | | * along with this program; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
20 | | */ |
21 | | |
22 | | /* This framework provides a central place for crypto hardware integration |
23 | | using the devId scheme. If not supported return `CRYPTOCB_UNAVAILABLE`. */ |
24 | | |
25 | | #ifdef HAVE_CONFIG_H |
26 | | #include <config.h> |
27 | | #endif |
28 | | |
29 | | #include <wolfssl/wolfcrypt/settings.h> |
30 | | |
31 | | #ifdef WOLF_CRYPTO_CB |
32 | | |
33 | | #include <wolfssl/wolfcrypt/cryptocb.h> |
34 | | #include <wolfssl/wolfcrypt/error-crypt.h> |
35 | | #include <wolfssl/wolfcrypt/logging.h> |
36 | | |
37 | | |
38 | | /* TODO: Consider linked list with mutex */ |
39 | | #ifndef MAX_CRYPTO_DEVID_CALLBACKS |
40 | 25.9k | #define MAX_CRYPTO_DEVID_CALLBACKS 8 |
41 | | #endif |
42 | | |
43 | | typedef struct CryptoCb { |
44 | | int devId; |
45 | | CryptoDevCallbackFunc cb; |
46 | | void* ctx; |
47 | | } CryptoCb; |
48 | | static WOLFSSL_GLOBAL CryptoCb gCryptoDev[MAX_CRYPTO_DEVID_CALLBACKS]; |
49 | | |
50 | | |
51 | | #ifdef DEBUG_CRYPTOCB |
52 | | static const char* GetAlgoTypeStr(int algo) |
53 | | { |
54 | | switch (algo) { /* enum wc_AlgoType */ |
55 | | case WC_ALGO_TYPE_HASH: return "Hash"; |
56 | | case WC_ALGO_TYPE_CIPHER: return "Cipher"; |
57 | | case WC_ALGO_TYPE_PK: return "PK"; |
58 | | case WC_ALGO_TYPE_RNG: return "RNG"; |
59 | | case WC_ALGO_TYPE_SEED: return "Seed"; |
60 | | case WC_ALGO_TYPE_HMAC: return "HMAC"; |
61 | | } |
62 | | return NULL; |
63 | | } |
64 | | static const char* GetPkTypeStr(int pk) |
65 | | { |
66 | | switch (pk) { |
67 | | case WC_PK_TYPE_RSA: return "RSA"; |
68 | | case WC_PK_TYPE_DH: return "DH"; |
69 | | case WC_PK_TYPE_ECDH: return "ECDH"; |
70 | | case WC_PK_TYPE_ECDSA_SIGN: return "ECDSA-Sign"; |
71 | | case WC_PK_TYPE_ECDSA_VERIFY: return "ECDSA-Verify"; |
72 | | case WC_PK_TYPE_ED25519_SIGN: return "ED25519-Sign"; |
73 | | case WC_PK_TYPE_ED25519_VERIFY: return "ED25519-Verify"; |
74 | | case WC_PK_TYPE_CURVE25519: return "CURVE25519"; |
75 | | case WC_PK_TYPE_RSA_KEYGEN: return "RSA KeyGen"; |
76 | | case WC_PK_TYPE_EC_KEYGEN: return "ECC KeyGen"; |
77 | | } |
78 | | return NULL; |
79 | | } |
80 | | static const char* GetCipherTypeStr(int cipher) |
81 | | { |
82 | | switch (cipher) { |
83 | | case WC_CIPHER_AES: return "AES ECB"; |
84 | | case WC_CIPHER_AES_CBC: return "AES CBC"; |
85 | | case WC_CIPHER_AES_GCM: return "AES GCM"; |
86 | | case WC_CIPHER_AES_CTR: return "AES CTR"; |
87 | | case WC_CIPHER_AES_XTS: return "AES XTS"; |
88 | | case WC_CIPHER_AES_CFB: return "AES CFB"; |
89 | | case WC_CIPHER_DES3: return "DES3"; |
90 | | case WC_CIPHER_DES: return "DES"; |
91 | | case WC_CIPHER_CHACHA: return "ChaCha20"; |
92 | | } |
93 | | return NULL; |
94 | | } |
95 | | static const char* GetHashTypeStr(int hash) |
96 | | { |
97 | | switch (hash) { |
98 | | case WC_HASH_TYPE_MD2: return "MD2"; |
99 | | case WC_HASH_TYPE_MD4: return "MD4"; |
100 | | case WC_HASH_TYPE_MD5: return "MD5"; |
101 | | case WC_HASH_TYPE_SHA: return "SHA-1"; |
102 | | case WC_HASH_TYPE_SHA224: return "SHA-224"; |
103 | | case WC_HASH_TYPE_SHA256: return "SHA-256"; |
104 | | case WC_HASH_TYPE_SHA384: return "SHA-384"; |
105 | | case WC_HASH_TYPE_SHA512: return "SHA-512"; |
106 | | case WC_HASH_TYPE_MD5_SHA: return "MD5-SHA1"; |
107 | | case WC_HASH_TYPE_SHA3_224: return "SHA3-224"; |
108 | | case WC_HASH_TYPE_SHA3_256: return "SHA3-256"; |
109 | | case WC_HASH_TYPE_SHA3_384: return "SHA3-384"; |
110 | | case WC_HASH_TYPE_SHA3_512: return "SHA3-512"; |
111 | | case WC_HASH_TYPE_BLAKE2B: return "Blake2B"; |
112 | | case WC_HASH_TYPE_BLAKE2S: return "Blake2S"; |
113 | | } |
114 | | return NULL; |
115 | | } |
116 | | |
117 | | #ifndef NO_RSA |
118 | | static const char* GetRsaType(int type) |
119 | | { |
120 | | switch (type) { |
121 | | case RSA_PUBLIC_ENCRYPT: return "Public Encrypt"; |
122 | | case RSA_PUBLIC_DECRYPT: return "Public Decrypt"; |
123 | | case RSA_PRIVATE_ENCRYPT: return "Private Encrypt"; |
124 | | case RSA_PRIVATE_DECRYPT: return "Private Decrypt"; |
125 | | } |
126 | | return NULL; |
127 | | } |
128 | | #endif |
129 | | |
130 | | WOLFSSL_API void wc_CryptoCb_InfoString(wc_CryptoInfo* info) |
131 | | { |
132 | | if (info == NULL) |
133 | | return; |
134 | | |
135 | | if (info->algo_type == WC_ALGO_TYPE_PK) { |
136 | | #ifndef NO_RSA |
137 | | if (info->pk.type == WC_PK_TYPE_RSA) { |
138 | | printf("Crypto CB: %s %s (%d), %s, Len %d\n", |
139 | | GetAlgoTypeStr(info->algo_type), |
140 | | GetPkTypeStr(info->pk.type), info->pk.type, |
141 | | GetRsaType(info->pk.rsa.type), info->pk.rsa.inLen); |
142 | | } |
143 | | else |
144 | | #endif |
145 | | { |
146 | | printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type), |
147 | | GetPkTypeStr(info->pk.type), info->pk.type); |
148 | | } |
149 | | } |
150 | | else if (info->algo_type == WC_ALGO_TYPE_CIPHER) { |
151 | | printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type), |
152 | | GetCipherTypeStr(info->cipher.type), info->cipher.type); |
153 | | } |
154 | | else if (info->algo_type == WC_ALGO_TYPE_HASH) { |
155 | | printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type), |
156 | | GetHashTypeStr(info->hash.type), info->hash.type); |
157 | | } |
158 | | else if (info->algo_type == WC_ALGO_TYPE_HMAC) { |
159 | | printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type), |
160 | | GetHashTypeStr(info->hmac.macType), info->hmac.macType); |
161 | | } |
162 | | else { |
163 | | printf("CryptoCb: %s \n", GetAlgoTypeStr(info->algo_type)); |
164 | | } |
165 | | } |
166 | | #endif /* DEBUG_CRYPTOCB */ |
167 | | |
168 | | static CryptoCb* wc_CryptoCb_FindDevice(int devId) |
169 | 25.9k | { |
170 | 25.9k | int i; |
171 | 25.9k | for (i=0; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
172 | 25.9k | if (gCryptoDev[i].devId == devId) |
173 | 25.9k | return &gCryptoDev[i]; |
174 | 25.9k | } |
175 | 2 | return NULL; |
176 | 25.9k | } |
177 | | static CryptoCb* wc_CryptoCb_FindDeviceByIndex(int startIdx) |
178 | 0 | { |
179 | 0 | int i; |
180 | 0 | for (i=startIdx; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
181 | 0 | if (gCryptoDev[i].devId != INVALID_DEVID) |
182 | 0 | return &gCryptoDev[i]; |
183 | 0 | } |
184 | 0 | return NULL; |
185 | 0 | } |
186 | | |
187 | | static WC_INLINE int wc_CryptoCb_TranslateErrorCode(int ret) |
188 | 25.9k | { |
189 | 25.9k | if (ret == NOT_COMPILED_IN) { |
190 | | /* backwards compatibility for older NOT_COMPILED_IN syntax */ |
191 | 36 | ret = CRYPTOCB_UNAVAILABLE; |
192 | 36 | } |
193 | 25.9k | return ret; |
194 | 25.9k | } |
195 | | |
196 | | void wc_CryptoCb_Init(void) |
197 | 2 | { |
198 | 2 | int i; |
199 | 18 | for (i=0; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
200 | 16 | gCryptoDev[i].devId = INVALID_DEVID; |
201 | 16 | } |
202 | 2 | } |
203 | | |
204 | | int wc_CryptoCb_GetDevIdAtIndex(int startIdx) |
205 | 0 | { |
206 | 0 | int devId = INVALID_DEVID; |
207 | 0 | CryptoCb* dev = wc_CryptoCb_FindDeviceByIndex(startIdx); |
208 | 0 | if (dev) { |
209 | 0 | devId = dev->devId; |
210 | 0 | } |
211 | 0 | return devId; |
212 | 0 | } |
213 | | |
214 | | int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx) |
215 | 2 | { |
216 | | /* find existing or new */ |
217 | 2 | CryptoCb* dev = wc_CryptoCb_FindDevice(devId); |
218 | 2 | if (dev == NULL) |
219 | 2 | dev = wc_CryptoCb_FindDevice(INVALID_DEVID); |
220 | | |
221 | 2 | if (dev == NULL) |
222 | 0 | return BUFFER_E; /* out of devices */ |
223 | | |
224 | 2 | dev->devId = devId; |
225 | 2 | dev->cb = cb; |
226 | 2 | dev->ctx = ctx; |
227 | | |
228 | 2 | return 0; |
229 | 2 | } |
230 | | |
231 | | void wc_CryptoCb_UnRegisterDevice(int devId) |
232 | 0 | { |
233 | 0 | CryptoCb* dev = wc_CryptoCb_FindDevice(devId); |
234 | 0 | if (dev) { |
235 | 0 | XMEMSET(dev, 0, sizeof(*dev)); |
236 | 0 | dev->devId = INVALID_DEVID; |
237 | 0 | } |
238 | 0 | } |
239 | | |
240 | | #ifndef NO_RSA |
241 | | int wc_CryptoCb_Rsa(const byte* in, word32 inLen, byte* out, |
242 | | word32* outLen, int type, RsaKey* key, WC_RNG* rng) |
243 | 0 | { |
244 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
245 | 0 | CryptoCb* dev; |
246 | |
|
247 | 0 | if (key == NULL) |
248 | 0 | return ret; |
249 | | |
250 | | /* locate registered callback */ |
251 | 0 | dev = wc_CryptoCb_FindDevice(key->devId); |
252 | 0 | if (dev && dev->cb) { |
253 | 0 | wc_CryptoInfo cryptoInfo; |
254 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
255 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
256 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_RSA; |
257 | 0 | cryptoInfo.pk.rsa.in = in; |
258 | 0 | cryptoInfo.pk.rsa.inLen = inLen; |
259 | 0 | cryptoInfo.pk.rsa.out = out; |
260 | 0 | cryptoInfo.pk.rsa.outLen = outLen; |
261 | 0 | cryptoInfo.pk.rsa.type = type; |
262 | 0 | cryptoInfo.pk.rsa.key = key; |
263 | 0 | cryptoInfo.pk.rsa.rng = rng; |
264 | |
|
265 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
266 | 0 | } |
267 | |
|
268 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
269 | 0 | } |
270 | | |
271 | | #ifdef WOLFSSL_KEY_GEN |
272 | | int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng) |
273 | 0 | { |
274 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
275 | 0 | CryptoCb* dev; |
276 | |
|
277 | 0 | if (key == NULL) |
278 | 0 | return ret; |
279 | | |
280 | | /* locate registered callback */ |
281 | 0 | dev = wc_CryptoCb_FindDevice(key->devId); |
282 | 0 | if (dev && dev->cb) { |
283 | 0 | wc_CryptoInfo cryptoInfo; |
284 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
285 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
286 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_RSA_KEYGEN; |
287 | 0 | cryptoInfo.pk.rsakg.key = key; |
288 | 0 | cryptoInfo.pk.rsakg.size = size; |
289 | 0 | cryptoInfo.pk.rsakg.e = e; |
290 | 0 | cryptoInfo.pk.rsakg.rng = rng; |
291 | |
|
292 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
293 | 0 | } |
294 | |
|
295 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
296 | 0 | } |
297 | | #endif |
298 | | |
299 | | int wc_CryptoCb_RsaCheckPrivKey(RsaKey* key, const byte* pubKey, |
300 | | word32 pubKeySz) |
301 | 0 | { |
302 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
303 | 0 | CryptoCb* dev; |
304 | |
|
305 | 0 | if (key == NULL) |
306 | 0 | return ret; |
307 | | |
308 | | /* locate registered callback */ |
309 | 0 | dev = wc_CryptoCb_FindDevice(key->devId); |
310 | 0 | if (dev && dev->cb) { |
311 | 0 | wc_CryptoInfo cryptoInfo; |
312 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
313 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
314 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_RSA_CHECK_PRIV_KEY; |
315 | 0 | cryptoInfo.pk.rsa_check.key = key; |
316 | 0 | cryptoInfo.pk.rsa_check.pubKey = pubKey; |
317 | 0 | cryptoInfo.pk.rsa_check.pubKeySz = pubKeySz; |
318 | |
|
319 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
320 | 0 | } |
321 | |
|
322 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
323 | 0 | } |
324 | | #endif /* !NO_RSA */ |
325 | | |
326 | | #ifdef HAVE_ECC |
327 | | int wc_CryptoCb_MakeEccKey(WC_RNG* rng, int keySize, ecc_key* key, int curveId) |
328 | 0 | { |
329 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
330 | 0 | CryptoCb* dev; |
331 | |
|
332 | 0 | if (key == NULL) |
333 | 0 | return ret; |
334 | | |
335 | | /* locate registered callback */ |
336 | 0 | dev = wc_CryptoCb_FindDevice(key->devId); |
337 | 0 | if (dev && dev->cb) { |
338 | 0 | wc_CryptoInfo cryptoInfo; |
339 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
340 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
341 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_KEYGEN; |
342 | 0 | cryptoInfo.pk.eckg.rng = rng; |
343 | 0 | cryptoInfo.pk.eckg.size = keySize; |
344 | 0 | cryptoInfo.pk.eckg.key = key; |
345 | 0 | cryptoInfo.pk.eckg.curveId = curveId; |
346 | |
|
347 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
348 | 0 | } |
349 | |
|
350 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
351 | 0 | } |
352 | | |
353 | | int wc_CryptoCb_Ecdh(ecc_key* private_key, ecc_key* public_key, |
354 | | byte* out, word32* outlen) |
355 | 0 | { |
356 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
357 | 0 | CryptoCb* dev; |
358 | |
|
359 | 0 | if (private_key == NULL) |
360 | 0 | return ret; |
361 | | |
362 | | /* locate registered callback */ |
363 | 0 | dev = wc_CryptoCb_FindDevice(private_key->devId); |
364 | 0 | if (dev && dev->cb) { |
365 | 0 | wc_CryptoInfo cryptoInfo; |
366 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
367 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
368 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ECDH; |
369 | 0 | cryptoInfo.pk.ecdh.private_key = private_key; |
370 | 0 | cryptoInfo.pk.ecdh.public_key = public_key; |
371 | 0 | cryptoInfo.pk.ecdh.out = out; |
372 | 0 | cryptoInfo.pk.ecdh.outlen = outlen; |
373 | |
|
374 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
375 | 0 | } |
376 | |
|
377 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
378 | 0 | } |
379 | | |
380 | | int wc_CryptoCb_EccSign(const byte* in, word32 inlen, byte* out, |
381 | | word32 *outlen, WC_RNG* rng, ecc_key* key) |
382 | 0 | { |
383 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
384 | 0 | CryptoCb* dev; |
385 | |
|
386 | 0 | if (key == NULL) |
387 | 0 | return ret; |
388 | | |
389 | | /* locate registered callback */ |
390 | 0 | dev = wc_CryptoCb_FindDevice(key->devId); |
391 | 0 | if (dev && dev->cb) { |
392 | 0 | wc_CryptoInfo cryptoInfo; |
393 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
394 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
395 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_SIGN; |
396 | 0 | cryptoInfo.pk.eccsign.in = in; |
397 | 0 | cryptoInfo.pk.eccsign.inlen = inlen; |
398 | 0 | cryptoInfo.pk.eccsign.out = out; |
399 | 0 | cryptoInfo.pk.eccsign.outlen = outlen; |
400 | 0 | cryptoInfo.pk.eccsign.rng = rng; |
401 | 0 | cryptoInfo.pk.eccsign.key = key; |
402 | |
|
403 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
404 | 0 | } |
405 | |
|
406 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
407 | 0 | } |
408 | | |
409 | | int wc_CryptoCb_EccVerify(const byte* sig, word32 siglen, |
410 | | const byte* hash, word32 hashlen, int* res, ecc_key* key) |
411 | 0 | { |
412 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
413 | 0 | CryptoCb* dev; |
414 | |
|
415 | 0 | if (key == NULL) |
416 | 0 | return ret; |
417 | | |
418 | | /* locate registered callback */ |
419 | 0 | dev = wc_CryptoCb_FindDevice(key->devId); |
420 | 0 | if (dev && dev->cb) { |
421 | 0 | wc_CryptoInfo cryptoInfo; |
422 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
423 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
424 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_VERIFY; |
425 | 0 | cryptoInfo.pk.eccverify.sig = sig; |
426 | 0 | cryptoInfo.pk.eccverify.siglen = siglen; |
427 | 0 | cryptoInfo.pk.eccverify.hash = hash; |
428 | 0 | cryptoInfo.pk.eccverify.hashlen = hashlen; |
429 | 0 | cryptoInfo.pk.eccverify.res = res; |
430 | 0 | cryptoInfo.pk.eccverify.key = key; |
431 | |
|
432 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
433 | 0 | } |
434 | |
|
435 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
436 | 0 | } |
437 | | |
438 | | int wc_CryptoCb_EccCheckPrivKey(ecc_key* key, const byte* pubKey, |
439 | | word32 pubKeySz) |
440 | 0 | { |
441 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
442 | 0 | CryptoCb* dev; |
443 | |
|
444 | 0 | if (key == NULL) |
445 | 0 | return ret; |
446 | | |
447 | | /* locate registered callback */ |
448 | 0 | dev = wc_CryptoCb_FindDevice(key->devId); |
449 | 0 | if (dev && dev->cb) { |
450 | 0 | wc_CryptoInfo cryptoInfo; |
451 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
452 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
453 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_CHECK_PRIV_KEY; |
454 | 0 | cryptoInfo.pk.ecc_check.key = key; |
455 | 0 | cryptoInfo.pk.ecc_check.pubKey = pubKey; |
456 | 0 | cryptoInfo.pk.ecc_check.pubKeySz = pubKeySz; |
457 | |
|
458 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
459 | 0 | } |
460 | |
|
461 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
462 | 0 | } |
463 | | #endif /* HAVE_ECC */ |
464 | | |
465 | | #ifdef HAVE_CURVE25519 |
466 | | int wc_CryptoCb_Curve25519Gen(WC_RNG* rng, int keySize, |
467 | | curve25519_key* key) |
468 | 0 | { |
469 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
470 | 0 | CryptoCb* dev; |
471 | |
|
472 | 0 | if (key == NULL) |
473 | 0 | return ret; |
474 | | |
475 | | /* locate registered callback */ |
476 | 0 | dev = wc_CryptoCb_FindDevice(key->devId); |
477 | 0 | if (dev && dev->cb) { |
478 | 0 | wc_CryptoInfo cryptoInfo; |
479 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
480 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
481 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519_KEYGEN; |
482 | 0 | cryptoInfo.pk.curve25519kg.rng = rng; |
483 | 0 | cryptoInfo.pk.curve25519kg.size = keySize; |
484 | 0 | cryptoInfo.pk.curve25519kg.key = key; |
485 | |
|
486 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
487 | 0 | } |
488 | |
|
489 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
490 | 0 | } |
491 | | |
492 | | int wc_CryptoCb_Curve25519(curve25519_key* private_key, |
493 | | curve25519_key* public_key, byte* out, word32* outlen, int endian) |
494 | 0 | { |
495 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
496 | 0 | CryptoCb* dev; |
497 | |
|
498 | 0 | if (private_key == NULL) |
499 | 0 | return ret; |
500 | | |
501 | | /* locate registered callback */ |
502 | 0 | dev = wc_CryptoCb_FindDevice(private_key->devId); |
503 | 0 | if (dev && dev->cb) { |
504 | 0 | wc_CryptoInfo cryptoInfo; |
505 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
506 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
507 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519; |
508 | 0 | cryptoInfo.pk.curve25519.private_key = private_key; |
509 | 0 | cryptoInfo.pk.curve25519.public_key = public_key; |
510 | 0 | cryptoInfo.pk.curve25519.out = out; |
511 | 0 | cryptoInfo.pk.curve25519.outlen = outlen; |
512 | 0 | cryptoInfo.pk.curve25519.endian = endian; |
513 | |
|
514 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
515 | 0 | } |
516 | |
|
517 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
518 | 0 | } |
519 | | #endif /* HAVE_CURVE25519 */ |
520 | | |
521 | | #ifdef HAVE_ED25519 |
522 | | int wc_CryptoCb_Ed25519Gen(WC_RNG* rng, int keySize, |
523 | | ed25519_key* key) |
524 | 0 | { |
525 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
526 | 0 | CryptoCb* dev; |
527 | |
|
528 | 0 | if (key == NULL) |
529 | 0 | return ret; |
530 | | |
531 | | /* locate registered callback */ |
532 | 0 | dev = wc_CryptoCb_FindDevice(key->devId); |
533 | 0 | if (dev && dev->cb) { |
534 | 0 | wc_CryptoInfo cryptoInfo; |
535 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
536 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
537 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED25519_KEYGEN; |
538 | 0 | cryptoInfo.pk.ed25519kg.rng = rng; |
539 | 0 | cryptoInfo.pk.ed25519kg.size = keySize; |
540 | 0 | cryptoInfo.pk.ed25519kg.key = key; |
541 | |
|
542 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
543 | 0 | } |
544 | |
|
545 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
546 | 0 | } |
547 | | |
548 | | int wc_CryptoCb_Ed25519Sign(const byte* in, word32 inLen, byte* out, |
549 | | word32 *outLen, ed25519_key* key, byte type, |
550 | | const byte* context, byte contextLen) |
551 | 0 | { |
552 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
553 | 0 | CryptoCb* dev; |
554 | |
|
555 | 0 | if (key == NULL) |
556 | 0 | return ret; |
557 | | |
558 | | /* locate registered callback */ |
559 | 0 | dev = wc_CryptoCb_FindDevice(key->devId); |
560 | 0 | if (dev && dev->cb) { |
561 | 0 | wc_CryptoInfo cryptoInfo; |
562 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
563 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
564 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED25519_SIGN; |
565 | 0 | cryptoInfo.pk.ed25519sign.in = in; |
566 | 0 | cryptoInfo.pk.ed25519sign.inLen = inLen; |
567 | 0 | cryptoInfo.pk.ed25519sign.out = out; |
568 | 0 | cryptoInfo.pk.ed25519sign.outLen = outLen; |
569 | 0 | cryptoInfo.pk.ed25519sign.key = key; |
570 | 0 | cryptoInfo.pk.ed25519sign.type = type; |
571 | 0 | cryptoInfo.pk.ed25519sign.context = context; |
572 | 0 | cryptoInfo.pk.ed25519sign.contextLen = contextLen; |
573 | |
|
574 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
575 | 0 | } |
576 | |
|
577 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
578 | 0 | } |
579 | | |
580 | | int wc_CryptoCb_Ed25519Verify(const byte* sig, word32 sigLen, |
581 | | const byte* msg, word32 msgLen, int* res, ed25519_key* key, byte type, |
582 | | const byte* context, byte contextLen) |
583 | 0 | { |
584 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
585 | 0 | CryptoCb* dev; |
586 | |
|
587 | 0 | if (key == NULL) |
588 | 0 | return ret; |
589 | | |
590 | | /* locate registered callback */ |
591 | 0 | dev = wc_CryptoCb_FindDevice(key->devId); |
592 | 0 | if (dev && dev->cb) { |
593 | 0 | wc_CryptoInfo cryptoInfo; |
594 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
595 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
596 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED25519_VERIFY; |
597 | 0 | cryptoInfo.pk.ed25519verify.sig = sig; |
598 | 0 | cryptoInfo.pk.ed25519verify.sigLen = sigLen; |
599 | 0 | cryptoInfo.pk.ed25519verify.msg = msg; |
600 | 0 | cryptoInfo.pk.ed25519verify.msgLen = msgLen; |
601 | 0 | cryptoInfo.pk.ed25519verify.res = res; |
602 | 0 | cryptoInfo.pk.ed25519verify.key = key; |
603 | 0 | cryptoInfo.pk.ed25519verify.type = type; |
604 | 0 | cryptoInfo.pk.ed25519verify.context = context; |
605 | 0 | cryptoInfo.pk.ed25519verify.contextLen = contextLen; |
606 | |
|
607 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
608 | 0 | } |
609 | |
|
610 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
611 | 0 | } |
612 | | #endif /* HAVE_ED25519 */ |
613 | | |
614 | | #ifndef NO_AES |
615 | | #ifdef HAVE_AESGCM |
616 | | int wc_CryptoCb_AesGcmEncrypt(Aes* aes, byte* out, |
617 | | const byte* in, word32 sz, |
618 | | const byte* iv, word32 ivSz, |
619 | | byte* authTag, word32 authTagSz, |
620 | | const byte* authIn, word32 authInSz) |
621 | 0 | { |
622 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
623 | 0 | CryptoCb* dev; |
624 | | |
625 | | /* locate registered callback */ |
626 | 0 | if (aes) { |
627 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId); |
628 | 0 | } |
629 | 0 | else { |
630 | | /* locate first callback and try using it */ |
631 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
632 | 0 | } |
633 | |
|
634 | 0 | if (dev && dev->cb) { |
635 | 0 | wc_CryptoInfo cryptoInfo; |
636 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
637 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
638 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_GCM; |
639 | 0 | cryptoInfo.cipher.enc = 1; |
640 | 0 | cryptoInfo.cipher.aesgcm_enc.aes = aes; |
641 | 0 | cryptoInfo.cipher.aesgcm_enc.out = out; |
642 | 0 | cryptoInfo.cipher.aesgcm_enc.in = in; |
643 | 0 | cryptoInfo.cipher.aesgcm_enc.sz = sz; |
644 | 0 | cryptoInfo.cipher.aesgcm_enc.iv = iv; |
645 | 0 | cryptoInfo.cipher.aesgcm_enc.ivSz = ivSz; |
646 | 0 | cryptoInfo.cipher.aesgcm_enc.authTag = authTag; |
647 | 0 | cryptoInfo.cipher.aesgcm_enc.authTagSz = authTagSz; |
648 | 0 | cryptoInfo.cipher.aesgcm_enc.authIn = authIn; |
649 | 0 | cryptoInfo.cipher.aesgcm_enc.authInSz = authInSz; |
650 | |
|
651 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
652 | 0 | } |
653 | |
|
654 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
655 | 0 | } |
656 | | |
657 | | int wc_CryptoCb_AesGcmDecrypt(Aes* aes, byte* out, |
658 | | const byte* in, word32 sz, |
659 | | const byte* iv, word32 ivSz, |
660 | | const byte* authTag, word32 authTagSz, |
661 | | const byte* authIn, word32 authInSz) |
662 | 0 | { |
663 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
664 | 0 | CryptoCb* dev; |
665 | | |
666 | | /* locate registered callback */ |
667 | 0 | if (aes) { |
668 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId); |
669 | 0 | } |
670 | 0 | else { |
671 | | /* locate first callback and try using it */ |
672 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
673 | 0 | } |
674 | |
|
675 | 0 | if (dev && dev->cb) { |
676 | 0 | wc_CryptoInfo cryptoInfo; |
677 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
678 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
679 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_GCM; |
680 | 0 | cryptoInfo.cipher.enc = 0; |
681 | 0 | cryptoInfo.cipher.aesgcm_dec.aes = aes; |
682 | 0 | cryptoInfo.cipher.aesgcm_dec.out = out; |
683 | 0 | cryptoInfo.cipher.aesgcm_dec.in = in; |
684 | 0 | cryptoInfo.cipher.aesgcm_dec.sz = sz; |
685 | 0 | cryptoInfo.cipher.aesgcm_dec.iv = iv; |
686 | 0 | cryptoInfo.cipher.aesgcm_dec.ivSz = ivSz; |
687 | 0 | cryptoInfo.cipher.aesgcm_dec.authTag = authTag; |
688 | 0 | cryptoInfo.cipher.aesgcm_dec.authTagSz = authTagSz; |
689 | 0 | cryptoInfo.cipher.aesgcm_dec.authIn = authIn; |
690 | 0 | cryptoInfo.cipher.aesgcm_dec.authInSz = authInSz; |
691 | |
|
692 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
693 | 0 | } |
694 | |
|
695 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
696 | 0 | } |
697 | | #endif /* HAVE_AESGCM */ |
698 | | |
699 | | #ifdef HAVE_AESCCM |
700 | | int wc_CryptoCb_AesCcmEncrypt(Aes* aes, byte* out, |
701 | | const byte* in, word32 sz, |
702 | | const byte* nonce, word32 nonceSz, |
703 | | byte* authTag, word32 authTagSz, |
704 | | const byte* authIn, word32 authInSz) |
705 | 0 | { |
706 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
707 | 0 | CryptoCb* dev; |
708 | | |
709 | | /* locate registered callback */ |
710 | 0 | if (aes) { |
711 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId); |
712 | 0 | } |
713 | 0 | else { |
714 | | /* locate first callback and try using it */ |
715 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
716 | 0 | } |
717 | |
|
718 | 0 | if (dev && dev->cb) { |
719 | 0 | wc_CryptoInfo cryptoInfo; |
720 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
721 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
722 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CCM; |
723 | 0 | cryptoInfo.cipher.enc = 1; |
724 | 0 | cryptoInfo.cipher.aesccm_enc.aes = aes; |
725 | 0 | cryptoInfo.cipher.aesccm_enc.out = out; |
726 | 0 | cryptoInfo.cipher.aesccm_enc.in = in; |
727 | 0 | cryptoInfo.cipher.aesccm_enc.sz = sz; |
728 | 0 | cryptoInfo.cipher.aesccm_enc.nonce = nonce; |
729 | 0 | cryptoInfo.cipher.aesccm_enc.nonceSz = nonceSz; |
730 | 0 | cryptoInfo.cipher.aesccm_enc.authTag = authTag; |
731 | 0 | cryptoInfo.cipher.aesccm_enc.authTagSz = authTagSz; |
732 | 0 | cryptoInfo.cipher.aesccm_enc.authIn = authIn; |
733 | 0 | cryptoInfo.cipher.aesccm_enc.authInSz = authInSz; |
734 | |
|
735 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
736 | 0 | } |
737 | |
|
738 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
739 | 0 | } |
740 | | |
741 | | int wc_CryptoCb_AesCcmDecrypt(Aes* aes, byte* out, |
742 | | const byte* in, word32 sz, |
743 | | const byte* nonce, word32 nonceSz, |
744 | | const byte* authTag, word32 authTagSz, |
745 | | const byte* authIn, word32 authInSz) |
746 | 0 | { |
747 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
748 | 0 | CryptoCb* dev; |
749 | | |
750 | | /* locate registered callback */ |
751 | 0 | if (aes) { |
752 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId); |
753 | 0 | } |
754 | 0 | else { |
755 | | /* locate first callback and try using it */ |
756 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
757 | 0 | } |
758 | |
|
759 | 0 | if (dev && dev->cb) { |
760 | 0 | wc_CryptoInfo cryptoInfo; |
761 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
762 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
763 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CCM; |
764 | 0 | cryptoInfo.cipher.enc = 0; |
765 | 0 | cryptoInfo.cipher.aesccm_dec.aes = aes; |
766 | 0 | cryptoInfo.cipher.aesccm_dec.out = out; |
767 | 0 | cryptoInfo.cipher.aesccm_dec.in = in; |
768 | 0 | cryptoInfo.cipher.aesccm_dec.sz = sz; |
769 | 0 | cryptoInfo.cipher.aesccm_dec.nonce = nonce; |
770 | 0 | cryptoInfo.cipher.aesccm_dec.nonceSz = nonceSz; |
771 | 0 | cryptoInfo.cipher.aesccm_dec.authTag = authTag; |
772 | 0 | cryptoInfo.cipher.aesccm_dec.authTagSz = authTagSz; |
773 | 0 | cryptoInfo.cipher.aesccm_dec.authIn = authIn; |
774 | 0 | cryptoInfo.cipher.aesccm_dec.authInSz = authInSz; |
775 | |
|
776 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
777 | 0 | } |
778 | |
|
779 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
780 | 0 | } |
781 | | #endif /* HAVE_AESCCM */ |
782 | | |
783 | | #ifdef HAVE_AES_CBC |
784 | | int wc_CryptoCb_AesCbcEncrypt(Aes* aes, byte* out, |
785 | | const byte* in, word32 sz) |
786 | 0 | { |
787 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
788 | 0 | CryptoCb* dev; |
789 | | |
790 | | /* locate registered callback */ |
791 | 0 | if (aes) { |
792 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId); |
793 | 0 | } |
794 | 0 | else { |
795 | | /* locate first callback and try using it */ |
796 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
797 | 0 | } |
798 | |
|
799 | 0 | if (dev && dev->cb) { |
800 | 0 | wc_CryptoInfo cryptoInfo; |
801 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
802 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
803 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CBC; |
804 | 0 | cryptoInfo.cipher.enc = 1; |
805 | 0 | cryptoInfo.cipher.aescbc.aes = aes; |
806 | 0 | cryptoInfo.cipher.aescbc.out = out; |
807 | 0 | cryptoInfo.cipher.aescbc.in = in; |
808 | 0 | cryptoInfo.cipher.aescbc.sz = sz; |
809 | |
|
810 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
811 | 0 | } |
812 | |
|
813 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
814 | 0 | } |
815 | | |
816 | | int wc_CryptoCb_AesCbcDecrypt(Aes* aes, byte* out, |
817 | | const byte* in, word32 sz) |
818 | 0 | { |
819 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
820 | 0 | CryptoCb* dev; |
821 | | |
822 | | /* locate registered callback */ |
823 | 0 | if (aes) { |
824 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId); |
825 | 0 | } |
826 | 0 | else { |
827 | | /* locate first callback and try using it */ |
828 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
829 | 0 | } |
830 | |
|
831 | 0 | if (dev && dev->cb) { |
832 | 0 | wc_CryptoInfo cryptoInfo; |
833 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
834 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
835 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CBC; |
836 | 0 | cryptoInfo.cipher.enc = 0; |
837 | 0 | cryptoInfo.cipher.aescbc.aes = aes; |
838 | 0 | cryptoInfo.cipher.aescbc.out = out; |
839 | 0 | cryptoInfo.cipher.aescbc.in = in; |
840 | 0 | cryptoInfo.cipher.aescbc.sz = sz; |
841 | |
|
842 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
843 | 0 | } |
844 | |
|
845 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
846 | 0 | } |
847 | | #endif /* HAVE_AES_CBC */ |
848 | | #ifdef WOLFSSL_AES_COUNTER |
849 | | int wc_CryptoCb_AesCtrEncrypt(Aes* aes, byte* out, |
850 | | const byte* in, word32 sz) |
851 | 0 | { |
852 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
853 | 0 | CryptoCb* dev; |
854 | | |
855 | | /* locate registered callback */ |
856 | 0 | if (aes) { |
857 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId); |
858 | 0 | } |
859 | 0 | else { |
860 | | /* locate first callback and try using it */ |
861 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
862 | 0 | } |
863 | |
|
864 | 0 | if (dev && dev->cb) { |
865 | 0 | wc_CryptoInfo cryptoInfo; |
866 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
867 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
868 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CTR; |
869 | 0 | cryptoInfo.cipher.enc = 1; |
870 | 0 | cryptoInfo.cipher.aesctr.aes = aes; |
871 | 0 | cryptoInfo.cipher.aesctr.out = out; |
872 | 0 | cryptoInfo.cipher.aesctr.in = in; |
873 | 0 | cryptoInfo.cipher.aesctr.sz = sz; |
874 | |
|
875 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
876 | 0 | } |
877 | |
|
878 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
879 | 0 | } |
880 | | #endif /* WOLFSSL_AES_COUNTER */ |
881 | | #ifdef HAVE_AES_ECB |
882 | | int wc_CryptoCb_AesEcbEncrypt(Aes* aes, byte* out, |
883 | | const byte* in, word32 sz) |
884 | 0 | { |
885 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
886 | 0 | CryptoCb* dev; |
887 | | |
888 | | /* locate registered callback */ |
889 | 0 | if (aes) { |
890 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId); |
891 | 0 | } |
892 | 0 | else { |
893 | | /* locate first callback and try using it */ |
894 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
895 | 0 | } |
896 | |
|
897 | 0 | if (dev && dev->cb) { |
898 | 0 | wc_CryptoInfo cryptoInfo; |
899 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
900 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
901 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_ECB; |
902 | 0 | cryptoInfo.cipher.enc = 1; |
903 | 0 | cryptoInfo.cipher.aesecb.aes = aes; |
904 | 0 | cryptoInfo.cipher.aesecb.out = out; |
905 | 0 | cryptoInfo.cipher.aesecb.in = in; |
906 | 0 | cryptoInfo.cipher.aesecb.sz = sz; |
907 | |
|
908 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
909 | 0 | } |
910 | |
|
911 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
912 | 0 | } |
913 | | |
914 | | int wc_CryptoCb_AesEcbDecrypt(Aes* aes, byte* out, |
915 | | const byte* in, word32 sz) |
916 | 0 | { |
917 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
918 | 0 | CryptoCb* dev; |
919 | | |
920 | | /* locate registered callback */ |
921 | 0 | if (aes) { |
922 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId); |
923 | 0 | } |
924 | 0 | else { |
925 | | /* locate first callback and try using it */ |
926 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
927 | 0 | } |
928 | |
|
929 | 0 | if (dev && dev->cb) { |
930 | 0 | wc_CryptoInfo cryptoInfo; |
931 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
932 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
933 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_ECB; |
934 | 0 | cryptoInfo.cipher.enc = 0; |
935 | 0 | cryptoInfo.cipher.aesecb.aes = aes; |
936 | 0 | cryptoInfo.cipher.aesecb.out = out; |
937 | 0 | cryptoInfo.cipher.aesecb.in = in; |
938 | 0 | cryptoInfo.cipher.aesecb.sz = sz; |
939 | |
|
940 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
941 | 0 | } |
942 | |
|
943 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
944 | 0 | } |
945 | | #endif /* HAVE_AES_ECB */ |
946 | | #endif /* !NO_AES */ |
947 | | |
948 | | #ifndef NO_DES3 |
949 | | int wc_CryptoCb_Des3Encrypt(Des3* des3, byte* out, |
950 | | const byte* in, word32 sz) |
951 | 0 | { |
952 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
953 | 0 | CryptoCb* dev; |
954 | | |
955 | | /* locate registered callback */ |
956 | 0 | if (des3) { |
957 | 0 | dev = wc_CryptoCb_FindDevice(des3->devId); |
958 | 0 | } |
959 | 0 | else { |
960 | | /* locate first callback and try using it */ |
961 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
962 | 0 | } |
963 | |
|
964 | 0 | if (dev && dev->cb) { |
965 | 0 | wc_CryptoInfo cryptoInfo; |
966 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
967 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
968 | 0 | cryptoInfo.cipher.type = WC_CIPHER_DES3; |
969 | 0 | cryptoInfo.cipher.enc = 1; |
970 | 0 | cryptoInfo.cipher.des3.des = des3; |
971 | 0 | cryptoInfo.cipher.des3.out = out; |
972 | 0 | cryptoInfo.cipher.des3.in = in; |
973 | 0 | cryptoInfo.cipher.des3.sz = sz; |
974 | |
|
975 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
976 | 0 | } |
977 | |
|
978 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
979 | 0 | } |
980 | | |
981 | | int wc_CryptoCb_Des3Decrypt(Des3* des3, byte* out, |
982 | | const byte* in, word32 sz) |
983 | 0 | { |
984 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
985 | 0 | CryptoCb* dev; |
986 | | |
987 | | /* locate registered callback */ |
988 | 0 | if (des3) { |
989 | 0 | dev = wc_CryptoCb_FindDevice(des3->devId); |
990 | 0 | } |
991 | 0 | else { |
992 | | /* locate first callback and try using it */ |
993 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
994 | 0 | } |
995 | |
|
996 | 0 | if (dev && dev->cb) { |
997 | 0 | wc_CryptoInfo cryptoInfo; |
998 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
999 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
1000 | 0 | cryptoInfo.cipher.type = WC_CIPHER_DES3; |
1001 | 0 | cryptoInfo.cipher.enc = 0; |
1002 | 0 | cryptoInfo.cipher.des3.des = des3; |
1003 | 0 | cryptoInfo.cipher.des3.out = out; |
1004 | 0 | cryptoInfo.cipher.des3.in = in; |
1005 | 0 | cryptoInfo.cipher.des3.sz = sz; |
1006 | |
|
1007 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1008 | 0 | } |
1009 | |
|
1010 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1011 | 0 | } |
1012 | | #endif /* !NO_DES3 */ |
1013 | | |
1014 | | #ifndef NO_SHA |
1015 | | int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in, |
1016 | | word32 inSz, byte* digest) |
1017 | 0 | { |
1018 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
1019 | 0 | CryptoCb* dev; |
1020 | | |
1021 | | /* locate registered callback */ |
1022 | 0 | if (sha) { |
1023 | 0 | dev = wc_CryptoCb_FindDevice(sha->devId); |
1024 | 0 | } |
1025 | 0 | else { |
1026 | | /* locate first callback and try using it */ |
1027 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1028 | 0 | } |
1029 | |
|
1030 | 0 | if (dev && dev->cb) { |
1031 | 0 | wc_CryptoInfo cryptoInfo; |
1032 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1033 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
1034 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA; |
1035 | 0 | cryptoInfo.hash.sha1 = sha; |
1036 | 0 | cryptoInfo.hash.in = in; |
1037 | 0 | cryptoInfo.hash.inSz = inSz; |
1038 | 0 | cryptoInfo.hash.digest = digest; |
1039 | |
|
1040 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1041 | 0 | } |
1042 | |
|
1043 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1044 | 0 | } |
1045 | | #endif /* !NO_SHA */ |
1046 | | |
1047 | | #ifndef NO_SHA256 |
1048 | | int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in, |
1049 | | word32 inSz, byte* digest) |
1050 | 36 | { |
1051 | 36 | int ret = CRYPTOCB_UNAVAILABLE; |
1052 | 36 | CryptoCb* dev; |
1053 | | |
1054 | | /* locate registered callback */ |
1055 | 36 | if (sha256) { |
1056 | 36 | dev = wc_CryptoCb_FindDevice(sha256->devId); |
1057 | 36 | } |
1058 | 0 | else { |
1059 | | /* locate first callback and try using it */ |
1060 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1061 | 0 | } |
1062 | | |
1063 | 36 | if (dev && dev->cb) { |
1064 | 36 | wc_CryptoInfo cryptoInfo; |
1065 | 36 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1066 | 36 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
1067 | 36 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA256; |
1068 | 36 | cryptoInfo.hash.sha256 = sha256; |
1069 | 36 | cryptoInfo.hash.in = in; |
1070 | 36 | cryptoInfo.hash.inSz = inSz; |
1071 | 36 | cryptoInfo.hash.digest = digest; |
1072 | | |
1073 | 36 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1074 | 36 | } |
1075 | | |
1076 | 36 | return wc_CryptoCb_TranslateErrorCode(ret); |
1077 | 36 | } |
1078 | | #endif /* !NO_SHA256 */ |
1079 | | |
1080 | | #ifdef WOLFSSL_SHA384 |
1081 | | int wc_CryptoCb_Sha384Hash(wc_Sha384* sha384, const byte* in, |
1082 | | word32 inSz, byte* digest) |
1083 | 0 | { |
1084 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
1085 | 0 | CryptoCb* dev; |
1086 | | |
1087 | | /* locate registered callback */ |
1088 | 0 | #ifndef NO_SHA2_CRYPTO_CB |
1089 | 0 | if (sha384) { |
1090 | 0 | dev = wc_CryptoCb_FindDevice(sha384->devId); |
1091 | 0 | } |
1092 | 0 | else |
1093 | 0 | #endif |
1094 | 0 | { |
1095 | | /* locate first callback and try using it */ |
1096 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1097 | 0 | } |
1098 | |
|
1099 | 0 | if (dev && dev->cb) { |
1100 | 0 | wc_CryptoInfo cryptoInfo; |
1101 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1102 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
1103 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA384; |
1104 | 0 | cryptoInfo.hash.sha384 = sha384; |
1105 | 0 | cryptoInfo.hash.in = in; |
1106 | 0 | cryptoInfo.hash.inSz = inSz; |
1107 | 0 | cryptoInfo.hash.digest = digest; |
1108 | |
|
1109 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1110 | 0 | } |
1111 | |
|
1112 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1113 | 0 | } |
1114 | | #endif /* WOLFSSL_SHA384 */ |
1115 | | |
1116 | | #ifdef WOLFSSL_SHA512 |
1117 | | int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in, |
1118 | | word32 inSz, byte* digest) |
1119 | 0 | { |
1120 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
1121 | 0 | CryptoCb* dev; |
1122 | | |
1123 | | /* locate registered callback */ |
1124 | 0 | #ifndef NO_SHA2_CRYPTO_CB |
1125 | 0 | if (sha512) { |
1126 | 0 | dev = wc_CryptoCb_FindDevice(sha512->devId); |
1127 | 0 | } |
1128 | 0 | else |
1129 | 0 | #endif |
1130 | 0 | { |
1131 | | /* locate first callback and try using it */ |
1132 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1133 | 0 | } |
1134 | |
|
1135 | 0 | if (dev && dev->cb) { |
1136 | 0 | wc_CryptoInfo cryptoInfo; |
1137 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1138 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
1139 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA512; |
1140 | 0 | cryptoInfo.hash.sha512 = sha512; |
1141 | 0 | cryptoInfo.hash.in = in; |
1142 | 0 | cryptoInfo.hash.inSz = inSz; |
1143 | 0 | cryptoInfo.hash.digest = digest; |
1144 | |
|
1145 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1146 | 0 | } |
1147 | |
|
1148 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1149 | 0 | } |
1150 | | #endif /* WOLFSSL_SHA512 */ |
1151 | | |
1152 | | #ifndef NO_HMAC |
1153 | | int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in, word32 inSz, |
1154 | | byte* digest) |
1155 | 0 | { |
1156 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
1157 | 0 | CryptoCb* dev; |
1158 | |
|
1159 | 0 | if (hmac == NULL) |
1160 | 0 | return ret; |
1161 | | |
1162 | | /* locate registered callback */ |
1163 | 0 | dev = wc_CryptoCb_FindDevice(hmac->devId); |
1164 | 0 | if (dev && dev->cb) { |
1165 | 0 | wc_CryptoInfo cryptoInfo; |
1166 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1167 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HMAC; |
1168 | 0 | cryptoInfo.hmac.macType = macType; |
1169 | 0 | cryptoInfo.hmac.in = in; |
1170 | 0 | cryptoInfo.hmac.inSz = inSz; |
1171 | 0 | cryptoInfo.hmac.digest = digest; |
1172 | 0 | cryptoInfo.hmac.hmac = hmac; |
1173 | |
|
1174 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1175 | 0 | } |
1176 | |
|
1177 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1178 | 0 | } |
1179 | | #endif /* !NO_HMAC */ |
1180 | | |
1181 | | #ifndef WC_NO_RNG |
1182 | | int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz) |
1183 | 25.8k | { |
1184 | 25.8k | int ret = CRYPTOCB_UNAVAILABLE; |
1185 | 25.8k | CryptoCb* dev; |
1186 | | |
1187 | | /* locate registered callback */ |
1188 | 25.8k | if (rng) { |
1189 | 25.8k | dev = wc_CryptoCb_FindDevice(rng->devId); |
1190 | 25.8k | } |
1191 | 0 | else { |
1192 | | /* locate first callback and try using it */ |
1193 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1194 | 0 | } |
1195 | | |
1196 | 25.8k | if (dev && dev->cb) { |
1197 | 25.8k | wc_CryptoInfo cryptoInfo; |
1198 | 25.8k | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1199 | 25.8k | cryptoInfo.algo_type = WC_ALGO_TYPE_RNG; |
1200 | 25.8k | cryptoInfo.rng.rng = rng; |
1201 | 25.8k | cryptoInfo.rng.out = out; |
1202 | 25.8k | cryptoInfo.rng.sz = sz; |
1203 | | |
1204 | 25.8k | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1205 | 25.8k | } |
1206 | | |
1207 | 25.8k | return wc_CryptoCb_TranslateErrorCode(ret); |
1208 | 25.8k | } |
1209 | | |
1210 | | int wc_CryptoCb_RandomSeed(OS_Seed* os, byte* seed, word32 sz) |
1211 | 2 | { |
1212 | 2 | int ret = CRYPTOCB_UNAVAILABLE; |
1213 | 2 | CryptoCb* dev; |
1214 | | |
1215 | | /* locate registered callback */ |
1216 | 2 | dev = wc_CryptoCb_FindDevice(os->devId); |
1217 | 2 | if (dev && dev->cb) { |
1218 | 2 | wc_CryptoInfo cryptoInfo; |
1219 | 2 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1220 | 2 | cryptoInfo.algo_type = WC_ALGO_TYPE_SEED; |
1221 | 2 | cryptoInfo.seed.os = os; |
1222 | 2 | cryptoInfo.seed.seed = seed; |
1223 | 2 | cryptoInfo.seed.sz = sz; |
1224 | | |
1225 | 2 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1226 | 2 | } |
1227 | | |
1228 | 2 | return wc_CryptoCb_TranslateErrorCode(ret); |
1229 | 2 | } |
1230 | | #endif /* !WC_NO_RNG */ |
1231 | | #ifdef WOLFSSL_CMAC |
1232 | | int wc_CryptoCb_Cmac(Cmac* cmac, const byte* key, word32 keySz, |
1233 | | const byte* in, word32 inSz, byte* out, word32* outSz, int type, |
1234 | | void* ctx) |
1235 | 0 | { |
1236 | 0 | int ret = CRYPTOCB_UNAVAILABLE; |
1237 | 0 | CryptoCb* dev; |
1238 | | |
1239 | | /* locate registered callback */ |
1240 | 0 | if (cmac) { |
1241 | 0 | dev = wc_CryptoCb_FindDevice(cmac->devId); |
1242 | 0 | } |
1243 | 0 | else { |
1244 | | /* locate first callback and try using it */ |
1245 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1246 | 0 | } |
1247 | |
|
1248 | 0 | if (dev && dev->cb) { |
1249 | 0 | wc_CryptoInfo cryptoInfo; |
1250 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1251 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CMAC; |
1252 | |
|
1253 | 0 | cryptoInfo.cmac.cmac = cmac; |
1254 | 0 | cryptoInfo.cmac.ctx = ctx; |
1255 | 0 | cryptoInfo.cmac.key = key; |
1256 | 0 | cryptoInfo.cmac.in = in; |
1257 | 0 | cryptoInfo.cmac.out = out; |
1258 | 0 | cryptoInfo.cmac.outSz = outSz; |
1259 | 0 | cryptoInfo.cmac.keySz = keySz; |
1260 | 0 | cryptoInfo.cmac.inSz = inSz; |
1261 | 0 | cryptoInfo.cmac.type = type; |
1262 | |
|
1263 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1264 | 0 | } |
1265 | |
|
1266 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1267 | 0 | } |
1268 | | #endif |
1269 | | |
1270 | | /* returns the default dev id for the current build */ |
1271 | | int wc_CryptoCb_DefaultDevID(void) |
1272 | 233k | { |
1273 | 233k | int ret; |
1274 | | |
1275 | | /* conditional macro selection based on build */ |
1276 | | #ifdef WOLFSSL_CAAM_DEVID |
1277 | | ret = WOLFSSL_CAAM_DEVID; |
1278 | | #else |
1279 | 233k | ret = INVALID_DEVID; |
1280 | 233k | #endif |
1281 | | |
1282 | 233k | return ret; |
1283 | 233k | } |
1284 | | |
1285 | | #endif /* WOLF_CRYPTO_CB */ |