ecc.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) 2013, 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
  7. * met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef _CRYPTO_ECC_H
  27. #define _CRYPTO_ECC_H
  28. /* One digit is u64 qword. */
  29. #define ECC_CURVE_NIST_P192_DIGITS 3
  30. #define ECC_CURVE_NIST_P256_DIGITS 4
  31. #define ECC_MAX_DIGITS (512 / 64)
  32. #define ECC_DIGITS_TO_BYTES_SHIFT 3
  33. /**
  34. * struct ecc_point - elliptic curve point in affine coordinates
  35. *
  36. * @x: X coordinate in vli form.
  37. * @y: Y coordinate in vli form.
  38. * @ndigits: Length of vlis in u64 qwords.
  39. */
  40. struct ecc_point {
  41. u64 *x;
  42. u64 *y;
  43. u8 ndigits;
  44. };
  45. #define ECC_POINT_INIT(x, y, ndigits) (struct ecc_point) { x, y, ndigits }
  46. /**
  47. * struct ecc_curve - definition of elliptic curve
  48. *
  49. * @name: Short name of the curve.
  50. * @g: Generator point of the curve.
  51. * @p: Prime number, if Barrett's reduction is used for this curve
  52. * pre-calculated value 'mu' is appended to the @p after ndigits.
  53. * Use of Barrett's reduction is heuristically determined in
  54. * vli_mmod_fast().
  55. * @n: Order of the curve group.
  56. * @a: Curve parameter a.
  57. * @b: Curve parameter b.
  58. */
  59. struct ecc_curve {
  60. char *name;
  61. struct ecc_point g;
  62. u64 *p;
  63. u64 *n;
  64. u64 *a;
  65. u64 *b;
  66. };
  67. /**
  68. * ecc_is_key_valid() - Validate a given ECDH private key
  69. *
  70. * @curve_id: id representing the curve to use
  71. * @ndigits: curve's number of digits
  72. * @private_key: private key to be used for the given curve
  73. * @private_key_len: private key length
  74. *
  75. * Returns 0 if the key is acceptable, a negative value otherwise
  76. */
  77. int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
  78. const u64 *private_key, unsigned int private_key_len);
  79. /**
  80. * ecc_gen_privkey() - Generates an ECC private key.
  81. * The private key is a random integer in the range 0 < random < n, where n is a
  82. * prime that is the order of the cyclic subgroup generated by the distinguished
  83. * point G.
  84. * @curve_id: id representing the curve to use
  85. * @ndigits: curve number of digits
  86. * @private_key: buffer for storing the generated private key
  87. *
  88. * Returns 0 if the private key was generated successfully, a negative value
  89. * if an error occurred.
  90. */
  91. int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey);
  92. /**
  93. * ecc_make_pub_key() - Compute an ECC public key
  94. *
  95. * @curve_id: id representing the curve to use
  96. * @ndigits: curve's number of digits
  97. * @private_key: pregenerated private key for the given curve
  98. * @public_key: buffer for storing the generated public key
  99. *
  100. * Returns 0 if the public key was generated successfully, a negative value
  101. * if an error occurred.
  102. */
  103. int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
  104. const u64 *private_key, u64 *public_key);
  105. /**
  106. * crypto_ecdh_shared_secret() - Compute a shared secret
  107. *
  108. * @curve_id: id representing the curve to use
  109. * @ndigits: curve's number of digits
  110. * @private_key: private key of part A
  111. * @public_key: public key of counterpart B
  112. * @secret: buffer for storing the calculated shared secret
  113. *
  114. * Note: It is recommended that you hash the result of crypto_ecdh_shared_secret
  115. * before using it for symmetric encryption or HMAC.
  116. *
  117. * Returns 0 if the shared secret was generated successfully, a negative value
  118. * if an error occurred.
  119. */
  120. int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
  121. const u64 *private_key, const u64 *public_key,
  122. u64 *secret);
  123. /**
  124. * ecc_is_pubkey_valid_partial() - Partial public key validation
  125. *
  126. * @curve: elliptic curve domain parameters
  127. * @pk: public key as a point
  128. *
  129. * Valdiate public key according to SP800-56A section 5.6.2.3.4 ECC Partial
  130. * Public-Key Validation Routine.
  131. *
  132. * Note: There is no check that the public key is in the correct elliptic curve
  133. * subgroup.
  134. *
  135. * Return: 0 if validation is successful, -EINVAL if validation is failed.
  136. */
  137. int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
  138. struct ecc_point *pk);
  139. /**
  140. * ecc_is_pubkey_valid_full() - Full public key validation
  141. *
  142. * @curve: elliptic curve domain parameters
  143. * @pk: public key as a point
  144. *
  145. * Valdiate public key according to SP800-56A section 5.6.2.3.3 ECC Full
  146. * Public-Key Validation Routine.
  147. *
  148. * Return: 0 if validation is successful, -EINVAL if validation is failed.
  149. */
  150. int ecc_is_pubkey_valid_full(const struct ecc_curve *curve,
  151. struct ecc_point *pk);
  152. /**
  153. * vli_is_zero() - Determine is vli is zero
  154. *
  155. * @vli: vli to check.
  156. * @ndigits: length of the @vli
  157. */
  158. bool vli_is_zero(const u64 *vli, unsigned int ndigits);
  159. /**
  160. * vli_cmp() - compare left and right vlis
  161. *
  162. * @left: vli
  163. * @right: vli
  164. * @ndigits: length of both vlis
  165. *
  166. * Returns sign of @left - @right, i.e. -1 if @left < @right,
  167. * 0 if @left == @right, 1 if @left > @right.
  168. */
  169. int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits);
  170. /**
  171. * vli_sub() - Subtracts right from left
  172. *
  173. * @result: where to write result
  174. * @left: vli
  175. * @right vli
  176. * @ndigits: length of all vlis
  177. *
  178. * Note: can modify in-place.
  179. *
  180. * Return: carry bit.
  181. */
  182. u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
  183. unsigned int ndigits);
  184. /**
  185. * vli_from_be64() - Load vli from big-endian u64 array
  186. *
  187. * @dest: destination vli
  188. * @src: source array of u64 BE values
  189. * @ndigits: length of both vli and array
  190. */
  191. void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits);
  192. /**
  193. * vli_from_le64() - Load vli from little-endian u64 array
  194. *
  195. * @dest: destination vli
  196. * @src: source array of u64 LE values
  197. * @ndigits: length of both vli and array
  198. */
  199. void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits);
  200. /**
  201. * vli_mod_inv() - Modular inversion
  202. *
  203. * @result: where to write vli number
  204. * @input: vli value to operate on
  205. * @mod: modulus
  206. * @ndigits: length of all vlis
  207. */
  208. void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
  209. unsigned int ndigits);
  210. /**
  211. * vli_mod_mult_slow() - Modular multiplication
  212. *
  213. * @result: where to write result value
  214. * @left: vli number to multiply with @right
  215. * @right: vli number to multiply with @left
  216. * @mod: modulus
  217. * @ndigits: length of all vlis
  218. *
  219. * Note: Assumes that mod is big enough curve order.
  220. */
  221. void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,
  222. const u64 *mod, unsigned int ndigits);
  223. /**
  224. * ecc_point_mult_shamir() - Add two points multiplied by scalars
  225. *
  226. * @result: resulting point
  227. * @x: scalar to multiply with @p
  228. * @p: point to multiply with @x
  229. * @y: scalar to multiply with @q
  230. * @q: point to multiply with @y
  231. * @curve: curve
  232. *
  233. * Returns result = x * p + x * q over the curve.
  234. * This works faster than two multiplications and addition.
  235. */
  236. void ecc_point_mult_shamir(const struct ecc_point *result,
  237. const u64 *x, const struct ecc_point *p,
  238. const u64 *y, const struct ecc_point *q,
  239. const struct ecc_curve *curve);
  240. #endif