Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1 | /* ec_dh.c - TinyCrypt implementation of EC-DH */ |
| 2 | |
Jarno Lamsa | 187fbb1 | 2019-04-25 09:03:19 +0300 | [diff] [blame] | 3 | /* |
Simon Butcher | 92c3d1f | 2019-09-09 17:25:08 +0100 | [diff] [blame] | 4 | * Copyright (c) 2019, Arm Limited (or its affiliates), All Rights Reserved. |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | */ |
| 7 | |
| 8 | /* |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 9 | * 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 Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 15 | * this list of conditions and the following disclaimer. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 16 | * * Redistributions in binary form must reproduce the above copyright notice, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 17 | * this list of conditions and the following disclaimer in the documentation |
| 18 | * and/or other materials provided with the distribution. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 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 | * |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 39 | * - Redistributions of source code must retain the above copyright notice, |
| 40 | * this list of conditions and the following disclaimer. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 41 | * |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 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. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 45 | * |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 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. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 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 Becker | 36ae758 | 2019-07-23 15:52:35 +0100 | [diff] [blame] | 62 | |
| 63 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 64 | #include "mbedtls/config.h" |
| 65 | #else |
| 66 | #include MBEDTLS_CONFIG_FILE |
| 67 | #endif |
| 68 | |
Andrzej Kurek | 7e62c31 | 2020-10-14 12:02:40 +0200 | [diff] [blame] | 69 | #if defined(MBEDTLS_USE_TINYCRYPT) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 70 | #include <tinycrypt/ecc.h> |
| 71 | #include <tinycrypt/ecc_dh.h> |
| 72 | #include <string.h> |
Jarno Lamsa | 187fbb1 | 2019-04-25 09:03:19 +0300 | [diff] [blame] | 73 | #include "mbedtls/platform_util.h" |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 74 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 75 | int uECC_make_key_with_d(uint8_t *public_key, uint8_t *private_key, |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 76 | unsigned int *d) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 77 | { |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 78 | int ret = UECC_FAULT_DETECTED; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 79 | uECC_word_t _private[NUM_ECC_WORDS]; |
| 80 | uECC_word_t _public[NUM_ECC_WORDS * 2]; |
| 81 | |
| 82 | /* This function is designed for test purposes-only (such as validating NIST |
| 83 | * test vectors) as it uses a provided value for d instead of generating |
| 84 | * it uniformly at random. */ |
Piotr Nowicki | 305a5ec | 2020-08-10 17:42:18 +0200 | [diff] [blame] | 85 | if( mbedtls_platform_memcpy (_private, d, NUM_ECC_BYTES) != _private ) |
| 86 | { |
| 87 | goto exit; |
| 88 | } |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 89 | |
| 90 | /* Computing public-key from private: */ |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 91 | ret = EccPoint_compute_public_key(_public, _private); |
| 92 | if (ret != UECC_SUCCESS) { |
| 93 | goto exit; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 94 | } |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 95 | |
| 96 | /* Converting buffers to correct bit order: */ |
| 97 | uECC_vli_nativeToBytes(private_key, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 98 | BITS_TO_BYTES(NUM_ECC_BITS), |
| 99 | _private); |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 100 | uECC_vli_nativeToBytes(public_key, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 101 | NUM_ECC_BYTES, |
| 102 | _public); |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 103 | uECC_vli_nativeToBytes(public_key + NUM_ECC_BYTES, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 104 | NUM_ECC_BYTES, |
| 105 | _public + NUM_ECC_WORDS); |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 106 | |
| 107 | exit: |
| 108 | /* erasing temporary buffer used to store secret: */ |
| 109 | mbedtls_platform_memset(_private, 0, NUM_ECC_BYTES); |
| 110 | |
| 111 | return ret; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 112 | } |
| 113 | |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 114 | int uECC_make_key(uint8_t *public_key, uint8_t *private_key) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 115 | { |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 116 | int ret = UECC_FAULT_DETECTED; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 117 | uECC_word_t _random[NUM_ECC_WORDS * 2]; |
| 118 | uECC_word_t _private[NUM_ECC_WORDS]; |
| 119 | uECC_word_t _public[NUM_ECC_WORDS * 2]; |
| 120 | uECC_word_t tries; |
Andrzej Kurek | 74f7d0f | 2020-07-06 14:28:12 -0400 | [diff] [blame] | 121 | volatile uint8_t *public_key_dup = public_key; |
| 122 | volatile uint8_t *private_key_dup = private_key; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 123 | |
| 124 | for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) { |
| 125 | /* Generating _private uniformly at random: */ |
| 126 | uECC_RNG_Function rng_function = uECC_get_rng(); |
| 127 | if (!rng_function || |
Andrzej Kurek | 090365f | 2020-06-08 11:00:51 -0400 | [diff] [blame] | 128 | rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE) != 2 * NUM_ECC_WORDS*uECC_WORD_SIZE) { |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 129 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* computing modular reduction of _random (see FIPS 186.4 B.4.1): */ |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 133 | uECC_vli_mmod(_private, _random, curve_n); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 134 | |
| 135 | /* Computing public-key from private: */ |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 136 | ret = EccPoint_compute_public_key(_public, _private); |
| 137 | /* don't try again if a fault was detected */ |
| 138 | if (ret == UECC_FAULT_DETECTED) { |
| 139 | return ret; |
| 140 | } |
| 141 | if (ret == UECC_SUCCESS) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 142 | |
| 143 | /* Converting buffers to correct bit order: */ |
| 144 | uECC_vli_nativeToBytes(private_key, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 145 | BITS_TO_BYTES(NUM_ECC_BITS), |
| 146 | _private); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 147 | uECC_vli_nativeToBytes(public_key, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 148 | NUM_ECC_BYTES, |
| 149 | _public); |
Manuel Pégourié-Gonnard | 72c1764 | 2019-11-21 09:34:09 +0100 | [diff] [blame] | 150 | uECC_vli_nativeToBytes(public_key + NUM_ECC_BYTES, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 151 | NUM_ECC_BYTES, |
| 152 | _public + NUM_ECC_WORDS); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 153 | |
| 154 | /* erasing temporary buffer that stored secret: */ |
Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 155 | mbedtls_platform_memset(_private, 0, NUM_ECC_BYTES); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 156 | |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 157 | if (private_key == private_key_dup && public_key == public_key_dup) { |
| 158 | return UECC_SUCCESS; |
Andrzej Kurek | 74f7d0f | 2020-07-06 14:28:12 -0400 | [diff] [blame] | 159 | } |
Andrzej Kurek | ca60937 | 2020-07-08 03:19:02 -0400 | [diff] [blame] | 160 | /* Erase key in case of FI */ |
| 161 | mbedtls_platform_memset(public_key, 0, 2*NUM_ECC_BYTES); |
Andrzej Kurek | 74f7d0f | 2020-07-06 14:28:12 -0400 | [diff] [blame] | 162 | return UECC_FAULT_DETECTED; |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 163 | } |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 164 | } |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 165 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 169 | uint8_t *secret) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 170 | { |
| 171 | |
| 172 | uECC_word_t _public[NUM_ECC_WORDS * 2]; |
| 173 | uECC_word_t _private[NUM_ECC_WORDS]; |
Manuel Pégourié-Gonnard | 1765933 | 2019-11-21 09:27:38 +0100 | [diff] [blame] | 174 | wordcount_t num_words = NUM_ECC_WORDS; |
Manuel Pégourié-Gonnard | 72c1764 | 2019-11-21 09:34:09 +0100 | [diff] [blame] | 175 | wordcount_t num_bytes = NUM_ECC_BYTES; |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 176 | int r = UECC_FAULT_DETECTED; |
Andrzej Kurek | 74f7d0f | 2020-07-06 14:28:12 -0400 | [diff] [blame] | 177 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 178 | /* Converting buffers to correct bit order: */ |
| 179 | uECC_vli_bytesToNative(_private, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 180 | private_key, |
| 181 | BITS_TO_BYTES(NUM_ECC_BITS)); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 182 | uECC_vli_bytesToNative(_public, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 183 | public_key, |
| 184 | num_bytes); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 185 | uECC_vli_bytesToNative(_public + num_words, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 186 | public_key + num_bytes, |
| 187 | num_bytes); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 188 | |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 189 | r = EccPoint_mult_safer(_public, _public, _private); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 190 | uECC_vli_nativeToBytes(secret, num_bytes, _public); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 191 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 192 | /* erasing temporary buffer used to store secret: */ |
Piotr Nowicki | a6348ed | 2020-06-29 15:03:56 +0200 | [diff] [blame] | 193 | if (_private == mbedtls_platform_zeroize(_private, sizeof(_private))) { |
| 194 | return r; |
| 195 | } |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 196 | |
Piotr Nowicki | a6348ed | 2020-06-29 15:03:56 +0200 | [diff] [blame] | 197 | return UECC_FAULT_DETECTED; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 198 | } |
Andrzej Kurek | 7e62c31 | 2020-10-14 12:02:40 +0200 | [diff] [blame] | 199 | #endif /* MBEDTLS_USE_TINYCRYPT */ |