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.
wolfcrypt/src/cryptodev.c@16:048e5e270a58, 2019-11-19 (annotated)
- Committer:
- sPymbed
- Date:
- Tue Nov 19 14:32:16 2019 +0000
- Revision:
- 16:048e5e270a58
- Parent:
- 14:167253f4e170
working ssl
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 14:167253f4e170 | 1 | /* cryptodev.c |
wolfSSL | 14:167253f4e170 | 2 | * |
wolfSSL | 14:167253f4e170 | 3 | * Copyright (C) 2006-2018 wolfSSL Inc. |
wolfSSL | 14:167253f4e170 | 4 | * |
wolfSSL | 14:167253f4e170 | 5 | * This file is part of wolfSSL. |
wolfSSL | 14:167253f4e170 | 6 | * |
wolfSSL | 14:167253f4e170 | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 14:167253f4e170 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 14:167253f4e170 | 9 | * the Free Software Foundation; either version 3 of the License, or |
wolfSSL | 14:167253f4e170 | 10 | * (at your option) any later version. |
wolfSSL | 14:167253f4e170 | 11 | * |
wolfSSL | 14:167253f4e170 | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 14:167253f4e170 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 14:167253f4e170 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 14:167253f4e170 | 15 | * GNU General Public License for more details. |
wolfSSL | 14:167253f4e170 | 16 | * |
wolfSSL | 14:167253f4e170 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 14:167253f4e170 | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
wolfSSL | 14:167253f4e170 | 19 | */ |
wolfSSL | 14:167253f4e170 | 20 | |
wolfSSL | 14:167253f4e170 | 21 | /* This framework provides a central place for crypto hardware integration |
wolfSSL | 14:167253f4e170 | 22 | using the devId scheme. If not supported return `NOT_COMPILED_IN`. */ |
wolfSSL | 14:167253f4e170 | 23 | |
wolfSSL | 14:167253f4e170 | 24 | #ifdef HAVE_CONFIG_H |
wolfSSL | 14:167253f4e170 | 25 | #include <config.h> |
wolfSSL | 14:167253f4e170 | 26 | #endif |
wolfSSL | 14:167253f4e170 | 27 | |
wolfSSL | 14:167253f4e170 | 28 | #include <wolfssl/wolfcrypt/settings.h> |
wolfSSL | 14:167253f4e170 | 29 | |
wolfSSL | 14:167253f4e170 | 30 | #ifdef WOLF_CRYPTO_DEV |
wolfSSL | 14:167253f4e170 | 31 | |
wolfSSL | 14:167253f4e170 | 32 | #include <wolfssl/wolfcrypt/cryptodev.h> |
wolfSSL | 14:167253f4e170 | 33 | #include <wolfssl/wolfcrypt/error-crypt.h> |
wolfSSL | 14:167253f4e170 | 34 | #include <wolfssl/wolfcrypt/logging.h> |
wolfSSL | 14:167253f4e170 | 35 | |
wolfSSL | 14:167253f4e170 | 36 | |
wolfSSL | 14:167253f4e170 | 37 | /* TODO: Consider linked list with mutex */ |
wolfSSL | 14:167253f4e170 | 38 | #ifndef MAX_CRYPTO_DEVICES |
wolfSSL | 14:167253f4e170 | 39 | #define MAX_CRYPTO_DEVICES 8 |
wolfSSL | 14:167253f4e170 | 40 | #endif |
wolfSSL | 14:167253f4e170 | 41 | |
wolfSSL | 14:167253f4e170 | 42 | typedef struct CryptoDev { |
wolfSSL | 14:167253f4e170 | 43 | int devId; |
wolfSSL | 14:167253f4e170 | 44 | CryptoDevCallbackFunc cb; |
wolfSSL | 14:167253f4e170 | 45 | void* ctx; |
wolfSSL | 14:167253f4e170 | 46 | } CryptoDev; |
wolfSSL | 14:167253f4e170 | 47 | static CryptoDev gCryptoDev[MAX_CRYPTO_DEVICES]; |
wolfSSL | 14:167253f4e170 | 48 | |
wolfSSL | 14:167253f4e170 | 49 | static CryptoDev* wc_CryptoDev_FindDevice(int devId) |
wolfSSL | 14:167253f4e170 | 50 | { |
wolfSSL | 14:167253f4e170 | 51 | int i; |
wolfSSL | 14:167253f4e170 | 52 | for (i=0; i<MAX_CRYPTO_DEVICES; i++) { |
wolfSSL | 14:167253f4e170 | 53 | if (gCryptoDev[i].devId == devId) |
wolfSSL | 14:167253f4e170 | 54 | return &gCryptoDev[i]; |
wolfSSL | 14:167253f4e170 | 55 | } |
wolfSSL | 14:167253f4e170 | 56 | return NULL; |
wolfSSL | 14:167253f4e170 | 57 | } |
wolfSSL | 14:167253f4e170 | 58 | |
wolfSSL | 14:167253f4e170 | 59 | void wc_CryptoDev_Init(void) |
wolfSSL | 14:167253f4e170 | 60 | { |
wolfSSL | 14:167253f4e170 | 61 | int i; |
wolfSSL | 14:167253f4e170 | 62 | for (i=0; i<MAX_CRYPTO_DEVICES; i++) |
wolfSSL | 14:167253f4e170 | 63 | gCryptoDev[i].devId = INVALID_DEVID; |
wolfSSL | 14:167253f4e170 | 64 | } |
wolfSSL | 14:167253f4e170 | 65 | |
wolfSSL | 14:167253f4e170 | 66 | int wc_CryptoDev_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx) |
wolfSSL | 14:167253f4e170 | 67 | { |
wolfSSL | 14:167253f4e170 | 68 | /* find existing or new */ |
wolfSSL | 14:167253f4e170 | 69 | CryptoDev* dev = wc_CryptoDev_FindDevice(devId); |
wolfSSL | 14:167253f4e170 | 70 | if (dev == NULL) |
wolfSSL | 14:167253f4e170 | 71 | dev = wc_CryptoDev_FindDevice(INVALID_DEVID); |
wolfSSL | 14:167253f4e170 | 72 | |
wolfSSL | 14:167253f4e170 | 73 | if (dev == NULL) |
wolfSSL | 14:167253f4e170 | 74 | return BUFFER_E; /* out of devices */ |
wolfSSL | 14:167253f4e170 | 75 | |
wolfSSL | 14:167253f4e170 | 76 | dev->devId = devId; |
wolfSSL | 14:167253f4e170 | 77 | dev->cb = cb; |
wolfSSL | 14:167253f4e170 | 78 | dev->ctx = ctx; |
wolfSSL | 14:167253f4e170 | 79 | |
wolfSSL | 14:167253f4e170 | 80 | return 0; |
wolfSSL | 14:167253f4e170 | 81 | } |
wolfSSL | 14:167253f4e170 | 82 | |
wolfSSL | 14:167253f4e170 | 83 | void wc_CryptoDev_UnRegisterDevice(int devId) |
wolfSSL | 14:167253f4e170 | 84 | { |
wolfSSL | 14:167253f4e170 | 85 | CryptoDev* dev = wc_CryptoDev_FindDevice(devId); |
wolfSSL | 14:167253f4e170 | 86 | if (dev) { |
wolfSSL | 14:167253f4e170 | 87 | XMEMSET(dev, 0, sizeof(*dev)); |
wolfSSL | 14:167253f4e170 | 88 | dev->devId = INVALID_DEVID; |
wolfSSL | 14:167253f4e170 | 89 | } |
wolfSSL | 14:167253f4e170 | 90 | } |
wolfSSL | 14:167253f4e170 | 91 | |
wolfSSL | 14:167253f4e170 | 92 | #ifndef NO_RSA |
wolfSSL | 14:167253f4e170 | 93 | int wc_CryptoDev_Rsa(const byte* in, word32 inLen, byte* out, |
wolfSSL | 14:167253f4e170 | 94 | word32* outLen, int type, RsaKey* key, WC_RNG* rng) |
wolfSSL | 14:167253f4e170 | 95 | { |
wolfSSL | 14:167253f4e170 | 96 | int ret = NOT_COMPILED_IN; |
wolfSSL | 14:167253f4e170 | 97 | CryptoDev* dev; |
wolfSSL | 14:167253f4e170 | 98 | |
wolfSSL | 14:167253f4e170 | 99 | /* locate registered callback */ |
wolfSSL | 14:167253f4e170 | 100 | dev = wc_CryptoDev_FindDevice(key->devId); |
wolfSSL | 14:167253f4e170 | 101 | if (dev) { |
wolfSSL | 14:167253f4e170 | 102 | if (dev->cb) { |
wolfSSL | 14:167253f4e170 | 103 | wc_CryptoInfo cryptoInfo; |
wolfSSL | 14:167253f4e170 | 104 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
wolfSSL | 14:167253f4e170 | 105 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
wolfSSL | 14:167253f4e170 | 106 | cryptoInfo.pk.type = WC_PK_TYPE_RSA; |
wolfSSL | 14:167253f4e170 | 107 | cryptoInfo.pk.rsa.in = in; |
wolfSSL | 14:167253f4e170 | 108 | cryptoInfo.pk.rsa.inLen = inLen; |
wolfSSL | 14:167253f4e170 | 109 | cryptoInfo.pk.rsa.out = out; |
wolfSSL | 14:167253f4e170 | 110 | cryptoInfo.pk.rsa.outLen = outLen; |
wolfSSL | 14:167253f4e170 | 111 | cryptoInfo.pk.rsa.type = type; |
wolfSSL | 14:167253f4e170 | 112 | cryptoInfo.pk.rsa.key = key; |
wolfSSL | 14:167253f4e170 | 113 | cryptoInfo.pk.rsa.rng = rng; |
wolfSSL | 14:167253f4e170 | 114 | |
wolfSSL | 14:167253f4e170 | 115 | ret = dev->cb(key->devId, &cryptoInfo, dev->ctx); |
wolfSSL | 14:167253f4e170 | 116 | } |
wolfSSL | 14:167253f4e170 | 117 | } |
wolfSSL | 14:167253f4e170 | 118 | |
wolfSSL | 14:167253f4e170 | 119 | return ret; |
wolfSSL | 14:167253f4e170 | 120 | } |
wolfSSL | 14:167253f4e170 | 121 | #endif /* !NO_RSA */ |
wolfSSL | 14:167253f4e170 | 122 | |
wolfSSL | 14:167253f4e170 | 123 | #ifdef HAVE_ECC |
wolfSSL | 14:167253f4e170 | 124 | int wc_CryptoDev_Ecdh(ecc_key* private_key, ecc_key* public_key, |
wolfSSL | 14:167253f4e170 | 125 | byte* out, word32* outlen) |
wolfSSL | 14:167253f4e170 | 126 | { |
wolfSSL | 14:167253f4e170 | 127 | int ret = NOT_COMPILED_IN; |
wolfSSL | 14:167253f4e170 | 128 | CryptoDev* dev; |
wolfSSL | 14:167253f4e170 | 129 | |
wolfSSL | 14:167253f4e170 | 130 | /* locate registered callback */ |
wolfSSL | 14:167253f4e170 | 131 | dev = wc_CryptoDev_FindDevice(private_key->devId); |
wolfSSL | 14:167253f4e170 | 132 | if (dev) { |
wolfSSL | 14:167253f4e170 | 133 | if (dev->cb) { |
wolfSSL | 14:167253f4e170 | 134 | wc_CryptoInfo cryptoInfo; |
wolfSSL | 14:167253f4e170 | 135 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
wolfSSL | 14:167253f4e170 | 136 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
wolfSSL | 14:167253f4e170 | 137 | cryptoInfo.pk.type = WC_PK_TYPE_ECDH; |
wolfSSL | 14:167253f4e170 | 138 | cryptoInfo.pk.ecdh.private_key = private_key; |
wolfSSL | 14:167253f4e170 | 139 | cryptoInfo.pk.ecdh.public_key = public_key; |
wolfSSL | 14:167253f4e170 | 140 | cryptoInfo.pk.ecdh.out = out; |
wolfSSL | 14:167253f4e170 | 141 | cryptoInfo.pk.ecdh.outlen = outlen; |
wolfSSL | 14:167253f4e170 | 142 | |
wolfSSL | 14:167253f4e170 | 143 | ret = dev->cb(private_key->devId, &cryptoInfo, dev->ctx); |
wolfSSL | 14:167253f4e170 | 144 | } |
wolfSSL | 14:167253f4e170 | 145 | } |
wolfSSL | 14:167253f4e170 | 146 | |
wolfSSL | 14:167253f4e170 | 147 | return ret; |
wolfSSL | 14:167253f4e170 | 148 | } |
wolfSSL | 14:167253f4e170 | 149 | |
wolfSSL | 14:167253f4e170 | 150 | int wc_CryptoDev_EccSign(const byte* in, word32 inlen, byte* out, |
wolfSSL | 14:167253f4e170 | 151 | word32 *outlen, WC_RNG* rng, ecc_key* key) |
wolfSSL | 14:167253f4e170 | 152 | { |
wolfSSL | 14:167253f4e170 | 153 | int ret = NOT_COMPILED_IN; |
wolfSSL | 14:167253f4e170 | 154 | CryptoDev* dev; |
wolfSSL | 14:167253f4e170 | 155 | |
wolfSSL | 14:167253f4e170 | 156 | /* locate registered callback */ |
wolfSSL | 14:167253f4e170 | 157 | dev = wc_CryptoDev_FindDevice(key->devId); |
wolfSSL | 14:167253f4e170 | 158 | if (dev) { |
wolfSSL | 14:167253f4e170 | 159 | if (dev->cb) { |
wolfSSL | 14:167253f4e170 | 160 | wc_CryptoInfo cryptoInfo; |
wolfSSL | 14:167253f4e170 | 161 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
wolfSSL | 14:167253f4e170 | 162 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
wolfSSL | 14:167253f4e170 | 163 | cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_SIGN; |
wolfSSL | 14:167253f4e170 | 164 | cryptoInfo.pk.eccsign.in = in; |
wolfSSL | 14:167253f4e170 | 165 | cryptoInfo.pk.eccsign.inlen = inlen; |
wolfSSL | 14:167253f4e170 | 166 | cryptoInfo.pk.eccsign.out = out; |
wolfSSL | 14:167253f4e170 | 167 | cryptoInfo.pk.eccsign.outlen = outlen; |
wolfSSL | 14:167253f4e170 | 168 | cryptoInfo.pk.eccsign.rng = rng; |
wolfSSL | 14:167253f4e170 | 169 | cryptoInfo.pk.eccsign.key = key; |
wolfSSL | 14:167253f4e170 | 170 | |
wolfSSL | 14:167253f4e170 | 171 | ret = dev->cb(key->devId, &cryptoInfo, dev->ctx); |
wolfSSL | 14:167253f4e170 | 172 | } |
wolfSSL | 14:167253f4e170 | 173 | } |
wolfSSL | 14:167253f4e170 | 174 | |
wolfSSL | 14:167253f4e170 | 175 | return ret; |
wolfSSL | 14:167253f4e170 | 176 | } |
wolfSSL | 14:167253f4e170 | 177 | |
wolfSSL | 14:167253f4e170 | 178 | int wc_CryptoDev_EccVerify(const byte* sig, word32 siglen, |
wolfSSL | 14:167253f4e170 | 179 | const byte* hash, word32 hashlen, int* res, ecc_key* key) |
wolfSSL | 14:167253f4e170 | 180 | { |
wolfSSL | 14:167253f4e170 | 181 | int ret = NOT_COMPILED_IN; |
wolfSSL | 14:167253f4e170 | 182 | CryptoDev* dev; |
wolfSSL | 14:167253f4e170 | 183 | |
wolfSSL | 14:167253f4e170 | 184 | /* locate registered callback */ |
wolfSSL | 14:167253f4e170 | 185 | dev = wc_CryptoDev_FindDevice(key->devId); |
wolfSSL | 14:167253f4e170 | 186 | if (dev) { |
wolfSSL | 14:167253f4e170 | 187 | if (dev->cb) { |
wolfSSL | 14:167253f4e170 | 188 | wc_CryptoInfo cryptoInfo; |
wolfSSL | 14:167253f4e170 | 189 | XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo)); |
wolfSSL | 14:167253f4e170 | 190 | cryptoInfo.algo_type = WC_ALGO_TYPE_PK; |
wolfSSL | 14:167253f4e170 | 191 | cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_VERIFY; |
wolfSSL | 14:167253f4e170 | 192 | cryptoInfo.pk.eccverify.sig = sig; |
wolfSSL | 14:167253f4e170 | 193 | cryptoInfo.pk.eccverify.siglen = siglen; |
wolfSSL | 14:167253f4e170 | 194 | cryptoInfo.pk.eccverify.hash = hash; |
wolfSSL | 14:167253f4e170 | 195 | cryptoInfo.pk.eccverify.hashlen = hashlen; |
wolfSSL | 14:167253f4e170 | 196 | cryptoInfo.pk.eccverify.res = res; |
wolfSSL | 14:167253f4e170 | 197 | cryptoInfo.pk.eccverify.key = key; |
wolfSSL | 14:167253f4e170 | 198 | |
wolfSSL | 14:167253f4e170 | 199 | ret = dev->cb(key->devId, &cryptoInfo, dev->ctx); |
wolfSSL | 14:167253f4e170 | 200 | } |
wolfSSL | 14:167253f4e170 | 201 | } |
wolfSSL | 14:167253f4e170 | 202 | |
wolfSSL | 14:167253f4e170 | 203 | return ret; |
wolfSSL | 14:167253f4e170 | 204 | } |
wolfSSL | 14:167253f4e170 | 205 | #endif /* HAVE_ECC */ |
wolfSSL | 14:167253f4e170 | 206 | |
wolfSSL | 14:167253f4e170 | 207 | #endif /* WOLF_CRYPTO_DEV */ |
wolfSSL | 14:167253f4e170 | 208 |