blob: 08b92a423511367aab265df1fe6f9d0bb30d85db [file] [log] [blame]
Jarno Lamsa18987a42019-04-24 15:40:43 +03001/* ec_dh.c - TinyCrypt implementation of EC-DH */
2
Jarno Lamsa187fbb12019-04-25 09:03:19 +03003/*
Jarno Lamsa18987a42019-04-24 15:40:43 +03004 * Copyright (c) 2014, Kenneth MacKay
5 * All rights reserved.
6 *
Simon Butchercffedb52019-09-09 16:28:54 +01007 * SPDX-License-Identifier: BSD-3-Clause
8 *
Jarno Lamsa18987a42019-04-24 15:40:43 +03009 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions are met:
35 *
36 * - Redistributions of source code must retain the above copyright notice,
37 * this list of conditions and the following disclaimer.
38 *
39 * - Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 *
43 * - Neither the name of Intel Corporation nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
58 */
Hanno Becker36ae7582019-07-23 15:52:35 +010059
60#if !defined(MBEDTLS_CONFIG_FILE)
61#include "mbedtls/config.h"
62#else
63#include MBEDTLS_CONFIG_FILE
64#endif
65
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +020066#if defined(MBEDTLS_USE_TINYCRYPT)
Jarno Lamsa18987a42019-04-24 15:40:43 +030067#include <tinycrypt/ecc.h>
68#include <tinycrypt/ecc_dh.h>
69#include <string.h>
Jarno Lamsa187fbb12019-04-25 09:03:19 +030070#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
78int uECC_make_key_with_d(uint8_t *public_key, uint8_t *private_key,
79 unsigned int *d, uECC_Curve curve)
80{
81
82 uECC_word_t _private[NUM_ECC_WORDS];
83 uECC_word_t _public[NUM_ECC_WORDS * 2];
84
85 /* This function is designed for test purposes-only (such as validating NIST
86 * test vectors) as it uses a provided value for d instead of generating
87 * it uniformly at random. */
88 memcpy (_private, d, NUM_ECC_BYTES);
89
90 /* Computing public-key from private: */
91 if (EccPoint_compute_public_key(_public, _private, curve)) {
92
93 /* Converting buffers to correct bit order: */
94 uECC_vli_nativeToBytes(private_key,
95 BITS_TO_BYTES(curve->num_n_bits),
96 _private);
97 uECC_vli_nativeToBytes(public_key,
98 curve->num_bytes,
99 _public);
100 uECC_vli_nativeToBytes(public_key + curve->num_bytes,
101 curve->num_bytes,
102 _public + curve->num_words);
103
104 /* erasing temporary buffer used to store secret: */
105 memset(_private, 0, NUM_ECC_BYTES);
106
107 return 1;
108 }
109 return 0;
110}
111
112int uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve)
113{
114
115 uECC_word_t _random[NUM_ECC_WORDS * 2];
116 uECC_word_t _private[NUM_ECC_WORDS];
117 uECC_word_t _public[NUM_ECC_WORDS * 2];
118 uECC_word_t tries;
119
120 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
121 /* Generating _private uniformly at random: */
122 uECC_RNG_Function rng_function = uECC_get_rng();
123 if (!rng_function ||
124 !rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE)) {
125 return 0;
126 }
127
128 /* computing modular reduction of _random (see FIPS 186.4 B.4.1): */
129 uECC_vli_mmod(_private, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));
130
131 /* Computing public-key from private: */
132 if (EccPoint_compute_public_key(_public, _private, curve)) {
133
134 /* Converting buffers to correct bit order: */
135 uECC_vli_nativeToBytes(private_key,
136 BITS_TO_BYTES(curve->num_n_bits),
137 _private);
138 uECC_vli_nativeToBytes(public_key,
139 curve->num_bytes,
140 _public);
141 uECC_vli_nativeToBytes(public_key + curve->num_bytes,
142 curve->num_bytes,
143 _public + curve->num_words);
144
145 /* erasing temporary buffer that stored secret: */
146 memset(_private, 0, NUM_ECC_BYTES);
147
148 return 1;
149 }
150 }
151 return 0;
152}
153
154int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key,
155 uint8_t *secret, uECC_Curve curve)
156{
157
158 uECC_word_t _public[NUM_ECC_WORDS * 2];
159 uECC_word_t _private[NUM_ECC_WORDS];
160
161 uECC_word_t tmp[NUM_ECC_WORDS];
162 uECC_word_t *p2[2] = {_private, tmp};
163 uECC_word_t *initial_Z = 0;
164 uECC_word_t carry;
165 wordcount_t num_words = curve->num_words;
166 wordcount_t num_bytes = curve->num_bytes;
167 int r;
168
169 /* Converting buffers to correct bit order: */
170 uECC_vli_bytesToNative(_private,
171 private_key,
172 BITS_TO_BYTES(curve->num_n_bits));
173 uECC_vli_bytesToNative(_public,
174 public_key,
175 num_bytes);
176 uECC_vli_bytesToNative(_public + num_words,
177 public_key + num_bytes,
178 num_bytes);
179
180 /* Regularize the bitcount for the private key so that attackers cannot use a
181 * side channel attack to learn the number of leading zeros. */
182 carry = regularize_k(_private, _private, tmp, curve);
183
184 /* If an RNG function was specified, try to get a random initial Z value to
185 * improve protection against side-channel attacks. */
186 if (g_rng_function) {
187 if (!uECC_generate_random_int(p2[carry], curve->p, num_words)) {
188 r = 0;
189 goto clear_and_out;
190 }
191 initial_Z = p2[carry];
192 }
193
194 EccPoint_mult(_public, _public, p2[!carry], initial_Z, curve->num_n_bits + 1,
195 curve);
196
197 uECC_vli_nativeToBytes(secret, num_bytes, _public);
198 r = !EccPoint_isZero(_public, curve);
199
200clear_and_out:
201 /* erasing temporary buffer used to store secret: */
Jarno Lamsa187fbb12019-04-25 09:03:19 +0300202 mbedtls_platform_zeroize(p2, sizeof(p2));
203 mbedtls_platform_zeroize(tmp, sizeof(tmp));
204 mbedtls_platform_zeroize(_private, sizeof(_private));
Jarno Lamsa18987a42019-04-24 15:40:43 +0300205
206 return r;
207}
Jarno Lamsa46132202019-04-29 14:29:52 +0300208#else
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +0200209typedef int mbedtls_dummy_tinycrypt_def;
210#endif /* MBEDTLS_USE_TINYCRYPT */