blob: a3893d37f4d2df2956205ff14d9c05dcad5072f5 [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];
119 uECC_word_t carry;
120 wordcount_t num_words = curve->num_words;
121 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
122 bitcount_t num_n_bits = curve->num_n_bits;
123
124 /* Make sure 0 < k < curve_n */
125 if (uECC_vli_isZero(k, num_words) ||
126 uECC_vli_cmp(curve->n, k, num_n_words) != 1) {
127 return 0;
128 }
129
130 carry = regularize_k(k, tmp, s, curve);
131 EccPoint_mult(p, curve->G, k2[!carry], 0, num_n_bits + 1, curve);
132 if (uECC_vli_isZero(p, num_words)) {
133 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) {
139 uECC_vli_clear(tmp, num_n_words);
140 tmp[0] = 1;
141 }
142 else if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
143 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 */
148 uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k' = rand * k */
149 uECC_vli_modInv(k, k, curve->n, num_n_words); /* k = 1 / k' */
150 uECC_vli_modMult(k, k, tmp, curve->n, num_n_words); /* k = 1 / k */
151
152 uECC_vli_nativeToBytes(signature, curve->num_bytes, p); /* store r */
153
154 /* tmp = d: */
155 uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(curve->num_n_bits));
156
157 s[num_n_words - 1] = 0;
158 uECC_vli_set(s, p, num_words);
159 uECC_vli_modMult(s, tmp, s, curve->n, num_n_words); /* s = r*d */
160
161 bits2int(tmp, message_hash, hash_size, curve);
162 uECC_vli_modAdd(s, tmp, s, curve->n, num_n_words); /* s = e + r*d */
163 uECC_vli_modMult(s, s, k, curve->n, num_n_words); /* s = (e + r*d) / k */
164 if (uECC_vli_numBits(s, num_n_words) > (bitcount_t)curve->num_bytes * 8) {
165 return 0;
166 }
167
168 uECC_vli_nativeToBytes(signature + curve->num_bytes, curve->num_bytes, s);
169 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):
188 uECC_vli_mmod(k, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));
189
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;
220
221 uECC_word_t _public[NUM_ECC_WORDS * 2];
222 uECC_word_t r[NUM_ECC_WORDS], s[NUM_ECC_WORDS];
223 wordcount_t num_words = curve->num_words;
224 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
225
226 rx[num_n_words - 1] = 0;
227 r[num_n_words - 1] = 0;
228 s[num_n_words - 1] = 0;
229
230 uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
231 uECC_vli_bytesToNative(_public + num_words, public_key + curve->num_bytes,
232 curve->num_bytes);
233 uECC_vli_bytesToNative(r, signature, curve->num_bytes);
234 uECC_vli_bytesToNative(s, signature + curve->num_bytes, curve->num_bytes);
235
236 /* r, s must not be 0. */
237 if (uECC_vli_isZero(r, num_words) || uECC_vli_isZero(s, num_words)) {
238 return 0;
239 }
240
241 /* r, s must be < n. */
242 if (uECC_vli_cmp_unsafe(curve->n, r, num_n_words) != 1 ||
243 uECC_vli_cmp_unsafe(curve->n, s, num_n_words) != 1) {
244 return 0;
245 }
246
247 /* Calculate u1 and u2. */
248 uECC_vli_modInv(z, s, curve->n, num_n_words); /* z = 1/s */
249 u1[num_n_words - 1] = 0;
250 bits2int(u1, message_hash, hash_size, curve);
251 uECC_vli_modMult(u1, u1, z, curve->n, num_n_words); /* u1 = e/s */
252 uECC_vli_modMult(u2, r, z, curve->n, num_n_words); /* u2 = r/s */
253
254 /* Calculate sum = G + Q. */
255 uECC_vli_set(sum, _public, num_words);
256 uECC_vli_set(sum + num_words, _public + num_words, num_words);
257 uECC_vli_set(tx, curve->G, num_words);
258 uECC_vli_set(ty, curve->G + num_words, num_words);
259 uECC_vli_modSub(z, sum, tx, curve->p, num_words); /* z = x2 - x1 */
260 XYcZ_add(tx, ty, sum, sum + num_words, curve);
261 uECC_vli_modInv(z, z, curve->p, num_words); /* z = 1/z */
262 apply_z(sum, sum + num_words, z, curve);
263
264 /* Use Shamir's trick to calculate u1*G + u2*Q */
265 points[0] = 0;
266 points[1] = curve->G;
267 points[2] = _public;
268 points[3] = sum;
269 num_bits = smax(uECC_vli_numBits(u1, num_n_words),
270 uECC_vli_numBits(u2, num_n_words));
271
272 point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
273 ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
274 uECC_vli_set(rx, point, num_words);
275 uECC_vli_set(ry, point + num_words, num_words);
276 uECC_vli_clear(z, num_words);
277 z[0] = 1;
278
279 for (i = num_bits - 2; i >= 0; --i) {
280 uECC_word_t index;
281 curve->double_jacobian(rx, ry, z, curve);
282
283 index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
284 point = points[index];
285 if (point) {
286 uECC_vli_set(tx, point, num_words);
287 uECC_vli_set(ty, point + num_words, num_words);
288 apply_z(tx, ty, z, curve);
289 uECC_vli_modSub(tz, rx, tx, curve->p, num_words); /* Z = x2 - x1 */
290 XYcZ_add(tx, ty, rx, ry, curve);
291 uECC_vli_modMult_fast(z, z, tz, curve);
292 }
293 }
294
295 uECC_vli_modInv(z, z, curve->p, num_words); /* Z = 1/Z */
296 apply_z(rx, ry, z, curve);
297
298 /* v = x1 (mod n) */
299 if (uECC_vli_cmp_unsafe(curve->n, rx, num_n_words) != 1) {
300 uECC_vli_sub(rx, rx, curve->n, num_n_words);
301 }
302
303 /* Accept only if v == r. */
304 return (int)(uECC_vli_equal(rx, r, num_words) == 0);
305}
Jarno Lamsa46132202019-04-29 14:29:52 +0300306#else
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +0200307typedef int mbedtls_dummy_tinycrypt_def;
308#endif /* MBEDTLS_USE_TINYCRYPT */