blob: 8aae1a214c6e8c212c01a74fe31e10e70a0de0eb [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 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
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 */
Hanno Becker36ae7582019-07-23 15:52:35 +010057
58#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_dh.h>
67#include <string.h>
Jarno Lamsa187fbb12019-04-25 09:03:19 +030068#include "mbedtls/platform_util.h"
Jarno Lamsa18987a42019-04-24 15:40:43 +030069
70#if default_RNG_defined
71static uECC_RNG_Function g_rng_function = &default_CSPRNG;
72#else
73static uECC_RNG_Function g_rng_function = 0;
74#endif
75
76int uECC_make_key_with_d(uint8_t *public_key, uint8_t *private_key,
77 unsigned int *d, uECC_Curve curve)
78{
79
80 uECC_word_t _private[NUM_ECC_WORDS];
81 uECC_word_t _public[NUM_ECC_WORDS * 2];
82
83 /* This function is designed for test purposes-only (such as validating NIST
84 * test vectors) as it uses a provided value for d instead of generating
85 * it uniformly at random. */
86 memcpy (_private, d, NUM_ECC_BYTES);
87
88 /* Computing public-key from private: */
89 if (EccPoint_compute_public_key(_public, _private, curve)) {
90
91 /* Converting buffers to correct bit order: */
92 uECC_vli_nativeToBytes(private_key,
93 BITS_TO_BYTES(curve->num_n_bits),
94 _private);
95 uECC_vli_nativeToBytes(public_key,
96 curve->num_bytes,
97 _public);
98 uECC_vli_nativeToBytes(public_key + curve->num_bytes,
99 curve->num_bytes,
100 _public + curve->num_words);
101
102 /* erasing temporary buffer used to store secret: */
103 memset(_private, 0, NUM_ECC_BYTES);
104
105 return 1;
106 }
107 return 0;
108}
109
110int uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve)
111{
112
113 uECC_word_t _random[NUM_ECC_WORDS * 2];
114 uECC_word_t _private[NUM_ECC_WORDS];
115 uECC_word_t _public[NUM_ECC_WORDS * 2];
116 uECC_word_t tries;
117
118 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
119 /* Generating _private uniformly at random: */
120 uECC_RNG_Function rng_function = uECC_get_rng();
121 if (!rng_function ||
122 !rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE)) {
123 return 0;
124 }
125
126 /* computing modular reduction of _random (see FIPS 186.4 B.4.1): */
127 uECC_vli_mmod(_private, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));
128
129 /* Computing public-key from private: */
130 if (EccPoint_compute_public_key(_public, _private, curve)) {
131
132 /* Converting buffers to correct bit order: */
133 uECC_vli_nativeToBytes(private_key,
134 BITS_TO_BYTES(curve->num_n_bits),
135 _private);
136 uECC_vli_nativeToBytes(public_key,
137 curve->num_bytes,
138 _public);
139 uECC_vli_nativeToBytes(public_key + curve->num_bytes,
140 curve->num_bytes,
141 _public + curve->num_words);
142
143 /* erasing temporary buffer that stored secret: */
144 memset(_private, 0, NUM_ECC_BYTES);
145
146 return 1;
147 }
148 }
149 return 0;
150}
151
152int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key,
153 uint8_t *secret, uECC_Curve curve)
154{
155
156 uECC_word_t _public[NUM_ECC_WORDS * 2];
157 uECC_word_t _private[NUM_ECC_WORDS];
158
159 uECC_word_t tmp[NUM_ECC_WORDS];
160 uECC_word_t *p2[2] = {_private, tmp};
161 uECC_word_t *initial_Z = 0;
162 uECC_word_t carry;
163 wordcount_t num_words = curve->num_words;
164 wordcount_t num_bytes = curve->num_bytes;
165 int r;
166
167 /* Converting buffers to correct bit order: */
168 uECC_vli_bytesToNative(_private,
169 private_key,
170 BITS_TO_BYTES(curve->num_n_bits));
171 uECC_vli_bytesToNative(_public,
172 public_key,
173 num_bytes);
174 uECC_vli_bytesToNative(_public + num_words,
175 public_key + num_bytes,
176 num_bytes);
177
178 /* Regularize the bitcount for the private key so that attackers cannot use a
179 * side channel attack to learn the number of leading zeros. */
180 carry = regularize_k(_private, _private, tmp, curve);
181
182 /* If an RNG function was specified, try to get a random initial Z value to
183 * improve protection against side-channel attacks. */
184 if (g_rng_function) {
185 if (!uECC_generate_random_int(p2[carry], curve->p, num_words)) {
186 r = 0;
187 goto clear_and_out;
188 }
189 initial_Z = p2[carry];
190 }
191
192 EccPoint_mult(_public, _public, p2[!carry], initial_Z, curve->num_n_bits + 1,
193 curve);
194
195 uECC_vli_nativeToBytes(secret, num_bytes, _public);
196 r = !EccPoint_isZero(_public, curve);
197
198clear_and_out:
199 /* erasing temporary buffer used to store secret: */
Jarno Lamsa187fbb12019-04-25 09:03:19 +0300200 mbedtls_platform_zeroize(p2, sizeof(p2));
201 mbedtls_platform_zeroize(tmp, sizeof(tmp));
202 mbedtls_platform_zeroize(_private, sizeof(_private));
Jarno Lamsa18987a42019-04-24 15:40:43 +0300203
204 return r;
205}
Jarno Lamsa46132202019-04-29 14:29:52 +0300206#else
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +0200207typedef int mbedtls_dummy_tinycrypt_def;
208#endif /* MBEDTLS_USE_TINYCRYPT */