From 91f17dc7c4e79343b8fba924e7cea6380a0e6653 Mon Sep 17 00:00:00 2001 From: Damjan Marion Date: Mon, 18 Mar 2019 18:59:25 +0100 Subject: crypto: introduce crypto infra Change-Id: Ibf320b3e7b054b686f3af9a55afd5d5bda9b1048 Signed-off-by: Damjan Marion Signed-off-by: Filip Tehlar --- src/plugins/unittest/CMakeLists.txt | 4 + src/plugins/unittest/crypto/aes_cbc.c | 157 +++++++++++++++++++++++++++ src/plugins/unittest/crypto/crypto.h | 67 ++++++++++++ src/plugins/unittest/crypto/rfc2202.c | 84 +++++++++++++++ src/plugins/unittest/crypto/rfc4231.c | 197 ++++++++++++++++++++++++++++++++++ src/plugins/unittest/crypto_test.c | 152 ++++++++++++++++++++++++++ 6 files changed, 661 insertions(+) create mode 100644 src/plugins/unittest/crypto/aes_cbc.c create mode 100644 src/plugins/unittest/crypto/crypto.h create mode 100644 src/plugins/unittest/crypto/rfc2202.c create mode 100644 src/plugins/unittest/crypto/rfc4231.c create mode 100644 src/plugins/unittest/crypto_test.c (limited to 'src/plugins/unittest') diff --git a/src/plugins/unittest/CMakeLists.txt b/src/plugins/unittest/CMakeLists.txt index 1716077e129..555404b24a6 100644 --- a/src/plugins/unittest/CMakeLists.txt +++ b/src/plugins/unittest/CMakeLists.txt @@ -15,6 +15,10 @@ add_vpp_plugin(unittest SOURCES bier_test.c bihash_test.c + crypto_test.c + crypto/aes_cbc.c + crypto/rfc2202.c + crypto/rfc4231.c fib_test.c interface_test.c mfib_test.c diff --git a/src/plugins/unittest/crypto/aes_cbc.c b/src/plugins/unittest/crypto/aes_cbc.c new file mode 100644 index 00000000000..465f91e675f --- /dev/null +++ b/src/plugins/unittest/crypto/aes_cbc.c @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2019 Cisco and/or its affiliates. + * 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. + */ + +/* Test vectors published by NIST as SP 800-38A + https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/AES_CFB.pdf + */ + +#include +#include +#include + +static u8 iv[] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, +}; + +static u8 plaintext[] = { + 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, +}; + +static u8 key128[] = { + 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, + 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C +}; + +static u8 ciphertext128[] = { + 0x76, 0x49, 0xAB, 0xAC, 0x81, 0x19, 0xB2, 0x46, + 0xCE, 0xE9, 0x8E, 0x9B, 0x12, 0xE9, 0x19, 0x7D, + 0x50, 0x86, 0xCB, 0x9B, 0x50, 0x72, 0x19, 0xEE, + 0x95, 0xDB, 0x11, 0x3A, 0x91, 0x76, 0x78, 0xB2, + 0x73, 0xBE, 0xD6, 0xB8, 0xE3, 0xC1, 0x74, 0x3B, + 0x71, 0x16, 0xE6, 0x9E, 0x22, 0x22, 0x95, 0x16, + 0x3F, 0xF1, 0xCA, 0xA1, 0x68, 0x1F, 0xAC, 0x09, + 0x12, 0x0E, 0xCA, 0x30, 0x75, 0x86, 0xE1, 0xA7, +}; + +/* *INDENT-OFF* */ +UNITTEST_REGISTER_CRYPTO_TEST (nist_aes128_cbc_enc) = { + .name = "NIST SP 800-38A", + .op = VNET_CRYPTO_OP_AES_128_CBC_ENC, + .iv = TEST_DATA (iv), + .key = TEST_DATA (key128), + .data = TEST_DATA (plaintext), + .expected = TEST_DATA (ciphertext128), +}; + +UNITTEST_REGISTER_CRYPTO_TEST (nist_aes128_cbc_dec) = { + .name = "NIST SP 800-38A", + .op = VNET_CRYPTO_OP_AES_128_CBC_DEC, + .iv = TEST_DATA (iv), + .key = TEST_DATA (key128), + .data = TEST_DATA (ciphertext128), + .expected = TEST_DATA (plaintext), +}; +/* *INDENT-ON* */ + +static u8 key192[24] = { + 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52, + 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5, + 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B, +}; + +static u8 ciphertext192[64] = { + 0x4F, 0x02, 0x1D, 0xB2, 0x43, 0xBC, 0x63, 0x3D, + 0x71, 0x78, 0x18, 0x3A, 0x9F, 0xA0, 0x71, 0xE8, + 0xB4, 0xD9, 0xAD, 0xA9, 0xAD, 0x7D, 0xED, 0xF4, + 0xE5, 0xE7, 0x38, 0x76, 0x3F, 0x69, 0x14, 0x5A, + 0x57, 0x1B, 0x24, 0x20, 0x12, 0xFB, 0x7A, 0xE0, + 0x7F, 0xA9, 0xBA, 0xAC, 0x3D, 0xF1, 0x02, 0xE0, + 0x08, 0xB0, 0xE2, 0x79, 0x88, 0x59, 0x88, 0x81, + 0xD9, 0x20, 0xA9, 0xE6, 0x4F, 0x56, 0x15, 0xCD, +}; + +/* *INDENT-OFF* */ +UNITTEST_REGISTER_CRYPTO_TEST (nist_aes192_cbc_enc) = { + .name = "NIST SP 800-38A", + .op = VNET_CRYPTO_OP_AES_192_CBC_ENC, + .iv = TEST_DATA (iv), + .key = TEST_DATA (key192), + .data = TEST_DATA (plaintext), + .expected = TEST_DATA (ciphertext192), +}; + +UNITTEST_REGISTER_CRYPTO_TEST (nist_aes192_cbc_dec) = { + .name = "NIST SP 800-38A", + .op = VNET_CRYPTO_OP_AES_192_CBC_DEC, + .iv = TEST_DATA (iv), + .key = TEST_DATA (key192), + .data = TEST_DATA (ciphertext192), + .expected = TEST_DATA (plaintext), +}; +/* *INDENT-ON* */ + +static u8 key256[32] = { + 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE, + 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81, + 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7, + 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4, +}; + +static u8 ciphertext256[64] = { + 0xF5, 0x8C, 0x4C, 0x04, 0xD6, 0xE5, 0xF1, 0xBA, + 0x77, 0x9E, 0xAB, 0xFB, 0x5F, 0x7B, 0xFB, 0xD6, + 0x9C, 0xFC, 0x4E, 0x96, 0x7E, 0xDB, 0x80, 0x8D, + 0x67, 0x9F, 0x77, 0x7B, 0xC6, 0x70, 0x2C, 0x7D, + 0x39, 0xF2, 0x33, 0x69, 0xA9, 0xD9, 0xBA, 0xCF, + 0xA5, 0x30, 0xE2, 0x63, 0x04, 0x23, 0x14, 0x61, + 0xB2, 0xEB, 0x05, 0xE2, 0xC3, 0x9B, 0xE9, 0xFC, + 0xDA, 0x6C, 0x19, 0x07, 0x8C, 0x6A, 0x9D, 0x1B, +}; + +/* *INDENT-OFF* */ +UNITTEST_REGISTER_CRYPTO_TEST (nist_aes256_cbc_enc) = { + .name = "NIST SP 800-38A", + .op = VNET_CRYPTO_OP_AES_256_CBC_ENC, + .iv = TEST_DATA (iv), + .key = TEST_DATA (key256), + .data = TEST_DATA (plaintext), + .expected = TEST_DATA (ciphertext256), +}; + +UNITTEST_REGISTER_CRYPTO_TEST (nist_aes256_cbc_dec) = { + .name = "NIST SP 800-38A", + .op = VNET_CRYPTO_OP_AES_256_CBC_DEC, + .iv = TEST_DATA (iv), + .key = TEST_DATA (key256), + .data = TEST_DATA (ciphertext256), + .expected = TEST_DATA (plaintext), +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/unittest/crypto/crypto.h b/src/plugins/unittest/crypto/crypto.h new file mode 100644 index 00000000000..fe6e7f2831c --- /dev/null +++ b/src/plugins/unittest/crypto/crypto.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2019 Cisco and/or its affiliates. + * 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 included_unittest_crypto_crypto_h +#define included_unittest_crypto_crypto_h + +typedef struct +{ + u32 length; + u8 *data; +} unittest_crypto_test_data_t; + +typedef struct unittest_crypto_test_registration +{ + char *name; + vnet_crypto_alg_t alg:8; + vnet_crypto_op_type_t op:8; + unittest_crypto_test_data_t iv, key, data, expected; + + /* next */ + struct unittest_crypto_test_registration *next; +} unittest_crypto_test_registration_t; + + +typedef struct +{ + int verbose; + unittest_crypto_test_registration_t *test_registrations; +} crypto_test_main_t; + +extern crypto_test_main_t crypto_test_main; + +#define TEST_DATA(n) { .data = (u8 *) n, .length = sizeof (n)} + +#define UNITTEST_REGISTER_CRYPTO_TEST(x) \ + unittest_crypto_test_registration_t __unittest_crypto_test_##x; \ +static void __clib_constructor \ +__unittest_crypto_test_registration_##x (void) \ +{ \ + crypto_test_main_t * cm = &crypto_test_main; \ + __unittest_crypto_test_##x.next = cm->test_registrations; \ + cm->test_registrations = & __unittest_crypto_test_##x; \ +} \ +unittest_crypto_test_registration_t __unittest_crypto_test_##x + +#endif + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/unittest/crypto/rfc2202.c b/src/plugins/unittest/crypto/rfc2202.c new file mode 100644 index 00000000000..935aafd9bca --- /dev/null +++ b/src/plugins/unittest/crypto/rfc2202.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2019 Cisco and/or its affiliates. + * 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. + */ + +/* Test vectors published in RFC2202 */ + +#include +#include +#include + +static u8 sha1_tc1_key[] = { + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b +}; + +static char sha1_tc1_data[8] = "Hi There"; + +static u8 sha1_tc1_digest[] = { + 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, + 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e, + 0xf1, 0x46, 0xbe, 0x00 +}; + +/* *INDENT-OFF* */ +UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc1) = { + .name = "RFC2202 HMAC-SHA-1 TC1", + .op = VNET_CRYPTO_OP_SHA1_HMAC, + .key = TEST_DATA (sha1_tc1_key), + .data = TEST_DATA (sha1_tc1_data), + .expected = TEST_DATA (sha1_tc1_digest), +}; +/* *INDENT-ON* */ + +static u8 sha1_tc7_key[80] = { + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, + 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, +}; + +static char sha1_tc7_data[73] = + "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"; + +static u8 sha1_tc7_digest[20] = { + 0xe8, 0xe9, 0x9d, 0x0f, 0x45, 0x23, 0x7d, 0x78, + 0x6d, 0x6b, 0xba, 0xa7, 0x96, 0x5c, 0x78, 0x08, + 0xbb, 0xff, 0x1a, 0x91 +}; + +/* *INDENT-OFF* */ +UNITTEST_REGISTER_CRYPTO_TEST (rfc_2202_sha1_tc7) = { + .name = "RFC2202 HMAC-SHA-1 TC7", + .op = VNET_CRYPTO_OP_SHA1_HMAC, + .key = TEST_DATA (sha1_tc7_key), + .data = TEST_DATA (sha1_tc7_data), + .expected = TEST_DATA (sha1_tc7_digest), +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/unittest/crypto/rfc4231.c b/src/plugins/unittest/crypto/rfc4231.c new file mode 100644 index 00000000000..88a3debbe1e --- /dev/null +++ b/src/plugins/unittest/crypto/rfc4231.c @@ -0,0 +1,197 @@ +/* + * Copyright (c) 2019 Cisco and/or its affiliates. + * 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. + */ + +/* Test vectors published in RFC2202 */ + +#include +#include +#include + +static u8 tc1_key[] = { + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b +}; + +static char tc1_data[8] = "Hi There"; + +static u8 tc1_digest_sha224[] = { + 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19, + 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f, + 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f, + 0x53, 0x68, 0x4b, 0x22 +}; + +static u8 tc1_digest_sha256[] = { + 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, +}; + +static u8 tc1_digest_sha384[] = { + 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62, + 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f, + 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6, + 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c, + 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f, + 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6 +}; + +static u8 tc1_digest_sha512[] = { + 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d, + 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0, + 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78, + 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde, + 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02, + 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4, + 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70, + 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54 +}; + +/* *INDENT-OFF* */ +UNITTEST_REGISTER_CRYPTO_TEST (rfc4231_tc1_sha224) = { + .name = "RFC4231 TC1", + .op = VNET_CRYPTO_OP_SHA224_HMAC, + .key = TEST_DATA (tc1_key), + .data = TEST_DATA (tc1_data), + .expected = TEST_DATA (tc1_digest_sha224), +}; + +UNITTEST_REGISTER_CRYPTO_TEST (rfc4231_tc1_sha256) = { + .name = "RFC4231 TC1", + .op = VNET_CRYPTO_OP_SHA256_HMAC, + .key = TEST_DATA (tc1_key), + .data = TEST_DATA (tc1_data), + .expected = TEST_DATA (tc1_digest_sha256), +}; + +UNITTEST_REGISTER_CRYPTO_TEST (rfc4231_tc1_sha384) = { + .name = "RFC4231 TC1", + .op = VNET_CRYPTO_OP_SHA384_HMAC, + .key = TEST_DATA (tc1_key), + .data = TEST_DATA (tc1_data), + .expected = TEST_DATA (tc1_digest_sha384), +}; + +UNITTEST_REGISTER_CRYPTO_TEST (rfc4231_tc1_sha512) = { + .name = "RFC4231 TC1", + .op = VNET_CRYPTO_OP_SHA512_HMAC, + .key = TEST_DATA (tc1_key), + .data = TEST_DATA (tc1_data), + .expected = TEST_DATA (tc1_digest_sha512), +}; +/* *INDENT-ON* */ + +static u8 tc7_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 +}; + +static char tc7_data[152] = + "This is a test using a larger than block-size key and a larger than " + "block-size data. The key needs to be hashed before being used by the " + "HMAC algorithm."; + +static u8 tc7_digest_sha224[] = { + 0x3a, 0x85, 0x41, 0x66, 0xac, 0x5d, 0x9f, 0x02, + 0x3f, 0x54, 0xd5, 0x17, 0xd0, 0xb3, 0x9d, 0xbd, + 0x94, 0x67, 0x70, 0xdb, 0x9c, 0x2b, 0x95, 0xc9, + 0xf6, 0xf5, 0x65, 0xd1 +}; + +static u8 tc7_digest_sha256[] = { + 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 +}; + +static u8 tc7_digest_sha384[] = { + 0x66, 0x17, 0x17, 0x8e, 0x94, 0x1f, 0x02, 0x0d, + 0x35, 0x1e, 0x2f, 0x25, 0x4e, 0x8f, 0xd3, 0x2c, + 0x60, 0x24, 0x20, 0xfe, 0xb0, 0xb8, 0xfb, 0x9a, + 0xdc, 0xce, 0xbb, 0x82, 0x46, 0x1e, 0x99, 0xc5, + 0xa6, 0x78, 0xcc, 0x31, 0xe7, 0x99, 0x17, 0x6d, + 0x38, 0x60, 0xe6, 0x11, 0x0c, 0x46, 0x52, 0x3e +}; + +static u8 tc7_digest_sha512[] = { + 0xe3, 0x7b, 0x6a, 0x77, 0x5d, 0xc8, 0x7d, 0xba, + 0xa4, 0xdf, 0xa9, 0xf9, 0x6e, 0x5e, 0x3f, 0xfd, + 0xde, 0xbd, 0x71, 0xf8, 0x86, 0x72, 0x89, 0x86, + 0x5d, 0xf5, 0xa3, 0x2d, 0x20, 0xcd, 0xc9, 0x44, + 0xb6, 0x02, 0x2c, 0xac, 0x3c, 0x49, 0x82, 0xb1, + 0x0d, 0x5e, 0xeb, 0x55, 0xc3, 0xe4, 0xde, 0x15, + 0x13, 0x46, 0x76, 0xfb, 0x6d, 0xe0, 0x44, 0x60, + 0x65, 0xc9, 0x74, 0x40, 0xfa, 0x8c, 0x6a, 0x58 +}; + +/* *INDENT-OFF* */ +UNITTEST_REGISTER_CRYPTO_TEST (rfc4231_tc7_sha224) = { + .name = "RFC4231 TC7", + .op = VNET_CRYPTO_OP_SHA224_HMAC, + .key = TEST_DATA (tc7_key), + .data = TEST_DATA (tc7_data), + .expected = TEST_DATA (tc7_digest_sha224), +}; + +UNITTEST_REGISTER_CRYPTO_TEST (rfc4231_tc7_sha256) = { + .name = "RFC4231 TC7", + .op = VNET_CRYPTO_OP_SHA256_HMAC, + .key = TEST_DATA (tc7_key), + .data = TEST_DATA (tc7_data), + .expected = TEST_DATA (tc7_digest_sha256), +}; + +UNITTEST_REGISTER_CRYPTO_TEST (rfc4231_tc7_sha384) = { + .name = "RFC4231 TC7", + .op = VNET_CRYPTO_OP_SHA384_HMAC, + .key = TEST_DATA (tc7_key), + .data = TEST_DATA (tc7_data), + .expected = TEST_DATA (tc7_digest_sha384), +}; + +UNITTEST_REGISTER_CRYPTO_TEST (rfc4231_tc7_sha512) = { + .name = "RFC4231 TC7", + .op = VNET_CRYPTO_OP_SHA512_HMAC, + .key = TEST_DATA (tc7_key), + .data = TEST_DATA (tc7_data), + .expected = TEST_DATA (tc7_digest_sha512), +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/src/plugins/unittest/crypto_test.c b/src/plugins/unittest/crypto_test.c new file mode 100644 index 00000000000..1df483efd56 --- /dev/null +++ b/src/plugins/unittest/crypto_test.c @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2019 Cisco and/or its affiliates. + * 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 +#include +#include +#include +#include +#include + +crypto_test_main_t crypto_test_main; + +static int +sort_registrations (void *a0, void *a1) +{ + unittest_crypto_test_registration_t **r0 = a0; + unittest_crypto_test_registration_t **r1 = a1; + + return (r0[0]->op > r1[0]->op); +} + +static clib_error_t * +test_crypto (vlib_main_t * vm, crypto_test_main_t * tm) +{ + unittest_crypto_test_registration_t *r = tm->test_registrations; + unittest_crypto_test_registration_t **rv = 0; + vnet_crypto_op_t *ops = 0, *op; + u8 *computed_data = 0, *s = 0; + u32 computed_data_total_len = 0, n_tests = 0; + u32 i; + + /* construct registration vector */ + while (r) + { + vec_add1 (rv, r); + computed_data_total_len += r->data.length; + n_tests += 1; + /* next */ + r = r->next; + } + + vec_sort_with_function (rv, sort_registrations); + + vec_validate_aligned (computed_data, computed_data_total_len - 1, + CLIB_CACHE_LINE_BYTES); + vec_validate_aligned (ops, n_tests - 1, CLIB_CACHE_LINE_BYTES); + computed_data_total_len = 0; + + /* *INDENT-OFF* */ + vec_foreach_index (i, rv) + { + r = rv[i]; + op = ops + i; + op->op = r->op; + op->iv = r->iv.data; + op->key = r->key.data; + op->src = r->data.data; + op->dst = computed_data + computed_data_total_len; + op->len = r->data.length; + op->key_len = r->key.length; + computed_data_total_len += r->expected.length; + /* next */ + r = r->next; + } + /* *INDENT-ON* */ + + vnet_crypto_process_ops (vm, ops, vec_len (ops)); + + /* *INDENT-OFF* */ + vec_foreach_index (i, rv) + { + int fail = 0; + r = rv[i]; + op = ops + i; + + if (memcmp (op->dst, r->expected.data, r->expected.length) != 0) + fail = 1; + + vec_reset_length (s); + s = format (s, "%s (%U)", r->name, + format_vnet_crypto_op, r->op); + + vlib_cli_output (vm, "%-60v%s", s, fail ? "FAIL" : "OK"); + if (fail & tm->verbose) + { + vlib_cli_output (vm, "Expected:\n%U\nCalculated:\n%U", + format_hexdump, r->expected, r->expected.length, + format_hexdump, op->dst, r->expected.length); + } + } + /* *INDENT-ON* */ + + vec_free (computed_data); + vec_free (ops); + vec_free (rv); + vec_free (s); + return 0; +} + +static clib_error_t * +test_crypto_command_fn (vlib_main_t * vm, + unformat_input_t * input, vlib_cli_command_t * cmd) +{ + crypto_test_main_t *tm = &crypto_test_main; + + while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) + { + if (unformat (input, "verbose %d", &tm->verbose)) + ; + else + return clib_error_return (0, "unknown input '%U'", + format_unformat_error, input); + } + + return test_crypto (vm, tm); +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (test_crypto_command, static) = +{ + .path = "test crypto", + .short_help = "test crypto", + .function = test_crypto_command_fn, +}; +/* *INDENT-ON* */ + +static clib_error_t * +crypto_test_init (vlib_main_t * vm) +{ + return (0); +} + +VLIB_INIT_FUNCTION (crypto_test_init); + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ -- cgit 1.2.3-korg