ecc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. #include <linux/random.h>
  27. #include <linux/slab.h>
  28. #include <linux/swab.h>
  29. #include <linux/fips.h>
  30. #include <crypto/ecdh.h>
  31. #include <crypto/rng.h>
  32. #include "ecc.h"
  33. #include "ecc_curve_defs.h"
  34. typedef struct {
  35. u64 m_low;
  36. u64 m_high;
  37. } uint128_t;
  38. static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
  39. {
  40. switch (curve_id) {
  41. /* In FIPS mode only allow P256 and higher */
  42. case ECC_CURVE_NIST_P192:
  43. return fips_enabled ? NULL : &nist_p192;
  44. case ECC_CURVE_NIST_P256:
  45. return &nist_p256;
  46. default:
  47. return NULL;
  48. }
  49. }
  50. static u64 *ecc_alloc_digits_space(unsigned int ndigits)
  51. {
  52. size_t len = ndigits * sizeof(u64);
  53. if (!len)
  54. return NULL;
  55. return kmalloc(len, GFP_KERNEL);
  56. }
  57. static void ecc_free_digits_space(u64 *space)
  58. {
  59. kzfree(space);
  60. }
  61. static struct ecc_point *ecc_alloc_point(unsigned int ndigits)
  62. {
  63. struct ecc_point *p = kmalloc(sizeof(*p), GFP_KERNEL);
  64. if (!p)
  65. return NULL;
  66. p->x = ecc_alloc_digits_space(ndigits);
  67. if (!p->x)
  68. goto err_alloc_x;
  69. p->y = ecc_alloc_digits_space(ndigits);
  70. if (!p->y)
  71. goto err_alloc_y;
  72. p->ndigits = ndigits;
  73. return p;
  74. err_alloc_y:
  75. ecc_free_digits_space(p->x);
  76. err_alloc_x:
  77. kfree(p);
  78. return NULL;
  79. }
  80. static void ecc_free_point(struct ecc_point *p)
  81. {
  82. if (!p)
  83. return;
  84. kzfree(p->x);
  85. kzfree(p->y);
  86. kzfree(p);
  87. }
  88. static void vli_clear(u64 *vli, unsigned int ndigits)
  89. {
  90. int i;
  91. for (i = 0; i < ndigits; i++)
  92. vli[i] = 0;
  93. }
  94. /* Returns true if vli == 0, false otherwise. */
  95. static bool vli_is_zero(const u64 *vli, unsigned int ndigits)
  96. {
  97. int i;
  98. for (i = 0; i < ndigits; i++) {
  99. if (vli[i])
  100. return false;
  101. }
  102. return true;
  103. }
  104. /* Returns nonzero if bit bit of vli is set. */
  105. static u64 vli_test_bit(const u64 *vli, unsigned int bit)
  106. {
  107. return (vli[bit / 64] & ((u64)1 << (bit % 64)));
  108. }
  109. /* Counts the number of 64-bit "digits" in vli. */
  110. static unsigned int vli_num_digits(const u64 *vli, unsigned int ndigits)
  111. {
  112. int i;
  113. /* Search from the end until we find a non-zero digit.
  114. * We do it in reverse because we expect that most digits will
  115. * be nonzero.
  116. */
  117. for (i = ndigits - 1; i >= 0 && vli[i] == 0; i--);
  118. return (i + 1);
  119. }
  120. /* Counts the number of bits required for vli. */
  121. static unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits)
  122. {
  123. unsigned int i, num_digits;
  124. u64 digit;
  125. num_digits = vli_num_digits(vli, ndigits);
  126. if (num_digits == 0)
  127. return 0;
  128. digit = vli[num_digits - 1];
  129. for (i = 0; digit; i++)
  130. digit >>= 1;
  131. return ((num_digits - 1) * 64 + i);
  132. }
  133. /* Sets dest = src. */
  134. static void vli_set(u64 *dest, const u64 *src, unsigned int ndigits)
  135. {
  136. int i;
  137. for (i = 0; i < ndigits; i++)
  138. dest[i] = src[i];
  139. }
  140. /* Returns sign of left - right. */
  141. static int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits)
  142. {
  143. int i;
  144. for (i = ndigits - 1; i >= 0; i--) {
  145. if (left[i] > right[i])
  146. return 1;
  147. else if (left[i] < right[i])
  148. return -1;
  149. }
  150. return 0;
  151. }
  152. /* Computes result = in << c, returning carry. Can modify in place
  153. * (if result == in). 0 < shift < 64.
  154. */
  155. static u64 vli_lshift(u64 *result, const u64 *in, unsigned int shift,
  156. unsigned int ndigits)
  157. {
  158. u64 carry = 0;
  159. int i;
  160. for (i = 0; i < ndigits; i++) {
  161. u64 temp = in[i];
  162. result[i] = (temp << shift) | carry;
  163. carry = temp >> (64 - shift);
  164. }
  165. return carry;
  166. }
  167. /* Computes vli = vli >> 1. */
  168. static void vli_rshift1(u64 *vli, unsigned int ndigits)
  169. {
  170. u64 *end = vli;
  171. u64 carry = 0;
  172. vli += ndigits;
  173. while (vli-- > end) {
  174. u64 temp = *vli;
  175. *vli = (temp >> 1) | carry;
  176. carry = temp << 63;
  177. }
  178. }
  179. /* Computes result = left + right, returning carry. Can modify in place. */
  180. static u64 vli_add(u64 *result, const u64 *left, const u64 *right,
  181. unsigned int ndigits)
  182. {
  183. u64 carry = 0;
  184. int i;
  185. for (i = 0; i < ndigits; i++) {
  186. u64 sum;
  187. sum = left[i] + right[i] + carry;
  188. if (sum != left[i])
  189. carry = (sum < left[i]);
  190. result[i] = sum;
  191. }
  192. return carry;
  193. }
  194. /* Computes result = left - right, returning borrow. Can modify in place. */
  195. static u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
  196. unsigned int ndigits)
  197. {
  198. u64 borrow = 0;
  199. int i;
  200. for (i = 0; i < ndigits; i++) {
  201. u64 diff;
  202. diff = left[i] - right[i] - borrow;
  203. if (diff != left[i])
  204. borrow = (diff > left[i]);
  205. result[i] = diff;
  206. }
  207. return borrow;
  208. }
  209. static uint128_t mul_64_64(u64 left, u64 right)
  210. {
  211. u64 a0 = left & 0xffffffffull;
  212. u64 a1 = left >> 32;
  213. u64 b0 = right & 0xffffffffull;
  214. u64 b1 = right >> 32;
  215. u64 m0 = a0 * b0;
  216. u64 m1 = a0 * b1;
  217. u64 m2 = a1 * b0;
  218. u64 m3 = a1 * b1;
  219. uint128_t result;
  220. m2 += (m0 >> 32);
  221. m2 += m1;
  222. /* Overflow */
  223. if (m2 < m1)
  224. m3 += 0x100000000ull;
  225. result.m_low = (m0 & 0xffffffffull) | (m2 << 32);
  226. result.m_high = m3 + (m2 >> 32);
  227. return result;
  228. }
  229. static uint128_t add_128_128(uint128_t a, uint128_t b)
  230. {
  231. uint128_t result;
  232. result.m_low = a.m_low + b.m_low;
  233. result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low);
  234. return result;
  235. }
  236. static void vli_mult(u64 *result, const u64 *left, const u64 *right,
  237. unsigned int ndigits)
  238. {
  239. uint128_t r01 = { 0, 0 };
  240. u64 r2 = 0;
  241. unsigned int i, k;
  242. /* Compute each digit of result in sequence, maintaining the
  243. * carries.
  244. */
  245. for (k = 0; k < ndigits * 2 - 1; k++) {
  246. unsigned int min;
  247. if (k < ndigits)
  248. min = 0;
  249. else
  250. min = (k + 1) - ndigits;
  251. for (i = min; i <= k && i < ndigits; i++) {
  252. uint128_t product;
  253. product = mul_64_64(left[i], right[k - i]);
  254. r01 = add_128_128(r01, product);
  255. r2 += (r01.m_high < product.m_high);
  256. }
  257. result[k] = r01.m_low;
  258. r01.m_low = r01.m_high;
  259. r01.m_high = r2;
  260. r2 = 0;
  261. }
  262. result[ndigits * 2 - 1] = r01.m_low;
  263. }
  264. static void vli_square(u64 *result, const u64 *left, unsigned int ndigits)
  265. {
  266. uint128_t r01 = { 0, 0 };
  267. u64 r2 = 0;
  268. int i, k;
  269. for (k = 0; k < ndigits * 2 - 1; k++) {
  270. unsigned int min;
  271. if (k < ndigits)
  272. min = 0;
  273. else
  274. min = (k + 1) - ndigits;
  275. for (i = min; i <= k && i <= k - i; i++) {
  276. uint128_t product;
  277. product = mul_64_64(left[i], left[k - i]);
  278. if (i < k - i) {
  279. r2 += product.m_high >> 63;
  280. product.m_high = (product.m_high << 1) |
  281. (product.m_low >> 63);
  282. product.m_low <<= 1;
  283. }
  284. r01 = add_128_128(r01, product);
  285. r2 += (r01.m_high < product.m_high);
  286. }
  287. result[k] = r01.m_low;
  288. r01.m_low = r01.m_high;
  289. r01.m_high = r2;
  290. r2 = 0;
  291. }
  292. result[ndigits * 2 - 1] = r01.m_low;
  293. }
  294. /* Computes result = (left + right) % mod.
  295. * Assumes that left < mod and right < mod, result != mod.
  296. */
  297. static void vli_mod_add(u64 *result, const u64 *left, const u64 *right,
  298. const u64 *mod, unsigned int ndigits)
  299. {
  300. u64 carry;
  301. carry = vli_add(result, left, right, ndigits);
  302. /* result > mod (result = mod + remainder), so subtract mod to
  303. * get remainder.
  304. */
  305. if (carry || vli_cmp(result, mod, ndigits) >= 0)
  306. vli_sub(result, result, mod, ndigits);
  307. }
  308. /* Computes result = (left - right) % mod.
  309. * Assumes that left < mod and right < mod, result != mod.
  310. */
  311. static void vli_mod_sub(u64 *result, const u64 *left, const u64 *right,
  312. const u64 *mod, unsigned int ndigits)
  313. {
  314. u64 borrow = vli_sub(result, left, right, ndigits);
  315. /* In this case, p_result == -diff == (max int) - diff.
  316. * Since -x % d == d - x, we can get the correct result from
  317. * result + mod (with overflow).
  318. */
  319. if (borrow)
  320. vli_add(result, result, mod, ndigits);
  321. }
  322. /* Computes p_result = p_product % curve_p.
  323. * See algorithm 5 and 6 from
  324. * http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf
  325. */
  326. static void vli_mmod_fast_192(u64 *result, const u64 *product,
  327. const u64 *curve_prime, u64 *tmp)
  328. {
  329. const unsigned int ndigits = 3;
  330. int carry;
  331. vli_set(result, product, ndigits);
  332. vli_set(tmp, &product[3], ndigits);
  333. carry = vli_add(result, result, tmp, ndigits);
  334. tmp[0] = 0;
  335. tmp[1] = product[3];
  336. tmp[2] = product[4];
  337. carry += vli_add(result, result, tmp, ndigits);
  338. tmp[0] = tmp[1] = product[5];
  339. tmp[2] = 0;
  340. carry += vli_add(result, result, tmp, ndigits);
  341. while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
  342. carry -= vli_sub(result, result, curve_prime, ndigits);
  343. }
  344. /* Computes result = product % curve_prime
  345. * from http://www.nsa.gov/ia/_files/nist-routines.pdf
  346. */
  347. static void vli_mmod_fast_256(u64 *result, const u64 *product,
  348. const u64 *curve_prime, u64 *tmp)
  349. {
  350. int carry;
  351. const unsigned int ndigits = 4;
  352. /* t */
  353. vli_set(result, product, ndigits);
  354. /* s1 */
  355. tmp[0] = 0;
  356. tmp[1] = product[5] & 0xffffffff00000000ull;
  357. tmp[2] = product[6];
  358. tmp[3] = product[7];
  359. carry = vli_lshift(tmp, tmp, 1, ndigits);
  360. carry += vli_add(result, result, tmp, ndigits);
  361. /* s2 */
  362. tmp[1] = product[6] << 32;
  363. tmp[2] = (product[6] >> 32) | (product[7] << 32);
  364. tmp[3] = product[7] >> 32;
  365. carry += vli_lshift(tmp, tmp, 1, ndigits);
  366. carry += vli_add(result, result, tmp, ndigits);
  367. /* s3 */
  368. tmp[0] = product[4];
  369. tmp[1] = product[5] & 0xffffffff;
  370. tmp[2] = 0;
  371. tmp[3] = product[7];
  372. carry += vli_add(result, result, tmp, ndigits);
  373. /* s4 */
  374. tmp[0] = (product[4] >> 32) | (product[5] << 32);
  375. tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
  376. tmp[2] = product[7];
  377. tmp[3] = (product[6] >> 32) | (product[4] << 32);
  378. carry += vli_add(result, result, tmp, ndigits);
  379. /* d1 */
  380. tmp[0] = (product[5] >> 32) | (product[6] << 32);
  381. tmp[1] = (product[6] >> 32);
  382. tmp[2] = 0;
  383. tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
  384. carry -= vli_sub(result, result, tmp, ndigits);
  385. /* d2 */
  386. tmp[0] = product[6];
  387. tmp[1] = product[7];
  388. tmp[2] = 0;
  389. tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
  390. carry -= vli_sub(result, result, tmp, ndigits);
  391. /* d3 */
  392. tmp[0] = (product[6] >> 32) | (product[7] << 32);
  393. tmp[1] = (product[7] >> 32) | (product[4] << 32);
  394. tmp[2] = (product[4] >> 32) | (product[5] << 32);
  395. tmp[3] = (product[6] << 32);
  396. carry -= vli_sub(result, result, tmp, ndigits);
  397. /* d4 */
  398. tmp[0] = product[7];
  399. tmp[1] = product[4] & 0xffffffff00000000ull;
  400. tmp[2] = product[5];
  401. tmp[3] = product[6] & 0xffffffff00000000ull;
  402. carry -= vli_sub(result, result, tmp, ndigits);
  403. if (carry < 0) {
  404. do {
  405. carry += vli_add(result, result, curve_prime, ndigits);
  406. } while (carry < 0);
  407. } else {
  408. while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
  409. carry -= vli_sub(result, result, curve_prime, ndigits);
  410. }
  411. }
  412. /* Computes result = product % curve_prime
  413. * from http://www.nsa.gov/ia/_files/nist-routines.pdf
  414. */
  415. static bool vli_mmod_fast(u64 *result, u64 *product,
  416. const u64 *curve_prime, unsigned int ndigits)
  417. {
  418. u64 tmp[2 * ECC_MAX_DIGITS];
  419. switch (ndigits) {
  420. case 3:
  421. vli_mmod_fast_192(result, product, curve_prime, tmp);
  422. break;
  423. case 4:
  424. vli_mmod_fast_256(result, product, curve_prime, tmp);
  425. break;
  426. default:
  427. pr_err("unsupports digits size!\n");
  428. return false;
  429. }
  430. return true;
  431. }
  432. /* Computes result = (left * right) % curve_prime. */
  433. static void vli_mod_mult_fast(u64 *result, const u64 *left, const u64 *right,
  434. const u64 *curve_prime, unsigned int ndigits)
  435. {
  436. u64 product[2 * ECC_MAX_DIGITS];
  437. vli_mult(product, left, right, ndigits);
  438. vli_mmod_fast(result, product, curve_prime, ndigits);
  439. }
  440. /* Computes result = left^2 % curve_prime. */
  441. static void vli_mod_square_fast(u64 *result, const u64 *left,
  442. const u64 *curve_prime, unsigned int ndigits)
  443. {
  444. u64 product[2 * ECC_MAX_DIGITS];
  445. vli_square(product, left, ndigits);
  446. vli_mmod_fast(result, product, curve_prime, ndigits);
  447. }
  448. #define EVEN(vli) (!(vli[0] & 1))
  449. /* Computes result = (1 / p_input) % mod. All VLIs are the same size.
  450. * See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
  451. * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
  452. */
  453. static void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
  454. unsigned int ndigits)
  455. {
  456. u64 a[ECC_MAX_DIGITS], b[ECC_MAX_DIGITS];
  457. u64 u[ECC_MAX_DIGITS], v[ECC_MAX_DIGITS];
  458. u64 carry;
  459. int cmp_result;
  460. if (vli_is_zero(input, ndigits)) {
  461. vli_clear(result, ndigits);
  462. return;
  463. }
  464. vli_set(a, input, ndigits);
  465. vli_set(b, mod, ndigits);
  466. vli_clear(u, ndigits);
  467. u[0] = 1;
  468. vli_clear(v, ndigits);
  469. while ((cmp_result = vli_cmp(a, b, ndigits)) != 0) {
  470. carry = 0;
  471. if (EVEN(a)) {
  472. vli_rshift1(a, ndigits);
  473. if (!EVEN(u))
  474. carry = vli_add(u, u, mod, ndigits);
  475. vli_rshift1(u, ndigits);
  476. if (carry)
  477. u[ndigits - 1] |= 0x8000000000000000ull;
  478. } else if (EVEN(b)) {
  479. vli_rshift1(b, ndigits);
  480. if (!EVEN(v))
  481. carry = vli_add(v, v, mod, ndigits);
  482. vli_rshift1(v, ndigits);
  483. if (carry)
  484. v[ndigits - 1] |= 0x8000000000000000ull;
  485. } else if (cmp_result > 0) {
  486. vli_sub(a, a, b, ndigits);
  487. vli_rshift1(a, ndigits);
  488. if (vli_cmp(u, v, ndigits) < 0)
  489. vli_add(u, u, mod, ndigits);
  490. vli_sub(u, u, v, ndigits);
  491. if (!EVEN(u))
  492. carry = vli_add(u, u, mod, ndigits);
  493. vli_rshift1(u, ndigits);
  494. if (carry)
  495. u[ndigits - 1] |= 0x8000000000000000ull;
  496. } else {
  497. vli_sub(b, b, a, ndigits);
  498. vli_rshift1(b, ndigits);
  499. if (vli_cmp(v, u, ndigits) < 0)
  500. vli_add(v, v, mod, ndigits);
  501. vli_sub(v, v, u, ndigits);
  502. if (!EVEN(v))
  503. carry = vli_add(v, v, mod, ndigits);
  504. vli_rshift1(v, ndigits);
  505. if (carry)
  506. v[ndigits - 1] |= 0x8000000000000000ull;
  507. }
  508. }
  509. vli_set(result, u, ndigits);
  510. }
  511. /* ------ Point operations ------ */
  512. /* Returns true if p_point is the point at infinity, false otherwise. */
  513. static bool ecc_point_is_zero(const struct ecc_point *point)
  514. {
  515. return (vli_is_zero(point->x, point->ndigits) &&
  516. vli_is_zero(point->y, point->ndigits));
  517. }
  518. /* Point multiplication algorithm using Montgomery's ladder with co-Z
  519. * coordinates. From http://eprint.iacr.org/2011/338.pdf
  520. */
  521. /* Double in place */
  522. static void ecc_point_double_jacobian(u64 *x1, u64 *y1, u64 *z1,
  523. u64 *curve_prime, unsigned int ndigits)
  524. {
  525. /* t1 = x, t2 = y, t3 = z */
  526. u64 t4[ECC_MAX_DIGITS];
  527. u64 t5[ECC_MAX_DIGITS];
  528. if (vli_is_zero(z1, ndigits))
  529. return;
  530. /* t4 = y1^2 */
  531. vli_mod_square_fast(t4, y1, curve_prime, ndigits);
  532. /* t5 = x1*y1^2 = A */
  533. vli_mod_mult_fast(t5, x1, t4, curve_prime, ndigits);
  534. /* t4 = y1^4 */
  535. vli_mod_square_fast(t4, t4, curve_prime, ndigits);
  536. /* t2 = y1*z1 = z3 */
  537. vli_mod_mult_fast(y1, y1, z1, curve_prime, ndigits);
  538. /* t3 = z1^2 */
  539. vli_mod_square_fast(z1, z1, curve_prime, ndigits);
  540. /* t1 = x1 + z1^2 */
  541. vli_mod_add(x1, x1, z1, curve_prime, ndigits);
  542. /* t3 = 2*z1^2 */
  543. vli_mod_add(z1, z1, z1, curve_prime, ndigits);
  544. /* t3 = x1 - z1^2 */
  545. vli_mod_sub(z1, x1, z1, curve_prime, ndigits);
  546. /* t1 = x1^2 - z1^4 */
  547. vli_mod_mult_fast(x1, x1, z1, curve_prime, ndigits);
  548. /* t3 = 2*(x1^2 - z1^4) */
  549. vli_mod_add(z1, x1, x1, curve_prime, ndigits);
  550. /* t1 = 3*(x1^2 - z1^4) */
  551. vli_mod_add(x1, x1, z1, curve_prime, ndigits);
  552. if (vli_test_bit(x1, 0)) {
  553. u64 carry = vli_add(x1, x1, curve_prime, ndigits);
  554. vli_rshift1(x1, ndigits);
  555. x1[ndigits - 1] |= carry << 63;
  556. } else {
  557. vli_rshift1(x1, ndigits);
  558. }
  559. /* t1 = 3/2*(x1^2 - z1^4) = B */
  560. /* t3 = B^2 */
  561. vli_mod_square_fast(z1, x1, curve_prime, ndigits);
  562. /* t3 = B^2 - A */
  563. vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
  564. /* t3 = B^2 - 2A = x3 */
  565. vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
  566. /* t5 = A - x3 */
  567. vli_mod_sub(t5, t5, z1, curve_prime, ndigits);
  568. /* t1 = B * (A - x3) */
  569. vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
  570. /* t4 = B * (A - x3) - y1^4 = y3 */
  571. vli_mod_sub(t4, x1, t4, curve_prime, ndigits);
  572. vli_set(x1, z1, ndigits);
  573. vli_set(z1, y1, ndigits);
  574. vli_set(y1, t4, ndigits);
  575. }
  576. /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
  577. static void apply_z(u64 *x1, u64 *y1, u64 *z, u64 *curve_prime,
  578. unsigned int ndigits)
  579. {
  580. u64 t1[ECC_MAX_DIGITS];
  581. vli_mod_square_fast(t1, z, curve_prime, ndigits); /* z^2 */
  582. vli_mod_mult_fast(x1, x1, t1, curve_prime, ndigits); /* x1 * z^2 */
  583. vli_mod_mult_fast(t1, t1, z, curve_prime, ndigits); /* z^3 */
  584. vli_mod_mult_fast(y1, y1, t1, curve_prime, ndigits); /* y1 * z^3 */
  585. }
  586. /* P = (x1, y1) => 2P, (x2, y2) => P' */
  587. static void xycz_initial_double(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
  588. u64 *p_initial_z, u64 *curve_prime,
  589. unsigned int ndigits)
  590. {
  591. u64 z[ECC_MAX_DIGITS];
  592. vli_set(x2, x1, ndigits);
  593. vli_set(y2, y1, ndigits);
  594. vli_clear(z, ndigits);
  595. z[0] = 1;
  596. if (p_initial_z)
  597. vli_set(z, p_initial_z, ndigits);
  598. apply_z(x1, y1, z, curve_prime, ndigits);
  599. ecc_point_double_jacobian(x1, y1, z, curve_prime, ndigits);
  600. apply_z(x2, y2, z, curve_prime, ndigits);
  601. }
  602. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  603. * Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
  604. * or P => P', Q => P + Q
  605. */
  606. static void xycz_add(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
  607. unsigned int ndigits)
  608. {
  609. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  610. u64 t5[ECC_MAX_DIGITS];
  611. /* t5 = x2 - x1 */
  612. vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
  613. /* t5 = (x2 - x1)^2 = A */
  614. vli_mod_square_fast(t5, t5, curve_prime, ndigits);
  615. /* t1 = x1*A = B */
  616. vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
  617. /* t3 = x2*A = C */
  618. vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
  619. /* t4 = y2 - y1 */
  620. vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
  621. /* t5 = (y2 - y1)^2 = D */
  622. vli_mod_square_fast(t5, y2, curve_prime, ndigits);
  623. /* t5 = D - B */
  624. vli_mod_sub(t5, t5, x1, curve_prime, ndigits);
  625. /* t5 = D - B - C = x3 */
  626. vli_mod_sub(t5, t5, x2, curve_prime, ndigits);
  627. /* t3 = C - B */
  628. vli_mod_sub(x2, x2, x1, curve_prime, ndigits);
  629. /* t2 = y1*(C - B) */
  630. vli_mod_mult_fast(y1, y1, x2, curve_prime, ndigits);
  631. /* t3 = B - x3 */
  632. vli_mod_sub(x2, x1, t5, curve_prime, ndigits);
  633. /* t4 = (y2 - y1)*(B - x3) */
  634. vli_mod_mult_fast(y2, y2, x2, curve_prime, ndigits);
  635. /* t4 = y3 */
  636. vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
  637. vli_set(x2, t5, ndigits);
  638. }
  639. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  640. * Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
  641. * or P => P - Q, Q => P + Q
  642. */
  643. static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
  644. unsigned int ndigits)
  645. {
  646. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  647. u64 t5[ECC_MAX_DIGITS];
  648. u64 t6[ECC_MAX_DIGITS];
  649. u64 t7[ECC_MAX_DIGITS];
  650. /* t5 = x2 - x1 */
  651. vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
  652. /* t5 = (x2 - x1)^2 = A */
  653. vli_mod_square_fast(t5, t5, curve_prime, ndigits);
  654. /* t1 = x1*A = B */
  655. vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
  656. /* t3 = x2*A = C */
  657. vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
  658. /* t4 = y2 + y1 */
  659. vli_mod_add(t5, y2, y1, curve_prime, ndigits);
  660. /* t4 = y2 - y1 */
  661. vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
  662. /* t6 = C - B */
  663. vli_mod_sub(t6, x2, x1, curve_prime, ndigits);
  664. /* t2 = y1 * (C - B) */
  665. vli_mod_mult_fast(y1, y1, t6, curve_prime, ndigits);
  666. /* t6 = B + C */
  667. vli_mod_add(t6, x1, x2, curve_prime, ndigits);
  668. /* t3 = (y2 - y1)^2 */
  669. vli_mod_square_fast(x2, y2, curve_prime, ndigits);
  670. /* t3 = x3 */
  671. vli_mod_sub(x2, x2, t6, curve_prime, ndigits);
  672. /* t7 = B - x3 */
  673. vli_mod_sub(t7, x1, x2, curve_prime, ndigits);
  674. /* t4 = (y2 - y1)*(B - x3) */
  675. vli_mod_mult_fast(y2, y2, t7, curve_prime, ndigits);
  676. /* t4 = y3 */
  677. vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
  678. /* t7 = (y2 + y1)^2 = F */
  679. vli_mod_square_fast(t7, t5, curve_prime, ndigits);
  680. /* t7 = x3' */
  681. vli_mod_sub(t7, t7, t6, curve_prime, ndigits);
  682. /* t6 = x3' - B */
  683. vli_mod_sub(t6, t7, x1, curve_prime, ndigits);
  684. /* t6 = (y2 + y1)*(x3' - B) */
  685. vli_mod_mult_fast(t6, t6, t5, curve_prime, ndigits);
  686. /* t2 = y3' */
  687. vli_mod_sub(y1, t6, y1, curve_prime, ndigits);
  688. vli_set(x1, t7, ndigits);
  689. }
  690. static void ecc_point_mult(struct ecc_point *result,
  691. const struct ecc_point *point, const u64 *scalar,
  692. u64 *initial_z, const struct ecc_curve *curve,
  693. unsigned int ndigits)
  694. {
  695. /* R0 and R1 */
  696. u64 rx[2][ECC_MAX_DIGITS];
  697. u64 ry[2][ECC_MAX_DIGITS];
  698. u64 z[ECC_MAX_DIGITS];
  699. u64 sk[2][ECC_MAX_DIGITS];
  700. u64 *curve_prime = curve->p;
  701. int i, nb;
  702. int num_bits;
  703. int carry;
  704. carry = vli_add(sk[0], scalar, curve->n, ndigits);
  705. vli_add(sk[1], sk[0], curve->n, ndigits);
  706. scalar = sk[!carry];
  707. num_bits = sizeof(u64) * ndigits * 8 + 1;
  708. vli_set(rx[1], point->x, ndigits);
  709. vli_set(ry[1], point->y, ndigits);
  710. xycz_initial_double(rx[1], ry[1], rx[0], ry[0], initial_z, curve_prime,
  711. ndigits);
  712. for (i = num_bits - 2; i > 0; i--) {
  713. nb = !vli_test_bit(scalar, i);
  714. xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
  715. ndigits);
  716. xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime,
  717. ndigits);
  718. }
  719. nb = !vli_test_bit(scalar, 0);
  720. xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
  721. ndigits);
  722. /* Find final 1/Z value. */
  723. /* X1 - X0 */
  724. vli_mod_sub(z, rx[1], rx[0], curve_prime, ndigits);
  725. /* Yb * (X1 - X0) */
  726. vli_mod_mult_fast(z, z, ry[1 - nb], curve_prime, ndigits);
  727. /* xP * Yb * (X1 - X0) */
  728. vli_mod_mult_fast(z, z, point->x, curve_prime, ndigits);
  729. /* 1 / (xP * Yb * (X1 - X0)) */
  730. vli_mod_inv(z, z, curve_prime, point->ndigits);
  731. /* yP / (xP * Yb * (X1 - X0)) */
  732. vli_mod_mult_fast(z, z, point->y, curve_prime, ndigits);
  733. /* Xb * yP / (xP * Yb * (X1 - X0)) */
  734. vli_mod_mult_fast(z, z, rx[1 - nb], curve_prime, ndigits);
  735. /* End 1/Z calculation */
  736. xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime, ndigits);
  737. apply_z(rx[0], ry[0], z, curve_prime, ndigits);
  738. vli_set(result->x, rx[0], ndigits);
  739. vli_set(result->y, ry[0], ndigits);
  740. }
  741. static inline void ecc_swap_digits(const u64 *in, u64 *out,
  742. unsigned int ndigits)
  743. {
  744. const __be64 *src = (__force __be64 *)in;
  745. int i;
  746. for (i = 0; i < ndigits; i++)
  747. out[i] = be64_to_cpu(src[ndigits - 1 - i]);
  748. }
  749. static int __ecc_is_key_valid(const struct ecc_curve *curve,
  750. const u64 *private_key, unsigned int ndigits)
  751. {
  752. u64 one[ECC_MAX_DIGITS] = { 1, };
  753. u64 res[ECC_MAX_DIGITS];
  754. if (!private_key)
  755. return -EINVAL;
  756. if (curve->g.ndigits != ndigits)
  757. return -EINVAL;
  758. /* Make sure the private key is in the range [2, n-3]. */
  759. if (vli_cmp(one, private_key, ndigits) != -1)
  760. return -EINVAL;
  761. vli_sub(res, curve->n, one, ndigits);
  762. vli_sub(res, res, one, ndigits);
  763. if (vli_cmp(res, private_key, ndigits) != 1)
  764. return -EINVAL;
  765. return 0;
  766. }
  767. int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
  768. const u64 *private_key, unsigned int private_key_len)
  769. {
  770. int nbytes;
  771. const struct ecc_curve *curve = ecc_get_curve(curve_id);
  772. nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
  773. if (private_key_len != nbytes)
  774. return -EINVAL;
  775. return __ecc_is_key_valid(curve, private_key, ndigits);
  776. }
  777. /*
  778. * ECC private keys are generated using the method of extra random bits,
  779. * equivalent to that described in FIPS 186-4, Appendix B.4.1.
  780. *
  781. * d = (c mod(n–1)) + 1 where c is a string of random bits, 64 bits longer
  782. * than requested
  783. * 0 <= c mod(n-1) <= n-2 and implies that
  784. * 1 <= d <= n-1
  785. *
  786. * This method generates a private key uniformly distributed in the range
  787. * [1, n-1].
  788. */
  789. int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey)
  790. {
  791. const struct ecc_curve *curve = ecc_get_curve(curve_id);
  792. u64 priv[ECC_MAX_DIGITS];
  793. unsigned int nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
  794. unsigned int nbits = vli_num_bits(curve->n, ndigits);
  795. int err;
  796. /* Check that N is included in Table 1 of FIPS 186-4, section 6.1.1 */
  797. if (nbits < 160 || ndigits > ARRAY_SIZE(priv))
  798. return -EINVAL;
  799. /*
  800. * FIPS 186-4 recommends that the private key should be obtained from a
  801. * RBG with a security strength equal to or greater than the security
  802. * strength associated with N.
  803. *
  804. * The maximum security strength identified by NIST SP800-57pt1r4 for
  805. * ECC is 256 (N >= 512).
  806. *
  807. * This condition is met by the default RNG because it selects a favored
  808. * DRBG with a security strength of 256.
  809. */
  810. if (crypto_get_default_rng())
  811. return -EFAULT;
  812. err = crypto_rng_get_bytes(crypto_default_rng, (u8 *)priv, nbytes);
  813. crypto_put_default_rng();
  814. if (err)
  815. return err;
  816. /* Make sure the private key is in the valid range. */
  817. if (__ecc_is_key_valid(curve, priv, ndigits))
  818. return -EINVAL;
  819. ecc_swap_digits(priv, privkey, ndigits);
  820. return 0;
  821. }
  822. int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
  823. const u64 *private_key, u64 *public_key)
  824. {
  825. int ret = 0;
  826. struct ecc_point *pk;
  827. u64 priv[ECC_MAX_DIGITS];
  828. const struct ecc_curve *curve = ecc_get_curve(curve_id);
  829. if (!private_key || !curve || ndigits > ARRAY_SIZE(priv)) {
  830. ret = -EINVAL;
  831. goto out;
  832. }
  833. ecc_swap_digits(private_key, priv, ndigits);
  834. pk = ecc_alloc_point(ndigits);
  835. if (!pk) {
  836. ret = -ENOMEM;
  837. goto out;
  838. }
  839. ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits);
  840. if (ecc_point_is_zero(pk)) {
  841. ret = -EAGAIN;
  842. goto err_free_point;
  843. }
  844. ecc_swap_digits(pk->x, public_key, ndigits);
  845. ecc_swap_digits(pk->y, &public_key[ndigits], ndigits);
  846. err_free_point:
  847. ecc_free_point(pk);
  848. out:
  849. return ret;
  850. }
  851. /* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
  852. static int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
  853. struct ecc_point *pk)
  854. {
  855. u64 yy[ECC_MAX_DIGITS], xxx[ECC_MAX_DIGITS], w[ECC_MAX_DIGITS];
  856. /* Check 1: Verify key is not the zero point. */
  857. if (ecc_point_is_zero(pk))
  858. return -EINVAL;
  859. /* Check 2: Verify key is in the range [1, p-1]. */
  860. if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1)
  861. return -EINVAL;
  862. if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1)
  863. return -EINVAL;
  864. /* Check 3: Verify that y^2 == (x^3 + a·x + b) mod p */
  865. vli_mod_square_fast(yy, pk->y, curve->p, pk->ndigits); /* y^2 */
  866. vli_mod_square_fast(xxx, pk->x, curve->p, pk->ndigits); /* x^2 */
  867. vli_mod_mult_fast(xxx, xxx, pk->x, curve->p, pk->ndigits); /* x^3 */
  868. vli_mod_mult_fast(w, curve->a, pk->x, curve->p, pk->ndigits); /* a·x */
  869. vli_mod_add(w, w, curve->b, curve->p, pk->ndigits); /* a·x + b */
  870. vli_mod_add(w, w, xxx, curve->p, pk->ndigits); /* x^3 + a·x + b */
  871. if (vli_cmp(yy, w, pk->ndigits) != 0) /* Equation */
  872. return -EINVAL;
  873. return 0;
  874. }
  875. int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
  876. const u64 *private_key, const u64 *public_key,
  877. u64 *secret)
  878. {
  879. int ret = 0;
  880. struct ecc_point *product, *pk;
  881. u64 priv[ECC_MAX_DIGITS];
  882. u64 rand_z[ECC_MAX_DIGITS];
  883. unsigned int nbytes;
  884. const struct ecc_curve *curve = ecc_get_curve(curve_id);
  885. if (!private_key || !public_key || !curve ||
  886. ndigits > ARRAY_SIZE(priv) || ndigits > ARRAY_SIZE(rand_z)) {
  887. ret = -EINVAL;
  888. goto out;
  889. }
  890. nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
  891. get_random_bytes(rand_z, nbytes);
  892. pk = ecc_alloc_point(ndigits);
  893. if (!pk) {
  894. ret = -ENOMEM;
  895. goto out;
  896. }
  897. ecc_swap_digits(public_key, pk->x, ndigits);
  898. ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
  899. ret = ecc_is_pubkey_valid_partial(curve, pk);
  900. if (ret)
  901. goto err_alloc_product;
  902. ecc_swap_digits(private_key, priv, ndigits);
  903. product = ecc_alloc_point(ndigits);
  904. if (!product) {
  905. ret = -ENOMEM;
  906. goto err_alloc_product;
  907. }
  908. ecc_point_mult(product, pk, priv, rand_z, curve, ndigits);
  909. ecc_swap_digits(product->x, secret, ndigits);
  910. if (ecc_point_is_zero(product))
  911. ret = -EFAULT;
  912. ecc_free_point(product);
  913. err_alloc_product:
  914. ecc_free_point(pk);
  915. out:
  916. return ret;
  917. }