/src/wolfssl-sp-math-all-8bit/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 | | * WOLF_CRYPTO_CB_KEYGEN_SW_FALLBACK: default: off |
41 | | * Software keygen for id/label keys a device declines. |
42 | | * DEBUG_CRYPTOCB: Enable debug InfoString functions default: off |
43 | | * |
44 | | * Device ID options: |
45 | | * WC_USE_DEVID: Specify a default device ID to use default: off |
46 | | * when no hardware device is detected. |
47 | | * WC_NO_DEFAULT_DEVID: Disable automatic default device ID default: off |
48 | | * selection. Requires explicit devId passing. |
49 | | * WOLFSSL_CAAM_DEVID: Device ID constant (value 7) for NXP default: off |
50 | | * CAAM hardware crypto. |
51 | | * |
52 | | * Algorithm-specific callback options: |
53 | | * NO_SHA2_CRYPTO_CB: Disable crypto callbacks for SHA-384 default: off |
54 | | * and SHA-512 operations. |
55 | | * WOLF_CRYPTO_CB_NO_SHA512_FALLBACK: default: off |
56 | | * Do not fall back to the generic SHA-512 |
57 | | * callback for SHA-384, SHA-512/224 and |
58 | | * SHA-512/256 when no variant-specific |
59 | | * callback is registered. Required for |
60 | | * backends whose hash context has no |
61 | | * digest[] state field or that keep the |
62 | | * hash state on the device (auto-enabled |
63 | | * for Renesas FSPSM). |
64 | | * WOLF_CRYPTO_CB_ONLY_ECC: Use only callbacks for ECC default: off |
65 | | * WOLF_CRYPTO_CB_ONLY_RSA: Use only callbacks for RSA default: off |
66 | | * WOLF_CRYPTO_CB_ONLY_SHA256: Use only callbacks for SHA-256 default: off |
67 | | * WOLF_CRYPTO_CB_ONLY_SHA512: Use only callbacks for SHA-512 default: off |
68 | | * WOLF_CRYPTO_CB_ONLY_AES: Use only callbacks for AES default: off |
69 | | * WOLF_CRYPTO_CB_ONLY_ED25519: Use only callbacks for Ed25519 default: off |
70 | | * WOLF_CRYPTO_CB_ONLY_CURVE25519: Use only callbacks for X25519 default: off |
71 | | * WOLF_CRYPTO_CB_ONLY_MLKEM: Use only callbacks for ML-KEM default: off |
72 | | * WOLF_CRYPTO_CB_ONLY_MLDSA: Use only callbacks for ML-DSA default: off |
73 | | * WOLF_CRYPTO_CB_ONLY_LMS: Use only callbacks for LMS/HSS default: off |
74 | | * WOLF_CRYPTO_CB_ONLY_XMSS: Use only callbacks for XMSS default: off |
75 | | * WOLF_CRYPTO_CB_ONLY_FRODOKEM: Use only callbacks for FrodoKEM default: off |
76 | | * WOLF_CRYPTO_CB_SHAKE_XOF: Dispatch SHAKE absorb and squeeze default: off |
77 | | * as well as update and final. Off by |
78 | | * default because a callback that predates |
79 | | * hash.shakeOp would misread them. |
80 | | */ |
81 | | |
82 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
83 | | |
84 | | #ifdef WOLF_CRYPTO_CB |
85 | | |
86 | | #include <wolfssl/wolfcrypt/cryptocb.h> |
87 | | |
88 | | #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WOLF_CRYPTO_CB_ASYNC_POLL) && \ |
89 | | !defined(WOLFSSL_ASYNC_CRYPT_SW) && !defined(HAVE_INTEL_QA) && \ |
90 | | !defined(HAVE_CAVIUM) && !defined(WOLF_CRYPTO_CB_ASYNC_NO_WARN) |
91 | | #warning "crypto callbacks with async crypt cannot complete TLS 1.2 \ |
92 | | record ciphers (TLS 1.3 resumes them by re-invoking the callback). Define \ |
93 | | WOLF_CRYPTO_CB_ASYNC_POLL to enable them, or WOLF_CRYPTO_CB_ASYNC_NO_WARN to \ |
94 | | silence." |
95 | | #endif |
96 | | |
97 | | #ifdef HAVE_ARIA |
98 | | #include <wolfssl/wolfcrypt/port/aria/aria-cryptocb.h> |
99 | | #endif |
100 | | |
101 | | #ifdef WOLFSSL_CAAM |
102 | | #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h> |
103 | | #endif |
104 | | |
105 | | /* Fixed table, read without a lock on every offloaded operation. Lookups |
106 | | * match on devId, so an entry is filled before devId is stored and cleared |
107 | | * after devId is retired, with a WC_BARRIER() between the two so the |
108 | | * compiler cannot reorder them. Serializing register/unregister against each |
109 | | * other, and against operations already dispatched to that device, remains |
110 | | * the caller's job. */ |
111 | | |
112 | | typedef struct CryptoCb { |
113 | | int devId; |
114 | | CryptoDevCallbackFunc cb; |
115 | | void* ctx; |
116 | | } CryptoCb; |
117 | | static WC_THREADSHARED CryptoCb gCryptoDev[MAX_CRYPTO_DEVID_CALLBACKS]; |
118 | | |
119 | | #ifdef WOLF_CRYPTO_CB_FIND |
120 | | static CryptoDevCallbackFind CryptoCb_FindCb = NULL; |
121 | | #endif |
122 | | |
123 | | #ifdef DEBUG_CRYPTOCB |
124 | | static const char* GetAlgoTypeStr(int algo) |
125 | | { |
126 | | switch (algo) { /* enum wc_AlgoType */ |
127 | | #ifdef WOLF_CRYPTO_CB_CMD |
128 | | case WC_ALGO_TYPE_NONE: return "None-Command"; |
129 | | #endif |
130 | | case WC_ALGO_TYPE_HASH: return "Hash"; |
131 | | case WC_ALGO_TYPE_CIPHER: return "Cipher"; |
132 | | case WC_ALGO_TYPE_PK: return "PK"; |
133 | | case WC_ALGO_TYPE_RNG: return "RNG"; |
134 | | case WC_ALGO_TYPE_SEED: return "Seed"; |
135 | | case WC_ALGO_TYPE_HMAC: return "HMAC"; |
136 | | case WC_ALGO_TYPE_CMAC: return "CMAC"; |
137 | | case WC_ALGO_TYPE_CERT: return "Cert"; |
138 | | case WC_ALGO_TYPE_KDF: return "KDF"; |
139 | | #ifdef WOLF_CRYPTO_CB_COPY |
140 | | case WC_ALGO_TYPE_COPY: return "Copy"; |
141 | | #endif /* WOLF_CRYPTO_CB_COPY */ |
142 | | #ifdef WOLF_CRYPTO_CB_FREE |
143 | | case WC_ALGO_TYPE_FREE: return "Free"; |
144 | | #endif /* WOLF_CRYPTO_CB_FREE */ |
145 | | #ifdef WOLF_CRYPTO_CB_SETKEY |
146 | | case WC_ALGO_TYPE_SETKEY: return "SetKey"; |
147 | | #endif /* WOLF_CRYPTO_CB_SETKEY */ |
148 | | #ifdef WOLF_CRYPTO_CB_EXPORT_KEY |
149 | | case WC_ALGO_TYPE_EXPORT_KEY: return "ExportKey"; |
150 | | #endif /* WOLF_CRYPTO_CB_EXPORT_KEY */ |
151 | | #ifdef WOLF_CRYPTO_CB_KEYSTORE |
152 | | case WC_ALGO_TYPE_KEYSTORE: return "KeyStore"; |
153 | | #endif /* WOLF_CRYPTO_CB_KEYSTORE */ |
154 | | } |
155 | | return NULL; |
156 | | } |
157 | | #ifdef WOLF_CRYPTO_CB_SETKEY |
158 | | static const char* GetSetKeyTypeStr(int type) |
159 | | { |
160 | | switch (type) { |
161 | | case WC_SETKEY_NONE: return "None"; |
162 | | case WC_SETKEY_HMAC: return "HMAC"; |
163 | | case WC_SETKEY_RSA_PUB: return "RSA-Pub"; |
164 | | case WC_SETKEY_RSA_PRIV: return "RSA-Priv"; |
165 | | case WC_SETKEY_ECC_PUB: return "ECC-Pub"; |
166 | | case WC_SETKEY_ECC_PRIV: return "ECC-Priv"; |
167 | | case WC_SETKEY_AES: return "AES"; |
168 | | default: break; |
169 | | } |
170 | | return NULL; |
171 | | } |
172 | | #endif /* WOLF_CRYPTO_CB_SETKEY */ |
173 | | static const char* GetPkTypeStr(int pk) |
174 | | { |
175 | | switch (pk) { |
176 | | case WC_PK_TYPE_RSA: return "RSA"; |
177 | | case WC_PK_TYPE_RSA_PSS_VERIFY: return "RSA-PSS-Verify"; |
178 | | case WC_PK_TYPE_DH: return "DH"; |
179 | | case WC_PK_TYPE_ECDH: return "ECDH"; |
180 | | case WC_PK_TYPE_ECDSA_SIGN: return "ECDSA-Sign"; |
181 | | case WC_PK_TYPE_ECDSA_VERIFY: return "ECDSA-Verify"; |
182 | | case WC_PK_TYPE_ED25519_SIGN: return "ED25519-Sign"; |
183 | | case WC_PK_TYPE_ED25519_VERIFY: return "ED25519-Verify"; |
184 | | case WC_PK_TYPE_ED448: return "ED448-Sign"; |
185 | | case WC_PK_TYPE_ED448_VERIFY: return "ED448-Verify"; |
186 | | case WC_PK_TYPE_ED448_MAKE_PUB: return "ED448 MakePub"; |
187 | | case WC_PK_TYPE_ED448_CHECK_KEY: return "ED448 CheckKey"; |
188 | | case WC_PK_TYPE_ED448_KEYGEN: return "ED448 KeyGen"; |
189 | | case WC_PK_TYPE_CURVE25519: return "CURVE25519"; |
190 | | case WC_PK_TYPE_RSA_KEYGEN: return "RSA KeyGen"; |
191 | | case WC_PK_TYPE_EC_KEYGEN: return "ECC KeyGen"; |
192 | | case WC_PK_TYPE_EC_GET_SIZE: return "ECC GetSize"; |
193 | | case WC_PK_TYPE_EC_GET_SIG_SIZE: return "ECC GetSigSize"; |
194 | | case WC_PK_TYPE_EC_MAKE_PUB: return "ECC MakePub"; |
195 | | case WC_PK_TYPE_EC_CHECK_PUB_KEY: return "ECC CheckPubKey"; |
196 | | case WC_PK_TYPE_ED25519_MAKE_PUB: return "ED25519 MakePub"; |
197 | | case WC_PK_TYPE_ED25519_CHECK_KEY: return "ED25519 CheckKey"; |
198 | | case WC_PK_TYPE_CURVE25519_MAKE_PUB: return "CURVE25519 MakePub"; |
199 | | case WC_PK_TYPE_CURVE25519_GENERIC: return "CURVE25519 Generic"; |
200 | | case WC_PK_TYPE_CURVE448: return "CURVE448"; |
201 | | case WC_PK_TYPE_CURVE448_KEYGEN: return "CURVE448 KeyGen"; |
202 | | case WC_PK_TYPE_CURVE448_MAKE_PUB: return "CURVE448 MakePub"; |
203 | | case WC_PK_TYPE_CURVE448_GENERIC: return "CURVE448 Generic"; |
204 | | #if defined(WOLFSSL_HAVE_MLKEM) || defined(WOLFSSL_HAVE_FRODOKEM) |
205 | | case WC_PK_TYPE_PQC_KEM_KEYGEN: return "PQC KEM KeyGen"; |
206 | | case WC_PK_TYPE_PQC_KEM_ENCAPS: return "PQC KEM Encapsulate"; |
207 | | case WC_PK_TYPE_PQC_KEM_DECAPS: return "PQC KEM Decapsulate"; |
208 | | #endif |
209 | | #if defined(WOLFSSL_HAVE_MLDSA) || defined(HAVE_FALCON) || \ |
210 | | defined(WOLFSSL_HAVE_SLHDSA) |
211 | | case WC_PK_TYPE_PQC_SIG_KEYGEN: return "PQC Sig KeyGen"; |
212 | | case WC_PK_TYPE_PQC_SIG_KEYGEN_SEED: return "PQC Sig KeyGen Seed"; |
213 | | case WC_PK_TYPE_PQC_SIG_SIGN: return "PQC Sig Sign"; |
214 | | case WC_PK_TYPE_PQC_SIG_VERIFY: return "PQC Sig Verify"; |
215 | | case WC_PK_TYPE_PQC_SIG_CHECK_PRIV_KEY: return "PQC Sig CheckPrivKey"; |
216 | | case WC_PK_TYPE_PQC_SIG_SIGN_MSG: return "PQC Sig SignMsg"; |
217 | | case WC_PK_TYPE_PQC_SIG_VERIFY_MSG: return "PQC Sig VerifyMsg"; |
218 | | #endif |
219 | | #ifdef WOLFSSL_SM2 |
220 | | case WC_PK_TYPE_SM2_SIGN: return "SM2-Sign"; |
221 | | case WC_PK_TYPE_SM2_VERIFY: return "SM2-Verify"; |
222 | | case WC_PK_TYPE_SM2_SHARED_SECRET: return "SM2-SharedSecret"; |
223 | | case WC_PK_TYPE_SM2_CREATE_DIGEST: return "SM2-CreateDigest"; |
224 | | #endif |
225 | | #if defined(WOLFSSL_HAVE_LMS) || defined(WOLFSSL_HAVE_XMSS) |
226 | | case WC_PK_TYPE_PQC_STATEFUL_SIG_KEYGEN: |
227 | | return "PQC Stateful Sig KeyGen"; |
228 | | case WC_PK_TYPE_PQC_STATEFUL_SIG_SIGN: |
229 | | return "PQC Stateful Sig Sign"; |
230 | | case WC_PK_TYPE_PQC_STATEFUL_SIG_VERIFY: |
231 | | return "PQC Stateful Sig Verify"; |
232 | | case WC_PK_TYPE_PQC_STATEFUL_SIG_SIGS_LEFT: |
233 | | return "PQC Stateful Sig SigsLeft"; |
234 | | #endif |
235 | | } |
236 | | return NULL; |
237 | | } |
238 | | #if !defined(NO_AES) || !defined(NO_DES3) || defined(WOLFSSL_SM4) |
239 | | static const char* GetCipherTypeStr(int cipher) |
240 | | { |
241 | | switch (cipher) { |
242 | | case WC_CIPHER_AES: return "AES ECB"; |
243 | | case WC_CIPHER_AES_CBC: return "AES CBC"; |
244 | | case WC_CIPHER_AES_GCM: return "AES GCM"; |
245 | | case WC_CIPHER_AES_CTR: return "AES CTR"; |
246 | | case WC_CIPHER_AES_XTS: return "AES XTS"; |
247 | | case WC_CIPHER_AES_CFB: return "AES CFB"; |
248 | | case WC_CIPHER_AES_OFB: return "AES OFB"; |
249 | | case WC_CIPHER_AES_KEYWRAP: return "AES KeyWrap"; |
250 | | case WC_CIPHER_DES3: return "DES3"; |
251 | | case WC_CIPHER_DES: return "DES"; |
252 | | case WC_CIPHER_CHACHA: return "ChaCha20"; |
253 | | #if defined(WOLFSSL_SM4) && defined(WOLFSSL_SM4_ECB) |
254 | | case WC_CIPHER_SM4_ECB: return "SM4 ECB"; |
255 | | #endif |
256 | | #if defined(WOLFSSL_SM4) && defined(WOLFSSL_SM4_CBC) |
257 | | case WC_CIPHER_SM4_CBC: return "SM4 CBC"; |
258 | | #endif |
259 | | #if defined(WOLFSSL_SM4) && defined(WOLFSSL_SM4_CTR) |
260 | | case WC_CIPHER_SM4_CTR: return "SM4 CTR"; |
261 | | #endif |
262 | | #if defined(WOLFSSL_SM4) && defined(WOLFSSL_SM4_GCM) |
263 | | case WC_CIPHER_SM4_GCM: return "SM4 GCM"; |
264 | | #endif |
265 | | #if defined(WOLFSSL_SM4) && defined(WOLFSSL_SM4_CCM) |
266 | | case WC_CIPHER_SM4_CCM: return "SM4 CCM"; |
267 | | #endif |
268 | | #ifdef WOLFSSL_SM4 |
269 | | case WC_CIPHER_SM4: return "SM4"; |
270 | | #endif |
271 | | } |
272 | | return NULL; |
273 | | } |
274 | | #endif /* !NO_AES || !NO_DES3 || WOLFSSL_SM4 */ |
275 | | static const char* GetHashTypeStr(int hash) |
276 | | { |
277 | | switch (hash) { |
278 | | case WC_HASH_TYPE_MD2: return "MD2"; |
279 | | case WC_HASH_TYPE_MD4: return "MD4"; |
280 | | case WC_HASH_TYPE_MD5: return "MD5"; |
281 | | case WC_HASH_TYPE_SHA: return "SHA-1"; |
282 | | case WC_HASH_TYPE_SHA224: return "SHA-224"; |
283 | | case WC_HASH_TYPE_SHA256: return "SHA-256"; |
284 | | case WC_HASH_TYPE_SHA384: return "SHA-384"; |
285 | | case WC_HASH_TYPE_SHA512: return "SHA-512"; |
286 | | case WC_HASH_TYPE_MD5_SHA: return "MD5-SHA1"; |
287 | | case WC_HASH_TYPE_SHA3_224: return "SHA3-224"; |
288 | | case WC_HASH_TYPE_SHA3_256: return "SHA3-256"; |
289 | | case WC_HASH_TYPE_SHA3_384: return "SHA3-384"; |
290 | | case WC_HASH_TYPE_SHA3_512: return "SHA3-512"; |
291 | | case WC_HASH_TYPE_BLAKE2B: return "Blake2B"; |
292 | | case WC_HASH_TYPE_BLAKE2S: return "Blake2S"; |
293 | | #ifdef WOLFSSL_SM3 |
294 | | case WC_HASH_TYPE_SM3: return "SM3"; |
295 | | #endif |
296 | | } |
297 | | return NULL; |
298 | | } |
299 | | |
300 | | #ifdef WOLFSSL_CMAC |
301 | | static const char* GetCmacTypeStr(int type) |
302 | | { |
303 | | switch (type) { |
304 | | case WC_CMAC_AES: return "AES"; |
305 | | } |
306 | | return NULL; |
307 | | } |
308 | | #endif /* WOLFSSL_CMAC */ |
309 | | |
310 | | #ifndef NO_RSA |
311 | | static const char* GetRsaType(int type) |
312 | | { |
313 | | switch (type) { |
314 | | case RSA_PUBLIC_ENCRYPT: return "Public Encrypt"; |
315 | | case RSA_PUBLIC_DECRYPT: return "Public Decrypt"; |
316 | | case RSA_PRIVATE_ENCRYPT: return "Private Encrypt"; |
317 | | case RSA_PRIVATE_DECRYPT: return "Private Decrypt"; |
318 | | } |
319 | | return NULL; |
320 | | } |
321 | | #endif |
322 | | |
323 | | #ifdef WOLF_CRYPTO_CB_CMD |
324 | | static const char* GetCryptoCbCmdTypeStr(int type) |
325 | | { |
326 | | switch (type) { |
327 | | case WC_CRYPTOCB_CMD_TYPE_REGISTER: return "Register"; |
328 | | case WC_CRYPTOCB_CMD_TYPE_UNREGISTER: return "UnRegister"; |
329 | | } |
330 | | return NULL; |
331 | | } |
332 | | #endif |
333 | | |
334 | | |
335 | | #if (defined(HAVE_HKDF) && !defined(NO_HMAC)) || defined(HAVE_CMAC_KDF) |
336 | | static const char* GetKdfTypeStr(int type) |
337 | | { |
338 | | switch (type) { |
339 | | case WC_KDF_TYPE_HKDF: |
340 | | return "HKDF"; |
341 | | case WC_KDF_TYPE_HKDF_EXTRACT: |
342 | | return "HKDF Extract"; |
343 | | case WC_KDF_TYPE_HKDF_EXPAND: |
344 | | return "HKDF Expand"; |
345 | | case WC_KDF_TYPE_PBKDF2: |
346 | | return "PBKDF2"; |
347 | | case WC_KDF_TYPE_TWOSTEP_CMAC: |
348 | | return "TWOSTEP_CMAC"; |
349 | | } |
350 | | return NULL; |
351 | | } |
352 | | #endif |
353 | | |
354 | | void wc_CryptoCb_InfoString(wc_CryptoInfo* info) |
355 | | { |
356 | | if (info == NULL) |
357 | | return; |
358 | | |
359 | | if (info->algo_type == WC_ALGO_TYPE_PK) { |
360 | | #ifndef NO_RSA |
361 | | if (info->pk.type == WC_PK_TYPE_RSA) { |
362 | | printf("Crypto CB: %s %s (%d), %s, Len %d\n", |
363 | | GetAlgoTypeStr(info->algo_type), |
364 | | GetPkTypeStr(info->pk.type), info->pk.type, |
365 | | GetRsaType(info->pk.rsa.type), info->pk.rsa.inLen); |
366 | | } |
367 | | else |
368 | | #endif |
369 | | { |
370 | | printf("Crypto CB: %s %s (%d)\n", |
371 | | GetAlgoTypeStr(info->algo_type), |
372 | | GetPkTypeStr(info->pk.type), info->pk.type); |
373 | | } |
374 | | } |
375 | | #if !defined(NO_AES) || !defined(NO_DES3) || defined(WOLFSSL_SM4) |
376 | | else if (info->algo_type == WC_ALGO_TYPE_CIPHER) { |
377 | | printf("Crypto CB: %s %s (%d) (%p ctx)\n", |
378 | | GetAlgoTypeStr(info->algo_type), |
379 | | GetCipherTypeStr(info->cipher.type), |
380 | | info->cipher.type, (void*)info->cipher.ctx); |
381 | | } |
382 | | #endif /* !NO_AES || !NO_DES3 || WOLFSSL_SM4 */ |
383 | | #if !defined(NO_SHA) || !defined(NO_SHA256) || \ |
384 | | defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384) || \ |
385 | | defined(WOLFSSL_SHA3) || defined(WOLFSSL_SM3) |
386 | | else if (info->algo_type == WC_ALGO_TYPE_HASH) { |
387 | | printf("Crypto CB: %s %s (%d) (%p ctx) %s\n", |
388 | | GetAlgoTypeStr(info->algo_type), |
389 | | GetHashTypeStr(info->hash.type), |
390 | | info->hash.type, (void*)info->hash.ctx, |
391 | | (info->hash.in != NULL) ? "Update" : "Final"); |
392 | | } |
393 | | #endif |
394 | | #ifndef NO_HMAC |
395 | | else if (info->algo_type == WC_ALGO_TYPE_HMAC) { |
396 | | printf("Crypto CB: %s %s (%d) (%p ctx) %s\n", |
397 | | GetAlgoTypeStr(info->algo_type), |
398 | | GetHashTypeStr(info->hmac.macType), |
399 | | info->hmac.macType, (void*)info->hmac.hmac, |
400 | | (info->hmac.in != NULL) ? "Update" : "Final"); |
401 | | } |
402 | | #endif |
403 | | #ifdef WOLFSSL_CMAC |
404 | | else if (info->algo_type == WC_ALGO_TYPE_CMAC) { |
405 | | printf("Crypto CB: %s %s (%d) (%p ctx) %s %s %s\n", |
406 | | GetAlgoTypeStr(info->algo_type), |
407 | | GetCmacTypeStr(info->cmac.type), |
408 | | info->cmac.type, (void*)info->cmac.cmac, |
409 | | (info->cmac.key != NULL) ? "Init " : "", |
410 | | (info->cmac.in != NULL) ? "Update " : "", |
411 | | (info->cmac.out != NULL) ? "Final" : ""); |
412 | | } |
413 | | #endif |
414 | | #ifdef WOLF_CRYPTO_CB_CMD |
415 | | else if (info->algo_type == WC_ALGO_TYPE_NONE) { |
416 | | printf("Crypto CB: %s %s (%d)\n", |
417 | | GetAlgoTypeStr(info->algo_type), |
418 | | GetCryptoCbCmdTypeStr(info->cmd.type), info->cmd.type); |
419 | | } |
420 | | #endif |
421 | | #ifdef WOLF_CRYPTO_CB_COPY |
422 | | else if (info->algo_type == WC_ALGO_TYPE_COPY) { |
423 | | printf("Crypto CB: %s %s Type=%d\n", |
424 | | GetAlgoTypeStr(info->algo_type), |
425 | | GetAlgoTypeStr(info->copy.algo), info->copy.type); |
426 | | } |
427 | | #endif /* WOLF_CRYPTO_CB_COPY */ |
428 | | #ifdef WOLF_CRYPTO_CB_FREE |
429 | | else if (info->algo_type == WC_ALGO_TYPE_FREE) { |
430 | | printf("Crypto CB: %s %s Type=%d\n", |
431 | | GetAlgoTypeStr(info->algo_type), |
432 | | GetAlgoTypeStr(info->free.algo), info->free.type); |
433 | | } |
434 | | #endif /* WOLF_CRYPTO_CB_FREE */ |
435 | | #ifdef WOLF_CRYPTO_CB_SETKEY |
436 | | else if (info->algo_type == WC_ALGO_TYPE_SETKEY) { |
437 | | printf("Crypto CB: %s %s KeySz=%u\n", |
438 | | GetAlgoTypeStr(info->algo_type), |
439 | | GetSetKeyTypeStr(info->setkey.type), info->setkey.keySz); |
440 | | } |
441 | | #endif /* WOLF_CRYPTO_CB_SETKEY */ |
442 | | #ifdef WOLF_CRYPTO_CB_EXPORT_KEY |
443 | | else if (info->algo_type == WC_ALGO_TYPE_EXPORT_KEY) { |
444 | | printf("Crypto CB: %s Type=%d\n", |
445 | | GetAlgoTypeStr(info->algo_type), info->export_key.type); |
446 | | } |
447 | | #endif /* WOLF_CRYPTO_CB_EXPORT_KEY */ |
448 | | #ifdef WOLF_CRYPTO_CB_KEYSTORE |
449 | | else if (info->algo_type == WC_ALGO_TYPE_KEYSTORE) { |
450 | | printf("Crypto CB: %s Type=%d\n", |
451 | | GetAlgoTypeStr(info->algo_type), info->keystore.type); |
452 | | } |
453 | | #endif /* WOLF_CRYPTO_CB_KEYSTORE */ |
454 | | #if (defined(HAVE_HKDF) && !defined(NO_HMAC)) || \ |
455 | | defined(HAVE_CMAC_KDF) |
456 | | else if (info->algo_type == WC_ALGO_TYPE_KDF) { |
457 | | printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type), |
458 | | GetKdfTypeStr(info->kdf.type), info->kdf.type); |
459 | | } |
460 | | #endif |
461 | | else { |
462 | | printf("CryptoCb: %s \n", GetAlgoTypeStr(info->algo_type)); |
463 | | } |
464 | | } |
465 | | #endif /* DEBUG_CRYPTOCB */ |
466 | | |
467 | | /* Search through listed devices and return the first matching device ID |
468 | | * found. */ |
469 | | static CryptoCb* wc_CryptoCb_GetDevice(int devId) |
470 | 19.3k | { |
471 | 19.3k | int i; |
472 | 19.4k | for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
473 | 19.4k | if (gCryptoDev[i].devId == devId) { |
474 | | /* Pairs with the publish barrier in wc_CryptoCb_RegisterDevice(): |
475 | | * cb and ctx must not be read before the devId that selected |
476 | | * this entry. */ |
477 | 19.3k | WC_BARRIER(); |
478 | 19.3k | return &gCryptoDev[i]; |
479 | 19.3k | } |
480 | 19.4k | } |
481 | 12 | return NULL; |
482 | 19.3k | } |
483 | | |
484 | | /* Find a slot that nothing is using yet. A slot that is part way through |
485 | | * being registered has a callback but no devId, so check both fields. */ |
486 | | static CryptoCb* wc_CryptoCb_GetFreeDevice(void) |
487 | 12 | { |
488 | 12 | int i; |
489 | 12 | for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
490 | 12 | if ((gCryptoDev[i].devId == INVALID_DEVID) && |
491 | 12 | (gCryptoDev[i].cb == NULL)) { |
492 | 12 | return &gCryptoDev[i]; |
493 | 12 | } |
494 | 12 | } |
495 | 0 | return NULL; |
496 | 12 | } |
497 | | |
498 | | /* Returns 1 if the given device ID is currently registered, 0 otherwise. |
499 | | * INVALID_DEVID marks free table slots, so it is never reported registered. */ |
500 | | int wc_CryptoCb_IsDeviceRegistered(int devId) |
501 | 0 | { |
502 | 0 | if (devId == INVALID_DEVID) |
503 | 0 | return 0; |
504 | 0 | return wc_CryptoCb_GetDevice(devId) != NULL; |
505 | 0 | } |
506 | | |
507 | | |
508 | | /* Filters through find callback set when trying to get the device, |
509 | | * returns the device found on success and null if not found. */ |
510 | | static CryptoCb* wc_CryptoCb_FindDevice(int devId, int algoType) |
511 | 19.3k | { |
512 | 19.3k | int localDevId = devId; |
513 | | |
514 | | #ifdef WOLF_CRYPTO_CB_FIND |
515 | | if (CryptoCb_FindCb != NULL) { |
516 | | localDevId = CryptoCb_FindCb(devId, algoType); |
517 | | } |
518 | | #endif /* WOLF_CRYPTO_CB_FIND */ |
519 | 19.3k | (void)algoType; |
520 | 19.3k | return wc_CryptoCb_GetDevice(localDevId); |
521 | 19.3k | } |
522 | | |
523 | | #ifdef WC_CRYPTOCB_KEYGEN_ID_CHECK |
524 | | /* Returns 1 if public key operations on devId reach a registered device. */ |
525 | | int wc_CryptoCb_HasPkDevice(int devId) |
526 | 0 | { |
527 | 0 | CryptoCb* dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
528 | |
|
529 | 0 | return (dev != NULL) && (dev->cb != NULL) && (dev->devId != INVALID_DEVID); |
530 | 0 | } |
531 | | #endif |
532 | | |
533 | | |
534 | | static CryptoCb* wc_CryptoCb_FindDeviceByIndex(int startIdx) |
535 | 73.4k | { |
536 | 73.4k | int i; |
537 | 648k | for (i=startIdx; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
538 | 576k | if (gCryptoDev[i].devId != INVALID_DEVID) |
539 | 1.55k | return &gCryptoDev[i]; |
540 | 576k | } |
541 | 71.8k | return NULL; |
542 | 73.4k | } |
543 | | |
544 | | static WC_INLINE int wc_CryptoCb_TranslateErrorCode(int ret) |
545 | 19.3k | { |
546 | 19.3k | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) { |
547 | | /* backwards compatibility for older NOT_COMPILED_IN syntax */ |
548 | 2.03k | ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
549 | 2.03k | } |
550 | 19.3k | return ret; |
551 | 19.3k | } |
552 | | |
553 | | /* Helper function to reset a device entry to invalid */ |
554 | | static WC_INLINE void wc_CryptoCb_ClearDev(CryptoCb *dev) |
555 | 96 | { |
556 | | /* Retire the entry, then clear the rest of it. */ |
557 | 96 | dev->devId = INVALID_DEVID; |
558 | 96 | WC_BARRIER(); |
559 | 96 | dev->cb = NULL; |
560 | 96 | dev->ctx = NULL; |
561 | 96 | } |
562 | | |
563 | | void wc_CryptoCb_Init(void) |
564 | 28 | { |
565 | 28 | int i; |
566 | 252 | for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
567 | 224 | wc_CryptoCb_ClearDev(&gCryptoDev[i]); |
568 | 224 | } |
569 | 28 | } |
570 | | |
571 | | int wc_CryptoCb_Cleanup(void) |
572 | 16 | { |
573 | 16 | int i; |
574 | 16 | int ret; |
575 | | |
576 | | /* Tear down in reverse registration order. Stop at the first busy device |
577 | | * so dependencies registered before it remain available for live objects. */ |
578 | 144 | for (i = MAX_CRYPTO_DEVID_CALLBACKS - 1; i >= 0; i--) { |
579 | 128 | if(gCryptoDev[i].devId != INVALID_DEVID) { |
580 | 0 | ret = wc_CryptoCb_UnRegisterDeviceEx(gCryptoDev[i].devId); |
581 | 0 | if (ret != 0) { |
582 | 0 | return ret; |
583 | 0 | } |
584 | 0 | } |
585 | 128 | } |
586 | 16 | return 0; |
587 | 16 | } |
588 | | |
589 | | int wc_CryptoCb_GetDevIdAtIndex(int startIdx) |
590 | 73.4k | { |
591 | 73.4k | int devId = INVALID_DEVID; |
592 | 73.4k | CryptoCb* dev = wc_CryptoCb_FindDeviceByIndex(startIdx); |
593 | 73.4k | if (dev) { |
594 | 1.55k | devId = dev->devId; |
595 | 1.55k | } |
596 | 73.4k | return devId; |
597 | 73.4k | } |
598 | | |
599 | | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WOLF_CRYPTO_CB_ASYNC_POLL) |
600 | | /* Returns WC_PENDING_E while in-flight, 0 or an error when done. */ |
601 | | int wc_CryptoCb_Poll(int devId) |
602 | | { |
603 | | /* Default hard-fails: a missing or unregistered device cannot complete |
604 | | * the pending job, so never report "no pending" and leave the output |
605 | | * buffer unfilled. */ |
606 | | int ret = WC_NO_ERR_TRACE(WC_HW_E); |
607 | | /* Resolve with WC_ALGO_TYPE_CIPHER, the algo the op was submitted under, so |
608 | | * a WOLF_CRYPTO_CB_FIND remap lands on the same device as the submit. */ |
609 | | CryptoCb* dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_CIPHER); |
610 | | if (dev != NULL && dev->cb != NULL) { |
611 | | wc_CryptoInfo info; |
612 | | XMEMSET(&info, 0, sizeof(info)); |
613 | | info.algo_type = WC_ALGO_TYPE_ASYNC_POLL; |
614 | | /* Call with the resolved device id (dev->devId), matching submit-time |
615 | | * dispatch, so a remapped device is invoked under its own id. */ |
616 | | ret = dev->cb(dev->devId, &info, dev->ctx); |
617 | | if (ret == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE) || |
618 | | ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN) || |
619 | | ret == WC_NO_ERR_TRACE(WC_NO_PENDING_E)) { |
620 | | /* Device cannot complete the in-flight job (no poll support, or it |
621 | | * reports nothing pending for an op we are polling): fail hard |
622 | | * rather than let the async layer treat it as done. */ |
623 | | ret = WC_NO_ERR_TRACE(WC_HW_E); |
624 | | } |
625 | | } |
626 | | return ret; |
627 | | } |
628 | | #endif /* WOLFSSL_ASYNC_CRYPT && WOLF_CRYPTO_CB_ASYNC_POLL */ |
629 | | |
630 | | |
631 | | #ifdef WOLF_CRYPTO_CB_FIND |
632 | | /* Used to register a find device function. Useful for cases where the |
633 | | * device ID in the struct may not have been set but still wanting to use |
634 | | * a specific crypto callback device ID. The find callback is global and |
635 | | * not thread safe. */ |
636 | | void wc_CryptoCb_SetDeviceFindCb(CryptoDevCallbackFind cb) |
637 | | { |
638 | | CryptoCb_FindCb = cb; |
639 | | } |
640 | | #endif |
641 | | |
642 | | int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx) |
643 | 12 | { |
644 | 12 | int rc = 0; |
645 | 12 | CryptoCb* dev; |
646 | | |
647 | | /* INVALID_DEVID marks a free slot and cannot be registered as a device. */ |
648 | 12 | if (devId == INVALID_DEVID) |
649 | 0 | return BAD_FUNC_ARG; |
650 | | |
651 | | /* Reject re-registration of an already-registered device ID. */ |
652 | 12 | if (wc_CryptoCb_GetDevice(devId) != NULL) |
653 | 0 | return ALREADY_E; |
654 | | |
655 | | /* find a free slot */ |
656 | 12 | dev = wc_CryptoCb_GetFreeDevice(); |
657 | 12 | if (dev == NULL) |
658 | 0 | return BUFFER_E; /* out of devices */ |
659 | | |
660 | | /* Fill the entry before publishing it - see the note on gCryptoDev. */ |
661 | 12 | dev->cb = cb; |
662 | 12 | dev->ctx = ctx; |
663 | | |
664 | | #ifdef WOLF_CRYPTO_CB_CMD |
665 | | if (cb != NULL) { |
666 | | /* Invoke callback with register command */ |
667 | | wc_CryptoInfo info; |
668 | | XMEMSET(&info, 0, sizeof(info)); |
669 | | info.algo_type = WC_ALGO_TYPE_NONE; |
670 | | info.cmd.type = WC_CRYPTOCB_CMD_TYPE_REGISTER; |
671 | | info.cmd.ctx = ctx; /* cb may update on success */ |
672 | | |
673 | | rc = cb(devId, &info, ctx); |
674 | | if (rc == 0) { |
675 | | /* Success. Update dev->ctx */ |
676 | | dev->ctx = info.cmd.ctx; |
677 | | } |
678 | | else if ((rc == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) || |
679 | | (rc == WC_NO_ERR_TRACE(NOT_COMPILED_IN))) { |
680 | | /* Not implemented. Return success*/ |
681 | | rc = 0; |
682 | | } |
683 | | else { |
684 | | /* Error in callback register cmd. Don't register */ |
685 | | wc_CryptoCb_ClearDev(dev); |
686 | | return rc; |
687 | | } |
688 | | } |
689 | | #endif |
690 | | |
691 | | /* Publish the entry last, after everything it points at is in place. |
692 | | * The slot is therefore not discoverable from the register command |
693 | | * itself - a handler must not dispatch through, or re-register, its |
694 | | * own devId (a nested register of it would claim a second slot). */ |
695 | 12 | WC_BARRIER(); |
696 | 12 | dev->devId = devId; |
697 | | |
698 | 12 | return rc; |
699 | 12 | } |
700 | | |
701 | | int wc_CryptoCb_UnRegisterDeviceEx(int devId) |
702 | 0 | { |
703 | 0 | CryptoCb* dev = NULL; |
704 | | #ifdef WOLF_CRYPTO_CB_CMD |
705 | | int ret = 0; |
706 | | #endif |
707 | | |
708 | | /* Can't unregister the invalid device */ |
709 | 0 | if (devId == INVALID_DEVID) |
710 | 0 | return 0; |
711 | | |
712 | | /* Find the matching dev */ |
713 | 0 | dev = wc_CryptoCb_GetDevice(devId); |
714 | 0 | if (dev == NULL) |
715 | 0 | return 0; |
716 | | |
717 | | #ifdef WOLF_CRYPTO_CB_CMD |
718 | | if (dev->cb != NULL) { |
719 | | /* Invoke callback with unregister command.*/ |
720 | | wc_CryptoInfo info; |
721 | | XMEMSET(&info, 0, sizeof(info)); |
722 | | info.algo_type = WC_ALGO_TYPE_NONE; |
723 | | info.cmd.type = WC_CRYPTOCB_CMD_TYPE_UNREGISTER; |
724 | | info.cmd.ctx = NULL; /* Not used */ |
725 | | |
726 | | ret = dev->cb(devId, &info, dev->ctx); |
727 | | /* A callback with live device-owned objects must remain registered so |
728 | | * their free callbacks can release sensitive state and device slots. */ |
729 | | if (ret == WC_NO_ERR_TRACE(BUSY_E)) |
730 | | return ret; |
731 | | } |
732 | | #endif |
733 | 0 | wc_CryptoCb_ClearDev(dev); |
734 | 0 | return 0; |
735 | 0 | } |
736 | | |
737 | | void wc_CryptoCb_UnRegisterDevice(int devId) |
738 | 0 | { |
739 | 0 | (void)wc_CryptoCb_UnRegisterDeviceEx(devId); |
740 | 0 | } |
741 | | |
742 | | #ifndef NO_RSA |
743 | | int wc_CryptoCb_Rsa(const byte* in, word32 inLen, byte* out, |
744 | | word32* outLen, int type, RsaKey* key, WC_RNG* rng) |
745 | 0 | { |
746 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
747 | 0 | CryptoCb* dev; |
748 | |
|
749 | 0 | if (key == NULL) |
750 | 0 | return ret; |
751 | | |
752 | | /* locate registered callback */ |
753 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
754 | 0 | if (dev && dev->cb) { |
755 | 0 | wc_CryptoInfo cryptoInfo; |
756 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
757 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
758 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_RSA; |
759 | 0 | cryptoInfo.pk.rsa.in = in; |
760 | 0 | cryptoInfo.pk.rsa.inLen = inLen; |
761 | 0 | cryptoInfo.pk.rsa.out = out; |
762 | 0 | cryptoInfo.pk.rsa.outLen = outLen; |
763 | 0 | cryptoInfo.pk.rsa.type = type; |
764 | 0 | cryptoInfo.pk.rsa.key = key; |
765 | 0 | cryptoInfo.pk.rsa.rng = rng; |
766 | |
|
767 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
768 | 0 | } |
769 | |
|
770 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
771 | 0 | } |
772 | | |
773 | | #ifdef WOLF_CRYPTO_CB_RSA_PAD |
774 | | int wc_CryptoCb_RsaPad(const byte* in, word32 inLen, byte* out, |
775 | | word32* outLen, int type, RsaKey* key, WC_RNG* rng, |
776 | | RsaPadding *padding) |
777 | | { |
778 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
779 | | CryptoCb* dev; |
780 | | int pk_type; |
781 | | |
782 | | if (key == NULL) |
783 | | return ret; |
784 | | |
785 | | /* locate registered callback */ |
786 | | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
787 | | |
788 | | if (padding != NULL) { |
789 | | switch (padding->pad_type) { |
790 | | case WC_RSA_PKCSV15_PAD: |
791 | | pk_type = WC_PK_TYPE_RSA_PKCS; |
792 | | break; |
793 | | case WC_RSA_PSS_PAD: |
794 | | pk_type = WC_PK_TYPE_RSA_PSS; |
795 | | break; |
796 | | case WC_RSA_OAEP_PAD: |
797 | | pk_type = WC_PK_TYPE_RSA_OAEP; |
798 | | break; |
799 | | default: |
800 | | pk_type = WC_PK_TYPE_RSA; |
801 | | } |
802 | | } else { |
803 | | pk_type = WC_PK_TYPE_RSA; |
804 | | } |
805 | | |
806 | | if (dev && dev->cb) { |
807 | | wc_CryptoInfo cryptoInfo; |
808 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
809 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
810 | | cryptoInfo.pk.type = pk_type; |
811 | | cryptoInfo.pk.rsa.in = in; |
812 | | cryptoInfo.pk.rsa.inLen = inLen; |
813 | | cryptoInfo.pk.rsa.out = out; |
814 | | cryptoInfo.pk.rsa.outLen = outLen; |
815 | | cryptoInfo.pk.rsa.type = type; |
816 | | cryptoInfo.pk.rsa.key = key; |
817 | | cryptoInfo.pk.rsa.rng = rng; |
818 | | cryptoInfo.pk.rsa.padding = padding; |
819 | | |
820 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
821 | | } |
822 | | |
823 | | return wc_CryptoCb_TranslateErrorCode(ret); |
824 | | } |
825 | | |
826 | | /* Verify an RSA-PSS signature on the device (padding included). Passes both the |
827 | | * signature and digest so the device does the whole verify and returns a verdict. */ |
828 | | int wc_CryptoCb_RsaPssVerify(const byte* sig, word32 sigSz, const byte* digest, |
829 | | word32 digestSz, enum wc_HashType hash, int mgf, int saltLen, RsaKey* key, |
830 | | int* res, byte* out, word32 outSz, word32* outLen) |
831 | | { |
832 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
833 | | CryptoCb* dev; |
834 | | |
835 | | if (key == NULL) |
836 | | return ret; |
837 | | |
838 | | /* locate registered callback */ |
839 | | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
840 | | |
841 | | if (dev && dev->cb) { |
842 | | wc_CryptoInfo cryptoInfo; |
843 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
844 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
845 | | cryptoInfo.pk.type = WC_PK_TYPE_RSA_PSS_VERIFY; |
846 | | cryptoInfo.pk.rsa_pss_verify.sig = sig; |
847 | | cryptoInfo.pk.rsa_pss_verify.sigSz = sigSz; |
848 | | cryptoInfo.pk.rsa_pss_verify.digest = digest; |
849 | | cryptoInfo.pk.rsa_pss_verify.digestSz = digestSz; |
850 | | cryptoInfo.pk.rsa_pss_verify.hash = hash; |
851 | | cryptoInfo.pk.rsa_pss_verify.mgf = mgf; |
852 | | cryptoInfo.pk.rsa_pss_verify.saltLen = saltLen; |
853 | | cryptoInfo.pk.rsa_pss_verify.key = key; |
854 | | cryptoInfo.pk.rsa_pss_verify.res = res; |
855 | | cryptoInfo.pk.rsa_pss_verify.out = out; |
856 | | cryptoInfo.pk.rsa_pss_verify.outSz = outSz; |
857 | | cryptoInfo.pk.rsa_pss_verify.outLen = outLen; |
858 | | |
859 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
860 | | } |
861 | | |
862 | | return wc_CryptoCb_TranslateErrorCode(ret); |
863 | | } |
864 | | #endif /* WOLF_CRYPTO_CB_RSA_PAD */ |
865 | | |
866 | | #ifdef WOLFSSL_KEY_GEN |
867 | | int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng) |
868 | 0 | { |
869 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
870 | 0 | CryptoCb* dev; |
871 | |
|
872 | 0 | if (key == NULL) |
873 | 0 | return ret; |
874 | | |
875 | | /* locate registered callback */ |
876 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
877 | 0 | if (dev && dev->cb) { |
878 | 0 | wc_CryptoInfo cryptoInfo; |
879 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
880 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
881 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_RSA_KEYGEN; |
882 | 0 | cryptoInfo.pk.rsakg.key = key; |
883 | 0 | cryptoInfo.pk.rsakg.size = size; |
884 | 0 | cryptoInfo.pk.rsakg.e = e; |
885 | 0 | cryptoInfo.pk.rsakg.rng = rng; |
886 | |
|
887 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
888 | 0 | } |
889 | |
|
890 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
891 | 0 | } |
892 | | #endif |
893 | | |
894 | | int wc_CryptoCb_RsaCheckPrivKey(RsaKey* key, const byte* pubKey, |
895 | | word32 pubKeySz) |
896 | 0 | { |
897 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
898 | 0 | CryptoCb* dev; |
899 | |
|
900 | 0 | if (key == NULL) |
901 | 0 | return ret; |
902 | | |
903 | | /* locate registered callback */ |
904 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
905 | 0 | if (dev && dev->cb) { |
906 | 0 | wc_CryptoInfo cryptoInfo; |
907 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
908 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
909 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_RSA_CHECK_PRIV_KEY; |
910 | 0 | cryptoInfo.pk.rsa_check.key = key; |
911 | 0 | cryptoInfo.pk.rsa_check.pubKey = pubKey; |
912 | 0 | cryptoInfo.pk.rsa_check.pubKeySz = pubKeySz; |
913 | |
|
914 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
915 | 0 | } |
916 | |
|
917 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
918 | 0 | } |
919 | | |
920 | | int wc_CryptoCb_RsaGetSize(const RsaKey* key, int* keySize) |
921 | 0 | { |
922 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
923 | 0 | CryptoCb* dev; |
924 | |
|
925 | 0 | if (key == NULL) |
926 | 0 | return ret; |
927 | | |
928 | | /* locate registered callback */ |
929 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
930 | 0 | if (dev && dev->cb) { |
931 | 0 | wc_CryptoInfo cryptoInfo; |
932 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
933 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
934 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_RSA_GET_SIZE; |
935 | 0 | cryptoInfo.pk.rsa_get_size.key = key; |
936 | 0 | cryptoInfo.pk.rsa_get_size.keySize = keySize; |
937 | |
|
938 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
939 | 0 | } |
940 | |
|
941 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
942 | 0 | } |
943 | | #endif /* !NO_RSA */ |
944 | | |
945 | | #ifndef NO_DH |
946 | | int wc_CryptoCb_Dh(DhKey* key, const byte* priv, word32 privSz, |
947 | | const byte* otherPub, word32 pubSz, byte* agree, word32* agreeSz) |
948 | 0 | { |
949 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
950 | 0 | CryptoCb* dev; |
951 | |
|
952 | 0 | if (key == NULL) |
953 | 0 | return ret; |
954 | | |
955 | | /* locate registered callback */ |
956 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
957 | 0 | if (dev && dev->cb) { |
958 | 0 | wc_CryptoInfo cryptoInfo; |
959 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
960 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
961 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_DH; |
962 | 0 | cryptoInfo.pk.dh.key = key; |
963 | 0 | cryptoInfo.pk.dh.priv = priv; |
964 | 0 | cryptoInfo.pk.dh.privSz = privSz; |
965 | 0 | cryptoInfo.pk.dh.otherPub = otherPub; |
966 | 0 | cryptoInfo.pk.dh.pubSz = pubSz; |
967 | 0 | cryptoInfo.pk.dh.agree = agree; |
968 | 0 | cryptoInfo.pk.dh.agreeSz = agreeSz; |
969 | |
|
970 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
971 | 0 | } |
972 | |
|
973 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
974 | 0 | } |
975 | | #endif /* !NO_DH */ |
976 | | |
977 | | #ifdef HAVE_ECC |
978 | | #ifdef HAVE_ECC_DHE |
979 | | int wc_CryptoCb_MakeEccKey(WC_RNG* rng, int keySize, ecc_key* key, int curveId) |
980 | 0 | { |
981 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
982 | 0 | CryptoCb* dev; |
983 | |
|
984 | 0 | if (key == NULL) |
985 | 0 | return ret; |
986 | | |
987 | | /* locate registered callback */ |
988 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
989 | 0 | if (dev && dev->cb) { |
990 | 0 | wc_CryptoInfo cryptoInfo; |
991 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
992 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
993 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_KEYGEN; |
994 | 0 | cryptoInfo.pk.eckg.rng = rng; |
995 | 0 | cryptoInfo.pk.eckg.size = keySize; |
996 | 0 | cryptoInfo.pk.eckg.key = key; |
997 | 0 | cryptoInfo.pk.eckg.curveId = curveId; |
998 | |
|
999 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1000 | 0 | } |
1001 | |
|
1002 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1003 | 0 | } |
1004 | | |
1005 | | int wc_CryptoCb_Ecdh(ecc_key* private_key, ecc_key* public_key, |
1006 | | byte* out, word32* outlen) |
1007 | 0 | { |
1008 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1009 | 0 | CryptoCb* dev; |
1010 | |
|
1011 | 0 | if (private_key == NULL) |
1012 | 0 | return ret; |
1013 | | |
1014 | | /* locate registered callback */ |
1015 | 0 | dev = wc_CryptoCb_FindDevice(private_key->devId, WC_ALGO_TYPE_PK); |
1016 | 0 | if (dev && dev->cb) { |
1017 | 0 | wc_CryptoInfo cryptoInfo; |
1018 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1019 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1020 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ECDH; |
1021 | 0 | cryptoInfo.pk.ecdh.private_key = private_key; |
1022 | 0 | cryptoInfo.pk.ecdh.public_key = public_key; |
1023 | 0 | cryptoInfo.pk.ecdh.out = out; |
1024 | 0 | cryptoInfo.pk.ecdh.outlen = outlen; |
1025 | |
|
1026 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1027 | 0 | } |
1028 | |
|
1029 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1030 | 0 | } |
1031 | | #endif |
1032 | | |
1033 | | #ifdef HAVE_ECC_SIGN |
1034 | | int wc_CryptoCb_EccSign(const byte* in, word32 inlen, byte* out, |
1035 | | word32 *outlen, WC_RNG* rng, ecc_key* key) |
1036 | 0 | { |
1037 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1038 | 0 | CryptoCb* dev; |
1039 | |
|
1040 | 0 | if (key == NULL) |
1041 | 0 | return ret; |
1042 | | |
1043 | | /* locate registered callback */ |
1044 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1045 | 0 | if (dev && dev->cb) { |
1046 | 0 | wc_CryptoInfo cryptoInfo; |
1047 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1048 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1049 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_SIGN; |
1050 | 0 | cryptoInfo.pk.eccsign.in = in; |
1051 | 0 | cryptoInfo.pk.eccsign.inlen = inlen; |
1052 | 0 | cryptoInfo.pk.eccsign.out = out; |
1053 | 0 | cryptoInfo.pk.eccsign.outlen = outlen; |
1054 | 0 | cryptoInfo.pk.eccsign.rng = rng; |
1055 | 0 | cryptoInfo.pk.eccsign.key = key; |
1056 | |
|
1057 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1058 | 0 | } |
1059 | |
|
1060 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1061 | 0 | } |
1062 | | #endif |
1063 | | |
1064 | | #ifdef HAVE_ECC_VERIFY |
1065 | | int wc_CryptoCb_EccVerify(const byte* sig, word32 siglen, |
1066 | | const byte* hash, word32 hashlen, int* res, ecc_key* key) |
1067 | 0 | { |
1068 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1069 | 0 | CryptoCb* dev; |
1070 | |
|
1071 | 0 | if (key == NULL) |
1072 | 0 | return ret; |
1073 | | |
1074 | | /* locate registered callback */ |
1075 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1076 | 0 | if (dev && dev->cb) { |
1077 | 0 | wc_CryptoInfo cryptoInfo; |
1078 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1079 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1080 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_VERIFY; |
1081 | 0 | cryptoInfo.pk.eccverify.sig = sig; |
1082 | 0 | cryptoInfo.pk.eccverify.siglen = siglen; |
1083 | 0 | cryptoInfo.pk.eccverify.hash = hash; |
1084 | 0 | cryptoInfo.pk.eccverify.hashlen = hashlen; |
1085 | 0 | cryptoInfo.pk.eccverify.res = res; |
1086 | 0 | cryptoInfo.pk.eccverify.key = key; |
1087 | |
|
1088 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1089 | 0 | } |
1090 | |
|
1091 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1092 | 0 | } |
1093 | | #endif |
1094 | | |
1095 | | #ifdef HAVE_ECC_CHECK_KEY |
1096 | | int wc_CryptoCb_EccCheckPrivKey(ecc_key* key, const byte* pubKey, |
1097 | | word32 pubKeySz) |
1098 | 0 | { |
1099 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1100 | 0 | CryptoCb* dev; |
1101 | |
|
1102 | 0 | if (key == NULL) |
1103 | 0 | return ret; |
1104 | | |
1105 | | /* locate registered callback */ |
1106 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1107 | 0 | if (dev && dev->cb) { |
1108 | 0 | wc_CryptoInfo cryptoInfo; |
1109 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1110 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1111 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_CHECK_PRIV_KEY; |
1112 | 0 | cryptoInfo.pk.ecc_check.key = key; |
1113 | 0 | cryptoInfo.pk.ecc_check.pubKey = pubKey; |
1114 | 0 | cryptoInfo.pk.ecc_check.pubKeySz = pubKeySz; |
1115 | |
|
1116 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1117 | 0 | } |
1118 | |
|
1119 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1120 | 0 | } |
1121 | | #endif |
1122 | | |
1123 | | int wc_CryptoCb_EccGetSize(const ecc_key* key, int* keySize) |
1124 | 0 | { |
1125 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1126 | 0 | CryptoCb* dev; |
1127 | |
|
1128 | 0 | if (key == NULL) |
1129 | 0 | return ret; |
1130 | | |
1131 | | /* locate registered callback */ |
1132 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1133 | 0 | if (dev && dev->cb) { |
1134 | 0 | wc_CryptoInfo cryptoInfo; |
1135 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1136 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1137 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_GET_SIZE; |
1138 | 0 | cryptoInfo.pk.ecc_get_size.key = key; |
1139 | 0 | cryptoInfo.pk.ecc_get_size.keySize = keySize; |
1140 | |
|
1141 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1142 | 0 | } |
1143 | |
|
1144 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1145 | 0 | } |
1146 | | |
1147 | | int wc_CryptoCb_EccGetSigSize(const ecc_key* key, int* sigSize) |
1148 | 0 | { |
1149 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1150 | 0 | CryptoCb* dev; |
1151 | |
|
1152 | 0 | if (key == NULL) |
1153 | 0 | return ret; |
1154 | | |
1155 | | /* locate registered callback */ |
1156 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1157 | 0 | if (dev && dev->cb) { |
1158 | 0 | wc_CryptoInfo cryptoInfo; |
1159 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1160 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1161 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_GET_SIG_SIZE; |
1162 | 0 | cryptoInfo.pk.ecc_get_sig_size.key = key; |
1163 | 0 | cryptoInfo.pk.ecc_get_sig_size.sigSize = sigSize; |
1164 | |
|
1165 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1166 | 0 | } |
1167 | |
|
1168 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1169 | 0 | } |
1170 | | |
1171 | | int wc_CryptoCb_EccMakePub(ecc_key* key, ecc_point* pubOut) |
1172 | 0 | { |
1173 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1174 | 0 | CryptoCb* dev; |
1175 | |
|
1176 | 0 | if (key == NULL || pubOut == NULL || key->dp == NULL) |
1177 | 0 | return ret; |
1178 | | |
1179 | 0 | if (key->dp->size > MAX_ECC_BYTES) |
1180 | 0 | return ret; |
1181 | | |
1182 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1183 | 0 | if (dev && dev->cb) { |
1184 | 0 | wc_CryptoInfo cryptoInfo; |
1185 | 0 | word32 curveSz = (word32)key->dp->size; |
1186 | 0 | word32 ptSz = 1 + 2 * curveSz; /* X9.63 uncompressed length */ |
1187 | 0 | word32 outSz = 1 + 2 * MAX_ECC_BYTES; /* buffer size on input */ |
1188 | 0 | WC_DECLARE_VAR(buf, byte, (1 + 2 * MAX_ECC_BYTES), key->heap); |
1189 | 0 | WC_ALLOC_VAR_EX(buf, byte, (1 + 2 * MAX_ECC_BYTES), key->heap, |
1190 | 0 | DYNAMIC_TYPE_ECC_BUFFER, return MEMORY_E); |
1191 | | |
1192 | | /* zero the result buffer so a handler that returns success without |
1193 | | * writing output is rejected deterministically by the tag check */ |
1194 | 0 | XMEMSET(buf, 0, ptSz); |
1195 | |
|
1196 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1197 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1198 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_MAKE_PUB; |
1199 | 0 | cryptoInfo.pk.ecc_make_pub.key = key; |
1200 | 0 | cryptoInfo.pk.ecc_make_pub.pubOut = buf; |
1201 | 0 | cryptoInfo.pk.ecc_make_pub.pubOutSz = &outSz; |
1202 | |
|
1203 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1204 | | |
1205 | | /* deserialize X9.63 uncompressed result into pubOut; reject a result |
1206 | | * whose size is not exactly the curve's X9.63 length */ |
1207 | 0 | if (ret == 0) { |
1208 | 0 | if (outSz != ptSz || buf[0] != ECC_POINT_UNCOMP) { |
1209 | 0 | ret = BUFFER_E; |
1210 | 0 | } |
1211 | 0 | else { |
1212 | 0 | int err = mp_read_unsigned_bin(pubOut->x, buf + 1, curveSz); |
1213 | 0 | if (err == MP_OKAY) |
1214 | 0 | err = mp_read_unsigned_bin(pubOut->y, buf + 1 + curveSz, |
1215 | 0 | curveSz); |
1216 | 0 | if (err == MP_OKAY) |
1217 | 0 | err = mp_set(pubOut->z, 1); |
1218 | 0 | if (err != MP_OKAY) |
1219 | 0 | ret = err; |
1220 | 0 | } |
1221 | 0 | } |
1222 | |
|
1223 | 0 | WC_FREE_VAR_EX(buf, key->heap, DYNAMIC_TYPE_ECC_BUFFER); |
1224 | 0 | } |
1225 | | |
1226 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1227 | 0 | } |
1228 | | |
1229 | | #ifdef HAVE_ECC_CHECK_KEY |
1230 | | int wc_CryptoCb_EccCheckPubKey(ecc_key* key, int checkOrder, int checkPriv) |
1231 | 0 | { |
1232 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1233 | 0 | CryptoCb* dev; |
1234 | |
|
1235 | 0 | if (key == NULL || key->dp == NULL) |
1236 | 0 | return ret; |
1237 | | |
1238 | 0 | if (key->dp->size > MAX_ECC_BYTES) |
1239 | 0 | return ret; |
1240 | | |
1241 | | /* locate registered callback */ |
1242 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1243 | 0 | if (dev && dev->cb) { |
1244 | 0 | wc_CryptoInfo cryptoInfo; |
1245 | 0 | word32 curveSz = (word32)key->dp->size; |
1246 | 0 | word32 ptSz = 1 + 2 * curveSz; |
1247 | 0 | word32 xSz = curveSz; |
1248 | 0 | word32 ySz = curveSz; |
1249 | | /* a key with no host-side public point is represented by |
1250 | | * ECC_PRIVATEKEY_ONLY and crosses as pubKey = NULL / pubKeySz = 0. |
1251 | | * If the key state says a public point exists, serialize it even when |
1252 | | * the coordinates are invalid (e.g. 0,0) so the device can reject the |
1253 | | * actual input instead of seeing "no public point". */ |
1254 | 0 | int havePub = (key->type != ECC_PRIVATEKEY_ONLY); |
1255 | 0 | WC_DECLARE_VAR(buf, byte, (1 + 2 * MAX_ECC_BYTES), key->heap); |
1256 | |
|
1257 | 0 | ret = MP_OKAY; |
1258 | 0 | if (havePub) { |
1259 | 0 | WC_ALLOC_VAR_EX(buf, byte, (1 + 2 * MAX_ECC_BYTES), key->heap, |
1260 | 0 | DYNAMIC_TYPE_ECC_BUFFER, return MEMORY_E); |
1261 | | /* serialize key->pubkey to X9.63 uncompressed (0x04 || X || Y) */ |
1262 | 0 | buf[0] = ECC_POINT_UNCOMP; |
1263 | 0 | ret = wc_export_int(key->pubkey.x, buf + 1, &xSz, curveSz, |
1264 | 0 | WC_TYPE_UNSIGNED_BIN); |
1265 | 0 | if (ret == MP_OKAY) |
1266 | 0 | ret = wc_export_int(key->pubkey.y, buf + 1 + curveSz, &ySz, |
1267 | 0 | curveSz, WC_TYPE_UNSIGNED_BIN); |
1268 | 0 | } |
1269 | | |
1270 | 0 | if (ret == MP_OKAY) { |
1271 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1272 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1273 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_EC_CHECK_PUB_KEY; |
1274 | 0 | cryptoInfo.pk.ecc_check_pub.key = key; |
1275 | 0 | cryptoInfo.pk.ecc_check_pub.pubKey = havePub ? buf : NULL; |
1276 | 0 | cryptoInfo.pk.ecc_check_pub.pubKeySz = havePub ? ptSz : 0; |
1277 | 0 | cryptoInfo.pk.ecc_check_pub.checkOrder = checkOrder; |
1278 | 0 | cryptoInfo.pk.ecc_check_pub.checkPriv = checkPriv; |
1279 | |
|
1280 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1281 | 0 | } |
1282 | |
|
1283 | 0 | if (havePub) { |
1284 | 0 | WC_FREE_VAR_EX(buf, key->heap, DYNAMIC_TYPE_ECC_BUFFER); |
1285 | 0 | } |
1286 | 0 | } |
1287 | | |
1288 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1289 | 0 | } |
1290 | | #endif /* HAVE_ECC_CHECK_KEY */ |
1291 | | |
1292 | | #ifdef HAVE_ECC_ENCRYPT |
1293 | | int wc_CryptoCb_EciesEncrypt(int devId, ecc_key* privKey, ecc_key* pubKey, |
1294 | | const byte* msg, word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx, |
1295 | | int compressed) |
1296 | 0 | { |
1297 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1298 | 0 | CryptoCb* dev; |
1299 | |
|
1300 | 0 | if (privKey == NULL) |
1301 | 0 | return ret; |
1302 | | |
1303 | | /* find the registered callback. The device comes from the ECIES |
1304 | | * context, not from privKey->devId. */ |
1305 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1306 | 0 | if (dev && dev->cb) { |
1307 | 0 | wc_CryptoInfo cryptoInfo; |
1308 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1309 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1310 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ECIES_ENCRYPT; |
1311 | 0 | cryptoInfo.pk.eciesencrypt.privKey = privKey; |
1312 | 0 | cryptoInfo.pk.eciesencrypt.pubKey = pubKey; |
1313 | 0 | cryptoInfo.pk.eciesencrypt.msg = msg; |
1314 | 0 | cryptoInfo.pk.eciesencrypt.msgSz = msgSz; |
1315 | 0 | cryptoInfo.pk.eciesencrypt.out = out; |
1316 | 0 | cryptoInfo.pk.eciesencrypt.outSz = outSz; |
1317 | 0 | cryptoInfo.pk.eciesencrypt.ctx = ctx; |
1318 | 0 | cryptoInfo.pk.eciesencrypt.compressed = compressed; |
1319 | |
|
1320 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1321 | 0 | } |
1322 | |
|
1323 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1324 | 0 | } |
1325 | | |
1326 | | int wc_CryptoCb_EciesDecrypt(int devId, ecc_key* privKey, ecc_key* pubKey, |
1327 | | const byte* msg, word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx) |
1328 | 0 | { |
1329 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1330 | 0 | CryptoCb* dev; |
1331 | |
|
1332 | 0 | if (privKey == NULL) |
1333 | 0 | return ret; |
1334 | | |
1335 | | /* find the registered callback. The device comes from the ECIES |
1336 | | * context, not from privKey->devId. */ |
1337 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1338 | 0 | if (dev && dev->cb) { |
1339 | 0 | wc_CryptoInfo cryptoInfo; |
1340 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1341 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1342 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ECIES_DECRYPT; |
1343 | 0 | cryptoInfo.pk.eciesdecrypt.privKey = privKey; |
1344 | 0 | cryptoInfo.pk.eciesdecrypt.pubKey = pubKey; |
1345 | 0 | cryptoInfo.pk.eciesdecrypt.msg = msg; |
1346 | 0 | cryptoInfo.pk.eciesdecrypt.msgSz = msgSz; |
1347 | 0 | cryptoInfo.pk.eciesdecrypt.out = out; |
1348 | 0 | cryptoInfo.pk.eciesdecrypt.outSz = outSz; |
1349 | 0 | cryptoInfo.pk.eciesdecrypt.ctx = ctx; |
1350 | |
|
1351 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1352 | 0 | } |
1353 | |
|
1354 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1355 | 0 | } |
1356 | | #endif /* HAVE_ECC_ENCRYPT */ |
1357 | | #endif /* HAVE_ECC */ |
1358 | | |
1359 | | #if defined(WOLFSSL_SM2) |
1360 | | int wc_CryptoCb_Sm2Sign(const byte* in, word32 inlen, byte* out, |
1361 | | word32* outlen, WC_RNG* rng, ecc_key* key) |
1362 | 0 | { |
1363 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1364 | 0 | CryptoCb* dev; |
1365 | |
|
1366 | 0 | if (key == NULL) |
1367 | 0 | return ret; |
1368 | | |
1369 | | /* locate registered callback */ |
1370 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1371 | 0 | if (dev && dev->cb) { |
1372 | 0 | wc_CryptoInfo cryptoInfo; |
1373 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1374 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1375 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_SM2_SIGN; |
1376 | 0 | cryptoInfo.pk.sm2sign.in = in; |
1377 | 0 | cryptoInfo.pk.sm2sign.inlen = inlen; |
1378 | 0 | cryptoInfo.pk.sm2sign.out = out; |
1379 | 0 | cryptoInfo.pk.sm2sign.outlen = outlen; |
1380 | 0 | cryptoInfo.pk.sm2sign.rng = rng; |
1381 | 0 | cryptoInfo.pk.sm2sign.key = key; |
1382 | |
|
1383 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1384 | 0 | } |
1385 | |
|
1386 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1387 | 0 | } |
1388 | | |
1389 | | int wc_CryptoCb_Sm2Verify(const byte* sig, word32 siglen, |
1390 | | const byte* hash, word32 hashlen, int* res, ecc_key* key) |
1391 | 0 | { |
1392 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1393 | 0 | CryptoCb* dev; |
1394 | |
|
1395 | 0 | if (key == NULL) |
1396 | 0 | return ret; |
1397 | | |
1398 | | /* locate registered callback */ |
1399 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1400 | 0 | if (dev && dev->cb) { |
1401 | 0 | wc_CryptoInfo cryptoInfo; |
1402 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1403 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1404 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_SM2_VERIFY; |
1405 | 0 | cryptoInfo.pk.sm2verify.sig = sig; |
1406 | 0 | cryptoInfo.pk.sm2verify.siglen = siglen; |
1407 | 0 | cryptoInfo.pk.sm2verify.hash = hash; |
1408 | 0 | cryptoInfo.pk.sm2verify.hashlen = hashlen; |
1409 | 0 | cryptoInfo.pk.sm2verify.res = res; |
1410 | 0 | cryptoInfo.pk.sm2verify.key = key; |
1411 | |
|
1412 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1413 | 0 | } |
1414 | |
|
1415 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1416 | 0 | } |
1417 | | |
1418 | | int wc_CryptoCb_Sm2SharedSecret(ecc_key* private_key, ecc_key* public_key, |
1419 | | byte* out, word32* outlen) |
1420 | 0 | { |
1421 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1422 | 0 | CryptoCb* dev; |
1423 | |
|
1424 | 0 | if (private_key == NULL) |
1425 | 0 | return ret; |
1426 | | |
1427 | | /* locate registered callback */ |
1428 | 0 | dev = wc_CryptoCb_FindDevice(private_key->devId, WC_ALGO_TYPE_PK); |
1429 | 0 | if (dev && dev->cb) { |
1430 | 0 | wc_CryptoInfo cryptoInfo; |
1431 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1432 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1433 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_SM2_SHARED_SECRET; |
1434 | 0 | cryptoInfo.pk.sm2dh.private_key = private_key; |
1435 | 0 | cryptoInfo.pk.sm2dh.public_key = public_key; |
1436 | 0 | cryptoInfo.pk.sm2dh.out = out; |
1437 | 0 | cryptoInfo.pk.sm2dh.outlen = outlen; |
1438 | |
|
1439 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1440 | 0 | } |
1441 | |
|
1442 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1443 | 0 | } |
1444 | | |
1445 | | int wc_CryptoCb_Sm2CreateDigest(const byte* id, word16 idSz, |
1446 | | const byte* msg, int msgSz, enum wc_HashType hashType, byte* out, |
1447 | | int outSz, ecc_key* key) |
1448 | 0 | { |
1449 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1450 | 0 | CryptoCb* dev; |
1451 | |
|
1452 | 0 | if (key == NULL) |
1453 | 0 | return ret; |
1454 | | |
1455 | | /* locate registered callback */ |
1456 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1457 | 0 | if (dev && dev->cb) { |
1458 | 0 | wc_CryptoInfo cryptoInfo; |
1459 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1460 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1461 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_SM2_CREATE_DIGEST; |
1462 | 0 | cryptoInfo.pk.sm2digest.id = id; |
1463 | 0 | cryptoInfo.pk.sm2digest.idSz = idSz; |
1464 | 0 | cryptoInfo.pk.sm2digest.msg = msg; |
1465 | 0 | cryptoInfo.pk.sm2digest.msgSz = msgSz; |
1466 | 0 | cryptoInfo.pk.sm2digest.hashType = hashType; |
1467 | 0 | cryptoInfo.pk.sm2digest.out = out; |
1468 | 0 | cryptoInfo.pk.sm2digest.outSz = outSz; |
1469 | 0 | cryptoInfo.pk.sm2digest.key = key; |
1470 | |
|
1471 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1472 | 0 | } |
1473 | |
|
1474 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1475 | 0 | } |
1476 | | #endif /* WOLFSSL_SM2 */ |
1477 | | |
1478 | | #ifdef HAVE_CURVE25519 |
1479 | | int wc_CryptoCb_Curve25519Gen(WC_RNG* rng, int keySize, |
1480 | | curve25519_key* key) |
1481 | 0 | { |
1482 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1483 | 0 | CryptoCb* dev; |
1484 | |
|
1485 | 0 | if (key == NULL) |
1486 | 0 | return ret; |
1487 | | |
1488 | | /* locate registered callback */ |
1489 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1490 | 0 | if (dev && dev->cb) { |
1491 | 0 | wc_CryptoInfo cryptoInfo; |
1492 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1493 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1494 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519_KEYGEN; |
1495 | 0 | cryptoInfo.pk.curve25519kg.rng = rng; |
1496 | 0 | cryptoInfo.pk.curve25519kg.size = keySize; |
1497 | 0 | cryptoInfo.pk.curve25519kg.key = key; |
1498 | |
|
1499 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1500 | 0 | } |
1501 | |
|
1502 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1503 | 0 | } |
1504 | | |
1505 | | int wc_CryptoCb_Curve25519(curve25519_key* private_key, |
1506 | | curve25519_key* public_key, byte* out, word32* outlen, int endian) |
1507 | 0 | { |
1508 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1509 | 0 | CryptoCb* dev; |
1510 | |
|
1511 | 0 | if (private_key == NULL) |
1512 | 0 | return ret; |
1513 | | |
1514 | | /* locate registered callback */ |
1515 | 0 | dev = wc_CryptoCb_FindDevice(private_key->devId, WC_ALGO_TYPE_PK); |
1516 | 0 | if (dev && dev->cb) { |
1517 | 0 | wc_CryptoInfo cryptoInfo; |
1518 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1519 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1520 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519; |
1521 | 0 | cryptoInfo.pk.curve25519.private_key = private_key; |
1522 | 0 | cryptoInfo.pk.curve25519.public_key = public_key; |
1523 | 0 | cryptoInfo.pk.curve25519.out = out; |
1524 | 0 | cryptoInfo.pk.curve25519.outlen = outlen; |
1525 | 0 | cryptoInfo.pk.curve25519.endian = endian; |
1526 | |
|
1527 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1528 | 0 | } |
1529 | |
|
1530 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1531 | 0 | } |
1532 | | |
1533 | | int wc_CryptoCb_Curve25519MakePub(int devId, int public_size, byte* pub, |
1534 | | int private_size, const byte* priv) |
1535 | 1.33k | { |
1536 | 1.33k | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1537 | 1.33k | CryptoCb* dev; |
1538 | | |
1539 | 1.33k | if (pub == NULL || priv == NULL) |
1540 | 0 | return ret; |
1541 | | |
1542 | | /* locate registered callback */ |
1543 | 1.33k | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1544 | | /* only a caller that selected no device settles for the first registered |
1545 | | * one; a devId names the single device allowed to see the scalar */ |
1546 | 1.33k | if ((dev == NULL || dev->cb == NULL) && (devId == INVALID_DEVID)) |
1547 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1548 | 1.33k | if (dev && dev->cb) { |
1549 | 1.33k | wc_CryptoInfo cryptoInfo; |
1550 | 1.33k | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1551 | 1.33k | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1552 | 1.33k | cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519_MAKE_PUB; |
1553 | 1.33k | cryptoInfo.pk.curve25519makepub.pub = pub; |
1554 | 1.33k | cryptoInfo.pk.curve25519makepub.pubSz = (word32)public_size; |
1555 | 1.33k | cryptoInfo.pk.curve25519makepub.priv = priv; |
1556 | 1.33k | cryptoInfo.pk.curve25519makepub.privSz = (word32)private_size; |
1557 | | |
1558 | 1.33k | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1559 | 1.33k | } |
1560 | | |
1561 | 1.33k | return wc_CryptoCb_TranslateErrorCode(ret); |
1562 | 1.33k | } |
1563 | | |
1564 | | int wc_CryptoCb_Curve25519Generic(int devId, int public_size, byte* pub, |
1565 | | int private_size, const byte* priv, int basepoint_size, |
1566 | | const byte* basepoint) |
1567 | 0 | { |
1568 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1569 | 0 | CryptoCb* dev; |
1570 | |
|
1571 | 0 | if (pub == NULL || priv == NULL || basepoint == NULL) |
1572 | 0 | return ret; |
1573 | | |
1574 | | /* locate registered callback */ |
1575 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1576 | | /* only a caller that selected no device settles for the first registered |
1577 | | * one; a devId names the single device allowed to see the scalar */ |
1578 | 0 | if ((dev == NULL || dev->cb == NULL) && (devId == INVALID_DEVID)) |
1579 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1580 | 0 | if (dev && dev->cb) { |
1581 | 0 | wc_CryptoInfo cryptoInfo; |
1582 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1583 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1584 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519_GENERIC; |
1585 | 0 | cryptoInfo.pk.curve25519generic.pub = pub; |
1586 | 0 | cryptoInfo.pk.curve25519generic.pubSz = (word32)public_size; |
1587 | 0 | cryptoInfo.pk.curve25519generic.priv = priv; |
1588 | 0 | cryptoInfo.pk.curve25519generic.privSz = (word32)private_size; |
1589 | 0 | cryptoInfo.pk.curve25519generic.basepoint = basepoint; |
1590 | 0 | cryptoInfo.pk.curve25519generic.basepointSz = (word32)basepoint_size; |
1591 | |
|
1592 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1593 | 0 | } |
1594 | |
|
1595 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1596 | 0 | } |
1597 | | #endif /* HAVE_CURVE25519 */ |
1598 | | |
1599 | | #ifdef HAVE_ED25519 |
1600 | | int wc_CryptoCb_Ed25519Gen(WC_RNG* rng, int keySize, |
1601 | | ed25519_key* key) |
1602 | 0 | { |
1603 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1604 | 0 | CryptoCb* dev; |
1605 | |
|
1606 | 0 | if (key == NULL) |
1607 | 0 | return ret; |
1608 | | |
1609 | | /* locate registered callback */ |
1610 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1611 | 0 | if (dev && dev->cb) { |
1612 | 0 | wc_CryptoInfo cryptoInfo; |
1613 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1614 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1615 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED25519_KEYGEN; |
1616 | 0 | cryptoInfo.pk.ed25519kg.rng = rng; |
1617 | 0 | cryptoInfo.pk.ed25519kg.size = keySize; |
1618 | 0 | cryptoInfo.pk.ed25519kg.key = key; |
1619 | |
|
1620 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1621 | 0 | } |
1622 | |
|
1623 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1624 | 0 | } |
1625 | | |
1626 | | int wc_CryptoCb_Ed25519Sign(const byte* in, word32 inLen, byte* out, |
1627 | | word32 *outLen, ed25519_key* key, byte type, |
1628 | | const byte* context, byte contextLen) |
1629 | 0 | { |
1630 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1631 | 0 | CryptoCb* dev; |
1632 | |
|
1633 | 0 | if (key == NULL) |
1634 | 0 | return ret; |
1635 | | |
1636 | | /* locate registered callback */ |
1637 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1638 | 0 | if (dev && dev->cb) { |
1639 | 0 | wc_CryptoInfo cryptoInfo; |
1640 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1641 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1642 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED25519_SIGN; |
1643 | 0 | cryptoInfo.pk.ed25519sign.in = in; |
1644 | 0 | cryptoInfo.pk.ed25519sign.inLen = inLen; |
1645 | 0 | cryptoInfo.pk.ed25519sign.out = out; |
1646 | 0 | cryptoInfo.pk.ed25519sign.outLen = outLen; |
1647 | 0 | cryptoInfo.pk.ed25519sign.key = key; |
1648 | 0 | cryptoInfo.pk.ed25519sign.type = type; |
1649 | 0 | cryptoInfo.pk.ed25519sign.context = context; |
1650 | 0 | cryptoInfo.pk.ed25519sign.contextLen = contextLen; |
1651 | |
|
1652 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1653 | 0 | } |
1654 | |
|
1655 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1656 | 0 | } |
1657 | | |
1658 | | int wc_CryptoCb_Ed25519Verify(const byte* sig, word32 sigLen, |
1659 | | const byte* msg, word32 msgLen, int* res, ed25519_key* key, byte type, |
1660 | | const byte* context, byte contextLen) |
1661 | 0 | { |
1662 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1663 | 0 | CryptoCb* dev; |
1664 | |
|
1665 | 0 | if (key == NULL) |
1666 | 0 | return ret; |
1667 | | |
1668 | | /* locate registered callback */ |
1669 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1670 | 0 | if (dev && dev->cb) { |
1671 | 0 | wc_CryptoInfo cryptoInfo; |
1672 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1673 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1674 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED25519_VERIFY; |
1675 | 0 | cryptoInfo.pk.ed25519verify.sig = sig; |
1676 | 0 | cryptoInfo.pk.ed25519verify.sigLen = sigLen; |
1677 | 0 | cryptoInfo.pk.ed25519verify.msg = msg; |
1678 | 0 | cryptoInfo.pk.ed25519verify.msgLen = msgLen; |
1679 | 0 | cryptoInfo.pk.ed25519verify.res = res; |
1680 | 0 | cryptoInfo.pk.ed25519verify.key = key; |
1681 | 0 | cryptoInfo.pk.ed25519verify.type = type; |
1682 | 0 | cryptoInfo.pk.ed25519verify.context = context; |
1683 | 0 | cryptoInfo.pk.ed25519verify.contextLen = contextLen; |
1684 | |
|
1685 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1686 | 0 | } |
1687 | |
|
1688 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1689 | 0 | } |
1690 | | |
1691 | | int wc_CryptoCb_Ed25519MakePub(ed25519_key* key, byte* pubKey, word32 pubKeySz) |
1692 | 0 | { |
1693 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1694 | 0 | CryptoCb* dev; |
1695 | |
|
1696 | 0 | if (key == NULL || pubKey == NULL || pubKeySz != ED25519_PUB_KEY_SIZE) |
1697 | 0 | return ret; |
1698 | | |
1699 | | /* locate registered callback */ |
1700 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1701 | 0 | if (dev && dev->cb) { |
1702 | 0 | wc_CryptoInfo cryptoInfo; |
1703 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1704 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1705 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED25519_MAKE_PUB; |
1706 | 0 | cryptoInfo.pk.ed25519makepub.key = key; |
1707 | 0 | cryptoInfo.pk.ed25519makepub.pubOut = pubKey; |
1708 | 0 | cryptoInfo.pk.ed25519makepub.pubOutSz = pubKeySz; |
1709 | |
|
1710 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1711 | 0 | } |
1712 | |
|
1713 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1714 | 0 | } |
1715 | | |
1716 | | int wc_CryptoCb_Ed25519CheckKey(ed25519_key* key) |
1717 | 0 | { |
1718 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1719 | 0 | CryptoCb* dev; |
1720 | 0 | int checkPriv; |
1721 | |
|
1722 | 0 | if (key == NULL) |
1723 | 0 | return ret; |
1724 | | |
1725 | 0 | checkPriv = key->privKeySet ? 1 : 0; |
1726 | 0 | #ifdef ED25519_MAX_ID_LEN |
1727 | 0 | if ((key->idLen > 0) || (key->labelLen > 0)) |
1728 | 0 | checkPriv = 1; |
1729 | 0 | #endif |
1730 | | |
1731 | | /* locate registered callback */ |
1732 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1733 | 0 | if (dev && dev->cb) { |
1734 | 0 | wc_CryptoInfo cryptoInfo; |
1735 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1736 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1737 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED25519_CHECK_KEY; |
1738 | 0 | cryptoInfo.pk.ed25519checkkey.key = key; |
1739 | | /* key->p is already the compressed wire form; cross it as bytes so a |
1740 | | * device can validate the actual input without touching key internals |
1741 | | */ |
1742 | 0 | cryptoInfo.pk.ed25519checkkey.pubKey = key->p; |
1743 | 0 | cryptoInfo.pk.ed25519checkkey.pubKeySz = ED25519_PUB_KEY_SIZE; |
1744 | 0 | cryptoInfo.pk.ed25519checkkey.checkPriv = checkPriv; |
1745 | |
|
1746 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1747 | 0 | } |
1748 | |
|
1749 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1750 | 0 | } |
1751 | | #endif /* HAVE_ED25519 */ |
1752 | | |
1753 | | #ifdef HAVE_CURVE448 |
1754 | | int wc_CryptoCb_Curve448Gen(WC_RNG* rng, int keySize, |
1755 | | curve448_key* key) |
1756 | 0 | { |
1757 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1758 | 0 | CryptoCb* dev; |
1759 | |
|
1760 | 0 | if (key == NULL) |
1761 | 0 | return ret; |
1762 | | |
1763 | | /* locate registered callback */ |
1764 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1765 | 0 | if (dev && dev->cb) { |
1766 | 0 | wc_CryptoInfo cryptoInfo; |
1767 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1768 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1769 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_CURVE448_KEYGEN; |
1770 | 0 | cryptoInfo.pk.curve448kg.rng = rng; |
1771 | 0 | cryptoInfo.pk.curve448kg.size = keySize; |
1772 | 0 | cryptoInfo.pk.curve448kg.key = key; |
1773 | |
|
1774 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1775 | 0 | } |
1776 | |
|
1777 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1778 | 0 | } |
1779 | | |
1780 | | int wc_CryptoCb_Curve448(curve448_key* private_key, |
1781 | | curve448_key* public_key, byte* out, word32* outlen, int endian) |
1782 | 0 | { |
1783 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1784 | 0 | CryptoCb* dev; |
1785 | |
|
1786 | 0 | if (private_key == NULL) |
1787 | 0 | return ret; |
1788 | | |
1789 | | /* locate registered callback */ |
1790 | 0 | dev = wc_CryptoCb_FindDevice(private_key->devId, WC_ALGO_TYPE_PK); |
1791 | 0 | if (dev && dev->cb) { |
1792 | 0 | wc_CryptoInfo cryptoInfo; |
1793 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1794 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1795 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_CURVE448; |
1796 | 0 | cryptoInfo.pk.curve448.private_key = private_key; |
1797 | 0 | cryptoInfo.pk.curve448.public_key = public_key; |
1798 | 0 | cryptoInfo.pk.curve448.out = out; |
1799 | 0 | cryptoInfo.pk.curve448.outlen = outlen; |
1800 | 0 | cryptoInfo.pk.curve448.endian = endian; |
1801 | |
|
1802 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1803 | 0 | } |
1804 | |
|
1805 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1806 | 0 | } |
1807 | | |
1808 | | int wc_CryptoCb_Curve448MakePub(int devId, int public_size, byte* pub, |
1809 | | int private_size, const byte* priv) |
1810 | 0 | { |
1811 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1812 | 0 | CryptoCb* dev; |
1813 | |
|
1814 | 0 | if (pub == NULL || priv == NULL) |
1815 | 0 | return ret; |
1816 | | |
1817 | | /* locate registered callback */ |
1818 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1819 | | /* only a caller that selected no device settles for the first registered |
1820 | | * one; a devId names the single device allowed to see the scalar */ |
1821 | 0 | if ((dev == NULL || dev->cb == NULL) && (devId == INVALID_DEVID)) |
1822 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1823 | 0 | if (dev && dev->cb) { |
1824 | 0 | wc_CryptoInfo cryptoInfo; |
1825 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1826 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1827 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_CURVE448_MAKE_PUB; |
1828 | 0 | cryptoInfo.pk.curve448makepub.pub = pub; |
1829 | 0 | cryptoInfo.pk.curve448makepub.pubSz = (word32)public_size; |
1830 | 0 | cryptoInfo.pk.curve448makepub.priv = priv; |
1831 | 0 | cryptoInfo.pk.curve448makepub.privSz = (word32)private_size; |
1832 | |
|
1833 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1834 | 0 | } |
1835 | |
|
1836 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1837 | 0 | } |
1838 | | |
1839 | | int wc_CryptoCb_Curve448Generic(int devId, int public_size, byte* pub, |
1840 | | int private_size, const byte* priv, int basepoint_size, |
1841 | | const byte* basepoint) |
1842 | 0 | { |
1843 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1844 | 0 | CryptoCb* dev; |
1845 | |
|
1846 | 0 | if (pub == NULL || priv == NULL || basepoint == NULL) |
1847 | 0 | return ret; |
1848 | | |
1849 | | /* locate registered callback */ |
1850 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
1851 | | /* only a caller that selected no device settles for the first registered |
1852 | | * one; a devId names the single device allowed to see the scalar */ |
1853 | 0 | if ((dev == NULL || dev->cb == NULL) && (devId == INVALID_DEVID)) |
1854 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
1855 | 0 | if (dev && dev->cb) { |
1856 | 0 | wc_CryptoInfo cryptoInfo; |
1857 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1858 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1859 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_CURVE448_GENERIC; |
1860 | 0 | cryptoInfo.pk.curve448generic.pub = pub; |
1861 | 0 | cryptoInfo.pk.curve448generic.pubSz = (word32)public_size; |
1862 | 0 | cryptoInfo.pk.curve448generic.priv = priv; |
1863 | 0 | cryptoInfo.pk.curve448generic.privSz = (word32)private_size; |
1864 | 0 | cryptoInfo.pk.curve448generic.basepoint = basepoint; |
1865 | 0 | cryptoInfo.pk.curve448generic.basepointSz = (word32)basepoint_size; |
1866 | |
|
1867 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1868 | 0 | } |
1869 | |
|
1870 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1871 | 0 | } |
1872 | | #endif /* HAVE_CURVE448 */ |
1873 | | |
1874 | | #ifdef HAVE_ED448 |
1875 | | int wc_CryptoCb_Ed448Gen(WC_RNG* rng, int keySize, ed448_key* key) |
1876 | 0 | { |
1877 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1878 | 0 | CryptoCb* dev; |
1879 | |
|
1880 | 0 | if (key == NULL) |
1881 | 0 | return ret; |
1882 | | |
1883 | | /* locate registered callback */ |
1884 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1885 | 0 | if (dev && dev->cb) { |
1886 | 0 | wc_CryptoInfo cryptoInfo; |
1887 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1888 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1889 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED448_KEYGEN; |
1890 | 0 | cryptoInfo.pk.ed448kg.rng = rng; |
1891 | 0 | cryptoInfo.pk.ed448kg.size = keySize; |
1892 | 0 | cryptoInfo.pk.ed448kg.key = key; |
1893 | |
|
1894 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1895 | 0 | } |
1896 | |
|
1897 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1898 | 0 | } |
1899 | | |
1900 | | int wc_CryptoCb_Ed448Sign(const byte* in, word32 inLen, byte* out, |
1901 | | word32 *outLen, ed448_key* key, byte type, const byte* context, |
1902 | | byte contextLen) |
1903 | 0 | { |
1904 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1905 | 0 | CryptoCb* dev; |
1906 | |
|
1907 | 0 | if (key == NULL) |
1908 | 0 | return ret; |
1909 | | |
1910 | | /* locate registered callback */ |
1911 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1912 | 0 | if (dev && dev->cb) { |
1913 | 0 | wc_CryptoInfo cryptoInfo; |
1914 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1915 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1916 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED448; |
1917 | 0 | cryptoInfo.pk.ed448sign.in = in; |
1918 | 0 | cryptoInfo.pk.ed448sign.inLen = inLen; |
1919 | 0 | cryptoInfo.pk.ed448sign.out = out; |
1920 | 0 | cryptoInfo.pk.ed448sign.outLen = outLen; |
1921 | 0 | cryptoInfo.pk.ed448sign.key = key; |
1922 | 0 | cryptoInfo.pk.ed448sign.type = type; |
1923 | 0 | cryptoInfo.pk.ed448sign.context = context; |
1924 | 0 | cryptoInfo.pk.ed448sign.contextLen = contextLen; |
1925 | |
|
1926 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1927 | 0 | } |
1928 | |
|
1929 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1930 | 0 | } |
1931 | | |
1932 | | int wc_CryptoCb_Ed448Verify(const byte* sig, word32 sigLen, |
1933 | | const byte* msg, word32 msgLen, int* res, ed448_key* key, byte type, |
1934 | | const byte* context, byte contextLen) |
1935 | 0 | { |
1936 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1937 | 0 | CryptoCb* dev; |
1938 | |
|
1939 | 0 | if (key == NULL) |
1940 | 0 | return ret; |
1941 | | |
1942 | | /* locate registered callback */ |
1943 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1944 | 0 | if (dev && dev->cb) { |
1945 | 0 | wc_CryptoInfo cryptoInfo; |
1946 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1947 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1948 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED448_VERIFY; |
1949 | 0 | cryptoInfo.pk.ed448verify.sig = sig; |
1950 | 0 | cryptoInfo.pk.ed448verify.sigLen = sigLen; |
1951 | 0 | cryptoInfo.pk.ed448verify.msg = msg; |
1952 | 0 | cryptoInfo.pk.ed448verify.msgLen = msgLen; |
1953 | 0 | cryptoInfo.pk.ed448verify.res = res; |
1954 | 0 | cryptoInfo.pk.ed448verify.key = key; |
1955 | 0 | cryptoInfo.pk.ed448verify.type = type; |
1956 | 0 | cryptoInfo.pk.ed448verify.context = context; |
1957 | 0 | cryptoInfo.pk.ed448verify.contextLen = contextLen; |
1958 | |
|
1959 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1960 | 0 | } |
1961 | |
|
1962 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1963 | 0 | } |
1964 | | |
1965 | | int wc_CryptoCb_Ed448MakePub(ed448_key* key, byte* pubKey, word32 pubKeySz) |
1966 | 0 | { |
1967 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1968 | 0 | CryptoCb* dev; |
1969 | |
|
1970 | 0 | if (key == NULL || pubKey == NULL || pubKeySz != ED448_PUB_KEY_SIZE) |
1971 | 0 | return ret; |
1972 | | |
1973 | | /* locate registered callback */ |
1974 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
1975 | 0 | if (dev && dev->cb) { |
1976 | 0 | wc_CryptoInfo cryptoInfo; |
1977 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
1978 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
1979 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED448_MAKE_PUB; |
1980 | 0 | cryptoInfo.pk.ed448makepub.key = key; |
1981 | 0 | cryptoInfo.pk.ed448makepub.pubOut = pubKey; |
1982 | 0 | cryptoInfo.pk.ed448makepub.pubOutSz = pubKeySz; |
1983 | |
|
1984 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
1985 | 0 | } |
1986 | |
|
1987 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
1988 | 0 | } |
1989 | | |
1990 | | int wc_CryptoCb_Ed448CheckKey(ed448_key* key) |
1991 | 0 | { |
1992 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
1993 | 0 | CryptoCb* dev; |
1994 | 0 | int checkPriv; |
1995 | |
|
1996 | 0 | if (key == NULL) |
1997 | 0 | return ret; |
1998 | | |
1999 | 0 | checkPriv = key->privKeySet ? 1 : 0; |
2000 | 0 | #ifdef ED448_MAX_ID_LEN |
2001 | 0 | if ((key->idLen > 0) || (key->labelLen > 0)) |
2002 | 0 | checkPriv = 1; |
2003 | 0 | #endif |
2004 | | |
2005 | | /* locate registered callback */ |
2006 | 0 | dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK); |
2007 | 0 | if (dev && dev->cb) { |
2008 | 0 | wc_CryptoInfo cryptoInfo; |
2009 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2010 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2011 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_ED448_CHECK_KEY; |
2012 | 0 | cryptoInfo.pk.ed448checkkey.key = key; |
2013 | 0 | cryptoInfo.pk.ed448checkkey.pubKey = key->p; |
2014 | 0 | cryptoInfo.pk.ed448checkkey.pubKeySz = ED448_PUB_KEY_SIZE; |
2015 | 0 | cryptoInfo.pk.ed448checkkey.checkPriv = checkPriv; |
2016 | |
|
2017 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2018 | 0 | } |
2019 | |
|
2020 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2021 | 0 | } |
2022 | | #endif /* HAVE_ED448 */ |
2023 | | |
2024 | | #if defined(WOLFSSL_HAVE_LMS) || defined(WOLFSSL_HAVE_XMSS) |
2025 | | int wc_CryptoCb_PqcStatefulSigGetDevId(int type, void* key) |
2026 | | { |
2027 | | int devId = INVALID_DEVID; |
2028 | | |
2029 | | if (key == NULL) |
2030 | | return devId; |
2031 | | |
2032 | | #if defined(WOLFSSL_HAVE_LMS) |
2033 | | if (type == WC_PQC_STATEFUL_SIG_TYPE_LMS) { |
2034 | | devId = ((LmsKey*)key)->devId; |
2035 | | } |
2036 | | #endif |
2037 | | #if defined(WOLFSSL_HAVE_XMSS) |
2038 | | if (type == WC_PQC_STATEFUL_SIG_TYPE_XMSS) { |
2039 | | devId = ((XmssKey*)key)->devId; |
2040 | | } |
2041 | | #endif |
2042 | | |
2043 | | return devId; |
2044 | | } |
2045 | | |
2046 | | int wc_CryptoCb_PqcStatefulSigKeyGen(int type, void* key, WC_RNG* rng) |
2047 | | { |
2048 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2049 | | int devId = INVALID_DEVID; |
2050 | | CryptoCb* dev; |
2051 | | |
2052 | | if (key == NULL) |
2053 | | return ret; |
2054 | | |
2055 | | /* get devId; an unbound key still goes through the find callback */ |
2056 | | devId = wc_CryptoCb_PqcStatefulSigGetDevId(type, key); |
2057 | | #ifndef WOLF_CRYPTO_CB_FIND |
2058 | | if (devId == INVALID_DEVID) |
2059 | | return ret; |
2060 | | #endif |
2061 | | |
2062 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
2063 | | if (dev && dev->cb) { |
2064 | | wc_CryptoInfo cryptoInfo; |
2065 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2066 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2067 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_STATEFUL_SIG_KEYGEN; |
2068 | | cryptoInfo.pk.pqc_stateful_sig_kg.rng = rng; |
2069 | | cryptoInfo.pk.pqc_stateful_sig_kg.key = key; |
2070 | | cryptoInfo.pk.pqc_stateful_sig_kg.type = type; |
2071 | | |
2072 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2073 | | } |
2074 | | |
2075 | | return wc_CryptoCb_TranslateErrorCode(ret); |
2076 | | } |
2077 | | |
2078 | | int wc_CryptoCb_PqcStatefulSigSign(const byte* msg, word32 msgSz, byte* out, |
2079 | | word32* outSz, int type, void* key) |
2080 | | { |
2081 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2082 | | int devId = INVALID_DEVID; |
2083 | | CryptoCb* dev; |
2084 | | |
2085 | | if (key == NULL) |
2086 | | return ret; |
2087 | | |
2088 | | /* get devId; an unbound key still goes through the find callback */ |
2089 | | devId = wc_CryptoCb_PqcStatefulSigGetDevId(type, key); |
2090 | | #ifndef WOLF_CRYPTO_CB_FIND |
2091 | | if (devId == INVALID_DEVID) |
2092 | | return ret; |
2093 | | #endif |
2094 | | |
2095 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
2096 | | if (dev && dev->cb) { |
2097 | | wc_CryptoInfo cryptoInfo; |
2098 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2099 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2100 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_STATEFUL_SIG_SIGN; |
2101 | | cryptoInfo.pk.pqc_stateful_sig_sign.msg = msg; |
2102 | | cryptoInfo.pk.pqc_stateful_sig_sign.msgSz = msgSz; |
2103 | | cryptoInfo.pk.pqc_stateful_sig_sign.out = out; |
2104 | | cryptoInfo.pk.pqc_stateful_sig_sign.outSz = outSz; |
2105 | | cryptoInfo.pk.pqc_stateful_sig_sign.key = key; |
2106 | | cryptoInfo.pk.pqc_stateful_sig_sign.type = type; |
2107 | | |
2108 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2109 | | } |
2110 | | |
2111 | | return wc_CryptoCb_TranslateErrorCode(ret); |
2112 | | } |
2113 | | |
2114 | | int wc_CryptoCb_PqcStatefulSigVerify(const byte* sig, word32 sigSz, |
2115 | | const byte* msg, word32 msgSz, int* res, int type, void* key) |
2116 | | { |
2117 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2118 | | int devId = INVALID_DEVID; |
2119 | | CryptoCb* dev; |
2120 | | |
2121 | | if (key == NULL) |
2122 | | return ret; |
2123 | | |
2124 | | /* get devId; an unbound key still goes through the find callback */ |
2125 | | devId = wc_CryptoCb_PqcStatefulSigGetDevId(type, key); |
2126 | | #ifndef WOLF_CRYPTO_CB_FIND |
2127 | | if (devId == INVALID_DEVID) |
2128 | | return ret; |
2129 | | #endif |
2130 | | |
2131 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
2132 | | if (dev && dev->cb) { |
2133 | | wc_CryptoInfo cryptoInfo; |
2134 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2135 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2136 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_STATEFUL_SIG_VERIFY; |
2137 | | cryptoInfo.pk.pqc_stateful_sig_verify.sig = sig; |
2138 | | cryptoInfo.pk.pqc_stateful_sig_verify.sigSz = sigSz; |
2139 | | cryptoInfo.pk.pqc_stateful_sig_verify.msg = msg; |
2140 | | cryptoInfo.pk.pqc_stateful_sig_verify.msgSz = msgSz; |
2141 | | cryptoInfo.pk.pqc_stateful_sig_verify.res = res; |
2142 | | cryptoInfo.pk.pqc_stateful_sig_verify.key = key; |
2143 | | cryptoInfo.pk.pqc_stateful_sig_verify.type = type; |
2144 | | |
2145 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2146 | | } |
2147 | | |
2148 | | ret = wc_CryptoCb_TranslateErrorCode(ret); |
2149 | | /* A device may write *res and then fail or decline; any non-zero |
2150 | | * return reports the signature as invalid. */ |
2151 | | if ((ret != 0) && (res != NULL)) { |
2152 | | *res = 0; |
2153 | | } |
2154 | | |
2155 | | return ret; |
2156 | | } |
2157 | | |
2158 | | int wc_CryptoCb_PqcStatefulSigSigsLeft(int type, void* key, word32* sigsLeft) |
2159 | | { |
2160 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2161 | | int devId = INVALID_DEVID; |
2162 | | CryptoCb* dev; |
2163 | | |
2164 | | if (key == NULL) |
2165 | | return ret; |
2166 | | |
2167 | | /* get devId; an unbound key still goes through the find callback */ |
2168 | | devId = wc_CryptoCb_PqcStatefulSigGetDevId(type, key); |
2169 | | #ifndef WOLF_CRYPTO_CB_FIND |
2170 | | if (devId == INVALID_DEVID) |
2171 | | return ret; |
2172 | | #endif |
2173 | | |
2174 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
2175 | | if (dev && dev->cb) { |
2176 | | wc_CryptoInfo cryptoInfo; |
2177 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2178 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2179 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_STATEFUL_SIG_SIGS_LEFT; |
2180 | | cryptoInfo.pk.pqc_stateful_sig_sigs_left.key = key; |
2181 | | cryptoInfo.pk.pqc_stateful_sig_sigs_left.sigsLeft = sigsLeft; |
2182 | | cryptoInfo.pk.pqc_stateful_sig_sigs_left.type = type; |
2183 | | |
2184 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2185 | | } |
2186 | | |
2187 | | return wc_CryptoCb_TranslateErrorCode(ret); |
2188 | | } |
2189 | | #endif /* WOLFSSL_HAVE_LMS || WOLFSSL_HAVE_XMSS */ |
2190 | | |
2191 | | #if defined(WOLFSSL_HAVE_MLKEM) || defined(WOLFSSL_HAVE_FRODOKEM) |
2192 | | int wc_CryptoCb_PqcKemGetDevId(int type, void* key) |
2193 | 0 | { |
2194 | 0 | int devId = INVALID_DEVID; |
2195 | |
|
2196 | 0 | if (key == NULL) |
2197 | 0 | return devId; |
2198 | | |
2199 | | /* get devId */ |
2200 | 0 | #ifdef WOLFSSL_HAVE_MLKEM |
2201 | 0 | if (type == WC_PQC_KEM_TYPE_MLKEM) { |
2202 | 0 | devId = ((MlKemKey*) key)->devId; |
2203 | 0 | } |
2204 | 0 | #endif |
2205 | | #ifdef WOLFSSL_HAVE_FRODOKEM |
2206 | | if (type == WC_PQC_KEM_TYPE_FRODOKEM) { |
2207 | | devId = ((FrodoKemKey*) key)->devId; |
2208 | | } |
2209 | | #endif |
2210 | |
|
2211 | 0 | return devId; |
2212 | 0 | } |
2213 | | |
2214 | | int wc_CryptoCb_MakePqcKemKey(WC_RNG* rng, int type, int keySize, void* key) |
2215 | 0 | { |
2216 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2217 | 0 | int devId = INVALID_DEVID; |
2218 | 0 | CryptoCb* dev; |
2219 | |
|
2220 | 0 | if (key == NULL) |
2221 | 0 | return ret; |
2222 | | |
2223 | | /* get devId */ |
2224 | 0 | devId = wc_CryptoCb_PqcKemGetDevId(type, key); |
2225 | 0 | #ifndef WOLF_CRYPTO_CB_FIND |
2226 | 0 | if (devId == INVALID_DEVID) |
2227 | 0 | return ret; |
2228 | 0 | #endif |
2229 | | |
2230 | | /* locate registered callback */ |
2231 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
2232 | 0 | if (dev && dev->cb) { |
2233 | 0 | wc_CryptoInfo cryptoInfo; |
2234 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2235 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2236 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_PQC_KEM_KEYGEN; |
2237 | 0 | cryptoInfo.pk.pqc_kem_kg.rng = rng; |
2238 | 0 | cryptoInfo.pk.pqc_kem_kg.size = keySize; |
2239 | 0 | cryptoInfo.pk.pqc_kem_kg.key = key; |
2240 | 0 | cryptoInfo.pk.pqc_kem_kg.type = type; |
2241 | |
|
2242 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2243 | 0 | } |
2244 | |
|
2245 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2246 | 0 | } |
2247 | | |
2248 | | int wc_CryptoCb_PqcEncapsulate(byte* ciphertext, word32 ciphertextLen, |
2249 | | byte* sharedSecret, word32 sharedSecretLen, WC_RNG* rng, int type, |
2250 | | void* key) |
2251 | 0 | { |
2252 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2253 | 0 | int devId = INVALID_DEVID; |
2254 | 0 | CryptoCb* dev; |
2255 | |
|
2256 | 0 | if (key == NULL) |
2257 | 0 | return ret; |
2258 | | |
2259 | | /* get devId */ |
2260 | 0 | devId = wc_CryptoCb_PqcKemGetDevId(type, key); |
2261 | 0 | #ifndef WOLF_CRYPTO_CB_FIND |
2262 | 0 | if (devId == INVALID_DEVID) |
2263 | 0 | return ret; |
2264 | 0 | #endif |
2265 | | |
2266 | | /* locate registered callback */ |
2267 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
2268 | 0 | if (dev && dev->cb) { |
2269 | 0 | wc_CryptoInfo cryptoInfo; |
2270 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2271 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2272 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_PQC_KEM_ENCAPS; |
2273 | 0 | cryptoInfo.pk.pqc_encaps.ciphertext = ciphertext; |
2274 | 0 | cryptoInfo.pk.pqc_encaps.ciphertextLen = ciphertextLen; |
2275 | 0 | cryptoInfo.pk.pqc_encaps.sharedSecret = sharedSecret; |
2276 | 0 | cryptoInfo.pk.pqc_encaps.sharedSecretLen = sharedSecretLen; |
2277 | 0 | cryptoInfo.pk.pqc_encaps.rng = rng; |
2278 | 0 | cryptoInfo.pk.pqc_encaps.key = key; |
2279 | 0 | cryptoInfo.pk.pqc_encaps.type = type; |
2280 | |
|
2281 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2282 | 0 | } |
2283 | |
|
2284 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2285 | 0 | } |
2286 | | |
2287 | | int wc_CryptoCb_PqcDecapsulate(const byte* ciphertext, word32 ciphertextLen, |
2288 | | byte* sharedSecret, word32 sharedSecretLen, int type, void* key) |
2289 | 0 | { |
2290 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2291 | 0 | int devId = INVALID_DEVID; |
2292 | 0 | CryptoCb* dev; |
2293 | |
|
2294 | 0 | if (key == NULL) |
2295 | 0 | return ret; |
2296 | | |
2297 | | /* get devId */ |
2298 | 0 | devId = wc_CryptoCb_PqcKemGetDevId(type, key); |
2299 | 0 | #ifndef WOLF_CRYPTO_CB_FIND |
2300 | 0 | if (devId == INVALID_DEVID) |
2301 | 0 | return ret; |
2302 | 0 | #endif |
2303 | | |
2304 | | /* locate registered callback */ |
2305 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
2306 | 0 | if (dev && dev->cb) { |
2307 | 0 | wc_CryptoInfo cryptoInfo; |
2308 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2309 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2310 | 0 | cryptoInfo.pk.type = WC_PK_TYPE_PQC_KEM_DECAPS; |
2311 | 0 | cryptoInfo.pk.pqc_decaps.ciphertext = ciphertext; |
2312 | 0 | cryptoInfo.pk.pqc_decaps.ciphertextLen = ciphertextLen; |
2313 | 0 | cryptoInfo.pk.pqc_decaps.sharedSecret = sharedSecret; |
2314 | 0 | cryptoInfo.pk.pqc_decaps.sharedSecretLen = sharedSecretLen; |
2315 | 0 | cryptoInfo.pk.pqc_decaps.key = key; |
2316 | 0 | cryptoInfo.pk.pqc_decaps.type = type; |
2317 | |
|
2318 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2319 | 0 | } |
2320 | |
|
2321 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2322 | 0 | } |
2323 | | #endif /* WOLFSSL_HAVE_MLKEM || WOLFSSL_HAVE_FRODOKEM */ |
2324 | | |
2325 | | #if defined(HAVE_FALCON) || defined(WOLFSSL_HAVE_MLDSA) || \ |
2326 | | defined(WOLFSSL_HAVE_SLHDSA) |
2327 | | int wc_CryptoCb_PqcSigGetDevId(int type, void* key) |
2328 | | { |
2329 | | int devId = INVALID_DEVID; |
2330 | | |
2331 | | if (key == NULL) |
2332 | | return devId; |
2333 | | |
2334 | | /* get devId */ |
2335 | | #if defined(WOLFSSL_HAVE_MLDSA) |
2336 | | if (type == WC_PQC_SIG_TYPE_MLDSA) { |
2337 | | devId = ((wc_MlDsaKey*) key)->devId; |
2338 | | } |
2339 | | #endif |
2340 | | #if defined(HAVE_FALCON) |
2341 | | if (type == WC_PQC_SIG_TYPE_FALCON) { |
2342 | | devId = ((falcon_key*) key)->devId; |
2343 | | } |
2344 | | #endif |
2345 | | #if defined(WOLFSSL_HAVE_SLHDSA) |
2346 | | if (type == WC_PQC_SIG_TYPE_SLHDSA) { |
2347 | | devId = ((SlhDsaKey*) key)->devId; |
2348 | | } |
2349 | | #endif |
2350 | | |
2351 | | return devId; |
2352 | | } |
2353 | | |
2354 | | int wc_CryptoCb_MakePqcSignatureKey(WC_RNG* rng, int type, int keySize, |
2355 | | void* key) |
2356 | | { |
2357 | | return wc_CryptoCb_MakePqcSignatureKeyEx(rng, type, keySize, NULL, 0, key); |
2358 | | } |
2359 | | |
2360 | | int wc_CryptoCb_MakePqcSignatureKeyEx(WC_RNG* rng, int type, int keySize, |
2361 | | const byte* seed, word32 seedSz, void* key) |
2362 | | { |
2363 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2364 | | int devId = INVALID_DEVID; |
2365 | | CryptoCb* dev; |
2366 | | |
2367 | | if (key == NULL) |
2368 | | return ret; |
2369 | | |
2370 | | /* get devId; an unbound key still goes through the find callback */ |
2371 | | devId = wc_CryptoCb_PqcSigGetDevId(type, key); |
2372 | | #ifndef WOLF_CRYPTO_CB_FIND |
2373 | | if (devId == INVALID_DEVID) |
2374 | | return ret; |
2375 | | #endif |
2376 | | |
2377 | | /* locate registered callback. A slot sits at INVALID_DEVID while it is |
2378 | | * being registered or retired, so never dispatch through one. */ |
2379 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
2380 | | if (dev && dev->cb && (dev->devId != INVALID_DEVID)) { |
2381 | | wc_CryptoInfo cryptoInfo; |
2382 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2383 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2384 | | /* Seeded generation is a different operation: a device that does not |
2385 | | * know this type declines and the caller keeps the seed. */ |
2386 | | cryptoInfo.pk.type = (seed != NULL) ? WC_PK_TYPE_PQC_SIG_KEYGEN_SEED : |
2387 | | WC_PK_TYPE_PQC_SIG_KEYGEN; |
2388 | | cryptoInfo.pk.pqc_sig_kg.rng = rng; |
2389 | | cryptoInfo.pk.pqc_sig_kg.size = keySize; |
2390 | | cryptoInfo.pk.pqc_sig_kg.key = key; |
2391 | | cryptoInfo.pk.pqc_sig_kg.type = type; |
2392 | | cryptoInfo.pk.pqc_sig_kg.seed = seed; |
2393 | | cryptoInfo.pk.pqc_sig_kg.seedSz = seedSz; |
2394 | | |
2395 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2396 | | } |
2397 | | |
2398 | | return wc_CryptoCb_TranslateErrorCode(ret); |
2399 | | } |
2400 | | |
2401 | | int wc_CryptoCb_PqcSign(const byte* in, word32 inlen, byte* out, word32 *outlen, |
2402 | | const byte* context, byte contextLen, word32 preHashType, WC_RNG* rng, |
2403 | | int type, void* key) |
2404 | | { |
2405 | | return wc_CryptoCb_PqcSignEx(in, inlen, out, outlen, context, contextLen, |
2406 | | preHashType, rng, NULL, 0, type, key); |
2407 | | } |
2408 | | |
2409 | | int wc_CryptoCb_PqcSignEx(const byte* in, word32 inlen, byte* out, |
2410 | | word32 *outlen, const byte* context, byte contextLen, word32 preHashType, |
2411 | | WC_RNG* rng, const byte* addRnd, byte addRndSz, int type, void* key) |
2412 | | { |
2413 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2414 | | int devId = INVALID_DEVID; |
2415 | | CryptoCb* dev; |
2416 | | |
2417 | | if (key == NULL) |
2418 | | return ret; |
2419 | | |
2420 | | /* get devId; an unbound key still goes through the find callback */ |
2421 | | devId = wc_CryptoCb_PqcSigGetDevId(type, key); |
2422 | | |
2423 | | /* locate registered callback */ |
2424 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
2425 | | if (dev && dev->cb) { |
2426 | | wc_CryptoInfo cryptoInfo; |
2427 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2428 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2429 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_SIG_SIGN; |
2430 | | cryptoInfo.pk.pqc_sign.in = in; |
2431 | | cryptoInfo.pk.pqc_sign.inlen = inlen; |
2432 | | cryptoInfo.pk.pqc_sign.out = out; |
2433 | | cryptoInfo.pk.pqc_sign.outlen = outlen; |
2434 | | cryptoInfo.pk.pqc_sign.context = context; |
2435 | | cryptoInfo.pk.pqc_sign.contextLen = contextLen; |
2436 | | cryptoInfo.pk.pqc_sign.preHashType = preHashType; |
2437 | | cryptoInfo.pk.pqc_sign.rng = rng; |
2438 | | cryptoInfo.pk.pqc_sign.addRnd = addRnd; |
2439 | | cryptoInfo.pk.pqc_sign.addRndSz = addRndSz; |
2440 | | cryptoInfo.pk.pqc_sign.key = key; |
2441 | | cryptoInfo.pk.pqc_sign.type = type; |
2442 | | |
2443 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2444 | | } |
2445 | | |
2446 | | return wc_CryptoCb_TranslateErrorCode(ret); |
2447 | | } |
2448 | | |
2449 | | int wc_CryptoCb_PqcSignMsg(const byte* mprime, word32 mprimeSz, byte* out, |
2450 | | word32* outlen, WC_RNG* rng, const byte* addRnd, byte addRndSz, int type, |
2451 | | void* key) |
2452 | | { |
2453 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2454 | | int devId = INVALID_DEVID; |
2455 | | CryptoCb* dev; |
2456 | | |
2457 | | if (key == NULL) |
2458 | | return ret; |
2459 | | |
2460 | | /* get devId; an unbound key still goes through the find callback */ |
2461 | | devId = wc_CryptoCb_PqcSigGetDevId(type, key); |
2462 | | |
2463 | | /* locate registered callback */ |
2464 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
2465 | | if (dev && dev->cb) { |
2466 | | wc_CryptoInfo cryptoInfo; |
2467 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2468 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2469 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_SIG_SIGN_MSG; |
2470 | | cryptoInfo.pk.pqc_sign.in = mprime; |
2471 | | cryptoInfo.pk.pqc_sign.inlen = mprimeSz; |
2472 | | cryptoInfo.pk.pqc_sign.out = out; |
2473 | | cryptoInfo.pk.pqc_sign.outlen = outlen; |
2474 | | cryptoInfo.pk.pqc_sign.preHashType = WC_HASH_TYPE_NONE; |
2475 | | cryptoInfo.pk.pqc_sign.rng = rng; |
2476 | | cryptoInfo.pk.pqc_sign.addRnd = addRnd; |
2477 | | cryptoInfo.pk.pqc_sign.addRndSz = addRndSz; |
2478 | | cryptoInfo.pk.pqc_sign.key = key; |
2479 | | cryptoInfo.pk.pqc_sign.type = type; |
2480 | | |
2481 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2482 | | } |
2483 | | |
2484 | | return wc_CryptoCb_TranslateErrorCode(ret); |
2485 | | } |
2486 | | |
2487 | | int wc_CryptoCb_PqcVerify(const byte* sig, word32 siglen, const byte* msg, |
2488 | | word32 msglen, const byte* context, byte contextLen, word32 preHashType, |
2489 | | int* res, int type, void* key) |
2490 | | { |
2491 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2492 | | int devId = INVALID_DEVID; |
2493 | | CryptoCb* dev; |
2494 | | |
2495 | | if (key == NULL) |
2496 | | return ret; |
2497 | | |
2498 | | /* get devId; an unbound key still goes through the find callback */ |
2499 | | devId = wc_CryptoCb_PqcSigGetDevId(type, key); |
2500 | | |
2501 | | /* locate registered callback */ |
2502 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
2503 | | if (dev && dev->cb) { |
2504 | | wc_CryptoInfo cryptoInfo; |
2505 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2506 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2507 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_SIG_VERIFY; |
2508 | | cryptoInfo.pk.pqc_verify.sig = sig; |
2509 | | cryptoInfo.pk.pqc_verify.siglen = siglen; |
2510 | | cryptoInfo.pk.pqc_verify.msg = msg; |
2511 | | cryptoInfo.pk.pqc_verify.msglen = msglen; |
2512 | | cryptoInfo.pk.pqc_verify.context = context; |
2513 | | cryptoInfo.pk.pqc_verify.contextLen = contextLen; |
2514 | | cryptoInfo.pk.pqc_verify.preHashType = preHashType; |
2515 | | cryptoInfo.pk.pqc_verify.res = res; |
2516 | | cryptoInfo.pk.pqc_verify.key = key; |
2517 | | cryptoInfo.pk.pqc_verify.type = type; |
2518 | | |
2519 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2520 | | } |
2521 | | |
2522 | | ret = wc_CryptoCb_TranslateErrorCode(ret); |
2523 | | /* A device may write *res and then fail or decline; any non-zero |
2524 | | * return reports the signature as invalid. */ |
2525 | | if ((ret != 0) && (res != NULL)) { |
2526 | | *res = 0; |
2527 | | } |
2528 | | |
2529 | | return ret; |
2530 | | } |
2531 | | |
2532 | | int wc_CryptoCb_PqcVerifyMsg(const byte* sig, word32 siglen, const byte* mprime, |
2533 | | word32 mprimeSz, int* res, int type, void* key) |
2534 | | { |
2535 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2536 | | int devId = INVALID_DEVID; |
2537 | | CryptoCb* dev; |
2538 | | |
2539 | | if (key == NULL) |
2540 | | return ret; |
2541 | | |
2542 | | /* get devId; an unbound key still goes through the find callback */ |
2543 | | devId = wc_CryptoCb_PqcSigGetDevId(type, key); |
2544 | | |
2545 | | /* locate registered callback */ |
2546 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
2547 | | if (dev && dev->cb) { |
2548 | | wc_CryptoInfo cryptoInfo; |
2549 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2550 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2551 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_SIG_VERIFY_MSG; |
2552 | | cryptoInfo.pk.pqc_verify.sig = sig; |
2553 | | cryptoInfo.pk.pqc_verify.siglen = siglen; |
2554 | | cryptoInfo.pk.pqc_verify.msg = mprime; |
2555 | | cryptoInfo.pk.pqc_verify.msglen = mprimeSz; |
2556 | | cryptoInfo.pk.pqc_verify.preHashType = WC_HASH_TYPE_NONE; |
2557 | | cryptoInfo.pk.pqc_verify.res = res; |
2558 | | cryptoInfo.pk.pqc_verify.key = key; |
2559 | | cryptoInfo.pk.pqc_verify.type = type; |
2560 | | |
2561 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2562 | | } |
2563 | | |
2564 | | ret = wc_CryptoCb_TranslateErrorCode(ret); |
2565 | | /* A device may write *res and then fail or decline; any non-zero |
2566 | | * return reports the signature as invalid. */ |
2567 | | if ((ret != 0) && (res != NULL)) { |
2568 | | *res = 0; |
2569 | | } |
2570 | | |
2571 | | return ret; |
2572 | | } |
2573 | | |
2574 | | int wc_CryptoCb_PqcSignatureCheckPrivKey(void* key, int type, |
2575 | | const byte* pubKey, word32 pubKeySz) |
2576 | | { |
2577 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2578 | | int devId = INVALID_DEVID; |
2579 | | CryptoCb* dev; |
2580 | | |
2581 | | if (key == NULL) |
2582 | | return ret; |
2583 | | |
2584 | | /* get devId; an unbound key still goes through the find callback */ |
2585 | | devId = wc_CryptoCb_PqcSigGetDevId(type, key); |
2586 | | |
2587 | | /* locate registered callback */ |
2588 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK); |
2589 | | if (dev && dev->cb) { |
2590 | | wc_CryptoInfo cryptoInfo; |
2591 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2592 | | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
2593 | | cryptoInfo.pk.type = WC_PK_TYPE_PQC_SIG_CHECK_PRIV_KEY; |
2594 | | cryptoInfo.pk.pqc_sig_check.key = key; |
2595 | | cryptoInfo.pk.pqc_sig_check.pubKey = pubKey; |
2596 | | cryptoInfo.pk.pqc_sig_check.pubKeySz = pubKeySz; |
2597 | | cryptoInfo.pk.pqc_sig_check.type = type; |
2598 | | |
2599 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2600 | | } |
2601 | | |
2602 | | return wc_CryptoCb_TranslateErrorCode(ret); |
2603 | | } |
2604 | | #endif /* HAVE_FALCON || WOLFSSL_HAVE_MLDSA || WOLFSSL_HAVE_SLHDSA */ |
2605 | | |
2606 | | #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) |
2607 | | int wc_CryptoCb_Chacha20Poly1305Encrypt(int devId, const byte* inKey, |
2608 | | const byte* inIV, const byte* inAAD, word32 inAADSz, const byte* in, |
2609 | | word32 inSz, byte* out, byte* outAuthTag) |
2610 | 0 | { |
2611 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2612 | 0 | CryptoCb* dev; |
2613 | |
|
2614 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_CIPHER); |
2615 | |
|
2616 | 0 | if (dev && dev->cb) { |
2617 | 0 | wc_CryptoInfo cryptoInfo; |
2618 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2619 | |
|
2620 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2621 | 0 | cryptoInfo.cipher.type = WC_CIPHER_CHACHA; |
2622 | 0 | cryptoInfo.cipher.enc = 1; |
2623 | 0 | cryptoInfo.cipher.chacha20_poly1305_enc.inKey = inKey; |
2624 | 0 | cryptoInfo.cipher.chacha20_poly1305_enc.inIV = inIV; |
2625 | 0 | cryptoInfo.cipher.chacha20_poly1305_enc.inAAD = inAAD; |
2626 | 0 | cryptoInfo.cipher.chacha20_poly1305_enc.inAADSz = inAADSz; |
2627 | 0 | cryptoInfo.cipher.chacha20_poly1305_enc.in = in; |
2628 | 0 | cryptoInfo.cipher.chacha20_poly1305_enc.inSz = inSz; |
2629 | 0 | cryptoInfo.cipher.chacha20_poly1305_enc.out = out; |
2630 | 0 | cryptoInfo.cipher.chacha20_poly1305_enc.outAuthTag = outAuthTag; |
2631 | |
|
2632 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2633 | 0 | } |
2634 | |
|
2635 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2636 | 0 | } |
2637 | | |
2638 | | int wc_CryptoCb_Chacha20Poly1305Decrypt(int devId, const byte* inKey, |
2639 | | const byte* inIV, const byte* inAAD, word32 inAADSz, const byte* in, |
2640 | | word32 inSz, const byte* inAuthTag, byte* out) |
2641 | 0 | { |
2642 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2643 | 0 | CryptoCb* dev; |
2644 | |
|
2645 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_CIPHER); |
2646 | |
|
2647 | 0 | if (dev && dev->cb) { |
2648 | 0 | wc_CryptoInfo cryptoInfo; |
2649 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2650 | |
|
2651 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2652 | 0 | cryptoInfo.cipher.type = WC_CIPHER_CHACHA; |
2653 | 0 | cryptoInfo.cipher.enc = 0; |
2654 | 0 | cryptoInfo.cipher.chacha20_poly1305_dec.inKey = inKey; |
2655 | 0 | cryptoInfo.cipher.chacha20_poly1305_dec.inIV = inIV; |
2656 | 0 | cryptoInfo.cipher.chacha20_poly1305_dec.inAAD = inAAD; |
2657 | 0 | cryptoInfo.cipher.chacha20_poly1305_dec.inAADSz = inAADSz; |
2658 | 0 | cryptoInfo.cipher.chacha20_poly1305_dec.in = in; |
2659 | 0 | cryptoInfo.cipher.chacha20_poly1305_dec.inSz = inSz; |
2660 | 0 | cryptoInfo.cipher.chacha20_poly1305_dec.inAuthTag = inAuthTag; |
2661 | 0 | cryptoInfo.cipher.chacha20_poly1305_dec.out = out; |
2662 | |
|
2663 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2664 | 0 | } |
2665 | |
|
2666 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2667 | 0 | } |
2668 | | #endif /* HAVE_CHACHA && HAVE_POLY1305 */ |
2669 | | |
2670 | | #ifndef NO_AES |
2671 | | #ifdef HAVE_AESGCM |
2672 | | |
2673 | | int wc_CryptoCb_AesGcmEncrypt(Aes* aes, byte* out, |
2674 | | const byte* in, word32 sz, |
2675 | | const byte* iv, word32 ivSz, |
2676 | | byte* authTag, word32 authTagSz, |
2677 | | const byte* authIn, word32 authInSz) |
2678 | 0 | { |
2679 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2680 | 0 | CryptoCb* dev; |
2681 | | |
2682 | | /* locate registered callback */ |
2683 | 0 | if (aes) { |
2684 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2685 | 0 | } |
2686 | 0 | else { |
2687 | | /* locate first callback and try using it */ |
2688 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2689 | 0 | } |
2690 | |
|
2691 | 0 | if (dev && dev->cb) { |
2692 | 0 | wc_CryptoInfo cryptoInfo; |
2693 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2694 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2695 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_GCM; |
2696 | 0 | cryptoInfo.cipher.enc = 1; |
2697 | 0 | cryptoInfo.cipher.aesgcm_enc.aes = aes; |
2698 | 0 | cryptoInfo.cipher.aesgcm_enc.out = out; |
2699 | 0 | cryptoInfo.cipher.aesgcm_enc.in = in; |
2700 | 0 | cryptoInfo.cipher.aesgcm_enc.sz = sz; |
2701 | 0 | cryptoInfo.cipher.aesgcm_enc.iv = iv; |
2702 | 0 | cryptoInfo.cipher.aesgcm_enc.ivSz = ivSz; |
2703 | 0 | cryptoInfo.cipher.aesgcm_enc.authTag = authTag; |
2704 | 0 | cryptoInfo.cipher.aesgcm_enc.authTagSz = authTagSz; |
2705 | 0 | cryptoInfo.cipher.aesgcm_enc.authIn = authIn; |
2706 | 0 | cryptoInfo.cipher.aesgcm_enc.authInSz = authInSz; |
2707 | |
|
2708 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2709 | 0 | } |
2710 | |
|
2711 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2712 | 0 | } |
2713 | | |
2714 | | int wc_CryptoCb_AesGcmDecrypt(Aes* aes, byte* out, |
2715 | | const byte* in, word32 sz, |
2716 | | const byte* iv, word32 ivSz, |
2717 | | const byte* authTag, word32 authTagSz, |
2718 | | const byte* authIn, word32 authInSz) |
2719 | 0 | { |
2720 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2721 | 0 | CryptoCb* dev; |
2722 | | |
2723 | | /* locate registered callback */ |
2724 | 0 | if (aes) { |
2725 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2726 | 0 | } |
2727 | 0 | else { |
2728 | | /* locate first callback and try using it */ |
2729 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2730 | 0 | } |
2731 | |
|
2732 | 0 | if (dev && dev->cb) { |
2733 | 0 | wc_CryptoInfo cryptoInfo; |
2734 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2735 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2736 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_GCM; |
2737 | 0 | cryptoInfo.cipher.enc = 0; |
2738 | 0 | cryptoInfo.cipher.aesgcm_dec.aes = aes; |
2739 | 0 | cryptoInfo.cipher.aesgcm_dec.out = out; |
2740 | 0 | cryptoInfo.cipher.aesgcm_dec.in = in; |
2741 | 0 | cryptoInfo.cipher.aesgcm_dec.sz = sz; |
2742 | 0 | cryptoInfo.cipher.aesgcm_dec.iv = iv; |
2743 | 0 | cryptoInfo.cipher.aesgcm_dec.ivSz = ivSz; |
2744 | 0 | cryptoInfo.cipher.aesgcm_dec.authTag = authTag; |
2745 | 0 | cryptoInfo.cipher.aesgcm_dec.authTagSz = authTagSz; |
2746 | 0 | cryptoInfo.cipher.aesgcm_dec.authIn = authIn; |
2747 | 0 | cryptoInfo.cipher.aesgcm_dec.authInSz = authInSz; |
2748 | |
|
2749 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2750 | 0 | } |
2751 | |
|
2752 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2753 | 0 | } |
2754 | | #endif /* HAVE_AESGCM */ |
2755 | | |
2756 | | #ifdef HAVE_AESCCM |
2757 | | int wc_CryptoCb_AesCcmEncrypt(Aes* aes, byte* out, |
2758 | | const byte* in, word32 sz, |
2759 | | const byte* nonce, word32 nonceSz, |
2760 | | byte* authTag, word32 authTagSz, |
2761 | | const byte* authIn, word32 authInSz) |
2762 | 0 | { |
2763 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2764 | 0 | CryptoCb* dev; |
2765 | | |
2766 | | /* locate registered callback */ |
2767 | 0 | if (aes) { |
2768 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2769 | 0 | } |
2770 | 0 | else { |
2771 | | /* locate first callback and try using it */ |
2772 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2773 | 0 | } |
2774 | |
|
2775 | 0 | if (dev && dev->cb) { |
2776 | 0 | wc_CryptoInfo cryptoInfo; |
2777 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2778 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2779 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CCM; |
2780 | 0 | cryptoInfo.cipher.enc = 1; |
2781 | 0 | cryptoInfo.cipher.aesccm_enc.aes = aes; |
2782 | 0 | cryptoInfo.cipher.aesccm_enc.out = out; |
2783 | 0 | cryptoInfo.cipher.aesccm_enc.in = in; |
2784 | 0 | cryptoInfo.cipher.aesccm_enc.sz = sz; |
2785 | 0 | cryptoInfo.cipher.aesccm_enc.nonce = nonce; |
2786 | 0 | cryptoInfo.cipher.aesccm_enc.nonceSz = nonceSz; |
2787 | 0 | cryptoInfo.cipher.aesccm_enc.authTag = authTag; |
2788 | 0 | cryptoInfo.cipher.aesccm_enc.authTagSz = authTagSz; |
2789 | 0 | cryptoInfo.cipher.aesccm_enc.authIn = authIn; |
2790 | 0 | cryptoInfo.cipher.aesccm_enc.authInSz = authInSz; |
2791 | |
|
2792 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2793 | 0 | } |
2794 | |
|
2795 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2796 | 0 | } |
2797 | | |
2798 | | int wc_CryptoCb_AesCcmDecrypt(Aes* aes, byte* out, |
2799 | | const byte* in, word32 sz, |
2800 | | const byte* nonce, word32 nonceSz, |
2801 | | const byte* authTag, word32 authTagSz, |
2802 | | const byte* authIn, word32 authInSz) |
2803 | 0 | { |
2804 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2805 | 0 | CryptoCb* dev; |
2806 | | |
2807 | | /* locate registered callback */ |
2808 | 0 | if (aes) { |
2809 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2810 | 0 | } |
2811 | 0 | else { |
2812 | | /* locate first callback and try using it */ |
2813 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2814 | 0 | } |
2815 | |
|
2816 | 0 | if (dev && dev->cb) { |
2817 | 0 | wc_CryptoInfo cryptoInfo; |
2818 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2819 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2820 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CCM; |
2821 | 0 | cryptoInfo.cipher.enc = 0; |
2822 | 0 | cryptoInfo.cipher.aesccm_dec.aes = aes; |
2823 | 0 | cryptoInfo.cipher.aesccm_dec.out = out; |
2824 | 0 | cryptoInfo.cipher.aesccm_dec.in = in; |
2825 | 0 | cryptoInfo.cipher.aesccm_dec.sz = sz; |
2826 | 0 | cryptoInfo.cipher.aesccm_dec.nonce = nonce; |
2827 | 0 | cryptoInfo.cipher.aesccm_dec.nonceSz = nonceSz; |
2828 | 0 | cryptoInfo.cipher.aesccm_dec.authTag = authTag; |
2829 | 0 | cryptoInfo.cipher.aesccm_dec.authTagSz = authTagSz; |
2830 | 0 | cryptoInfo.cipher.aesccm_dec.authIn = authIn; |
2831 | 0 | cryptoInfo.cipher.aesccm_dec.authInSz = authInSz; |
2832 | |
|
2833 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2834 | 0 | } |
2835 | |
|
2836 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2837 | 0 | } |
2838 | | #endif /* HAVE_AESCCM */ |
2839 | | |
2840 | | #ifdef HAVE_AES_CBC |
2841 | | int wc_CryptoCb_AesCbcEncrypt(Aes* aes, byte* out, |
2842 | | const byte* in, word32 sz) |
2843 | 0 | { |
2844 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2845 | 0 | CryptoCb* dev; |
2846 | | |
2847 | | /* locate registered callback */ |
2848 | 0 | if (aes) { |
2849 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2850 | 0 | } |
2851 | 0 | else { |
2852 | | /* locate first callback and try using it */ |
2853 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2854 | 0 | } |
2855 | |
|
2856 | 0 | if (dev && dev->cb) { |
2857 | 0 | wc_CryptoInfo cryptoInfo; |
2858 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2859 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2860 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CBC; |
2861 | 0 | cryptoInfo.cipher.enc = 1; |
2862 | 0 | cryptoInfo.cipher.aescbc.aes = aes; |
2863 | 0 | cryptoInfo.cipher.aescbc.out = out; |
2864 | 0 | cryptoInfo.cipher.aescbc.in = in; |
2865 | 0 | cryptoInfo.cipher.aescbc.sz = sz; |
2866 | |
|
2867 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2868 | 0 | } |
2869 | |
|
2870 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2871 | 0 | } |
2872 | | |
2873 | | int wc_CryptoCb_AesCbcDecrypt(Aes* aes, byte* out, |
2874 | | const byte* in, word32 sz) |
2875 | 0 | { |
2876 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2877 | 0 | CryptoCb* dev; |
2878 | | |
2879 | | /* locate registered callback */ |
2880 | 0 | if (aes) { |
2881 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2882 | 0 | } |
2883 | 0 | else { |
2884 | | /* locate first callback and try using it */ |
2885 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2886 | 0 | } |
2887 | |
|
2888 | 0 | if (dev && dev->cb) { |
2889 | 0 | wc_CryptoInfo cryptoInfo; |
2890 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2891 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2892 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CBC; |
2893 | 0 | cryptoInfo.cipher.enc = 0; |
2894 | 0 | cryptoInfo.cipher.aescbc.aes = aes; |
2895 | 0 | cryptoInfo.cipher.aescbc.out = out; |
2896 | 0 | cryptoInfo.cipher.aescbc.in = in; |
2897 | 0 | cryptoInfo.cipher.aescbc.sz = sz; |
2898 | |
|
2899 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2900 | 0 | } |
2901 | |
|
2902 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2903 | 0 | } |
2904 | | #endif /* HAVE_AES_CBC */ |
2905 | | #ifdef WOLFSSL_AES_COUNTER |
2906 | | int wc_CryptoCb_AesCtrEncrypt(Aes* aes, byte* out, |
2907 | | const byte* in, word32 sz) |
2908 | 0 | { |
2909 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2910 | 0 | CryptoCb* dev; |
2911 | | |
2912 | | /* locate registered callback */ |
2913 | 0 | if (aes) { |
2914 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2915 | 0 | } |
2916 | 0 | else { |
2917 | | /* locate first callback and try using it */ |
2918 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2919 | 0 | } |
2920 | |
|
2921 | 0 | if (dev && dev->cb) { |
2922 | 0 | wc_CryptoInfo cryptoInfo; |
2923 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2924 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2925 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CTR; |
2926 | 0 | cryptoInfo.cipher.enc = 1; |
2927 | 0 | cryptoInfo.cipher.aesctr.aes = aes; |
2928 | 0 | cryptoInfo.cipher.aesctr.out = out; |
2929 | 0 | cryptoInfo.cipher.aesctr.in = in; |
2930 | 0 | cryptoInfo.cipher.aesctr.sz = sz; |
2931 | |
|
2932 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2933 | 0 | } |
2934 | |
|
2935 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2936 | 0 | } |
2937 | | #endif /* WOLFSSL_AES_COUNTER */ |
2938 | | #ifdef WOLFSSL_AES_CFB |
2939 | | int wc_CryptoCb_AesCfbEncrypt(Aes* aes, byte* out, |
2940 | | const byte* in, word32 sz) |
2941 | 0 | { |
2942 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2943 | 0 | CryptoCb* dev; |
2944 | | |
2945 | | /* locate registered callback */ |
2946 | 0 | if (aes) { |
2947 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2948 | 0 | } |
2949 | 0 | else { |
2950 | | /* locate first callback and try using it */ |
2951 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2952 | 0 | } |
2953 | |
|
2954 | 0 | if (dev && dev->cb) { |
2955 | 0 | wc_CryptoInfo cryptoInfo; |
2956 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2957 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2958 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CFB; |
2959 | 0 | cryptoInfo.cipher.enc = 1; |
2960 | 0 | cryptoInfo.cipher.aescfb.aes = aes; |
2961 | 0 | cryptoInfo.cipher.aescfb.out = out; |
2962 | 0 | cryptoInfo.cipher.aescfb.in = in; |
2963 | 0 | cryptoInfo.cipher.aescfb.sz = sz; |
2964 | |
|
2965 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2966 | 0 | } |
2967 | |
|
2968 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
2969 | 0 | } |
2970 | | |
2971 | | int wc_CryptoCb_AesCfbDecrypt(Aes* aes, byte* out, |
2972 | | const byte* in, word32 sz) |
2973 | 0 | { |
2974 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
2975 | 0 | CryptoCb* dev; |
2976 | | |
2977 | | /* locate registered callback */ |
2978 | 0 | if (aes) { |
2979 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
2980 | 0 | } |
2981 | 0 | else { |
2982 | | /* locate first callback and try using it */ |
2983 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
2984 | 0 | } |
2985 | |
|
2986 | 0 | if (dev && dev->cb) { |
2987 | 0 | wc_CryptoInfo cryptoInfo; |
2988 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
2989 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
2990 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_CFB; |
2991 | 0 | cryptoInfo.cipher.enc = 0; |
2992 | 0 | cryptoInfo.cipher.aescfb.aes = aes; |
2993 | 0 | cryptoInfo.cipher.aescfb.out = out; |
2994 | 0 | cryptoInfo.cipher.aescfb.in = in; |
2995 | 0 | cryptoInfo.cipher.aescfb.sz = sz; |
2996 | |
|
2997 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
2998 | 0 | } |
2999 | |
|
3000 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3001 | 0 | } |
3002 | | #endif /* WOLFSSL_AES_CFB */ |
3003 | | #ifdef WOLFSSL_AES_OFB |
3004 | | int wc_CryptoCb_AesOfbEncrypt(Aes* aes, byte* out, |
3005 | | const byte* in, word32 sz) |
3006 | 0 | { |
3007 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3008 | 0 | CryptoCb* dev; |
3009 | | |
3010 | | /* locate registered callback */ |
3011 | 0 | if (aes) { |
3012 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
3013 | 0 | } |
3014 | 0 | else { |
3015 | | /* locate first callback and try using it */ |
3016 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3017 | 0 | } |
3018 | |
|
3019 | 0 | if (dev && dev->cb) { |
3020 | 0 | wc_CryptoInfo cryptoInfo; |
3021 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3022 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3023 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_OFB; |
3024 | 0 | cryptoInfo.cipher.enc = 1; |
3025 | 0 | cryptoInfo.cipher.aesofb.aes = aes; |
3026 | 0 | cryptoInfo.cipher.aesofb.out = out; |
3027 | 0 | cryptoInfo.cipher.aesofb.in = in; |
3028 | 0 | cryptoInfo.cipher.aesofb.sz = sz; |
3029 | |
|
3030 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3031 | 0 | } |
3032 | |
|
3033 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3034 | 0 | } |
3035 | | |
3036 | | int wc_CryptoCb_AesOfbDecrypt(Aes* aes, byte* out, |
3037 | | const byte* in, word32 sz) |
3038 | 0 | { |
3039 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3040 | 0 | CryptoCb* dev; |
3041 | | |
3042 | | /* locate registered callback */ |
3043 | 0 | if (aes) { |
3044 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
3045 | 0 | } |
3046 | 0 | else { |
3047 | | /* locate first callback and try using it */ |
3048 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3049 | 0 | } |
3050 | |
|
3051 | 0 | if (dev && dev->cb) { |
3052 | 0 | wc_CryptoInfo cryptoInfo; |
3053 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3054 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3055 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_OFB; |
3056 | 0 | cryptoInfo.cipher.enc = 0; |
3057 | 0 | cryptoInfo.cipher.aesofb.aes = aes; |
3058 | 0 | cryptoInfo.cipher.aesofb.out = out; |
3059 | 0 | cryptoInfo.cipher.aesofb.in = in; |
3060 | 0 | cryptoInfo.cipher.aesofb.sz = sz; |
3061 | |
|
3062 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3063 | 0 | } |
3064 | |
|
3065 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3066 | 0 | } |
3067 | | #endif /* WOLFSSL_AES_OFB */ |
3068 | | #if defined(HAVE_AES_ECB) || defined(WOLFSSL_AES_DIRECT) || \ |
3069 | | defined(WOLF_CRYPTO_CB_ONLY_AES) |
3070 | | int wc_CryptoCb_AesEcbEncrypt(Aes* aes, byte* out, |
3071 | | const byte* in, word32 sz) |
3072 | 119 | { |
3073 | 119 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3074 | 119 | CryptoCb* dev; |
3075 | | |
3076 | | /* locate registered callback */ |
3077 | 119 | if (aes) { |
3078 | 119 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
3079 | 119 | } |
3080 | 0 | else { |
3081 | | /* locate first callback and try using it */ |
3082 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3083 | 0 | } |
3084 | | |
3085 | 119 | if (dev && dev->cb) { |
3086 | 0 | wc_CryptoInfo cryptoInfo; |
3087 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3088 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3089 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_ECB; |
3090 | 0 | cryptoInfo.cipher.enc = 1; |
3091 | 0 | cryptoInfo.cipher.aesecb.aes = aes; |
3092 | 0 | cryptoInfo.cipher.aesecb.out = out; |
3093 | 0 | cryptoInfo.cipher.aesecb.in = in; |
3094 | 0 | cryptoInfo.cipher.aesecb.sz = sz; |
3095 | |
|
3096 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3097 | 0 | } |
3098 | | |
3099 | 119 | return wc_CryptoCb_TranslateErrorCode(ret); |
3100 | 119 | } |
3101 | | |
3102 | | int wc_CryptoCb_AesEcbDecrypt(Aes* aes, byte* out, |
3103 | | const byte* in, word32 sz) |
3104 | 242 | { |
3105 | 242 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3106 | 242 | CryptoCb* dev; |
3107 | | |
3108 | | /* locate registered callback */ |
3109 | 242 | if (aes) { |
3110 | 242 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
3111 | 242 | } |
3112 | 0 | else { |
3113 | | /* locate first callback and try using it */ |
3114 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3115 | 0 | } |
3116 | | |
3117 | 242 | if (dev && dev->cb) { |
3118 | 0 | wc_CryptoInfo cryptoInfo; |
3119 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3120 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3121 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_ECB; |
3122 | 0 | cryptoInfo.cipher.enc = 0; |
3123 | 0 | cryptoInfo.cipher.aesecb.aes = aes; |
3124 | 0 | cryptoInfo.cipher.aesecb.out = out; |
3125 | 0 | cryptoInfo.cipher.aesecb.in = in; |
3126 | 0 | cryptoInfo.cipher.aesecb.sz = sz; |
3127 | |
|
3128 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3129 | 0 | } |
3130 | | |
3131 | 242 | return wc_CryptoCb_TranslateErrorCode(ret); |
3132 | 242 | } |
3133 | | #endif /* HAVE_AES_ECB || WOLFSSL_AES_DIRECT || WOLF_CRYPTO_CB_ONLY_AES */ |
3134 | | |
3135 | | #ifdef HAVE_AES_KEYWRAP |
3136 | | /* On success returns the number of output bytes produced (the callback reports |
3137 | | * it via aeskeywrap.outResSz), otherwise a negative error or |
3138 | | * CRYPTOCB_UNAVAILABLE. pad selects RFC 5649 (1) vs RFC 3394 (0). */ |
3139 | | int wc_CryptoCb_AesKeyWrap(Aes* aes, const byte* in, word32 inSz, byte* out, |
3140 | | word32 outSz, const byte* iv, int pad) |
3141 | 0 | { |
3142 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3143 | 0 | CryptoCb* dev; |
3144 | | |
3145 | | /* locate registered callback */ |
3146 | 0 | if (aes) { |
3147 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
3148 | 0 | } |
3149 | 0 | else { |
3150 | | /* locate first callback and try using it */ |
3151 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3152 | 0 | } |
3153 | |
|
3154 | 0 | if (dev && dev->cb) { |
3155 | 0 | wc_CryptoInfo cryptoInfo; |
3156 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3157 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3158 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_KEYWRAP; |
3159 | 0 | cryptoInfo.cipher.enc = 1; |
3160 | 0 | cryptoInfo.cipher.aeskeywrap.aes = aes; |
3161 | 0 | cryptoInfo.cipher.aeskeywrap.in = in; |
3162 | 0 | cryptoInfo.cipher.aeskeywrap.inSz = inSz; |
3163 | 0 | cryptoInfo.cipher.aeskeywrap.out = out; |
3164 | 0 | cryptoInfo.cipher.aeskeywrap.outSz = outSz; |
3165 | 0 | cryptoInfo.cipher.aeskeywrap.iv = iv; |
3166 | 0 | cryptoInfo.cipher.aeskeywrap.pad = pad; |
3167 | |
|
3168 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3169 | 0 | if (ret == 0) { |
3170 | | /* device reports the wrapped length */ |
3171 | 0 | if (cryptoInfo.cipher.aeskeywrap.outResSz > outSz) { |
3172 | 0 | return BUFFER_E; |
3173 | 0 | } |
3174 | 0 | return (int)cryptoInfo.cipher.aeskeywrap.outResSz; |
3175 | 0 | } |
3176 | 0 | } |
3177 | | |
3178 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3179 | 0 | } |
3180 | | |
3181 | | int wc_CryptoCb_AesKeyUnWrap(Aes* aes, const byte* in, word32 inSz, byte* out, |
3182 | | word32 outSz, const byte* iv, int pad) |
3183 | 0 | { |
3184 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3185 | 0 | CryptoCb* dev; |
3186 | | |
3187 | | /* locate registered callback */ |
3188 | 0 | if (aes) { |
3189 | 0 | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
3190 | 0 | } |
3191 | 0 | else { |
3192 | | /* locate first callback and try using it */ |
3193 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3194 | 0 | } |
3195 | |
|
3196 | 0 | if (dev && dev->cb) { |
3197 | 0 | wc_CryptoInfo cryptoInfo; |
3198 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3199 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3200 | 0 | cryptoInfo.cipher.type = WC_CIPHER_AES_KEYWRAP; |
3201 | 0 | cryptoInfo.cipher.enc = 0; |
3202 | 0 | cryptoInfo.cipher.aeskeywrap.aes = aes; |
3203 | 0 | cryptoInfo.cipher.aeskeywrap.in = in; |
3204 | 0 | cryptoInfo.cipher.aeskeywrap.inSz = inSz; |
3205 | 0 | cryptoInfo.cipher.aeskeywrap.out = out; |
3206 | 0 | cryptoInfo.cipher.aeskeywrap.outSz = outSz; |
3207 | 0 | cryptoInfo.cipher.aeskeywrap.iv = iv; |
3208 | 0 | cryptoInfo.cipher.aeskeywrap.pad = pad; |
3209 | |
|
3210 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3211 | 0 | if (ret == 0) { |
3212 | | /* device reports the recovered length */ |
3213 | 0 | if (cryptoInfo.cipher.aeskeywrap.outResSz > outSz) { |
3214 | 0 | return BUFFER_E; |
3215 | 0 | } |
3216 | 0 | return (int)cryptoInfo.cipher.aeskeywrap.outResSz; |
3217 | 0 | } |
3218 | 0 | } |
3219 | | |
3220 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3221 | 0 | } |
3222 | | #endif /* HAVE_AES_KEYWRAP */ |
3223 | | |
3224 | | #ifdef WOLF_CRYPTO_CB_AES_SETKEY |
3225 | | int wc_CryptoCb_AesSetKey(Aes* aes, const byte* key, word32 keySz) |
3226 | | { |
3227 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3228 | | CryptoCb* dev; |
3229 | | |
3230 | | if (aes == NULL || key == NULL) |
3231 | | return BAD_FUNC_ARG; |
3232 | | |
3233 | | if (aes->devId == INVALID_DEVID) |
3234 | | return CRYPTOCB_UNAVAILABLE; |
3235 | | |
3236 | | /* locate registered callback */ |
3237 | | dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER); |
3238 | | if (dev && dev->cb) { |
3239 | | wc_CryptoInfo cryptoInfo; |
3240 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3241 | | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3242 | | cryptoInfo.cipher.type = WC_CIPHER_AES; |
3243 | | cryptoInfo.cipher.aessetkey.aes = aes; |
3244 | | cryptoInfo.cipher.aessetkey.key = key; |
3245 | | cryptoInfo.cipher.aessetkey.keySz = keySz; |
3246 | | |
3247 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3248 | | } |
3249 | | |
3250 | | return wc_CryptoCb_TranslateErrorCode(ret); |
3251 | | } |
3252 | | #endif /* WOLF_CRYPTO_CB_AES_SETKEY */ |
3253 | | #endif /* !NO_AES */ |
3254 | | |
3255 | | #ifndef NO_DES3 |
3256 | | int wc_CryptoCb_Des3Encrypt(Des3* des3, byte* out, |
3257 | | const byte* in, word32 sz) |
3258 | 308 | { |
3259 | 308 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3260 | 308 | CryptoCb* dev; |
3261 | | |
3262 | | /* locate registered callback */ |
3263 | 308 | if (des3) { |
3264 | 308 | dev = wc_CryptoCb_FindDevice(des3->devId, WC_ALGO_TYPE_CIPHER); |
3265 | 308 | } |
3266 | 0 | else { |
3267 | | /* locate first callback and try using it */ |
3268 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3269 | 0 | } |
3270 | | |
3271 | 308 | if (dev && dev->cb) { |
3272 | 0 | wc_CryptoInfo cryptoInfo; |
3273 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3274 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3275 | 0 | cryptoInfo.cipher.type = WC_CIPHER_DES3; |
3276 | 0 | cryptoInfo.cipher.enc = 1; |
3277 | 0 | cryptoInfo.cipher.des3.des = des3; |
3278 | 0 | cryptoInfo.cipher.des3.out = out; |
3279 | 0 | cryptoInfo.cipher.des3.in = in; |
3280 | 0 | cryptoInfo.cipher.des3.sz = sz; |
3281 | |
|
3282 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3283 | 0 | } |
3284 | | |
3285 | 308 | return wc_CryptoCb_TranslateErrorCode(ret); |
3286 | 308 | } |
3287 | | |
3288 | | int wc_CryptoCb_Des3Decrypt(Des3* des3, byte* out, |
3289 | | const byte* in, word32 sz) |
3290 | 114 | { |
3291 | 114 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3292 | 114 | CryptoCb* dev; |
3293 | | |
3294 | | /* locate registered callback */ |
3295 | 114 | if (des3) { |
3296 | 114 | dev = wc_CryptoCb_FindDevice(des3->devId, WC_ALGO_TYPE_CIPHER); |
3297 | 114 | } |
3298 | 0 | else { |
3299 | | /* locate first callback and try using it */ |
3300 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3301 | 0 | } |
3302 | | |
3303 | 114 | if (dev && dev->cb) { |
3304 | 0 | wc_CryptoInfo cryptoInfo; |
3305 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3306 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3307 | 0 | cryptoInfo.cipher.type = WC_CIPHER_DES3; |
3308 | 0 | cryptoInfo.cipher.enc = 0; |
3309 | 0 | cryptoInfo.cipher.des3.des = des3; |
3310 | 0 | cryptoInfo.cipher.des3.out = out; |
3311 | 0 | cryptoInfo.cipher.des3.in = in; |
3312 | 0 | cryptoInfo.cipher.des3.sz = sz; |
3313 | |
|
3314 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3315 | 0 | } |
3316 | | |
3317 | 114 | return wc_CryptoCb_TranslateErrorCode(ret); |
3318 | 114 | } |
3319 | | #endif /* !NO_DES3 */ |
3320 | | |
3321 | | #if defined(WOLFSSL_SM4) |
3322 | | #ifdef WOLFSSL_SM4_GCM |
3323 | | int wc_CryptoCb_Sm4GcmEncrypt(wc_Sm4* sm4, byte* out, |
3324 | | const byte* in, word32 sz, |
3325 | | const byte* nonce, word32 nonceSz, |
3326 | | byte* authTag, word32 authTagSz, |
3327 | | const byte* authIn, word32 authInSz) |
3328 | 0 | { |
3329 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3330 | 0 | CryptoCb* dev; |
3331 | | |
3332 | | /* locate registered callback */ |
3333 | 0 | if (sm4) { |
3334 | 0 | dev = wc_CryptoCb_FindDevice(sm4->devId, WC_ALGO_TYPE_CIPHER); |
3335 | 0 | } |
3336 | 0 | else { |
3337 | | /* locate first callback and try using it */ |
3338 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3339 | 0 | } |
3340 | |
|
3341 | 0 | if (dev && dev->cb) { |
3342 | 0 | wc_CryptoInfo cryptoInfo; |
3343 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3344 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3345 | 0 | cryptoInfo.cipher.type = WC_CIPHER_SM4_GCM; |
3346 | 0 | cryptoInfo.cipher.enc = 1; |
3347 | 0 | cryptoInfo.cipher.sm4gcm_enc.sm4 = sm4; |
3348 | 0 | cryptoInfo.cipher.sm4gcm_enc.out = out; |
3349 | 0 | cryptoInfo.cipher.sm4gcm_enc.in = in; |
3350 | 0 | cryptoInfo.cipher.sm4gcm_enc.sz = sz; |
3351 | 0 | cryptoInfo.cipher.sm4gcm_enc.nonce = nonce; |
3352 | 0 | cryptoInfo.cipher.sm4gcm_enc.nonceSz = nonceSz; |
3353 | 0 | cryptoInfo.cipher.sm4gcm_enc.authTag = authTag; |
3354 | 0 | cryptoInfo.cipher.sm4gcm_enc.authTagSz = authTagSz; |
3355 | 0 | cryptoInfo.cipher.sm4gcm_enc.authIn = authIn; |
3356 | 0 | cryptoInfo.cipher.sm4gcm_enc.authInSz = authInSz; |
3357 | |
|
3358 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3359 | 0 | } |
3360 | |
|
3361 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3362 | 0 | } |
3363 | | |
3364 | | int wc_CryptoCb_Sm4GcmDecrypt(wc_Sm4* sm4, byte* out, |
3365 | | const byte* in, word32 sz, |
3366 | | const byte* nonce, word32 nonceSz, |
3367 | | const byte* authTag, word32 authTagSz, |
3368 | | const byte* authIn, word32 authInSz) |
3369 | 0 | { |
3370 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3371 | 0 | CryptoCb* dev; |
3372 | | |
3373 | | /* locate registered callback */ |
3374 | 0 | if (sm4) { |
3375 | 0 | dev = wc_CryptoCb_FindDevice(sm4->devId, WC_ALGO_TYPE_CIPHER); |
3376 | 0 | } |
3377 | 0 | else { |
3378 | | /* locate first callback and try using it */ |
3379 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3380 | 0 | } |
3381 | |
|
3382 | 0 | if (dev && dev->cb) { |
3383 | 0 | wc_CryptoInfo cryptoInfo; |
3384 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3385 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3386 | 0 | cryptoInfo.cipher.type = WC_CIPHER_SM4_GCM; |
3387 | 0 | cryptoInfo.cipher.enc = 0; |
3388 | 0 | cryptoInfo.cipher.sm4gcm_dec.sm4 = sm4; |
3389 | 0 | cryptoInfo.cipher.sm4gcm_dec.out = out; |
3390 | 0 | cryptoInfo.cipher.sm4gcm_dec.in = in; |
3391 | 0 | cryptoInfo.cipher.sm4gcm_dec.sz = sz; |
3392 | 0 | cryptoInfo.cipher.sm4gcm_dec.nonce = nonce; |
3393 | 0 | cryptoInfo.cipher.sm4gcm_dec.nonceSz = nonceSz; |
3394 | 0 | cryptoInfo.cipher.sm4gcm_dec.authTag = authTag; |
3395 | 0 | cryptoInfo.cipher.sm4gcm_dec.authTagSz = authTagSz; |
3396 | 0 | cryptoInfo.cipher.sm4gcm_dec.authIn = authIn; |
3397 | 0 | cryptoInfo.cipher.sm4gcm_dec.authInSz = authInSz; |
3398 | |
|
3399 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3400 | 0 | } |
3401 | |
|
3402 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3403 | 0 | } |
3404 | | #endif /* WOLFSSL_SM4_GCM */ |
3405 | | |
3406 | | #ifdef WOLFSSL_SM4_CCM |
3407 | | int wc_CryptoCb_Sm4CcmEncrypt(wc_Sm4* sm4, byte* out, |
3408 | | const byte* in, word32 sz, |
3409 | | const byte* nonce, word32 nonceSz, |
3410 | | byte* authTag, word32 authTagSz, |
3411 | | const byte* authIn, word32 authInSz) |
3412 | 0 | { |
3413 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3414 | 0 | CryptoCb* dev; |
3415 | | |
3416 | | /* locate registered callback */ |
3417 | 0 | if (sm4) { |
3418 | 0 | dev = wc_CryptoCb_FindDevice(sm4->devId, WC_ALGO_TYPE_CIPHER); |
3419 | 0 | } |
3420 | 0 | else { |
3421 | | /* locate first callback and try using it */ |
3422 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3423 | 0 | } |
3424 | |
|
3425 | 0 | if (dev && dev->cb) { |
3426 | 0 | wc_CryptoInfo cryptoInfo; |
3427 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3428 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3429 | 0 | cryptoInfo.cipher.type = WC_CIPHER_SM4_CCM; |
3430 | 0 | cryptoInfo.cipher.enc = 1; |
3431 | 0 | cryptoInfo.cipher.sm4ccm_enc.sm4 = sm4; |
3432 | 0 | cryptoInfo.cipher.sm4ccm_enc.out = out; |
3433 | 0 | cryptoInfo.cipher.sm4ccm_enc.in = in; |
3434 | 0 | cryptoInfo.cipher.sm4ccm_enc.sz = sz; |
3435 | 0 | cryptoInfo.cipher.sm4ccm_enc.nonce = nonce; |
3436 | 0 | cryptoInfo.cipher.sm4ccm_enc.nonceSz = nonceSz; |
3437 | 0 | cryptoInfo.cipher.sm4ccm_enc.authTag = authTag; |
3438 | 0 | cryptoInfo.cipher.sm4ccm_enc.authTagSz = authTagSz; |
3439 | 0 | cryptoInfo.cipher.sm4ccm_enc.authIn = authIn; |
3440 | 0 | cryptoInfo.cipher.sm4ccm_enc.authInSz = authInSz; |
3441 | |
|
3442 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3443 | 0 | } |
3444 | |
|
3445 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3446 | 0 | } |
3447 | | |
3448 | | int wc_CryptoCb_Sm4CcmDecrypt(wc_Sm4* sm4, byte* out, |
3449 | | const byte* in, word32 sz, |
3450 | | const byte* nonce, word32 nonceSz, |
3451 | | const byte* authTag, word32 authTagSz, |
3452 | | const byte* authIn, word32 authInSz) |
3453 | 0 | { |
3454 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3455 | 0 | CryptoCb* dev; |
3456 | | |
3457 | | /* locate registered callback */ |
3458 | 0 | if (sm4) { |
3459 | 0 | dev = wc_CryptoCb_FindDevice(sm4->devId, WC_ALGO_TYPE_CIPHER); |
3460 | 0 | } |
3461 | 0 | else { |
3462 | | /* locate first callback and try using it */ |
3463 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3464 | 0 | } |
3465 | |
|
3466 | 0 | if (dev && dev->cb) { |
3467 | 0 | wc_CryptoInfo cryptoInfo; |
3468 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3469 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3470 | 0 | cryptoInfo.cipher.type = WC_CIPHER_SM4_CCM; |
3471 | 0 | cryptoInfo.cipher.enc = 0; |
3472 | 0 | cryptoInfo.cipher.sm4ccm_dec.sm4 = sm4; |
3473 | 0 | cryptoInfo.cipher.sm4ccm_dec.out = out; |
3474 | 0 | cryptoInfo.cipher.sm4ccm_dec.in = in; |
3475 | 0 | cryptoInfo.cipher.sm4ccm_dec.sz = sz; |
3476 | 0 | cryptoInfo.cipher.sm4ccm_dec.nonce = nonce; |
3477 | 0 | cryptoInfo.cipher.sm4ccm_dec.nonceSz = nonceSz; |
3478 | 0 | cryptoInfo.cipher.sm4ccm_dec.authTag = authTag; |
3479 | 0 | cryptoInfo.cipher.sm4ccm_dec.authTagSz = authTagSz; |
3480 | 0 | cryptoInfo.cipher.sm4ccm_dec.authIn = authIn; |
3481 | 0 | cryptoInfo.cipher.sm4ccm_dec.authInSz = authInSz; |
3482 | |
|
3483 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3484 | 0 | } |
3485 | |
|
3486 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3487 | 0 | } |
3488 | | #endif /* WOLFSSL_SM4_CCM */ |
3489 | | |
3490 | | #ifdef WOLFSSL_SM4_CBC |
3491 | | int wc_CryptoCb_Sm4CbcEncrypt(wc_Sm4* sm4, byte* out, |
3492 | | const byte* in, word32 sz) |
3493 | 0 | { |
3494 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3495 | 0 | CryptoCb* dev; |
3496 | | |
3497 | | /* locate registered callback */ |
3498 | 0 | if (sm4) { |
3499 | 0 | dev = wc_CryptoCb_FindDevice(sm4->devId, WC_ALGO_TYPE_CIPHER); |
3500 | 0 | } |
3501 | 0 | else { |
3502 | | /* locate first callback and try using it */ |
3503 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3504 | 0 | } |
3505 | |
|
3506 | 0 | if (dev && dev->cb) { |
3507 | 0 | wc_CryptoInfo cryptoInfo; |
3508 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3509 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3510 | 0 | cryptoInfo.cipher.type = WC_CIPHER_SM4_CBC; |
3511 | 0 | cryptoInfo.cipher.enc = 1; |
3512 | 0 | cryptoInfo.cipher.sm4cbc.sm4 = sm4; |
3513 | 0 | cryptoInfo.cipher.sm4cbc.out = out; |
3514 | 0 | cryptoInfo.cipher.sm4cbc.in = in; |
3515 | 0 | cryptoInfo.cipher.sm4cbc.sz = sz; |
3516 | |
|
3517 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3518 | 0 | } |
3519 | |
|
3520 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3521 | 0 | } |
3522 | | |
3523 | | int wc_CryptoCb_Sm4CbcDecrypt(wc_Sm4* sm4, byte* out, |
3524 | | const byte* in, word32 sz) |
3525 | 0 | { |
3526 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3527 | 0 | CryptoCb* dev; |
3528 | | |
3529 | | /* locate registered callback */ |
3530 | 0 | if (sm4) { |
3531 | 0 | dev = wc_CryptoCb_FindDevice(sm4->devId, WC_ALGO_TYPE_CIPHER); |
3532 | 0 | } |
3533 | 0 | else { |
3534 | | /* locate first callback and try using it */ |
3535 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3536 | 0 | } |
3537 | |
|
3538 | 0 | if (dev && dev->cb) { |
3539 | 0 | wc_CryptoInfo cryptoInfo; |
3540 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3541 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3542 | 0 | cryptoInfo.cipher.type = WC_CIPHER_SM4_CBC; |
3543 | 0 | cryptoInfo.cipher.enc = 0; |
3544 | 0 | cryptoInfo.cipher.sm4cbc.sm4 = sm4; |
3545 | 0 | cryptoInfo.cipher.sm4cbc.out = out; |
3546 | 0 | cryptoInfo.cipher.sm4cbc.in = in; |
3547 | 0 | cryptoInfo.cipher.sm4cbc.sz = sz; |
3548 | |
|
3549 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3550 | 0 | } |
3551 | |
|
3552 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3553 | 0 | } |
3554 | | #endif /* WOLFSSL_SM4_CBC */ |
3555 | | |
3556 | | #ifdef WOLFSSL_SM4_CTR |
3557 | | int wc_CryptoCb_Sm4CtrEncrypt(wc_Sm4* sm4, byte* out, |
3558 | | const byte* in, word32 sz) |
3559 | 0 | { |
3560 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3561 | 0 | CryptoCb* dev; |
3562 | | |
3563 | | /* locate registered callback */ |
3564 | 0 | if (sm4) { |
3565 | 0 | dev = wc_CryptoCb_FindDevice(sm4->devId, WC_ALGO_TYPE_CIPHER); |
3566 | 0 | } |
3567 | 0 | else { |
3568 | | /* locate first callback and try using it */ |
3569 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3570 | 0 | } |
3571 | |
|
3572 | 0 | if (dev && dev->cb) { |
3573 | 0 | wc_CryptoInfo cryptoInfo; |
3574 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3575 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3576 | 0 | cryptoInfo.cipher.type = WC_CIPHER_SM4_CTR; |
3577 | 0 | cryptoInfo.cipher.enc = 1; |
3578 | 0 | cryptoInfo.cipher.sm4ctr.sm4 = sm4; |
3579 | 0 | cryptoInfo.cipher.sm4ctr.out = out; |
3580 | 0 | cryptoInfo.cipher.sm4ctr.in = in; |
3581 | 0 | cryptoInfo.cipher.sm4ctr.sz = sz; |
3582 | |
|
3583 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3584 | 0 | } |
3585 | |
|
3586 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3587 | 0 | } |
3588 | | #endif /* WOLFSSL_SM4_CTR */ |
3589 | | |
3590 | | #ifdef WOLFSSL_SM4_ECB |
3591 | | int wc_CryptoCb_Sm4EcbEncrypt(wc_Sm4* sm4, byte* out, |
3592 | | const byte* in, word32 sz) |
3593 | 0 | { |
3594 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3595 | 0 | CryptoCb* dev; |
3596 | | |
3597 | | /* locate registered callback */ |
3598 | 0 | if (sm4) { |
3599 | 0 | dev = wc_CryptoCb_FindDevice(sm4->devId, WC_ALGO_TYPE_CIPHER); |
3600 | 0 | } |
3601 | 0 | else { |
3602 | | /* locate first callback and try using it */ |
3603 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3604 | 0 | } |
3605 | |
|
3606 | 0 | if (dev && dev->cb) { |
3607 | 0 | wc_CryptoInfo cryptoInfo; |
3608 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3609 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3610 | 0 | cryptoInfo.cipher.type = WC_CIPHER_SM4_ECB; |
3611 | 0 | cryptoInfo.cipher.enc = 1; |
3612 | 0 | cryptoInfo.cipher.sm4ecb.sm4 = sm4; |
3613 | 0 | cryptoInfo.cipher.sm4ecb.out = out; |
3614 | 0 | cryptoInfo.cipher.sm4ecb.in = in; |
3615 | 0 | cryptoInfo.cipher.sm4ecb.sz = sz; |
3616 | |
|
3617 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3618 | 0 | } |
3619 | |
|
3620 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3621 | 0 | } |
3622 | | |
3623 | | int wc_CryptoCb_Sm4EcbDecrypt(wc_Sm4* sm4, byte* out, |
3624 | | const byte* in, word32 sz) |
3625 | 0 | { |
3626 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3627 | 0 | CryptoCb* dev; |
3628 | | |
3629 | | /* locate registered callback */ |
3630 | 0 | if (sm4) { |
3631 | 0 | dev = wc_CryptoCb_FindDevice(sm4->devId, WC_ALGO_TYPE_CIPHER); |
3632 | 0 | } |
3633 | 0 | else { |
3634 | | /* locate first callback and try using it */ |
3635 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3636 | 0 | } |
3637 | |
|
3638 | 0 | if (dev && dev->cb) { |
3639 | 0 | wc_CryptoInfo cryptoInfo; |
3640 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3641 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
3642 | 0 | cryptoInfo.cipher.type = WC_CIPHER_SM4_ECB; |
3643 | 0 | cryptoInfo.cipher.enc = 0; |
3644 | 0 | cryptoInfo.cipher.sm4ecb.sm4 = sm4; |
3645 | 0 | cryptoInfo.cipher.sm4ecb.out = out; |
3646 | 0 | cryptoInfo.cipher.sm4ecb.in = in; |
3647 | 0 | cryptoInfo.cipher.sm4ecb.sz = sz; |
3648 | |
|
3649 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3650 | 0 | } |
3651 | |
|
3652 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3653 | 0 | } |
3654 | | #endif /* WOLFSSL_SM4_ECB */ |
3655 | | #endif /* WOLFSSL_SM4 */ |
3656 | | |
3657 | | #ifndef NO_SHA |
3658 | | int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in, |
3659 | | word32 inSz, byte* digest) |
3660 | 0 | { |
3661 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3662 | 0 | CryptoCb* dev; |
3663 | | |
3664 | | /* locate registered callback */ |
3665 | 0 | if (sha) { |
3666 | 0 | dev = wc_CryptoCb_FindDevice(sha->devId, WC_ALGO_TYPE_HASH); |
3667 | 0 | } |
3668 | 0 | else { |
3669 | | /* locate first callback and try using it */ |
3670 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3671 | 0 | } |
3672 | |
|
3673 | 0 | if (dev && dev->cb) { |
3674 | 0 | wc_CryptoInfo cryptoInfo; |
3675 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3676 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
3677 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA; |
3678 | 0 | cryptoInfo.hash.sha1 = sha; |
3679 | 0 | cryptoInfo.hash.in = in; |
3680 | 0 | cryptoInfo.hash.inSz = inSz; |
3681 | 0 | cryptoInfo.hash.digest = digest; |
3682 | |
|
3683 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3684 | 0 | } |
3685 | |
|
3686 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3687 | 0 | } |
3688 | | #endif /* !NO_SHA */ |
3689 | | |
3690 | | #ifdef WOLFSSL_SHA224 |
3691 | | int wc_CryptoCb_Sha224Hash(wc_Sha224* sha224, const byte* in, |
3692 | | word32 inSz, byte* digest) |
3693 | 197 | { |
3694 | 197 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3695 | 197 | CryptoCb* dev; |
3696 | | |
3697 | | /* locate registered callback */ |
3698 | 197 | if (sha224) { |
3699 | 197 | dev = wc_CryptoCb_FindDevice(sha224->devId, WC_ALGO_TYPE_HASH); |
3700 | 197 | } |
3701 | 0 | else { |
3702 | | /* locate first callback and try using it */ |
3703 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3704 | 0 | } |
3705 | | |
3706 | 197 | if (dev && dev->cb) { |
3707 | 0 | wc_CryptoInfo cryptoInfo; |
3708 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3709 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
3710 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA224; |
3711 | 0 | cryptoInfo.hash.sha224 = sha224; |
3712 | 0 | cryptoInfo.hash.in = in; |
3713 | 0 | cryptoInfo.hash.inSz = inSz; |
3714 | 0 | cryptoInfo.hash.digest = digest; |
3715 | |
|
3716 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3717 | 0 | } |
3718 | | |
3719 | 197 | return wc_CryptoCb_TranslateErrorCode(ret); |
3720 | 197 | } |
3721 | | #endif /* WOLFSSL_SHA224 */ |
3722 | | |
3723 | | |
3724 | | #ifndef NO_SHA256 |
3725 | | int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in, |
3726 | | word32 inSz, byte* digest) |
3727 | 190 | { |
3728 | 190 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3729 | 190 | CryptoCb* dev; |
3730 | | |
3731 | | /* locate registered callback */ |
3732 | 190 | if (sha256) { |
3733 | 190 | dev = wc_CryptoCb_FindDevice(sha256->devId, WC_ALGO_TYPE_HASH); |
3734 | 190 | } |
3735 | 0 | else { |
3736 | | /* locate first callback and try using it */ |
3737 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3738 | 0 | } |
3739 | | |
3740 | 190 | if (dev && dev->cb) { |
3741 | 0 | wc_CryptoInfo cryptoInfo; |
3742 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3743 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
3744 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA256; |
3745 | 0 | cryptoInfo.hash.sha256 = sha256; |
3746 | 0 | cryptoInfo.hash.in = in; |
3747 | 0 | cryptoInfo.hash.inSz = inSz; |
3748 | 0 | cryptoInfo.hash.digest = digest; |
3749 | |
|
3750 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3751 | 0 | } |
3752 | | |
3753 | 190 | return wc_CryptoCb_TranslateErrorCode(ret); |
3754 | 190 | } |
3755 | | #endif /* !NO_SHA256 */ |
3756 | | |
3757 | | #ifdef WOLFSSL_SHA384 |
3758 | | int wc_CryptoCb_Sha384Hash(wc_Sha384* sha384, const byte* in, |
3759 | | word32 inSz, byte* digest) |
3760 | 191 | { |
3761 | 191 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3762 | 191 | CryptoCb* dev; |
3763 | | |
3764 | | /* locate registered callback */ |
3765 | 191 | #ifndef NO_SHA2_CRYPTO_CB |
3766 | 191 | if (sha384) { |
3767 | 191 | dev = wc_CryptoCb_FindDevice(sha384->devId, WC_ALGO_TYPE_HASH); |
3768 | 191 | } |
3769 | 0 | else |
3770 | 0 | #endif |
3771 | 0 | { |
3772 | | /* locate first callback and try using it */ |
3773 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3774 | 0 | } |
3775 | | |
3776 | 191 | if (dev && dev->cb) { |
3777 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLF_CRYPTO_CB_NO_SHA512_FALLBACK) |
3778 | 0 | byte localHash[WC_SHA512_DIGEST_SIZE]; |
3779 | 0 | #endif |
3780 | 0 | wc_CryptoInfo cryptoInfo; |
3781 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3782 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
3783 | 0 | cryptoInfo.hash.in = in; |
3784 | 0 | cryptoInfo.hash.inSz = inSz; |
3785 | | |
3786 | | /* try the SHA-384 callback first */ |
3787 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA384; |
3788 | 0 | cryptoInfo.hash.sha384 = sha384; |
3789 | 0 | cryptoInfo.hash.digest = digest; |
3790 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3791 | 0 | ret = wc_CryptoCb_TranslateErrorCode(ret); |
3792 | 0 | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
3793 | 0 | return ret; |
3794 | | |
3795 | 0 | #if defined(WOLFSSL_SHA512) && !defined(WOLF_CRYPTO_CB_NO_SHA512_FALLBACK) |
3796 | | /* fall back to the SHA-512 core: SHA-384 is the SHA-512 core with a |
3797 | | * different IV (in the caller-supplied state) and a 48-byte |
3798 | | * truncation done here */ |
3799 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA512; |
3800 | 0 | cryptoInfo.hash.sha512 = (wc_Sha512*)sha384; |
3801 | | /* use local buffer for the final digest so we can truncate */ |
3802 | 0 | if (digest != NULL) |
3803 | 0 | cryptoInfo.hash.digest = localHash; |
3804 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3805 | 0 | ret = wc_CryptoCb_TranslateErrorCode(ret); |
3806 | 0 | if (ret == 0 && digest != NULL) { |
3807 | 0 | XMEMCPY(digest, localHash, WC_SHA384_DIGEST_SIZE); |
3808 | | /* the SHA-512 callback left the SHA-512 IV in the state; write |
3809 | | * the SHA-384 IV back so the struct is ready for reuse */ |
3810 | 0 | if (sha384 != NULL) { |
3811 | 0 | sha384->digest[0] = W64LIT(0xcbbb9d5dc1059ed8); |
3812 | 0 | sha384->digest[1] = W64LIT(0x629a292a367cd507); |
3813 | 0 | sha384->digest[2] = W64LIT(0x9159015a3070dd17); |
3814 | 0 | sha384->digest[3] = W64LIT(0x152fecd8f70e5939); |
3815 | 0 | sha384->digest[4] = W64LIT(0x67332667ffc00b31); |
3816 | 0 | sha384->digest[5] = W64LIT(0x8eb44a8768581511); |
3817 | 0 | sha384->digest[6] = W64LIT(0xdb0c2e0d64f98fa7); |
3818 | 0 | sha384->digest[7] = W64LIT(0x47b5481dbefa4fa4); |
3819 | 0 | } |
3820 | 0 | } |
3821 | 0 | return ret; |
3822 | 0 | #endif /* WOLFSSL_SHA512 && !WOLF_CRYPTO_CB_NO_SHA512_FALLBACK */ |
3823 | 0 | } |
3824 | | |
3825 | 191 | return wc_CryptoCb_TranslateErrorCode(ret); |
3826 | 191 | } |
3827 | | #endif /* WOLFSSL_SHA384 */ |
3828 | | |
3829 | | #ifdef WOLFSSL_SHA512 |
3830 | | int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in, |
3831 | | word32 inSz, byte* digest |
3832 | | #if !(defined(HAVE_FIPS) && FIPS_VERSION_LT(7,0)) |
3833 | | , size_t digestSz |
3834 | | #endif |
3835 | | ) |
3836 | 842 | { |
3837 | 842 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3838 | 842 | CryptoCb* dev; |
3839 | | #if defined(HAVE_FIPS) && FIPS_VERSION_LT(7,0) |
3840 | | /* Older FIPS sha512.c snapshots call the 4-arg API (no digestSz). Treat as |
3841 | | * full-size SHA-512 with no variant dispatch, matching pre-digestSz |
3842 | | * behavior. */ |
3843 | | size_t digestSz = WC_SHA512_DIGEST_SIZE; |
3844 | | #endif |
3845 | | |
3846 | | /* locate registered callback */ |
3847 | 842 | #ifndef NO_SHA2_CRYPTO_CB |
3848 | 842 | if (sha512) { |
3849 | 842 | dev = wc_CryptoCb_FindDevice(sha512->devId, WC_ALGO_TYPE_HASH); |
3850 | 842 | } |
3851 | 0 | else |
3852 | 0 | #endif |
3853 | 0 | { |
3854 | | /* locate first callback and try using it */ |
3855 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3856 | 0 | } |
3857 | | |
3858 | 842 | if (dev && dev->cb) { |
3859 | 696 | #ifndef WOLF_CRYPTO_CB_NO_SHA512_FALLBACK |
3860 | 696 | byte localHash[WC_SHA512_DIGEST_SIZE]; |
3861 | 696 | #endif |
3862 | 696 | wc_CryptoInfo cryptoInfo; |
3863 | 696 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3864 | 696 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
3865 | 696 | cryptoInfo.hash.sha512 = sha512; |
3866 | 696 | cryptoInfo.hash.in = in; |
3867 | 696 | cryptoInfo.hash.inSz = inSz; |
3868 | 696 | cryptoInfo.hash.digest = digest; |
3869 | | |
3870 | | /* try the specific family callbacks first */ |
3871 | 696 | #if !defined(WOLFSSL_NOSHA512_224) |
3872 | 696 | if (digest != NULL && digestSz == WC_SHA512_224_DIGEST_SIZE) { |
3873 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA512_224; |
3874 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3875 | 0 | ret = wc_CryptoCb_TranslateErrorCode(ret); |
3876 | 0 | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
3877 | 0 | return ret; |
3878 | | #ifdef WOLF_CRYPTO_CB_NO_SHA512_FALLBACK |
3879 | | return ret; |
3880 | | #endif |
3881 | 0 | } |
3882 | 696 | #endif |
3883 | 696 | #if !defined(WOLFSSL_NOSHA512_256) |
3884 | 696 | if (digest != NULL && digestSz == WC_SHA512_256_DIGEST_SIZE) { |
3885 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA512_256; |
3886 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3887 | 0 | ret = wc_CryptoCb_TranslateErrorCode(ret); |
3888 | 0 | if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) |
3889 | 0 | return ret; |
3890 | | #ifdef WOLF_CRYPTO_CB_NO_SHA512_FALLBACK |
3891 | | return ret; |
3892 | | #endif |
3893 | 0 | } |
3894 | 696 | #endif |
3895 | 696 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA512; |
3896 | 696 | #ifndef WOLF_CRYPTO_CB_NO_SHA512_FALLBACK |
3897 | | /* use local buffer if not full size */ |
3898 | 696 | if (digest != NULL && digestSz != WC_SHA512_DIGEST_SIZE) |
3899 | 0 | cryptoInfo.hash.digest = localHash; |
3900 | 696 | #endif |
3901 | 696 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3902 | 696 | ret = wc_CryptoCb_TranslateErrorCode(ret); |
3903 | 696 | #ifndef WOLF_CRYPTO_CB_NO_SHA512_FALLBACK |
3904 | 696 | if (ret == 0 && digest != NULL && digestSz != WC_SHA512_DIGEST_SIZE) { |
3905 | 0 | XMEMCPY(digest, localHash, digestSz); |
3906 | 0 | #if !defined(WOLFSSL_NOSHA512_224) |
3907 | 0 | if (sha512 != NULL && digestSz == WC_SHA512_224_DIGEST_SIZE) { |
3908 | 0 | sha512->digest[0] = W64LIT(0x8c3d37c819544da2); |
3909 | 0 | sha512->digest[1] = W64LIT(0x73e1996689dcd4d6); |
3910 | 0 | sha512->digest[2] = W64LIT(0x1dfab7ae32ff9c82); |
3911 | 0 | sha512->digest[3] = W64LIT(0x679dd514582f9fcf); |
3912 | 0 | sha512->digest[4] = W64LIT(0x0f6d2b697bd44da8); |
3913 | 0 | sha512->digest[5] = W64LIT(0x77e36f7304c48942); |
3914 | 0 | sha512->digest[6] = W64LIT(0x3f9d85a86a1d36c8); |
3915 | 0 | sha512->digest[7] = W64LIT(0x1112e6ad91d692a1); |
3916 | 0 | } |
3917 | 0 | #endif |
3918 | 0 | #if !defined(WOLFSSL_NOSHA512_256) |
3919 | 0 | if (sha512 != NULL && digestSz == WC_SHA512_256_DIGEST_SIZE) { |
3920 | 0 | sha512->digest[0] = W64LIT(0x22312194fc2bf72c); |
3921 | 0 | sha512->digest[1] = W64LIT(0x9f555fa3c84c64c2); |
3922 | 0 | sha512->digest[2] = W64LIT(0x2393b86b6f53b151); |
3923 | 0 | sha512->digest[3] = W64LIT(0x963877195940eabd); |
3924 | 0 | sha512->digest[4] = W64LIT(0x96283ee2a88effe3); |
3925 | 0 | sha512->digest[5] = W64LIT(0xbe5e1e2553863992); |
3926 | 0 | sha512->digest[6] = W64LIT(0x2b0199fc2c85b8aa); |
3927 | 0 | sha512->digest[7] = W64LIT(0x0eb72ddc81c52ca2); |
3928 | 0 | } |
3929 | 0 | #endif |
3930 | 0 | } |
3931 | 696 | #endif /* !WOLF_CRYPTO_CB_NO_SHA512_FALLBACK */ |
3932 | 696 | return ret; |
3933 | 696 | } |
3934 | | |
3935 | 146 | return wc_CryptoCb_TranslateErrorCode(ret); |
3936 | 842 | } |
3937 | | #endif /* WOLFSSL_SHA512 */ |
3938 | | |
3939 | | #if defined(WOLFSSL_SHA3) && (!defined(HAVE_FIPS) || FIPS_VERSION_GE(6, 0)) |
3940 | | int wc_CryptoCb_Sha3Hash(wc_Sha3* sha3, int type, const byte* in, |
3941 | | word32 inSz, byte* digest) |
3942 | 0 | { |
3943 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3944 | 0 | CryptoCb* dev; |
3945 | | |
3946 | | /* locate registered callback */ |
3947 | 0 | if (sha3) { |
3948 | 0 | dev = wc_CryptoCb_FindDevice(sha3->devId, WC_ALGO_TYPE_HASH); |
3949 | 0 | } |
3950 | 0 | else |
3951 | 0 | { |
3952 | | /* locate first callback and try using it */ |
3953 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3954 | 0 | } |
3955 | |
|
3956 | 0 | if (dev && dev->cb) { |
3957 | 0 | wc_CryptoInfo cryptoInfo; |
3958 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3959 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
3960 | 0 | cryptoInfo.hash.type = type; |
3961 | 0 | cryptoInfo.hash.sha3 = sha3; |
3962 | 0 | cryptoInfo.hash.in = in; |
3963 | 0 | cryptoInfo.hash.inSz = inSz; |
3964 | 0 | cryptoInfo.hash.digest = digest; |
3965 | |
|
3966 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
3967 | 0 | } |
3968 | |
|
3969 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
3970 | 0 | } |
3971 | | |
3972 | | #if defined(WOLFSSL_SHAKE128) || defined(WOLFSSL_SHAKE256) |
3973 | | int wc_CryptoCb_Shake(wc_Sha3* shake, int type, const byte* in, |
3974 | | word32 inSz, byte* out, word32 outSz, int shakeOp) |
3975 | 0 | { |
3976 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
3977 | 0 | CryptoCb* dev; |
3978 | |
|
3979 | 0 | #ifndef WOLF_CRYPTO_CB_SHAKE_XOF |
3980 | 0 | (void)shakeOp; |
3981 | 0 | #endif |
3982 | | |
3983 | | /* locate registered callback */ |
3984 | 0 | if (shake) { |
3985 | 0 | dev = wc_CryptoCb_FindDevice(shake->devId, WC_ALGO_TYPE_HASH); |
3986 | 0 | } |
3987 | 0 | else { |
3988 | | /* locate first callback and try using it */ |
3989 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
3990 | 0 | } |
3991 | |
|
3992 | 0 | if (dev && dev->cb) { |
3993 | 0 | wc_CryptoInfo cryptoInfo; |
3994 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
3995 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
3996 | 0 | cryptoInfo.hash.type = type; |
3997 | 0 | cryptoInfo.hash.sha3 = shake; /* wc_Shake is a wc_Sha3 */ |
3998 | 0 | cryptoInfo.hash.in = in; |
3999 | 0 | cryptoInfo.hash.inSz = inSz; |
4000 | 0 | cryptoInfo.hash.digest = out; |
4001 | 0 | cryptoInfo.hash.outSz = outSz; |
4002 | | #ifdef WOLF_CRYPTO_CB_SHAKE_XOF |
4003 | | cryptoInfo.hash.shakeOp = shakeOp; |
4004 | | #endif |
4005 | |
|
4006 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4007 | 0 | } |
4008 | |
|
4009 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
4010 | 0 | } |
4011 | | #endif /* WOLFSSL_SHAKE128 || WOLFSSL_SHAKE256 */ |
4012 | | #endif /* WOLFSSL_SHA3 && (!HAVE_FIPS || FIPS_VERSION_GE(6, 0)) */ |
4013 | | |
4014 | | #if defined(WOLFSSL_SM3) |
4015 | | int wc_CryptoCb_Sm3Hash(wc_Sm3* sm3, const byte* in, |
4016 | | word32 inSz, byte* digest) |
4017 | 0 | { |
4018 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4019 | 0 | CryptoCb* dev; |
4020 | | |
4021 | | /* locate registered callback */ |
4022 | 0 | if (sm3) { |
4023 | 0 | dev = wc_CryptoCb_FindDevice(sm3->devId, WC_ALGO_TYPE_HASH); |
4024 | 0 | } |
4025 | 0 | else { |
4026 | | /* locate first callback and try using it */ |
4027 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
4028 | 0 | } |
4029 | |
|
4030 | 0 | if (dev && dev->cb) { |
4031 | 0 | wc_CryptoInfo cryptoInfo; |
4032 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4033 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
4034 | 0 | cryptoInfo.hash.type = WC_HASH_TYPE_SM3; |
4035 | 0 | cryptoInfo.hash.sm3 = sm3; |
4036 | 0 | cryptoInfo.hash.in = in; |
4037 | 0 | cryptoInfo.hash.inSz = inSz; |
4038 | 0 | cryptoInfo.hash.digest = digest; |
4039 | |
|
4040 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4041 | 0 | } |
4042 | |
|
4043 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
4044 | 0 | } |
4045 | | #endif /* WOLFSSL_SM3 */ |
4046 | | |
4047 | | #ifndef NO_HMAC |
4048 | | int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in, word32 inSz, |
4049 | | byte* digest) |
4050 | 227 | { |
4051 | 227 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4052 | 227 | CryptoCb* dev; |
4053 | | |
4054 | 227 | if (hmac == NULL) |
4055 | 0 | return ret; |
4056 | | |
4057 | | /* locate registered callback */ |
4058 | 227 | dev = wc_CryptoCb_FindDevice(hmac->devId, WC_ALGO_TYPE_HMAC); |
4059 | 227 | if (dev && dev->cb) { |
4060 | 0 | wc_CryptoInfo cryptoInfo; |
4061 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4062 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_HMAC; |
4063 | 0 | cryptoInfo.hmac.macType = macType; |
4064 | 0 | cryptoInfo.hmac.in = in; |
4065 | 0 | cryptoInfo.hmac.inSz = inSz; |
4066 | 0 | cryptoInfo.hmac.digest = digest; |
4067 | 0 | cryptoInfo.hmac.hmac = hmac; |
4068 | |
|
4069 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4070 | 0 | } |
4071 | | |
4072 | 227 | return wc_CryptoCb_TranslateErrorCode(ret); |
4073 | 227 | } |
4074 | | #endif /* !NO_HMAC */ |
4075 | | |
4076 | | #ifndef WC_NO_RNG |
4077 | | int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz) |
4078 | 15.5k | { |
4079 | 15.5k | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4080 | 15.5k | CryptoCb* dev; |
4081 | | |
4082 | | /* locate registered callback */ |
4083 | 15.5k | if (rng) { |
4084 | 15.5k | dev = wc_CryptoCb_FindDevice(rng->devId, WC_ALGO_TYPE_RNG); |
4085 | 15.5k | } |
4086 | 0 | else { |
4087 | | /* locate first callback and try using it */ |
4088 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
4089 | 0 | } |
4090 | | |
4091 | 15.5k | if (dev && dev->cb) { |
4092 | 15.5k | wc_CryptoInfo cryptoInfo; |
4093 | 15.5k | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4094 | 15.5k | cryptoInfo.algo_type = WC_ALGO_TYPE_RNG; |
4095 | 15.5k | cryptoInfo.rng.rng = rng; |
4096 | 15.5k | cryptoInfo.rng.out = out; |
4097 | 15.5k | cryptoInfo.rng.sz = sz; |
4098 | | |
4099 | 15.5k | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4100 | 15.5k | } |
4101 | | |
4102 | 15.5k | return wc_CryptoCb_TranslateErrorCode(ret); |
4103 | 15.5k | } |
4104 | | |
4105 | | int wc_CryptoCb_RandomSeed(OS_Seed* os, byte* seed, word32 sz) |
4106 | 12 | { |
4107 | 12 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4108 | 12 | CryptoCb* dev; |
4109 | | |
4110 | | /* locate registered callback */ |
4111 | 12 | dev = wc_CryptoCb_FindDevice(os->devId, WC_ALGO_TYPE_SEED); |
4112 | 12 | if (dev && dev->cb) { |
4113 | 12 | wc_CryptoInfo cryptoInfo; |
4114 | 12 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4115 | 12 | cryptoInfo.algo_type = WC_ALGO_TYPE_SEED; |
4116 | 12 | cryptoInfo.seed.os = os; |
4117 | 12 | cryptoInfo.seed.seed = seed; |
4118 | 12 | cryptoInfo.seed.sz = sz; |
4119 | | |
4120 | 12 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4121 | 12 | } |
4122 | | |
4123 | 12 | return wc_CryptoCb_TranslateErrorCode(ret); |
4124 | 12 | } |
4125 | | #endif /* !WC_NO_RNG */ |
4126 | | |
4127 | | #ifndef NO_CERTS |
4128 | | int wc_CryptoCb_GetCert(int devId, const char *label, word32 labelLen, |
4129 | | const byte *id, word32 idLen, byte** out, |
4130 | | word32* outSz, int *format, void *heap) |
4131 | 0 | { |
4132 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4133 | 0 | CryptoCb* dev; |
4134 | | |
4135 | | /* locate registered callback */ |
4136 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_CERT); |
4137 | 0 | if (dev && dev->cb) { |
4138 | 0 | wc_CryptoInfo cryptoInfo; |
4139 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4140 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CERT; |
4141 | 0 | cryptoInfo.cert.label = label; |
4142 | 0 | cryptoInfo.cert.labelLen = labelLen; |
4143 | 0 | cryptoInfo.cert.id = id; |
4144 | 0 | cryptoInfo.cert.idLen = idLen; |
4145 | 0 | cryptoInfo.cert.heap = heap; |
4146 | 0 | cryptoInfo.cert.certDataOut = out; |
4147 | 0 | cryptoInfo.cert.certSz = outSz; |
4148 | 0 | cryptoInfo.cert.certFormatOut = format; |
4149 | 0 | cryptoInfo.cert.heap = heap; |
4150 | |
|
4151 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4152 | 0 | } |
4153 | |
|
4154 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
4155 | 0 | } |
4156 | | #endif /* ifndef NO_CERTS */ |
4157 | | |
4158 | | #if defined(WOLFSSL_CMAC) |
4159 | | int wc_CryptoCb_Cmac(Cmac* cmac, const byte* key, word32 keySz, |
4160 | | const byte* in, word32 inSz, byte* out, word32* outSz, int type, |
4161 | | void* ctx) |
4162 | 0 | { |
4163 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4164 | 0 | CryptoCb* dev; |
4165 | | |
4166 | | /* locate registered callback */ |
4167 | 0 | if (cmac) { |
4168 | 0 | dev = wc_CryptoCb_FindDevice(cmac->devId, WC_ALGO_TYPE_CMAC); |
4169 | 0 | } |
4170 | 0 | else { |
4171 | | /* locate first callback and try using it */ |
4172 | 0 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
4173 | 0 | } |
4174 | 0 | if (dev && dev->cb) { |
4175 | 0 | wc_CryptoInfo cryptoInfo; |
4176 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4177 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_CMAC; |
4178 | |
|
4179 | 0 | cryptoInfo.cmac.cmac = cmac; |
4180 | 0 | cryptoInfo.cmac.ctx = ctx; |
4181 | 0 | cryptoInfo.cmac.key = key; |
4182 | 0 | cryptoInfo.cmac.in = in; |
4183 | 0 | cryptoInfo.cmac.out = out; |
4184 | 0 | cryptoInfo.cmac.outSz = outSz; |
4185 | 0 | cryptoInfo.cmac.keySz = keySz; |
4186 | 0 | cryptoInfo.cmac.inSz = inSz; |
4187 | 0 | cryptoInfo.cmac.type = type; |
4188 | |
|
4189 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4190 | 0 | } |
4191 | |
|
4192 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
4193 | 0 | } |
4194 | | #endif /* WOLFSSL_CMAC */ |
4195 | | |
4196 | | #ifdef WOLFSSL_SHE |
4197 | | int wc_CryptoCb_SheGetUid(wc_SHE* she, byte* uid, word32 uidSz, |
4198 | | const void* ctx) |
4199 | | { |
4200 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4201 | | CryptoCb* dev; |
4202 | | |
4203 | | if (she == NULL) { |
4204 | | return BAD_FUNC_ARG; |
4205 | | } |
4206 | | |
4207 | | /* locate registered callback */ |
4208 | | dev = wc_CryptoCb_FindDevice(she->devId, WC_ALGO_TYPE_SHE); |
4209 | | if (dev && dev->cb) { |
4210 | | wc_CryptoInfo cryptoInfo; |
4211 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4212 | | cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; |
4213 | | cryptoInfo.she.she = she; |
4214 | | cryptoInfo.she.type = WC_SHE_GET_UID; |
4215 | | cryptoInfo.she.ctx = ctx; |
4216 | | cryptoInfo.she.op.getUid.uid = uid; |
4217 | | cryptoInfo.she.op.getUid.uidSz = uidSz; |
4218 | | |
4219 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4220 | | } |
4221 | | |
4222 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4223 | | } |
4224 | | |
4225 | | int wc_CryptoCb_SheGetCounter(wc_SHE* she, word32* counter, const void* ctx) |
4226 | | { |
4227 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4228 | | CryptoCb* dev; |
4229 | | |
4230 | | if (she == NULL || counter == NULL) { |
4231 | | return BAD_FUNC_ARG; |
4232 | | } |
4233 | | |
4234 | | dev = wc_CryptoCb_FindDevice(she->devId, WC_ALGO_TYPE_SHE); |
4235 | | if (dev && dev->cb) { |
4236 | | wc_CryptoInfo cryptoInfo; |
4237 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4238 | | cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; |
4239 | | cryptoInfo.she.she = she; |
4240 | | cryptoInfo.she.type = WC_SHE_GET_COUNTER; |
4241 | | cryptoInfo.she.ctx = ctx; |
4242 | | cryptoInfo.she.op.getCounter.counter = counter; |
4243 | | |
4244 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4245 | | } |
4246 | | |
4247 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4248 | | } |
4249 | | |
4250 | | int wc_CryptoCb_SheGenerateM1M2M3(wc_SHE* she, |
4251 | | const byte* uid, word32 uidSz, |
4252 | | byte authKeyId, const byte* authKey, word32 authKeySz, |
4253 | | byte targetKeyId, const byte* newKey, word32 newKeySz, |
4254 | | word32 counter, byte flags, |
4255 | | byte* m1, word32 m1Sz, |
4256 | | byte* m2, word32 m2Sz, |
4257 | | byte* m3, word32 m3Sz) |
4258 | | { |
4259 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4260 | | CryptoCb* dev; |
4261 | | |
4262 | | if (she == NULL) { |
4263 | | return BAD_FUNC_ARG; |
4264 | | } |
4265 | | |
4266 | | dev = wc_CryptoCb_FindDevice(she->devId, WC_ALGO_TYPE_SHE); |
4267 | | if (dev && dev->cb) { |
4268 | | wc_CryptoInfo cryptoInfo; |
4269 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4270 | | cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; |
4271 | | cryptoInfo.she.she = she; |
4272 | | cryptoInfo.she.type = WC_SHE_GENERATE_M1M2M3; |
4273 | | cryptoInfo.she.op.generateM1M2M3.uid = uid; |
4274 | | cryptoInfo.she.op.generateM1M2M3.uidSz = uidSz; |
4275 | | cryptoInfo.she.op.generateM1M2M3.authKeyId = authKeyId; |
4276 | | cryptoInfo.she.op.generateM1M2M3.authKey = authKey; |
4277 | | cryptoInfo.she.op.generateM1M2M3.authKeySz = authKeySz; |
4278 | | cryptoInfo.she.op.generateM1M2M3.targetKeyId = targetKeyId; |
4279 | | cryptoInfo.she.op.generateM1M2M3.newKey = newKey; |
4280 | | cryptoInfo.she.op.generateM1M2M3.newKeySz = newKeySz; |
4281 | | cryptoInfo.she.op.generateM1M2M3.counter = counter; |
4282 | | cryptoInfo.she.op.generateM1M2M3.flags = flags; |
4283 | | cryptoInfo.she.op.generateM1M2M3.m1 = m1; |
4284 | | cryptoInfo.she.op.generateM1M2M3.m1Sz = m1Sz; |
4285 | | cryptoInfo.she.op.generateM1M2M3.m2 = m2; |
4286 | | cryptoInfo.she.op.generateM1M2M3.m2Sz = m2Sz; |
4287 | | cryptoInfo.she.op.generateM1M2M3.m3 = m3; |
4288 | | cryptoInfo.she.op.generateM1M2M3.m3Sz = m3Sz; |
4289 | | |
4290 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4291 | | } |
4292 | | |
4293 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4294 | | } |
4295 | | |
4296 | | int wc_CryptoCb_SheGenerateM4M5(wc_SHE* she, |
4297 | | const byte* uid, word32 uidSz, |
4298 | | byte authKeyId, byte targetKeyId, |
4299 | | const byte* newKey, word32 newKeySz, |
4300 | | word32 counter, |
4301 | | byte* m4, word32 m4Sz, |
4302 | | byte* m5, word32 m5Sz) |
4303 | | { |
4304 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4305 | | CryptoCb* dev; |
4306 | | |
4307 | | if (she == NULL) { |
4308 | | return BAD_FUNC_ARG; |
4309 | | } |
4310 | | |
4311 | | dev = wc_CryptoCb_FindDevice(she->devId, WC_ALGO_TYPE_SHE); |
4312 | | if (dev && dev->cb) { |
4313 | | wc_CryptoInfo cryptoInfo; |
4314 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4315 | | cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; |
4316 | | cryptoInfo.she.she = she; |
4317 | | cryptoInfo.she.type = WC_SHE_GENERATE_M4M5; |
4318 | | cryptoInfo.she.op.generateM4M5.uid = uid; |
4319 | | cryptoInfo.she.op.generateM4M5.uidSz = uidSz; |
4320 | | cryptoInfo.she.op.generateM4M5.authKeyId = authKeyId; |
4321 | | cryptoInfo.she.op.generateM4M5.targetKeyId = targetKeyId; |
4322 | | cryptoInfo.she.op.generateM4M5.newKey = newKey; |
4323 | | cryptoInfo.she.op.generateM4M5.newKeySz = newKeySz; |
4324 | | cryptoInfo.she.op.generateM4M5.counter = counter; |
4325 | | cryptoInfo.she.op.generateM4M5.m4 = m4; |
4326 | | cryptoInfo.she.op.generateM4M5.m4Sz = m4Sz; |
4327 | | cryptoInfo.she.op.generateM4M5.m5 = m5; |
4328 | | cryptoInfo.she.op.generateM4M5.m5Sz = m5Sz; |
4329 | | |
4330 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4331 | | } |
4332 | | |
4333 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4334 | | } |
4335 | | |
4336 | | int wc_CryptoCb_SheExportKey(wc_SHE* she, |
4337 | | byte* m1, word32 m1Sz, |
4338 | | byte* m2, word32 m2Sz, |
4339 | | byte* m3, word32 m3Sz, |
4340 | | byte* m4, word32 m4Sz, |
4341 | | byte* m5, word32 m5Sz, |
4342 | | const void* ctx) |
4343 | | { |
4344 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4345 | | CryptoCb* dev; |
4346 | | |
4347 | | if (she == NULL) { |
4348 | | return BAD_FUNC_ARG; |
4349 | | } |
4350 | | |
4351 | | dev = wc_CryptoCb_FindDevice(she->devId, WC_ALGO_TYPE_SHE); |
4352 | | if (dev && dev->cb) { |
4353 | | wc_CryptoInfo cryptoInfo; |
4354 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4355 | | cryptoInfo.algo_type = WC_ALGO_TYPE_SHE; |
4356 | | cryptoInfo.she.she = she; |
4357 | | cryptoInfo.she.type = WC_SHE_EXPORT_KEY; |
4358 | | cryptoInfo.she.ctx = ctx; |
4359 | | cryptoInfo.she.op.exportKey.m1 = m1; |
4360 | | cryptoInfo.she.op.exportKey.m1Sz = m1Sz; |
4361 | | cryptoInfo.she.op.exportKey.m2 = m2; |
4362 | | cryptoInfo.she.op.exportKey.m2Sz = m2Sz; |
4363 | | cryptoInfo.she.op.exportKey.m3 = m3; |
4364 | | cryptoInfo.she.op.exportKey.m3Sz = m3Sz; |
4365 | | cryptoInfo.she.op.exportKey.m4 = m4; |
4366 | | cryptoInfo.she.op.exportKey.m4Sz = m4Sz; |
4367 | | cryptoInfo.she.op.exportKey.m5 = m5; |
4368 | | cryptoInfo.she.op.exportKey.m5Sz = m5Sz; |
4369 | | |
4370 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4371 | | } |
4372 | | |
4373 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4374 | | } |
4375 | | #endif /* WOLFSSL_SHE */ |
4376 | | |
4377 | | /* returns the default dev id for the current build */ |
4378 | | int wc_CryptoCb_DefaultDevID(void) |
4379 | 73.4k | { |
4380 | 73.4k | int ret; |
4381 | | |
4382 | | /* Explicitly disable the "default devId" behavior. Ensures that any devId |
4383 | | * will only be used if explicitly passed as an argument to crypto functions, |
4384 | | * and never automatically selected. */ |
4385 | | #ifdef WC_NO_DEFAULT_DEVID |
4386 | | ret = INVALID_DEVID; |
4387 | | #else |
4388 | | /* conditional macro selection based on build */ |
4389 | | #ifdef WOLFSSL_CAAM_DEVID |
4390 | | ret = WOLFSSL_CAAM_DEVID; |
4391 | | #elif defined(HAVE_ARIA) |
4392 | | ret = WOLFSSL_ARIA_DEVID; |
4393 | | #elif defined(WC_USE_DEVID) |
4394 | | ret = WC_USE_DEVID; |
4395 | | #else |
4396 | | /* try first available */ |
4397 | 73.4k | ret = wc_CryptoCb_GetDevIdAtIndex(0); |
4398 | 73.4k | #endif |
4399 | 73.4k | #endif /* WC_NO_DEFAULT_DEVID */ |
4400 | | |
4401 | 73.4k | return ret; |
4402 | 73.4k | } |
4403 | | |
4404 | | #if (defined(HAVE_PBKDF2) && !defined(NO_HMAC) && !defined(NO_PWDBASED)) |
4405 | | int wc_CryptoCb_Pbkdf2(byte* output, const byte* passwd, int pLen, |
4406 | | const byte* salt, int sLen, int iterations, int kLen, int hashType, |
4407 | | int devId) |
4408 | 0 | { |
4409 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4410 | 0 | CryptoCb* dev; |
4411 | | |
4412 | | /* Find registered callback device */ |
4413 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KDF); |
4414 | |
|
4415 | 0 | if (dev && dev->cb) { |
4416 | 0 | wc_CryptoInfo cryptoInfo; |
4417 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4418 | |
|
4419 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_KDF; |
4420 | 0 | cryptoInfo.kdf.type = WC_KDF_TYPE_PBKDF2; |
4421 | 0 | cryptoInfo.kdf.pbkdf2.output = output; |
4422 | 0 | cryptoInfo.kdf.pbkdf2.passwd = passwd; |
4423 | 0 | cryptoInfo.kdf.pbkdf2.pLen = pLen; |
4424 | 0 | cryptoInfo.kdf.pbkdf2.salt = salt; |
4425 | 0 | cryptoInfo.kdf.pbkdf2.sLen = sLen; |
4426 | 0 | cryptoInfo.kdf.pbkdf2.iterations = iterations; |
4427 | 0 | cryptoInfo.kdf.pbkdf2.kLen = kLen; |
4428 | 0 | cryptoInfo.kdf.pbkdf2.hashType = hashType; |
4429 | |
|
4430 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4431 | 0 | } |
4432 | |
|
4433 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
4434 | 0 | } |
4435 | | #endif /* HAVE_PBKDF2 && !NO_HMAC && !NO_PWDBASED */ |
4436 | | |
4437 | | #if defined(HAVE_HKDF) && !defined(NO_HMAC) |
4438 | | int wc_CryptoCb_Hkdf(int hashType, const byte* inKey, word32 inKeySz, |
4439 | | const byte* salt, word32 saltSz, const byte* info, |
4440 | | word32 infoSz, byte* out, word32 outSz, int devId) |
4441 | 0 | { |
4442 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4443 | 0 | CryptoCb* dev; |
4444 | | |
4445 | | /* Find registered callback device */ |
4446 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KDF); |
4447 | |
|
4448 | 0 | if (dev && dev->cb) { |
4449 | 0 | wc_CryptoInfo cryptoInfo; |
4450 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4451 | |
|
4452 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_KDF; |
4453 | 0 | cryptoInfo.kdf.type = WC_KDF_TYPE_HKDF; |
4454 | 0 | cryptoInfo.kdf.hkdf.hashType = hashType; |
4455 | 0 | cryptoInfo.kdf.hkdf.inKey = inKey; |
4456 | 0 | cryptoInfo.kdf.hkdf.inKeySz = inKeySz; |
4457 | 0 | cryptoInfo.kdf.hkdf.salt = salt; |
4458 | 0 | cryptoInfo.kdf.hkdf.saltSz = saltSz; |
4459 | 0 | cryptoInfo.kdf.hkdf.info = info; |
4460 | 0 | cryptoInfo.kdf.hkdf.infoSz = infoSz; |
4461 | 0 | cryptoInfo.kdf.hkdf.out = out; |
4462 | 0 | cryptoInfo.kdf.hkdf.outSz = outSz; |
4463 | |
|
4464 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4465 | 0 | } |
4466 | |
|
4467 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
4468 | 0 | } |
4469 | | |
4470 | | /* NOTE: size of 'out' must be the digest size per hashType */ |
4471 | | int wc_CryptoCb_Hkdf_Extract(int hashType, const byte* salt, word32 saltSz, |
4472 | | const byte* inKey, word32 inKeySz, byte* out, int devId) |
4473 | 0 | { |
4474 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4475 | 0 | CryptoCb* dev; |
4476 | | |
4477 | | /* Find registered callback device */ |
4478 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KDF); |
4479 | |
|
4480 | 0 | if (dev && dev->cb) { |
4481 | 0 | wc_CryptoInfo cryptoInfo; |
4482 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4483 | |
|
4484 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_KDF; |
4485 | 0 | cryptoInfo.kdf.type = WC_KDF_TYPE_HKDF_EXTRACT; |
4486 | 0 | cryptoInfo.kdf.hkdf_extract.hashType = hashType; |
4487 | 0 | cryptoInfo.kdf.hkdf_extract.salt = salt; |
4488 | 0 | cryptoInfo.kdf.hkdf_extract.saltSz = saltSz; |
4489 | 0 | cryptoInfo.kdf.hkdf_extract.inKey = inKey; |
4490 | 0 | cryptoInfo.kdf.hkdf_extract.inKeySz = inKeySz; |
4491 | 0 | cryptoInfo.kdf.hkdf_extract.out = out; |
4492 | |
|
4493 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4494 | 0 | } |
4495 | |
|
4496 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
4497 | 0 | } |
4498 | | |
4499 | | int wc_CryptoCb_Hkdf_Expand(int hashType, const byte* inKey, word32 inKeySz, |
4500 | | const byte* info, word32 infoSz, byte* out, word32 outSz, |
4501 | | int devId) |
4502 | 0 | { |
4503 | 0 | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4504 | 0 | CryptoCb* dev; |
4505 | | |
4506 | | /* Find registered callback device */ |
4507 | 0 | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KDF); |
4508 | |
|
4509 | 0 | if (dev && dev->cb) { |
4510 | 0 | wc_CryptoInfo cryptoInfo; |
4511 | 0 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4512 | |
|
4513 | 0 | cryptoInfo.algo_type = WC_ALGO_TYPE_KDF; |
4514 | 0 | cryptoInfo.kdf.type = WC_KDF_TYPE_HKDF_EXPAND; |
4515 | 0 | cryptoInfo.kdf.hkdf_expand.hashType = hashType; |
4516 | 0 | cryptoInfo.kdf.hkdf_expand.inKey = inKey; |
4517 | 0 | cryptoInfo.kdf.hkdf_expand.inKeySz = inKeySz; |
4518 | 0 | cryptoInfo.kdf.hkdf_expand.info = info; |
4519 | 0 | cryptoInfo.kdf.hkdf_expand.infoSz = infoSz; |
4520 | 0 | cryptoInfo.kdf.hkdf_expand.out = out; |
4521 | 0 | cryptoInfo.kdf.hkdf_expand.outSz = outSz; |
4522 | |
|
4523 | 0 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4524 | 0 | } |
4525 | |
|
4526 | 0 | return wc_CryptoCb_TranslateErrorCode(ret); |
4527 | 0 | } |
4528 | | #endif /* HAVE_HKDF && !NO_HMAC */ |
4529 | | |
4530 | | #ifdef WOLF_CRYPTO_CB_COPY |
4531 | | /* General copy callback function for algorithm structures |
4532 | | * devId: The device ID to use for the callback |
4533 | | * algo: Algorithm type (enum wc_AlgoType) - WC_ALGO_TYPE_HASH, |
4534 | | * WC_ALGO_TYPE_CIPHER, etc |
4535 | | * type: Specific type - for HASH: enum wc_HashType, for CIPHER: |
4536 | | * enum wc_CipherType |
4537 | | * src: Pointer to source structure |
4538 | | * dst: Pointer to destination structure |
4539 | | * Returns: 0 on success, negative on error, CRYPTOCB_UNAVAILABLE if not |
4540 | | * handled |
4541 | | */ |
4542 | | int wc_CryptoCb_Copy(int devId, int algo, int type, void* src, void* dst) |
4543 | | { |
4544 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4545 | | CryptoCb* dev; |
4546 | | |
4547 | | /* Find registered callback device */ |
4548 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_COPY); |
4549 | | if (dev && dev->cb) { |
4550 | | wc_CryptoInfo cryptoInfo; |
4551 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4552 | | cryptoInfo.algo_type = WC_ALGO_TYPE_COPY; |
4553 | | cryptoInfo.copy.algo = algo; |
4554 | | cryptoInfo.copy.type = type; |
4555 | | cryptoInfo.copy.src = src; |
4556 | | cryptoInfo.copy.dst = dst; |
4557 | | |
4558 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4559 | | } |
4560 | | |
4561 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4562 | | } |
4563 | | #endif /* WOLF_CRYPTO_CB_COPY */ |
4564 | | |
4565 | | #ifdef WOLF_CRYPTO_CB_FREE |
4566 | | /* General free callback function for algorithm structures |
4567 | | * devId: The device ID to use for the callback |
4568 | | * algo: Algorithm type (enum wc_AlgoType) - WC_ALGO_TYPE_HASH, |
4569 | | * WC_ALGO_TYPE_CIPHER, etc |
4570 | | * type: Specific type - for HASH: enum wc_HashType, for CIPHER: |
4571 | | * enum wc_CipherType |
4572 | | * subType: Specific subtype - for PQC: enum wc_PqcKemType, |
4573 | | * enum wc_PqcSignatureType |
4574 | | * obj: Pointer to object structure to free |
4575 | | * Returns: 0 on success, negative on error, CRYPTOCB_UNAVAILABLE if not |
4576 | | * handled |
4577 | | */ |
4578 | | int wc_CryptoCb_Free(int devId, int algo, int type, int subType, void* obj) |
4579 | | { |
4580 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4581 | | CryptoCb* dev; |
4582 | | |
4583 | | /* Find registered callback device */ |
4584 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_FREE); |
4585 | | if (dev && dev->cb) { |
4586 | | wc_CryptoInfo cryptoInfo; |
4587 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4588 | | cryptoInfo.algo_type = WC_ALGO_TYPE_FREE; |
4589 | | cryptoInfo.free.algo = algo; |
4590 | | cryptoInfo.free.type = type; |
4591 | | cryptoInfo.free.subType = subType; |
4592 | | cryptoInfo.free.obj = obj; |
4593 | | |
4594 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4595 | | } |
4596 | | |
4597 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4598 | | } |
4599 | | #endif /* WOLF_CRYPTO_CB_FREE */ |
4600 | | |
4601 | | #ifdef WOLF_CRYPTO_CB_SETKEY |
4602 | | /* Generic SetKey callback for importing keys into hardware. |
4603 | | * devId: Device ID for the registered callback |
4604 | | * type: enum wc_SetKeyType (AES, HMAC, RSA_PUB, RSA_PRIV, ECC_PUB, ECC_PRIV) |
4605 | | * obj: Context struct being operated on (Aes*, Hmac*, RsaKey*, ecc_key*) |
4606 | | * key: Key material: raw bytes (AES/HMAC) or temp struct to export from (RSA/ECC) |
4607 | | * keySz: Key size in bytes (0 when key is a struct pointer) |
4608 | | * aux: Auxiliary data (IV, etc.) or NULL |
4609 | | * auxSz: Aux data size, 0 if unused |
4610 | | * flags: AES: direction (AES_ENCRYPTION/DECRYPTION). Others: 0 |
4611 | | * Returns: 0 on success, CRYPTOCB_UNAVAILABLE if not handled, negative on error |
4612 | | */ |
4613 | | int wc_CryptoCb_SetKey(int devId, int type, void* obj, |
4614 | | void* key, word32 keySz, |
4615 | | void* aux, word32 auxSz, |
4616 | | int flags) |
4617 | | { |
4618 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4619 | | CryptoCb* dev; |
4620 | | |
4621 | | /* Find registered callback device */ |
4622 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_SETKEY); |
4623 | | if (dev && dev->cb) { |
4624 | | wc_CryptoInfo cryptoInfo; |
4625 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4626 | | cryptoInfo.algo_type = WC_ALGO_TYPE_SETKEY; |
4627 | | cryptoInfo.setkey.type = type; |
4628 | | cryptoInfo.setkey.obj = obj; |
4629 | | cryptoInfo.setkey.key = key; |
4630 | | cryptoInfo.setkey.keySz = keySz; |
4631 | | cryptoInfo.setkey.aux = aux; |
4632 | | cryptoInfo.setkey.auxSz = auxSz; |
4633 | | cryptoInfo.setkey.flags = flags; |
4634 | | |
4635 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4636 | | } |
4637 | | |
4638 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4639 | | } |
4640 | | #endif /* WOLF_CRYPTO_CB_SETKEY */ |
4641 | | |
4642 | | #ifdef WOLF_CRYPTO_CB_EXPORT_KEY |
4643 | | /* Generic ExportKey callback for exporting key material from hardware. |
4644 | | * devId: Device ID for the registered callback |
4645 | | * type: enum wc_PkType (WC_PK_TYPE_RSA, WC_PK_TYPE_ECDSA, etc.) |
4646 | | * obj: Hardware key object (has devCtx, id[], etc.) |
4647 | | * out: Software key object to fill (same type as obj, caller-allocated) |
4648 | | * The callback exports from hardware into the software key. The caller then |
4649 | | * uses normal software export functions on 'out' and frees it. |
4650 | | * Returns: 0 on success, CRYPTOCB_UNAVAILABLE if not handled, negative on error |
4651 | | */ |
4652 | | int wc_CryptoCb_ExportKey(int devId, int type, const void* obj, void* out) |
4653 | | { |
4654 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4655 | | CryptoCb* dev; |
4656 | | |
4657 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_EXPORT_KEY); |
4658 | | if (dev && dev->cb) { |
4659 | | wc_CryptoInfo cryptoInfo; |
4660 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4661 | | cryptoInfo.algo_type = WC_ALGO_TYPE_EXPORT_KEY; |
4662 | | cryptoInfo.export_key.type = type; |
4663 | | cryptoInfo.export_key.obj = obj; |
4664 | | cryptoInfo.export_key.out = out; |
4665 | | |
4666 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4667 | | } |
4668 | | |
4669 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4670 | | } |
4671 | | #endif /* WOLF_CRYPTO_CB_EXPORT_KEY */ |
4672 | | |
4673 | | #ifdef WOLF_CRYPTO_CB_KEYSTORE |
4674 | | /* Hardware key store operations. Key references are opaque to wolfCrypt: it |
4675 | | * copies the pointers through and never interprets them, exactly as it treats |
4676 | | * the id[] blob on a key object. */ |
4677 | | int wc_CryptoCb_KeyStoreImportPlain(int devId, |
4678 | | const byte* keyRef, word32 keyRefSz, |
4679 | | word32 keyType, const byte* key, word32 keySz, |
4680 | | word32 attrs, const void* ctx) |
4681 | | { |
4682 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4683 | | CryptoCb* dev; |
4684 | | |
4685 | | if (keyRef == NULL || keyRefSz == 0 || key == NULL || keySz == 0) { |
4686 | | return BAD_FUNC_ARG; |
4687 | | } |
4688 | | |
4689 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KEYSTORE); |
4690 | | if (dev && dev->cb) { |
4691 | | wc_CryptoInfo cryptoInfo; |
4692 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4693 | | cryptoInfo.algo_type = WC_ALGO_TYPE_KEYSTORE; |
4694 | | cryptoInfo.keystore.type = WC_KEYSTORE_IMPORT_PLAIN; |
4695 | | cryptoInfo.keystore.ctx = ctx; |
4696 | | cryptoInfo.keystore.op.importPlain.keyRef = keyRef; |
4697 | | cryptoInfo.keystore.op.importPlain.keyRefSz = keyRefSz; |
4698 | | cryptoInfo.keystore.op.importPlain.keyType = keyType; |
4699 | | cryptoInfo.keystore.op.importPlain.key = key; |
4700 | | cryptoInfo.keystore.op.importPlain.keySz = keySz; |
4701 | | cryptoInfo.keystore.op.importPlain.attrs = attrs; |
4702 | | |
4703 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4704 | | } |
4705 | | |
4706 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4707 | | } |
4708 | | |
4709 | | int wc_CryptoCb_KeyStoreExportPlain(int devId, |
4710 | | const byte* keyRef, word32 keyRefSz, |
4711 | | byte* key, word32* keySz, const void* ctx) |
4712 | | { |
4713 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4714 | | CryptoCb* dev; |
4715 | | |
4716 | | /* key == NULL is the required-size query. Tie buffer and capacity together |
4717 | | * so a non-NULL buffer cannot arrive with an uninitialised capacity. */ |
4718 | | if (keyRef == NULL || keyRefSz == 0 || keySz == NULL) { |
4719 | | return BAD_FUNC_ARG; |
4720 | | } |
4721 | | if (key != NULL && *keySz == 0) { |
4722 | | return BAD_FUNC_ARG; |
4723 | | } |
4724 | | |
4725 | | /* On the query form it is a pure output, so clear it as getInfo does; on |
4726 | | * the buffer form it carries the capacity in and must be left alone. */ |
4727 | | if (key == NULL) { |
4728 | | *keySz = 0; |
4729 | | } |
4730 | | |
4731 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KEYSTORE); |
4732 | | if (dev && dev->cb) { |
4733 | | wc_CryptoInfo cryptoInfo; |
4734 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4735 | | cryptoInfo.algo_type = WC_ALGO_TYPE_KEYSTORE; |
4736 | | cryptoInfo.keystore.type = WC_KEYSTORE_EXPORT_PLAIN; |
4737 | | cryptoInfo.keystore.ctx = ctx; |
4738 | | cryptoInfo.keystore.op.exportPlain.keyRef = keyRef; |
4739 | | cryptoInfo.keystore.op.exportPlain.keyRefSz = keyRefSz; |
4740 | | cryptoInfo.keystore.op.exportPlain.key = key; |
4741 | | cryptoInfo.keystore.op.exportPlain.keySz = keySz; |
4742 | | |
4743 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4744 | | } |
4745 | | |
4746 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4747 | | } |
4748 | | |
4749 | | int wc_CryptoCb_KeyStoreImportWrapped(int devId, |
4750 | | const byte* keyRef, word32 keyRefSz, word32 keyType, |
4751 | | const byte* wrapKeyRef, word32 wrapKeyRefSz, |
4752 | | word32 format, const byte* blob, word32 blobSz, |
4753 | | word32 attrs, const void* ctx) |
4754 | | { |
4755 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4756 | | CryptoCb* dev; |
4757 | | |
4758 | | if (keyRef == NULL || keyRefSz == 0 || blob == NULL || blobSz == 0) { |
4759 | | return BAD_FUNC_ARG; |
4760 | | } |
4761 | | if (wrapKeyRef == NULL && wrapKeyRefSz != 0) { |
4762 | | return BAD_FUNC_ARG; |
4763 | | } |
4764 | | |
4765 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KEYSTORE); |
4766 | | if (dev && dev->cb) { |
4767 | | wc_CryptoInfo cryptoInfo; |
4768 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4769 | | cryptoInfo.algo_type = WC_ALGO_TYPE_KEYSTORE; |
4770 | | cryptoInfo.keystore.type = WC_KEYSTORE_IMPORT_WRAPPED; |
4771 | | cryptoInfo.keystore.ctx = ctx; |
4772 | | cryptoInfo.keystore.op.importWrapped.keyRef = keyRef; |
4773 | | cryptoInfo.keystore.op.importWrapped.keyRefSz = keyRefSz; |
4774 | | cryptoInfo.keystore.op.importWrapped.keyType = keyType; |
4775 | | cryptoInfo.keystore.op.importWrapped.wrapKeyRef = wrapKeyRef; |
4776 | | cryptoInfo.keystore.op.importWrapped.wrapKeyRefSz = wrapKeyRefSz; |
4777 | | cryptoInfo.keystore.op.importWrapped.blob = blob; |
4778 | | cryptoInfo.keystore.op.importWrapped.blobSz = blobSz; |
4779 | | cryptoInfo.keystore.op.importWrapped.format = format; |
4780 | | cryptoInfo.keystore.op.importWrapped.attrs = attrs; |
4781 | | |
4782 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4783 | | } |
4784 | | |
4785 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4786 | | } |
4787 | | |
4788 | | int wc_CryptoCb_KeyStoreExportWrapped(int devId, |
4789 | | const byte* keyRef, word32 keyRefSz, |
4790 | | const byte* wrapKeyRef, word32 wrapKeyRefSz, |
4791 | | word32 format, byte* blob, word32* blobSz, const void* ctx) |
4792 | | { |
4793 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4794 | | CryptoCb* dev; |
4795 | | |
4796 | | /* blob == NULL is the required-size query. Tie buffer and capacity together |
4797 | | * so a non-NULL buffer cannot arrive with an uninitialised capacity. */ |
4798 | | if (keyRef == NULL || keyRefSz == 0 || blobSz == NULL) { |
4799 | | return BAD_FUNC_ARG; |
4800 | | } |
4801 | | if (blob != NULL && *blobSz == 0) { |
4802 | | return BAD_FUNC_ARG; |
4803 | | } |
4804 | | if (wrapKeyRef == NULL && wrapKeyRefSz != 0) { |
4805 | | return BAD_FUNC_ARG; |
4806 | | } |
4807 | | |
4808 | | /* Cleared only on the query form; see wc_CryptoCb_KeyStoreExportPlain. */ |
4809 | | if (blob == NULL) { |
4810 | | *blobSz = 0; |
4811 | | } |
4812 | | |
4813 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KEYSTORE); |
4814 | | if (dev && dev->cb) { |
4815 | | wc_CryptoInfo cryptoInfo; |
4816 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4817 | | cryptoInfo.algo_type = WC_ALGO_TYPE_KEYSTORE; |
4818 | | cryptoInfo.keystore.type = WC_KEYSTORE_EXPORT_WRAPPED; |
4819 | | cryptoInfo.keystore.ctx = ctx; |
4820 | | cryptoInfo.keystore.op.exportWrapped.keyRef = keyRef; |
4821 | | cryptoInfo.keystore.op.exportWrapped.keyRefSz = keyRefSz; |
4822 | | cryptoInfo.keystore.op.exportWrapped.wrapKeyRef = wrapKeyRef; |
4823 | | cryptoInfo.keystore.op.exportWrapped.wrapKeyRefSz = wrapKeyRefSz; |
4824 | | cryptoInfo.keystore.op.exportWrapped.blob = blob; |
4825 | | cryptoInfo.keystore.op.exportWrapped.blobSz = blobSz; |
4826 | | cryptoInfo.keystore.op.exportWrapped.format = format; |
4827 | | |
4828 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4829 | | } |
4830 | | |
4831 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4832 | | } |
4833 | | |
4834 | | int wc_CryptoCb_KeyStoreDerive(int devId, |
4835 | | const byte* keyRef, word32 keyRefSz, word32 keyType, word32 keySz, |
4836 | | const byte* srcKeyRef, word32 srcKeyRefSz, |
4837 | | word32 kdfType, const byte* deriv, word32 derivSz, |
4838 | | word32 attrs, const void* ctx) |
4839 | | { |
4840 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4841 | | CryptoCb* dev; |
4842 | | |
4843 | | if (keyRef == NULL || keyRefSz == 0 || |
4844 | | srcKeyRef == NULL || srcKeyRefSz == 0) { |
4845 | | return BAD_FUNC_ARG; |
4846 | | } |
4847 | | if (deriv == NULL && derivSz != 0) { |
4848 | | return BAD_FUNC_ARG; |
4849 | | } |
4850 | | |
4851 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KEYSTORE); |
4852 | | if (dev && dev->cb) { |
4853 | | wc_CryptoInfo cryptoInfo; |
4854 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4855 | | cryptoInfo.algo_type = WC_ALGO_TYPE_KEYSTORE; |
4856 | | cryptoInfo.keystore.type = WC_KEYSTORE_DERIVE; |
4857 | | cryptoInfo.keystore.ctx = ctx; |
4858 | | cryptoInfo.keystore.op.derive.keyRef = keyRef; |
4859 | | cryptoInfo.keystore.op.derive.keyRefSz = keyRefSz; |
4860 | | cryptoInfo.keystore.op.derive.keyType = keyType; |
4861 | | cryptoInfo.keystore.op.derive.keySz = keySz; |
4862 | | cryptoInfo.keystore.op.derive.srcKeyRef = srcKeyRef; |
4863 | | cryptoInfo.keystore.op.derive.srcKeyRefSz = srcKeyRefSz; |
4864 | | cryptoInfo.keystore.op.derive.deriv = deriv; |
4865 | | cryptoInfo.keystore.op.derive.derivSz = derivSz; |
4866 | | cryptoInfo.keystore.op.derive.kdfType = kdfType; |
4867 | | cryptoInfo.keystore.op.derive.attrs = attrs; |
4868 | | |
4869 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4870 | | } |
4871 | | |
4872 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4873 | | } |
4874 | | |
4875 | | int wc_CryptoCb_KeyStoreDelete(int devId, const byte* keyRef, word32 keyRefSz, |
4876 | | const void* ctx) |
4877 | | { |
4878 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4879 | | CryptoCb* dev; |
4880 | | |
4881 | | if (keyRef == NULL || keyRefSz == 0) { |
4882 | | return BAD_FUNC_ARG; |
4883 | | } |
4884 | | |
4885 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KEYSTORE); |
4886 | | if (dev && dev->cb) { |
4887 | | wc_CryptoInfo cryptoInfo; |
4888 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4889 | | cryptoInfo.algo_type = WC_ALGO_TYPE_KEYSTORE; |
4890 | | cryptoInfo.keystore.type = WC_KEYSTORE_DELETE; |
4891 | | cryptoInfo.keystore.ctx = ctx; |
4892 | | cryptoInfo.keystore.op.deleteKey.keyRef = keyRef; |
4893 | | cryptoInfo.keystore.op.deleteKey.keyRefSz = keyRefSz; |
4894 | | |
4895 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4896 | | } |
4897 | | |
4898 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4899 | | } |
4900 | | |
4901 | | int wc_CryptoCb_KeyStoreGetInfo(int devId, const byte* keyRef, word32 keyRefSz, |
4902 | | word32* keyType, word32* keyBits, word32* attrs, const void* ctx) |
4903 | | { |
4904 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4905 | | CryptoCb* dev; |
4906 | | |
4907 | | if (keyRef == NULL || keyRefSz == 0) { |
4908 | | return BAD_FUNC_ARG; |
4909 | | } |
4910 | | |
4911 | | /* Clear the caller's storage first: a device may answer only part of the |
4912 | | * query, and attrs & WC_KEYSTORE_ATTR_EXPORTABLE is a security decision. */ |
4913 | | if (keyType != NULL) { |
4914 | | *keyType = 0; |
4915 | | } |
4916 | | if (keyBits != NULL) { |
4917 | | *keyBits = 0; |
4918 | | } |
4919 | | if (attrs != NULL) { |
4920 | | *attrs = 0; |
4921 | | } |
4922 | | |
4923 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KEYSTORE); |
4924 | | if (dev && dev->cb) { |
4925 | | wc_CryptoInfo cryptoInfo; |
4926 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4927 | | cryptoInfo.algo_type = WC_ALGO_TYPE_KEYSTORE; |
4928 | | cryptoInfo.keystore.type = WC_KEYSTORE_GET_INFO; |
4929 | | cryptoInfo.keystore.ctx = ctx; |
4930 | | cryptoInfo.keystore.op.getInfo.keyRef = keyRef; |
4931 | | cryptoInfo.keystore.op.getInfo.keyRefSz = keyRefSz; |
4932 | | cryptoInfo.keystore.op.getInfo.keyType = keyType; |
4933 | | cryptoInfo.keystore.op.getInfo.keyBits = keyBits; |
4934 | | cryptoInfo.keystore.op.getInfo.attrs = attrs; |
4935 | | |
4936 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4937 | | } |
4938 | | |
4939 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4940 | | } |
4941 | | #endif /* WOLF_CRYPTO_CB_KEYSTORE */ |
4942 | | |
4943 | | #if defined(HAVE_CMAC_KDF) |
4944 | | /* Crypto callback for NIST SP 800 56C two-step CMAC KDF. See software |
4945 | | * implementation in wc_KDA_KDF_twostep_cmac for more comments. |
4946 | | * */ |
4947 | | int wc_CryptoCb_Kdf_TwostepCmac(const byte * salt, word32 saltSz, |
4948 | | const byte* z, word32 zSz, |
4949 | | const byte* fixedInfo, word32 fixedInfoSz, |
4950 | | byte* output, word32 outputSz, int devId) |
4951 | | { |
4952 | | int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE); |
4953 | | CryptoCb* dev; |
4954 | | |
4955 | | /* Find registered callback device */ |
4956 | | dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KDF); |
4957 | | |
4958 | | if (dev && dev->cb) { |
4959 | | wc_CryptoInfo cryptoInfo; |
4960 | | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
4961 | | |
4962 | | cryptoInfo.algo_type = WC_ALGO_TYPE_KDF; |
4963 | | cryptoInfo.kdf.type = WC_KDF_TYPE_TWOSTEP_CMAC; |
4964 | | cryptoInfo.kdf.twostep_cmac.salt = salt; |
4965 | | cryptoInfo.kdf.twostep_cmac.saltSz = saltSz; |
4966 | | cryptoInfo.kdf.twostep_cmac.z = z; |
4967 | | cryptoInfo.kdf.twostep_cmac.zSz = zSz; |
4968 | | cryptoInfo.kdf.twostep_cmac.fixedInfo = fixedInfo; |
4969 | | cryptoInfo.kdf.twostep_cmac.fixedInfoSz = fixedInfoSz; |
4970 | | cryptoInfo.kdf.twostep_cmac.out = output; |
4971 | | cryptoInfo.kdf.twostep_cmac.outSz = outputSz; |
4972 | | |
4973 | | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
4974 | | } |
4975 | | |
4976 | | return wc_CryptoCb_TranslateErrorCode(ret); |
4977 | | } |
4978 | | #endif /* HAVE_CMAC_KDF */ |
4979 | | |
4980 | | #endif /* WOLF_CRYPTO_CB */ |