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