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