Coverage Report

Created: 2026-02-14 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wolfssl-normal-math/wolfcrypt/src/cryptocb.c
Line
Count
Source
1
/* cryptocb.c
2
 *
3
 * Copyright (C) 2006-2025 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
/* Some common, optional build settings:
26
 * these can also be set in wolfssl/options.h or user_settings.h
27
 * -------------------------------------------------------------
28
 * enable the find device callback functions
29
 * WOLF_CRYPTO_CB_FIND
30
 *
31
 * enable the command callback functions to invoke the callback during
32
 * register and unregister
33
 * WOLF_CRYPTO_CB_CMD
34
 *
35
 * enable debug InfoString functions
36
 * DEBUG_CRYPTOCB
37
 */
38
39
#include <wolfssl/wolfcrypt/libwolfssl_sources.h>
40
41
#ifdef WOLF_CRYPTO_CB
42
43
#include <wolfssl/wolfcrypt/cryptocb.h>
44
45
#ifdef HAVE_ARIA
46
    #include <wolfssl/wolfcrypt/port/aria/aria-cryptocb.h>
47
#endif
48
49
#ifdef WOLFSSL_CAAM
50
    #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
51
#endif
52
/* TODO: Consider linked list with mutex */
53
#ifndef MAX_CRYPTO_DEVID_CALLBACKS
54
868k
#define MAX_CRYPTO_DEVID_CALLBACKS 8
55
#endif
56
57
typedef struct CryptoCb {
58
    int devId;
59
    CryptoDevCallbackFunc cb;
60
    void* ctx;
61
} CryptoCb;
62
static WC_THREADSHARED CryptoCb gCryptoDev[MAX_CRYPTO_DEVID_CALLBACKS];
63
64
#ifdef WOLF_CRYPTO_CB_FIND
65
static CryptoDevCallbackFind CryptoCb_FindCb = NULL;
66
#endif
67
68
#ifdef DEBUG_CRYPTOCB
69
static const char* GetAlgoTypeStr(int algo)
70
{
71
    switch (algo) { /* enum wc_AlgoType */
72
#ifdef WOLF_CRYPTO_CB_CMD
73
        case WC_ALGO_TYPE_NONE:   return "None-Command";
74
#endif
75
        case WC_ALGO_TYPE_HASH:   return "Hash";
76
        case WC_ALGO_TYPE_CIPHER: return "Cipher";
77
        case WC_ALGO_TYPE_PK:     return "PK";
78
        case WC_ALGO_TYPE_RNG:    return "RNG";
79
        case WC_ALGO_TYPE_SEED:   return "Seed";
80
        case WC_ALGO_TYPE_HMAC:   return "HMAC";
81
        case WC_ALGO_TYPE_CMAC:   return "CMAC";
82
        case WC_ALGO_TYPE_CERT:   return "Cert";
83
        case WC_ALGO_TYPE_KDF:    return "KDF";
84
#ifdef WOLF_CRYPTO_CB_COPY
85
        case WC_ALGO_TYPE_COPY:   return "Copy";
86
#endif /* WOLF_CRYPTO_CB_COPY */
87
#ifdef WOLF_CRYPTO_CB_FREE
88
        case WC_ALGO_TYPE_FREE:   return "Free";
89
#endif /* WOLF_CRYPTO_CB_FREE */
90
    }
91
    return NULL;
92
}
93
static const char* GetPkTypeStr(int pk)
94
{
95
    switch (pk) {
96
        case WC_PK_TYPE_RSA: return "RSA";
97
        case WC_PK_TYPE_DH: return "DH";
98
        case WC_PK_TYPE_ECDH: return "ECDH";
99
        case WC_PK_TYPE_ECDSA_SIGN: return "ECDSA-Sign";
100
        case WC_PK_TYPE_ECDSA_VERIFY: return "ECDSA-Verify";
101
        case WC_PK_TYPE_ED25519_SIGN: return "ED25519-Sign";
102
        case WC_PK_TYPE_ED25519_VERIFY: return "ED25519-Verify";
103
        case WC_PK_TYPE_CURVE25519: return "CURVE25519";
104
        case WC_PK_TYPE_RSA_KEYGEN: return "RSA KeyGen";
105
        case WC_PK_TYPE_EC_KEYGEN: return "ECC KeyGen";
106
    }
107
    return NULL;
108
}
109
#if !defined(NO_AES) || !defined(NO_DES3)
110
static const char* GetCipherTypeStr(int cipher)
111
{
112
    switch (cipher) {
113
        case WC_CIPHER_AES: return "AES ECB";
114
        case WC_CIPHER_AES_CBC: return "AES CBC";
115
        case WC_CIPHER_AES_GCM: return "AES GCM";
116
        case WC_CIPHER_AES_CTR: return "AES CTR";
117
        case WC_CIPHER_AES_XTS: return "AES XTS";
118
        case WC_CIPHER_AES_CFB: return "AES CFB";
119
        case WC_CIPHER_DES3: return "DES3";
120
        case WC_CIPHER_DES: return "DES";
121
        case WC_CIPHER_CHACHA: return "ChaCha20";
122
    }
123
    return NULL;
124
}
125
#endif /* !NO_AES || !NO_DES3 */
126
static const char* GetHashTypeStr(int hash)
127
{
128
    switch (hash) {
129
        case WC_HASH_TYPE_MD2: return "MD2";
130
        case WC_HASH_TYPE_MD4: return "MD4";
131
        case WC_HASH_TYPE_MD5: return "MD5";
132
        case WC_HASH_TYPE_SHA: return "SHA-1";
133
        case WC_HASH_TYPE_SHA224: return "SHA-224";
134
        case WC_HASH_TYPE_SHA256: return "SHA-256";
135
        case WC_HASH_TYPE_SHA384: return "SHA-384";
136
        case WC_HASH_TYPE_SHA512: return "SHA-512";
137
        case WC_HASH_TYPE_MD5_SHA: return "MD5-SHA1";
138
        case WC_HASH_TYPE_SHA3_224: return "SHA3-224";
139
        case WC_HASH_TYPE_SHA3_256: return "SHA3-256";
140
        case WC_HASH_TYPE_SHA3_384: return "SHA3-384";
141
        case WC_HASH_TYPE_SHA3_512: return "SHA3-512";
142
        case WC_HASH_TYPE_BLAKE2B: return "Blake2B";
143
        case WC_HASH_TYPE_BLAKE2S: return "Blake2S";
144
    }
145
    return NULL;
146
}
147
148
#ifdef WOLFSSL_CMAC
149
static const char* GetCmacTypeStr(int type)
150
{
151
    switch (type) {
152
        case WC_CMAC_AES: return "AES";
153
    }
154
    return NULL;
155
}
156
#endif  /* WOLFSSL_CMAC */
157
158
#ifndef NO_RSA
159
static const char* GetRsaType(int type)
160
{
161
    switch (type) {
162
        case RSA_PUBLIC_ENCRYPT:  return "Public Encrypt";
163
        case RSA_PUBLIC_DECRYPT:  return "Public Decrypt";
164
        case RSA_PRIVATE_ENCRYPT: return "Private Encrypt";
165
        case RSA_PRIVATE_DECRYPT: return "Private Decrypt";
166
    }
167
    return NULL;
168
}
169
#endif
170
171
#ifdef WOLF_CRYPTO_CB_CMD
172
static const char* GetCryptoCbCmdTypeStr(int type)
173
{
174
    switch (type) {
175
        case WC_CRYPTOCB_CMD_TYPE_REGISTER:   return "Register";
176
        case WC_CRYPTOCB_CMD_TYPE_UNREGISTER: return "UnRegister";
177
    }
178
    return NULL;
179
}
180
#endif
181
182
183
#if (defined(HAVE_HKDF) && !defined(NO_HMAC)) || defined(HAVE_CMAC_KDF)
184
static const char* GetKdfTypeStr(int type)
185
{
186
    switch (type) {
187
        case WC_KDF_TYPE_HKDF:
188
            return "HKDF";
189
        case WC_KDF_TYPE_TWOSTEP_CMAC:
190
            return "TWOSTEP_CMAC";
191
    }
192
    return NULL;
193
}
194
#endif
195
196
void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
197
{
198
    if (info == NULL)
199
        return;
200
201
    if (info->algo_type == WC_ALGO_TYPE_PK) {
202
    #ifndef NO_RSA
203
        if (info->pk.type == WC_PK_TYPE_RSA) {
204
            printf("Crypto CB: %s %s (%d), %s, Len %d\n",
205
                GetAlgoTypeStr(info->algo_type),
206
                GetPkTypeStr(info->pk.type), info->pk.type,
207
                GetRsaType(info->pk.rsa.type), info->pk.rsa.inLen);
208
        }
209
        else
210
    #endif
211
        {
212
            printf("Crypto CB: %s %s (%d)\n",
213
                GetAlgoTypeStr(info->algo_type),
214
                GetPkTypeStr(info->pk.type), info->pk.type);
215
        }
216
    }
217
#if !defined(NO_AES) || !defined(NO_DES3)
218
    else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
219
        printf("Crypto CB: %s %s (%d) (%p ctx)\n",
220
            GetAlgoTypeStr(info->algo_type),
221
            GetCipherTypeStr(info->cipher.type),
222
            info->cipher.type, info->cipher.ctx);
223
    }
224
#endif /* !NO_AES || !NO_DES3 */
225
#if !defined(NO_SHA) || !defined(NO_SHA256) || \
226
    defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA3)
227
    else if (info->algo_type == WC_ALGO_TYPE_HASH) {
228
        printf("Crypto CB: %s %s (%d) (%p ctx) %s\n",
229
            GetAlgoTypeStr(info->algo_type),
230
            GetHashTypeStr(info->hash.type),
231
            info->hash.type, info->hash.ctx,
232
            (info->hash.in != NULL) ? "Update" : "Final");
233
    }
234
#endif
235
#ifndef NO_HMAC
236
    else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
237
        printf("Crypto CB: %s %s (%d) (%p ctx) %s\n",
238
            GetAlgoTypeStr(info->algo_type),
239
            GetHashTypeStr(info->hmac.macType),
240
            info->hmac.macType, info->hmac.hmac,
241
            (info->hmac.in != NULL) ? "Update" : "Final");
242
    }
243
#endif
244
#ifdef WOLFSSL_CMAC
245
    else if (info->algo_type == WC_ALGO_TYPE_CMAC) {
246
        printf("Crypto CB: %s %s (%d) (%p ctx) %s %s %s\n",
247
            GetAlgoTypeStr(info->algo_type),
248
            GetCmacTypeStr(info->cmac.type),
249
            info->cmac.type, info->cmac.cmac,
250
            (info->cmac.key != NULL) ? "Init " : "",
251
            (info->cmac.in != NULL) ? "Update " : "",
252
            (info->cmac.out != NULL) ? "Final" : "");
253
    }
254
#endif
255
#ifdef WOLF_CRYPTO_CB_CMD
256
    else if (info->algo_type == WC_ALGO_TYPE_NONE) {
257
        printf("Crypto CB: %s %s (%d)\n",
258
            GetAlgoTypeStr(info->algo_type),
259
            GetCryptoCbCmdTypeStr(info->cmd.type), info->cmd.type);
260
    }
