1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
3
* LibTomCrypt is a library that provides various cryptographic
4
* algorithms in a highly modular and flexible manner.
6
* The library is free for all purposes without any express
9
* Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
12
/* Implements ECC over Z/pZ for curve y^2 = x^3 - 3x + b
14
* All curves taken from NIST recommendation paper of July 1999
15
* Available at http://csrc.nist.gov/cryptval/dss.htm
20
@file ecc_verify_hash.c
21
ECC Crypto, Tom St Denis
37
Verify an ECC signature
38
@param sig The signature to verify
39
@param siglen The length of the signature (octets)
40
@param hash The hash (message digest) that was signed
41
@param hashlen The length of the hash (octets)
42
@param stat Result of signature, 1==valid, 0==invalid
43
@param key The corresponding public ECC key
44
@return CRYPT_OK if successful (even if the signature is not valid)
46
int ecc_verify_hash(const unsigned char *sig, unsigned long siglen,
47
const unsigned char *hash, unsigned long hashlen,
48
int *stat, ecc_key *key)
51
void *r, *s, *v, *w, *u1, *u2, *e, *p, *m;
55
LTC_ARGCHK(sig != NULL);
56
LTC_ARGCHK(hash != NULL);
57
LTC_ARGCHK(stat != NULL);
58
LTC_ARGCHK(key != NULL);
60
/* default to invalid signature */
64
/* is the IDX valid ? */
65
if (ltc_ecc_is_valid_idx(key->idx) != 1) {
66
return CRYPT_PK_INVALID_TYPE;
70
if ((err = mp_init_multi(&r, &s, &v, &w, &u1, &u2, &p, &e, &m, NULL)) != CRYPT_OK) {
75
mG = ltc_ecc_new_point();
76
mQ = ltc_ecc_new_point();
77
if (mQ == NULL || mG == NULL) {
83
if ((err = der_decode_sequence_multi(sig, siglen,
84
LTC_ASN1_INTEGER, 1UL, r,
85
LTC_ASN1_INTEGER, 1UL, s,
86
LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) {
91
if ((err = mp_read_radix(p, (char *)key->dp->order, 16)) != CRYPT_OK) { goto error; }
94
if ((err = mp_read_radix(m, (char *)key->dp->prime, 16)) != CRYPT_OK) { goto error; }
97
if (mp_iszero(r) || mp_iszero(s) || mp_cmp(r, p) != LTC_MP_LT || mp_cmp(s, p) != LTC_MP_LT) {
98
err = CRYPT_INVALID_PACKET;
103
if ((err = mp_read_unsigned_bin(e, (unsigned char *)hash, (int)hashlen)) != CRYPT_OK) { goto error; }
106
if ((err = mp_invmod(s, p, w)) != CRYPT_OK) { goto error; }
109
if ((err = mp_mulmod(e, w, p, u1)) != CRYPT_OK) { goto error; }
112
if ((err = mp_mulmod(r, w, p, u2)) != CRYPT_OK) { goto error; }
115
if ((err = mp_read_radix(mG->x, (char *)key->dp->Gx, 16)) != CRYPT_OK) { goto error; }
116
if ((err = mp_read_radix(mG->y, (char *)key->dp->Gy, 16)) != CRYPT_OK) { goto error; }
117
if ((err = mp_set(mG->z, 1)) != CRYPT_OK) { goto error; }
119
if ((err = mp_copy(key->pubkey.x, mQ->x)) != CRYPT_OK) { goto error; }
120
if ((err = mp_copy(key->pubkey.y, mQ->y)) != CRYPT_OK) { goto error; }
121
if ((err = mp_copy(key->pubkey.z, mQ->z)) != CRYPT_OK) { goto error; }
123
/* compute u1*mG + u2*mQ = mG */
124
if (ltc_mp.ecc_mul2add == NULL) {
125
if ((err = ltc_mp.ecc_ptmul(u1, mG, mG, m, 0)) != CRYPT_OK) { goto error; }
126
if ((err = ltc_mp.ecc_ptmul(u2, mQ, mQ, m, 0)) != CRYPT_OK) { goto error; }
128
/* find the montgomery mp */
129
if ((err = mp_montgomery_setup(m, &mp)) != CRYPT_OK) { goto error; }
132
if ((err = ltc_mp.ecc_ptadd(mQ, mG, mG, m, mp)) != CRYPT_OK) { goto error; }
135
if ((err = ltc_mp.ecc_map(mG, m, mp)) != CRYPT_OK) { goto error; }
137
/* use Shamir's trick to compute u1*mG + u2*mQ using half of the doubles */
138
if ((err = ltc_mp.ecc_mul2add(mG, u1, mQ, u2, mG, m)) != CRYPT_OK) { goto error; }
142
if ((err = mp_mod(mG->x, p, v)) != CRYPT_OK) { goto error; }
145
if (mp_cmp(v, r) == LTC_MP_EQ) {
149
/* clear up and return */
152
ltc_ecc_del_point(mG);
153
ltc_ecc_del_point(mQ);
154
mp_clear_multi(r, s, v, w, u1, u2, p, e, m, NULL);
156
mp_montgomery_free(mp);
162
/* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_verify_hash.c,v $ */
163
/* $Revision: 1.12 $ */
164
/* $Date: 2006/12/04 05:07:59 $ */