kcapi_ecc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /* kcapi_ecc.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_KCAPI_ECC) && defined(HAVE_ECC)
  26. #include <wolfssl/wolfcrypt/error-crypt.h>
  27. #include <wolfssl/wolfcrypt/logging.h>
  28. #include <wolfssl/wolfcrypt/port/kcapi/wc_kcapi.h>
  29. #include <wolfssl/wolfcrypt/port/kcapi/kcapi_ecc.h>
  30. #include <wolfssl/wolfcrypt/ecc.h>
  31. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  32. /* FIPS build has replaced ecc.h. */
  33. #define wc_ecc_key_get_priv(key) (&((key)->k))
  34. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  35. #endif
  36. #ifndef ECC_CURVE_NIST_P256
  37. #define ECC_CURVE_NIST_P256 2
  38. #endif
  39. #ifndef ECC_CURVE_NIST_P384
  40. #define ECC_CURVE_NIST_P384 3
  41. #endif
  42. #ifndef ECC_CURVE_NIST_P521
  43. #define ECC_CURVE_NIST_P521 4
  44. #endif
  45. #define ECDSA_KEY_VERSION 1
  46. #define ECDH_KEY_VERSION 1
  47. static const char WC_NAME_ECDH[] = "ecdh";
  48. #if defined(HAVE_ECC_SIGN) || defined(HAVE_ECC_VERIFY)
  49. static const char WC_NAME_ECDSA[] = "ecdsa";
  50. #endif
  51. void KcapiEcc_Free(ecc_key* key)
  52. {
  53. if (key->handle != NULL) {
  54. kcapi_kpp_destroy(key->handle);
  55. key->handle = NULL;
  56. }
  57. }
  58. static int KcapiEcc_CurveId(int curve_id, word32* kcapiCurveId)
  59. {
  60. int ret = 0;
  61. switch (curve_id) {
  62. case ECC_SECP256R1:
  63. *kcapiCurveId = ECC_CURVE_NIST_P256;
  64. break;
  65. case ECC_SECP384R1:
  66. *kcapiCurveId = ECC_CURVE_NIST_P384;
  67. break;
  68. case ECC_SECP521R1:
  69. *kcapiCurveId = ECC_CURVE_NIST_P521;
  70. break;
  71. default:
  72. ret = BAD_FUNC_ARG;
  73. break;
  74. }
  75. return ret;
  76. }
  77. int KcapiEcc_LoadKey(ecc_key* key, byte* pubkey_raw, word32* pubkey_sz,
  78. int release_handle)
  79. {
  80. int ret = 0;
  81. word32 kcapiCurveId = 0;
  82. word32 keySz;
  83. int handleInit = 0;
  84. if (key == NULL || key->dp == NULL) {
  85. ret = BAD_FUNC_ARG;
  86. }
  87. if (ret == 0) {
  88. keySz = key->dp->size;
  89. ret = KcapiEcc_CurveId(key->dp->id, &kcapiCurveId);
  90. }
  91. /* if handle doesn't exist create one */
  92. if (ret == 0 && key->handle == NULL) {
  93. ret = kcapi_kpp_init(&key->handle, WC_NAME_ECDH, 0);
  94. if (ret == 0) {
  95. handleInit = 1;
  96. ret = kcapi_kpp_ecdh_setcurve(key->handle, kcapiCurveId);
  97. if (ret >= 0) {
  98. ret = 0;
  99. }
  100. }
  101. }
  102. /* set the key */
  103. if (ret == 0) {
  104. if (mp_iszero(wc_ecc_key_get_priv(key)) != MP_YES) {
  105. /* if a private key value is set, load and use it */
  106. byte priv[MAX_ECC_BYTES];
  107. ret = wc_export_int(wc_ecc_key_get_priv(key), priv, &keySz, keySz,
  108. WC_TYPE_UNSIGNED_BIN);
  109. if (ret == 0) {
  110. ret = kcapi_kpp_setkey(key->handle, priv, keySz);
  111. }
  112. }
  113. else {
  114. /* generate new ephemeral key */
  115. ret = kcapi_kpp_setkey(key->handle, NULL, 0);
  116. }
  117. if (ret >= 0) {
  118. ret = 0;
  119. }
  120. }
  121. /* optionally export public key */
  122. if (ret == 0 && pubkey_raw != NULL && pubkey_sz != NULL) {
  123. if (*pubkey_sz < keySz*2) {
  124. ret = BUFFER_E;
  125. }
  126. if (ret == 0) {
  127. ret = (int)kcapi_kpp_keygen(key->handle, pubkey_raw, keySz*2,
  128. KCAPI_ACCESS_HEURISTIC);
  129. if (ret >= 0) {
  130. *pubkey_sz = ret;
  131. ret = 0;
  132. }
  133. }
  134. }
  135. if (handleInit && release_handle && key != NULL && key->handle != NULL) {
  136. kcapi_kpp_destroy(key->handle);
  137. key->handle = NULL;
  138. }
  139. return ret;
  140. }
  141. int KcapiEcc_MakeKey(ecc_key* key, int keysize, int curve_id)
  142. {
  143. int ret = 0;
  144. word32 pubkey_sz = (word32)sizeof(key->pubkey_raw);
  145. /* free existing handle */
  146. if (key != NULL && key->handle != NULL) {
  147. kcapi_kpp_destroy(key->handle);
  148. key->handle = NULL;
  149. }
  150. /* check arguments */
  151. if (key == NULL || key->dp == NULL) {
  152. ret = BAD_FUNC_ARG;
  153. }
  154. ret = KcapiEcc_LoadKey(key, key->pubkey_raw, &pubkey_sz, 0);
  155. if (ret == 0) {
  156. ret = mp_read_unsigned_bin(key->pubkey.x,
  157. key->pubkey_raw, pubkey_sz / 2);
  158. }
  159. if (ret == 0) {
  160. ret = mp_read_unsigned_bin(key->pubkey.y,
  161. key->pubkey_raw + pubkey_sz / 2, pubkey_sz / 2);
  162. }
  163. if (ret == 0) {
  164. ret = mp_set(key->pubkey.z, 1);
  165. }
  166. if (ret == 0) {
  167. key->type = ECC_PRIVATEKEY;
  168. }
  169. /* if error release handle now */
  170. if (ret != 0 && key->handle != NULL) {
  171. kcapi_kpp_destroy(key->handle);
  172. key->handle = NULL;
  173. }
  174. /* These are not used. The key->dp is set */
  175. (void)keysize;
  176. (void)curve_id;
  177. return ret;
  178. }
  179. #ifdef HAVE_ECC_DHE
  180. int KcapiEcc_SharedSecret(ecc_key* private_key, ecc_key* public_key, byte* out,
  181. word32* outlen)
  182. {
  183. int ret = 0;
  184. word32 kcapiCurveId = 0;
  185. byte* buf_aligned = NULL;
  186. byte* pub = NULL;
  187. word32 keySz;
  188. #ifndef KCAPI_USE_XMALLOC
  189. size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
  190. #endif
  191. if (private_key == NULL || private_key->dp == NULL || public_key == NULL) {
  192. ret = BAD_FUNC_ARG;
  193. }
  194. if (ret == 0) {
  195. pub = public_key->pubkey_raw;
  196. keySz = private_key->dp->size;
  197. ret = KcapiEcc_CurveId(private_key->dp->id, &kcapiCurveId);
  198. }
  199. if (ret == 0 && private_key->handle == NULL) {
  200. ret = kcapi_kpp_init(&private_key->handle, WC_NAME_ECDH, 0);
  201. if (ret == 0) {
  202. ret = kcapi_kpp_ecdh_setcurve(private_key->handle, kcapiCurveId);
  203. if (ret >= 0) {
  204. ret = 0;
  205. }
  206. }
  207. }
  208. /* if a private key value is set, load and use it */
  209. if (ret == 0 && mp_iszero(wc_ecc_key_get_priv(private_key)) != MP_YES) {
  210. byte priv[MAX_ECC_BYTES];
  211. ret = wc_export_int(wc_ecc_key_get_priv(private_key), priv, &keySz,
  212. keySz, WC_TYPE_UNSIGNED_BIN);
  213. if (ret == 0) {
  214. ret = kcapi_kpp_setkey(private_key->handle, priv, keySz);
  215. if (ret >= 0) {
  216. ret = 0;
  217. }
  218. }
  219. }
  220. if (ret == 0) {
  221. #ifdef KCAPI_USE_XMALLOC
  222. buf_aligned = (byte*)XMALLOC(keySz * 2, private_key->heap,
  223. DYNAMIC_TYPE_TMP_BUFFER);
  224. if (buf_aligned == NULL) {
  225. ret = MEMORY_E;
  226. }
  227. #else
  228. ret = posix_memalign((void*)&buf_aligned, pageSz, keySz * 2);
  229. if (ret != 0) {
  230. ret = MEMORY_E;
  231. }
  232. #endif
  233. }
  234. if (ret == 0) {
  235. XMEMCPY(buf_aligned, pub, keySz * 2);
  236. ret = (int)kcapi_kpp_ssgen(private_key->handle, buf_aligned,
  237. keySz * 2, buf_aligned, keySz * 2, KCAPI_ACCESS_HEURISTIC);
  238. if (ret >= 0) {
  239. *outlen = ret / 2;
  240. XMEMCPY(out, buf_aligned, *outlen);
  241. ret = 0; /* success */
  242. }
  243. }
  244. if (buf_aligned != NULL) {
  245. #ifdef KCAPI_USE_XMALLOC
  246. XFREE(buf_aligned, private_key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  247. #else
  248. free(buf_aligned);
  249. #endif
  250. }
  251. return ret;
  252. }
  253. #endif
  254. #ifdef HAVE_ECC_SIGN
  255. static int KcapiEcc_SetPrivKey(ecc_key* key)
  256. {
  257. int ret;
  258. byte priv[KCAPI_PARAM_SZ + MAX_ECC_BYTES];
  259. word32 keySz = key->dp->size;
  260. word32 kcapiCurveId;
  261. ret = KcapiEcc_CurveId(key->dp->id, &kcapiCurveId);
  262. if (ret == 0) {
  263. priv[0] = ECDSA_KEY_VERSION;
  264. priv[1] = kcapiCurveId;
  265. #ifdef WOLF_PRIVATE_KEY_ID
  266. if (key->idLen > 0) {
  267. WOLFSSL_MSG("Using ID based private key");
  268. keySz = key->idLen;
  269. XMEMCPY(priv + KCAPI_PARAM_SZ, key->id, keySz);
  270. }
  271. else
  272. #endif
  273. {
  274. ret = wc_export_int(wc_ecc_key_get_priv(key), priv + KCAPI_PARAM_SZ,
  275. &keySz, keySz, WC_TYPE_UNSIGNED_BIN);
  276. }
  277. }
  278. if (ret == 0) {
  279. /* call with NULL to so KCAPI treats incoming data as hash */
  280. ret = kcapi_akcipher_setkey(key->handle, NULL, 0);
  281. if (ret >= 0) {
  282. ret = kcapi_akcipher_setkey(key->handle, priv, KCAPI_PARAM_SZ + keySz);
  283. if (ret >= 0) {
  284. ret = 0;
  285. }
  286. }
  287. }
  288. return ret;
  289. }
  290. int KcapiEcc_Sign(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
  291. word32 sigLen)
  292. {
  293. int ret = 0;
  294. byte* buf_aligned = NULL;
  295. int handleInit = 0;
  296. word32 keySz;
  297. word32 maxBufSz;
  298. #ifndef KCAPI_USE_XMALLOC
  299. size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
  300. #endif
  301. if (key == NULL || key->dp == NULL) {
  302. ret = BAD_FUNC_ARG;
  303. }
  304. if (ret == 0 && key->handle == NULL) {
  305. ret = kcapi_akcipher_init(&key->handle, WC_NAME_ECDSA, 0);
  306. if (ret != 0) {
  307. WOLFSSL_MSG("KcapiEcc_Sign: Failed to initialize");
  308. }
  309. if (ret == 0) {
  310. handleInit = 1;
  311. ret = KcapiEcc_SetPrivKey(key);
  312. }
  313. }
  314. if (ret == 0) {
  315. /* make sure signature output is large enough */
  316. keySz = key->dp->size;
  317. if (sigLen < keySz*2) {
  318. ret = BUFFER_E;
  319. }
  320. }
  321. if (ret == 0) {
  322. maxBufSz = (hashLen > keySz * 2) ? hashLen : (keySz * 2);
  323. #ifdef KCAPI_USE_XMALLOC
  324. buf_aligned = (unsigned char*)XMALLOC(maxBufSz, key->heap,
  325. DYNAMIC_TYPE_TMP_BUFFER);
  326. if (buf_aligned == NULL) {
  327. ret = MEMORY_E;
  328. }
  329. #else
  330. ret = posix_memalign((void*)&buf_aligned, pageSz, maxBufSz);
  331. if (ret != 0) {
  332. ret = MEMORY_E;
  333. }
  334. #endif
  335. }
  336. if (ret == 0) {
  337. XMEMCPY(buf_aligned, hash, hashLen);
  338. ret = (int)kcapi_akcipher_sign(key->handle, buf_aligned, hashLen,
  339. buf_aligned, keySz * 2, KCAPI_ACCESS_HEURISTIC);
  340. if (ret >= 0) {
  341. XMEMCPY(sig, buf_aligned, ret);
  342. ret = 0; /* mark success */
  343. }
  344. }
  345. if (buf_aligned != NULL) {
  346. #ifdef KCAPI_USE_XMALLOC
  347. XFREE(buf_aligned, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  348. #else
  349. free(buf_aligned);
  350. #endif
  351. }
  352. if (handleInit) {
  353. kcapi_kpp_destroy(key->handle);
  354. key->handle = NULL;
  355. }
  356. return ret;
  357. }
  358. #endif
  359. #ifdef HAVE_ECC_VERIFY
  360. static int KcapiEcc_SetPubKey(ecc_key* key)
  361. {
  362. int ret;
  363. word32 kcapiCurveId;
  364. word32 keySz = key->dp->size;
  365. byte pub[KCAPI_PARAM_SZ + (MAX_ECC_BYTES * 2)];
  366. int pubLen;
  367. ret = KcapiEcc_CurveId(key->dp->id, &kcapiCurveId);
  368. if (ret == 0) {
  369. pub[0] = ECDSA_KEY_VERSION;
  370. pub[1] = kcapiCurveId;
  371. XMEMCPY(&pub[KCAPI_PARAM_SZ], key->pubkey_raw, keySz * 2);
  372. pubLen = KCAPI_PARAM_SZ + (keySz * 2);
  373. /* call with NULL to so KCAPI treats incoming data as hash */
  374. ret = kcapi_akcipher_setpubkey(key->handle, NULL, 0);
  375. if (ret >= 0) {
  376. ret = kcapi_akcipher_setpubkey(key->handle, pub, pubLen);
  377. if (ret >= 0) {
  378. ret = 0;
  379. }
  380. }
  381. }
  382. return ret;
  383. }
  384. int KcapiEcc_Verify(ecc_key* key, const byte* hash, word32 hashLen, byte* sig,
  385. word32 sigLen)
  386. {
  387. int ret = 0;
  388. byte* buf_aligned = NULL;
  389. int handleInit = 0;
  390. word32 keySz = 0;
  391. #ifndef KCAPI_USE_XMALLOC
  392. size_t pageSz = (size_t)sysconf(_SC_PAGESIZE);
  393. #endif
  394. if (key == NULL || key->dp == NULL) {
  395. ret = BAD_FUNC_ARG;
  396. }
  397. if (ret == 0 && key->handle == NULL) {
  398. ret = kcapi_akcipher_init(&key->handle, WC_NAME_ECDSA, 0);
  399. if (ret != 0) {
  400. WOLFSSL_MSG("KcapiEcc_Verify: Failed to initialize");
  401. }
  402. if (ret == 0) {
  403. handleInit = 1;
  404. ret = KcapiEcc_SetPubKey(key);
  405. }
  406. }
  407. if (ret == 0) {
  408. keySz = key->dp->size;
  409. #ifdef KCAPI_USE_XMALLOC
  410. buf_aligned = (byte*)XMALLOC(sigLen + hashLen, key->heap,
  411. DYNAMIC_TYPE_TMP_BUFFER);
  412. if (buf_aligned == NULL) {
  413. ret = MEMORY_E;
  414. }
  415. #else
  416. ret = posix_memalign((void*)&buf_aligned, pageSz, sigLen + hashLen);
  417. if (ret != 0) {
  418. ret = MEMORY_E;
  419. }
  420. #endif
  421. }
  422. if (ret == 0) {
  423. XMEMCPY(buf_aligned, sig, sigLen);
  424. XMEMCPY(buf_aligned + sigLen, hash, hashLen);
  425. ret = (int)kcapi_akcipher_verify(key->handle, buf_aligned,
  426. sigLen + hashLen, buf_aligned, keySz * 2,
  427. KCAPI_ACCESS_HEURISTIC);
  428. if (ret >= 0) {
  429. /* verify output in buf_aligned is not used */
  430. ret = 0;
  431. }
  432. }
  433. if (buf_aligned != NULL) {
  434. #ifdef KCAPI_USE_XMALLOC
  435. XFREE(buf_aligned, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  436. #else
  437. free(buf_aligned);
  438. #endif
  439. }
  440. if (handleInit) {
  441. kcapi_kpp_destroy(key->handle);
  442. key->handle = NULL;
  443. }
  444. return ret;
  445. }
  446. #endif
  447. #endif /* WOLFSSL_KCAPI_ECC && HAVE_ECC */