dsa.c 13.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/* dsa.c
 *
 * $Id$
 */

/* lsh, an implementation of the ssh protocol
 *
 * Copyright (C) 1998 Niels Mller
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

25
#include "dsa.h"
26
27
28
29
30

#include "atoms.h"
#include "crypto.h"
#include "format.h"
#include "parse.h"
31
#include "sexp.h"
32
#include "sha.h"
33
#include "spki.h"
34
35
36
37
38
39
#include "ssh.h"
#include "werror.h"
#include "xalloc.h"

#include <assert.h>

40
#include "dsa.c.x" 
41

42
43
44
45
46
47
48
49
/* The standard says that DSA public keys are at most 1024 bits, i.e.
 * 128 octets. We are a little more liberal than that. Note that
 * allowing really large keys opens for Denial-of-service attacks. */

#define DSA_MAX_SIZE 300

#define DSA_MAX_QSIZE SHA_DIGESTSIZE

Niels Möller's avatar
Niels Möller committed
50
#define SA(x) sexp_a(ATOM_##x)
51

Niels Möller's avatar
Niels Möller committed
52
/* DSA signatures */
53
54
55
56
57
58
59
60
61

/* GABA:
   (class
     (name dsa_algorithm)
     (super signature_algorithm)
     (vars
       (random object randomness)))
*/

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* GABA:
   (class
     (name dsa_verifier)
     (super verifier)
     (vars
       (p bignum)
       (q bignum)
       (g bignum)
       (y bignum)))
*/

/* GABA:
   (class
     (name dsa_signer)
     (super signer)
     (vars
       (verifier object dsa_verifier)
       (random object randomness)
       (a bignum)))
*/

83
84
85
86
87

/* Utility functions */

static void
dsa_hash(mpz_t h, UINT32 length, const UINT8 *msg)
88
89
{
  /* Compute hash */
Niels Möller's avatar
Niels Möller committed
90
  struct hash_instance *hash = MAKE_HASH(&sha1_algorithm);
91
92
93
94
95
96
97
98
99
100
101
  UINT8 *digest = alloca(hash->hash_size);
  HASH_UPDATE(hash, length, msg);
  HASH_DIGEST(hash, digest);

  bignum_parse_u(h, hash->hash_size, digest);

  debug("DSA hash: %xn\n", h);
  
  KILL(hash);
}

102
103
104
static struct sexp *
encode_dsa_sig_val(mpz_t r, mpz_t s)
{
Niels Möller's avatar
Niels Möller committed
105
106
  return sexp_l(2, sexp_l(2, SA(R), sexp_un(r), -1),
		sexp_l(2, SA(S), sexp_un(s), -1), -1);
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
}

static int
decode_dsa_sig_val(struct sexp *e, mpz_t r, mpz_t s)
{
  if (sexp_atomp(e))
    return 0;
  else
    {
      struct sexp_iterator *i = SEXP_ITER(e);
      return ( (SEXP_LEFT(i) == 2)
	       && sexp_get_un(i, ATOM_R, r, DSA_MAX_QSIZE)
	       && sexp_get_un(i, ATOM_S, s, DSA_MAX_QSIZE));
    }
}

123
124
125

/* Verifying DSA signatures */

126
127
/* The caller should make sure that r and s are non-negative, and not
 * extremely large. That they are less than q is checked here. */
Niels Möller's avatar
Niels Möller committed
128
static int
129
generic_dsa_verify(struct dsa_verifier *key,
Niels Möller's avatar
Niels Möller committed
130
131
132
		   UINT32 length,
		   const UINT8 *msg,
		   mpz_t r, mpz_t s)
