comparison src/pk/dsa/dsa_sign_hash.c @ 209:39d5d58461d6 libtomcrypt-orig LTC_1.05

Import of libtomcrypt 1.05
author Matt Johnston <matt@ucc.asn.au>
date Wed, 06 Jul 2005 03:53:40 +0000
parents 1c15b283127b
children
comparison
equal deleted inserted replaced
191:1c15b283127b 209:39d5d58461d6
19 19
20 /** 20 /**
21 Sign a hash with DSA 21 Sign a hash with DSA
22 @param in The hash to sign 22 @param in The hash to sign
23 @param inlen The length of the hash to sign 23 @param inlen The length of the hash to sign
24 @param out [out] Where to store the signature 24 @param r The "r" integer of the signature (caller must initialize with mp_init() first)
25 @param outlen [in/out] The max size and resulting size of the signature 25 @param s The "s" integer of the signature (caller must initialize with mp_init() first)
26 @param prng An active PRNG state 26 @param prng An active PRNG state
27 @param wprng The index of the PRNG desired 27 @param wprng The index of the PRNG desired
28 @param key A private DSA key 28 @param key A private DSA key
29 @return CRYPT_OK if successful 29 @return CRYPT_OK if successful
30 */ 30 */
31 int dsa_sign_hash(const unsigned char *in, unsigned long inlen, 31 int dsa_sign_hash_raw(const unsigned char *in, unsigned long inlen,
32 unsigned char *out, unsigned long *outlen, 32 mp_int *r, mp_int *s,
33 prng_state *prng, int wprng, dsa_key *key) 33 prng_state *prng, int wprng, dsa_key *key)
34 { 34 {
35 mp_int k, kinv, tmp, r, s; 35 mp_int k, kinv, tmp;
36 unsigned char *buf; 36 unsigned char *buf;
37 int err; 37 int err;
38 unsigned long out1, out2;
39 38
40 LTC_ARGCHK(in != NULL); 39 LTC_ARGCHK(in != NULL);
41 LTC_ARGCHK(out != NULL); 40 LTC_ARGCHK(r != NULL);
42 LTC_ARGCHK(outlen != NULL); 41 LTC_ARGCHK(s != NULL);
43 LTC_ARGCHK(key != NULL); 42 LTC_ARGCHK(key != NULL);
44 43
45 if ((err = prng_is_valid(wprng)) != CRYPT_OK) { 44 if ((err = prng_is_valid(wprng)) != CRYPT_OK) {
46 return err; 45 return err;
47 } 46 }
48 if (key->type != PK_PRIVATE) { 47 if (key->type != PK_PRIVATE) {
58 if (buf == NULL) { 57 if (buf == NULL) {
59 return CRYPT_MEM; 58 return CRYPT_MEM;
60 } 59 }
61 60
62 /* Init our temps */ 61 /* Init our temps */
63 if ((err = mp_init_multi(&k, &kinv, &r, &s, &tmp, NULL)) != MP_OKAY) { goto error; } 62 if ((err = mp_init_multi(&k, &kinv, &tmp, NULL)) != MP_OKAY) { goto error; }
64 63
65 retry: 64 retry:
66 65
67 do { 66 do {
68 /* gen random k */ 67 /* gen random k */
83 82
84 /* now find 1/k mod q */ 83 /* now find 1/k mod q */
85 if ((err = mp_invmod(&k, &key->q, &kinv)) != MP_OKAY) { goto error; } 84 if ((err = mp_invmod(&k, &key->q, &kinv)) != MP_OKAY) { goto error; }
86 85
87 /* now find r = g^k mod p mod q */ 86 /* now find r = g^k mod p mod q */
88 if ((err = mp_exptmod(&key->g, &k, &key->p, &r)) != MP_OKAY) { goto error; } 87 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; } 88 if ((err = mp_mod(r, &key->q, r)) != MP_OKAY) { goto error; }
90 89
91 if (mp_iszero(&r) == MP_YES) { goto retry; } 90 if (mp_iszero(r) == MP_YES) { goto retry; }
92 91
93 /* now find s = (in + xr)/k mod q */ 92 /* now find s = (in + xr)/k mod q */
94 if ((err = mp_read_unsigned_bin(&tmp, (unsigned char *)in, inlen)) != MP_OKAY) { goto error; } 93 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; } 94 if ((err = mp_mul(&key->x, r, s)) != MP_OKAY) { goto error; }
96 if ((err = mp_add(&s, &tmp, &s)) != MP_OKAY) { goto error; } 95 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; } 96 if ((err = mp_mulmod(s, &kinv, &key->q, s)) != MP_OKAY) { goto error; }
98 97
99 if (mp_iszero(&s) == MP_YES) { goto retry; } 98 if (mp_iszero(s) == MP_YES) { goto retry; }
100 99
101 /* now store em both */ 100 err = CRYPT_OK;
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; 101 goto LBL_ERR;
114 102
115 error: 103 error:
116 err = mpi_to_ltc_error(err); 104 err = mpi_to_ltc_error(err);
117 LBL_ERR: 105 LBL_ERR:
118 mp_clear_multi(&k, &kinv, &r, &s, &tmp, NULL); 106 mp_clear_multi(&k, &kinv, &tmp, NULL);
119 #ifdef LTC_CLEAN_STACK 107 #ifdef LTC_CLEAN_STACK
120 zeromem(buf, MDSA_MAX_GROUP); 108 zeromem(buf, MDSA_MAX_GROUP);
121 #endif 109 #endif
122 XFREE(buf); 110 XFREE(buf);
123 return err; 111 return err;
124 } 112 }
125 113
114 /**
115 Sign a hash with DSA
116 @param in The hash to sign
117 @param inlen The length of the hash to sign
118 @param out [out] Where to store the signature
119 @param outlen [in/out] The max size and resulting size of the signature
120 @param prng An active PRNG state
121 @param wprng The index of the PRNG desired
122 @param key A private DSA key
123 @return CRYPT_OK if successful
124 */
125 int dsa_sign_hash(const unsigned char *in, unsigned long inlen,
126 unsigned char *out, unsigned long *outlen,
127 prng_state *prng, int wprng, dsa_key *key)
128 {
129 mp_int r, s;
130 int err;
131
132 LTC_ARGCHK(in != NULL);
133 LTC_ARGCHK(out != NULL);
134 LTC_ARGCHK(outlen != NULL);
135 LTC_ARGCHK(key != NULL);
136
137 if (mp_init_multi(&r, &s, NULL) != MP_OKAY) {
138 return CRYPT_MEM;
139 }
140
141 if ((err = dsa_sign_hash_raw(in, inlen, &r, &s, prng, wprng, key)) != CRYPT_OK) {
142 goto LBL_ERR;
143 }
144
145 err = der_encode_sequence_multi(out, outlen,
146 LTC_ASN1_INTEGER, 1UL, &r,
147 LTC_ASN1_INTEGER, 1UL, &s,
148 LTC_ASN1_EOL, 0UL, NULL);
149
150 LBL_ERR:
151 mp_clear_multi(&r, &s, NULL);
152 return err;
153 }
154
126 #endif 155 #endif
156
157 /* $Source: /cvs/libtom/libtomcrypt/src/pk/dsa/dsa_sign_hash.c,v $ */
158 /* $Revision: 1.6 $ */
159 /* $Date: 2005/05/15 21:48:59 $ */