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, |
| 15 | * this list of conditions and the following disclaimer. |
| 16 | * * Redistributions in binary form must reproduce the above copyright notice, |
| 17 | * this list of conditions and the following disclaimer in the documentation |
| 18 | * and/or other materials provided with the distribution. |
| 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 | * |
| 39 | * - Redistributions of source code must retain the above copyright notice, |
| 40 | * this list of conditions and the following disclaimer. |
| 41 | * |
| 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. |
| 45 | * |
| 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. |
| 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 | |
Manuel Pégourié-Gonnard | afdc1b5 | 2019-05-09 11:24:11 +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 | |
| 75 | #if default_RNG_defined |
| 76 | static uECC_RNG_Function g_rng_function = &default_CSPRNG; |
| 77 | #else |
| 78 | static uECC_RNG_Function g_rng_function = 0; |
| 79 | #endif |
| 80 | |
| 81 | int uECC_make_key_with_d(uint8_t *public_key, uint8_t *private_key, |
| 82 | unsigned int *d, uECC_Curve curve) |
| 83 | { |
| 84 | |
| 85 | uECC_word_t _private[NUM_ECC_WORDS]; |
| 86 | uECC_word_t _public[NUM_ECC_WORDS * 2]; |
| 87 | |
| 88 | /* This function is designed for test purposes-only (such as validating NIST |
| 89 | * test vectors) as it uses a provided value for d instead of generating |
| 90 | * it uniformly at random. */ |
| 91 | memcpy (_private, d, NUM_ECC_BYTES); |
| 92 | |
| 93 | /* Computing public-key from private: */ |
| 94 | if (EccPoint_compute_public_key(_public, _private, curve)) { |
| 95 | |
| 96 | /* Converting buffers to correct bit order: */ |
| 97 | uECC_vli_nativeToBytes(private_key, |
| 98 | BITS_TO_BYTES(curve->num_n_bits), |
| 99 | _private); |
| 100 | uECC_vli_nativeToBytes(public_key, |
| 101 | curve->num_bytes, |
| 102 | _public); |
| 103 | uECC_vli_nativeToBytes(public_key + curve->num_bytes, |
| 104 | curve->num_bytes, |
| 105 | _public + curve->num_words); |
| 106 | |
| 107 | /* erasing temporary buffer used to store secret: */ |
| 108 | memset(_private, 0, NUM_ECC_BYTES); |
| 109 | |
| 110 | return 1; |
| 111 | } |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | int uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve) |
| 116 | { |
| 117 | |
| 118 | uECC_word_t _random[NUM_ECC_WORDS * 2]; |
| 119 | uECC_word_t _private[NUM_ECC_WORDS]; |
| 120 | uECC_word_t _public[NUM_ECC_WORDS * 2]; |
| 121 | uECC_word_t tries; |
| 122 | |
| 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 || |
| 127 | !rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE)) { |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /* computing modular reduction of _random (see FIPS 186.4 B.4.1): */ |
| 132 | uECC_vli_mmod(_private, _random, curve->n, BITS_TO_WORDS(curve->num_n_bits)); |
| 133 | |
| 134 | /* Computing public-key from private: */ |
| 135 | if (EccPoint_compute_public_key(_public, _private, curve)) { |
| 136 | |
| 137 | /* Converting buffers to correct bit order: */ |
| 138 | uECC_vli_nativeToBytes(private_key, |
| 139 | BITS_TO_BYTES(curve->num_n_bits), |
| 140 | _private); |
| 141 | uECC_vli_nativeToBytes(public_key, |
| 142 | curve->num_bytes, |
| 143 | _public); |
| 144 | uECC_vli_nativeToBytes(public_key + curve->num_bytes, |
| 145 | curve->num_bytes, |
| 146 | _public + curve->num_words); |
| 147 | |
| 148 | /* erasing temporary buffer that stored secret: */ |
| 149 | memset(_private, 0, NUM_ECC_BYTES); |
| 150 | |
| 151 | return 1; |
| 152 | } |
| 153 | } |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key, |
| 158 | uint8_t *secret, uECC_Curve curve) |
| 159 | { |
| 160 | |
| 161 | uECC_word_t _public[NUM_ECC_WORDS * 2]; |
| 162 | uECC_word_t _private[NUM_ECC_WORDS]; |
| 163 | |
| 164 | uECC_word_t tmp[NUM_ECC_WORDS]; |
| 165 | uECC_word_t *p2[2] = {_private, tmp}; |
| 166 | uECC_word_t *initial_Z = 0; |
| 167 | uECC_word_t carry; |
| 168 | wordcount_t num_words = curve->num_words; |
| 169 | wordcount_t num_bytes = curve->num_bytes; |
| 170 | int r; |
| 171 | |
| 172 | /* Converting buffers to correct bit order: */ |
| 173 | uECC_vli_bytesToNative(_private, |
| 174 | private_key, |
| 175 | BITS_TO_BYTES(curve->num_n_bits)); |
| 176 | uECC_vli_bytesToNative(_public, |
| 177 | public_key, |
| 178 | num_bytes); |
| 179 | uECC_vli_bytesToNative(_public + num_words, |
| 180 | public_key + num_bytes, |
| 181 | num_bytes); |
| 182 | |
| 183 | /* Regularize the bitcount for the private key so that attackers cannot use a |
| 184 | * side channel attack to learn the number of leading zeros. */ |
| 185 | carry = regularize_k(_private, _private, tmp, curve); |
| 186 | |
| 187 | /* If an RNG function was specified, try to get a random initial Z value to |
| 188 | * improve protection against side-channel attacks. */ |
| 189 | if (g_rng_function) { |
| 190 | if (!uECC_generate_random_int(p2[carry], curve->p, num_words)) { |
| 191 | r = 0; |
| 192 | goto clear_and_out; |
| 193 | } |
| 194 | initial_Z = p2[carry]; |
| 195 | } |
| 196 | |
| 197 | EccPoint_mult(_public, _public, p2[!carry], initial_Z, curve->num_n_bits + 1, |
| 198 | curve); |
| 199 | |
| 200 | uECC_vli_nativeToBytes(secret, num_bytes, _public); |
| 201 | r = !EccPoint_isZero(_public, curve); |
| 202 | |
| 203 | clear_and_out: |
| 204 | /* erasing temporary buffer used to store secret: */ |
Jarno Lamsa | 187fbb1 | 2019-04-25 09:03:19 +0300 | [diff] [blame] | 205 | mbedtls_platform_zeroize(p2, sizeof(p2)); |
| 206 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
| 207 | mbedtls_platform_zeroize(_private, sizeof(_private)); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 208 | |
| 209 | return r; |
| 210 | } |
Jarno Lamsa | 4613220 | 2019-04-29 14:29:52 +0300 | [diff] [blame] | 211 | #else |
Manuel Pégourié-Gonnard | afdc1b5 | 2019-05-09 11:24:11 +0200 | [diff] [blame] | 212 | typedef int mbedtls_dummy_tinycrypt_def; |
| 213 | #endif /* MBEDTLS_USE_TINYCRYPT */ |