|
17 | 17 | */
|
18 | 18 |
|
19 | 19 | /**
|
20 |
| - Verify an ECC signature in RFC7518 format |
| 20 | + Verify an ECC signature (ANSI X9.62 format) |
21 | 21 | @param sig The signature to verify
|
22 | 22 | @param siglen The length of the signature (octets)
|
23 | 23 | @param hash The hash (message digest) that was signed
|
24 | 24 | @param hashlen The length of the hash (octets)
|
25 |
| - @param sigformat The format of the signature (ecc_signature_type) |
26 |
| - @param stat Result of signature, 1==valid, 0==invalid |
| 25 | + @param stat [out] Result of signature, 1==valid, 0==invalid |
27 | 26 | @param key The corresponding public ECC key
|
28 | 27 | @return CRYPT_OK if successful (even if the signature is not valid)
|
29 | 28 | */
|
30 |
| -int ecc_verify_hash_ex(const unsigned char *sig, unsigned long siglen, |
31 |
| - const unsigned char *hash, unsigned long hashlen, |
32 |
| - ecc_signature_type sigformat, int *stat, const ecc_key *key) |
| 29 | +int ecc_verify_hash(const unsigned char *sig, unsigned long siglen, |
| 30 | + const unsigned char *hash, unsigned long hashlen, |
| 31 | + int *stat, const ecc_key *key) |
33 | 32 | {
|
34 |
| - ecc_point *mG = NULL, *mQ = NULL; |
35 |
| - void *r, *s, *v, *w, *u1, *u2, *e, *p, *m, *a, *a_plus3; |
36 |
| - void *mu = NULL, *ma = NULL; |
37 |
| - void *mp = NULL; |
38 |
| - int err; |
39 |
| - unsigned long pbits, pbytes, i, shift_right; |
40 |
| - unsigned char ch, buf[MAXBLOCKSIZE]; |
| 33 | + void *r, *s; |
| 34 | + int err; |
41 | 35 |
|
42 |
| - LTC_ARGCHK(sig != NULL); |
43 |
| - LTC_ARGCHK(hash != NULL); |
44 |
| - LTC_ARGCHK(stat != NULL); |
45 |
| - LTC_ARGCHK(key != NULL); |
| 36 | + LTC_ARGCHK(sig != NULL); |
46 | 37 |
|
47 |
| - /* default to invalid signature */ |
48 |
| - *stat = 0; |
| 38 | + if ((err = mp_init_multi(&r, &s, NULL)) != CRYPT_OK) return err; |
49 | 39 |
|
50 |
| - /* allocate ints */ |
51 |
| - if ((err = mp_init_multi(&r, &s, &v, &w, &u1, &u2, &e, &a_plus3, NULL)) != CRYPT_OK) { |
52 |
| - return err; |
53 |
| - } |
54 |
| - |
55 |
| - p = key->dp.order; |
56 |
| - m = key->dp.prime; |
57 |
| - a = key->dp.A; |
58 |
| - if ((err = mp_add_d(a, 3, a_plus3)) != CRYPT_OK) { |
59 |
| - goto error; |
60 |
| - } |
61 |
| - |
62 |
| - /* allocate points */ |
63 |
| - mG = ltc_ecc_new_point(); |
64 |
| - mQ = ltc_ecc_new_point(); |
65 |
| - if (mQ == NULL || mG == NULL) { |
66 |
| - err = CRYPT_MEM; |
67 |
| - goto error; |
68 |
| - } |
69 |
| - |
70 |
| - if (sigformat == LTC_ECCSIG_ANSIX962) { |
71 |
| - /* ANSI X9.62 format - ASN.1 encoded SEQUENCE{ INTEGER(r), INTEGER(s) } */ |
72 |
| - if ((err = der_decode_sequence_multi_ex(sig, siglen, LTC_DER_SEQ_SEQUENCE | LTC_DER_SEQ_STRICT, |
| 40 | + /* ANSI X9.62 format - ASN.1 encoded SEQUENCE{ INTEGER(r), INTEGER(s) } */ |
| 41 | + if ((err = der_decode_sequence_multi_ex(sig, siglen, LTC_DER_SEQ_SEQUENCE | LTC_DER_SEQ_STRICT, |
73 | 42 | LTC_ASN1_INTEGER, 1UL, r,
|
74 | 43 | LTC_ASN1_INTEGER, 1UL, s,
|
75 |
| - LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) { goto error; } |
76 |
| - } |
77 |
| - else if (sigformat == LTC_ECCSIG_RFC7518) { |
78 |
| - /* RFC7518 format - raw (r,s) */ |
79 |
| - i = mp_unsigned_bin_size(key->dp.order); |
80 |
| - if (siglen != (2 * i)) { |
81 |
| - err = CRYPT_INVALID_PACKET; |
82 |
| - goto error; |
83 |
| - } |
84 |
| - if ((err = mp_read_unsigned_bin(r, (unsigned char *)sig, i)) != CRYPT_OK) { goto error; } |
85 |
| - if ((err = mp_read_unsigned_bin(s, (unsigned char *)sig+i, i)) != CRYPT_OK) { goto error; } |
86 |
| - } |
87 |
| - else if (sigformat == LTC_ECCSIG_ETH27) { |
88 |
| - /* Ethereum (v,r,s) format */ |
89 |
| - if (pk_oid_cmp_with_ulong("1.3.132.0.10", key->dp.oid, key->dp.oidlen) != CRYPT_OK) { |
90 |
| - /* Only valid for secp256k1 - OID 1.3.132.0.10 */ |
91 |
| - err = CRYPT_ERROR; goto error; |
92 |
| - } |
93 |
| - if (siglen != 65) { /* Only secp256k1 curves use this format, so must be 65 bytes long */ |
94 |
| - err = CRYPT_INVALID_PACKET; |
95 |
| - goto error; |
96 |
| - } |
97 |
| - if ((err = mp_read_unsigned_bin(r, (unsigned char *)sig, 32)) != CRYPT_OK) { goto error; } |
98 |
| - if ((err = mp_read_unsigned_bin(s, (unsigned char *)sig+32, 32)) != CRYPT_OK) { goto error; } |
99 |
| - } |
100 |
| -#ifdef LTC_SSH |
101 |
| - else if (sigformat == LTC_ECCSIG_RFC5656) { |
102 |
| - char name[64], name2[64]; |
103 |
| - unsigned long namelen = sizeof(name2); |
104 |
| - |
105 |
| - /* Decode as SSH data sequence, per RFC4251 */ |
106 |
| - if ((err = ssh_decode_sequence_multi(sig, siglen, |
107 |
| - LTC_SSHDATA_STRING, name, 64, |
108 |
| - LTC_SSHDATA_MPINT, r, |
109 |
| - LTC_SSHDATA_MPINT, s, |
110 |
| - LTC_SSHDATA_EOL, NULL)) != CRYPT_OK) { goto error; } |
111 |
| - |
112 |
| - |
113 |
| - /* Check curve matches identifier string */ |
114 |
| - if ((err = ecc_ssh_ecdsa_encode_name(name2, &namelen, key)) != CRYPT_OK) { goto error; } |
115 |
| - if (XSTRCMP(name,name2) != 0) { |
116 |
| - err = CRYPT_INVALID_ARG; |
117 |
| - goto error; |
118 |
| - } |
119 |
| - } |
120 |
| -#endif |
121 |
| - else { |
122 |
| - /* Unknown signature format */ |
123 |
| - err = CRYPT_ERROR; |
124 |
| - goto error; |
125 |
| - } |
126 |
| - |
127 |
| - /* check for zero */ |
128 |
| - if (mp_cmp_d(r, 0) != LTC_MP_GT || mp_cmp_d(s, 0) != LTC_MP_GT || |
129 |
| - mp_cmp(r, p) != LTC_MP_LT || mp_cmp(s, p) != LTC_MP_LT) { |
130 |
| - err = CRYPT_INVALID_PACKET; |
131 |
| - goto error; |
132 |
| - } |
133 |
| - |
134 |
| - /* read hash - truncate if needed */ |
135 |
| - pbits = mp_count_bits(p); |
136 |
| - pbytes = (pbits+7) >> 3; |
137 |
| - if (pbits > hashlen*8) { |
138 |
| - if ((err = mp_read_unsigned_bin(e, (unsigned char *)hash, hashlen)) != CRYPT_OK) { goto error; } |
139 |
| - } |
140 |
| - else if (pbits % 8 == 0) { |
141 |
| - if ((err = mp_read_unsigned_bin(e, (unsigned char *)hash, pbytes)) != CRYPT_OK) { goto error; } |
142 |
| - } |
143 |
| - else { |
144 |
| - shift_right = 8 - pbits % 8; |
145 |
| - for (i=0, ch=0; i<pbytes; i++) { |
146 |
| - buf[i] = ch; |
147 |
| - ch = (hash[i] << (8-shift_right)); |
148 |
| - buf[i] = buf[i] ^ (hash[i] >> shift_right); |
149 |
| - } |
150 |
| - if ((err = mp_read_unsigned_bin(e, (unsigned char *)buf, pbytes)) != CRYPT_OK) { goto error; } |
151 |
| - } |
152 |
| - |
153 |
| - /* w = s^-1 mod n */ |
154 |
| - if ((err = mp_invmod(s, p, w)) != CRYPT_OK) { goto error; } |
155 |
| - |
156 |
| - /* u1 = ew */ |
157 |
| - if ((err = mp_mulmod(e, w, p, u1)) != CRYPT_OK) { goto error; } |
158 |
| - |
159 |
| - /* u2 = rw */ |
160 |
| - if ((err = mp_mulmod(r, w, p, u2)) != CRYPT_OK) { goto error; } |
161 |
| - |
162 |
| - /* find mG and mQ */ |
163 |
| - if ((err = ltc_ecc_copy_point(&key->dp.base, mG)) != CRYPT_OK) { goto error; } |
164 |
| - if ((err = ltc_ecc_copy_point(&key->pubkey, mQ)) != CRYPT_OK) { goto error; } |
165 |
| - |
166 |
| - /* find the montgomery mp */ |
167 |
| - if ((err = mp_montgomery_setup(m, &mp)) != CRYPT_OK) { goto error; } |
168 |
| - |
169 |
| - /* for curves with a == -3 keep ma == NULL */ |
170 |
| - if (mp_cmp(a_plus3, m) != LTC_MP_EQ) { |
171 |
| - if ((err = mp_init_multi(&mu, &ma, NULL)) != CRYPT_OK) { goto error; } |
172 |
| - if ((err = mp_montgomery_normalization(mu, m)) != CRYPT_OK) { goto error; } |
173 |
| - if ((err = mp_mulmod(a, mu, m, ma)) != CRYPT_OK) { goto error; } |
174 |
| - } |
175 |
| - |
176 |
| - /* compute u1*mG + u2*mQ = mG */ |
177 |
| - if (ltc_mp.ecc_mul2add == NULL) { |
178 |
| - if ((err = ltc_mp.ecc_ptmul(u1, mG, mG, a, m, 0)) != CRYPT_OK) { goto error; } |
179 |
| - if ((err = ltc_mp.ecc_ptmul(u2, mQ, mQ, a, m, 0)) != CRYPT_OK) { goto error; } |
180 |
| - |
181 |
| - /* add them */ |
182 |
| - if ((err = ltc_mp.ecc_ptadd(mQ, mG, mG, ma, m, mp)) != CRYPT_OK) { goto error; } |
183 |
| - |
184 |
| - /* reduce */ |
185 |
| - if ((err = ltc_mp.ecc_map(mG, m, mp)) != CRYPT_OK) { goto error; } |
186 |
| - } else { |
187 |
| - /* use Shamir's trick to compute u1*mG + u2*mQ using half of the doubles */ |
188 |
| - if ((err = ltc_mp.ecc_mul2add(mG, u1, mQ, u2, mG, ma, m)) != CRYPT_OK) { goto error; } |
189 |
| - } |
190 |
| - |
191 |
| - /* v = X_x1 mod n */ |
192 |
| - if ((err = mp_mod(mG->x, p, v)) != CRYPT_OK) { goto error; } |
| 44 | + LTC_ASN1_EOL, 0UL, NULL)) != CRYPT_OK) goto error; |
193 | 45 |
|
194 |
| - /* does v == r */ |
195 |
| - if (mp_cmp(v, r) == LTC_MP_EQ) { |
196 |
| - *stat = 1; |
197 |
| - } |
| 46 | + err = ecc_verify_hash_internal(r, s, hash, hashlen, stat, key); |
198 | 47 |
|
199 |
| - /* clear up and return */ |
200 |
| - err = CRYPT_OK; |
201 | 48 | error:
|
202 |
| - if (mG != NULL) ltc_ecc_del_point(mG); |
203 |
| - if (mQ != NULL) ltc_ecc_del_point(mQ); |
204 |
| - if (mu != NULL) mp_clear(mu); |
205 |
| - if (ma != NULL) mp_clear(ma); |
206 |
| - mp_clear_multi(r, s, v, w, u1, u2, e, a_plus3, NULL); |
207 |
| - if (mp != NULL) mp_montgomery_free(mp); |
| 49 | + mp_clear_multi(r, s, NULL); |
208 | 50 | return err;
|
209 | 51 | }
|
210 | 52 |
|
|
0 commit comments