cryptocb.c 43 KB

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