ecc.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #include <crypto/ecc_curve.h>
  29. #include <linux/unaligned.h>
  30. /* One digit is u64 qword. */
  31. #define ECC_CURVE_NIST_P192_DIGITS 3
  32. #define ECC_CURVE_NIST_P256_DIGITS 4
  33. #define ECC_CURVE_NIST_P384_DIGITS 6
  34. #define ECC_CURVE_NIST_P521_DIGITS 9
  35. #define ECC_MAX_DIGITS DIV_ROUND_UP(521, 64) /* NIST P521 */
  36. #define ECC_DIGITS_TO_BYTES_SHIFT 3
  37. #define ECC_MAX_BYTES (ECC_MAX_DIGITS << ECC_DIGITS_TO_BYTES_SHIFT)
  38. #define ECC_POINT_INIT(x, y, ndigits) (struct ecc_point) { x, y, ndigits }
  39. /**
  40. * ecc_swap_digits() - Copy ndigits from big endian array to native array
  41. * @in: Input array
  42. * @out: Output array
  43. * @ndigits: Number of digits to copy
  44. */
  45. static inline void ecc_swap_digits(const void *in, u64 *out, unsigned int ndigits)
  46. {
  47. const __be64 *src = (__force __be64 *)in;
  48. int i;
  49. for (i = 0; i < ndigits; i++)
  50. out[i] = get_unaligned_be64(&src[ndigits - 1 - i]);
  51. }
  52. /**
  53. * ecc_digits_from_bytes() - Create ndigits-sized digits array from byte array
  54. * @in: Input byte array
  55. * @nbytes Size of input byte array
  56. * @out Output digits array
  57. * @ndigits: Number of digits to create from byte array
  58. *
  59. * The first byte in the input byte array is expected to hold the most
  60. * significant bits of the large integer.
  61. */
  62. void ecc_digits_from_bytes(const u8 *in, unsigned int nbytes,
  63. u64 *out, unsigned int ndigits);
  64. /**
  65. * ecc_is_key_valid() - Validate a given ECDH private key
  66. *
  67. * @curve_id: id representing the curve to use
  68. * @ndigits: curve's number of digits
  69. * @private_key: private key to be used for the given curve
  70. * @private_key_len: private key length
  71. *
  72. * Returns 0 if the key is acceptable, a negative value otherwise
  73. */
  74. int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
  75. const u64 *private_key, unsigned int private_key_len);
  76. /**
  77. * ecc_gen_privkey() - Generates an ECC private key.
  78. * The private key is a random integer in the range 0 < random < n, where n is a
  79. * prime that is the order of the cyclic subgroup generated by the distinguished
  80. * point G.
  81. * @curve_id: id representing the curve to use
  82. * @ndigits: curve number of digits
  83. * @private_key: buffer for storing the generated private key
  84. *
  85. * Returns 0 if the private key was generated successfully, a negative value
  86. * if an error occurred.
  87. */
  88. int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits,
  89. u64 *private_key);
  90. /**
  91. * ecc_make_pub_key() - Compute an ECC public key
  92. *
  93. * @curve_id: id representing the curve to use
  94. * @ndigits: curve's number of digits
  95. * @private_key: pregenerated private key for the given curve
  96. * @public_key: buffer for storing the generated public key
  97. *
  98. * Returns 0 if the public key was generated successfully, a negative value
  99. * if an error occurred.
  100. */
  101. int ecc_make_pub_key(const unsigned int curve_id, unsigned int ndigits,
  102. const u64 *private_key, u64 *public_key);
  103. /**
  104. * crypto_ecdh_shared_secret() - Compute a shared secret
  105. *
  106. * @curve_id: id representing the curve to use
  107. * @ndigits: curve's number of digits
  108. * @private_key: private key of part A
  109. * @public_key: public key of counterpart B
  110. * @secret: buffer for storing the calculated shared secret
  111. *
  112. * Note: It is recommended that you hash the result of crypto_ecdh_shared_secret
  113. * before using it for symmetric encryption or HMAC.
  114. *
  115. * Returns 0 if the shared secret was generated successfully, a negative value
  116. * if an error occurred.
  117. */
  118. int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
  119. const u64 *private_key, const u64 *public_key,
  120. u64 *secret);
  121. /**
  122. * ecc_is_pubkey_valid_partial() - Partial public key validation
  123. *
  124. * @curve: elliptic curve domain parameters
  125. * @pk: public key as a point
  126. *
  127. * Valdiate public key according to SP800-56A section 5.6.2.3.4 ECC Partial
  128. * Public-Key Validation Routine.
  129. *
  130. * Note: There is no check that the public key is in the correct elliptic curve
  131. * subgroup.
  132. *
  133. * Return: 0 if validation is successful, -EINVAL if validation is failed.
  134. */
  135. int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
  136. struct ecc_point *pk);
  137. /**
  138. * ecc_is_pubkey_valid_full() - Full public key validation
  139. *
  140. * @curve: elliptic curve domain parameters
  141. * @pk: public key as a point
  142. *
  143. * Valdiate public key according to SP800-56A section 5.6.2.3.3 ECC Full
  144. * Public-Key Validation Routine.
  145. *
  146. * Return: 0 if validation is successful, -EINVAL if validation is failed.
  147. */
  148. int ecc_is_pubkey_valid_full(const struct ecc_curve *curve,
  149. struct ecc_point *pk);
  150. /**
  151. * vli_is_zero() - Determine is vli is zero
  152. *
  153. * @vli: vli to check.
  154. * @ndigits: length of the @vli
  155. */
  156. bool vli_is_zero(const u64 *vli, unsigned int ndigits);
  157. /**
  158. * vli_cmp() - compare left and right vlis
  159. *
  160. * @left: vli
  161. * @right: vli
  162. * @ndigits: length of both vlis
  163. *
  164. * Returns sign of @left - @right, i.e. -1 if @left < @right,
  165. * 0 if @left == @right, 1 if @left > @right.
  166. */
  167. int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits);
  168. /**
  169. * vli_sub() - Subtracts right from left
  170. *
  171. * @result: where to write result
  172. * @left: vli
  173. * @right vli
  174. * @ndigits: length of all vlis
  175. *
  176. * Note: can modify in-place.
  177. *
  178. * Return: carry bit.
  179. */
  180. u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
  181. unsigned int ndigits);
  182. /**
  183. * vli_from_be64() - Load vli from big-endian u64 array
  184. *
  185. * @dest: destination vli
  186. * @src: source array of u64 BE values
  187. * @ndigits: length of both vli and array
  188. */
  189. void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits);
  190. /**
  191. * vli_from_le64() - Load vli from little-endian u64 array
  192. *
  193. * @dest: destination vli
  194. * @src: source array of u64 LE values
  195. * @ndigits: length of both vli and array
  196. */
  197. void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits);
  198. /**
  199. * vli_mod_inv() - Modular inversion
  200. *
  201. * @result: where to write vli number
  202. * @input: vli value to operate on
  203. * @mod: modulus
  204. * @ndigits: length of all vlis
  205. */
  206. void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
  207. unsigned int ndigits);
  208. /**
  209. * vli_mod_mult_slow() - Modular multiplication
  210. *
  211. * @result: where to write result value
  212. * @left: vli number to multiply with @right
  213. * @right: vli number to multiply with @left
  214. * @mod: modulus
  215. * @ndigits: length of all vlis
  216. *
  217. * Note: Assumes that mod is big enough curve order.
  218. */
  219. void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,
  220. const u64 *mod, unsigned int ndigits);
  221. /**
  222. * vli_num_bits() - Counts the number of bits required for vli.
  223. *
  224. * @vli: vli to check.
  225. * @ndigits: Length of the @vli
  226. *
  227. * Return: The number of bits required to represent @vli.
  228. */
  229. unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits);
  230. /**
  231. * ecc_aloc_point() - Allocate ECC point.
  232. *
  233. * @ndigits: Length of vlis in u64 qwords.
  234. *
  235. * Return: Pointer to the allocated point or NULL if allocation failed.
  236. */
  237. struct ecc_point *ecc_alloc_point(unsigned int ndigits);
  238. /**
  239. * ecc_free_point() - Free ECC point.
  240. *
  241. * @p: The point to free.
  242. */
  243. void ecc_free_point(struct ecc_point *p);
  244. /**
  245. * ecc_point_is_zero() - Check if point is zero.
  246. *
  247. * @p: Point to check for zero.
  248. *
  249. * Return: true if point is the point at infinity, false otherwise.
  250. */
  251. bool ecc_point_is_zero(const struct ecc_point *point);
  252. /**
  253. * ecc_point_mult_shamir() - Add two points multiplied by scalars
  254. *
  255. * @result: resulting point
  256. * @x: scalar to multiply with @p
  257. * @p: point to multiply with @x
  258. * @y: scalar to multiply with @q
  259. * @q: point to multiply with @y
  260. * @curve: curve
  261. *
  262. * Returns result = x * p + x * q over the curve.
  263. * This works faster than two multiplications and addition.
  264. */
  265. void ecc_point_mult_shamir(const struct ecc_point *result,
  266. const u64 *x, const struct ecc_point *p,
  267. const u64 *y, const struct ecc_point *q,
  268. const struct ecc_curve *curve);
  269. #endif