Coverage Report

Created: 2026-06-05 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dropbear/src/curve25519.c
Line
Count
Source
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
330M
#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
  /* p = 2^255 - 19 */
59
  field_prime = {0xffed, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0x7fff};
60
#endif /* DROPBEAR_SIGNKEY_VERIFY */
61
#endif /* DROPBEAR_ED25519 */
62
63
#if DROPBEAR_ED25519
64
#if DROPBEAR_SIGNKEY_VERIFY
65
static int vn(const u8 *x,const u8 *y,u32 n)
66
0
{
67
0
  u32 i,d = 0;
68
0
  FOR(i,n) d |= x[i]^y[i];
69
0
  return (1 & ((d - 1) >> 8)) - 1;
70
0
}
71
72
static int crypto_verify_32(const u8 *x,const u8 *y)
73
0
{
74
0
  return vn(x,y,32);
75
0
}
76
#endif /* DROPBEAR_SIGNKEY_VERIFY */
77
78
sv set25519(gf r, const gf a)
79
0
{
80
0
  int i;
81
0
  FOR(i,16) r[i]=a[i];
82
0
}
83
#endif /* DROPBEAR_ED25519 */
84
85
sv car25519(gf o)
86
1.63M
{
87
1.63M
  int i;
88
1.63M
  i64 c;
89
26.1M
  FOR(i,16) {
90
26.1M
    o[i]+=(1LL<<16);
91
26.1M
    c=o[i]>>16;
92
26.1M
    o[(i+1)*(i<15)]+=c-1+37*(c-1)*(i==15);
93
26.1M
    o[i]-=((u64)c)<<16;
94
26.1M
  }
95
1.63M
}
96
97
sv sel25519(gf p,gf q,int b)
98
272k
{
99
272k
  i64 t,i,c=~(b-1);
100
4.36M
  FOR(i,16) {
101
4.36M
    t= c&(p[i]^q[i]);
102
4.36M
    p[i]^=t;
103
4.36M
    q[i]^=t;
104
4.36M
  }
105
272k
}
106
107
sv pack25519(u8 *o,const gf n)
108
267
{
109
267
  int i,j,b;
110
267
  gf m,t;
111
4.27k
  FOR(i,16) t[i]=n[i];
112
267
  car25519(t);
113
267
  car25519(t);
114
267
  car25519(t);
115
534
  FOR(j,2) {
116
534
    m[0]=t[0]-0xffed;
117
8.01k
    for(i=1;i<15;i++) {
118
7.47k
      m[i]=t[i]-0xffff-((m[i-1]>>16)&1);
119
7.47k
      m[i-1]&=0xffff;
120
7.47k
    }
121
534
    m[15]=t[15]-0x7fff-((m[14]>>16)&1);
122
534
    b=(m[15]>>16)&1;
123
534
    m[14]&=0xffff;
124
534
    sel25519(t,m,1-b);
125
534
  }
126
4.27k
  FOR(i,16) {
127
4.27k
    o[2*i]=t[i]&0xff;
128
4.27k
    o[2*i+1]=t[i]>>8;
129
4.27k
  }
130
267
}
131
132
#if DROPBEAR_ED25519
133
#if DROPBEAR_SIGNKEY_VERIFY
134
static int neq25519(const gf a, const gf b)
135
0
{
136
0
  u8 c[32],d[32];
137
0
  pack25519(c,a);
138
0
  pack25519(d,b);
139
0
  return crypto_verify_32(c,d);
140
0
}
141
#endif /* DROPBEAR_SIGNKEY_VERIFY */
142
143
static u8 par25519(const gf a)
144
0
{
145
0
  u8 d[32];
146
0
  pack25519(d,a);
147
0
  return d[0]&1;
148
0
}
149
#endif /* DROPBEAR_ED25519 */
150
151
sv unpack25519(gf o, const u8 *n)
152
267
{
153
267
  int i;
154
4.27k
  FOR(i,16) o[i]=n[2*i]+((i64)n[2*i+1]<<8);
155
267
  o[15]&=0x7fff;
156
267
}
157
158
sv A(gf o,const gf a,const gf b)
159
272k
{
160
272k
  int i;
161
4.35M
  FOR(i,16) o[i]=a[i]+b[i];
162
272k
}
163
164
sv Z(gf o,const gf a,const gf b)
165
272k
{
166
272k
  int i;
167
4.35M
  FOR(i,16) o[i]=a[i]-b[i];
168
272k
}
169
170
sv M(gf o,const gf a,const gf b)
171
816k
{
172
816k
  i64 i,j,t[31];
173
25.3M
  FOR(i,31) t[i]=0;
174
208M
  FOR(i,16) FOR(j,16) t[i+j]+=a[i]*b[j];
175
12.2M
  FOR(i,15) t[i]+=38*t[i+16];
176
13.0M
  FOR(i,16) o[i]=t[i];
177
816k
  car25519(o);
178
816k
  car25519(o);
179
816k
}
180
181
sv S(gf o,const gf a)
182
340k
{
183
340k
  M(o,a,a);
184
340k
}
185
186
sv inv25519(gf o,const gf i)
187
267
{
188
267
  gf c;
189
267
  int a;
190
4.27k
  FOR(a,16) c[a]=i[a];
191
68.0k
  for(a=253;a>=0;a--) {
192
67.8k
    S(c,c);
193
67.8k
    if(a!=2&&a!=4) M(c,c,i);
194
67.8k
  }
195
4.27k
  FOR(a,16) o[a]=c[a];
196
267
}
197
198
#if DROPBEAR_ED25519 && DROPBEAR_SIGNKEY_VERIFY
199
sv pow2523(gf o,const gf i)
200
0
{
201
0
  gf c;
202
0
  int a;
203
0
  FOR(a,16) c[a]=i[a];
204
0
  for(a=250;a>=0;a--) {
205
0
    S(c,c);
206
0
    if(a!=1) M(c,c,i);
207
0
  }
208
0
  FOR(a,16) o[a]=c[a];
209
0
}
210
#endif /* DROPBEAR_ED25519 && DROPBEAR_SIGNKEY_VERIFY */
211
212
#if DROPBEAR_CURVE25519_DEP
213
void dropbear_curve25519_scalarmult(u8 *q,const u8 *n,const u8 *p)
214
267
{
215
267
  u8 z[32];
216
267
  i64 x[80],r,i;
217
267
  gf a,b,c,d,e,f;
218
8.27k
  FOR(i,31) z[i]=n[i];
219
267
  z[31]=(n[31]&127)|64;
220
267
  z[0]&=248;
221
267
  unpack25519(x,p);
222
4.27k
  FOR(i,16) {
223
4.27k
    b[i]=x[i];
224
4.27k
    d[i]=a[i]=c[i]=0;
225
4.27k
  }
226
267
  a[0]=d[0]=1;
227
68.3k
  for(i=254;i>=0;--i) {
228
68.0k
    r=(z[i>>3]>>(i&7))&1;
229
68.0k
    sel25519(a,b,r);
230
68.0k
    sel25519(c,d,r);
231
68.0k
    A(e,a,c);
232
68.0k
    Z(a,a,c);
233
68.0k
    A(c,b,d);
234
68.0k
    Z(b,b,d);
235
68.0k
    S(d,e);
236
68.0k
    S(f,a);
237
68.0k
    M(a,c,a);
238
68.0k
    M(c,b,e);
239
68.0k
    A(e,a,c);
240
68.0k
    Z(a,a,c);
241
68.0k
    S(b,a);
242
68.0k
    Z(c,d,f);
243
68.0k
    M(a,c,_121665);
244
68.0k
    A(a,a,d);
245
68.0k
    M(c,c,a);
246
68.0k
    M(a,d,f);
247
68.0k
    M(d,b,x);
248
68.0k
    S(b,e);
249
68.0k
    sel25519(a,b,r);
250
68.0k
    sel25519(c,d,r);
251
68.0k
  }
252
4.27k
  FOR(i,16) {
253
4.27k
    x[i+16]=a[i];
254
4.27k
    x[i+32]=c[i];
255
4.27k
    x[i+48]=b[i];
256
4.27k
    x[i+64]=d[i];
257
4.27k
  }
258
267
  inv25519(x+32,x+32);
259
267
  M(x+16,x+16,x+32);
260
267
  pack25519(q,x+16);
261
267
}
262
#endif /* DROPBEAR_CURVE25519_DEP */
263
264
#if DROPBEAR_ED25519
265
static int crypto_hash(u8 *out,const u8 *m,u64 n)
266
0
{
267
0
  hash_state hs;
268
269
0
  sha512_init(&hs);
270
0
  sha512_process(&hs, m, n);
271
0
  return sha512_done(&hs, out);
272
0
}
273
274
sv add(gf p[4],gf q[4])
275
0
{
276
0
  gf a,b,c,d,t,e,f,g,h;
277
  
278
0
  Z(a, p[1], p[0]);
279
0
  Z(t, q[1], q[0]);
280
0
  M(a, a, t);
281
0
  A(b, p[0], p[1]);
282
0
  A(t, q[0], q[1]);
283
0
  M(b, b, t);
284
0
  M(c, p[3], q[3]);
285
0
  M(c, c, D2);
286
0
  M(d, p[2], q[2]);
287
0
  A(d, d, d);
288
0
  Z(e, b, a);
289
0
  Z(f, d, c);
290
0
  A(g, d, c);
291
0
  A(h, b, a);
292
293
0
  M(p[0], e, f);
294
0
  M(p[1], h, g);
295
0
  M(p[2], g, f);
296
0
  M(p[3], e, h);
297
0
}
298
299
sv cswap(gf p[4],gf q[4],u8 b)
300
0
{
301
0
  int i;
302
0
  FOR(i,4)
303
0
    sel25519(p[i],q[i],b);
304
0
}
305
306
sv pack(u8 *r,gf p[4])
307
0
{
308
0
  gf tx, ty, zi;
309
0
  inv25519(zi, p[2]); 
310
0
  M(tx, p[0], zi);
311
0
  M(ty, p[1], zi);
312
0
  pack25519(r, ty);
313
0
  r[31] ^= par25519(tx) << 7;
314
0
}
315
316
sv scalarmult(gf p[4],gf q[4],const u8 *s)
317
0
{
318
0
  int i;
319
0
  set25519(p[0],gf0);
320
0
  set25519(p[1],gf1);
321
0
  set25519(p[2],gf1);
322
0
  set25519(p[3],gf0);
323
0
  for (i = 255;i >= 0;--i) {
324
0
    u8 b = (s[i/8]>>(i&7))&1;
325
0
    cswap(p,q,b);
326
0
    add(q,p);
327
0
    add(p,p);
328
0
    cswap(p,q,b);
329
0
  }
330
0
}
331
332
sv scalarbase(gf p[4],const u8 *s)
333
0
{
334
0
  gf q[4];
335
0
  set25519(q[0],X);
336
0
  set25519(q[1],Y);
337
0
  set25519(q[2],gf1);
338
0
  M(q[3],X,Y);
339
0
  scalarmult(p,q,s);
340
0
}
341
342
void dropbear_ed25519_make_key(u8 *pk,u8 *sk)
343
0
{
344
0
  u8 d[64];
345
0
  gf p[4];
346
347
0
  genrandom(sk, 32);
348
349
0
  crypto_hash(d, sk, 32);
350
0
  d[0] &= 248;
351
0
  d[31] &= 127;
352
0
  d[31] |= 64;
353
354
0
  scalarbase(p,d);
355
0
  pack(pk,p);
356
0
}
357
358
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};
359
360
sv modL(u8 *r,i64 x[64])
361
0
{
362
0
  i64 carry,i,j;
363
0
  for (i = 63;i >= 32;--i) {
364
0
    carry = 0;
365
0
    for (j = i - 32;j < i - 12;++j) {
366
0
      x[j] += carry - 16 * x[i] * L[j - (i - 32)];
367
0
      carry = (x[j] + 128) >> 8;
368
0
      x[j] -= ((u64)carry) << 8;
369
0
    }
370
0
    x[j] += carry;
371
0
    x[i] = 0;
372
0
  }
373
0
  carry = 0;
374
0
  FOR(j,32) {
375
0
    x[j] += carry - (x[31] >> 4) * L[j];
376
0
    carry = x[j] >> 8;
377
0
    x[j] &= 255;
378
0
  }
379
0
  FOR(j,32) x[j] -= carry * L[j];
380
0
  FOR(i,32) {
381
0
    x[i+1] += x[i] >> 8;
382
0
    r[i] = x[i] & 255;
383
0
  }
384
0
}
385
386
sv reduce(u8 *r)
387
0
{
388
0
  i64 x[64],i;
389
0
  FOR(i,64) x[i] = (u64) r[i];
390
0
  FOR(i,64) r[i] = 0;
391
0
  modL(r,x);
392
0
}
393
394
void dropbear_ed25519_sign(const u8 *m,u32 mlen,u8 *s,u32 *slen,const u8 *sk, const u8 *pk)
395
0
{
396
0
  hash_state hs;
397
0
  u8 d[64],h[64],r[64];
398
0
  i64 x[64];
399
0
  gf p[4];
400
0
  u32 i,j;
401
402
0
  crypto_hash(d, sk, 32);
403
0
  d[0] &= 248;
404
0
  d[31] &= 127;
405
0
  d[31] |= 64;
406
407
0
  *slen = 64;
408
409
0
  sha512_init(&hs);
410
0
  sha512_process(&hs,d + 32,32);
411
0
  sha512_process(&hs,m,mlen);
412
0
  sha512_done(&hs,r);
413
0
  reduce(r);
414
0
  scalarbase(p,r);
415
0
  pack(s,p);
416
417
0
  sha512_init(&hs);
418
0
  sha512_process(&hs,s,32);
419
0
  sha512_process(&hs,pk,32);
420
0
  sha512_process(&hs,m,mlen);
421
0
  sha512_done(&hs,h);
422
0
  reduce(h);
423
424
0
  FOR(i,64) x[i] = 0;
425
0
  FOR(i,32) x[i] = (u64) r[i];
426
0
  FOR(i,32) FOR(j,32) x[i+j] += h[i] * (u64) d[j];
427
0
  modL(s + 32,x);
428
0
}
429
430
#if DROPBEAR_SIGNKEY_VERIFY
431
432
/* Return 0 if S < L, -1 otherwise.
433
 * Only used during verify so timing side-channel is OK */
