cryptocb.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /* cryptocb.c
  2. *
  3. * Copyright (C) 2006-2022 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. /* This framework provides a central place for crypto hardware integration
  22. using the devId scheme. If not supported return `CRYPTOCB_UNAVAILABLE`. */
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26. #include <wolfssl/wolfcrypt/settings.h>
  27. #ifdef WOLF_CRYPTO_CB
  28. #include <wolfssl/wolfcrypt/cryptocb.h>
  29. #include <wolfssl/wolfcrypt/error-crypt.h>
  30. #include <wolfssl/wolfcrypt/logging.h>
  31. /* TODO: Consider linked list with mutex */
  32. #ifndef MAX_CRYPTO_DEVID_CALLBACKS
  33. #define MAX_CRYPTO_DEVID_CALLBACKS 8
  34. #endif
  35. typedef struct CryptoCb {
  36. int devId;
  37. CryptoDevCallbackFunc cb;
  38. void* ctx;
  39. } CryptoCb;
  40. static WOLFSSL_GLOBAL CryptoCb gCryptoDev[MAX_CRYPTO_DEVID_CALLBACKS];
  41. #ifdef DEBUG_CRYPTOCB
  42. static const char* GetAlgoTypeStr(int algo)
  43. {
  44. switch (algo) { /* enum wc_AlgoType */
  45. case WC_ALGO_TYPE_HASH: return "Hash";
  46. case WC_ALGO_TYPE_CIPHER: return "Cipher";
  47. case WC_ALGO_TYPE_PK: return "PK";
  48. case WC_ALGO_TYPE_RNG: return "RNG";
  49. case WC_ALGO_TYPE_SEED: return "Seed";
  50. case WC_ALGO_TYPE_HMAC: return "HMAC";
  51. }
  52. return NULL;
  53. }
  54. static const char* GetPkTypeStr(int pk)
  55. {
  56. switch (pk) {
  57. case WC_PK_TYPE_RSA: return "RSA";
  58. case WC_PK_TYPE_DH: return "DH";
  59. case WC_PK_TYPE_ECDH: return "ECDH";
  60. case WC_PK_TYPE_ECDSA_SIGN: return "ECDSA-Sign";
  61. case WC_PK_TYPE_ECDSA_VERIFY: return "ECDSA-Verify";
  62. case WC_PK_TYPE_ED25519_SIGN: return "ED25519-Sign";
  63. case WC_PK_TYPE_ED25519_VERIFY: return "ED25519-Verify";
  64. case WC_PK_TYPE_CURVE25519: return "CURVE25519";
  65. case WC_PK_TYPE_RSA_KEYGEN: return "RSA KeyGen";
  66. case WC_PK_TYPE_EC_KEYGEN: return "ECC KeyGen";
  67. }
  68. return NULL;
  69. }
  70. static const char* GetCipherTypeStr(int cipher)
  71. {
  72. switch (cipher) {
  73. case WC_CIPHER_AES: return "AES ECB";
  74. case WC_CIPHER_AES_CBC: return "AES CBC";
  75. case WC_CIPHER_AES_GCM: return "AES GCM";
  76. case WC_CIPHER_AES_CTR: return "AES CTR";
  77. case WC_CIPHER_AES_XTS: return "AES XTS";
  78. case WC_CIPHER_AES_CFB: return "AES CFB";
  79. case WC_CIPHER_DES3: return "DES3";
  80. case WC_CIPHER_DES: return "DES";
  81. case WC_CIPHER_CHACHA: return "ChaCha20";
  82. }
  83. return NULL;
  84. }
  85. static const char* GetHashTypeStr(int hash)
  86. {
  87. switch (hash) {
  88. case WC_HASH_TYPE_MD2: return "MD2";
  89. case WC_HASH_TYPE_MD4: return "MD4";
  90. case WC_HASH_TYPE_MD5: return "MD5";
  91. case WC_HASH_TYPE_SHA: return "SHA-1";
  92. case WC_HASH_TYPE_SHA224: return "SHA-224";
  93. case WC_HASH_TYPE_SHA256: return "SHA-256";
  94. case WC_HASH_TYPE_SHA384: return "SHA-384";
  95. case WC_HASH_TYPE_SHA512: return "SHA-512";
  96. case WC_HASH_TYPE_MD5_SHA: return "MD5-SHA1";
  97. case WC_HASH_TYPE_SHA3_224: return "SHA3-224";
  98. case WC_HASH_TYPE_SHA3_256: return "SHA3-256";
  99. case WC_HASH_TYPE_SHA3_384: return "SHA3-384";
  100. case WC_HASH_TYPE_SHA3_512: return "SHA3-512";
  101. case WC_HASH_TYPE_BLAKE2B: return "Blake2B";
  102. case WC_HASH_TYPE_BLAKE2S: return "Blake2S";
  103. }
  104. return NULL;
  105. }
  106. #ifndef NO_RSA
  107. static const char* GetRsaType(int type)
  108. {
  109. switch (type) {
  110. case RSA_PUBLIC_ENCRYPT: return "Public Encrypt";
  111. case RSA_PUBLIC_DECRYPT: return "Public Decrypt";
  112. case RSA_PRIVATE_ENCRYPT: return "Private Encrypt";
  113. case RSA_PRIVATE_DECRYPT: return "Private Decrypt";
  114. }
  115. return NULL;
  116. }
  117. #endif
  118. WOLFSSL_API void wc_CryptoCb_InfoString(wc_CryptoInfo* info)
  119. {
  120. if (info == NULL)
  121. return;
  122. if (info->algo_type == WC_ALGO_TYPE_PK) {
  123. #ifndef NO_RSA
  124. if (info->pk.type == WC_PK_TYPE_RSA) {
  125. printf("Crypto CB: %s %s (%d), %s, Len %d\n",
  126. GetAlgoTypeStr(info->algo_type),
  127. GetPkTypeStr(info->pk.type), info->pk.type,
  128. GetRsaType(info->pk.rsa.type), info->pk.rsa.inLen);
  129. }
  130. else
  131. #endif
  132. {
  133. printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
  134. GetPkTypeStr(info->pk.type), info->pk.type);
  135. }
  136. }
  137. else if (info->algo_type == WC_ALGO_TYPE_CIPHER) {
  138. printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
  139. GetCipherTypeStr(info->cipher.type), info->cipher.type);
  140. }
  141. else if (info->algo_type == WC_ALGO_TYPE_HASH) {
  142. printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
  143. GetHashTypeStr(info->hash.type), info->hash.type);
  144. }
  145. else if (info->algo_type == WC_ALGO_TYPE_HMAC) {
  146. printf("Crypto CB: %s %s (%d)\n", GetAlgoTypeStr(info->algo_type),
  147. GetHashTypeStr(info->hmac.macType), info->hmac.macType);
  148. }
  149. else {
  150. printf("CryptoCb: %s \n", GetAlgoTypeStr(info->algo_type));
  151. }
  152. }
  153. #endif /* DEBUG_CRYPTOCB */
  154. static CryptoCb* wc_CryptoCb_FindDevice(int devId)
  155. {
  156. int i;
  157. for (i=0; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) {
  158. if (gCryptoDev[i].devId == devId)
  159. return &gCryptoDev[i];
  160. }
  161. return NULL;
  162. }
  163. static CryptoCb* wc_CryptoCb_FindDeviceByIndex(int startIdx)
  164. {
  165. int i;
  166. for (i=startIdx; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) {
  167. if (gCryptoDev[i].devId != INVALID_DEVID)
  168. return &gCryptoDev[i];
  169. }
  170. return NULL;
  171. }
  172. static WC_INLINE int wc_CryptoCb_TranslateErrorCode(int ret)
  173. {
  174. if (ret == NOT_COMPILED_IN) {
  175. /* backwards compatibility for older NOT_COMPILED_IN syntax */
  176. ret = CRYPTOCB_UNAVAILABLE;
  177. }
  178. return ret;
  179. }
  180. void wc_CryptoCb_Init(void)
  181. {
  182. int i;
  183. for (i=0; i<MAX_CRYPTO_DEVID_CALLBACKS; i++) {
  184. gCryptoDev[i].devId = INVALID_DEVID;
  185. }
  186. }
  187. int wc_CryptoCb_GetDevIdAtIndex(int startIdx)
  188. {
  189. int devId = INVALID_DEVID;
  190. CryptoCb* dev = wc_CryptoCb_FindDeviceByIndex(startIdx);
  191. if (dev) {
  192. devId = dev->devId;
  193. }
  194. return devId;
  195. }
  196. int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx)
  197. {
  198. /* find existing or new */
  199. CryptoCb* dev = wc_CryptoCb_FindDevice(devId);
  200. if (dev == NULL)
  201. dev = wc_CryptoCb_FindDevice(INVALID_DEVID);
  202. if (dev == NULL)
  203. return BUFFER_E; /* out of devices */
  204. dev->devId = devId;
  205. dev->cb = cb;
  206. dev->ctx = ctx;
  207. return 0;
  208. }
  209. void wc_CryptoCb_UnRegisterDevice(int devId)
  210. {
  211. CryptoCb* dev = wc_CryptoCb_FindDevice(devId);
  212. if (dev) {
  213. XMEMSET(dev, 0, sizeof(*dev));
  214. dev->devId = INVALID_DEVID;
  215. }
  216. }
  217. #ifndef NO_RSA
  218. int wc_CryptoCb_Rsa(const byte* in, word32 inLen, byte* out,
  219. word32* outLen, int type, RsaKey* key, WC_RNG* rng)
  220. {
  221. int ret = CRYPTOCB_UNAVAILABLE;
  222. CryptoCb* dev;
  223. if (key == NULL)
  224. return ret;
  225. /* locate registered callback */
  226. dev = wc_CryptoCb_FindDevice(key->devId);
  227. if (dev && dev->cb) {
  228. wc_CryptoInfo cryptoInfo;
  229. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  230. cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
  231. cryptoInfo.pk.type = WC_PK_TYPE_RSA;
  232. cryptoInfo.pk.rsa.in = in;
  233. cryptoInfo.pk.rsa.inLen = inLen;
  234. cryptoInfo.pk.rsa.out = out;
  235. cryptoInfo.pk.rsa.outLen = outLen;
  236. cryptoInfo.pk.rsa.type = type;
  237. cryptoInfo.pk.rsa.key = key;
  238. cryptoInfo.pk.rsa.rng = rng;
  239. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  240. }
  241. return wc_CryptoCb_TranslateErrorCode(ret);
  242. }
  243. #ifdef WOLFSSL_KEY_GEN
  244. int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
  245. {
  246. int ret = CRYPTOCB_UNAVAILABLE;
  247. CryptoCb* dev;
  248. if (key == NULL)
  249. return ret;
  250. /* locate registered callback */
  251. dev = wc_CryptoCb_FindDevice(key->devId);
  252. if (dev && dev->cb) {
  253. wc_CryptoInfo cryptoInfo;
  254. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  255. cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
  256. cryptoInfo.pk.type = WC_PK_TYPE_RSA_KEYGEN;
  257. cryptoInfo.pk.rsakg.key = key;
  258. cryptoInfo.pk.rsakg.size = size;
  259. cryptoInfo.pk.rsakg.e = e;
  260. cryptoInfo.pk.rsakg.rng = rng;
  261. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  262. }
  263. return wc_CryptoCb_TranslateErrorCode(ret);
  264. }
  265. #endif
  266. int wc_CryptoCb_RsaCheckPrivKey(RsaKey* key, const byte* pubKey,
  267. word32 pubKeySz)
  268. {
  269. int ret = CRYPTOCB_UNAVAILABLE;
  270. CryptoCb* dev;
  271. if (key == NULL)
  272. return ret;
  273. /* locate registered callback */
  274. dev = wc_CryptoCb_FindDevice(key->devId);
  275. if (dev && dev->cb) {
  276. wc_CryptoInfo cryptoInfo;
  277. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  278. cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
  279. cryptoInfo.pk.type = WC_PK_TYPE_RSA_CHECK_PRIV_KEY;
  280. cryptoInfo.pk.rsa_check.key = key;
  281. cryptoInfo.pk.rsa_check.pubKey = pubKey;
  282. cryptoInfo.pk.rsa_check.pubKeySz = pubKeySz;
  283. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  284. }
  285. return wc_CryptoCb_TranslateErrorCode(ret);
  286. }
  287. #endif /* !NO_RSA */
  288. #ifdef HAVE_ECC
  289. int wc_CryptoCb_MakeEccKey(WC_RNG* rng, int keySize, ecc_key* key, int curveId)
  290. {
  291. int ret = CRYPTOCB_UNAVAILABLE;
  292. CryptoCb* dev;
  293. if (key == NULL)
  294. return ret;
  295. /* locate registered callback */
  296. dev = wc_CryptoCb_FindDevice(key->devId);
  297. if (dev && dev->cb) {
  298. wc_CryptoInfo cryptoInfo;
  299. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  300. cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
  301. cryptoInfo.pk.type = WC_PK_TYPE_EC_KEYGEN;
  302. cryptoInfo.pk.eckg.rng = rng;
  303. cryptoInfo.pk.eckg.size = keySize;
  304. cryptoInfo.pk.eckg.key = key;
  305. cryptoInfo.pk.eckg.curveId = curveId;
  306. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  307. }
  308. return wc_CryptoCb_TranslateErrorCode(ret);
  309. }
  310. int wc_CryptoCb_Ecdh(ecc_key* private_key, ecc_key* public_key,
  311. byte* out, word32* outlen)
  312. {
  313. int ret = CRYPTOCB_UNAVAILABLE;
  314. CryptoCb* dev;
  315. if (private_key == NULL)
  316. return ret;
  317. /* locate registered callback */
  318. dev = wc_CryptoCb_FindDevice(private_key->devId);
  319. if (dev && dev->cb) {
  320. wc_CryptoInfo cryptoInfo;
  321. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  322. cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
  323. cryptoInfo.pk.type = WC_PK_TYPE_ECDH;
  324. cryptoInfo.pk.ecdh.private_key = private_key;
  325. cryptoInfo.pk.ecdh.public_key = public_key;
  326. cryptoInfo.pk.ecdh.out = out;
  327. cryptoInfo.pk.ecdh.outlen = outlen;
  328. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  329. }
  330. return wc_CryptoCb_TranslateErrorCode(ret);
  331. }
  332. int wc_CryptoCb_EccSign(const byte* in, word32 inlen, byte* out,
  333. word32 *outlen, WC_RNG* rng, ecc_key* key)
  334. {
  335. int ret = CRYPTOCB_UNAVAILABLE;
  336. CryptoCb* dev;
  337. if (key == NULL)
  338. return ret;
  339. /* locate registered callback */
  340. dev = wc_CryptoCb_FindDevice(key->devId);
  341. if (dev && dev->cb) {
  342. wc_CryptoInfo cryptoInfo;
  343. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  344. cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
  345. cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_SIGN;
  346. cryptoInfo.pk.eccsign.in = in;
  347. cryptoInfo.pk.eccsign.inlen = inlen;
  348. cryptoInfo.pk.eccsign.out = out;
  349. cryptoInfo.pk.eccsign.outlen = outlen;
  350. cryptoInfo.pk.eccsign.rng = rng;
  351. cryptoInfo.pk.eccsign.key = key;
  352. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  353. }
  354. return wc_CryptoCb_TranslateErrorCode(ret);
  355. }
  356. int wc_CryptoCb_EccVerify(const byte* sig, word32 siglen,
  357. const byte* hash, word32 hashlen, int* res, ecc_key* key)
  358. {
  359. int ret = CRYPTOCB_UNAVAILABLE;
  360. CryptoCb* dev;
  361. if (key == NULL)
  362. return ret;
  363. /* locate registered callback */
  364. dev = wc_CryptoCb_FindDevice(key->devId);
  365. if (dev && dev->cb) {
  366. wc_CryptoInfo cryptoInfo;
  367. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  368. cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
  369. cryptoInfo.pk.type = WC_PK_TYPE_ECDSA_VERIFY;
  370. cryptoInfo.pk.eccverify.sig = sig;
  371. cryptoInfo.pk.eccverify.siglen = siglen;
  372. cryptoInfo.pk.eccverify.hash = hash;
  373. cryptoInfo.pk.eccverify.hashlen = hashlen;
  374. cryptoInfo.pk.eccverify.res = res;
  375. cryptoInfo.pk.eccverify.key = key;
  376. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  377. }
  378. return wc_CryptoCb_TranslateErrorCode(ret);
  379. }
  380. int wc_CryptoCb_EccCheckPrivKey(ecc_key* key, const byte* pubKey,
  381. word32 pubKeySz)
  382. {
  383. int ret = CRYPTOCB_UNAVAILABLE;
  384. CryptoCb* dev;
  385. if (key == NULL)
  386. return ret;
  387. /* locate registered callback */
  388. dev = wc_CryptoCb_FindDevice(key->devId);
  389. if (dev && dev->cb) {
  390. wc_CryptoInfo cryptoInfo;
  391. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  392. cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
  393. cryptoInfo.pk.type = WC_PK_TYPE_EC_CHECK_PRIV_KEY;
  394. cryptoInfo.pk.ecc_check.key = key;
  395. cryptoInfo.pk.ecc_check.pubKey = pubKey;
  396. cryptoInfo.pk.ecc_check.pubKeySz = pubKeySz;
  397. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  398. }
  399. return wc_CryptoCb_TranslateErrorCode(ret);
  400. }
  401. #endif /* HAVE_ECC */
  402. #ifdef HAVE_CURVE25519
  403. int wc_CryptoCb_Curve25519Gen(WC_RNG* rng, int keySize,
  404. curve25519_key* key)
  405. {
  406. int ret = CRYPTOCB_UNAVAILABLE;
  407. CryptoCb* dev;
  408. if (key == NULL)
  409. return ret;
  410. /* locate registered callback */
  411. dev = wc_CryptoCb_FindDevice(key->devId);
  412. if (dev && dev->cb) {
  413. wc_CryptoInfo cryptoInfo;
  414. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  415. cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
  416. cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519_KEYGEN;
  417. cryptoInfo.pk.curve25519kg.rng = rng;
  418. cryptoInfo.pk.curve25519kg.size = keySize;
  419. cryptoInfo.pk.curve25519kg.key = key;
  420. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  421. }
  422. return wc_CryptoCb_TranslateErrorCode(ret);
  423. }
  424. int wc_CryptoCb_Curve25519(curve25519_key* private_key,
  425. curve25519_key* public_key, byte* out, word32* outlen, int endian)
  426. {
  427. int ret = CRYPTOCB_UNAVAILABLE;
  428. CryptoCb* dev;
  429. if (private_key == NULL)
  430. return ret;
  431. /* locate registered callback */
  432. dev = wc_CryptoCb_FindDevice(private_key->devId);
  433. if (dev && dev->cb) {
  434. wc_CryptoInfo cryptoInfo;
  435. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  436. cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
  437. cryptoInfo.pk.type = WC_PK_TYPE_CURVE25519;
  438. cryptoInfo.pk.curve25519.private_key = private_key;
  439. cryptoInfo.pk.curve25519.public_key = public_key;
  440. cryptoInfo.pk.curve25519.out = out;
  441. cryptoInfo.pk.curve25519.outlen = outlen;
  442. cryptoInfo.pk.curve25519.endian = endian;
  443. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  444. }
  445. return wc_CryptoCb_TranslateErrorCode(ret);
  446. }
  447. #endif /* HAVE_CURVE25519 */
  448. #ifdef HAVE_ED25519
  449. int wc_CryptoCb_Ed25519Gen(WC_RNG* rng, int keySize,
  450. ed25519_key* key)
  451. {
  452. int ret = CRYPTOCB_UNAVAILABLE;
  453. CryptoCb* dev;
  454. if (key == NULL)
  455. return ret;
  456. /* locate registered callback */
  457. dev = wc_CryptoCb_FindDevice(key->devId);
  458. if (dev && dev->cb) {
  459. wc_CryptoInfo cryptoInfo;
  460. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  461. cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
  462. cryptoInfo.pk.type = WC_PK_TYPE_ED25519_KEYGEN;
  463. cryptoInfo.pk.ed25519kg.rng = rng;
  464. cryptoInfo.pk.ed25519kg.size = keySize;
  465. cryptoInfo.pk.ed25519kg.key = key;
  466. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  467. }
  468. return wc_CryptoCb_TranslateErrorCode(ret);
  469. }
  470. int wc_CryptoCb_Ed25519Sign(const byte* in, word32 inLen, byte* out,
  471. word32 *outLen, ed25519_key* key, byte type,
  472. const byte* context, byte contextLen)
  473. {
  474. int ret = CRYPTOCB_UNAVAILABLE;
  475. CryptoCb* dev;
  476. if (key == NULL)
  477. return ret;
  478. /* locate registered callback */
  479. dev = wc_CryptoCb_FindDevice(key->devId);
  480. if (dev && dev->cb) {
  481. wc_CryptoInfo cryptoInfo;
  482. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  483. cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
  484. cryptoInfo.pk.type = WC_PK_TYPE_ED25519_SIGN;
  485. cryptoInfo.pk.ed25519sign.in = in;
  486. cryptoInfo.pk.ed25519sign.inLen = inLen;
  487. cryptoInfo.pk.ed25519sign.out = out;
  488. cryptoInfo.pk.ed25519sign.outLen = outLen;
  489. cryptoInfo.pk.ed25519sign.key = key;
  490. cryptoInfo.pk.ed25519sign.type = type;
  491. cryptoInfo.pk.ed25519sign.context = context;
  492. cryptoInfo.pk.ed25519sign.contextLen = contextLen;
  493. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  494. }
  495. return wc_CryptoCb_TranslateErrorCode(ret);
  496. }
  497. int wc_CryptoCb_Ed25519Verify(const byte* sig, word32 sigLen,
  498. const byte* msg, word32 msgLen, int* res, ed25519_key* key, byte type,
  499. const byte* context, byte contextLen)
  500. {
  501. int ret = CRYPTOCB_UNAVAILABLE;
  502. CryptoCb* dev;
  503. if (key == NULL)
  504. return ret;
  505. /* locate registered callback */
  506. dev = wc_CryptoCb_FindDevice(key->devId);
  507. if (dev && dev->cb) {
  508. wc_CryptoInfo cryptoInfo;
  509. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  510. cryptoInfo.algo_type = WC_ALGO_TYPE_PK;
  511. cryptoInfo.pk.type = WC_PK_TYPE_ED25519_VERIFY;
  512. cryptoInfo.pk.ed25519verify.sig = sig;
  513. cryptoInfo.pk.ed25519verify.sigLen = sigLen;
  514. cryptoInfo.pk.ed25519verify.msg = msg;
  515. cryptoInfo.pk.ed25519verify.msgLen = msgLen;
  516. cryptoInfo.pk.ed25519verify.res = res;
  517. cryptoInfo.pk.ed25519verify.key = key;
  518. cryptoInfo.pk.ed25519verify.type = type;
  519. cryptoInfo.pk.ed25519verify.context = context;
  520. cryptoInfo.pk.ed25519verify.contextLen = contextLen;
  521. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  522. }
  523. return wc_CryptoCb_TranslateErrorCode(ret);
  524. }
  525. #endif /* HAVE_ED25519 */
  526. #ifndef NO_AES
  527. #ifdef HAVE_AESGCM
  528. int wc_CryptoCb_AesGcmEncrypt(Aes* aes, byte* out,
  529. const byte* in, word32 sz,
  530. const byte* iv, word32 ivSz,
  531. byte* authTag, word32 authTagSz,
  532. const byte* authIn, word32 authInSz)
  533. {
  534. int ret = CRYPTOCB_UNAVAILABLE;
  535. CryptoCb* dev;
  536. /* locate registered callback */
  537. if (aes) {
  538. dev = wc_CryptoCb_FindDevice(aes->devId);
  539. }
  540. else {
  541. /* locate first callback and try using it */
  542. dev = wc_CryptoCb_FindDeviceByIndex(0);
  543. }
  544. if (dev && dev->cb) {
  545. wc_CryptoInfo cryptoInfo;
  546. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  547. cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
  548. cryptoInfo.cipher.type = WC_CIPHER_AES_GCM;
  549. cryptoInfo.cipher.enc = 1;
  550. cryptoInfo.cipher.aesgcm_enc.aes = aes;
  551. cryptoInfo.cipher.aesgcm_enc.out = out;
  552. cryptoInfo.cipher.aesgcm_enc.in = in;
  553. cryptoInfo.cipher.aesgcm_enc.sz = sz;
  554. cryptoInfo.cipher.aesgcm_enc.iv = iv;
  555. cryptoInfo.cipher.aesgcm_enc.ivSz = ivSz;
  556. cryptoInfo.cipher.aesgcm_enc.authTag = authTag;
  557. cryptoInfo.cipher.aesgcm_enc.authTagSz = authTagSz;
  558. cryptoInfo.cipher.aesgcm_enc.authIn = authIn;
  559. cryptoInfo.cipher.aesgcm_enc.authInSz = authInSz;
  560. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  561. }
  562. return wc_CryptoCb_TranslateErrorCode(ret);
  563. }
  564. int wc_CryptoCb_AesGcmDecrypt(Aes* aes, byte* out,
  565. const byte* in, word32 sz,
  566. const byte* iv, word32 ivSz,
  567. const byte* authTag, word32 authTagSz,
  568. const byte* authIn, word32 authInSz)
  569. {
  570. int ret = CRYPTOCB_UNAVAILABLE;
  571. CryptoCb* dev;
  572. /* locate registered callback */
  573. if (aes) {
  574. dev = wc_CryptoCb_FindDevice(aes->devId);
  575. }
  576. else {
  577. /* locate first callback and try using it */
  578. dev = wc_CryptoCb_FindDeviceByIndex(0);
  579. }
  580. if (dev && dev->cb) {
  581. wc_CryptoInfo cryptoInfo;
  582. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  583. cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
  584. cryptoInfo.cipher.type = WC_CIPHER_AES_GCM;
  585. cryptoInfo.cipher.enc = 0;
  586. cryptoInfo.cipher.aesgcm_dec.aes = aes;
  587. cryptoInfo.cipher.aesgcm_dec.out = out;
  588. cryptoInfo.cipher.aesgcm_dec.in = in;
  589. cryptoInfo.cipher.aesgcm_dec.sz = sz;
  590. cryptoInfo.cipher.aesgcm_dec.iv = iv;
  591. cryptoInfo.cipher.aesgcm_dec.ivSz = ivSz;
  592. cryptoInfo.cipher.aesgcm_dec.authTag = authTag;
  593. cryptoInfo.cipher.aesgcm_dec.authTagSz = authTagSz;
  594. cryptoInfo.cipher.aesgcm_dec.authIn = authIn;
  595. cryptoInfo.cipher.aesgcm_dec.authInSz = authInSz;
  596. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  597. }
  598. return wc_CryptoCb_TranslateErrorCode(ret);
  599. }
  600. #endif /* HAVE_AESGCM */
  601. #ifdef HAVE_AESCCM
  602. int wc_CryptoCb_AesCcmEncrypt(Aes* aes, byte* out,
  603. const byte* in, word32 sz,
  604. const byte* nonce, word32 nonceSz,
  605. byte* authTag, word32 authTagSz,
  606. const byte* authIn, word32 authInSz)
  607. {
  608. int ret = CRYPTOCB_UNAVAILABLE;
  609. CryptoCb* dev;
  610. /* locate registered callback */
  611. if (aes) {
  612. dev = wc_CryptoCb_FindDevice(aes->devId);
  613. }
  614. else {
  615. /* locate first callback and try using it */
  616. dev = wc_CryptoCb_FindDeviceByIndex(0);
  617. }
  618. if (dev && dev->cb) {
  619. wc_CryptoInfo cryptoInfo;
  620. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  621. cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
  622. cryptoInfo.cipher.type = WC_CIPHER_AES_CCM;
  623. cryptoInfo.cipher.enc = 1;
  624. cryptoInfo.cipher.aesccm_enc.aes = aes;
  625. cryptoInfo.cipher.aesccm_enc.out = out;
  626. cryptoInfo.cipher.aesccm_enc.in = in;
  627. cryptoInfo.cipher.aesccm_enc.sz = sz;
  628. cryptoInfo.cipher.aesccm_enc.nonce = nonce;
  629. cryptoInfo.cipher.aesccm_enc.nonceSz = nonceSz;
  630. cryptoInfo.cipher.aesccm_enc.authTag = authTag;
  631. cryptoInfo.cipher.aesccm_enc.authTagSz = authTagSz;
  632. cryptoInfo.cipher.aesccm_enc.authIn = authIn;
  633. cryptoInfo.cipher.aesccm_enc.authInSz = authInSz;
  634. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  635. }
  636. return wc_CryptoCb_TranslateErrorCode(ret);
  637. }
  638. int wc_CryptoCb_AesCcmDecrypt(Aes* aes, byte* out,
  639. const byte* in, word32 sz,
  640. const byte* nonce, word32 nonceSz,
  641. const byte* authTag, word32 authTagSz,
  642. const byte* authIn, word32 authInSz)
  643. {
  644. int ret = CRYPTOCB_UNAVAILABLE;
  645. CryptoCb* dev;
  646. /* locate registered callback */
  647. if (aes) {
  648. dev = wc_CryptoCb_FindDevice(aes->devId);
  649. }
  650. else {
  651. /* locate first callback and try using it */
  652. dev = wc_CryptoCb_FindDeviceByIndex(0);
  653. }
  654. if (dev && dev->cb) {
  655. wc_CryptoInfo cryptoInfo;
  656. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  657. cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
  658. cryptoInfo.cipher.type = WC_CIPHER_AES_CCM;
  659. cryptoInfo.cipher.enc = 0;
  660. cryptoInfo.cipher.aesccm_dec.aes = aes;
  661. cryptoInfo.cipher.aesccm_dec.out = out;
  662. cryptoInfo.cipher.aesccm_dec.in = in;
  663. cryptoInfo.cipher.aesccm_dec.sz = sz;
  664. cryptoInfo.cipher.aesccm_dec.nonce = nonce;
  665. cryptoInfo.cipher.aesccm_dec.nonceSz = nonceSz;
  666. cryptoInfo.cipher.aesccm_dec.authTag = authTag;
  667. cryptoInfo.cipher.aesccm_dec.authTagSz = authTagSz;
  668. cryptoInfo.cipher.aesccm_dec.authIn = authIn;
  669. cryptoInfo.cipher.aesccm_dec.authInSz = authInSz;
  670. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  671. }
  672. return wc_CryptoCb_TranslateErrorCode(ret);
  673. }
  674. #endif /* HAVE_AESCCM */
  675. #ifdef HAVE_AES_CBC
  676. int wc_CryptoCb_AesCbcEncrypt(Aes* aes, byte* out,
  677. const byte* in, word32 sz)
  678. {
  679. int ret = CRYPTOCB_UNAVAILABLE;
  680. CryptoCb* dev;
  681. /* locate registered callback */
  682. if (aes) {
  683. dev = wc_CryptoCb_FindDevice(aes->devId);
  684. }
  685. else {
  686. /* locate first callback and try using it */
  687. dev = wc_CryptoCb_FindDeviceByIndex(0);
  688. }
  689. if (dev && dev->cb) {
  690. wc_CryptoInfo cryptoInfo;
  691. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  692. cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
  693. cryptoInfo.cipher.type = WC_CIPHER_AES_CBC;
  694. cryptoInfo.cipher.enc = 1;
  695. cryptoInfo.cipher.aescbc.aes = aes;
  696. cryptoInfo.cipher.aescbc.out = out;
  697. cryptoInfo.cipher.aescbc.in = in;
  698. cryptoInfo.cipher.aescbc.sz = sz;
  699. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  700. }
  701. return wc_CryptoCb_TranslateErrorCode(ret);
  702. }
  703. int wc_CryptoCb_AesCbcDecrypt(Aes* aes, byte* out,
  704. const byte* in, word32 sz)
  705. {
  706. int ret = CRYPTOCB_UNAVAILABLE;
  707. CryptoCb* dev;
  708. /* locate registered callback */
  709. if (aes) {
  710. dev = wc_CryptoCb_FindDevice(aes->devId);
  711. }
  712. else {
  713. /* locate first callback and try using it */
  714. dev = wc_CryptoCb_FindDeviceByIndex(0);
  715. }
  716. if (dev && dev->cb) {
  717. wc_CryptoInfo cryptoInfo;
  718. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  719. cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
  720. cryptoInfo.cipher.type = WC_CIPHER_AES_CBC;
  721. cryptoInfo.cipher.enc = 0;
  722. cryptoInfo.cipher.aescbc.aes = aes;
  723. cryptoInfo.cipher.aescbc.out = out;
  724. cryptoInfo.cipher.aescbc.in = in;
  725. cryptoInfo.cipher.aescbc.sz = sz;
  726. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  727. }
  728. return wc_CryptoCb_TranslateErrorCode(ret);
  729. }
  730. #endif /* HAVE_AES_CBC */
  731. #ifdef WOLFSSL_AES_COUNTER
  732. int wc_CryptoCb_AesCtrEncrypt(Aes* aes, byte* out,
  733. const byte* in, word32 sz)
  734. {
  735. int ret = CRYPTOCB_UNAVAILABLE;
  736. CryptoCb* dev;
  737. /* locate registered callback */
  738. if (aes) {
  739. dev = wc_CryptoCb_FindDevice(aes->devId);
  740. }
  741. else {
  742. /* locate first callback and try using it */
  743. dev = wc_CryptoCb_FindDeviceByIndex(0);
  744. }
  745. if (dev && dev->cb) {
  746. wc_CryptoInfo cryptoInfo;
  747. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  748. cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
  749. cryptoInfo.cipher.type = WC_CIPHER_AES_CTR;
  750. cryptoInfo.cipher.enc = 1;
  751. cryptoInfo.cipher.aesctr.aes = aes;
  752. cryptoInfo.cipher.aesctr.out = out;
  753. cryptoInfo.cipher.aesctr.in = in;
  754. cryptoInfo.cipher.aesctr.sz = sz;
  755. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  756. }
  757. return wc_CryptoCb_TranslateErrorCode(ret);
  758. }
  759. #endif /* WOLFSSL_AES_COUNTER */
  760. #ifdef HAVE_AES_ECB
  761. int wc_CryptoCb_AesEcbEncrypt(Aes* aes, byte* out,
  762. const byte* in, word32 sz)
  763. {
  764. int ret = CRYPTOCB_UNAVAILABLE;
  765. CryptoCb* dev;
  766. /* locate registered callback */
  767. if (aes) {
  768. dev = wc_CryptoCb_FindDevice(aes->devId);
  769. }
  770. else {
  771. /* locate first callback and try using it */
  772. dev = wc_CryptoCb_FindDeviceByIndex(0);
  773. }
  774. if (dev && dev->cb) {
  775. wc_CryptoInfo cryptoInfo;
  776. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  777. cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
  778. cryptoInfo.cipher.type = WC_CIPHER_AES_ECB;
  779. cryptoInfo.cipher.enc = 1;
  780. cryptoInfo.cipher.aesecb.aes = aes;
  781. cryptoInfo.cipher.aesecb.out = out;
  782. cryptoInfo.cipher.aesecb.in = in;
  783. cryptoInfo.cipher.aesecb.sz = sz;
  784. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  785. }
  786. return wc_CryptoCb_TranslateErrorCode(ret);
  787. }
  788. int wc_CryptoCb_AesEcbDecrypt(Aes* aes, byte* out,
  789. const byte* in, word32 sz)
  790. {
  791. int ret = CRYPTOCB_UNAVAILABLE;
  792. CryptoCb* dev;
  793. /* locate registered callback */
  794. if (aes) {
  795. dev = wc_CryptoCb_FindDevice(aes->devId);
  796. }
  797. else {
  798. /* locate first callback and try using it */
  799. dev = wc_CryptoCb_FindDeviceByIndex(0);
  800. }
  801. if (dev && dev->cb) {
  802. wc_CryptoInfo cryptoInfo;
  803. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  804. cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
  805. cryptoInfo.cipher.type = WC_CIPHER_AES_ECB;
  806. cryptoInfo.cipher.enc = 0;
  807. cryptoInfo.cipher.aesecb.aes = aes;
  808. cryptoInfo.cipher.aesecb.out = out;
  809. cryptoInfo.cipher.aesecb.in = in;
  810. cryptoInfo.cipher.aesecb.sz = sz;
  811. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  812. }
  813. return wc_CryptoCb_TranslateErrorCode(ret);
  814. }
  815. #endif /* HAVE_AES_ECB */
  816. #endif /* !NO_AES */
  817. #ifndef NO_DES3
  818. int wc_CryptoCb_Des3Encrypt(Des3* des3, byte* out,
  819. const byte* in, word32 sz)
  820. {
  821. int ret = CRYPTOCB_UNAVAILABLE;
  822. CryptoCb* dev;
  823. /* locate registered callback */
  824. if (des3) {
  825. dev = wc_CryptoCb_FindDevice(des3->devId);
  826. }
  827. else {
  828. /* locate first callback and try using it */
  829. dev = wc_CryptoCb_FindDeviceByIndex(0);
  830. }
  831. if (dev && dev->cb) {
  832. wc_CryptoInfo cryptoInfo;
  833. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  834. cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
  835. cryptoInfo.cipher.type = WC_CIPHER_DES3;
  836. cryptoInfo.cipher.enc = 1;
  837. cryptoInfo.cipher.des3.des = des3;
  838. cryptoInfo.cipher.des3.out = out;
  839. cryptoInfo.cipher.des3.in = in;
  840. cryptoInfo.cipher.des3.sz = sz;
  841. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  842. }
  843. return wc_CryptoCb_TranslateErrorCode(ret);
  844. }
  845. int wc_CryptoCb_Des3Decrypt(Des3* des3, byte* out,
  846. const byte* in, word32 sz)
  847. {
  848. int ret = CRYPTOCB_UNAVAILABLE;
  849. CryptoCb* dev;
  850. /* locate registered callback */
  851. if (des3) {
  852. dev = wc_CryptoCb_FindDevice(des3->devId);
  853. }
  854. else {
  855. /* locate first callback and try using it */
  856. dev = wc_CryptoCb_FindDeviceByIndex(0);
  857. }
  858. if (dev && dev->cb) {
  859. wc_CryptoInfo cryptoInfo;
  860. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  861. cryptoInfo.algo_type = WC_ALGO_TYPE_CIPHER;
  862. cryptoInfo.cipher.type = WC_CIPHER_DES3;
  863. cryptoInfo.cipher.enc = 0;
  864. cryptoInfo.cipher.des3.des = des3;
  865. cryptoInfo.cipher.des3.out = out;
  866. cryptoInfo.cipher.des3.in = in;
  867. cryptoInfo.cipher.des3.sz = sz;
  868. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  869. }
  870. return wc_CryptoCb_TranslateErrorCode(ret);
  871. }
  872. #endif /* !NO_DES3 */
  873. #ifndef NO_SHA
  874. int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in,
  875. word32 inSz, byte* digest)
  876. {
  877. int ret = CRYPTOCB_UNAVAILABLE;
  878. CryptoCb* dev;
  879. /* locate registered callback */
  880. if (sha) {
  881. dev = wc_CryptoCb_FindDevice(sha->devId);
  882. }
  883. else {
  884. /* locate first callback and try using it */
  885. dev = wc_CryptoCb_FindDeviceByIndex(0);
  886. }
  887. if (dev && dev->cb) {
  888. wc_CryptoInfo cryptoInfo;
  889. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  890. cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
  891. cryptoInfo.hash.type = WC_HASH_TYPE_SHA;
  892. cryptoInfo.hash.sha1 = sha;
  893. cryptoInfo.hash.in = in;
  894. cryptoInfo.hash.inSz = inSz;
  895. cryptoInfo.hash.digest = digest;
  896. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  897. }
  898. return wc_CryptoCb_TranslateErrorCode(ret);
  899. }
  900. #endif /* !NO_SHA */
  901. #ifndef NO_SHA256
  902. int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in,
  903. word32 inSz, byte* digest)
  904. {
  905. int ret = CRYPTOCB_UNAVAILABLE;
  906. CryptoCb* dev;
  907. /* locate registered callback */
  908. if (sha256) {
  909. dev = wc_CryptoCb_FindDevice(sha256->devId);
  910. }
  911. else {
  912. /* locate first callback and try using it */
  913. dev = wc_CryptoCb_FindDeviceByIndex(0);
  914. }
  915. if (dev && dev->cb) {
  916. wc_CryptoInfo cryptoInfo;
  917. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  918. cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
  919. cryptoInfo.hash.type = WC_HASH_TYPE_SHA256;
  920. cryptoInfo.hash.sha256 = sha256;
  921. cryptoInfo.hash.in = in;
  922. cryptoInfo.hash.inSz = inSz;
  923. cryptoInfo.hash.digest = digest;
  924. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  925. }
  926. return wc_CryptoCb_TranslateErrorCode(ret);
  927. }
  928. #endif /* !NO_SHA256 */
  929. #ifdef WOLFSSL_SHA384
  930. int wc_CryptoCb_Sha384Hash(wc_Sha384* sha384, const byte* in,
  931. word32 inSz, byte* digest)
  932. {
  933. int ret = CRYPTOCB_UNAVAILABLE;
  934. CryptoCb* dev;
  935. /* locate registered callback */
  936. #ifndef NO_SHA2_CRYPTO_CB
  937. if (sha384) {
  938. dev = wc_CryptoCb_FindDevice(sha384->devId);
  939. }
  940. else
  941. #endif
  942. {
  943. /* locate first callback and try using it */
  944. dev = wc_CryptoCb_FindDeviceByIndex(0);
  945. }
  946. if (dev && dev->cb) {
  947. wc_CryptoInfo cryptoInfo;
  948. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  949. cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
  950. cryptoInfo.hash.type = WC_HASH_TYPE_SHA384;
  951. cryptoInfo.hash.sha384 = sha384;
  952. cryptoInfo.hash.in = in;
  953. cryptoInfo.hash.inSz = inSz;
  954. cryptoInfo.hash.digest = digest;
  955. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  956. }
  957. return wc_CryptoCb_TranslateErrorCode(ret);
  958. }
  959. #endif /* WOLFSSL_SHA384 */
  960. #ifdef WOLFSSL_SHA512
  961. int wc_CryptoCb_Sha512Hash(wc_Sha512* sha512, const byte* in,
  962. word32 inSz, byte* digest)
  963. {
  964. int ret = CRYPTOCB_UNAVAILABLE;
  965. CryptoCb* dev;
  966. /* locate registered callback */
  967. #ifndef NO_SHA2_CRYPTO_CB
  968. if (sha512) {
  969. dev = wc_CryptoCb_FindDevice(sha512->devId);
  970. }
  971. else
  972. #endif
  973. {
  974. /* locate first callback and try using it */
  975. dev = wc_CryptoCb_FindDeviceByIndex(0);
  976. }
  977. if (dev && dev->cb) {
  978. wc_CryptoInfo cryptoInfo;
  979. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  980. cryptoInfo.algo_type = WC_ALGO_TYPE_HASH;
  981. cryptoInfo.hash.type = WC_HASH_TYPE_SHA512;
  982. cryptoInfo.hash.sha512 = sha512;
  983. cryptoInfo.hash.in = in;
  984. cryptoInfo.hash.inSz = inSz;
  985. cryptoInfo.hash.digest = digest;
  986. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  987. }
  988. return wc_CryptoCb_TranslateErrorCode(ret);
  989. }
  990. #endif /* WOLFSSL_SHA512 */
  991. #ifndef NO_HMAC
  992. int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in, word32 inSz,
  993. byte* digest)
  994. {
  995. int ret = CRYPTOCB_UNAVAILABLE;
  996. CryptoCb* dev;
  997. if (hmac == NULL)
  998. return ret;
  999. /* locate registered callback */
  1000. dev = wc_CryptoCb_FindDevice(hmac->devId);
  1001. if (dev && dev->cb) {
  1002. wc_CryptoInfo cryptoInfo;
  1003. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  1004. cryptoInfo.algo_type = WC_ALGO_TYPE_HMAC;
  1005. cryptoInfo.hmac.macType = macType;
  1006. cryptoInfo.hmac.in = in;
  1007. cryptoInfo.hmac.inSz = inSz;
  1008. cryptoInfo.hmac.digest = digest;
  1009. cryptoInfo.hmac.hmac = hmac;
  1010. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  1011. }
  1012. return wc_CryptoCb_TranslateErrorCode(ret);
  1013. }
  1014. #endif /* !NO_HMAC */
  1015. #ifndef WC_NO_RNG
  1016. int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz)
  1017. {
  1018. int ret = CRYPTOCB_UNAVAILABLE;
  1019. CryptoCb* dev;
  1020. /* locate registered callback */
  1021. if (rng) {
  1022. dev = wc_CryptoCb_FindDevice(rng->devId);
  1023. }
  1024. else {
  1025. /* locate first callback and try using it */
  1026. dev = wc_CryptoCb_FindDeviceByIndex(0);
  1027. }
  1028. if (dev && dev->cb) {
  1029. wc_CryptoInfo cryptoInfo;
  1030. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  1031. cryptoInfo.algo_type = WC_ALGO_TYPE_RNG;
  1032. cryptoInfo.rng.rng = rng;
  1033. cryptoInfo.rng.out = out;
  1034. cryptoInfo.rng.sz = sz;
  1035. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  1036. }
  1037. return wc_CryptoCb_TranslateErrorCode(ret);
  1038. }
  1039. int wc_CryptoCb_RandomSeed(OS_Seed* os, byte* seed, word32 sz)
  1040. {
  1041. int ret = CRYPTOCB_UNAVAILABLE;
  1042. CryptoCb* dev;
  1043. /* locate registered callback */
  1044. dev = wc_CryptoCb_FindDevice(os->devId);
  1045. if (dev && dev->cb) {
  1046. wc_CryptoInfo cryptoInfo;
  1047. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  1048. cryptoInfo.algo_type = WC_ALGO_TYPE_SEED;
  1049. cryptoInfo.seed.os = os;
  1050. cryptoInfo.seed.seed = seed;
  1051. cryptoInfo.seed.sz = sz;
  1052. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  1053. }
  1054. return wc_CryptoCb_TranslateErrorCode(ret);
  1055. }
  1056. #endif /* !WC_NO_RNG */
  1057. #ifdef WOLFSSL_CMAC
  1058. int wc_CryptoCb_Cmac(Cmac* cmac, const byte* key, word32 keySz,
  1059. const byte* in, word32 inSz, byte* out, word32* outSz, int type,
  1060. void* ctx)
  1061. {
  1062. int ret = CRYPTOCB_UNAVAILABLE;
  1063. CryptoCb* dev;
  1064. /* locate registered callback */
  1065. if (cmac) {
  1066. dev = wc_CryptoCb_FindDevice(cmac->devId);
  1067. }
  1068. else {
  1069. /* locate first callback and try using it */
  1070. dev = wc_CryptoCb_FindDeviceByIndex(0);
  1071. }
  1072. if (dev && dev->cb) {
  1073. wc_CryptoInfo cryptoInfo;
  1074. XMEMSET(&cryptoInfo, 0, sizeof(cryptoInfo));
  1075. cryptoInfo.algo_type = WC_ALGO_TYPE_CMAC;
  1076. cryptoInfo.cmac.cmac = cmac;
  1077. cryptoInfo.cmac.ctx = ctx;
  1078. cryptoInfo.cmac.key = key;
  1079. cryptoInfo.cmac.in = in;
  1080. cryptoInfo.cmac.out = out;
  1081. cryptoInfo.cmac.outSz = outSz;
  1082. cryptoInfo.cmac.keySz = keySz;
  1083. cryptoInfo.cmac.inSz = inSz;
  1084. cryptoInfo.cmac.type = type;
  1085. ret = dev->cb(dev->devId, &cryptoInfo, dev->ctx);
  1086. }
  1087. return wc_CryptoCb_TranslateErrorCode(ret);
  1088. }
  1089. #endif
  1090. /* returns the default dev id for the current build */
  1091. int wc_CryptoCb_DefaultDevID(void)
  1092. {
  1093. int ret;
  1094. /* conditional macro selection based on build */
  1095. #ifdef WOLFSSL_CAAM_DEVID
  1096. ret = WOLFSSL_CAAM_DEVID;
  1097. #else
  1098. ret = INVALID_DEVID;
  1099. #endif
  1100. return ret;
  1101. }
  1102. #endif /* WOLF_CRYPTO_CB */