261
#endif
262
#ifdef WOLF_CRYPTO_CB_COPY
263
    else if (info->algo_type == WC_ALGO_TYPE_COPY) {
264
        printf("Crypto CB: %s %s Type=%d\n",
265
            GetAlgoTypeStr(info->algo_type),
266
            GetAlgoTypeStr(info->copy.algo), info->copy.type);
267
    }
268
#endif /* WOLF_CRYPTO_CB_COPY */
269
#ifdef WOLF_CRYPTO_CB_FREE
270
    else if (info->algo_type == WC_ALGO_TYPE_FREE) {
271
        printf("Crypto CB: %s %s Type=%d\n",
272
            GetAlgoTypeStr(info->algo_type),
273
            GetAlgoTypeStr(info->free.algo), info->free.type);
274
    }
275
#endif /* WOLF_CRYPTO_CB_FREE */
276
#if (defined(HAVE_HKDF) && !defined(NO_HMAC)) || \
277
    defined(HAVE_CMAC_KDF)
278
    else if (info->algo_type == WC_ALGO_TYPE_KDF) {
279
        printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
280
               GetKdfTypeStr(info->kdf.type), info->kdf.type);
281
    }
282
#endif
283
    else {
284
        printf("CryptoCb: %s \n", GetAlgoTypeStr(info->algo_type));
285
    }
286
}
287
#endif /* DEBUG_CRYPTOCB */
288
289
/* Search through listed devices and return the first matching device ID
290
 * found. */
291
static CryptoCb* wc_CryptoCb_GetDevice(int devId)
292
41.9k
{
293
41.9k
    int i;
294
42.0k
    for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) {
295
42.0k
        if (gCryptoDev[i].devId == devId)
296
41.9k
            return &gCryptoDev[i];
297
42.0k
    }
298
12
    return NULL;
299
41.9k
}
300
301
302
/* Filters through find callback set when trying to get the device,
303
 * returns the device found on success and null if not found. */
304
static CryptoCb* wc_CryptoCb_FindDevice(int devId, int algoType)
305
41.8k
{
306
41.8k
    int localDevId = devId;
307
308
#ifdef WOLF_CRYPTO_CB_FIND
309
    if (CryptoCb_FindCb != NULL) {
310
        localDevId = CryptoCb_FindCb(devId, algoType);
311
    }
312
#endif /* WOLF_CRYPTO_CB_FIND */
313
41.8k
    (void)algoType;
314
41.8k
    return wc_CryptoCb_GetDevice(localDevId);
315
41.8k
}
316
317
318
static CryptoCb* wc_CryptoCb_FindDeviceByIndex(int startIdx)
319
92.1k
{
320
92.1k
    int i;
321
825k
    for (i=startIdx; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) {
322
733k
        if (gCryptoDev[i].devId != INVALID_DEVID)
323
403
            return &gCryptoDev[i];
324
733k
    }
325
91.6k
    return NULL;
326
92.1k
}
327
328
static WC_INLINE int wc_CryptoCb_TranslateErrorCode(int ret)
329
41.8k
{
330
41.8k
    if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) {
331
        /* backwards compatibility for older NOT_COMPILED_IN syntax */
332
1.20k
        ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
333
1.20k
    }
334
41.8k
    return ret;
335
41.8k
}
336
337
/* Helper function to reset a device entry to invalid */
338
static WC_INLINE void wc_CryptoCb_ClearDev(CryptoCb *dev)
339
224
{
340
224
    XMEMSET(dev, 0, sizeof(*dev));
341
224
    dev->devId = INVALID_DEVID;
342
224
}
343
344
void wc_CryptoCb_Init(void)
345
28
{
346
28
    int i;
347
252
    for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) {
348
224
        wc_CryptoCb_ClearDev(&gCryptoDev[i]);
349
224
    }
350
28
}
351
352
void wc_CryptoCb_Cleanup(void)
353
16
{
354
16
    int i;
355
144
    for (i = 0; i < MAX_CRYPTO_DEVID_CALLBACKS; i++) {
356
128
        if(gCryptoDev[i].devId != INVALID_DEVID) {
357
0
            wc_CryptoCb_UnRegisterDevice(gCryptoDev[i].devId);
358
0
        }
359
128
    }
360
16
}
361
362
int wc_CryptoCb_GetDevIdAtIndex(int startIdx)
363
92.1k
{
364
92.1k
    int devId = INVALID_DEVID;
365
92.1k
    CryptoCb* dev = wc_CryptoCb_FindDeviceByIndex(startIdx);
366
92.1k
    if (dev) {
367
403
        devId = dev->devId;
368
403
    }
369
92.1k
    return devId;
370
92.1k
}
371
372
373
#ifdef WOLF_CRYPTO_CB_FIND
374
/* Used to register a find device function. Useful for cases where the
375
 * device ID in the struct may not have been set but still wanting to use
376
 * a specific crypto callback device ID. The find callback is global and
377
 * not thread safe. */
378
void wc_CryptoCb_SetDeviceFindCb(CryptoDevCallbackFind cb)
379
{
380
    CryptoCb_FindCb = cb;
381
}
382
#endif
383
384
int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx)
385
12
{
386
12
    int rc = 0;
387
388
    /* find existing or new */
389
12
    CryptoCb* dev = wc_CryptoCb_GetDevice(devId);
390
12
    if (dev == NULL)
391
12
        dev = wc_CryptoCb_GetDevice(INVALID_DEVID);
392
393
12
    if (dev == NULL)
394
0
        return BUFFER_E; /* out of devices */
395
396
12
    dev->devId = devId;
397
12
    dev->cb    = cb;
398
12
    dev->ctx   = ctx;
399
400
#ifdef WOLF_CRYPTO_CB_CMD
401
    if (cb != NULL) {
402
        /* Invoke callback with register command */
403
        wc_CryptoInfo info;
404
        XMEMSET(&info, 0, sizeof(info));
405
        info.algo_type = WC_ALGO_TYPE_NONE;
406
        info.cmd.type  = WC_CRYPTOCB_CMD_TYPE_REGISTER;
407
        info.cmd.ctx   = ctx;  /* cb may update on success */
408
409
        rc = cb(devId, &info, ctx);
410
        if (rc == 0) {
411
            /* Success.  Update dev->ctx */
412
            dev->ctx = info.cmd.ctx;
413
        }
414
        else if ((rc == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) ||
415
                 (rc == WC_NO_ERR_TRACE(NOT_COMPILED_IN))) {
416
            /* Not implemented.  Return success*/
417
            rc = 0;
418
        }
419
        else {
420
            /* Error in callback register cmd. Don't register */
421
            wc_CryptoCb_ClearDev(dev);
422
        }
423
    }
424
#endif
425
12
    return rc;
426
12
}
427
428
void wc_CryptoCb_UnRegisterDevice(int devId)
429
0
{
430
0
    CryptoCb* dev = NULL;
431
432
    /* Can't unregister the invalid device */
433
0
    if (devId == INVALID_DEVID)
434
0
        return;
435
436
    /* Find the matching dev */
437
0
    dev = wc_CryptoCb_GetDevice(devId);
438
0
    if (dev == NULL)
439
0
        return;
440
441
#ifdef WOLF_CRYPTO_CB_CMD
442
    if (dev->cb != NULL) {
443
        /* Invoke callback with unregister command.*/
444
        wc_CryptoInfo info;
445
        XMEMSET(&info, 0, sizeof(info));
446
        info.algo_type = WC_ALGO_TYPE_NONE;
447
        info.cmd.type  = WC_CRYPTOCB_CMD_TYPE_UNREGISTER;
448
        info.cmd.ctx   = NULL;  /* Not used */
449
450
        /* Ignore errors here */
451
        dev->cb(devId, &info, dev->ctx);
452
    }
453
#endif
454
0
    wc_CryptoCb_ClearDev(dev);
455
0
}
456
457
#ifndef NO_RSA
458
int wc_CryptoCb_Rsa(const byte* in, word32 inLen, byte* out,
459
    word32* outLen, int type, RsaKey* key, WC_RNG* rng)
460
0
{
461
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
462
0
    CryptoCb* dev;
463
464
0
    if (key == NULL)
465
0
        return ret;
466
467
    /* locate registered callback */
468
0
    dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
469
0
    if (dev && dev->cb) {
470
0
        wc_CryptoInfo cryptoInfo;
471
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
472
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
473
0
        cryptoInfo.pk.type = WC_PK_TYPE_RSA;
474
0
        cryptoInfo.pk.rsa.in = in;
475
0
        cryptoInfo.pk.rsa.inLen = inLen;
476
0
        cryptoInfo.pk.rsa.out = out;
477
0
        cryptoInfo.pk.rsa.outLen = outLen;
478
0
        cryptoInfo.pk.rsa.type = type;
479
0
        cryptoInfo.pk.rsa.key = key;
480
0
        cryptoInfo.pk.rsa.rng = rng;
481
482
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
483
0
    }
484
485
0
    return wc_CryptoCb_TranslateErrorCode(ret);
486
0
}
487
488
#ifdef WOLF_CRYPTO_CB_RSA_PAD
489
int wc_CryptoCb_RsaPad(const byte* in, word32 inLen, byte* out,
490
                       word32* outLen, int type, RsaKey* key, WC_RNG* rng,
491
                       RsaPadding *padding)
492
{
493
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
494
    CryptoCb* dev;
495
    int pk_type;
496
497
    if (key == NULL)
498
        return ret;
499
500
    /* locate registered callback */
501
    dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
502
503
    if (padding != NULL) {
504
        switch (padding->pad_type) {
505
        case WC_RSA_PKCSV15_PAD:
506
            pk_type = WC_PK_TYPE_RSA_PKCS;
507
            break;
508
        case WC_RSA_PSS_PAD:
509
            pk_type = WC_PK_TYPE_RSA_PSS;
510
            break;
511
        case WC_RSA_OAEP_PAD:
512
            pk_type = WC_PK_TYPE_RSA_OAEP;
513
            break;
514
        default:
515
            pk_type = WC_PK_TYPE_RSA;
516
        }
517
    } else {
518
        pk_type = WC_PK_TYPE_RSA;
519
    }
520
521
    if (dev && dev->cb) {
522
        wc_CryptoInfo cryptoInfo;
523
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
524
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
525
        cryptoInfo.pk.type = pk_type;
526
        cryptoInfo.pk.rsa.in = in;
527
        cryptoInfo.pk.rsa.inLen = inLen;
528
        cryptoInfo.pk.rsa.out = out;
529
        cryptoInfo.pk.rsa.outLen = outLen;
530
        cryptoInfo.pk.rsa.type = type;
531
        cryptoInfo.pk.rsa.key = key;
532
        cryptoInfo.pk.rsa.rng = rng;
533
        cryptoInfo.pk.rsa.padding = padding;
534
535
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
536
    }
537
538
    return wc_CryptoCb_TranslateErrorCode(ret);
539
}
540
#endif /* WOLF_CRYPTO_CB_RSA_PAD */
541
542
#ifdef WOLFSSL_KEY_GEN
543
int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
544
0
{
545
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
546
0
    CryptoCb* dev;
547
548
0
    if (key == NULL)
549
0
        return ret;
550
551
    /* locate registered callback */
552
0
    dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
553
0
    if (dev && dev->cb) {
554
0
        wc_CryptoInfo cryptoInfo;
555
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
556
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
557
0
        cryptoInfo.pk.type = WC_PK_TYPE_RSA_KEYGEN;
558
0
        cryptoInfo.pk.rsakg.key = key;
559
0
        cryptoInfo.pk.rsakg.size = size;
560
0
        cryptoInfo.pk.rsakg.e = e;
561
0
        cryptoInfo.pk.rsakg.rng = rng;
562
563
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
564
0
    }
565
566
0
    return wc_CryptoCb_TranslateErrorCode(ret);
567
0
}
568
#endif
569
570
int wc_CryptoCb_RsaCheckPrivKey(RsaKey* key, const byte* pubKey,
571
    word32 pubKeySz)
