From c214460d7f760e2a75908cb41000afcc0bfca282 Mon Sep 17 00:00:00 2001 From: Constanza Heath Date: Fri, 30 Jun 2017 23:47:05 -0700 Subject: Updating micro-ecc to more current algorithms to improve performance of the generation of shared secrets Signed-off-by: Constanza Heath --- tests/test_ecc_utils.c | 345 +++++++++++++++++++++++++++---------------------- 1 file changed, 193 insertions(+), 152 deletions(-) (limited to 'tests/test_ecc_utils.c') diff --git a/tests/test_ecc_utils.c b/tests/test_ecc_utils.c index a635ebe..5c81eba 100644 --- a/tests/test_ecc_utils.c +++ b/tests/test_ecc_utils.c @@ -1,7 +1,30 @@ /* test_ecc_utils.c - TinyCrypt common functions for ECC tests */ +/* 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) 2015 by Intel Corporation, All Rights Reserved. + * 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: @@ -32,8 +55,7 @@ * test_ecc_utils.c -- Implementation of some common functions for ECC tests. * */ -#include -#include + #include #include @@ -41,190 +63,209 @@ #include #include #include +#include +#include +#include -extern int randfd; - -void getRandomBytes(void *p_dest, unsigned p_size) { - if(read(randfd, p_dest, p_size) != (int)p_size) { - printf("Failed to get random bytes.\n"); - } -} - -int hex2int (char hex) { - uint8_t dec; +int hex2int (char hex) +{ + uint8_t dec; - if ('0' <= hex && hex <= '9') dec = hex - '0'; - else if ('a' <= hex && hex <= 'f') dec = hex - 'a' + 10; - else if ('A' <= hex && hex <= 'F') dec = hex - 'A' + 10; - else return -1; + if ('0' <= hex && hex <= '9') dec = hex - '0'; + else if ('a' <= hex && hex <= 'f') dec = hex - 'a' + 10; + else if ('A' <= hex && hex <= 'F') dec = hex - 'A' + 10; + else return -1; - return dec; + return dec; } /* * Convert hex string to byte string * Return number of bytes written to buf, or 0 on error */ -int hex2bin( - uint8_t *buf, - const size_t buflen, - const char *hex, - const size_t hexlen) { - - int dec; - - if (buflen < hexlen/2 + hexlen%2) - return false; - - // if hexlen is uneven, insert leading zero nibble - if (hexlen%2) { - dec = hex2int(hex[0]); - if (dec == -1) - return false; - buf[0] = dec; - buf++; - hex++; - } - - // regular hex conversion - for (size_t i = 0; i (padding = 2*num_bytes - strlen(str))) { - printf( - "Error: 2*num_bytes(%u) < strlen(hex) (%zu)\n", - 2*num_bytes, - strlen(str)); - exit(-1); - } + if (0 > (padding = 2 * num_bytes - strlen(str))) + { + printf("Error: 2 * num_bytes(%d) < strlen(hex) (%zu)\n", + 2 * num_bytes, strlen(str)); + exit(-1); + } - memset(tmp, 0, padding/2); + memset(tmp, 0, padding / 2); - if (false == hex2bin(tmp+padding/2, num_bytes, str, hexlen)) - exit(-1); - ecc_bytes2native(scalar, tmp); + if (false == hex2bin(tmp + padding / 2, num_bytes, str, hexlen)) + { + exit(-1); + } + uECC_vli_bytesToNative(scalar, tmp, num_bytes); } -void vli_print(uint32_t *p_vli, unsigned int p_size) { - while(p_size) { - printf("%08X ", (unsigned)p_vli[p_size - 1]); - --p_size; - } +void vli_print_bytes(uint8_t *vli, unsigned int size) +{ + for(unsigned i = 0; i < size; ++i) + { + printf("%02X ", (unsigned)vli[i]); + } } -void print_ecc_scalar( - const char *label, - const uint32_t * p_vli, - uint32_t num_word32) { - uint32_t i; +void print_ecc_scalar(const char *label, const unsigned int * p_vli, + unsigned int num_word32) +{ + unsigned int i; - if (label) - printf("%s = { ", label); + if (label) { + printf("%s = { ", label); + } - for(i=0; i