cryptocb.h 16 KB

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