ecc.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2018 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #define _GNU_SOURCE
  26. #include <stdio.h>
  27. #include <stdint.h>
  28. #include <stdbool.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <errno.h>
  32. #include "ecc.h"
  33. #include "ecc-private.h"
  34. #include "random.h"
  35. #include "useful.h"
  36. #include "private.h"
  37. #include "missing.h"
  38. /*
  39. * RFC 5114 - Section 2.6 256-bit Random ECP Group
  40. */
  41. #define P256_CURVE_P { 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull, \
  42. 0x0000000000000000ull, 0xFFFFFFFF00000001ull }
  43. #define P256_CURVE_GX { 0xF4A13945D898C296ull, 0x77037D812DEB33A0ull, \
  44. 0xF8BCE6E563A440F2ull, 0x6B17D1F2E12C4247ull }
  45. #define P256_CURVE_GY { 0xCBB6406837BF51F5ull, 0x2BCE33576B315ECEull, \
  46. 0x8EE7EB4A7C0F9E16ull, 0x4FE342E2FE1A7F9Bull }
  47. #define P256_CURVE_N { 0xF3B9CAC2FC632551ull, 0xBCE6FAADA7179E84ull, \
  48. 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull }
  49. #define P256_CURVE_B { 0x3BCE3C3E27D2604Bull, 0x651D06B0CC53B0F6ull, \
  50. 0xB3EBBD55769886BCull, 0x5AC635D8AA3A93E7ull }
  51. static const struct l_ecc_curve p256 = {
  52. .name = "secp256r1",
  53. .ike_group = 19,
  54. .tls_group = 23,
  55. .ndigits = 4,
  56. .g = {
  57. .x = P256_CURVE_GX,
  58. .y = P256_CURVE_GY,
  59. .curve = &p256
  60. },
  61. .p = P256_CURVE_P,
  62. .n = P256_CURVE_N,
  63. .b = P256_CURVE_B,
  64. .z = -10,
  65. };
  66. /*
  67. * RFC 5114 - Section 2.7 384-bit Random ECP Group
  68. */
  69. #define P384_CURVE_P { 0x00000000FFFFFFFFull, 0xFFFFFFFF00000000ull, \
  70. 0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull, \
  71. 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFFull }
  72. #define P384_CURVE_GX { 0x3A545E3872760AB7ull, 0x5502F25DBF55296Cull, \
  73. 0x59F741E082542A38ull, 0x6E1D3B628BA79B98ull, \
  74. 0x8EB1C71EF320AD74ull, 0xAA87CA22BE8B0537ull }
  75. #define P384_CURVE_GY { 0x7A431D7C90EA0E5Full, 0x0A60B1CE1D7E819Dull, \
  76. 0xE9DA3113B5F0B8C0ull, 0xF8F41DBD289A147Cull, \
  77. 0x5D9E98BF9292DC29ull, 0x3617DE4A96262C6Full }
  78. #define P384_CURVE_N { 0xECEC196ACCC52973ull, 0x581A0DB248B0A77Aull, \
  79. 0xC7634D81F4372DDFull, 0xFFFFFFFFFFFFFFFFull, \
  80. 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFFull }
  81. #define P384_CURVE_B { 0x2A85C8EDD3EC2AEFull, 0xC656398D8A2ED19Dull, \
  82. 0x0314088F5013875Aull, 0x181D9C6EFE814112ull, \
  83. 0x988E056BE3F82D19ull, 0xB3312FA7E23EE7E4ull }
  84. static const struct l_ecc_curve p384 = {
  85. .name = "secp384r1",
  86. .ike_group = 20,
  87. .tls_group = 24,
  88. .ndigits = 6,
  89. .g = {
  90. .x = P384_CURVE_GX,
  91. .y = P384_CURVE_GY,
  92. .curve = &p384
  93. },
  94. .p = P384_CURVE_P,
  95. .n = P384_CURVE_N,
  96. .b = P384_CURVE_B,
  97. .z = -12,
  98. };
  99. static const struct l_ecc_curve *curves[] = {
  100. &p384,
  101. &p256,
  102. };
  103. /* Returns supported IKE groups, sorted by the highest effective key size */
  104. LIB_EXPORT const unsigned int *l_ecc_supported_ike_groups(void)
  105. {
  106. static unsigned int supported_ike_groups[L_ARRAY_SIZE(curves) + 1];
  107. static bool ike_first = true;
  108. if (ike_first) {
  109. unsigned int i;
  110. for (i = 0; i < L_ARRAY_SIZE(curves); i++)
  111. supported_ike_groups[i] = curves[i]->ike_group;
  112. supported_ike_groups[i] = 0;
  113. ike_first = false;
  114. }
  115. return supported_ike_groups;
  116. }
  117. /* Returns supported TLS groups, sorted by the highest effective key size */
  118. LIB_EXPORT const unsigned int *l_ecc_supported_tls_groups(void)
  119. {
  120. static unsigned int supported_tls_groups[L_ARRAY_SIZE(curves) + 1];
  121. static bool tls_first = true;
  122. if (tls_first) {
  123. unsigned int i;
  124. for (i = 0; i < L_ARRAY_SIZE(curves); i++)
  125. supported_tls_groups[i] = curves[i]->tls_group;
  126. supported_tls_groups[i] = 0;
  127. tls_first = false;
  128. }
  129. return supported_tls_groups;
  130. }
  131. LIB_EXPORT const struct l_ecc_curve *l_ecc_curve_from_name(const char *name)
  132. {
  133. int i;
  134. if (unlikely(!name))
  135. return NULL;
  136. for (i = 0; curves[i]; i++) {
  137. if (!strcmp(curves[i]->name, name))
  138. return curves[i];
  139. }
  140. return NULL;
  141. }
  142. LIB_EXPORT const struct l_ecc_curve *l_ecc_curve_from_ike_group(
  143. unsigned int group)
  144. {
  145. unsigned int i;
  146. for (i = 0; i < L_ARRAY_SIZE(curves); i++) {
  147. if (curves[i]->ike_group == group)
  148. return curves[i];
  149. }
  150. return NULL;
  151. }
  152. LIB_EXPORT const struct l_ecc_curve *l_ecc_curve_from_tls_group(
  153. unsigned int group)
  154. {
  155. unsigned int i;
  156. for (i = 0; i < L_ARRAY_SIZE(curves); i++) {
  157. if (curves[i]->tls_group == group)
  158. return curves[i];
  159. }
  160. return NULL;
  161. }
  162. LIB_EXPORT const char *l_ecc_curve_get_name(const struct l_ecc_curve *curve)
  163. {
  164. if (unlikely(!curve))
  165. return NULL;
  166. return curve->name;
  167. }
  168. LIB_EXPORT unsigned int l_ecc_curve_get_ike_group(
  169. const struct l_ecc_curve *curve)
  170. {
  171. if (unlikely(!curve))
  172. return 0;
  173. return curve->ike_group;
  174. }
  175. LIB_EXPORT unsigned int l_ecc_curve_get_tls_group(
  176. const struct l_ecc_curve *curve)
  177. {
  178. if (unlikely(!curve))
  179. return 0;
  180. return curve->tls_group;
  181. }
  182. LIB_EXPORT struct l_ecc_scalar *l_ecc_curve_get_order(
  183. const struct l_ecc_curve *curve)
  184. {
  185. return _ecc_constant_new(curve, curve->n, curve->ndigits * 8);
  186. }
  187. LIB_EXPORT struct l_ecc_scalar *l_ecc_curve_get_prime(
  188. const struct l_ecc_curve *curve)
  189. {
  190. if (unlikely(!curve))
  191. return NULL;
  192. return _ecc_constant_new(curve, curve->p, curve->ndigits * 8);
  193. }
  194. LIB_EXPORT size_t l_ecc_curve_get_scalar_bytes(const struct l_ecc_curve *curve)
  195. {
  196. if (unlikely(!curve))
  197. return 0;
  198. return curve->ndigits * 8;
  199. }
  200. static bool ecc_valid_point(struct l_ecc_point *point)
  201. {
  202. const struct l_ecc_curve *curve = point->curve;
  203. uint64_t tmp1[L_ECC_MAX_DIGITS];
  204. uint64_t tmp2[L_ECC_MAX_DIGITS];
  205. uint64_t _3[L_ECC_MAX_DIGITS] = { 3 }; /* -a = 3 */
  206. unsigned int ndigits = curve->ndigits;
  207. /* The point at infinity is invalid. */
  208. if (_ecc_point_is_zero(point))
  209. return false;
  210. /* x and y must be smaller than p. */
  211. if (_vli_cmp(curve->p, point->x, ndigits) != 1 ||
  212. _vli_cmp(curve->p, point->y, ndigits) != 1)
  213. return false;
  214. /* Computes result = y^2. */
  215. _vli_mod_square_fast(tmp1, point->y, curve->p, ndigits);
  216. /* Computes result = x^3 + ax + b. result must not overlap x. */
  217. /* r = x^2 */
  218. _vli_mod_square_fast(tmp2, point->x, curve->p, ndigits);
  219. /* r = x^2 - 3 */
  220. _vli_mod_sub(tmp2, tmp2, _3, curve->p, ndigits);
  221. /* r = x^3 - 3x */
  222. _vli_mod_mult_fast(tmp2, tmp2, point->x, curve->p, ndigits);
  223. /* r = x^3 - 3x + b */
  224. _vli_mod_add(tmp2, tmp2, curve->b, curve->p, ndigits);
  225. /* Make sure that y^2 == x^3 + ax + b */
  226. return (_vli_cmp(tmp1, tmp2, ndigits) == 0);
  227. }
  228. void _ecc_be2native(uint64_t *dest, const uint64_t *bytes,
  229. unsigned int ndigits)
  230. {
  231. unsigned int i;
  232. uint64_t tmp[2 * L_ECC_MAX_DIGITS];
  233. for (i = 0; i < ndigits; i++)
  234. tmp[ndigits - 1 - i] = l_get_be64(&bytes[i]);
  235. memcpy(dest, tmp, ndigits * 8);
  236. }
  237. void _ecc_native2be(uint64_t *dest, const uint64_t *native,
  238. unsigned int ndigits)
  239. {
  240. unsigned int i;
  241. uint64_t tmp[L_ECC_MAX_DIGITS];
  242. for (i = 0; i < ndigits; i++)
  243. l_put_be64(native[ndigits - 1 - i], &tmp[i]);
  244. memcpy(dest, tmp, ndigits * 8);
  245. }
  246. static void ecc_compute_y_sqr(const struct l_ecc_curve *curve,
  247. uint64_t *y_sqr, const uint64_t *x)
  248. {
  249. uint64_t sum[L_ECC_MAX_DIGITS] = { 0 };
  250. uint64_t tmp[L_ECC_MAX_DIGITS] = { 0 };
  251. uint64_t _3[L_ECC_MAX_DIGITS] = { 3ull }; /* -a = 3 */
  252. /* x^3 */
  253. _vli_mod_square_fast(sum, x, curve->p, curve->ndigits);
  254. _vli_mod_mult_fast(sum, sum, x, curve->p, curve->ndigits);
  255. /* x^3 - ax */
  256. _vli_mod_mult_fast(tmp, _3, x, curve->p, curve->ndigits);
  257. _vli_mod_sub(sum, sum, tmp, curve->p, curve->ndigits);
  258. /* x^3 - ax + b */
  259. _vli_mod_add(sum, sum, curve->b, curve->p, curve->ndigits);
  260. memcpy(y_sqr, sum, curve->ndigits * 8);
  261. }
  262. /*
  263. * Compute sqrt(y^2)
  264. * Since our prime p satisfies p = 3 (mod 4), we can say:
  265. *
  266. * y = (y^2)^((p + 1) / 4)
  267. *
  268. * This avoids the need for a square root function.
  269. */
  270. static void ecc_compute_sqrt(const struct l_ecc_curve *curve,
  271. uint64_t *y, const uint64_t *y_sqr)
  272. {
  273. uint64_t expo[L_ECC_MAX_DIGITS];
  274. uint64_t one[L_ECC_MAX_DIGITS] = { 1ull };
  275. memcpy(expo, curve->p, curve->ndigits * 8);
  276. /* (p + 1) / 4 == (p >> 2) + 1 */
  277. _vli_rshift1(expo, curve->ndigits);
  278. _vli_rshift1(expo, curve->ndigits);
  279. _vli_mod_add(expo, expo, one, curve->p, curve->ndigits);
  280. /* sum ^ ((p + 1) / 4) */
  281. _vli_mod_exp(y, y_sqr, expo, curve->p, curve->ndigits);
  282. }
  283. bool _ecc_compute_y(const struct l_ecc_curve *curve, uint64_t *y,
  284. const uint64_t *x)
  285. {
  286. uint64_t sum[L_ECC_MAX_DIGITS] = { 0 };
  287. uint64_t check[L_ECC_MAX_DIGITS] = { 0 };
  288. /* y = sqrt(x^3 + ax + b) (mod p) */
  289. ecc_compute_y_sqr(curve, sum, x);
  290. ecc_compute_sqrt(curve, y, sum);
  291. /* square y to ensure we have a correct value */
  292. _vli_mod_mult_fast(check, y, y, curve->p, curve->ndigits);
  293. if (_vli_cmp(check, sum, curve->ndigits) != 0)
  294. return false;
  295. return true;
  296. }
  297. /*
  298. * IETF - Compact representation of an elliptic curve point:
  299. * https://tools.ietf.org/id/draft-jivsov-ecc-compact-00.xml
  300. *
  301. * "min(y,p-y) can be calculated with the help of the pre-calculated value
  302. * p2=(p-1)/2. min(y,p-y) is y if y<p2 and p-y otherwise."
  303. */
  304. void _ecc_calculate_p2(const struct l_ecc_curve *curve, uint64_t *p2)
  305. {
  306. uint64_t one[L_ECC_MAX_DIGITS] = { 1 };
  307. _vli_mod_sub(p2, curve->p, one, curve->p, curve->ndigits);
  308. _vli_rshift1(p2, curve->ndigits);
  309. }
  310. /*
  311. * IETF draft-jivsov-ecc-compact-00 Section 4.1
  312. * Encoding and decoding of an elliptic curve point
  313. * ...
  314. * Decoding:
  315. * Given the compact representation of Q, return canonical representation
  316. * of Q=(x,y) as follows:
  317. * 1. y' = sqrt( x^3 + a*x + b ), where y'>0
  318. * 2. y = min(y',p-y')
  319. * 3. Q=(x,y) is the canonical representation of the point
  320. */
  321. static bool decode_point(const struct l_ecc_curve *curve, uint64_t *x,
  322. struct l_ecc_point *point)
  323. {
  324. uint64_t y_min[L_ECC_MAX_DIGITS];
  325. uint64_t p2[L_ECC_MAX_DIGITS];
  326. if (!_ecc_compute_y(curve, y_min, (uint64_t *)x))
  327. return false;
  328. _ecc_calculate_p2(curve, p2);
  329. if (_vli_cmp(y_min, p2, curve->ndigits) >= 0)
  330. _vli_mod_sub(point->y, curve->p, y_min,
  331. curve->p, curve->ndigits);
  332. else
  333. memcpy(point->y, y_min, curve->ndigits * 8);
  334. memcpy(point->x, x, curve->ndigits * 8);
  335. return true;
  336. }
  337. /* (rx, ry) = (px, py) + (qx, qy) */
  338. void _ecc_point_add(struct l_ecc_point *ret, const struct l_ecc_point *p,
  339. const struct l_ecc_point *q,
  340. const uint64_t *curve_prime)
  341. {
  342. /*
  343. * s = (py - qy)/(px - qx)
  344. *
  345. * rx = s^2 - px - qx
  346. * ry = s(px - rx) - py
  347. */
  348. uint64_t s[L_ECC_MAX_DIGITS];
  349. uint64_t kp1[L_ECC_MAX_DIGITS];
  350. uint64_t kp2[L_ECC_MAX_DIGITS];
  351. uint64_t resx[L_ECC_MAX_DIGITS];
  352. uint64_t resy[L_ECC_MAX_DIGITS];
  353. unsigned int ndigits = p->curve->ndigits;
  354. memset(s, 0, ndigits * 8);
  355. /* kp1 = py - qy */
  356. _vli_mod_sub(kp1, q->y, p->y, curve_prime, ndigits);
  357. /* kp2 = px - qx */
  358. _vli_mod_sub(kp2, q->x, p->x, curve_prime, ndigits);
  359. /* s = kp1/kp2 */
  360. _vli_mod_inv(kp2, kp2, curve_prime, ndigits);
  361. _vli_mod_mult_fast(s, kp1, kp2, curve_prime, ndigits);
  362. /* rx = s^2 - px - qx */
  363. _vli_mod_mult_fast(kp1, s, s, curve_prime, ndigits);
  364. _vli_mod_sub(kp1, kp1, p->x, curve_prime, ndigits);
  365. _vli_mod_sub(resx, kp1, q->x, curve_prime, ndigits);
  366. /* ry = s(px - rx) - py */
  367. _vli_mod_sub(kp1, p->x, resx, curve_prime, ndigits);
  368. _vli_mod_mult_fast(kp1, s, kp1, curve_prime, ndigits);
  369. _vli_mod_sub(resy, kp1, p->y, curve_prime, ndigits);
  370. memcpy(ret->x, resx, ndigits * 8);
  371. memcpy(ret->y, resy, ndigits * 8);
  372. }
  373. /* result = (base ^ exp) % p */
  374. void _vli_mod_exp(uint64_t *result, const uint64_t *base, const uint64_t *exp,
  375. const uint64_t *mod, unsigned int ndigits)
  376. {
  377. unsigned int i;
  378. int bit;
  379. uint64_t n[L_ECC_MAX_DIGITS];
  380. uint64_t r[L_ECC_MAX_DIGITS] = { 1 };
  381. memcpy(n, base, ndigits * 8);
  382. for (i = 0; i < ndigits; i++) {
  383. for (bit = 0; bit < 64; bit++) {
  384. uint64_t tmp[L_ECC_MAX_DIGITS];
  385. if (exp[i] & (1ull << bit)) {
  386. _vli_mod_mult_fast(tmp, r, n, mod, ndigits);
  387. memcpy(r, tmp, ndigits * 8);
  388. }
  389. _vli_mod_mult_fast(tmp, n, n, mod, ndigits);
  390. memcpy(n, tmp, ndigits * 8);
  391. }
  392. }
  393. memcpy(result, r, ndigits * 8);
  394. }
  395. __attribute__((noinline)) static int vli_equal(const uint64_t *a,
  396. const uint64_t *b,
  397. unsigned int ndigits)
  398. {
  399. uint64_t diff = 0;
  400. unsigned int i;
  401. for (i = 0; i < ndigits; i++) {
  402. diff |= a[i] ^ b[i];
  403. __asm__ ("" : "=r" (diff) : "0" (diff));
  404. }
  405. return (~diff & (diff - 1)) >> 63;
  406. }
  407. int _vli_legendre(uint64_t *val, const uint64_t *p, unsigned int ndigits)
  408. {
  409. uint64_t tmp[L_ECC_MAX_DIGITS];
  410. uint64_t exp[L_ECC_MAX_DIGITS];
  411. uint64_t _1[L_ECC_MAX_DIGITS] = { 1ull };
  412. uint64_t _0[L_ECC_MAX_DIGITS] = { 0 };
  413. /* check that val ^ ((p - 1) / 2) == [1, 0 or -1] */
  414. _vli_sub(exp, p, _1, ndigits);
  415. _vli_rshift1(exp, ndigits);
  416. _vli_mod_exp(tmp, val, exp, p, ndigits);
  417. if (_vli_cmp(tmp, _1, ndigits) == 0)
  418. return 1;
  419. if (_vli_cmp(tmp, _0, ndigits) == 0)
  420. return 0;
  421. return -1;
  422. }
  423. bool _vli_is_zero_or_one(const uint64_t *vli, unsigned int ndigits)
  424. {
  425. uint64_t _1[L_ECC_MAX_DIGITS] = { 1ull };
  426. int ret;
  427. ret = secure_select(vli_equal(vli, _1, ndigits), true, false);
  428. ret = secure_select(l_secure_memeq(vli, ndigits * 8, 0), true, ret);
  429. return ret;
  430. }
  431. LIB_EXPORT struct l_ecc_point *l_ecc_point_new(const struct l_ecc_curve *curve)
  432. {
  433. struct l_ecc_point *p = l_new(struct l_ecc_point, 1);
  434. p->curve = curve;
  435. return p;
  436. }
  437. LIB_EXPORT struct l_ecc_point *l_ecc_point_from_data(
  438. const struct l_ecc_curve *curve,
  439. enum l_ecc_point_type type,
  440. const void *data, size_t len)
  441. {
  442. struct l_ecc_point *p;
  443. size_t bytes = curve->ndigits * 8;
  444. if (!data)
  445. return NULL;
  446. /* In all cases there should be an X coordinate in data */
  447. if (len < bytes)
  448. return NULL;
  449. p = l_ecc_point_new(curve);
  450. _ecc_be2native(p->x, (void *) data, curve->ndigits);
  451. switch (type) {
  452. case L_ECC_POINT_TYPE_COMPLIANT:
  453. if (!decode_point(curve, p->x, p))
  454. goto failed;
  455. break;
  456. case L_ECC_POINT_TYPE_COMPRESSED_BIT0:
  457. if (!_ecc_compute_y(curve, p->y, p->x))
  458. goto failed;
  459. if (!(p->y[0] & 1))
  460. _vli_mod_sub(p->y, curve->p, p->y, curve->p,
  461. curve->ndigits);
  462. break;
  463. case L_ECC_POINT_TYPE_COMPRESSED_BIT1:
  464. if (!_ecc_compute_y(curve, p->y, p->x))
  465. goto failed;
  466. if (p->y[0] & 1)
  467. _vli_mod_sub(p->y, curve->p, p->y, curve->p,
  468. curve->ndigits);
  469. break;
  470. case L_ECC_POINT_TYPE_FULL:
  471. if (len < bytes * 2)
  472. goto failed;
  473. _ecc_be2native(p->y, (void *) data + bytes, curve->ndigits);
  474. if (!ecc_valid_point(p))
  475. goto failed;
  476. break;
  477. }
  478. return p;
  479. failed:
  480. l_free(p);
  481. return NULL;
  482. }
  483. LIB_EXPORT struct l_ecc_point *l_ecc_point_from_sswu(
  484. const struct l_ecc_scalar *u)
  485. {
  486. const struct l_ecc_curve *curve = u->curve;
  487. unsigned int ndigits = curve->ndigits;
  488. uint64_t z[L_ECC_MAX_DIGITS] = { abs(curve->z) };
  489. uint64_t _3[L_ECC_MAX_DIGITS] = { 3ull }; /* -a = 3 */
  490. uint64_t u2z[L_ECC_MAX_DIGITS];
  491. uint64_t t1[L_ECC_MAX_DIGITS];
  492. uint64_t t2[L_ECC_MAX_DIGITS];
  493. uint64_t m[L_ECC_MAX_DIGITS];
  494. uint64_t t[L_ECC_MAX_DIGITS];
  495. uint64_t x1l[L_ECC_MAX_DIGITS];
  496. uint64_t x1r[L_ECC_MAX_DIGITS];
  497. uint64_t x1[L_ECC_MAX_DIGITS];
  498. uint64_t gx1[L_ECC_MAX_DIGITS];
  499. uint64_t x2[L_ECC_MAX_DIGITS];
  500. uint64_t gx2[L_ECC_MAX_DIGITS];
  501. /* reuse m/t/x1l,x1r, they are unused by the time x/v/y/p-y is needed */
  502. uint64_t *x = m;
  503. uint64_t *v = t;
  504. uint64_t *yl = x1l;
  505. uint64_t *yr = x1r;
  506. bool l;
  507. struct l_ecc_point *P;
  508. /*
  509. * m = (z^2 * u^4 + z * u^2) modulo p
  510. * u2z = u^2 * z
  511. * t2 = u2z^2
  512. * m = t2 - u2z since for all our curves z is negative
  513. */
  514. _vli_mod_square_fast(u2z, u->c, curve->p, ndigits);
  515. _vli_mod_mult_fast(u2z, u2z, z, curve->p, ndigits);
  516. _vli_mod_square_fast(t2, u2z, curve->p, ndigits);
  517. _vli_mod_sub(m, t2, u2z, curve->p, ndigits);
  518. /*
  519. * l = CEQ(m, 0)
  520. * t = inv0(m) where inv0(x) is calculated as x^(p-2) modulo p
  521. */
  522. l = l_secure_memeq(m, sizeof(m), 0);
  523. memset(t2, 0, sizeof(t2));
  524. t2[0] = 2ull;
  525. _vli_mod_sub(t1, curve->p, t2, curve->p, ndigits);
  526. _vli_mod_exp(t, m, t1, curve->p, ndigits);
  527. /* Calculate: b / z*a, both z and a are negative */
  528. _vli_mod_mult_fast(t1, z, _3, curve->p, ndigits);
  529. _vli_mod_inv(t1, t1, curve->p, ndigits);
  530. _vli_mod_mult_fast(x1l, curve->b, t1, curve->p, ndigits);
  531. /* t = 1 + t */
  532. memset(t2, 0, sizeof(t2));
  533. t2[0] = 1ull;
  534. _vli_mod_add(t, t, t2, curve->p, ndigits);
  535. /* t1 = 1 / a */
  536. _vli_mod_inv(t1, _3, curve->p, ndigits);
  537. /* x1r = b * t1 * t */
  538. _vli_mod_mult_fast(x1r, curve->b, t1, curve->p, ndigits);
  539. _vli_mod_mult_fast(x1r, x1r, t, curve->p, ndigits);
  540. /* x1 = CSEL(l, (b / (z*a) modulo p), ((-b/a) * (1 + t)) modulo p) */
  541. l_secure_select(l, x1l, x1r, x1, ndigits * 8);
  542. /* gx1 = (x1^3 + a*x1 + b) modulo p */
  543. ecc_compute_y_sqr(curve, gx1, x1);
  544. /* x2 = (z*u^2*x1) modulo p, z is negative, hence the second op */
  545. _vli_mod_mult_fast(x2, u2z, x1, curve->p, ndigits);
  546. _vli_mod_sub(x2, curve->p, x2, curve->p, ndigits);
  547. /* gx2 = (x2^3 + a*x2 + b) modulo p */
  548. ecc_compute_y_sqr(curve, gx2, x2);
  549. /*
  550. * l = gx1 is a quadratic residue modulo p
  551. * x is a quadratic residue if x^((p-1)/2) modulo p is zero or one
  552. */
  553. _vli_mod_sub(t1, curve->p, t2, curve->p, ndigits);
  554. _vli_rshift1(t1, ndigits);
  555. _vli_mod_exp(t2, gx1, t1, curve->p, ndigits);
  556. l = _vli_is_zero_or_one(t2, ndigits);
  557. /* v = CSEL(l, gx1, gx2) */
  558. l_secure_select(l, gx1, gx2, v, ndigits * 8);
  559. /* x = CSEL(l, x1, x2) */
  560. l_secure_select(l, x1, x2, x, ndigits * 8);
  561. /* y = sqrt(v) */
  562. ecc_compute_sqrt(curve, yl, v);
  563. /* l = CEQ(LSB(u), LSB(y)) */
  564. l = !((u->c[0] & 1ull) ^ (yl[0] & 1ull));
  565. /* p - y */
  566. _vli_mod_sub(yr, curve->p, yl, curve->p, ndigits);
  567. /* P = CSEL(l, (x,y), (x, p-y)) */
  568. P = l_ecc_point_new(curve);
  569. memcpy(P->x, x, ndigits * 8);
  570. l_secure_select(l, yl, yr, P->y, ndigits * 8);
  571. return P;
  572. }
  573. LIB_EXPORT struct l_ecc_point *l_ecc_point_clone(const struct l_ecc_point *p)
  574. {
  575. if (!p)
  576. return NULL;
  577. return l_memdup(p, sizeof(*p));
  578. }
  579. LIB_EXPORT const struct l_ecc_curve *l_ecc_point_get_curve(
  580. const struct l_ecc_point *p)
  581. {
  582. if (!p)
  583. return NULL;
  584. return p->curve;
  585. }
  586. LIB_EXPORT ssize_t l_ecc_point_get_x(const struct l_ecc_point *p, void *x,
  587. size_t xlen)
  588. {
  589. if (xlen < p->curve->ndigits * 8)
  590. return -EMSGSIZE;
  591. _ecc_native2be(x, p->x, p->curve->ndigits);
  592. return p->curve->ndigits * 8;
  593. }
  594. LIB_EXPORT ssize_t l_ecc_point_get_y(const struct l_ecc_point *p, void *y,
  595. size_t ylen)
  596. {
  597. if (ylen < p->curve->ndigits * 8)
  598. return -EMSGSIZE;
  599. _ecc_native2be(y, p->y, p->curve->ndigits);
  600. return p->curve->ndigits * 8;
  601. }
  602. LIB_EXPORT ssize_t l_ecc_point_get_data(const struct l_ecc_point *p, void *buf,
  603. size_t len)
  604. {
  605. if (len < (p->curve->ndigits * 8) * 2)
  606. return -EMSGSIZE;
  607. _ecc_native2be(buf, (uint64_t *) p->x, p->curve->ndigits);
  608. _ecc_native2be(buf + (p->curve->ndigits * 8), (uint64_t *) p->y,
  609. p->curve->ndigits);
  610. return (p->curve->ndigits * 8) * 2;
  611. }
  612. LIB_EXPORT void l_ecc_point_free(struct l_ecc_point *p)
  613. {
  614. if (unlikely(!p))
  615. return;
  616. explicit_bzero(p->x, p->curve->ndigits * 8);
  617. explicit_bzero(p->y, p->curve->ndigits * 8);
  618. l_free(p);
  619. }
  620. struct l_ecc_scalar *_ecc_constant_new(const struct l_ecc_curve *curve,
  621. const void *buf, size_t len)
  622. {
  623. struct l_ecc_scalar *c;
  624. if (unlikely(!curve))
  625. return NULL;
  626. if (buf && len != curve->ndigits * 8)
  627. return NULL;
  628. c = l_new(struct l_ecc_scalar, 1);
  629. c->curve = curve;
  630. if (buf)
  631. memcpy(c->c, buf, len);
  632. return c;
  633. }
  634. LIB_EXPORT struct l_ecc_scalar *l_ecc_scalar_new(
  635. const struct l_ecc_curve *curve,
  636. const void *buf, size_t len)
  637. {
  638. struct l_ecc_scalar *c;
  639. c = _ecc_constant_new(curve, NULL, 0);
  640. if (!c)
  641. return NULL;
  642. if (!buf)
  643. return c;
  644. _ecc_be2native(c->c, buf, curve->ndigits);
  645. if (!_vli_is_zero_or_one(c->c, curve->ndigits) &&
  646. secure_memcmp_64(curve->n, c->c, curve->ndigits) > 0)
  647. return c;
  648. l_ecc_scalar_free(c);
  649. return NULL;
  650. }
  651. /*
  652. * Build a scalar = value modulo p where p is the prime number for a given
  653. * curve. bytes can contain a numer with up to 2x number of digits as the
  654. * curve. This is used in Hash to Curve calculations.
  655. */
  656. LIB_EXPORT struct l_ecc_scalar *l_ecc_scalar_new_modp(
  657. const struct l_ecc_curve *curve,
  658. const void *bytes, size_t len)
  659. {
  660. struct l_ecc_scalar *c;
  661. uint64_t tmp[2 * L_ECC_MAX_DIGITS];
  662. unsigned int ndigits = len / 8;
  663. if (!bytes)
  664. return NULL;
  665. if (len % 8)
  666. return NULL;
  667. if (ndigits > curve->ndigits * 2)
  668. return NULL;
  669. c = _ecc_constant_new(curve, NULL, 0);
  670. if (!c)
  671. return NULL;
  672. memset(tmp, 0, sizeof(tmp));
  673. _ecc_be2native(tmp, bytes, ndigits);
  674. _vli_mmod_fast(c->c, tmp, curve->p, curve->ndigits);
  675. if (!_vli_is_zero_or_one(c->c, curve->ndigits) &&
  676. secure_memcmp_64(curve->n, c->c, curve->ndigits) > 0)
  677. return c;
  678. l_ecc_scalar_free(c);
  679. return NULL;
  680. }
  681. /*
  682. * Takes a buffer of the same size as the curve and scales it to a range
  683. * 1..n using value = (value mod (n - 1)) + 1. For the curves we support
  684. * this can be done using a subtraction operation due to the size of n
  685. */
  686. LIB_EXPORT struct l_ecc_scalar *l_ecc_scalar_new_reduced_1_to_n(
  687. const struct l_ecc_curve *curve,
  688. const void *buf, size_t len)
  689. {
  690. uint64_t _1[L_ECC_MAX_DIGITS] = { 1ull };
  691. uint64_t tmp[L_ECC_MAX_DIGITS];
  692. struct l_ecc_scalar *c;
  693. if (!buf)
  694. return NULL;
  695. if (len != curve->ndigits * 8)
  696. return NULL;
  697. c = _ecc_constant_new(curve, NULL, 0);
  698. if (!c)
  699. return NULL;
  700. _vli_sub(tmp, curve->n, _1, curve->ndigits);
  701. _ecc_be2native(c->c, buf, curve->ndigits);
  702. if (_vli_cmp(c->c, tmp, curve->ndigits) >= 0)
  703. _vli_sub(c->c, c->c, tmp, curve->ndigits);
  704. _vli_add(c->c, c->c, _1, curve->ndigits);
  705. return c;
  706. }
  707. LIB_EXPORT struct l_ecc_scalar *l_ecc_scalar_new_random(
  708. const struct l_ecc_curve *curve)
  709. {
  710. uint64_t r[L_ECC_MAX_DIGITS];
  711. l_getrandom(r, curve->ndigits * 8);
  712. while (_vli_cmp(r, curve->p, curve->ndigits) > 0 ||
  713. _vli_cmp(r, curve->n, curve->ndigits) > 0 ||
  714. _vli_is_zero_or_one(r, curve->ndigits))
  715. l_getrandom(r, curve->ndigits * 8);
  716. return _ecc_constant_new(curve, r, curve->ndigits * 8);
  717. }
  718. LIB_EXPORT ssize_t l_ecc_scalar_get_data(const struct l_ecc_scalar *c,
  719. void *buf, size_t len)
  720. {
  721. if (len < c->curve->ndigits * 8)
  722. return -EMSGSIZE;
  723. _ecc_native2be(buf, (uint64_t *) c->c, c->curve->ndigits);
  724. return c->curve->ndigits * 8;
  725. }
  726. LIB_EXPORT void l_ecc_scalar_free(struct l_ecc_scalar *c)
  727. {
  728. if (unlikely(!c))
  729. return;
  730. explicit_bzero(c->c, c->curve->ndigits * 8);
  731. l_free(c);
  732. }
  733. LIB_EXPORT bool l_ecc_scalar_add(struct l_ecc_scalar *ret,
  734. const struct l_ecc_scalar *a,
  735. const struct l_ecc_scalar *b,
  736. const struct l_ecc_scalar *mod)
  737. {
  738. if (unlikely(!ret || !a || !b || !mod))
  739. return false;
  740. _vli_mod_add(ret->c, a->c, b->c, mod->c, a->curve->ndigits);
  741. return true;
  742. }
  743. LIB_EXPORT bool l_ecc_point_multiply(struct l_ecc_point *ret,
  744. const struct l_ecc_scalar *scalar,
  745. const struct l_ecc_point *point)
  746. {
  747. if (unlikely(!ret || !scalar || !point))
  748. return false;
  749. _ecc_point_mult(ret, point, scalar->c, NULL, scalar->curve->p);
  750. return true;
  751. }
  752. LIB_EXPORT bool l_ecc_point_add(struct l_ecc_point *ret,
  753. const struct l_ecc_point *a,
  754. const struct l_ecc_point *b)
  755. {
  756. if (unlikely(!ret || !a || !b))
  757. return false;
  758. _ecc_point_add(ret, a, b, a->curve->p);
  759. return true;
  760. }
  761. LIB_EXPORT bool l_ecc_point_inverse(struct l_ecc_point *p)
  762. {
  763. if (unlikely(!p))
  764. return false;
  765. _vli_mod_sub(p->y, p->curve->p, p->y, p->curve->p, p->curve->ndigits);
  766. return true;
  767. }
  768. LIB_EXPORT bool l_ecc_scalar_multiply(struct l_ecc_scalar *ret,
  769. const struct l_ecc_scalar *a,
  770. const struct l_ecc_scalar *b)
  771. {
  772. if (unlikely(!ret || !a || !b))
  773. return false;
  774. _vli_mod_mult_fast(ret->c, a->c, b->c, a->curve->p, a->curve->ndigits);
  775. return true;
  776. }
  777. LIB_EXPORT int l_ecc_scalar_legendre(struct l_ecc_scalar *value)
  778. {
  779. if (unlikely(!value))
  780. return -1;
  781. return _vli_legendre(value->c, value->curve->p, value->curve->ndigits);
  782. }
  783. LIB_EXPORT bool l_ecc_scalar_sum_x(struct l_ecc_scalar *ret,
  784. const struct l_ecc_scalar *x)
  785. {
  786. if (unlikely(!ret || !x))
  787. return false;
  788. ecc_compute_y_sqr(x->curve, ret->c, x->c);
  789. return true;
  790. }
  791. LIB_EXPORT bool l_ecc_scalars_are_equal(const struct l_ecc_scalar *a,
  792. const struct l_ecc_scalar *b)
  793. {
  794. if (unlikely(!a || !b))
  795. return false;
  796. return (memcmp(a->c, b->c, a->curve->ndigits * 8) == 0);
  797. }
  798. LIB_EXPORT bool l_ecc_points_are_equal(const struct l_ecc_point *a,
  799. const struct l_ecc_point *b)
  800. {
  801. if (unlikely(!a || !b))
  802. return false;
  803. return ((memcmp(a->x, b->x, a->curve->ndigits * 8) == 0) &&
  804. (memcmp(a->y, b->y, a->curve->ndigits * 8) == 0));
  805. }