blob: 2df89a5043a60b63c3d3983b0aaf8f81d531bcfd [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];
Jarno Lamsa18987a42019-04-24 15:40:43 +0300117 uECC_word_t p[NUM_ECC_WORDS * 2];
Jarno Lamsa18987a42019-04-24 15:40:43 +0300118 wordcount_t num_words = curve->num_words;
119 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100120 int r;
121
Jarno Lamsa18987a42019-04-24 15:40:43 +0300122
123 /* Make sure 0 < k < curve_n */
124 if (uECC_vli_isZero(k, num_words) ||
125 uECC_vli_cmp(curve->n, k, num_n_words) != 1) {
126 return 0;
127 }
128
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100129 r = EccPoint_mult_safer(p, curve->G, k, curve);
130 if (r == 0 || uECC_vli_isZero(p, num_words)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300131 return 0;
132 }
133
134 /* If an RNG function was specified, get a random number
135 to prevent side channel analysis of k. */
136 if (!g_rng_function) {
137 uECC_vli_clear(tmp, num_n_words);
138 tmp[0] = 1;
139 }
140 else if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
141 return 0;
142 }
143
144 /* Prevent side channel analysis of uECC_vli_modInv() to determine
145 bits of k / the private key by premultiplying by a random number */
146 uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k' = rand * k */
147 uECC_vli_modInv(k, k, curve->n, num_n_words); /* k = 1 / k' */
148 uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k = 1 / k */
149
150 uECC_vli_nativeToBytes(signature, curve->num_bytes, p); /* store r */
151
152 /* tmp = d: */
153 uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(curve->num_n_bits));
154
155 s[num_n_words - 1] = 0;
156 uECC_vli_set(s, p, num_words);
157 uECC_vli_modMult(s, tmp, s, curve->n, num_n_words); /* s = r*d */
158
159 bits2int(tmp, message_hash, hash_size, curve);
160 uECC_vli_modAdd(s, tmp, s, curve->n, num_n_words); /* s = e + r*d */
161 uECC_vli_modMult(s, s, k, curve->n, num_n_words); /* s = (e + r*d) / k */
162 if (uECC_vli_numBits(s, num_n_words) > (bitcount_t)curve->num_bytes * 8) {
163 return 0;
164 }
165
166 uECC_vli_nativeToBytes(signature + curve->num_bytes, curve->num_bytes, s);
167 return 1;
168}
169
170int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
171 unsigned hash_size, uint8_t *signature, uECC_Curve curve)
172{
173 uECC_word_t _random[2*NUM_ECC_WORDS];
174 uECC_word_t k[NUM_ECC_WORDS];
175 uECC_word_t tries;
176
177 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
178 /* Generating _random uniformly at random: */
179 uECC_RNG_Function rng_function = uECC_get_rng();
180 if (!rng_function ||
181 !rng_function((uint8_t *)_random, 2*NUM_ECC_WORDS*uECC_WORD_SIZE)) {
182 return 0;
183 }
184
185 // computing k as modular reduction of _random (see FIPS 186.4 B.5.1):
186 uECC_vli_mmod(k, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));
187
188 if (uECC_sign_with_k(private_key, message_hash, hash_size, k, signature,
189 curve)) {
190 return 1;
191 }
192 }
193 return 0;
194}
195
196static bitcount_t smax(bitcount_t a, bitcount_t b)
197{
198 return (a > b ? a : b);
199}
200
201int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
202 unsigned hash_size, const uint8_t *signature,
203 uECC_Curve curve)
204{
205
206 uECC_word_t u1[NUM_ECC_WORDS], u2[NUM_ECC_WORDS];
207 uECC_word_t z[NUM_ECC_WORDS];
208 uECC_word_t sum[NUM_ECC_WORDS * 2];
209 uECC_word_t rx[NUM_ECC_WORDS];
210 uECC_word_t ry[NUM_ECC_WORDS];
211 uECC_word_t tx[NUM_ECC_WORDS];
212 uECC_word_t ty[NUM_ECC_WORDS];
213 uECC_word_t tz[NUM_ECC_WORDS];
214 const uECC_word_t *points[4];
215 const uECC_word_t *point;
216 bitcount_t num_bits;
217 bitcount_t i;
218
219 uECC_word_t _public[NUM_ECC_WORDS * 2];
220 uECC_word_t r[NUM_ECC_WORDS], s[NUM_ECC_WORDS];
221 wordcount_t num_words = curve->num_words;
222 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
223
224 rx[num_n_words - 1] = 0;
225 r[num_n_words - 1] = 0;
226 s[num_n_words - 1] = 0;
227
228 uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
229 uECC_vli_bytesToNative(_public + num_words, public_key + curve->num_bytes,
230 curve->num_bytes);
231 uECC_vli_bytesToNative(r, signature, curve->num_bytes);
232 uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes);
233
234 /* r, s must not be 0. */
235 if (uECC_vli_isZero(r, num_words) || uECC_vli_isZero(s, num_words)) {
236 return 0;
237 }
238
239 /* r, s must be < n. */
240 if (uECC_vli_cmp_unsafe(curve->n, r, num_n_words) != 1 ||
241 uECC_vli_cmp_unsafe(curve->n, s, num_n_words) != 1) {
242 return 0;
243 }
244
245 /* Calculate u1 and u2. */
246 uECC_vli_modInv(z, s, curve->n, num_n_words); /* z = 1/s */
247 u1[num_n_words - 1] = 0;
248 bits2int(u1, message_hash, hash_size, curve);
249 uECC_vli_modMult(u1, u1, z, curve->n, num_n_words); /* u1 = e/s */
250 uECC_vli_modMult(u2, r, z, curve->n, num_n_words); /* u2 = r/s */
251
252 /* Calculate sum = G + Q. */
253 uECC_vli_set(sum, _public, num_words);
254 uECC_vli_set(sum + num_words, _public + num_words, num_words);
255 uECC_vli_set(tx, curve->G, num_words);
256 uECC_vli_set(ty, curve->G + num_words, num_words);
257 uECC_vli_modSub(z, sum, tx, curve->p, num_words); /* z = x2 - x1 */
258 XYcZ_add(tx, ty, sum, sum + num_words, curve);
259 uECC_vli_modInv(z, z, curve->p, num_words); /* z = 1/z */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100260 apply_z(sum, sum + num_words, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300261
262 /* Use Shamir's trick to calculate u1*G + u2*Q */
263 points[0] = 0;
264 points[1] = curve->G;
265 points[2] = _public;
266 points[3] = sum;
267 num_bits = smax(uECC_vli_numBits(u1, num_n_words),
268 uECC_vli_numBits(u2, num_n_words));
269
270 point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
271 ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
272 uECC_vli_set(rx, point, num_words);
273 uECC_vli_set(ry, point + num_words, num_words);
274 uECC_vli_clear(z, num_words);
275 z[0] = 1;
276
277 for (i = num_bits - 2; i >= 0; --i) {
278 uECC_word_t index;
279 curve->double_jacobian(rx, ry, z, curve);
280
281 index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
282 point = points[index];
283 if (point) {
284 uECC_vli_set(tx, point, num_words);
285 uECC_vli_set(ty, point + num_words, num_words);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100286 apply_z(tx, ty, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300287 uECC_vli_modSub(tz, rx, tx, curve->p, num_words); /* Z = x2 - x1 */
288 XYcZ_add(tx, ty, rx, ry, curve);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100289 uECC_vli_modMult_fast(z, z, tz);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300290 }
291 }
292
293 uECC_vli_modInv(z, z, curve->p, num_words); /* Z = 1/Z */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100294 apply_z(rx, ry, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300295
296 /* v = x1 (mod n) */
297 if (uECC_vli_cmp_unsafe(curve->n, rx, num_n_words) != 1) {
298 uECC_vli_sub(rx, rx, curve->n, num_n_words);
299 }
300
301 /* Accept only if v == r. */
302 return (int)(uECC_vli_equal(rx, r, num_words) == 0);
303}
Jarno Lamsa46132202019-04-29 14:29:52 +0300304#else
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +0200305typedef int mbedtls_dummy_tinycrypt_def;
306#endif /* MBEDTLS_USE_TINYCRYPT */