ecc.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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, u64 *curve_prime,
  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. int i, nb;
  700. int num_bits = vli_num_bits(scalar, ndigits);
  701. vli_set(rx[1], point->x, ndigits);
  702. vli_set(ry[1], point->y, ndigits);
  703. xycz_initial_double(rx[1], ry[1], rx[0], ry[0], initial_z, curve_prime,
  704. ndigits);
  705. for (i = num_bits - 2; i > 0; i--) {
  706. nb = !vli_test_bit(scalar, i);
  707. xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
  708. ndigits);
  709. xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime,
  710. ndigits);
  711. }
  712. nb = !vli_test_bit(scalar, 0);
  713. xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
  714. ndigits);
  715. /* Find final 1/Z value. */
  716. /* X1 - X0 */
  717. vli_mod_sub(z, rx[1], rx[0], curve_prime, ndigits);
  718. /* Yb * (X1 - X0) */
  719. vli_mod_mult_fast(z, z, ry[1 - nb], curve_prime, ndigits);
  720. /* xP * Yb * (X1 - X0) */
  721. vli_mod_mult_fast(z, z, point->x, curve_prime, ndigits);
  722. /* 1 / (xP * Yb * (X1 - X0)) */
  723. vli_mod_inv(z, z, curve_prime, point->ndigits);
  724. /* yP / (xP * Yb * (X1 - X0)) */
  725. vli_mod_mult_fast(z, z, point->y, curve_prime, ndigits);
  726. /* Xb * yP / (xP * Yb * (X1 - X0)) */
  727. vli_mod_mult_fast(z, z, rx[1 - nb], curve_prime, ndigits);
  728. /* End 1/Z calculation */
  729. xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime, ndigits);
  730. apply_z(rx[0], ry[0], z, curve_prime, ndigits);
  731. vli_set(result->x, rx[0], ndigits);
  732. vli_set(result->y, ry[0], ndigits);
  733. }
  734. static inline void ecc_swap_digits(const u64 *in, u64 *out,
  735. unsigned int ndigits)
  736. {
  737. int i;
  738. for (i = 0; i < ndigits; i++)
  739. out[i] = __swab64(in[ndigits - 1 - i]);
  740. }
  741. int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
  742. const u64 *private_key, unsigned int private_key_len)
  743. {
  744. int nbytes;
  745. const struct ecc_curve *curve = ecc_get_curve(curve_id);
  746. if (!private_key)
  747. return -EINVAL;
  748. nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
  749. if (private_key_len != nbytes)
  750. return -EINVAL;
  751. if (vli_is_zero(private_key, ndigits))
  752. return -EINVAL;
  753. /* Make sure the private key is in the range [1, n-1]. */
  754. if (vli_cmp(curve->n, private_key, ndigits) != 1)
  755. return -EINVAL;
  756. return 0;
  757. }
  758. /*
  759. * ECC private keys are generated using the method of extra random bits,
  760. * equivalent to that described in FIPS 186-4, Appendix B.4.1.
  761. *
  762. * d = (c mod(n–1)) + 1 where c is a string of random bits, 64 bits longer
  763. * than requested
  764. * 0 <= c mod(n-1) <= n-2 and implies that
  765. * 1 <= d <= n-1
  766. *
  767. * This method generates a private key uniformly distributed in the range
  768. * [1, n-1].
  769. */
  770. int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey)
  771. {
  772. const struct ecc_curve *curve = ecc_get_curve(curve_id);
  773. u64 priv[ECC_MAX_DIGITS];
  774. unsigned int nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
  775. unsigned int nbits = vli_num_bits(curve->n, ndigits);
  776. int err;
  777. /* Check that N is included in Table 1 of FIPS 186-4, section 6.1.1 */
  778. if (nbits < 160 || ndigits > ARRAY_SIZE(priv))
  779. return -EINVAL;
  780. /*
  781. * FIPS 186-4 recommends that the private key should be obtained from a
  782. * RBG with a security strength equal to or greater than the security
  783. * strength associated with N.
  784. *
  785. * The maximum security strength identified by NIST SP800-57pt1r4 for
  786. * ECC is 256 (N >= 512).
  787. *
  788. * This condition is met by the default RNG because it selects a favored
  789. * DRBG with a security strength of 256.
  790. */
  791. if (crypto_get_default_rng())
  792. return -EFAULT;
  793. err = crypto_rng_get_bytes(crypto_default_rng, (u8 *)priv, nbytes);
  794. crypto_put_default_rng();
  795. if (err)
  796. return err;
  797. if (vli_is_zero(priv, ndigits))
  798. return -EINVAL;
  799. /* Make sure the private key is in the range [1, n-1]. */
  800. if (vli_cmp(curve->n, priv, ndigits) != 1)
  801. return -EINVAL;
  802. ecc_swap_digits(priv, privkey, ndigits);
  803. return 0;
  804. }
  805. int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
  806. const u64 *private_key, u64 *public_key)
  807. {
  808. int ret = 0;
  809. struct ecc_point *pk;
  810. u64 priv[ECC_MAX_DIGITS];
  811. const struct ecc_curve *curve = ecc_get_curve(curve_id);
  812. if (!private_key || !curve || ndigits > ARRAY_SIZE(priv)) {
  813. ret = -EINVAL;
  814. goto out;
  815. }
  816. ecc_swap_digits(private_key, priv, ndigits);
  817. pk = ecc_alloc_point(ndigits);
  818. if (!pk) {
  819. ret = -ENOMEM;
  820. goto out;
  821. }
  822. ecc_point_mult(pk, &curve->g, priv, NULL, curve->p, ndigits);
  823. if (ecc_point_is_zero(pk)) {
  824. ret = -EAGAIN;
  825. goto err_free_point;
  826. }
  827. ecc_swap_digits(pk->x, public_key, ndigits);
  828. ecc_swap_digits(pk->y, &public_key[ndigits], ndigits);
  829. err_free_point:
  830. ecc_free_point(pk);
  831. out:
  832. return ret;
  833. }
  834. /* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
  835. static int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
  836. struct ecc_point *pk)
  837. {
  838. u64 yy[ECC_MAX_DIGITS], xxx[ECC_MAX_DIGITS], w[ECC_MAX_DIGITS];
  839. /* Check 1: Verify key is not the zero point. */
  840. if (ecc_point_is_zero(pk))
  841. return -EINVAL;
  842. /* Check 2: Verify key is in the range [1, p-1]. */
  843. if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1)
  844. return -EINVAL;
  845. if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1)
  846. return -EINVAL;
  847. /* Check 3: Verify that y^2 == (x^3 + a·x + b) mod p */
  848. vli_mod_square_fast(yy, pk->y, curve->p, pk->ndigits); /* y^2 */
  849. vli_mod_square_fast(xxx, pk->x, curve->p, pk->ndigits); /* x^2 */
  850. vli_mod_mult_fast(xxx, xxx, pk->x, curve->p, pk->ndigits); /* x^3 */
  851. vli_mod_mult_fast(w, curve->a, pk->x, curve->p, pk->ndigits); /* a·x */
  852. vli_mod_add(w, w, curve->b, curve->p, pk->ndigits); /* a·x + b */
  853. vli_mod_add(w, w, xxx, curve->p, pk->ndigits); /* x^3 + a·x + b */
  854. if (vli_cmp(yy, w, pk->ndigits) != 0) /* Equation */
  855. return -EINVAL;
  856. return 0;
  857. }
  858. int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
  859. const u64 *private_key, const u64 *public_key,
  860. u64 *secret)
  861. {
  862. int ret = 0;
  863. struct ecc_point *product, *pk;
  864. u64 priv[ECC_MAX_DIGITS];
  865. u64 rand_z[ECC_MAX_DIGITS];
  866. unsigned int nbytes;
  867. const struct ecc_curve *curve = ecc_get_curve(curve_id);
  868. if (!private_key || !public_key || !curve ||
  869. ndigits > ARRAY_SIZE(priv) || ndigits > ARRAY_SIZE(rand_z)) {
  870. ret = -EINVAL;
  871. goto out;
  872. }
  873. nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
  874. get_random_bytes(rand_z, nbytes);
  875. pk = ecc_alloc_point(ndigits);
  876. if (!pk) {
  877. ret = -ENOMEM;
  878. goto out;
  879. }
  880. ecc_swap_digits(public_key, pk->x, ndigits);
  881. ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
  882. ret = ecc_is_pubkey_valid_partial(curve, pk);
  883. if (ret)
  884. goto err_alloc_product;
  885. ecc_swap_digits(private_key, priv, ndigits);
  886. product = ecc_alloc_point(ndigits);
  887. if (!product) {
  888. ret = -ENOMEM;
  889. goto err_alloc_product;
  890. }
  891. ecc_point_mult(product, pk, priv, rand_z, curve->p, ndigits);
  892. ecc_swap_digits(product->x, secret, ndigits);
  893. if (ecc_point_is_zero(product))
  894. ret = -EFAULT;
  895. ecc_free_point(product);
  896. err_alloc_product:
  897. ecc_free_point(pk);
  898. out:
  899. return ret;
  900. }