wolfcaam_seco.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. /* wolfcaam_seco.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. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #if defined(WOLFSSL_SECO_CAAM)
  26. #include <hsm/hsm_api.h>
  27. #include <seco_nvm.h>
  28. #include <wolfssl/wolfcrypt/logging.h>
  29. #include <wolfssl/wolfcrypt/error-crypt.h>
  30. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  31. #include <wolfssl/wolfcrypt/cmac.h>
  32. #include <wolfssl/wolfcrypt/aes.h>
  33. #define MAX_SECO_TIMEOUT 1000
  34. static wolfSSL_Mutex caamMutex;
  35. static pthread_t tid;
  36. static uint32_t nvm_status = 0;
  37. static hsm_hdl_t hsm_session;
  38. static hsm_hdl_t key_store_hdl;
  39. static int wc_TranslateHSMError(int current, hsm_err_t err);
  40. static void* hsm_storage_init(void* args)
  41. {
  42. seco_nvm_manager(NVM_FLAGS_HSM, &nvm_status);
  43. (void)args;
  44. return NULL;
  45. }
  46. /* return 0 on success */
  47. int wc_SECOInitInterface()
  48. {
  49. int i;
  50. open_session_args_t session_args;
  51. hsm_err_t err;
  52. nvm_status = NVM_STATUS_UNDEF;
  53. if (wc_InitMutex(&caamMutex) != 0) {
  54. WOLFSSL_MSG("Could not init mutex");
  55. return -1;
  56. }
  57. (void)pthread_create(&tid, NULL, hsm_storage_init, NULL);
  58. /* wait for NVM to be ready for SECO */
  59. for (i = 0 ; i < MAX_SECO_TIMEOUT && nvm_status <= NVM_STATUS_STARTING;
  60. i++) {
  61. usleep(1000);
  62. }
  63. if (i == MAX_SECO_TIMEOUT) {
  64. WOLFSSL_MSG("Timed out waiting for SECO setup");
  65. return -1;
  66. }
  67. if (nvm_status == NVM_STATUS_STOPPED) {
  68. WOLFSSL_MSG("Error with SECO setup");
  69. return -1;
  70. }
  71. session_args.session_priority = 0;
  72. session_args.operating_mode = 0;
  73. err = hsm_open_session(&session_args, &hsm_session);
  74. if (err != HSM_NO_ERROR) {
  75. WOLFSSL_MSG("Error with HSM session open");
  76. return -1;
  77. }
  78. WOLFSSL_MSG("SECO HSM setup done");
  79. return 0;
  80. }
  81. void wc_SECOFreeInterface()
  82. {
  83. hsm_err_t err;
  84. err = hsm_close_session(hsm_session);
  85. if (err != HSM_NO_ERROR) {
  86. WOLFSSL_MSG("Error with HSM session close");
  87. }
  88. if (nvm_status != NVM_STATUS_STOPPED) {
  89. if (pthread_cancel(tid) != 0) {
  90. WOLFSSL_MSG("SECO HSM thread shutdown failed");
  91. }
  92. }
  93. seco_nvm_close_session();
  94. WOLFSSL_MSG("SECO HSM shutdown");
  95. wc_FreeMutex(&caamMutex);
  96. }
  97. /* open the key management HSM handle
  98. * return 0 on success
  99. */
  100. int wc_SECO_OpenHSM(word32 keyStoreId, word32 nonce, word16 maxUpdates,
  101. byte flag)
  102. {
  103. hsm_err_t err;
  104. open_svc_key_store_args_t key_store_args;
  105. XMEMSET(&key_store_args, 0, sizeof(open_svc_key_store_args_t));
  106. key_store_args.key_store_identifier = keyStoreId,
  107. key_store_args.authentication_nonce = nonce;
  108. key_store_args.max_updates_number = maxUpdates;
  109. switch (flag) {
  110. case CAAM_KEYSTORE_CREATE:
  111. key_store_args.flags = HSM_SVC_KEY_STORE_FLAGS_CREATE;
  112. break;
  113. case CAAM_KEYSTORE_UPDATE:
  114. #ifdef HSM_SVC_KEY_STORE_FLAGS_UPDATE
  115. key_store_args.flags = HSM_SVC_KEY_STORE_FLAGS_UPDATE;
  116. #else
  117. key_store_args.flags = 0;
  118. #endif
  119. break;
  120. default:
  121. WOLFSSL_MSG("Unknown flag");
  122. return -1;
  123. }
  124. err = hsm_open_key_store_service(hsm_session, &key_store_args,
  125. &key_store_hdl);
  126. if (wc_TranslateHSMError(0, err) != Success) {
  127. return -1;
  128. }
  129. else {
  130. return 0;
  131. }
  132. }
  133. /* close the key management HSM handle
  134. * return 0 on success
  135. */
  136. int wc_SECO_CloseHSM()
  137. {
  138. hsm_err_t err = hsm_close_key_store_service(key_store_hdl);
  139. if (wc_TranslateHSMError(0, err) != Success) {
  140. return -1;
  141. }
  142. else {
  143. return 0;
  144. }
  145. }
  146. /* returns error enum found from hsm calls, HSM_NO_ERROR on success */
  147. static hsm_err_t wc_SECO_RNG(unsigned int args[4], CAAM_BUFFER *buf, int sz)
  148. {
  149. hsm_hdl_t rng;
  150. hsm_err_t err;
  151. open_svc_rng_args_t svcArgs = {0};
  152. op_get_random_args_t rngArgs = {0};
  153. err = hsm_open_rng_service(hsm_session, &svcArgs, &rng);
  154. if (err == HSM_NO_ERROR) {
  155. rngArgs.output = (uint8_t*)buf[0].TheAddress;
  156. rngArgs.random_size = (uint32_t)buf[0].Length;
  157. err = hsm_get_random(rng, &rngArgs);
  158. #ifdef DEBUG_SECO
  159. {
  160. uint32_t z;
  161. printf("Pulled rng data from HSM :");
  162. for (z = 0; z < rngArgs.random_size; z++)
  163. printf("%02X", rngArgs.output[z]);
  164. printf("\n");
  165. }
  166. #endif
  167. }
  168. if (err == HSM_NO_ERROR) {
  169. err = hsm_close_rng_service(rng);
  170. }
  171. (void)args;
  172. (void)sz;
  173. return err;
  174. }
  175. static hsm_err_t wc_SECO_Hash(unsigned args[4], CAAM_BUFFER *buf, int sz,
  176. int type)
  177. {
  178. hsm_hdl_t hash;
  179. hsm_err_t err = HSM_NO_ERROR;
  180. op_hash_one_go_args_t hashArgs = {0};
  181. open_svc_hash_args_t sessionArgs = {0};
  182. if (args[0] != CAAM_ALG_FINAL) {
  183. WOLFSSL_MSG("Only expecting to call the HSM on final");
  184. err = HSM_GENERAL_ERROR;
  185. }
  186. if (err == HSM_NO_ERROR) {
  187. err = hsm_open_hash_service(hsm_session, &sessionArgs, &hash);
  188. }
  189. if (err == HSM_NO_ERROR) {
  190. switch (type) {
  191. case CAAM_SHA224:
  192. hashArgs.algo = HSM_HASH_ALGO_SHA_224;
  193. break;
  194. case CAAM_SHA256:
  195. hashArgs.algo = HSM_HASH_ALGO_SHA_256;
  196. break;
  197. case CAAM_SHA384:
  198. hashArgs.algo = HSM_HASH_ALGO_SHA_384;
  199. break;
  200. case CAAM_SHA512:
  201. hashArgs.algo = HSM_HASH_ALGO_SHA_512;
  202. break;
  203. }
  204. hashArgs.output = (uint8_t*)buf[0].TheAddress;
  205. hashArgs.output_size = buf[0].Length;
  206. hashArgs.input = (uint8_t*)buf[1].TheAddress;
  207. hashArgs.input_size = buf[1].Length;
  208. err = hsm_hash_one_go(hash, &hashArgs);
  209. if (err != HSM_NO_ERROR) {
  210. WOLFSSL_MSG("Error with HSM hash call");
  211. }
  212. #ifdef DEBUG_SECO
  213. {
  214. word32 z;
  215. printf("hash algo type = %d\n", hashArgs.algo);
  216. printf("\tlength of input data = %d\n", hashArgs.input_size);
  217. printf("\toutput : ");
  218. for (z = 0; z < hashArgs.output_size; z++)
  219. printf("%02X", hashArgs.output[z]);
  220. printf("\n");
  221. }
  222. #endif
  223. /* always try to close the hash handle */
  224. if (hsm_close_hash_service(hash) != HSM_NO_ERROR) {
  225. WOLFSSL_MSG("Error with HSM hash close");
  226. if (err == HSM_NO_ERROR) {
  227. err = HSM_GENERAL_ERROR;
  228. }
  229. }
  230. }
  231. (void)sz;
  232. return err;
  233. }
  234. /* convert ECDSEL type to HSM type
  235. * return 0 on fail
  236. */
  237. static hsm_key_type_t ECDSELtoHSM(int ecdsel)
  238. {
  239. switch (ecdsel) {
  240. case CAAM_ECDSA_P192:
  241. case CAAM_ECDSA_P224:
  242. WOLFSSL_MSG("P192 and P224 are not supported");
  243. break;
  244. case CAAM_ECDSA_P256:
  245. return CAAM_KEYTYPE_ECDSA_P256;
  246. case CAAM_ECDSA_P384:
  247. return CAAM_KEYTYPE_ECDSA_P384;
  248. case CAAM_ECDSA_P521:
  249. return CAAM_KEYTYPE_ECDSA_P521;
  250. }
  251. return 0;
  252. }
  253. static hsm_key_type_t KeyTypeToHSM(int keyTypeIn)
  254. {
  255. hsm_key_type_t ret = 0;
  256. switch (keyTypeIn) {
  257. case CAAM_KEYTYPE_ECDSA_P256:
  258. ret = HSM_KEY_TYPE_ECDSA_NIST_P256;
  259. break;
  260. case CAAM_KEYTYPE_ECDSA_P384:
  261. ret = HSM_KEY_TYPE_ECDSA_NIST_P384;
  262. break;
  263. case CAAM_KEYTYPE_ECDSA_P521:
  264. ret = HSM_KEY_TYPE_ECDSA_NIST_P521;
  265. break;
  266. case CAAM_KEYTYPE_AES128:
  267. ret = HSM_KEY_TYPE_AES_128;
  268. break;
  269. case CAAM_KEYTYPE_AES192:
  270. ret = HSM_KEY_TYPE_AES_192;
  271. break;
  272. case CAAM_KEYTYPE_AES256:
  273. ret = HSM_KEY_TYPE_AES_256;
  274. break;
  275. #ifdef HSM_KEY_TYPE_HMAC_224
  276. case CAAM_KEYTYPE_HMAC224:
  277. ret = HSM_KEY_TYPE_HMAC_224;
  278. break;
  279. #endif
  280. #ifdef HSM_KEY_TYPE_HMAC_256
  281. case CAAM_KEYTYPE_HMAC256:
  282. ret = HSM_KEY_TYPE_HMAC_256;
  283. break;
  284. #endif
  285. #ifdef HSM_KEY_TYPE_HMAC_384
  286. case CAAM_KEYTYPE_HMAC384:
  287. ret = HSM_KEY_TYPE_HMAC_384;
  288. break;
  289. #endif
  290. #ifdef HSM_KEY_TYPE_HMAC_512
  291. case CAAM_KEYTYPE_HMAC512:
  292. ret = HSM_KEY_TYPE_HMAC_512;
  293. break;
  294. #endif
  295. }
  296. return ret;
  297. }
  298. static hsm_key_info_t KeyInfoToHSM(int keyInfoIn)
  299. {
  300. hsm_key_info_t ret = 0;
  301. switch (keyInfoIn) {
  302. case CAAM_KEY_PERSISTENT:
  303. ret = HSM_KEY_INFO_PERSISTENT;
  304. break;
  305. case CAAM_KEY_TRANSIENT:
  306. ret = HSM_KEY_INFO_TRANSIENT;
  307. break;
  308. case CAAM_KEY_KEK:
  309. ret = HSM_KEY_INFO_KEK;
  310. break;
  311. }
  312. return ret;
  313. }
  314. static int KeyFlagsToHSM(int flags)
  315. {
  316. int ret = 0;
  317. #ifdef HSM_OP_KEY_GENERATION_FLAGS_UPDATE
  318. if (flags & CAAM_UPDATE_KEY) {
  319. ret = HSM_OP_KEY_GENERATION_FLAGS_UPDATE;
  320. }
  321. #endif
  322. #ifdef HSM_OP_KEY_GENERATION_FLAGS_CREATE
  323. if (flags & CAAM_GENERATE_KEY) {
  324. ret = HSM_OP_KEY_GENERATION_FLAGS_CREATE;
  325. }
  326. #endif
  327. return ret;
  328. }
  329. /* generic generate key with HSM
  330. * return 0 on success
  331. */
  332. int wc_SECO_GenerateKey(int flags, int group, byte* out, int outSz,
  333. int keyTypeIn, int keyInfoIn, unsigned int* keyIdOut)
  334. {
  335. hsm_err_t err;
  336. hsm_hdl_t key_mgmt_hdl;
  337. open_svc_key_management_args_t key_mgmt_args;
  338. op_generate_key_args_t key_args;
  339. hsm_key_type_t keyType;
  340. hsm_key_info_t keyInfo;
  341. if (flags == CAAM_UPDATE_KEY && group != 0) {
  342. WOLFSSL_MSG("Group must be 0 if updating key");
  343. return BAD_FUNC_ARG;
  344. }
  345. keyType = KeyTypeToHSM(keyTypeIn);
  346. keyInfo = KeyInfoToHSM(keyInfoIn);
  347. if (wc_LockMutex(&caamMutex) != 0) {
  348. return BAD_MUTEX_E;
  349. }
  350. XMEMSET(&key_mgmt_args, 0, sizeof(key_mgmt_args));
  351. err = hsm_open_key_management_service(
  352. key_store_hdl, &key_mgmt_args, &key_mgmt_hdl);
  353. /* setup key arguments */
  354. if (err == HSM_NO_ERROR) {
  355. XMEMSET(&key_args, 0, sizeof(key_args));
  356. key_args.key_identifier = keyIdOut;
  357. key_args.out_size = outSz;
  358. key_args.out_key = out;
  359. /* default to strict operations with key in NVM */
  360. key_args.flags = KeyFlagsToHSM(flags) |
  361. HSM_OP_KEY_GENERATION_FLAGS_STRICT_OPERATION;
  362. key_args.key_group = group;
  363. key_args.key_info = keyInfo;
  364. key_args.key_type = keyType;
  365. #ifdef DEBUG_SECO
  366. printf("Generating key using:\n");
  367. printf("\tflags = %d\n", key_args.flags);
  368. printf("\tgroup = %d\n", key_args.key_group);
  369. printf("\tinfo = %d\n", key_args.key_info);
  370. printf("\ttype = %d\n", key_args.key_type);
  371. printf("\tout = %p\n", key_args.out_key);
  372. printf("\toutSZ = %d\n", key_args.out_size);
  373. #endif
  374. err = hsm_generate_key(key_mgmt_hdl, &key_args);
  375. if (err != HSM_NO_ERROR) {
  376. WOLFSSL_MSG("Key generation error");
  377. }
  378. #ifdef DEBUG_SECO
  379. if (err == HSM_NO_ERROR) {
  380. printf("KeyID generated = %u\n", *key_args.key_identifier);
  381. }
  382. #endif
  383. /* always try to close key management if open */
  384. if (hsm_close_key_management_service(key_mgmt_hdl) != HSM_NO_ERROR) {
  385. err = HSM_GENERAL_ERROR;
  386. }
  387. }
  388. else {
  389. WOLFSSL_MSG("Could not open key management");
  390. }
  391. wc_UnLockMutex(&caamMutex);
  392. if (wc_TranslateHSMError(0, err) == Success) {
  393. return 0;
  394. }
  395. else {
  396. return -1;
  397. }
  398. }
  399. /* delete a key
  400. * return 0 on success
  401. */
  402. int wc_SECO_DeleteKey(unsigned int keyId, int group, int keyTypeIn)
  403. {
  404. hsm_hdl_t key_mgmt_hdl;
  405. open_svc_key_management_args_t key_mgmt_args;
  406. op_manage_key_args_t del_args;
  407. hsm_err_t err;
  408. XMEMSET(&key_mgmt_args, 0, sizeof(key_mgmt_args));
  409. err = hsm_open_key_management_service(
  410. key_store_hdl, &key_mgmt_args, &key_mgmt_hdl);
  411. if (err == HSM_NO_ERROR) {
  412. XMEMSET(&del_args, 0, sizeof(del_args));
  413. del_args.key_identifier = &keyId;
  414. del_args.flags = HSM_OP_MANAGE_KEY_FLAGS_DELETE;
  415. del_args.key_type = KeyTypeToHSM(keyTypeIn);
  416. del_args.key_group = group;
  417. #ifdef DEBUG_SECO
  418. printf("Trying to delete key:\n");
  419. printf("\tkeyID : %u\n", keyId);
  420. printf("\tkey type : %d\n", del_args.key_type);
  421. printf("\tkey grp : %d\n", del_args.key_group);
  422. #endif
  423. err = hsm_manage_key(key_mgmt_hdl, &del_args);
  424. /* always try to close key management if open */
  425. if (hsm_close_key_management_service(key_mgmt_hdl) != HSM_NO_ERROR) {
  426. err = HSM_GENERAL_ERROR;
  427. }
  428. }
  429. if (wc_TranslateHSMError(0, err) == Success) {
  430. return 0;
  431. }
  432. else {
  433. return -1;
  434. }
  435. }
  436. #if defined(WOLFSSL_CMAC)
  437. void wc_SECO_CMACSetKeyID(Cmac* cmac, int keyId)
  438. {
  439. cmac->blackKey = keyId;
  440. }
  441. int wc_SECO_CMACGetKeyID(Cmac* cmac)
  442. {
  443. return cmac->blackKey;
  444. }
  445. #endif
  446. void wc_SECO_AesSetKeyID(Aes* aes, int keyId)
  447. {
  448. aes->blackKey = keyId;
  449. }
  450. int wc_SECO_AesGetKeyID(Aes* aes)
  451. {
  452. return aes->blackKey;
  453. }
  454. /* return 0 on success and fill in out buffer (must be 32 bytes) */
  455. int wc_SECO_ExportKEK(byte* out, byte outSz, byte isCommon)
  456. {
  457. hsm_err_t err;
  458. op_export_root_kek_args_t export_args;
  459. XMEMSET(&export_args, 0, sizeof(export_args));
  460. export_args.signed_message = NULL;
  461. export_args.signed_msg_size = 0;
  462. if (isCommon == 1) {
  463. export_args.flags = HSM_OP_EXPORT_ROOT_KEK_FLAGS_COMMON_KEK;
  464. }
  465. else {
  466. export_args.flags = HSM_OP_EXPORT_ROOT_KEK_FLAGS_UNIQUE_KEK;
  467. }
  468. export_args.out_root_kek = out;
  469. export_args.root_kek_size = outSz;
  470. err = hsm_export_root_key_encryption_key (hsm_session, &export_args);
  471. if (wc_TranslateHSMError(0, err) != Success) {
  472. return -1;
  473. }
  474. else {
  475. return 0;
  476. }
  477. }
  478. /* make a black key using HSM */
  479. static hsm_err_t wc_SECO_ECDSA_Make(unsigned int args[4], CAAM_BUFFER *buf,
  480. int sz)
  481. {
  482. hsm_key_type_t keyType;
  483. (void)sz;
  484. keyType = ECDSELtoHSM(args[1] ^ CAAM_ECDSA_KEYGEN_PD);
  485. if (wc_SECO_GenerateKey(HSM_OP_KEY_GENERATION_FLAGS_CREATE,
  486. 1,
  487. (byte*)buf[1].TheAddress,
  488. buf[1].Length,
  489. keyType,
  490. CAAM_KEY_TRANSIENT,
  491. (word32*)&buf[0].TheAddress) == 0) {
  492. return HSM_NO_ERROR;
  493. }
  494. else {
  495. return HSM_GENERAL_ERROR;
  496. }
  497. }
  498. /* sign a message (hash(msg)) using a hsm key */
  499. static hsm_err_t wc_SECO_ECDSA_Sign(unsigned int args[4], CAAM_BUFFER *buf,
  500. int sz)
  501. {
  502. hsm_err_t err;
  503. hsm_hdl_t sig_gen_hdl;
  504. open_svc_sign_gen_args_t open_args;
  505. op_generate_sign_args_t sig_args;
  506. byte sigOut[2*MAX_ECC_BYTES];
  507. if (args[3] != 32 && args[3] != 48) {
  508. WOLFSSL_MSG("Unexpected key size");
  509. return BAD_FUNC_ARG;
  510. }
  511. if (buf[1].Length != (int)args[3]) {
  512. WOLFSSL_MSG("Bad message input size");
  513. return BAD_FUNC_ARG;
  514. }
  515. if (wc_LockMutex(&caamMutex) != 0) {
  516. return BAD_MUTEX_E;
  517. }
  518. XMEMSET(&open_args, 0, sizeof(open_args));
  519. err = hsm_open_signature_generation_service(key_store_hdl, &open_args,
  520. &sig_gen_hdl);
  521. if (err == HSM_NO_ERROR) {
  522. XMEMSET(&sig_args, 0, sizeof(sig_args));
  523. sig_args.key_identifier = buf[0].TheAddress;
  524. sig_args.message = (uint8_t*)buf[1].TheAddress;
  525. sig_args.message_size = buf[1].Length;
  526. sig_args.signature = sigOut;
  527. sig_args.signature_size = buf[2].Length + buf[3].Length + 1;
  528. if (args[3] == 32) {
  529. sig_args.scheme_id = HSM_SIGNATURE_SCHEME_ECDSA_NIST_P256_SHA_256;
  530. }
  531. else {
  532. sig_args.scheme_id = HSM_SIGNATURE_SCHEME_ECDSA_NIST_P384_SHA_384;
  533. }
  534. sig_args.flags = HSM_OP_GENERATE_SIGN_FLAGS_INPUT_DIGEST;
  535. #ifdef DEBUG_SECO
  536. printf("Trying to create an ECC signature:\n");
  537. printf("\tkeyID : %u\n", sig_args.key_identifier);
  538. printf("\tmsg size : %d\n", sig_args.message_size);
  539. printf("\tsig size : %d\n", sig_args.signature_size);
  540. #endif
  541. err = hsm_generate_signature(sig_gen_hdl, &sig_args);
  542. /* always try to close sign service when open */
  543. if (hsm_close_signature_generation_service(sig_gen_hdl)
  544. != HSM_NO_ERROR) {
  545. err = HSM_GENERAL_ERROR;
  546. }
  547. }
  548. /* copy out r and s on success */
  549. if (err == HSM_NO_ERROR) {
  550. XMEMCPY((byte*)buf[2].TheAddress, sigOut, buf[2].Length);
  551. XMEMCPY((byte*)buf[3].TheAddress, sigOut + buf[2].Length,
  552. buf[3].Length);
  553. }
  554. wc_UnLockMutex(&caamMutex);
  555. (void)sz;
  556. if (wc_TranslateHSMError(0, err) != Success) {
  557. return -1;
  558. }
  559. else {
  560. return 0;
  561. }
  562. }
  563. /* verify a signature (hash(msg)) using HSM */
  564. static hsm_err_t wc_SECO_ECDSA_Verify(unsigned int args[4], CAAM_BUFFER *buf,
  565. int sz)
  566. {
  567. hsm_err_t err;
  568. hsm_hdl_t sig_ver_hdl;
  569. open_svc_sign_ver_args_t open_sig_ver_args;
  570. op_verify_sign_args_t sig_ver_args;
  571. hsm_verification_status_t verify;
  572. byte rsR[2*MAX_ECC_BYTES];
  573. word32 rsRSz = 2*MAX_ECC_BYTES;
  574. if (args[3] != 32 && args[3] != 48) {
  575. WOLFSSL_MSG("Unexpected key size");
  576. return BAD_FUNC_ARG;
  577. }
  578. if (buf[1].Length != (int)args[3]) {
  579. WOLFSSL_MSG("Bad message input size");
  580. return BAD_FUNC_ARG;
  581. }
  582. if (wc_LockMutex(&caamMutex) != 0) {
  583. return BAD_MUTEX_E;
  584. }
  585. XMEMSET(rsR, 0, rsRSz);
  586. XMEMCPY(rsR, (byte*)buf[2].TheAddress, buf[2].Length);
  587. XMEMCPY(rsR + buf[2].Length, (byte*)buf[3].TheAddress, buf[3].Length);
  588. rsRSz = buf[2].Length + buf[3].Length + 1; /* +1 for the HSM compression */
  589. XMEMSET(&open_sig_ver_args, 0, sizeof(open_sig_ver_args));
  590. err = hsm_open_signature_verification_service(hsm_session,
  591. &open_sig_ver_args, &sig_ver_hdl);
  592. if (err == HSM_NO_ERROR) {
  593. XMEMSET(&sig_ver_args, 0, sizeof(sig_ver_args));
  594. sig_ver_args.key = (uint8_t*)buf[0].TheAddress;
  595. sig_ver_args.key_size = buf[0].Length;
  596. sig_ver_args.message = (uint8_t*)buf[1].TheAddress;
  597. sig_ver_args.message_size = buf[1].Length;
  598. sig_ver_args.signature = rsR;
  599. sig_ver_args.signature_size = rsRSz;
  600. if (args[3] == 32) {
  601. sig_ver_args.scheme_id =
  602. HSM_SIGNATURE_SCHEME_ECDSA_NIST_P256_SHA_256;
  603. }
  604. else {
  605. sig_ver_args.scheme_id =
  606. HSM_SIGNATURE_SCHEME_ECDSA_NIST_P384_SHA_384;
  607. }
  608. sig_ver_args.flags = HSM_OP_VERIFY_SIGN_FLAGS_INPUT_DIGEST;
  609. #ifdef DEBUG_SECO
  610. {
  611. word32 i;
  612. printf("Trying to verify an ECC signature:\n");
  613. printf("\tpublic key : ");
  614. for (i = 0; i < sig_ver_args.key_size; i++)
  615. printf("%02X", sig_ver_args.key[i]);
  616. printf("\n");
  617. printf("\tsignature : ");
  618. for (i = 0; i < sig_ver_args.signature_size; i++)
  619. printf("%02X", sig_ver_args.signature[i]);
  620. printf("\n");
  621. printf("\tmsg size : %d\n", sig_ver_args.message_size);
  622. }
  623. #endif
  624. err = hsm_verify_signature (sig_ver_hdl, &sig_ver_args,
  625. &verify);
  626. if (verify != HSM_VERIFICATION_STATUS_SUCCESS) {
  627. WOLFSSL_MSG("Verification found bad signature");
  628. err = HSM_GENERAL_ERROR;
  629. }
  630. if (hsm_close_signature_verification_service(sig_ver_hdl) !=
  631. HSM_NO_ERROR) {
  632. err = HSM_GENERAL_ERROR;
  633. }
  634. }
  635. wc_UnLockMutex(&caamMutex);
  636. (void)sz;
  637. return err;
  638. }
  639. static hsm_err_t wc_SECO_CMAC(unsigned int args[4], CAAM_BUFFER* buf, int sz)
  640. {
  641. unsigned int blackKey;
  642. hsm_err_t err;
  643. hsm_hdl_t mac_hdl;
  644. open_svc_mac_args_t mac_svc_args;
  645. op_mac_one_go_args_t mac_args;
  646. hsm_mac_verification_status_t status;
  647. if ((args[0] & CAAM_ALG_FINAL) == 0) {
  648. WOLFSSL_MSG("CMAC expected only in final case!");
  649. return HSM_GENERAL_ERROR;
  650. }
  651. blackKey = args[2];
  652. /* black key listed as 0, the key needs to be imported */
  653. if (blackKey == 0) {
  654. int keyGroup = 1; /* group one was chosen arbitrarily */
  655. byte importIV[GCM_NONCE_MID_SZ];
  656. int importIVSz = GCM_NONCE_MID_SZ;
  657. int keyType = 0;
  658. WC_RNG rng;
  659. if (wc_InitRng(&rng) != 0) {
  660. WOLFSSL_MSG("RNG init for IV failed");
  661. return HSM_GENERAL_ERROR;
  662. }
  663. if (wc_RNG_GenerateBlock(&rng, importIV, importIVSz) != 0) {
  664. WOLFSSL_MSG("Generate IV failed");
  665. wc_FreeRng(&rng);
  666. return HSM_GENERAL_ERROR;
  667. }
  668. wc_FreeRng(&rng);
  669. switch (buf[0].Length) {
  670. case AES_128_KEY_SIZE: keyType = CAAM_KEYTYPE_AES128; break;
  671. case AES_192_KEY_SIZE: keyType = CAAM_KEYTYPE_AES192; break;
  672. case AES_256_KEY_SIZE: keyType = CAAM_KEYTYPE_AES256; break;
  673. }
  674. blackKey = wc_SECO_WrapKey(0, (byte*)buf[0].TheAddress, buf[0].Length,
  675. importIV, importIVSz, keyType, CAAM_KEY_TRANSIENT, keyGroup);
  676. if (blackKey == 0) {
  677. return WC_HW_E;
  678. }
  679. }
  680. err = hsm_open_mac_service(key_store_hdl, &mac_svc_args, &mac_hdl);
  681. if (err == HSM_NO_ERROR) {
  682. mac_args.key_identifier = blackKey; /* blackKey / HSM */
  683. mac_args.algorithm = HSM_OP_MAC_ONE_GO_ALGO_AES_CMAC;
  684. mac_args.flags = HSM_OP_MAC_ONE_GO_FLAGS_MAC_GENERATION;
  685. mac_args.payload = (uint8_t*)buf[2].TheAddress;
  686. mac_args.payload_size = buf[2].Length;
  687. mac_args.mac = (uint8_t*)buf[1].TheAddress;
  688. mac_args.mac_size = (buf[1].Length < AES_BLOCK_SIZE)? buf[1].Length:
  689. AES_BLOCK_SIZE;
  690. #ifdef DEBUG_SECO
  691. printf("CMAC arguments used:\n");
  692. printf("\tkey id = %d\n", mac_args.key_identifier);
  693. printf("\tpayload = %p\n", mac_args.payload);
  694. printf("\tpayload size = %d\n", mac_args.payload_size);
  695. printf("\tmac out = %p\n", mac_args.mac);
  696. printf("\tmac out size = %d\n", mac_args.mac_size);
  697. #endif
  698. err = hsm_mac_one_go(mac_hdl, &mac_args, &status);
  699. /* always try to close mac service if open */
  700. if (hsm_close_mac_service(mac_hdl) != HSM_NO_ERROR) {
  701. WOLFSSL_MSG("Error closing down mac service handle");
  702. err = HSM_GENERAL_ERROR;
  703. }
  704. }
  705. (void)sz;
  706. return err;
  707. }
  708. /* common code between CBC,ECB, and CCM modes */
  709. static hsm_err_t wc_SEC_AES_Common(unsigned int args[4], CAAM_BUFFER* buf,
  710. int sz, hsm_op_cipher_one_go_algo_t algo,
  711. uint8_t* in, int inSz, uint8_t* out, int outSz)
  712. {
  713. int dir;
  714. hsm_hdl_t cipher_hdl;
  715. open_svc_cipher_args_t open_args;
  716. op_cipher_one_go_args_t cipher_args;
  717. hsm_err_t err;
  718. XMEMSET(&open_args, 0, sizeof(open_args));
  719. err = hsm_open_cipher_service(key_store_hdl, &open_args, &cipher_hdl);
  720. if (err == HSM_NO_ERROR) {
  721. XMEMSET(&cipher_args, 0, sizeof(cipher_args));
  722. cipher_args.key_identifier = args[3]; /* black key / HSM */
  723. if (algo == HSM_CIPHER_ONE_GO_ALGO_AES_ECB) {
  724. cipher_args.iv_size = 0; /* no iv with AES-ECB */
  725. }
  726. else {
  727. cipher_args.iv = (uint8_t*)buf[1].TheAddress;
  728. cipher_args.iv_size = buf[1].Length;
  729. }
  730. cipher_args.cipher_algo = algo;
  731. dir = args[0] & 0xFFFF; /* extract direction enc/dec from input args */
  732. if (dir == CAAM_DEC) {
  733. cipher_args.flags = HSM_CIPHER_ONE_GO_FLAGS_DECRYPT;
  734. }
  735. else {
  736. cipher_args.flags = HSM_CIPHER_ONE_GO_FLAGS_ENCRYPT;
  737. }
  738. cipher_args.input = in;
  739. cipher_args.input_size = inSz;
  740. cipher_args.output = out;
  741. cipher_args.output_size = outSz;
  742. #ifdef DEBUG_SECO
  743. printf("AES Operation :\n");
  744. printf("\tkeyID : %u\n", cipher_args.key_identifier);
  745. printf("\tinput : %p\n", cipher_args.input);
  746. printf("\tinput sz : %d\n", cipher_args.input_size);
  747. printf("\toutput : %p\n", cipher_args.output);
  748. printf("\toutput sz : %d\n", cipher_args.output_size);
  749. printf("\tiv : %p\n", cipher_args.iv);
  750. printf("\tiv sz : %d\n", cipher_args.iv_size);
  751. #endif
  752. err = hsm_cipher_one_go(cipher_hdl, &cipher_args);
  753. /* always try to close cipher service if open */
  754. if (hsm_close_cipher_service(cipher_hdl) != HSM_NO_ERROR) {
  755. err = HSM_GENERAL_ERROR;
  756. }
  757. }
  758. (void)sz;
  759. return err;
  760. }
  761. static hsm_err_t wc_SECO_AESECB(unsigned int args[4], CAAM_BUFFER* buf, int sz)
  762. {
  763. return wc_SEC_AES_Common(args, buf, sz, HSM_CIPHER_ONE_GO_ALGO_AES_ECB,
  764. (uint8_t*)buf[1].TheAddress, buf[1].Length,
  765. (uint8_t*)buf[2].TheAddress, buf[2].Length);
  766. }
  767. static hsm_err_t wc_SECO_AESCBC(unsigned int args[4], CAAM_BUFFER* buf, int sz)
  768. {
  769. return wc_SEC_AES_Common(args, buf, sz, HSM_CIPHER_ONE_GO_ALGO_AES_CBC,
  770. (uint8_t*)buf[2].TheAddress, buf[2].Length,
  771. (uint8_t*)buf[3].TheAddress, buf[3].Length);
  772. }
  773. static hsm_err_t wc_SECO_AESCCM(unsigned int args[4], CAAM_BUFFER* buf, int sz)
  774. {
  775. hsm_err_t err;
  776. uint8_t* in;
  777. uint8_t* out;
  778. int inSz;
  779. int outSz;
  780. int dir;
  781. byte* cipherAndTag = NULL;
  782. int cipherAndTagSz = 0;
  783. if (buf[1].Length != 12) {
  784. WOLFSSL_MSG("SECO expecting nonce size of 12");
  785. return HSM_GENERAL_ERROR;
  786. }
  787. if (buf[4].Length != 16) {
  788. WOLFSSL_MSG("SECO expecting tag size of 16");
  789. return HSM_GENERAL_ERROR;
  790. }
  791. if (buf[5].Length != 0) {
  792. WOLFSSL_MSG("SECO expecting adata size of 0");
  793. return HSM_GENERAL_ERROR;
  794. }
  795. cipherAndTagSz = buf[4].Length + buf[2].Length;
  796. cipherAndTag = (byte*)XMALLOC(cipherAndTagSz, NULL,
  797. DYNAMIC_TYPE_TMP_BUFFER);
  798. dir = args[0] & 0xFFFF; /* get if doing enc or dec */
  799. if (dir == CAAM_ENC) {
  800. in = (uint8_t*)buf[2].TheAddress;
  801. inSz = buf[2].Length;
  802. out = cipherAndTag;
  803. outSz = cipherAndTagSz;
  804. }
  805. else {
  806. XMEMCPY(cipherAndTag, (uint8_t*)buf[2].TheAddress, buf[2].Length);
  807. XMEMCPY(cipherAndTag + buf[2].Length, (uint8_t*)buf[4].TheAddress,
  808. buf[4].Length);
  809. in = cipherAndTag;
  810. inSz = cipherAndTagSz;
  811. out = (uint8_t*)buf[3].TheAddress;
  812. outSz = buf[3].Length;
  813. }
  814. err = wc_SEC_AES_Common(args, buf, sz, HSM_CIPHER_ONE_GO_ALGO_AES_CCM,
  815. in, inSz, out, outSz);
  816. if (err == HSM_NO_ERROR) {
  817. if (dir == CAAM_ENC) {
  818. XMEMCPY((uint8_t*)buf[4].TheAddress, cipherAndTag + inSz,
  819. buf[4].Length);
  820. XMEMCPY((uint8_t*)buf[3].TheAddress, cipherAndTag, buf[3].Length);
  821. }
  822. }
  823. XFREE(cipherAndTag, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  824. return err;
  825. }
  826. static hsm_err_t wc_SECO_AESGCM(unsigned int args[4], CAAM_BUFFER* buf, int sz)
  827. {
  828. hsm_err_t err;
  829. hsm_hdl_t cipher_hdl;
  830. op_auth_enc_args_t auth_args;
  831. open_svc_cipher_args_t open_args;
  832. uint8_t* in;
  833. uint8_t* out;
  834. int inSz;
  835. int outSz;
  836. byte* cipherAndTag = NULL;
  837. int cipherAndTagSz = 0;
  838. int dir;
  839. dir = args[0] & 0xFFFF; /* extract direction enc/dec from input args */
  840. XMEMSET(&open_args, 0, sizeof(open_args));
  841. err = hsm_open_cipher_service(key_store_hdl, &open_args, &cipher_hdl);
  842. if (err == HSM_NO_ERROR) {
  843. cipherAndTagSz = buf[4].Length + buf[2].Length;
  844. cipherAndTag = (byte*)XMALLOC(cipherAndTagSz, NULL,
  845. DYNAMIC_TYPE_TMP_BUFFER);
  846. if (dir == CAAM_ENC) {
  847. in = (uint8_t*)buf[2].TheAddress;
  848. inSz = buf[2].Length;
  849. out = cipherAndTag;
  850. outSz = cipherAndTagSz;
  851. }
  852. else {
  853. XMEMCPY(cipherAndTag, (uint8_t*)buf[2].TheAddress, buf[2].Length);
  854. XMEMCPY(cipherAndTag + buf[2].Length, (uint8_t*)buf[4].TheAddress,
  855. buf[4].Length);
  856. in = cipherAndTag;
  857. inSz = cipherAndTagSz;
  858. out = (uint8_t*)buf[3].TheAddress;
  859. outSz = buf[3].Length;
  860. }
  861. auth_args.key_identifier = args[3]; /* black key / HSM */
  862. auth_args.iv = (uint8_t*)buf[1].TheAddress;
  863. auth_args.iv_size = buf[1].Length;
  864. auth_args.input = in;
  865. auth_args.input_size = inSz;
  866. auth_args.output = out;
  867. auth_args.output_size = outSz;
  868. auth_args.aad = (uint8_t*)buf[5].TheAddress;
  869. auth_args.aad_size = buf[5].Length;
  870. if (dir == CAAM_DEC) {
  871. auth_args.flags = HSM_AUTH_ENC_FLAGS_DECRYPT;
  872. }
  873. else {
  874. auth_args.flags = HSM_AUTH_ENC_FLAGS_ENCRYPT;
  875. }
  876. auth_args.ae_algo = HSM_AUTH_ENC_ALGO_AES_GCM;
  877. #ifdef DEBUG_SECO
  878. printf("AES GCM Operation :\n");
  879. printf("\tkeyID : %u\n", auth_args.key_identifier);
  880. printf("\tinput : %p\n", auth_args.input);
  881. printf("\tinput sz : %d\n", auth_args.input_size);
  882. printf("\toutput : %p\n", auth_args.output);
  883. printf("\toutput sz : %d\n", auth_args.output_size);
  884. printf("\tiv : %p\n", auth_args.iv);
  885. printf("\tiv sz : %d\n", auth_args.iv_size);
  886. printf("\taad : %p\n", auth_args.aad);
  887. printf("\taad sz : %d\n", auth_args.aad_size);
  888. #endif
  889. err = hsm_auth_enc(cipher_hdl, &auth_args);
  890. /* always try to close cipher service if open */
  891. if (hsm_close_cipher_service(cipher_hdl) != HSM_NO_ERROR) {
  892. err = HSM_GENERAL_ERROR;
  893. }
  894. }
  895. if (err == HSM_NO_ERROR) {
  896. if (dir == CAAM_ENC) {
  897. XMEMCPY((uint8_t*)buf[4].TheAddress, cipherAndTag + inSz,
  898. buf[4].Length);
  899. XMEMCPY((uint8_t*)buf[3].TheAddress, cipherAndTag, buf[3].Length);
  900. }
  901. }
  902. XFREE(cipherAndTag, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  903. (void)sz;
  904. return HSM_NO_ERROR;
  905. }
  906. /* use KEK to encrypt and import a key
  907. * return 0 on failure and new key ID on success */
  908. word32 wc_SECO_WrapKey(word32 keyId, byte* in, word32 inSz, byte* iv,
  909. word32 ivSz, int keyType, int keyInfo, int group)
  910. {
  911. op_manage_key_args_t key_args;
  912. hsm_hdl_t key_mgmt_hdl;
  913. Aes aes;
  914. int ret = 0;
  915. word32 outId = 0;
  916. byte *wrappedKey = NULL;
  917. word32 wrappedKeySz;
  918. open_svc_key_management_args_t key_mgmt_args;
  919. hsm_err_t err;
  920. if (group > MAX_GROUP) {
  921. WOLFSSL_MSG("group number is too large");
  922. return 0;
  923. }
  924. if (ivSz != (word32)GCM_NONCE_MID_SZ) {
  925. WOLFSSL_MSG("expected an IV size of 12");
  926. return 0;
  927. }
  928. /* iv + key + tag */
  929. wrappedKeySz = GCM_NONCE_MID_SZ + inSz + AES_BLOCK_SIZE;
  930. wrappedKey = (byte*)XMALLOC(wrappedKeySz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  931. if (wrappedKey == NULL) {
  932. WOLFSSL_MSG("Error malloc'ing buffer for wrapped key");
  933. return 0;
  934. }
  935. XMEMSET(&key_mgmt_args, 0, sizeof(key_mgmt_args));
  936. err = hsm_open_key_management_service(
  937. key_store_hdl, &key_mgmt_args, &key_mgmt_hdl);
  938. XMEMSET(&key_args, 0, sizeof(key_args));
  939. XMEMSET(wrappedKey, 0, wrappedKeySz);
  940. XMEMCPY(wrappedKey, iv, ivSz);
  941. key_args.flags = HSM_OP_MANAGE_KEY_FLAGS_IMPORT_CREATE;
  942. if (keyId == 0) { /* use the root unique key if no ID is provided */
  943. byte KEK[AES_256_KEY_SIZE];
  944. byte KEKSz = AES_256_KEY_SIZE;
  945. ret = wc_SECO_ExportKEK(KEK, KEKSz, 0);
  946. if (ret != 0) {
  947. WOLFSSL_MSG("error with getting KEK from device");
  948. }
  949. if (ret == 0) {
  950. /* use software implementation for encrypting with KEK */
  951. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  952. }
  953. if (ret == 0) {
  954. ret = wc_AesGcmSetKey(&aes, KEK, KEKSz);
  955. if (ret != 0) {
  956. WOLFSSL_MSG("error with AES-GCM set key");
  957. }
  958. }
  959. key_args.flags |= HSM_OP_MANAGE_KEY_FLAGS_PART_UNIQUE_ROOT_KEK;
  960. #if 0
  961. /* for now only using the unique kek, this would be for common */
  962. key_args.flags |= HSM_OP_MANAGE_KEY_FLAGS_COMMON_ROOT_KEK;
  963. #endif
  964. }
  965. else {
  966. wc_AesInit(&aes, NULL, WOLFSSL_SECO_DEVID);
  967. wc_SECO_AesSetKeyID(&aes, keyId);
  968. }
  969. if (ret == 0) {
  970. ret = wc_AesGcmEncrypt(&aes, wrappedKey + ivSz, in, inSz,
  971. wrappedKey, ivSz, wrappedKey + ivSz + inSz, AES_BLOCK_SIZE,
  972. NULL, 0);
  973. if (ret != 0) {
  974. WOLFSSL_MSG("error with AES-GCM encrypt when wrapping key");
  975. }
  976. }
  977. if (err == HSM_NO_ERROR) {
  978. key_args.key_identifier = &outId;
  979. key_args.kek_identifier = keyId;
  980. key_args.key_group = group;
  981. key_args.key_type = KeyTypeToHSM(keyType);
  982. key_args.key_info = KeyInfoToHSM(keyInfo);
  983. key_args.input_data = wrappedKey;
  984. key_args.input_size = wrappedKeySz;
  985. #ifdef DEBUG_SECO
  986. {
  987. word32 i;
  988. printf("Import Key Operation :\n");
  989. printf("\tkey ID : %u\n", *key_args.key_identifier);
  990. printf("\tkEK ID : %u\n", key_args.kek_identifier);
  991. printf("\tflags : %u\n", key_args.flags);
  992. printf("\tgroup : %u\n", key_args.key_group);
  993. printf("\tkey type : %d\n", key_args.key_type);
  994. printf("\tkey info : %d\n", key_args.key_info);
  995. printf("\tkey input Size [iv | key | tag ]: %d\n", key_args.input_size);
  996. printf("\t[iv] = ");
  997. for (i = 0; i < 12; i++)
  998. printf("%02X", key_args.input_data[i]);
  999. printf("\n");
  1000. printf("\t[enc] = ");
  1001. for (i = 12; i < 12 + inSz; i++)
  1002. printf("%02X", key_args.input_data[i]);
  1003. printf("\n");
  1004. printf("\t[tag] = ");
  1005. for (i = 12 + inSz; i < 12 + inSz + 16; i++)
  1006. printf("%02X", key_args.input_data[i]);
  1007. printf("\n");
  1008. }
  1009. #endif
  1010. /* only try to import if the AES-GCM encrypt was successful */
  1011. if (ret == 0) {
  1012. err = hsm_manage_key(key_mgmt_hdl, &key_args);
  1013. }
  1014. #ifdef DEBUG_SECO
  1015. if (err == HSM_NO_ERROR) {
  1016. printf("Result of Import Key Operation :\n");
  1017. printf("\tkey ID : %u\n", *key_args.key_identifier);
  1018. }
  1019. #endif
  1020. /* always try to close key management if open */
  1021. if (hsm_close_key_management_service(key_mgmt_hdl) != HSM_NO_ERROR) {
  1022. err = HSM_GENERAL_ERROR;
  1023. }
  1024. }
  1025. if (wrappedKey != NULL) {
  1026. XFREE(wrappedKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1027. }
  1028. if (wc_TranslateHSMError(0, err) != Success) {
  1029. return 0;
  1030. }
  1031. else {
  1032. return *key_args.key_identifier;
  1033. }
  1034. }
  1035. /* trasnlates the HSM error to wolfSSL error and does debug print out */
  1036. int wc_TranslateHSMError(int current, hsm_err_t err)
  1037. {
  1038. int ret = -1;
  1039. switch (err) {
  1040. case HSM_NO_ERROR:
  1041. ret = Success;
  1042. break;
  1043. case HSM_INVALID_MESSAGE:
  1044. WOLFSSL_MSG("SECO HSM: Invalid/unknown msg");
  1045. break;
  1046. case HSM_INVALID_ADDRESS:
  1047. WOLFSSL_MSG("SECO HSM: Invalid address");
  1048. break;
  1049. case HSM_UNKNOWN_ID:
  1050. WOLFSSL_MSG("SECO HSM: unknown ID");
  1051. break;
  1052. case HSM_INVALID_PARAM:
  1053. WOLFSSL_MSG("SECO HSM: invalid param");
  1054. break;
  1055. case HSM_NVM_ERROR:
  1056. WOLFSSL_MSG("SECO HSM: generic nvm error");
  1057. break;
  1058. case HSM_OUT_OF_MEMORY:
  1059. WOLFSSL_MSG("SECO HSM: out of memory");
  1060. break;
  1061. case HSM_UNKNOWN_HANDLE:
  1062. WOLFSSL_MSG("SECO HSM: unknown handle");
  1063. break;
  1064. case HSM_UNKNOWN_KEY_STORE:
  1065. WOLFSSL_MSG("SECO HSM: unknown key store");
  1066. break;
  1067. case HSM_KEY_STORE_AUTH:
  1068. WOLFSSL_MSG("SECO HSM: key store auth error");
  1069. break;
  1070. case HSM_KEY_STORE_ERROR:
  1071. WOLFSSL_MSG("SECO HSM: key store error");
  1072. break;
  1073. case HSM_ID_CONFLICT:
  1074. WOLFSSL_MSG("SECO HSM: id conflict");
  1075. break;
  1076. case HSM_RNG_NOT_STARTED:
  1077. WOLFSSL_MSG("SECO HSM: RNG not started");
  1078. break;
  1079. case HSM_CMD_NOT_SUPPORTED:
  1080. WOLFSSL_MSG("SECO HSM: CMD not support");
  1081. break;
  1082. case HSM_INVALID_LIFECYCLE:
  1083. WOLFSSL_MSG("SECO HSM: invalid lifecycle");
  1084. break;
  1085. case HSM_KEY_STORE_CONFLICT:
  1086. WOLFSSL_MSG("SECO HSM: store conflict");
  1087. break;
  1088. case HSM_KEY_STORE_COUNTER:
  1089. WOLFSSL_MSG("SECO HSM: key store counter error");
  1090. break;
  1091. case HSM_FEATURE_NOT_SUPPORTED:
  1092. WOLFSSL_MSG("SECO HSM: feature not supported");
  1093. break;
  1094. case HSM_SELF_TEST_FAILURE:
  1095. WOLFSSL_MSG("SECO HSM: self test failure");
  1096. break;
  1097. case HSM_NOT_READY_RATING:
  1098. WOLFSSL_MSG("SECO HSM: not ready");
  1099. break;
  1100. case HSM_FEATURE_DISABLED:
  1101. WOLFSSL_MSG("SECO HSM: feature is disabled error");
  1102. break;
  1103. case HSM_GENERAL_ERROR:
  1104. WOLFSSL_MSG("SECO HSM: general error found");
  1105. break;
  1106. default:
  1107. WOLFSSL_MSG("SECO HSM: unknown error value found");
  1108. }
  1109. if (current != 0) {
  1110. WOLFSSL_MSG("In an error state before SECO HSM error");
  1111. ret = current;
  1112. }
  1113. return ret;
  1114. }
  1115. /* Do a synchronous operations and block till done
  1116. * returns Success on success */
  1117. int SynchronousSendRequest(int type, unsigned int args[4], CAAM_BUFFER *buf,
  1118. int sz)
  1119. {
  1120. int ret = 0;
  1121. hsm_err_t err = HSM_NO_ERROR;
  1122. CAAM_ADDRESS pubkey, privkey;
  1123. switch (type) {
  1124. case CAAM_ENTROPY:
  1125. err = wc_SECO_RNG(args, buf, sz);
  1126. break;
  1127. case CAAM_SHA224:
  1128. case CAAM_SHA256:
  1129. case CAAM_SHA384:
  1130. case CAAM_SHA512:
  1131. err = wc_SECO_Hash(args, buf, sz, type);
  1132. break;
  1133. case CAAM_GET_PART:
  1134. case CAAM_FREE_PART:
  1135. case CAAM_FIND_PART:
  1136. case CAAM_READ_PART:
  1137. case CAAM_WRITE_PART:
  1138. break;
  1139. case CAAM_ECDSA_KEYPAIR:
  1140. err = wc_SECO_ECDSA_Make(args, buf, sz);
  1141. break;
  1142. case CAAM_ECDSA_VERIFY:
  1143. err = wc_SECO_ECDSA_Verify(args, buf, sz);
  1144. break;
  1145. case CAAM_ECDSA_SIGN:
  1146. err = wc_SECO_ECDSA_Sign(args, buf, sz);
  1147. break;
  1148. case CAAM_ECDSA_ECDH:
  1149. break;
  1150. case CAAM_BLOB_ENCAP:
  1151. case CAAM_BLOB_DECAP:
  1152. break;
  1153. case CAAM_AESECB:
  1154. err = wc_SECO_AESECB(args, buf, sz);
  1155. break;
  1156. case CAAM_AESCBC:
  1157. err = wc_SECO_AESCBC(args, buf, sz);
  1158. break;
  1159. case CAAM_AESCCM:
  1160. err = wc_SECO_AESCCM(args, buf, sz);
  1161. break;
  1162. case CAAM_AESGCM:
  1163. err = wc_SECO_AESGCM(args, buf, sz);
  1164. break;
  1165. case CAAM_CMAC:
  1166. err = wc_SECO_CMAC(args, buf, sz);
  1167. break;
  1168. case CAAM_FIFO_S:
  1169. default:
  1170. WOLFSSL_MSG("Unknown/unsupported type");
  1171. ret = -1;
  1172. }
  1173. (void)pubkey;
  1174. (void)privkey;
  1175. (void)sz;
  1176. return wc_TranslateHSMError(ret, err);
  1177. }
  1178. #endif /* WOLFSSL_SECO_CAAM */