ge_low_mem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /* ge_low_mem.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL 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
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* Based from Daniel Beer's public domain work. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #include <wolfssl/wolfcrypt/settings.h>
  26. #ifdef HAVE_ED25519
  27. #ifdef ED25519_SMALL /* use slower code that takes less memory */
  28. #include <wolfssl/wolfcrypt/ge_operations.h>
  29. #include <wolfssl/wolfcrypt/error-crypt.h>
  30. #ifdef NO_INLINE
  31. #include <wolfssl/wolfcrypt/misc.h>
  32. #else
  33. #define WOLFSSL_MISC_INCLUDED
  34. #include <wolfcrypt/src/misc.c>
  35. #endif
  36. void ed25519_smult(ge_p3 *r, const ge_p3 *a, const byte *e);
  37. void ed25519_add(ge_p3 *r, const ge_p3 *a, const ge_p3 *b);
  38. void ed25519_double(ge_p3 *r, const ge_p3 *a);
  39. static const byte ed25519_order[F25519_SIZE] = {
  40. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  41. 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  42. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10
  44. };
  45. /*Arithmetic modulo the group order mod = 2^252 +
  46. 27742317777372353535851937790883648493 =
  47. 7237005577332262213973186563042994240857116359379907606001950938285454250989 */
  48. static const word32 mod[32] = {
  49. 0xED,0xD3,0xF5,0x5C,0x1A,0x63,0x12,0x58,0xD6,0x9C,0xF7,0xA2,0xDE,0xF9,
  50. 0xDE,0x14,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51. 0x00,0x00,0x00,0x10
  52. };
  53. static const word32 mu[33] = {
  54. 0x1B,0x13,0x2C,0x0A,0xA3,0xE5,0x9C,0xED,0xA7,0x29,0x63,0x08,0x5D,0x21,
  55. 0x06,0x21,0xEB,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  56. 0xFF,0xFF,0xFF,0xFF,0x0F
  57. };
  58. int ge_compress_key(byte* out, const byte* xIn, const byte* yIn,
  59. word32 keySz)
  60. {
  61. byte tmp[F25519_SIZE];
  62. byte parity;
  63. byte pt[32];
  64. int i;
  65. lm_copy(tmp, xIn);
  66. parity = (tmp[0] & 1) << 7;
  67. lm_copy(pt, yIn);
  68. pt[31] |= parity;
  69. for(i = 0; i < 32; i++) {
  70. out[32-i-1] = pt[i];
  71. }
  72. (void)keySz;
  73. return 0;
  74. }
  75. static word32 lt(word32 a,word32 b) /* 16-bit inputs */
  76. {
  77. word32 x = a;
  78. x -= (unsigned int) b; /* 0..65535: no; 4294901761..4294967295: yes */
  79. x >>= 31; /* 0: no; 1: yes */
  80. return x;
  81. }
  82. /* Reduce coefficients of r before calling reduce_add_sub */
  83. static void reduce_add_sub(word32 *r)
  84. {
  85. word32 pb = 0;
  86. word32 b;
  87. word32 mask;
  88. int i;
  89. unsigned char t[32];
  90. for(i=0;i<32;i++)
  91. {
  92. pb += mod[i];
  93. b = lt(r[i],pb);
  94. t[i] = r[i]-pb+(b<<8);
  95. pb = b;
  96. }
  97. mask = b - 1;
  98. for(i=0;i<32;i++)
  99. r[i] ^= mask & (r[i] ^ t[i]);
  100. }
  101. /* Reduce coefficients of x before calling barrett_reduce */
  102. static void barrett_reduce(word32* r, word32 x[64])
  103. {
  104. /* See HAC, Alg. 14.42 */
  105. int i,j;
  106. word32 q2[66];
  107. word32 *q3 = q2 + 33;
  108. word32 r1[33];
  109. word32 r2[33];
  110. word32 carry;
  111. word32 pb = 0;
  112. word32 b;
  113. for (i = 0;i < 66;++i) q2[i] = 0;
  114. for (i = 0;i < 33;++i) r2[i] = 0;
  115. for(i=0;i<33;i++)
  116. for(j=0;j<33;j++)
  117. if(i+j >= 31) q2[i+j] += mu[i]*x[j+31];
  118. carry = q2[31] >> 8;
  119. q2[32] += carry;
  120. carry = q2[32] >> 8;
  121. q2[33] += carry;
  122. for(i=0;i<33;i++)r1[i] = x[i];
  123. for(i=0;i<32;i++)
  124. for(j=0;j<33;j++)
  125. if(i+j < 33) r2[i+j] += mod[i]*q3[j];
  126. for(i=0;i<32;i++)
  127. {
  128. carry = r2[i] >> 8;
  129. r2[i+1] += carry;
  130. r2[i] &= 0xff;
  131. }
  132. for(i=0;i<32;i++)
  133. {
  134. pb += r2[i];
  135. b = lt(r1[i],pb);
  136. r[i] = r1[i]-pb+(b<<8);
  137. pb = b;
  138. }
  139. /* XXX: Can it really happen that r<0?, See HAC, Alg 14.42, Step 3
  140. * r is an unsigned type.
  141. * If so: Handle it here!
  142. */
  143. reduce_add_sub(r);
  144. reduce_add_sub(r);
  145. }
  146. void sc_reduce(unsigned char *x)
  147. {
  148. int i;
  149. word32 t[64];
  150. word32 r[32];
  151. for(i=0;i<64;i++) t[i] = x[i];
  152. barrett_reduce(r, t);
  153. for(i=0;i<32;i++) x[i] = (r[i] & 0xFF);
  154. }
  155. void sc_muladd(byte* out, const byte* a, const byte* b, const byte* c)
  156. {
  157. byte s[32];
  158. byte e[64];
  159. XMEMSET(e, 0, sizeof(e));
  160. XMEMCPY(e, b, 32);
  161. /* Obtain e */
  162. sc_reduce(e);
  163. /* Compute s = ze + k */
  164. fprime_mul(s, a, e, ed25519_order);
  165. fprime_add(s, c, ed25519_order);
  166. XMEMCPY(out, s, 32);
  167. }
  168. /* Base point is (numbers wrapped):
  169. *
  170. * x = 151122213495354007725011514095885315114
  171. * 54012693041857206046113283949847762202
  172. * y = 463168356949264781694283940034751631413
  173. * 07993866256225615783033603165251855960
  174. *
  175. * y is derived by transforming the original Montgomery base (u=9). x
  176. * is the corresponding positive coordinate for the new curve equation.
  177. * t is x*y.
  178. */
  179. const ge_p3 ed25519_base = {
  180. {
  181. 0x1a, 0xd5, 0x25, 0x8f, 0x60, 0x2d, 0x56, 0xc9,
  182. 0xb2, 0xa7, 0x25, 0x95, 0x60, 0xc7, 0x2c, 0x69,
  183. 0x5c, 0xdc, 0xd6, 0xfd, 0x31, 0xe2, 0xa4, 0xc0,
  184. 0xfe, 0x53, 0x6e, 0xcd, 0xd3, 0x36, 0x69, 0x21
  185. },
  186. {
  187. 0x58, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  188. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  189. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
  190. 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66
  191. },
  192. {1, 0},
  193. {
  194. 0xa3, 0xdd, 0xb7, 0xa5, 0xb3, 0x8a, 0xde, 0x6d,
  195. 0xf5, 0x52, 0x51, 0x77, 0x80, 0x9f, 0xf0, 0x20,
  196. 0x7d, 0xe3, 0xab, 0x64, 0x8e, 0x4e, 0xea, 0x66,
  197. 0x65, 0x76, 0x8b, 0xd7, 0x0f, 0x5f, 0x87, 0x67
  198. },
  199. };
  200. const ge_p3 ed25519_neutral = {
  201. {0},
  202. {1, 0},
  203. {1, 0},
  204. {0},
  205. };
  206. static const byte ed25519_d[F25519_SIZE] = {
  207. 0xa3, 0x78, 0x59, 0x13, 0xca, 0x4d, 0xeb, 0x75,
  208. 0xab, 0xd8, 0x41, 0x41, 0x4d, 0x0a, 0x70, 0x00,
  209. 0x98, 0xe8, 0x79, 0x77, 0x79, 0x40, 0xc7, 0x8c,
  210. 0x73, 0xfe, 0x6f, 0x2b, 0xee, 0x6c, 0x03, 0x52
  211. };
  212. /* k = 2d */
  213. static const byte ed25519_k[F25519_SIZE] = {
  214. 0x59, 0xf1, 0xb2, 0x26, 0x94, 0x9b, 0xd6, 0xeb,
  215. 0x56, 0xb1, 0x83, 0x82, 0x9a, 0x14, 0xe0, 0x00,
  216. 0x30, 0xd1, 0xf3, 0xee, 0xf2, 0x80, 0x8e, 0x19,
  217. 0xe7, 0xfc, 0xdf, 0x56, 0xdc, 0xd9, 0x06, 0x24
  218. };
  219. void ed25519_add(ge_p3 *r,
  220. const ge_p3 *p1, const ge_p3 *p2)
  221. {
  222. /* Explicit formulas database: add-2008-hwcd-3
  223. *
  224. * source 2008 Hisil--Wong--Carter--Dawson,
  225. * http://eprint.iacr.org/2008/522, Section 3.1
  226. * appliesto extended-1
  227. * parameter k
  228. * assume k = 2 d
  229. * compute A = (Y1-X1)(Y2-X2)
  230. * compute B = (Y1+X1)(Y2+X2)
  231. * compute C = T1 k T2
  232. * compute D = Z1 2 Z2
  233. * compute E = B - A
  234. * compute F = D - C
  235. * compute G = D + C
  236. * compute H = B + A
  237. * compute X3 = E F
  238. * compute Y3 = G H
  239. * compute T3 = E H
  240. * compute Z3 = F G
  241. */
  242. byte a[F25519_SIZE];
  243. byte b[F25519_SIZE];
  244. byte c[F25519_SIZE];
  245. byte d[F25519_SIZE];
  246. byte e[F25519_SIZE];
  247. byte f[F25519_SIZE];
  248. byte g[F25519_SIZE];
  249. byte h[F25519_SIZE];
  250. /* A = (Y1-X1)(Y2-X2) */
  251. lm_sub(c, p1->Y, p1->X);
  252. lm_sub(d, p2->Y, p2->X);
  253. fe_mul__distinct(a, c, d);
  254. /* B = (Y1+X1)(Y2+X2) */
  255. lm_add(c, p1->Y, p1->X);
  256. lm_add(d, p2->Y, p2->X);
  257. fe_mul__distinct(b, c, d);
  258. /* C = T1 k T2 */
  259. fe_mul__distinct(d, p1->T, p2->T);
  260. fe_mul__distinct(c, d, ed25519_k);
  261. /* D = Z1 2 Z2 */
  262. fe_mul__distinct(d, p1->Z, p2->Z);
  263. lm_add(d, d, d);
  264. /* E = B - A */
  265. lm_sub(e, b, a);
  266. /* F = D - C */
  267. lm_sub(f, d, c);
  268. /* G = D + C */
  269. lm_add(g, d, c);
  270. /* H = B + A */
  271. lm_add(h, b, a);
  272. /* X3 = E F */
  273. fe_mul__distinct(r->X, e, f);
  274. /* Y3 = G H */
  275. fe_mul__distinct(r->Y, g, h);
  276. /* T3 = E H */
  277. fe_mul__distinct(r->T, e, h);
  278. /* Z3 = F G */
  279. fe_mul__distinct(r->Z, f, g);
  280. }
  281. void ed25519_double(ge_p3 *r, const ge_p3 *p)
  282. {
  283. /* Explicit formulas database: dbl-2008-hwcd
  284. *
  285. * source 2008 Hisil--Wong--Carter--Dawson,
  286. * http://eprint.iacr.org/2008/522, Section 3.3
  287. * compute A = X1^2
  288. * compute B = Y1^2
  289. * compute C = 2 Z1^2
  290. * compute D = a A
  291. * compute E = (X1+Y1)^2-A-B
  292. * compute G = D + B
  293. * compute F = G - C
  294. * compute H = D - B
  295. * compute X3 = E F
  296. * compute Y3 = G H
  297. * compute T3 = E H
  298. * compute Z3 = F G
  299. */
  300. byte a[F25519_SIZE];
  301. byte b[F25519_SIZE];
  302. byte c[F25519_SIZE];
  303. byte e[F25519_SIZE];
  304. byte f[F25519_SIZE];
  305. byte g[F25519_SIZE];
  306. byte h[F25519_SIZE];
  307. /* A = X1^2 */
  308. fe_mul__distinct(a, p->X, p->X);
  309. /* B = Y1^2 */
  310. fe_mul__distinct(b, p->Y, p->Y);
  311. /* C = 2 Z1^2 */
  312. fe_mul__distinct(c, p->Z, p->Z);
  313. lm_add(c, c, c);
  314. /* D = a A (alter sign) */
  315. /* E = (X1+Y1)^2-A-B */
  316. lm_add(f, p->X, p->Y);
  317. fe_mul__distinct(e, f, f);
  318. lm_sub(e, e, a);
  319. lm_sub(e, e, b);
  320. /* G = D + B */
  321. lm_sub(g, b, a);
  322. /* F = G - C */
  323. lm_sub(f, g, c);
  324. /* H = D - B */
  325. lm_neg(h, b);
  326. lm_sub(h, h, a);
  327. /* X3 = E F */
  328. fe_mul__distinct(r->X, e, f);
  329. /* Y3 = G H */
  330. fe_mul__distinct(r->Y, g, h);
  331. /* T3 = E H */
  332. fe_mul__distinct(r->T, e, h);
  333. /* Z3 = F G */
  334. fe_mul__distinct(r->Z, f, g);
  335. }
  336. void ed25519_smult(ge_p3 *r_out, const ge_p3 *p, const byte *e)
  337. {
  338. ge_p3 r;
  339. int i;
  340. XMEMCPY(&r, &ed25519_neutral, sizeof(r));
  341. for (i = 255; i >= 0; i--) {
  342. const byte bit = (e[i >> 3] >> (i & 7)) & 1;
  343. ge_p3 s;
  344. ed25519_double(&r, &r);
  345. ed25519_add(&s, &r, p);
  346. fe_select(r.X, r.X, s.X, bit);
  347. fe_select(r.Y, r.Y, s.Y, bit);
  348. fe_select(r.Z, r.Z, s.Z, bit);
  349. fe_select(r.T, r.T, s.T, bit);
  350. }
  351. XMEMCPY(r_out, &r, sizeof(r));
  352. }
  353. void ge_scalarmult_base(ge_p3 *R,const unsigned char *nonce)
  354. {
  355. ed25519_smult(R, &ed25519_base, nonce);
  356. }
  357. /* pack the point h into array s */
  358. void ge_p3_tobytes(unsigned char *s,const ge_p3 *h)
  359. {
  360. byte x[F25519_SIZE];
  361. byte y[F25519_SIZE];
  362. byte z1[F25519_SIZE];
  363. byte parity;
  364. fe_inv__distinct(z1, h->Z);
  365. fe_mul__distinct(x, h->X, z1);
  366. fe_mul__distinct(y, h->Y, z1);
  367. fe_normalize(x);
  368. fe_normalize(y);
  369. parity = (x[0] & 1) << 7;
  370. lm_copy(s, y);
  371. fe_normalize(s);
  372. s[31] |= parity;
  373. }
  374. /* pack the point h into array s */
  375. void ge_tobytes(unsigned char *s,const ge_p2 *h)
  376. {
  377. byte x[F25519_SIZE];
  378. byte y[F25519_SIZE];
  379. byte z1[F25519_SIZE];
  380. byte parity;
  381. fe_inv__distinct(z1, h->Z);
  382. fe_mul__distinct(x, h->X, z1);
  383. fe_mul__distinct(y, h->Y, z1);
  384. fe_normalize(x);
  385. fe_normalize(y);
  386. parity = (x[0] & 1) << 7;
  387. lm_copy(s, y);
  388. fe_normalize(s);
  389. s[31] |= parity;
  390. }
  391. /*
  392. Test if the public key can be uncompressed and negate it (-X,Y,Z,-T)
  393. return 0 on success
  394. */
  395. int ge_frombytes_negate_vartime(ge_p3 *p,const unsigned char *s)
  396. {
  397. byte parity;
  398. byte x[F25519_SIZE];
  399. byte y[F25519_SIZE];
  400. byte a[F25519_SIZE];
  401. byte b[F25519_SIZE];
  402. byte c[F25519_SIZE];
  403. int ret = 0;
  404. /* unpack the key s */
  405. parity = s[31] >> 7;
  406. lm_copy(y, s);
  407. y[31] &= 127;
  408. fe_mul__distinct(c, y, y);
  409. fe_mul__distinct(b, c, ed25519_d);
  410. lm_add(a, b, f25519_one);
  411. fe_inv__distinct(b, a);
  412. lm_sub(a, c, f25519_one);
  413. fe_mul__distinct(c, a, b);
  414. fe_sqrt(a, c);
  415. lm_neg(b, a);
  416. fe_select(x, a, b, (a[0] ^ parity) & 1);
  417. /* test that x^2 is equal to c */
  418. fe_mul__distinct(a, x, x);
  419. fe_normalize(a);
  420. fe_normalize(c);
  421. ret |= ConstantCompare(a, c, F25519_SIZE);
  422. /* project the key s onto p */
  423. lm_copy(p->X, x);
  424. lm_copy(p->Y, y);
  425. fe_load(p->Z, 1);
  426. fe_mul__distinct(p->T, x, y);
  427. /* negate, the point becomes (-X,Y,Z,-T) */
  428. lm_neg(p->X,p->X);
  429. lm_neg(p->T,p->T);
  430. return ret;
  431. }
  432. int ge_double_scalarmult_vartime(ge_p2* R, const unsigned char *h,
  433. const ge_p3 *inA,const unsigned char *sig)
  434. {
  435. ge_p3 p, A;
  436. int ret = 0;
  437. XMEMCPY(&A, inA, sizeof(ge_p3));
  438. /* find SB */
  439. ed25519_smult(&p, &ed25519_base, sig);
  440. /* find H(R,A,M) * -A */
  441. ed25519_smult(&A, &A, h);
  442. /* SB + -H(R,A,M)A */
  443. ed25519_add(&A, &p, &A);
  444. lm_copy(R->X, A.X);
  445. lm_copy(R->Y, A.Y);
  446. lm_copy(R->Z, A.Z);
  447. return ret;
  448. }
  449. #endif /* ED25519_SMALL */
  450. #endif /* HAVE_ED25519 */