572
0
{
573
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
574
0
    CryptoCb* dev;
575
576
0
    if (key == NULL)
577
0
        return ret;
578
579
    /* locate registered callback */
580
0
    dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
581
0
    if (dev && dev->cb) {
582
0
        wc_CryptoInfo cryptoInfo;
583
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
584
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
585
0
        cryptoInfo.pk.type = WC_PK_TYPE_RSA_CHECK_PRIV_KEY;
586
0
        cryptoInfo.pk.rsa_check.key = key;
587
0
        cryptoInfo.pk.rsa_check.pubKey = pubKey;
588
0
        cryptoInfo.pk.rsa_check.pubKeySz = pubKeySz;
589
590
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
591
0
    }
592
593
0
    return wc_CryptoCb_TranslateErrorCode(ret);
594
0
}
595
596
int wc_CryptoCb_RsaGetSize(const RsaKey* key, int* keySize)
597
0
{
598
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
599
0
    CryptoCb* dev;
600
601
0
    if (key == NULL)
602
0
        return ret;
603
604
    /* locate registered callback */
605
0
    dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
606
0
    if (dev && dev->cb) {
607
0
        wc_CryptoInfo cryptoInfo;
608
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
609
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
610
0
        cryptoInfo.pk.type = WC_PK_TYPE_RSA_GET_SIZE;
611
0
        cryptoInfo.pk.rsa_get_size.key = key;
612
0
        cryptoInfo.pk.rsa_get_size.keySize = keySize;
613
614
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
615
0
    }
616
617
0
    return wc_CryptoCb_TranslateErrorCode(ret);
618
0
}
619
#endif /* !NO_RSA */
620
621
#ifdef HAVE_ECC
622
#ifdef HAVE_ECC_DHE
623
int wc_CryptoCb_MakeEccKey(WC_RNG* rng, int keySize, ecc_key* key, int curveId)
624
0
{
625
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
626
0
    CryptoCb* dev;
627
628
0
    if (key == NULL)
629
0
        return ret;
630
631
    /* locate registered callback */
632
0
    dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
633
0
    if (dev && dev->cb) {
634
0
        wc_CryptoInfo cryptoInfo;
635
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
636
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
637
0
        cryptoInfo.pk.type = WC_PK_TYPE_EC_KEYGEN;
638
0
        cryptoInfo.pk.eckg.rng = rng;
639
0
        cryptoInfo.pk.eckg.size = keySize;
640
0
        cryptoInfo.pk.eckg.key = key;
641
0
        cryptoInfo.pk.eckg.curveId = curveId;
642
643
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
644
0
    }
645
646
0
    return wc_CryptoCb_TranslateErrorCode(ret);
647
0
}
648
649
int wc_CryptoCb_Ecdh(ecc_key* private_key, ecc_key* public_key,
650
    byte* out, word32* outlen)
651
0
{
652
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
653
0
    CryptoCb* dev;
654
655
0
    if (private_key == NULL)
656
0
        return ret;
657
658
    /* locate registered callback */
659
0
    dev = wc_CryptoCb_FindDevice(private_key->devId, WC_ALGO_TYPE_PK);
660
0
    if (dev && dev->cb) {
661
0
        wc_CryptoInfo cryptoInfo;
662
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
663
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
664
0
        cryptoInfo.pk.type = WC_PK_TYPE_ECDH;
665
0
        cryptoInfo.pk.ecdh.private_key = private_key;
666
0
        cryptoInfo.pk.ecdh.public_key = public_key;
667
0
        cryptoInfo.pk.ecdh.out = out;
668
0
        cryptoInfo.pk.ecdh.outlen = outlen;
669
670
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
671
0
    }
672
673
0
    return wc_CryptoCb_TranslateErrorCode(ret);
674
0
}
675
#endif
676
677
#ifdef HAVE_ECC_SIGN
678
int wc_CryptoCb_EccSign(const byte* in, word32 inlen, byte* out,
679
    word32 *outlen, WC_RNG* rng, ecc_key* key)
680
0
{
681
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
682
0
    CryptoCb* dev;
683
684
0
    if (key == NULL)
685
0
        return ret;
686
687
    /* locate registered callback */
688
0
    dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
689
0
    if (dev && dev->cb) {
690
0
        wc_CryptoInfo cryptoInfo;
691
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
692
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
693
0
        cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_SIGN;
694
0
        cryptoInfo.pk.eccsign.in = in;
695
0
        cryptoInfo.pk.eccsign.inlen = inlen;
696
0
        cryptoInfo.pk.eccsign.out = out;
697
0
        cryptoInfo.pk.eccsign.outlen = outlen;
698
0
        cryptoInfo.pk.eccsign.rng = rng;
699
0
        cryptoInfo.pk.eccsign.key = key;
700
701
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
702
0
    }
703
704
0
    return wc_CryptoCb_TranslateErrorCode(ret);
705
0
}
706
#endif
707
708
#ifdef HAVE_ECC_VERIFY
709
int wc_CryptoCb_EccVerify(const byte* sig, word32 siglen,
710
    const byte* hash, word32 hashlen, int* res, ecc_key* key)
711
0
{
712
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
713
0
    CryptoCb* dev;
714
715
0
    if (key == NULL)
716
0
        return ret;
717
718
    /* locate registered callback */
719
0
    dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
720
0
    if (dev && dev->cb) {
721
0
        wc_CryptoInfo cryptoInfo;
722
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
723
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
724
0
        cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_VERIFY;
725
0
        cryptoInfo.pk.eccverify.sig = sig;
726
0
        cryptoInfo.pk.eccverify.siglen = siglen;
727
0
        cryptoInfo.pk.eccverify.hash = hash;
728
0
        cryptoInfo.pk.eccverify.hashlen = hashlen;
729
0
        cryptoInfo.pk.eccverify.res = res;
730
0
        cryptoInfo.pk.eccverify.key = key;
731
732
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
733
0
    }
734
735
0
    return wc_CryptoCb_TranslateErrorCode(ret);
736
0
}
737
#endif
738
739
#ifdef HAVE_ECC_CHECK_KEY
740
int wc_CryptoCb_EccCheckPrivKey(ecc_key* key, const byte* pubKey,
741
    word32 pubKeySz)
742
0
{
743
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
744
0
    CryptoCb* dev;
745
746
0
    if (key == NULL)
747
0
        return ret;
748
749
    /* locate registered callback */
750
0
    dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
751
0
    if (dev && dev->cb) {
752
0
        wc_CryptoInfo cryptoInfo;
753
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
754
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
755
0
        cryptoInfo.pk.type = WC_PK_TYPE_EC_CHECK_PRIV_KEY;
756
0
        cryptoInfo.pk.ecc_check.key = key;
757
0
        cryptoInfo.pk.ecc_check.pubKey = pubKey;
758
0
        cryptoInfo.pk.ecc_check.pubKeySz = pubKeySz;
759
760
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
761
0
    }
762
763
0
    return wc_CryptoCb_TranslateErrorCode(ret);
764
0
}
765
#endif
766
#endif /* HAVE_ECC */
767
768
#ifdef HAVE_CURVE25519
769
int wc_CryptoCb_Curve25519Gen(WC_RNG* rng, int keySize,
770
    curve25519_key* key)
771
0
{
772
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
773
0
    CryptoCb* dev;
774
775
0
    if (key == NULL)
776
0
        return ret;
777
778
    /* locate registered callback */
779
0
    dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
780
0
    if (dev && dev->cb) {
781
0
        wc_CryptoInfo cryptoInfo;
782
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
783
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
784
0
        cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519_KEYGEN;
785
0
        cryptoInfo.pk.curve25519kg.rng = rng;
786
0
        cryptoInfo.pk.curve25519kg.size = keySize;
787
0
        cryptoInfo.pk.curve25519kg.key = key;
788
789
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
790
0
    }
791
792
0
    return wc_CryptoCb_TranslateErrorCode(ret);
793
0
}
794
795
int wc_CryptoCb_Curve25519(curve25519_key* private_key,
796
    curve25519_key* public_key, byte* out, word32* outlen, int endian)
797
0
{
798
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
799
0
    CryptoCb* dev;
800
801
0
    if (private_key == NULL)
802
0
        return ret;
803
804
    /* locate registered callback */
805
0
    dev = wc_CryptoCb_FindDevice(private_key->devId, WC_ALGO_TYPE_PK);
806
0
    if (dev && dev->cb) {
807
0
        wc_CryptoInfo cryptoInfo;
808
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
809
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
810
0
        cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519;
811
0
        cryptoInfo.pk.curve25519.private_key = private_key;
812
0
        cryptoInfo.pk.curve25519.public_key = public_key;
813
0
        cryptoInfo.pk.curve25519.out = out;
814
0
        cryptoInfo.pk.curve25519.outlen = outlen;
815
0
        cryptoInfo.pk.curve25519.endian = endian;
816
817
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
818
0
    }
819
820
0
    return wc_CryptoCb_TranslateErrorCode(ret);
821
0
}
822
#endif /* HAVE_CURVE25519 */
823
824
#ifdef HAVE_ED25519
825
int wc_CryptoCb_Ed25519Gen(WC_RNG* rng, int keySize,
826
    ed25519_key* key)
827
0
{
828
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
829
0
    CryptoCb* dev;
830
831
0
    if (key == NULL)
832
0
        return ret;
833
834
    /* locate registered callback */
835
0
    dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
836
0
    if (dev && dev->cb) {
837
0
        wc_CryptoInfo cryptoInfo;
838
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
839
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
840
0
        cryptoInfo.pk.type = WC_PK_TYPE_ED25519_KEYGEN;
841
0
        cryptoInfo.pk.ed25519kg.rng = rng;
842
0
        cryptoInfo.pk.ed25519kg.size = keySize;
843
0
        cryptoInfo.pk.ed25519kg.key = key;
844
845
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
846
0
    }
847
848
0
    return wc_CryptoCb_TranslateErrorCode(ret);
849
0
}
850
851
int wc_CryptoCb_Ed25519Sign(const byte* in, word32 inLen, byte* out,
852
                            word32 *outLen, ed25519_key* key, byte type,
853
                            const byte* context, byte contextLen)