133
134
135
136
137
138
139
140
141
142
143
144
145
146
{
  mpz_t w, tmp, v;
  int res;
  
  debug("generic_dsa_verify, r: %xn\n"
	"                    s: %xn\n", r, s);

  if ( (mpz_cmp(r, key->q) >= 0)
       || (mpz_cmp(s, key->q) >= 0) )
    return 0;
  
  /* Compute w = s^-1 (mod q) */
  mpz_init(w);

147
  /* NOTE: mpz_invert sometimes generates negative inverses. */
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  if (!mpz_invert(w, s, key->q))
    {
      werror("generic_dsa_verify: s non-invertible.\n");
      mpz_clear(w);
      return 0;
    }

  debug("generic_dsa_verify, w: %xn\n", w);

  /* Compute hash */
  mpz_init(tmp);
  dsa_hash(tmp, length, msg);

  /* g^{w * h (mod q)} (mod p)  */

  mpz_init(v);

  mpz_mul(tmp, tmp, w);
  mpz_fdiv_r(tmp, tmp, key->q);

  debug("u1: %xn\n", tmp);
  
  mpz_powm(v, key->g, tmp, key->p);

  /* y^{w * r (mod q) } (mod p) */
  mpz_mul(tmp, r, w);
  mpz_fdiv_r(tmp, tmp, key->q);

  debug("u2: %xn\n", tmp);

  mpz_powm(tmp, key->y, tmp, key->p);
  
  /* (g^{w * h} * y^{w * r} (mod p) ) (mod q) */
  mpz_mul(v, v, tmp);
  mpz_fdiv_r(v, v, key->p);

  debug("generic_dsa_verify, group element: %xn\n", v);
  
  mpz_fdiv_r(v, v, key->q);

  debug("generic_dsa_verify, v: %xn\n", v);

  res = !mpz_cmp(v, r);

  mpz_clear(w);
  mpz_clear(tmp);
  mpz_clear(v);

  return res;
}

Niels Möller's avatar
Niels Möller committed
199
static int
200
do_dsa_verify(struct verifier *c, int algorithm,
Niels Möller's avatar
Niels Möller committed
201
202
203
204
	      UINT32 length,
	      const UINT8 *msg,
	      UINT32 signature_length,
	      const UINT8 *signature_data)
205
{
206
  CAST(dsa_verifier, self, c);
207
208
  struct simple_buffer buffer;

209
  int res = 0;
210
211
212
  
  mpz_t r, s;

213
  trace("do_dsa_verify: Verifying %a signature\n", algorithm);
214
215
216
217
  
  mpz_init(r);
  mpz_init(s);
  
218
219
220
221
222
223
224
225
226
  switch (algorithm)
    {
    case ATOM_SSH_DSS:
      {
	/* NOTE: draft-ietf-secsh-transport-X.txt (x <= 07) uses an extra
	 * length field, which should be removed in the next version. */
	
	UINT32 buf_length;
	const UINT8 *buf;
227
	int atom;
228
229
230
231
232
233
      
	simple_buffer_init(&buffer, signature_length, signature_data);
	if (!(parse_atom(&buffer, &atom)
	      && (atom == ATOM_SSH_DSS)
	      && parse_string(&buffer, &buf_length, &buf)
	      && !(buf_length % 2)
234
235
	      && (buf_length <= (2 * DSA_MAX_QSIZE))
	      && parse_eod(&buffer)))
236
237
238
	  goto fail;

	buf_length /= 2;
239
  
240
241
242
243
244
	bignum_parse_u(r, buf_length, buf);
	bignum_parse_u(s, buf_length, buf + buf_length);

	break;
      }
245
246

#if DATAFELLOWS_WORKAROUNDS
247
248
249
250
251
252
253
254
255
256
257
258
    case ATOM_SSH_DSS_KLUDGE_LOCAL:
      {
	UINT32 buf_length;

	/* NOTE: This doesn't include any length field. Is that right? */

	if ( (signature_length % 2)
	     || (signature_length > (2 * DSA_MAX_QSIZE)) )
	  goto fail;

	buf_length = signature_length / 2;

259
	bignum_parse_u(r, buf_length, signature_data);
260
261
262
	bignum_parse_u(s, buf_length, signature_data + buf_length);
	break;
      }
263
#endif
264
265
266
      /* It doesn't matter here which flavour of SPKI is used. */
    case ATOM_SPKI_SIGN_RSA:
    case ATOM_SPKI_SIGN_DSS:
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
      {
	struct simple_buffer buffer;
	struct sexp *e;
	
	simple_buffer_init(&buffer, signature_length, signature_data);
	
	if (! ( (e = sexp_parse_canonical(&buffer))
		&& parse_eod(&buffer)
		&& decode_dsa_sig_val(e, r, s)) )
	  goto fail;

	break;
      }
    default:
      fatal("do_dsa_verify: Internal error!\n");
    }
283
  res = generic_dsa_verify(self, length, msg, r, s);
284
 fail:
285
286
287
288
289
290
  mpz_clear(r);
  mpz_clear(s);

  return res;
}

