blob: f1620d08f41b30c157fc4db184fc4e356a20ad37 [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
Jarno Lamsa18987a42019-04-24 15:40:43 +030067#include <tinycrypt/ecc.h>
68#include <tinycrypt/ecc_dsa.h>
Manuel Pégourié-Gonnard72a8c9e2019-11-08 10:21:00 +010069#include "mbedtls/platform_util.h"
Jarno Lamsa18987a42019-04-24 15:40:43 +030070
Jarno Lamsa18987a42019-04-24 15:40:43 +030071static void bits2int(uECC_word_t *native, const uint8_t *bits,
72 unsigned bits_size, uECC_Curve curve)
73{
74 unsigned num_n_bytes = BITS_TO_BYTES(curve->num_n_bits);
75 unsigned num_n_words = BITS_TO_WORDS(curve->num_n_bits);
76 int shift;
77 uECC_word_t carry;
78 uECC_word_t *ptr;
79
80 if (bits_size > num_n_bytes) {
81 bits_size = num_n_bytes;
82 }
83
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +010084 uECC_vli_clear(native);
Jarno Lamsa18987a42019-04-24 15:40:43 +030085 uECC_vli_bytesToNative(native, bits, bits_size);
86 if (bits_size * 8 <= (unsigned)curve->num_n_bits) {
87 return;
88 }
89 shift = bits_size * 8 - curve->num_n_bits;
90 carry = 0;
91 ptr = native + num_n_words;
92 while (ptr-- > native) {
93 uECC_word_t temp = *ptr;
94 *ptr = (temp >> shift) | carry;
95 carry = temp << (uECC_WORD_BITS - shift);
96 }
97
98 /* Reduce mod curve_n */
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +010099 if (uECC_vli_cmp_unsafe(curve->n, native) != 1) {
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100100 uECC_vli_sub(native, native, curve->n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300101 }
102}
103
104int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
105 unsigned hash_size, uECC_word_t *k, uint8_t *signature,
106 uECC_Curve curve)
107{
108
109 uECC_word_t tmp[NUM_ECC_WORDS];
110 uECC_word_t s[NUM_ECC_WORDS];
Jarno Lamsa18987a42019-04-24 15:40:43 +0300111 uECC_word_t p[NUM_ECC_WORDS * 2];
Jarno Lamsa18987a42019-04-24 15:40:43 +0300112 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100113 int r;
114
Jarno Lamsa18987a42019-04-24 15:40:43 +0300115
116 /* Make sure 0 < k < curve_n */
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100117 if (uECC_vli_isZero(k) ||
Manuel Pégourié-Gonnard2cb3eea2019-11-04 14:43:35 +0100118 uECC_vli_cmp(curve->n, k) != 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300119 return 0;
120 }
121
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100122 r = EccPoint_mult_safer(p, curve->G, k, curve);
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100123 if (r == 0 || uECC_vli_isZero(p)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300124 return 0;
125 }
126
127 /* If an RNG function was specified, get a random number
128 to prevent side channel analysis of k. */
Manuel Pégourié-Gonnarda4b42182019-12-18 10:29:58 +0100129 if (!uECC_get_rng()) {
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100130 uECC_vli_clear(tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300131 tmp[0] = 1;
132 }
133 else if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
134 return 0;
135 }
136
137 /* Prevent side channel analysis of uECC_vli_modInv() to determine
138 bits of k / the private key by premultiplying by a random number */
Manuel Pégourié-Gonnard3e20adf2019-11-04 15:00:43 +0100139 uECC_vli_modMult(k, k, tmp, curve->n); /* k' = rand * k */
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100140 uECC_vli_modInv(k, k, curve->n); /* k = 1 / k' */
Manuel Pégourié-Gonnard3e20adf2019-11-04 15:00:43 +0100141 uECC_vli_modMult(k, k, tmp, curve->n); /* k = 1 / k */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300142
143 uECC_vli_nativeToBytes(signature, curve->num_bytes, p); /* store r */
144
145 /* tmp = d: */
146 uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(curve->num_n_bits));
147
148 s[num_n_words - 1] = 0;
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100149 uECC_vli_set(s, p);
Manuel Pégourié-Gonnard3e20adf2019-11-04 15:00:43 +0100150 uECC_vli_modMult(s, tmp, s, curve->n); /* s = r*d */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300151
152 bits2int(tmp, message_hash, hash_size, curve);
Manuel Pégourié-Gonnard0779be72019-11-04 14:48:22 +0100153 uECC_vli_modAdd(s, tmp, s, curve->n); /* s = e + r*d */
Manuel Pégourié-Gonnard3e20adf2019-11-04 15:00:43 +0100154 uECC_vli_modMult(s, s, k, curve->n); /* s = (e + r*d) / k */
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100155 if (uECC_vli_numBits(s) > (bitcount_t)curve->num_bytes * 8) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300156 return 0;
157 }
158
159 uECC_vli_nativeToBytes(signature + curve->num_bytes, curve->num_bytes, s);
160 return 1;
161}
162
163int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
164 unsigned hash_size, uint8_t *signature, uECC_Curve curve)
165{
166 uECC_word_t _random[2*NUM_ECC_WORDS];
167 uECC_word_t k[NUM_ECC_WORDS];
168 uECC_word_t tries;
169
170 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
171 /* Generating _random uniformly at random: */
172 uECC_RNG_Function rng_function = uECC_get_rng();
173 if (!rng_function ||
174 !rng_function((uint8_t *)_random, 2*NUM_ECC_WORDS*uECC_WORD_SIZE)) {
175 return 0;
176 }
177
178 // computing k as modular reduction of _random (see FIPS 186.4 B.5.1):
Manuel Pégourié-Gonnard10349e42019-11-04 14:57:53 +0100179 uECC_vli_mmod(k, _random, curve->n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300180
181 if (uECC_sign_with_k(private_key, message_hash, hash_size, k, signature,
182 curve)) {
183 return 1;
184 }
185 }
186 return 0;
187}
188
189static bitcount_t smax(bitcount_t a, bitcount_t b)
190{
191 return (a > b ? a : b);
192}
193
194int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
195 unsigned hash_size, const uint8_t *signature,
196 uECC_Curve curve)
197{
198
199 uECC_word_t u1[NUM_ECC_WORDS], u2[NUM_ECC_WORDS];
200 uECC_word_t z[NUM_ECC_WORDS];
201 uECC_word_t sum[NUM_ECC_WORDS * 2];
202 uECC_word_t rx[NUM_ECC_WORDS];
203 uECC_word_t ry[NUM_ECC_WORDS];
204 uECC_word_t tx[NUM_ECC_WORDS];
205 uECC_word_t ty[NUM_ECC_WORDS];
206 uECC_word_t tz[NUM_ECC_WORDS];
207 const uECC_word_t *points[4];
208 const uECC_word_t *point;
209 bitcount_t num_bits;
210 bitcount_t i;
Manuel Pégourié-Gonnarde6d6f172019-11-06 11:14:38 +0100211 volatile uECC_word_t diff;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300212
213 uECC_word_t _public[NUM_ECC_WORDS * 2];
214 uECC_word_t r[NUM_ECC_WORDS], s[NUM_ECC_WORDS];
215 wordcount_t num_words = curve->num_words;
216 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
217
Manuel Pégourié-Gonnardad166d82019-11-04 15:37:42 +0100218 if (curve != uECC_secp256r1())
219 return 0;
220
Jarno Lamsa18987a42019-04-24 15:40:43 +0300221 rx[num_n_words - 1] = 0;
222 r[num_n_words - 1] = 0;
223 s[num_n_words - 1] = 0;
224
225 uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
226 uECC_vli_bytesToNative(_public + num_words, public_key + curve->num_bytes,
227 curve->num_bytes);
228 uECC_vli_bytesToNative(r, signature, curve->num_bytes);
229 uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes);
230
231 /* r, s must not be 0. */
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100232 if (uECC_vli_isZero(r) || uECC_vli_isZero(s)) {
Manuel Pégourié-Gonnard10d8e8e2019-11-06 10:30:26 +0100233 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300234 }
235
236 /* r, s must be < n. */
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100237 if (uECC_vli_cmp_unsafe(curve->n, r) != 1 ||
238 uECC_vli_cmp_unsafe(curve->n, s) != 1) {
Manuel Pégourié-Gonnard10d8e8e2019-11-06 10:30:26 +0100239 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300240 }
241
242 /* Calculate u1 and u2. */
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100243 uECC_vli_modInv(z, s, curve->n); /* z = 1/s */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300244 u1[num_n_words - 1] = 0;
245 bits2int(u1, message_hash, hash_size, curve);
Manuel Pégourié-Gonnard3e20adf2019-11-04 15:00:43 +0100246 uECC_vli_modMult(u1, u1, z, curve->n); /* u1 = e/s */
247 uECC_vli_modMult(u2, r, z, curve->n); /* u2 = r/s */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300248
249 /* Calculate sum = G + Q. */
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100250 uECC_vli_set(sum, _public);
251 uECC_vli_set(sum + num_words, _public + num_words);
252 uECC_vli_set(tx, curve->G);
253 uECC_vli_set(ty, curve->G + num_words);
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100254 uECC_vli_modSub(z, sum, tx, curve->p); /* z = x2 - x1 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300255 XYcZ_add(tx, ty, sum, sum + num_words, curve);
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100256 uECC_vli_modInv(z, z, curve->p); /* z = 1/z */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100257 apply_z(sum, sum + num_words, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300258
259 /* Use Shamir's trick to calculate u1*G + u2*Q */
260 points[0] = 0;
261 points[1] = curve->G;
262 points[2] = _public;
263 points[3] = sum;
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100264 num_bits = smax(uECC_vli_numBits(u1),
265 uECC_vli_numBits(u2));
Jarno Lamsa18987a42019-04-24 15:40:43 +0300266
267 point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
268 ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100269 uECC_vli_set(rx, point);
270 uECC_vli_set(ry, point + num_words);
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100271 uECC_vli_clear(z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300272 z[0] = 1;
273
274 for (i = num_bits - 2; i >= 0; --i) {
275 uECC_word_t index;
276 curve->double_jacobian(rx, ry, z, curve);
277
278 index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
279 point = points[index];
280 if (point) {
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100281 uECC_vli_set(tx, point);
282 uECC_vli_set(ty, point + num_words);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100283 apply_z(tx, ty, z);
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100284 uECC_vli_modSub(tz, rx, tx, curve->p); /* Z = x2 - x1 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300285 XYcZ_add(tx, ty, rx, ry, curve);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100286 uECC_vli_modMult_fast(z, z, tz);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300287 }
288 }
289
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100290 uECC_vli_modInv(z, z, curve->p); /* Z = 1/Z */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100291 apply_z(rx, ry, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300292
293 /* v = x1 (mod n) */
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100294 if (uECC_vli_cmp_unsafe(curve->n, rx) != 1) {
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100295 uECC_vli_sub(rx, rx, curve->n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300296 }
297
298 /* Accept only if v == r. */
Manuel Pégourié-Gonnarde6d6f172019-11-06 11:14:38 +0100299 diff = uECC_vli_equal(rx, r);
300 if (diff == 0) {
Manuel Pégourié-Gonnard72a8c9e2019-11-08 10:21:00 +0100301 mbedtls_platform_enforce_volatile_reads();
Manuel Pégourié-Gonnarde6d6f172019-11-06 11:14:38 +0100302 if (diff == 0) {
303 return UECC_SUCCESS;
304 }
305 else {
306 return UECC_ATTACK_DETECTED;
307 }
308 }
Manuel Pégourié-Gonnard10d8e8e2019-11-06 10:30:26 +0100309
310 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300311}