854
0
{
855
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
856
0
    CryptoCb* dev;
857
858
0
    if (key == NULL)
859
0
        return ret;
860
861
    /* locate registered callback */
862
0
    dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
863
0
    if (dev && dev->cb) {
864
0
        wc_CryptoInfo cryptoInfo;
865
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
866
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
867
0
        cryptoInfo.pk.type = WC_PK_TYPE_ED25519_SIGN;
868
0
        cryptoInfo.pk.ed25519sign.in = in;
869
0
        cryptoInfo.pk.ed25519sign.inLen = inLen;
870
0
        cryptoInfo.pk.ed25519sign.out = out;
871
0
        cryptoInfo.pk.ed25519sign.outLen = outLen;
872
0
        cryptoInfo.pk.ed25519sign.key = key;
873
0
        cryptoInfo.pk.ed25519sign.type = type;
874
0
        cryptoInfo.pk.ed25519sign.context = context;
875
0
        cryptoInfo.pk.ed25519sign.contextLen = contextLen;
876
877
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
878
0
    }
879
880
0
    return wc_CryptoCb_TranslateErrorCode(ret);
881
0
}
882
883
int wc_CryptoCb_Ed25519Verify(const byte* sig, word32 sigLen,
884
    const byte* msg, word32 msgLen, int* res, ed25519_key* key, byte type,
885
    const byte* context, byte contextLen)
886
0
{
887
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
888
0
    CryptoCb* dev;
889
890
0
    if (key == NULL)
891
0
        return ret;
892
893
    /* locate registered callback */
894
0
    dev = wc_CryptoCb_FindDevice(key->devId, WC_ALGO_TYPE_PK);
895
0
    if (dev && dev->cb) {
896
0
        wc_CryptoInfo cryptoInfo;
897
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
898
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
899
0
        cryptoInfo.pk.type = WC_PK_TYPE_ED25519_VERIFY;
900
0
        cryptoInfo.pk.ed25519verify.sig = sig;
901
0
        cryptoInfo.pk.ed25519verify.sigLen = sigLen;
902
0
        cryptoInfo.pk.ed25519verify.msg = msg;
903
0
        cryptoInfo.pk.ed25519verify.msgLen = msgLen;
904
0
        cryptoInfo.pk.ed25519verify.res = res;
905
0
        cryptoInfo.pk.ed25519verify.key = key;
906
0
        cryptoInfo.pk.ed25519verify.type = type;
907
0
        cryptoInfo.pk.ed25519verify.context = context;
908
0
        cryptoInfo.pk.ed25519verify.contextLen = contextLen;
909
910
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
911
0
    }
912
913
0
    return wc_CryptoCb_TranslateErrorCode(ret);
914
0
}
915
#endif /* HAVE_ED25519 */
916
917
#if defined(WOLFSSL_HAVE_MLKEM)
918
int wc_CryptoCb_PqcKemGetDevId(int type, void* key)
919
{
920
    int devId = INVALID_DEVID;
921
922
    if (key == NULL)
923
        return devId;
924
925
    /* get devId */
926
    if (type == WC_PQC_KEM_TYPE_KYBER) {
927
        devId = ((KyberKey*) key)->devId;
928
    }
929
930
    return devId;
931
}
932
933
int wc_CryptoCb_MakePqcKemKey(WC_RNG* rng, int type, int keySize, void* key)
934
{
935
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
936
    int devId = INVALID_DEVID;
937
    CryptoCb* dev;
938
939
    if (key == NULL)
940
        return ret;
941
942
    /* get devId */
943
    devId = wc_CryptoCb_PqcKemGetDevId(type, key);
944
    if (devId == INVALID_DEVID)
945
        return ret;
946
947
    /* locate registered callback */
948
    dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK);
949
    if (dev && dev->cb) {
950
        wc_CryptoInfo cryptoInfo;
951
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
952
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
953
        cryptoInfo.pk.type = WC_PK_TYPE_PQC_KEM_KEYGEN;
954
        cryptoInfo.pk.pqc_kem_kg.rng = rng;
955
        cryptoInfo.pk.pqc_kem_kg.size = keySize;
956
        cryptoInfo.pk.pqc_kem_kg.key = key;
957
        cryptoInfo.pk.pqc_kem_kg.type = type;
958
959
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
960
    }
961
962
    return wc_CryptoCb_TranslateErrorCode(ret);
963
}
964
965
int wc_CryptoCb_PqcEncapsulate(byte* ciphertext, word32 ciphertextLen,
966
    byte* sharedSecret, word32 sharedSecretLen, WC_RNG* rng, int type,
967
    void* key)
968
{
969
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
970
    int devId = INVALID_DEVID;
971
    CryptoCb* dev;
972
973
    if (key == NULL)
974
        return ret;
975
976
    /* get devId */
977
    devId = wc_CryptoCb_PqcKemGetDevId(type, key);
978
    if (devId == INVALID_DEVID)
979
        return ret;
980
981
    /* locate registered callback */
982
    dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK);
983
    if (dev && dev->cb) {
984
        wc_CryptoInfo cryptoInfo;
985
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
986
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
987
        cryptoInfo.pk.type = WC_PK_TYPE_PQC_KEM_ENCAPS;
988
        cryptoInfo.pk.pqc_encaps.ciphertext = ciphertext;
989
        cryptoInfo.pk.pqc_encaps.ciphertextLen = ciphertextLen;
990
        cryptoInfo.pk.pqc_encaps.sharedSecret = sharedSecret;
991
        cryptoInfo.pk.pqc_encaps.sharedSecretLen = sharedSecretLen;
992
        cryptoInfo.pk.pqc_encaps.rng = rng;
993
        cryptoInfo.pk.pqc_encaps.key = key;
994
        cryptoInfo.pk.pqc_encaps.type = type;
995
996
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
997
    }
998
999
    return wc_CryptoCb_TranslateErrorCode(ret);
1000
}
1001
1002
int wc_CryptoCb_PqcDecapsulate(const byte* ciphertext, word32 ciphertextLen,
1003
    byte* sharedSecret, word32 sharedSecretLen, int type, void* key)
1004
{
1005
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1006
    int devId = INVALID_DEVID;
1007
    CryptoCb* dev;
1008
1009
    if (key == NULL)
1010
        return ret;
1011
1012
    /* get devId */
1013
    devId = wc_CryptoCb_PqcKemGetDevId(type, key);
1014
    if (devId == INVALID_DEVID)
1015
        return ret;
1016
1017
    /* locate registered callback */
1018
    dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK);
1019
    if (dev && dev->cb) {
1020
        wc_CryptoInfo cryptoInfo;
1021
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1022
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
1023
        cryptoInfo.pk.type = WC_PK_TYPE_PQC_KEM_DECAPS;
1024
        cryptoInfo.pk.pqc_decaps.ciphertext = ciphertext;
1025
        cryptoInfo.pk.pqc_decaps.ciphertextLen = ciphertextLen;
1026
        cryptoInfo.pk.pqc_decaps.sharedSecret = sharedSecret;
1027
        cryptoInfo.pk.pqc_decaps.sharedSecretLen = sharedSecretLen;
1028
        cryptoInfo.pk.pqc_decaps.key = key;
1029
        cryptoInfo.pk.pqc_decaps.type = type;
1030
1031
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1032
    }
1033
1034
    return wc_CryptoCb_TranslateErrorCode(ret);
1035
}
1036
#endif /* WOLFSSL_HAVE_MLKEM */
1037
1038
#if defined(HAVE_FALCON) || defined(HAVE_DILITHIUM)
1039
int wc_CryptoCb_PqcSigGetDevId(int type, void* key)
1040
{
1041
    int devId = INVALID_DEVID;
1042
1043
    if (key == NULL)
1044
        return devId;
1045
1046
    /* get devId */
1047
#if defined(HAVE_DILITHIUM)
1048
    if (type == WC_PQC_SIG_TYPE_DILITHIUM) {
1049
        devId = ((dilithium_key*) key)->devId;
1050
    }
1051
#endif
1052
#if defined(HAVE_FALCON)
1053
    if (type == WC_PQC_SIG_TYPE_FALCON) {
1054
        devId = ((falcon_key*) key)->devId;
1055
    }
1056
#endif
1057
1058
    return devId;
1059
}
1060
1061
int wc_CryptoCb_MakePqcSignatureKey(WC_RNG* rng, int type, int keySize,
1062
    void* key)
1063
{
1064
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1065
    int devId = INVALID_DEVID;
1066
    CryptoCb* dev;
1067
1068
    if (key == NULL)
1069
        return ret;
1070
1071
    /* get devId */
1072
    devId = wc_CryptoCb_PqcSigGetDevId(type, key);
1073
    if (devId == INVALID_DEVID)
1074
        return ret;
1075
1076
    /* locate registered callback */
1077
    dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK);
1078
    if (dev && dev->cb) {
1079
        wc_CryptoInfo cryptoInfo;
1080
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1081
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
1082
        cryptoInfo.pk.type = WC_PK_TYPE_PQC_SIG_KEYGEN;
1083
        cryptoInfo.pk.pqc_sig_kg.rng = rng;
1084
        cryptoInfo.pk.pqc_sig_kg.size = keySize;
1085
        cryptoInfo.pk.pqc_sig_kg.key = key;
1086
        cryptoInfo.pk.pqc_sig_kg.type = type;
1087
1088
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1089
    }
1090
1091
    return wc_CryptoCb_TranslateErrorCode(ret);
1092
}
1093
1094
int wc_CryptoCb_PqcSign(const byte* in, word32 inlen, byte* out, word32 *outlen,
1095
    const byte* context, byte contextLen, word32 preHashType, WC_RNG* rng,
1096
    int type, void* key)
1097
{
1098
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1099
    int devId = INVALID_DEVID;
1100
    CryptoCb* dev;
1101
1102
    if (key == NULL)
1103
        return ret;
1104
1105
    /* get devId */
1106
    devId = wc_CryptoCb_PqcSigGetDevId(type, key);
1107
    if (devId == INVALID_DEVID)
1108
        return ret;
1109
1110
    /* locate registered callback */
1111
    dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK);
1112
    if (dev && dev->cb) {
1113
        wc_CryptoInfo cryptoInfo;
1114
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1115
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
1116
        cryptoInfo.pk.type = WC_PK_TYPE_PQC_SIG_SIGN;
1117
        cryptoInfo.pk.pqc_sign.in = in;
1118
        cryptoInfo.pk.pqc_sign.inlen = inlen;
1119
        cryptoInfo.pk.pqc_sign.out = out;
1120
        cryptoInfo.pk.pqc_sign.outlen = outlen;
1121
        cryptoInfo.pk.pqc_sign.context = context;
1122
        cryptoInfo.pk.pqc_sign.contextLen = contextLen;
1123
        cryptoInfo.pk.pqc_sign.preHashType = preHashType;
1124
        cryptoInfo.pk.pqc_sign.rng = rng;
1125
        cryptoInfo.pk.pqc_sign.key = key;
1126
        cryptoInfo.pk.pqc_sign.type = type;
1127
1128
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1129
    }
1130
1131
    return wc_CryptoCb_TranslateErrorCode(ret);
