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