ecc.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  1. /*
  2. * Copyright (c) 2013, 2014 Kenneth MacKay. All rights reserved.
  3. * Copyright (c) 2019 Vitaly Chikunov <vt@altlinux.org>
  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/module.h>
  27. #include <linux/random.h>
  28. #include <linux/slab.h>
  29. #include <linux/swab.h>
  30. #include <linux/fips.h>
  31. #include <crypto/ecdh.h>
  32. #include <crypto/rng.h>
  33. #include <asm/unaligned.h>
  34. #include <linux/ratelimit.h>
  35. #include "ecc.h"
  36. #include "ecc_curve_defs.h"
  37. typedef struct {
  38. u64 m_low;
  39. u64 m_high;
  40. } uint128_t;
  41. static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
  42. {
  43. switch (curve_id) {
  44. /* In FIPS mode only allow P256 and higher */
  45. case ECC_CURVE_NIST_P192:
  46. return fips_enabled ? NULL : &nist_p192;
  47. case ECC_CURVE_NIST_P256:
  48. return &nist_p256;
  49. default:
  50. return NULL;
  51. }
  52. }
  53. static u64 *ecc_alloc_digits_space(unsigned int ndigits)
  54. {
  55. size_t len = ndigits * sizeof(u64);
  56. if (!len)
  57. return NULL;
  58. return kmalloc(len, GFP_KERNEL);
  59. }
  60. static void ecc_free_digits_space(u64 *space)
  61. {
  62. kfree_sensitive(space);
  63. }
  64. static struct ecc_point *ecc_alloc_point(unsigned int ndigits)
  65. {
  66. struct ecc_point *p = kmalloc(sizeof(*p), GFP_KERNEL);
  67. if (!p)
  68. return NULL;
  69. p->x = ecc_alloc_digits_space(ndigits);
  70. if (!p->x)
  71. goto err_alloc_x;
  72. p->y = ecc_alloc_digits_space(ndigits);
  73. if (!p->y)
  74. goto err_alloc_y;
  75. p->ndigits = ndigits;
  76. return p;
  77. err_alloc_y:
  78. ecc_free_digits_space(p->x);
  79. err_alloc_x:
  80. kfree(p);
  81. return NULL;
  82. }
  83. static void ecc_free_point(struct ecc_point *p)
  84. {
  85. if (!p)
  86. return;
  87. kfree_sensitive(p->x);
  88. kfree_sensitive(p->y);
  89. kfree_sensitive(p);
  90. }
  91. static void vli_clear(u64 *vli, unsigned int ndigits)
  92. {
  93. int i;
  94. for (i = 0; i < ndigits; i++)
  95. vli[i] = 0;
  96. }
  97. /* Returns true if vli == 0, false otherwise. */
  98. bool vli_is_zero(const u64 *vli, unsigned int ndigits)
  99. {
  100. int i;
  101. for (i = 0; i < ndigits; i++) {
  102. if (vli[i])
  103. return false;
  104. }
  105. return true;
  106. }
  107. EXPORT_SYMBOL(vli_is_zero);
  108. /* Returns nonzero if bit bit of vli is set. */
  109. static u64 vli_test_bit(const u64 *vli, unsigned int bit)
  110. {
  111. return (vli[bit / 64] & ((u64)1 << (bit % 64)));
  112. }
  113. static bool vli_is_negative(const u64 *vli, unsigned int ndigits)
  114. {
  115. return vli_test_bit(vli, ndigits * 64 - 1);
  116. }
  117. /* Counts the number of 64-bit "digits" in vli. */
  118. static unsigned int vli_num_digits(const u64 *vli, unsigned int ndigits)
  119. {
  120. int i;
  121. /* Search from the end until we find a non-zero digit.
  122. * We do it in reverse because we expect that most digits will
  123. * be nonzero.
  124. */
  125. for (i = ndigits - 1; i >= 0 && vli[i] == 0; i--);
  126. return (i + 1);
  127. }
  128. /* Counts the number of bits required for vli. */
  129. static unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits)
  130. {
  131. unsigned int i, num_digits;
  132. u64 digit;
  133. num_digits = vli_num_digits(vli, ndigits);
  134. if (num_digits == 0)
  135. return 0;
  136. digit = vli[num_digits - 1];
  137. for (i = 0; digit; i++)
  138. digit >>= 1;
  139. return ((num_digits - 1) * 64 + i);
  140. }
  141. /* Set dest from unaligned bit string src. */
  142. void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits)
  143. {
  144. int i;
  145. const u64 *from = src;
  146. for (i = 0; i < ndigits; i++)
  147. dest[i] = get_unaligned_be64(&from[ndigits - 1 - i]);
  148. }
  149. EXPORT_SYMBOL(vli_from_be64);
  150. void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits)
  151. {
  152. int i;
  153. const u64 *from = src;
  154. for (i = 0; i < ndigits; i++)
  155. dest[i] = get_unaligned_le64(&from[i]);
  156. }
  157. EXPORT_SYMBOL(vli_from_le64);
  158. /* Sets dest = src. */
  159. static void vli_set(u64 *dest, const u64 *src, unsigned int ndigits)
  160. {
  161. int i;
  162. for (i = 0; i < ndigits; i++)
  163. dest[i] = src[i];
  164. }
  165. /* Returns sign of left - right. */
  166. int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits)
  167. {
  168. int i;
  169. for (i = ndigits - 1; i >= 0; i--) {
  170. if (left[i] > right[i])
  171. return 1;
  172. else if (left[i] < right[i])
  173. return -1;
  174. }
  175. return 0;
  176. }
  177. EXPORT_SYMBOL(vli_cmp);
  178. /* Computes result = in << c, returning carry. Can modify in place
  179. * (if result == in). 0 < shift < 64.
  180. */
  181. static u64 vli_lshift(u64 *result, const u64 *in, unsigned int shift,
  182. unsigned int ndigits)
  183. {
  184. u64 carry = 0;
  185. int i;
  186. for (i = 0; i < ndigits; i++) {
  187. u64 temp = in[i];
  188. result[i] = (temp << shift) | carry;
  189. carry = temp >> (64 - shift);
  190. }
  191. return carry;
  192. }
  193. /* Computes vli = vli >> 1. */
  194. static void vli_rshift1(u64 *vli, unsigned int ndigits)
  195. {
  196. u64 *end = vli;
  197. u64 carry = 0;
  198. vli += ndigits;
  199. while (vli-- > end) {
  200. u64 temp = *vli;
  201. *vli = (temp >> 1) | carry;
  202. carry = temp << 63;
  203. }
  204. }
  205. /* Computes result = left + right, returning carry. Can modify in place. */
  206. static u64 vli_add(u64 *result, const u64 *left, const u64 *right,
  207. unsigned int ndigits)
  208. {
  209. u64 carry = 0;
  210. int i;
  211. for (i = 0; i < ndigits; i++) {
  212. u64 sum;
  213. sum = left[i] + right[i] + carry;
  214. if (sum != left[i])
  215. carry = (sum < left[i]);
  216. result[i] = sum;
  217. }
  218. return carry;
  219. }
  220. /* Computes result = left + right, returning carry. Can modify in place. */
  221. static u64 vli_uadd(u64 *result, const u64 *left, u64 right,
  222. unsigned int ndigits)
  223. {
  224. u64 carry = right;
  225. int i;
  226. for (i = 0; i < ndigits; i++) {
  227. u64 sum;
  228. sum = left[i] + carry;
  229. if (sum != left[i])
  230. carry = (sum < left[i]);
  231. else
  232. carry = !!carry;
  233. result[i] = sum;
  234. }
  235. return carry;
  236. }
  237. /* Computes result = left - right, returning borrow. Can modify in place. */
  238. u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
  239. unsigned int ndigits)
  240. {
  241. u64 borrow = 0;
  242. int i;
  243. for (i = 0; i < ndigits; i++) {
  244. u64 diff;
  245. diff = left[i] - right[i] - borrow;
  246. if (diff != left[i])
  247. borrow = (diff > left[i]);
  248. result[i] = diff;
  249. }
  250. return borrow;
  251. }
  252. EXPORT_SYMBOL(vli_sub);
  253. /* Computes result = left - right, returning borrow. Can modify in place. */
  254. static u64 vli_usub(u64 *result, const u64 *left, u64 right,
  255. unsigned int ndigits)
  256. {
  257. u64 borrow = right;
  258. int i;
  259. for (i = 0; i < ndigits; i++) {
  260. u64 diff;
  261. diff = left[i] - borrow;
  262. if (diff != left[i])
  263. borrow = (diff > left[i]);
  264. result[i] = diff;
  265. }
  266. return borrow;
  267. }
  268. static uint128_t mul_64_64(u64 left, u64 right)
  269. {
  270. uint128_t result;
  271. #if defined(CONFIG_ARCH_SUPPORTS_INT128)
  272. unsigned __int128 m = (unsigned __int128)left * right;
  273. result.m_low = m;
  274. result.m_high = m >> 64;
  275. #else
  276. u64 a0 = left & 0xffffffffull;
  277. u64 a1 = left >> 32;
  278. u64 b0 = right & 0xffffffffull;
  279. u64 b1 = right >> 32;
  280. u64 m0 = a0 * b0;
  281. u64 m1 = a0 * b1;
  282. u64 m2 = a1 * b0;
  283. u64 m3 = a1 * b1;
  284. m2 += (m0 >> 32);
  285. m2 += m1;
  286. /* Overflow */
  287. if (m2 < m1)
  288. m3 += 0x100000000ull;
  289. result.m_low = (m0 & 0xffffffffull) | (m2 << 32);
  290. result.m_high = m3 + (m2 >> 32);
  291. #endif
  292. return result;
  293. }
  294. static uint128_t add_128_128(uint128_t a, uint128_t b)
  295. {
  296. uint128_t result;
  297. result.m_low = a.m_low + b.m_low;
  298. result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low);
  299. return result;
  300. }
  301. static void vli_mult(u64 *result, const u64 *left, const u64 *right,
  302. unsigned int ndigits)
  303. {
  304. uint128_t r01 = { 0, 0 };
  305. u64 r2 = 0;
  306. unsigned int i, k;
  307. /* Compute each digit of result in sequence, maintaining the
  308. * carries.
  309. */
  310. for (k = 0; k < ndigits * 2 - 1; k++) {
  311. unsigned int min;
  312. if (k < ndigits)
  313. min = 0;
  314. else
  315. min = (k + 1) - ndigits;
  316. for (i = min; i <= k && i < ndigits; i++) {
  317. uint128_t product;
  318. product = mul_64_64(left[i], right[k - i]);
  319. r01 = add_128_128(r01, product);
  320. r2 += (r01.m_high < product.m_high);
  321. }
  322. result[k] = r01.m_low;
  323. r01.m_low = r01.m_high;
  324. r01.m_high = r2;
  325. r2 = 0;
  326. }
  327. result[ndigits * 2 - 1] = r01.m_low;
  328. }
  329. /* Compute product = left * right, for a small right value. */
  330. static void vli_umult(u64 *result, const u64 *left, u32 right,
  331. unsigned int ndigits)
  332. {
  333. uint128_t r01 = { 0 };
  334. unsigned int k;
  335. for (k = 0; k < ndigits; k++) {
  336. uint128_t product;
  337. product = mul_64_64(left[k], right);
  338. r01 = add_128_128(r01, product);
  339. /* no carry */
  340. result[k] = r01.m_low;
  341. r01.m_low = r01.m_high;
  342. r01.m_high = 0;
  343. }
  344. result[k] = r01.m_low;
  345. for (++k; k < ndigits * 2; k++)
  346. result[k] = 0;
  347. }
  348. static void vli_square(u64 *result, const u64 *left, unsigned int ndigits)
  349. {
  350. uint128_t r01 = { 0, 0 };
  351. u64 r2 = 0;
  352. int i, k;
  353. for (k = 0; k < ndigits * 2 - 1; k++) {
  354. unsigned int min;
  355. if (k < ndigits)
  356. min = 0;
  357. else
  358. min = (k + 1) - ndigits;
  359. for (i = min; i <= k && i <= k - i; i++) {
  360. uint128_t product;
  361. product = mul_64_64(left[i], left[k - i]);
  362. if (i < k - i) {
  363. r2 += product.m_high >> 63;
  364. product.m_high = (product.m_high << 1) |
  365. (product.m_low >> 63);
  366. product.m_low <<= 1;
  367. }
  368. r01 = add_128_128(r01, product);
  369. r2 += (r01.m_high < product.m_high);
  370. }
  371. result[k] = r01.m_low;
  372. r01.m_low = r01.m_high;
  373. r01.m_high = r2;
  374. r2 = 0;
  375. }
  376. result[ndigits * 2 - 1] = r01.m_low;
  377. }
  378. /* Computes result = (left + right) % mod.
  379. * Assumes that left < mod and right < mod, result != mod.
  380. */
  381. static void vli_mod_add(u64 *result, const u64 *left, const u64 *right,
  382. const u64 *mod, unsigned int ndigits)
  383. {
  384. u64 carry;
  385. carry = vli_add(result, left, right, ndigits);
  386. /* result > mod (result = mod + remainder), so subtract mod to
  387. * get remainder.
  388. */
  389. if (carry || vli_cmp(result, mod, ndigits) >= 0)
  390. vli_sub(result, result, mod, ndigits);
  391. }
  392. /* Computes result = (left - right) % mod.
  393. * Assumes that left < mod and right < mod, result != mod.
  394. */
  395. static void vli_mod_sub(u64 *result, const u64 *left, const u64 *right,
  396. const u64 *mod, unsigned int ndigits)
  397. {
  398. u64 borrow = vli_sub(result, left, right, ndigits);
  399. /* In this case, p_result == -diff == (max int) - diff.
  400. * Since -x % d == d - x, we can get the correct result from
  401. * result + mod (with overflow).
  402. */
  403. if (borrow)
  404. vli_add(result, result, mod, ndigits);
  405. }
  406. /*
  407. * Computes result = product % mod
  408. * for special form moduli: p = 2^k-c, for small c (note the minus sign)
  409. *
  410. * References:
  411. * R. Crandall, C. Pomerance. Prime Numbers: A Computational Perspective.
  412. * 9 Fast Algorithms for Large-Integer Arithmetic. 9.2.3 Moduli of special form
  413. * Algorithm 9.2.13 (Fast mod operation for special-form moduli).
  414. */
  415. static void vli_mmod_special(u64 *result, const u64 *product,
  416. const u64 *mod, unsigned int ndigits)
  417. {
  418. u64 c = -mod[0];
  419. u64 t[ECC_MAX_DIGITS * 2];
  420. u64 r[ECC_MAX_DIGITS * 2];
  421. vli_set(r, product, ndigits * 2);
  422. while (!vli_is_zero(r + ndigits, ndigits)) {
  423. vli_umult(t, r + ndigits, c, ndigits);
  424. vli_clear(r + ndigits, ndigits);
  425. vli_add(r, r, t, ndigits * 2);
  426. }
  427. vli_set(t, mod, ndigits);
  428. vli_clear(t + ndigits, ndigits);
  429. while (vli_cmp(r, t, ndigits * 2) >= 0)
  430. vli_sub(r, r, t, ndigits * 2);
  431. vli_set(result, r, ndigits);
  432. }
  433. /*
  434. * Computes result = product % mod
  435. * for special form moduli: p = 2^{k-1}+c, for small c (note the plus sign)
  436. * where k-1 does not fit into qword boundary by -1 bit (such as 255).
  437. * References (loosely based on):
  438. * A. Menezes, P. van Oorschot, S. Vanstone. Handbook of Applied Cryptography.
  439. * 14.3.4 Reduction methods for moduli of special form. Algorithm 14.47.
  440. * URL: http://cacr.uwaterloo.ca/hac/about/chap14.pdf
  441. *
  442. * H. Cohen, G. Frey, R. Avanzi, C. Doche, T. Lange, K. Nguyen, F. Vercauteren.
  443. * Handbook of Elliptic and Hyperelliptic Curve Cryptography.
  444. * Algorithm 10.25 Fast reduction for special form moduli
  445. */
  446. static void vli_mmod_special2(u64 *result, const u64 *product,
  447. const u64 *mod, unsigned int ndigits)
  448. {
  449. u64 c2 = mod[0] * 2;
  450. u64 q[ECC_MAX_DIGITS];
  451. u64 r[ECC_MAX_DIGITS * 2];
  452. u64 m[ECC_MAX_DIGITS * 2]; /* expanded mod */
  453. int carry; /* last bit that doesn't fit into q */
  454. int i;
  455. vli_set(m, mod, ndigits);
  456. vli_clear(m + ndigits, ndigits);
  457. vli_set(r, product, ndigits);
  458. /* q and carry are top bits */
  459. vli_set(q, product + ndigits, ndigits);
  460. vli_clear(r + ndigits, ndigits);
  461. carry = vli_is_negative(r, ndigits);
  462. if (carry)
  463. r[ndigits - 1] &= (1ull << 63) - 1;
  464. for (i = 1; carry || !vli_is_zero(q, ndigits); i++) {
  465. u64 qc[ECC_MAX_DIGITS * 2];
  466. vli_umult(qc, q, c2, ndigits);
  467. if (carry)
  468. vli_uadd(qc, qc, mod[0], ndigits * 2);
  469. vli_set(q, qc + ndigits, ndigits);
  470. vli_clear(qc + ndigits, ndigits);
  471. carry = vli_is_negative(qc, ndigits);
  472. if (carry)
  473. qc[ndigits - 1] &= (1ull << 63) - 1;
  474. if (i & 1)
  475. vli_sub(r, r, qc, ndigits * 2);
  476. else
  477. vli_add(r, r, qc, ndigits * 2);
  478. }
  479. while (vli_is_negative(r, ndigits * 2))
  480. vli_add(r, r, m, ndigits * 2);
  481. while (vli_cmp(r, m, ndigits * 2) >= 0)
  482. vli_sub(r, r, m, ndigits * 2);
  483. vli_set(result, r, ndigits);
  484. }
  485. /*
  486. * Computes result = product % mod, where product is 2N words long.
  487. * Reference: Ken MacKay's micro-ecc.
  488. * Currently only designed to work for curve_p or curve_n.
  489. */
  490. static void vli_mmod_slow(u64 *result, u64 *product, const u64 *mod,
  491. unsigned int ndigits)
  492. {
  493. u64 mod_m[2 * ECC_MAX_DIGITS];
  494. u64 tmp[2 * ECC_MAX_DIGITS];
  495. u64 *v[2] = { tmp, product };
  496. u64 carry = 0;
  497. unsigned int i;
  498. /* Shift mod so its highest set bit is at the maximum position. */
  499. int shift = (ndigits * 2 * 64) - vli_num_bits(mod, ndigits);
  500. int word_shift = shift / 64;
  501. int bit_shift = shift % 64;
  502. vli_clear(mod_m, word_shift);
  503. if (bit_shift > 0) {
  504. for (i = 0; i < ndigits; ++i) {
  505. mod_m[word_shift + i] = (mod[i] << bit_shift) | carry;
  506. carry = mod[i] >> (64 - bit_shift);
  507. }
  508. } else
  509. vli_set(mod_m + word_shift, mod, ndigits);
  510. for (i = 1; shift >= 0; --shift) {
  511. u64 borrow = 0;
  512. unsigned int j;
  513. for (j = 0; j < ndigits * 2; ++j) {
  514. u64 diff = v[i][j] - mod_m[j] - borrow;
  515. if (diff != v[i][j])
  516. borrow = (diff > v[i][j]);
  517. v[1 - i][j] = diff;
  518. }
  519. i = !(i ^ borrow); /* Swap the index if there was no borrow */
  520. vli_rshift1(mod_m, ndigits);
  521. mod_m[ndigits - 1] |= mod_m[ndigits] << (64 - 1);
  522. vli_rshift1(mod_m + ndigits, ndigits);
  523. }
  524. vli_set(result, v[i], ndigits);
  525. }
  526. /* Computes result = product % mod using Barrett's reduction with precomputed
  527. * value mu appended to the mod after ndigits, mu = (2^{2w} / mod) and have
  528. * length ndigits + 1, where mu * (2^w - 1) should not overflow ndigits
  529. * boundary.
  530. *
  531. * Reference:
  532. * R. Brent, P. Zimmermann. Modern Computer Arithmetic. 2010.
  533. * 2.4.1 Barrett's algorithm. Algorithm 2.5.
  534. */
  535. static void vli_mmod_barrett(u64 *result, u64 *product, const u64 *mod,
  536. unsigned int ndigits)
  537. {
  538. u64 q[ECC_MAX_DIGITS * 2];
  539. u64 r[ECC_MAX_DIGITS * 2];
  540. const u64 *mu = mod + ndigits;
  541. vli_mult(q, product + ndigits, mu, ndigits);
  542. if (mu[ndigits])
  543. vli_add(q + ndigits, q + ndigits, product + ndigits, ndigits);
  544. vli_mult(r, mod, q + ndigits, ndigits);
  545. vli_sub(r, product, r, ndigits * 2);
  546. while (!vli_is_zero(r + ndigits, ndigits) ||
  547. vli_cmp(r, mod, ndigits) != -1) {
  548. u64 carry;
  549. carry = vli_sub(r, r, mod, ndigits);
  550. vli_usub(r + ndigits, r + ndigits, carry, ndigits);
  551. }
  552. vli_set(result, r, ndigits);
  553. }
  554. /* Computes p_result = p_product % curve_p.
  555. * See algorithm 5 and 6 from
  556. * http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf
  557. */
  558. static void vli_mmod_fast_192(u64 *result, const u64 *product,
  559. const u64 *curve_prime, u64 *tmp)
  560. {
  561. const unsigned int ndigits = 3;
  562. int carry;
  563. vli_set(result, product, ndigits);
  564. vli_set(tmp, &product[3], ndigits);
  565. carry = vli_add(result, result, tmp, ndigits);
  566. tmp[0] = 0;
  567. tmp[1] = product[3];
  568. tmp[2] = product[4];
  569. carry += vli_add(result, result, tmp, ndigits);
  570. tmp[0] = tmp[1] = product[5];
  571. tmp[2] = 0;
  572. carry += vli_add(result, result, tmp, ndigits);
  573. while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
  574. carry -= vli_sub(result, result, curve_prime, ndigits);
  575. }
  576. /* Computes result = product % curve_prime
  577. * from http://www.nsa.gov/ia/_files/nist-routines.pdf
  578. */
  579. static void vli_mmod_fast_256(u64 *result, const u64 *product,
  580. const u64 *curve_prime, u64 *tmp)
  581. {
  582. int carry;
  583. const unsigned int ndigits = 4;
  584. /* t */
  585. vli_set(result, product, ndigits);
  586. /* s1 */
  587. tmp[0] = 0;
  588. tmp[1] = product[5] & 0xffffffff00000000ull;
  589. tmp[2] = product[6];
  590. tmp[3] = product[7];
  591. carry = vli_lshift(tmp, tmp, 1, ndigits);
  592. carry += vli_add(result, result, tmp, ndigits);
  593. /* s2 */
  594. tmp[1] = product[6] << 32;
  595. tmp[2] = (product[6] >> 32) | (product[7] << 32);
  596. tmp[3] = product[7] >> 32;
  597. carry += vli_lshift(tmp, tmp, 1, ndigits);
  598. carry += vli_add(result, result, tmp, ndigits);
  599. /* s3 */
  600. tmp[0] = product[4];
  601. tmp[1] = product[5] & 0xffffffff;
  602. tmp[2] = 0;
  603. tmp[3] = product[7];
  604. carry += vli_add(result, result, tmp, ndigits);
  605. /* s4 */
  606. tmp[0] = (product[4] >> 32) | (product[5] << 32);
  607. tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
  608. tmp[2] = product[7];
  609. tmp[3] = (product[6] >> 32) | (product[4] << 32);
  610. carry += vli_add(result, result, tmp, ndigits);
  611. /* d1 */
  612. tmp[0] = (product[5] >> 32) | (product[6] << 32);
  613. tmp[1] = (product[6] >> 32);
  614. tmp[2] = 0;
  615. tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
  616. carry -= vli_sub(result, result, tmp, ndigits);
  617. /* d2 */
  618. tmp[0] = product[6];
  619. tmp[1] = product[7];
  620. tmp[2] = 0;
  621. tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
  622. carry -= vli_sub(result, result, tmp, ndigits);
  623. /* d3 */
  624. tmp[0] = (product[6] >> 32) | (product[7] << 32);
  625. tmp[1] = (product[7] >> 32) | (product[4] << 32);
  626. tmp[2] = (product[4] >> 32) | (product[5] << 32);
  627. tmp[3] = (product[6] << 32);
  628. carry -= vli_sub(result, result, tmp, ndigits);
  629. /* d4 */
  630. tmp[0] = product[7];
  631. tmp[1] = product[4] & 0xffffffff00000000ull;
  632. tmp[2] = product[5];
  633. tmp[3] = product[6] & 0xffffffff00000000ull;
  634. carry -= vli_sub(result, result, tmp, ndigits);
  635. if (carry < 0) {
  636. do {
  637. carry += vli_add(result, result, curve_prime, ndigits);
  638. } while (carry < 0);
  639. } else {
  640. while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
  641. carry -= vli_sub(result, result, curve_prime, ndigits);
  642. }
  643. }
  644. /* Computes result = product % curve_prime for different curve_primes.
  645. *
  646. * Note that curve_primes are distinguished just by heuristic check and
  647. * not by complete conformance check.
  648. */
  649. static bool vli_mmod_fast(u64 *result, u64 *product,
  650. const u64 *curve_prime, unsigned int ndigits)
  651. {
  652. u64 tmp[2 * ECC_MAX_DIGITS];
  653. /* Currently, both NIST primes have -1 in lowest qword. */
  654. if (curve_prime[0] != -1ull) {
  655. /* Try to handle Pseudo-Marsenne primes. */
  656. if (curve_prime[ndigits - 1] == -1ull) {
  657. vli_mmod_special(result, product, curve_prime,
  658. ndigits);
  659. return true;
  660. } else if (curve_prime[ndigits - 1] == 1ull << 63 &&
  661. curve_prime[ndigits - 2] == 0) {
  662. vli_mmod_special2(result, product, curve_prime,
  663. ndigits);
  664. return true;
  665. }
  666. vli_mmod_barrett(result, product, curve_prime, ndigits);
  667. return true;
  668. }
  669. switch (ndigits) {
  670. case 3:
  671. vli_mmod_fast_192(result, product, curve_prime, tmp);
  672. break;
  673. case 4:
  674. vli_mmod_fast_256(result, product, curve_prime, tmp);
  675. break;
  676. default:
  677. pr_err_ratelimited("ecc: unsupported digits size!\n");
  678. return false;
  679. }
  680. return true;
  681. }
  682. /* Computes result = (left * right) % mod.
  683. * Assumes that mod is big enough curve order.
  684. */
  685. void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,
  686. const u64 *mod, unsigned int ndigits)
  687. {
  688. u64 product[ECC_MAX_DIGITS * 2];
  689. vli_mult(product, left, right, ndigits);
  690. vli_mmod_slow(result, product, mod, ndigits);
  691. }
  692. EXPORT_SYMBOL(vli_mod_mult_slow);
  693. /* Computes result = (left * right) % curve_prime. */
  694. static void vli_mod_mult_fast(u64 *result, const u64 *left, const u64 *right,
  695. const u64 *curve_prime, unsigned int ndigits)
  696. {
  697. u64 product[2 * ECC_MAX_DIGITS];
  698. vli_mult(product, left, right, ndigits);
  699. vli_mmod_fast(result, product, curve_prime, ndigits);
  700. }
  701. /* Computes result = left^2 % curve_prime. */
  702. static void vli_mod_square_fast(u64 *result, const u64 *left,
  703. const u64 *curve_prime, unsigned int ndigits)
  704. {
  705. u64 product[2 * ECC_MAX_DIGITS];
  706. vli_square(product, left, ndigits);
  707. vli_mmod_fast(result, product, curve_prime, ndigits);
  708. }
  709. #define EVEN(vli) (!(vli[0] & 1))
  710. /* Computes result = (1 / p_input) % mod. All VLIs are the same size.
  711. * See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
  712. * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
  713. */
  714. void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
  715. unsigned int ndigits)
  716. {
  717. u64 a[ECC_MAX_DIGITS], b[ECC_MAX_DIGITS];
  718. u64 u[ECC_MAX_DIGITS], v[ECC_MAX_DIGITS];
  719. u64 carry;
  720. int cmp_result;
  721. if (vli_is_zero(input, ndigits)) {
  722. vli_clear(result, ndigits);
  723. return;
  724. }
  725. vli_set(a, input, ndigits);
  726. vli_set(b, mod, ndigits);
  727. vli_clear(u, ndigits);
  728. u[0] = 1;
  729. vli_clear(v, ndigits);
  730. while ((cmp_result = vli_cmp(a, b, ndigits)) != 0) {
  731. carry = 0;
  732. if (EVEN(a)) {
  733. vli_rshift1(a, ndigits);
  734. if (!EVEN(u))
  735. carry = vli_add(u, u, mod, ndigits);
  736. vli_rshift1(u, ndigits);
  737. if (carry)
  738. u[ndigits - 1] |= 0x8000000000000000ull;
  739. } else if (EVEN(b)) {
  740. vli_rshift1(b, ndigits);
  741. if (!EVEN(v))
  742. carry = vli_add(v, v, mod, ndigits);
  743. vli_rshift1(v, ndigits);
  744. if (carry)
  745. v[ndigits - 1] |= 0x8000000000000000ull;
  746. } else if (cmp_result > 0) {
  747. vli_sub(a, a, b, ndigits);
  748. vli_rshift1(a, ndigits);
  749. if (vli_cmp(u, v, ndigits) < 0)
  750. vli_add(u, u, mod, ndigits);
  751. vli_sub(u, u, v, ndigits);
  752. if (!EVEN(u))
  753. carry = vli_add(u, u, mod, ndigits);
  754. vli_rshift1(u, ndigits);
  755. if (carry)
  756. u[ndigits - 1] |= 0x8000000000000000ull;
  757. } else {
  758. vli_sub(b, b, a, ndigits);
  759. vli_rshift1(b, ndigits);
  760. if (vli_cmp(v, u, ndigits) < 0)
  761. vli_add(v, v, mod, ndigits);
  762. vli_sub(v, v, u, ndigits);
  763. if (!EVEN(v))
  764. carry = vli_add(v, v, mod, ndigits);
  765. vli_rshift1(v, ndigits);
  766. if (carry)
  767. v[ndigits - 1] |= 0x8000000000000000ull;
  768. }
  769. }
  770. vli_set(result, u, ndigits);
  771. }
  772. EXPORT_SYMBOL(vli_mod_inv);
  773. /* ------ Point operations ------ */
  774. /* Returns true if p_point is the point at infinity, false otherwise. */
  775. static bool ecc_point_is_zero(const struct ecc_point *point)
  776. {
  777. return (vli_is_zero(point->x, point->ndigits) &&
  778. vli_is_zero(point->y, point->ndigits));
  779. }
  780. /* Point multiplication algorithm using Montgomery's ladder with co-Z
  781. * coordinates. From https://eprint.iacr.org/2011/338.pdf
  782. */
  783. /* Double in place */
  784. static void ecc_point_double_jacobian(u64 *x1, u64 *y1, u64 *z1,
  785. u64 *curve_prime, unsigned int ndigits)
  786. {
  787. /* t1 = x, t2 = y, t3 = z */
  788. u64 t4[ECC_MAX_DIGITS];
  789. u64 t5[ECC_MAX_DIGITS];
  790. if (vli_is_zero(z1, ndigits))
  791. return;
  792. /* t4 = y1^2 */
  793. vli_mod_square_fast(t4, y1, curve_prime, ndigits);
  794. /* t5 = x1*y1^2 = A */
  795. vli_mod_mult_fast(t5, x1, t4, curve_prime, ndigits);
  796. /* t4 = y1^4 */
  797. vli_mod_square_fast(t4, t4, curve_prime, ndigits);
  798. /* t2 = y1*z1 = z3 */
  799. vli_mod_mult_fast(y1, y1, z1, curve_prime, ndigits);
  800. /* t3 = z1^2 */
  801. vli_mod_square_fast(z1, z1, curve_prime, ndigits);
  802. /* t1 = x1 + z1^2 */
  803. vli_mod_add(x1, x1, z1, curve_prime, ndigits);
  804. /* t3 = 2*z1^2 */
  805. vli_mod_add(z1, z1, z1, curve_prime, ndigits);
  806. /* t3 = x1 - z1^2 */
  807. vli_mod_sub(z1, x1, z1, curve_prime, ndigits);
  808. /* t1 = x1^2 - z1^4 */
  809. vli_mod_mult_fast(x1, x1, z1, curve_prime, ndigits);
  810. /* t3 = 2*(x1^2 - z1^4) */
  811. vli_mod_add(z1, x1, x1, curve_prime, ndigits);
  812. /* t1 = 3*(x1^2 - z1^4) */
  813. vli_mod_add(x1, x1, z1, curve_prime, ndigits);
  814. if (vli_test_bit(x1, 0)) {
  815. u64 carry = vli_add(x1, x1, curve_prime, ndigits);
  816. vli_rshift1(x1, ndigits);
  817. x1[ndigits - 1] |= carry << 63;
  818. } else {
  819. vli_rshift1(x1, ndigits);
  820. }
  821. /* t1 = 3/2*(x1^2 - z1^4) = B */
  822. /* t3 = B^2 */
  823. vli_mod_square_fast(z1, x1, curve_prime, ndigits);
  824. /* t3 = B^2 - A */
  825. vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
  826. /* t3 = B^2 - 2A = x3 */
  827. vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
  828. /* t5 = A - x3 */
  829. vli_mod_sub(t5, t5, z1, curve_prime, ndigits);
  830. /* t1 = B * (A - x3) */
  831. vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
  832. /* t4 = B * (A - x3) - y1^4 = y3 */
  833. vli_mod_sub(t4, x1, t4, curve_prime, ndigits);
  834. vli_set(x1, z1, ndigits);
  835. vli_set(z1, y1, ndigits);
  836. vli_set(y1, t4, ndigits);
  837. }
  838. /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
  839. static void apply_z(u64 *x1, u64 *y1, u64 *z, u64 *curve_prime,
  840. unsigned int ndigits)
  841. {
  842. u64 t1[ECC_MAX_DIGITS];
  843. vli_mod_square_fast(t1, z, curve_prime, ndigits); /* z^2 */
  844. vli_mod_mult_fast(x1, x1, t1, curve_prime, ndigits); /* x1 * z^2 */
  845. vli_mod_mult_fast(t1, t1, z, curve_prime, ndigits); /* z^3 */
  846. vli_mod_mult_fast(y1, y1, t1, curve_prime, ndigits); /* y1 * z^3 */
  847. }
  848. /* P = (x1, y1) => 2P, (x2, y2) => P' */
  849. static void xycz_initial_double(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
  850. u64 *p_initial_z, u64 *curve_prime,
  851. unsigned int ndigits)
  852. {
  853. u64 z[ECC_MAX_DIGITS];
  854. vli_set(x2, x1, ndigits);
  855. vli_set(y2, y1, ndigits);
  856. vli_clear(z, ndigits);
  857. z[0] = 1;
  858. if (p_initial_z)
  859. vli_set(z, p_initial_z, ndigits);
  860. apply_z(x1, y1, z, curve_prime, ndigits);
  861. ecc_point_double_jacobian(x1, y1, z, curve_prime, ndigits);
  862. apply_z(x2, y2, z, curve_prime, ndigits);
  863. }
  864. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  865. * Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
  866. * or P => P', Q => P + Q
  867. */
  868. static void xycz_add(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
  869. unsigned int ndigits)
  870. {
  871. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  872. u64 t5[ECC_MAX_DIGITS];
  873. /* t5 = x2 - x1 */
  874. vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
  875. /* t5 = (x2 - x1)^2 = A */
  876. vli_mod_square_fast(t5, t5, curve_prime, ndigits);
  877. /* t1 = x1*A = B */
  878. vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
  879. /* t3 = x2*A = C */
  880. vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
  881. /* t4 = y2 - y1 */
  882. vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
  883. /* t5 = (y2 - y1)^2 = D */
  884. vli_mod_square_fast(t5, y2, curve_prime, ndigits);
  885. /* t5 = D - B */
  886. vli_mod_sub(t5, t5, x1, curve_prime, ndigits);
  887. /* t5 = D - B - C = x3 */
  888. vli_mod_sub(t5, t5, x2, curve_prime, ndigits);
  889. /* t3 = C - B */
  890. vli_mod_sub(x2, x2, x1, curve_prime, ndigits);
  891. /* t2 = y1*(C - B) */
  892. vli_mod_mult_fast(y1, y1, x2, curve_prime, ndigits);
  893. /* t3 = B - x3 */
  894. vli_mod_sub(x2, x1, t5, curve_prime, ndigits);
  895. /* t4 = (y2 - y1)*(B - x3) */
  896. vli_mod_mult_fast(y2, y2, x2, curve_prime, ndigits);
  897. /* t4 = y3 */
  898. vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
  899. vli_set(x2, t5, ndigits);
  900. }
  901. /* Input P = (x1, y1, Z), Q = (x2, y2, Z)
  902. * Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
  903. * or P => P - Q, Q => P + Q
  904. */
  905. static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
  906. unsigned int ndigits)
  907. {
  908. /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
  909. u64 t5[ECC_MAX_DIGITS];
  910. u64 t6[ECC_MAX_DIGITS];
  911. u64 t7[ECC_MAX_DIGITS];
  912. /* t5 = x2 - x1 */
  913. vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
  914. /* t5 = (x2 - x1)^2 = A */
  915. vli_mod_square_fast(t5, t5, curve_prime, ndigits);
  916. /* t1 = x1*A = B */
  917. vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
  918. /* t3 = x2*A = C */
  919. vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
  920. /* t4 = y2 + y1 */
  921. vli_mod_add(t5, y2, y1, curve_prime, ndigits);
  922. /* t4 = y2 - y1 */
  923. vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
  924. /* t6 = C - B */
  925. vli_mod_sub(t6, x2, x1, curve_prime, ndigits);
  926. /* t2 = y1 * (C - B) */
  927. vli_mod_mult_fast(y1, y1, t6, curve_prime, ndigits);
  928. /* t6 = B + C */
  929. vli_mod_add(t6, x1, x2, curve_prime, ndigits);
  930. /* t3 = (y2 - y1)^2 */
  931. vli_mod_square_fast(x2, y2, curve_prime, ndigits);
  932. /* t3 = x3 */
  933. vli_mod_sub(x2, x2, t6, curve_prime, ndigits);
  934. /* t7 = B - x3 */
  935. vli_mod_sub(t7, x1, x2, curve_prime, ndigits);
  936. /* t4 = (y2 - y1)*(B - x3) */
  937. vli_mod_mult_fast(y2, y2, t7, curve_prime, ndigits);
  938. /* t4 = y3 */
  939. vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
  940. /* t7 = (y2 + y1)^2 = F */
  941. vli_mod_square_fast(t7, t5, curve_prime, ndigits);
  942. /* t7 = x3' */
  943. vli_mod_sub(t7, t7, t6, curve_prime, ndigits);
  944. /* t6 = x3' - B */
  945. vli_mod_sub(t6, t7, x1, curve_prime, ndigits);
  946. /* t6 = (y2 + y1)*(x3' - B) */
  947. vli_mod_mult_fast(t6, t6, t5, curve_prime, ndigits);
  948. /* t2 = y3' */
  949. vli_mod_sub(y1, t6, y1, curve_prime, ndigits);
  950. vli_set(x1, t7, ndigits);
  951. }
  952. static void ecc_point_mult(struct ecc_point *result,
  953. const struct ecc_point *point, const u64 *scalar,
  954. u64 *initial_z, const struct ecc_curve *curve,
  955. unsigned int ndigits)
  956. {
  957. /* R0 and R1 */
  958. u64 rx[2][ECC_MAX_DIGITS];
  959. u64 ry[2][ECC_MAX_DIGITS];
  960. u64 z[ECC_MAX_DIGITS];
  961. u64 sk[2][ECC_MAX_DIGITS];
  962. u64 *curve_prime = curve->p;
  963. int i, nb;
  964. int num_bits;
  965. int carry;
  966. carry = vli_add(sk[0], scalar, curve->n, ndigits);
  967. vli_add(sk[1], sk[0], curve->n, ndigits);
  968. scalar = sk[!carry];
  969. num_bits = sizeof(u64) * ndigits * 8 + 1;
  970. vli_set(rx[1], point->x, ndigits);
  971. vli_set(ry[1], point->y, ndigits);
  972. xycz_initial_double(rx[1], ry[1], rx[0], ry[0], initial_z, curve_prime,
  973. ndigits);
  974. for (i = num_bits - 2; i > 0; i--) {
  975. nb = !vli_test_bit(scalar, i);
  976. xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
  977. ndigits);
  978. xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime,
  979. ndigits);
  980. }
  981. nb = !vli_test_bit(scalar, 0);
  982. xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
  983. ndigits);
  984. /* Find final 1/Z value. */
  985. /* X1 - X0 */
  986. vli_mod_sub(z, rx[1], rx[0], curve_prime, ndigits);
  987. /* Yb * (X1 - X0) */
  988. vli_mod_mult_fast(z, z, ry[1 - nb], curve_prime, ndigits);
  989. /* xP * Yb * (X1 - X0) */
  990. vli_mod_mult_fast(z, z, point->x, curve_prime, ndigits);
  991. /* 1 / (xP * Yb * (X1 - X0)) */
  992. vli_mod_inv(z, z, curve_prime, point->ndigits);
  993. /* yP / (xP * Yb * (X1 - X0)) */
  994. vli_mod_mult_fast(z, z, point->y, curve_prime, ndigits);
  995. /* Xb * yP / (xP * Yb * (X1 - X0)) */
  996. vli_mod_mult_fast(z, z, rx[1 - nb], curve_prime, ndigits);
  997. /* End 1/Z calculation */
  998. xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime, ndigits);
  999. apply_z(rx[0], ry[0], z, curve_prime, ndigits);
  1000. vli_set(result->x, rx[0], ndigits);
  1001. vli_set(result->y, ry[0], ndigits);
  1002. }
  1003. /* Computes R = P + Q mod p */
  1004. static void ecc_point_add(const struct ecc_point *result,
  1005. const struct ecc_point *p, const struct ecc_point *q,
  1006. const struct ecc_curve *curve)
  1007. {
  1008. u64 z[ECC_MAX_DIGITS];
  1009. u64 px[ECC_MAX_DIGITS];
  1010. u64 py[ECC_MAX_DIGITS];
  1011. unsigned int ndigits = curve->g.ndigits;
  1012. vli_set(result->x, q->x, ndigits);
  1013. vli_set(result->y, q->y, ndigits);
  1014. vli_mod_sub(z, result->x, p->x, curve->p, ndigits);
  1015. vli_set(px, p->x, ndigits);
  1016. vli_set(py, p->y, ndigits);
  1017. xycz_add(px, py, result->x, result->y, curve->p, ndigits);
  1018. vli_mod_inv(z, z, curve->p, ndigits);
  1019. apply_z(result->x, result->y, z, curve->p, ndigits);
  1020. }
  1021. /* Computes R = u1P + u2Q mod p using Shamir's trick.
  1022. * Based on: Kenneth MacKay's micro-ecc (2014).
  1023. */
  1024. void ecc_point_mult_shamir(const struct ecc_point *result,
  1025. const u64 *u1, const struct ecc_point *p,
  1026. const u64 *u2, const struct ecc_point *q,
  1027. const struct ecc_curve *curve)
  1028. {
  1029. u64 z[ECC_MAX_DIGITS];
  1030. u64 sump[2][ECC_MAX_DIGITS];
  1031. u64 *rx = result->x;
  1032. u64 *ry = result->y;
  1033. unsigned int ndigits = curve->g.ndigits;
  1034. unsigned int num_bits;
  1035. struct ecc_point sum = ECC_POINT_INIT(sump[0], sump[1], ndigits);
  1036. const struct ecc_point *points[4];
  1037. const struct ecc_point *point;
  1038. unsigned int idx;
  1039. int i;
  1040. ecc_point_add(&sum, p, q, curve);
  1041. points[0] = NULL;
  1042. points[1] = p;
  1043. points[2] = q;
  1044. points[3] = &sum;
  1045. num_bits = max(vli_num_bits(u1, ndigits),
  1046. vli_num_bits(u2, ndigits));
  1047. i = num_bits - 1;
  1048. idx = (!!vli_test_bit(u1, i)) | ((!!vli_test_bit(u2, i)) << 1);
  1049. point = points[idx];
  1050. vli_set(rx, point->x, ndigits);
  1051. vli_set(ry, point->y, ndigits);
  1052. vli_clear(z + 1, ndigits - 1);
  1053. z[0] = 1;
  1054. for (--i; i >= 0; i--) {
  1055. ecc_point_double_jacobian(rx, ry, z, curve->p, ndigits);
  1056. idx = (!!vli_test_bit(u1, i)) | ((!!vli_test_bit(u2, i)) << 1);
  1057. point = points[idx];
  1058. if (point) {
  1059. u64 tx[ECC_MAX_DIGITS];
  1060. u64 ty[ECC_MAX_DIGITS];
  1061. u64 tz[ECC_MAX_DIGITS];
  1062. vli_set(tx, point->x, ndigits);
  1063. vli_set(ty, point->y, ndigits);
  1064. apply_z(tx, ty, z, curve->p, ndigits);
  1065. vli_mod_sub(tz, rx, tx, curve->p, ndigits);
  1066. xycz_add(tx, ty, rx, ry, curve->p, ndigits);
  1067. vli_mod_mult_fast(z, z, tz, curve->p, ndigits);
  1068. }
  1069. }
  1070. vli_mod_inv(z, z, curve->p, ndigits);
  1071. apply_z(rx, ry, z, curve->p, ndigits);
  1072. }
  1073. EXPORT_SYMBOL(ecc_point_mult_shamir);
  1074. static inline void ecc_swap_digits(const u64 *in, u64 *out,
  1075. unsigned int ndigits)
  1076. {
  1077. const __be64 *src = (__force __be64 *)in;
  1078. int i;
  1079. for (i = 0; i < ndigits; i++)
  1080. out[i] = be64_to_cpu(src[ndigits - 1 - i]);
  1081. }
  1082. static int __ecc_is_key_valid(const struct ecc_curve *curve,
  1083. const u64 *private_key, unsigned int ndigits)
  1084. {
  1085. u64 one[ECC_MAX_DIGITS] = { 1, };
  1086. u64 res[ECC_MAX_DIGITS];
  1087. if (!private_key)
  1088. return -EINVAL;
  1089. if (curve->g.ndigits != ndigits)
  1090. return -EINVAL;
  1091. /* Make sure the private key is in the range [2, n-3]. */
  1092. if (vli_cmp(one, private_key, ndigits) != -1)
  1093. return -EINVAL;
  1094. vli_sub(res, curve->n, one, ndigits);
  1095. vli_sub(res, res, one, ndigits);
  1096. if (vli_cmp(res, private_key, ndigits) != 1)
  1097. return -EINVAL;
  1098. return 0;
  1099. }
  1100. int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
  1101. const u64 *private_key, unsigned int private_key_len)
  1102. {
  1103. int nbytes;
  1104. const struct ecc_curve *curve = ecc_get_curve(curve_id);
  1105. nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
  1106. if (private_key_len != nbytes)
  1107. return -EINVAL;
  1108. return __ecc_is_key_valid(curve, private_key, ndigits);
  1109. }
  1110. EXPORT_SYMBOL(ecc_is_key_valid);
  1111. /*
  1112. * ECC private keys are generated using the method of extra random bits,
  1113. * equivalent to that described in FIPS 186-4, Appendix B.4.1.
  1114. *
  1115. * d = (c mod(n–1)) + 1 where c is a string of random bits, 64 bits longer
  1116. * than requested
  1117. * 0 <= c mod(n-1) <= n-2 and implies that
  1118. * 1 <= d <= n-1
  1119. *
  1120. * This method generates a private key uniformly distributed in the range
  1121. * [1, n-1].
  1122. */
  1123. int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey)
  1124. {
  1125. const struct ecc_curve *curve = ecc_get_curve(curve_id);
  1126. u64 priv[ECC_MAX_DIGITS];
  1127. unsigned int nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
  1128. unsigned int nbits = vli_num_bits(curve->n, ndigits);
  1129. int err;
  1130. /* Check that N is included in Table 1 of FIPS 186-4, section 6.1.1 */
  1131. if (nbits < 160 || ndigits > ARRAY_SIZE(priv))
  1132. return -EINVAL;
  1133. /*
  1134. * FIPS 186-4 recommends that the private key should be obtained from a
  1135. * RBG with a security strength equal to or greater than the security
  1136. * strength associated with N.
  1137. *
  1138. * The maximum security strength identified by NIST SP800-57pt1r4 for
  1139. * ECC is 256 (N >= 512).
  1140. *
  1141. * This condition is met by the default RNG because it selects a favored
  1142. * DRBG with a security strength of 256.
  1143. */
  1144. if (crypto_get_default_rng())
  1145. return -EFAULT;
  1146. err = crypto_rng_get_bytes(crypto_default_rng, (u8 *)priv, nbytes);
  1147. crypto_put_default_rng();
  1148. if (err)
  1149. return err;
  1150. /* Make sure the private key is in the valid range. */
  1151. if (__ecc_is_key_valid(curve, priv, ndigits))
  1152. return -EINVAL;
  1153. ecc_swap_digits(priv, privkey, ndigits);
  1154. return 0;
  1155. }
  1156. EXPORT_SYMBOL(ecc_gen_privkey);
  1157. int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
  1158. const u64 *private_key, u64 *public_key)
  1159. {
  1160. int ret = 0;
  1161. struct ecc_point *pk;
  1162. u64 priv[ECC_MAX_DIGITS];
  1163. const struct ecc_curve *curve = ecc_get_curve(curve_id);
  1164. if (!private_key || !curve || ndigits > ARRAY_SIZE(priv)) {
  1165. ret = -EINVAL;
  1166. goto out;
  1167. }
  1168. ecc_swap_digits(private_key, priv, ndigits);
  1169. pk = ecc_alloc_point(ndigits);
  1170. if (!pk) {
  1171. ret = -ENOMEM;
  1172. goto out;
  1173. }
  1174. ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits);
  1175. /* SP800-56A rev 3 5.6.2.1.3 key check */
  1176. if (ecc_is_pubkey_valid_full(curve, pk)) {
  1177. ret = -EAGAIN;
  1178. goto err_free_point;
  1179. }
  1180. ecc_swap_digits(pk->x, public_key, ndigits);
  1181. ecc_swap_digits(pk->y, &public_key[ndigits], ndigits);
  1182. err_free_point:
  1183. ecc_free_point(pk);
  1184. out:
  1185. return ret;
  1186. }
  1187. EXPORT_SYMBOL(ecc_make_pub_key);
  1188. /* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
  1189. int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
  1190. struct ecc_point *pk)
  1191. {
  1192. u64 yy[ECC_MAX_DIGITS], xxx[ECC_MAX_DIGITS], w[ECC_MAX_DIGITS];
  1193. if (WARN_ON(pk->ndigits != curve->g.ndigits))
  1194. return -EINVAL;
  1195. /* Check 1: Verify key is not the zero point. */
  1196. if (ecc_point_is_zero(pk))
  1197. return -EINVAL;
  1198. /* Check 2: Verify key is in the range [1, p-1]. */
  1199. if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1)
  1200. return -EINVAL;
  1201. if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1)
  1202. return -EINVAL;
  1203. /* Check 3: Verify that y^2 == (x^3 + a·x + b) mod p */
  1204. vli_mod_square_fast(yy, pk->y, curve->p, pk->ndigits); /* y^2 */
  1205. vli_mod_square_fast(xxx, pk->x, curve->p, pk->ndigits); /* x^2 */
  1206. vli_mod_mult_fast(xxx, xxx, pk->x, curve->p, pk->ndigits); /* x^3 */
  1207. vli_mod_mult_fast(w, curve->a, pk->x, curve->p, pk->ndigits); /* a·x */
  1208. vli_mod_add(w, w, curve->b, curve->p, pk->ndigits); /* a·x + b */
  1209. vli_mod_add(w, w, xxx, curve->p, pk->ndigits); /* x^3 + a·x + b */
  1210. if (vli_cmp(yy, w, pk->ndigits) != 0) /* Equation */
  1211. return -EINVAL;
  1212. return 0;
  1213. }
  1214. EXPORT_SYMBOL(ecc_is_pubkey_valid_partial);
  1215. /* SP800-56A section 5.6.2.3.3 full verification */
  1216. int ecc_is_pubkey_valid_full(const struct ecc_curve *curve,
  1217. struct ecc_point *pk)
  1218. {
  1219. struct ecc_point *nQ;
  1220. /* Checks 1 through 3 */
  1221. int ret = ecc_is_pubkey_valid_partial(curve, pk);
  1222. if (ret)
  1223. return ret;
  1224. /* Check 4: Verify that nQ is the zero point. */
  1225. nQ = ecc_alloc_point(pk->ndigits);
  1226. if (!nQ)
  1227. return -ENOMEM;
  1228. ecc_point_mult(nQ, pk, curve->n, NULL, curve, pk->ndigits);
  1229. if (!ecc_point_is_zero(nQ))
  1230. ret = -EINVAL;
  1231. ecc_free_point(nQ);
  1232. return ret;
  1233. }
  1234. EXPORT_SYMBOL(ecc_is_pubkey_valid_full);
  1235. int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
  1236. const u64 *private_key, const u64 *public_key,
  1237. u64 *secret)
  1238. {
  1239. int ret = 0;
  1240. struct ecc_point *product, *pk;
  1241. u64 priv[ECC_MAX_DIGITS];
  1242. u64 rand_z[ECC_MAX_DIGITS];
  1243. unsigned int nbytes;
  1244. const struct ecc_curve *curve = ecc_get_curve(curve_id);
  1245. if (!private_key || !public_key || !curve ||
  1246. ndigits > ARRAY_SIZE(priv) || ndigits > ARRAY_SIZE(rand_z)) {
  1247. ret = -EINVAL;
  1248. goto out;
  1249. }
  1250. nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
  1251. get_random_bytes(rand_z, nbytes);
  1252. pk = ecc_alloc_point(ndigits);
  1253. if (!pk) {
  1254. ret = -ENOMEM;
  1255. goto out;
  1256. }
  1257. ecc_swap_digits(public_key, pk->x, ndigits);
  1258. ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
  1259. ret = ecc_is_pubkey_valid_partial(curve, pk);
  1260. if (ret)
  1261. goto err_alloc_product;
  1262. ecc_swap_digits(private_key, priv, ndigits);
  1263. product = ecc_alloc_point(ndigits);
  1264. if (!product) {
  1265. ret = -ENOMEM;
  1266. goto err_alloc_product;
  1267. }
  1268. ecc_point_mult(product, pk, priv, rand_z, curve, ndigits);
  1269. if (ecc_point_is_zero(product)) {
  1270. ret = -EFAULT;
  1271. goto err_validity;
  1272. }
  1273. ecc_swap_digits(product->x, secret, ndigits);
  1274. err_validity:
  1275. memzero_explicit(priv, sizeof(priv));
  1276. memzero_explicit(rand_z, sizeof(rand_z));
  1277. ecc_free_point(product);
  1278. err_alloc_product:
  1279. ecc_free_point(pk);
  1280. out:
  1281. return ret;
  1282. }
  1283. EXPORT_SYMBOL(crypto_ecdh_shared_secret);
  1284. MODULE_LICENSE("Dual BSD/GPL");