Xuyi Wang / wolfcrypt

Dependents:   OS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cryptodev.c Source File

cryptodev.c

00001 /* cryptodev.c
00002  *
00003  * Copyright (C) 2006-2018 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 /* This framework provides a central place for crypto hardware integration
00022    using the devId scheme. If not supported return `NOT_COMPILED_IN`. */
00023 
00024 #ifdef HAVE_CONFIG_H
00025     #include <config.h>
00026 #endif
00027 
00028 #include <wolfcrypt/settings.h>
00029 
00030 #ifdef WOLF_CRYPTO_DEV
00031 
00032 #include <wolfcrypt/cryptodev.h>
00033 #include <wolfcrypt/error-crypt.h>
00034 #include <wolfcrypt/logging.h>
00035 
00036 
00037 /* TODO: Consider linked list with mutex */
00038 #ifndef MAX_CRYPTO_DEVICES
00039 #define MAX_CRYPTO_DEVICES 8
00040 #endif
00041 
00042 typedef struct CryptoDev {
00043     int devId;
00044     CryptoDevCallbackFunc cb;
00045     void* ctx;
00046 } CryptoDev;
00047 static CryptoDev gCryptoDev[MAX_CRYPTO_DEVICES];
00048 
00049 static CryptoDev* wc_CryptoDev_FindDevice(int devId)
00050 {
00051     int i;
00052     for (i=0; i<MAX_CRYPTO_DEVICES; i++) {
00053         if (gCryptoDev[i].devId == devId)
00054             return &gCryptoDev[i];
00055     }
00056     return NULL;
00057 }
00058 
00059 void wc_CryptoDev_Init(void)
00060 {
00061     int i;
00062     for (i=0; i<MAX_CRYPTO_DEVICES; i++)
00063         gCryptoDev[i].devId = INVALID_DEVID;
00064 }
00065 
00066 int wc_CryptoDev_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx)
00067 {
00068     /* find existing or new */
00069     CryptoDev* dev = wc_CryptoDev_FindDevice(devId);
00070     if (dev == NULL)
00071         dev = wc_CryptoDev_FindDevice(INVALID_DEVID);
00072 
00073     if (dev == NULL)
00074         return BUFFER_E; /* out of devices */
00075 
00076     dev->devId = devId;
00077     dev->cb = cb;
00078     dev->ctx = ctx;
00079 
00080     return 0;
00081 }
00082 
00083 void wc_CryptoDev_UnRegisterDevice(int devId)
00084 {
00085     CryptoDev* dev = wc_CryptoDev_FindDevice(devId);
00086     if (dev) {
00087         XMEMSET(dev, 0, sizeof(*dev));
00088         dev->devId = INVALID_DEVID;
00089     }
00090 }
00091 
00092 #ifndef NO_RSA
00093 int wc_CryptoDev_Rsa(const byte* in, word32 inLen, byte* out,
00094     word32* outLen, int type, RsaKey* key, WC_RNG* rng)
00095 {
00096     int ret = NOT_COMPILED_IN;
00097     CryptoDev* dev;
00098 
00099     /* locate registered callback */
00100     dev = wc_CryptoDev_FindDevice(key->devId);
00101     if (dev) {
00102         if (dev->cb) {
00103             wc_CryptoInfo cryptoInfo;
00104             XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
00105             cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
00106             cryptoInfo.pk.type = WC_PK_TYPE_RSA;
00107             cryptoInfo.pk.rsa.in = in;
00108             cryptoInfo.pk.rsa.inLen = inLen;
00109             cryptoInfo.pk.rsa.out = out;
00110             cryptoInfo.pk.rsa.outLen = outLen;
00111             cryptoInfo.pk.rsa.type = type;
00112             cryptoInfo.pk.rsa.key = key;
00113             cryptoInfo.pk.rsa.rng = rng;
00114 
00115             ret = dev->cb(key->devId, &cryptoInfo, dev->ctx);
00116         }
00117     }
00118 
00119     return ret;
00120 }
00121 #endif /* !NO_RSA */
00122 
00123 #ifdef HAVE_ECC
00124 int wc_CryptoDev_Ecdh(ecc_key* private_key, ecc_key* public_key,
00125     byte* out, word32* outlen)
00126 {
00127     int ret = NOT_COMPILED_IN;
00128     CryptoDev* dev;
00129 
00130     /* locate registered callback */
00131     dev = wc_CryptoDev_FindDevice(private_key->devId);
00132     if (dev) {
00133         if (dev->cb) {
00134             wc_CryptoInfo cryptoInfo;
00135             XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
00136             cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
00137             cryptoInfo.pk.type = WC_PK_TYPE_ECDH;
00138             cryptoInfo.pk.ecdh.private_key = private_key;
00139             cryptoInfo.pk.ecdh.public_key = public_key;
00140             cryptoInfo.pk.ecdh.out = out;
00141             cryptoInfo.pk.ecdh.outlen = outlen;
00142 
00143             ret = dev->cb(private_key->devId, &cryptoInfo, dev->ctx);
00144         }
00145     }
00146 
00147     return ret;
00148 }
00149 
00150 int wc_CryptoDev_EccSign(const byte* in, word32 inlen, byte* out,
00151     word32 *outlen, WC_RNG* rng, ecc_key* key)
00152 {
00153     int ret = NOT_COMPILED_IN;
00154     CryptoDev* dev;
00155 
00156     /* locate registered callback */
00157     dev = wc_CryptoDev_FindDevice(key->devId);
00158     if (dev) {
00159         if (dev->cb) {
00160             wc_CryptoInfo cryptoInfo;
00161             XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
00162             cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
00163             cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_SIGN;
00164             cryptoInfo.pk.eccsign.in = in;
00165             cryptoInfo.pk.eccsign.inlen = inlen;
00166             cryptoInfo.pk.eccsign.out = out;
00167             cryptoInfo.pk.eccsign.outlen = outlen;
00168             cryptoInfo.pk.eccsign.rng = rng;
00169             cryptoInfo.pk.eccsign.key = key;
00170 
00171             ret = dev->cb(key->devId, &cryptoInfo, dev->ctx);
00172         }
00173     }
00174 
00175     return ret;
00176 }
00177 
00178 int wc_CryptoDev_EccVerify(const byte* sig, word32 siglen,
00179     const byte* hash, word32 hashlen, int* res, ecc_key* key)
00180 {
00181     int ret = NOT_COMPILED_IN;
00182     CryptoDev* dev;
00183 
00184     /* locate registered callback */
00185     dev = wc_CryptoDev_FindDevice(key->devId);
00186     if (dev) {
00187         if (dev->cb) {
00188             wc_CryptoInfo cryptoInfo;
00189             XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
00190             cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
00191             cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_VERIFY;
00192             cryptoInfo.pk.eccverify.sig = sig;
00193             cryptoInfo.pk.eccverify.siglen = siglen;
00194             cryptoInfo.pk.eccverify.hash = hash;
00195             cryptoInfo.pk.eccverify.hashlen = hashlen;
00196             cryptoInfo.pk.eccverify.res = res;
00197             cryptoInfo.pk.eccverify.key = key;
00198 
00199             ret = dev->cb(key->devId, &cryptoInfo, dev->ctx);
00200         }
00201     }
00202 
00203     return ret;
00204 }
00205 #endif /* HAVE_ECC */
00206 
00207 #endif /* WOLF_CRYPTO_DEV */
00208