Coverage Report

Created: 2025-07-23 06:59

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