blob: e8c0e0b2bd2ecab0059b2fe46877624f69a0bccb [file] [log] [blame]
Jarno Lamsa18987a42019-04-24 15:40:43 +03001/* ec_dsa.c - TinyCrypt implementation of EC-DSA */
2
Simon Butcher92c3d1f2019-09-09 17:25:08 +01003/*
4 * Copyright (c) 2019, Arm Limited (or its affiliates), All Rights Reserved.
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
Jarno Lamsa18987a42019-04-24 15:40:43 +03008/* Copyright (c) 2014, Kenneth MacKay
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 * * Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.*/
30
31/*
32 * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions are met:
36 *
37 * - Redistributions of source code must retain the above copyright notice,
38 * this list of conditions and the following disclaimer.
39 *
40 * - Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 *
44 * - Neither the name of Intel Corporation nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
52 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
59 */
60
Hanno Becker36ae7582019-07-23 15:52:35 +010061#if !defined(MBEDTLS_CONFIG_FILE)
62#include "mbedtls/config.h"
63#else
64#include MBEDTLS_CONFIG_FILE
65#endif
66
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +020067#if defined(MBEDTLS_USE_TINYCRYPT)
Jarno Lamsa18987a42019-04-24 15:40:43 +030068#include <tinycrypt/ecc.h>
69#include <tinycrypt/ecc_dsa.h>
70
71#if default_RNG_defined
72static uECC_RNG_Function g_rng_function = &default_CSPRNG;
73#else
74static uECC_RNG_Function g_rng_function = 0;
75#endif
76
77static void bits2int(uECC_word_t *native, const uint8_t *bits,
78 unsigned bits_size, uECC_Curve curve)
79{
80 unsigned num_n_bytes = BITS_TO_BYTES(curve->num_n_bits);
81 unsigned num_n_words = BITS_TO_WORDS(curve->num_n_bits);
82 int shift;
83 uECC_word_t carry;
84 uECC_word_t *ptr;
85
86 if (bits_size > num_n_bytes) {
87 bits_size = num_n_bytes;
88 }
89
90 uECC_vli_clear(native, num_n_words);
91 uECC_vli_bytesToNative(native, bits, bits_size);
92 if (bits_size * 8 <= (unsigned)curve->num_n_bits) {
93 return;
94 }
95 shift = bits_size * 8 - curve->num_n_bits;
96 carry = 0;
97 ptr = native + num_n_words;
98 while (ptr-- > native) {
99 uECC_word_t temp = *ptr;
100 *ptr = (temp >> shift) | carry;
101 carry = temp << (uECC_WORD_BITS - shift);
102 }
103
104 /* Reduce mod curve_n */
105 if (uECC_vli_cmp_unsafe(curve->n, native, num_n_words) != 1) {
106 uECC_vli_sub(native, native, curve->n, num_n_words);
107 }
108}
109
110int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
111 unsigned hash_size, uECC_word_t *k, uint8_t *signature,
112 uECC_Curve curve)
113{
114
115 uECC_word_t tmp[NUM_ECC_WORDS];
116 uECC_word_t s[NUM_ECC_WORDS];
117 uECC_word_t *k2[2] = {tmp, s};
118 uECC_word_t p[NUM_ECC_WORDS * 2];
Manuel Pégourié-Gonnard4a658a02019-10-14 11:06:47 +0200119 uECC_word_t *initial_Z = 0;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300120 uECC_word_t carry;
121 wordcount_t num_words = curve->num_words;
122 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
123 bitcount_t num_n_bits = curve->num_n_bits;
124
125 /* Make sure 0 < k < curve_n */
126 if (uECC_vli_isZero(k, num_words) ||
127 uECC_vli_cmp(curve->n, k, num_n_words) != 1) {
128 return 0;
129 }
130
Manuel Pégourié-Gonnard4a658a02019-10-14 11:06:47 +0200131 /* Regularize the bitcount for the private key so that attackers cannot use a
132 * side channel attack to learn the number of leading zeros. */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300133 carry = regularize_k(k, tmp, s, curve);
Manuel Pégourié-Gonnard4a658a02019-10-14 11:06:47 +0200134
135 /* If an RNG function was specified, get a random initial Z value to
136 * protect against side-channel attacks such as Template SPA */
137 if (g_rng_function) {
138 if (!uECC_generate_random_int(k2[carry], curve->p, num_words)) {
139 return 0;
140 }
141 initial_Z = k2[carry];
142 }
143
144 EccPoint_mult(p, curve->G, k2[!carry], initial_Z, num_n_bits + 1, curve);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300145 if (uECC_vli_isZero(p, num_words)) {
146 return 0;
147 }
148
149 /* If an RNG function was specified, get a random number
150 to prevent side channel analysis of k. */
151 if (!g_rng_function) {
152 uECC_vli_clear(tmp, num_n_words);
153 tmp[0] = 1;
154 }
155 else if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
156 return 0;
157 }
158
159 /* Prevent side channel analysis of uECC_vli_modInv() to determine
160 bits of k / the private key by premultiplying by a random number */
161 uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k' = rand * k */
162 uECC_vli_modInv(k, k, curve->n, num_n_words); /* k = 1 / k' */
163 uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k = 1 / k */
164
165 uECC_vli_nativeToBytes(signature, curve->num_bytes, p); /* store r */
166
167 /* tmp = d: */
168 uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(curve->num_n_bits));
169
170 s[num_n_words - 1] = 0;
171 uECC_vli_set(s, p, num_words);
172 uECC_vli_modMult(s, tmp, s, curve->n, num_n_words); /* s = r*d */
173
174 bits2int(tmp, message_hash, hash_size, curve);
175 uECC_vli_modAdd(s, tmp, s, curve->n, num_n_words); /* s = e + r*d */
176 uECC_vli_modMult(s, s, k, curve->n, num_n_words); /* s = (e + r*d) / k */
177 if (uECC_vli_numBits(s, num_n_words) > (bitcount_t)curve->num_bytes * 8) {
178 return 0;
179 }
180
181 uECC_vli_nativeToBytes(signature + curve->num_bytes, curve->num_bytes, s);
182 return 1;
183}
184
185int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
186 unsigned hash_size, uint8_t *signature, uECC_Curve curve)
187{
188 uECC_word_t _random[2*NUM_ECC_WORDS];
189 uECC_word_t k[NUM_ECC_WORDS];
190 uECC_word_t tries;
191
192 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
193 /* Generating _random uniformly at random: */
194 uECC_RNG_Function rng_function = uECC_get_rng();
195 if (!rng_function ||
196 !rng_function((uint8_t *)_random, 2*NUM_ECC_WORDS*uECC_WORD_SIZE)) {
197 return 0;
198 }
199
200 // computing k as modular reduction of _random (see FIPS 186.4 B.5.1):
201 uECC_vli_mmod(k, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));
202
203 if (uECC_sign_with_k(private_key, message_hash, hash_size, k, signature,
204 curve)) {
205 return 1;
206 }
207 }
208 return 0;
209}
210
211static bitcount_t smax(bitcount_t a, bitcount_t b)
212{
213 return (a > b ? a : b);
214}
215
216int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
217 unsigned hash_size, const uint8_t *signature,
218 uECC_Curve curve)
219{
220
221 uECC_word_t u1[NUM_ECC_WORDS], u2[NUM_ECC_WORDS];
222 uECC_word_t z[NUM_ECC_WORDS];
223 uECC_word_t sum[NUM_ECC_WORDS * 2];
224 uECC_word_t rx[NUM_ECC_WORDS];
225 uECC_word_t ry[NUM_ECC_WORDS];
226 uECC_word_t tx[NUM_ECC_WORDS];
227 uECC_word_t ty[NUM_ECC_WORDS];
228 uECC_word_t tz[NUM_ECC_WORDS];
229 const uECC_word_t *points[4];
230 const uECC_word_t *point;
231 bitcount_t num_bits;
232 bitcount_t i;
233
234 uECC_word_t _public[NUM_ECC_WORDS * 2];
235 uECC_word_t r[NUM_ECC_WORDS], s[NUM_ECC_WORDS];
236 wordcount_t num_words = curve->num_words;
237 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
238
239 rx[num_n_words - 1] = 0;
240 r[num_n_words - 1] = 0;
241 s[num_n_words - 1] = 0;
242
243 uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
244 uECC_vli_bytesToNative(_public + num_words, public_key + curve->num_bytes,
245 curve->num_bytes);
246 uECC_vli_bytesToNative(r, signature, curve->num_bytes);
247 uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes);
248
249 /* r, s must not be 0. */
250 if (uECC_vli_isZero(r, num_words) || uECC_vli_isZero(s, num_words)) {
251 return 0;
252 }
253
254 /* r, s must be < n. */
255 if (uECC_vli_cmp_unsafe(curve->n, r, num_n_words) != 1 ||
256 uECC_vli_cmp_unsafe(curve->n, s, num_n_words) != 1) {
257 return 0;
258 }
259
260 /* Calculate u1 and u2. */
261 uECC_vli_modInv(z, s, curve->n, num_n_words); /* z = 1/s */
262 u1[num_n_words - 1] = 0;
263 bits2int(u1, message_hash, hash_size, curve);
264 uECC_vli_modMult(u1, u1, z, curve->n, num_n_words); /* u1 = e/s */
265 uECC_vli_modMult(u2, r, z, curve->n, num_n_words); /* u2 = r/s */
266
267 /* Calculate sum = G + Q. */
268 uECC_vli_set(sum, _public, num_words);
269 uECC_vli_set(sum + num_words, _public + num_words, num_words);
270 uECC_vli_set(tx, curve->G, num_words);
271 uECC_vli_set(ty, curve->G + num_words, num_words);
272 uECC_vli_modSub(z, sum, tx, curve->p, num_words); /* z = x2 - x1 */
273 XYcZ_add(tx, ty, sum, sum + num_words, curve);
274 uECC_vli_modInv(z, z, curve->p, num_words); /* z = 1/z */
275 apply_z(sum, sum + num_words, z, curve);
276
277 /* Use Shamir's trick to calculate u1*G + u2*Q */
278 points[0] = 0;
279 points[1] = curve->G;
280 points[2] = _public;
281 points[3] = sum;
282 num_bits = smax(uECC_vli_numBits(u1, num_n_words),
283 uECC_vli_numBits(u2, num_n_words));
284
285 point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
286 ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
287 uECC_vli_set(rx, point, num_words);
288 uECC_vli_set(ry, point + num_words, num_words);
289 uECC_vli_clear(z, num_words);
290 z[0] = 1;
291
292 for (i = num_bits - 2; i >= 0; --i) {
293 uECC_word_t index;
294 curve->double_jacobian(rx, ry, z, curve);
295
296 index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
297 point = points[index];
298 if (point) {
299 uECC_vli_set(tx, point, num_words);
300 uECC_vli_set(ty, point + num_words, num_words);
301 apply_z(tx, ty, z, curve);
302 uECC_vli_modSub(tz, rx, tx, curve->p, num_words); /* Z = x2 - x1 */
303 XYcZ_add(tx, ty, rx, ry, curve);
304 uECC_vli_modMult_fast(z, z, tz, curve);
305 }
306 }
307
308 uECC_vli_modInv(z, z, curve->p, num_words); /* Z = 1/Z */
309 apply_z(rx, ry, z, curve);
310
311 /* v = x1 (mod n) */
312 if (uECC_vli_cmp_unsafe(curve->n, rx, num_n_words) != 1) {
313 uECC_vli_sub(rx, rx, curve->n, num_n_words);
314 }
315
316 /* Accept only if v == r. */
317 return (int)(uECC_vli_equal(rx, r, num_words) == 0);
318}
Jarno Lamsa46132202019-04-29 14:29:52 +0300319#else
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +0200320typedef int mbedtls_dummy_tinycrypt_def;
321#endif /* MBEDTLS_USE_TINYCRYPT */