From 2ebbe6eb870313331bba48884112675107a6b866 Mon Sep 17 00:00:00 2001 From: areviu Date: Thu, 30 Nov 2017 21:12:58 +0000 Subject: update sha and trng lld git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@11091 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- test/crypto/source/test/cry_test_root.c | 10 + test/crypto/source/test/cry_test_root.h | 9 + test/crypto/source/test/cry_test_sequence_005.c | 128 ++++++++++ test/crypto/source/test/cry_test_sequence_005.h | 27 +++ test/crypto/source/test/cry_test_sequence_006.c | 295 ++++++++++++++++++++++++ test/crypto/source/test/cry_test_sequence_006.h | 27 +++ test/crypto/source/testref/ref_sha.c | 58 +++++ test/crypto/source/testref/ref_sha.h | 25 ++ 8 files changed, 579 insertions(+) create mode 100644 test/crypto/source/test/cry_test_sequence_005.c create mode 100644 test/crypto/source/test/cry_test_sequence_005.h create mode 100644 test/crypto/source/test/cry_test_sequence_006.c create mode 100644 test/crypto/source/test/cry_test_sequence_006.h create mode 100644 test/crypto/source/testref/ref_sha.c create mode 100644 test/crypto/source/testref/ref_sha.h (limited to 'test/crypto/source') diff --git a/test/crypto/source/test/cry_test_root.c b/test/crypto/source/test/cry_test_root.c index e80e1d3fb..bd176b409 100644 --- a/test/crypto/source/test/cry_test_root.c +++ b/test/crypto/source/test/cry_test_root.c @@ -25,6 +25,8 @@ * - @subpage cry_test_sequence_002 * - @subpage cry_test_sequence_003 * - @subpage cry_test_sequence_004 + * - @subpage cry_test_sequence_005 + * - @subpage cry_test_sequence_006 * . */ @@ -50,6 +52,8 @@ const testsequence_t * const cry_test_suite_array[] = { &cry_test_sequence_002, &cry_test_sequence_003, &cry_test_sequence_004, + &cry_test_sequence_005, + &cry_test_sequence_006, NULL }; @@ -107,6 +111,12 @@ am quis dolor in libero placerat congue. Sed sodales urna sceler\ isque dui faucibus, vitae malesuada dui fermentum. Proin ultrici\ es sit amet justo at ornare. Suspendisse efficitur purus nullam."; + +const uint8_t sha_msg0[SHA_LEN_0] = "hi!"; + +const uint8_t sha_msg1[SHA_LEN_1] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; + + ALIGNED_VAR(4) uint32_t msg_clear[TEST_MSG_DATA_WORD_LEN]; ALIGNED_VAR(4) uint32_t msg_encrypted[TEST_MSG_DATA_WORD_LEN]; ALIGNED_VAR(4) uint32_t msg_decrypted[TEST_MSG_DATA_WORD_LEN]; diff --git a/test/crypto/source/test/cry_test_root.h b/test/crypto/source/test/cry_test_root.h index 2bb5ab029..24842646f 100644 --- a/test/crypto/source/test/cry_test_root.h +++ b/test/crypto/source/test/cry_test_root.h @@ -28,6 +28,8 @@ #include "cry_test_sequence_002.h" #include "cry_test_sequence_003.h" #include "cry_test_sequence_004.h" +#include "cry_test_sequence_005.h" +#include "cry_test_sequence_006.h" #if !defined(__DOXYGEN__) @@ -53,9 +55,11 @@ extern void cryptoTest_printArray32(bool isLE,const uint32_t *a,size_t len); #ifdef LOG_CRYPTO_DATA #define SHOW_ENCRYPDATA(w) cryptoTest_printArray32(true,msg_encrypted,w) #define SHOW_DECRYPDATA(w) cryptoTest_printArray32(true,msg_decrypted,w) +#define SHOW_DATA(d,w) cryptoTest_printArray32(true,d,w) #else #define SHOW_ENCRYPDATA(w) #define SHOW_DECRYPDATA(w) +#define SHOW_DATA(d,w) #endif #define TEST_DATA_BYTE_LEN 640 @@ -65,12 +69,17 @@ extern void cryptoTest_printArray32(bool isLE,const uint32_t *a,size_t len); #define TEST_MSG_DATA_BYTE_LEN 640 #define TEST_MSG_DATA_WORD_LEN (TEST_MSG_DATA_BYTE_LEN / 4) +#define SHA_LEN_0 3 +#define SHA_LEN_1 56 + extern const char test_plain_data[TEST_DATA_BYTE_LEN]; extern uint32_t msg_clear[TEST_MSG_DATA_WORD_LEN]; extern uint32_t msg_encrypted[TEST_MSG_DATA_WORD_LEN]; extern uint32_t msg_decrypted[TEST_MSG_DATA_WORD_LEN]; extern const uint32_t test_keys[8]; extern const uint32_t test_vectors[4]; +extern const uint8_t sha_msg0[SHA_LEN_0]; +extern const uint8_t sha_msg1[SHA_LEN_1]; #endif /* !defined(__DOXYGEN__) */ diff --git a/test/crypto/source/test/cry_test_sequence_005.c b/test/crypto/source/test/cry_test_sequence_005.c new file mode 100644 index 000000000..fb15c13c1 --- /dev/null +++ b/test/crypto/source/test/cry_test_sequence_005.c @@ -0,0 +1,128 @@ +/* + ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#include "hal.h" +#include "cry_test_root.h" + +/** + * @file cry_test_sequence_005.c + * @brief Test Sequence 005 code. + * + * @page cry_test_sequence_005 [5] TRNG + * + * File: @ref cry_test_sequence_005.c + * + *

