Debian Codegraph @

src/external/curve25519_ref.c

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