aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

ed25519.c (9175B)


      1 /* see LICENSE file for copyright and license details */
      2 /* field and group arithmetic after tweetnacl (public domain) */
      3 #include <stdint.h>
      4 #include <string.h>
      5 
      6 #include "../ed25519.h"
      7 #include "../sha512.h"
      8 
      9 typedef int64_t gf[16];
     10 
     11 static const gf gf0;
     12 static const gf gf1 = {1};
     13 static const gf D   = {
     14     0x78a3,
     15     0x1359,
     16     0x4dca,
     17     0x75eb,
     18     0xd8ab,
     19     0x4141,
     20     0x0a4d,
     21     0x0070,
     22     0xe898,
     23     0x7779,
     24     0x4079,
     25     0x8cc7,
     26     0xfe73,
     27     0x2b6f,
     28     0x6cee,
     29     0x5203
     30 };
     31 static const gf D2 = {
     32     0xf159,
     33     0x26b2,
     34     0x9b94,
     35     0xebd6,
     36     0xb156,
     37     0x8283,
     38     0x149a,
     39     0x00e0,
     40     0xd130,
     41     0xeef3,
     42     0x80f2,
     43     0x198e,
     44     0xfce7,
     45     0x56df,
     46     0xd9dc,
     47     0x2406
     48 };
     49 static const gf X = {
     50     0xd51a,
     51     0x8f25,
     52     0x2d60,
     53     0xc956,
     54     0xa7b2,
     55     0x9525,
     56     0xc760,
     57     0x692c,
     58     0xdc5c,
     59     0xfdd6,
     60     0xe231,
     61     0xc0a4,
     62     0x53fe,
     63     0xcd6e,
     64     0x36d3,
     65     0x2169
     66 };
     67 static const gf Y = {
     68     0x6658,
     69     0x6666,
     70     0x6666,
     71     0x6666,
     72     0x6666,
     73     0x6666,
     74     0x6666,
     75     0x6666,
     76     0x6666,
     77     0x6666,
     78     0x6666,
     79     0x6666,
     80     0x6666,
     81     0x6666,
     82     0x6666,
     83     0x6666
     84 };
     85 static const gf I = {
     86     0xa0b0,
     87     0x4a0e,
     88     0x1b27,
     89     0xc4ee,
     90     0xe478,
     91     0xad2f,
     92     0x1806,
     93     0x2f43,
     94     0xd7a7,
     95     0x3dfb,
     96     0x0099,
     97     0x2b4d,
     98     0xdf0b,
     99     0x4fc1,
    100     0x2480,
    101     0x2b83
    102 };
    103 static const uint64_t L[32] = {0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7,
    104                                0xa2, 0xde, 0xf9, 0xde, 0x14, 0,    0,    0,    0,    0,    0,
    105                                0,    0,    0,    0,    0,    0,    0,    0,    0,    0x10};
    106 
    107 static int
    108 verify32(const uint8_t *x, const uint8_t *y)
    109 {
    110   uint32_t d = 0;
    111   int      i;
    112 
    113   for (i = 0; i < 32; i++)
    114     d |= x[i] ^ y[i];
    115   return (1 & ((d - 1) >> 8)) - 1;
    116 }
    117 
    118 static void
    119 set25519(gf r, const gf a)
    120 {
    121   int i;
    122 
    123   for (i = 0; i < 16; i++)
    124     r[i] = a[i];
    125 }
    126 
    127 static void
    128 car25519(gf o)
    129 {
    130   int64_t c;
    131   int     i;
    132 
    133   for (i = 0; i < 16; i++) {
    134     o[i] += (int64_t)1 << 16;
    135     c = o[i] >> 16;
    136     o[(i + 1) * (i < 15)] += c - 1 + 37 * (c - 1) * (i == 15);
    137     o[i] -= c * ((int64_t)1 << 16);
    138   }
    139 }
    140 
    141 static void
    142 sel25519(gf p, gf q, int b)
    143 {
    144   int64_t t, c = ~(b - 1);
    145   int     i;
    146 
    147   for (i = 0; i < 16; i++) {
    148     t = c & (p[i] ^ q[i]);
    149     p[i] ^= t;
    150     q[i] ^= t;
    151   }
    152 }
    153 
    154 static void
    155 pack25519(uint8_t *o, const gf n)
    156 {
    157   int i, j, b;
    158   gf  m, t;
    159 
    160   for (i = 0; i < 16; i++)
    161     t[i] = n[i];
    162   car25519(t);
    163   car25519(t);
    164   car25519(t);
    165   for (j = 0; j < 2; j++) {
    166     m[0] = t[0] - 0xffed;
    167     for (i = 1; i < 15; i++) {
    168       m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1);
    169       m[i - 1] &= 0xffff;
    170     }
    171     m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1);
    172     b     = (m[15] >> 16) & 1;
    173     m[14] &= 0xffff;
    174     sel25519(t, m, 1 - b);
    175   }
    176   for (i = 0; i < 16; i++) {
    177     o[2 * i]     = t[i] & 0xff;
    178     o[2 * i + 1] = t[i] >> 8;
    179   }
    180 }
    181 
    182 static int
    183 neq25519(const gf a, const gf b)
    184 {
    185   uint8_t c[32], d[32];
    186 
    187   pack25519(c, a);
    188   pack25519(d, b);
    189   return verify32(c, d);
    190 }
    191 
    192 static uint8_t
    193 par25519(const gf a)
    194 {
    195   uint8_t d[32];
    196 
    197   pack25519(d, a);
    198   return d[0] & 1;
    199 }
    200 
    201 static void
    202 unpack25519(gf o, const uint8_t *n)
    203 {
    204   int i;
    205 
    206   for (i = 0; i < 16; i++)
    207     o[i] = n[2 * i] + ((int64_t)n[2 * i + 1] << 8);
    208   o[15] &= 0x7fff;
    209 }
    210 
    211 static void
    212 A(gf o, const gf a, const gf b)
    213 {
    214   int i;
    215 
    216   for (i = 0; i < 16; i++)
    217     o[i] = a[i] + b[i];
    218 }
    219 
    220 static void
    221 Z(gf o, const gf a, const gf b)
    222 {
    223   int i;
    224 
    225   for (i = 0; i < 16; i++)
    226     o[i] = a[i] - b[i];
    227 }
    228 
    229 static void
    230 M(gf o, const gf a, const gf b)
    231 {
    232   int64_t t[31];
    233   int     i, j;
    234 
    235   for (i = 0; i < 31; i++)
    236     t[i] = 0;
    237   for (i = 0; i < 16; i++)
    238     for (j = 0; j < 16; j++)
    239       t[i + j] += a[i] * b[j];
    240   for (i = 0; i < 15; i++)
    241     t[i] += 38 * t[i + 16];
    242   for (i = 0; i < 16; i++)
    243     o[i] = t[i];
    244   car25519(o);
    245   car25519(o);
    246 }
    247 
    248 static void
    249 S(gf o, const gf a)
    250 {
    251   M(o, a, a);
    252 }
    253 
    254 static void
    255 inv25519(gf o, const gf i)
    256 {
    257   gf  c;
    258   int a;
    259 
    260   for (a = 0; a < 16; a++)
    261     c[a] = i[a];
    262   for (a = 253; a >= 0; a--) {
    263     S(c, c);
    264     if (a != 2 && a != 4)
    265       M(c, c, i);
    266   }
    267   for (a = 0; a < 16; a++)
    268     o[a] = c[a];
    269 }
    270 
    271 static void
    272 pow2523(gf o, const gf i)
    273 {
    274   gf  c;
    275   int a;
    276 
    277   for (a = 0; a < 16; a++)
    278     c[a] = i[a];
    279   for (a = 250; a >= 0; a--) {
    280     S(c, c);
    281     if (a != 1)
    282       M(c, c, i);
    283   }
    284   for (a = 0; a < 16; a++)
    285     o[a] = c[a];
    286 }
    287 
    288 static void
    289 add(gf p[4], gf q[4])
    290 {
    291   gf a, b, c, d, t, e, f, g, h;
    292 
    293   Z(a, p[1], p[0]);
    294   Z(t, q[1], q[0]);
    295   M(a, a, t);
    296   A(b, p[0], p[1]);
    297   A(t, q[0], q[1]);
    298   M(b, b, t);
    299   M(c, p[3], q[3]);
    300   M(c, c, D2);
    301   M(d, p[2], q[2]);
    302   A(d, d, d);
    303   Z(e, b, a);
    304   Z(f, d, c);
    305   A(g, d, c);
    306   A(h, b, a);
    307   M(p[0], e, f);
    308   M(p[1], h, g);
    309   M(p[2], g, f);
    310   M(p[3], e, h);
    311 }
    312 
    313 static void
    314 cswap(gf p[4], gf q[4], uint8_t b)
    315 {
    316   int i;
    317 
    318   for (i = 0; i < 4; i++)
    319     sel25519(p[i], q[i], b);
    320 }
    321 
    322 static void
    323 pack(uint8_t *r, gf p[4])
    324 {
    325   gf tx, ty, zi;
    326 
    327   inv25519(zi, p[2]);
    328   M(tx, p[0], zi);
    329   M(ty, p[1], zi);
    330   pack25519(r, ty);
    331   r[31] ^= par25519(tx) << 7;
    332 }
    333 
    334 static void
    335 scalarmult(gf p[4], gf q[4], const uint8_t *s)
    336 {
    337   int i;
    338 
    339   set25519(p[0], gf0);
    340   set25519(p[1], gf1);
    341   set25519(p[2], gf1);
    342   set25519(p[3], gf0);
    343   for (i = 255; i >= 0; --i) {
    344     uint8_t b = (s[i / 8] >> (i & 7)) & 1;
    345 
    346     cswap(p, q, b);
    347     add(q, p);
    348     add(p, p);
    349     cswap(p, q, b);
    350   }
    351 }
    352 
    353 static void
    354 scalarbase(gf p[4], const uint8_t *s)
    355 {
    356   gf q[4];
    357 
    358   set25519(q[0], X);
    359   set25519(q[1], Y);
    360   set25519(q[2], gf1);
    361   M(q[3], X, Y);
    362   scalarmult(p, q, s);
    363 }
    364 
    365 static void
    366 modL(uint8_t *r, int64_t x[64])
    367 {
    368   int64_t carry;
    369   int     i, j;
    370 
    371   for (i = 63; i >= 32; --i) {
    372     carry = 0;
    373     for (j = i - 32; j < i - 12; ++j) {
    374       x[j] += carry - 16 * x[i] * (int64_t)L[j - (i - 32)];
    375       carry = (x[j] + 128) >> 8;
    376       x[j] -= carry * 256;
    377     }
    378     x[j] += carry;
    379     x[i] = 0;
    380   }
    381   carry = 0;
    382   for (j = 0; j < 32; j++) {
    383     x[j] += carry - (x[31] >> 4) * (int64_t)L[j];
    384     carry = x[j] >> 8;
    385     x[j] &= 255;
    386   }
    387   for (j = 0; j < 32; j++)
    388     x[j] -= carry * (int64_t)L[j];
    389   for (i = 0; i < 32; i++) {
    390     x[i + 1] += x[i] >> 8;
    391     r[i] = x[i] & 255;
    392   }
    393 }
    394 
    395 static void
    396 reduce(uint8_t *r)
    397 {
    398   int64_t x[64];
    399   int     i;
    400 
    401   for (i = 0; i < 64; i++)
    402     x[i] = (uint64_t)r[i];
    403   for (i = 0; i < 64; i++)
    404     r[i] = 0;
    405   modL(r, x);
    406 }
    407 
    408 static int
    409 unpackneg(gf r[4], const uint8_t p[32])
    410 {
    411   gf t, chk, num, den, den2, den4, den6;
    412 
    413   set25519(r[2], gf1);
    414   unpack25519(r[1], p);
    415   S(num, r[1]);
    416   M(den, num, D);
    417   Z(num, num, r[2]);
    418   A(den, r[2], den);
    419   S(den2, den);
    420   S(den4, den2);
    421   M(den6, den4, den2);
    422   M(t, den6, num);
    423   M(t, t, den);
    424   pow2523(t, t);
    425   M(t, t, num);
    426   M(t, t, den);
    427   M(t, t, den);
    428   M(r[0], t, den);
    429   S(chk, r[0]);
    430   M(chk, chk, den);
    431   if (neq25519(chk, num))
    432     M(r[0], r[0], I);
    433   S(chk, r[0]);
    434   M(chk, chk, den);
    435   if (neq25519(chk, num))
    436     return -1;
    437   if (par25519(r[0]) == (p[31] >> 7))
    438     Z(r[0], gf0, r[0]);
    439   M(r[3], r[0], r[1]);
    440   return 0;
    441 }
    442 
    443 /* the secret scalar and the nonce prefix: sha512 of the seed, clamped */
    444 static void
    445 expand(uint8_t d[64], const uint8_t seed[ED25519_SEED])
    446 {
    447   struct sha512 h;
    448 
    449   sha512_init(&h);
    450   sha512_update(&h, seed, ED25519_SEED);
    451   sha512_sum(&h, d);
    452   d[0] &= 248;
    453   d[31] &= 127;
    454   d[31] |= 64;
    455 }
    456 
    457 void
    458 ed25519_keypair(
    459     uint8_t pk[ED25519_PUBLIC], uint8_t sk[ED25519_SECRET], const uint8_t seed[ED25519_SEED]
    460 )
    461 {
    462   uint8_t d[64];
    463   gf      p[4];
    464 
    465   expand(d, seed);
    466   scalarbase(p, d);
    467   pack(pk, p);
    468   memcpy(sk, seed, ED25519_SEED);
    469   memcpy(sk + ED25519_SEED, pk, ED25519_PUBLIC);
    470   memset(d, 0, sizeof(d));
    471 }
    472 
    473 /* sha512 of a, b and m, reduced mod L */
    474 static void
    475 hram(
    476     uint8_t        out[64],
    477     const uint8_t *a,
    478     size_t         na,
    479     const uint8_t *b,
    480     size_t         nb,
    481     const uint8_t *m,
    482     size_t         n
    483 )
    484 {
    485   struct sha512 h;
    486 
    487   sha512_init(&h);
    488   sha512_update(&h, a, na);
    489   if (nb)
    490     sha512_update(&h, b, nb);
    491   sha512_update(&h, m, n);
    492   sha512_sum(&h, out);
    493   reduce(out);
    494 }
    495 
    496 void
    497 ed25519_sign(uint8_t sig[ED25519_SIG], const uint8_t *m, size_t n, const uint8_t sk[ED25519_SECRET])
    498 {
    499   uint8_t d[64], r[64], h[64];
    500   int64_t x[64];
    501   gf      p[4];
    502   int     i, j;
    503 
    504   expand(d, sk);
    505   hram(r, d + 32, 32, NULL, 0, m, n);
    506   scalarbase(p, r);
    507   pack(sig, p);
    508   hram(h, sig, 32, sk + ED25519_SEED, ED25519_PUBLIC, m, n);
    509   for (i = 0; i < 64; i++)
    510     x[i] = 0;
    511   for (i = 0; i < 32; i++)
    512     x[i] = (uint64_t)r[i];
    513   for (i = 0; i < 32; i++)
    514     for (j = 0; j < 32; j++)
    515       x[i + j] += h[i] * (uint64_t)d[j];
    516   modL(sig + 32, x);
    517   memset(d, 0, sizeof(d));
    518   memset(r, 0, sizeof(r));
    519 }
    520 
    521 int
    522 ed25519_verify(
    523     const uint8_t sig[ED25519_SIG], const uint8_t *m, size_t n, const uint8_t pk[ED25519_PUBLIC]
    524 )
    525 {
    526   uint8_t h[64], t[32];
    527   gf      p[4], q[4];
    528   int     i;
    529 
    530   /* s must be below L, or the signature is malleable */
    531   for (i = 31; i >= 0; i--) {
    532     if (sig[32 + i] < L[i])
    533       break;
    534     if (sig[32 + i] > L[i] || i == 0)
    535       return -1;
    536   }
    537   if (unpackneg(q, pk))
    538     return -1;
    539   hram(h, sig, 32, pk, ED25519_PUBLIC, m, n);
    540   scalarmult(p, q, h);
    541   scalarbase(q, sig + 32);
    542   add(p, q);
    543   pack(t, p);
    544   return verify32(sig, t) ? -1 : 0;
    545 }