Description

+ * TRNG testing. + * + *

Test Cases

+ * - @subpage cry_test_005_001 + * . + */ + +/**************************************************************************** + * Shared code. + ****************************************************************************/ + +#include + +static const CRYConfig configTRNG_Polling= +{ + TRANSFER_POLLING, + 0, + 0 +}; + + + +/**************************************************************************** + * Test cases. + ****************************************************************************/ + +/** + * @page cry_test_005_001 [5.1] TRNG Polling + * + *

Description

+ * testing TRNG in polled mode. + * + *

Test Steps

+ * - [5.1.1] Random generation and test. + * . + */ + +static void cry_test_005_001_setup(void) { + cryStart(&CRYD1, &configTRNG_Polling); + + +} + +static void cry_test_005_001_teardown(void) { + cryStop(&CRYD1); +} + +static void cry_test_005_001_execute(void) { + cryerror_t ret; + + /* [5.1.1] Random generation and test.*/ + test_set_step(1); + { + uint32_t random[10]; + int i,j; + for (i=0;i<10;i++) + { + ret = cryTRNG(&CRYD1,(uint8_t*)&random[i]); + + test_assert(ret == CRY_NOERROR , "failed random"); + + SHOW_DATA(&random[i],1); + + test_assert(random[i] != 0 , "failed random generation (zero)"); + + for (j=0;jDescription + * SHA testing. + * + *

Test Cases

+ * - @subpage cry_test_006_001 + * - @subpage cry_test_006_002 + * - @subpage cry_test_006_003 + * . + */ + +/**************************************************************************** + * Shared code. + ****************************************************************************/ + +#include +#include "ref_sha.h" + +#define MAX_DIGEST_SIZE_INBYTE 64 +#define MAX_DIGEST_SIZE_INWORD (MAX_DIGEST_SIZE_INBYTE/4) +static uint32_t digest[MAX_DIGEST_SIZE_INWORD]; + +static const CRYConfig configSHA_Polling= +{ + TRANSFER_POLLING, + 0, + 0 +}; + + + +/**************************************************************************** + * Test cases. + ****************************************************************************/ + +/** + * @page cry_test_006_001 [6.1] SHA1 Polling + * + *

Description

+ * testing SHA1 in polled mode. + * + *

Test Steps

+ * - [6.1.1] Digest. + * . + */ + +static void cry_test_006_001_setup(void) { + memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN); + memset(digest, 0, MAX_DIGEST_SIZE_INWORD); + memcpy((char*) msg_clear, sha_msg0, SHA_LEN_0); + cryStart(&CRYD1, &configSHA_Polling); + + +} + +static void cry_test_006_001_teardown(void) { + cryStop(&CRYD1); +} + +static void cry_test_006_001_execute(void) { + cryerror_t ret; + uint32_t *ref; + + /* [6.1.1] Digest.*/ + test_set_step(1); + { + //---- One Block Test + ret = crySHA1(&CRYD1,SHA_LEN_0,(uint8_t*)msg_clear,(uint8_t*)digest); + + + test_assert(ret == CRY_NOERROR, "sha1 failed"); + + + SHOW_DATA(digest,5); + + ref = (uint32_t*)refSHA_SHA1_3; + for (int i = 0; i < 5; i++) { + test_assert(digest[i] == ref[i], "sha1 digest mismatch"); + } + + //---- Multi Block Test + memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN); + memcpy((char*) msg_clear, sha_msg1, SHA_LEN_1); + + ret = crySHA1(&CRYD1,SHA_LEN_1,(uint8_t*)msg_clear,(uint8_t*)digest); + + test_assert(ret == CRY_NOERROR, "sha1 failed"); + + SHOW_DATA(digest,5); + + + ref = (uint32_t*)refSHA_SHA1_56; + for (int i = 0; i < 5; i++) { + test_assert(digest[i] == ref[i], "sha1 digest mismatch"); + } + + + } +} + +static const testcase_t cry_test_006_001 = { + "SHA1 Polling", + cry_test_006_001_setup, + cry_test_006_001_teardown, + cry_test_006_001_execute +}; + +/** + * @page cry_test_006_002 [6.2] SHA256 Polling + * + *

Description

+ * testing SHA256 in polled mode. + * + *

Test Steps

+ * - [6.2.1] Digest. + * . + */ + +static void cry_test_006_002_setup(void) { + memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN); + memset(digest, 0, MAX_DIGEST_SIZE_INWORD); + memcpy((char*) msg_clear, sha_msg0, SHA_LEN_0); + cryStart(&CRYD1, &configSHA_Polling); + + +} + +static void cry_test_006_002_teardown(void) { + cryStop(&CRYD1); +} + +static void cry_test_006_002_execute(void) { + cryerror_t ret; + uint32_t *ref; + + /* [6.2.1] Digest.*/ + test_set_step(1); + { + + //---- One Block Test + ret = crySHA256(&CRYD1,SHA_LEN_0,(uint8_t*)msg_clear,(uint8_t*)digest); + + test_assert(ret == CRY_NOERROR, "sha256 failed"); + + SHOW_DATA(digest,8); + + ref = (uint32_t*)refSHA_SHA256_3; + for (int i = 0; i < 8; i++) { + test_assert(digest[i] == ref[i], "sha256 digest mismatch"); + } + + //---- Multi Block Test + memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN); + memcpy((char*) msg_clear, sha_msg1, SHA_LEN_1); + + ret = crySHA256(&CRYD1,SHA_LEN_1,(uint8_t*)msg_clear,(uint8_t*)digest); + + test_assert(ret == CRY_NOERROR, "sha256 failed"); + + + SHOW_DATA(digest,8); + + ref = (uint32_t*)refSHA_SHA256_56; + for (int i = 0; i < 8; i++) { + test_assert(digest[i] == ref[i], "sha256 digest mismatch"); + } + + + } +} + +static const testcase_t cry_test_006_002 = { + "SHA256 Polling", + cry_test_006_002_setup, + cry_test_006_002_teardown, + cry_test_006_002_execute +}; + +/** + * @page cry_test_006_003 [6.3] SHA512 Polling + * + *

Description

+ * testing SHA512 in polled mode. + * + *

Test Steps

+ * - [6.3.1] Digest. + * . + */ + +static void cry_test_006_003_setup(void) { + memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN); + memset(digest, 0, MAX_DIGEST_SIZE_INWORD); + memcpy((char*) msg_clear, sha_msg0, SHA_LEN_0); + cryStart(&CRYD1, &configSHA_Polling); + + +} + +static void cry_test_006_003_teardown(void) { + cryStop(&CRYD1); +} + +static void cry_test_006_003_execute(void) { + cryerror_t ret; + uint32_t *ref; + + /* [6.3.1] Digest.*/ + test_set_step(1); + { + //---- One Block Test + ret = crySHA512(&CRYD1,SHA_LEN_0,(uint8_t*)msg_clear,(uint8_t*)digest); + + test_assert(ret == CRY_NOERROR, "sha512 failed"); + + SHOW_DATA(digest,16); + + ref = (uint32_t*)refSHA_SHA512_3; + for (int i = 0; i < 16; i++) { + test_assert(digest[i] == ref[i], "sha512 digest mismatch"); + } + + //---- Multi Block Test + memset(msg_clear, 0, TEST_MSG_DATA_BYTE_LEN); + memcpy((char*) msg_clear, sha_msg1, SHA_LEN_1); + + ret = crySHA512(&CRYD1,SHA_LEN_1,(uint8_t*)msg_clear,(uint8_t*)digest); + + test_assert(ret == CRY_NOERROR, "sha512 failed"); + + + SHOW_DATA(digest,16); + + ref = (uint32_t*)refSHA_SHA512_56; + for (int i = 0; i < 16; i++) { + test_assert(digest[i] == ref[i], "sha512 digest mismatch"); + } + + + } +} + +static const testcase_t cry_test_006_003 = { + "SHA512 Polling", + cry_test_006_003_setup, + cry_test_006_003_teardown, + cry_test_006_003_execute +}; + +/**************************************************************************** + * Exported data. + ****************************************************************************/ + +/** + * @brief Array of test cases. + */ +const testcase_t * const cry_test_sequence_006_array[] = { + &cry_test_006_001, + &cry_test_006_002, + &cry_test_006_003, + NULL +}; + +/** + * @brief SHA. + */ +const testsequence_t cry_test_sequence_006 = { + "SHA", + cry_test_sequence_006_array +}; diff --git a/test/crypto/source/test/cry_test_sequence_006.h b/test/crypto/source/test/cry_test_sequence_006.h new file mode 100644 index 000000000..8bc6061ce --- /dev/null +++ b/test/crypto/source/test/cry_test_sequence_006.h @@ -0,0 +1,27 @@ +/* + ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +/** + * @file cry_test_sequence_006.h + * @brief Test Sequence 006 header. + */ + +#ifndef CRY_TEST_SEQUENCE_006_H +#define CRY_TEST_SEQUENCE_006_H + +extern const testsequence_t cry_test_sequence_006; + +#endif /* CRY_TEST_SEQUENCE_006_H */ diff --git a/test/crypto/source/testref/ref_sha.c b/test/crypto/source/testref/ref_sha.c new file mode 100644 index 000000000..bada14c93 --- /dev/null +++ b/test/crypto/source/testref/ref_sha.c @@ -0,0 +1,58 @@ +/* + ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#include "hal.h" + +const uint8_t refSHA_SHA1_3[]={ +0x3A,0x98,0x7A,0xCF,0x8C,0xBC,0x10,0x28,0xB7,0xDB, +0xC8,0x6B,0xD0,0x86,0x83,0x11,0x51,0x89,0x9A,0x2B, + +}; +const uint8_t refSHA_SHA1_56[]={ +0x84,0x98,0x3E,0x44,0x1C,0x3B,0xD2,0x6E,0xBA,0xAE, +0x4A,0xA1,0xF9,0x51,0x29,0xE5,0xE5,0x46,0x70,0xF1, + +}; +const uint8_t refSHA_SHA256_3[]={ +0xC0,0xDD,0xD6,0x2C,0x77,0x17,0x18,0x0E,0x7F,0xFB, +0x8A,0x15,0xBB,0x96,0x74,0xD3,0xEC,0x92,0x59,0x2E, +0x0B,0x7A,0xC7,0xD1,0xD5,0x28,0x98,0x36,0xB4,0x55, +0x3B,0xE2, +}; +const uint8_t refSHA_SHA256_56[]={ +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 uint8_t refSHA_SHA512_3[]={ +0x3E,0xBB,0x6E,0x93,0x1E,0xAA,0x4D,0xCF,0x74,0x1A, +0xD1,0x23,0x37,0xD4,0xF7,0x10,0x5B,0x02,0xD4,0xA9, +0xB1,0x94,0x21,0x4E,0x88,0x55,0x9E,0x8E,0x41,0xEC, +0x04,0xD3,0x20,0xE9,0x6A,0x3C,0xF9,0x12,0xED,0x27, +0x34,0x29,0x35,0xA6,0xF8,0x9D,0x1F,0x5C,0x1A,0x5C, +0xD7,0xF7,0xFF,0xBF,0xA9,0xB1,0xBE,0x1A,0x41,0x62, +0xED,0x32,0x3C,0x7A, +}; +const uint8_t refSHA_SHA512_56[]={ +0x20,0x4A,0x8F,0xC6,0xDD,0xA8,0x2F,0x0A,0x0C,0xED, +0x7B,0xEB,0x8E,0x08,0xA4,0x16,0x57,0xC1,0x6E,0xF4, +0x68,0xB2,0x28,0xA8,0x27,0x9B,0xE3,0x31,0xA7,0x03, +0xC3,0x35,0x96,0xFD,0x15,0xC1,0x3B,0x1B,0x07,0xF9, +0xAA,0x1D,0x3B,0xEA,0x57,0x78,0x9C,0xA0,0x31,0xAD, +0x85,0xC7,0xA7,0x1D,0xD7,0x03,0x54,0xEC,0x63,0x12, +0x38,0xCA,0x34,0x45, +}; diff --git a/test/crypto/source/testref/ref_sha.h b/test/crypto/source/testref/ref_sha.h new file mode 100644 index 000000000..b487d519b --- /dev/null +++ b/test/crypto/source/testref/ref_sha.h @@ -0,0 +1,25 @@ +/* + ChibiOS - Copyright (C) 2006..2017 Giovanni Di Sirio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +#ifndef TEST_REF_SHA_H_ +#define TEST_REF_SHA_H_ + +extern const uint8_t refSHA_SHA1_3[]; +extern const uint8_t refSHA_SHA1_56[]; +extern const uint8_t refSHA_SHA256_3[]; +extern const uint8_t refSHA_SHA256_56[]; +extern const uint8_t refSHA_SHA512_3[]; +extern const uint8_t refSHA_SHA512_56[]; +#endif //TEST_REF_SHA_H_ -- cgit v1.2.3