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