yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* cryptocb.h |
| 2 | * |
| 3 | * Copyright (C) 2006-2021 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 | #ifndef _WOLF_CRYPTO_CB_H_ |
| 23 | #define _WOLF_CRYPTO_CB_H_ |
| 24 | |
| 25 | #include <wolfssl/wolfcrypt/types.h> |
| 26 | |
| 27 | #ifdef __cplusplus |
| 28 | extern "C" { |
| 29 | #endif |
| 30 | |
| 31 | /* Defines the Crypto Callback interface version, for compatibility */ |
| 32 | /* Increment this when Crypto Callback interface changes are made */ |
| 33 | #define CRYPTO_CB_VER 2 |
| 34 | |
| 35 | |
| 36 | #ifdef WOLF_CRYPTO_CB |
| 37 | |
| 38 | #ifndef NO_RSA |
| 39 | #include <wolfssl/wolfcrypt/rsa.h> |
| 40 | #endif |
| 41 | #ifdef HAVE_ECC |
| 42 | #include <wolfssl/wolfcrypt/ecc.h> |
| 43 | #endif |
| 44 | #ifndef NO_AES |
| 45 | #include <wolfssl/wolfcrypt/aes.h> |
| 46 | #endif |
| 47 | #ifndef NO_SHA |
| 48 | #include <wolfssl/wolfcrypt/sha.h> |
| 49 | #endif |
| 50 | #ifndef NO_SHA256 |
| 51 | #include <wolfssl/wolfcrypt/sha256.h> |
| 52 | #endif |
| 53 | #ifndef NO_HMAC |
| 54 | #include <wolfssl/wolfcrypt/hmac.h> |
| 55 | #endif |
| 56 | #ifndef WC_NO_RNG |
| 57 | #include <wolfssl/wolfcrypt/random.h> |
| 58 | #endif |
| 59 | #ifndef NO_DES3 |
| 60 | #include <wolfssl/wolfcrypt/des3.h> |
| 61 | #endif |
| 62 | #ifdef WOLFSSL_CMAC |
| 63 | #include <wolfssl/wolfcrypt/cmac.h> |
| 64 | #endif |
| 65 | #ifdef HAVE_ED25519 |
| 66 | #include <wolfssl/wolfcrypt/ed25519.h> |
| 67 | #endif |
| 68 | #ifdef HAVE_CURVE25519 |
| 69 | #include <wolfssl/wolfcrypt/curve25519.h> |
| 70 | #endif |
| 71 | #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384) |
| 72 | #include <wolfssl/wolfcrypt/sha512.h> |
| 73 | #endif |
| 74 | |
| 75 | /* Crypto Information Structure for callbacks */ |
| 76 | typedef struct wc_CryptoInfo { |
| 77 | int algo_type; /* enum wc_AlgoType */ |
| 78 | #if !defined(NO_RSA) || defined(HAVE_ECC) |
| 79 | struct { |
| 80 | int type; /* enum wc_PkType */ |
| 81 | union { |
| 82 | #ifndef NO_RSA |
| 83 | struct { |
| 84 | const byte* in; |
| 85 | word32 inLen; |
| 86 | byte* out; |
| 87 | word32* outLen; |
| 88 | int type; |
| 89 | RsaKey* key; |
| 90 | WC_RNG* rng; |
| 91 | } rsa; |
| 92 | #ifdef WOLFSSL_KEY_GEN |
| 93 | struct { |
| 94 | RsaKey* key; |
| 95 | int size; |
| 96 | long e; |
| 97 | WC_RNG* rng; |
| 98 | } rsakg; |
| 99 | #endif |
| 100 | struct { |
| 101 | RsaKey* key; |
| 102 | const byte* pubKey; |
| 103 | word32 pubKeySz; |
| 104 | } rsa_check; |
| 105 | #endif |
| 106 | #ifdef HAVE_ECC |
| 107 | struct { |
| 108 | WC_RNG* rng; |
| 109 | int size; |
| 110 | ecc_key* key; |
| 111 | int curveId; |
| 112 | } eckg; |
| 113 | struct { |
| 114 | ecc_key* private_key; |
| 115 | ecc_key* public_key; |
| 116 | byte* out; |
| 117 | word32* outlen; |
| 118 | } ecdh; |
| 119 | struct { |
| 120 | const byte* in; |
| 121 | word32 inlen; |
| 122 | byte* out; |
| 123 | word32* outlen; |
| 124 | WC_RNG* rng; |
| 125 | ecc_key* key; |
| 126 | } eccsign; |
| 127 | struct { |
| 128 | const byte* sig; |
| 129 | word32 siglen; |
| 130 | const byte* hash; |
| 131 | word32 hashlen; |
| 132 | int* res; |
| 133 | ecc_key* key; |
| 134 | } eccverify; |
| 135 | struct { |
| 136 | ecc_key* key; |
| 137 | const byte* pubKey; |
| 138 | word32 pubKeySz; |
| 139 | } ecc_check; |
| 140 | #endif |
| 141 | #ifdef HAVE_CURVE25519 |
| 142 | struct { |
| 143 | WC_RNG* rng; |
| 144 | int size; |
| 145 | curve25519_key* key; |
| 146 | int curveId; |
| 147 | } curve25519kg; |
| 148 | struct { |
| 149 | curve25519_key* private_key; |
| 150 | curve25519_key* public_key; |
| 151 | byte* out; |
| 152 | word32* outlen; |
| 153 | int endian; |
| 154 | } curve25519; |
| 155 | #endif |
| 156 | #ifdef HAVE_ED25519 |
| 157 | struct { |
| 158 | WC_RNG* rng; |
| 159 | int size; |
| 160 | ed25519_key* key; |
| 161 | int curveId; |
| 162 | } ed25519kg; |
| 163 | struct { |
| 164 | const byte* in; |
| 165 | word32 inLen; |
| 166 | byte* out; |
| 167 | word32* outLen; |
| 168 | ed25519_key* key; |
| 169 | byte type; |
| 170 | const byte* context; |
| 171 | byte contextLen; |
| 172 | } ed25519sign; |
| 173 | struct { |
| 174 | const byte* sig; |
| 175 | word32 sigLen; |
| 176 | const byte* msg; |
| 177 | word32 msgLen; |
| 178 | int* res; |
| 179 | ed25519_key* key; |
| 180 | byte type; |
| 181 | const byte* context; |
| 182 | byte contextLen; |
| 183 | } ed25519verify; |
| 184 | #endif |
| 185 | }; |
| 186 | } pk; |
| 187 | #endif /* !NO_RSA || HAVE_ECC */ |
| 188 | #if !defined(NO_AES) || !defined(NO_DES3) |
| 189 | struct { |
| 190 | int type; /* enum wc_CipherType */ |
| 191 | int enc; |
| 192 | union { |
| 193 | #ifdef HAVE_AESGCM |
| 194 | struct { |
| 195 | Aes* aes; |
| 196 | byte* out; |
| 197 | const byte* in; |
| 198 | word32 sz; |
| 199 | const byte* iv; |
| 200 | word32 ivSz; |
| 201 | byte* authTag; |
| 202 | word32 authTagSz; |
| 203 | const byte* authIn; |
| 204 | word32 authInSz; |
| 205 | } aesgcm_enc; |
| 206 | struct { |
| 207 | Aes* aes; |
| 208 | byte* out; |
| 209 | const byte* in; |
| 210 | word32 sz; |
| 211 | const byte* iv; |
| 212 | word32 ivSz; |
| 213 | const byte* authTag; |
| 214 | word32 authTagSz; |
| 215 | const byte* authIn; |
| 216 | word32 authInSz; |
| 217 | } aesgcm_dec; |
| 218 | #endif /* HAVE_AESGCM */ |
| 219 | #ifdef HAVE_AES_CBC |
| 220 | struct { |
| 221 | Aes* aes; |
| 222 | byte* out; |
| 223 | const byte* in; |
| 224 | word32 sz; |
| 225 | } aescbc; |
| 226 | #endif /* HAVE_AES_CBC */ |
| 227 | #ifndef NO_DES3 |
| 228 | struct { |
| 229 | Des3* des; |
| 230 | byte* out; |
| 231 | const byte* in; |
| 232 | word32 sz; |
| 233 | } des3; |
| 234 | #endif |
| 235 | }; |
| 236 | } cipher; |
| 237 | #endif /* !NO_AES || !NO_DES3 */ |
| 238 | #if !defined(NO_SHA) || !defined(NO_SHA256) || \ |
| 239 | defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384) |
| 240 | struct { |
| 241 | int type; /* enum wc_HashType */ |
| 242 | const byte* in; |
| 243 | word32 inSz; |
| 244 | byte* digest; |
| 245 | union { |
| 246 | #ifndef NO_SHA |
| 247 | wc_Sha* sha1; |
| 248 | #endif |
| 249 | #ifndef NO_SHA256 |
| 250 | wc_Sha256* sha256; |
| 251 | #endif |
| 252 | #ifdef WOLFSSL_SHA384 |
| 253 | wc_Sha384* sha384; |
| 254 | #endif |
| 255 | #ifdef WOLFSSL_SHA512 |
| 256 | wc_Sha512* sha512; |
| 257 | #endif |
| 258 | }; |
| 259 | } hash; |
| 260 | #endif /* !NO_SHA || !NO_SHA256 */ |
| 261 | #ifndef NO_HMAC |
| 262 | struct { |
| 263 | int macType; /* enum wc_HashType */ |
| 264 | const byte* in; |
| 265 | word32 inSz; |
| 266 | byte* digest; |
| 267 | Hmac* hmac; |
| 268 | } hmac; |
| 269 | #endif |
| 270 | #ifndef WC_NO_RNG |
| 271 | struct { |
| 272 | WC_RNG* rng; |
| 273 | byte* out; |
| 274 | word32 sz; |
| 275 | } rng; |
| 276 | struct { |
| 277 | OS_Seed* os; |
| 278 | byte* seed; |
| 279 | word32 sz; |
| 280 | } seed; |
| 281 | #endif |
| 282 | #ifdef WOLFSSL_CMAC |
| 283 | struct { |
| 284 | Cmac* cmac; |
| 285 | void* ctx; |
| 286 | const byte* key; |
| 287 | const byte* in; |
| 288 | byte* out; |
| 289 | word32* outSz; |
| 290 | word32 keySz; |
| 291 | word32 inSz; |
| 292 | int type; |
| 293 | } cmac; |
| 294 | #endif |
| 295 | } wc_CryptoInfo; |
| 296 | |
| 297 | |
| 298 | typedef int (*CryptoDevCallbackFunc)(int devId, wc_CryptoInfo* info, void* ctx); |
| 299 | |
| 300 | WOLFSSL_LOCAL void wc_CryptoCb_Init(void); |
| 301 | WOLFSSL_LOCAL int wc_CryptoCb_GetDevIdAtIndex(int startIdx); |
| 302 | WOLFSSL_API int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx); |
| 303 | WOLFSSL_API void wc_CryptoCb_UnRegisterDevice(int devId); |
| 304 | |
| 305 | /* old function names */ |
| 306 | #define wc_CryptoDev_RegisterDevice wc_CryptoCb_RegisterDevice |
| 307 | #define wc_CryptoDev_UnRegisterDevice wc_CryptoCb_UnRegisterDevice |
| 308 | |
| 309 | |
| 310 | #ifndef NO_RSA |
| 311 | WOLFSSL_LOCAL int wc_CryptoCb_Rsa(const byte* in, word32 inLen, byte* out, |
| 312 | word32* outLen, int type, RsaKey* key, WC_RNG* rng); |
| 313 | |
| 314 | #ifdef WOLFSSL_KEY_GEN |
| 315 | WOLFSSL_LOCAL int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e, |
| 316 | WC_RNG* rng); |
| 317 | #endif /* WOLFSSL_KEY_GEN */ |
| 318 | |
| 319 | WOLFSSL_LOCAL int wc_CryptoCb_RsaCheckPrivKey(RsaKey* key, const byte* pubKey, |
| 320 | word32 pubKeySz); |
| 321 | #endif /* !NO_RSA */ |
| 322 | |
| 323 | #ifdef HAVE_ECC |
| 324 | WOLFSSL_LOCAL int wc_CryptoCb_MakeEccKey(WC_RNG* rng, int keySize, |
| 325 | ecc_key* key, int curveId); |
| 326 | |
| 327 | WOLFSSL_LOCAL int wc_CryptoCb_Ecdh(ecc_key* private_key, ecc_key* public_key, |
| 328 | byte* out, word32* outlen); |
| 329 | |
| 330 | WOLFSSL_LOCAL int wc_CryptoCb_EccSign(const byte* in, word32 inlen, byte* out, |
| 331 | word32 *outlen, WC_RNG* rng, ecc_key* key); |
| 332 | |
| 333 | WOLFSSL_LOCAL int wc_CryptoCb_EccVerify(const byte* sig, word32 siglen, |
| 334 | const byte* hash, word32 hashlen, int* res, ecc_key* key); |
| 335 | |
| 336 | WOLFSSL_LOCAL int wc_CryptoCb_EccCheckPrivKey(ecc_key* key, const byte* pubKey, |
| 337 | word32 pubKeySz); |
| 338 | #endif /* HAVE_ECC */ |
| 339 | |
| 340 | #ifdef HAVE_CURVE25519 |
| 341 | WOLFSSL_LOCAL int wc_CryptoCb_Curve25519Gen(WC_RNG* rng, int keySize, |
| 342 | curve25519_key* key); |
| 343 | |
| 344 | WOLFSSL_LOCAL int wc_CryptoCb_Curve25519(curve25519_key* private_key, |
| 345 | curve25519_key* public_key, byte* out, word32* outlen, int endian); |
| 346 | #endif /* HAVE_CURVE25519 */ |
| 347 | |
| 348 | #ifdef HAVE_ED25519 |
| 349 | WOLFSSL_LOCAL int wc_CryptoCb_Ed25519Gen(WC_RNG* rng, int keySize, |
| 350 | ed25519_key* key); |
| 351 | WOLFSSL_LOCAL int wc_CryptoCb_Ed25519Sign(const byte* in, word32 inLen, |
| 352 | byte* out, word32 *outLen, ed25519_key* key, byte type, const byte* context, |
| 353 | byte contextLen); |
| 354 | WOLFSSL_LOCAL int wc_CryptoCb_Ed25519Verify(const byte* sig, word32 sigLen, |
| 355 | const byte* msg, word32 msgLen, int* res, ed25519_key* key, byte type, |
| 356 | const byte* context, byte contextLen); |
| 357 | #endif /* HAVE_ED25519 */ |
| 358 | |
| 359 | #ifndef NO_AES |
| 360 | #ifdef HAVE_AESGCM |
| 361 | WOLFSSL_LOCAL int wc_CryptoCb_AesGcmEncrypt(Aes* aes, byte* out, |
| 362 | const byte* in, word32 sz, const byte* iv, word32 ivSz, |
| 363 | byte* authTag, word32 authTagSz, const byte* authIn, word32 authInSz); |
| 364 | |
| 365 | WOLFSSL_LOCAL int wc_CryptoCb_AesGcmDecrypt(Aes* aes, byte* out, |
| 366 | const byte* in, word32 sz, const byte* iv, word32 ivSz, |
| 367 | const byte* authTag, word32 authTagSz, |
| 368 | const byte* authIn, word32 authInSz); |
| 369 | #endif /* HAVE_AESGCM */ |
| 370 | #ifdef HAVE_AES_CBC |
| 371 | WOLFSSL_LOCAL int wc_CryptoCb_AesCbcEncrypt(Aes* aes, byte* out, |
| 372 | const byte* in, word32 sz); |
| 373 | WOLFSSL_LOCAL int wc_CryptoCb_AesCbcDecrypt(Aes* aes, byte* out, |
| 374 | const byte* in, word32 sz); |
| 375 | #endif /* HAVE_AES_CBC */ |
| 376 | #endif /* !NO_AES */ |
| 377 | |
| 378 | #ifndef NO_DES3 |
| 379 | WOLFSSL_LOCAL int wc_CryptoCb_Des3Encrypt(Des3* des3, byte* out, |
| 380 | const byte* in, word32 sz); |
| 381 | WOLFSSL_LOCAL int wc_CryptoCb_Des3Decrypt(Des3* des3, byte* out, |
| 382 | const byte* in, word32 sz); |
| 383 | #endif /* !NO_DES3 */ |
| 384 | |
| 385 | #ifndef NO_SHA |
| 386 | WOLFSSL_LOCAL int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in, |
| 387 | word32 inSz, byte* digest); |
| 388 | #endif /* !NO_SHA */ |
| 389 | |
| 390 | #ifndef NO_SHA256 |
| 391 | WOLFSSL_LOCAL int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in, |
| 392 | word32 inSz, byte* digest); |
| 393 | #endif /* !NO_SHA256 */ |
| 394 | #ifdef WOLFSSL_SHA384 |
| 395 | WOLFSSL_LOCAL int wc_CryptoCb_Sha384Hash(wc_Sha384* sha384, const byte* in, |
| 396 | word32 inSz, byte* digest); |
| 397 | #endif |
| 398 | #ifdef WOLFSSL_SHA512 |
| 399 | WOLFSSL_LOCAL int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in, |
| 400 | word32 inSz, byte* digest); |
| 401 | #endif |
| 402 | |
| 403 | #ifndef NO_HMAC |
| 404 | WOLFSSL_LOCAL int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in, |
| 405 | word32 inSz, byte* digest); |
| 406 | #endif /* !NO_HMAC */ |
| 407 | |
| 408 | #ifndef WC_NO_RNG |
| 409 | WOLFSSL_LOCAL int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz); |
| 410 | WOLFSSL_LOCAL int wc_CryptoCb_RandomSeed(OS_Seed* os, byte* seed, word32 sz); |
| 411 | #endif |
| 412 | |
| 413 | #ifdef WOLFSSL_CMAC |
| 414 | WOLFSSL_LOCAL int wc_CryptoCb_Cmac(Cmac* cmac, const byte* key, word32 keySz, |
| 415 | const byte* in, word32 inSz, byte* out, word32* outSz, int type, |
| 416 | void* ctx); |
| 417 | #endif |
| 418 | |
| 419 | #endif /* WOLF_CRYPTO_CB */ |
| 420 | |
| 421 | #ifdef __cplusplus |
| 422 | } /* extern "C" */ |
| 423 | #endif |
| 424 | |
| 425 | #endif /* _WOLF_CRYPTO_CB_H_ */ |