1132
}
1133
1134
int wc_CryptoCb_PqcVerify(const byte* sig, word32 siglen, const byte* msg,
1135
    word32 msglen, const byte* context, byte contextLen, word32 preHashType,
1136
    int* res, int type, void* key)
1137
{
1138
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1139
    int devId = INVALID_DEVID;
1140
    CryptoCb* dev;
1141
1142
    if (key == NULL)
1143
        return ret;
1144
1145
    /* get devId */
1146
    devId = wc_CryptoCb_PqcSigGetDevId(type, key);
1147
    if (devId == INVALID_DEVID)
1148
        return ret;
1149
1150
    /* locate registered callback */
1151
    dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK);
1152
    if (dev && dev->cb) {
1153
        wc_CryptoInfo cryptoInfo;
1154
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1155
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
1156
        cryptoInfo.pk.type = WC_PK_TYPE_PQC_SIG_VERIFY;
1157
        cryptoInfo.pk.pqc_verify.sig = sig;
1158
        cryptoInfo.pk.pqc_verify.siglen = siglen;
1159
        cryptoInfo.pk.pqc_verify.msg = msg;
1160
        cryptoInfo.pk.pqc_verify.msglen = msglen;
1161
        cryptoInfo.pk.pqc_verify.context = context;
1162
        cryptoInfo.pk.pqc_verify.contextLen = contextLen;
1163
        cryptoInfo.pk.pqc_verify.preHashType = preHashType;
1164
        cryptoInfo.pk.pqc_verify.res = res;
1165
        cryptoInfo.pk.pqc_verify.key = key;
1166
        cryptoInfo.pk.pqc_verify.type = type;
1167
1168
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1169
    }
1170
1171
    return wc_CryptoCb_TranslateErrorCode(ret);
1172
}
1173
1174
int wc_CryptoCb_PqcSignatureCheckPrivKey(void* key, int type,
1175
    const byte* pubKey, word32 pubKeySz)
1176
{
1177
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1178
    int devId = INVALID_DEVID;
1179
    CryptoCb* dev;
1180
1181
    if (key == NULL)
1182
        return ret;
1183
1184
    /* get devId */
1185
    devId = wc_CryptoCb_PqcSigGetDevId(type, key);
1186
    if (devId == INVALID_DEVID)
1187
        return ret;
1188
1189
    /* locate registered callback */
1190
    dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_PK);
1191
    if (dev && dev->cb) {
1192
        wc_CryptoInfo cryptoInfo;
1193
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1194
        cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
1195
        cryptoInfo.pk.type = WC_PK_TYPE_PQC_SIG_CHECK_PRIV_KEY;
1196
        cryptoInfo.pk.pqc_sig_check.key = key;
1197
        cryptoInfo.pk.pqc_sig_check.pubKey = pubKey;
1198
        cryptoInfo.pk.pqc_sig_check.pubKeySz = pubKeySz;
1199
        cryptoInfo.pk.pqc_sig_check.type = type;
1200
1201
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1202
    }
1203
1204
    return wc_CryptoCb_TranslateErrorCode(ret);
1205
}
1206
#endif /* HAVE_FALCON || HAVE_DILITHIUM */
1207
1208
#ifndef NO_AES
1209
#ifdef HAVE_AESGCM
1210
int wc_CryptoCb_AesGcmEncrypt(Aes* aes, byte* out,
1211
                               const byte* in, word32 sz,
1212
                               const byte* iv, word32 ivSz,
1213
                               byte* authTag, word32 authTagSz,
1214
                               const byte* authIn, word32 authInSz)
1215
0
{
1216
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1217
0
    CryptoCb* dev;
1218
1219
    /* locate registered callback */
1220
0
    if (aes) {
1221
0
        dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER);
1222
0
    }
1223
0
    else {
1224
        /* locate first callback and try using it */
1225
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1226
0
    }
1227
1228
0
    if (dev && dev->cb) {
1229
0
        wc_CryptoInfo cryptoInfo;
1230
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1231
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
1232
0
        cryptoInfo.cipher.type = WC_CIPHER_AES_GCM;
1233
0
        cryptoInfo.cipher.enc = 1;
1234
0
        cryptoInfo.cipher.aesgcm_enc.aes       = aes;
1235
0
        cryptoInfo.cipher.aesgcm_enc.out       = out;
1236
0
        cryptoInfo.cipher.aesgcm_enc.in        = in;
1237
0
        cryptoInfo.cipher.aesgcm_enc.sz        = sz;
1238
0
        cryptoInfo.cipher.aesgcm_enc.iv        = iv;
1239
0
        cryptoInfo.cipher.aesgcm_enc.ivSz      = ivSz;
1240
0
        cryptoInfo.cipher.aesgcm_enc.authTag   = authTag;
1241
0
        cryptoInfo.cipher.aesgcm_enc.authTagSz = authTagSz;
1242
0
        cryptoInfo.cipher.aesgcm_enc.authIn    = authIn;
1243
0
        cryptoInfo.cipher.aesgcm_enc.authInSz  = authInSz;
1244
1245
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1246
0
    }
1247
1248
0
    return wc_CryptoCb_TranslateErrorCode(ret);
1249
0
}
1250
1251
int wc_CryptoCb_AesGcmDecrypt(Aes* aes, byte* out,
1252
                               const byte* in, word32 sz,
1253
                               const byte* iv, word32 ivSz,
1254
                               const byte* authTag, word32 authTagSz,
1255
                               const byte* authIn, word32 authInSz)
1256
0
{
1257
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1258
0
    CryptoCb* dev;
1259
1260
    /* locate registered callback */
1261
0
    if (aes) {
1262
0
        dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER);
1263
0
    }
1264
0
    else {
1265
        /* locate first callback and try using it */
1266
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1267
0
    }
1268
1269
0
    if (dev && dev->cb) {
1270
0
        wc_CryptoInfo cryptoInfo;
1271
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1272
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
1273
0
        cryptoInfo.cipher.type = WC_CIPHER_AES_GCM;
1274
0
        cryptoInfo.cipher.enc = 0;
1275
0
        cryptoInfo.cipher.aesgcm_dec.aes       = aes;
1276
0
        cryptoInfo.cipher.aesgcm_dec.out       = out;
1277
0
        cryptoInfo.cipher.aesgcm_dec.in        = in;
1278
0
        cryptoInfo.cipher.aesgcm_dec.sz        = sz;
1279
0
        cryptoInfo.cipher.aesgcm_dec.iv        = iv;
1280
0
        cryptoInfo.cipher.aesgcm_dec.ivSz      = ivSz;
1281
0
        cryptoInfo.cipher.aesgcm_dec.authTag   = authTag;
1282
0
        cryptoInfo.cipher.aesgcm_dec.authTagSz = authTagSz;
1283
0
        cryptoInfo.cipher.aesgcm_dec.authIn    = authIn;
1284
0
        cryptoInfo.cipher.aesgcm_dec.authInSz  = authInSz;
1285
1286
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1287
0
    }
1288
1289
0
    return wc_CryptoCb_TranslateErrorCode(ret);
1290
0
}
1291
#endif /* HAVE_AESGCM */
1292
1293
#ifdef HAVE_AESCCM
1294
int wc_CryptoCb_AesCcmEncrypt(Aes* aes, byte* out,
1295
                               const byte* in, word32 sz,
1296
                               const byte* nonce, word32 nonceSz,
1297
                               byte* authTag, word32 authTagSz,
1298
                               const byte* authIn, word32 authInSz)
1299
0
{
1300
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1301
0
    CryptoCb* dev;
1302
1303
    /* locate registered callback */
1304
0
    if (aes) {
1305
0
        dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER);
1306
0
    }
1307
0
    else {
1308
        /* locate first callback and try using it */
1309
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1310
0
    }
1311
1312
0
    if (dev && dev->cb) {
1313
0
        wc_CryptoInfo cryptoInfo;
1314
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1315
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
1316
0
        cryptoInfo.cipher.type = WC_CIPHER_AES_CCM;
1317
0
        cryptoInfo.cipher.enc = 1;
1318
0
        cryptoInfo.cipher.aesccm_enc.aes       = aes;
1319
0
        cryptoInfo.cipher.aesccm_enc.out       = out;
1320
0
        cryptoInfo.cipher.aesccm_enc.in        = in;
1321
0
        cryptoInfo.cipher.aesccm_enc.sz        = sz;
1322
0
        cryptoInfo.cipher.aesccm_enc.nonce     = nonce;
1323
0
        cryptoInfo.cipher.aesccm_enc.nonceSz   = nonceSz;
1324
0
        cryptoInfo.cipher.aesccm_enc.authTag   = authTag;
1325
0
        cryptoInfo.cipher.aesccm_enc.authTagSz = authTagSz;
1326
0
        cryptoInfo.cipher.aesccm_enc.authIn    = authIn;
1327
0
        cryptoInfo.cipher.aesccm_enc.authInSz  = authInSz;
1328
1329
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1330
0
    }
1331
1332
0
    return wc_CryptoCb_TranslateErrorCode(ret);
1333
0
}
1334
1335
int wc_CryptoCb_AesCcmDecrypt(Aes* aes, byte* out,
1336
                               const byte* in, word32 sz,
1337
                               const byte* nonce, word32 nonceSz,
1338
                               const byte* authTag, word32 authTagSz,
1339
                               const byte* authIn, word32 authInSz)
1340
0
{
1341
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1342
0
    CryptoCb* dev;
1343
1344
    /* locate registered callback */
1345
0
    if (aes) {
1346
0
        dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER);
1347
0
    }
1348
0
    else {
1349
        /* locate first callback and try using it */
1350
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1351
0
    }
1352
1353
0
    if (dev && dev->cb) {
1354
0
        wc_CryptoInfo cryptoInfo;
1355
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1356
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
1357
0
        cryptoInfo.cipher.type = WC_CIPHER_AES_CCM;
1358
0
        cryptoInfo.cipher.enc = 0;
1359
0
        cryptoInfo.cipher.aesccm_dec.aes       = aes;
1360
0
        cryptoInfo.cipher.aesccm_dec.out       = out;
1361
0
        cryptoInfo.cipher.aesccm_dec.in        = in;
1362
0
        cryptoInfo.cipher.aesccm_dec.sz        = sz;
1363
0
        cryptoInfo.cipher.aesccm_dec.nonce     = nonce;
1364
0
        cryptoInfo.cipher.aesccm_dec.nonceSz   = nonceSz;
1365
0
        cryptoInfo.cipher.aesccm_dec.authTag   = authTag;
1366
0
        cryptoInfo.cipher.aesccm_dec.authTagSz = authTagSz;
1367
0
        cryptoInfo.cipher.aesccm_dec.authIn    = authIn;
1368
0
        cryptoInfo.cipher.aesccm_dec.authInSz  = authInSz;
1369
1370
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1371
0
    }
1372
1373
0
    return wc_CryptoCb_TranslateErrorCode(ret);
1374
0
}
1375
#endif /* HAVE_AESCCM */
1376
1377
#ifdef HAVE_AES_CBC
1378
int wc_CryptoCb_AesCbcEncrypt(Aes* aes, byte* out,
1379
                               const byte* in, word32 sz)
