ecc.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /* ecc.h
  2. *
  3. * Copyright (C) 2006-2022 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. /*!
  22. \file wolfssl/wolfcrypt/ecc.h
  23. */
  24. #ifndef WOLF_CRYPT_ECC_H
  25. #define WOLF_CRYPT_ECC_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #ifdef HAVE_ECC
  28. #if defined(HAVE_FIPS) && \
  29. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  30. #include <wolfssl/wolfcrypt/fips.h>
  31. #endif /* HAVE_FIPS_VERSION >= 2 */
  32. #include <wolfssl/wolfcrypt/integer.h>
  33. #include <wolfssl/wolfcrypt/random.h>
  34. #ifdef HAVE_X963_KDF
  35. #include <wolfssl/wolfcrypt/hash.h>
  36. #endif
  37. #ifdef WOLFSSL_ASYNC_CRYPT
  38. #include <wolfssl/wolfcrypt/async.h>
  39. #ifdef WOLFSSL_CERT_GEN
  40. #include <wolfssl/wolfcrypt/asn.h>
  41. #endif
  42. #endif
  43. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  44. #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
  45. #endif /* WOLFSSL_ATECC508A */
  46. #if defined(WOLFSSL_CRYPTOCELL)
  47. #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h>
  48. #endif
  49. #ifdef WOLFSSL_SILABS_SE_ACCEL
  50. #include <wolfssl/wolfcrypt/port/silabs/silabs_ecc.h>
  51. #endif
  52. #if defined(WOLFSSL_KCAPI_ECC)
  53. #include <wolfssl/wolfcrypt/port/kcapi/kcapi_ecc.h>
  54. #endif
  55. #ifdef WOLFSSL_SE050
  56. #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
  57. #endif
  58. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  59. #include <wolfssl/wolfcrypt/port/xilinx/xil-versal-glue.h>
  60. #endif
  61. #ifdef WOLFSSL_HAVE_SP_ECC
  62. #include <wolfssl/wolfcrypt/sp_int.h>
  63. #endif
  64. #ifdef __cplusplus
  65. extern "C" {
  66. #endif
  67. /* Enable curve B parameter if needed */
  68. #if defined(HAVE_COMP_KEY) || defined(ECC_CACHE_CURVE)
  69. #ifndef USE_ECC_B_PARAM /* Allow someone to force enable */
  70. #define USE_ECC_B_PARAM
  71. #endif
  72. #endif
  73. /* Use this as the key->idx if a custom ecc_set is used for key->dp */
  74. #define ECC_CUSTOM_IDX (-1)
  75. /* Determine max ECC bits based on enabled curves */
  76. #if defined(WOLFCRYPT_HAVE_SAKKE)
  77. #define MAX_ECC_BITS_NEEDED 1024
  78. #elif defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  79. #define MAX_ECC_BITS_NEEDED 521
  80. #elif defined(HAVE_ECC512)
  81. #define MAX_ECC_BITS_NEEDED 512
  82. #elif defined(HAVE_ECC384)
  83. #define MAX_ECC_BITS_NEEDED 384
  84. #elif defined(HAVE_ECC320)
  85. #define MAX_ECC_BITS_NEEDED 320
  86. #elif !defined(NO_ECC256)
  87. #define MAX_ECC_BITS_NEEDED 256
  88. #elif defined(HAVE_ECC239)
  89. #define MAX_ECC_BITS_NEEDED 239
  90. #elif defined(HAVE_ECC224)
  91. #define MAX_ECC_BITS_NEEDED 224
  92. #elif defined(HAVE_ECC192)
  93. #define MAX_ECC_BITS_NEEDED 192
  94. #elif defined(HAVE_ECC160)
  95. #define MAX_ECC_BITS_NEEDED 160
  96. #elif defined(HAVE_ECC128)
  97. #define MAX_ECC_BITS_NEEDED 128
  98. #elif defined(HAVE_ECC112)
  99. #define MAX_ECC_BITS_NEEDED 112
  100. #endif
  101. #ifndef MAX_ECC_BITS
  102. #define MAX_ECC_BITS MAX_ECC_BITS_NEEDED
  103. #else
  104. #if MAX_ECC_BITS_NEEDED > MAX_ECC_BITS
  105. #error configured MAX_ECC_BITS is less than required by enabled curves.
  106. #endif
  107. #endif
  108. /* calculate max ECC bytes */
  109. #if ((MAX_ECC_BITS * 2) % 8) == 0
  110. #define MAX_ECC_BYTES (MAX_ECC_BITS / 8)
  111. #else
  112. /* add byte if not aligned */
  113. #define MAX_ECC_BYTES ((MAX_ECC_BITS / 8) + 1)
  114. #endif
  115. #ifndef ECC_MAX_PAD_SZ
  116. /* ECC maximum padding size (when MSB is set extra byte required for R and S) */
  117. #define ECC_MAX_PAD_SZ 2
  118. #endif
  119. enum {
  120. ECC_PUBLICKEY = 1,
  121. ECC_PRIVATEKEY = 2,
  122. ECC_PRIVATEKEY_ONLY = 3,
  123. ECC_MAXNAME = 16, /* MAX CURVE NAME LENGTH */
  124. SIG_HEADER_SZ = 7, /* ECC signature header size (30 81 87 02 42 [R] 02 42 [S]) */
  125. ECC_BUFSIZE = 257, /* for exported keys temp buffer */
  126. ECC_MINSIZE = ECC_MIN_KEY_SZ/8, /* MIN Private Key size */
  127. #ifdef WOLFCRYPT_HAVE_SAKKE
  128. ECC_MAXSIZE = 128, /* MAX Private Key size */
  129. ECC_MAXSIZE_GEN = 128, /* MAX Buffer size required when generating ECC keys*/
  130. #else
  131. ECC_MAXSIZE = 66, /* MAX Private Key size */
  132. ECC_MAXSIZE_GEN = 74, /* MAX Buffer size required when generating ECC keys*/
  133. #endif
  134. ECC_MAX_OID_LEN = 16,
  135. ECC_MAX_SIG_SIZE= ((MAX_ECC_BYTES * 2) + ECC_MAX_PAD_SZ + SIG_HEADER_SZ),
  136. /* max crypto hardware size */
  137. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  138. ECC_MAX_CRYPTO_HW_SIZE = ATECC_KEY_SIZE, /* from port/atmel/atmel.h */
  139. ECC_MAX_CRYPTO_HW_PUBKEY_SIZE = (ATECC_KEY_SIZE*2),
  140. #elif defined(PLUTON_CRYPTO_ECC)
  141. ECC_MAX_CRYPTO_HW_SIZE = 32,
  142. #elif defined(WOLFSSL_SILABS_SE_ACCEL)
  143. ECC_MAX_CRYPTO_HW_SIZE = 32,
  144. #elif defined(WOLFSSL_CRYPTOCELL)
  145. #ifndef CRYPTOCELL_KEY_SIZE
  146. CRYPTOCELL_KEY_SIZE = ECC_MAXSIZE,
  147. #endif
  148. ECC_MAX_CRYPTO_HW_SIZE = CRYPTOCELL_KEY_SIZE,
  149. #elif defined(WOLFSSL_SE050)
  150. ECC_MAX_CRYPTO_HW_SIZE = 32,
  151. ECC_MAX_CRYPTO_HW_PUBKEY_SIZE = 64,
  152. #elif defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  153. ECC_MAX_CRYPTO_HW_SIZE = MAX_ECC_BYTES,
  154. #endif
  155. /* point compression type */
  156. ECC_POINT_COMP_EVEN = 0x02,
  157. ECC_POINT_COMP_ODD = 0x03,
  158. ECC_POINT_UNCOMP = 0x04,
  159. /* Shamir's dual add constants */
  160. SHAMIR_PRECOMP_SZ = 16,
  161. #ifdef WOLF_PRIVATE_KEY_ID
  162. ECC_MAX_ID_LEN = 32,
  163. ECC_MAX_LABEL_LEN = 32,
  164. #endif
  165. };
  166. #endif /* HAVE_ECC */
  167. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  168. defined(HAVE_CURVE448) || defined(WOLFCRYPT_HAVE_SAKKE)
  169. /* Curve Types */
  170. typedef enum ecc_curve_id {
  171. ECC_CURVE_INVALID = -1,
  172. ECC_CURVE_DEF = 0, /* NIST or SECP */
  173. /* NIST Prime Curves */
  174. ECC_SECP192R1,
  175. ECC_PRIME192V2,
  176. ECC_PRIME192V3,
  177. ECC_PRIME239V1,
  178. ECC_PRIME239V2,
  179. ECC_PRIME239V3,
  180. ECC_SECP256R1,
  181. /* SECP Curves */
  182. ECC_SECP112R1,
  183. ECC_SECP112R2,
  184. ECC_SECP128R1,
  185. ECC_SECP128R2,
  186. ECC_SECP160R1,
  187. ECC_SECP160R2,
  188. ECC_SECP224R1,
  189. ECC_SECP384R1,
  190. ECC_SECP521R1,
  191. /* Koblitz */
  192. ECC_SECP160K1,
  193. ECC_SECP192K1,
  194. ECC_SECP224K1,
  195. ECC_SECP256K1,
  196. /* Brainpool Curves */
  197. ECC_BRAINPOOLP160R1,
  198. ECC_BRAINPOOLP192R1,
  199. ECC_BRAINPOOLP224R1,
  200. ECC_BRAINPOOLP256R1,
  201. ECC_BRAINPOOLP320R1,
  202. ECC_BRAINPOOLP384R1,
  203. ECC_BRAINPOOLP512R1,
  204. /* Twisted Edwards Curves */
  205. #ifdef HAVE_CURVE25519
  206. ECC_X25519,
  207. #endif
  208. #ifdef HAVE_CURVE448
  209. ECC_X448,
  210. #endif
  211. #ifdef WOLFCRYPT_HAVE_SAKKE
  212. ECC_SAKKE_1,
  213. #endif
  214. #ifdef WOLFSSL_CUSTOM_CURVES
  215. ECC_CURVE_CUSTOM,
  216. #endif
  217. ECC_CURVE_MAX
  218. } ecc_curve_id;
  219. #endif
  220. #ifdef HAVE_ECC
  221. #ifdef HAVE_OID_ENCODING
  222. typedef word16 ecc_oid_t;
  223. #else
  224. typedef byte ecc_oid_t;
  225. /* OID encoded with ASN scheme:
  226. first element = (oid[0] * 40) + oid[1]
  227. if any element > 127 then MSB 0x80 indicates additional byte */
  228. #endif
  229. #if !defined(WOLFSSL_ECC_CURVE_STATIC) && defined(USE_WINDOWS_API)
  230. /* MSC does something different with the pointers to the arrays than GCC,
  231. * and it causes the FIPS checksum to fail. In the case of windows builds,
  232. * store everything as arrays instead of pointers to strings. */
  233. #define WOLFSSL_ECC_CURVE_STATIC
  234. #endif
  235. /* ECC set type defined a GF(p) curve */
  236. #ifndef WOLFSSL_ECC_CURVE_STATIC
  237. typedef struct ecc_set_type {
  238. int size; /* The size of the curve in octets */
  239. int id; /* id of this curve */
  240. const char* name; /* name of this curve */
  241. const char* prime; /* prime that defines the field, curve is in (hex) */
  242. const char* Af; /* fields A param (hex) */
  243. const char* Bf; /* fields B param (hex) */
  244. const char* order; /* order of the curve (hex) */
  245. const char* Gx; /* x coordinate of the base point on curve (hex) */
  246. const char* Gy; /* y coordinate of the base point on curve (hex) */
  247. const ecc_oid_t* oid;
  248. word32 oidSz;
  249. word32 oidSum; /* sum of encoded OID bytes */
  250. int cofactor;
  251. } ecc_set_type;
  252. #else
  253. #define MAX_ECC_NAME 16
  254. #define MAX_ECC_STRING ((MAX_ECC_BYTES * 2) + 2)
  255. /* The values are stored as text strings. */
  256. typedef struct ecc_set_type {
  257. int size; /* The size of the curve in octets */
  258. int id; /* id of this curve */
  259. const char name[MAX_ECC_NAME]; /* name of this curve */
  260. const char prime[MAX_ECC_STRING]; /* prime that defines the field, curve is in (hex) */
  261. const char Af[MAX_ECC_STRING]; /* fields A param (hex) */
  262. const char Bf[MAX_ECC_STRING]; /* fields B param (hex) */
  263. const char order[MAX_ECC_STRING]; /* order of the curve (hex) */
  264. const char Gx[MAX_ECC_STRING]; /* x coordinate of the base point on curve (hex) */
  265. const char Gy[MAX_ECC_STRING]; /* y coordinate of the base point on curve (hex) */
  266. const ecc_oid_t oid[10];
  267. word32 oidSz;
  268. word32 oidSum; /* sum of encoded OID bytes */
  269. int cofactor;
  270. } ecc_set_type;
  271. #endif
  272. #ifdef ALT_ECC_SIZE
  273. /* Note on ALT_ECC_SIZE:
  274. * The fast math code uses an array of a fixed size to store the big integers.
  275. * By default, the array is big enough for RSA keys. There is a size,
  276. * FP_MAX_BITS which can be used to make the array smaller when one wants ECC
  277. * but not RSA. Some people want fast math sized for both RSA and ECC, where
  278. * ECC won't use as much as RSA. The flag ALT_ECC_SIZE switches in an alternate
  279. * ecc_point structure that uses an alternate fp_int that has a shorter array
  280. * of fp_digits.
  281. *
  282. * Now, without ALT_ECC_SIZE, the ecc_point has three single item arrays of
  283. * mp_ints for the components of the point. With ALT_ECC_SIZE, the components
  284. * of the point are pointers that are set to each of a three item array of
  285. * alt_fp_ints. While an mp_int will have 4096 bits of digit inside the
  286. * structure, the alt_fp_int will only have 512 bits for ECC 256-bit and
  287. * 1056-bits for ECC 521-bit. A size value was added in the ALT case, as well,
  288. * and is set by mp_init() and alt_fp_init(). The functions fp_zero() and
  289. * fp_copy() use the size parameter. An int needs to be initialized before
  290. * using it instead of just fp_zeroing it, the init will call zero. The
  291. * FP_MAX_BITS_ECC defaults to calculating based on MAX_ECC_BITS, but
  292. * can be set to change the number of bits used in the alternate FP_INT.
  293. *
  294. * The ALT_ECC_SIZE option only applies to stack based fast math USE_FAST_MATH.
  295. */
  296. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH_ALL) && \
  297. !defined(WOLFSSL_SP_MATH)
  298. #error USE_FAST_MATH must be defined to use ALT_ECC_SIZE
  299. #endif
  300. #ifdef WOLFSSL_NO_MALLOC
  301. #error ALT_ECC_SIZE cannot be used with no malloc (WOLFSSL_NO_MALLOC)
  302. #endif
  303. /* determine max bits required for ECC math */
  304. #ifndef FP_MAX_BITS_ECC
  305. /* max bits rounded up by 8 then doubled */
  306. /* (ROUND8(MAX_ECC_BITS) * 2) */
  307. #define FP_MAX_BITS_ECC (2 * \
  308. ((MAX_ECC_BITS + DIGIT_BIT - 1) / DIGIT_BIT) * DIGIT_BIT)
  309. /* Note: For ECC verify only FP_MAX_BITS_ECC can be reduced to:
  310. ROUND8(MAX_ECC_BITS) + ROUND8(DIGIT_BIT) */
  311. #endif
  312. /* verify alignment */
  313. #if CHAR_BIT == 0
  314. #error CHAR_BIT must be nonzero
  315. #endif
  316. #if FP_MAX_BITS_ECC % CHAR_BIT
  317. #error FP_MAX_BITS_ECC must be a multiple of CHAR_BIT
  318. #endif
  319. /* determine buffer size */
  320. /* Add one to accommodate extra digit used by sp_mul(), sp_mulmod(), sp_sqr(), and sp_sqrmod(). */
  321. #define FP_SIZE_ECC ((FP_MAX_BITS_ECC/DIGIT_BIT) + 1)
  322. /* This needs to match the size of the fp_int struct, except the
  323. * fp_digit array will be shorter. */
  324. typedef struct alt_fp_int {
  325. int used, sign, size;
  326. mp_digit dp[FP_SIZE_ECC];
  327. } alt_fp_int;
  328. #endif /* ALT_ECC_SIZE */
  329. #ifndef WC_ECCKEY_TYPE_DEFINED
  330. typedef struct ecc_key ecc_key;
  331. #define WC_ECCKEY_TYPE_DEFINED
  332. #endif
  333. /* A point on an ECC curve, stored in Jacobian format such that (x,y,z) =>
  334. (x/z^2, y/z^3, 1) when interpreted as affine */
  335. typedef struct {
  336. #ifndef ALT_ECC_SIZE
  337. mp_int x[1]; /* The x coordinate */
  338. mp_int y[1]; /* The y coordinate */
  339. mp_int z[1]; /* The z coordinate */
  340. #else
  341. mp_int* x; /* The x coordinate */
  342. mp_int* y; /* The y coordinate */
  343. mp_int* z; /* The z coordinate */
  344. alt_fp_int xyz[3];
  345. #endif
  346. #if defined(WOLFSSL_SMALL_STACK_CACHE) && !defined(WOLFSSL_ECC_NO_SMALL_STACK)
  347. ecc_key* key;
  348. #endif
  349. } ecc_point;
  350. /* ECC Flags */
  351. enum {
  352. WC_ECC_FLAG_NONE = 0x00,
  353. WC_ECC_FLAG_COFACTOR = 0x01,
  354. WC_ECC_FLAG_DEC_SIGN = 0x02,
  355. };
  356. /* ECC non-blocking */
  357. #ifdef WC_ECC_NONBLOCK
  358. typedef struct ecc_nb_ctx {
  359. #if defined(WOLFSSL_HAVE_SP_ECC) && defined(WOLFSSL_SP_NONBLOCK)
  360. sp_ecc_ctx_t sp_ctx;
  361. #else
  362. /* build configuration not supported */
  363. #error ECC non-blocking only supports SP (--enable-sp=nonblock)
  364. #endif
  365. } ecc_nb_ctx_t;
  366. #endif /* WC_ECC_NONBLOCK */
  367. /* An ECC Key */
  368. struct ecc_key {
  369. int type; /* Public or Private */
  370. int idx; /* Index into the ecc_sets[] for the parameters of
  371. this curve if -1, this key is using user supplied
  372. curve in dp */
  373. int state;
  374. word32 flags;
  375. const ecc_set_type* dp; /* domain parameters, either points to NIST
  376. curves (idx >= 0) or user supplied */
  377. #ifdef WOLFSSL_CUSTOM_CURVES
  378. int deallocSet;
  379. #endif
  380. void* heap; /* heap hint */
  381. ecc_point pubkey; /* public key */
  382. mp_int k; /* private key */
  383. #ifdef WOLFSSL_CAAM
  384. word32 blackKey; /* address of key encrypted and in secure memory */
  385. word32 securePubKey; /* address of public key in secure memory */
  386. int partNum; /* partition number*/
  387. #endif
  388. #ifdef WOLFSSL_SE050
  389. int keyId;
  390. #endif
  391. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
  392. int slot; /* Key Slot Number (-1 unknown) */
  393. byte pubkey_raw[ECC_MAX_CRYPTO_HW_PUBKEY_SIZE];
  394. #endif
  395. #if defined(PLUTON_CRYPTO_ECC) || defined(WOLF_CRYPTO_CB)
  396. int devId;
  397. #endif
  398. #ifdef WOLFSSL_SILABS_SE_ACCEL
  399. sl_se_command_context_t cmd_ctx;
  400. sl_se_key_descriptor_t key;
  401. /* Used for SiLabs "plaintext" with public X, public Y, and
  402. * private D concatenated. These are respectively at offset `0`,
  403. * offset `keysize`, and offset `2 * keysize`.
  404. */
  405. byte key_raw[3 * ECC_MAX_CRYPTO_HW_SIZE];
  406. #endif
  407. #ifdef WOLFSSL_KCAPI_ECC
  408. struct kcapi_handle* handle;
  409. byte pubkey_raw[MAX_ECC_BYTES * 2];
  410. #endif
  411. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  412. wc_Xsecure xSec;
  413. byte keyRaw[3 * ECC_MAX_CRYPTO_HW_SIZE] ALIGN32;
  414. byte* privKey;
  415. #endif
  416. #ifdef WOLFSSL_ASYNC_CRYPT
  417. mp_int* r; /* sign/verify temps */
  418. mp_int* s;
  419. WC_ASYNC_DEV asyncDev;
  420. #ifdef HAVE_CAVIUM_V
  421. mp_int* e; /* Sign, Verify and Shared Secret */
  422. mp_int* signK;
  423. #endif
  424. #ifdef WOLFSSL_CERT_GEN
  425. CertSignCtx certSignCtx; /* context info for cert sign (MakeSignature) */
  426. #endif
  427. #endif /* WOLFSSL_ASYNC_CRYPT */
  428. #ifdef WOLF_PRIVATE_KEY_ID
  429. byte id[ECC_MAX_ID_LEN];
  430. int idLen;
  431. char label[ECC_MAX_LABEL_LEN];
  432. int labelLen;
  433. #endif
  434. #if defined(WOLFSSL_CRYPTOCELL)
  435. ecc_context_t ctx;
  436. #endif
  437. #if defined(WOLFSSL_ECDSA_SET_K) || defined(WOLFSSL_ECDSA_SET_K_ONE_LOOP) || \
  438. defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  439. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  440. #ifndef WOLFSSL_NO_MALLOC
  441. mp_int* sign_k;
  442. #else
  443. mp_int sign_k[1];
  444. byte sign_k_set:1;
  445. #endif
  446. #endif
  447. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  448. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  449. byte deterministic:1;
  450. #endif
  451. #if defined(WOLFSSL_SMALL_STACK_CACHE) && !defined(WOLFSSL_ECC_NO_SMALL_STACK)
  452. mp_int* t1;
  453. mp_int* t2;
  454. #ifdef ALT_ECC_SIZE
  455. mp_int* x;
  456. mp_int* y;
  457. mp_int* z;
  458. #endif
  459. #endif
  460. #ifdef WOLFSSL_DSP
  461. remote_handle64 handle;
  462. #endif
  463. #ifdef ECC_TIMING_RESISTANT
  464. WC_RNG* rng;
  465. #endif
  466. #ifdef WC_ECC_NONBLOCK
  467. ecc_nb_ctx_t* nb_ctx;
  468. #endif
  469. };
  470. WOLFSSL_ABI WOLFSSL_API ecc_key* wc_ecc_key_new(void* heap);
  471. WOLFSSL_ABI WOLFSSL_API void wc_ecc_key_free(ecc_key* key);
  472. /* ECC predefined curve sets */
  473. extern const ecc_set_type ecc_sets[];
  474. extern const size_t ecc_sets_count;
  475. WOLFSSL_API
  476. const char* wc_ecc_get_name(int curve_id);
  477. #ifdef WOLFSSL_PUBLIC_ECC_ADD_DBL
  478. #define ECC_API WOLFSSL_API
  479. #else
  480. #define ECC_API WOLFSSL_LOCAL
  481. #endif
  482. ECC_API int ecc_mul2add(ecc_point* A, mp_int* kA,
  483. ecc_point* B, mp_int* kB,
  484. ecc_point* C, mp_int* a, mp_int* modulus, void* heap);
  485. ECC_API int ecc_map(ecc_point* P, mp_int* modulus, mp_digit mp);
  486. ECC_API int ecc_map_ex(ecc_point* P, mp_int* modulus, mp_digit mp, int ct);
  487. ECC_API int ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R,
  488. mp_int* a, mp_int* modulus, mp_digit mp);
  489. ECC_API int ecc_projective_dbl_point(ecc_point* P, ecc_point* R, mp_int* a,
  490. mp_int* modulus, mp_digit mp);
  491. WOLFSSL_LOCAL
  492. int ecc_projective_add_point_safe(ecc_point* A, ecc_point* B, ecc_point* R,
  493. mp_int* a, mp_int* modulus, mp_digit mp, int* infinity);
  494. WOLFSSL_LOCAL
  495. int ecc_projective_dbl_point_safe(ecc_point* P, ecc_point* R, mp_int* a,
  496. mp_int* modulus, mp_digit mp);
  497. WOLFSSL_ABI WOLFSSL_API
  498. int wc_ecc_make_key(WC_RNG* rng, int keysize, ecc_key* key);
  499. WOLFSSL_ABI WOLFSSL_API
  500. int wc_ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, int curve_id);
  501. WOLFSSL_API
  502. int wc_ecc_make_key_ex2(WC_RNG* rng, int keysize, ecc_key* key, int curve_id,
  503. int flags);
  504. WOLFSSL_API
  505. int wc_ecc_make_pub(ecc_key* key, ecc_point* pubOut);
  506. WOLFSSL_API
  507. int wc_ecc_make_pub_ex(ecc_key* key, ecc_point* pubOut, WC_RNG* rng);
  508. WOLFSSL_ABI WOLFSSL_API
  509. int wc_ecc_check_key(ecc_key* key);
  510. WOLFSSL_API
  511. int wc_ecc_is_point(ecc_point* ecp, mp_int* a, mp_int* b, mp_int* prime);
  512. WOLFSSL_API
  513. int wc_ecc_get_generator(ecc_point* ecp, int curve_idx);
  514. #ifdef HAVE_ECC_DHE
  515. WOLFSSL_ABI WOLFSSL_API
  516. int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
  517. word32* outlen);
  518. WOLFSSL_LOCAL
  519. int wc_ecc_shared_secret_gen(ecc_key* private_key, ecc_point* point,
  520. byte* out, word32 *outlen);
  521. WOLFSSL_API
  522. int wc_ecc_shared_secret_ex(ecc_key* private_key, ecc_point* point,
  523. byte* out, word32 *outlen);
  524. #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  525. defined(PLUTON_CRYPTO_ECC) || defined(WOLFSSL_CRYPTOCELL)
  526. #define wc_ecc_shared_secret_ssh wc_ecc_shared_secret
  527. #else
  528. #define wc_ecc_shared_secret_ssh wc_ecc_shared_secret_ex /* For backwards compat */
  529. #endif
  530. #endif /* HAVE_ECC_DHE */
  531. #ifdef HAVE_ECC_SIGN
  532. WOLFSSL_ABI WOLFSSL_API
  533. int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
  534. WC_RNG* rng, ecc_key* key);
  535. WOLFSSL_API
  536. int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
  537. ecc_key* key, mp_int *r, mp_int *s);
  538. #if defined(WOLFSSL_ECDSA_DETERMINISTIC_K) || \
  539. defined(WOLFSSL_ECDSA_DETERMINISTIC_K_VARIANT)
  540. WOLFSSL_API
  541. int wc_ecc_set_deterministic(ecc_key* key, byte flag);
  542. WOLFSSL_API
  543. int wc_ecc_gen_deterministic_k(const byte* hash, word32 hashSz,
  544. enum wc_HashType hashType, mp_int* priv, mp_int* k, mp_int* order,
  545. void* heap);
  546. #endif
  547. #if defined(WOLFSSL_ECDSA_SET_K) || defined(WOLFSSL_ECDSA_SET_K_ONE_LOOP)
  548. WOLFSSL_API
  549. int wc_ecc_sign_set_k(const byte* k, word32 klen, ecc_key* key);
  550. #endif
  551. #endif /* HAVE_ECC_SIGN */
  552. #ifdef HAVE_ECC_VERIFY
  553. WOLFSSL_ABI WOLFSSL_API
  554. int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
  555. word32 hashlen, int* res, ecc_key* key);
  556. WOLFSSL_API
  557. int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
  558. word32 hashlen, int* res, ecc_key* key);
  559. #endif /* HAVE_ECC_VERIFY */
  560. WOLFSSL_ABI WOLFSSL_API
  561. int wc_ecc_init(ecc_key* key);
  562. WOLFSSL_ABI WOLFSSL_API
  563. int wc_ecc_init_ex(ecc_key* key, void* heap, int devId);
  564. #ifdef WOLF_PRIVATE_KEY_ID
  565. WOLFSSL_API
  566. int wc_ecc_init_id(ecc_key* key, unsigned char* id, int len, void* heap,
  567. int devId);
  568. WOLFSSL_API
  569. int wc_ecc_init_label(ecc_key* key, const char* label, void* heap, int devId);
  570. #endif
  571. #ifdef WOLFSSL_CUSTOM_CURVES
  572. WOLFSSL_LOCAL
  573. void wc_ecc_free_curve(const ecc_set_type* curve, void* heap);
  574. #endif
  575. WOLFSSL_ABI WOLFSSL_API
  576. int wc_ecc_free(ecc_key* key);
  577. WOLFSSL_API
  578. int wc_ecc_set_flags(ecc_key* key, word32 flags);
  579. WOLFSSL_ABI WOLFSSL_API
  580. void wc_ecc_fp_free(void);
  581. WOLFSSL_LOCAL
  582. void wc_ecc_fp_init(void);
  583. #ifdef ECC_TIMING_RESISTANT
  584. WOLFSSL_API
  585. int wc_ecc_set_rng(ecc_key* key, WC_RNG* rng);
  586. #endif
  587. WOLFSSL_API
  588. int wc_ecc_set_curve(ecc_key* key, int keysize, int curve_id);
  589. WOLFSSL_API
  590. int wc_ecc_is_valid_idx(int n);
  591. WOLFSSL_API
  592. int wc_ecc_get_curve_idx(int curve_id);
  593. WOLFSSL_API
  594. int wc_ecc_get_curve_id(int curve_idx);
  595. #define wc_ecc_get_curve_name_from_id wc_ecc_get_name
  596. WOLFSSL_API
  597. int wc_ecc_get_curve_size_from_id(int curve_id);
  598. WOLFSSL_API
  599. int wc_ecc_get_curve_idx_from_name(const char* curveName);
  600. WOLFSSL_API
  601. int wc_ecc_get_curve_size_from_name(const char* curveName);
  602. WOLFSSL_API
  603. int wc_ecc_get_curve_id_from_name(const char* curveName);
  604. WOLFSSL_API
  605. int wc_ecc_get_curve_id_from_params(int fieldSize,
  606. const byte* prime, word32 primeSz, const byte* Af, word32 AfSz,
  607. const byte* Bf, word32 BfSz, const byte* order, word32 orderSz,
  608. const byte* Gx, word32 GxSz, const byte* Gy, word32 GySz, int cofactor);
  609. WOLFSSL_API
  610. int wc_ecc_get_curve_id_from_dp_params(const ecc_set_type* dp);
  611. WOLFSSL_API
  612. int wc_ecc_get_curve_id_from_oid(const byte* oid, word32 len);
  613. WOLFSSL_API const ecc_set_type* wc_ecc_get_curve_params(int curve_idx);
  614. WOLFSSL_API
  615. ecc_point* wc_ecc_new_point(void);
  616. WOLFSSL_API
  617. ecc_point* wc_ecc_new_point_h(void* h);
  618. WOLFSSL_API
  619. void wc_ecc_del_point(ecc_point* p);
  620. WOLFSSL_API
  621. void wc_ecc_del_point_h(ecc_point* p, void* h);
  622. WOLFSSL_API
  623. void wc_ecc_forcezero_point(ecc_point* p);
  624. WOLFSSL_API
  625. int wc_ecc_copy_point(const ecc_point* p, ecc_point *r);
  626. WOLFSSL_API
  627. int wc_ecc_cmp_point(ecc_point* a, ecc_point *b);
  628. WOLFSSL_API
  629. int wc_ecc_point_is_at_infinity(ecc_point *p);
  630. WOLFSSL_API
  631. int wc_ecc_point_is_on_curve(ecc_point *p, int curve_idx);
  632. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A)
  633. WOLFSSL_API
  634. int wc_ecc_mulmod(const mp_int* k, ecc_point *G, ecc_point *R,
  635. mp_int* a, mp_int* modulus, int map);
  636. WOLFSSL_LOCAL
  637. int wc_ecc_mulmod_ex(const mp_int* k, ecc_point *G, ecc_point *R,
  638. mp_int* a, mp_int* modulus, int map, void* heap);
  639. WOLFSSL_LOCAL
  640. int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
  641. mp_int* modulus, mp_int* order, WC_RNG* rng, int map,
  642. void* heap);
  643. #endif /* !WOLFSSL_ATECC508A */
  644. #ifdef HAVE_ECC_KEY_EXPORT
  645. /* ASN key helpers */
  646. WOLFSSL_ABI WOLFSSL_API
  647. int wc_ecc_export_x963(ecc_key* key, byte* out, word32* outLen);
  648. WOLFSSL_ABI WOLFSSL_API
  649. int wc_ecc_export_x963_ex(ecc_key* key, byte* out, word32* outLen,
  650. int compressed);
  651. /* extended functionality with compressed option */
  652. #endif /* HAVE_ECC_KEY_EXPORT */
  653. #ifdef HAVE_ECC_KEY_IMPORT
  654. WOLFSSL_ABI WOLFSSL_API
  655. int wc_ecc_import_x963(const byte* in, word32 inLen, ecc_key* key);
  656. WOLFSSL_API
  657. int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key,
  658. int curve_id);
  659. WOLFSSL_ABI WOLFSSL_API
  660. int wc_ecc_import_private_key(const byte* priv, word32 privSz, const byte* pub,
  661. word32 pubSz, ecc_key* key);
  662. WOLFSSL_API
  663. int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz,
  664. const byte* pub, word32 pubSz, ecc_key* key, int curve_id);
  665. WOLFSSL_ABI WOLFSSL_API
  666. int wc_ecc_rs_to_sig(const char* r, const char* s, byte* out, word32* outlen);
  667. WOLFSSL_API
  668. int wc_ecc_rs_raw_to_sig(const byte* r, word32 rSz, const byte* s, word32 sSz,
  669. byte* out, word32* outlen);
  670. WOLFSSL_API
  671. int wc_ecc_sig_to_rs(const byte* sig, word32 sigLen, byte* r, word32* rLen,
  672. byte* s, word32* sLen);
  673. WOLFSSL_ABI WOLFSSL_API
  674. int wc_ecc_import_raw(ecc_key* key, const char* qx, const char* qy,
  675. const char* d, const char* curveName);
  676. WOLFSSL_API
  677. int wc_ecc_import_raw_ex(ecc_key* key, const char* qx, const char* qy,
  678. const char* d, int curve_id);
  679. WOLFSSL_API
  680. int wc_ecc_import_unsigned(ecc_key* key, const byte* qx, const byte* qy,
  681. const byte* d, int curve_id);
  682. #endif /* HAVE_ECC_KEY_IMPORT */
  683. #ifdef HAVE_ECC_KEY_EXPORT
  684. WOLFSSL_API
  685. int wc_ecc_export_ex(ecc_key* key, byte* qx, word32* qxLen,
  686. byte* qy, word32* qyLen, byte* d, word32* dLen,
  687. int encType);
  688. WOLFSSL_ABI WOLFSSL_API
  689. int wc_ecc_export_private_only(ecc_key* key, byte* out, word32* outLen);
  690. WOLFSSL_API
  691. int wc_ecc_export_public_raw(ecc_key* key, byte* qx, word32* qxLen,
  692. byte* qy, word32* qyLen);
  693. WOLFSSL_API
  694. int wc_ecc_export_private_raw(ecc_key* key, byte* qx, word32* qxLen,
  695. byte* qy, word32* qyLen, byte* d, word32* dLen);
  696. #endif /* HAVE_ECC_KEY_EXPORT */
  697. #ifdef HAVE_ECC_KEY_EXPORT
  698. WOLFSSL_API
  699. int wc_ecc_export_point_der_ex(const int curve_idx, ecc_point* point, byte* out,
  700. word32* outLen, int compressed);
  701. WOLFSSL_API
  702. int wc_ecc_export_point_der(const int curve_idx, ecc_point* point,
  703. byte* out, word32* outLen);
  704. WOLFSSL_LOCAL
  705. int wc_ecc_export_point_der_compressed(const int curve_idx, ecc_point* point,
  706. byte* out, word32* outLen);
  707. #endif /* HAVE_ECC_KEY_EXPORT */
  708. #ifdef HAVE_ECC_KEY_IMPORT
  709. WOLFSSL_API
  710. int wc_ecc_import_point_der_ex(const byte* in, word32 inLen,
  711. const int curve_idx, ecc_point* point,
  712. int shortKeySize);
  713. WOLFSSL_API
  714. int wc_ecc_import_point_der(const byte* in, word32 inLen, const int curve_idx,
  715. ecc_point* point);
  716. #endif /* HAVE_ECC_KEY_IMPORT */
  717. /* size helper */
  718. WOLFSSL_ABI WOLFSSL_API
  719. int wc_ecc_size(ecc_key* key);
  720. WOLFSSL_ABI WOLFSSL_API
  721. int wc_ecc_sig_size_calc(int sz);
  722. WOLFSSL_ABI WOLFSSL_API
  723. int wc_ecc_sig_size(const ecc_key* key);
  724. WOLFSSL_API
  725. int wc_ecc_get_oid(word32 oidSum, const byte** oid, word32* oidSz);
  726. #ifdef WOLFSSL_CUSTOM_CURVES
  727. WOLFSSL_API
  728. int wc_ecc_set_custom_curve(ecc_key* key, const ecc_set_type* dp);
  729. #endif
  730. #ifdef HAVE_ECC_ENCRYPT
  731. /* ecc encrypt */
  732. enum ecEncAlgo {
  733. ecAES_128_CBC = 1, /* default */
  734. ecAES_256_CBC = 2,
  735. ecAES_128_CTR = 3,
  736. ecAES_256_CTR = 4
  737. };
  738. enum ecKdfAlgo {
  739. ecHKDF_SHA256 = 1, /* default */
  740. ecHKDF_SHA1 = 2
  741. };
  742. enum ecMacAlgo {
  743. ecHMAC_SHA256 = 1, /* default */
  744. ecHMAC_SHA1 = 2
  745. };
  746. enum {
  747. KEY_SIZE_128 = 16,
  748. KEY_SIZE_256 = 32,
  749. IV_SIZE_64 = 8,
  750. IV_SIZE_128 = 16,
  751. ECC_MAX_IV_SIZE = 16,
  752. EXCHANGE_SALT_SZ = 16,
  753. EXCHANGE_INFO_SZ = 23
  754. };
  755. enum ecFlags {
  756. REQ_RESP_CLIENT = 1,
  757. REQ_RESP_SERVER = 2
  758. };
  759. #ifndef WOLFSSL_ECIES_GEN_IV_SIZE
  760. #define WOLFSSL_ECIES_GEN_IV_SIZE 12
  761. #endif
  762. typedef struct ecEncCtx ecEncCtx;
  763. WOLFSSL_ABI WOLFSSL_API
  764. ecEncCtx* wc_ecc_ctx_new(int flags, WC_RNG* rng);
  765. WOLFSSL_API
  766. ecEncCtx* wc_ecc_ctx_new_ex(int flags, WC_RNG* rng, void* heap);
  767. WOLFSSL_ABI WOLFSSL_API
  768. void wc_ecc_ctx_free(ecEncCtx* ctx);
  769. WOLFSSL_ABI WOLFSSL_API
  770. int wc_ecc_ctx_reset(ecEncCtx* ctx, WC_RNG* rng); /* reset for use again w/o alloc/free */
  771. WOLFSSL_API
  772. int wc_ecc_ctx_set_algo(ecEncCtx* ctx, byte encAlgo, byte kdfAlgo,
  773. byte macAlgo);
  774. WOLFSSL_API
  775. const byte* wc_ecc_ctx_get_own_salt(ecEncCtx* ctx);
  776. WOLFSSL_API
  777. int wc_ecc_ctx_set_peer_salt(ecEncCtx* ctx, const byte* salt);
  778. WOLFSSL_API
  779. int wc_ecc_ctx_set_kdf_salt(ecEncCtx* ctx, const byte* salt, word32 sz);
  780. WOLFSSL_API
  781. int wc_ecc_ctx_set_info(ecEncCtx* ctx, const byte* info, int sz);
  782. WOLFSSL_ABI WOLFSSL_API
  783. int wc_ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
  784. word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
  785. WOLFSSL_API
  786. int wc_ecc_encrypt_ex(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
  787. word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx, int compressed);
  788. WOLFSSL_ABI WOLFSSL_API
  789. int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
  790. word32 msgSz, byte* out, word32* outSz, ecEncCtx* ctx);
  791. #endif /* HAVE_ECC_ENCRYPT */
  792. #ifdef HAVE_X963_KDF
  793. WOLFSSL_API int wc_X963_KDF(enum wc_HashType type, const byte* secret,
  794. word32 secretSz, const byte* sinfo, word32 sinfoSz,
  795. byte* out, word32 outSz);
  796. #endif
  797. #ifdef ECC_CACHE_CURVE
  798. WOLFSSL_API int wc_ecc_curve_cache_init(void);
  799. WOLFSSL_API void wc_ecc_curve_cache_free(void);
  800. #endif
  801. WOLFSSL_API
  802. int wc_ecc_gen_k(WC_RNG* rng, int size, mp_int* k, mp_int* order);
  803. #ifdef WOLFSSL_DSP
  804. WOLFSSL_API
  805. int wc_ecc_set_handle(ecc_key* key, remote_handle64 handle);
  806. WOLFSSL_LOCAL
  807. int sp_dsp_ecc_verify_256(remote_handle64 handle, const byte* hash, word32 hashLen, mp_int* pX,
  808. mp_int* pY, mp_int* pZ, mp_int* r, mp_int* sm, int* res, void* heap);
  809. #endif
  810. #ifdef WC_ECC_NONBLOCK
  811. WOLFSSL_API int wc_ecc_set_nonblock(ecc_key *key, ecc_nb_ctx_t* ctx);
  812. #endif
  813. #ifdef __cplusplus
  814. } /* extern "C" */
  815. #endif
  816. #endif /* HAVE_ECC */
  817. #endif /* WOLF_CRYPT_ECC_H */