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