1380
0
{
1381
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1382
0
    CryptoCb* dev;
1383
1384
    /* locate registered callback */
1385
0
    if (aes) {
1386
0
        dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER);
1387
0
    }
1388
0
    else {
1389
        /* locate first callback and try using it */
1390
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1391
0
    }
1392
1393
0
    if (dev && dev->cb) {
1394
0
        wc_CryptoInfo cryptoInfo;
1395
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1396
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
1397
0
        cryptoInfo.cipher.type = WC_CIPHER_AES_CBC;
1398
0
        cryptoInfo.cipher.enc = 1;
1399
0
        cryptoInfo.cipher.aescbc.aes = aes;
1400
0
        cryptoInfo.cipher.aescbc.out = out;
1401
0
        cryptoInfo.cipher.aescbc.in = in;
1402
0
        cryptoInfo.cipher.aescbc.sz = sz;
1403
1404
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1405
0
    }
1406
1407
0
    return wc_CryptoCb_TranslateErrorCode(ret);
1408
0
}
1409
1410
int wc_CryptoCb_AesCbcDecrypt(Aes* aes, byte* out,
1411
                               const byte* in, word32 sz)
1412
0
{
1413
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1414
0
    CryptoCb* dev;
1415
1416
    /* locate registered callback */
1417
0
    if (aes) {
1418
0
        dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER);
1419
0
    }
1420
0
    else {
1421
        /* locate first callback and try using it */
1422
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1423
0
    }
1424
1425
0
    if (dev && dev->cb) {
1426
0
        wc_CryptoInfo cryptoInfo;
1427
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1428
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
1429
0
        cryptoInfo.cipher.type = WC_CIPHER_AES_CBC;
1430
0
        cryptoInfo.cipher.enc = 0;
1431
0
        cryptoInfo.cipher.aescbc.aes = aes;
1432
0
        cryptoInfo.cipher.aescbc.out = out;
1433
0
        cryptoInfo.cipher.aescbc.in = in;
1434
0
        cryptoInfo.cipher.aescbc.sz = sz;
1435
1436
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1437
0
    }
1438
1439
0
    return wc_CryptoCb_TranslateErrorCode(ret);
1440
0
}
1441
#endif /* HAVE_AES_CBC */
1442
#ifdef WOLFSSL_AES_COUNTER
1443
int wc_CryptoCb_AesCtrEncrypt(Aes* aes, byte* out,
1444
                               const byte* in, word32 sz)
1445
0
{
1446
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1447
0
    CryptoCb* dev;
1448
1449
    /* locate registered callback */
1450
0
    if (aes) {
1451
0
        dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER);
1452
0
    }
1453
0
    else {
1454
        /* locate first callback and try using it */
1455
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1456
0
    }
1457
1458
0
    if (dev && dev->cb) {
1459
0
        wc_CryptoInfo cryptoInfo;
1460
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1461
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
1462
0
        cryptoInfo.cipher.type = WC_CIPHER_AES_CTR;
1463
0
        cryptoInfo.cipher.enc = 1;
1464
0
        cryptoInfo.cipher.aesctr.aes = aes;
1465
0
        cryptoInfo.cipher.aesctr.out = out;
1466
0
        cryptoInfo.cipher.aesctr.in = in;
1467
0
        cryptoInfo.cipher.aesctr.sz = sz;
1468
1469
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1470
0
    }
1471
1472
0
    return wc_CryptoCb_TranslateErrorCode(ret);
1473
0
}
1474
#endif /* WOLFSSL_AES_COUNTER */
1475
#ifdef HAVE_AES_ECB
1476
int wc_CryptoCb_AesEcbEncrypt(Aes* aes, byte* out,
1477
                               const byte* in, word32 sz)
1478
151
{
1479
151
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1480
151
    CryptoCb* dev;
1481
1482
    /* locate registered callback */
1483
151
    if (aes) {
1484
151
        dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER);
1485
151
    }
1486
0
    else {
1487
        /* locate first callback and try using it */
1488
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1489
0
    }
1490
1491
151
    if (dev && dev->cb) {
1492
0
        wc_CryptoInfo cryptoInfo;
1493
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1494
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
1495
0
        cryptoInfo.cipher.type = WC_CIPHER_AES_ECB;
1496
0
        cryptoInfo.cipher.enc = 1;
1497
0
        cryptoInfo.cipher.aesecb.aes = aes;
1498
0
        cryptoInfo.cipher.aesecb.out = out;
1499
0
        cryptoInfo.cipher.aesecb.in = in;
1500
0
        cryptoInfo.cipher.aesecb.sz = sz;
1501
1502
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1503
0
    }
1504
1505
151
    return wc_CryptoCb_TranslateErrorCode(ret);
1506
151
}
1507
1508
int wc_CryptoCb_AesEcbDecrypt(Aes* aes, byte* out,
1509
                               const byte* in, word32 sz)
1510
221
{
1511
221
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1512
221
    CryptoCb* dev;
1513
1514
    /* locate registered callback */
1515
221
    if (aes) {
1516
221
        dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER);
1517
221
    }
1518
0
    else {
1519
        /* locate first callback and try using it */
1520
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1521
0
    }
1522
1523
221
    if (dev && dev->cb) {
1524
0
        wc_CryptoInfo cryptoInfo;
1525
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1526
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
1527
0
        cryptoInfo.cipher.type = WC_CIPHER_AES_ECB;
1528
0
        cryptoInfo.cipher.enc = 0;
1529
0
        cryptoInfo.cipher.aesecb.aes = aes;
1530
0
        cryptoInfo.cipher.aesecb.out = out;
1531
0
        cryptoInfo.cipher.aesecb.in = in;
1532
0
        cryptoInfo.cipher.aesecb.sz = sz;
1533
1534
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1535
0
    }
1536
1537
221
    return wc_CryptoCb_TranslateErrorCode(ret);
1538
221
}
1539
#endif /* HAVE_AES_ECB */
1540
1541
#ifdef WOLF_CRYPTO_CB_AES_SETKEY
1542
int wc_CryptoCb_AesSetKey(Aes* aes, const byte* key, word32 keySz)
1543
{
1544
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1545
    CryptoCb* dev;
1546
1547
    if (aes == NULL || key == NULL)
1548
        return BAD_FUNC_ARG;
1549
1550
    if (aes->devId == INVALID_DEVID)
1551
        return CRYPTOCB_UNAVAILABLE;
1552
1553
    /* locate registered callback */
1554
    dev = wc_CryptoCb_FindDevice(aes->devId, WC_ALGO_TYPE_CIPHER);
1555
    if (dev && dev->cb) {
1556
        wc_CryptoInfo cryptoInfo;
1557
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1558
        cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
1559
        cryptoInfo.cipher.type = WC_CIPHER_AES;
1560
        cryptoInfo.cipher.aessetkey.aes = aes;
1561
        cryptoInfo.cipher.aessetkey.key = key;
1562
        cryptoInfo.cipher.aessetkey.keySz = keySz;
1563
1564
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1565
    }
1566
1567
    return wc_CryptoCb_TranslateErrorCode(ret);
1568
}
1569
#endif /* WOLF_CRYPTO_CB_AES_SETKEY */
1570
#endif /* !NO_AES */
1571
1572
#ifndef NO_DES3
1573
int wc_CryptoCb_Des3Encrypt(Des3* des3, byte* out,
1574
                               const byte* in, word32 sz)
1575
245
{
1576
245
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1577
245
    CryptoCb* dev;
1578
1579
    /* locate registered callback */
1580
245
    if (des3) {
1581
245
        dev = wc_CryptoCb_FindDevice(des3->devId, WC_ALGO_TYPE_CIPHER);
1582
245
    }
1583
0
    else {
1584
        /* locate first callback and try using it */
1585
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1586
0
    }
1587
1588
245
    if (dev && dev->cb) {
1589
0
        wc_CryptoInfo cryptoInfo;
1590
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1591
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
1592
0
        cryptoInfo.cipher.type = WC_CIPHER_DES3;
1593
0
        cryptoInfo.cipher.enc = 1;
1594
0
        cryptoInfo.cipher.des3.des = des3;
1595
0
        cryptoInfo.cipher.des3.out = out;
1596
0
        cryptoInfo.cipher.des3.in = in;
1597
0
        cryptoInfo.cipher.des3.sz = sz;
1598
1599
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1600
0
    }
1601
1602
245
    return wc_CryptoCb_TranslateErrorCode(ret);
1603
245
}
1604
1605
int wc_CryptoCb_Des3Decrypt(Des3* des3, byte* out,
1606
                               const byte* in, word32 sz)
1607
80
{
1608
80
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1609
80
    CryptoCb* dev;
1610
1611
    /* locate registered callback */
1612
80
    if (des3) {
1613
80
        dev = wc_CryptoCb_FindDevice(des3->devId, WC_ALGO_TYPE_CIPHER);
1614
80
    }
1615
0
    else {
1616
        /* locate first callback and try using it */
1617
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1618
0
    }
1619
1620
80
    if (dev && dev->cb) {
1621
0
        wc_CryptoInfo cryptoInfo;
1622
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1623
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
1624
0
        cryptoInfo.cipher.type = WC_CIPHER_DES3;
1625
0
        cryptoInfo.cipher.enc = 0;
1626
0
        cryptoInfo.cipher.des3.des = des3;
1627
0
        cryptoInfo.cipher.des3.out = out;
1628
0
        cryptoInfo.cipher.des3.in = in;
1629
0
        cryptoInfo.cipher.des3.sz = sz;
1630
1631
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1632
0
    }
1633
1634
80
    return wc_CryptoCb_TranslateErrorCode(ret);
1635
80
}
1636
#endif /* !NO_DES3 */
1637
1638
#ifndef NO_SHA
1639
int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in,
1640
    word32 inSz, byte* digest)
1641
0
{
1642
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1643
0
    CryptoCb* dev;
1644
1645
    /* locate registered callback */
1646
0
    if (sha) {
1647
0
        dev = wc_CryptoCb_FindDevice(sha->devId, WC_ALGO_TYPE_HASH);
1648
0
    }
1649
0
    else {
1650
        /* locate first callback and try using it */
1651
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1652
0
    }
1653
1654
0
    if (dev && dev->cb) {
1655
0
        wc_CryptoInfo cryptoInfo;
1656
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1657
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
1658
0
        cryptoInfo.hash.type = WC_HASH_TYPE_SHA;
1659
0
        cryptoInfo.hash.sha1 = sha;
1660
0
        cryptoInfo.hash.in = in;
1661
0
        cryptoInfo.hash.inSz = inSz;
1662
0
        cryptoInfo.hash.digest = digest;
1663
1664
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1665
0
    }
1666
1667
0
    return wc_CryptoCb_TranslateErrorCode(ret);
1668
0
}
1669
#endif /* !NO_SHA */
1670
1671
#ifdef WOLFSSL_SHA224
1672
int wc_CryptoCb_Sha224Hash(wc_Sha224* sha224, const byte* in,
1673
    word32 inSz, byte* digest)
