Coverage Report

Created: 2023-03-26 06:46

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