~ubuntu-branches/ubuntu/quantal/nettle/quantal

« back to all changes in this revision

Viewing changes to dsa-sign.c

  • Committer: Bazaar Package Importer
  • Author(s): Marek Habersack
  • Date: 2004-05-04 15:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040504155602-7jbhw5mabvwksl3j
Tags: upstream-1.10
Import upstream version 1.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* dsa-sign.c
 
2
 *
 
3
 * The DSA publickey algorithm.
 
4
 */
 
5
 
 
6
/* nettle, low-level cryptographics library
 
7
 *
 
8
 * Copyright (C) 2002 Niels M�ller
 
9
 *  
 
10
 * The nettle library is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU Lesser General Public License as published by
 
12
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 
13
 * option) any later version.
 
14
 * 
 
15
 * The nettle library is distributed in the hope that it will be useful, but
 
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
17
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 
18
 * License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU Lesser General Public License
 
21
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
 
22
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
23
 * MA 02111-1307, USA.
 
24
 */
 
25
 
 
26
#if HAVE_CONFIG_H
 
27
# include "config.h"
 
28
#endif
 
29
 
 
30
#if WITH_PUBLIC_KEY
 
31
 
 
32
#include <stdlib.h>
 
33
 
 
34
#include "dsa.h"
 
35
 
 
36
#include "bignum.h"
 
37
 
 
38
 
 
39
void
 
40
dsa_sign_digest(const struct dsa_public_key *pub,
 
41
                const struct dsa_private_key *key,
 
42
                void *random_ctx, nettle_random_func random,
 
43
                const uint8_t *digest,
 
44
                struct dsa_signature *signature)
 
45
{
 
46
  mpz_t k;
 
47
  mpz_t h;
 
48
  mpz_t tmp;
 
49
  
 
50
  /* Select k, 0<k<q, randomly */
 
51
  mpz_init_set(tmp, pub->q);
 
52
  mpz_sub_ui(tmp, tmp, 1);
 
53
 
 
54
  mpz_init(k);
 
55
  nettle_mpz_random(k, random_ctx, random, tmp);
 
56
  mpz_add_ui(k, k, 1);
 
57
 
 
58
  /* Compute r = (g^k (mod p)) (mod q) */
 
59
  mpz_powm(tmp, pub->g, k, pub->p);
 
60
  mpz_fdiv_r(signature->r, tmp, pub->q);
 
61
 
 
62
  /* Compute hash */
 
63
  mpz_init(h);
 
64
  nettle_mpz_set_str_256_u(h, SHA1_DIGEST_SIZE, digest);
 
65
 
 
66
  /* Compute k^-1 (mod q) */
 
67
  if (!mpz_invert(k, k, pub->q))
 
68
    /* What do we do now? The key is invalid. */
 
69
    abort();
 
70
 
 
71
  /* Compute signature s = k^-1 (h + xr) (mod q) */
 
72
  mpz_mul(tmp, signature->r, key->x);
 
73
  mpz_fdiv_r(tmp, tmp, pub->q);
 
74
  mpz_add(tmp, tmp, h);
 
75
  mpz_mul(tmp, tmp, k);
 
76
  mpz_fdiv_r(signature->s, tmp, pub->q);
 
77
 
 
78
  mpz_clear(k);
 
79
  mpz_clear(h);
 
80
  mpz_clear(tmp);
 
81
}
 
82
 
 
83
void
 
84
dsa_sign(const struct dsa_public_key *pub,
 
85
         const struct dsa_private_key *key,
 
86
         void *random_ctx, nettle_random_func random,
 
87
         struct sha1_ctx *hash,
 
88
         struct dsa_signature *signature)
 
89
{
 
90
  uint8_t digest[SHA1_DIGEST_SIZE];
 
91
  sha1_digest(hash, sizeof(digest), digest);
 
92
 
 
93
  dsa_sign_digest(pub, key, random_ctx, random,
 
94
                  digest, signature);
 
95
}
 
96
 
 
97
#endif /* WITH_PUBLIC_KEY */