2019-04-24 12:40:43 +00:00
|
|
|
/* ec_dh.c - TinyCrypt implementation of EC-DH */
|
|
|
|
|
2019-09-09 16:25:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019, Arm Limited (or its affiliates), All Rights Reserved.
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*/
|
|
|
|
|
2019-04-25 06:03:19 +00:00
|
|
|
/*
|
2019-04-24 12:40:43 +00:00
|
|
|
* Copyright (c) 2014, Kenneth MacKay
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* - Neither the name of Intel Corporation nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2019-07-23 14:52:35 +00:00
|
|
|
|
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
|
|
#include "mbedtls/config.h"
|
|
|
|
#else
|
|
|
|
#include MBEDTLS_CONFIG_FILE
|
|
|
|
#endif
|
|
|
|
|
2019-04-24 12:40:43 +00:00
|
|
|
#include <tinycrypt/ecc.h>
|
|
|
|
#include <tinycrypt/ecc_dh.h>
|
|
|
|
#include <string.h>
|
2019-04-25 06:03:19 +00:00
|
|
|
#include "mbedtls/platform_util.h"
|
2019-04-24 12:40:43 +00:00
|
|
|
|
|
|
|
int uECC_make_key_with_d(uint8_t *public_key, uint8_t *private_key,
|
2019-11-21 11:00:43 +00:00
|
|
|
unsigned int *d)
|
2019-04-24 12:40:43 +00:00
|
|
|
{
|
2020-05-25 15:52:05 +00:00
|
|
|
int ret = UECC_FAULT_DETECTED;
|
2019-04-24 12:40:43 +00:00
|
|
|
uECC_word_t _private[NUM_ECC_WORDS];
|
|
|
|
uECC_word_t _public[NUM_ECC_WORDS * 2];
|
|
|
|
|
|
|
|
/* This function is designed for test purposes-only (such as validating NIST
|
|
|
|
* test vectors) as it uses a provided value for d instead of generating
|
|
|
|
* it uniformly at random. */
|
2019-10-02 06:09:31 +00:00
|
|
|
mbedtls_platform_memcpy (_private, d, NUM_ECC_BYTES);
|
2019-04-24 12:40:43 +00:00
|
|
|
|
|
|
|
/* Computing public-key from private: */
|
2019-11-25 12:06:05 +00:00
|
|
|
ret = EccPoint_compute_public_key(_public, _private);
|
|
|
|
if (ret != UECC_SUCCESS) {
|
|
|
|
goto exit;
|
2019-04-24 12:40:43 +00:00
|
|
|
}
|
2019-11-25 12:06:05 +00:00
|
|
|
|
|
|
|
/* Converting buffers to correct bit order: */
|
|
|
|
uECC_vli_nativeToBytes(private_key,
|
|
|
|
BITS_TO_BYTES(NUM_ECC_BITS),
|
|
|
|
_private);
|
|
|
|
uECC_vli_nativeToBytes(public_key,
|
|
|
|
NUM_ECC_BYTES,
|
|
|
|
_public);
|
|
|
|
uECC_vli_nativeToBytes(public_key + NUM_ECC_BYTES,
|
|
|
|
NUM_ECC_BYTES,
|
|
|
|
_public + NUM_ECC_WORDS);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
/* erasing temporary buffer used to store secret: */
|
|
|
|
mbedtls_platform_memset(_private, 0, NUM_ECC_BYTES);
|
|
|
|
|
|
|
|
return ret;
|
2019-04-24 12:40:43 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 11:00:43 +00:00
|
|
|
int uECC_make_key(uint8_t *public_key, uint8_t *private_key)
|
2019-04-24 12:40:43 +00:00
|
|
|
{
|
2020-05-25 15:52:05 +00:00
|
|
|
int ret = UECC_FAULT_DETECTED;
|
2019-04-24 12:40:43 +00:00
|
|
|
uECC_word_t _random[NUM_ECC_WORDS * 2];
|
|
|
|
uECC_word_t _private[NUM_ECC_WORDS];
|
|
|
|
uECC_word_t _public[NUM_ECC_WORDS * 2];
|
|
|
|
uECC_word_t tries;
|
|
|
|
|
|
|
|
for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
|
|
|
|
/* Generating _private uniformly at random: */
|
|
|
|
uECC_RNG_Function rng_function = uECC_get_rng();
|
|
|
|
if (!rng_function ||
|
2020-06-08 15:00:51 +00:00
|
|
|
rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE) != 2 * NUM_ECC_WORDS*uECC_WORD_SIZE) {
|
2019-11-25 12:06:05 +00:00
|
|
|
return UECC_FAILURE;
|
2019-04-24 12:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* computing modular reduction of _random (see FIPS 186.4 B.4.1): */
|
2019-11-21 09:23:05 +00:00
|
|
|
uECC_vli_mmod(_private, _random, curve_n);
|
2019-04-24 12:40:43 +00:00
|
|
|
|
|
|
|
/* Computing public-key from private: */
|
2019-11-25 12:06:05 +00:00
|
|
|
ret = EccPoint_compute_public_key(_public, _private);
|
|
|
|
/* don't try again if a fault was detected */
|
|
|
|
if (ret == UECC_FAULT_DETECTED) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (ret == UECC_SUCCESS) {
|
2019-04-24 12:40:43 +00:00
|
|
|
|
|
|
|
/* Converting buffers to correct bit order: */
|
|
|
|
uECC_vli_nativeToBytes(private_key,
|
2019-11-21 08:46:52 +00:00
|
|
|
BITS_TO_BYTES(NUM_ECC_BITS),
|
2019-04-24 12:40:43 +00:00
|
|
|
_private);
|
|
|
|
uECC_vli_nativeToBytes(public_key,
|
2019-11-21 08:34:09 +00:00
|
|
|
NUM_ECC_BYTES,
|
2019-04-24 12:40:43 +00:00
|
|
|
_public);
|
2019-11-21 08:34:09 +00:00
|
|
|
uECC_vli_nativeToBytes(public_key + NUM_ECC_BYTES,
|
|
|
|
NUM_ECC_BYTES,
|
2019-11-21 08:27:38 +00:00
|
|
|
_public + NUM_ECC_WORDS);
|
2019-04-24 12:40:43 +00:00
|
|
|
|
|
|
|
/* erasing temporary buffer that stored secret: */
|
2019-10-02 12:47:01 +00:00
|
|
|
mbedtls_platform_memset(_private, 0, NUM_ECC_BYTES);
|
2019-04-24 12:40:43 +00:00
|
|
|
|
2019-11-25 12:06:05 +00:00
|
|
|
return UECC_SUCCESS;
|
2019-04-24 12:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-25 12:06:05 +00:00
|
|
|
return UECC_FAILURE;
|
2019-04-24 12:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key,
|
2019-11-21 11:00:43 +00:00
|
|
|
uint8_t *secret)
|
2019-04-24 12:40:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
uECC_word_t _public[NUM_ECC_WORDS * 2];
|
|
|
|
uECC_word_t _private[NUM_ECC_WORDS];
|
2019-11-21 08:27:38 +00:00
|
|
|
wordcount_t num_words = NUM_ECC_WORDS;
|
2019-11-21 08:34:09 +00:00
|
|
|
wordcount_t num_bytes = NUM_ECC_BYTES;
|
2020-05-25 15:52:05 +00:00
|
|
|
int r = UECC_FAULT_DETECTED;
|
2019-04-24 12:40:43 +00:00
|
|
|
|
|
|
|
/* Converting buffers to correct bit order: */
|
|
|
|
uECC_vli_bytesToNative(_private,
|
|
|
|
private_key,
|
2019-11-21 08:46:52 +00:00
|
|
|
BITS_TO_BYTES(NUM_ECC_BITS));
|
2019-04-24 12:40:43 +00:00
|
|
|
uECC_vli_bytesToNative(_public,
|
|
|
|
public_key,
|
|
|
|
num_bytes);
|
|
|
|
uECC_vli_bytesToNative(_public + num_words,
|
|
|
|
public_key + num_bytes,
|
|
|
|
num_bytes);
|
|
|
|
|
2019-11-21 11:00:43 +00:00
|
|
|
r = EccPoint_mult_safer(_public, _public, _private);
|
2019-04-24 12:40:43 +00:00
|
|
|
uECC_vli_nativeToBytes(secret, num_bytes, _public);
|
|
|
|
|
|
|
|
/* erasing temporary buffer used to store secret: */
|
2019-04-25 06:03:19 +00:00
|
|
|
mbedtls_platform_zeroize(_private, sizeof(_private));
|
2019-04-24 12:40:43 +00:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|