blob: 6285cf357b486d6e66baf3d61fe2c46802bf950f [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,
Andrzej Kurek0919b142020-07-06 15:28:59 -040015 * this list of conditions and the following disclaimer.
Jarno Lamsa18987a42019-04-24 15:40:43 +030016 * * Redistributions in binary form must reproduce the above copyright notice,
Andrzej Kurek0919b142020-07-06 15:28:59 -040017 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
Jarno Lamsa18987a42019-04-24 15:40:43 +030019 *
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 *
Andrzej Kurek0919b142020-07-06 15:28:59 -040039 * - Redistributions of source code must retain the above copyright notice,
40 * this list of conditions and the following disclaimer.
Jarno Lamsa18987a42019-04-24 15:40:43 +030041 *
Andrzej Kurek0919b142020-07-06 15:28:59 -040042 * - 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.
Jarno Lamsa18987a42019-04-24 15:40:43 +030045 *
Andrzej Kurek0919b142020-07-06 15:28:59 -040046 * - 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.
Jarno Lamsa18987a42019-04-24 15:40:43 +030049 *
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
Jarno Lamsa18987a42019-04-24 15:40:43 +030069#include <tinycrypt/ecc.h>
70#include <tinycrypt/ecc_dh.h>
71#include <string.h>
Jarno Lamsa187fbb12019-04-25 09:03:19 +030072#include "mbedtls/platform_util.h"
Jarno Lamsa18987a42019-04-24 15:40:43 +030073
Jarno Lamsa18987a42019-04-24 15:40:43 +030074int uECC_make_key_with_d(uint8_t *public_key, uint8_t *private_key,
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +010075 unsigned int *d)
Jarno Lamsa18987a42019-04-24 15:40:43 +030076{
Andrzej Kurekfd56f402020-05-25 11:52:05 -040077 int ret = UECC_FAULT_DETECTED;
Jarno Lamsa18987a42019-04-24 15:40:43 +030078 uECC_word_t _private[NUM_ECC_WORDS];
79 uECC_word_t _public[NUM_ECC_WORDS * 2];
80
81 /* This function is designed for test purposes-only (such as validating NIST
82 * test vectors) as it uses a provided value for d instead of generating
83 * it uniformly at random. */
Piotr Nowicki305a5ec2020-08-10 17:42:18 +020084 if( mbedtls_platform_memcpy (_private, d, NUM_ECC_BYTES) != _private )
85 {
86 goto exit;
87 }
Jarno Lamsa18987a42019-04-24 15:40:43 +030088
89 /* Computing public-key from private: */
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +010090 ret = EccPoint_compute_public_key(_public, _private);
91 if (ret != UECC_SUCCESS) {
92 goto exit;
Jarno Lamsa18987a42019-04-24 15:40:43 +030093 }
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +010094
95 /* Converting buffers to correct bit order: */
96 uECC_vli_nativeToBytes(private_key,
Andrzej Kurek0919b142020-07-06 15:28:59 -040097 BITS_TO_BYTES(NUM_ECC_BITS),
98 _private);
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +010099 uECC_vli_nativeToBytes(public_key,
Andrzej Kurek0919b142020-07-06 15:28:59 -0400100 NUM_ECC_BYTES,
101 _public);
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100102 uECC_vli_nativeToBytes(public_key + NUM_ECC_BYTES,
Andrzej Kurek0919b142020-07-06 15:28:59 -0400103 NUM_ECC_BYTES,
104 _public + NUM_ECC_WORDS);
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100105
106exit:
107 /* erasing temporary buffer used to store secret: */
108 mbedtls_platform_memset(_private, 0, NUM_ECC_BYTES);
109
110 return ret;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300111}
112
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100113int uECC_make_key(uint8_t *public_key, uint8_t *private_key)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300114{
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400115 int ret = UECC_FAULT_DETECTED;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300116 uECC_word_t _random[NUM_ECC_WORDS * 2];
117 uECC_word_t _private[NUM_ECC_WORDS];
118 uECC_word_t _public[NUM_ECC_WORDS * 2];
119 uECC_word_t tries;
Andrzej Kurek74f7d0f2020-07-06 14:28:12 -0400120 volatile uint8_t *public_key_dup = public_key;
121 volatile uint8_t *private_key_dup = private_key;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300122
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 ||
Andrzej Kurek090365f2020-06-08 11:00:51 -0400127 rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE) != 2 * NUM_ECC_WORDS*uECC_WORD_SIZE) {
Andrzej Kurek0919b142020-07-06 15:28:59 -0400128 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300129 }
130
131 /* computing modular reduction of _random (see FIPS 186.4 B.4.1): */
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100132 uECC_vli_mmod(_private, _random, curve_n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300133
134 /* Computing public-key from private: */
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100135 ret = EccPoint_compute_public_key(_public, _private);
136 /* don't try again if a fault was detected */
137 if (ret == UECC_FAULT_DETECTED) {
138 return ret;
139 }
140 if (ret == UECC_SUCCESS) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300141
142 /* Converting buffers to correct bit order: */
143 uECC_vli_nativeToBytes(private_key,
Andrzej Kurek0919b142020-07-06 15:28:59 -0400144 BITS_TO_BYTES(NUM_ECC_BITS),
145 _private);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300146 uECC_vli_nativeToBytes(public_key,
Andrzej Kurek0919b142020-07-06 15:28:59 -0400147 NUM_ECC_BYTES,
148 _public);
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +0100149 uECC_vli_nativeToBytes(public_key + NUM_ECC_BYTES,
Andrzej Kurek0919b142020-07-06 15:28:59 -0400150 NUM_ECC_BYTES,
151 _public + NUM_ECC_WORDS);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300152
153 /* erasing temporary buffer that stored secret: */
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200154 mbedtls_platform_memset(_private, 0, NUM_ECC_BYTES);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300155
Andrzej Kurek0919b142020-07-06 15:28:59 -0400156 if (private_key == private_key_dup && public_key == public_key_dup) {
157 return UECC_SUCCESS;
Andrzej Kurek74f7d0f2020-07-06 14:28:12 -0400158 }
Andrzej Kurekca609372020-07-08 03:19:02 -0400159 /* Erase key in case of FI */
160 mbedtls_platform_memset(public_key, 0, 2*NUM_ECC_BYTES);
Andrzej Kurek74f7d0f2020-07-06 14:28:12 -0400161 return UECC_FAULT_DETECTED;
Andrzej Kurek0919b142020-07-06 15:28:59 -0400162 }
Jarno Lamsa18987a42019-04-24 15:40:43 +0300163 }
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100164 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300165}
166
167int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key,
Andrzej Kurek0919b142020-07-06 15:28:59 -0400168 uint8_t *secret)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300169{
170
171 uECC_word_t _public[NUM_ECC_WORDS * 2];
172 uECC_word_t _private[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard17659332019-11-21 09:27:38 +0100173 wordcount_t num_words = NUM_ECC_WORDS;
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +0100174 wordcount_t num_bytes = NUM_ECC_BYTES;
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400175 int r = UECC_FAULT_DETECTED;
Andrzej Kurek74f7d0f2020-07-06 14:28:12 -0400176
Jarno Lamsa18987a42019-04-24 15:40:43 +0300177 /* Converting buffers to correct bit order: */
178 uECC_vli_bytesToNative(_private,
Andrzej Kurek0919b142020-07-06 15:28:59 -0400179 private_key,
180 BITS_TO_BYTES(NUM_ECC_BITS));
Jarno Lamsa18987a42019-04-24 15:40:43 +0300181 uECC_vli_bytesToNative(_public,
Andrzej Kurek0919b142020-07-06 15:28:59 -0400182 public_key,
183 num_bytes);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300184 uECC_vli_bytesToNative(_public + num_words,
Andrzej Kurek0919b142020-07-06 15:28:59 -0400185 public_key + num_bytes,
186 num_bytes);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300187
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100188 r = EccPoint_mult_safer(_public, _public, _private);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300189 uECC_vli_nativeToBytes(secret, num_bytes, _public);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300190
Jarno Lamsa18987a42019-04-24 15:40:43 +0300191 /* erasing temporary buffer used to store secret: */
Piotr Nowickia6348ed2020-06-29 15:03:56 +0200192 if (_private == mbedtls_platform_zeroize(_private, sizeof(_private))) {
193 return r;
194 }
Jarno Lamsa18987a42019-04-24 15:40:43 +0300195
Piotr Nowickia6348ed2020-06-29 15:03:56 +0200196 return UECC_FAULT_DETECTED;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300197}