1 /*
2  * SPDX-FileCopyrightText: 2014, Kenneth MacKay
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * SPDX-FileContributor: 2020-2021 Espressif Systems (Shanghai) CO LTD
7  */
8 
9 /* uECC_verify() calls a number of static functions form here and
10    uses other definitions, so we just build that whole source file here and then append
11    our modified version uECC_verify_antifault(). */
12 #include "micro-ecc/uECC.c"
13 
14 /* Version of uECC_verify() which also copies message_hash into verified_hash,
15    but only if the signature is valid. Does this in an FI resistant way.
16 */
uECC_verify_antifault(const uint8_t * public_key,const uint8_t * message_hash,unsigned hash_size,const uint8_t * signature,uECC_Curve curve,uint8_t * verified_hash)17 int uECC_verify_antifault(const uint8_t *public_key,
18                 const uint8_t *message_hash,
19                 unsigned hash_size,
20                 const uint8_t *signature,
21                 uECC_Curve curve,
22                 uint8_t *verified_hash) {
23     uECC_word_t u1[uECC_MAX_WORDS], u2[uECC_MAX_WORDS];
24     uECC_word_t z[uECC_MAX_WORDS];
25     uECC_word_t sum[uECC_MAX_WORDS * 2];
26     uECC_word_t rx[uECC_MAX_WORDS];
27     uECC_word_t ry[uECC_MAX_WORDS];
28     uECC_word_t tx[uECC_MAX_WORDS];
29     uECC_word_t ty[uECC_MAX_WORDS];
30     uECC_word_t tz[uECC_MAX_WORDS];
31     const uECC_word_t *points[4];
32     const uECC_word_t *point;
33     bitcount_t num_bits;
34     bitcount_t i;
35 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
36     uECC_word_t *_public = (uECC_word_t *)public_key;
37 #else
38     uECC_word_t _public[uECC_MAX_WORDS * 2];
39 #endif
40     uECC_word_t r[uECC_MAX_WORDS], s[uECC_MAX_WORDS];
41     wordcount_t num_words = curve->num_words;
42     wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
43 
44     rx[num_n_words - 1] = 0;
45     r[num_n_words - 1] = 0;
46     s[num_n_words - 1] = 0;
47 
48 #if uECC_VLI_NATIVE_LITTLE_ENDIAN
49     bcopy((uint8_t *) r, signature, curve->num_bytes);
50     bcopy((uint8_t *) s, signature + curve->num_bytes, curve->num_bytes);
51 #else
52     uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
53     uECC_vli_bytesToNative(
54         _public + num_words, public_key + curve->num_bytes, curve->num_bytes);
55     uECC_vli_bytesToNative(r, signature, curve->num_bytes);
56     uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes);
57 #endif
58 
59     /* r, s must not be 0. */
60     if (uECC_vli_isZero(r, num_words) || uECC_vli_isZero(s, num_words)) {
61         return 0;
62     }
63 
64     /* r, s must be < n. */
65     if (uECC_vli_cmp(curve->n, r, num_n_words) != 1 ||
66             uECC_vli_cmp(curve->n, s, num_n_words) != 1) {
67         return 0;
68     }
69 
70     /* Calculate u1 and u2. */
71     uECC_vli_modInv(z, s, curve->n, num_n_words); /* z = 1/s */
72     u1[num_n_words - 1] = 0;
73     bits2int(u1, message_hash, hash_size, curve);
74     uECC_vli_modMult(u1, u1, z, curve->n, num_n_words); /* u1 = e/s */
75     uECC_vli_modMult(u2, r, z, curve->n, num_n_words); /* u2 = r/s */
76 
77     /* Calculate sum = G + Q. */
78     uECC_vli_set(sum, _public, num_words);
79     uECC_vli_set(sum + num_words, _public + num_words, num_words);
80     uECC_vli_set(tx, curve->G, num_words);
81     uECC_vli_set(ty, curve->G + num_words, num_words);
82     uECC_vli_modSub(z, sum, tx, curve->p, num_words); /* z = x2 - x1 */
83     XYcZ_add(tx, ty, sum, sum + num_words, curve);
84     uECC_vli_modInv(z, z, curve->p, num_words); /* z = 1/z */
85     apply_z(sum, sum + num_words, z, curve);
86 
87     /* Use Shamir's trick to calculate u1*G + u2*Q */
88     points[0] = 0;
89     points[1] = curve->G;
90     points[2] = _public;
91     points[3] = sum;
92     num_bits = smax(uECC_vli_numBits(u1, num_n_words),
93                     uECC_vli_numBits(u2, num_n_words));
94 
95     point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
96                    ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
97     uECC_vli_set(rx, point, num_words);
98     uECC_vli_set(ry, point + num_words, num_words);
99     uECC_vli_clear(z, num_words);
100     z[0] = 1;
101 
102     for (i = num_bits - 2; i >= 0; --i) {
103         uECC_word_t index;
104         curve->double_jacobian(rx, ry, z, curve);
105 
106         index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
107         point = points[index];
108         if (point) {
109             uECC_vli_set(tx, point, num_words);
110             uECC_vli_set(ty, point + num_words, num_words);
111             apply_z(tx, ty, z, curve);
112             uECC_vli_modSub(tz, rx, tx, curve->p, num_words); /* Z = x2 - x1 */
113             XYcZ_add(tx, ty, rx, ry, curve);
114             uECC_vli_modMult_fast(z, z, tz, curve);
115         }
116     }
117 
118     uECC_vli_modInv(z, z, curve->p, num_words); /* Z = 1/Z */
119     apply_z(rx, ry, z, curve);
120 
121     /* v = x1 (mod n) */
122     if (uECC_vli_cmp(curve->n, rx, num_n_words) != 1) {
123         uECC_vli_sub(rx, rx, curve->n, num_n_words);
124     }
125 
126     /* Anti-FI addition. Copy message_hash into verified_hash, but do it in a
127        way that it will only happen if v == r (ie, rx == r)
128     */
129     const uECC_word_t *mhash_words = (const uECC_word_t *)message_hash;
130     uECC_word_t *vhash_words = (uECC_word_t *)verified_hash;
131     unsigned hash_words = hash_size / sizeof(uECC_word_t);
132     for (unsigned int w = 0; w < hash_words; w++) {
133         /* note: using curve->num_words here to encourage compiler to re-read this variable */
134         vhash_words[w] = mhash_words[w] ^ rx[w % curve->num_words] ^ r[w % curve->num_words];
135     }
136     /* Curve may be longer than hash, in which case keep reading the rest of the bytes */
137     for (int w = hash_words; w < curve->num_words; w++) {
138         vhash_words[w % hash_words] |= rx[w] ^ r[w];
139     }
140 
141     /* Accept only if v == r. */
142     return (int)(uECC_vli_equal(rx, r, num_words));
143 }
144