Niels Möller's avatar
Niels Möller committed
291
292
293
294
static int
do_dsa_verify_spki(struct verifier *c,
		   UINT32 length,
		   const UINT8 *msg,
295
		   struct sexp *e)
296
{
297
  CAST(dsa_verifier, self, c);
298
299
300
301
302
303
304
305
306

  int res;
  mpz_t r, s;

  mpz_init(r);
  mpz_init(s);

  /* NOTE: With the current definition of sexp_get_un, there are no
   * requirements on the order in which r and s occur. */
307
308
  res = (decode_dsa_sig_val(e, r, s)
	 && generic_dsa_verify(self, length, msg, r, s));
309
310
311
312
313
314
315

  mpz_clear(r);
  mpz_clear(s);

  return res;
}

316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
static struct lsh_string *
do_dsa_public_key(struct verifier *s)
{
  CAST(dsa_verifier, self, s);
  return ssh_format("%a%n%n%n%n",
		    ATOM_SSH_DSS,
		    self->p, self->q,
		    self->g, self->y);
}

static struct sexp *
do_dsa_public_spki_key(struct verifier *s)
{
  CAST(dsa_verifier, self, s);

  return sexp_l(5, SA(DSA),
		sexp_l(2, SA(P), sexp_un(self->p), -1),
		sexp_l(2, SA(Q), sexp_un(self->q), -1),
		sexp_l(2, SA(G), sexp_un(self->g), -1),
		sexp_l(2, SA(Y), sexp_un(self->y), -1),
		-1);
}
338

339
340
static void
init_dsa_verifier(struct dsa_verifier *self)
341
{
342
343
  /* FIXME: The allocator could do this kind of initialization
   * automatically. */
344
345
346
347
348
349
350
351
352
  mpz_init(self->p);
  mpz_init(self->q);
  mpz_init(self->g);
  mpz_init(self->y);

  self->super.verify = do_dsa_verify;
  self->super.verify_spki = do_dsa_verify_spki;
  self->super.public_spki_key = do_dsa_public_spki_key;
  self->super.public_key = do_dsa_public_key;
353
354
}

355
356
357
358
359
360
361
static struct dsa_verifier *
make_dsa_verifier_internal(struct sexp_iterator *i)
{
  NEW(dsa_verifier, res);
  init_dsa_verifier(res);

  assert(SEXP_LEFT(i) >= 4);
362
363
364
365
366

  if (sexp_get_un(i, ATOM_P, res->p, DSA_MAX_SIZE)
      && sexp_get_un(i, ATOM_Q, res->q, DSA_MAX_QSIZE)
      && sexp_get_un(i, ATOM_G, res->g, DSA_MAX_SIZE)
      && sexp_get_un(i, ATOM_Y, res->y, DSA_MAX_SIZE))
367
368
369
370
371
372
373
374
375
    {
      return res;
    }
  else
    {
      KILL(res);
      return NULL;
    }
}
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
  
/* Alternative constructor using a key of type ssh-dss, when the atom
 * "ssh-dss" is already read from the buffer. */
