/src/wolfssl-sp-math/wolfcrypt/src/cryptocb.c
Line | Count | Source |
1 | | /* cryptocb.c |
2 | | * |
3 | | * Copyright (C) 2006-2026 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 3 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 | | /* |
26 | | Crypto Callback Build Options: |
27 | | * WOLF_CRYPTO_CB: Master enable for crypto callback default: off |
28 | | * framework. Required for all options below. |
29 | | * WOLF_CRYPTO_CB_FIND: Enable find device callback functions default: off |
30 | | * Allows lookup of registered crypto devices. |
31 | | * WOLF_CRYPTO_CB_CMD: Enable command callbacks invoked during default: off |
32 | | * register and unregister of crypto devices. |
33 | | * WOLF_CRYPTO_CB_COPY: Enable copy callback for algorithm default: off |
34 | | * structures (hash, cipher state copying). |
35 | | * WOLF_CRYPTO_CB_FREE: Enable free callback for algorithm default: off |
36 | | * structures (cleanup of crypto objects). |
37 | | * WOLF_CRYPTO_CB_AES_SETKEY: Enable callback for AES key setup default: off |
38 | | * WOLF_CRYPTO_CB_RSA_PAD: Enable callback for RSA padding default: off |
39 | | * operations (custom padding handling). |
40 | | * DEBUG_CRYPTOCB: Enable debug InfoString functions default: off |
41 | | * |
42 | | * Device ID options: |
43 | | * WC_USE_DEVID: Specify a default device ID to use default: off |
44 | | * when no hardware device is detected. |
45 | | * WC_NO_DEFAULT_DEVID: Disable automatic default device ID default: off |
46 | | * selection. Requires explicit devId passing. |
47 | | * WOLFSSL_CAAM_DEVID: Device ID constant (value 7) for NXP default: off |
48 | | * CAAM hardware crypto. |
49 | | * |
50 | | * Algorithm-specific callback options: |
51 | | * NO_SHA2_CRYPTO_CB: Disable crypto callbacks for SHA-384 default: off |
52 | | * and SHA-512 operations. |
53 | | * WOLF_CRYPTO_CB_NO_SHA512_FALLBACK: default: off |
54 | | * Do not fall back to the generic SHA-512 |
55 | | * callback for SHA-384, SHA-512/224 and |
56 | | * SHA-512/256 when no variant-specific |
57 | | * callback is registered. Required for |
58 | | * backends whose hash context has no |
59 | | * digest[] state field or that keep the |
60 | | * hash state on the device (auto-enabled |
61 | | * for Renesas FSPSM). |
62 | | * WOLF_CRYPTO_CB_ONLY_ECC: Use only callbacks for ECC default: off |
63 | | * WOLF_CRYPTO_CB_ONLY_RSA: Use only callbacks for RSA default: off |
64 | | * WOLF_CRYPTO_CB_ONLY_SHA256: Use only callbacks for SHA-256 default: off |
65 | | * WOLF_CRYPTO_CB_ONLY_SHA512: Use only callbacks for SHA-512 default: off |
66 | | * WOLF_CRYPTO_CB_ONLY_AES: Use only callbacks for AES default: off |
67 | | * WOLF_CRYPTO_CB_ONLY_ED25519: Use only callbacks for Ed25519 default: off |
68 | | * WOLF_CRYPTO_CB_ONLY_CURVE25519: Use only callbacks for X25519 default: off |
69 | | */ |
70 | | |
71 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
72 | | |
73 | | #ifdef WOLF_CRYPTO_CB |
74 | | |
75 | | #include <wolfssl/wolfcrypt/cryptocb.h> |
76 | | |
77 | | #ifdef HAVE_ARIA |
78 | | #include <wolfssl/wolfcrypt/port/aria/aria-cryptocb.h> |
79 | | #endif |
80 | | |
81 | | #ifdef WOLFSSL_CAAM |
82 | | #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h> |
83 | | #endif |
84 | | /* TODO: Consider linked list with mutex */ |
85 | | |
86 | | typedef struct CryptoCb { |
87 | | int devId; |
88 | | CryptoDevCallbackFunc cb; |
89 | | void* ctx; |
90 | | } CryptoCb; |
91 | | static WC_THREADSHARED CryptoCb gCryptoDev[MAX_CRYPTO_DEVID_CALLBACKS]; |
92 | | |
93 | | #ifdef WOLF_CRYPTO_CB_FIND |
94 | | static CryptoDevCallbackFind CryptoCb_FindCb = NULL; |
95 | | #endif |
96 | | |
97 | | #ifdef DEBUG_CRYPTOCB |
98 | | static const char* GetAlgoTypeStr(int algo) |
99 | | { |
100 | | switch (algo) { /* enum wc_AlgoType */ |
101 | | #ifdef WOLF_CRYPTO_CB_CMD |
102 | | case WC_ALGO_TYPE_NONE: return "None-Command"; |
103 | | #endif |
104 | | case WC_ALGO_TYPE_HASH: return "Hash"; |
105 | | case WC_ALGO_TYPE_CIPHER: return "Cipher"; |
106 | | case WC_ALGO_TYPE_PK: return "PK"; |
107 | | case WC_ALGO_TYPE_RNG: return "RNG"; |
108 | | case WC_ALGO_TYPE_SEED: return "Seed"; |
109 | | case WC_ALGO_TYPE_HMAC: return "HMAC"; |
110 | | case WC_ALGO_TYPE_CMAC: return "CMAC"; |
111 | | case WC_ALGO_TYPE_CERT: return "Cert"; |
112 | | case WC_ALGO_TYPE_KDF: return "KDF"; |
113 | | #ifdef WOLF_CRYPTO_CB_COPY |
114 | | case WC_ALGO_TYPE_COPY: return "Copy"; |
115 | | #endif /* WOLF_CRYPTO_CB_COPY */ |
116 | | #ifdef WOLF_CRYPTO_CB_FREE |
117 | | case WC_ALGO_TYPE_FREE: return "Free"; |
118 | | #endif /* WOLF_CRYPTO_CB_FREE */ |
119 | | #ifdef WOLF_CRYPTO_CB_SETKEY |
120 | | case WC_ALGO_TYPE_SETKEY: return "SetKey"; |
121 | | #endif /* WOLF_CRYPTO_CB_SETKEY */ |
122 | | #ifdef WOLF_CRYPTO_CB_EXPORT_KEY |
123 | | case WC_ALGO_TYPE_EXPORT_KEY: return "ExportKey"; |
124 | | #endif /* WOLF_CRYPTO_CB_EXPORT_KEY */ |
125 | | } |
126 | | return NULL; |
127 | | } |
128 | | #ifdef WOLF_CRYPTO_CB_SETKEY |
129 | | static const char* GetSetKeyTypeStr(int type) |
130 | | { |
131 | | switch (type) { |
132 | | case WC_SETKEY_NONE: return "None"; |
133 | | case WC_SETKEY_HMAC: return "HMAC"; |
134 | | case WC_SETKEY_RSA_PUB: return "RSA-Pub"; |
135 | | case WC_SETKEY_RSA_PRIV: return "RSA-Priv"; |
136 | | case WC_SETKEY_ECC_PUB: return "ECC-Pub"; |
137 | | case WC_SETKEY_ECC_PRIV: return "ECC-Priv"; |
138 | | case WC_SETKEY_AES: return "AES"; |
139 | | default: break; |
140 | | } |
141 | | return NULL; |
142 | | } |
143 | | #endif /* WOLF_CRYPTO_CB_SETKEY */ |
144 | | static const char* GetPkTypeStr(int pk) |
145 | | { |
146 | | switch (pk) { |
147 | | case WC_PK_TYPE_RSA: return "RSA"; |
148 | | case WC_PK_TYPE_RSA_PSS_VERIFY: return "RSA-PSS-Verify"; |
149 | | case WC_PK_TYPE_DH: return "DH"; |
150 | | case WC_PK_TYPE_ECDH: return "ECDH"; |
151 | | case WC_PK_TYPE_ECDSA_SIGN: return "ECDSA-Sign"; |
152 | | case WC_PK_TYPE_ECDSA_VERIFY: return "ECDSA-Verify"; |
153 | | case WC_PK_TYPE_ED25519_SIGN: return "ED25519-Sign"; |
154 | | case WC_PK_TYPE_ED25519_VERIFY: return "ED25519-Verify"; |
155 | | case WC_PK_TYPE_ED448: return "ED448-Sign"; |
156 | | case WC_PK_TYPE_ED448_VERIFY: return "ED448-Verify"; |
157 | | case WC_PK_TYPE_CURVE25519: return "CURVE25519"; |
158 | | case WC_PK_TYPE_RSA_KEYGEN: return "RSA KeyGen"; |
159 | | case WC_PK_TYPE_EC_KEYGEN: return "ECC KeyGen"; |
160 | | case WC_PK_TYPE_EC_GET_SIZE: return "ECC GetSize"; |
161 | | case WC_PK_TYPE_EC_GET_SIG_SIZE: return "ECC GetSigSize"; |
162 | | case WC_PK_TYPE_EC_MAKE_PUB: return "ECC MakePub"; |
163 | | case WC_PK_TYPE_EC_CHECK_PUB_KEY: return "ECC CheckPubKey"; |
164 | | case WC_PK_TYPE_ED25519_MAKE_PUB: return "ED25519 MakePub"; |
165 | | case WC_PK_TYPE_ED25519_CHECK_KEY: return "ED25519 CheckKey"; |
166 | | case WC_PK_TYPE_CURVE25519_MAKE_PUB: return "CURVE25519 MakePub"; |
167 | | case WC_PK_TYPE_CURVE25519_GENERIC: return "CURVE25519 Generic"; |
168 | | } |
169 | | return NULL; |
170 | | } |
171 | | #if !defined(NO_AES) || !defined(NO_DES3) |
172 | | static const char* GetCipherTypeStr(int cipher) |
173 | | { |
174 | | switch (cipher) { |
175 | | case WC_CIPHER_AES: return "AES ECB"; |
176 | | case WC_CIPHER_AES_CBC: return "AES CBC"; |
177 | | case WC_CIPHER_AES_GCM: return "AES GCM"; |
178 | | case WC_CIPHER_AES_CTR: return "AES CTR"; |
179 | | case WC_CIPHER_AES_XTS: return "AES XTS"; |
180 | | case WC_CIPHER_AES_CFB: return "AES CFB"; |
181 | | case WC_CIPHER_AES_OFB: return "AES OFB"; |
182 | | case WC_CIPHER_AES_KEYWRAP: return "AES KeyWrap"; |
183 | | case WC_CIPHER_DES3: return "DES3"; |
184 | | case WC_CIPHER_DES: return "DES"; |
185 | | case WC_CIPHER_CHACHA: return "ChaCha20"; |
186 | | } |
187 | | return NULL; |
188 | | } |
189 | | #endif /* !NO_AES || !NO_DES3 */ |
190 | | static const char* GetHashTypeStr(int hash) |
191 | | { |
192 | | switch (hash) { |
193 | | case WC_HASH_TYPE_MD2: return "MD2"; |
194 | | case WC_HASH_TYPE_MD4: return "MD4"; |
195 | | case WC_HASH_TYPE_MD5: return "MD5"; |
196 | | case WC_HASH_TYPE_SHA: return "SHA-1"; |
197 | | case WC_HASH_TYPE_SHA224: return "SHA-224"; |
198 | | case WC_HASH_TYPE_SHA256: return "SHA-256"; |
199 | | case WC_HASH_TYPE_SHA384: return "SHA-384"; |
200 | | case WC_HASH_TYPE_SHA512: return "SHA-512"; |
201 | | case WC_HASH_TYPE_MD5_SHA: return "MD5-SHA1"; |
202 | | case WC_HASH_TYPE_SHA3_224: return "SHA3-224"; |
203 | | case WC_HASH_TYPE_SHA3_256: return "SHA3-256"; |
204 | | case WC_HASH_TYPE_SHA3_384: return "SHA3-384"; |
205 | | case WC_HASH_TYPE_SHA3_512: return "SHA3-512"; |
206 | | case WC_HASH_TYPE_BLAKE2B: return "Blake2B"; |
207 | | case WC_HASH_TYPE_BLAKE2S: return "Blake2S"; |
208 | | } |
209 | | return NULL; |
210 | | } |
211 | | |
212 | | #ifdef WOLFSSL_CMAC |
213 | | static const char* GetCmacTypeStr(int type) |
214 | | { |
215 | | switch (type) { |
216 | | case WC_CMAC_AES: return "AES"; |
217 | | } |
218 | | return NULL; |
219 | | } |
220 | | #endif /* WOLFSSL_CMAC */ |
221 | | |
222 | | #ifndef NO_RSA |
223 | | static const char* GetRsaType(int type) |
224 | | { |
225 | | switch (type) { |
226 | | case RSA_PUBLIC_ENCRYPT: return "Public Encrypt"; |
227 | | case RSA_PUBLIC_DECRYPT: return "Public Decrypt"; |
228 | | case RSA_PRIVATE_ENCRYPT: return "Private Encrypt"; |
229 | | case RSA_PRIVATE_DECRYPT: return "Private Decrypt"; |
230 | | } |
231 | | return NULL; |
232 | | } |
233 | | #endif |
234 | | |
235 | | #ifdef WOLF_CRYPTO_CB_CMD |
236 | | static const char* GetCryptoCbCmdTypeStr(int type) |
237 | | { |
238 | | switch (type) { |
239 | | case WC_CRYPTOCB_CMD_TYPE_REGISTER: return "Register"; |
240 | | case WC_CRYPTOCB_CMD_TYPE_UNREGISTER: return "UnRegister"; |
241 | | } |
242 | | return NULL; |
243 | | } |
244 | | #endif |
245 | | |
246 | | |
247 | | #if (defined(HAVE_HKDF) && !defined(NO_HMAC)) || defined(HAVE_CMAC_KDF) |
248 | | static const char* GetKdfTypeStr(int type) |
249 | | { |
250 | | switch (type) { |
251 | | case WC_KDF_TYPE_HKDF: |
252 | | return "HKDF"; |
253 | | case WC_KDF_TYPE_HKDF_EXTRACT: |
254 | | return "HKDF Extract"; |
255 | | case WC_KDF_TYPE_HKDF_EXPAND: |
256 | | return "HKDF Expand"; |
257 | | case WC_KDF_TYPE_TWOSTEP_CMAC: |
258 | | return "TWOSTEP_CMAC"; |
259 | | } |
260 | | return NULL; |
261 | | } |
262 | | #endif |
263 | | |
264 | | void wc_CryptoCb_InfoString(wc_CryptoInfo* info) |
265 | | { |
266 | | if (info == NULL) |
267 | | return; |
268 | | |
269 | | if (info->algo_type == WC_ALGO_TYPE_PK) { |
270 | | #ifndef NO_RSA |
271 | | if (info->pk.type == WC_PK_TYPE_RSA) { |
272 | | printf("Crypto CB: %s %s (%d), %s, Len %d\n", |
273 | | GetAlgoTypeStr(info->algo_type), |
274 | | GetPkTypeStr(info->pk.type), info->pk.type, |
275 | | GetRsaType(info->pk.rsa.type), info->pk.rsa.inLen); |
276 | | } |
277 | | else |
278 | | #endif |
279 | | { |
280 | | printf("Crypto CB: %s %s (%d)\n", |
281 | | GetAlgoTypeStr(info->algo_type), |
282 | | GetPkTypeStr(info->pk.type), info->pk.type); |
283 | | } |
284 | | } |
285 | | #if !defined(NO_AES) || !defined(NO_DES3) |
286 | | else if (info->algo_type == WC_ALGO_TYPE_CIPHER) { |
287 | | printf("Crypto CB: %s %s (%d) (%p ctx)\n", |
288 | | GetAlgoTypeStr(info->algo_type), |
289 | | GetCipherTypeStr(info->cipher.type), |
290 | | info->cipher.type, (void*)info->cipher.ctx); |
291 | | } |
292 | | #endif /* !NO_AES || !NO_DES3 */ |
293 | | #if !defined(NO_SHA) || !defined(NO_SHA256) || \ |
294 | | defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA3) |
295 | | else if (info->algo_type == WC_ALGO_TYPE_HASH) { |
296 | | printf("Crypto CB: %s %s (%d) (%p ctx) %s\n", |
297 | | GetAlgoTypeStr(info->algo_type), |
298 | | GetHashTypeStr(info->hash.type), |
299 | | info->hash.type, (void*)info->hash.ctx, |
300 | | (info->hash.in != NULL) ? "Update" : "Final"); |
301 | | } |
302 | | #endif |
303 | | #ifndef NO_HMAC |
304 | | else if (info->algo_type == WC_ALGO_TYPE_HMAC) { |
305 | | printf("Crypto CB: %s %s (%d) (%p ctx) %s\n", |
306 | | GetAlgoTypeStr(info->algo_type), |
307 | | GetHashTypeStr(info->hmac.macType), |
308 | | info->hmac.macType, (void*)info->hmac.hmac, |
309 | | (info->hmac.in != NULL) ? "Update" : "Final"); |
310 | | } |
311 | | #endif |
312 | | #ifdef WOLFSSL_CMAC |
313 | | else if (info->algo_type == WC_ALGO_TYPE_CMAC) { |
314 | | printf("Crypto CB: %s %s (%d) (%p ctx) %s %s %s\n", |
315 | | GetAlgoTypeStr(info->algo_type), |
316 | | GetCmacTypeStr(info->cmac.type), |
317 | | info->cmac.type, (void*)info->cmac.cmac, |
318 | | (info->cmac.key != NULL) ? "Init " : "", |
319 | | (info->cmac.in != NULL) ? "Update " : "", |
320 | | (info->cmac.out != NULL) ? "Final" : ""); |
321 | | } |
322 | | #endif |
323 | | #ifdef WOLF_CRYPTO_CB_CMD |
324 | | else if (info->algo_type == WC_ALGO_TYPE_NONE) { |
325 | | printf("Crypto CB: %s %s (%d)\n", |
326 | | GetAlgoTypeStr(info->algo_type), |
327 | | GetCryptoCbCmdTypeStr(info->cmd.type), info->cmd.type); |
328 | | } |
329 | | #endif |
330 | | #ifdef WOLF_CRYPTO_CB_COPY |
331 | | else if (info->algo_type == WC_ALGO_TYPE_COPY) { |
332 | | printf("Crypto CB: %s %s Type=%d\n", |
333 | | GetAlgoTypeStr(info->algo_type), |
334 | | GetAlgoTypeStr(info->copy.algo), info->copy.type); |
335 | | } |
336 | | #endif /* WOLF_CRYPTO_CB_COPY */ |
337 | | #ifdef WOLF_CRYPTO_CB_FREE |
338 | | else if (info->algo_type == WC_ALGO_TYPE_FREE) { |
339 | | printf("Crypto CB: %s %s Type=%d\n", |
340 | | GetAlgoTypeStr(info->algo_type), |
341 | | GetAlgoTypeStr(info->free.algo), info->free.type); |
342 | | } |
343 | | #endif /* WOLF_CRYPTO_CB_FREE */ |
344 | | #ifdef WOLF_CRYPTO_CB_SETKEY |
345 | | else if (info->algo_type == WC_ALGO_TYPE_SETKEY) { |
346 | | printf("Crypto CB: %s %s KeySz=%u\n", |
347 | | GetAlgoTypeStr(info->algo_type), |
348 | | GetSetKeyTypeStr(info->setkey.type), info->setkey.keySz); |
349 | | } |
350 | | #endif /* WOLF_CRYPTO_CB_SETKEY */ |
351 | | #ifdef WOLF_CRYPTO_CB_EXPORT_KEY |
352 | | else if (info->algo_type == WC_ALGO_TYPE_EXPORT_KEY) { |
353 | | printf("Crypto CB: %s Type=%d\n", |
354 | | GetAlgoTypeStr(info->algo_type), info->export_key.type); |
355 | | } |
356 | | #endif /* WOLF_CRYPTO_CB_EXPORT_KEY */ |
357 | | #if (defined(HAVE_HKDF) && !defined(NO_HMAC)) || \ |
358 | | defined(HAVE_CMAC_KDF) |
359 | | else if (info->algo_type == WC_ALGO_TYPE_KDF) { |
360 | | printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type), |
361 | | GetKdfTypeStr(info->kdf.type), info->kdf.type); |
362 | | } |
363 | | #endif |
364 | | else { |
365 | | printf("CryptoCb: %s \n", GetAlgoTypeStr(info->algo_type)); |
366 | | } |
367 | | } |
368 | | #endif /* DEBUG_CRYPTOCB */ |
369 | | |
370 | | /* Search through listed devices and return the first matching device ID |
371 | | * found. */ |
372 | | static CryptoCb* wc_CryptoCb_GetDevice(int devId) |
373 | 87.1k | { |
374 | 87.1k | int i; |
375 | 496k | for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
376 | 445k | if (gCryptoDev[i].devId == devId) |
377 | 36.2k | return &gCryptoDev[i]; |
378 | 445k | } |
379 | 50.9k | return NULL; |
380 | 87.1k | } |
381 | | |
382 | | /* Returns 1 if the given device ID is currently registered, 0 otherwise. |
383 | | * INVALID_DEVID marks free table slots, so it is never reported registered. */ |
384 | | int wc_CryptoCb_IsDeviceRegistered(int devId) |
385 | 0 | { |
386 | 0 | if (devId == INVALID_DEVID) |
387 | 0 | return 0; |
388 | 0 | return wc_CryptoCb_GetDevice(devId) != NULL; |
389 | 0 | } |
390 | | |
391 | | |
392 | | /* Filters through find callback set when trying to get the device, |
393 | | * returns the device found on success and null if not found. */ |
394 | | static CryptoCb* wc_CryptoCb_FindDevice(int devId, int algoType) |
395 | 87.0k | { |
396 | 87.0k | int localDevId = devId; |
397 | | |
398 | | #ifdef WOLF_CRYPTO_CB_FIND |
399 | | if (CryptoCb_FindCb != NULL) { |
400 | | localDevId = CryptoCb_FindCb(devId, algoType); |
401 | | } |
402 | | #endif /* WOLF_CRYPTO_CB_FIND */ |
403 | 87.0k | (void)algoType; |
404 | 87.0k | return wc_CryptoCb_GetDevice(localDevId); |
405 | 87.0k | } |
406 | | |
407 | | |
408 | | static CryptoCb* wc_CryptoCb_FindDeviceByIndex(int startIdx) |
409 | 87.4k | { |
410 | 87.4k | int i; |
411 | 768k | for (i=startIdx; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
412 | 683k | if (gCryptoDev[i].devId != INVALID_DEVID) |
413 | 2.21k | return &gCryptoDev[i]; |
414 | 683k | } |
415 | 85.1k | return NULL; |
416 | 87.4k | } |
417 | | |
418 | | static WC_INLINE int wc_CryptoCb_TranslateErrorCode(int ret) |
419 | 87.0k | { |
420 | 87.0k | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) { |
421 | | /* backwards compatibility for older NOT_COMPILED_IN syntax */ |
422 | 2.62k | ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
423 | 2.62k | } |
424 | 87.0k | return ret; |
425 | 87.0k | } |
426 | | |
427 | | /* Helper function to reset a device entry to invalid */ |
428 | | static WC_INLINE void wc_CryptoCb_ClearDev(CryptoCb *dev) |
429 | 208 | { |
430 | 208 | XMEMSET(dev, 0, sizeof(*dev)); |
431 | 208 | dev->devId = INVALID_DEVID; |
432 | 208 | } |
433 | | |
434 | | void wc_CryptoCb_Init(void) |
435 | 26 | { |
436 | 26 | int i; |
437 | 234 | for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
438 | 208 | wc_CryptoCb_ClearDev(&gCryptoDev[i]); |
439 | 208 | } |
440 | 26 | } |
441 | | |
442 | | void wc_CryptoCb_Cleanup(void) |
443 | 16 | { |
444 | 16 | int i; |
445 | 144 | for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
446 | 128 | if(gCryptoDev[i].devId != INVALID_DEVID) { |
447 | 0 | wc_CryptoCb_UnRegisterDevice(gCryptoDev[i].devId); |
448 | 0 | } |
449 | 128 | } |
450 | 16 | } |
451 | | |
452 | | int wc_CryptoCb_GetDevIdAtIndex(int startIdx) |
453 | 79.4k | { |
454 | 79.4k | int devId = INVALID_DEVID; |
455 | 79.4k | CryptoCb* dev = wc_CryptoCb_FindDeviceByIndex(startIdx); |
456 | 79.4k | if (dev) { |
457 | 179 | devId = dev->devId; |
458 | 179 | } |
459 | 79.4k | return devId; |
460 | 79.4k | } |
461 | | |
462 | | |
463 | | #ifdef WOLF_CRYPTO_CB_FIND |
464 | | /* Used to register a find device function. Useful for cases where the |
465 | | * device ID in the struct may not have been set but still wanting to use |
466 | | * a specific crypto callback device ID. The find callback is global and |
467 | | * not thread safe. */ |
468 | | void wc_CryptoCb_SetDeviceFindCb(CryptoDevCallbackFind cb) |
469 | | { |
470 | | CryptoCb_FindCb = cb; |
471 | | } |
472 | | #endif |
473 | | |
474 | | int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx) |
475 | 10 | { |
476 | 10 | int rc = 0; |
477 | 10 | CryptoCb* dev; |
478 | | |
479 | | /* INVALID_DEVID marks a free slot and cannot be registered as a device. */ |
480 | 10 | if (devId == INVALID_DEVID) |
481 | 0 | return BAD_FUNC_ARG; |
482 | | |
483 | | /* Reject re-registration of an already-registered device ID. */ |
484 | 10 | if (wc_CryptoCb_GetDevice(devId) != NULL) |
485 | 0 | return ALREADY_E; |
486 | | |
487 | | /* find a free slot */ |
488 | 10 | dev = wc_CryptoCb_GetDevice(INVALID_DEVID); |
489 | 10 | if (dev == NULL) |
490 | 0 | return BUFFER_E; /* out of devices */ |
491 | | |
492 | 10 | dev->devId = devId; |
493 | 10 | dev->cb = cb; |
494 | 10 | dev->ctx = ctx; |
495 | | |
496 | | #ifdef WOLF_CRYPTO_CB_CMD |
497 | | if (cb != NULL) { |
498 | | /* Invoke callback with register command */ |
499 | | wc_CryptoInfo info; |
500 | | XMEMSET(&info, 0, sizeof(info)); |
501 | | info.algo_type = WC_ALGO_TYPE_NONE; |
502 | | info.cmd.type = WC_CRYPTOCB_CMD_TYPE_REGISTER; |
503 | | info.cmd.ctx = ctx; /* cb may update on success */ |
504 | | |
505 | | rc = cb(devId, &info, ctx); |
506 | | if (rc == 0) { |
507 | | /* Success. Update dev->ctx */ |
508 | | dev->ctx = info.cmd.ctx; |
509 | | } |
510 | | else if ((rc == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) || |
511 | | (rc == WC_NO_ERR_TRACE(NOT_COMPILED_IN))) { |
512 | | /* Not implemented. Return success*/ |
513 | | rc = 0; |
514 | | } |
515 | | else { |
516 | | /* Error in callback register cmd. Don't register */ |
517 | | wc_CryptoCb_ClearDev(dev); |
518 | | } |
519 | | } |
520 | | #endif |
521 | 10 | return rc; |
522 | 10 | } |
523 | | |
524 | | void wc_CryptoCb_UnRegisterDevice(int devId) |
525 | 0 | { |
526 | 0 | CryptoCb* dev = NULL; |
527 | | |
528 | | /* Can't unregister the invalid device */ |
529 | 0 | if (devId == INVALID_DEVID) |
530 | 0 | return; |
531 | | |
532 | | /* Find the matching dev */ |
533 | 0 | dev = wc_CryptoCb_GetDevice(devId); |
534 | 0 | if (dev == NULL) |
535 | 0 | return; |
536 | | |
537 | | #ifdef WOLF_CRYPTO_CB_CMD |
538 | | if (dev->cb != NULL) { |
539 | | /* Invoke callback with unregister command.*/ |
540 | | wc_CryptoInfo info; |
541 | | XMEMSET(&info, 0, sizeof(info)); |
542 | | info.algo_type = WC_ALGO_TYPE_NONE; |
543 | | info.cmd.type = WC_CRYPTOCB_CMD_TYPE_UNREGISTER; |
544 | | info.cmd.ctx = NULL; /* Not used */ |
545 | | |
546 | | /* Ignore errors here */ |
547 | | dev->cb(devId, &info, dev->ctx); |
548 | | } |
549 | | #endif |
550 | 0 | wc_CryptoCb_ClearDev(dev); |
551 | 0 | } |
552 | | |
553 | | #ifndef NO_RSA |
554 | | int wc_CryptoCb_Rsa(const byte* in, word32 inLen, byte* out, |
555 | | word32* outLen, int type, RsaKey* key, WC_RNG* rng) |
556 | 0 | { |
557 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
558 | 0 | CryptoCb* dev; |
559 | |
|
560 | 0 | if (key == NULL) |
561 | 0 | return ret; |
562 | | |
563 | | /* locate registered callback */ |
564 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
565 | 0 | if (dev && dev->cb) { |
566 | 0 | wc_CryptoInfo cryptoInfo; |
567 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
568 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
569 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_RSA; |
570 | 0 | cryptoInfo.pk.rsa.in = in; |
571 | 0 | cryptoInfo.pk.rsa.inLen = inLen; |
572 | 0 | cryptoInfo.pk.rsa.out = out; |
573 | 0 | cryptoInfo.pk.rsa.outLen = outLen; |
574 | 0 | cryptoInfo.pk.rsa.type = type; |
575 | 0 | cryptoInfo.pk.rsa.key = key; |
576 | 0 | cryptoInfo.pk.rsa.rng = rng; |
577 | |
|
578 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
579 | 0 | } |
580 | |
|
581 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
582 | 0 | } |
583 | | |
584 | | #ifdef WOLF_CRYPTO_CB_RSA_PAD |
585 | | int wc_CryptoCb_RsaPad(const byte* in, word32 inLen, byte* out, |
586 | | word32* outLen, int type, RsaKey* key, WC_RNG* rng, |
587 | | RsaPadding *padding) |
588 | | { |
589 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
590 | | CryptoCb* dev; |
591 | | int pk_type; |
592 | | |
593 | | if (key == NULL) |
594 | | return ret; |
595 | | |
596 | | /* locate registered callback */ |
597 | | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
598 | | |
599 | | if (padding != NULL) { |
600 | | switch (padding->pad_type) { |
601 | | case WC_RSA_PKCSV15_PAD: |
602 | | pk_type = WC_PK_TYPE_RSA_PKCS; |
603 | | break; |
604 | | case WC_RSA_PSS_PAD: |
605 | | pk_type = WC_PK_TYPE_RSA_PSS; |
606 | | break; |
607 | | case WC_RSA_OAEP_PAD: |
608 | | pk_type = WC_PK_TYPE_RSA_OAEP; |
609 | | break; |
610 | | default: |
611 | | pk_type = WC_PK_TYPE_RSA; |
612 | | } |
613 | | } else { |
614 | | pk_type = WC_PK_TYPE_RSA; |
615 | | } |
616 | | |
617 | | if (dev && dev->cb) { |
618 | | wc_CryptoInfo cryptoInfo; |
619 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
620 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
621 | | cryptoInfo.pk.type = pk_type; |
622 | | cryptoInfo.pk.rsa.in = in; |
623 | | cryptoInfo.pk.rsa.inLen = inLen; |
624 | | cryptoInfo.pk.rsa.out = out; |
625 | | cryptoInfo.pk.rsa.outLen = outLen; |
626 | | cryptoInfo.pk.rsa.type = type; |
627 | | cryptoInfo.pk.rsa.key = key; |
628 | | cryptoInfo.pk.rsa.rng = rng; |
629 | | cryptoInfo.pk.rsa.padding = padding; |
630 | | |
631 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
632 | | } |
633 | | |
634 | | return wc_CryptoCb_TranslateErrorCode(ret); |
635 | | } |
636 | | |
637 | | /* Verify an RSA-PSS signature on the device (padding included). Passes both the |
638 | | * signature and digest so the device does the whole verify and returns a verdict. */ |
639 | | int wc_CryptoCb_RsaPssVerify(const byte* sig, word32 sigSz, const byte* digest, |
640 | | word32 digestSz, enum wc_HashType hash, int mgf, int saltLen, RsaKey* key, |
641 | | int* res, byte* out, word32 outSz, word32* outLen) |
642 | | { |
643 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
644 | | CryptoCb* dev; |
645 | | |
646 | | if (key == NULL) |
647 | | return ret; |
648 | | |
649 | | /* locate registered callback */ |
650 | | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
651 | | |
652 | | if (dev && dev->cb) { |
653 | | wc_CryptoInfo cryptoInfo; |
654 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
655 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
656 | | cryptoInfo.pk.type = WC_PK_TYPE_RSA_PSS_VERIFY; |
657 | | cryptoInfo.pk.rsa_pss_verify.sig = sig; |
658 | | cryptoInfo.pk.rsa_pss_verify.sigSz = sigSz; |
659 | | cryptoInfo.pk.rsa_pss_verify.digest = digest; |
660 | | cryptoInfo.pk.rsa_pss_verify.digestSz = digestSz; |
661 | | cryptoInfo.pk.rsa_pss_verify.hash = hash; |
662 | | cryptoInfo.pk.rsa_pss_verify.mgf = mgf; |
663 | | cryptoInfo.pk.rsa_pss_verify.saltLen = saltLen; |
664 | | cryptoInfo.pk.rsa_pss_verify.key = key; |
665 | | cryptoInfo.pk.rsa_pss_verify.res = res; |
666 | | cryptoInfo.pk.rsa_pss_verify.out = out; |
667 | | cryptoInfo.pk.rsa_pss_verify.outSz = outSz; |
668 | | cryptoInfo.pk.rsa_pss_verify.outLen = outLen; |
669 | | |
670 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
671 | | } |
672 | | |
673 | | return wc_CryptoCb_TranslateErrorCode(ret); |
674 | | } |
675 | | #endif /* WOLF_CRYPTO_CB_RSA_PAD */ |
676 | | |
677 | | #ifdef WOLFSSL_KEY_GEN |
678 | | int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng) |
679 | 0 | { |
680 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
681 | 0 | CryptoCb* dev; |
682 | |
|
683 | 0 | if (key == NULL) |
684 | 0 | return ret; |
685 | | |
686 | | /* locate registered callback */ |
687 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
688 | 0 | if (dev && dev->cb) { |
689 | 0 | wc_CryptoInfo cryptoInfo; |
690 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
691 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
692 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_RSA_KEYGEN; |
693 | 0 | cryptoInfo.pk.rsakg.key = key; |
694 | 0 | cryptoInfo.pk.rsakg.size = size; |
695 | 0 | cryptoInfo.pk.rsakg.e = e; |
696 | 0 | cryptoInfo.pk.rsakg.rng = rng; |
697 | |
|
698 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
699 | 0 | } |
700 | |
|
701 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
702 | 0 | } |
703 | | #endif |
704 | | |
705 | | int wc_CryptoCb_RsaCheckPrivKey(RsaKey* key, const byte* pubKey, |
706 | | word32 pubKeySz) |
707 | 0 | { |
708 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
709 | 0 | CryptoCb* dev; |
710 | |
|
711 | 0 | if (key == NULL) |
712 | 0 | return ret; |
713 | | |
714 | | /* locate registered callback */ |
715 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
716 | 0 | if (dev && dev->cb) { |
717 | 0 | wc_CryptoInfo cryptoInfo; |
718 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
719 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
720 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_RSA_CHECK_PRIV_KEY; |
721 | 0 | cryptoInfo.pk.rsa_check.key = key; |
722 | 0 | cryptoInfo.pk.rsa_check.pubKey = pubKey; |
723 | 0 | cryptoInfo.pk.rsa_check.pubKeySz = pubKeySz; |
724 | |
|
725 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
726 | 0 | } |
727 | |
|
728 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
729 | 0 | } |
730 | | |
731 | | int wc_CryptoCb_RsaGetSize(const RsaKey* key, int* keySize) |
732 | 0 | { |
733 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
734 | 0 | CryptoCb* dev; |
735 | |
|
736 | 0 | if (key == NULL) |
737 | 0 | return ret; |
738 | | |
739 | | /* locate registered callback */ |
740 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
741 | 0 | if (dev && dev->cb) { |
742 | 0 | wc_CryptoInfo cryptoInfo; |
743 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
744 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
745 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_RSA_GET_SIZE; |
746 | 0 | cryptoInfo.pk.rsa_get_size.key = key; |
747 | 0 | cryptoInfo.pk.rsa_get_size.keySize = keySize; |
748 | |
|
749 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
750 | 0 | } |
751 | |
|
752 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
753 | 0 | } |
754 | | #endif /* !NO_RSA */ |
755 | | |
756 | | #ifdef HAVE_ECC |
757 | | #ifdef HAVE_ECC_DHE |
758 | | int wc_CryptoCb_MakeEccKey(WC_RNG* rng, int keySize, ecc_key* key, int curveId) |
759 | 0 | { |
760 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
761 | 0 | CryptoCb* dev; |
762 | |
|
763 | 0 | if (key == NULL) |
764 | 0 | return ret; |
765 | | |
766 | | /* locate registered callback */ |
767 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
768 | 0 | if (dev && dev->cb) { |
769 | 0 | wc_CryptoInfo cryptoInfo; |
770 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
771 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
772 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_KEYGEN; |
773 | 0 | cryptoInfo.pk.eckg.rng = rng; |
774 | 0 | cryptoInfo.pk.eckg.size = keySize; |
775 | 0 | cryptoInfo.pk.eckg.key = key; |
776 | 0 | cryptoInfo.pk.eckg.curveId = curveId; |
777 | |
|
778 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
779 | 0 | } |
780 | |
|
781 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
782 | 0 | } |
783 | | |
784 | | int wc_CryptoCb_Ecdh(ecc_key* private_key, ecc_key* public_key, |
785 | | byte* out, word32* outlen) |
786 | 0 | { |
787 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
788 | 0 | CryptoCb* dev; |
789 | |
|
790 | 0 | if (private_key == NULL) |
791 | 0 | return ret; |
792 | | |
793 | | /* locate registered callback */ |
794 | 0 | dev = wc_CryptoCb_FindDevice(private_key->devId, WC_ALGO_TYPE_PK); |
795 | 0 | if (dev && dev->cb) { |
796 | 0 | wc_CryptoInfo cryptoInfo; |
797 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
798 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
799 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ECDH; |
800 | 0 | cryptoInfo.pk.ecdh.private_key = private_key; |
801 | 0 | cryptoInfo.pk.ecdh.public_key = public_key; |
802 | 0 | cryptoInfo.pk.ecdh.out = out; |
803 | 0 | cryptoInfo.pk.ecdh.outlen = outlen; |
804 | |
|
805 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
806 | 0 | } |
807 | |
|
808 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
809 | 0 | } |
810 | | #endif |
811 | | |
812 | | #ifdef HAVE_ECC_SIGN |
813 | | int wc_CryptoCb_EccSign(const byte* in, word32 inlen, byte* out, |
814 | | word32 *outlen, WC_RNG* rng, ecc_key* key) |
815 | 0 | { |
816 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
817 | 0 | CryptoCb* dev; |
818 | |
|
819 | 0 | if (key == NULL) |
820 | 0 | return ret; |
821 | | |
822 | | /* locate registered callback */ |
823 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
824 | 0 | if (dev && dev->cb) { |
825 | 0 | wc_CryptoInfo cryptoInfo; |
826 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
827 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
828 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_SIGN; |
829 | 0 | cryptoInfo.pk.eccsign.in = in; |
830 | 0 | cryptoInfo.pk.eccsign.inlen = inlen; |
831 | 0 | cryptoInfo.pk.eccsign.out = out; |
832 | 0 | cryptoInfo.pk.eccsign.outlen = outlen; |
833 | 0 | cryptoInfo.pk.eccsign.rng = rng; |
834 | 0 | cryptoInfo.pk.eccsign.key = key; |
835 | |
|
836 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
837 | 0 | } |
838 | |
|
839 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
840 | 0 | } |
841 | | #endif |
842 | | |
843 | | #ifdef HAVE_ECC_VERIFY |
844 | | int wc_CryptoCb_EccVerify(const byte* sig, word32 siglen, |
845 | | const byte* hash, word32 hashlen, int* res, ecc_key* key) |
846 | 0 | { |
847 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
848 | 0 | CryptoCb* dev; |
849 | |
|
850 | 0 | if (key == NULL) |
851 | 0 | return ret; |
852 | | |
853 | | /* locate registered callback */ |
854 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
855 | 0 | if (dev && dev->cb) { |
856 | 0 | wc_CryptoInfo cryptoInfo; |
857 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
858 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
859 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_VERIFY; |
860 | 0 | cryptoInfo.pk.eccverify.sig = sig; |
861 | 0 | cryptoInfo.pk.eccverify.siglen = siglen; |
862 | 0 | cryptoInfo.pk.eccverify.hash = hash; |
863 | 0 | cryptoInfo.pk.eccverify.hashlen = hashlen; |
864 | 0 | cryptoInfo.pk.eccverify.res = res; |
865 | 0 | cryptoInfo.pk.eccverify.key = key; |
866 | |
|
867 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
868 | 0 | } |
869 | |
|
870 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
871 | 0 | } |
872 | | #endif |
873 | | |
874 | | #ifdef HAVE_ECC_CHECK_KEY |
875 | | int wc_CryptoCb_EccCheckPrivKey(ecc_key* key, const byte* pubKey, |
876 | | word32 pubKeySz) |
877 | 0 | { |
878 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
879 | 0 | CryptoCb* dev; |
880 | |
|
881 | 0 | if (key == NULL) |
882 | 0 | return ret; |
883 | | |
884 | | /* locate registered callback */ |
885 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
886 | 0 | if (dev && dev->cb) { |
887 | 0 | wc_CryptoInfo cryptoInfo; |
888 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
889 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
890 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_CHECK_PRIV_KEY; |
891 | 0 | cryptoInfo.pk.ecc_check.key = key; |
892 | 0 | cryptoInfo.pk.ecc_check.pubKey = pubKey; |
893 | 0 | cryptoInfo.pk.ecc_check.pubKeySz = pubKeySz; |
894 | |
|
895 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
896 | 0 | } |
897 | |
|
898 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
899 | 0 | } |
900 | | #endif |
901 | | |
902 | | int wc_CryptoCb_EccGetSize(const ecc_key* key, int* keySize) |
903 | 0 | { |
904 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
905 | 0 | CryptoCb* dev; |
906 | |
|
907 | 0 | if (key == NULL) |
908 | 0 | return ret; |
909 | | |
910 | | /* locate registered callback */ |
911 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
912 | 0 | if (dev && dev->cb) { |
913 | 0 | wc_CryptoInfo cryptoInfo; |
914 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
915 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
916 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_GET_SIZE; |
917 | 0 | cryptoInfo.pk.ecc_get_size.key = key; |
918 | 0 | cryptoInfo.pk.ecc_get_size.keySize = keySize; |
919 | |
|
920 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
921 | 0 | } |
922 | |
|
923 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
924 | 0 | } |
925 | | |
926 | | int wc_CryptoCb_EccGetSigSize(const ecc_key* key, int* sigSize) |
927 | 0 | { |
928 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
929 | 0 | CryptoCb* dev; |
930 | |
|
931 | 0 | if (key == NULL) |
932 | 0 | return ret; |
933 | | |
934 | | /* locate registered callback */ |
935 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
936 | 0 | if (dev && dev->cb) { |
937 | 0 | wc_CryptoInfo cryptoInfo; |
938 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
939 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
940 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_GET_SIG_SIZE; |
941 | 0 | cryptoInfo.pk.ecc_get_sig_size.key = key; |
942 | 0 | cryptoInfo.pk.ecc_get_sig_size.sigSize = sigSize; |
943 | |
|
944 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
945 | 0 | } |
946 | |
|
947 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
948 | 0 | } |
949 | | |
950 | | int wc_CryptoCb_EccMakePub(ecc_key* key, ecc_point* pubOut) |
951 | 0 | { |
952 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
953 | 0 | CryptoCb* dev; |
954 | |
|
955 | 0 | if (key == NULL || pubOut == NULL || key->dp == NULL) |
956 | 0 | return ret; |
957 | | |
958 | 0 | if (key->dp->size > MAX_ECC_BYTES) |
959 | 0 | return ret; |
960 | | |
961 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
962 | 0 | if (dev && dev->cb) { |
963 | 0 | wc_CryptoInfo cryptoInfo; |
964 | 0 | word32 curveSz = (word32)key->dp->size; |
965 | 0 | word32 ptSz = 1 + 2 * curveSz; /* X9.63 uncompressed length */ |
966 | 0 | word32 outSz = 1 + 2 * MAX_ECC_BYTES; /* buffer size on input */ |
967 | 0 | WC_DECLARE_VAR(buf, byte, (1 + 2 * MAX_ECC_BYTES), key->heap); |
968 | 0 | WC_ALLOC_VAR_EX(buf, byte, (1 + 2 * MAX_ECC_BYTES), key->heap, |
969 | 0 | DYNAMIC_TYPE_ECC_BUFFER, return MEMORY_E); |
970 | | |
971 | | /* zero the result buffer so a handler that returns success without |
972 | | * writing output is rejected deterministically by the tag check */ |
973 | 0 | XMEMSET(buf, 0, ptSz); |
974 | |
|
975 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
976 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
977 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_MAKE_PUB; |
978 | 0 | cryptoInfo.pk.ecc_make_pub.key = key; |
979 | 0 | cryptoInfo.pk.ecc_make_pub.pubOut = buf; |
980 | 0 | cryptoInfo.pk.ecc_make_pub.pubOutSz = &outSz; |
981 | |
|
982 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
983 | | |
984 | | /* deserialize X9.63 uncompressed result into pubOut; reject a result |
985 | | * whose size is not exactly the curve's X9.63 length */ |
986 | 0 | if (ret == 0) { |
987 | 0 | if (outSz != ptSz || buf[0] != ECC_POINT_UNCOMP) { |
988 | 0 | ret = BUFFER_E; |
989 | 0 | } |
990 | 0 | else { |
991 | 0 | int err = mp_read_unsigned_bin(pubOut->x, buf + 1, curveSz); |
992 | 0 | if (err == MP_OKAY) |
993 | 0 | err = mp_read_unsigned_bin(pubOut->y, buf + 1 + curveSz, |
994 | 0 | curveSz); |
995 | 0 | if (err == MP_OKAY) |
996 | 0 | err = mp_set(pubOut->z, 1); |
997 | 0 | if (err != MP_OKAY) |
998 | 0 | ret = err; |
999 | 0 | } |
1000 | 0 | } |
1001 | |
|
1002 | 0 | WC_FREE_VAR_EX(buf, key->heap, DYNAMIC_TYPE_ECC_BUFFER); |
1003 | 0 | } |
1004 | | |
1005 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1006 | 0 | } |
1007 | | |
1008 | | #ifdef HAVE_ECC_CHECK_KEY |
1009 | | int wc_CryptoCb_EccCheckPubKey(ecc_key* key, int checkOrder, int checkPriv) |
1010 | 0 | { |
1011 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1012 | 0 | CryptoCb* dev; |
1013 | |
|
1014 | 0 | if (key == NULL || key->dp == NULL) |
1015 | 0 | return ret; |
1016 | | |
1017 | 0 | if (key->dp->size > MAX_ECC_BYTES) |
1018 | 0 | return ret; |
1019 | | |
1020 | | /* locate registered callback */ |
1021 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1022 | 0 | if (dev && dev->cb) { |
1023 | 0 | wc_CryptoInfo cryptoInfo; |
1024 | 0 | word32 curveSz = (word32)key->dp->size; |
1025 | 0 | word32 ptSz = 1 + 2 * curveSz; |
1026 | 0 | word32 xSz = curveSz; |
1027 | 0 | word32 ySz = curveSz; |
1028 | | /* a key with no host-side public point is represented by |
1029 | | * ECC_PRIVATEKEY_ONLY and crosses as pubKey = NULL / pubKeySz = 0. |
1030 | | * If the key state says a public point exists, serialize it even when |
1031 | | * the coordinates are invalid (e.g. 0,0) so the device can reject the |
1032 | | * actual input instead of seeing "no public point". */ |
1033 | 0 | int havePub = (key->type != ECC_PRIVATEKEY_ONLY); |
1034 | 0 | WC_DECLARE_VAR(buf, byte, (1 + 2 * MAX_ECC_BYTES), key->heap); |
1035 | |
|
1036 | 0 | ret = MP_OKAY; |
1037 | 0 | if (havePub) { |
1038 | 0 | WC_ALLOC_VAR_EX(buf, byte, (1 + 2 * MAX_ECC_BYTES), key->heap, |
1039 | 0 | DYNAMIC_TYPE_ECC_BUFFER, return MEMORY_E); |
1040 | | /* serialize key->pubkey to X9.63 uncompressed (0x04 || X || Y) */ |
1041 | 0 | buf[0] = ECC_POINT_UNCOMP; |
1042 | 0 | ret = wc_export_int(key->pubkey.x, buf + 1, &xSz, curveSz, |
1043 | 0 | WC_TYPE_UNSIGNED_BIN); |
1044 | 0 | if (ret == MP_OKAY) |
1045 | 0 | ret = wc_export_int(key->pubkey.y, buf + 1 + curveSz, &ySz, |
1046 | 0 | curveSz, WC_TYPE_UNSIGNED_BIN); |
1047 | 0 | } |
1048 | | |
1049 | 0 | if (ret == MP_OKAY) { |
1050 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1051 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1052 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_CHECK_PUB_KEY; |
1053 | 0 | cryptoInfo.pk.ecc_check_pub.key = key; |
1054 | 0 | cryptoInfo.pk.ecc_check_pub.pubKey = havePub ? buf : NULL; |
1055 | 0 | cryptoInfo.pk.ecc_check_pub.pubKeySz = havePub ? ptSz : 0; |
1056 | 0 | cryptoInfo.pk.ecc_check_pub.checkOrder = checkOrder; |
1057 | 0 | cryptoInfo.pk.ecc_check_pub.checkPriv = checkPriv; |
1058 | |
|
1059 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1060 | 0 | } |
1061 | |
|
1062 | 0 | if (havePub) { |
1063 | 0 | WC_FREE_VAR_EX(buf, key->heap, DYNAMIC_TYPE_ECC_BUFFER); |
1064 | 0 | } |
1065 | 0 | } |
1066 | | |
1067 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1068 | 0 | } |
1069 | | #endif /* HAVE_ECC_CHECK_KEY */ |
1070 | | |
1071 | | #ifdef HAVE_ECC_ENCRYPT |
1072 | | int wc_CryptoCb_EciesEncrypt(ecc_key* privKey, ecc_key* pubKey, |
1073 | | const byte* msg, word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx, |
1074 | | int compressed) |
1075 | 0 | { |
1076 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1077 | 0 | CryptoCb* dev; |
1078 | |
|
1079 | 0 | if (privKey == NULL) |
1080 | 0 | return ret; |
1081 | | |
1082 | | /* locate registered callback */ |
1083 | 0 | dev = wc_CryptoCb_FindDevice(privKey->devId, WC_ALGO_TYPE_PK); |
1084 | 0 | if (dev && dev->cb) { |
1085 | 0 | wc_CryptoInfo cryptoInfo; |
1086 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1087 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1088 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ECIES_ENCRYPT; |
1089 | 0 | cryptoInfo.pk.eciesencrypt.privKey = privKey; |
1090 | 0 | cryptoInfo.pk.eciesencrypt.pubKey = pubKey; |
1091 | 0 | cryptoInfo.pk.eciesencrypt.msg = msg; |
1092 | 0 | cryptoInfo.pk.eciesencrypt.msgSz = msgSz; |
1093 | 0 | cryptoInfo.pk.eciesencrypt.out = out; |
1094 | 0 | cryptoInfo.pk.eciesencrypt.outSz = outSz; |
1095 | 0 | cryptoInfo.pk.eciesencrypt.ctx = ctx; |
1096 | 0 | cryptoInfo.pk.eciesencrypt.compressed = compressed; |
1097 | |
|
1098 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1099 | 0 | } |
1100 | |
|
1101 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1102 | 0 | } |
1103 | | |
1104 | | int wc_CryptoCb_EciesDecrypt(ecc_key* privKey, ecc_key* pubKey, |
1105 | | const byte* msg, word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx) |
1106 | 0 | { |
1107 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1108 | 0 | CryptoCb* dev; |
1109 | |
|
1110 | 0 | if (privKey == NULL) |
1111 | 0 | return ret; |
1112 | | |
1113 | | /* locate registered callback */ |
1114 | 0 | dev = wc_CryptoCb_FindDevice(privKey->devId, WC_ALGO_TYPE_PK); |
1115 | 0 | if (dev && dev->cb) { |
1116 | 0 | wc_CryptoInfo cryptoInfo; |
1117 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1118 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1119 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ECIES_DECRYPT; |
1120 | 0 | cryptoInfo.pk.eciesdecrypt.privKey = privKey; |
1121 | 0 | cryptoInfo.pk.eciesdecrypt.pubKey = pubKey; |
1122 | 0 | cryptoInfo.pk.eciesdecrypt.msg = msg; |
1123 | 0 | cryptoInfo.pk.eciesdecrypt.msgSz = msgSz; |
1124 | 0 | cryptoInfo.pk.eciesdecrypt.out = out; |
1125 | 0 | cryptoInfo.pk.eciesdecrypt.outSz = outSz; |
1126 | 0 | cryptoInfo.pk.eciesdecrypt.ctx = ctx; |
1127 | |
|
1128 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1129 | 0 | } |
1130 | |
|
1131 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1132 | 0 | } |
1133 | | #endif /* HAVE_ECC_ENCRYPT */ |
1134 | | #endif /* HAVE_ECC */ |
1135 | | |
1136 | | #ifdef HAVE_CURVE25519 |
1137 | | int wc_CryptoCb_Curve25519Gen(WC_RNG* rng, int keySize, |
1138 | | curve25519_key* key) |
1139 | 0 | { |
1140 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1141 | 0 | CryptoCb* dev; |
1142 | |
|
1143 | 0 | if (key == NULL) |
1144 | 0 | return ret; |
1145 | | |
1146 | | /* locate registered callback */ |
1147 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1148 | 0 | if (dev && dev->cb) { |
1149 | 0 | wc_CryptoInfo cryptoInfo; |
1150 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1151 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1152 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519_KEYGEN; |
1153 | 0 | cryptoInfo.pk.curve25519kg.rng = rng; |
1154 | 0 | cryptoInfo.pk.curve25519kg.size = keySize; |
1155 | 0 | cryptoInfo.pk.curve25519kg.key = key; |
1156 | |
|
1157 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1158 | 0 | } |
1159 | |
|
1160 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1161 | 0 | } |
1162 | | |
1163 | | int wc_CryptoCb_Curve25519(curve25519_key* private_key, |
1164 | | curve25519_key* public_key, byte* out, word32* outlen, int endian) |
1165 | 0 | { |
1166 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1167 | 0 | CryptoCb* dev; |
1168 | |
|
1169 | 0 | if (private_key == NULL) |
1170 | 0 | return ret; |
1171 | | |
1172 | | /* locate registered callback */ |
1173 | 0 | dev = wc_CryptoCb_FindDevice(private_key->devId, WC_ALGO_TYPE_PK); |
1174 | 0 | if (dev && dev->cb) { |
1175 | 0 | wc_CryptoInfo cryptoInfo; |
1176 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1177 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1178 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519; |
1179 | 0 | cryptoInfo.pk.curve25519.private_key = private_key; |
1180 | 0 | cryptoInfo.pk.curve25519.public_key = public_key; |
1181 | 0 | cryptoInfo.pk.curve25519.out = out; |
1182 | 0 | cryptoInfo.pk.curve25519.outlen = outlen; |
1183 | 0 | cryptoInfo.pk.curve25519.endian = endian; |
1184 | |
|
1185 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1186 | 0 | } |
1187 | |
|
1188 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1189 | 0 | } |
1190 | | |
1191 | | int wc_CryptoCb_Curve25519MakePub(int public_size, byte* pub, |
1192 | | int private_size, const byte* priv) |
1193 | 8.00k | { |
1194 | 8.00k | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1195 | 8.00k | CryptoCb* dev; |
1196 | | |
1197 | 8.00k | if (pub == NULL || priv == NULL) |
1198 | 0 | return ret; |
1199 | | |
1200 | | /* try the find callback first, else grab the first registered device */ |
1201 | 8.00k | dev = wc_CryptoCb_FindDevice(INVALID_DEVID, WC_ALGO_TYPE_PK); |
1202 | 8.00k | if (dev == NULL || dev->cb == NULL) |
1203 | 8.00k | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1204 | 8.00k | if (dev && dev->cb) { |
1205 | 2.04k | wc_CryptoInfo cryptoInfo; |
1206 | 2.04k | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1207 | 2.04k | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1208 | 2.04k | cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519_MAKE_PUB; |
1209 | 2.04k | cryptoInfo.pk.curve25519makepub.pub = pub; |
1210 | 2.04k | cryptoInfo.pk.curve25519makepub.pubSz = (word32)public_size; |
1211 | 2.04k | cryptoInfo.pk.curve25519makepub.priv = priv; |
1212 | 2.04k | cryptoInfo.pk.curve25519makepub.privSz = (word32)private_size; |
1213 | | |
1214 | 2.04k | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1215 | 2.04k | } |
1216 | | |
1217 | 8.00k | return wc_CryptoCb_TranslateErrorCode(ret); |
1218 | 8.00k | } |
1219 | | |
1220 | | int wc_CryptoCb_Curve25519Generic(int public_size, byte* pub, |
1221 | | int private_size, const byte* priv, int basepoint_size, |
1222 | | const byte* basepoint) |
1223 | 0 | { |
1224 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1225 | 0 | CryptoCb* dev; |
1226 | |
|
1227 | 0 | if (pub == NULL || priv == NULL || basepoint == NULL) |
1228 | 0 | return ret; |
1229 | | |
1230 | | /* try the find callback first, else grab the first registered device */ |
1231 | 0 | dev = wc_CryptoCb_FindDevice(INVALID_DEVID, WC_ALGO_TYPE_PK); |
1232 | 0 | if (dev == NULL || dev->cb == NULL) |
1233 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1234 | 0 | if (dev && dev->cb) { |
1235 | 0 | wc_CryptoInfo cryptoInfo; |
1236 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1237 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1238 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519_GENERIC; |
1239 | 0 | cryptoInfo.pk.curve25519generic.pub = pub; |
1240 | 0 | cryptoInfo.pk.curve25519generic.pubSz = (word32)public_size; |
1241 | 0 | cryptoInfo.pk.curve25519generic.priv = priv; |
1242 | 0 | cryptoInfo.pk.curve25519generic.privSz = (word32)private_size; |
1243 | 0 | cryptoInfo.pk.curve25519generic.basepoint = basepoint; |
1244 | 0 | cryptoInfo.pk.curve25519generic.basepointSz = (word32)basepoint_size; |
1245 | |
|
1246 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1247 | 0 | } |
1248 | |
|
1249 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1250 | 0 | } |
1251 | | #endif /* HAVE_CURVE25519 */ |
1252 | | |
1253 | | #ifdef HAVE_ED25519 |
1254 | | int wc_CryptoCb_Ed25519Gen(WC_RNG* rng, int keySize, |
1255 | | ed25519_key* key) |
1256 | 0 | { |
1257 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1258 | 0 | CryptoCb* dev; |
1259 | |
|
1260 | 0 | if (key == NULL) |
1261 | 0 | return ret; |
1262 | | |
1263 | | /* locate registered callback */ |
1264 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1265 | 0 | if (dev && dev->cb) { |
1266 | 0 | wc_CryptoInfo cryptoInfo; |
1267 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1268 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1269 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED25519_KEYGEN; |
1270 | 0 | cryptoInfo.pk.ed25519kg.rng = rng; |
1271 | 0 | cryptoInfo.pk.ed25519kg.size = keySize; |
1272 | 0 | cryptoInfo.pk.ed25519kg.key = key; |
1273 | |
|
1274 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1275 | 0 | } |
1276 | |
|
1277 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1278 | 0 | } |
1279 | | |
1280 | | int wc_CryptoCb_Ed25519Sign(const byte* in, word32 inLen, byte* out, |
1281 | | word32 *outLen, ed25519_key* key, byte type, |
1282 | | const byte* context, byte contextLen) |
1283 | 0 | { |
1284 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1285 | 0 | CryptoCb* dev; |
1286 | |
|
1287 | 0 | if (key == NULL) |
1288 | 0 | return ret; |
1289 | | |
1290 | | /* locate registered callback */ |
1291 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1292 | 0 | if (dev && dev->cb) { |
1293 | 0 | wc_CryptoInfo cryptoInfo; |
1294 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1295 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1296 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED25519_SIGN; |
1297 | 0 | cryptoInfo.pk.ed25519sign.in = in; |
1298 | 0 | cryptoInfo.pk.ed25519sign.inLen = inLen; |
1299 | 0 | cryptoInfo.pk.ed25519sign.out = out; |
1300 | 0 | cryptoInfo.pk.ed25519sign.outLen = outLen; |
1301 | 0 | cryptoInfo.pk.ed25519sign.key = key; |
1302 | 0 | cryptoInfo.pk.ed25519sign.type = type; |
1303 | 0 | cryptoInfo.pk.ed25519sign.context = context; |
1304 | 0 | cryptoInfo.pk.ed25519sign.contextLen = contextLen; |
1305 | |
|
1306 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1307 | 0 | } |
1308 | |
|
1309 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1310 | 0 | } |
1311 | | |
1312 | | int wc_CryptoCb_Ed25519Verify(const byte* sig, word32 sigLen, |
1313 | | const byte* msg, word32 msgLen, int* res, ed25519_key* key, byte type, |
1314 | | const byte* context, byte contextLen) |
1315 | 0 | { |
1316 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1317 | 0 | CryptoCb* dev; |
1318 | |
|
1319 | 0 | if (key == NULL) |
1320 | 0 | return ret; |
1321 | | |
1322 | | /* locate registered callback */ |
1323 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1324 | 0 | if (dev && dev->cb) { |
1325 | 0 | wc_CryptoInfo cryptoInfo; |
1326 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1327 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1328 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED25519_VERIFY; |
1329 | 0 | cryptoInfo.pk.ed25519verify.sig = sig; |
1330 | 0 | cryptoInfo.pk.ed25519verify.sigLen = sigLen; |
1331 | 0 | cryptoInfo.pk.ed25519verify.msg = msg; |
1332 | 0 | cryptoInfo.pk.ed25519verify.msgLen = msgLen; |
1333 | 0 | cryptoInfo.pk.ed25519verify.res = res; |
1334 | 0 | cryptoInfo.pk.ed25519verify.key = key; |
1335 | 0 | cryptoInfo.pk.ed25519verify.type = type; |
1336 | 0 | cryptoInfo.pk.ed25519verify.context = context; |
1337 | 0 | cryptoInfo.pk.ed25519verify.contextLen = contextLen; |
1338 | |
|
1339 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1340 | 0 | } |
1341 | |
|
1342 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1343 | 0 | } |
1344 | | |
1345 | | int wc_CryptoCb_Ed25519MakePub(ed25519_key* key, byte* pubKey, word32 pubKeySz) |
1346 | 0 | { |
1347 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1348 | 0 | CryptoCb* dev; |
1349 | |
|
1350 | 0 | if (key == NULL || pubKey == NULL || pubKeySz != ED25519_PUB_KEY_SIZE) |
1351 | 0 | return ret; |
1352 | | |
1353 | | /* locate registered callback */ |
1354 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1355 | 0 | if (dev && dev->cb) { |
1356 | 0 | wc_CryptoInfo cryptoInfo; |
1357 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1358 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1359 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED25519_MAKE_PUB; |
1360 | 0 | cryptoInfo.pk.ed25519makepub.key = key; |
1361 | 0 | cryptoInfo.pk.ed25519makepub.pubOut = pubKey; |
1362 | 0 | cryptoInfo.pk.ed25519makepub.pubOutSz = pubKeySz; |
1363 | |
|
1364 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1365 | 0 | } |
1366 | |
|
1367 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1368 | 0 | } |
1369 | | |
1370 | | int wc_CryptoCb_Ed25519CheckKey(ed25519_key* key) |
1371 | 0 | { |
1372 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1373 | 0 | CryptoCb* dev; |
1374 | |
|
1375 | 0 | if (key == NULL) |
1376 | 0 | return ret; |
1377 | | |
1378 | | /* locate registered callback */ |
1379 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1380 | 0 | if (dev && dev->cb) { |
1381 | 0 | wc_CryptoInfo cryptoInfo; |
1382 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1383 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1384 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED25519_CHECK_KEY; |
1385 | 0 | cryptoInfo.pk.ed25519checkkey.key = key; |
1386 | | /* key->p is already the compressed wire form; cross it as bytes so a |
1387 | | * device can validate the actual input without touching key internals |
1388 | | */ |
1389 | 0 | cryptoInfo.pk.ed25519checkkey.pubKey = key->p; |
1390 | 0 | cryptoInfo.pk.ed25519checkkey.pubKeySz = ED25519_PUB_KEY_SIZE; |
1391 | 0 | cryptoInfo.pk.ed25519checkkey.checkPriv = key->privKeySet ? 1 : 0; |
1392 | |
|
1393 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1394 | 0 | } |
1395 | |
|
1396 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1397 | 0 | } |
1398 | | #endif /* HAVE_ED25519 */ |
1399 | | |
1400 | | #ifdef HAVE_ED448 |
1401 | | int wc_CryptoCb_Ed448Sign(const byte* in, word32 inLen, byte* out, |
1402 | | word32 *outLen, ed448_key* key, byte type, const byte* context, |
1403 | | byte contextLen) |
1404 | 0 | { |
1405 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1406 | 0 | CryptoCb* dev; |
1407 | |
|
1408 | 0 | if (key == NULL) |
1409 | 0 | return ret; |
1410 | | |
1411 | | /* locate registered callback */ |
1412 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1413 | 0 | if (dev && dev->cb) { |
1414 | 0 | wc_CryptoInfo cryptoInfo; |
1415 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1416 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1417 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED448; |
1418 | 0 | cryptoInfo.pk.ed448sign.in = in; |
1419 | 0 | cryptoInfo.pk.ed448sign.inLen = inLen; |
1420 | 0 | cryptoInfo.pk.ed448sign.out = out; |
1421 | 0 | cryptoInfo.pk.ed448sign.outLen = outLen; |
1422 | 0 | cryptoInfo.pk.ed448sign.key = key; |
1423 | 0 | cryptoInfo.pk.ed448sign.type = type; |
1424 | 0 | cryptoInfo.pk.ed448sign.context = context; |
1425 | 0 | cryptoInfo.pk.ed448sign.contextLen = contextLen; |
1426 | |
|
1427 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1428 | 0 | } |
1429 | |
|
1430 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1431 | 0 | } |
1432 | | |
1433 | | int wc_CryptoCb_Ed448Verify(const byte* sig, word32 sigLen, |
1434 | | const byte* msg, word32 msgLen, int* res, ed448_key* key, byte type, |
1435 | | const byte* context, byte contextLen) |
1436 | 0 | { |
1437 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1438 | 0 | CryptoCb* dev; |
1439 | |
|
1440 | 0 | if (key == NULL) |
1441 | 0 | return ret; |
1442 | | |
1443 | | /* locate registered callback */ |
1444 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1445 | 0 | if (dev && dev->cb) { |
1446 | 0 | wc_CryptoInfo cryptoInfo; |
1447 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1448 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1449 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED448_VERIFY; |
1450 | 0 | cryptoInfo.pk.ed448verify.sig = sig; |
1451 | 0 | cryptoInfo.pk.ed448verify.sigLen = sigLen; |
1452 | 0 | cryptoInfo.pk.ed448verify.msg = msg; |
1453 | 0 | cryptoInfo.pk.ed448verify.msgLen = msgLen; |
1454 | 0 | cryptoInfo.pk.ed448verify.res = res; |
1455 | 0 | cryptoInfo.pk.ed448verify.key = key; |
1456 | 0 | cryptoInfo.pk.ed448verify.type = type; |
1457 | 0 | cryptoInfo.pk.ed448verify.context = context; |
1458 | 0 | cryptoInfo.pk.ed448verify.contextLen = contextLen; |
1459 | |
|
1460 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1461 | 0 | } |
1462 | |
|
1463 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1464 | 0 | } |
1465 | | #endif /* HAVE_ED448 */ |
1466 | | |
1467 | | #if defined(WOLFSSL_HAVE_LMS) || defined(WOLFSSL_HAVE_XMSS) |
1468 | | int wc_CryptoCb_PqcStatefulSigGetDevId(int type, void* key) |
1469 | | { |
1470 | | int devId = INVALID_DEVID; |
1471 | | |
1472 | | if (key == NULL) |
1473 | | return devId; |
1474 | | |
1475 | | #if defined(WOLFSSL_HAVE_LMS) |
1476 | | if (type == WC_PQC_STATEFUL_SIG_TYPE_LMS) { |
1477 | | devId = ((LmsKey*)key)->devId; |
1478 | | } |
1479 | | #endif |
1480 | | #if defined(WOLFSSL_HAVE_XMSS) |
1481 | | if (type == WC_PQC_STATEFUL_SIG_TYPE_XMSS) { |
1482 | | devId = ((XmssKey*)key)->devId; |
1483 | | } |
1484 | | #endif |
1485 | | |
1486 | | return devId; |
1487 | | } |
1488 | | |
1489 | | int wc_CryptoCb_PqcStatefulSigKeyGen(int type, void* key, WC_RNG* rng) |
1490 | | { |
1491 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1492 | | int devId = INVALID_DEVID; |
1493 | | CryptoCb* dev; |
1494 | | |
1495 | | if (key == NULL) |
1496 | | return ret; |
1497 | | |
1498 | | devId = wc_CryptoCb_PqcStatefulSigGetDevId(type, key); |
1499 | | if (devId == INVALID_DEVID) |
1500 | | return ret; |
1501 | | |
1502 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1503 | | if (dev && dev->cb) { |
1504 | | wc_CryptoInfo cryptoInfo; |
1505 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1506 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1507 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_STATEFUL_SIG_KEYGEN; |
1508 | | cryptoInfo.pk.pqc_stateful_sig_kg.rng = rng; |
1509 | | cryptoInfo.pk.pqc_stateful_sig_kg.key = key; |
1510 | | cryptoInfo.pk.pqc_stateful_sig_kg.type = type; |
1511 | | |
1512 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1513 | | } |
1514 | | |
1515 | | return wc_CryptoCb_TranslateErrorCode(ret); |
1516 | | } |
1517 | | |
1518 | | int wc_CryptoCb_PqcStatefulSigSign(const byte* msg, word32 msgSz, byte* out, |
1519 | | word32* outSz, int type, void* key) |
1520 | | { |
1521 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1522 | | int devId = INVALID_DEVID; |
1523 | | CryptoCb* dev; |
1524 | | |
1525 | | if (key == NULL) |
1526 | | return ret; |
1527 | | |
1528 | | devId = wc_CryptoCb_PqcStatefulSigGetDevId(type, key); |
1529 | | if (devId == INVALID_DEVID) |
1530 | | return ret; |
1531 | | |
1532 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1533 | | if (dev && dev->cb) { |
1534 | | wc_CryptoInfo cryptoInfo; |
1535 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1536 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1537 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_STATEFUL_SIG_SIGN; |
1538 | | cryptoInfo.pk.pqc_stateful_sig_sign.msg = msg; |
1539 | | cryptoInfo.pk.pqc_stateful_sig_sign.msgSz = msgSz; |
1540 | | cryptoInfo.pk.pqc_stateful_sig_sign.out = out; |
1541 | | cryptoInfo.pk.pqc_stateful_sig_sign.outSz = outSz; |
1542 | | cryptoInfo.pk.pqc_stateful_sig_sign.key = key; |
1543 | | cryptoInfo.pk.pqc_stateful_sig_sign.type = type; |
1544 | | |
1545 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1546 | | } |
1547 | | |
1548 | | return wc_CryptoCb_TranslateErrorCode(ret); |
1549 | | } |
1550 | | |
1551 | | int wc_CryptoCb_PqcStatefulSigVerify(const byte* sig, word32 sigSz, |
1552 | | const byte* msg, word32 msgSz, int* res, int type, void* key) |
1553 | | { |
1554 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1555 | | int devId = INVALID_DEVID; |
1556 | | CryptoCb* dev; |
1557 | | |
1558 | | if (key == NULL) |
1559 | | return ret; |
1560 | | |
1561 | | devId = wc_CryptoCb_PqcStatefulSigGetDevId(type, key); |
1562 | | if (devId == INVALID_DEVID) |
1563 | | return ret; |
1564 | | |
1565 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1566 | | if (dev && dev->cb) { |
1567 | | wc_CryptoInfo cryptoInfo; |
1568 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1569 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1570 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_STATEFUL_SIG_VERIFY; |
1571 | | cryptoInfo.pk.pqc_stateful_sig_verify.sig = sig; |
1572 | | cryptoInfo.pk.pqc_stateful_sig_verify.sigSz = sigSz; |
1573 | | cryptoInfo.pk.pqc_stateful_sig_verify.msg = msg; |
1574 | | cryptoInfo.pk.pqc_stateful_sig_verify.msgSz = msgSz; |
1575 | | cryptoInfo.pk.pqc_stateful_sig_verify.res = res; |
1576 | | cryptoInfo.pk.pqc_stateful_sig_verify.key = key; |
1577 | | cryptoInfo.pk.pqc_stateful_sig_verify.type = type; |
1578 | | |
1579 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1580 | | } |
1581 | | |
1582 | | return wc_CryptoCb_TranslateErrorCode(ret); |
1583 | | } |
1584 | | |
1585 | | int wc_CryptoCb_PqcStatefulSigSigsLeft(int type, void* key, word32* sigsLeft) |
1586 | | { |
1587 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1588 | | int devId = INVALID_DEVID; |
1589 | | CryptoCb* dev; |
1590 | | |
1591 | | if (key == NULL) |
1592 | | return ret; |
1593 | | |
1594 | | devId = wc_CryptoCb_PqcStatefulSigGetDevId(type, key); |
1595 | | if (devId == INVALID_DEVID) |
1596 | | return ret; |
1597 | | |
1598 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1599 | | if (dev && dev->cb) { |
1600 | | wc_CryptoInfo cryptoInfo; |
1601 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1602 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1603 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_STATEFUL_SIG_SIGS_LEFT; |
1604 | | cryptoInfo.pk.pqc_stateful_sig_sigs_left.key = key; |
1605 | | cryptoInfo.pk.pqc_stateful_sig_sigs_left.sigsLeft = sigsLeft; |
1606 | | cryptoInfo.pk.pqc_stateful_sig_sigs_left.type = type; |
1607 | | |
1608 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1609 | | } |
1610 | | |
1611 | | return wc_CryptoCb_TranslateErrorCode(ret); |
1612 | | } |
1613 | | #endif /* WOLFSSL_HAVE_LMS || WOLFSSL_HAVE_XMSS */ |
1614 | | |
1615 | | #if defined(WOLFSSL_HAVE_MLKEM) || defined(WOLFSSL_HAVE_FRODOKEM) |
1616 | | int wc_CryptoCb_PqcKemGetDevId(int type, void* key) |
1617 | 0 | { |
1618 | 0 | int devId = INVALID_DEVID; |
1619 | |
|
1620 | 0 | if (key == NULL) |
1621 | 0 | return devId; |
1622 | | |
1623 | | /* get devId */ |
1624 | 0 | #ifdef WOLFSSL_HAVE_MLKEM |
1625 | 0 | if (type == WC_PQC_KEM_TYPE_MLKEM) { |
1626 | 0 | devId = ((MlKemKey*) key)->devId; |
1627 | 0 | } |
1628 | 0 | #endif |
1629 | | #ifdef WOLFSSL_HAVE_FRODOKEM |
1630 | | if (type == WC_PQC_KEM_TYPE_FRODOKEM) { |
1631 | | devId = ((FrodoKemKey*) key)->devId; |
1632 | | } |
1633 | | #endif |
1634 | |
|
1635 | 0 | return devId; |
1636 | 0 | } |
1637 | | |
1638 | | int wc_CryptoCb_MakePqcKemKey(WC_RNG* rng, int type, int keySize, void* key) |
1639 | 0 | { |
1640 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1641 | 0 | int devId = INVALID_DEVID; |
1642 | 0 | CryptoCb* dev; |
1643 | |
|
1644 | 0 | if (key == NULL) |
1645 | 0 | return ret; |
1646 | | |
1647 | | /* get devId */ |
1648 | 0 | devId = wc_CryptoCb_PqcKemGetDevId(type, key); |
1649 | 0 | if (devId == INVALID_DEVID) |
1650 | 0 | return ret; |
1651 | | |
1652 | | /* locate registered callback */ |
1653 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1654 | 0 | if (dev && dev->cb) { |
1655 | 0 | wc_CryptoInfo cryptoInfo; |
1656 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1657 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1658 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_PQC_KEM_KEYGEN; |
1659 | 0 | cryptoInfo.pk.pqc_kem_kg.rng = rng; |
1660 | 0 | cryptoInfo.pk.pqc_kem_kg.size = keySize; |
1661 | 0 | cryptoInfo.pk.pqc_kem_kg.key = key; |
1662 | 0 | cryptoInfo.pk.pqc_kem_kg.type = type; |
1663 | |
|
1664 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1665 | 0 | } |
1666 | |
|
1667 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1668 | 0 | } |
1669 | | |
1670 | | int wc_CryptoCb_PqcEncapsulate(byte* ciphertext, word32 ciphertextLen, |
1671 | | byte* sharedSecret, word32 sharedSecretLen, WC_RNG* rng, int type, |
1672 | | void* key) |
1673 | 0 | { |
1674 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1675 | 0 | int devId = INVALID_DEVID; |
1676 | 0 | CryptoCb* dev; |
1677 | |
|
1678 | 0 | if (key == NULL) |
1679 | 0 | return ret; |
1680 | | |
1681 | | /* get devId */ |
1682 | 0 | devId = wc_CryptoCb_PqcKemGetDevId(type, key); |
1683 | 0 | if (devId == INVALID_DEVID) |
1684 | 0 | return ret; |
1685 | | |
1686 | | /* locate registered callback */ |
1687 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1688 | 0 | if (dev && dev->cb) { |
1689 | 0 | wc_CryptoInfo cryptoInfo; |
1690 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1691 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1692 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_PQC_KEM_ENCAPS; |
1693 | 0 | cryptoInfo.pk.pqc_encaps.ciphertext = ciphertext; |
1694 | 0 | cryptoInfo.pk.pqc_encaps.ciphertextLen = ciphertextLen; |
1695 | 0 | cryptoInfo.pk.pqc_encaps.sharedSecret = sharedSecret; |
1696 | 0 | cryptoInfo.pk.pqc_encaps.sharedSecretLen = sharedSecretLen; |
1697 | 0 | cryptoInfo.pk.pqc_encaps.rng = rng; |
1698 | 0 | cryptoInfo.pk.pqc_encaps.key = key; |
1699 | 0 | cryptoInfo.pk.pqc_encaps.type = type; |
1700 | |
|
1701 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1702 | 0 | } |
1703 | |
|
1704 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1705 | 0 | } |
1706 | | |
1707 | | int wc_CryptoCb_PqcDecapsulate(const byte* ciphertext, word32 ciphertextLen, |
1708 | | byte* sharedSecret, word32 sharedSecretLen, int type, void* key) |
1709 | 0 | { |
1710 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1711 | 0 | int devId = INVALID_DEVID; |
1712 | 0 | CryptoCb* dev; |
1713 | |
|
1714 | 0 | if (key == NULL) |
1715 | 0 | return ret; |
1716 | | |
1717 | | /* get devId */ |
1718 | 0 | devId = wc_CryptoCb_PqcKemGetDevId(type, key); |
1719 | 0 | if (devId == INVALID_DEVID) |
1720 | 0 | return ret; |
1721 | | |
1722 | | /* locate registered callback */ |
1723 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1724 | 0 | if (dev && dev->cb) { |
1725 | 0 | wc_CryptoInfo cryptoInfo; |
1726 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1727 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1728 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_PQC_KEM_DECAPS; |
1729 | 0 | cryptoInfo.pk.pqc_decaps.ciphertext = ciphertext; |
1730 | 0 | cryptoInfo.pk.pqc_decaps.ciphertextLen = ciphertextLen; |
1731 | 0 | cryptoInfo.pk.pqc_decaps.sharedSecret = sharedSecret; |
1732 | 0 | cryptoInfo.pk.pqc_decaps.sharedSecretLen = sharedSecretLen; |
1733 | 0 | cryptoInfo.pk.pqc_decaps.key = key; |
1734 | 0 | cryptoInfo.pk.pqc_decaps.type = type; |
1735 | |
|
1736 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1737 | 0 | } |
1738 | |
|
1739 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1740 | 0 | } |
1741 | | #endif /* WOLFSSL_HAVE_MLKEM || WOLFSSL_HAVE_FRODOKEM */ |
1742 | | |
1743 | | #if defined(HAVE_FALCON) || defined(WOLFSSL_HAVE_MLDSA) || \ |
1744 | | defined(WOLFSSL_HAVE_SLHDSA) |
1745 | | int wc_CryptoCb_PqcSigGetDevId(int type, void* key) |
1746 | | { |
1747 | | int devId = INVALID_DEVID; |
1748 | | |
1749 | | if (key == NULL) |
1750 | | return devId; |
1751 | | |
1752 | | /* get devId */ |
1753 | | #if defined(WOLFSSL_HAVE_MLDSA) |
1754 | | if (type == WC_PQC_SIG_TYPE_MLDSA) { |
1755 | | devId = ((wc_MlDsaKey*) key)->devId; |
1756 | | } |
1757 | | #endif |
1758 | | #if defined(HAVE_FALCON) |
1759 | | if (type == WC_PQC_SIG_TYPE_FALCON) { |
1760 | | devId = ((falcon_key*) key)->devId; |
1761 | | } |
1762 | | #endif |
1763 | | #if defined(WOLFSSL_HAVE_SLHDSA) |
1764 | | if (type == WC_PQC_SIG_TYPE_SLHDSA) { |
1765 | | devId = ((SlhDsaKey*) key)->devId; |
1766 | | } |
1767 | | #endif |
1768 | | |
1769 | | return devId; |
1770 | | } |
1771 | | |
1772 | | int wc_CryptoCb_MakePqcSignatureKey(WC_RNG* rng, int type, int keySize, |
1773 | | void* key) |
1774 | | { |
1775 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1776 | | int devId = INVALID_DEVID; |
1777 | | CryptoCb* dev; |
1778 | | |
1779 | | if (key == NULL) |
1780 | | return ret; |
1781 | | |
1782 | | /* get devId */ |
1783 | | devId = wc_CryptoCb_PqcSigGetDevId(type, key); |
1784 | | if (devId == INVALID_DEVID) |
1785 | | return ret; |
1786 | | |
1787 | | /* locate registered callback */ |
1788 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1789 | | if (dev && dev->cb) { |
1790 | | wc_CryptoInfo cryptoInfo; |
1791 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1792 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1793 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_SIG_KEYGEN; |
1794 | | cryptoInfo.pk.pqc_sig_kg.rng = rng; |
1795 | | cryptoInfo.pk.pqc_sig_kg.size = keySize; |
1796 | | cryptoInfo.pk.pqc_sig_kg.key = key; |
1797 | | cryptoInfo.pk.pqc_sig_kg.type = type; |
1798 | | |
1799 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1800 | | } |
1801 | | |
1802 | | return wc_CryptoCb_TranslateErrorCode(ret); |
1803 | | } |
1804 | | |
1805 | | int wc_CryptoCb_PqcSign(const byte* in, word32 inlen, byte* out, word32 *outlen, |
1806 | | const byte* context, byte contextLen, word32 preHashType, WC_RNG* rng, |
1807 | | int type, void* key) |
1808 | | { |
1809 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1810 | | int devId = INVALID_DEVID; |
1811 | | CryptoCb* dev; |
1812 | | |
1813 | | if (key == NULL) |
1814 | | return ret; |
1815 | | |
1816 | | /* get devId */ |
1817 | | devId = wc_CryptoCb_PqcSigGetDevId(type, key); |
1818 | | if (devId == INVALID_DEVID) |
1819 | | return ret; |
1820 | | |
1821 | | /* locate registered callback */ |
1822 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1823 | | if (dev && dev->cb) { |
1824 | | wc_CryptoInfo cryptoInfo; |
1825 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1826 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1827 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_SIG_SIGN; |
1828 | | cryptoInfo.pk.pqc_sign.in = in; |
1829 | | cryptoInfo.pk.pqc_sign.inlen = inlen; |
1830 | | cryptoInfo.pk.pqc_sign.out = out; |
1831 | | cryptoInfo.pk.pqc_sign.outlen = outlen; |
1832 | | cryptoInfo.pk.pqc_sign.context = context; |
1833 | | cryptoInfo.pk.pqc_sign.contextLen = contextLen; |
1834 | | cryptoInfo.pk.pqc_sign.preHashType = preHashType; |
1835 | | cryptoInfo.pk.pqc_sign.rng = rng; |
1836 | | cryptoInfo.pk.pqc_sign.key = key; |
1837 | | cryptoInfo.pk.pqc_sign.type = type; |
1838 | | |
1839 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1840 | | } |
1841 | | |
1842 | | return wc_CryptoCb_TranslateErrorCode(ret); |
1843 | | } |
1844 | | |
1845 | | int wc_CryptoCb_PqcVerify(const byte* sig, word32 siglen, const byte* msg, |
1846 | | word32 msglen, const byte* context, byte contextLen, word32 preHashType, |
1847 | | int* res, int type, void* key) |
1848 | | { |
1849 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1850 | | int devId = INVALID_DEVID; |
1851 | | CryptoCb* dev; |
1852 | | |
1853 | | if (key == NULL) |
1854 | | return ret; |
1855 | | |
1856 | | /* get devId */ |
1857 | | devId = wc_CryptoCb_PqcSigGetDevId(type, key); |
1858 | | if (devId == INVALID_DEVID) |
1859 | | return ret; |
1860 | | |
1861 | | /* locate registered callback */ |
1862 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1863 | | if (dev && dev->cb) { |
1864 | | wc_CryptoInfo cryptoInfo; |
1865 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1866 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1867 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_SIG_VERIFY; |
1868 | | cryptoInfo.pk.pqc_verify.sig = sig; |
1869 | | cryptoInfo.pk.pqc_verify.siglen = siglen; |
1870 | | cryptoInfo.pk.pqc_verify.msg = msg; |
1871 | | cryptoInfo.pk.pqc_verify.msglen = msglen; |
1872 | | cryptoInfo.pk.pqc_verify.context = context; |
1873 | | cryptoInfo.pk.pqc_verify.contextLen = contextLen; |
1874 | | cryptoInfo.pk.pqc_verify.preHashType = preHashType; |
1875 | | cryptoInfo.pk.pqc_verify.res = res; |
1876 | | cryptoInfo.pk.pqc_verify.key = key; |
1877 | | cryptoInfo.pk.pqc_verify.type = type; |
1878 | | |
1879 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1880 | | } |
1881 | | |
1882 | | return wc_CryptoCb_TranslateErrorCode(ret); |
1883 | | } |
1884 | | |
1885 | | int wc_CryptoCb_PqcSignatureCheckPrivKey(void* key, int type, |
1886 | | const byte* pubKey, word32 pubKeySz) |
1887 | | { |
1888 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1889 | | int devId = INVALID_DEVID; |
1890 | | CryptoCb* dev; |
1891 | | |
1892 | | if (key == NULL) |
1893 | | return ret; |
1894 | | |
1895 | | /* get devId */ |
1896 | | devId = wc_CryptoCb_PqcSigGetDevId(type, key); |
1897 | | if (devId == INVALID_DEVID) |
1898 | | return ret; |
1899 | | |
1900 | | /* locate registered callback */ |
1901 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1902 | | if (dev && dev->cb) { |
1903 | | wc_CryptoInfo cryptoInfo; |
1904 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1905 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1906 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_SIG_CHECK_PRIV_KEY; |
1907 | | cryptoInfo.pk.pqc_sig_check.key = key; |
1908 | | cryptoInfo.pk.pqc_sig_check.pubKey = pubKey; |
1909 | | cryptoInfo.pk.pqc_sig_check.pubKeySz = pubKeySz; |
1910 | | cryptoInfo.pk.pqc_sig_check.type = type; |
1911 | | |
1912 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1913 | | } |
1914 | | |
1915 | | return wc_CryptoCb_TranslateErrorCode(ret); |
1916 | | } |
1917 | | #endif /* HAVE_FALCON || WOLFSSL_HAVE_MLDSA || WOLFSSL_HAVE_SLHDSA */ |
1918 | | |
1919 | | #ifndef NO_AES |
1920 | | #ifdef HAVE_AESGCM |
1921 | | int wc_CryptoCb_AesGcmEncrypt(Aes* aes, byte* out, |
1922 | | const byte* in, word32 sz, |
1923 | | const byte* iv, word32 ivSz, |
1924 | | byte* authTag, word32 authTagSz, |
1925 | | const byte* authIn, word32 authInSz) |
1926 | 0 | { |
1927 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1928 | 0 | CryptoCb* dev; |
1929 | | |
1930 | | /* locate registered callback */ |
1931 | 0 | if (aes) { |
1932 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
1933 | 0 | } |
1934 | 0 | else { |
1935 | | /* locate first callback and try using it */ |
1936 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1937 | 0 | } |
1938 | |
|
1939 | 0 | if (dev && dev->cb) { |
1940 | 0 | wc_CryptoInfo cryptoInfo; |
1941 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1942 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
1943 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_GCM; |
1944 | 0 | cryptoInfo.cipher.enc = 1; |
1945 | 0 | cryptoInfo.cipher.aesgcm_enc.aes = aes; |
1946 | 0 | cryptoInfo.cipher.aesgcm_enc.out = out; |
1947 | 0 | cryptoInfo.cipher.aesgcm_enc.in = in; |
1948 | 0 | cryptoInfo.cipher.aesgcm_enc.sz = sz; |
1949 | 0 | cryptoInfo.cipher.aesgcm_enc.iv = iv; |
1950 | 0 | cryptoInfo.cipher.aesgcm_enc.ivSz = ivSz; |
1951 | 0 | cryptoInfo.cipher.aesgcm_enc.authTag = authTag; |
1952 | 0 | cryptoInfo.cipher.aesgcm_enc.authTagSz = authTagSz; |
1953 | 0 | cryptoInfo.cipher.aesgcm_enc.authIn = authIn; |
1954 | 0 | cryptoInfo.cipher.aesgcm_enc.authInSz = authInSz; |
1955 | |
|
1956 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1957 | 0 | } |
1958 | |
|
1959 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1960 | 0 | } |
1961 | | |
1962 | | int wc_CryptoCb_AesGcmDecrypt(Aes* aes, byte* out, |
1963 | | const byte* in, word32 sz, |
1964 | | const byte* iv, word32 ivSz, |
1965 | | const byte* authTag, word32 authTagSz, |
1966 | | const byte* authIn, word32 authInSz) |
1967 | 0 | { |
1968 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1969 | 0 | CryptoCb* dev; |
1970 | | |
1971 | | /* locate registered callback */ |
1972 | 0 | if (aes) { |
1973 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
1974 | 0 | } |
1975 | 0 | else { |
1976 | | /* locate first callback and try using it */ |
1977 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1978 | 0 | } |
1979 | |
|
1980 | 0 | if (dev && dev->cb) { |
1981 | 0 | wc_CryptoInfo cryptoInfo; |
1982 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1983 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
1984 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_GCM; |
1985 | 0 | cryptoInfo.cipher.enc = 0; |
1986 | 0 | cryptoInfo.cipher.aesgcm_dec.aes = aes; |
1987 | 0 | cryptoInfo.cipher.aesgcm_dec.out = out; |
1988 | 0 | cryptoInfo.cipher.aesgcm_dec.in = in; |
1989 | 0 | cryptoInfo.cipher.aesgcm_dec.sz = sz; |
1990 | 0 | cryptoInfo.cipher.aesgcm_dec.iv = iv; |
1991 | 0 | cryptoInfo.cipher.aesgcm_dec.ivSz = ivSz; |
1992 | 0 | cryptoInfo.cipher.aesgcm_dec.authTag = authTag; |
1993 | 0 | cryptoInfo.cipher.aesgcm_dec.authTagSz = authTagSz; |
1994 | 0 | cryptoInfo.cipher.aesgcm_dec.authIn = authIn; |
1995 | 0 | cryptoInfo.cipher.aesgcm_dec.authInSz = authInSz; |
1996 | |
|
1997 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1998 | 0 | } |
1999 | |
|
2000 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2001 | 0 | } |
2002 | | #endif /* HAVE_AESGCM */ |
2003 | | |
2004 | | #ifdef HAVE_AESCCM |
2005 | | int wc_CryptoCb_AesCcmEncrypt(Aes* aes, byte* out, |
2006 | | const byte* in, word32 sz, |
2007 | | const byte* nonce, word32 nonceSz, |
2008 | | byte* authTag, word32 authTagSz, |
2009 | | const byte* authIn, word32 authInSz) |
2010 | 0 | { |
2011 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2012 | 0 | CryptoCb* dev; |
2013 | | |
2014 | | /* locate registered callback */ |
2015 | 0 | if (aes) { |
2016 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2017 | 0 | } |
2018 | 0 | else { |
2019 | | /* locate first callback and try using it */ |
2020 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2021 | 0 | } |
2022 | |
|
2023 | 0 | if (dev && dev->cb) { |
2024 | 0 | wc_CryptoInfo cryptoInfo; |
2025 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2026 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2027 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CCM; |
2028 | 0 | cryptoInfo.cipher.enc = 1; |
2029 | 0 | cryptoInfo.cipher.aesccm_enc.aes = aes; |
2030 | 0 | cryptoInfo.cipher.aesccm_enc.out = out; |
2031 | 0 | cryptoInfo.cipher.aesccm_enc.in = in; |
2032 | 0 | cryptoInfo.cipher.aesccm_enc.sz = sz; |
2033 | 0 | cryptoInfo.cipher.aesccm_enc.nonce = nonce; |
2034 | 0 | cryptoInfo.cipher.aesccm_enc.nonceSz = nonceSz; |
2035 | 0 | cryptoInfo.cipher.aesccm_enc.authTag = authTag; |
2036 | 0 | cryptoInfo.cipher.aesccm_enc.authTagSz = authTagSz; |
2037 | 0 | cryptoInfo.cipher.aesccm_enc.authIn = authIn; |
2038 | 0 | cryptoInfo.cipher.aesccm_enc.authInSz = authInSz; |
2039 | |
|
2040 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2041 | 0 | } |
2042 | |
|
2043 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2044 | 0 | } |
2045 | | |
2046 | | int wc_CryptoCb_AesCcmDecrypt(Aes* aes, byte* out, |
2047 | | const byte* in, word32 sz, |
2048 | | const byte* nonce, word32 nonceSz, |
2049 | | const byte* authTag, word32 authTagSz, |
2050 | | const byte* authIn, word32 authInSz) |
2051 | 0 | { |
2052 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2053 | 0 | CryptoCb* dev; |
2054 | | |
2055 | | /* locate registered callback */ |
2056 | 0 | if (aes) { |
2057 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2058 | 0 | } |
2059 | 0 | else { |
2060 | | /* locate first callback and try using it */ |
2061 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2062 | 0 | } |
2063 | |
|
2064 | 0 | if (dev && dev->cb) { |
2065 | 0 | wc_CryptoInfo cryptoInfo; |
2066 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2067 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2068 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CCM; |
2069 | 0 | cryptoInfo.cipher.enc = 0; |
2070 | 0 | cryptoInfo.cipher.aesccm_dec.aes = aes; |
2071 | 0 | cryptoInfo.cipher.aesccm_dec.out = out; |
2072 | 0 | cryptoInfo.cipher.aesccm_dec.in = in; |
2073 | 0 | cryptoInfo.cipher.aesccm_dec.sz = sz; |
2074 | 0 | cryptoInfo.cipher.aesccm_dec.nonce = nonce; |
2075 | 0 | cryptoInfo.cipher.aesccm_dec.nonceSz = nonceSz; |
2076 | 0 | cryptoInfo.cipher.aesccm_dec.authTag = authTag; |
2077 | 0 | cryptoInfo.cipher.aesccm_dec.authTagSz = authTagSz; |
2078 | 0 | cryptoInfo.cipher.aesccm_dec.authIn = authIn; |
2079 | 0 | cryptoInfo.cipher.aesccm_dec.authInSz = authInSz; |
2080 | |
|
2081 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2082 | 0 | } |
2083 | |
|
2084 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2085 | 0 | } |
2086 | | #endif /* HAVE_AESCCM */ |
2087 | | |
2088 | | #ifdef HAVE_AES_CBC |
2089 | | int wc_CryptoCb_AesCbcEncrypt(Aes* aes, byte* out, |
2090 | | const byte* in, word32 sz) |
2091 | 0 | { |
2092 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2093 | 0 | CryptoCb* dev; |
2094 | | |
2095 | | /* locate registered callback */ |
2096 | 0 | if (aes) { |
2097 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2098 | 0 | } |
2099 | 0 | else { |
2100 | | /* locate first callback and try using it */ |
2101 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2102 | 0 | } |
2103 | |
|
2104 | 0 | if (dev && dev->cb) { |
2105 | 0 | wc_CryptoInfo cryptoInfo; |
2106 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2107 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2108 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CBC; |
2109 | 0 | cryptoInfo.cipher.enc = 1; |
2110 | 0 | cryptoInfo.cipher.aescbc.aes = aes; |
2111 | 0 | cryptoInfo.cipher.aescbc.out = out; |
2112 | 0 | cryptoInfo.cipher.aescbc.in = in; |
2113 | 0 | cryptoInfo.cipher.aescbc.sz = sz; |
2114 | |
|
2115 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2116 | 0 | } |
2117 | |
|
2118 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2119 | 0 | } |
2120 | | |
2121 | | int wc_CryptoCb_AesCbcDecrypt(Aes* aes, byte* out, |
2122 | | const byte* in, word32 sz) |
2123 | 0 | { |
2124 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2125 | 0 | CryptoCb* dev; |
2126 | | |
2127 | | /* locate registered callback */ |
2128 | 0 | if (aes) { |
2129 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2130 | 0 | } |
2131 | 0 | else { |
2132 | | /* locate first callback and try using it */ |
2133 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2134 | 0 | } |
2135 | |
|
2136 | 0 | if (dev && dev->cb) { |
2137 | 0 | wc_CryptoInfo cryptoInfo; |
2138 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2139 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2140 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CBC; |
2141 | 0 | cryptoInfo.cipher.enc = 0; |
2142 | 0 | cryptoInfo.cipher.aescbc.aes = aes; |
2143 | 0 | cryptoInfo.cipher.aescbc.out = out; |
2144 | 0 | cryptoInfo.cipher.aescbc.in = in; |
2145 | 0 | cryptoInfo.cipher.aescbc.sz = sz; |
2146 | |
|
2147 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2148 | 0 | } |
2149 | |
|
2150 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2151 | 0 | } |
2152 | | #endif /* HAVE_AES_CBC */ |
2153 | | #ifdef WOLFSSL_AES_COUNTER |
2154 | | int wc_CryptoCb_AesCtrEncrypt(Aes* aes, byte* out, |
2155 | | const byte* in, word32 sz) |
2156 | 0 | { |
2157 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2158 | 0 | CryptoCb* dev; |
2159 | | |
2160 | | /* locate registered callback */ |
2161 | 0 | if (aes) { |
2162 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2163 | 0 | } |
2164 | 0 | else { |
2165 | | /* locate first callback and try using it */ |
2166 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2167 | 0 | } |
2168 | |
|
2169 | 0 | if (dev && dev->cb) { |
2170 | 0 | wc_CryptoInfo cryptoInfo; |
2171 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2172 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2173 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CTR; |
2174 | 0 | cryptoInfo.cipher.enc = 1; |
2175 | 0 | cryptoInfo.cipher.aesctr.aes = aes; |
2176 | 0 | cryptoInfo.cipher.aesctr.out = out; |
2177 | 0 | cryptoInfo.cipher.aesctr.in = in; |
2178 | 0 | cryptoInfo.cipher.aesctr.sz = sz; |
2179 | |
|
2180 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2181 | 0 | } |
2182 | |
|
2183 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2184 | 0 | } |
2185 | | #endif /* WOLFSSL_AES_COUNTER */ |
2186 | | #ifdef WOLFSSL_AES_CFB |
2187 | | int wc_CryptoCb_AesCfbEncrypt(Aes* aes, byte* out, |
2188 | | const byte* in, word32 sz) |
2189 | 0 | { |
2190 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2191 | 0 | CryptoCb* dev; |
2192 | | |
2193 | | /* locate registered callback */ |
2194 | 0 | if (aes) { |
2195 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2196 | 0 | } |
2197 | 0 | else { |
2198 | | /* locate first callback and try using it */ |
2199 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2200 | 0 | } |
2201 | |
|
2202 | 0 | if (dev && dev->cb) { |
2203 | 0 | wc_CryptoInfo cryptoInfo; |
2204 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2205 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2206 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CFB; |
2207 | 0 | cryptoInfo.cipher.enc = 1; |
2208 | 0 | cryptoInfo.cipher.aescfb.aes = aes; |
2209 | 0 | cryptoInfo.cipher.aescfb.out = out; |
2210 | 0 | cryptoInfo.cipher.aescfb.in = in; |
2211 | 0 | cryptoInfo.cipher.aescfb.sz = sz; |
2212 | |
|
2213 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2214 | 0 | } |
2215 | |
|
2216 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2217 | 0 | } |
2218 | | |
2219 | | int wc_CryptoCb_AesCfbDecrypt(Aes* aes, byte* out, |
2220 | | const byte* in, word32 sz) |
2221 | 0 | { |
2222 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2223 | 0 | CryptoCb* dev; |
2224 | | |
2225 | | /* locate registered callback */ |
2226 | 0 | if (aes) { |
2227 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2228 | 0 | } |
2229 | 0 | else { |
2230 | | /* locate first callback and try using it */ |
2231 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2232 | 0 | } |
2233 | |
|
2234 | 0 | if (dev && dev->cb) { |
2235 | 0 | wc_CryptoInfo cryptoInfo; |
2236 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2237 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2238 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CFB; |
2239 | 0 | cryptoInfo.cipher.enc = 0; |
2240 | 0 | cryptoInfo.cipher.aescfb.aes = aes; |
2241 | 0 | cryptoInfo.cipher.aescfb.out = out; |
2242 | 0 | cryptoInfo.cipher.aescfb.in = in; |
2243 | 0 | cryptoInfo.cipher.aescfb.sz = sz; |
2244 | |
|
2245 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2246 | 0 | } |
2247 | |
|
2248 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2249 | 0 | } |
2250 | | #endif /* WOLFSSL_AES_CFB */ |
2251 | | #ifdef WOLFSSL_AES_OFB |
2252 | | int wc_CryptoCb_AesOfbEncrypt(Aes* aes, byte* out, |
2253 | | const byte* in, word32 sz) |
2254 | 0 | { |
2255 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2256 | 0 | CryptoCb* dev; |
2257 | | |
2258 | | /* locate registered callback */ |
2259 | 0 | if (aes) { |
2260 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2261 | 0 | } |
2262 | 0 | else { |
2263 | | /* locate first callback and try using it */ |
2264 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2265 | 0 | } |
2266 | |
|
2267 | 0 | if (dev && dev->cb) { |
2268 | 0 | wc_CryptoInfo cryptoInfo; |
2269 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2270 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2271 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_OFB; |
2272 | 0 | cryptoInfo.cipher.enc = 1; |
2273 | 0 | cryptoInfo.cipher.aesofb.aes = aes; |
2274 | 0 | cryptoInfo.cipher.aesofb.out = out; |
2275 | 0 | cryptoInfo.cipher.aesofb.in = in; |
2276 | 0 | cryptoInfo.cipher.aesofb.sz = sz; |
2277 | |
|
2278 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2279 | 0 | } |
2280 | |
|
2281 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2282 | 0 | } |
2283 | | |
2284 | | int wc_CryptoCb_AesOfbDecrypt(Aes* aes, byte* out, |
2285 | | const byte* in, word32 sz) |
2286 | 0 | { |
2287 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2288 | 0 | CryptoCb* dev; |
2289 | | |
2290 | | /* locate registered callback */ |
2291 | 0 | if (aes) { |
2292 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2293 | 0 | } |
2294 | 0 | else { |
2295 | | /* locate first callback and try using it */ |
2296 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2297 | 0 | } |
2298 | |
|
2299 | 0 | if (dev && dev->cb) { |
2300 | 0 | wc_CryptoInfo cryptoInfo; |
2301 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2302 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2303 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_OFB; |
2304 | 0 | cryptoInfo.cipher.enc = 0; |
2305 | 0 | cryptoInfo.cipher.aesofb.aes = aes; |
2306 | 0 | cryptoInfo.cipher.aesofb.out = out; |
2307 | 0 | cryptoInfo.cipher.aesofb.in = in; |
2308 | 0 | cryptoInfo.cipher.aesofb.sz = sz; |
2309 | |
|
2310 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2311 | 0 | } |
2312 | |
|
2313 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2314 | 0 | } |
2315 | | #endif /* WOLFSSL_AES_OFB */ |
2316 | | #if defined(HAVE_AES_ECB) || defined(WOLFSSL_AES_DIRECT) || \ |
2317 | | defined(WOLF_CRYPTO_CB_ONLY_AES) |
2318 | | int wc_CryptoCb_AesEcbEncrypt(Aes* aes, byte* out, |
2319 | | const byte* in, word32 sz) |
2320 | 119 | { |
2321 | 119 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2322 | 119 | CryptoCb* dev; |
2323 | | |
2324 | | /* locate registered callback */ |
2325 | 119 | if (aes) { |
2326 | 119 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2327 | 119 | } |
2328 | 0 | else { |
2329 | | /* locate first callback and try using it */ |
2330 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2331 | 0 | } |
2332 | | |
2333 | 119 | if (dev && dev->cb) { |
2334 | 0 | wc_CryptoInfo cryptoInfo; |
2335 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2336 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2337 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_ECB; |
2338 | 0 | cryptoInfo.cipher.enc = 1; |
2339 | 0 | cryptoInfo.cipher.aesecb.aes = aes; |
2340 | 0 | cryptoInfo.cipher.aesecb.out = out; |
2341 | 0 | cryptoInfo.cipher.aesecb.in = in; |
2342 | 0 | cryptoInfo.cipher.aesecb.sz = sz; |
2343 | |
|
2344 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2345 | 0 | } |
2346 | | |
2347 | 119 | return wc_CryptoCb_TranslateErrorCode(ret); |
2348 | 119 | } |
2349 | | |
2350 | | int wc_CryptoCb_AesEcbDecrypt(Aes* aes, byte* out, |
2351 | | const byte* in, word32 sz) |
2352 | 248 | { |
2353 | 248 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2354 | 248 | CryptoCb* dev; |
2355 | | |
2356 | | /* locate registered callback */ |
2357 | 248 | if (aes) { |
2358 | 248 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2359 | 248 | } |
2360 | 0 | else { |
2361 | | /* locate first callback and try using it */ |
2362 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2363 | 0 | } |
2364 | | |
2365 | 248 | if (dev && dev->cb) { |
2366 | 0 | wc_CryptoInfo cryptoInfo; |
2367 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2368 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2369 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_ECB; |
2370 | 0 | cryptoInfo.cipher.enc = 0; |
2371 | 0 | cryptoInfo.cipher.aesecb.aes = aes; |
2372 | 0 | cryptoInfo.cipher.aesecb.out = out; |
2373 | 0 | cryptoInfo.cipher.aesecb.in = in; |
2374 | 0 | cryptoInfo.cipher.aesecb.sz = sz; |
2375 | |
|
2376 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2377 | 0 | } |
2378 | | |
2379 | 248 | return wc_CryptoCb_TranslateErrorCode(ret); |
2380 | 248 | } |
2381 | | #endif /* HAVE_AES_ECB || WOLFSSL_AES_DIRECT || WOLF_CRYPTO_CB_ONLY_AES */ |
2382 | | |
2383 | | #ifdef HAVE_AES_KEYWRAP |
2384 | | /* On success returns the number of output bytes produced (the callback reports |
2385 | | * it via aeskeywrap.outResSz), otherwise a negative error or |
2386 | | * CRYPTOCB_UNAVAILABLE. pad selects RFC 5649 (1) vs RFC 3394 (0). */ |
2387 | | int wc_CryptoCb_AesKeyWrap(Aes* aes, const byte* in, word32 inSz, byte* out, |
2388 | | word32 outSz, const byte* iv, int pad) |
2389 | 0 | { |
2390 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2391 | 0 | CryptoCb* dev; |
2392 | | |
2393 | | /* locate registered callback */ |
2394 | 0 | if (aes) { |
2395 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2396 | 0 | } |
2397 | 0 | else { |
2398 | | /* locate first callback and try using it */ |
2399 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2400 | 0 | } |
2401 | |
|
2402 | 0 | if (dev && dev->cb) { |
2403 | 0 | wc_CryptoInfo cryptoInfo; |
2404 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2405 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2406 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_KEYWRAP; |
2407 | 0 | cryptoInfo.cipher.enc = 1; |
2408 | 0 | cryptoInfo.cipher.aeskeywrap.aes = aes; |
2409 | 0 | cryptoInfo.cipher.aeskeywrap.in = in; |
2410 | 0 | cryptoInfo.cipher.aeskeywrap.inSz = inSz; |
2411 | 0 | cryptoInfo.cipher.aeskeywrap.out = out; |
2412 | 0 | cryptoInfo.cipher.aeskeywrap.outSz = outSz; |
2413 | 0 | cryptoInfo.cipher.aeskeywrap.iv = iv; |
2414 | 0 | cryptoInfo.cipher.aeskeywrap.pad = pad; |
2415 | |
|
2416 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2417 | 0 | if (ret == 0) { |
2418 | | /* device reports the wrapped length */ |
2419 | 0 | if (cryptoInfo.cipher.aeskeywrap.outResSz > outSz) { |
2420 | 0 | return BUFFER_E; |
2421 | 0 | } |
2422 | 0 | return (int)cryptoInfo.cipher.aeskeywrap.outResSz; |
2423 | 0 | } |
2424 | 0 | } |
2425 | | |
2426 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2427 | 0 | } |
2428 | | |
2429 | | int wc_CryptoCb_AesKeyUnWrap(Aes* aes, const byte* in, word32 inSz, byte* out, |
2430 | | word32 outSz, const byte* iv, int pad) |
2431 | 0 | { |
2432 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2433 | 0 | CryptoCb* dev; |
2434 | | |
2435 | | /* locate registered callback */ |
2436 | 0 | if (aes) { |
2437 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2438 | 0 | } |
2439 | 0 | else { |
2440 | | /* locate first callback and try using it */ |
2441 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2442 | 0 | } |
2443 | |
|
2444 | 0 | if (dev && dev->cb) { |
2445 | 0 | wc_CryptoInfo cryptoInfo; |
2446 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2447 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2448 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_KEYWRAP; |
2449 | 0 | cryptoInfo.cipher.enc = 0; |
2450 | 0 | cryptoInfo.cipher.aeskeywrap.aes = aes; |
2451 | 0 | cryptoInfo.cipher.aeskeywrap.in = in; |
2452 | 0 | cryptoInfo.cipher.aeskeywrap.inSz = inSz; |
2453 | 0 | cryptoInfo.cipher.aeskeywrap.out = out; |
2454 | 0 | cryptoInfo.cipher.aeskeywrap.outSz = outSz; |
2455 | 0 | cryptoInfo.cipher.aeskeywrap.iv = iv; |
2456 | 0 | cryptoInfo.cipher.aeskeywrap.pad = pad; |
2457 | |
|
2458 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2459 | 0 | if (ret == 0) { |
2460 | | /* device reports the recovered length */ |
2461 | 0 | if (cryptoInfo.cipher.aeskeywrap.outResSz > outSz) { |
2462 | 0 | return BUFFER_E; |
2463 | 0 | } |
2464 | 0 | return (int)cryptoInfo.cipher.aeskeywrap.outResSz; |
2465 | 0 | } |
2466 | 0 | } |
2467 | | |
2468 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2469 | 0 | } |
2470 | | #endif /* HAVE_AES_KEYWRAP */ |
2471 | | |
2472 | | #ifdef WOLF_CRYPTO_CB_AES_SETKEY |
2473 | | int wc_CryptoCb_AesSetKey(Aes* aes, const byte* key, word32 keySz) |
2474 | | { |
2475 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2476 | | CryptoCb* dev; |
2477 | | |
2478 | | if (aes == NULL || key == NULL) |
2479 | | return BAD_FUNC_ARG; |
2480 | | |
2481 | | if (aes->devId == INVALID_DEVID) |
2482 | | return CRYPTOCB_UNAVAILABLE; |
2483 | | |
2484 | | /* locate registered callback */ |
2485 | | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2486 | | if (dev && dev->cb) { |
2487 | | wc_CryptoInfo cryptoInfo; |
2488 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2489 | | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2490 | | cryptoInfo.cipher.type = WC_CIPHER_AES; |
2491 | | cryptoInfo.cipher.aessetkey.aes = aes; |
2492 | | cryptoInfo.cipher.aessetkey.key = key; |
2493 | | cryptoInfo.cipher.aessetkey.keySz = keySz; |
2494 | | |
2495 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2496 | | } |
2497 | | |
2498 | | return wc_CryptoCb_TranslateErrorCode(ret); |
2499 | | } |
2500 | | #endif /* WOLF_CRYPTO_CB_AES_SETKEY */ |
2501 | | #endif /* !NO_AES */ |
2502 | | |
2503 | | #ifndef NO_DES3 |
2504 | | int wc_CryptoCb_Des3Encrypt(Des3* des3, byte* out, |
2505 | | const byte* in, word32 sz) |
2506 | 326 | { |
2507 | 326 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2508 | 326 | CryptoCb* dev; |
2509 | | |
2510 | | /* locate registered callback */ |
2511 | 326 | if (des3) { |
2512 | 326 | dev = wc_CryptoCb_FindDevice(des3->devId, WC_ALGO_TYPE_CIPHER); |
2513 | 326 | } |
2514 | 0 | else { |
2515 | | /* locate first callback and try using it */ |
2516 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2517 | 0 | } |
2518 | | |
2519 | 326 | if (dev && dev->cb) { |
2520 | 0 | wc_CryptoInfo cryptoInfo; |
2521 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2522 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2523 | 0 | cryptoInfo.cipher.type = WC_CIPHER_DES3; |
2524 | 0 | cryptoInfo.cipher.enc = 1; |
2525 | 0 | cryptoInfo.cipher.des3.des = des3; |
2526 | 0 | cryptoInfo.cipher.des3.out = out; |
2527 | 0 | cryptoInfo.cipher.des3.in = in; |
2528 | 0 | cryptoInfo.cipher.des3.sz = sz; |
2529 | |
|
2530 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2531 | 0 | } |
2532 | | |
2533 | 326 | return wc_CryptoCb_TranslateErrorCode(ret); |
2534 | 326 | } |
2535 | | |
2536 | | int wc_CryptoCb_Des3Decrypt(Des3* des3, byte* out, |
2537 | | const byte* in, word32 sz) |
2538 | 105 | { |
2539 | 105 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2540 | 105 | CryptoCb* dev; |
2541 | | |
2542 | | /* locate registered callback */ |
2543 | 105 | if (des3) { |
2544 | 105 | dev = wc_CryptoCb_FindDevice(des3->devId, WC_ALGO_TYPE_CIPHER); |
2545 | 105 | } |
2546 | 0 | else { |
2547 | | /* locate first callback and try using it */ |
2548 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2549 | 0 | } |
2550 | | |
2551 | 105 | if (dev && dev->cb) { |
2552 | 0 | wc_CryptoInfo cryptoInfo; |
2553 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2554 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2555 | 0 | cryptoInfo.cipher.type = WC_CIPHER_DES3; |
2556 | 0 | cryptoInfo.cipher.enc = 0; |
2557 | 0 | cryptoInfo.cipher.des3.des = des3; |
2558 | 0 | cryptoInfo.cipher.des3.out = out; |
2559 | 0 | cryptoInfo.cipher.des3.in = in; |
2560 | 0 | cryptoInfo.cipher.des3.sz = sz; |
2561 | |
|
2562 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2563 | 0 | } |
2564 | | |
2565 | 105 | return wc_CryptoCb_TranslateErrorCode(ret); |
2566 | 105 | } |
2567 | | #endif /* !NO_DES3 */ |
2568 | | |
2569 | | #ifndef NO_SHA |
2570 | | int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in, |
2571 | | word32 inSz, byte* digest) |
2572 | 0 | { |
2573 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2574 | 0 | CryptoCb* dev; |
2575 | | |
2576 | | /* locate registered callback */ |
2577 | 0 | if (sha) { |
2578 | 0 | dev = wc_CryptoCb_FindDevice(sha->devId, WC_ALGO_TYPE_HASH); |
2579 | 0 | } |
2580 | 0 | else { |
2581 | | /* locate first callback and try using it */ |
2582 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2583 | 0 | } |
2584 | |
|
2585 | 0 | if (dev && dev->cb) { |
2586 | 0 | wc_CryptoInfo cryptoInfo; |
2587 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2588 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
2589 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA; |
2590 | 0 | cryptoInfo.hash.sha1 = sha; |
2591 | 0 | cryptoInfo.hash.in = in; |
2592 | 0 | cryptoInfo.hash.inSz = inSz; |
2593 | 0 | cryptoInfo.hash.digest = digest; |
2594 | |
|
2595 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2596 | 0 | } |
2597 | |
|
2598 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2599 | 0 | } |
2600 | | #endif /* !NO_SHA */ |
2601 | | |
2602 | | #ifdef WOLFSSL_SHA224 |
2603 | | int wc_CryptoCb_Sha224Hash(wc_Sha224* sha224, const byte* in, |
2604 | | word32 inSz, byte* digest) |
2605 | 178 | { |
2606 | 178 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2607 | 178 | CryptoCb* dev; |
2608 | | |
2609 | | /* locate registered callback */ |
2610 | 178 | if (sha224) { |
2611 | 178 | dev = wc_CryptoCb_FindDevice(sha224->devId, WC_ALGO_TYPE_HASH); |
2612 | 178 | } |
2613 | 0 | else { |
2614 | | /* locate first callback and try using it */ |
2615 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2616 | 0 | } |
2617 | | |
2618 | 178 | if (dev && dev->cb) { |
2619 | 0 | wc_CryptoInfo cryptoInfo; |
2620 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2621 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
2622 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA224; |
2623 | 0 | cryptoInfo.hash.sha224 = sha224; |
2624 | 0 | cryptoInfo.hash.in = in; |
2625 | 0 | cryptoInfo.hash.inSz = inSz; |
2626 | 0 | cryptoInfo.hash.digest = digest; |
2627 | |
|
2628 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2629 | 0 | } |
2630 | | |
2631 | 178 | return wc_CryptoCb_TranslateErrorCode(ret); |
2632 | 178 | } |
2633 | | #endif /* WOLFSSL_SHA224 */ |
2634 | | |
2635 | | |
2636 | | #ifndef NO_SHA256 |
2637 | | int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in, |
2638 | | word32 inSz, byte* digest) |
2639 | 153 | { |
2640 | 153 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2641 | 153 | CryptoCb* dev; |
2642 | | |
2643 | | /* locate registered callback */ |
2644 | 153 | if (sha256) { |
2645 | 153 | dev = wc_CryptoCb_FindDevice(sha256->devId, WC_ALGO_TYPE_HASH); |
2646 | 153 | } |
2647 | 0 | else { |
2648 | | /* locate first callback and try using it */ |
2649 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2650 | 0 | } |
2651 | | |
2652 | 153 | if (dev && dev->cb) { |
2653 | 0 | wc_CryptoInfo cryptoInfo; |
2654 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2655 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
2656 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA256; |
2657 | 0 | cryptoInfo.hash.sha256 = sha256; |
2658 | 0 | cryptoInfo.hash.in = in; |
2659 | 0 | cryptoInfo.hash.inSz = inSz; |
2660 | 0 | cryptoInfo.hash.digest = digest; |
2661 | |
|
2662 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2663 | 0 | } |
2664 | | |
2665 | 153 | return wc_CryptoCb_TranslateErrorCode(ret); |
2666 | 153 | } |
2667 | | #endif /* !NO_SHA256 */ |
2668 | | |
2669 | | #ifdef WOLFSSL_SHA384 |
2670 | | int wc_CryptoCb_Sha384Hash(wc_Sha384* sha384, const byte* in, |
2671 | | word32 inSz, byte* digest) |
2672 | 4.11k | { |
2673 | 4.11k | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2674 | 4.11k | CryptoCb* dev; |
2675 | | |
2676 | | /* locate registered callback */ |
2677 | 4.11k | #ifndef NO_SHA2_CRYPTO_CB |
2678 | 4.11k | if (sha384) { |
2679 | 4.11k | dev = wc_CryptoCb_FindDevice(sha384->devId, WC_ALGO_TYPE_HASH); |
2680 | 4.11k | } |
2681 | 0 | else |
2682 | 0 | #endif |
2683 | 0 | { |
2684 | | /* locate first callback and try using it */ |
2685 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2686 | 0 | } |
2687 | | |
2688 | 4.11k | if (dev && dev->cb) { |
2689 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLF_CRYPTO_CB_NO_SHA512_FALLBACK) |
2690 | 0 | byte localHash[WC_SHA512_DIGEST_SIZE]; |
2691 | 0 | #endif |
2692 | 0 | wc_CryptoInfo cryptoInfo; |
2693 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2694 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
2695 | 0 | cryptoInfo.hash.in = in; |
2696 | 0 | cryptoInfo.hash.inSz = inSz; |
2697 | | |
2698 | | /* try the SHA-384 callback first */ |
2699 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA384; |
2700 | 0 | cryptoInfo.hash.sha384 = sha384; |
2701 | 0 | cryptoInfo.hash.digest = digest; |
2702 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2703 | 0 | ret = wc_CryptoCb_TranslateErrorCode(ret); |
2704 | 0 | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
2705 | 0 | return ret; |
2706 | | |
2707 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLF_CRYPTO_CB_NO_SHA512_FALLBACK) |
2708 | | /* fall back to the SHA-512 core: SHA-384 is the SHA-512 core with a |
2709 | | * different IV (in the caller-supplied state) and a 48-byte |
2710 | | * truncation done here */ |
2711 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA512; |
2712 | 0 | cryptoInfo.hash.sha512 = (wc_Sha512*)sha384; |
2713 | | /* use local buffer for the final digest so we can truncate */ |
2714 | 0 | if (digest != NULL) |
2715 | 0 | cryptoInfo.hash.digest = localHash; |
2716 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2717 | 0 | ret = wc_CryptoCb_TranslateErrorCode(ret); |
2718 | 0 | if (ret == 0 && digest != NULL) { |
2719 | 0 | XMEMCPY(digest, localHash, WC_SHA384_DIGEST_SIZE); |
2720 | | /* the SHA-512 callback left the SHA-512 IV in the state; write |
2721 | | * the SHA-384 IV back so the struct is ready for reuse */ |
2722 | 0 | if (sha384 != NULL) { |
2723 | 0 | sha384->digest[0] = W64LIT(0xcbbb9d5dc1059ed8); |
2724 | 0 | sha384->digest[1] = W64LIT(0x629a292a367cd507); |
2725 | 0 | sha384->digest[2] = W64LIT(0x9159015a3070dd17); |
2726 | 0 | sha384->digest[3] = W64LIT(0x152fecd8f70e5939); |
2727 | 0 | sha384->digest[4] = W64LIT(0x67332667ffc00b31); |
2728 | 0 | sha384->digest[5] = W64LIT(0x8eb44a8768581511); |
2729 | 0 | sha384->digest[6] = W64LIT(0xdb0c2e0d64f98fa7); |
2730 | 0 | sha384->digest[7] = W64LIT(0x47b5481dbefa4fa4); |
2731 | 0 | } |
2732 | 0 | } |
2733 | 0 | return ret; |
2734 | 0 | #endif /* WOLFSSL_SHA512 && !WOLF_CRYPTO_CB_NO_SHA512_FALLBACK */ |
2735 | 0 | } |
2736 | | |
2737 | 4.11k | return wc_CryptoCb_TranslateErrorCode(ret); |
2738 | 4.11k | } |
2739 | | #endif /* WOLFSSL_SHA384 */ |
2740 | | |
2741 | | #ifdef WOLFSSL_SHA512 |
2742 | | int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in, |
2743 | | word32 inSz, byte* digest |
2744 | | #if !(defined(HAVE_FIPS) && FIPS_VERSION_LT(7,0)) |
2745 | | , size_t digestSz |
2746 | | #endif |
2747 | | ) |
2748 | 6.28k | { |
2749 | 6.28k | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2750 | 6.28k | CryptoCb* dev; |
2751 | | #if defined(HAVE_FIPS) && FIPS_VERSION_LT(7,0) |
2752 | | /* Older FIPS sha512.c snapshots call the 4-arg API (no digestSz). Treat as |
2753 | | * full-size SHA-512 with no variant dispatch, matching pre-digestSz |
2754 | | * behavior. */ |
2755 | | size_t digestSz = WC_SHA512_DIGEST_SIZE; |
2756 | | #endif |
2757 | | |
2758 | | /* locate registered callback */ |
2759 | 6.28k | #ifndef NO_SHA2_CRYPTO_CB |
2760 | 6.28k | if (sha512) { |
2761 | 6.28k | dev = wc_CryptoCb_FindDevice(sha512->devId, WC_ALGO_TYPE_HASH); |
2762 | 6.28k | } |
2763 | 0 | else |
2764 | 0 | #endif |
2765 | 0 | { |
2766 | | /* locate first callback and try using it */ |
2767 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2768 | 0 | } |
2769 | | |
2770 | 6.28k | if (dev && dev->cb) { |
2771 | 580 | #ifndef WOLF_CRYPTO_CB_NO_SHA512_FALLBACK |
2772 | 580 | byte localHash[WC_SHA512_DIGEST_SIZE]; |
2773 | 580 | #endif |
2774 | 580 | wc_CryptoInfo cryptoInfo; |
2775 | 580 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2776 | 580 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
2777 | 580 | cryptoInfo.hash.sha512 = sha512; |
2778 | 580 | cryptoInfo.hash.in = in; |
2779 | 580 | cryptoInfo.hash.inSz = inSz; |
2780 | 580 | cryptoInfo.hash.digest = digest; |
2781 | | |
2782 | | /* try the specific family callbacks first */ |
2783 | 580 | #if !defined(WOLFSSL_NOSHA512_224) |
2784 | 580 | if (digest != NULL && digestSz == WC_SHA512_224_DIGEST_SIZE) { |
2785 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA512_224; |
2786 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2787 | 0 | ret = wc_CryptoCb_TranslateErrorCode(ret); |
2788 | 0 | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
2789 | 0 | return ret; |
2790 | | #ifdef WOLF_CRYPTO_CB_NO_SHA512_FALLBACK |
2791 | | return ret; |
2792 | | #endif |
2793 | 0 | } |
2794 | 580 | #endif |
2795 | 580 | #if !defined(WOLFSSL_NOSHA512_256) |
2796 | 580 | if (digest != NULL && digestSz == WC_SHA512_256_DIGEST_SIZE) { |
2797 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA512_256; |
2798 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2799 | 0 | ret = wc_CryptoCb_TranslateErrorCode(ret); |
2800 | 0 | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
2801 | 0 | return ret; |
2802 | | #ifdef WOLF_CRYPTO_CB_NO_SHA512_FALLBACK |
2803 | | return ret; |
2804 | | #endif |
2805 | 0 | } |
2806 | 580 | #endif |
2807 | 580 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA512; |
2808 | 580 | #ifndef WOLF_CRYPTO_CB_NO_SHA512_FALLBACK |
2809 | | /* use local buffer if not full size */ |
2810 | 580 | if (digest != NULL && digestSz != WC_SHA512_DIGEST_SIZE) |
2811 | 0 | cryptoInfo.hash.digest = localHash; |
2812 | 580 | #endif |
2813 | 580 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2814 | 580 | ret = wc_CryptoCb_TranslateErrorCode(ret); |
2815 | 580 | #ifndef WOLF_CRYPTO_CB_NO_SHA512_FALLBACK |
2816 | 580 | if (ret == 0 && digest != NULL && digestSz != WC_SHA512_DIGEST_SIZE) { |
2817 | 0 | XMEMCPY(digest, localHash, digestSz); |
2818 | 0 | #if !defined(WOLFSSL_NOSHA512_224) |
2819 | 0 | if (sha512 != NULL && digestSz == WC_SHA512_224_DIGEST_SIZE) { |
2820 | 0 | sha512->digest[0] = W64LIT(0x8c3d37c819544da2); |
2821 | 0 | sha512->digest[1] = W64LIT(0x73e1996689dcd4d6); |
2822 | 0 | sha512->digest[2] = W64LIT(0x1dfab7ae32ff9c82); |
2823 | 0 | sha512->digest[3] = W64LIT(0x679dd514582f9fcf); |
2824 | 0 | sha512->digest[4] = W64LIT(0x0f6d2b697bd44da8); |
2825 | 0 | sha512->digest[5] = W64LIT(0x77e36f7304c48942); |
2826 | 0 | sha512->digest[6] = W64LIT(0x3f9d85a86a1d36c8); |
2827 | 0 | sha512->digest[7] = W64LIT(0x1112e6ad91d692a1); |
2828 | 0 | } |
2829 | 0 | #endif |
2830 | 0 | #if !defined(WOLFSSL_NOSHA512_256) |
2831 | 0 | if (sha512 != NULL && digestSz == WC_SHA512_256_DIGEST_SIZE) { |
2832 | 0 | sha512->digest[0] = W64LIT(0x22312194fc2bf72c); |
2833 | 0 | sha512->digest[1] = W64LIT(0x9f555fa3c84c64c2); |
2834 | 0 | sha512->digest[2] = W64LIT(0x2393b86b6f53b151); |
2835 | 0 | sha512->digest[3] = W64LIT(0x963877195940eabd); |
2836 | 0 | sha512->digest[4] = W64LIT(0x96283ee2a88effe3); |
2837 | 0 | sha512->digest[5] = W64LIT(0xbe5e1e2553863992); |
2838 | 0 | sha512->digest[6] = W64LIT(0x2b0199fc2c85b8aa); |
2839 | 0 | sha512->digest[7] = W64LIT(0x0eb72ddc81c52ca2); |
2840 | 0 | } |
2841 | 0 | #endif |
2842 | 0 | } |
2843 | 580 | #endif /* !WOLF_CRYPTO_CB_NO_SHA512_FALLBACK */ |
2844 | 580 | return ret; |
2845 | 580 | } |
2846 | | |
2847 | 5.70k | return wc_CryptoCb_TranslateErrorCode(ret); |
2848 | 6.28k | } |
2849 | | #endif /* WOLFSSL_SHA512 */ |
2850 | | |
2851 | | #if defined(WOLFSSL_SHA3) && (!defined(HAVE_FIPS) || FIPS_VERSION_GE(6, 0)) |
2852 | | int wc_CryptoCb_Sha3Hash(wc_Sha3* sha3, int type, const byte* in, |
2853 | | word32 inSz, byte* digest) |
2854 | 0 | { |
2855 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2856 | 0 | CryptoCb* dev; |
2857 | | |
2858 | | /* locate registered callback */ |
2859 | 0 | if (sha3) { |
2860 | 0 | dev = wc_CryptoCb_FindDevice(sha3->devId, WC_ALGO_TYPE_HASH); |
2861 | 0 | } |
2862 | 0 | else |
2863 | 0 | { |
2864 | | /* locate first callback and try using it */ |
2865 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2866 | 0 | } |
2867 | |
|
2868 | 0 | if (dev && dev->cb) { |
2869 | 0 | wc_CryptoInfo cryptoInfo; |
2870 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2871 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
2872 | 0 | cryptoInfo.hash.type = type; |
2873 | 0 | cryptoInfo.hash.sha3 = sha3; |
2874 | 0 | cryptoInfo.hash.in = in; |
2875 | 0 | cryptoInfo.hash.inSz = inSz; |
2876 | 0 | cryptoInfo.hash.digest = digest; |
2877 | |
|
2878 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2879 | 0 | } |
2880 | |
|
2881 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2882 | 0 | } |
2883 | | |
2884 | | #if defined(WOLFSSL_SHAKE128) || defined(WOLFSSL_SHAKE256) |
2885 | | int wc_CryptoCb_Shake(wc_Sha3* shake, int type, const byte* in, |
2886 | | word32 inSz, byte* out, word32 outSz) |
2887 | 50.8k | { |
2888 | 50.8k | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2889 | 50.8k | CryptoCb* dev; |
2890 | | |
2891 | | /* locate registered callback */ |
2892 | 50.8k | if (shake) { |
2893 | 50.8k | dev = wc_CryptoCb_FindDevice(shake->devId, WC_ALGO_TYPE_HASH); |
2894 | 50.8k | } |
2895 | 0 | else { |
2896 | | /* locate first callback and try using it */ |
2897 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2898 | 0 | } |
2899 | | |
2900 | 50.8k | if (dev && dev->cb) { |
2901 | 0 | wc_CryptoInfo cryptoInfo; |
2902 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2903 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
2904 | 0 | cryptoInfo.hash.type = type; |
2905 | 0 | cryptoInfo.hash.sha3 = shake; /* wc_Shake is a wc_Sha3 */ |
2906 | 0 | cryptoInfo.hash.in = in; |
2907 | 0 | cryptoInfo.hash.inSz = inSz; |
2908 | 0 | cryptoInfo.hash.digest = out; |
2909 | 0 | cryptoInfo.hash.outSz = outSz; |
2910 | |
|
2911 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2912 | 0 | } |
2913 | | |
2914 | 50.8k | return wc_CryptoCb_TranslateErrorCode(ret); |
2915 | 50.8k | } |
2916 | | #endif /* WOLFSSL_SHAKE128 || WOLFSSL_SHAKE256 */ |
2917 | | #endif /* WOLFSSL_SHA3 && (!HAVE_FIPS || FIPS_VERSION_GE(6, 0)) */ |
2918 | | |
2919 | | #ifndef NO_HMAC |
2920 | | int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in, word32 inSz, |
2921 | | byte* digest) |
2922 | 268 | { |
2923 | 268 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2924 | 268 | CryptoCb* dev; |
2925 | | |
2926 | 268 | if (hmac == NULL) |
2927 | 0 | return ret; |
2928 | | |
2929 | | /* locate registered callback */ |
2930 | 268 | dev = wc_CryptoCb_FindDevice(hmac->devId, WC_ALGO_TYPE_HMAC); |
2931 | 268 | if (dev && dev->cb) { |
2932 | 0 | wc_CryptoInfo cryptoInfo; |
2933 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2934 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HMAC; |
2935 | 0 | cryptoInfo.hmac.macType = macType; |
2936 | 0 | cryptoInfo.hmac.in = in; |
2937 | 0 | cryptoInfo.hmac.inSz = inSz; |
2938 | 0 | cryptoInfo.hmac.digest = digest; |
2939 | 0 | cryptoInfo.hmac.hmac = hmac; |
2940 | |
|
2941 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2942 | 0 | } |
2943 | | |
2944 | 268 | return wc_CryptoCb_TranslateErrorCode(ret); |
2945 | 268 | } |
2946 | | #endif /* !NO_HMAC */ |
2947 | | |
2948 | | #ifndef WC_NO_RNG |
2949 | | int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz) |
2950 | 16.3k | { |
2951 | 16.3k | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2952 | 16.3k | CryptoCb* dev; |
2953 | | |
2954 | | /* locate registered callback */ |
2955 | 16.3k | if (rng) { |
2956 | 16.3k | dev = wc_CryptoCb_FindDevice(rng->devId, WC_ALGO_TYPE_RNG); |
2957 | 16.3k | } |
2958 | 0 | else { |
2959 | | /* locate first callback and try using it */ |
2960 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2961 | 0 | } |
2962 | | |
2963 | 16.3k | if (dev && dev->cb) { |
2964 | 16.3k | wc_CryptoInfo cryptoInfo; |
2965 | 16.3k | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2966 | 16.3k | cryptoInfo.algo_type = WC_ALGO_TYPE_RNG; |
2967 | 16.3k | cryptoInfo.rng.rng = rng; |
2968 | 16.3k | cryptoInfo.rng.out = out; |
2969 | 16.3k | cryptoInfo.rng.sz = sz; |
2970 | | |
2971 | 16.3k | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2972 | 16.3k | } |
2973 | | |
2974 | 16.3k | return wc_CryptoCb_TranslateErrorCode(ret); |
2975 | 16.3k | } |
2976 | | |
2977 | | int wc_CryptoCb_RandomSeed(OS_Seed* os, byte* seed, word32 sz) |
2978 | 10 | { |
2979 | 10 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2980 | 10 | CryptoCb* dev; |
2981 | | |
2982 | | /* locate registered callback */ |
2983 | 10 | dev = wc_CryptoCb_FindDevice(os->devId, WC_ALGO_TYPE_SEED); |
2984 | 10 | if (dev && dev->cb) { |
2985 | 10 | wc_CryptoInfo cryptoInfo; |
2986 | 10 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2987 | 10 | cryptoInfo.algo_type = WC_ALGO_TYPE_SEED; |
2988 | 10 | cryptoInfo.seed.os = os; |
2989 | 10 | cryptoInfo.seed.seed = seed; |
2990 | 10 | cryptoInfo.seed.sz = sz; |
2991 | | |
2992 | 10 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2993 | 10 | } |
2994 | | |
2995 | 10 | return wc_CryptoCb_TranslateErrorCode(ret); |
2996 | 10 | } |
2997 | | #endif /* !WC_NO_RNG */ |
2998 | | |
2999 | | #ifndef NO_CERTS |
3000 | | int wc_CryptoCb_GetCert(int devId, const char *label, word32 labelLen, |
3001 | | const byte *id, word32 idLen, byte** out, |
3002 | | word32* outSz, int *format, void *heap) |
3003 | 0 | { |
3004 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3005 | 0 | CryptoCb* dev; |
3006 | | |
3007 | | /* locate registered callback */ |
3008 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_CERT); |
3009 | 0 | if (dev && dev->cb) { |
3010 | 0 | wc_CryptoInfo cryptoInfo; |
3011 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3012 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CERT; |
3013 | 0 | cryptoInfo.cert.label = label; |
3014 | 0 | cryptoInfo.cert.labelLen = labelLen; |
3015 | 0 | cryptoInfo.cert.id = id; |
3016 | 0 | cryptoInfo.cert.idLen = idLen; |
3017 | 0 | cryptoInfo.cert.heap = heap; |
3018 | 0 | cryptoInfo.cert.certDataOut = out; |
3019 | 0 | cryptoInfo.cert.certSz = outSz; |
3020 | 0 | cryptoInfo.cert.certFormatOut = format; |
3021 | 0 | cryptoInfo.cert.heap = heap; |
3022 | |
|
3023 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3024 | 0 | } |
3025 | |
|
3026 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3027 | 0 | } |
3028 | | #endif /* ifndef NO_CERTS */ |
3029 | | |
3030 | | #if defined(WOLFSSL_CMAC) |
3031 | | int wc_CryptoCb_Cmac(Cmac* cmac, const byte* key, word32 keySz, |
3032 | | const byte* in, word32 inSz, byte* out, word32* outSz, int type, |
3033 | | void* ctx) |
3034 | 0 | { |
3035 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3036 | 0 | CryptoCb* dev; |
3037 | | |
3038 | | /* locate registered callback */ |
3039 | 0 | if (cmac) { |
3040 | 0 | dev = wc_CryptoCb_FindDevice(cmac->devId, WC_ALGO_TYPE_CMAC); |
3041 | 0 | } |
3042 | 0 | else { |
3043 | | /* locate first callback and try using it */ |
3044 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3045 | 0 | } |
3046 | 0 | if (dev && dev->cb) { |
3047 | 0 | wc_CryptoInfo cryptoInfo; |
3048 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3049 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CMAC; |
3050 | |
|
3051 | 0 | cryptoInfo.cmac.cmac = cmac; |
3052 | 0 | cryptoInfo.cmac.ctx = ctx; |
3053 | 0 | cryptoInfo.cmac.key = key; |
3054 | 0 | cryptoInfo.cmac.in = in; |
3055 | 0 | cryptoInfo.cmac.out = out; |
3056 | 0 | cryptoInfo.cmac.outSz = outSz; |
3057 | 0 | cryptoInfo.cmac.keySz = keySz; |
3058 | 0 | cryptoInfo.cmac.inSz = inSz; |
3059 | 0 | cryptoInfo.cmac.type = type; |
3060 | |
|
3061 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3062 | 0 | } |
3063 | |
|
3064 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3065 | 0 | } |
3066 | | #endif /* WOLFSSL_CMAC */ |
3067 | | |
3068 | | #ifdef WOLFSSL_SHE |
3069 | | int wc_CryptoCb_SheGetUid(wc_SHE* she, byte* uid, word32 uidSz, |
3070 | | const void* ctx) |
3071 | | { |
3072 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3073 | | CryptoCb* dev; |
3074 | | |
3075 | | if (she == NULL) { |
3076 | | return BAD_FUNC_ARG; |
3077 | | } |
3078 | | |
3079 | | /* locate registered callback */ |
3080 | | dev = wc_CryptoCb_FindDevice(she->devId, WC_ALGO_TYPE_SHE); |
3081 | | if (dev && dev->cb) { |
3082 | | wc_CryptoInfo cryptoInfo; |
3083 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3084 | | cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; |
3085 | | cryptoInfo.she.she = she; |
3086 | | cryptoInfo.she.type = WC_SHE_GET_UID; |
3087 | | cryptoInfo.she.ctx = ctx; |
3088 | | cryptoInfo.she.op.getUid.uid = uid; |
3089 | | cryptoInfo.she.op.getUid.uidSz = uidSz; |
3090 | | |
3091 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3092 | | } |
3093 | | |
3094 | | return wc_CryptoCb_TranslateErrorCode(ret); |
3095 | | } |
3096 | | |
3097 | | int wc_CryptoCb_SheGetCounter(wc_SHE* she, word32* counter, const void* ctx) |
3098 | | { |
3099 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3100 | | CryptoCb* dev; |
3101 | | |
3102 | | if (she == NULL || counter == NULL) { |
3103 | | return BAD_FUNC_ARG; |
3104 | | } |
3105 | | |
3106 | | dev = wc_CryptoCb_FindDevice(she->devId, WC_ALGO_TYPE_SHE); |
3107 | | if (dev && dev->cb) { |
3108 | | wc_CryptoInfo cryptoInfo; |
3109 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3110 | | cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; |
3111 | | cryptoInfo.she.she = she; |
3112 | | cryptoInfo.she.type = WC_SHE_GET_COUNTER; |
3113 | | cryptoInfo.she.ctx = ctx; |
3114 | | cryptoInfo.she.op.getCounter.counter = counter; |
3115 | | |
3116 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3117 | | } |
3118 | | |
3119 | | return wc_CryptoCb_TranslateErrorCode(ret); |
3120 | | } |
3121 | | |
3122 | | int wc_CryptoCb_SheGenerateM1M2M3(wc_SHE* she, |
3123 | | const byte* uid, word32 uidSz, |
3124 | | byte authKeyId, const byte* authKey, word32 authKeySz, |
3125 | | byte targetKeyId, const byte* newKey, word32 newKeySz, |
3126 | | word32 counter, byte flags, |
3127 | | byte* m1, word32 m1Sz, |
3128 | | byte* m2, word32 m2Sz, |
3129 | | byte* m3, word32 m3Sz) |
3130 | | { |
3131 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3132 | | CryptoCb* dev; |
3133 | | |
3134 | | if (she == NULL) { |
3135 | | return BAD_FUNC_ARG; |
3136 | | } |
3137 | | |
3138 | | dev = wc_CryptoCb_FindDevice(she->devId, WC_ALGO_TYPE_SHE); |
3139 | | if (dev && dev->cb) { |
3140 | | wc_CryptoInfo cryptoInfo; |
3141 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3142 | | cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; |
3143 | | cryptoInfo.she.she = she; |
3144 | | cryptoInfo.she.type = WC_SHE_GENERATE_M1M2M3; |
3145 | | cryptoInfo.she.op.generateM1M2M3.uid = uid; |
3146 | | cryptoInfo.she.op.generateM1M2M3.uidSz = uidSz; |
3147 | | cryptoInfo.she.op.generateM1M2M3.authKeyId = authKeyId; |
3148 | | cryptoInfo.she.op.generateM1M2M3.authKey = authKey; |
3149 | | cryptoInfo.she.op.generateM1M2M3.authKeySz = authKeySz; |
3150 | | cryptoInfo.she.op.generateM1M2M3.targetKeyId = targetKeyId; |
3151 | | cryptoInfo.she.op.generateM1M2M3.newKey = newKey; |
3152 | | cryptoInfo.she.op.generateM1M2M3.newKeySz = newKeySz; |
3153 | | cryptoInfo.she.op.generateM1M2M3.counter = counter; |
3154 | | cryptoInfo.she.op.generateM1M2M3.flags = flags; |
3155 | | cryptoInfo.she.op.generateM1M2M3.m1 = m1; |
3156 | | cryptoInfo.she.op.generateM1M2M3.m1Sz = m1Sz; |
3157 | | cryptoInfo.she.op.generateM1M2M3.m2 = m2; |
3158 | | cryptoInfo.she.op.generateM1M2M3.m2Sz = m2Sz; |
3159 | | cryptoInfo.she.op.generateM1M2M3.m3 = m3; |
3160 | | cryptoInfo.she.op.generateM1M2M3.m3Sz = m3Sz; |
3161 | | |
3162 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3163 | | } |
3164 | | |
3165 | | return wc_CryptoCb_TranslateErrorCode(ret); |
3166 | | } |
3167 | | |
3168 | | int wc_CryptoCb_SheGenerateM4M5(wc_SHE* she, |
3169 | | const byte* uid, word32 uidSz, |
3170 | | byte authKeyId, byte targetKeyId, |
3171 | | const byte* newKey, word32 newKeySz, |
3172 | | word32 counter, |
3173 | | byte* m4, word32 m4Sz, |
3174 | | byte* m5, word32 m5Sz) |
3175 | | { |
3176 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3177 | | CryptoCb* dev; |
3178 | | |
3179 | | if (she == NULL) { |
3180 | | return BAD_FUNC_ARG; |
3181 | | } |
3182 | | |
3183 | | dev = wc_CryptoCb_FindDevice(she->devId, WC_ALGO_TYPE_SHE); |
3184 | | if (dev && dev->cb) { |
3185 | | wc_CryptoInfo cryptoInfo; |
3186 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3187 | | cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; |
3188 | | cryptoInfo.she.she = she; |
3189 | | cryptoInfo.she.type = WC_SHE_GENERATE_M4M5; |
3190 | | cryptoInfo.she.op.generateM4M5.uid = uid; |
3191 | | cryptoInfo.she.op.generateM4M5.uidSz = uidSz; |
3192 | | cryptoInfo.she.op.generateM4M5.authKeyId = authKeyId; |
3193 | | cryptoInfo.she.op.generateM4M5.targetKeyId = targetKeyId; |
3194 | | cryptoInfo.she.op.generateM4M5.newKey = newKey; |
3195 | | cryptoInfo.she.op.generateM4M5.newKeySz = newKeySz; |
3196 | | cryptoInfo.she.op.generateM4M5.counter = counter; |
3197 | | cryptoInfo.she.op.generateM4M5.m4 = m4; |
3198 | | cryptoInfo.she.op.generateM4M5.m4Sz = m4Sz; |
3199 | | cryptoInfo.she.op.generateM4M5.m5 = m5; |
3200 | | cryptoInfo.she.op.generateM4M5.m5Sz = m5Sz; |
3201 | | |
3202 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3203 | | } |
3204 | | |
3205 | | return wc_CryptoCb_TranslateErrorCode(ret); |
3206 | | } |
3207 | | |
3208 | | int wc_CryptoCb_SheExportKey(wc_SHE* she, |
3209 | | byte* m1, word32 m1Sz, |
3210 | | byte* m2, word32 m2Sz, |
3211 | | byte* m3, word32 m3Sz, |
3212 | | byte* m4, word32 m4Sz, |
3213 | | byte* m5, word32 m5Sz, |
3214 | | const void* ctx) |
3215 | | { |
3216 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3217 | | CryptoCb* dev; |
3218 | | |
3219 | | if (she == NULL) { |
3220 | | return BAD_FUNC_ARG; |
3221 | | } |
3222 | | |
3223 | | dev = wc_CryptoCb_FindDevice(she->devId, WC_ALGO_TYPE_SHE); |
3224 | | if (dev && dev->cb) { |
3225 | | wc_CryptoInfo cryptoInfo; |
3226 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3227 | | cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; |
3228 | | cryptoInfo.she.she = she; |
3229 | | cryptoInfo.she.type = WC_SHE_EXPORT_KEY; |
3230 | | cryptoInfo.she.ctx = ctx; |
3231 | | cryptoInfo.she.op.exportKey.m1 = m1; |
3232 | | cryptoInfo.she.op.exportKey.m1Sz = m1Sz; |
3233 | | cryptoInfo.she.op.exportKey.m2 = m2; |
3234 | | cryptoInfo.she.op.exportKey.m2Sz = m2Sz; |
3235 | | cryptoInfo.she.op.exportKey.m3 = m3; |
3236 | | cryptoInfo.she.op.exportKey.m3Sz = m3Sz; |
3237 | | cryptoInfo.she.op.exportKey.m4 = m4; |
3238 | | cryptoInfo.she.op.exportKey.m4Sz = m4Sz; |
3239 | | cryptoInfo.she.op.exportKey.m5 = m5; |
3240 | | cryptoInfo.she.op.exportKey.m5Sz = m5Sz; |
3241 | | |
3242 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3243 | | } |
3244 | | |
3245 | | return wc_CryptoCb_TranslateErrorCode(ret); |
3246 | | } |
3247 | | #endif /* WOLFSSL_SHE */ |
3248 | | |
3249 | | /* returns the default dev id for the current build */ |
3250 | | int wc_CryptoCb_DefaultDevID(void) |
3251 | 79.4k | { |
3252 | 79.4k | int ret; |
3253 | | |
3254 | | /* Explicitly disable the "default devId" behavior. Ensures that any devId |
3255 | | * will only be used if explicitly passed as an argument to crypto functions, |
3256 | | * and never automatically selected. */ |
3257 | | #ifdef WC_NO_DEFAULT_DEVID |
3258 | | ret = INVALID_DEVID; |
3259 | | #else |
3260 | | /* conditional macro selection based on build */ |
3261 | | #ifdef WOLFSSL_CAAM_DEVID |
3262 | | ret = WOLFSSL_CAAM_DEVID; |
3263 | | #elif defined(HAVE_ARIA) |
3264 | | ret = WOLFSSL_ARIA_DEVID; |
3265 | | #elif defined(WC_USE_DEVID) |
3266 | | ret = WC_USE_DEVID; |
3267 | | #else |
3268 | | /* try first available */ |
3269 | 79.4k | ret = wc_CryptoCb_GetDevIdAtIndex(0); |
3270 | 79.4k | #endif |
3271 | 79.4k | #endif /* WC_NO_DEFAULT_DEVID */ |
3272 | | |
3273 | 79.4k | return ret; |
3274 | 79.4k | } |
3275 | | |
3276 | | #if defined(HAVE_HKDF) && !defined(NO_HMAC) |
3277 | | int wc_CryptoCb_Hkdf(int hashType, const byte* inKey, word32 inKeySz, |
3278 | | const byte* salt, word32 saltSz, const byte* info, |
3279 | | word32 infoSz, byte* out, word32 outSz, int devId) |
3280 | 0 | { |
3281 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3282 | 0 | CryptoCb* dev; |
3283 | | |
3284 | | /* Find registered callback device */ |
3285 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KDF); |
3286 | |
|
3287 | 0 | if (dev && dev->cb) { |
3288 | 0 | wc_CryptoInfo cryptoInfo; |
3289 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3290 | |
|
3291 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_KDF; |
3292 | 0 | cryptoInfo.kdf.type = WC_KDF_TYPE_HKDF; |
3293 | 0 | cryptoInfo.kdf.hkdf.hashType = hashType; |
3294 | 0 | cryptoInfo.kdf.hkdf.inKey = inKey; |
3295 | 0 | cryptoInfo.kdf.hkdf.inKeySz = inKeySz; |
3296 | 0 | cryptoInfo.kdf.hkdf.salt = salt; |
3297 | 0 | cryptoInfo.kdf.hkdf.saltSz = saltSz; |
3298 | 0 | cryptoInfo.kdf.hkdf.info = info; |
3299 | 0 | cryptoInfo.kdf.hkdf.infoSz = infoSz; |
3300 | 0 | cryptoInfo.kdf.hkdf.out = out; |
3301 | 0 | cryptoInfo.kdf.hkdf.outSz = outSz; |
3302 | |
|
3303 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3304 | 0 | } |
3305 | |
|
3306 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3307 | 0 | } |
3308 | | |
3309 | | /* NOTE: size of 'out' must be the digest size per hashType */ |
3310 | | int wc_CryptoCb_Hkdf_Extract(int hashType, const byte* salt, word32 saltSz, |
3311 | | const byte* inKey, word32 inKeySz, byte* out, int devId) |
3312 | 0 | { |
3313 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3314 | 0 | CryptoCb* dev; |
3315 | | |
3316 | | /* Find registered callback device */ |
3317 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KDF); |
3318 | |
|
3319 | 0 | if (dev && dev->cb) { |
3320 | 0 | wc_CryptoInfo cryptoInfo; |
3321 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3322 | |
|
3323 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_KDF; |
3324 | 0 | cryptoInfo.kdf.type = WC_KDF_TYPE_HKDF_EXTRACT; |
3325 | 0 | cryptoInfo.kdf.hkdf_extract.hashType = hashType; |
3326 | 0 | cryptoInfo.kdf.hkdf_extract.salt = salt; |
3327 | 0 | cryptoInfo.kdf.hkdf_extract.saltSz = saltSz; |
3328 | 0 | cryptoInfo.kdf.hkdf_extract.inKey = inKey; |
3329 | 0 | cryptoInfo.kdf.hkdf_extract.inKeySz = inKeySz; |
3330 | 0 | cryptoInfo.kdf.hkdf_extract.out = out; |
3331 | |
|
3332 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3333 | 0 | } |
3334 | |
|
3335 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3336 | 0 | } |
3337 | | |
3338 | | int wc_CryptoCb_Hkdf_Expand(int hashType, const byte* inKey, word32 inKeySz, |
3339 | | const byte* info, word32 infoSz, byte* out, word32 outSz, |
3340 | | int devId) |
3341 | 0 | { |
3342 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3343 | 0 | CryptoCb* dev; |
3344 | | |
3345 | | /* Find registered callback device */ |
3346 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KDF); |
3347 | |
|
3348 | 0 | if (dev && dev->cb) { |
3349 | 0 | wc_CryptoInfo cryptoInfo; |
3350 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3351 | |
|
3352 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_KDF; |
3353 | 0 | cryptoInfo.kdf.type = WC_KDF_TYPE_HKDF_EXPAND; |
3354 | 0 | cryptoInfo.kdf.hkdf_expand.hashType = hashType; |
3355 | 0 | cryptoInfo.kdf.hkdf_expand.inKey = inKey; |
3356 | 0 | cryptoInfo.kdf.hkdf_expand.inKeySz = inKeySz; |
3357 | 0 | cryptoInfo.kdf.hkdf_expand.info = info; |
3358 | 0 | cryptoInfo.kdf.hkdf_expand.infoSz = infoSz; |
3359 | 0 | cryptoInfo.kdf.hkdf_expand.out = out; |
3360 | 0 | cryptoInfo.kdf.hkdf_expand.outSz = outSz; |
3361 | |
|
3362 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3363 | 0 | } |
3364 | |
|
3365 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3366 | 0 | } |
3367 | | #endif /* HAVE_HKDF && !NO_HMAC */ |
3368 | | |
3369 | | #ifdef WOLF_CRYPTO_CB_COPY |
3370 | | /* General copy callback function for algorithm structures |
3371 | | * devId: The device ID to use for the callback |
3372 | | * algo: Algorithm type (enum wc_AlgoType) - WC_ALGO_TYPE_HASH, |
3373 | | * WC_ALGO_TYPE_CIPHER, etc |
3374 | | * type: Specific type - for HASH: enum wc_HashType, for CIPHER: |
3375 | | * enum wc_CipherType |
3376 | | * src: Pointer to source structure |
3377 | | * dst: Pointer to destination structure |
3378 | | * Returns: 0 on success, negative on error, CRYPTOCB_UNAVAILABLE if not |
3379 | | * handled |
3380 | | */ |
3381 | | int wc_CryptoCb_Copy(int devId, int algo, int type, void* src, void* dst) |
3382 | | { |
3383 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3384 | | CryptoCb* dev; |
3385 | | |
3386 | | /* Find registered callback device */ |
3387 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_COPY); |
3388 | | if (dev && dev->cb) { |
3389 | | wc_CryptoInfo cryptoInfo; |
3390 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3391 | | cryptoInfo.algo_type = WC_ALGO_TYPE_COPY; |
3392 | | cryptoInfo.copy.algo = algo; |
3393 | | cryptoInfo.copy.type = type; |
3394 | | cryptoInfo.copy.src = src; |
3395 | | cryptoInfo.copy.dst = dst; |
3396 | | |
3397 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3398 | | } |
3399 | | |
3400 | | return wc_CryptoCb_TranslateErrorCode(ret); |
3401 | | } |
3402 | | #endif /* WOLF_CRYPTO_CB_COPY */ |
3403 | | |
3404 | | #ifdef WOLF_CRYPTO_CB_FREE |
3405 | | /* General free callback function for algorithm structures |
3406 | | * devId: The device ID to use for the callback |
3407 | | * algo: Algorithm type (enum wc_AlgoType) - WC_ALGO_TYPE_HASH, |
3408 | | * WC_ALGO_TYPE_CIPHER, etc |
3409 | | * type: Specific type - for HASH: enum wc_HashType, for CIPHER: |
3410 | | * enum wc_CipherType |
3411 | | * subType: Specific subtype - for PQC: enum wc_PqcKemType, |
3412 | | * enum wc_PqcSignatureType |
3413 | | * obj: Pointer to object structure to free |
3414 | | * Returns: 0 on success, negative on error, CRYPTOCB_UNAVAILABLE if not |
3415 | | * handled |
3416 | | */ |
3417 | | int wc_CryptoCb_Free(int devId, int algo, int type, int subType, void* obj) |
3418 | | { |
3419 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3420 | | CryptoCb* dev; |
3421 | | |
3422 | | /* Find registered callback device */ |
3423 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_FREE); |
3424 | | if (dev && dev->cb) { |
3425 | | wc_CryptoInfo cryptoInfo; |
3426 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3427 | | cryptoInfo.algo_type = WC_ALGO_TYPE_FREE; |
3428 | | cryptoInfo.free.algo = algo; |
3429 | | cryptoInfo.free.type = type; |
3430 | | cryptoInfo.free.subType = subType; |
3431 | | cryptoInfo.free.obj = obj; |
3432 | | |
3433 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3434 | | } |
3435 | | |
3436 | | return wc_CryptoCb_TranslateErrorCode(ret); |
3437 | | } |
3438 | | #endif /* WOLF_CRYPTO_CB_FREE */ |
3439 | | |
3440 | | #ifdef WOLF_CRYPTO_CB_SETKEY |
3441 | | /* Generic SetKey callback for importing keys into hardware. |
3442 | | * devId: Device ID for the registered callback |
3443 | | * type: enum wc_SetKeyType (AES, HMAC, RSA_PUB, RSA_PRIV, ECC_PUB, ECC_PRIV) |
3444 | | * obj: Context struct being operated on (Aes*, Hmac*, RsaKey*, ecc_key*) |
3445 | | * key: Key material: raw bytes (AES/HMAC) or temp struct to export from (RSA/ECC) |
3446 | | * keySz: Key size in bytes (0 when key is a struct pointer) |
3447 | | * aux: Auxiliary data (IV, etc.) or NULL |
3448 | | * auxSz: Aux data size, 0 if unused |
3449 | | * flags: AES: direction (AES_ENCRYPTION/DECRYPTION). Others: 0 |
3450 | | * Returns: 0 on success, CRYPTOCB_UNAVAILABLE if not handled, negative on error |
3451 | | */ |
3452 | | int wc_CryptoCb_SetKey(int devId, int type, void* obj, |
3453 | | void* key, word32 keySz, |
3454 | | void* aux, word32 auxSz, |
3455 | | int flags) |
3456 | | { |
3457 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3458 | | CryptoCb* dev; |
3459 | | |
3460 | | /* Find registered callback device */ |
3461 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_SETKEY); |
3462 | | if (dev && dev->cb) { |
3463 | | wc_CryptoInfo cryptoInfo; |
3464 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3465 | | cryptoInfo.algo_type = WC_ALGO_TYPE_SETKEY; |
3466 | | cryptoInfo.setkey.type = type; |
3467 | | cryptoInfo.setkey.obj = obj; |
3468 | | cryptoInfo.setkey.key = key; |
3469 | | cryptoInfo.setkey.keySz = keySz; |
3470 | | cryptoInfo.setkey.aux = aux; |
3471 | | cryptoInfo.setkey.auxSz = auxSz; |
3472 | | cryptoInfo.setkey.flags = flags; |
3473 | | |
3474 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3475 | | } |
3476 | | |
3477 | | return wc_CryptoCb_TranslateErrorCode(ret); |
3478 | | } |
3479 | | #endif /* WOLF_CRYPTO_CB_SETKEY */ |
3480 | | |
3481 | | #ifdef WOLF_CRYPTO_CB_EXPORT_KEY |
3482 | | /* Generic ExportKey callback for exporting key material from hardware. |
3483 | | * devId: Device ID for the registered callback |
3484 | | * type: enum wc_PkType (WC_PK_TYPE_RSA, WC_PK_TYPE_ECDSA, etc.) |
3485 | | * obj: Hardware key object (has devCtx, id[], etc.) |
3486 | | * out: Software key object to fill (same type as obj, caller-allocated) |
3487 | | * The callback exports from hardware into the software key. The caller then |
3488 | | * uses normal software export functions on 'out' and frees it. |
3489 | | * Returns: 0 on success, CRYPTOCB_UNAVAILABLE if not handled, negative on error |
3490 | | */ |
3491 | | int wc_CryptoCb_ExportKey(int devId, int type, const void* obj, void* out) |
3492 | | { |
3493 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3494 | | CryptoCb* dev; |
3495 | | |
3496 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_EXPORT_KEY); |
3497 | | if (dev && dev->cb) { |
3498 | | wc_CryptoInfo cryptoInfo; |
3499 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3500 | | cryptoInfo.algo_type = WC_ALGO_TYPE_EXPORT_KEY; |
3501 | | cryptoInfo.export_key.type = type; |
3502 | | cryptoInfo.export_key.obj = obj; |
3503 | | cryptoInfo.export_key.out = out; |
3504 | | |
3505 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3506 | | } |
3507 | | |
3508 | | return wc_CryptoCb_TranslateErrorCode(ret); |
3509 | | } |
3510 | | #endif /* WOLF_CRYPTO_CB_EXPORT_KEY */ |
3511 | | |
3512 | | #if defined(HAVE_CMAC_KDF) |
3513 | | /* Crypto callback for NIST SP 800 56C two-step CMAC KDF. See software |
3514 | | * implementation in wc_KDA_KDF_twostep_cmac for more comments. |
3515 | | * */ |
3516 | | int wc_CryptoCb_Kdf_TwostepCmac(const byte * salt, word32 saltSz, |
3517 | | const byte* z, word32 zSz, |
3518 | | const byte* fixedInfo, word32 fixedInfoSz, |
3519 | | byte* output, word32 outputSz, int devId) |
3520 | | { |
3521 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3522 | | CryptoCb* dev; |
3523 | | |
3524 | | /* Find registered callback device */ |
3525 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KDF); |
3526 | | |
3527 | | if (dev && dev->cb) { |
3528 | | wc_CryptoInfo cryptoInfo; |
3529 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3530 | | |
3531 | | cryptoInfo.algo_type = WC_ALGO_TYPE_KDF; |
3532 | | cryptoInfo.kdf.type = WC_KDF_TYPE_TWOSTEP_CMAC; |
3533 | | cryptoInfo.kdf.twostep_cmac.salt = salt; |
3534 | | cryptoInfo.kdf.twostep_cmac.saltSz = saltSz; |
3535 | | cryptoInfo.kdf.twostep_cmac.z = z; |
3536 | | cryptoInfo.kdf.twostep_cmac.zSz = zSz; |
3537 | | cryptoInfo.kdf.twostep_cmac.fixedInfo = fixedInfo; |
3538 | | cryptoInfo.kdf.twostep_cmac.fixedInfoSz = fixedInfoSz; |
3539 | | cryptoInfo.kdf.twostep_cmac.out = output; |
3540 | | cryptoInfo.kdf.twostep_cmac.outSz = outputSz; |
3541 | | |
3542 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3543 | | } |
3544 | | |
3545 | | return wc_CryptoCb_TranslateErrorCode(ret); |
3546 | | } |
3547 | | #endif /* HAVE_CMAC_KDF */ |
3548 | | |
3549 | | #endif /* WOLF_CRYPTO_CB */ |