cryptocb.c 40 KB

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