struct verifier *
parse_ssh_dss_public(struct simple_buffer *buffer)
{
  NEW(dsa_verifier, res);
  init_dsa_verifier(res);

  if (parse_bignum(buffer, res->p, DSA_MAX_SIZE)
      && (mpz_sgn(res->p) == 1)
      && parse_bignum(buffer, res->q, DSA_MAX_QSIZE)
      && (mpz_sgn(res->q) == 1)
      && (mpz_cmp(res->q, res->p) < 0) /* q < p */ 
      && parse_bignum(buffer, res->g, DSA_MAX_SIZE)
      && (mpz_sgn(res->g) == 1)
      && (mpz_cmp(res->g, res->p) < 0) /* g < p */ 
      && parse_bignum(buffer, res->y, DSA_MAX_SIZE) 
      && (mpz_sgn(res->y) == 1)
      && (mpz_cmp(res->y, res->p) < 0) /* y < p */
      && parse_eod(buffer))
    
    return &res->super;

  else
    {
      KILL(res);
      return NULL;
    }
}

  
/* Creating signatures */
static void
generic_dsa_sign(struct dsa_signer *self,
		 UINT32 length, const UINT8 *msg,
		 mpz_t r, mpz_t s)
{
  mpz_t k, tmp;

  assert(r && s);
  
  /* Select k, 0<k<q, randomly */
  mpz_init_set(tmp, self->verifier->q);
  mpz_sub_ui(tmp, tmp, 1);

  mpz_init(k);
  bignum_random(k, self->random, tmp);
  mpz_add_ui(k, k, 1);

426
427
428
  /* NOTE: Enabling this leaks information about the secret
   * key to the debug output. */
  /* debug("generic_dsa_sign, k: %xn\n", k); */
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
  
  /* Compute r = (g^k (mod p)) (mod q) */
  mpz_powm(r, self->verifier->g, k, self->verifier->p);

  debug("generic_dsa_sign, group element: %xn\n", r);
  
  mpz_fdiv_r(r, r, self->verifier->q);

  debug("generic_dsa_sign, r: %xn\n", r);

  /* Compute hash */
  dsa_hash(tmp, length, msg);
  
  /* Compute k^-1 (mod q) */
  if (!mpz_invert(k, k, self->verifier->q))
    {
      fatal("generic_dsa_sign: k non-invertible\n");
    }

  /* Compute signature s = k^-1(h + ar) (mod q) */
  mpz_mul(s, r, self->a);
  mpz_fdiv_r(s, s, self->verifier->q);
  mpz_add(s, s, tmp);
  mpz_mul(s, s, k);
  mpz_fdiv_r(s, s, self->verifier->q);

  debug("generic_dsa_sign, s: %xn\n", s);
  
  mpz_clear(k);
  mpz_clear(tmp);
}

static UINT32
dsa_blob_length(mpz_t r, mpz_t s)
{
  UINT32 r_length = bignum_format_u_length(r);
  UINT32 s_length = bignum_format_u_length(s);

  return MAX(r_length, s_length);
}

static void
dsa_blob_write(mpz_t r, mpz_t s, UINT32 length, UINT8 *buf)
{
  bignum_write(r, length, buf);
  bignum_write(s, length, buf + length);
}

static struct lsh_string *
do_dsa_sign(struct signer *c,
	    int algorithm,
	    UINT32 msg_length,
	    const UINT8 *msg)
{
  CAST(dsa_signer, self, c);
  mpz_t r, s;
  struct lsh_string *signature;
  UINT32 buf_length;
  UINT8 *p;

  trace("do_dsa_sign: Signing according to %a\n", algorithm);
  
  mpz_init(r); mpz_init(s);
  generic_dsa_sign(self, msg_length, msg, r, s);

  /* Build signature */
  buf_length = dsa_blob_length(r, s);

  switch (algorithm)
    {
    case ATOM_SSH_DSS:
      /* NOTE: draft-ietf-secsh-transport-X.txt (x <= 07) uses an extra
       * length field, which should be removed in the next version. */
      signature = ssh_format("%a%r", ATOM_SSH_DSS, buf_length * 2, &p);
      dsa_blob_write(r, s, buf_length, p);

      break;
      
#if DATAFELLOWS_WORKAROUNDS
    case ATOM_SSH_DSS_KLUDGE_LOCAL:
      
      /* NOTE: This doesn't include any length field. Is that right? */
      signature = ssh_format("%lr", buf_length * 2, &p);
      dsa_blob_write(r, s, buf_length, p);

      break;

#endif
517
518
519
520
      /* It doesn't matter here which flavour of SPKI is used. */
    case ATOM_SPKI_SIGN_RSA:
    case ATOM_SPKI_SIGN_DSS:

521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
      /* NOTE: Generates the <sig-val> only. */
      signature
	= sexp_format(encode_dsa_sig_val(r, s),
		      SEXP_CANONICAL, 0);
      
      break;
    default:
      fatal("do_dsa_sign: Internal error.");
    }
  mpz_clear(r);
  mpz_clear(s);

  return signature;
}

