comparison src/pk/dsa/dsa_sign_hash.c @ 191:1c15b283127b libtomcrypt-orig

Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author Matt Johnston <matt@ucc.asn.au>
date Fri, 06 May 2005 13:23:02 +0000
parents
children 39d5d58461d6
comparison
equal deleted inserted replaced
143:5d99163f7e32 191:1c15b283127b
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 *
9 * Tom St Denis, [email protected], http://libtomcrypt.org
10 */
11 #include "tomcrypt.h"
12
13 /**
14 @file dsa_sign_hash.c
15 DSA implementation, sign a hash, Tom St Denis
16 */
17
18 #ifdef MDSA
19
20 /**
21 Sign a hash with DSA
22 @param in The hash to sign
23 @param inlen The length of the hash to sign
24 @param out [out] Where to store the signature
25 @param outlen [in/out] The max size and resulting size of the signature
26 @param prng An active PRNG state
27 @param wprng The index of the PRNG desired
28 @param key A private DSA key
29 @return CRYPT_OK if successful
30 */
31 int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
32 unsigned char *out, unsigned long *outlen,
33 prng_state *prng, int wprng, dsa_key *key)
34 {
35 mp_int k, kinv, tmp, r, s;
36 unsigned char *buf;
37 int err;
38 unsigned long out1, out2;
39
40 LTC_ARGCHK(in != NULL);
41 LTC_ARGCHK(out != NULL);
42 LTC_ARGCHK(outlen != NULL);
43 LTC_ARGCHK(key != NULL);
44
45 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
46 return err;
47 }
48 if (key->type != PK_PRIVATE) {
49 return CRYPT_PK_NOT_PRIVATE;
50 }
51
52 /* check group order size */
53 if (key->qord >= MDSA_MAX_GROUP) {
54 return CRYPT_INVALID_ARG;
55 }
56
57 buf = XMALLOC(MDSA_MAX_GROUP);
58 if (buf == NULL) {
59 return CRYPT_MEM;
60 }
61
62 /* Init our temps */
63 if ((err = mp_init_multi(&k, &kinv, &r, &s, &tmp, NULL)) != MP_OKAY) { goto error; }
64
65 retry:
66
67 do {
68 /* gen random k */
69 if (prng_descriptor[wprng].read(buf, key->qord, prng) != (unsigned long)key->qord) {
70 err = CRYPT_ERROR_READPRNG;
71 goto LBL_ERR;
72 }
73
74 /* read k */
75 if ((err = mp_read_unsigned_bin(&k, buf, key->qord)) != MP_OKAY) { goto error; }
76
77 /* k > 1 ? */
78 if (mp_cmp_d(&k, 1) != MP_GT) { goto retry; }
79
80 /* test gcd */
81 if ((err = mp_gcd(&k, &key->q, &tmp)) != MP_OKAY) { goto error; }
82 } while (mp_cmp_d(&tmp, 1) != MP_EQ);
83
84 /* now find 1/k mod q */
85 if ((err = mp_invmod(&k, &key->q, &kinv)) != MP_OKAY) { goto error; }
86
87 /* now find r = g^k mod p mod q */
88 if ((err = mp_exptmod(&key->g, &k, &key->p, &r)) != MP_OKAY) { goto error; }
89 if ((err = mp_mod(&r, &key->q, &r)) != MP_OKAY) { goto error; }
90
91 if (mp_iszero(&r) == MP_YES) { goto retry; }
92
93 /* now find s = (in + xr)/k mod q */
94 if ((err = mp_read_unsigned_bin(&tmp, (unsigned char *)in, inlen)) != MP_OKAY) { goto error; }
95 if ((err = mp_mul(&key->x, &r, &s)) != MP_OKAY) { goto error; }
96 if ((err = mp_add(&s, &tmp, &s)) != MP_OKAY) { goto error; }
97 if ((err = mp_mulmod(&s, &kinv, &key->q, &s)) != MP_OKAY) { goto error; }
98
99 if (mp_iszero(&s) == MP_YES) { goto retry; }
100
101 /* now store em both */
102
103 /* first check that we have enough room */
104 if ((err = der_length_integer(&s, &out1)) != CRYPT_OK) { goto LBL_ERR; }
105 if ((err = der_length_integer(&r, &out2)) != CRYPT_OK) { goto LBL_ERR; }
106 if (*outlen < (out1+out2)) {
107 err = CRYPT_BUFFER_OVERFLOW;
108 goto LBL_ERR;
109 }
110
111 /* store ints */
112 err = der_put_multi_integer(out, outlen, &r, &s, NULL);
113 goto LBL_ERR;
114
115 error:
116 err = mpi_to_ltc_error(err);
117 LBL_ERR:
118 mp_clear_multi(&k, &kinv, &r, &s, &tmp, NULL);
119 #ifdef LTC_CLEAN_STACK
120 zeromem(buf, MDSA_MAX_GROUP);
121 #endif
122 XFREE(buf);
123 return err;
124 }
125
126 #endif