blob: 0f7a9fd2f12900cf4b5c54a8779d38d60d28ffb8 [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>
Manuel Pégourié-Gonnard72a8c9e2019-11-08 10:21:00 +010070#include "mbedtls/platform_util.h"
Jarno Lamsa18987a42019-04-24 15:40:43 +030071
72#if default_RNG_defined
73static uECC_RNG_Function g_rng_function = &default_CSPRNG;
74#else
75static uECC_RNG_Function g_rng_function = 0;
76#endif
77
78static void bits2int(uECC_word_t *native, const uint8_t *bits,
79 unsigned bits_size, uECC_Curve curve)
80{
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +010081 unsigned num_n_bytes = BITS_TO_BYTES(NUM_ECC_BITS);
82 unsigned num_n_words = BITS_TO_WORDS(NUM_ECC_BITS);
Jarno Lamsa18987a42019-04-24 15:40:43 +030083 int shift;
84 uECC_word_t carry;
85 uECC_word_t *ptr;
86
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +010087 (void) curve;
88
Jarno Lamsa18987a42019-04-24 15:40:43 +030089 if (bits_size > num_n_bytes) {
90 bits_size = num_n_bytes;
91 }
92
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +010093 uECC_vli_clear(native);
Jarno Lamsa18987a42019-04-24 15:40:43 +030094 uECC_vli_bytesToNative(native, bits, bits_size);
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +010095 if (bits_size * 8 <= (unsigned)NUM_ECC_BITS) {
Jarno Lamsa18987a42019-04-24 15:40:43 +030096 return;
97 }
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +010098 shift = bits_size * 8 - NUM_ECC_BITS;
Jarno Lamsa18987a42019-04-24 15:40:43 +030099 carry = 0;
100 ptr = native + num_n_words;
101 while (ptr-- > native) {
102 uECC_word_t temp = *ptr;
103 *ptr = (temp >> shift) | carry;
104 carry = temp << (uECC_WORD_BITS - shift);
105 }
106
107 /* Reduce mod curve_n */
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100108 if (uECC_vli_cmp_unsafe(curve_n, native) != 1) {
109 uECC_vli_sub(native, native, curve_n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300110 }
111}
112
113int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
114 unsigned hash_size, uECC_word_t *k, uint8_t *signature,
115 uECC_Curve curve)
116{
117
118 uECC_word_t tmp[NUM_ECC_WORDS];
119 uECC_word_t s[NUM_ECC_WORDS];
Jarno Lamsa18987a42019-04-24 15:40:43 +0300120 uECC_word_t p[NUM_ECC_WORDS * 2];
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +0100121 wordcount_t num_n_words = BITS_TO_WORDS(NUM_ECC_BITS);
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100122 int r;
123
Jarno Lamsa18987a42019-04-24 15:40:43 +0300124
125 /* Make sure 0 < k < curve_n */
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100126 if (uECC_vli_isZero(k) ||
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100127 uECC_vli_cmp(curve_n, k) != 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300128 return 0;
129 }
130
Manuel Pégourié-Gonnarda6115082019-11-21 10:29:14 +0100131 r = EccPoint_mult_safer(p, curve_G, k, curve);
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100132 if (r == 0 || uECC_vli_isZero(p)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300133 return 0;
134 }
135
136 /* If an RNG function was specified, get a random number
137 to prevent side channel analysis of k. */
138 if (!g_rng_function) {
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100139 uECC_vli_clear(tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300140 tmp[0] = 1;
141 }
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100142 else if (!uECC_generate_random_int(tmp, curve_n, num_n_words)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300143 return 0;
144 }
145
146 /* Prevent side channel analysis of uECC_vli_modInv() to determine
147 bits of k / the private key by premultiplying by a random number */
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100148 uECC_vli_modMult(k, k, tmp, curve_n); /* k' = rand * k */
149 uECC_vli_modInv(k, k, curve_n); /* k = 1 / k' */
150 uECC_vli_modMult(k, k, tmp, curve_n); /* k = 1 / k */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300151
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +0100152 uECC_vli_nativeToBytes(signature, NUM_ECC_BYTES, p); /* store r */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300153
154 /* tmp = d: */
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +0100155 uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(NUM_ECC_BITS));
Jarno Lamsa18987a42019-04-24 15:40:43 +0300156
157 s[num_n_words - 1] = 0;
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100158 uECC_vli_set(s, p);
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100159 uECC_vli_modMult(s, tmp, s, curve_n); /* s = r*d */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300160
161 bits2int(tmp, message_hash, hash_size, curve);
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100162 uECC_vli_modAdd(s, tmp, s, curve_n); /* s = e + r*d */
163 uECC_vli_modMult(s, s, k, curve_n); /* s = (e + r*d) / k */
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +0100164 if (uECC_vli_numBits(s) > (bitcount_t)NUM_ECC_BYTES * 8) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300165 return 0;
166 }
167
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +0100168 uECC_vli_nativeToBytes(signature + NUM_ECC_BYTES, NUM_ECC_BYTES, s);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300169 return 1;
170}
171
172int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
173 unsigned hash_size, uint8_t *signature, uECC_Curve curve)
174{
175 uECC_word_t _random[2*NUM_ECC_WORDS];
176 uECC_word_t k[NUM_ECC_WORDS];
177 uECC_word_t tries;
178
179 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
180 /* Generating _random uniformly at random: */
181 uECC_RNG_Function rng_function = uECC_get_rng();
182 if (!rng_function ||
183 !rng_function((uint8_t *)_random, 2*NUM_ECC_WORDS*uECC_WORD_SIZE)) {
184 return 0;
185 }
186
187 // computing k as modular reduction of _random (see FIPS 186.4 B.5.1):
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100188 uECC_vli_mmod(k, _random, curve_n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300189
190 if (uECC_sign_with_k(private_key, message_hash, hash_size, k, signature,
191 curve)) {
192 return 1;
193 }
194 }
195 return 0;
196}
197
198static bitcount_t smax(bitcount_t a, bitcount_t b)
199{
200 return (a > b ? a : b);
201}
202
203int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
204 unsigned hash_size, const uint8_t *signature,
205 uECC_Curve curve)
206{
207
208 uECC_word_t u1[NUM_ECC_WORDS], u2[NUM_ECC_WORDS];
209 uECC_word_t z[NUM_ECC_WORDS];
210 uECC_word_t sum[NUM_ECC_WORDS * 2];
211 uECC_word_t rx[NUM_ECC_WORDS];
212 uECC_word_t ry[NUM_ECC_WORDS];
213 uECC_word_t tx[NUM_ECC_WORDS];
214 uECC_word_t ty[NUM_ECC_WORDS];
215 uECC_word_t tz[NUM_ECC_WORDS];
216 const uECC_word_t *points[4];
217 const uECC_word_t *point;
218 bitcount_t num_bits;
219 bitcount_t i;
Manuel Pégourié-Gonnarde6d6f172019-11-06 11:14:38 +0100220 volatile uECC_word_t diff;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300221
222 uECC_word_t _public[NUM_ECC_WORDS * 2];
223 uECC_word_t r[NUM_ECC_WORDS], s[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard17659332019-11-21 09:27:38 +0100224 wordcount_t num_words = NUM_ECC_WORDS;
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +0100225 wordcount_t num_n_words = BITS_TO_WORDS(NUM_ECC_BITS);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300226
Manuel Pégourié-Gonnardad166d82019-11-04 15:37:42 +0100227 if (curve != uECC_secp256r1())
228 return 0;
229
Jarno Lamsa18987a42019-04-24 15:40:43 +0300230 rx[num_n_words - 1] = 0;
231 r[num_n_words - 1] = 0;
232 s[num_n_words - 1] = 0;
233
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +0100234 uECC_vli_bytesToNative(_public, public_key, NUM_ECC_BYTES);
235 uECC_vli_bytesToNative(_public + num_words, public_key + NUM_ECC_BYTES,
236 NUM_ECC_BYTES);
237 uECC_vli_bytesToNative(r, signature, NUM_ECC_BYTES);
238 uECC_vli_bytesToNative(s, signature + NUM_ECC_BYTES, NUM_ECC_BYTES);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300239
240 /* r, s must not be 0. */
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100241 if (uECC_vli_isZero(r) || uECC_vli_isZero(s)) {
Manuel Pégourié-Gonnard10d8e8e2019-11-06 10:30:26 +0100242 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300243 }
244
245 /* r, s must be < n. */
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100246 if (uECC_vli_cmp_unsafe(curve_n, r) != 1 ||
247 uECC_vli_cmp_unsafe(curve_n, s) != 1) {
Manuel Pégourié-Gonnard10d8e8e2019-11-06 10:30:26 +0100248 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300249 }
250
251 /* Calculate u1 and u2. */
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100252 uECC_vli_modInv(z, s, curve_n); /* z = 1/s */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300253 u1[num_n_words - 1] = 0;
254 bits2int(u1, message_hash, hash_size, curve);
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100255 uECC_vli_modMult(u1, u1, z, curve_n); /* u1 = e/s */
256 uECC_vli_modMult(u2, r, z, curve_n); /* u2 = r/s */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300257
258 /* Calculate sum = G + Q. */
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100259 uECC_vli_set(sum, _public);
260 uECC_vli_set(sum + num_words, _public + num_words);
Manuel Pégourié-Gonnarda6115082019-11-21 10:29:14 +0100261 uECC_vli_set(tx, curve_G);
262 uECC_vli_set(ty, curve_G + num_words);
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100263 uECC_vli_modSub(z, sum, tx, curve_p); /* z = x2 - x1 */
Manuel Pégourié-Gonnardbe5f8332019-11-21 11:02:38 +0100264 XYcZ_add(tx, ty, sum, sum + num_words);
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100265 uECC_vli_modInv(z, z, curve_p); /* z = 1/z */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100266 apply_z(sum, sum + num_words, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300267
268 /* Use Shamir's trick to calculate u1*G + u2*Q */
269 points[0] = 0;
Manuel Pégourié-Gonnarda6115082019-11-21 10:29:14 +0100270 points[1] = curve_G;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300271 points[2] = _public;
272 points[3] = sum;
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100273 num_bits = smax(uECC_vli_numBits(u1),
274 uECC_vli_numBits(u2));
Jarno Lamsa18987a42019-04-24 15:40:43 +0300275
276 point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
277 ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100278 uECC_vli_set(rx, point);
279 uECC_vli_set(ry, point + num_words);
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100280 uECC_vli_clear(z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300281 z[0] = 1;
282
283 for (i = num_bits - 2; i >= 0; --i) {
284 uECC_word_t index;
Manuel Pégourié-Gonnardbe5f8332019-11-21 11:02:38 +0100285 double_jacobian_default(rx, ry, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300286
287 index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
288 point = points[index];
289 if (point) {
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100290 uECC_vli_set(tx, point);
291 uECC_vli_set(ty, point + num_words);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100292 apply_z(tx, ty, z);
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100293 uECC_vli_modSub(tz, rx, tx, curve_p); /* Z = x2 - x1 */
Manuel Pégourié-Gonnardbe5f8332019-11-21 11:02:38 +0100294 XYcZ_add(tx, ty, rx, ry);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100295 uECC_vli_modMult_fast(z, z, tz);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300296 }
297 }
298
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100299 uECC_vli_modInv(z, z, curve_p); /* Z = 1/Z */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100300 apply_z(rx, ry, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300301
302 /* v = x1 (mod n) */
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100303 if (uECC_vli_cmp_unsafe(curve_n, rx) != 1) {
304 uECC_vli_sub(rx, rx, curve_n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300305 }
306
307 /* Accept only if v == r. */
Manuel Pégourié-Gonnarde6d6f172019-11-06 11:14:38 +0100308 diff = uECC_vli_equal(rx, r);
309 if (diff == 0) {
Manuel Pégourié-Gonnard72a8c9e2019-11-08 10:21:00 +0100310 mbedtls_platform_enforce_volatile_reads();
Manuel Pégourié-Gonnarde6d6f172019-11-06 11:14:38 +0100311 if (diff == 0) {
312 return UECC_SUCCESS;
313 }
314 else {
315 return UECC_ATTACK_DETECTED;
316 }
317 }
Manuel Pégourié-Gonnard10d8e8e2019-11-06 10:30:26 +0100318
319 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300320}
Jarno Lamsa46132202019-04-29 14:29:52 +0300321#else
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +0200322typedef int mbedtls_dummy_tinycrypt_def;
323#endif /* MBEDTLS_USE_TINYCRYPT */