static struct sexp *
do_dsa_sign_spki(struct signer *c,
		 /* struct sexp *hash, struct sexp *principal, */
		 UINT32 msg_length,
		 const UINT8 *msg)
{
  CAST(dsa_signer, self, c);
  mpz_t r, s;
  struct sexp *signature;
    
  mpz_init(r); mpz_init(s);
  generic_dsa_sign(self, msg_length, msg, r, s);
      
  /* Build signature */
  signature = encode_dsa_sig_val(r, s);
  
  mpz_clear(r);
  mpz_clear(s);

  return signature;
}

static struct verifier *
do_dsa_get_verifier(struct signer *s)
{
  CAST(dsa_signer, self, s);
  return &self->verifier->super;
}

565
566
567
568
569
570
571
572
573
574

static struct verifier *
make_dsa_verifier(struct signature_algorithm *self UNUSED,
		  struct sexp_iterator *i)
{
  return ( (SEXP_LEFT(i) == 4)
	   ? &make_dsa_verifier_internal(i)->super
	   : NULL);
}

575
576
static struct signer *
make_dsa_signer(struct signature_algorithm *c,
577
		struct sexp_iterator *i)
578
{
579
  CAST(dsa_algorithm, self, c);
580
581
  NEW(dsa_signer, res);
  mpz_init(res->a);
582

583
584
585
586
587
588
#if 0
  debug("make_dsa_signer: SEXP_LEFT(i) == %i\n");
  debug("make_dsa_signer: SEXP_GET(i) == %fS\n",
	sexp_format(SEXP_GET(i), SEXP_ADVANCED, 0));
#endif
  
589
  if ( (SEXP_LEFT(i) == 5)
590
       && ( (res->verifier = make_dsa_verifier_internal(i)) )
591
       && sexp_get_un(i, ATOM_X, res->a, DSA_MAX_QSIZE) )
592
    {
593
      res->random = self->random;
594
595
      res->super.sign = do_dsa_sign;
      res->super.sign_spki = do_dsa_sign_spki;
596
      res->super.get_verifier = do_dsa_get_verifier;
597
      
598
599
600
      return &res->super;
    }
  else
601
602
603
604
605
606
    {
      KILL(res);
      return NULL;
    }
}

607
608
struct signature_algorithm *
make_dsa_algorithm(struct randomness *random)
609
610
611
612
613
614
615
616
617
618
{
  NEW(dsa_algorithm, dsa);

  dsa->super.make_signer = make_dsa_signer;
  dsa->super.make_verifier = make_dsa_verifier;
  dsa->random = random;

  return &dsa->super;
}

619

620
struct verifier *
621
make_ssh_dss_verifier(UINT32 public_length,
Niels Möller's avatar
Niels Möller committed
622
		      const UINT8 *public)
623
{
624
625
  struct simple_buffer buffer;
  int atom;
626
  
627
628
  simple_buffer_init(&buffer, public_length, public);

629
630
631
632
  return ( (parse_atom(&buffer, &atom)
	    && (atom == ATOM_SSH_DSS))
	   ? parse_ssh_dss_public(&buffer)
	   : NULL);
633
}