Coverage Report

Created: 2025-08-03 06:31

/src/dropbear/src/curve25519.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Dropbear - a SSH2 server
3
 * 
4
 * Copyright (c) 2002,2003 Matt Johnston
5
 * All rights reserved.
6
 * 
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 * 
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 * 
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE. */
24
25
#include "includes.h"
26
#include "dbrandom.h"
27
#include "curve25519.h"
28
29
#if DROPBEAR_CURVE25519_DEP || DROPBEAR_ED25519
30
31
/* Modified TweetNaCl version 20140427, a self-contained public-domain C library.
32
 * https://tweetnacl.cr.yp.to/ */
33
34
304M
#define FOR(i,n) for (i = 0;i < n;++i)
35
#define sv static void
36
37
typedef unsigned char u8;
38
typedef unsigned long u32;
39
typedef unsigned long long u64;
40
typedef long long i64;
41
typedef i64 gf[16];
42
43
#if DROPBEAR_CURVE25519_DEP
44
static const gf
45
  _121665 = {0xDB41,1};
46
#endif /* DROPBEAR_CURVE25519_DEP */
47
#if DROPBEAR_ED25519
48
static const gf
49
  gf0,
50
  gf1 = {1},
51
  D2 = {0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406},
52
  X = {0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169},
53
  Y = {0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666};
54
#if DROPBEAR_SIGNKEY_VERIFY
55
static const gf
56
  D = {0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203},
57
  I = {0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83};
