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/Makefile | 6 +- tests/include/test_ecc_utils.h | 86 +-- tests/include/test_utils.h | 15 +- tests/test_aes.c | 41 +- tests/test_cbc_mode.c | 27 +- tests/test_ccm_mode.c | 61 ++- tests/test_cmac_mode.c | 25 +- tests/test_ctr_mode.c | 44 +- tests/test_ctr_prng.c | 327 +++++------ tests/test_ecc_dh.c | 811 ++++++++++++++++------------ tests/test_ecc_dsa.c | 1170 +++++++++++++++++++++------------------- tests/test_ecc_utils.c | 345 ++++++------ tests/test_hmac.c | 268 ++++----- tests/test_hmac_prng.c | 37 +- tests/test_sha256.c | 209 +++---- 15 files changed, 1846 insertions(+), 1626 deletions(-) (limited to 'tests') diff --git a/tests/Makefile b/tests/Makefile index 8067a1d..eff5a88 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,6 +1,6 @@ ################################################################################ # -# Copyright (C) 2015 by Intel Corporation, All Rights Reserved. +# Copyright (C) 2017 by Intel Corporation, All Rights Reserved. # # Tests Makefile. # @@ -56,11 +56,11 @@ test_hmac_prng$(DOTEXE): test_hmac_prng.o hmac_prng.o hmac.o \ test_sha256$(DOTEXE): test_sha256.o sha256.o utils.o $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@ -test_ecc_dh$(DOTEXE): test_ecc_dh.o ecc.o ecc_dh.o test_ecc_utils.o +test_ecc_dh$(DOTEXE): test_ecc_dh.o ecc.o ecc_dh.o test_ecc_utils.o ecc_platform_specific.o $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@ test_ecc_dsa$(DOTEXE): test_ecc_dsa.o ecc.o utils.o ecc_dh.o \ - ecc_dsa.o sha256.o test_ecc_utils.o + ecc_dsa.o sha256.o test_ecc_utils.o ecc_platform_specific.o $(LINK.o) $^ $(LOADLIBES) $(LDLIBS) -o $@ diff --git a/tests/include/test_ecc_utils.h b/tests/include/test_ecc_utils.h index 97c183c..4e29054 100644 --- a/tests/include/test_ecc_utils.h +++ b/tests/include/test_ecc_utils.h @@ -1,7 +1,30 @@ /* test_ecc_utils.h - TinyCrypt interface to 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: @@ -35,48 +58,43 @@ #ifndef __TEST_ECC_UTILS_H__ #define __TEST_ECC_UTILS_H__ -#include -#include -#include +#include +#include +#include -EccPoint keygen_vectors(char **d_vec, - char **qx_vec, - char **qy_vec, - int tests, - bool verbose); +int hex2int (char hex); -void getRandomBytes(void *p_dest, unsigned p_size); -void string2host(uint32_t *native, const uint8_t *bytes, size_t len); +/* + * 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 hex2int (char hex); +/* + * Convert hex string to zero-padded nanoECC scalar + */ +void string2scalar(unsigned int * scalar, unsigned int num_word32, char *str); + + +void print_ecc_scalar(const char *label, const unsigned int * p_vli, + unsigned int num_word32); -int hex2bin( - uint8_t *buf, - const size_t buflen, - const char *hex, - const size_t hexlen); +int check_ecc_result(const int num, const char *name, + const unsigned int *expected, + const unsigned int *computed, + const unsigned int num_word32, const bool verbose); -void string2scalar(uint32_t * scalar, uint32_t num_word32, char *str); +/* Test ecc_make_keys, and also as keygen part of other tests */ +int keygen_vectors(char **d_vec, char **qx_vec, char **qy_vec, int tests, bool verbose); -void vli_print(uint32_t *p_vli, unsigned int p_size); +void vli_print_bytes(uint8_t *vli, unsigned int size); -void print_ecc_scalar( - const char *label, - const uint32_t * p_vli, - uint32_t num_word32); -void check_code(const int num, - const char *name, - const int expected, - const int computed, - const int verbose); +int check_code(const int num, const char *name, const int expected, + const int computed, const int verbose); -void check_ecc_result(const int num, const char *name, - const uint32_t *expected, - const uint32_t *computed, - const uint32_t num_word32, - const bool verbose); -#endif +#endif /* __TEST_ECC_UTILS_H__ */ diff --git a/tests/include/test_utils.h b/tests/include/test_utils.h index 5f51511..055f91b 100644 --- a/tests/include/test_utils.h +++ b/tests/include/test_utils.h @@ -1,7 +1,7 @@ /* test_utils.h - TinyCrypt interface to common functions for tests */ /* - * 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: @@ -83,18 +83,19 @@ "PROJECT EXECUTION %s\n", \ result == TC_PASS ? "SUCCESSFUL" : "FAILED"); \ } while (0) + static inline void show_str(const char *label, const uint8_t *s, size_t len) { - uint32_t i; + unsigned int i; TC_PRINT("%s = ", label); - for (i = 0; i < (uint32_t) len; ++i) { + for (i = 0; i < (unsigned int) len; ++i) { TC_PRINT("%02x", s[i]); } TC_PRINT("\n"); } -static inline void fatal(uint32_t testnum, const void *expected, size_t expectedlen, +static inline void fatal(unsigned int testnum, const void *expected, size_t expectedlen, const void *computed, size_t computedlen) { @@ -104,10 +105,10 @@ static inline void fatal(uint32_t testnum, const void *expected, size_t expected TC_PRINT("\n"); } -static inline uint32_t check_result(uint32_t testnum, const void *expected, size_t expectedlen, +static inline unsigned int check_result(unsigned int testnum, const void *expected, size_t expectedlen, const void *computed, size_t computedlen) { - uint32_t result = TC_PASS; + unsigned int result = TC_PASS; if (expectedlen != computedlen) { TC_ERROR("The length of the computed buffer (%zu)", computedlen); @@ -121,4 +122,4 @@ static inline uint32_t check_result(uint32_t testnum, const void *expected, size return result; } -#endif +#endif /* __TEST_UTILS_H__ */ diff --git a/tests/test_aes.c b/tests/test_aes.c index 40c89fa..ea3e2da 100644 --- a/tests/test_aes.c +++ b/tests/test_aes.c @@ -1,9 +1,7 @@ -/* test_aes.c - TinyCrypt - * Implementation of some AES-128 tests (including NIST tests) - */ +/* test_aes.c - TinyCrypt AES-128 tests (including NIST tests) */ /* - * 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: @@ -73,7 +71,8 @@ int test_1(void) 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; - const struct tc_aes_key_sched_struct expected = {{ + const struct tc_aes_key_sched_struct expected = { + { 0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c, 0xa0fafe17, 0x88542cb1, 0x23a33939, 0x2a6c7605, 0xf2c295f2, 0x7a96b943, 0x5935807a, 0x7359f67f, @@ -85,7 +84,8 @@ int test_1(void) 0xead27321, 0xb58dbad2, 0x312bf560, 0x7f8d292f, 0xac7766f3, 0x19fadc21, 0x28d12941, 0x575c006e, 0xd014f9a8, 0xc9ee2589, 0xe13f0cc8, 0xb6630ca6 - } }; + } + }; struct tc_aes_key_sched_struct s; TC_PRINT("AES128 %s (NIST key schedule test):\n", __func__); @@ -97,11 +97,10 @@ int test_1(void) goto exitTest1; } - result = check_result(1, expected.words, - sizeof(expected.words), - s.words, sizeof(s.words)); + result = check_result(1, expected.words, sizeof(expected.words), s.words, + sizeof(s.words)); - exitTest1: +exitTest1: TC_END_RESULT(result); return result; } @@ -137,16 +136,16 @@ int test_2(void) goto exitTest2; } - result = check_result(2, expected, sizeof(expected), - ciphertext, sizeof(ciphertext)); + result = check_result(2, expected, sizeof(expected), ciphertext, + sizeof(ciphertext)); - exitTest2: +exitTest2: TC_END_RESULT(result); return result; } -int var_text_test(uint32_t r, const uint8_t *in, const uint8_t *out, +int var_text_test(unsigned int r, const uint8_t *in, const uint8_t *out, TCAesKeySched_t s) { uint8_t ciphertext[NUM_OF_NIST_KEYS]; @@ -154,8 +153,8 @@ int var_text_test(uint32_t r, const uint8_t *in, const uint8_t *out, int result = TC_PASS; (void)tc_aes_encrypt(ciphertext, in, s); - result = check_result(r, out, NUM_OF_NIST_KEYS, - ciphertext, sizeof(ciphertext)); + result = check_result(r, out, NUM_OF_NIST_KEYS, ciphertext, + sizeof(ciphertext)); if (result != TC_FAIL) { if (tc_aes_decrypt(decrypted, ciphertext, s) == 0) { TC_ERROR("aes_decrypt failed\n"); @@ -1078,7 +1077,7 @@ int test_3(void) } } }; struct tc_aes_key_sched_struct s; - uint32_t i; + unsigned int i; TC_PRINT("AES128 %s (NIST fixed-key and variable-text):\n", __func__); @@ -1096,7 +1095,7 @@ int test_3(void) return result; } -int var_key_test(uint32_t r, const uint8_t *in, const uint8_t *out) +int var_key_test(unsigned int r, const uint8_t *in, const uint8_t *out) { int result = TC_PASS; @@ -1110,8 +1109,8 @@ int var_key_test(uint32_t r, const uint8_t *in, const uint8_t *out) (void)tc_aes128_set_encrypt_key(&s, in); (void)tc_aes_encrypt(ciphertext, plaintext, &s); - result = check_result(r, out, NUM_OF_NIST_KEYS, - ciphertext, sizeof(ciphertext)); + result = check_result(r, out, NUM_OF_NIST_KEYS, ciphertext, + sizeof(ciphertext)); return result; } @@ -2020,7 +2019,7 @@ int test_4(void) 0x89, 0x64, 0x48, 0x45, 0x38, 0xbf, 0xc9, 0x2c } } }; - uint32_t i; + unsigned int i; TC_PRINT("AES128 test #4 (NIST variable-key and fixed-text):\n"); diff --git a/tests/test_cbc_mode.c b/tests/test_cbc_mode.c index 24fe14b..ffa0a88 100644 --- a/tests/test_cbc_mode.c +++ b/tests/test_cbc_mode.c @@ -1,7 +1,7 @@ /* test_cbc_mode.c - TinyCrypt implementation of some AES-CBC tests */ /* - * 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: @@ -108,7 +108,7 @@ int test_1_and_2(void) uint8_t encrypted[80]; uint8_t decrypted[64]; uint8_t *p; - uint32_t length; + unsigned int length; int result = TC_PASS; (void)tc_aes128_set_encrypt_key(&a, key); @@ -116,36 +116,34 @@ int test_1_and_2(void) (void)memcpy(iv_buffer, iv, TC_AES_BLOCK_SIZE); TC_PRINT("CBC test #1 (encryption SP 800-38a tests):\n"); - if (tc_cbc_mode_encrypt(encrypted, - sizeof(plaintext) + TC_AES_BLOCK_SIZE, - plaintext, sizeof(plaintext), - iv_buffer, &a) == 0) { + if (tc_cbc_mode_encrypt(encrypted, sizeof(plaintext) + TC_AES_BLOCK_SIZE, + plaintext, sizeof(plaintext), iv_buffer, &a) == 0) { TC_ERROR("CBC test #1 (encryption SP 800-38a tests) failed in " "%s.\n", __func__); result = TC_FAIL; goto exitTest1; } - result = check_result(1, ciphertext, sizeof(encrypted), - encrypted, sizeof(encrypted)); + result = check_result(1, ciphertext, sizeof(encrypted), encrypted, + sizeof(encrypted)); TC_END_RESULT(result); TC_PRINT("CBC test #2 (decryption SP 800-38a tests):\n"); (void)tc_aes128_set_decrypt_key(&a, key); p = &encrypted[TC_AES_BLOCK_SIZE]; - length = ((uint32_t) sizeof(encrypted)) - TC_AES_BLOCK_SIZE; + length = ((unsigned int) sizeof(encrypted)) - TC_AES_BLOCK_SIZE; - if (tc_cbc_mode_decrypt(decrypted, length - TC_AES_BLOCK_SIZE, p, - length, encrypted, &a) == 0) { + if (tc_cbc_mode_decrypt(decrypted, length - TC_AES_BLOCK_SIZE, p, length, + encrypted, &a) == 0) { TC_ERROR("CBC test #2 (decryption SP 800-38a tests) failed in. " "%s\n", __func__); result = TC_FAIL; goto exitTest1; } - result = check_result(2, plaintext, sizeof(decrypted), - decrypted, sizeof(decrypted)); + result = check_result(2, plaintext, sizeof(decrypted), decrypted, + sizeof(decrypted)); exitTest1: TC_END_RESULT(result); @@ -163,7 +161,8 @@ int main(void) TC_PRINT("Performing CBC tests:\n"); result = test_1_and_2(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("CBC test #1 failed.\n"); goto exitTest; } diff --git a/tests/test_ccm_mode.c b/tests/test_ccm_mode.c index 4ed0f51..878edb3 100644 --- a/tests/test_ccm_mode.c +++ b/tests/test_ccm_mode.c @@ -1,7 +1,7 @@ /* test_ccm_mode.c - TinyCrypt AES-CCM tests (RFC 3610 tests) */ /* - * 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: @@ -51,8 +51,8 @@ #include -#define CIPHERTEXT_LEN 50 -#define DECRYPTED_LEN 25 +#define TC_CCM_MAX_CT_SIZE 50 +#define TC_CCM_MAX_PT_SIZE 25 #define NUM_NIST_KEYS 16 #define NONCE_LEN 13 #define HEADER_LEN 8 @@ -67,16 +67,17 @@ #define EXPECTED_BUF_LEN34 34 #define EXPECTED_BUF_LEN35 35 -int do_test(const uint8_t *key, - uint8_t *nonce, size_t nlen, - const uint8_t *hdr, size_t hlen, - const uint8_t *data, size_t dlen, - const uint8_t *expected, size_t elen, - const int mlen) +int do_test(const uint8_t *key, uint8_t *nonce, + size_t nlen, const uint8_t *hdr, + size_t hlen, const uint8_t *data, + size_t dlen, const uint8_t *expected, + size_t elen, const int mlen) { + int result = TC_PASS; - uint8_t ciphertext[CIPHERTEXT_LEN]; - uint8_t decrypted[DECRYPTED_LEN]; + + uint8_t ciphertext[TC_CCM_MAX_CT_SIZE]; + uint8_t decrypted[TC_CCM_MAX_PT_SIZE]; struct tc_ccm_mode_struct c; struct tc_aes_key_sched_struct sched; @@ -90,8 +91,8 @@ int do_test(const uint8_t *key, goto exitTest1; } - result = tc_ccm_generation_encryption(ciphertext, hdr, hlen, - data, dlen, &c); + result = tc_ccm_generation_encryption(ciphertext, TC_CCM_MAX_CT_SIZE, hdr, + hlen, data, dlen, &c); if (result == 0) { TC_ERROR("ccm_encrypt failed in %s.\n", __func__); @@ -110,8 +111,8 @@ int do_test(const uint8_t *key, goto exitTest1; } - result = tc_ccm_decryption_verification(decrypted, hdr, hlen, - ciphertext, dlen + mlen, &c); + result = tc_ccm_decryption_verification(decrypted, TC_CCM_MAX_PT_SIZE, hdr, + hlen, ciphertext, dlen+mlen, &c); if (result == 0) { TC_ERROR("ccm_decrypt failed in %s.\n", __func__); show_str("\t\tExpected", data, dlen); @@ -378,8 +379,8 @@ int test_vector_7(void) }; struct tc_ccm_mode_struct c; struct tc_aes_key_sched_struct sched; - uint8_t decrypted[DECRYPTED_LEN]; - uint8_t ciphertext[CIPHERTEXT_LEN]; + uint8_t decrypted[TC_CCM_MAX_PT_SIZE]; + uint8_t ciphertext[TC_CCM_MAX_CT_SIZE]; uint16_t mlen = M_LEN10; TC_PRINT("%s: Performing CCM test #7 (no associated data):\n", @@ -393,8 +394,8 @@ int test_vector_7(void) goto exitTest1; } - result = tc_ccm_generation_encryption(ciphertext, hdr, 0, - data, sizeof(data), &c); + result = tc_ccm_generation_encryption(ciphertext, TC_CCM_MAX_CT_SIZE, hdr, + 0, data, sizeof(data), &c); if (result == 0) { TC_ERROR("ccm_encryption failed in %s.\n", __func__); @@ -402,8 +403,8 @@ int test_vector_7(void) goto exitTest1; } - result = tc_ccm_decryption_verification(decrypted, hdr, 0, ciphertext, - sizeof(data) + mlen, &c); + result = tc_ccm_decryption_verification (decrypted, TC_CCM_MAX_PT_SIZE, hdr, + 0, ciphertext, sizeof(data)+mlen, &c); if (result == 0) { TC_ERROR("ccm_decrypt failed in %s.\n", __func__); show_str("\t\tExpected", data, sizeof(data)); @@ -436,11 +437,15 @@ int test_vector_8(void) const uint8_t hdr[8] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; - uint8_t data[] = {}; + + uint8_t *data = NULL; + struct tc_ccm_mode_struct c; struct tc_aes_key_sched_struct sched; - uint8_t decrypted[DECRYPTED_LEN]; - uint8_t ciphertext[CIPHERTEXT_LEN]; + + uint8_t decrypted[TC_CCM_MAX_PT_SIZE]; + uint8_t ciphertext[TC_CCM_MAX_CT_SIZE]; + uint16_t mlen = M_LEN10; TC_PRINT("%s: Performing CCM test #8 (no payload data):\n", __func__); @@ -453,8 +458,8 @@ int test_vector_8(void) goto exitTest1; } - result = tc_ccm_generation_encryption(ciphertext, hdr, sizeof(hdr), - data, sizeof(data), &c); + result = tc_ccm_generation_encryption(ciphertext, TC_CCM_MAX_CT_SIZE, hdr, + sizeof(hdr), data, 0, &c); if (result == 0) { TC_ERROR("ccm_encrypt failed in %s.\n", __func__); @@ -462,8 +467,8 @@ int test_vector_8(void) goto exitTest1; } - result = tc_ccm_decryption_verification(decrypted, hdr, sizeof(hdr), - ciphertext, mlen, &c); + result = tc_ccm_decryption_verification(decrypted, TC_CCM_MAX_PT_SIZE, hdr, + sizeof(hdr), ciphertext, mlen, &c); if (result == 0) { TC_ERROR("ccm_decrypt failed in %s.\n", __func__); show_str("\t\tExpected", data, sizeof(data)); diff --git a/tests/test_cmac_mode.c b/tests/test_cmac_mode.c index 759c330..dc757fe 100644 --- a/tests/test_cmac_mode.c +++ b/tests/test_cmac_mode.c @@ -1,7 +1,7 @@ /* test_cmac_mode.c - TinyCrypt AES-CMAC tests (including SP 800-38B tests) */ /* - * 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: @@ -38,7 +38,7 @@ * - CMAC test #2 null msg (SP 800-38B test vector #1) * - CMAC test #3 1 block msg (SP 800-38B test vector #2) * - CMAC test #4 320 bit msg (SP 800-38B test vector #3) - * - CMAC test #5 512 bit msg(SP 800-38B test vector #4) + * - CMAC test #5 512 bit msg (SP 800-38B test vector #4) */ #include @@ -53,7 +53,7 @@ static void show(const char *label, const uint8_t *s, size_t slen) { - uint32_t i; + unsigned int i; TC_PRINT("%s\t", label); for (i = 0; i < slen; ++i) { @@ -65,7 +65,7 @@ static void show(const char *label, const uint8_t *s, size_t slen) extern void gf_double(uint8_t *out, uint8_t *in); static int verify_gf_2_128_double(uint8_t *K1, uint8_t *K2, - struct tc_cmac_struct s) + struct tc_cmac_struct s) { int result = TC_PASS; @@ -249,6 +249,8 @@ static int verify_cmac_512_bit_msg(TCCmacState_t s) /* * Main task to test CMAC + * effects: returns 1 if all tests pass + * exceptions: returns a negative value if some test fails */ int main(void) { @@ -268,31 +270,36 @@ int main(void) (void) tc_cmac_setup(&state, key, &sched); result = verify_gf_2_128_double(K1, K2, state); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("CMAC test #1 (128 double) failed.\n"); goto exitTest; } (void) tc_cmac_setup(&state, key, &sched); result = verify_cmac_null_msg(&state); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("CMAC test #2 (null msg) failed.\n"); goto exitTest; } (void) tc_cmac_setup(&state, key, &sched); result = verify_cmac_1_block_msg(&state); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("CMAC test #3 (1 block msg)failed.\n"); goto exitTest; } (void) tc_cmac_setup(&state, key, &sched); result = verify_cmac_320_bit_msg(&state); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("CMAC test #4 (320 bit msg) failed.\n"); goto exitTest; } (void) tc_cmac_setup(&state, key, &sched); result = verify_cmac_512_bit_msg(&state); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("CMAC test #5 (512 bit msg)failed.\n"); goto exitTest; } diff --git a/tests/test_ctr_mode.c b/tests/test_ctr_mode.c index d185aee..daf3787 100644 --- a/tests/test_ctr_mode.c +++ b/tests/test_ctr_mode.c @@ -1,7 +1,7 @@ /* test_ctr_mode.c - TinyCrypt implementation of some AES-CTR tests */ /* - * 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: @@ -50,37 +50,37 @@ /* * NIST SP 800-38a CTR Test for encryption and decryption. */ -uint32_t test_1_and_2(void) +unsigned int test_1_and_2(void) { const uint8_t key[16] = { - 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, - 0x09, 0xcf, 0x4f, 0x3c + 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, + 0x09, 0xcf, 0x4f, 0x3c }; uint8_t ctr[16] = { - 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, - 0xfc, 0xfd, 0xfe, 0xff + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, + 0xfc, 0xfd, 0xfe, 0xff }; const uint8_t plaintext[64] = { - 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, - 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, - 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, 0xc8, 0x1c, 0x46, - 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, - 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, - 0xe6, 0x6c, 0x37, 0x10 + 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, + 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, + 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, 0xc8, 0x1c, 0x46, + 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, + 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, + 0xe6, 0x6c, 0x37, 0x10 }; const uint8_t ciphertext[80] = { - 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, - 0xfc, 0xfd, 0xfe, 0xff, 0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, - 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce, 0x98, 0x06, 0xf6, 0x6b, - 0x79, 0x70, 0xfd, 0xff, 0x86, 0x17, 0x18, 0x7b, 0xb9, 0xff, 0xfd, 0xff, - 0x5a, 0xe4, 0xdf, 0x3e, 0xdb, 0xd5, 0xd3, 0x5e, 0x5b, 0x4f, 0x09, 0x02, - 0x0d, 0xb0, 0x3e, 0xab, 0x1e, 0x03, 0x1d, 0xda, 0x2f, 0xbe, 0x03, 0xd1, - 0x79, 0x21, 0x70, 0xa0, 0xf3, 0x00, 0x9c, 0xee + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, + 0xfc, 0xfd, 0xfe, 0xff, 0x87, 0x4d, 0x61, 0x91, 0xb6, 0x20, 0xe3, 0x26, + 0x1b, 0xef, 0x68, 0x64, 0x99, 0x0d, 0xb6, 0xce, 0x98, 0x06, 0xf6, 0x6b, + 0x79, 0x70, 0xfd, 0xff, 0x86, 0x17, 0x18, 0x7b, 0xb9, 0xff, 0xfd, 0xff, + 0x5a, 0xe4, 0xdf, 0x3e, 0xdb, 0xd5, 0xd3, 0x5e, 0x5b, 0x4f, 0x09, 0x02, + 0x0d, 0xb0, 0x3e, 0xab, 0x1e, 0x03, 0x1d, 0xda, 0x2f, 0xbe, 0x03, 0xd1, + 0x79, 0x21, 0x70, 0xa0, 0xf3, 0x00, 0x9c, 0xee }; struct tc_aes_key_sched_struct sched; uint8_t out[80]; uint8_t decrypted[64]; - uint32_t result = TC_PASS; + unsigned int result = TC_PASS; TC_PRINT("CTR test #1 (encryption SP 800-38a tests):\n"); (void)tc_aes128_set_encrypt_key(&sched, key); @@ -99,7 +99,7 @@ uint32_t test_1_and_2(void) TC_PRINT("CTR test #2 (decryption SP 800-38a tests):\n"); (void) memcpy(ctr, out, sizeof(ctr)); if (tc_ctr_mode(decrypted, sizeof(decrypted), &out[TC_AES_BLOCK_SIZE], - sizeof(decrypted), ctr, &sched) == 0) { + sizeof(decrypted), ctr, &sched) == 0) { TC_ERROR("CTR test #2 (decryption SP 800-38a tests) failed in %s.\n", __func__); result = TC_FAIL; goto exitTest1; @@ -119,7 +119,7 @@ uint32_t test_1_and_2(void) int main(void) { - uint32_t result = TC_PASS; + unsigned int result = TC_PASS; TC_START("Performing AES128-CTR mode tests:"); diff --git a/tests/test_ctr_prng.c b/tests/test_ctr_prng.c index b429670..e95f7f1 100644 --- a/tests/test_ctr_prng.c +++ b/tests/test_ctr_prng.c @@ -60,18 +60,17 @@ uint8_t *hexStringToBytes(char *inhex) uint8_t *p; int len, i; - len = strlen(inhex) / 2; + len = strlen(inhex) / 2; retval = (uint8_t *)malloc(len+1); for(i=0, p = (uint8_t *) inhex; i #include +#include #include #include #include #include +#include #include #include #include -void ecdh_vectors(char **qx_vec, - char **qy_vec, - char **d_vec, - char **z_vec, - int tests, - int verbose) { +int ecdh_vectors(char **qx_vec, char **qy_vec, char **d_vec, char **z_vec, + int tests, int verbose) +{ - EccPoint Q; - uint32_t prv[NUM_ECC_DIGITS]; - uint32_t z[NUM_ECC_DIGITS]; + unsigned int pub[2*NUM_ECC_WORDS]; + unsigned int prv[NUM_ECC_WORDS]; + unsigned int z[NUM_ECC_WORDS]; + unsigned int result = TC_PASS; - int rc; - uint32_t exp_z[NUM_ECC_DIGITS]; + int rc; + unsigned int exp_z[NUM_ECC_WORDS]; - for (int i=0; i 2*NUM_ECC_BYTES || - strlen(qy_vec[i]) > 2*NUM_ECC_BYTES) { - // invalid input to ECC digit conversion (string2native()) - rc = -2; - } else { - string2scalar(pub.x, NUM_ECC_DIGITS, qx_vec[i]); - string2scalar(pub.y, NUM_ECC_DIGITS, qy_vec[i]); - rc = ecc_valid_public_key(&pub); - } - - /* - * map to CAVP error codes - * 0 => 0 - success - * -1 => ? - (x,y) = (0,0) (not covered) - * -2 => 1 - out of bounds (pubverify or ecc import) - * -3 => 2 - not on curve - */ - - if (rc == -3) rc = 2; - if (rc == -2) rc = 1; - - check_code(i, res_vec[i], exp_rc, rc, verbose); - } + result = check_ecc_result(i, "Z", exp_z, z, NUM_ECC_WORDS, verbose); + if (result == TC_FAIL) { + return result; + } + } + return result; } -void cavp_pkv(bool verbose) { - - /* - * [P-256] - */ - char *x[] = { - "e0f7449c5588f24492c338f2bc8f7865f755b958d48edb0f2d0056e50c3fd5b7", - "d17c446237d9df87266ba3a91ff27f45abfdcb77bfd83536e92903efb861a9a9", - "17875397ae87369365656d490e8ce956911bd97607f2aff41b56f6f3a61989826", - "f2d1c0dc0852c3d8a2a2500a23a44813ccce1ac4e58444175b440469ffc12273", - "10b0ca230fff7c04768f4b3d5c75fa9f6c539bea644dffbec5dc796a213061b58", - "2c1052f25360a15062d204a056274e93cbe8fc4c4e9b9561134ad5c15ce525da", - "a40d077a87dae157d93dcccf3fe3aca9c6479a75aa2669509d2ef05c7de6782f", - "2633d398a3807b1895548adbb0ea2495ef4b930f91054891030817df87d4ac0a", - "14bf57f76c260b51ec6bbc72dbd49f02a56eaed070b774dc4bad75a54653c3d56", - "2fa74931ae816b426f484180e517f5050c92decfc8daf756cd91f54d51b302f1", - "f8c6dd3181a76aa0e36c2790bba47041acbe7b1e473ff71eee39a824dc595ff0", - "7a81a7e0b015252928d8b36e4ca37e92fdc328eb25c774b4f872693028c4be38", - }; - - char *y[] = { - "86d7e9255d0f4b6f44fa2cd6f8ba3c0aa828321d6d8cc430ca6284ce1d5b43a0", - "1eabb6a349ce2cd447d777b6739c5fc066add2002d2029052c408d0701066231c", - "980a3c4f61b9692633fbba5ef04c9cb546dd05cdec9fa8428b8849670e2fba92", - "32bfe992831b305d8c37b9672df5d29fcb5c29b4a40534683e3ace23d24647dd", - "f5edf37c11052b75f771b7f9fa050e353e464221fec916684ed45b6fead38205", - "ced9783713a8a2a09eff366987639c625753295d9a85d0f5325e32dedbcada0b", - "503d86b87d743ba20804fd7e7884aa017414a7b5b5963e0d46e3a9611419ddf3", - "d6b2f738e3873cc8364a2d364038ce7d0798bb092e3dd77cbdae7c263ba618d2", - "7a231a23bf8b3aa31d9600d888a0678677a30e573decd3dc56b33f365cc11236", - "5b994346137988c58c14ae2152ac2f6ad96d97decb33099bd8a0210114cd1141", - "9c965f227f281b3072b95b8daf29e88b35284f3574462e268e529bbdc50e9e52", - "08862f7335147261e7b1c3d055f9a316e4cab7daf99cc09d1c647f5dd6e7d5bb", - }; - - char *Result[] = { - "P (0 )", "F (1 - Q_x or Q_y out of range)", - "F (1 - Q_x or Q_y out of range)", "F (2 - Point not on curve)", - "F (1 - Q_x or Q_y out of range)", "P (0 )", - "F (2 - Point not on curve)", "P (0 )", - "F (1 - Q_x or Q_y out of range)", "P (0 )", - "F (2 - Point not on curve)", "F (2 - Point not on curve)", - }; - - printf("Test #3: PubKeyVerify "); - printf("NIST-p256-SHA2-256\n"); - - pkv_vectors(x, y, Result, 12, verbose); +int cavp_ecdh(bool verbose) +{ + unsigned int result = TC_PASS; + /* + * P-256 + */ + char *d[] = { + "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534", + "38f65d6dce47676044d58ce5139582d568f64bb16098d179dbab07741dd5caf5", + "1accfaf1b97712b85a6f54b148985a1bdc4c9bec0bd258cad4b3d603f49f32c8", + "207c43a79bfee03db6f4b944f53d2fb76cc49ef1c9c4d34d51b6c65c4db6932d", + "59137e38152350b195c9718d39673d519838055ad908dd4757152fd8255c09bf", + "f5f8e0174610a661277979b58ce5c90fee6c9b3bb346a90a7196255e40b132ef", + "3b589af7db03459c23068b64f63f28d3c3c6bc25b5bf76ac05f35482888b5190", + "d8bf929a20ea7436b2461b541a11c80e61d826c0a4c9d322b31dd54e7f58b9c8", + "0f9883ba0ef32ee75ded0d8bda39a5146a29f1f2507b3bd458dbea0b2bb05b4d", + "2beedb04b05c6988f6a67500bb813faf2cae0d580c9253b6339e4a3337bb6c08", + "77c15dcf44610e41696bab758943eff1409333e4d5a11bbe72c8f6c395e9f848", + "42a83b985011d12303db1a800f2610f74aa71cdf19c67d54ce6c9ed951e9093e", + "ceed35507b5c93ead5989119b9ba342cfe38e6e638ba6eea343a55475de2800b", + "43e0e9d95af4dc36483cdd1968d2b7eeb8611fcce77f3a4e7d059ae43e509604", + "b2f3600df3368ef8a0bb85ab22f41fc0e5f4fdd54be8167a5c3cd4b08db04903", + "4002534307f8b62a9bf67ff641ddc60fef593b17c3341239e95bdb3e579bfdc8", + "4dfa12defc60319021b681b3ff84a10a511958c850939ed45635934ba4979147", + "1331f6d874a4ed3bc4a2c6e9c74331d3039796314beee3b7152fcdba5556304e", + "dd5e9f70ae740073ca0204df60763fb6036c45709bf4a7bb4e671412fad65da3", + "5ae026cfc060d55600717e55b8a12e116d1d0df34af831979057607c2d9c2f76", + "b601ac425d5dbf9e1735c5e2d5bdb79ca98b3d5be4a2cfd6f2273f150e064d9d", + "fefb1dda1845312b5fce6b81b2be205af2f3a274f5a212f66c0d9fc33d7ae535", + "334ae0c4693d23935a7e8e043ebbde21e168a7cba3fa507c9be41d7681e049ce", + "2c4bde40214fcc3bfc47d4cf434b629acbe9157f8fd0282540331de7942cf09d", + "85a268f9d7772f990c36b42b0a331adc92b5941de0b862d5d89a347cbf8faab0", + }; + + char *x[] = { + "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287", + "809f04289c64348c01515eb03d5ce7ac1a8cb9498f5caa50197e58d43a86a7ae", + "a2339c12d4a03c33546de533268b4ad667debf458b464d77443636440ee7fec3", + "df3989b9fa55495719b3cf46dccd28b5153f7808191dd518eff0c3cff2b705ed", + "41192d2813e79561e6a1d6f53c8bc1a433a199c835e141b05a74a97b0faeb922", + "33e82092a0f1fb38f5649d5867fba28b503172b7035574bf8e5b7100a3052792", + "6a9e0c3f916e4e315c91147be571686d90464e8bf981d34a90b6353bca6eeba7", + "a9c0acade55c2a73ead1a86fb0a9713223c82475791cd0e210b046412ce224bb", + "94e94f16a98255fff2b9ac0c9598aac35487b3232d3231bd93b7db7df36f9eb9", + "e099bf2a4d557460b5544430bbf6da11004d127cb5d67f64ab07c94fcdf5274f", + "f75a5fe56bda34f3c1396296626ef012dc07e4825838778a645c8248cff01658", + "2db4540d50230756158abf61d9835712b6486c74312183ccefcaef2797b7674d", + "cd94fc9497e8990750309e9a8534fd114b0a6e54da89c4796101897041d14ecb", + "15b9e467af4d290c417402e040426fe4cf236bae72baa392ed89780dfccdb471", + "49c503ba6c4fa605182e186b5e81113f075bc11dcfd51c932fb21e951eee2fa1", + "19b38de39fdd2f70f7091631a4f75d1993740ba9429162c2a45312401636b29c", + "2c91c61f33adfe9311c942fdbff6ba47020feff416b7bb63cec13faf9b099954", + "a28a2edf58025668f724aaf83a50956b7ac1cfbbff79b08c3bf87dfd2828d767", + "a2ef857a081f9d6eb206a81c4cf78a802bdf598ae380c8886ecd85fdc1ed7644", + "ccd8a2d86bc92f2e01bce4d6922cf7fe1626aed044685e95e2eebd464505f01f", + "c188ffc8947f7301fb7b53e36746097c2134bf9cc981ba74b4e9c4361f595e4e", + "317e1020ff53fccef18bf47bb7f2dd7707fb7b7a7578e04f35b3beed222a0eb6", + "45fb02b2ceb9d7c79d9c2fa93e9c7967c2fa4df5789f9640b24264b1e524fcb1", + "a19ef7bff98ada781842fbfc51a47aff39b5935a1c7d9625c8d323d511c92de6", + "356c5a444c049a52fee0adeb7e5d82ae5aa83030bfff31bbf8ce2096cf161c4b", + }; + + char *y[] = { + "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac", + "b29d84e811197f25eba8f5194092cb6ff440e26d4421011372461f579271cda3", + "ef48a3ab26e20220bcda2c1851076839dae88eae962869a497bf73cb66faf536", + "422294ff46003429d739a33206c8752552c8ba54a270defc06e221e0feaf6ac4", + "1af98cc45e98a7e041b01cf35f462b7562281351c8ebf3ffa02e33a0722a1328", + "f2cf6b601e0a05945e335550bf648d782f46186c772c0f20d3cd0d6b8ca14b2f", + "40f9bead39c2f2bcc2602f75b8a73ec7bdffcbcead159d0174c6c4d3c5357f05", + "f6de0afa20e93e078467c053d241903edad734c6b403ba758c2b5ff04c9d4229", + "d8049a43579cfa90b8093a94416cbefbf93386f15b3f6e190b6e3455fedfe69a", + "d9c50dbe70d714edb5e221f4e020610eeb6270517e688ca64fb0e98c7ef8c1c5", + "33bbdf1b1772d8059df568b061f3f1122f28a8d819167c97be448e3dc3fb0c3c", + "62f57f314e3f3495dc4e099012f5e0ba71770f9660a1eada54104cdfde77243e", + "c3def4b5fe04faee0a11932229fff563637bfdee0e79c6deeaf449f85401c5c4", + "cdf4e9170fb904302b8fd93a820ba8cc7ed4efd3a6f2d6b05b80b2ff2aee4e77", + "8af706ff0922d87b3f0c5e4e31d8b259aeb260a9269643ed520a13bb25da5924", + "09aed7232b28e060941741b6828bcdfa2bc49cc844f3773611504f82a390a5ae", + "6cab31b06419e5221fca014fb84ec870622a1b12bab5ae43682aa7ea73ea08d0", + "dfa7bfffd4c766b86abeaf5c99b6e50cb9ccc9d9d00b7ffc7804b0491b67bc03", + "563c4c20419f07bc17d0539fade1855e34839515b892c0f5d26561f97fa04d1a", + "e9ddd583a9635a667777d5b8a8f31b0f79eba12c75023410b54b8567dddc0f38", + "bf7d2f2056e72421ef393f0c0f2b0e00130e3cac4abbcc00286168e85ec55051", + "09420ce5a19d77c6fe1ee587e6a49fbaf8f280e8df033d75403302e5a27db2ae", + "5c6e8ecf1f7d3023893b7b1ca1e4d178972ee2a230757ddc564ffe37f5c5a321", + "e9c184df75c955e02e02e400ffe45f78f339e1afe6d056fb3245f4700ce606ef", + "57d128de8b2a57a094d1a001e572173f96e8866ae352bf29cddaf92fc85b2f92", + }; + + char *Z[] = { + "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b", + "057d636096cb80b67a8c038c890e887d1adfa4195e9b3ce241c8a778c59cda67", + "2d457b78b4614132477618a5b077965ec90730a8c81a1c75d6d4ec68005d67ec", + "96441259534b80f6aee3d287a6bb17b5094dd4277d9e294f8fe73e48bf2a0024", + "19d44c8d63e8e8dd12c22a87b8cd4ece27acdde04dbf47f7f27537a6999a8e62", + "664e45d5bba4ac931cd65d52017e4be9b19a515f669bea4703542a2c525cd3d3", + "ca342daa50dc09d61be7c196c85e60a80c5cb04931746820be548cdde055679d", + "35aa9b52536a461bfde4e85fc756be928c7de97923f0416c7a3ac8f88b3d4489", + "605c16178a9bc875dcbff54d63fe00df699c03e8a888e9e94dfbab90b25f39b4", + "f96e40a1b72840854bb62bc13c40cc2795e373d4e715980b261476835a092e0b", + "8388fa79c4babdca02a8e8a34f9e43554976e420a4ad273c81b26e4228e9d3a3", + "72877cea33ccc4715038d4bcbdfe0e43f42a9e2c0c3b017fc2370f4b9acbda4a", + "e4e7408d85ff0e0e9c838003f28cdbd5247cdce31f32f62494b70e5f1bc36307", + "ed56bcf695b734142c24ecb1fc1bb64d08f175eb243a31f37b3d9bb4407f3b96", + "bc5c7055089fc9d6c89f83c1ea1ada879d9934b2ea28fcf4e4a7e984b28ad2cf", + "9a4e8e657f6b0e097f47954a63c75d74fcba71a30d83651e3e5a91aa7ccd8343", + "3ca1fc7ad858fb1a6aba232542f3e2a749ffc7203a2374a3f3d3267f1fc97b78", + "1aaabe7ee6e4a6fa732291202433a237df1b49bc53866bfbe00db96a0f58224f", + "430e6a4fba4449d700d2733e557f66a3bf3d50517c1271b1ddae1161b7ac798c", + "1ce9e6740529499f98d1f1d71329147a33df1d05e4765b539b11cf615d6974d3", + "4690e3743c07d643f1bc183636ab2a9cb936a60a802113c49bb1b3f2d0661660", + "30c2261bd0004e61feda2c16aa5e21ffa8d7e7f7dbf6ec379a43b48e4b36aeb0", + "2adae4a138a239dcd93c243a3803c3e4cf96e37fe14e6a9b717be9599959b11c", + "2e277ec30f5ea07d6ce513149b9479b96e07f4b6913b1b5c11305c1444a1bc0b", + "1e51373bd2c6044c129c436e742a55be2a668a85ae08441b6756445df5493857", + }; + + TC_PRINT("Test #1: ECDH"); + TC_PRINT("NIST-p256\n"); + + result = ecdh_vectors(x, y, d, Z, 25, verbose); + if(result == TC_FAIL) { + goto exitTest1; + } + + exitTest1: + TC_END_RESULT(result); + return result; } -int randfd; - -void montecarlo_ecdh(uint32_t num, bool verbose) { - - EccPoint l_Q1, l_Q2; /* public keys */ - uint32_t l_random1[2 * NUM_ECC_DIGITS]; - uint32_t l_random2[2 * NUM_ECC_DIGITS]; - uint32_t l_secret1[NUM_ECC_DIGITS]; - uint32_t l_secret2[NUM_ECC_DIGITS]; - - uint32_t l_shared1[NUM_ECC_DIGITS]; - uint32_t l_shared2[NUM_ECC_DIGITS]; +int cavp_keygen(bool verbose) +{ + unsigned int result = TC_PASS; + /* + * [P-256, B.4.2 Key Pair Generation by Testing Candidates] + */ + char *d[] = { + "c9806898a0334916c860748880a541f093b579a9b1f32934d86c363c39800357", + "710735c8388f48c684a97bd66751cc5f5a122d6b9a96a2dbe73662f78217446d", + "78d5d8b7b3e2c16b3e37e7e63becd8ceff61e2ce618757f514620ada8a11f6e4", + "2a61a0703860585fe17420c244e1de5a6ac8c25146b208ef88ad51ae34c8cb8c", + "01b965b45ff386f28c121c077f1d7b2710acc6b0cb58d8662d549391dcf5a883", + "fac92c13d374c53a085376fe4101618e1e181b5a63816a84a0648f3bdc24e519", + "f257a192dde44227b3568008ff73bcf599a5c45b32ab523b5b21ca582fef5a0a", + "add67e57c42a3d28708f0235eb86885a4ea68e0d8cfd76eb46134c596522abfd", + "4494860fd2c805c5c0d277e58f802cff6d731f76314eb1554142a637a9bc5538", + "d40b07b1ea7b86d4709ef9dc634c61229feb71abd63dc7fc85ef46711a87b210", + }; + + char *x[] = { + "d0720dc691aa80096ba32fed1cb97c2b620690d06de0317b8618d5ce65eb728f", + "f6836a8add91cb182d8d258dda6680690eb724a66dc3bb60d2322565c39e4ab9", + "76711126cbb2af4f6a5fe5665dad4c88d27b6cb018879e03e54f779f203a854e", + "e1aa7196ceeac088aaddeeba037abb18f67e1b55c0a5c4e71ec70ad666fcddc8", + "1f038c5422e88eec9e88b815e8f6b3e50852333fc423134348fc7d79ef8e8a10", + "7258f2ab96fc84ef6ccb33e308cd392d8b568ea635730ceb4ebd72fa870583b9", + "d2e01411817b5512b79bbbe14d606040a4c90deb09e827d25b9f2fc068997872", + "55bed2d9c029b7f230bde934c7124ed52b1330856f13cbac65a746f9175f85d7", + "5190277a0c14d8a3d289292f8a544ce6ea9183200e51aec08440e0c1a463a4e4", + "fbcea7c2827e0e8085d7707b23a3728823ea6f4878b24747fb4fd2842d406c73", + }; + + char *y[] = { + "9681b517b1cda17d0d83d335d9c4a8a9a9b0b1b3c7106d8f3c72bc5093dc275f", + "1f837aa32864870cb8e8d0ac2ff31f824e7beddc4bb7ad72c173ad974b289dc2", + "a26df39960ab5248fd3620fd018398e788bd89a3cea509b352452b69811e6856", + "d7d35bdce6dedc5de98a7ecb27a9cd066a08f586a733b59f5a2cdb54f971d5c8", + "43a047cb20e94b4ffb361ef68952b004c0700b2962e0c0635a70269bc789b849", + "489807ca55bdc29ca5c8fe69b94f227b0345cccdbe89975e75d385cc2f6bb1e2", + "503f138f8bab1df2c4507ff663a1fdf7f710e7adb8e7841eaa902703e314e793", + "32805e311d583b4e007c40668185e85323948e21912b6b0d2cda8557389ae7b0", + "ecd98514821bd5aaf3419ab79b71780569470e4fed3da3c1353b28fe137f36eb", + "2393c85f1f710c5afc115a39ba7e18abe03f19c9d4bb3d47d19468b818efa535", + }; + + TC_PRINT("Test #2: ECC KeyGen "); + TC_PRINT("NIST-p256\n"); + + result = keygen_vectors(d, x, y, 10, verbose); + if(result == TC_FAIL) { + goto exitTest1; + } + + exitTest1: + TC_END_RESULT(result); + return result; +} - randfd = open("/dev/urandom", O_RDONLY); - if(randfd == -1) { - printf("No access to urandom\n"); - exit(-1); +/* Test ecc_make_keys, and also as keygen part of other tests */ +int pkv_vectors(char **qx_vec, char **qy_vec, char **res_vec, int tests, + bool verbose) +{ + + unsigned int pub[2 * NUM_ECC_WORDS]; + uint8_t _public[2 * NUM_ECC_BYTES]; + int rc; + int exp_rc; + char tmp; + unsigned int result = TC_PASS; + const struct uECC_Curve_t * curve = uECC_secp256r1(); + + for (int i = 0; i < tests; i++) { + + if (2 != sscanf(res_vec[i], "%c (%d ", &tmp, &exp_rc)) { + TC_ERROR("Error: failed to parse CAVP response.\n"); + result = TC_FAIL; + goto exitTest1; + } + + if (strlen(qx_vec[i]) > 2 * NUM_ECC_BYTES || + strlen(qy_vec[i]) > 2 * NUM_ECC_BYTES) { + /* invalid input to ECC digit conversion (string2native()) */ + rc = -2; + } else { + string2scalar(pub, NUM_ECC_WORDS, qx_vec[i]); + string2scalar(pub + NUM_ECC_WORDS, NUM_ECC_WORDS, qy_vec[i]); + + uECC_vli_nativeToBytes(_public, NUM_ECC_BYTES, pub); + uECC_vli_nativeToBytes(_public + NUM_ECC_BYTES, NUM_ECC_BYTES, pub+NUM_ECC_WORDS); + + rc = uECC_valid_public_key(_public, curve); + } + + /* + * map to CAVP error codes + * 0 => 0 - success + * -1 => ? - (x,y) = (0,0) (not covered) + * -2 => 1 - out of bounds (pubverify or ecc import) + * -3 => 2 - not on curve + * -4 => ? - public key is the group generator + */ + + if (rc == -3) rc = 2; + if (rc == -2) rc = 1; + + result = check_code(i, res_vec[i], exp_rc, rc, verbose); + if(result == TC_FAIL) { + goto exitTest1; + } } - printf("Test #4: Monte Carlo (Randomized EC-DH key-exchange) "); - printf("NIST-p256\n"); - - for(uint32_t i=0; i #include +#include +#include #include +#include +#include #include #include -#include -#include #include +#include #include + #include +/* Maximum size of message to be signed. */ #define BUF_SIZE 256 -int sign_vectors(TCSha256State_t hash, - char **d_vec, - char **k_vec, - char **msg_vec, - char **qx_vec, - char **qy_vec, - char **r_vec, - char **s_vec, - int tests, - bool verbose) { - - uint32_t seed[2*NUM_ECC_DIGITS]; - uint32_t prv[NUM_ECC_DIGITS]; - uint32_t r[NUM_ECC_DIGITS]; - uint32_t s[NUM_ECC_DIGITS]; - uint8_t digest[TC_SHA256_DIGEST_SIZE]; - uint32_t dig32[NUM_ECC_DIGITS]; - - /* expected outputs (converted input vectors) */ - uint32_t exp_r[NUM_ECC_DIGITS]; - uint32_t exp_s[NUM_ECC_DIGITS]; - - uint8_t msg[BUF_SIZE]; - size_t msglen; - - for (int i=0; in, BITS_TO_WORDS(curve->num_n_bits)); + uECC_vli_nativeToBytes(hash, NUM_ECC_BYTES, hash_words); + + if (!uECC_make_key(public, private, curve)) { + TC_ERROR("uECC_make_key() failed\n"); + return TC_FAIL; + } + + if (!uECC_sign(private, hash, sizeof(hash), sig, curve)) { + TC_ERROR("uECC_sign() failed\n"); + return TC_FAIL; + } + + if (!uECC_verify(public, hash, sizeof(hash), sig, curve)) { + TC_ERROR("uECC_verify() failed\n"); + return TC_FAIL; + } + if (verbose) { + fflush(stdout); + printf("."); + } + } + TC_PRINT("\n"); + return TC_PASS; } -int main() { - - bool verbose = true; - - cavp_sign(verbose); - cavp_verify(verbose); - montecarlo_signverify(10, verbose); - - printf("\nAll ECC-DSA tests succeeded.\n"); - - return 0; +int main() +{ + unsigned int result = TC_PASS; + + TC_START("Performing ECC-DSA tests:"); + /* Setup of the Cryptographically Secure PRNG. */ + uECC_set_rng(&default_CSPRNG); + + bool verbose = true; + + TC_PRINT("Performing cavp_sign test:\n"); + result = cavp_sign(verbose); + if (result == TC_FAIL) { /* terminate test */ + TC_ERROR("cavp_sign test failed.\n"); + goto exitTest; + } + TC_PRINT("Performing cavp_verify test:\n"); + result = cavp_verify(verbose); + if (result == TC_FAIL) { + TC_ERROR("cavp_verify test failed.\n"); + goto exitTest; + } + TC_PRINT("Performing montecarlo_signverify test:\n"); + result = montecarlo_signverify(10, verbose); + if (result == TC_FAIL) { + TC_ERROR("montecarlo_signverify test failed.\n"); + goto exitTest; + } + + TC_PRINT("\nAll ECC-DSA tests succeeded.\n"); + + exitTest: + TC_END_RESULT(result); + TC_END_REPORT(result); } 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 #include -uint32_t do_hmac_test(TCHmacState_t h, uint32_t testnum, const uint8_t *data, - size_t datalen, const uint8_t *expected, - size_t expectedlen) +unsigned int do_hmac_test(TCHmacState_t h, unsigned int testnum, const uint8_t *data, + size_t datalen, const uint8_t *expected, + size_t expectedlen) { uint8_t digest[32]; - uint32_t result = TC_PASS; + unsigned int result = TC_PASS; (void)tc_hmac_init(h); (void)tc_hmac_update(h, data, datalen); @@ -66,243 +66,238 @@ uint32_t do_hmac_test(TCHmacState_t h, uint32_t testnum, const uint8_t *data, /* * NIST test vectors for encryption. */ -uint32_t test_1(void) +unsigned int test_1(void) { - uint32_t result = TC_PASS; + unsigned int result = TC_PASS; TC_PRINT("HMAC %s:\n", __func__); const uint8_t key[20] = { - 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, - 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; const uint8_t data[8] = { - 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 + 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 }; const uint8_t expected[32] = { - 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, - 0xaf, 0x0b, 0xf1, 0x2b, 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, - 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7 + 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53, 0x5c, 0xa8, 0xaf, 0xce, + 0xaf, 0x0b, 0xf1, 0x2b, 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7, + 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7 }; struct tc_hmac_state_struct h; (void)memset(&h, 0x00, sizeof(h)); (void)tc_hmac_set_key(&h, key, sizeof(key)); - result = do_hmac_test(&h, 1, data, sizeof(data), - expected, sizeof(expected)); + result = do_hmac_test(&h, 1, data, sizeof(data),expected, + sizeof(expected)); TC_END_RESULT(result); return result; } -uint32_t test_2(void) +unsigned int test_2(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("HMAC %s:\n", __func__); const uint8_t key[4] = { 0x4a, 0x65, 0x66, 0x65 }; const uint8_t data[28] = { - 0x77, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20, 0x79, 0x61, 0x20, 0x77, - 0x61, 0x6e, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x6e, 0x6f, 0x74, 0x68, - 0x69, 0x6e, 0x67, 0x3f + 0x77, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20, 0x79, 0x61, 0x20, 0x77, + 0x61, 0x6e, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x6e, 0x6f, 0x74, 0x68, + 0x69, 0x6e, 0x67, 0x3f }; const uint8_t expected[32] = { - 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 0x6a, 0x04, 0x24, 0x26, - 0x08, 0x95, 0x75, 0xc7, 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83, - 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43 + 0x5b, 0xdc, 0xc1, 0x46, 0xbf, 0x60, 0x75, 0x4e, 0x6a, 0x04, 0x24, 0x26, + 0x08, 0x95, 0x75, 0xc7, 0x5a, 0x00, 0x3f, 0x08, 0x9d, 0x27, 0x39, 0x83, + 0x9d, 0xec, 0x58, 0xb9, 0x64, 0xec, 0x38, 0x43 }; struct tc_hmac_state_struct h; (void)memset(&h, 0x00, sizeof(h)); (void)tc_hmac_set_key(&h, key, sizeof(key)); - result = do_hmac_test(&h, 2, data, sizeof(data), - expected, sizeof(expected)); + result = do_hmac_test(&h, 2, data, sizeof(data), expected, + sizeof(expected)); TC_END_RESULT(result); return result; } -uint32_t test_3(void) +unsigned int test_3(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("HMAC %s:\n", __func__); const uint8_t key[20] = { - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; const uint8_t data[50] = { - 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, - 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, - 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, - 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, - 0xdd, 0xdd + 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, + 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, + 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, + 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, + 0xdd, 0xdd }; const uint8_t expected[32] = { - 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d, 0xb8, 0xeb, - 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, 0xc1, 0x22, - 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe + 0x77, 0x3e, 0xa9, 0x1e, 0x36, 0x80, 0x0e, 0x46, 0x85, 0x4d, 0xb8, 0xeb, + 0xd0, 0x91, 0x81, 0xa7, 0x29, 0x59, 0x09, 0x8b, 0x3e, 0xf8, 0xc1, 0x22, + 0xd9, 0x63, 0x55, 0x14, 0xce, 0xd5, 0x65, 0xfe }; struct tc_hmac_state_struct h; (void)memset(&h, 0x00, sizeof(h)); (void)tc_hmac_set_key(&h, key, sizeof(key)); - result = do_hmac_test(&h, 3, data, sizeof(data), - expected, sizeof(expected)); + result = do_hmac_test(&h, 3, data, sizeof(data), expected, + sizeof(expected)); TC_END_RESULT(result); return result; } -uint32_t test_4(void) +unsigned int test_4(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("HMAC %s:\n", __func__); + const uint8_t key[25] = { - 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, - 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, - 0x19 + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, + 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, + 0x19 }; const uint8_t data[50] = { - 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, - 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, - 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, - 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, - 0xcd, 0xcd + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, + 0xcd, 0xcd }; const uint8_t expected[32] = { - 0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e, 0xa4, 0xcc, 0x81, 0x98, - 0x99, 0xf2, 0x08, 0x3a, 0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78, 0xf8, 0x07, - 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29, 0x66, 0x5b + 0x82, 0x55, 0x8a, 0x38, 0x9a, 0x44, 0x3c, 0x0e, 0xa4, 0xcc, 0x81, 0x98, + 0x99, 0xf2, 0x08, 0x3a, 0x85, 0xf0, 0xfa, 0xa3, 0xe5, 0x78, 0xf8, 0x07, + 0x7a, 0x2e, 0x3f, 0xf4, 0x67, 0x29, 0x66, 0x5b }; struct tc_hmac_state_struct h; (void)memset(&h, 0x00, sizeof(h)); (void)tc_hmac_set_key(&h, key, sizeof(key)); - result = do_hmac_test(&h, 4, data, sizeof(data), - expected, sizeof(expected)); + result = do_hmac_test(&h, 4, data, sizeof(data), expected, + sizeof(expected)); TC_END_RESULT(result); return result; } -uint32_t test_5(void) +unsigned int test_5(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("HMAC %s:\n", __func__); const uint8_t key[20] = { - 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, - 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c + 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, + 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c }; const uint8_t data[20] = { - 0x54, 0x65, 0x73, 0x74, 0x20, 0x57, 0x69, 0x74, 0x68, 0x20, 0x54, 0x72, - 0x75, 0x6e, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e + 0x54, 0x65, 0x73, 0x74, 0x20, 0x57, 0x69, 0x74, 0x68, 0x20, 0x54, 0x72, + 0x75, 0x6e, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e }; const uint8_t expected[32] = { - 0xa3, 0xb6, 0x16, 0x74, 0x73, 0x10, 0x0e, 0xe0, 0x6e, 0x0c, 0x79, 0x6c, - 0x29, 0x55, 0x55, 0x2b, 0xfa, 0x6f, 0x7c, 0x0a, 0x6a, 0x8a, 0xef, 0x8b, - 0x93, 0xf8, 0x60, 0xaa, 0xb0, 0xcd, 0x20, 0xc5 + 0xa3, 0xb6, 0x16, 0x74, 0x73, 0x10, 0x0e, 0xe0, 0x6e, 0x0c, 0x79, 0x6c, + 0x29, 0x55, 0x55, 0x2b, 0xfa, 0x6f, 0x7c, 0x0a, 0x6a, 0x8a, 0xef, 0x8b, + 0x93, 0xf8, 0x60, 0xaa, 0xb0, 0xcd, 0x20, 0xc5 }; struct tc_hmac_state_struct h; (void)memset(&h, 0x00, sizeof(h)); (void)tc_hmac_set_key(&h, key, sizeof(key)); - result = do_hmac_test(&h, 5, data, sizeof(data), - expected, sizeof(expected)); + result = do_hmac_test(&h, 5, data, sizeof(data), expected, + sizeof(expected)); TC_END_RESULT(result); return result; } -uint32_t test_6(void) +unsigned int test_6(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("HMAC %s:\n", __func__); const uint8_t key[131] = { - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; const uint8_t data[54] = { - 0x54, 0x65, 0x73, 0x74, 0x20, 0x55, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x4c, - 0x61, 0x72, 0x67, 0x65, 0x72, 0x20, 0x54, 0x68, 0x61, 0x6e, 0x20, 0x42, - 0x6c, 0x6f, 0x63, 0x6b, 0x2d, 0x53, 0x69, 0x7a, 0x65, 0x20, 0x4b, 0x65, - 0x79, 0x20, 0x2d, 0x20, 0x48, 0x61, 0x73, 0x68, 0x20, 0x4b, 0x65, 0x79, - 0x20, 0x46, 0x69, 0x72, 0x73, 0x74 + 0x54, 0x65, 0x73, 0x74, 0x20, 0x55, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x4c, + 0x61, 0x72, 0x67, 0x65, 0x72, 0x20, 0x54, 0x68, 0x61, 0x6e, 0x20, 0x42, + 0x6c, 0x6f, 0x63, 0x6b, 0x2d, 0x53, 0x69, 0x7a, 0x65, 0x20, 0x4b, 0x65, + 0x79, 0x20, 0x2d, 0x20, 0x48, 0x61, 0x73, 0x68, 0x20, 0x4b, 0x65, 0x79, + 0x20, 0x46, 0x69, 0x72, 0x73, 0x74 }; const uint8_t expected[32] = { - 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, 0x0d, 0x8a, 0x26, 0xaa, - 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14, - 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54 + 0x60, 0xe4, 0x31, 0x59, 0x1e, 0xe0, 0xb6, 0x7f, 0x0d, 0x8a, 0x26, 0xaa, + 0xcb, 0xf5, 0xb7, 0x7f, 0x8e, 0x0b, 0xc6, 0x21, 0x37, 0x28, 0xc5, 0x14, + 0x05, 0x46, 0x04, 0x0f, 0x0e, 0xe3, 0x7f, 0x54 }; struct tc_hmac_state_struct h; (void)memset(&h, 0x00, sizeof(h)); (void)tc_hmac_set_key(&h, key, sizeof(key)); - result = do_hmac_test(&h, 6, data, sizeof(data), - expected, sizeof(expected)); + result = do_hmac_test(&h, 6, data, sizeof(data), expected, + sizeof(expected)); TC_END_RESULT(result); return result; } -uint32_t test_7(void) +unsigned int test_7(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("HMAC %s:\n", __func__); const uint8_t key[131] = { - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, - 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; const uint8_t data[152] = { - 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, - 0x73, 0x74, 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x6c, - 0x61, 0x72, 0x67, 0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6e, 0x20, 0x62, - 0x6c, 0x6f, 0x63, 0x6b, 0x2d, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6b, 0x65, - 0x79, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x61, 0x20, 0x6c, 0x61, 0x72, 0x67, - 0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6e, 0x20, 0x62, 0x6c, 0x6f, 0x63, - 0x6b, 0x2d, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x64, 0x61, 0x74, 0x61, 0x2e, - 0x20, 0x54, 0x68, 0x65, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x6e, 0x65, 0x65, - 0x64, 0x73, 0x20, 0x74, 0x6f, 0x20, 0x62, 0x65, 0x20, 0x68, 0x61, 0x73, - 0x68, 0x65, 0x64, 0x20, 0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x20, 0x62, - 0x65, 0x69, 0x6e, 0x67, 0x20, 0x75, 0x73, 0x65, 0x64, 0x20, 0x62, 0x79, - 0x20, 0x74, 0x68, 0x65, 0x20, 0x48, 0x4d, 0x41, 0x43, 0x20, 0x61, 0x6c, - 0x67, 0x6f, 0x72, 0x69, 0x74, 0x68, 0x6d, 0x2e + 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, + 0x73, 0x74, 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x6c, + 0x61, 0x72, 0x67, 0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6e, 0x20, 0x62, + 0x6c, 0x6f, 0x63, 0x6b, 0x2d, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x6b, 0x65, + 0x79, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x61, 0x20, 0x6c, 0x61, 0x72, 0x67, + 0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6e, 0x20, 0x62, 0x6c, 0x6f, 0x63, + 0x6b, 0x2d, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x64, 0x61, 0x74, 0x61, 0x2e, + 0x20, 0x54, 0x68, 0x65, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x6e, 0x65, 0x65, + 0x64, 0x73, 0x20, 0x74, 0x6f, 0x20, 0x62, 0x65, 0x20, 0x68, 0x61, 0x73, + 0x68, 0x65, 0x64, 0x20, 0x62, 0x65, 0x66, 0x6f, 0x72, 0x65, 0x20, 0x62, + 0x65, 0x69, 0x6e, 0x67, 0x20, 0x75, 0x73, 0x65, 0x64, 0x20, 0x62, 0x79, + 0x20, 0x74, 0x68, 0x65, 0x20, 0x48, 0x4d, 0x41, 0x43, 0x20, 0x61, 0x6c, + 0x67, 0x6f, 0x72, 0x69, 0x74, 0x68, 0x6d, 0x2e }; const uint8_t expected[32] = { - 0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, 0x27, 0x63, 0x5f, 0xbc, - 0xd5, 0xb0, 0xe9, 0x44, 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93, - 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2 + 0x9b, 0x09, 0xff, 0xa7, 0x1b, 0x94, 0x2f, 0xcb, 0x27, 0x63, 0x5f, 0xbc, + 0xd5, 0xb0, 0xe9, 0x44, 0xbf, 0xdc, 0x63, 0x64, 0x4f, 0x07, 0x13, 0x93, + 0x8a, 0x7f, 0x51, 0x53, 0x5c, 0x3a, 0x35, 0xe2 }; struct tc_hmac_state_struct h; (void)memset(&h, 0x00, sizeof(h)); (void)tc_hmac_set_key(&h, key, sizeof(key)); - result = do_hmac_test(&h, 7, data, sizeof(data), - expected, sizeof(expected)); + result = do_hmac_test(&h, 7, data, sizeof(data), expected, + sizeof(expected)); TC_END_RESULT(result); return result; } @@ -312,42 +307,49 @@ uint32_t test_7(void) */ int main(void) { - uint32_t result = TC_PASS; + unsigned int result = TC_PASS; TC_START("Performing HMAC tests (RFC4231 test vectors):"); result = test_1(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("HMAC test #1 failed.\n"); goto exitTest; } result = test_2(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("HMAC test #2 failed.\n"); goto exitTest; } result = test_3(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("HMAC test #3 failed.\n"); goto exitTest; } result = test_4(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("HMAC test #4 failed.\n"); goto exitTest; } result = test_5(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("HMAC test #5 failed.\n"); goto exitTest; } result = test_6(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("HMAC #6 test failed.\n"); goto exitTest; } result = test_7(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("HMAC test #7 failed.\n"); goto exitTest; } diff --git a/tests/test_hmac_prng.c b/tests/test_hmac_prng.c index 9071305..b45e984 100644 --- a/tests/test_hmac_prng.c +++ b/tests/test_hmac_prng.c @@ -1,7 +1,7 @@ /* test_hmac_prng.c - TinyCrypt implementation of some HMAC-PRNG tests */ /* - * 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: @@ -48,24 +48,44 @@ #include #include +#define TC_DEBUG_MODE 0 + +#ifdef TC_DEBUG_MODE +void show(const char *label, const uint8_t *s, size_t len) +{ + unsigned int i; + printf ("%s = ", label); + for (i = 0; i < (unsigned int) len; ++i) { + printf ("%02x", s[i]); + } + printf ("\n"); +} + +void printBinaryFile(const uint8_t *s, unsigned int slen) +{ + FILE *write_ptr; + write_ptr = fopen("pseudo-random-data.bin","wb"); + fwrite(s, slen, 1, write_ptr); +} +#endif + /* * Main task to test AES */ - int main(void) { uint8_t seed[128]; struct tc_hmac_prng_struct h; - uint32_t size = (1 << 15); + unsigned int size = (1 << 19); uint8_t random[size]; - uint32_t i; - uint32_t result = TC_PASS; + unsigned int i; + unsigned int result = TC_PASS; TC_START("Performing HMAC-PRNG tests:"); TC_PRINT("HMAC-PRNG test#1 (init, reseed, generate):\n"); /* Fake seed (replace by a a truly random seed): */ - for (i = 0; i < (uint32_t) sizeof(seed); ++i) { + for (i = 0; i < (unsigned int) sizeof(seed); ++i) { seed[i] = i; } @@ -102,6 +122,11 @@ int main(void) } TC_END_RESULT(result); +#ifdef TC_DEBUG_MODE + printBinaryFile(random, size); + show ("Pseudo-random data", random, size); +#endif + TC_PRINT("All HMAC tests succeeded!\n"); exitTest: diff --git a/tests/test_sha256.c b/tests/test_sha256.c index 5da973f..b309ed1 100644 --- a/tests/test_sha256.c +++ b/tests/test_sha256.c @@ -1,7 +1,7 @@ /* test_sha256.c - TinyCrypt implementation of some SHA-256 tests */ /* - * 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: @@ -50,15 +50,15 @@ /* * NIST SHA256 test vector 1. */ -uint32_t test_1(void) +unsigned int test_1(void) { - uint32_t result = TC_PASS; + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #1:\n"); const uint8_t expected[32] = { - 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, - 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, - 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad + 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, + 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, + 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }; const char *m = "abc"; uint8_t digest[32]; @@ -76,15 +76,14 @@ uint32_t test_1(void) /* * NIST SHA256 test vector 2. */ -uint32_t test_2(void) +unsigned int test_2(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #2:\n"); const uint8_t expected[32] = { - 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93, - 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, - 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 + 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93, + 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, + 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 }; const char *m = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; uint8_t digest[32]; @@ -100,15 +99,14 @@ uint32_t test_2(void) return result; } -uint32_t test_3(void) +unsigned int test_3(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #3:\n"); const uint8_t expected[32] = { - 0x68, 0x32, 0x57, 0x20, 0xaa, 0xbd, 0x7c, 0x82, 0xf3, 0x0f, 0x55, 0x4b, - 0x31, 0x3d, 0x05, 0x70, 0xc9, 0x5a, 0xcc, 0xbb, 0x7d, 0xc4, 0xb5, 0xaa, - 0xe1, 0x12, 0x04, 0xc0, 0x8f, 0xfe, 0x73, 0x2b + 0x68, 0x32, 0x57, 0x20, 0xaa, 0xbd, 0x7c, 0x82, 0xf3, 0x0f, 0x55, 0x4b, + 0x31, 0x3d, 0x05, 0x70, 0xc9, 0x5a, 0xcc, 0xbb, 0x7d, 0xc4, 0xb5, 0xaa, + 0xe1, 0x12, 0x04, 0xc0, 0x8f, 0xfe, 0x73, 0x2b }; const uint8_t m[1] = { 0xbd }; uint8_t digest[32]; @@ -124,15 +122,14 @@ uint32_t test_3(void) return result; } -uint32_t test_4(void) +unsigned int test_4(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #4:\n"); const uint8_t expected[32] = { - 0x7a, 0xbc, 0x22, 0xc0, 0xae, 0x5a, 0xf2, 0x6c, 0xe9, 0x3d, 0xbb, 0x94, - 0x43, 0x3a, 0x0e, 0x0b, 0x2e, 0x11, 0x9d, 0x01, 0x4f, 0x8e, 0x7f, 0x65, - 0xbd, 0x56, 0xc6, 0x1c, 0xcc, 0xcd, 0x95, 0x04 + 0x7a, 0xbc, 0x22, 0xc0, 0xae, 0x5a, 0xf2, 0x6c, 0xe9, 0x3d, 0xbb, 0x94, + 0x43, 0x3a, 0x0e, 0x0b, 0x2e, 0x11, 0x9d, 0x01, 0x4f, 0x8e, 0x7f, 0x65, + 0xbd, 0x56, 0xc6, 0x1c, 0xcc, 0xcd, 0x95, 0x04 }; const uint8_t m[4] = { 0xc9, 0x8c, 0x8e, 0x55 }; uint8_t digest[32]; @@ -148,15 +145,15 @@ uint32_t test_4(void) return result; } -uint32_t test_5(void) +unsigned int test_5(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #5:\n"); + const uint8_t expected[32] = { - 0x02, 0x77, 0x94, 0x66, 0xcd, 0xec, 0x16, 0x38, 0x11, 0xd0, 0x78, 0x81, - 0x5c, 0x63, 0x3f, 0x21, 0x90, 0x14, 0x13, 0x08, 0x14, 0x49, 0x00, 0x2f, - 0x24, 0xaa, 0x3e, 0x80, 0xf0, 0xb8, 0x8e, 0xf7 + 0x02, 0x77, 0x94, 0x66, 0xcd, 0xec, 0x16, 0x38, 0x11, 0xd0, 0x78, 0x81, + 0x5c, 0x63, 0x3f, 0x21, 0x90, 0x14, 0x13, 0x08, 0x14, 0x49, 0x00, 0x2f, + 0x24, 0xaa, 0x3e, 0x80, 0xf0, 0xb8, 0x8e, 0xf7 }; uint8_t m[55]; uint8_t digest[32]; @@ -174,15 +171,14 @@ uint32_t test_5(void) return result; } -uint32_t test_6(void) +unsigned int test_6(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #6:\n"); const uint8_t expected[32] = { - 0xd4, 0x81, 0x7a, 0xa5, 0x49, 0x76, 0x28, 0xe7, 0xc7, 0x7e, 0x6b, 0x60, - 0x61, 0x07, 0x04, 0x2b, 0xbb, 0xa3, 0x13, 0x08, 0x88, 0xc5, 0xf4, 0x7a, - 0x37, 0x5e, 0x61, 0x79, 0xbe, 0x78, 0x9f, 0xbb + 0xd4, 0x81, 0x7a, 0xa5, 0x49, 0x76, 0x28, 0xe7, 0xc7, 0x7e, 0x6b, 0x60, + 0x61, 0x07, 0x04, 0x2b, 0xbb, 0xa3, 0x13, 0x08, 0x88, 0xc5, 0xf4, 0x7a, + 0x37, 0x5e, 0x61, 0x79, 0xbe, 0x78, 0x9f, 0xbb }; uint8_t m[56]; uint8_t digest[32]; @@ -200,15 +196,14 @@ uint32_t test_6(void) return result; } -uint32_t test_7(void) +unsigned int test_7(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #7:\n"); const uint8_t expected[32] = { - 0x65, 0xa1, 0x6c, 0xb7, 0x86, 0x13, 0x35, 0xd5, 0xac, 0xe3, 0xc6, 0x07, - 0x18, 0xb5, 0x05, 0x2e, 0x44, 0x66, 0x07, 0x26, 0xda, 0x4c, 0xd1, 0x3b, - 0xb7, 0x45, 0x38, 0x1b, 0x23, 0x5a, 0x17, 0x85 + 0x65, 0xa1, 0x6c, 0xb7, 0x86, 0x13, 0x35, 0xd5, 0xac, 0xe3, 0xc6, 0x07, + 0x18, 0xb5, 0x05, 0x2e, 0x44, 0x66, 0x07, 0x26, 0xda, 0x4c, 0xd1, 0x3b, + 0xb7, 0x45, 0x38, 0x1b, 0x23, 0x5a, 0x17, 0x85 }; uint8_t m[57]; uint8_t digest[32]; @@ -226,15 +221,15 @@ uint32_t test_7(void) return result; } -uint32_t test_8(void) +unsigned int test_8(void) { - uint32_t result = TC_PASS; + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #8:\n"); const uint8_t expected[32] = { - 0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30, 0x27, 0x98, 0xef, 0x6e, - 0xd3, 0x09, 0x97, 0x9b, 0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9, 0xf0, 0xe8, - 0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b + 0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30, 0x27, 0x98, 0xef, 0x6e, + 0xd3, 0x09, 0x97, 0x9b, 0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9, 0xf0, 0xe8, + 0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b }; uint8_t m[64]; uint8_t digest[32]; @@ -252,15 +247,14 @@ uint32_t test_8(void) return result; } -uint32_t test_9(void) +unsigned int test_9(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #9:\n"); const uint8_t expected[32] = { - 0x54, 0x1b, 0x3e, 0x9d, 0xaa, 0x09, 0xb2, 0x0b, 0xf8, 0x5f, 0xa2, 0x73, - 0xe5, 0xcb, 0xd3, 0xe8, 0x01, 0x85, 0xaa, 0x4e, 0xc2, 0x98, 0xe7, 0x65, - 0xdb, 0x87, 0x74, 0x2b, 0x70, 0x13, 0x8a, 0x53 + 0x54, 0x1b, 0x3e, 0x9d, 0xaa, 0x09, 0xb2, 0x0b, 0xf8, 0x5f, 0xa2, 0x73, + 0xe5, 0xcb, 0xd3, 0xe8, 0x01, 0x85, 0xaa, 0x4e, 0xc2, 0x98, 0xe7, 0x65, + 0xdb, 0x87, 0x74, 0x2b, 0x70, 0x13, 0x8a, 0x53 }; uint8_t m[1000]; uint8_t digest[32]; @@ -278,15 +272,14 @@ uint32_t test_9(void) return result; } -uint32_t test_10(void) +unsigned int test_10(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #10:\n"); const uint8_t expected[32] = { - 0xc2, 0xe6, 0x86, 0x82, 0x34, 0x89, 0xce, 0xd2, 0x01, 0x7f, 0x60, 0x59, - 0xb8, 0xb2, 0x39, 0x31, 0x8b, 0x63, 0x64, 0xf6, 0xdc, 0xd8, 0x35, 0xd0, - 0xa5, 0x19, 0x10, 0x5a, 0x1e, 0xad, 0xd6, 0xe4 + 0xc2, 0xe6, 0x86, 0x82, 0x34, 0x89, 0xce, 0xd2, 0x01, 0x7f, 0x60, 0x59, + 0xb8, 0xb2, 0x39, 0x31, 0x8b, 0x63, 0x64, 0xf6, 0xdc, 0xd8, 0x35, 0xd0, + 0xa5, 0x19, 0x10, 0x5a, 0x1e, 0xad, 0xd6, 0xe4 }; uint8_t m[1000]; uint8_t digest[32]; @@ -304,15 +297,14 @@ uint32_t test_10(void) return result; } -uint32_t test_11(void) +unsigned int test_11(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #11:\n"); const uint8_t expected[32] = { - 0xf4, 0xd6, 0x2d, 0xde, 0xc0, 0xf3, 0xdd, 0x90, 0xea, 0x13, 0x80, 0xfa, - 0x16, 0xa5, 0xff, 0x8d, 0xc4, 0xc5, 0x4b, 0x21, 0x74, 0x06, 0x50, 0xf2, - 0x4a, 0xfc, 0x41, 0x20, 0x90, 0x35, 0x52, 0xb0 + 0xf4, 0xd6, 0x2d, 0xde, 0xc0, 0xf3, 0xdd, 0x90, 0xea, 0x13, 0x80, 0xfa, + 0x16, 0xa5, 0xff, 0x8d, 0xc4, 0xc5, 0x4b, 0x21, 0x74, 0x06, 0x50, 0xf2, + 0x4a, 0xfc, 0x41, 0x20, 0x90, 0x35, 0x52, 0xb0 }; uint8_t m[1005]; uint8_t digest[32]; @@ -330,20 +322,20 @@ uint32_t test_11(void) return result; } -uint32_t test_12(void) +unsigned int test_12(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #12:\n"); + const uint8_t expected[32] = { - 0xd2, 0x97, 0x51, 0xf2, 0x64, 0x9b, 0x32, 0xff, 0x57, 0x2b, 0x5e, 0x0a, - 0x9f, 0x54, 0x1e, 0xa6, 0x60, 0xa5, 0x0f, 0x94, 0xff, 0x0b, 0xee, 0xdf, - 0xb0, 0xb6, 0x92, 0xb9, 0x24, 0xcc, 0x80, 0x25 + 0xd2, 0x97, 0x51, 0xf2, 0x64, 0x9b, 0x32, 0xff, 0x57, 0x2b, 0x5e, 0x0a, + 0x9f, 0x54, 0x1e, 0xa6, 0x60, 0xa5, 0x0f, 0x94, 0xff, 0x0b, 0xee, 0xdf, + 0xb0, 0xb6, 0x92, 0xb9, 0x24, 0xcc, 0x80, 0x25 }; uint8_t m[1000]; uint8_t digest[32]; struct tc_sha256_state_struct s; - uint32_t i; + unsigned int i; (void)memset(m, 0x00, sizeof(m)); @@ -359,20 +351,19 @@ uint32_t test_12(void) return result; } -uint32_t test_13(void) +unsigned int test_13(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #13:\n"); const uint8_t expected[32] = { - 0x15, 0xa1, 0x86, 0x8c, 0x12, 0xcc, 0x53, 0x95, 0x1e, 0x18, 0x23, 0x44, - 0x27, 0x74, 0x47, 0xcd, 0x09, 0x79, 0x53, 0x6b, 0xad, 0xcc, 0x51, 0x2a, - 0xd2, 0x4c, 0x67, 0xe9, 0xb2, 0xd4, 0xf3, 0xdd + 0x15, 0xa1, 0x86, 0x8c, 0x12, 0xcc, 0x53, 0x95, 0x1e, 0x18, 0x23, 0x44, + 0x27, 0x74, 0x47, 0xcd, 0x09, 0x79, 0x53, 0x6b, 0xad, 0xcc, 0x51, 0x2a, + 0xd2, 0x4c, 0x67, 0xe9, 0xb2, 0xd4, 0xf3, 0xdd }; uint8_t m[32768]; uint8_t digest[32]; struct tc_sha256_state_struct s; - uint32_t i; + unsigned int i; (void)memset(m, 0x5a, sizeof(m)); @@ -388,20 +379,19 @@ uint32_t test_13(void) return result; } -uint32_t test_14(void) +unsigned int test_14(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_PRINT("SHA256 test #14:\n"); const uint8_t expected[32] = { - 0x46, 0x1c, 0x19, 0xa9, 0x3b, 0xd4, 0x34, 0x4f, 0x92, 0x15, 0xf5, 0xec, - 0x64, 0x35, 0x70, 0x90, 0x34, 0x2b, 0xc6, 0x6b, 0x15, 0xa1, 0x48, 0x31, - 0x7d, 0x27, 0x6e, 0x31, 0xcb, 0xc2, 0x0b, 0x53 + 0x46, 0x1c, 0x19, 0xa9, 0x3b, 0xd4, 0x34, 0x4f, 0x92, 0x15, 0xf5, 0xec, + 0x64, 0x35, 0x70, 0x90, 0x34, 0x2b, 0xc6, 0x6b, 0x15, 0xa1, 0x48, 0x31, + 0x7d, 0x27, 0x6e, 0x31, 0xcb, 0xc2, 0x0b, 0x53 }; uint8_t m[32768]; uint8_t digest[32]; struct tc_sha256_state_struct s; - uint32_t i; + unsigned int i; (void)memset(m, 0x00, sizeof(m)); @@ -423,77 +413,91 @@ uint32_t test_14(void) int main(void) { - uint32_t result = TC_PASS; - + unsigned int result = TC_PASS; TC_START("Performing SHA256 tests (NIST tests vectors):"); result = test_1(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #1 failed.\n"); goto exitTest; } result = test_2(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #2 failed.\n"); goto exitTest; } result = test_3(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #3 failed.\n"); goto exitTest; } result = test_4(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #4 failed.\n"); goto exitTest; } result = test_5(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #5 failed.\n"); goto exitTest; } result = test_6(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #6 failed.\n"); goto exitTest; } result = test_7(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #7 failed.\n"); goto exitTest; } result = test_8(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #8 failed.\n"); goto exitTest; } result = test_9(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #9 failed.\n"); goto exitTest; } result = test_10(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #10 failed.\n"); goto exitTest; } result = test_11(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #11 failed.\n"); goto exitTest; } result = test_12(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #12 failed.\n"); goto exitTest; } + /* memory and computation intensive test cases: */ result = test_13(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #13 failed.\n"); goto exitTest; } result = test_14(); - if (result == TC_FAIL) { /* terminate test */ + if (result == TC_FAIL) { + /* terminate test */ TC_ERROR("SHA256 test #14 failed.\n"); goto exitTest; } @@ -504,3 +508,4 @@ exitTest: TC_END_RESULT(result); TC_END_REPORT(result); } + -- cgit v1.2.3