ecc.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /* ecc.h - TinyCrypt interface to common ECC functions */
  2. /* Copyright (c) 2014, Kenneth MacKay
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  29. *
  30. * Redistribution and use in source and binary forms, with or without
  31. * modification, are permitted provided that the following conditions are met:
  32. *
  33. * - Redistributions of source code must retain the above copyright notice,
  34. * this list of conditions and the following disclaimer.
  35. *
  36. * - Redistributions in binary form must reproduce the above copyright
  37. * notice, this list of conditions and the following disclaimer in the
  38. * documentation and/or other materials provided with the distribution.
  39. *
  40. * - Neither the name of Intel Corporation nor the names of its contributors
  41. * may be used to endorse or promote products derived from this software
  42. * without specific prior written permission.
  43. *
  44. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  45. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  48. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  49. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  50. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  51. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  53. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  54. * POSSIBILITY OF SUCH DAMAGE.
  55. */
  56. /**
  57. * @file
  58. * @brief -- Interface to common ECC functions.
  59. *
  60. * Overview: This software is an implementation of common functions
  61. * necessary to elliptic curve cryptography. This implementation uses
  62. * curve NIST p-256.
  63. *
  64. * Security: The curve NIST p-256 provides approximately 128 bits of security.
  65. *
  66. */
  67. #ifndef __TC_UECC_H__
  68. #define __TC_UECC_H__
  69. #include <stdint.h>
  70. #ifdef __cplusplus
  71. extern "C" {
  72. #endif
  73. /* Word size (4 bytes considering 32-bits architectures) */
  74. #define uECC_WORD_SIZE 4
  75. /* setting max number of calls to prng: */
  76. #ifndef uECC_RNG_MAX_TRIES
  77. #define uECC_RNG_MAX_TRIES 64
  78. #endif
  79. /* defining data types to store word and bit counts: */
  80. typedef int8_t wordcount_t;
  81. typedef int16_t bitcount_t;
  82. /* defining data type for comparison result: */
  83. typedef int8_t cmpresult_t;
  84. /* defining data type to store ECC coordinate/point in 32bits words: */
  85. typedef unsigned int uECC_word_t;
  86. /* defining data type to store an ECC coordinate/point in 64bits words: */
  87. typedef uint64_t uECC_dword_t;
  88. /* defining masks useful for ecc computations: */
  89. #define HIGH_BIT_SET 0x80000000
  90. #define uECC_WORD_BITS 32
  91. #define uECC_WORD_BITS_SHIFT 5
  92. #define uECC_WORD_BITS_MASK 0x01F
  93. /* Number of words of 32 bits to represent an element of the the curve p-256: */
  94. #define NUM_ECC_WORDS 8
  95. /* Number of bytes to represent an element of the the curve p-256: */
  96. #define NUM_ECC_BYTES (uECC_WORD_SIZE*NUM_ECC_WORDS)
  97. /* structure that represents an elliptic curve (e.g. p256):*/
  98. struct uECC_Curve_t;
  99. typedef const struct uECC_Curve_t * uECC_Curve;
  100. struct uECC_Curve_t {
  101. wordcount_t num_words;
  102. wordcount_t num_bytes;
  103. bitcount_t num_n_bits;
  104. uECC_word_t p[NUM_ECC_WORDS];
  105. uECC_word_t n[NUM_ECC_WORDS];
  106. uECC_word_t G[NUM_ECC_WORDS * 2];
  107. uECC_word_t b[NUM_ECC_WORDS];
  108. void (*double_jacobian)(uECC_word_t * X1, uECC_word_t * Y1, uECC_word_t * Z1,
  109. uECC_Curve curve);
  110. void (*x_side)(uECC_word_t *result, const uECC_word_t *x, uECC_Curve curve);
  111. void (*mmod_fast)(uECC_word_t *result, uECC_word_t *product);
  112. };
  113. /*
  114. * @brief computes doubling of point ion jacobian coordinates, in place.
  115. * @param X1 IN/OUT -- x coordinate
  116. * @param Y1 IN/OUT -- y coordinate
  117. * @param Z1 IN/OUT -- z coordinate
  118. * @param curve IN -- elliptic curve
  119. */
  120. void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
  121. uECC_word_t * Z1, uECC_Curve curve);
  122. /*
  123. * @brief Computes x^3 + ax + b. result must not overlap x.
  124. * @param result OUT -- x^3 + ax + b
  125. * @param x IN -- value of x
  126. * @param curve IN -- elliptic curve
  127. */
  128. void x_side_default(uECC_word_t *result, const uECC_word_t *x,
  129. uECC_Curve curve);
  130. /*
  131. * @brief Computes result = product % curve_p
  132. * from http://www.nsa.gov/ia/_files/nist-routines.pdf
  133. * @param result OUT -- product % curve_p
  134. * @param product IN -- value to be reduced mod curve_p
  135. */
  136. void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int *product);
  137. /* Bytes to words ordering: */
  138. #define BYTES_TO_WORDS_8(a, b, c, d, e, f, g, h) 0x##d##c##b##a, 0x##h##g##f##e
  139. #define BYTES_TO_WORDS_4(a, b, c, d) 0x##d##c##b##a
  140. #define BITS_TO_WORDS(num_bits) \
  141. ((num_bits + ((uECC_WORD_SIZE * 8) - 1)) / (uECC_WORD_SIZE * 8))
  142. #define BITS_TO_BYTES(num_bits) ((num_bits + 7) / 8)
  143. /* definition of curve NIST p-256: */
  144. static const struct uECC_Curve_t curve_secp256r1 = {
  145. NUM_ECC_WORDS,
  146. NUM_ECC_BYTES,
  147. 256, /* num_n_bits */ {
  148. BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
  149. BYTES_TO_WORDS_8(FF, FF, FF, FF, 00, 00, 00, 00),
  150. BYTES_TO_WORDS_8(00, 00, 00, 00, 00, 00, 00, 00),
  151. BYTES_TO_WORDS_8(01, 00, 00, 00, FF, FF, FF, FF)
  152. }, {
  153. BYTES_TO_WORDS_8(51, 25, 63, FC, C2, CA, B9, F3),
  154. BYTES_TO_WORDS_8(84, 9E, 17, A7, AD, FA, E6, BC),
  155. BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
  156. BYTES_TO_WORDS_8(00, 00, 00, 00, FF, FF, FF, FF)
  157. }, {
  158. BYTES_TO_WORDS_8(96, C2, 98, D8, 45, 39, A1, F4),
  159. BYTES_TO_WORDS_8(A0, 33, EB, 2D, 81, 7D, 03, 77),
  160. BYTES_TO_WORDS_8(F2, 40, A4, 63, E5, E6, BC, F8),
  161. BYTES_TO_WORDS_8(47, 42, 2C, E1, F2, D1, 17, 6B),
  162. BYTES_TO_WORDS_8(F5, 51, BF, 37, 68, 40, B6, CB),
  163. BYTES_TO_WORDS_8(CE, 5E, 31, 6B, 57, 33, CE, 2B),
  164. BYTES_TO_WORDS_8(16, 9E, 0F, 7C, 4A, EB, E7, 8E),
  165. BYTES_TO_WORDS_8(9B, 7F, 1A, FE, E2, 42, E3, 4F)
  166. }, {
  167. BYTES_TO_WORDS_8(4B, 60, D2, 27, 3E, 3C, CE, 3B),
  168. BYTES_TO_WORDS_8(F6, B0, 53, CC, B0, 06, 1D, 65),
  169. BYTES_TO_WORDS_8(BC, 86, 98, 76, 55, BD, EB, B3),
  170. BYTES_TO_WORDS_8(E7, 93, 3A, AA, D8, 35, C6, 5A)
  171. },
  172. &double_jacobian_default,
  173. &x_side_default,
  174. &vli_mmod_fast_secp256r1
  175. };
  176. uECC_Curve uECC_secp256r1(void);
  177. /*
  178. * @brief Generates a random integer in the range 0 < random < top.
  179. * Both random and top have num_words words.
  180. * @param random OUT -- random integer in the range 0 < random < top
  181. * @param top IN -- upper limit
  182. * @param num_words IN -- number of words
  183. * @return a random integer in the range 0 < random < top
  184. */
  185. int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
  186. wordcount_t num_words);
  187. /* uECC_RNG_Function type
  188. * The RNG function should fill 'size' random bytes into 'dest'. It should
  189. * return 1 if 'dest' was filled with random data, or 0 if the random data could
  190. * not be generated. The filled-in values should be either truly random, or from
  191. * a cryptographically-secure PRNG.
  192. *
  193. * A correctly functioning RNG function must be set (using uECC_set_rng())
  194. * before calling uECC_make_key() or uECC_sign().
  195. *
  196. * Setting a correctly functioning RNG function improves the resistance to
  197. * side-channel attacks for uECC_shared_secret().
  198. *
  199. * A correct RNG function is set by default. If you are building on another
  200. * POSIX-compliant system that supports /dev/random or /dev/urandom, you can
  201. * define uECC_POSIX to use the predefined RNG.
  202. */
  203. typedef int(*uECC_RNG_Function)(uint8_t *dest, unsigned int size);
  204. /*
  205. * @brief Set the function that will be used to generate random bytes. The RNG
  206. * function should return 1 if the random data was generated, or 0 if the random
  207. * data could not be generated.
  208. *
  209. * @note On platforms where there is no predefined RNG function, this must be
  210. * called before uECC_make_key() or uECC_sign() are used.
  211. *
  212. * @param rng_function IN -- function that will be used to generate random bytes
  213. */
  214. void uECC_set_rng(uECC_RNG_Function rng_function);
  215. /*
  216. * @brief provides current uECC_RNG_Function.
  217. * @return Returns the function that will be used to generate random bytes.
  218. */
  219. uECC_RNG_Function uECC_get_rng(void);
  220. /*
  221. * @brief computes the size of a private key for the curve in bytes.
  222. * @param curve IN -- elliptic curve
  223. * @return size of a private key for the curve in bytes.
  224. */
  225. int uECC_curve_private_key_size(uECC_Curve curve);
  226. /*
  227. * @brief computes the size of a public key for the curve in bytes.
  228. * @param curve IN -- elliptic curve
  229. * @return the size of a public key for the curve in bytes.
  230. */
  231. int uECC_curve_public_key_size(uECC_Curve curve);
  232. /*
  233. * @brief Compute the corresponding public key for a private key.
  234. * @param private_key IN -- The private key to compute the public key for
  235. * @param public_key OUT -- Will be filled in with the corresponding public key
  236. * @param curve
  237. * @return Returns 1 if key was computed successfully, 0 if an error occurred.
  238. */
  239. int uECC_compute_public_key(const uint8_t *private_key,
  240. uint8_t *public_key, uECC_Curve curve);
  241. /*
  242. * @brief Compute public-key.
  243. * @return corresponding public-key.
  244. * @param result OUT -- public-key
  245. * @param private_key IN -- private-key
  246. * @param curve IN -- elliptic curve
  247. */
  248. uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
  249. uECC_word_t *private_key, uECC_Curve curve);
  250. /*
  251. * @brief Regularize the bitcount for the private key so that attackers cannot
  252. * use a side channel attack to learn the number of leading zeros.
  253. * @return Regularized k
  254. * @param k IN -- private-key
  255. * @param k0 IN/OUT -- regularized k
  256. * @param k1 IN/OUT -- regularized k
  257. * @param curve IN -- elliptic curve
  258. */
  259. uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0,
  260. uECC_word_t *k1, uECC_Curve curve);
  261. /*
  262. * @brief Point multiplication algorithm using Montgomery's ladder with co-Z
  263. * coordinates. See http://eprint.iacr.org/2011/338.pdf.
  264. * @note Result may overlap point.
  265. * @param result OUT -- returns scalar*point
  266. * @param point IN -- elliptic curve point
  267. * @param scalar IN -- scalar
  268. * @param initial_Z IN -- initial value for z
  269. * @param num_bits IN -- number of bits in scalar
  270. * @param curve IN -- elliptic curve
  271. */
  272. void EccPoint_mult(uECC_word_t * result, const uECC_word_t * point,
  273. const uECC_word_t * scalar, const uECC_word_t * initial_Z,
  274. bitcount_t num_bits, uECC_Curve curve);
  275. /*
  276. * @brief Constant-time comparison to zero - secure way to compare long integers
  277. * @param vli IN -- very long integer
  278. * @param num_words IN -- number of words in the vli
  279. * @return 1 if vli == 0, 0 otherwise.
  280. */
  281. uECC_word_t uECC_vli_isZero(const uECC_word_t *vli, wordcount_t num_words);
  282. /*
  283. * @brief Check if 'point' is the point at infinity
  284. * @param point IN -- elliptic curve point
  285. * @param curve IN -- elliptic curve
  286. * @return if 'point' is the point at infinity, 0 otherwise.
  287. */
  288. uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve);
  289. /*
  290. * @brief computes the sign of left - right, in constant time.
  291. * @param left IN -- left term to be compared
  292. * @param right IN -- right term to be compared
  293. * @param num_words IN -- number of words
  294. * @return the sign of left - right
  295. */
  296. cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right,
  297. wordcount_t num_words);
  298. /*
  299. * @brief computes sign of left - right, not in constant time.
  300. * @note should not be used if inputs are part of a secret
  301. * @param left IN -- left term to be compared
  302. * @param right IN -- right term to be compared
  303. * @param num_words IN -- number of words
  304. * @return the sign of left - right
  305. */
  306. cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left, const uECC_word_t *right,
  307. wordcount_t num_words);
  308. /*
  309. * @brief Computes result = (left - right) % mod.
  310. * @note Assumes that (left < mod) and (right < mod), and that result does not
  311. * overlap mod.
  312. * @param result OUT -- (left - right) % mod
  313. * @param left IN -- leftright term in modular subtraction
  314. * @param right IN -- right term in modular subtraction
  315. * @param mod IN -- mod
  316. * @param num_words IN -- number of words
  317. */
  318. void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
  319. const uECC_word_t *right, const uECC_word_t *mod,
  320. wordcount_t num_words);
  321. /*
  322. * @brief Computes P' = (x1', y1', Z3), P + Q = (x3, y3, Z3) or
  323. * P => P', Q => P + Q
  324. * @note assumes Input P = (x1, y1, Z), Q = (x2, y2, Z)
  325. * @param X1 IN -- x coordinate of P
  326. * @param Y1 IN -- y coordinate of P
  327. * @param X2 IN -- x coordinate of Q
  328. * @param Y2 IN -- y coordinate of Q
  329. * @param curve IN -- elliptic curve
  330. */
  331. void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1, uECC_word_t * X2,
  332. uECC_word_t * Y2, uECC_Curve curve);
  333. /*
  334. * @brief Computes (x1 * z^2, y1 * z^3)
  335. * @param X1 IN -- previous x1 coordinate
  336. * @param Y1 IN -- previous y1 coordinate
  337. * @param Z IN -- z value
  338. * @param curve IN -- elliptic curve
  339. */
  340. void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z,
  341. uECC_Curve curve);
  342. /*
  343. * @brief Check if bit is set.
  344. * @return Returns nonzero if bit 'bit' of vli is set.
  345. * @warning It is assumed that the value provided in 'bit' is within the
  346. * boundaries of the word-array 'vli'.
  347. * @note The bit ordering layout assumed for vli is: {31, 30, ..., 0},
  348. * {63, 62, ..., 32}, {95, 94, ..., 64}, {127, 126,..., 96} for a vli consisting
  349. * of 4 uECC_word_t elements.
  350. */
  351. uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit);
  352. /*
  353. * @brief Computes result = product % mod, where product is 2N words long.
  354. * @param result OUT -- product % mod
  355. * @param mod IN -- module
  356. * @param num_words IN -- number of words
  357. * @warning Currently only designed to work for curve_p or curve_n.
  358. */
  359. void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
  360. const uECC_word_t *mod, wordcount_t num_words);
  361. /*
  362. * @brief Computes modular product (using curve->mmod_fast)
  363. * @param result OUT -- (left * right) mod % curve_p
  364. * @param left IN -- left term in product
  365. * @param right IN -- right term in product
  366. * @param curve IN -- elliptic curve
  367. */
  368. void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
  369. const uECC_word_t *right, uECC_Curve curve);
  370. /*
  371. * @brief Computes result = left - right.
  372. * @note Can modify in place.
  373. * @param result OUT -- left - right
  374. * @param left IN -- left term in subtraction
  375. * @param right IN -- right term in subtraction
  376. * @param num_words IN -- number of words
  377. * @return borrow
  378. */
  379. uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
  380. const uECC_word_t *right, wordcount_t num_words);
  381. /*
  382. * @brief Constant-time comparison function(secure way to compare long ints)
  383. * @param left IN -- left term in comparison
  384. * @param right IN -- right term in comparison
  385. * @param num_words IN -- number of words
  386. * @return Returns 0 if left == right, 1 otherwise.
  387. */
  388. uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right,
  389. wordcount_t num_words);
  390. /*
  391. * @brief Computes (left * right) % mod
  392. * @param result OUT -- (left * right) % mod
  393. * @param left IN -- left term in product
  394. * @param right IN -- right term in product
  395. * @param mod IN -- mod
  396. * @param num_words IN -- number of words
  397. */
  398. void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
  399. const uECC_word_t *right, const uECC_word_t *mod,
  400. wordcount_t num_words);
  401. /*
  402. * @brief Computes (1 / input) % mod
  403. * @note All VLIs are the same size.
  404. * @note See "Euclid's GCD to Montgomery Multiplication to the Great Divide"
  405. * @param result OUT -- (1 / input) % mod
  406. * @param input IN -- value to be modular inverted
  407. * @param mod IN -- mod
  408. * @param num_words -- number of words
  409. */
  410. void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
  411. const uECC_word_t *mod, wordcount_t num_words);
  412. /*
  413. * @brief Sets dest = src.
  414. * @param dest OUT -- destination buffer
  415. * @param src IN -- origin buffer
  416. * @param num_words IN -- number of words
  417. */
  418. void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src,
  419. wordcount_t num_words);
  420. /*
  421. * @brief Computes (left + right) % mod.
  422. * @note Assumes that (left < mod) and right < mod), and that result does not
  423. * overlap mod.
  424. * @param result OUT -- (left + right) % mod.
  425. * @param left IN -- left term in addition
  426. * @param right IN -- right term in addition
  427. * @param mod IN -- mod
  428. * @param num_words IN -- number of words
  429. */
  430. void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
  431. const uECC_word_t *right, const uECC_word_t *mod,
  432. wordcount_t num_words);
  433. /*
  434. * @brief Counts the number of bits required to represent vli.
  435. * @param vli IN -- very long integer
  436. * @param max_words IN -- number of words
  437. * @return number of bits in given vli
  438. */
  439. bitcount_t uECC_vli_numBits(const uECC_word_t *vli,
  440. const wordcount_t max_words);
  441. /*
  442. * @brief Erases (set to 0) vli
  443. * @param vli IN -- very long integer
  444. * @param num_words IN -- number of words
  445. */
  446. void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words);
  447. /*
  448. * @brief check if it is a valid point in the curve
  449. * @param point IN -- point to be checked
  450. * @param curve IN -- elliptic curve
  451. * @return 0 if point is valid
  452. * @exception returns -1 if it is a point at infinity
  453. * @exception returns -2 if x or y is smaller than p,
  454. * @exception returns -3 if y^2 != x^3 + ax + b.
  455. */
  456. int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve);
  457. /*
  458. * @brief Check if a public key is valid.
  459. * @param public_key IN -- The public key to be checked.
  460. * @return returns 0 if the public key is valid
  461. * @exception returns -1 if it is a point at infinity
  462. * @exception returns -2 if x or y is smaller than p,
  463. * @exception returns -3 if y^2 != x^3 + ax + b.
  464. * @exception returns -4 if public key is the group generator.
  465. *
  466. * @note Note that you are not required to check for a valid public key before
  467. * using any other uECC functions. However, you may wish to avoid spending CPU
  468. * time computing a shared secret or verifying a signature using an invalid
  469. * public key.
  470. */
  471. int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve);
  472. /*
  473. * @brief Converts an integer in uECC native format to big-endian bytes.
  474. * @param bytes OUT -- bytes representation
  475. * @param num_bytes IN -- number of bytes
  476. * @param native IN -- uECC native representation
  477. */
  478. void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
  479. const unsigned int *native);
  480. /*
  481. * @brief Converts big-endian bytes to an integer in uECC native format.
  482. * @param native OUT -- uECC native representation
  483. * @param bytes IN -- bytes representation
  484. * @param num_bytes IN -- number of bytes
  485. */
  486. void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
  487. int num_bytes);
  488. #ifdef __cplusplus
  489. }
  490. #endif
  491. #endif /* __TC_UECC_H__ */