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