Home | History | Annotate | Line # | Download | only in dist
      1 /* $OpenBSD: smult_curve25519_ref.c,v 1.2 2013/11/02 22:02:14 markus Exp $ */
      2 /*
      3 version 20081011
      4 Matthew Dempsky
      5 Public domain.
      6 Derived from public domain code by D. J. Bernstein.
      7 */
      8 
      9 #include "includes.h"
     10 __RCSID("$NetBSD: smult_curve25519_ref.c,v 1.5 2017/04/18 18:41:46 christos Exp $");
     11 
     12 int crypto_scalarmult_curve25519(unsigned char *, const unsigned char *, const unsigned char *);
     13 
     14 static void add(unsigned int out[32],const unsigned int a[32],const unsigned int b[32])
     15 {
     16   unsigned int j;
     17   unsigned int u;
     18   u = 0;
     19   for (j = 0;j < 31;++j) { u += a[j] + b[j]; out[j] = u & 255; u >>= 8; }
     20   u += a[31] + b[31]; out[31] = u;
     21 }
     22 
     23 static void sub(unsigned int out[32],const unsigned int a[32],const unsigned int b[32])
     24 {
     25   unsigned int j;
     26   unsigned int u;
     27   u = 218;
     28   for (j = 0;j < 31;++j) {
     29     u += a[j] + 65280 - b[j];
     30     out[j] = u & 255;
     31     u >>= 8;
     32   }
     33   u += a[31] - b[31];
     34   out[31] = u;
     35 }
     36 
     37 static void squeeze(unsigned int a[32])
     38 {
     39   unsigned int j;
     40   unsigned int u;
     41   u = 0;
     42   for (j = 0;j < 31;++j) { u += a[j]; a[j] = u & 255; u >>= 8; }
     43   u += a[31]; a[31] = u & 127;
     44   u = 19 * (u >> 7);
     45   for (j = 0;j < 31;++j) { u += a[j]; a[j] = u & 255; u >>= 8; }
     46   u += a[31]; a[31] = u;
     47 }
     48 
     49 static const unsigned int minusp[32] = {
     50  19, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128
     51 } ;
     52 
     53 static void freeze(unsigned int a[32])
     54 {
     55   unsigned int aorig[32];
     56   unsigned int j;
     57   unsigned int negative;
     58 
     59   for (j = 0;j < 32;++j) aorig[j] = a[j];
     60   add(a,a,minusp);
     61   negative = -((a[31] >> 7) & 1);
     62   for (j = 0;j < 32;++j) a[j] ^= negative & (aorig[j] ^ a[j]);
     63 }
     64 
     65 static void mult(unsigned int out[32],const unsigned int a[32],const unsigned int b[32])
     66 {
     67   unsigned int i;
     68   unsigned int j;
     69   unsigned int u;
     70 
     71   for (i = 0;i < 32;++i) {
     72     u = 0;
     73     for (j = 0;j <= i;++j) u += a[j] * b[i - j];
     74     for (j = i + 1;j < 32;++j) u += 38 * a[j] * b[i + 32 - j];
     75     out[i] = u;
     76   }
     77   squeeze(out);
     78 }
     79 
     80 static void mult121665(unsigned int out[32],const unsigned int a[32])
     81 {
     82   unsigned int j;
     83   unsigned int u;
     84 
     85   u = 0;
     86   for (j = 0;j < 31;++j) { u += 121665 * a[j]; out[j] = u & 255; u >>= 8; }
     87   u += 121665 * a[31]; out[31] = u & 127;
     88   u = 19 * (u >> 7);
     89   for (j = 0;j < 31;++j) { u += out[j]; out[j] = u & 255; u >>= 8; }
     90   u += out[j]; out[j] = u;
     91 }
     92 
     93 static void square(unsigned int out[32],const unsigned int a[32])
     94 {
     95   unsigned int i;
     96   unsigned int j;
     97   unsigned int u;
     98 
     99   for (i = 0;i < 32;++i) {
    100     u = 0;
    101     for (j = 0;j < i - j;++j) u += a[j] * a[i - j];
    102     for (j = i + 1;j < i + 32 - j;++j) u += 38 * a[j] * a[i + 32 - j];
    103     u *= 2;
    104     if ((i & 1) == 0) {
    105       u += a[i / 2] * a[i / 2];
    106       u += 38 * a[i / 2 + 16] * a[i / 2 + 16];
    107     }
    108     out[i] = u;
    109   }
    110   squeeze(out);
    111 }
    112 
    113 static void select(unsigned int p[64],unsigned int q[64],const unsigned int r[64],const unsigned int s[64],unsigned int b)
    114 {
    115   unsigned int j;
    116   unsigned int t;
    117   unsigned int bminus1;
    118 
    119   bminus1 = b - 1;
    120   for (j = 0;j < 64;++j) {
    121     t = bminus1 & (r[j] ^ s[j]);
    122     p[j] = s[j] ^ t;
    123     q[j] = r[j] ^ t;
    124   }
    125 }
    126 
    127 static void mainloop(unsigned int work[64],const unsigned char e[32])
    128 {
    129   unsigned int xzm1[64];
    130   unsigned int xzm[64];
    131   unsigned int xzmb[64];
    132   unsigned int xzm1b[64];
    133   unsigned int xznb[64];
    134   unsigned int xzn1b[64];
    135   unsigned int a0[64];
    136   unsigned int a1[64];
    137   unsigned int b0[64];
    138   unsigned int b1[64];
    139   unsigned int c1[64];
    140   unsigned int r[32];
    141   unsigned int s[32];
    142   unsigned int t[32];
    143   unsigned int u[32];
    144   unsigned int j;
    145   unsigned int b;
    146   int pos;
    147 
    148   for (j = 0;j < 32;++j) xzm1[j] = work[j];
    149   xzm1[32] = 1;
    150   for (j = 33;j < 64;++j) xzm1[j] = 0;
    151 
    152   xzm[0] = 1;
    153   for (j = 1;j < 64;++j) xzm[j] = 0;
    154 
    155   for (pos = 254;pos >= 0;--pos) {
    156     b = e[pos / 8] >> (pos & 7);
    157     b &= 1;
    158     select(xzmb,xzm1b,xzm,xzm1,b);
    159     add(a0,xzmb,xzmb + 32);
    160     sub(a0 + 32,xzmb,xzmb + 32);
    161     add(a1,xzm1b,xzm1b + 32);
    162     sub(a1 + 32,xzm1b,xzm1b + 32);
    163     square(b0,a0);
    164     square(b0 + 32,a0 + 32);
    165     mult(b1,a1,a0 + 32);
    166     mult(b1 + 32,a1 + 32,a0);
    167     add(c1,b1,b1 + 32);
    168     sub(c1 + 32,b1,b1 + 32);
    169     square(r,c1 + 32);
    170     sub(s,b0,b0 + 32);
    171     mult121665(t,s);
    172     add(u,t,b0);
    173     mult(xznb,b0,b0 + 32);
    174     mult(xznb + 32,s,u);
    175     square(xzn1b,c1);
    176     mult(xzn1b + 32,r,work);
    177     select(xzm,xzm1,xznb,xzn1b,b);
    178   }
    179 
    180   for (j = 0;j < 64;++j) work[j] = xzm[j];
    181 }
    182 
    183 static void recip(unsigned int out[32],const unsigned int z[32])
    184 {
    185   unsigned int z2[32];
    186   unsigned int z9[32];
    187   unsigned int z11[32];
    188   unsigned int z2_5_0[32];
    189   unsigned int z2_10_0[32];
    190   unsigned int z2_20_0[32];
    191   unsigned int z2_50_0[32];
    192   unsigned int z2_100_0[32];
    193   unsigned int t0[32];
    194   unsigned int t1[32];
    195   int i;
    196 
    197   /* 2 */ square(z2,z);
    198   /* 4 */ square(t1,z2);
    199   /* 8 */ square(t0,t1);
    200   /* 9 */ mult(z9,t0,z);
    201   /* 11 */ mult(z11,z9,z2);
    202   /* 22 */ square(t0,z11);
    203   /* 2^5 - 2^0 = 31 */ mult(z2_5_0,t0,z9);
    204 
    205   /* 2^6 - 2^1 */ square(t0,z2_5_0);
    206   /* 2^7 - 2^2 */ square(t1,t0);
    207   /* 2^8 - 2^3 */ square(t0,t1);
    208   /* 2^9 - 2^4 */ square(t1,t0);
    209   /* 2^10 - 2^5 */ square(t0,t1);
    210   /* 2^10 - 2^0 */ mult(z2_10_0,t0,z2_5_0);
    211 
    212   /* 2^11 - 2^1 */ square(t0,z2_10_0);
    213   /* 2^12 - 2^2 */ square(t1,t0);
    214   /* 2^20 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t0,t1); square(t1,t0); }
    215   /* 2^20 - 2^0 */ mult(z2_20_0,t1,z2_10_0);
    216 
    217   /* 2^21 - 2^1 */ square(t0,z2_20_0);
    218   /* 2^22 - 2^2 */ square(t1,t0);
    219   /* 2^40 - 2^20 */ for (i = 2;i < 20;i += 2) { square(t0,t1); square(t1,t0); }
    220   /* 2^40 - 2^0 */ mult(t0,t1,z2_20_0);
    221 
    222   /* 2^41 - 2^1 */ square(t1,t0);
    223   /* 2^42 - 2^2 */ square(t0,t1);
    224   /* 2^50 - 2^10 */ for (i = 2;i < 10;i += 2) { square(t1,t0); square(t0,t1); }
    225   /* 2^50 - 2^0 */ mult(z2_50_0,t0,z2_10_0);
    226 
    227   /* 2^51 - 2^1 */ square(t0,z2_50_0);
    228   /* 2^52 - 2^2 */ square(t1,t0);
    229   /* 2^100 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
    230   /* 2^100 - 2^0 */ mult(z2_100_0,t1,z2_50_0);
    231 
    232   /* 2^101 - 2^1 */ square(t1,z2_100_0);
    233   /* 2^102 - 2^2 */ square(t0,t1);
    234   /* 2^200 - 2^100 */ for (i = 2;i < 100;i += 2) { square(t1,t0); square(t0,t1); }
    235   /* 2^200 - 2^0 */ mult(t1,t0,z2_100_0);
    236 
    237   /* 2^201 - 2^1 */ square(t0,t1);
    238   /* 2^202 - 2^2 */ square(t1,t0);
    239   /* 2^250 - 2^50 */ for (i = 2;i < 50;i += 2) { square(t0,t1); square(t1,t0); }
    240   /* 2^250 - 2^0 */ mult(t0,t1,z2_50_0);
    241 
    242   /* 2^251 - 2^1 */ square(t1,t0);
    243   /* 2^252 - 2^2 */ square(t0,t1);
    244   /* 2^253 - 2^3 */ square(t1,t0);
    245   /* 2^254 - 2^4 */ square(t0,t1);
    246   /* 2^255 - 2^5 */ square(t1,t0);
    247   /* 2^255 - 21 */ mult(out,t1,z11);
    248 }
    249 
    250 int crypto_scalarmult_curve25519(unsigned char *q,
    251   const unsigned char *n,
    252   const unsigned char *p)
    253 {
    254   unsigned int work[96];
    255   unsigned char e[32];
    256   unsigned int i;
    257   for (i = 0;i < 32;++i) e[i] = n[i];
    258   e[0] &= 248;
    259   e[31] &= 127;
    260   e[31] |= 64;
    261   for (i = 0;i < 32;++i) work[i] = p[i];
    262   mainloop(work,e);
    263   recip(work + 32,work + 32);
    264   mult(work + 64,work,work + 32);
    265   freeze(work + 64);
    266   for (i = 0;i < 32;++i) q[i] = work[64 + i];
    267   return 0;
    268 }
    269