434
0
static int s_lt_l(const u8 *s) {
435
0
  int i;
436
0
  for (i = 31; i >= 0; i--) {
437
0
    if (s[i] < L[i]) {
438
0
      return 0;
439
0
    }
440
0
    if (s[i] > L[i]) {
441
0
      return -1;
442
0
    }
443
0
  }
444
0
  return -1;
445
0
}
446
447
/* Return 0 if y < p, -1 otherwise.
448
 * field prime p = 2^255 - 19
449
 * Only used during verify so timing side-channel is OK */
450
0
static int y_lt_p(const gf y) {
451
0
  int i;
452
0
  for (i = 15; i >= 0; i--) {
453
0
    if (y[i] < field_prime[i]) {
454
0
      return 0;
455
0
    }
456
0
    if (y[i] > field_prime[i]) {
457
0
      return -1;
458
0
    }
459
0
  }
460
0
  return -1;
461
0
}
462
463
static int unpackneg(gf r[4],const u8 p[32])
464
0
{
465
0
  gf t, chk, num, den, den2, den4, den6;
466
0
  set25519(r[2],gf1);
467
0
  unpack25519(r[1],p);
468
469
  /* Check that pubkey y < 2^255 - 19 */
470
0
  if (y_lt_p(r[1])) {
471
0
    return -1;
472
0
  }
473
474
0
  S(num,r[1]);
475
0
  M(den,num,D);
476
0
  Z(num,num,r[2]);
477
0
  A(den,r[2],den);
478
479
0
  S(den2,den);
480
0
  S(den4,den2);
481
0
  M(den6,den4,den2);
482
0
  M(t,den6,num);
483
0
  M(t,t,den);
484
485
0
  pow2523(t,t);
486
0
  M(t,t,num);
487
0
  M(t,t,den);
488
0
  M(t,t,den);
489
0
  M(r[0],t,den);
490
491
0
  S(chk,r[0]);
492
0
  M(chk,chk,den);
493
0
  if (neq25519(chk, num)) M(r[0],r[0],I);
494
495
0
  S(chk,r[0]);
496
0
  M(chk,chk,den);
497
0
  if (neq25519(chk, num)) return -1;
498
499
0
  if (par25519(r[0]) == (p[31]>>7)) Z(r[0],gf0,r[0]);
500
501
0
  M(r[3],r[0],r[1]);
502
0
  return 0;
503
0
}
504
505
int dropbear_ed25519_verify(const u8 *m,u32 mlen,const u8 *s,u32 slen,const u8 *pk)
506
0
{
507
0
  hash_state hs;
508
0
  u8 t[32],h[64];
509
0
  gf p[4],q[4];
510
511
0
  if (slen < 64) return -1;
512
513
0
  if (s_lt_l(s + 32) == -1) {
514
0
    return -1;
515
0
  }
516
517
0
  if (unpackneg(q,pk)) return -1;
518
519
0
  sha512_init(&hs);
520
0
  sha512_process(&hs,s,32);
521
0
  sha512_process(&hs,pk,32);
522
0
  sha512_process(&hs,m,mlen);
523
0
  sha512_done(&hs,h);
524
525
0
  reduce(h);
526
0
  scalarmult(p,q,h);
527
528
0
  scalarbase(q,s + 32);
529
0
  add(p,q);
530
0
  pack(t,p);
531
532
0
  if (crypto_verify_32(s, t))
533
0
    return -1;
534
535
0
  return 0;
536
0
}
537
#endif /* DROPBEAR_SIGNKEY_VERIFY */
538
539
#endif /* DROPBEAR_ED25519 */
540
541
#endif /* DROPBEAR_CURVE25519_DEP || DROPBEAR_ED25519 */