| 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 |  | 
| Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 69 | #include <tinycrypt/ecc.h> | 
|  | 70 | #include <tinycrypt/ecc_dh.h> | 
|  | 71 | #include <string.h> | 
| Jarno Lamsa | 187fbb1 | 2019-04-25 09:03:19 +0300 | [diff] [blame] | 72 | #include "mbedtls/platform_util.h" | 
| Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 73 |  | 
| Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 74 | int uECC_make_key_with_d(uint8_t *public_key, uint8_t *private_key, | 
|  | 75 | unsigned int *d, uECC_Curve curve) | 
|  | 76 | { | 
|  | 77 |  | 
|  | 78 | 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. */ | 
| Teppo Järvelin | 91d7938 | 2019-10-02 09:09:31 +0300 | [diff] [blame] | 84 | mbedtls_platform_memcpy (_private, d, NUM_ECC_BYTES); | 
| Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 85 |  | 
|  | 86 | /* Computing public-key from private: */ | 
|  | 87 | if (EccPoint_compute_public_key(_public, _private, curve)) { | 
|  | 88 |  | 
|  | 89 | /* Converting buffers to correct bit order: */ | 
|  | 90 | uECC_vli_nativeToBytes(private_key, | 
|  | 91 | BITS_TO_BYTES(curve->num_n_bits), | 
|  | 92 | _private); | 
|  | 93 | uECC_vli_nativeToBytes(public_key, | 
|  | 94 | curve->num_bytes, | 
|  | 95 | _public); | 
|  | 96 | uECC_vli_nativeToBytes(public_key + curve->num_bytes, | 
|  | 97 | curve->num_bytes, | 
|  | 98 | _public + curve->num_words); | 
|  | 99 |  | 
|  | 100 | /* erasing temporary buffer used to store secret: */ | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 101 | mbedtls_platform_memset(_private, 0, NUM_ECC_BYTES); | 
| Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 102 |  | 
|  | 103 | return 1; | 
|  | 104 | } | 
|  | 105 | return 0; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | int uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve) | 
|  | 109 | { | 
|  | 110 |  | 
|  | 111 | uECC_word_t _random[NUM_ECC_WORDS * 2]; | 
|  | 112 | uECC_word_t _private[NUM_ECC_WORDS]; | 
|  | 113 | uECC_word_t _public[NUM_ECC_WORDS * 2]; | 
|  | 114 | uECC_word_t tries; | 
|  | 115 |  | 
|  | 116 | for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) { | 
|  | 117 | /* Generating _private uniformly at random: */ | 
|  | 118 | uECC_RNG_Function rng_function = uECC_get_rng(); | 
|  | 119 | if (!rng_function || | 
|  | 120 | !rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE)) { | 
|  | 121 | return 0; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | /* computing modular reduction of _random (see FIPS 186.4 B.4.1): */ | 
| Manuel Pégourié-Gonnard | 10349e4 | 2019-11-04 14:57:53 +0100 | [diff] [blame] | 125 | uECC_vli_mmod(_private, _random, curve->n); | 
| Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 126 |  | 
|  | 127 | /* Computing public-key from private: */ | 
|  | 128 | if (EccPoint_compute_public_key(_public, _private, curve)) { | 
|  | 129 |  | 
|  | 130 | /* Converting buffers to correct bit order: */ | 
|  | 131 | uECC_vli_nativeToBytes(private_key, | 
|  | 132 | BITS_TO_BYTES(curve->num_n_bits), | 
|  | 133 | _private); | 
|  | 134 | uECC_vli_nativeToBytes(public_key, | 
|  | 135 | curve->num_bytes, | 
|  | 136 | _public); | 
|  | 137 | uECC_vli_nativeToBytes(public_key + curve->num_bytes, | 
|  | 138 | curve->num_bytes, | 
|  | 139 | _public + curve->num_words); | 
|  | 140 |  | 
|  | 141 | /* erasing temporary buffer that stored secret: */ | 
| Manuel Pégourié-Gonnard | 7a346b8 | 2019-10-02 14:47:01 +0200 | [diff] [blame] | 142 | mbedtls_platform_memset(_private, 0, NUM_ECC_BYTES); | 
| Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 143 |  | 
|  | 144 | return 1; | 
|  | 145 | } | 
|  | 146 | } | 
|  | 147 | return 0; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key, | 
|  | 151 | uint8_t *secret, uECC_Curve curve) | 
|  | 152 | { | 
|  | 153 |  | 
|  | 154 | uECC_word_t _public[NUM_ECC_WORDS * 2]; | 
|  | 155 | uECC_word_t _private[NUM_ECC_WORDS]; | 
| Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 156 | wordcount_t num_words = curve->num_words; | 
|  | 157 | wordcount_t num_bytes = curve->num_bytes; | 
|  | 158 | int r; | 
|  | 159 |  | 
| Manuel Pégourié-Gonnard | 6ee7a4e | 2019-10-14 14:02:07 +0200 | [diff] [blame] | 160 | /* Protect against invalid curve attacks */ | 
|  | 161 | if (uECC_valid_public_key(public_key, curve) != 0) { | 
|  | 162 | r = 0; | 
|  | 163 | goto clear_and_out; | 
|  | 164 | } | 
|  | 165 |  | 
| Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 166 | /* Converting buffers to correct bit order: */ | 
|  | 167 | uECC_vli_bytesToNative(_private, | 
|  | 168 | private_key, | 
|  | 169 | BITS_TO_BYTES(curve->num_n_bits)); | 
|  | 170 | uECC_vli_bytesToNative(_public, | 
|  | 171 | public_key, | 
|  | 172 | num_bytes); | 
|  | 173 | uECC_vli_bytesToNative(_public + num_words, | 
|  | 174 | public_key + num_bytes, | 
|  | 175 | num_bytes); | 
|  | 176 |  | 
| Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 177 | r = EccPoint_mult_safer(_public, _public, _private, curve); | 
|  | 178 | if (r == 0) | 
|  | 179 | goto clear_and_out; | 
| Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 180 |  | 
|  | 181 | uECC_vli_nativeToBytes(secret, num_bytes, _public); | 
|  | 182 | r = !EccPoint_isZero(_public, curve); | 
|  | 183 |  | 
|  | 184 | clear_and_out: | 
|  | 185 | /* erasing temporary buffer used to store secret: */ | 
| Jarno Lamsa | 187fbb1 | 2019-04-25 09:03:19 +0300 | [diff] [blame] | 186 | mbedtls_platform_zeroize(_private, sizeof(_private)); | 
| Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 187 |  | 
|  | 188 | return r; | 
|  | 189 | } |