58
#endif /* DROPBEAR_SIGNKEY_VERIFY */
59
#endif /* DROPBEAR_ED25519 */
60
61
#if DROPBEAR_ED25519
62
#if DROPBEAR_SIGNKEY_VERIFY
63
static int vn(const u8 *x,const u8 *y,u32 n)
64
0
{
65
0
  u32 i,d = 0;
66
0
  FOR(i,n) d |= x[i]^y[i];
67
0
  return (1 & ((d - 1) >> 8)) - 1;
68
0
}
69
70
static int crypto_verify_32(const u8 *x,const u8 *y)
71
0
{
72
0
  return vn(x,y,32);
73
0
}
74
#endif /* DROPBEAR_SIGNKEY_VERIFY */
75
76
sv set25519(gf r, const gf a)
77
0
{
78
0
  int i;
79
0
  FOR(i,16) r[i]=a[i];
80
0
}
81
#endif /* DROPBEAR_ED25519 */
82
83
sv car25519(gf o)
84
1.50M
{
85
1.50M
  int i;
86
1.50M
  i64 c;
87
24.0M
  FOR(i,16) {
88
24.0M
    o[i]+=(1LL<<16);
89
24.0M
    c=o[i]>>16;
90
24.0M
    o[(i+1)*(i<15)]+=c-1+37*(c-1)*(i==15);
91
24.0M
    o[i]-=((u64)c)<<16;
92
24.0M
  }
93
1.50M
}
94
95
sv sel25519(gf p,gf q,int b)
96
251k
{
97
251k
  i64 t,i,c=~(b-1);
98
4.02M
  FOR(i,16) {
99
4.02M
    t= c&(p[i]^q[i]);
100
4.02M
    p[i]^=t;
101
4.02M
    q[i]^=t;
102
4.02M
  }
103
251k
}
104
105
sv pack25519(u8 *o,const gf n)
106
246
{
107
246
  int i,j,b;
108
246
  gf m,t;
109
3.93k
  FOR(i,16) t[i]=n[i];
110
246
  car25519(t);
111
246
  car25519(t);
112
246
  car25519(t);
113
492
  FOR(j,2) {
114
492
    m[0]=t[0]-0xffed;
115
7.38k
    for(i=1;i<15;i++) {
116
6.88k
      m[i]=t[i]-0xffff-((m[i-1]>>16)&1);
117
6.88k
      m[i-1]&=0xffff;
118
6.88k
    }
119
492
    m[15]=t[15]-0x7fff-((m[14]>>16)&1);
120
492
    b=(m[15]>>16)&1;
121
492
    m[14]&=0xffff;
122
492
    sel25519(t,m,1-b);
123
492
  }
124
3.93k
  FOR(i,16) {
125
3.93k
    o[2*i]=t[i]&0xff;
126
3.93k
    o[2*i+1]=t[i]>>8;
127
3.93k
  }
128
246
}
129
130
#if DROPBEAR_ED25519
131
#if DROPBEAR_SIGNKEY_VERIFY
132
static int neq25519(const gf a, const gf b)
133
0
{
134
0
  u8 c[32],d[32];
135
0
  pack25519(c,a);
136
0
  pack25519(d,b);
137
0
  return crypto_verify_32(c,d);
138
0
}
139
#endif /* DROPBEAR_SIGNKEY_VERIFY */
140
141
static u8 par25519(const gf a)
142
0
{
143
0
  u8 d[32];
144
0
  pack25519(d,a);
145
0
  return d[0]&1;
146
0
}
147
#endif /* DROPBEAR_ED25519 */
148
149
sv unpack25519(gf o, const u8 *n)
150
246
{
151
246
  int i;
152
3.93k
  FOR(i,16) o[i]=n[2*i]+((i64)n[2*i+1]<<8);
153
246
  o[15]&=0x7fff;
154
246
}
155
156
sv A(gf o,const gf a,const gf b)
157
250k
{
158
250k
  int i;
159
4.01M
  FOR(i,16) o[i]=a[i]+b[i];
160
250k
}
161
162
sv Z(gf o,const gf a,const gf b)
163
250k
{
164
250k
  int i;
165
4.01M
  FOR(i,16) o[i]=a[i]-b[i];
166
250k
}
167
168
sv M(gf o,const gf a,const gf b)
169
752k
{
170
752k
  i64 i,j,t[31];
171
23.3M
  FOR(i,31) t[i]=0;
172
192M
  FOR(i,16) FOR(j,16) t[i+j]+=a[i]*b[j];
173
11.2M
  FOR(i,15) t[i]+=38*t[i+16];
174
12.0M
  FOR(i,16) o[i]=t[i];
175
752k
  car25519(o);
176
752k
  car25519(o);
177
752k
}
178
179
sv S(gf o,const gf a)
180
313k
{
181
313k
  M(o,a,a);
182
313k
}
183
184
sv inv25519(gf o,const gf i)
185
246
{
186
246
  gf c;
187
246
  int a;
188
3.93k
  FOR(a,16) c[a]=i[a];
189
62.7k
  for(a=253;a>=0;a--) {
190
62.4k
    S(c,c);
191
62.4k
    if(a!=2&&a!=4) M(c,c,i);
192
62.4k
  }
193
3.93k
  FOR(a,16) o[a]=c[a];
194
246
}
195
196
#if DROPBEAR_ED25519 && DROPBEAR_SIGNKEY_VERIFY
197
sv pow2523(gf o,const gf i)
198
0
{
199
0
  gf c;
200
0
  int a;
201
0
  FOR(a,16) c[a]=i[a];
202
0
  for(a=250;a>=0;a--) {
203
0
    S(c,c);
204
0
    if(a!=1) M(c,c,i);
205
0
  }
206
0
  FOR(a,16) o[a]=c[a];
207
0
}
208
#endif /* DROPBEAR_ED25519 && DROPBEAR_SIGNKEY_VERIFY */
209
210
#if DROPBEAR_CURVE25519_DEP
211
void dropbear_curve25519_scalarmult(u8 *q,const u8 *n,const u8 *p)
212
246
{
213
246
  u8 z[32];
214
246
  i64 x[80],r,i;
215
246
  gf a,b,c,d,e,f;
216
7.62k
  FOR(i,31) z[i]=n[i];
217
246
  z[31]=(n[31]&127)|64;
218
246
  z[0]&=248;
219
246
  unpack25519(x,p);
220
3.93k
  FOR(i,16) {
221
3.93k
    b[i]=x[i];
222
3.93k
    d[i]=a[i]=c[i]=0;
223
3.93k
  }
224
246
  a[0]=d[0]=1;
225
62.9k
  for(i=254;i>=0;--i) {
226
62.7k
    r=(z[i>>3]>>(i&7))&1;
227
62.7k
    sel25519(a,b,r);
228
62.7k
    sel25519(c,d,r);
229
62.7k
    A(e,a,c);
230
62.7k
    Z(a,a,c);
231
62.7k
    A(c,b,d);
232
62.7k
    Z(b,b,d);
233
62.7k
    S(d,e);
234
62.7k
    S(f,a);
235
62.7k
    M(a,c,a);
236
62.7k
    M(c,b,e);
237
62.7k
    A(e,a,c);
238
62.7k
    Z(a,a,c);
239
62.7k
    S(b,a);
240
62.7k
    Z(c,d,f);
241
62.7k
    M(a,c,_121665);
242
62.7k
    A(a,a,d);
243
62.7k
    M(c,c,a);
244
62.7k
    M(a,d,f);
245
62.7k
    M(d,b,x);
246
62.7k
    S(b,e);
247
62.7k
    sel25519(a,b,r);
248
62.7k
    sel25519(c,d,r);
249
62.7k
  }
250
3.93k
  FOR(i,16) {
251
3.93k
    x[i+16]=a[i];
252
3.93k
    x[i+32]=c[i];
253
3.93k
    x[i+48]=b[i];
254
3.93k
    x[i+64]=d[i];
255
3.93k
  }
256
246
  inv25519(x+32,x+32);
257
246
  M(x+16,x+16,x+32);
258
246
  pack25519(q,x+16);
259
246
}
260
#endif /* DROPBEAR_CURVE25519_DEP */
261
262
#if DROPBEAR_ED25519
263
static int crypto_hash(u8 *out,const u8 *m,u64 n)
264
0
{
265
0
  hash_state hs;
266
267
0
  sha512_init(&hs);
268
0
  sha512_process(&hs, m, n);
269
0
  return sha512_done(&hs, out);
270
0
}
271
272
sv add(gf p[4],gf q[4])
273
0
{
274
0
  gf a,b,c,d,t,e,f,g,h;
275
  
276
0
  Z(a, p[1], p[0]);
277
0
  Z(t, q[1], q[0]);
278
0
  M(a, a, t);
279
0
  A(b, p[0], p[1]);
280
0
  A(t, q[0], q[1]);
281
0
  M(b, b, t);
282
0
  M(c, p[3], q[3]);
283
0
  M(c, c, D2);
284
0
  M(d, p[2], q[2]);
285
0
  A(d, d, d);
286
0
  Z(e, b, a);
287
0
  Z(f, d, c);
288
0
  A(g, d, c);
289
0
  A(h, b, a);
290
291
0
  M(p[0], e, f);
292
0
  M(p[1], h, g);
293
0
  M(p[2], g, f);
294
0
  M(p[3], e, h);
295
0
}
296
297
sv cswap(gf p[4],gf q[4],u8 b)
298
0
{
299
0
  int i;
300
0
  FOR(i,4)
301
0
    sel25519(p[i],q[i],b);
302
0
}
303
304
sv pack(u8 *r,gf p[4])
305
0
{
306
0
  gf tx, ty, zi;
307
0
  inv25519(zi, p[2]); 
308
0
  M(tx, p[0], zi);
309
0
  M(ty, p[1], zi);
310
0
  pack25519(r, ty);
311
0
  r[31] ^= par25519(tx) << 7;
312
0
}
313
314
sv scalarmult(gf p[4],gf q[4],const u8 *s)
315
0
{
316
0
  int i;
317
0
  set25519(p[0],gf0);
318
0
  set25519(p[1],gf1);
319
0
  set25519(p[2],gf1);
320
0
  set25519(p[3],gf0);
321
0
  for (i = 255;i >= 0;--i) {
322
0
    u8 b = (s[i/8]>>(i&7))&1;
323
0
    cswap(p,q,b);
324
0
    add(q,p);
325
0
    add(p,p);
326
0
    cswap(p,q,b);
327
0
  }
328
0
}
329
330
sv scalarbase(gf p[4],const u8 *s)
331
0
{
332
0
  gf q[4];
333
0
  set25519(q[0],X);
334
0
  set25519(q[1],Y);
335
0
  set25519(q[2],gf1);
336
0
  M(q[3],X,Y);
337
0
  scalarmult(p,q,s);
338
0
}
339
340
void dropbear_ed25519_make_key(u8 *pk,u8 *sk)
341
0
{
342
0
  u8 d[64];
343
0
  gf p[4];
344
345
0
  genrandom(sk, 32);
346
347
0
  crypto_hash(d, sk, 32);
348
0
  d[0] &= 248;
349
0
  d[31] &= 127;
350
0
  d[31] |= 64;
351
352
0
  scalarbase(p,d);
353
0
  pack(pk,p);
354
0
}
355
356
static const u64 L[32] = {0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10};
357
358
sv modL(u8 *r,i64 x[64])
359
0
{
360
0
  i64 carry,i,j;
361
0
  for (i = 63;i >= 32;--i) {
362
0
    carry = 0;
363
0
    for (j = i - 32;j < i - 12;++j) {
364
0
      x[j] += carry - 16 * x[i] * L[j - (i - 32)];
365
0
      carry = (x[j] + 128) >> 8;
366
0
      x[j] -= ((u64)carry) << 8;
367
0
    }
368
0
    x[j] += carry;
369
0
    x[i] = 0;
370
0
  }
371
0
  carry = 0;
372
0
  FOR(j,32) {
373
0
    x[j] += carry - (x[31] >> 4) * L[j];
374
0
    carry = x[j] >> 8;
375
0
    x[j] &= 255;
376
0
  }
377
0
  FOR(j,32) x[j] -= carry * L[j];
378
0
  FOR(i,32) {
379
0
    x[i+1] += x[i] >> 8;
380
0
    r[i] = x[i] & 255;
381
0
  }
382
0
}
383
384
sv reduce(u8 *r)
385
0
{
386
0
  i64 x[64],i;
387
0
  FOR(i,64) x[i] = (u64) r[i];
388
0
  FOR(i,64) r[i] = 0;
389
0
  modL(r,x);
390
0
}
391
392
void dropbear_ed25519_sign(const u8 *m,u32 mlen,u8 *s,u32 *slen,const u8 *sk, const u8 *pk)
393
0
{
394
0
  hash_state hs;
395
0
  u8 d[64],h[64],r[64];
396
0
  i64 x[64];
397
0
  gf p[4];
398
0
  u32 i,j;
399
400
0
  crypto_hash(d, sk, 32);
401
0
  d[0] &= 248;
402
0
  d[31] &= 127;
403
0
  d[31] |= 64;
404
405
0
  *slen = 64;
406
407
0
  sha512_init(&hs);
408
0
  sha512_process(&hs,d + 32,32);
409
0
  sha512_process(&hs,m,mlen);
410
0
  sha512_done(&hs,r);
411
0
  reduce(r);
412
0
  scalarbase(p,r);
413
0
  pack(s,p);
414
415
0
  sha512_init(&hs);
416
0
  sha512_process(&hs,s,32);
417
0
  sha512_process(&hs,pk,32);
418
0
  sha512_process(&hs,m,mlen);
419
0
  sha512_done(&hs,h);
420
0
  reduce(h);
421
422
0
  FOR(i,64) x[i] = 0;
423
0
  FOR(i,32) x[i] = (u64) r[i];
424
0
  FOR(i,32) FOR(j,32) x[i+j] += h[i] * (u64) d[j];
425
0
  modL(s + 32,x);
426
0
}
427
428
#if DROPBEAR_SIGNKEY_VERIFY
429
static int unpackneg(gf r[4],const u8 p[32])
430
0
{
431
0
  gf t, chk, num, den, den2, den4, den6;
432
0
  set25519(r[2],gf1);
433
0
  unpack25519(r[1],p);
434
0
  S(num,r[1]);
435
0
  M(den,num,D);
436
0
  Z(num,num,r[2]);
437
0
  A(den,r[2],den);
438
439
0
  S(den2,den);
440
0
  S(den4,den2);
441
0
  M(den6,den4,den2);
442
0
  M(t,den6,num);
443
0
  M(t,t,den);
444
445
0
  pow2523(t,t);
446
0
  M(t,t,num);
447
0
  M(t,t,den);
448
0
  M(t,t,den);
449
0
  M(r[0],t,den);
450
451
0
  S(chk,r[0]);
452
0
  M(chk,chk,den);
453
0
  if (neq25519(chk, num)) M(r[0],r[0],I);
454
455
0
  S(chk,r[0]);
456
0
  M(chk,chk,den);
457
0
  if (neq25519(chk, num)) return -1;
458
459
0
  if (par25519(r[0]) == (p[31]>>7)) Z(r[0],gf0,r[0]);
460
461
0
  M(r[3],r[0],r[1]);
462
0
  return 0;
463
0
}
464
465
int dropbear_ed25519_verify(const u8 *m,u32 mlen,const u8 *s,u32 slen,const u8 *pk)
466
0
{
467
0
  hash_state hs;
468
0
  u8 t[32],h[64];
469
0
  gf p[4],q[4];
470
471
0
  if (slen < 64) return -1;
472
473
0
  if (unpackneg(q,pk)) return -1;
474
475
0
  sha512_init(&hs);
476
0
  sha512_process(&hs,s,32);
477
0
  sha512_process(&hs,pk,32);
478
0
  sha512_process(&hs,m,mlen);
479
0
  sha512_done(&hs,h);
480
481
0
  reduce(h);
482
0
  scalarmult(p,q,h);
483
484
0
  scalarbase(q,s + 32);
485
0
  add(p,q);
486
0
  pack(t,p);
487
488
0
  if (crypto_verify_32(s, t))
489
0
    return -1;
490
491
0
  return 0;
492
0
}
493
#endif /* DROPBEAR_SIGNKEY_VERIFY */
494
495
#endif /* DROPBEAR_ED25519 */
496
497
#endif /* DROPBEAR_CURVE25519_DEP || DROPBEAR_ED25519 */