Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more
wolfcrypt/src/cryptocb.c@17:a5f916481144, 2020-06-05 (annotated)
- Committer:
- wolfSSL
- Date:
- Fri Jun 05 00:11:07 2020 +0000
- Revision:
- 17:a5f916481144
- Parent:
- 16:8e0d178b1d1e
wolfSSL 4.4.0
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| wolfSSL | 16:8e0d178b1d1e | 1 | /* cryptocb.c |
| wolfSSL | 16:8e0d178b1d1e | 2 | * |
| wolfSSL | 16:8e0d178b1d1e | 3 | * Copyright (C) 2006-2020 wolfSSL Inc. |
| wolfSSL | 16:8e0d178b1d1e | 4 | * |
| wolfSSL | 16:8e0d178b1d1e | 5 | * This file is part of wolfSSL. |
| wolfSSL | 16:8e0d178b1d1e | 6 | * |
| wolfSSL | 16:8e0d178b1d1e | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
| wolfSSL | 16:8e0d178b1d1e | 8 | * it under the terms of the GNU General Public License as published by |
| wolfSSL | 16:8e0d178b1d1e | 9 | * the Free Software Foundation; either version 3 of the License, or |
| wolfSSL | 16:8e0d178b1d1e | 10 | * (at your option) any later version. |
| wolfSSL | 16:8e0d178b1d1e | 11 | * |
| wolfSSL | 16:8e0d178b1d1e | 12 | * wolfSSL is distributed in the hope that it will be useful, |
| wolfSSL | 16:8e0d178b1d1e | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| wolfSSL | 16:8e0d178b1d1e | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| wolfSSL | 16:8e0d178b1d1e | 15 | * GNU General Public License for more details. |
| wolfSSL | 16:8e0d178b1d1e | 16 | * |
| wolfSSL | 16:8e0d178b1d1e | 17 | * You should have received a copy of the GNU General Public License |
| wolfSSL | 16:8e0d178b1d1e | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| wolfSSL | 16:8e0d178b1d1e | 19 | */ |
| wolfSSL | 16:8e0d178b1d1e | 20 | |
| wolfSSL | 16:8e0d178b1d1e | 21 | /* This framework provides a central place for crypto hardware integration |
| wolfSSL | 16:8e0d178b1d1e | 22 | using the devId scheme. If not supported return `CRYPTOCB_UNAVAILABLE`. */ |
| wolfSSL | 16:8e0d178b1d1e | 23 | |
| wolfSSL | 16:8e0d178b1d1e | 24 | #ifdef HAVE_CONFIG_H |
| wolfSSL | 16:8e0d178b1d1e | 25 | #include <config.h> |
| wolfSSL | 16:8e0d178b1d1e | 26 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 27 | |
| wolfSSL | 16:8e0d178b1d1e | 28 | #include <wolfssl/wolfcrypt/settings.h> |
| wolfSSL | 16:8e0d178b1d1e | 29 | |
| wolfSSL | 16:8e0d178b1d1e | 30 | #ifdef WOLF_CRYPTO_CB |
| wolfSSL | 16:8e0d178b1d1e | 31 | |
| wolfSSL | 16:8e0d178b1d1e | 32 | #include <wolfssl/wolfcrypt/cryptocb.h> |
| wolfSSL | 16:8e0d178b1d1e | 33 | #include <wolfssl/wolfcrypt/error-crypt.h> |
| wolfSSL | 16:8e0d178b1d1e | 34 | #include <wolfssl/wolfcrypt/logging.h> |
| wolfSSL | 16:8e0d178b1d1e | 35 | |
| wolfSSL | 16:8e0d178b1d1e | 36 | |
| wolfSSL | 16:8e0d178b1d1e | 37 | /* TODO: Consider linked list with mutex */ |
| wolfSSL | 16:8e0d178b1d1e | 38 | #ifndef MAX_CRYPTO_DEVID_CALLBACKS |
| wolfSSL | 16:8e0d178b1d1e | 39 | #define MAX_CRYPTO_DEVID_CALLBACKS 8 |
| wolfSSL | 16:8e0d178b1d1e | 40 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 41 | |
| wolfSSL | 16:8e0d178b1d1e | 42 | typedef struct CryptoCb { |
| wolfSSL | 16:8e0d178b1d1e | 43 | int devId; |
| wolfSSL | 16:8e0d178b1d1e | 44 | CryptoDevCallbackFunc cb; |
| wolfSSL | 16:8e0d178b1d1e | 45 | void* ctx; |
| wolfSSL | 16:8e0d178b1d1e | 46 | } CryptoCb; |
| wolfSSL | 16:8e0d178b1d1e | 47 | static WOLFSSL_GLOBAL CryptoCb gCryptoDev[MAX_CRYPTO_DEVID_CALLBACKS]; |
| wolfSSL | 16:8e0d178b1d1e | 48 | |
| wolfSSL | 16:8e0d178b1d1e | 49 | static CryptoCb* wc_CryptoCb_FindDevice(int devId) |
| wolfSSL | 16:8e0d178b1d1e | 50 | { |
| wolfSSL | 16:8e0d178b1d1e | 51 | int i; |
| wolfSSL | 16:8e0d178b1d1e | 52 | for (i=0; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
| wolfSSL | 16:8e0d178b1d1e | 53 | if (gCryptoDev[i].devId == devId) |
| wolfSSL | 16:8e0d178b1d1e | 54 | return &gCryptoDev[i]; |
| wolfSSL | 16:8e0d178b1d1e | 55 | } |
| wolfSSL | 16:8e0d178b1d1e | 56 | return NULL; |
| wolfSSL | 16:8e0d178b1d1e | 57 | } |
| wolfSSL | 16:8e0d178b1d1e | 58 | static CryptoCb* wc_CryptoCb_FindDeviceByIndex(int startIdx) |
| wolfSSL | 16:8e0d178b1d1e | 59 | { |
| wolfSSL | 16:8e0d178b1d1e | 60 | int i; |
| wolfSSL | 16:8e0d178b1d1e | 61 | for (i=startIdx; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
| wolfSSL | 16:8e0d178b1d1e | 62 | if (gCryptoDev[i].devId != INVALID_DEVID) |
| wolfSSL | 16:8e0d178b1d1e | 63 | return &gCryptoDev[i]; |
| wolfSSL | 16:8e0d178b1d1e | 64 | } |
| wolfSSL | 16:8e0d178b1d1e | 65 | return NULL; |
| wolfSSL | 16:8e0d178b1d1e | 66 | } |
| wolfSSL | 16:8e0d178b1d1e | 67 | |
| wolfSSL | 16:8e0d178b1d1e | 68 | static WC_INLINE int wc_CryptoCb_TranslateErrorCode(int ret) |
| wolfSSL | 16:8e0d178b1d1e | 69 | { |
| wolfSSL | 16:8e0d178b1d1e | 70 | if (ret == NOT_COMPILED_IN) { |
| wolfSSL | 16:8e0d178b1d1e | 71 | /* backwards compatibility for older NOT_COMPILED_IN syntax */ |
| wolfSSL | 16:8e0d178b1d1e | 72 | ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 73 | } |
| wolfSSL | 16:8e0d178b1d1e | 74 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 75 | } |
| wolfSSL | 16:8e0d178b1d1e | 76 | |
| wolfSSL | 16:8e0d178b1d1e | 77 | void wc_CryptoCb_Init(void) |
| wolfSSL | 16:8e0d178b1d1e | 78 | { |
| wolfSSL | 16:8e0d178b1d1e | 79 | int i; |
| wolfSSL | 16:8e0d178b1d1e | 80 | for (i=0; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) { |
| wolfSSL | 16:8e0d178b1d1e | 81 | gCryptoDev[i].devId = INVALID_DEVID; |
| wolfSSL | 16:8e0d178b1d1e | 82 | } |
| wolfSSL | 16:8e0d178b1d1e | 83 | } |
| wolfSSL | 16:8e0d178b1d1e | 84 | |
| wolfSSL | 16:8e0d178b1d1e | 85 | int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx) |
| wolfSSL | 16:8e0d178b1d1e | 86 | { |
| wolfSSL | 16:8e0d178b1d1e | 87 | /* find existing or new */ |
| wolfSSL | 16:8e0d178b1d1e | 88 | CryptoCb* dev = wc_CryptoCb_FindDevice(devId); |
| wolfSSL | 16:8e0d178b1d1e | 89 | if (dev == NULL) |
| wolfSSL | 16:8e0d178b1d1e | 90 | dev = wc_CryptoCb_FindDevice(INVALID_DEVID); |
| wolfSSL | 16:8e0d178b1d1e | 91 | |
| wolfSSL | 16:8e0d178b1d1e | 92 | if (dev == NULL) |
| wolfSSL | 16:8e0d178b1d1e | 93 | return BUFFER_E; /* out of devices */ |
| wolfSSL | 16:8e0d178b1d1e | 94 | |
| wolfSSL | 16:8e0d178b1d1e | 95 | dev->devId = devId; |
| wolfSSL | 16:8e0d178b1d1e | 96 | dev->cb = cb; |
| wolfSSL | 16:8e0d178b1d1e | 97 | dev->ctx = ctx; |
| wolfSSL | 16:8e0d178b1d1e | 98 | |
| wolfSSL | 16:8e0d178b1d1e | 99 | return 0; |
| wolfSSL | 16:8e0d178b1d1e | 100 | } |
| wolfSSL | 16:8e0d178b1d1e | 101 | |
| wolfSSL | 16:8e0d178b1d1e | 102 | void wc_CryptoCb_UnRegisterDevice(int devId) |
| wolfSSL | 16:8e0d178b1d1e | 103 | { |
| wolfSSL | 16:8e0d178b1d1e | 104 | CryptoCb* dev = wc_CryptoCb_FindDevice(devId); |
| wolfSSL | 16:8e0d178b1d1e | 105 | if (dev) { |
| wolfSSL | 16:8e0d178b1d1e | 106 | XMEMSET(dev, 0, sizeof(*dev)); |
| wolfSSL | 16:8e0d178b1d1e | 107 | dev->devId = INVALID_DEVID; |
| wolfSSL | 16:8e0d178b1d1e | 108 | } |
| wolfSSL | 16:8e0d178b1d1e | 109 | } |
| wolfSSL | 16:8e0d178b1d1e | 110 | |
| wolfSSL | 16:8e0d178b1d1e | 111 | #ifndef NO_RSA |
| wolfSSL | 16:8e0d178b1d1e | 112 | int wc_CryptoCb_Rsa(const byte* in, word32 inLen, byte* out, |
| wolfSSL | 16:8e0d178b1d1e | 113 | word32* outLen, int type, RsaKey* key, WC_RNG* rng) |
| wolfSSL | 16:8e0d178b1d1e | 114 | { |
| wolfSSL | 16:8e0d178b1d1e | 115 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 116 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 117 | |
| wolfSSL | 16:8e0d178b1d1e | 118 | if (key == NULL) |
| wolfSSL | 16:8e0d178b1d1e | 119 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 120 | |
| wolfSSL | 16:8e0d178b1d1e | 121 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 122 | dev = wc_CryptoCb_FindDevice(key->devId); |
| wolfSSL | 16:8e0d178b1d1e | 123 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 124 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 125 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 126 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
| wolfSSL | 16:8e0d178b1d1e | 127 | cryptoInfo.pk.type = WC_PK_TYPE_RSA; |
| wolfSSL | 16:8e0d178b1d1e | 128 | cryptoInfo.pk.rsa.in = in; |
| wolfSSL | 16:8e0d178b1d1e | 129 | cryptoInfo.pk.rsa.inLen = inLen; |
| wolfSSL | 16:8e0d178b1d1e | 130 | cryptoInfo.pk.rsa.out = out; |
| wolfSSL | 16:8e0d178b1d1e | 131 | cryptoInfo.pk.rsa.outLen = outLen; |
| wolfSSL | 16:8e0d178b1d1e | 132 | cryptoInfo.pk.rsa.type = type; |
| wolfSSL | 16:8e0d178b1d1e | 133 | cryptoInfo.pk.rsa.key = key; |
| wolfSSL | 16:8e0d178b1d1e | 134 | cryptoInfo.pk.rsa.rng = rng; |
| wolfSSL | 16:8e0d178b1d1e | 135 | |
| wolfSSL | 16:8e0d178b1d1e | 136 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 137 | } |
| wolfSSL | 16:8e0d178b1d1e | 138 | |
| wolfSSL | 16:8e0d178b1d1e | 139 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 140 | } |
| wolfSSL | 16:8e0d178b1d1e | 141 | |
| wolfSSL | 16:8e0d178b1d1e | 142 | #ifdef WOLFSSL_KEY_GEN |
| wolfSSL | 16:8e0d178b1d1e | 143 | int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng) |
| wolfSSL | 16:8e0d178b1d1e | 144 | { |
| wolfSSL | 16:8e0d178b1d1e | 145 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 146 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 147 | |
| wolfSSL | 16:8e0d178b1d1e | 148 | if (key == NULL) |
| wolfSSL | 16:8e0d178b1d1e | 149 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 150 | |
| wolfSSL | 16:8e0d178b1d1e | 151 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 152 | dev = wc_CryptoCb_FindDevice(key->devId); |
| wolfSSL | 16:8e0d178b1d1e | 153 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 154 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 155 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 156 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
| wolfSSL | 16:8e0d178b1d1e | 157 | cryptoInfo.pk.type = WC_PK_TYPE_RSA_KEYGEN; |
| wolfSSL | 16:8e0d178b1d1e | 158 | cryptoInfo.pk.rsakg.key = key; |
| wolfSSL | 16:8e0d178b1d1e | 159 | cryptoInfo.pk.rsakg.size = size; |
| wolfSSL | 16:8e0d178b1d1e | 160 | cryptoInfo.pk.rsakg.e = e; |
| wolfSSL | 16:8e0d178b1d1e | 161 | cryptoInfo.pk.rsakg.rng = rng; |
| wolfSSL | 16:8e0d178b1d1e | 162 | |
| wolfSSL | 16:8e0d178b1d1e | 163 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 164 | } |
| wolfSSL | 16:8e0d178b1d1e | 165 | |
| wolfSSL | 16:8e0d178b1d1e | 166 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 167 | } |
| wolfSSL | 16:8e0d178b1d1e | 168 | #endif |
| wolfSSL | 16:8e0d178b1d1e | 169 | #endif /* !NO_RSA */ |
| wolfSSL | 16:8e0d178b1d1e | 170 | |
| wolfSSL | 16:8e0d178b1d1e | 171 | #ifdef HAVE_ECC |
| wolfSSL | 16:8e0d178b1d1e | 172 | int wc_CryptoCb_MakeEccKey(WC_RNG* rng, int keySize, ecc_key* key, int curveId) |
| wolfSSL | 16:8e0d178b1d1e | 173 | { |
| wolfSSL | 16:8e0d178b1d1e | 174 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 175 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 176 | |
| wolfSSL | 16:8e0d178b1d1e | 177 | if (key == NULL) |
| wolfSSL | 16:8e0d178b1d1e | 178 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 179 | |
| wolfSSL | 16:8e0d178b1d1e | 180 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 181 | dev = wc_CryptoCb_FindDevice(key->devId); |
| wolfSSL | 16:8e0d178b1d1e | 182 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 183 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 184 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 185 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
| wolfSSL | 16:8e0d178b1d1e | 186 | cryptoInfo.pk.type = WC_PK_TYPE_EC_KEYGEN; |
| wolfSSL | 16:8e0d178b1d1e | 187 | cryptoInfo.pk.eckg.rng = rng; |
| wolfSSL | 16:8e0d178b1d1e | 188 | cryptoInfo.pk.eckg.size = keySize; |
| wolfSSL | 16:8e0d178b1d1e | 189 | cryptoInfo.pk.eckg.key = key; |
| wolfSSL | 16:8e0d178b1d1e | 190 | cryptoInfo.pk.eckg.curveId = curveId; |
| wolfSSL | 16:8e0d178b1d1e | 191 | |
| wolfSSL | 16:8e0d178b1d1e | 192 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 193 | } |
| wolfSSL | 16:8e0d178b1d1e | 194 | |
| wolfSSL | 16:8e0d178b1d1e | 195 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 196 | } |
| wolfSSL | 16:8e0d178b1d1e | 197 | |
| wolfSSL | 16:8e0d178b1d1e | 198 | int wc_CryptoCb_Ecdh(ecc_key* private_key, ecc_key* public_key, |
| wolfSSL | 16:8e0d178b1d1e | 199 | byte* out, word32* outlen) |
| wolfSSL | 16:8e0d178b1d1e | 200 | { |
| wolfSSL | 16:8e0d178b1d1e | 201 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 202 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 203 | |
| wolfSSL | 16:8e0d178b1d1e | 204 | if (private_key == NULL) |
| wolfSSL | 16:8e0d178b1d1e | 205 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 206 | |
| wolfSSL | 16:8e0d178b1d1e | 207 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 208 | dev = wc_CryptoCb_FindDevice(private_key->devId); |
| wolfSSL | 16:8e0d178b1d1e | 209 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 210 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 211 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 212 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
| wolfSSL | 16:8e0d178b1d1e | 213 | cryptoInfo.pk.type = WC_PK_TYPE_ECDH; |
| wolfSSL | 16:8e0d178b1d1e | 214 | cryptoInfo.pk.ecdh.private_key = private_key; |
| wolfSSL | 16:8e0d178b1d1e | 215 | cryptoInfo.pk.ecdh.public_key = public_key; |
| wolfSSL | 16:8e0d178b1d1e | 216 | cryptoInfo.pk.ecdh.out = out; |
| wolfSSL | 16:8e0d178b1d1e | 217 | cryptoInfo.pk.ecdh.outlen = outlen; |
| wolfSSL | 16:8e0d178b1d1e | 218 | |
| wolfSSL | 16:8e0d178b1d1e | 219 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 220 | } |
| wolfSSL | 16:8e0d178b1d1e | 221 | |
| wolfSSL | 16:8e0d178b1d1e | 222 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 223 | } |
| wolfSSL | 16:8e0d178b1d1e | 224 | |
| wolfSSL | 16:8e0d178b1d1e | 225 | int wc_CryptoCb_EccSign(const byte* in, word32 inlen, byte* out, |
| wolfSSL | 16:8e0d178b1d1e | 226 | word32 *outlen, WC_RNG* rng, ecc_key* key) |
| wolfSSL | 16:8e0d178b1d1e | 227 | { |
| wolfSSL | 16:8e0d178b1d1e | 228 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 229 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 230 | |
| wolfSSL | 16:8e0d178b1d1e | 231 | if (key == NULL) |
| wolfSSL | 16:8e0d178b1d1e | 232 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 233 | |
| wolfSSL | 16:8e0d178b1d1e | 234 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 235 | dev = wc_CryptoCb_FindDevice(key->devId); |
| wolfSSL | 16:8e0d178b1d1e | 236 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 237 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 238 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 239 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
| wolfSSL | 16:8e0d178b1d1e | 240 | cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_SIGN; |
| wolfSSL | 16:8e0d178b1d1e | 241 | cryptoInfo.pk.eccsign.in = in; |
| wolfSSL | 16:8e0d178b1d1e | 242 | cryptoInfo.pk.eccsign.inlen = inlen; |
| wolfSSL | 16:8e0d178b1d1e | 243 | cryptoInfo.pk.eccsign.out = out; |
| wolfSSL | 16:8e0d178b1d1e | 244 | cryptoInfo.pk.eccsign.outlen = outlen; |
| wolfSSL | 16:8e0d178b1d1e | 245 | cryptoInfo.pk.eccsign.rng = rng; |
| wolfSSL | 16:8e0d178b1d1e | 246 | cryptoInfo.pk.eccsign.key = key; |
| wolfSSL | 16:8e0d178b1d1e | 247 | |
| wolfSSL | 16:8e0d178b1d1e | 248 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 249 | } |
| wolfSSL | 16:8e0d178b1d1e | 250 | |
| wolfSSL | 16:8e0d178b1d1e | 251 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 252 | } |
| wolfSSL | 16:8e0d178b1d1e | 253 | |
| wolfSSL | 16:8e0d178b1d1e | 254 | int wc_CryptoCb_EccVerify(const byte* sig, word32 siglen, |
| wolfSSL | 16:8e0d178b1d1e | 255 | const byte* hash, word32 hashlen, int* res, ecc_key* key) |
| wolfSSL | 16:8e0d178b1d1e | 256 | { |
| wolfSSL | 16:8e0d178b1d1e | 257 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 258 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 259 | |
| wolfSSL | 16:8e0d178b1d1e | 260 | if (key == NULL) |
| wolfSSL | 16:8e0d178b1d1e | 261 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 262 | |
| wolfSSL | 16:8e0d178b1d1e | 263 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 264 | dev = wc_CryptoCb_FindDevice(key->devId); |
| wolfSSL | 16:8e0d178b1d1e | 265 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 266 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 267 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 268 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
| wolfSSL | 16:8e0d178b1d1e | 269 | cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_VERIFY; |
| wolfSSL | 16:8e0d178b1d1e | 270 | cryptoInfo.pk.eccverify.sig = sig; |
| wolfSSL | 16:8e0d178b1d1e | 271 | cryptoInfo.pk.eccverify.siglen = siglen; |
| wolfSSL | 16:8e0d178b1d1e | 272 | cryptoInfo.pk.eccverify.hash = hash; |
| wolfSSL | 16:8e0d178b1d1e | 273 | cryptoInfo.pk.eccverify.hashlen = hashlen; |
| wolfSSL | 16:8e0d178b1d1e | 274 | cryptoInfo.pk.eccverify.res = res; |
| wolfSSL | 16:8e0d178b1d1e | 275 | cryptoInfo.pk.eccverify.key = key; |
| wolfSSL | 16:8e0d178b1d1e | 276 | |
| wolfSSL | 16:8e0d178b1d1e | 277 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 278 | } |
| wolfSSL | 16:8e0d178b1d1e | 279 | |
| wolfSSL | 16:8e0d178b1d1e | 280 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 281 | } |
| wolfSSL | 16:8e0d178b1d1e | 282 | #endif /* HAVE_ECC */ |
| wolfSSL | 16:8e0d178b1d1e | 283 | |
| wolfSSL | 16:8e0d178b1d1e | 284 | #ifndef NO_AES |
| wolfSSL | 16:8e0d178b1d1e | 285 | #ifdef HAVE_AESGCM |
| wolfSSL | 16:8e0d178b1d1e | 286 | int wc_CryptoCb_AesGcmEncrypt(Aes* aes, byte* out, |
| wolfSSL | 16:8e0d178b1d1e | 287 | const byte* in, word32 sz, |
| wolfSSL | 16:8e0d178b1d1e | 288 | const byte* iv, word32 ivSz, |
| wolfSSL | 16:8e0d178b1d1e | 289 | byte* authTag, word32 authTagSz, |
| wolfSSL | 16:8e0d178b1d1e | 290 | const byte* authIn, word32 authInSz) |
| wolfSSL | 16:8e0d178b1d1e | 291 | { |
| wolfSSL | 16:8e0d178b1d1e | 292 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 293 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 294 | |
| wolfSSL | 16:8e0d178b1d1e | 295 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 296 | if (aes) { |
| wolfSSL | 16:8e0d178b1d1e | 297 | dev = wc_CryptoCb_FindDevice(aes->devId); |
| wolfSSL | 16:8e0d178b1d1e | 298 | } |
| wolfSSL | 16:8e0d178b1d1e | 299 | else { |
| wolfSSL | 16:8e0d178b1d1e | 300 | /* locate first callback and try using it */ |
| wolfSSL | 16:8e0d178b1d1e | 301 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
| wolfSSL | 16:8e0d178b1d1e | 302 | } |
| wolfSSL | 16:8e0d178b1d1e | 303 | |
| wolfSSL | 16:8e0d178b1d1e | 304 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 305 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 306 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 307 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
| wolfSSL | 16:8e0d178b1d1e | 308 | cryptoInfo.cipher.type = WC_CIPHER_AES_GCM; |
| wolfSSL | 16:8e0d178b1d1e | 309 | cryptoInfo.cipher.enc = 1; |
| wolfSSL | 16:8e0d178b1d1e | 310 | cryptoInfo.cipher.aesgcm_enc.aes = aes; |
| wolfSSL | 16:8e0d178b1d1e | 311 | cryptoInfo.cipher.aesgcm_enc.out = out; |
| wolfSSL | 16:8e0d178b1d1e | 312 | cryptoInfo.cipher.aesgcm_enc.in = in; |
| wolfSSL | 16:8e0d178b1d1e | 313 | cryptoInfo.cipher.aesgcm_enc.sz = sz; |
| wolfSSL | 16:8e0d178b1d1e | 314 | cryptoInfo.cipher.aesgcm_enc.iv = iv; |
| wolfSSL | 16:8e0d178b1d1e | 315 | cryptoInfo.cipher.aesgcm_enc.ivSz = ivSz; |
| wolfSSL | 16:8e0d178b1d1e | 316 | cryptoInfo.cipher.aesgcm_enc.authTag = authTag; |
| wolfSSL | 16:8e0d178b1d1e | 317 | cryptoInfo.cipher.aesgcm_enc.authTagSz = authTagSz; |
| wolfSSL | 16:8e0d178b1d1e | 318 | cryptoInfo.cipher.aesgcm_enc.authIn = authIn; |
| wolfSSL | 16:8e0d178b1d1e | 319 | cryptoInfo.cipher.aesgcm_enc.authInSz = authInSz; |
| wolfSSL | 16:8e0d178b1d1e | 320 | |
| wolfSSL | 16:8e0d178b1d1e | 321 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 322 | } |
| wolfSSL | 16:8e0d178b1d1e | 323 | |
| wolfSSL | 16:8e0d178b1d1e | 324 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 325 | } |
| wolfSSL | 16:8e0d178b1d1e | 326 | |
| wolfSSL | 16:8e0d178b1d1e | 327 | int wc_CryptoCb_AesGcmDecrypt(Aes* aes, byte* out, |
| wolfSSL | 16:8e0d178b1d1e | 328 | const byte* in, word32 sz, |
| wolfSSL | 16:8e0d178b1d1e | 329 | const byte* iv, word32 ivSz, |
| wolfSSL | 16:8e0d178b1d1e | 330 | const byte* authTag, word32 authTagSz, |
| wolfSSL | 16:8e0d178b1d1e | 331 | const byte* authIn, word32 authInSz) |
| wolfSSL | 16:8e0d178b1d1e | 332 | { |
| wolfSSL | 16:8e0d178b1d1e | 333 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 334 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 335 | |
| wolfSSL | 16:8e0d178b1d1e | 336 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 337 | if (aes) { |
| wolfSSL | 16:8e0d178b1d1e | 338 | dev = wc_CryptoCb_FindDevice(aes->devId); |
| wolfSSL | 16:8e0d178b1d1e | 339 | } |
| wolfSSL | 16:8e0d178b1d1e | 340 | else { |
| wolfSSL | 16:8e0d178b1d1e | 341 | /* locate first callback and try using it */ |
| wolfSSL | 16:8e0d178b1d1e | 342 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
| wolfSSL | 16:8e0d178b1d1e | 343 | } |
| wolfSSL | 16:8e0d178b1d1e | 344 | |
| wolfSSL | 16:8e0d178b1d1e | 345 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 346 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 347 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 348 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
| wolfSSL | 16:8e0d178b1d1e | 349 | cryptoInfo.cipher.type = WC_CIPHER_AES_GCM; |
| wolfSSL | 16:8e0d178b1d1e | 350 | cryptoInfo.cipher.enc = 0; |
| wolfSSL | 16:8e0d178b1d1e | 351 | cryptoInfo.cipher.aesgcm_dec.aes = aes; |
| wolfSSL | 16:8e0d178b1d1e | 352 | cryptoInfo.cipher.aesgcm_dec.out = out; |
| wolfSSL | 16:8e0d178b1d1e | 353 | cryptoInfo.cipher.aesgcm_dec.in = in; |
| wolfSSL | 16:8e0d178b1d1e | 354 | cryptoInfo.cipher.aesgcm_dec.sz = sz; |
| wolfSSL | 16:8e0d178b1d1e | 355 | cryptoInfo.cipher.aesgcm_dec.iv = iv; |
| wolfSSL | 16:8e0d178b1d1e | 356 | cryptoInfo.cipher.aesgcm_dec.ivSz = ivSz; |
| wolfSSL | 16:8e0d178b1d1e | 357 | cryptoInfo.cipher.aesgcm_dec.authTag = authTag; |
| wolfSSL | 16:8e0d178b1d1e | 358 | cryptoInfo.cipher.aesgcm_dec.authTagSz = authTagSz; |
| wolfSSL | 16:8e0d178b1d1e | 359 | cryptoInfo.cipher.aesgcm_dec.authIn = authIn; |
| wolfSSL | 16:8e0d178b1d1e | 360 | cryptoInfo.cipher.aesgcm_dec.authInSz = authInSz; |
| wolfSSL | 16:8e0d178b1d1e | 361 | |
| wolfSSL | 16:8e0d178b1d1e | 362 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 363 | } |
| wolfSSL | 16:8e0d178b1d1e | 364 | |
| wolfSSL | 16:8e0d178b1d1e | 365 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 366 | } |
| wolfSSL | 16:8e0d178b1d1e | 367 | #endif /* HAVE_AESGCM */ |
| wolfSSL | 16:8e0d178b1d1e | 368 | |
| wolfSSL | 16:8e0d178b1d1e | 369 | #ifdef HAVE_AES_CBC |
| wolfSSL | 16:8e0d178b1d1e | 370 | int wc_CryptoCb_AesCbcEncrypt(Aes* aes, byte* out, |
| wolfSSL | 16:8e0d178b1d1e | 371 | const byte* in, word32 sz) |
| wolfSSL | 16:8e0d178b1d1e | 372 | { |
| wolfSSL | 16:8e0d178b1d1e | 373 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 374 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 375 | |
| wolfSSL | 16:8e0d178b1d1e | 376 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 377 | if (aes) { |
| wolfSSL | 16:8e0d178b1d1e | 378 | dev = wc_CryptoCb_FindDevice(aes->devId); |
| wolfSSL | 16:8e0d178b1d1e | 379 | } |
| wolfSSL | 16:8e0d178b1d1e | 380 | else { |
| wolfSSL | 16:8e0d178b1d1e | 381 | /* locate first callback and try using it */ |
| wolfSSL | 16:8e0d178b1d1e | 382 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
| wolfSSL | 16:8e0d178b1d1e | 383 | } |
| wolfSSL | 16:8e0d178b1d1e | 384 | |
| wolfSSL | 16:8e0d178b1d1e | 385 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 386 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 387 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 388 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
| wolfSSL | 16:8e0d178b1d1e | 389 | cryptoInfo.cipher.type = WC_CIPHER_AES_CBC; |
| wolfSSL | 16:8e0d178b1d1e | 390 | cryptoInfo.cipher.enc = 1; |
| wolfSSL | 16:8e0d178b1d1e | 391 | cryptoInfo.cipher.aescbc.aes = aes; |
| wolfSSL | 16:8e0d178b1d1e | 392 | cryptoInfo.cipher.aescbc.out = out; |
| wolfSSL | 16:8e0d178b1d1e | 393 | cryptoInfo.cipher.aescbc.in = in; |
| wolfSSL | 16:8e0d178b1d1e | 394 | cryptoInfo.cipher.aescbc.sz = sz; |
| wolfSSL | 16:8e0d178b1d1e | 395 | |
| wolfSSL | 16:8e0d178b1d1e | 396 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 397 | } |
| wolfSSL | 16:8e0d178b1d1e | 398 | |
| wolfSSL | 16:8e0d178b1d1e | 399 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 400 | } |
| wolfSSL | 16:8e0d178b1d1e | 401 | |
| wolfSSL | 16:8e0d178b1d1e | 402 | int wc_CryptoCb_AesCbcDecrypt(Aes* aes, byte* out, |
| wolfSSL | 16:8e0d178b1d1e | 403 | const byte* in, word32 sz) |
| wolfSSL | 16:8e0d178b1d1e | 404 | { |
| wolfSSL | 16:8e0d178b1d1e | 405 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 406 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 407 | |
| wolfSSL | 16:8e0d178b1d1e | 408 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 409 | if (aes) { |
| wolfSSL | 16:8e0d178b1d1e | 410 | dev = wc_CryptoCb_FindDevice(aes->devId); |
| wolfSSL | 16:8e0d178b1d1e | 411 | } |
| wolfSSL | 16:8e0d178b1d1e | 412 | else { |
| wolfSSL | 16:8e0d178b1d1e | 413 | /* locate first callback and try using it */ |
| wolfSSL | 16:8e0d178b1d1e | 414 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
| wolfSSL | 16:8e0d178b1d1e | 415 | } |
| wolfSSL | 16:8e0d178b1d1e | 416 | |
| wolfSSL | 16:8e0d178b1d1e | 417 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 418 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 419 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 420 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
| wolfSSL | 16:8e0d178b1d1e | 421 | cryptoInfo.cipher.type = WC_CIPHER_AES_CBC; |
| wolfSSL | 16:8e0d178b1d1e | 422 | cryptoInfo.cipher.enc = 0; |
| wolfSSL | 16:8e0d178b1d1e | 423 | cryptoInfo.cipher.aescbc.aes = aes; |
| wolfSSL | 16:8e0d178b1d1e | 424 | cryptoInfo.cipher.aescbc.out = out; |
| wolfSSL | 16:8e0d178b1d1e | 425 | cryptoInfo.cipher.aescbc.in = in; |
| wolfSSL | 16:8e0d178b1d1e | 426 | cryptoInfo.cipher.aescbc.sz = sz; |
| wolfSSL | 16:8e0d178b1d1e | 427 | |
| wolfSSL | 16:8e0d178b1d1e | 428 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 429 | } |
| wolfSSL | 16:8e0d178b1d1e | 430 | |
| wolfSSL | 16:8e0d178b1d1e | 431 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 432 | } |
| wolfSSL | 16:8e0d178b1d1e | 433 | #endif /* HAVE_AES_CBC */ |
| wolfSSL | 16:8e0d178b1d1e | 434 | #endif /* !NO_AES */ |
| wolfSSL | 16:8e0d178b1d1e | 435 | |
| wolfSSL | 16:8e0d178b1d1e | 436 | #ifndef NO_DES3 |
| wolfSSL | 16:8e0d178b1d1e | 437 | int wc_CryptoCb_Des3Encrypt(Des3* des3, byte* out, |
| wolfSSL | 16:8e0d178b1d1e | 438 | const byte* in, word32 sz) |
| wolfSSL | 16:8e0d178b1d1e | 439 | { |
| wolfSSL | 16:8e0d178b1d1e | 440 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 441 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 442 | |
| wolfSSL | 16:8e0d178b1d1e | 443 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 444 | if (des3) { |
| wolfSSL | 16:8e0d178b1d1e | 445 | dev = wc_CryptoCb_FindDevice(des3->devId); |
| wolfSSL | 16:8e0d178b1d1e | 446 | } |
| wolfSSL | 16:8e0d178b1d1e | 447 | else { |
| wolfSSL | 16:8e0d178b1d1e | 448 | /* locate first callback and try using it */ |
| wolfSSL | 16:8e0d178b1d1e | 449 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
| wolfSSL | 16:8e0d178b1d1e | 450 | } |
| wolfSSL | 16:8e0d178b1d1e | 451 | |
| wolfSSL | 16:8e0d178b1d1e | 452 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 453 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 454 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 455 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
| wolfSSL | 16:8e0d178b1d1e | 456 | cryptoInfo.cipher.type = WC_CIPHER_DES3; |
| wolfSSL | 16:8e0d178b1d1e | 457 | cryptoInfo.cipher.enc = 1; |
| wolfSSL | 16:8e0d178b1d1e | 458 | cryptoInfo.cipher.des3.des = des3; |
| wolfSSL | 16:8e0d178b1d1e | 459 | cryptoInfo.cipher.des3.out = out; |
| wolfSSL | 16:8e0d178b1d1e | 460 | cryptoInfo.cipher.des3.in = in; |
| wolfSSL | 16:8e0d178b1d1e | 461 | cryptoInfo.cipher.des3.sz = sz; |
| wolfSSL | 16:8e0d178b1d1e | 462 | |
| wolfSSL | 16:8e0d178b1d1e | 463 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 464 | } |
| wolfSSL | 16:8e0d178b1d1e | 465 | |
| wolfSSL | 16:8e0d178b1d1e | 466 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 467 | } |
| wolfSSL | 16:8e0d178b1d1e | 468 | |
| wolfSSL | 16:8e0d178b1d1e | 469 | int wc_CryptoCb_Des3Decrypt(Des3* des3, byte* out, |
| wolfSSL | 16:8e0d178b1d1e | 470 | const byte* in, word32 sz) |
| wolfSSL | 16:8e0d178b1d1e | 471 | { |
| wolfSSL | 16:8e0d178b1d1e | 472 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 473 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 474 | |
| wolfSSL | 16:8e0d178b1d1e | 475 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 476 | if (des3) { |
| wolfSSL | 16:8e0d178b1d1e | 477 | dev = wc_CryptoCb_FindDevice(des3->devId); |
| wolfSSL | 16:8e0d178b1d1e | 478 | } |
| wolfSSL | 16:8e0d178b1d1e | 479 | else { |
| wolfSSL | 16:8e0d178b1d1e | 480 | /* locate first callback and try using it */ |
| wolfSSL | 16:8e0d178b1d1e | 481 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
| wolfSSL | 16:8e0d178b1d1e | 482 | } |
| wolfSSL | 16:8e0d178b1d1e | 483 | |
| wolfSSL | 16:8e0d178b1d1e | 484 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 485 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 486 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 487 | cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER; |
| wolfSSL | 16:8e0d178b1d1e | 488 | cryptoInfo.cipher.type = WC_CIPHER_DES3; |
| wolfSSL | 16:8e0d178b1d1e | 489 | cryptoInfo.cipher.enc = 0; |
| wolfSSL | 16:8e0d178b1d1e | 490 | cryptoInfo.cipher.des3.des = des3; |
| wolfSSL | 16:8e0d178b1d1e | 491 | cryptoInfo.cipher.des3.out = out; |
| wolfSSL | 16:8e0d178b1d1e | 492 | cryptoInfo.cipher.des3.in = in; |
| wolfSSL | 16:8e0d178b1d1e | 493 | cryptoInfo.cipher.des3.sz = sz; |
| wolfSSL | 16:8e0d178b1d1e | 494 | |
| wolfSSL | 16:8e0d178b1d1e | 495 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 496 | } |
| wolfSSL | 16:8e0d178b1d1e | 497 | |
| wolfSSL | 16:8e0d178b1d1e | 498 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 499 | } |
| wolfSSL | 16:8e0d178b1d1e | 500 | #endif /* !NO_DES3 */ |
| wolfSSL | 16:8e0d178b1d1e | 501 | |
| wolfSSL | 16:8e0d178b1d1e | 502 | #ifndef NO_SHA |
| wolfSSL | 16:8e0d178b1d1e | 503 | int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in, |
| wolfSSL | 16:8e0d178b1d1e | 504 | word32 inSz, byte* digest) |
| wolfSSL | 16:8e0d178b1d1e | 505 | { |
| wolfSSL | 16:8e0d178b1d1e | 506 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 507 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 508 | |
| wolfSSL | 16:8e0d178b1d1e | 509 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 510 | if (sha) { |
| wolfSSL | 16:8e0d178b1d1e | 511 | dev = wc_CryptoCb_FindDevice(sha->devId); |
| wolfSSL | 16:8e0d178b1d1e | 512 | } |
| wolfSSL | 16:8e0d178b1d1e | 513 | else { |
| wolfSSL | 16:8e0d178b1d1e | 514 | /* locate first callback and try using it */ |
| wolfSSL | 16:8e0d178b1d1e | 515 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
| wolfSSL | 16:8e0d178b1d1e | 516 | } |
| wolfSSL | 16:8e0d178b1d1e | 517 | |
| wolfSSL | 16:8e0d178b1d1e | 518 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 519 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 520 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 521 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
| wolfSSL | 16:8e0d178b1d1e | 522 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA; |
| wolfSSL | 16:8e0d178b1d1e | 523 | cryptoInfo.hash.sha1 = sha; |
| wolfSSL | 16:8e0d178b1d1e | 524 | cryptoInfo.hash.in = in; |
| wolfSSL | 16:8e0d178b1d1e | 525 | cryptoInfo.hash.inSz = inSz; |
| wolfSSL | 16:8e0d178b1d1e | 526 | cryptoInfo.hash.digest = digest; |
| wolfSSL | 16:8e0d178b1d1e | 527 | |
| wolfSSL | 16:8e0d178b1d1e | 528 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 529 | } |
| wolfSSL | 16:8e0d178b1d1e | 530 | |
| wolfSSL | 16:8e0d178b1d1e | 531 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 532 | } |
| wolfSSL | 16:8e0d178b1d1e | 533 | #endif /* !NO_SHA */ |
| wolfSSL | 16:8e0d178b1d1e | 534 | |
| wolfSSL | 16:8e0d178b1d1e | 535 | #ifndef NO_SHA256 |
| wolfSSL | 16:8e0d178b1d1e | 536 | int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in, |
| wolfSSL | 16:8e0d178b1d1e | 537 | word32 inSz, byte* digest) |
| wolfSSL | 16:8e0d178b1d1e | 538 | { |
| wolfSSL | 16:8e0d178b1d1e | 539 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 540 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 541 | |
| wolfSSL | 16:8e0d178b1d1e | 542 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 543 | if (sha256) { |
| wolfSSL | 16:8e0d178b1d1e | 544 | dev = wc_CryptoCb_FindDevice(sha256->devId); |
| wolfSSL | 16:8e0d178b1d1e | 545 | } |
| wolfSSL | 16:8e0d178b1d1e | 546 | else { |
| wolfSSL | 16:8e0d178b1d1e | 547 | /* locate first callback and try using it */ |
| wolfSSL | 16:8e0d178b1d1e | 548 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
| wolfSSL | 16:8e0d178b1d1e | 549 | } |
| wolfSSL | 16:8e0d178b1d1e | 550 | |
| wolfSSL | 16:8e0d178b1d1e | 551 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 552 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 553 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 554 | cryptoInfo.algo_type = WC_ALGO_TYPE_HASH; |
| wolfSSL | 16:8e0d178b1d1e | 555 | cryptoInfo.hash.type = WC_HASH_TYPE_SHA256; |
| wolfSSL | 16:8e0d178b1d1e | 556 | cryptoInfo.hash.sha256 = sha256; |
| wolfSSL | 16:8e0d178b1d1e | 557 | cryptoInfo.hash.in = in; |
| wolfSSL | 16:8e0d178b1d1e | 558 | cryptoInfo.hash.inSz = inSz; |
| wolfSSL | 16:8e0d178b1d1e | 559 | cryptoInfo.hash.digest = digest; |
| wolfSSL | 16:8e0d178b1d1e | 560 | |
| wolfSSL | 16:8e0d178b1d1e | 561 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 562 | } |
| wolfSSL | 16:8e0d178b1d1e | 563 | |
| wolfSSL | 16:8e0d178b1d1e | 564 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 565 | } |
| wolfSSL | 16:8e0d178b1d1e | 566 | #endif /* !NO_SHA256 */ |
| wolfSSL | 16:8e0d178b1d1e | 567 | |
| wolfSSL | 16:8e0d178b1d1e | 568 | #ifndef NO_HMAC |
| wolfSSL | 16:8e0d178b1d1e | 569 | int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in, word32 inSz, |
| wolfSSL | 16:8e0d178b1d1e | 570 | byte* digest) |
| wolfSSL | 16:8e0d178b1d1e | 571 | { |
| wolfSSL | 16:8e0d178b1d1e | 572 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 573 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 574 | |
| wolfSSL | 16:8e0d178b1d1e | 575 | if (hmac == NULL) |
| wolfSSL | 16:8e0d178b1d1e | 576 | return ret; |
| wolfSSL | 16:8e0d178b1d1e | 577 | |
| wolfSSL | 16:8e0d178b1d1e | 578 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 579 | dev = wc_CryptoCb_FindDevice(hmac->devId); |
| wolfSSL | 16:8e0d178b1d1e | 580 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 581 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 582 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 583 | cryptoInfo.algo_type = WC_ALGO_TYPE_HMAC; |
| wolfSSL | 16:8e0d178b1d1e | 584 | cryptoInfo.hmac.macType = macType; |
| wolfSSL | 16:8e0d178b1d1e | 585 | cryptoInfo.hmac.in = in; |
| wolfSSL | 16:8e0d178b1d1e | 586 | cryptoInfo.hmac.inSz = inSz; |
| wolfSSL | 16:8e0d178b1d1e | 587 | cryptoInfo.hmac.digest = digest; |
| wolfSSL | 16:8e0d178b1d1e | 588 | cryptoInfo.hmac.hmac = hmac; |
| wolfSSL | 16:8e0d178b1d1e | 589 | |
| wolfSSL | 16:8e0d178b1d1e | 590 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 591 | } |
| wolfSSL | 16:8e0d178b1d1e | 592 | |
| wolfSSL | 16:8e0d178b1d1e | 593 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 594 | } |
| wolfSSL | 16:8e0d178b1d1e | 595 | #endif /* !NO_HMAC */ |
| wolfSSL | 16:8e0d178b1d1e | 596 | |
| wolfSSL | 16:8e0d178b1d1e | 597 | #ifndef WC_NO_RNG |
| wolfSSL | 16:8e0d178b1d1e | 598 | int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz) |
| wolfSSL | 16:8e0d178b1d1e | 599 | { |
| wolfSSL | 16:8e0d178b1d1e | 600 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 601 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 602 | |
| wolfSSL | 16:8e0d178b1d1e | 603 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 604 | if (rng) { |
| wolfSSL | 16:8e0d178b1d1e | 605 | dev = wc_CryptoCb_FindDevice(rng->devId); |
| wolfSSL | 16:8e0d178b1d1e | 606 | } |
| wolfSSL | 16:8e0d178b1d1e | 607 | else { |
| wolfSSL | 16:8e0d178b1d1e | 608 | /* locate first callback and try using it */ |
| wolfSSL | 16:8e0d178b1d1e | 609 | dev = wc_CryptoCb_FindDeviceByIndex(0); |
| wolfSSL | 16:8e0d178b1d1e | 610 | } |
| wolfSSL | 16:8e0d178b1d1e | 611 | |
| wolfSSL | 16:8e0d178b1d1e | 612 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 613 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 614 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 615 | cryptoInfo.algo_type = WC_ALGO_TYPE_RNG; |
| wolfSSL | 16:8e0d178b1d1e | 616 | cryptoInfo.rng.rng = rng; |
| wolfSSL | 16:8e0d178b1d1e | 617 | cryptoInfo.rng.out = out; |
| wolfSSL | 16:8e0d178b1d1e | 618 | cryptoInfo.rng.sz = sz; |
| wolfSSL | 16:8e0d178b1d1e | 619 | |
| wolfSSL | 16:8e0d178b1d1e | 620 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 621 | } |
| wolfSSL | 16:8e0d178b1d1e | 622 | |
| wolfSSL | 16:8e0d178b1d1e | 623 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 624 | } |
| wolfSSL | 16:8e0d178b1d1e | 625 | |
| wolfSSL | 16:8e0d178b1d1e | 626 | int wc_CryptoCb_RandomSeed(OS_Seed* os, byte* seed, word32 sz) |
| wolfSSL | 16:8e0d178b1d1e | 627 | { |
| wolfSSL | 16:8e0d178b1d1e | 628 | int ret = CRYPTOCB_UNAVAILABLE; |
| wolfSSL | 16:8e0d178b1d1e | 629 | CryptoCb* dev; |
| wolfSSL | 16:8e0d178b1d1e | 630 | |
| wolfSSL | 16:8e0d178b1d1e | 631 | /* locate registered callback */ |
| wolfSSL | 16:8e0d178b1d1e | 632 | dev = wc_CryptoCb_FindDevice(os->devId); |
| wolfSSL | 16:8e0d178b1d1e | 633 | if (dev && dev->cb) { |
| wolfSSL | 16:8e0d178b1d1e | 634 | wc_CryptoInfo cryptoInfo; |
| wolfSSL | 16:8e0d178b1d1e | 635 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
| wolfSSL | 16:8e0d178b1d1e | 636 | cryptoInfo.algo_type = WC_ALGO_TYPE_SEED; |
| wolfSSL | 16:8e0d178b1d1e | 637 | cryptoInfo.seed.os = os; |
| wolfSSL | 16:8e0d178b1d1e | 638 | cryptoInfo.seed.seed = seed; |
| wolfSSL | 16:8e0d178b1d1e | 639 | cryptoInfo.seed.sz = sz; |
| wolfSSL | 16:8e0d178b1d1e | 640 | |
| wolfSSL | 16:8e0d178b1d1e | 641 | ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx); |
| wolfSSL | 16:8e0d178b1d1e | 642 | } |
| wolfSSL | 16:8e0d178b1d1e | 643 | |
| wolfSSL | 16:8e0d178b1d1e | 644 | return wc_CryptoCb_TranslateErrorCode(ret); |
| wolfSSL | 16:8e0d178b1d1e | 645 | } |
| wolfSSL | 16:8e0d178b1d1e | 646 | #endif /* !WC_NO_RNG */ |
| wolfSSL | 16:8e0d178b1d1e | 647 | |
| wolfSSL | 16:8e0d178b1d1e | 648 | #endif /* WOLF_CRYPTO_CB */ |
| wolfSSL | 16:8e0d178b1d1e | 649 |