3
* The DSA publickey algorithm.
6
/* nettle, low-level cryptographics library
8
* Copyright (C) 2002 Niels M�ller
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.
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.
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,
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)
50
/* Select k, 0<k<q, randomly */
51
mpz_init_set(tmp, pub->q);
52
mpz_sub_ui(tmp, tmp, 1);
55
nettle_mpz_random(k, random_ctx, random, tmp);
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);
64
nettle_mpz_set_str_256_u(h, SHA1_DIGEST_SIZE, digest);
66
/* Compute k^-1 (mod q) */
67
if (!mpz_invert(k, k, pub->q))
68
/* What do we do now? The key is invalid. */
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);
76
mpz_fdiv_r(signature->s, tmp, pub->q);
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)
90
uint8_t digest[SHA1_DIGEST_SIZE];
91
sha1_digest(hash, sizeof(digest), digest);
93
dsa_sign_digest(pub, key, random_ctx, random,
97
#endif /* WITH_PUBLIC_KEY */