stsafe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /* stsafe.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. #include <wolfssl/wolfcrypt/types.h>
  22. #include <wolfssl/wolfcrypt/port/st/stsafe.h>
  23. #include <wolfssl/wolfcrypt/logging.h>
  24. #ifndef STSAFE_INTERFACE_PRINTF
  25. #define STSAFE_INTERFACE_PRINTF(...) WC_DO_NOTHING
  26. #endif
  27. #ifdef WOLFSSL_STSAFEA100
  28. int SSL_STSAFE_LoadDeviceCertificate(byte** pRawCertificate,
  29. word32* pRawCertificateLen)
  30. {
  31. int err;
  32. if (pRawCertificate == NULL || pRawCertificateLen == NULL) {
  33. return BAD_FUNC_ARG;
  34. }
  35. #ifdef USE_STSAFE_VERBOSE
  36. WOLFSSL_MSG("SSL_STSAFE_LoadDeviceCertificate");
  37. #endif
  38. /* Try reading device certificate from ST-SAFE Zone 0 */
  39. err = stsafe_interface_read_device_certificate_raw(
  40. pRawCertificate, (uint32_t*)pRawCertificateLen);
  41. if (err == STSAFE_A_OK) {
  42. #if 0
  43. /* example for loading into WOLFSSL_CTX */
  44. err = wolfSSL_CTX_use_certificate_buffer(ctx,
  45. *pRawCertificate, *pRawCertificateLen, SSL_FILETYPE_ASN1);
  46. if (err != WOLFSSL_SUCCESS) {
  47. /* failed */
  48. }
  49. /* can free now */
  50. XFREE(*pRawCertificate, NULL, DYNAMIC_TEMP_BUFFER);
  51. *pRawCertificate = NULL;
  52. #endif
  53. }
  54. else {
  55. err = WC_HW_E;
  56. }
  57. return err;
  58. }
  59. #ifdef HAVE_PK_CALLBACKS
  60. /**
  61. * \brief Key Gen Callback (used by TLS server)
  62. */
  63. int SSL_STSAFE_CreateKeyCb(WOLFSSL* ssl, ecc_key* key, word32 keySz,
  64. int ecc_curve, void* ctx)
  65. {
  66. int err;
  67. byte pubKeyRaw[STSAFE_MAX_PUBKEY_RAW_LEN];
  68. StSafeA_KeySlotNumber slot;
  69. StSafeA_CurveId curve_id;
  70. (void)ssl;
  71. (void)ctx;
  72. #ifdef USE_STSAFE_VERBOSE
  73. WOLFSSL_MSG("CreateKeyCb: STSAFE");
  74. #endif
  75. /* get curve */
  76. curve_id = stsafe_get_ecc_curve_id(ecc_curve);
  77. /* generate new ephemeral key on device */
  78. err = stsafe_interface_create_key(&slot, curve_id, (uint8_t*)&pubKeyRaw[0]);
  79. if (err != STSAFE_A_OK) {
  80. #ifdef USE_STSAFE_VERBOSE
  81. STSAFE_INTERFACE_PRINTF("stsafe_interface_create_key error: %d\n", err);
  82. #endif
  83. err = WC_HW_E;
  84. return err;
  85. }
  86. /* load generated public key into key, used by wolfSSL */
  87. err = wc_ecc_import_unsigned(key, &pubKeyRaw[0], &pubKeyRaw[keySz],
  88. NULL, ecc_curve);
  89. return err;
  90. }
  91. /**
  92. * \brief Verify Peer Cert Callback.
  93. */
  94. int SSL_STSAFE_VerifyPeerCertCb(WOLFSSL* ssl,
  95. const unsigned char* sig, unsigned int sigSz,
  96. const unsigned char* hash, unsigned int hashSz,
  97. const unsigned char* keyDer, unsigned int keySz,
  98. int* result, void* ctx)
  99. {
  100. int err;
  101. byte sigRS[STSAFE_MAX_SIG_LEN];
  102. byte *r, *s;
  103. word32 r_len = STSAFE_MAX_SIG_LEN/2, s_len = STSAFE_MAX_SIG_LEN/2;
  104. byte pubKeyX[STSAFE_MAX_PUBKEY_RAW_LEN/2];
  105. byte pubKeyY[STSAFE_MAX_PUBKEY_RAW_LEN/2];
  106. word32 pubKeyX_len = sizeof(pubKeyX);
  107. word32 pubKeyY_len = sizeof(pubKeyY);
  108. ecc_key key;
  109. word32 inOutIdx = 0;
  110. StSafeA_CurveId curve_id = STSAFE_A_NIST_P_256;
  111. int ecc_curve;
  112. (void)ssl;
  113. (void)ctx;
  114. #ifdef USE_STSAFE_VERBOSE
  115. WOLFSSL_MSG("VerifyPeerCertCB: STSAFE");
  116. #endif
  117. err = wc_ecc_init(&key);
  118. if (err != 0) {
  119. return err;
  120. }
  121. /* Decode the public key */
  122. err = wc_EccPublicKeyDecode(keyDer, &inOutIdx, &key, keySz);
  123. if (err == 0) {
  124. /* Extract Raw X and Y coordinates of the public key */
  125. err = wc_ecc_export_public_raw(&key, pubKeyX, &pubKeyX_len,
  126. pubKeyY, &pubKeyY_len);
  127. }
  128. if (err == 0) {
  129. int key_sz;
  130. /* determine curve */
  131. ecc_curve = key.dp->id;
  132. curve_id = stsafe_get_ecc_curve_id(ecc_curve);
  133. key_sz = stsafe_get_key_size(curve_id);
  134. /* Extract R and S from signature */
  135. XMEMSET(sigRS, 0, sizeof(sigRS));
  136. r = &sigRS[0];
  137. s = &sigRS[key_sz];
  138. err = wc_ecc_sig_to_rs(sig, sigSz, r, &r_len, s, &s_len);
  139. (void)r_len;
  140. (void)s_len;
  141. }
  142. if (err == 0) {
  143. /* Verify signature */
  144. err = stsafe_interface_verify(curve_id, (uint8_t*)hash, sigRS,
  145. pubKeyX, pubKeyY, (int32_t*)result);
  146. if (err != STSAFE_A_OK) {
  147. #ifdef USE_STSAFE_VERBOSE
  148. STSAFE_INTERFACE_PRINTF("stsafe_interface_verify error: %d\n", err);
  149. #endif
  150. err = -err;
  151. }
  152. }
  153. wc_ecc_free(&key);
  154. return err;
  155. }
  156. /**
  157. * \brief Sign Certificate Callback.
  158. */
  159. int SSL_STSAFE_SignCertificateCb(WOLFSSL* ssl, const byte* in,
  160. word32 inSz, byte* out, word32* outSz,
  161. const byte* key, word32 keySz, void* ctx)
  162. {
  163. int err;
  164. byte digest[STSAFE_MAX_KEY_LEN];
  165. byte sigRS[STSAFE_MAX_SIG_LEN];
  166. byte *r, *s;
  167. StSafeA_CurveId curve_id;
  168. int key_sz;
  169. (void)ssl;
  170. (void)ctx;
  171. #ifdef USE_STSAFE_VERBOSE
  172. WOLFSSL_MSG("SignCertificateCb: STSAFE");
  173. #endif
  174. curve_id = stsafe_get_curve_mode();
  175. key_sz = stsafe_get_key_size(curve_id);
  176. /* Build input digest */
  177. if (inSz > key_sz)
  178. inSz = key_sz;
  179. XMEMSET(&digest[0], 0, sizeof(digest));
  180. XMEMCPY(&digest[key_sz - inSz], in, inSz);
  181. /* Sign using slot 0: Result is R then S */
  182. /* Sign will always use the curve type in slot 0 (the TLS curve needs to match) */
  183. XMEMSET(sigRS, 0, sizeof(sigRS));
  184. err = stsafe_interface_sign(STSAFE_A_SLOT_0, curve_id, digest, sigRS);
  185. if (err != STSAFE_A_OK) {
  186. #ifdef USE_STSAFE_VERBOSE
  187. STSAFE_INTERFACE_PRINTF("stsafe_interface_sign error: %d\n", err);
  188. #endif
  189. err = WC_HW_E;
  190. return err;
  191. }
  192. /* Convert R and S to signature */
  193. r = &sigRS[0];
  194. s = &sigRS[key_sz];
  195. err = wc_ecc_rs_raw_to_sig((const byte*)r, key_sz, (const byte*)s, key_sz,
  196. out, outSz);
  197. if (err != 0) {
  198. #ifdef USE_STSAFE_VERBOSE
  199. WOLFSSL_MSG("Error converting RS to Signature");
  200. #endif
  201. }
  202. return err;
  203. }
  204. /**
  205. * \brief Create pre master secret using peer's public key and self private key.
  206. */
  207. int SSL_STSAFE_SharedSecretCb(WOLFSSL* ssl, ecc_key* otherKey,
  208. unsigned char* pubKeyDer, unsigned int* pubKeySz,
  209. unsigned char* out, unsigned int* outlen,
  210. int side, void* ctx)
  211. {
  212. int err;
  213. byte otherKeyX[STSAFE_MAX_KEY_LEN];
  214. byte otherKeyY[STSAFE_MAX_KEY_LEN];
  215. word32 otherKeyX_len = sizeof(otherKeyX);
  216. word32 otherKeyY_len = sizeof(otherKeyY);
  217. byte pubKeyRaw[STSAFE_MAX_PUBKEY_RAW_LEN];
  218. StSafeA_KeySlotNumber slot = STSAFE_A_SLOT_0;
  219. StSafeA_CurveId curve_id;
  220. ecc_key tmpKey;
  221. int ecc_curve;
  222. int key_sz;
  223. (void)ssl;
  224. (void)ctx;
  225. #ifdef USE_STSAFE_VERBOSE
  226. WOLFSSL_MSG("SharedSecretCb: STSAFE");
  227. #endif
  228. err = wc_ecc_init(&tmpKey);
  229. if (err != 0) {
  230. return err;
  231. }
  232. /* set curve */
  233. ecc_curve = otherKey->dp->id;
  234. curve_id = stsafe_get_ecc_curve_id(ecc_curve);
  235. key_sz = stsafe_get_key_size(curve_id);
  236. /* for client: create and export public key */
  237. if (side == WOLFSSL_CLIENT_END) {
  238. /* Export otherKey raw X and Y */
  239. err = wc_ecc_export_public_raw(otherKey,
  240. &otherKeyX[0], (word32*)&otherKeyX_len,
  241. &otherKeyY[0], (word32*)&otherKeyY_len);
  242. if (err != 0) {
  243. return err;
  244. }
  245. err = stsafe_interface_create_key(&slot, curve_id, (uint8_t*)&pubKeyRaw[0]);
  246. if (err != STSAFE_A_OK) {
  247. #ifdef USE_STSAFE_VERBOSE
  248. STSAFE_INTERFACE_PRINTF("stsafe_interface_create_key error: %d\n", err);
  249. #endif
  250. err = WC_HW_E;
  251. return err;
  252. }
  253. /* convert raw unsigned public key to X.963 format for TLS */
  254. err = wc_ecc_init(&tmpKey);
  255. if (err == 0) {
  256. err = wc_ecc_import_unsigned(&tmpKey, &pubKeyRaw[0], &pubKeyRaw[key_sz],
  257. NULL, ecc_curve);
  258. if (err == 0) {
  259. err = wc_ecc_export_x963(&tmpKey, pubKeyDer, pubKeySz);
  260. }
  261. wc_ecc_free(&tmpKey);
  262. }
  263. }
  264. /* for server: import public key */
  265. else if (side == WOLFSSL_SERVER_END) {
  266. /* import peer's key and export as raw unsigned for hardware */
  267. err = wc_ecc_import_x963_ex(pubKeyDer, *pubKeySz, &tmpKey, ecc_curve);
  268. if (err == 0) {
  269. err = wc_ecc_export_public_raw(&tmpKey, otherKeyX, &otherKeyX_len,
  270. otherKeyY, &otherKeyY_len);
  271. }
  272. }
  273. else {
  274. err = BAD_FUNC_ARG;
  275. }
  276. wc_ecc_free(&tmpKey);
  277. if (err != 0) {
  278. return err;
  279. }
  280. /* Compute shared secret */
  281. err = stsafe_interface_shared_secret(
  282. #ifdef WOLFSSL_STSAFE_TAKES_SLOT
  283. slot,
  284. #endif
  285. curve_id, &otherKeyX[0], &otherKeyY[0],
  286. out, (int32_t*)outlen);
  287. if (err != STSAFE_A_OK) {
  288. #ifdef USE_STSAFE_VERBOSE
  289. STSAFE_INTERFACE_PRINTF("stsafe_interface_shared_secret error: %d\n", err);
  290. #endif
  291. err = WC_HW_E;
  292. }
  293. return err;
  294. }
  295. int SSL_STSAFE_SetupPkCallbacks(WOLFSSL_CTX* ctx)
  296. {
  297. wolfSSL_CTX_SetEccKeyGenCb(ctx, SSL_STSAFE_CreateKeyCb);
  298. wolfSSL_CTX_SetEccSignCb(ctx, SSL_STSAFE_SignCertificateCb);
  299. wolfSSL_CTX_SetEccVerifyCb(ctx, SSL_STSAFE_VerifyPeerCertCb);
  300. wolfSSL_CTX_SetEccSharedSecretCb(ctx, SSL_STSAFE_SharedSecretCb);
  301. wolfSSL_CTX_SetDevId(ctx, 0); /* enables wolfCrypt `wc_ecc_*` ST-Safe use */
  302. return 0;
  303. }
  304. int SSL_STSAFE_SetupPkCallbackCtx(WOLFSSL* ssl, void* user_ctx)
  305. {
  306. wolfSSL_SetEccKeyGenCtx(ssl, user_ctx);
  307. wolfSSL_SetEccSharedSecretCtx(ssl, user_ctx);
  308. wolfSSL_SetEccSignCtx(ssl, user_ctx);
  309. wolfSSL_SetEccVerifyCtx(ssl, user_ctx);
  310. return 0;
  311. }
  312. #endif /* HAVE_PK_CALLBACKS */
  313. #ifdef WOLF_CRYPTO_CB
  314. int wolfSSL_STSAFE_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
  315. {
  316. int rc = CRYPTOCB_UNAVAILABLE;
  317. wolfSTSAFE_CryptoCb_Ctx* stsCtx = (wolfSTSAFE_CryptoCb_Ctx*)ctx;
  318. if (info == NULL || ctx == NULL)
  319. return BAD_FUNC_ARG;
  320. (void)devId;
  321. (void)stsCtx;
  322. if (info->algo_type == WC_ALGO_TYPE_SEED) {
  323. /* use the STSAFE hardware for RNG seed */
  324. #if !defined(WC_NO_RNG) && defined(USE_STSAFE_RNG_SEED)
  325. while (info->seed.sz > 0) {
  326. rc = stsafe_interface_getrandom(info->seed.seed, info->seed.sz);
  327. if (rc < 0) {
  328. return rc;
  329. }
  330. info->seed.seed += rc;
  331. info->seed.sz -= rc;
  332. }
  333. rc = 0;
  334. #else
  335. rc = CRYPTOCB_UNAVAILABLE;
  336. #endif
  337. }
  338. #ifdef HAVE_ECC
  339. else if (info->algo_type == WC_ALGO_TYPE_PK) {
  340. #ifdef USE_STSAFE_VERBOSE
  341. STSAFE_INTERFACE_PRINTF("STSAFE Pk: Type %d\n", info->pk.type);
  342. #endif
  343. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  344. byte pubKeyRaw[STSAFE_MAX_PUBKEY_RAW_LEN];
  345. StSafeA_KeySlotNumber slot;
  346. StSafeA_CurveId curve_id;
  347. int ecc_curve, key_sz;
  348. WOLFSSL_MSG("STSAFE: ECC KeyGen");
  349. /* get curve */
  350. ecc_curve = info->pk.eckg.curveId;
  351. curve_id = stsafe_get_ecc_curve_id(ecc_curve);
  352. key_sz = stsafe_get_key_size(curve_id);
  353. /* generate new ephemeral key on device */
  354. rc = stsafe_interface_create_key(&slot, curve_id,
  355. (uint8_t*)pubKeyRaw);
  356. if (rc != STSAFE_A_OK) {
  357. #ifdef USE_STSAFE_VERBOSE
  358. STSAFE_INTERFACE_PRINTF("stsafe_interface_create_key error: %d\n", rc);
  359. #endif
  360. rc = WC_HW_E;
  361. return rc;
  362. }
  363. /* load generated public key into key, used by wolfSSL */
  364. rc = wc_ecc_import_unsigned(info->pk.eckg.key, pubKeyRaw,
  365. &pubKeyRaw[key_sz], NULL, ecc_curve);
  366. }
  367. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  368. byte digest[STSAFE_MAX_KEY_LEN];
  369. byte sigRS[STSAFE_MAX_SIG_LEN];
  370. byte *r, *s;
  371. StSafeA_CurveId curve_id;
  372. word32 inSz = info->pk.eccsign.inlen;
  373. int key_sz;
  374. WOLFSSL_MSG("STSAFE: ECC Sign");
  375. curve_id = stsafe_get_curve_mode();
  376. key_sz = stsafe_get_key_size(curve_id);
  377. /* truncate input to match key size */
  378. if (inSz > key_sz)
  379. inSz = key_sz;
  380. /* Build input digest */
  381. XMEMSET(&digest[0], 0, sizeof(digest));
  382. XMEMCPY(&digest[key_sz - inSz], info->pk.eccsign.in, inSz);
  383. /* Sign using slot 0: Result is R then S */
  384. /* Sign will always use the curve type in slot 0
  385. (the TLS curve needs to match) */
  386. XMEMSET(sigRS, 0, sizeof(sigRS));
  387. rc = stsafe_interface_sign(STSAFE_A_SLOT_0, curve_id,
  388. (uint8_t*)info->pk.eccsign.in, sigRS);
  389. if (rc != STSAFE_A_OK) {
  390. #ifdef USE_STSAFE_VERBOSE
  391. STSAFE_INTERFACE_PRINTF("stsafe_interface_sign error: %d\n", rc);
  392. #endif
  393. rc = WC_HW_E;
  394. return rc;
  395. }
  396. /* Convert R and S to signature */
  397. r = &sigRS[0];
  398. s = &sigRS[key_sz];
  399. rc = wc_ecc_rs_raw_to_sig((const byte*)r, key_sz, (const byte*)s,
  400. key_sz, info->pk.eccsign.out, info->pk.eccsign.outlen);
  401. if (rc != 0) {
  402. WOLFSSL_MSG("Error converting RS to Signature");
  403. }
  404. }
  405. else if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
  406. byte sigRS[STSAFE_MAX_SIG_LEN];
  407. byte *r, *s;
  408. word32 r_len = STSAFE_MAX_SIG_LEN/2, s_len = STSAFE_MAX_SIG_LEN/2;
  409. byte pubKeyX[STSAFE_MAX_PUBKEY_RAW_LEN/2];
  410. byte pubKeyY[STSAFE_MAX_PUBKEY_RAW_LEN/2];
  411. word32 pubKeyX_len = sizeof(pubKeyX);
  412. word32 pubKeyY_len = sizeof(pubKeyY);
  413. StSafeA_CurveId curve_id;
  414. int ecc_curve, key_sz;
  415. WOLFSSL_MSG("STSAFE: ECC Verify");
  416. if (info->pk.eccverify.key == NULL)
  417. return BAD_FUNC_ARG;
  418. /* determine curve */
  419. ecc_curve = info->pk.eccverify.key->dp->id;
  420. curve_id = stsafe_get_ecc_curve_id(ecc_curve);
  421. key_sz = stsafe_get_key_size(curve_id);
  422. /* Extract Raw X and Y coordinates of the public key */
  423. rc = wc_ecc_export_public_raw(info->pk.eccverify.key,
  424. pubKeyX, &pubKeyX_len,
  425. pubKeyY, &pubKeyY_len);
  426. if (rc == 0) {
  427. /* Extract R and S from signature */
  428. XMEMSET(sigRS, 0, sizeof(sigRS));
  429. r = &sigRS[0];
  430. s = &sigRS[key_sz];
  431. rc = wc_ecc_sig_to_rs(info->pk.eccverify.sig,
  432. info->pk.eccverify.siglen, r, &r_len, s, &s_len);
  433. (void)r_len;
  434. (void)s_len;
  435. }
  436. if (rc == 0) {
  437. /* Verify signature */
  438. rc = stsafe_interface_verify(curve_id,
  439. (uint8_t*)info->pk.eccverify.hash, sigRS, pubKeyX, pubKeyY,
  440. (int32_t*)info->pk.eccverify.res);
  441. if (rc != STSAFE_A_OK) {
  442. #ifdef USE_STSAFE_VERBOSE
  443. STSAFE_INTERFACE_PRINTF("stsafe_interface_verify error: %d\n", rc);
  444. #endif
  445. rc = -rc;
  446. }
  447. }
  448. }
  449. else if (info->pk.type == WC_PK_TYPE_ECDH) {
  450. byte otherKeyX[STSAFE_MAX_KEY_LEN];
  451. byte otherKeyY[STSAFE_MAX_KEY_LEN];
  452. word32 otherKeyX_len = sizeof(otherKeyX);
  453. word32 otherKeyY_len = sizeof(otherKeyY);
  454. StSafeA_CurveId curve_id;
  455. int ecc_curve;
  456. WOLFSSL_MSG("STSAFE: PMS");
  457. if (info->pk.ecdh.public_key == NULL)
  458. return BAD_FUNC_ARG;
  459. /* get curve */
  460. ecc_curve = info->pk.ecdh.public_key->dp->id;
  461. curve_id = stsafe_get_ecc_curve_id(ecc_curve);
  462. /* Export otherKey raw X and Y */
  463. rc = wc_ecc_export_public_raw(info->pk.ecdh.public_key,
  464. &otherKeyX[0], (word32*)&otherKeyX_len,
  465. &otherKeyY[0], (word32*)&otherKeyY_len);
  466. if (rc == 0) {
  467. /* Compute shared secret */
  468. *info->pk.ecdh.outlen = 0;
  469. rc = stsafe_interface_shared_secret(
  470. #ifdef WOLFSSL_STSAFE_TAKES_SLOT
  471. STSAFE_A_SLOT_0,
  472. #endif
  473. curve_id,
  474. otherKeyX, otherKeyY,
  475. info->pk.ecdh.out, (int32_t*)info->pk.ecdh.outlen);
  476. if (rc != STSAFE_A_OK) {
  477. #ifdef USE_STSAFE_VERBOSE
  478. STSAFE_INTERFACE_PRINTF("stsafe_interface_shared_secret error: %d\n", rc);
  479. #endif
  480. rc = WC_HW_E;
  481. }
  482. }
  483. }
  484. }
  485. #endif /* HAVE_ECC */
  486. /* need to return negative here for error */
  487. if (rc != 0 && rc != CRYPTOCB_UNAVAILABLE) {
  488. WOLFSSL_MSG("STSAFE: CryptoCb failed");
  489. #ifdef USE_STSAFE_VERBOSE
  490. STSAFE_INTERFACE_PRINTF("STSAFE: CryptoCb failed %d\n", rc);
  491. #endif
  492. rc = WC_HW_E;
  493. }
  494. return rc;
  495. }
  496. #endif /* WOLF_CRYPTO_CB */
  497. #endif /* WOLFSSL_STSAFEA100 */