1674
3.38k
{
1675
3.38k
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1676
3.38k
    CryptoCb* dev;
1677
1678
    /* locate registered callback */
1679
3.38k
    if (sha224) {
1680
3.38k
        dev = wc_CryptoCb_FindDevice(sha224->devId, WC_ALGO_TYPE_HASH);
1681
3.38k
    }
1682
0
    else {
1683
        /* locate first callback and try using it */
1684
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1685
0
    }
1686
1687
3.38k
    if (dev && dev->cb) {
1688
0
        wc_CryptoInfo cryptoInfo;
1689
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1690
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
1691
0
        cryptoInfo.hash.type = WC_HASH_TYPE_SHA224;
1692
0
        cryptoInfo.hash.sha224 = sha224;
1693
0
        cryptoInfo.hash.in = in;
1694
0
        cryptoInfo.hash.inSz = inSz;
1695
0
        cryptoInfo.hash.digest = digest;
1696
1697
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1698
0
    }
1699
1700
3.38k
    return wc_CryptoCb_TranslateErrorCode(ret);
1701
3.38k
}
1702
#endif /* WOLFSSL_SHA224 */
1703
1704
1705
#ifndef NO_SHA256
1706
int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in,
1707
    word32 inSz, byte* digest)
1708
4.49k
{
1709
4.49k
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1710
4.49k
    CryptoCb* dev;
1711
1712
    /* locate registered callback */
1713
4.49k
    if (sha256) {
1714
4.49k
        dev = wc_CryptoCb_FindDevice(sha256->devId, WC_ALGO_TYPE_HASH);
1715
4.49k
    }
1716
0
    else {
1717
        /* locate first callback and try using it */
1718
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1719
0
    }
1720
1721
4.49k
    if (dev && dev->cb) {
1722
1.20k
        wc_CryptoInfo cryptoInfo;
1723
1.20k
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1724
1.20k
        cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
1725
1.20k
        cryptoInfo.hash.type = WC_HASH_TYPE_SHA256;
1726
1.20k
        cryptoInfo.hash.sha256 = sha256;
1727
1.20k
        cryptoInfo.hash.in = in;
1728
1.20k
        cryptoInfo.hash.inSz = inSz;
1729
1.20k
        cryptoInfo.hash.digest = digest;
1730
1731
1.20k
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1732
1.20k
    }
1733
1734
4.49k
    return wc_CryptoCb_TranslateErrorCode(ret);
1735
4.49k
}
1736
#endif /* !NO_SHA256 */
1737
1738
#ifdef WOLFSSL_SHA384
1739
int wc_CryptoCb_Sha384Hash(wc_Sha384* sha384, const byte* in,
1740
    word32 inSz, byte* digest)
1741
1.52k
{
1742
1.52k
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1743
1.52k
    CryptoCb* dev;
1744
1745
    /* locate registered callback */
1746
1.52k
    #ifndef NO_SHA2_CRYPTO_CB
1747
1.52k
    if (sha384) {
1748
1.52k
        dev = wc_CryptoCb_FindDevice(sha384->devId, WC_ALGO_TYPE_HASH);
1749
1.52k
    }
1750
0
    else
1751
0
    #endif
1752
0
    {
1753
        /* locate first callback and try using it */
1754
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1755
0
    }
1756
1757
1.52k
    if (dev && dev->cb) {
1758
0
        wc_CryptoInfo cryptoInfo;
1759
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1760
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
1761
0
        cryptoInfo.hash.type = WC_HASH_TYPE_SHA384;
1762
0
        cryptoInfo.hash.sha384 = sha384;
1763
0
        cryptoInfo.hash.in = in;
1764
0
        cryptoInfo.hash.inSz = inSz;
1765
0
        cryptoInfo.hash.digest = digest;
1766
1767
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1768
0
    }
1769
1770
1.52k
    return wc_CryptoCb_TranslateErrorCode(ret);
1771
1.52k
}
1772
#endif /* WOLFSSL_SHA384 */
1773
1774
#ifdef WOLFSSL_SHA512
1775
int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in,
1776
    word32 inSz, byte* digest, size_t digestSz)
1777
7.33k
{
1778
7.33k
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1779
7.33k
    CryptoCb* dev;
1780
1781
    /* locate registered callback */
1782
7.33k
    #ifndef NO_SHA2_CRYPTO_CB
1783
7.33k
    if (sha512) {
1784
7.33k
        dev = wc_CryptoCb_FindDevice(sha512->devId, WC_ALGO_TYPE_HASH);
1785
7.33k
    }
1786
0
    else
1787
0
    #endif
1788
0
    {
1789
        /* locate first callback and try using it */
1790
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1791
0
    }
1792
1793
7.33k
    if (dev && dev->cb) {
1794
0
        byte localHash[WC_SHA512_DIGEST_SIZE];
1795
0
        wc_CryptoInfo cryptoInfo;
1796
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1797
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
1798
0
        cryptoInfo.hash.sha512 = sha512;
1799
0
        cryptoInfo.hash.in = in;
1800
0
        cryptoInfo.hash.inSz = inSz;
1801
0
        cryptoInfo.hash.digest = digest;
1802
1803
        /* try the specific family callbacks first */
1804
0
#if !defined(WOLFSSL_NOSHA512_224)
1805
0
        if (digest != NULL && digestSz == WC_SHA512_224_DIGEST_SIZE) {
1806
0
          cryptoInfo.hash.type = WC_HASH_TYPE_SHA512_224;
1807
0
          ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1808
0
          ret = wc_CryptoCb_TranslateErrorCode(ret);
1809
0
          if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
1810
0
            return ret;
1811
0
        }
1812
0
#endif
1813
0
#if !defined(WOLFSSL_NOSHA512_256)
1814
0
        if (digest != NULL && digestSz == WC_SHA512_256_DIGEST_SIZE) {
1815
0
          cryptoInfo.hash.type = WC_HASH_TYPE_SHA512_256;
1816
0
          ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1817
0
          ret = wc_CryptoCb_TranslateErrorCode(ret);
1818
0
          if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
1819
0
            return ret;
1820
0
        }
1821
0
#endif
1822
0
        cryptoInfo.hash.type = WC_HASH_TYPE_SHA512;
1823
        /* use local buffer if not full size */
1824
0
        if (digest != NULL && digestSz != WC_SHA512_DIGEST_SIZE)
1825
0
            cryptoInfo.hash.digest = localHash;
1826
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1827
0
        ret = wc_CryptoCb_TranslateErrorCode(ret);
1828
0
        if (ret == 0 && digest != NULL && digestSz != WC_SHA512_DIGEST_SIZE)
1829
0
            XMEMCPY(digest, localHash, digestSz);
1830
0
        return ret;
1831
0
    }
1832
1833
7.33k
    return wc_CryptoCb_TranslateErrorCode(ret);
1834
7.33k
}
1835
#endif /* WOLFSSL_SHA512 */
1836
1837
#if defined(WOLFSSL_SHA3) && (!defined(HAVE_FIPS) || FIPS_VERSION_GE(6, 0))
1838
int wc_CryptoCb_Sha3Hash(wc_Sha3* sha3, int type, const byte* in,
1839
    word32 inSz, byte* digest)
1840
0
{
1841
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1842
0
    CryptoCb* dev;
1843
1844
    /* locate registered callback */
1845
0
    if (sha3) {
1846
0
        dev = wc_CryptoCb_FindDevice(sha3->devId, WC_ALGO_TYPE_HASH);
1847
0
    }
1848
0
    else
1849
0
    {
1850
        /* locate first callback and try using it */
1851
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1852
0
    }
1853
1854
0
    if (dev && dev->cb) {
1855
0
        wc_CryptoInfo cryptoInfo;
1856
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1857
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
1858
0
        cryptoInfo.hash.type = type;
1859
0
        cryptoInfo.hash.sha3 = sha3;
1860
0
        cryptoInfo.hash.in = in;
1861
0
        cryptoInfo.hash.inSz = inSz;
1862
0
        cryptoInfo.hash.digest = digest;
1863
1864
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1865
0
    }
1866
1867
0
    return wc_CryptoCb_TranslateErrorCode(ret);
1868
0
}
1869
#endif /* WOLFSSL_SHA3 && (!HAVE_FIPS || FIPS_VERSION_GE(6, 0)) */
1870
1871
#ifndef NO_HMAC
1872
int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in, word32 inSz,
1873
    byte* digest)
1874
313
{
1875
313
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1876
313
    CryptoCb* dev;
1877
1878
313
    if (hmac == NULL)
1879
0
        return ret;
1880
1881
    /* locate registered callback */
1882
313
    dev = wc_CryptoCb_FindDevice(hmac->devId, WC_ALGO_TYPE_HMAC);
1883
313
    if (dev && dev->cb) {
1884
0
        wc_CryptoInfo cryptoInfo;
1885
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1886
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_HMAC;
1887
0
        cryptoInfo.hmac.macType = macType;
1888
0
        cryptoInfo.hmac.in = in;
1889
0
        cryptoInfo.hmac.inSz = inSz;
1890
0
        cryptoInfo.hmac.digest = digest;
1891
0
        cryptoInfo.hmac.hmac = hmac;
1892
1893
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1894
0
    }
1895
1896
313
    return wc_CryptoCb_TranslateErrorCode(ret);
1897
313
}
1898
#endif /* !NO_HMAC */
1899
1900
#ifndef WC_NO_RNG
1901
int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz)
1902
24.1k
{
1903
24.1k
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1904
24.1k
    CryptoCb* dev;
1905
1906
    /* locate registered callback */
1907
24.1k
    if (rng) {
1908
24.1k
        dev = wc_CryptoCb_FindDevice(rng->devId, WC_ALGO_TYPE_RNG);
1909
24.1k
    }
1910
0
    else {
1911
        /* locate first callback and try using it */
1912
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1913
0
    }
1914
1915
24.1k
    if (dev && dev->cb) {
1916
24.1k
        wc_CryptoInfo cryptoInfo;
1917
24.1k
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1918
24.1k
        cryptoInfo.algo_type = WC_ALGO_TYPE_RNG;
1919
24.1k
        cryptoInfo.rng.rng = rng;
1920
24.1k
        cryptoInfo.rng.out = out;
1921
24.1k
        cryptoInfo.rng.sz = sz;
1922
1923
24.1k
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1924
24.1k
    }
1925
1926
24.1k
    return wc_CryptoCb_TranslateErrorCode(ret);
1927
24.1k
}
1928
1929
int wc_CryptoCb_RandomSeed(OS_Seed* os, byte* seed, word32 sz)
1930
12
{
1931
12
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1932
12
    CryptoCb* dev;
1933
1934
    /* locate registered callback */
1935
12
    dev = wc_CryptoCb_FindDevice(os->devId, WC_ALGO_TYPE_SEED);
1936
12
    if (dev && dev->cb) {
1937
12
        wc_CryptoInfo cryptoInfo;
1938
12
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1939
12
        cryptoInfo.algo_type = WC_ALGO_TYPE_SEED;
1940
12
        cryptoInfo.seed.os = os;
1941
12
        cryptoInfo.seed.seed = seed;
1942
12
        cryptoInfo.seed.sz = sz;
1943
1944
12
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1945
12
    }
1946
1947
12
    return wc_CryptoCb_TranslateErrorCode(ret);
1948
12
}
1949
#endif /* !WC_NO_RNG */
1950
1951
#ifndef NO_CERTS
1952
int wc_CryptoCb_GetCert(int devId, const char *label, word32 labelLen,
1953
                        const byte *id, word32 idLen, byte** out,
1954
                        word32* outSz, int *format, void *heap)
