1 /* ec_dh.c - TinyCrypt implementation of EC-DH */
2 
3 /*
4  * 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  */
57 #include <tinycrypt/constants.h>
58 #include <tinycrypt/ecc.h>
59 #include <tinycrypt/ecc_dh.h>
60 #include <tinycrypt/ecc_platform_specific.h>
61 #include <string.h>
62 
63 #if default_RNG_defined
64 static uECC_RNG_Function g_rng_function = &default_CSPRNG;
65 #else
66 static uECC_RNG_Function g_rng_function = 0;
67 #endif
68 
uECC_make_key_with_d(uint8_t * public_key,uint8_t * private_key,unsigned int * d,uECC_Curve curve)69 int uECC_make_key_with_d(uint8_t *public_key, uint8_t *private_key,
70 			 unsigned int *d, uECC_Curve curve)
71 {
72 
73 	uECC_word_t _private[NUM_ECC_WORDS];
74 	uECC_word_t _public[NUM_ECC_WORDS * 2];
75 
76 	/* This function is designed for test purposes-only (such as validating NIST
77 	 * test vectors) as it uses a provided value for d instead of generating
78 	 * it uniformly at random. */
79 	memcpy (_private, d, NUM_ECC_BYTES);
80 
81 	/* Computing public-key from private: */
82 	if (EccPoint_compute_public_key(_public, _private, curve)) {
83 
84 		/* Converting buffers to correct bit order: */
85 		uECC_vli_nativeToBytes(private_key,
86 				       BITS_TO_BYTES(curve->num_n_bits),
87 				       _private);
88 		uECC_vli_nativeToBytes(public_key,
89 				       curve->num_bytes,
90 				       _public);
91 		uECC_vli_nativeToBytes(public_key + curve->num_bytes,
92 				       curve->num_bytes,
93 				       _public + curve->num_words);
94 
95 		/* erasing temporary buffer used to store secret: */
96 		memset(_private, 0, NUM_ECC_BYTES);
97 
98 		return 1;
99 	}
100 	return 0;
101 }
102 
uECC_make_key(uint8_t * public_key,uint8_t * private_key,uECC_Curve curve)103 int uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve)
104 {
105 
106 	uECC_word_t _random[NUM_ECC_WORDS * 2];
107 	uECC_word_t _private[NUM_ECC_WORDS];
108 	uECC_word_t _public[NUM_ECC_WORDS * 2];
109 	uECC_word_t tries;
110 
111 	for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
112 		/* Generating _private uniformly at random: */
113 		uECC_RNG_Function rng_function = uECC_get_rng();
114 		if (!rng_function ||
115 			!rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE)) {
116         		return 0;
117 		}
118 
119 		/* computing modular reduction of _random (see FIPS 186.4 B.4.1): */
120 		uECC_vli_mmod(_private, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits));
121 
122 		/* Computing public-key from private: */
123 		if (EccPoint_compute_public_key(_public, _private, curve)) {
124 
125 			/* Converting buffers to correct bit order: */
126 			uECC_vli_nativeToBytes(private_key,
127 					       BITS_TO_BYTES(curve->num_n_bits),
128 					       _private);
129 			uECC_vli_nativeToBytes(public_key,
130 					       curve->num_bytes,
131 					       _public);
132 			uECC_vli_nativeToBytes(public_key + curve->num_bytes,
133  					       curve->num_bytes,
134 					       _public + curve->num_words);
135 
136 			/* erasing temporary buffer that stored secret: */
137 			memset(_private, 0, NUM_ECC_BYTES);
138 
139       			return 1;
140     		}
141   	}
142 	return 0;
143 }
144 
uECC_shared_secret(const uint8_t * public_key,const uint8_t * private_key,uint8_t * secret,uECC_Curve curve)145 int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key,
146 		       uint8_t *secret, uECC_Curve curve)
147 {
148 
149 	uECC_word_t _public[NUM_ECC_WORDS * 2];
150 	uECC_word_t _private[NUM_ECC_WORDS];
151 
152 	uECC_word_t tmp[NUM_ECC_WORDS];
153 	uECC_word_t *p2[2] = {_private, tmp};
154 	uECC_word_t *initial_Z = 0;
155 	uECC_word_t carry;
156 	wordcount_t num_words = curve->num_words;
157 	wordcount_t num_bytes = curve->num_bytes;
158 	int r;
159 
160 	/* Converting buffers to correct bit order: */
161 	uECC_vli_bytesToNative(_private,
162       			       private_key,
163 			       BITS_TO_BYTES(curve->num_n_bits));
164 	uECC_vli_bytesToNative(_public,
165       			       public_key,
166 			       num_bytes);
167 	uECC_vli_bytesToNative(_public + num_words,
168 			       public_key + num_bytes,
169 			       num_bytes);
170 
171 	/* Regularize the bitcount for the private key so that attackers cannot use a
172 	 * side channel attack to learn the number of leading zeros. */
173 	carry = regularize_k(_private, _private, tmp, curve);
174 
175 	/* If an RNG function was specified, try to get a random initial Z value to
176 	 * improve protection against side-channel attacks. */
177 	if (g_rng_function) {
178 		if (!uECC_generate_random_int(p2[carry], curve->p, num_words)) {
179 			r = 0;
180 			goto clear_and_out;
181     		}
182     		initial_Z = p2[carry];
183   	}
184 
185 	EccPoint_mult(_public, _public, p2[!carry], initial_Z, curve->num_n_bits + 1,
186 		      curve);
187 
188 	uECC_vli_nativeToBytes(secret, num_bytes, _public);
189 	r = !EccPoint_isZero(_public, curve);
190 
191 clear_and_out:
192 	/* erasing temporary buffer used to store secret: */
193 	memset(p2, 0, sizeof(p2));
194 	__asm__ __volatile__("" :: "g"(p2) : "memory");
195 	memset(tmp, 0, sizeof(tmp));
196 	__asm__ __volatile__("" :: "g"(tmp) : "memory");
197 	memset(_private, 0, sizeof(_private));
198 	__asm__ __volatile__("" :: "g"(_private) : "memory");
199 
200 	return r;
201 }
202