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