1955
0
{
1956
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1957
0
    CryptoCb* dev;
1958
1959
    /* locate registered callback */
1960
0
    dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_CERT);
1961
0
    if (dev && dev->cb) {
1962
0
        wc_CryptoInfo cryptoInfo;
1963
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
1964
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_CERT;
1965
0
        cryptoInfo.cert.label = label;
1966
0
        cryptoInfo.cert.labelLen = labelLen;
1967
0
        cryptoInfo.cert.id = id;
1968
0
        cryptoInfo.cert.idLen = idLen;
1969
0
        cryptoInfo.cert.heap = heap;
1970
0
        cryptoInfo.cert.certDataOut = out;
1971
0
        cryptoInfo.cert.certSz = outSz;
1972
0
        cryptoInfo.cert.certFormatOut = format;
1973
0
        cryptoInfo.cert.heap = heap;
1974
1975
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
1976
0
    }
1977
1978
0
    return wc_CryptoCb_TranslateErrorCode(ret);
1979
0
}
1980
#endif /* ifndef NO_CERTS */
1981
1982
#if defined(WOLFSSL_CMAC)
1983
int wc_CryptoCb_Cmac(Cmac* cmac, const byte* key, word32 keySz,
1984
        const byte* in, word32 inSz, byte* out, word32* outSz, int type,
1985
        void* ctx)
1986
0
{
1987
0
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
1988
0
    CryptoCb* dev;
1989
1990
    /* locate registered callback */
1991
0
    if (cmac) {
1992
0
        dev = wc_CryptoCb_FindDevice(cmac->devId, WC_ALGO_TYPE_CMAC);
1993
0
    }
1994
0
    else {
1995
        /* locate first callback and try using it */
1996
0
        dev = wc_CryptoCb_FindDeviceByIndex(0);
1997
0
    }
1998
0
    if (dev && dev->cb) {
1999
0
        wc_CryptoInfo cryptoInfo;
2000
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
2001
0
        cryptoInfo.algo_type = WC_ALGO_TYPE_CMAC;
2002
2003
0
        cryptoInfo.cmac.cmac  = cmac;
2004
0
        cryptoInfo.cmac.ctx   = ctx;
2005
0
        cryptoInfo.cmac.key   = key;
2006
0
        cryptoInfo.cmac.in    = in;
2007
0
        cryptoInfo.cmac.out   = out;
2008
0
        cryptoInfo.cmac.outSz = outSz;
2009
0
        cryptoInfo.cmac.keySz = keySz;
2010
0
        cryptoInfo.cmac.inSz  = inSz;
2011
0
        cryptoInfo.cmac.type  = type;
2012
2013
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
2014
0
    }
2015
2016
0
    return wc_CryptoCb_TranslateErrorCode(ret);
2017
0
}
2018
#endif /* WOLFSSL_CMAC */
2019
2020
/* returns the default dev id for the current build */
2021
int wc_CryptoCb_DefaultDevID(void)
2022
92.1k
{
2023
92.1k
    int ret;
2024
2025
/* Explicitly disable the "default devId" behavior. Ensures that any devId
2026
 * will only be used if explicitly passed as an argument to crypto functions,
2027
 * and never automatically selected. */
2028
#ifdef WC_NO_DEFAULT_DEVID
2029
    ret = INVALID_DEVID;
2030
#else
2031
    /* conditional macro selection based on build */
2032
#ifdef WOLFSSL_CAAM_DEVID
2033
    ret = WOLFSSL_CAAM_DEVID;
2034
#elif defined(HAVE_ARIA)
2035
    ret = WOLFSSL_ARIA_DEVID;
2036
#elif defined(WC_USE_DEVID)
2037
    ret = WC_USE_DEVID;
2038
#else
2039
    /* try first available */
2040
92.1k
    ret = wc_CryptoCb_GetDevIdAtIndex(0);
2041
92.1k
#endif
2042
92.1k
#endif /* WC_NO_DEFAULT_DEVID */
2043
2044
92.1k
    return ret;
2045
92.1k
}
2046
2047
#if defined(HAVE_HKDF) && !defined(NO_HMAC)
2048
int wc_CryptoCb_Hkdf(int hashType, const byte* inKey, word32 inKeySz,
2049
                     const byte* salt, word32 saltSz, const byte* info,
2050
                     word32 infoSz, byte* out, word32 outSz, int devId)
2051
0
{
2052
0
    int       ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
2053
0
    CryptoCb* dev;
2054
2055
    /* Find registered callback device */
2056
0
    dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KDF);
2057
2058
0
    if (dev && dev->cb) {
2059
0
        wc_CryptoInfo cryptoInfo;
2060
0
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
2061
2062
0
        cryptoInfo.algo_type         = WC_ALGO_TYPE_KDF;
2063
0
        cryptoInfo.kdf.type          = WC_KDF_TYPE_HKDF;
2064
0
        cryptoInfo.kdf.hkdf.hashType = hashType;
2065
0
        cryptoInfo.kdf.hkdf.inKey    = inKey;
2066
0
        cryptoInfo.kdf.hkdf.inKeySz  = inKeySz;
2067
0
        cryptoInfo.kdf.hkdf.salt     = salt;
2068
0
        cryptoInfo.kdf.hkdf.saltSz   = saltSz;
2069
0
        cryptoInfo.kdf.hkdf.info     = info;
2070
0
        cryptoInfo.kdf.hkdf.infoSz   = infoSz;
2071
0
        cryptoInfo.kdf.hkdf.out      = out;
2072
0
        cryptoInfo.kdf.hkdf.outSz    = outSz;
2073
2074
0
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
2075
0
    }
2076
2077
0
    return wc_CryptoCb_TranslateErrorCode(ret);
2078
0
}
2079
#endif /* HAVE_HKDF && !NO_HMAC */
2080
2081
#ifdef WOLF_CRYPTO_CB_COPY
2082
/* General copy callback function for algorithm structures
2083
 * devId: The device ID to use for the callback
2084
 * algo: Algorithm type (enum wc_AlgoType) - WC_ALGO_TYPE_HASH,
2085
 *       WC_ALGO_TYPE_CIPHER, etc
2086
 * type: Specific type - for HASH: enum wc_HashType, for CIPHER:
2087
 *       enum wc_CipherType
2088
 * src: Pointer to source structure
2089
 * dst: Pointer to destination structure
2090
 * Returns: 0 on success, negative on error, CRYPTOCB_UNAVAILABLE if not
2091
 *          handled
2092
 */
2093
int wc_CryptoCb_Copy(int devId, int algo, int type, void* src, void* dst)
2094
{
2095
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
2096
    CryptoCb* dev;
2097
2098
    /* Find registered callback device */
2099
    dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_COPY);
2100
    if (dev && dev->cb) {
2101
        wc_CryptoInfo cryptoInfo;
2102
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
2103
        cryptoInfo.algo_type = WC_ALGO_TYPE_COPY;
2104
        cryptoInfo.copy.algo = algo;
2105
        cryptoInfo.copy.type = type;
2106
        cryptoInfo.copy.src = src;
2107
        cryptoInfo.copy.dst = dst;
2108
2109
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
2110
    }
2111
2112
    return wc_CryptoCb_TranslateErrorCode(ret);
2113
}
2114
#endif /* WOLF_CRYPTO_CB_COPY */
2115
2116
#ifdef WOLF_CRYPTO_CB_FREE
2117
/* General free callback function for algorithm structures
2118
 * devId: The device ID to use for the callback
2119
 * algo: Algorithm type (enum wc_AlgoType) - WC_ALGO_TYPE_HASH,
2120
 *       WC_ALGO_TYPE_CIPHER, etc
2121
 * type: Specific type - for HASH: enum wc_HashType, for CIPHER:
2122
 *       enum wc_CipherType
2123
 * obj: Pointer to object structure to free
2124
 * Returns: 0 on success, negative on error, CRYPTOCB_UNAVAILABLE if not
2125
 *          handled
2126
 */
2127
int wc_CryptoCb_Free(int devId, int algo, int type, void* obj)
2128
{
2129
    int ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
2130
    CryptoCb* dev;
2131
2132
    /* Find registered callback device */
2133
    dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_FREE);
2134
    if (dev && dev->cb) {
2135
        wc_CryptoInfo cryptoInfo;
2136
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
2137
        cryptoInfo.algo_type = WC_ALGO_TYPE_FREE;
2138
        cryptoInfo.free.algo = algo;
2139
        cryptoInfo.free.type = type;
2140
        cryptoInfo.free.obj = obj;
2141
2142
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
2143
    }
2144
2145
    return wc_CryptoCb_TranslateErrorCode(ret);
2146
}
2147
#endif /* WOLF_CRYPTO_CB_FREE */
2148
2149
2150
#if defined(HAVE_CMAC_KDF)
2151
/* Crypto callback for NIST SP 800 56C two-step CMAC KDF. See software
2152
 * implementation in wc_KDA_KDF_twostep_cmac for more comments.
2153
 * */
2154
int wc_CryptoCb_Kdf_TwostepCmac(const byte * salt, word32 saltSz,
2155
                                const byte* z, word32 zSz,
2156
                                const byte* fixedInfo, word32 fixedInfoSz,
2157
                                byte* output, word32 outputSz, int devId)
2158
{
2159
    int       ret = WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE);
2160
    CryptoCb* dev;
2161
2162
    /* Find registered callback device */
2163
    dev = wc_CryptoCb_FindDevice(devId, WC_ALGO_TYPE_KDF);
2164
2165
    if (dev && dev->cb) {
2166
        wc_CryptoInfo cryptoInfo;
2167
        XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
2168
2169
        cryptoInfo.algo_type                    = WC_ALGO_TYPE_KDF;
2170
        cryptoInfo.kdf.type                     = WC_KDF_TYPE_TWOSTEP_CMAC;
2171
        cryptoInfo.kdf.twostep_cmac.salt        = salt;
2172
        cryptoInfo.kdf.twostep_cmac.saltSz      = saltSz;
2173
        cryptoInfo.kdf.twostep_cmac.z           = z;
2174
        cryptoInfo.kdf.twostep_cmac.zSz         = zSz;
2175
        cryptoInfo.kdf.twostep_cmac.fixedInfo   = fixedInfo;
2176
        cryptoInfo.kdf.twostep_cmac.fixedInfoSz = fixedInfoSz;
2177
        cryptoInfo.kdf.twostep_cmac.out         = output;
2178
        cryptoInfo.kdf.twostep_cmac.outSz       = outputSz;
2179
2180
        ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
2181
    }
2182
2183
    return wc_CryptoCb_TranslateErrorCode(ret);
2184
}
2185
#endif /* HAVE_CMAC_KDF */
2186
2187
#endif /* WOLF_CRYPTO_CB */