dsa-sign.c 2.26 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1
/* dsa-sign.c
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

   The DSA publickey algorithm.

   Copyright (C) 2002, 2010 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * 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.

   or both in parallel, as here.

   GNU Nettle 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 copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/
Niels Möller's avatar
Niels Möller committed
33
34

#if HAVE_CONFIG_H
35
# include "config.h"
Niels Möller's avatar
Niels Möller committed
36
37
#endif

38
#include <assert.h>
39
40
#include <stdlib.h>

Niels Möller's avatar
Niels Möller committed
41
42
43
44
45
#include "dsa.h"

#include "bignum.h"


46
int
47
48
dsa_sign(const struct dsa_params *params,
	 const mpz_t x,
49
50
51
52
	 void *random_ctx, nettle_random_func *random,
	 size_t digest_size,
	 const uint8_t *digest,
	 struct dsa_signature *signature)
Niels Möller's avatar
Niels Möller committed
53
54
55
56
{
  mpz_t k;
  mpz_t h;
  mpz_t tmp;
57
58
  int res;
  
Niels Möller's avatar
Niels Möller committed
59
  /* Select k, 0<k<q, randomly */
60
  mpz_init_set(tmp, params->q);
Niels Möller's avatar
Niels Möller committed
61
62
63
  mpz_sub_ui(tmp, tmp, 1);

  mpz_init(k);
Niels Möller's avatar
Niels Möller committed
64
  nettle_mpz_random(k, random_ctx, random, tmp);
Niels Möller's avatar
Niels Möller committed
65
66
67
  mpz_add_ui(k, k, 1);

  /* Compute r = (g^k (mod p)) (mod q) */
Niels Möller's avatar
Niels Möller committed
68
  mpz_powm_sec(tmp, params->g, k, params->p);
69
  mpz_fdiv_r(signature->r, tmp, params->q);
Niels Möller's avatar
Niels Möller committed
70
71

  /* Compute hash */
72
  mpz_init(h);
73
  _dsa_hash (h, mpz_sizeinbase(params->q, 2), digest_size, digest);
Niels Möller's avatar
Niels Möller committed
74
75

  /* Compute k^-1 (mod q) */
76
  if (mpz_invert(k, k, params->q))
77
78
    {
      /* Compute signature s = k^-1 (h + xr) (mod q) */
79
80
      mpz_mul(tmp, signature->r, x);
      mpz_fdiv_r(tmp, tmp, params->q);
81
82
      mpz_add(tmp, tmp, h);
      mpz_mul(tmp, tmp, k);
83
      mpz_fdiv_r(signature->s, tmp, params->q);
84
85
86
      res = 1;
    }
  else
Niels Möller's avatar
Niels Möller committed
87
    /* What do we do now? The key is invalid. */
88
    res = 0;
Niels Möller's avatar
Niels Möller committed
89
90
91
92

  mpz_clear(k);
  mpz_clear(h);
  mpz_clear(tmp);
93

94
  return res;
95
}