Coverage Report

Created: 2025-04-11 06:45

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