aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/plugins/crypto_openssl/CMakeLists.txt26
-rw-r--r--src/plugins/crypto_openssl/main.c213
-rw-r--r--src/plugins/unittest/CMakeLists.txt4
-rw-r--r--src/plugins/unittest/crypto/aes_cbc.c157
-rw-r--r--src/plugins/unittest/crypto/crypto.h67
-rw-r--r--src/plugins/unittest/crypto/rfc2202.c84
-rw-r--r--src/plugins/unittest/crypto/rfc4231.c197
-rw-r--r--src/plugins/unittest/crypto_test.c152
-rw-r--r--src/vnet/CMakeLists.txt14
-rw-r--r--src/vnet/crypto/cli.c101
-rw-r--r--src/vnet/crypto/crypto.c127
-rw-r--r--src/vnet/crypto/crypto.h163
-rw-r--r--src/vnet/crypto/format.c61
-rw-r--r--src/vnet/ipsec/ah.h6
-rw-r--r--src/vnet/ipsec/ah_decrypt.c6
-rw-r--r--src/vnet/ipsec/ah_encrypt.c6
-rw-r--r--src/vnet/ipsec/esp.h108
-rw-r--r--src/vnet/ipsec/esp_decrypt.c52
-rw-r--r--src/vnet/ipsec/esp_encrypt.c53
-rw-r--r--src/vnet/ipsec/ipsec.c71
-rw-r--r--src/vnet/ipsec/ipsec.h54
21 files changed, 1489 insertions, 233 deletions
diff --git a/src/plugins/crypto_openssl/CMakeLists.txt b/src/plugins/crypto_openssl/CMakeLists.txt
new file mode 100644
index 00000000000..d014144eca8
--- /dev/null
+++ b/src/plugins/crypto_openssl/CMakeLists.txt
@@ -0,0 +1,26 @@
+# Copyright (c) 2018 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.
+
+if(NOT OPENSSL_FOUND)
+ return()
+endif()
+
+include_directories(${OPENSSL_INCLUDE_DIR})
+
+add_vpp_plugin(crypto_openssl
+ SOURCES
+ main.c
+
+ LINK_LIBRARIES
+ ${OPENSSL_LIBRARIES}
+)
diff --git a/src/plugins/crypto_openssl/main.c b/src/plugins/crypto_openssl/main.c
new file mode 100644
index 00000000000..30c9c03f643
--- /dev/null
+++ b/src/plugins/crypto_openssl/main.c
@@ -0,0 +1,213 @@
+/*
+ *------------------------------------------------------------------
+ * 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 <openssl/evp.h>
+#include <openssl/hmac.h>
+#include <openssl/rand.h>
+
+#include <vlib/vlib.h>
+#include <vnet/plugin/plugin.h>
+#include <vnet/crypto/crypto.h>
+#include <vpp/app/version.h>
+
+typedef struct
+{
+ CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
+ EVP_CIPHER_CTX *evp_cipher_ctx;
+ HMAC_CTX *hmac_ctx;
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+ HMAC_CTX _hmac_ctx;
+#endif
+} openssl_per_thread_data_t;
+
+static openssl_per_thread_data_t *per_thread_data = 0;
+
+#define foreach_openssl_evp_op \
+ _(DES_CBC, EVP_des_cbc) \
+ _(3DES_CBC, EVP_des_ede3_cbc) \
+ _(AES_128_CBC, EVP_aes_128_cbc) \
+ _(AES_192_CBC, EVP_aes_192_cbc) \
+ _(AES_256_CBC, EVP_aes_256_cbc)
+
+#define foreach_openssl_hmac_op \
+ _(SHA1, EVP_sha1) \
+ _(SHA224, EVP_sha224) \
+ _(SHA256, EVP_sha256) \
+ _(SHA384, EVP_sha384) \
+ _(SHA512, EVP_sha512)
+
+static_always_inline u32
+openssl_ops_enc_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
+ const EVP_CIPHER * cipher)
+{
+ openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
+ vm->thread_index);
+ EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
+ u32 i;
+ for (i = 0; i < n_ops; i++)
+ {
+ vnet_crypto_op_t *op = ops[i];
+ int out_len;
+
+ if (op->flags & VNET_CRYPTO_OP_FLAG_INIT_IV)
+ RAND_bytes (op->iv, 16);
+
+ EVP_EncryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
+ EVP_EncryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
+ EVP_EncryptFinal_ex (ctx, op->dst + out_len, &out_len);
+ op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
+ }
+ return n_ops;
+}
+
+static_always_inline u32
+openssl_ops_dec_cbc (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
+ const EVP_CIPHER * cipher)
+{
+ openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
+ vm->thread_index);
+ EVP_CIPHER_CTX *ctx = ptd->evp_cipher_ctx;
+ u32 i;
+ for (i = 0; i < n_ops; i++)
+ {
+ vnet_crypto_op_t *op = ops[i];
+ int out_len;
+
+ EVP_DecryptInit_ex (ctx, cipher, NULL, op->key, op->iv);
+ EVP_DecryptUpdate (ctx, op->dst, &out_len, op->src, op->len);
+ EVP_DecryptFinal_ex (ctx, op->dst + out_len, &out_len);
+ op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
+ }
+ return n_ops;
+}
+
+static_always_inline u32
+openssl_ops_hmac (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops,
+ const EVP_MD * md)
+{
+ openssl_per_thread_data_t *ptd = vec_elt_at_index (per_thread_data,
+ vm->thread_index);
+ HMAC_CTX *ctx = ptd->hmac_ctx;
+ u32 i;
+ for (i = 0; i < n_ops; i++)
+ {
+ vnet_crypto_op_t *op = ops[i];
+ unsigned int out_len;
+
+ HMAC_Init_ex (ctx, op->key, op->key_len, md, NULL);
+ HMAC_Update (ctx, op->src, op->len);
+ HMAC_Final (ctx, op->dst, &out_len);
+ op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
+ }
+ return n_ops;
+}
+
+#define _(a, b) \
+static u32 \
+openssl_ops_enc_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
+{ return openssl_ops_enc_cbc (vm, ops, n_ops, b ()); } \
+\
+u32 \
+openssl_ops_dec_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
+{ return openssl_ops_dec_cbc (vm, ops, n_ops, b ()); }
+
+foreach_openssl_evp_op;
+#undef _
+
+#define _(a, b) \
+static u32 \
+openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
+{ return openssl_ops_hmac (vm, ops, n_ops, b ()); } \
+
+foreach_openssl_hmac_op;
+#undef _
+
+
+clib_error_t *
+crypto_openssl_init (vlib_main_t * vm)
+{
+ vlib_thread_main_t *tm = vlib_get_thread_main ();
+ openssl_per_thread_data_t *ptd;
+ u8 *seed_data = 0;
+ time_t t;
+ pid_t pid;
+
+ u32 eidx = vnet_crypto_register_engine (vm, "openssl", 50, "OpenSSL");
+ clib_error_t *error;
+
+ if ((error = vlib_call_init_function (vm, vnet_crypto_init)))
+ return error;
+
+#define _(a, b) \
+ vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_ENC, \
+ openssl_ops_enc_##a); \
+ vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_DEC, \
+ openssl_ops_dec_##a);
+
+ foreach_openssl_evp_op;
+#undef _
+
+#define _(a, b) \
+ vnet_crypto_register_ops_handler (vm, eidx, VNET_CRYPTO_OP_##a##_HMAC, \
+ openssl_ops_hmac_##a); \
+
+ foreach_openssl_hmac_op;
+#undef _
+
+ vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
+ CLIB_CACHE_LINE_BYTES);
+
+ vec_foreach (ptd, per_thread_data)
+ {
+ ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
+#if OPENSSL_VERSION_NUMBER >= 0x10100000L
+ ptd->hmac_ctx = HMAC_CTX_new ();
+#else
+ HMAC_CTX_init (&(ptd->_hmac_ctx));
+ ptd->hmac_ctx = &ptd->_hmac_ctx;
+#endif
+ }
+
+ t = time (NULL);
+ pid = getpid ();
+ vec_add (seed_data, &t, sizeof (t));
+ vec_add (seed_data, &pid, sizeof (pid));
+ vec_add (seed_data, seed_data, sizeof (seed_data));
+
+ RAND_seed ((const void *) seed_data, vec_len (seed_data));
+
+ vec_free (seed_data);
+
+ return 0;
+}
+
+VLIB_INIT_FUNCTION (crypto_openssl_init);
+
+/* *INDENT-OFF* */
+VLIB_PLUGIN_REGISTER () = {
+ .version = VPP_BUILD_VER,
+ .description = "OpenSSL Crypto Engine Plugin",
+};
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
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 <vppinfra/clib.h>
+#include <vnet/crypto/crypto.h>
+#include <unittest/crypto/crypto.h>
+
+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 <vppinfra/clib.h>
+#include <vnet/crypto/crypto.h>
+#include <unittest/crypto/crypto.h>
+
+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 <vppinfra/clib.h>
+#include <vnet/crypto/crypto.h>
+#include <unittest/crypto/crypto.h>
+
+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 <vlib/vlib.h>
+#include <vppinfra/time.h>
+#include <vppinfra/cache.h>
+#include <vppinfra/error.h>
+#include <vnet/crypto/crypto.h>
+#include <unittest/crypto/crypto.h>
+
+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:
+ */
diff --git a/src/vnet/CMakeLists.txt b/src/vnet/CMakeLists.txt
index e486aa97510..b6028e90742 100644
--- a/src/vnet/CMakeLists.txt
+++ b/src/vnet/CMakeLists.txt
@@ -531,6 +531,20 @@ list(APPEND VNET_SOURCES
list(APPEND VNET_API_FILES bfd/bfd.api)
##############################################################################
+# Crypto
+##############################################################################
+
+list(APPEND VNET_SOURCES
+ crypto/cli.c
+ crypto/crypto.c
+ crypto/format.c
+)
+
+list(APPEND VNET_HEADERS
+ crypto/crypto.h
+)
+
+##############################################################################
# Layer 3 protocol: IPSec
##############################################################################
list(APPEND VNET_SOURCES
diff --git a/src/vnet/crypto/cli.c b/src/vnet/crypto/cli.c
new file mode 100644
index 00000000000..d93577ed113
--- /dev/null
+++ b/src/vnet/crypto/cli.c
@@ -0,0 +1,101 @@
+/*
+ * 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 <stdbool.h>
+#include <vlib/vlib.h>
+#include <vnet/crypto/crypto.h>
+
+static clib_error_t *
+show_crypto_engines_command_fn (vlib_main_t * vm,
+ unformat_input_t * input,
+ vlib_cli_command_t * cmd)
+{
+ unformat_input_t _line_input, *line_input = &_line_input;
+ vnet_crypto_main_t *cm = &crypto_main;
+ vnet_crypto_engine_t *p;
+
+ if (unformat_user (input, unformat_line_input, line_input))
+ unformat_free (line_input);
+
+ if (vec_len (cm->engines) == 0)
+ {
+ vlib_cli_output (vm, "No crypto engines registered");
+ return 0;
+ }
+
+ vlib_cli_output (vm, "%-20s%-8s%s", "Name", "Prio", "Description");
+ /* *INDENT-OFF* */
+ vec_foreach (p, cm->engines)
+ {
+ vlib_cli_output (vm, "%-20s%-8u%s", p->name, p->priority, p->desc);
+ }
+ /* *INDENT-ON* */
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (show_crypto_engines_command, static) =
+{
+ .path = "show crypto engines",
+ .short_help = "show crypto engines",
+ .function = show_crypto_engines_command_fn,
+};
+
+static clib_error_t *
+show_crypto_handlers_command_fn (vlib_main_t * vm,
+ unformat_input_t * input, vlib_cli_command_t * cmd)
+{
+ vnet_crypto_main_t *cm = &crypto_main;
+ unformat_input_t _line_input, *line_input = &_line_input;
+ u8 *s = 0;
+
+ if (unformat_user (input, unformat_line_input, line_input))
+ unformat_free (line_input);
+
+ vlib_cli_output (vm, "%-40s%-20s%s", "Name", "Active", "Candidates");
+ for (int i = 1; i < VNET_CRYPTO_N_OP_TYPES; i++)
+ {
+ vnet_crypto_op_type_data_t *otd = cm->opt_data + i;
+ vnet_crypto_engine_t *e;
+
+ vec_reset_length (s);
+ vec_foreach (e, cm->engines)
+ {
+ if (e->ops_handlers[i] != 0)
+ s = format (s, "%U ", format_vnet_crypto_engine, e - cm->engines);
+ }
+ vlib_cli_output (vm, "%-40U%-20U%v", format_vnet_crypto_op, i,
+ format_vnet_crypto_engine, otd->active_engine_index,s);
+ }
+ vec_free (s);
+ return 0;
+}
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (show_crypto_handlers_command, static) =
+{
+ .path = "show crypto handlers",
+ .short_help = "show crypto handlers",
+ .function = show_crypto_handlers_command_fn,
+};
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/crypto/crypto.c b/src/vnet/crypto/crypto.c
new file mode 100644
index 00000000000..a6f45be053f
--- /dev/null
+++ b/src/vnet/crypto/crypto.c
@@ -0,0 +1,127 @@
+/*
+ * Copyright (c) 2018 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 <stdbool.h>
+#include <vlib/vlib.h>
+#include <vnet/crypto/crypto.h>
+
+vnet_crypto_main_t crypto_main;
+
+u32
+vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[], u32 n_ops)
+{
+ vnet_crypto_main_t *cm = &crypto_main;
+ u32 rv = 0, i;
+
+ for (i = 0; i < n_ops; i++)
+ {
+ vnet_crypto_op_type_t opt = ops[i].op;
+ vnet_crypto_op_t *opp = &ops[i];
+
+ if (cm->ops_handlers[opt])
+ rv += (cm->ops_handlers[opt]) (vm, &opp, 1);
+ else
+ ops[i].status = VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER;
+ }
+
+ return rv;
+}
+
+u32
+vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
+ char *desc)
+{
+ vnet_crypto_main_t *cm = &crypto_main;
+ vnet_crypto_engine_t *p;
+
+ vec_add2 (cm->engines, p, 1);
+ p->name = name;
+ p->desc = desc;
+ p->priority = prio;
+
+ return p - cm->engines;
+}
+
+vlib_error_t *
+vnet_crypto_register_ops_handler (vlib_main_t * vm, u32 engine_index,
+ vnet_crypto_op_type_t opt,
+ vnet_crypto_ops_handler_t * fn)
+{
+ vnet_crypto_main_t *cm = &crypto_main;
+ vnet_crypto_engine_t *ae, *e = vec_elt_at_index (cm->engines, engine_index);
+ vnet_crypto_op_type_data_t *otd = cm->opt_data + opt;
+ vec_validate_aligned (cm->ops_handlers, VNET_CRYPTO_N_OP_TYPES - 1,
+ CLIB_CACHE_LINE_BYTES);
+ e->ops_handlers[opt] = fn;
+
+ if (otd->active_engine_index == ~0)
+ {
+ otd->active_engine_index = engine_index;
+ cm->ops_handlers[opt] = fn;
+ return 0;
+ }
+ ae = vec_elt_at_index (cm->engines, otd->active_engine_index);
+ if (ae->priority < e->priority)
+ {
+ otd->active_engine_index = engine_index;
+ cm->ops_handlers[opt] = fn;
+ }
+
+ return 0;
+}
+
+clib_error_t *
+vnet_crypto_init (vlib_main_t * vm)
+{
+ vnet_crypto_main_t *cm = &crypto_main;
+ vlib_thread_main_t *tm = vlib_get_thread_main ();
+ const char *enc = "encrypt";
+ const char *dec = "decrypt";
+ const char *hmac = "hmac";
+
+ vec_validate_aligned (cm->threads, tm->n_vlib_mains, CLIB_CACHE_LINE_BYTES);
+ vec_validate (cm->algs, VNET_CRYPTO_N_ALGS);
+
+#define _(n, s) \
+ cm->algs[VNET_CRYPTO_ALG_##n].name = s; \
+ cm->opt_data[VNET_CRYPTO_OP_##n##_ENC].alg = VNET_CRYPTO_ALG_##n; \
+ cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].alg = VNET_CRYPTO_ALG_##n; \
+ cm->opt_data[VNET_CRYPTO_OP_##n##_ENC].desc = enc; \
+ cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].desc = dec; \
+ cm->opt_data[VNET_CRYPTO_OP_##n##_ENC].active_engine_index = ~0; \
+ cm->opt_data[VNET_CRYPTO_OP_##n##_DEC].active_engine_index = ~0;
+ foreach_crypto_alg;
+#undef _
+
+#define _(n, s) \
+ cm->algs[VNET_CRYPTO_ALG_##n].name = s; \
+ cm->opt_data[VNET_CRYPTO_OP_##n##_HMAC].alg = VNET_CRYPTO_ALG_##n; \
+ cm->opt_data[VNET_CRYPTO_OP_##n##_HMAC].desc = hmac; \
+ cm->opt_data[VNET_CRYPTO_OP_##n##_HMAC].active_engine_index = ~0;
+ foreach_hmac_alg;
+#undef _
+
+ return 0;
+}
+
+VLIB_INIT_FUNCTION (vnet_crypto_init);
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/crypto/crypto.h b/src/vnet/crypto/crypto.h
new file mode 100644
index 00000000000..9f4c85b36f5
--- /dev/null
+++ b/src/vnet/crypto/crypto.h
@@ -0,0 +1,163 @@
+/*
+ * 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_vnet_crypto_crypto_h
+#define included_vnet_crypto_crypto_h
+
+#define VNET_CRYPTO_RING_SIZE 512
+
+#include <vlib/vlib.h>
+
+#define foreach_crypto_alg \
+ _(DES_CBC, "des-cbc") \
+ _(3DES_CBC, "3des-cbc") \
+ _(AES_128_CBC, "aes-128-cbc") \
+ _(AES_192_CBC, "aes-192-cbc") \
+ _(AES_256_CBC, "aes-256-cbc")
+
+#define foreach_hmac_alg \
+ _(SHA1, "sha-1") \
+ _(SHA224, "sha-224") \
+ _(SHA256, "sha-256") \
+ _(SHA384, "sha-384") \
+ _(SHA512, "sha-512")
+
+/* *INDENT-OFF* */
+typedef enum
+{
+#define _(n, s) VNET_CRYPTO_ALG_##n,
+ foreach_crypto_alg
+#undef _
+#define _(n, s) VNET_CRYPTO_ALG_##n,
+ foreach_hmac_alg
+#undef _
+ VNET_CRYPTO_N_ALGS,
+} vnet_crypto_alg_t;
+
+typedef enum
+{
+ VNET_CRYPTO_OP_NONE = 0,
+#define _(n, s) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC,
+ foreach_crypto_alg
+#undef _
+#define _(n, s) VNET_CRYPTO_OP_##n##_HMAC,
+ foreach_hmac_alg
+#undef _
+ VNET_CRYPTO_N_OP_TYPES,
+} vnet_crypto_op_type_t;
+/* *INDENT-ON* */
+
+typedef struct
+{
+ char *name;
+} vnet_crypto_alg_data_t;
+
+typedef enum
+{
+ VNET_CRYPTO_OP_STATUS_PENDING,
+ VNET_CRYPTO_OP_STATUS_COMPLETED,
+ VNET_CRYPTO_OP_STATUS_FAIL_NO_HANDLER,
+} vnet_crypto_op_status_t;
+
+typedef struct
+{
+ CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
+ vnet_crypto_op_type_t op:16;
+ vnet_crypto_op_status_t status:8;
+ u8 key_len;
+ u16 flags;
+#define VNET_CRYPTO_OP_FLAG_INIT_IV 1
+ u32 len;
+ u8 *key;
+ u8 *iv;
+ u8 *src;
+ u8 *dst;
+} vnet_crypto_op_t;
+
+typedef struct
+{
+ vnet_crypto_alg_t alg;
+ const char *desc;
+ u32 active_engine_index;
+} vnet_crypto_op_type_data_t;
+
+typedef struct
+{
+ CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
+ u32 head;
+ u32 tail;
+ u32 size;
+ vnet_crypto_alg_t alg:8;
+ vnet_crypto_op_type_t op:8;
+ vnet_crypto_op_t *jobs[0];
+} vnet_crypto_queue_t;
+
+typedef struct
+{
+ CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
+ clib_bitmap_t *act_queues;
+ vnet_crypto_queue_t *queues[VNET_CRYPTO_N_OP_TYPES];
+} vnet_crypto_thread_t;
+
+typedef u32 (vnet_crypto_ops_handler_t) (vlib_main_t * vm,
+ vnet_crypto_op_t * ops[], u32 n_ops);
+
+u32 vnet_crypto_register_engine (vlib_main_t * vm, char *name, int prio,
+ char *desc);
+
+vlib_error_t *vnet_crypto_register_ops_handler (vlib_main_t * vm,
+ u32 provider_index,
+ vnet_crypto_op_type_t opt,
+ vnet_crypto_ops_handler_t *
+ f);
+
+typedef struct
+{
+ char *name;
+ char *desc;
+ int priority;
+ vnet_crypto_ops_handler_t *ops_handlers[VNET_CRYPTO_N_OP_TYPES];
+} vnet_crypto_engine_t;
+
+typedef struct
+{
+ vnet_crypto_alg_data_t *algs;
+ vnet_crypto_thread_t *threads;
+ vnet_crypto_ops_handler_t **ops_handlers;
+ vnet_crypto_op_type_data_t opt_data[VNET_CRYPTO_N_OP_TYPES];
+ vnet_crypto_engine_t *engines;
+} vnet_crypto_main_t;
+
+extern vnet_crypto_main_t crypto_main;
+
+u32 vnet_crypto_submit_ops (vlib_main_t * vm, vnet_crypto_op_t ** jobs,
+ u32 n_jobs);
+
+u32 vnet_crypto_process_ops (vlib_main_t * vm, vnet_crypto_op_t ops[],
+ u32 n_ops);
+
+format_function_t format_vnet_crypto_alg;
+format_function_t format_vnet_crypto_engine;
+format_function_t format_vnet_crypto_op;
+
+#endif /* included_vnet_crypto_crypto_h */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/crypto/format.c b/src/vnet/crypto/format.c
new file mode 100644
index 00000000000..88c7c0fe7f1
--- /dev/null
+++ b/src/vnet/crypto/format.c
@@ -0,0 +1,61 @@
+/*
+ * 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 <stdbool.h>
+#include <vlib/vlib.h>
+#include <vnet/crypto/crypto.h>
+
+u8 *
+format_vnet_crypto_alg (u8 * s, va_list * args)
+{
+ vnet_crypto_alg_t alg = va_arg (*args, vnet_crypto_alg_t);
+ vnet_crypto_main_t *cm = &crypto_main;
+ vnet_crypto_alg_data_t *d = vec_elt_at_index (cm->algs, alg);
+ return format (s, "%s", d->name);
+}
+
+u8 *
+format_vnet_crypto_op (u8 * s, va_list * args)
+{
+ vnet_crypto_main_t *cm = &crypto_main;
+ vnet_crypto_op_type_t op = va_arg (*args, vnet_crypto_op_type_t);
+ vnet_crypto_op_type_data_t *otd = cm->opt_data + op;
+
+ return format (s, "%s-%U", otd->desc, format_vnet_crypto_alg, otd->alg);
+}
+
+u8 *
+format_vnet_crypto_engine (u8 * s, va_list * args)
+{
+ vnet_crypto_main_t *cm = &crypto_main;
+ u32 crypto_engine_index = va_arg (*args, u32);
+ vnet_crypto_engine_t *e;
+
+ if (crypto_engine_index == ~0)
+ return s;
+
+ e = vec_elt_at_index (cm->engines, crypto_engine_index);
+
+ return format (s, "%s", e->name);
+}
+
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/vnet/ipsec/ah.h b/src/vnet/ipsec/ah.h
index f74ad9b01c1..d0b4c21a4bc 100644
--- a/src/vnet/ipsec/ah.h
+++ b/src/vnet/ipsec/ah.h
@@ -15,15 +15,9 @@
#ifndef __AH_H__
#define __AH_H__
-
#include <vnet/ip/ip.h>
#include <vnet/ipsec/ipsec.h>
-#include <openssl/hmac.h>
-#include <openssl/rand.h>
-#include <openssl/evp.h>
-
-
typedef struct
{
unsigned char nexthdr;
diff --git a/src/vnet/ipsec/ah_decrypt.c b/src/vnet/ipsec/ah_decrypt.c
index 0fc4f48571d..2488fa918f6 100644
--- a/src/vnet/ipsec/ah_decrypt.c
+++ b/src/vnet/ipsec/ah_decrypt.c
@@ -84,7 +84,6 @@ ah_decrypt_inline (vlib_main_t * vm,
{
u32 n_left_from, *from, next_index, *to_next, thread_index;
ipsec_main_t *im = &ipsec_main;
- ipsec_proto_main_t *em = &ipsec_proto_main;
from = vlib_frame_vector_args (from_frame);
n_left_from = from_frame->n_vectors;
int icv_size;
@@ -173,8 +172,7 @@ ah_decrypt_inline (vlib_main_t * vm,
(&ipsec_sa_counters, thread_index, sa_index0,
1, i_b0->current_length);
- icv_size =
- em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
+ icv_size = im->integ_algs[sa0->integ_alg].trunc_size;
if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
{
u8 sig[64];
@@ -205,7 +203,7 @@ ah_decrypt_inline (vlib_main_t * vm,
icv_padding_len =
ah_calc_icv_padding_len (icv_size, 0 /* is_ipv6 */ );
}
- hmac_calc (sa0->integ_alg, sa0->integ_key.data,
+ hmac_calc (vm, sa0->integ_alg, sa0->integ_key.data,
sa0->integ_key.len, (u8 *) ih4, i_b0->current_length,
sig, sa0->use_esn, sa0->seq_hi);
diff --git a/src/vnet/ipsec/ah_encrypt.c b/src/vnet/ipsec/ah_encrypt.c
index 2e561deb8b6..ce930bd5529 100644
--- a/src/vnet/ipsec/ah_encrypt.c
+++ b/src/vnet/ipsec/ah_encrypt.c
@@ -89,7 +89,6 @@ ah_encrypt_inline (vlib_main_t * vm,
from = vlib_frame_vector_args (from_frame);
n_left_from = from_frame->n_vectors;
ipsec_main_t *im = &ipsec_main;
- ipsec_proto_main_t *em = &ipsec_proto_main;
next_index = node->cached_next_index;
thread_index = vm->thread_index;
@@ -153,8 +152,7 @@ ah_encrypt_inline (vlib_main_t * vm,
adv = -sizeof (ah_header_t);
}
- icv_size =
- em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
+ icv_size = im->integ_algs[sa0->integ_alg].trunc_size;
const u8 padding_len = ah_calc_icv_padding_len (icv_size, is_ip6);
adv -= padding_len;
/* transport mode save the eth header before it is overwritten */
@@ -267,7 +265,7 @@ ah_encrypt_inline (vlib_main_t * vm,
sizeof (ah_header_t);
clib_memset (digest, 0, icv_size);
- unsigned size = hmac_calc (sa0->integ_alg, sa0->integ_key.data,
+ unsigned size = hmac_calc (vm, sa0->integ_alg, sa0->integ_key.data,
sa0->integ_key.len,
vlib_buffer_get_current (i_b0),
i_b0->current_length, sig, sa0->use_esn,
diff --git a/src/vnet/ipsec/esp.h b/src/vnet/ipsec/esp.h
index 00472654554..17300383e16 100644
--- a/src/vnet/ipsec/esp.h
+++ b/src/vnet/ipsec/esp.h
@@ -16,6 +16,7 @@
#define __ESP_H__
#include <vnet/ip/ip.h>
+#include <vnet/crypto/crypto.h>
#include <vnet/ipsec/ipsec.h>
typedef struct
@@ -202,106 +203,25 @@ esp_seq_advance (ipsec_sa_t * sa)
return 0;
}
-always_inline void
-ipsec_proto_init ()
-{
- ipsec_proto_main_t *em = &ipsec_proto_main;
- vlib_thread_main_t *tm = vlib_get_thread_main ();
-
- clib_memset (em, 0, sizeof (em[0]));
-
- vec_validate (em->ipsec_proto_main_crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].type =
- EVP_aes_128_cbc ();
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].type =
- EVP_aes_192_cbc ();
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].type =
- EVP_aes_256_cbc ();
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].iv_size = 16;
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].iv_size = 16;
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].iv_size = 16;
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_128].block_size =
- 16;
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_192].block_size =
- 16;
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_AES_CBC_256].block_size =
- 16;
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].type =
- EVP_des_cbc ();
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].type =
- EVP_des_ede3_cbc ();
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].block_size = 8;
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].block_size = 8;
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_DES_CBC].iv_size = 8;
- em->ipsec_proto_main_crypto_algs[IPSEC_CRYPTO_ALG_3DES_CBC].iv_size = 8;
-
- vec_validate (em->ipsec_proto_main_integ_algs, IPSEC_INTEG_N_ALG - 1);
- ipsec_proto_main_integ_alg_t *i;
-
- i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA1_96];
- i->md = EVP_sha1 ();
- i->trunc_size = 12;
-
- i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
- i->md = EVP_sha256 ();
- i->trunc_size = 12;
-
- i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
- i->md = EVP_sha256 ();
- i->trunc_size = 16;
-
- i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
- i->md = EVP_sha384 ();
- i->trunc_size = 24;
-
- i = &em->ipsec_proto_main_integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
- i->md = EVP_sha512 ();
- i->trunc_size = 32;
-
- vec_validate_aligned (em->per_thread_data, tm->n_vlib_mains - 1,
- CLIB_CACHE_LINE_BYTES);
- int thread_id;
-
- for (thread_id = 0; thread_id < tm->n_vlib_mains; thread_id++)
- {
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
- em->per_thread_data[thread_id].encrypt_ctx = EVP_CIPHER_CTX_new ();
- em->per_thread_data[thread_id].decrypt_ctx = EVP_CIPHER_CTX_new ();
- em->per_thread_data[thread_id].hmac_ctx = HMAC_CTX_new ();
-#else
- EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].encrypt_ctx));
- EVP_CIPHER_CTX_init (&(em->per_thread_data[thread_id].decrypt_ctx));
- HMAC_CTX_init (&(em->per_thread_data[thread_id].hmac_ctx));
-#endif
- }
-}
always_inline unsigned int
-hmac_calc (ipsec_integ_alg_t alg,
- u8 * key,
- int key_len,
+hmac_calc (vlib_main_t * vm, ipsec_integ_alg_t alg, u8 * key, int key_len,
u8 * data, int data_len, u8 * signature, u8 use_esn, u32 seq_hi)
{
- ipsec_proto_main_t *em = &ipsec_proto_main;
- u32 thread_index = vlib_get_thread_index ();
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
- HMAC_CTX *ctx = em->per_thread_data[thread_index].hmac_ctx;
-#else
- HMAC_CTX *ctx = &(em->per_thread_data[thread_index].hmac_ctx);
-#endif
- const EVP_MD *md = NULL;
- unsigned int len;
-
+ ipsec_main_t *im = &ipsec_main;
+ vnet_crypto_op_t _op, *op = &_op;
ASSERT (alg < IPSEC_INTEG_N_ALG);
- if (PREDICT_FALSE (em->ipsec_proto_main_integ_algs[alg].md == 0))
+ if (PREDICT_FALSE (im->integ_algs[alg].op_type == 0))
return 0;
- if (PREDICT_FALSE (alg != em->per_thread_data[thread_index].last_integ_alg))
- {
- md = em->ipsec_proto_main_integ_algs[alg].md;
- em->per_thread_data[thread_index].last_integ_alg = alg;
- }
+ op->op = im->integ_algs[alg].op_type;
+ op->key = key;
+ op->key_len = key_len;
+ op->src = data;
+ op->len = data_len;
+ op->dst = signature;
+#if 0
HMAC_Init_ex (ctx, key, key_len, md, NULL);
@@ -311,7 +231,9 @@ hmac_calc (ipsec_integ_alg_t alg,
HMAC_Update (ctx, (u8 *) & seq_hi, sizeof (seq_hi));
HMAC_Final (ctx, signature, &len);
- return em->ipsec_proto_main_integ_algs[alg].trunc_size;
+#endif
+ vnet_crypto_process_ops (vm, op, 1);
+ return im->integ_algs[alg].trunc_size;
}
#endif /* __ESP_H__ */
diff --git a/src/vnet/ipsec/esp_decrypt.c b/src/vnet/ipsec/esp_decrypt.c
index 7e5114ce2cf..1ee7ce8fd0a 100644
--- a/src/vnet/ipsec/esp_decrypt.c
+++ b/src/vnet/ipsec/esp_decrypt.c
@@ -82,35 +82,28 @@ format_esp_decrypt_trace (u8 * s, va_list * args)
}
always_inline void
-esp_decrypt_cbc (ipsec_crypto_alg_t alg,
+esp_decrypt_cbc (vlib_main_t * vm, ipsec_crypto_alg_t alg,
u8 * in, u8 * out, size_t in_len, u8 * key, u8 * iv)
{
- ipsec_proto_main_t *em = &ipsec_proto_main;
- u32 thread_index = vlib_get_thread_index ();
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
- EVP_CIPHER_CTX *ctx = em->per_thread_data[thread_index].decrypt_ctx;
-#else
- EVP_CIPHER_CTX *ctx = &(em->per_thread_data[thread_index].decrypt_ctx);
-#endif
- const EVP_CIPHER *cipher = NULL;
- int out_len;
+ ipsec_main_t *im = &ipsec_main;
+ ipsec_main_crypto_alg_t *a;
+ vnet_crypto_op_t _op, *op = &_op;
ASSERT (alg < IPSEC_CRYPTO_N_ALG);
- if (PREDICT_FALSE (em->ipsec_proto_main_crypto_algs[alg].type == 0))
- return;
+ a = &im->crypto_algs[alg];
- if (PREDICT_FALSE
- (alg != em->per_thread_data[thread_index].last_decrypt_alg))
- {
- cipher = em->ipsec_proto_main_crypto_algs[alg].type;
- em->per_thread_data[thread_index].last_decrypt_alg = alg;
- }
+ if (PREDICT_FALSE (a->dec_op_type == VNET_CRYPTO_OP_NONE))
+ return;
- EVP_DecryptInit_ex (ctx, cipher, NULL, key, iv);
+ op->op = a->dec_op_type;
+ op->iv = iv;
+ op->src = in;
+ op->dst = out;
+ op->len = in_len;
+ op->key = key;
- EVP_DecryptUpdate (ctx, out, &out_len, in, in_len);
- EVP_DecryptFinal_ex (ctx, out + out_len, &out_len);
+ vnet_crypto_process_ops (vm, op, 1);
}
always_inline uword
@@ -119,7 +112,6 @@ esp_decrypt_inline (vlib_main_t * vm,
int is_ip6)
{
ipsec_main_t *im = &ipsec_main;
- ipsec_proto_main_t *em = &ipsec_proto_main;
u32 *from = vlib_frame_vector_args (from_frame);
u32 n_left_from = from_frame->n_vectors;
u32 new_bufs[VLIB_FRAME_SIZE];
@@ -189,15 +181,13 @@ esp_decrypt_inline (vlib_main_t * vm,
if (PREDICT_TRUE (sa0->integ_alg != IPSEC_INTEG_ALG_NONE))
{
u8 sig[64];
- int icv_size =
- em->ipsec_proto_main_integ_algs[sa0->integ_alg].trunc_size;
+ int icv_size = im->integ_algs[sa0->integ_alg].trunc_size;
clib_memset (sig, 0, sizeof (sig));
- u8 *icv =
- vlib_buffer_get_current (ib[0]) + ib[0]->current_length -
+ u8 *icv = vlib_buffer_get_current (ib[0]) + ib[0]->current_length -
icv_size;
ib[0]->current_length -= icv_size;
- hmac_calc (sa0->integ_alg, sa0->integ_key.data,
+ hmac_calc (vm, sa0->integ_alg, sa0->integ_key.data,
sa0->integ_key.len, (u8 *) esp0,
ib[0]->current_length, sig, sa0->use_esn, sa0->seq_hi);
@@ -227,10 +217,8 @@ esp_decrypt_inline (vlib_main_t * vm,
(sa0->crypto_alg >= IPSEC_CRYPTO_ALG_DES_CBC &&
sa0->crypto_alg <= IPSEC_CRYPTO_ALG_3DES_CBC))
{
- const int BLOCK_SIZE =
- em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].block_size;;
- const int IV_SIZE =
- em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].iv_size;
+ const int BLOCK_SIZE = im->crypto_algs[sa0->crypto_alg].block_size;
+ const int IV_SIZE = im->crypto_algs[sa0->crypto_alg].iv_size;
esp_footer_t *f0;
u8 ip_hdr_size = 0;
@@ -263,7 +251,7 @@ esp_decrypt_inline (vlib_main_t * vm,
}
}
- esp_decrypt_cbc (sa0->crypto_alg,
+ esp_decrypt_cbc (vm, sa0->crypto_alg,
esp0->data + IV_SIZE,
(u8 *) vlib_buffer_get_current (ob[0]) +
ip_hdr_size, BLOCK_SIZE * blocks,
diff --git a/src/vnet/ipsec/esp_encrypt.c b/src/vnet/ipsec/esp_encrypt.c
index 080a3458926..37c2c953c87 100644
--- a/src/vnet/ipsec/esp_encrypt.c
+++ b/src/vnet/ipsec/esp_encrypt.c
@@ -20,6 +20,8 @@
#include <vnet/ip/ip.h>
#include <vnet/udp/udp.h>
+#include <vnet/crypto/crypto.h>
+
#include <vnet/ipsec/ipsec.h>
#include <vnet/ipsec/esp.h>
@@ -88,33 +90,26 @@ always_inline void
esp_encrypt_cbc (vlib_main_t * vm, ipsec_crypto_alg_t alg,
u8 * in, u8 * out, size_t in_len, u8 * key, u8 * iv)
{
- ipsec_proto_main_t *em = &ipsec_proto_main;
- u32 thread_index = vm->thread_index;
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
- EVP_CIPHER_CTX *ctx = em->per_thread_data[thread_index].encrypt_ctx;
-#else
- EVP_CIPHER_CTX *ctx = &(em->per_thread_data[thread_index].encrypt_ctx);
-#endif
- const EVP_CIPHER *cipher = NULL;
- int out_len;
+ ipsec_main_t *im = &ipsec_main;
+ ipsec_main_crypto_alg_t *a;
+ vnet_crypto_op_t _op, *op = &_op;
ASSERT (alg < IPSEC_CRYPTO_N_ALG);
- if (PREDICT_FALSE
- (em->ipsec_proto_main_crypto_algs[alg].type == IPSEC_CRYPTO_ALG_NONE))
- return;
+ a = &im->crypto_algs[alg];
- if (PREDICT_FALSE
- (alg != em->per_thread_data[thread_index].last_encrypt_alg))
- {
- cipher = em->ipsec_proto_main_crypto_algs[alg].type;
- em->per_thread_data[thread_index].last_encrypt_alg = alg;
- }
+ if (PREDICT_FALSE (a->enc_op_type == VNET_CRYPTO_OP_NONE))
+ return;
- EVP_EncryptInit_ex (ctx, cipher, NULL, key, iv);
+ op->op = a->enc_op_type;
+ op->flags = VNET_CRYPTO_OP_FLAG_INIT_IV;
+ op->iv = iv;
+ op->src = in;
+ op->dst = out;
+ op->len = in_len;
+ op->key = key;
- EVP_EncryptUpdate (ctx, out, &out_len, in, in_len);
- EVP_EncryptFinal_ex (ctx, out + out_len, &out_len);
+ vnet_crypto_process_ops (vm, op, 1);
}
always_inline uword
@@ -125,7 +120,6 @@ esp_encrypt_inline (vlib_main_t * vm,
u32 *from = vlib_frame_vector_args (from_frame);
u32 n_left_from = from_frame->n_vectors;
ipsec_main_t *im = &ipsec_main;
- ipsec_proto_main_t *em = &ipsec_proto_main;
u32 new_bufs[VLIB_FRAME_SIZE];
vlib_buffer_t *i_bufs[VLIB_FRAME_SIZE], **ib = i_bufs;
vlib_buffer_t *o_bufs[VLIB_FRAME_SIZE], **ob = o_bufs;
@@ -288,10 +282,8 @@ esp_encrypt_inline (vlib_main_t * vm,
if (PREDICT_TRUE (sa0->crypto_alg != IPSEC_CRYPTO_ALG_NONE))
{
- const int BLOCK_SIZE =
- em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].block_size;
- const int IV_SIZE =
- em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].iv_size;
+ const int BLOCK_SIZE = im->crypto_algs[sa0->crypto_alg].block_size;
+ const int IV_SIZE = im->crypto_algs[sa0->crypto_alg].iv_size;
int blocks = 1 + (ib[0]->current_length + 1) / BLOCK_SIZE;
/* pad packet in input buffer */
@@ -314,13 +306,12 @@ esp_encrypt_inline (vlib_main_t * vm,
vnet_buffer (ob[0])->sw_if_index[VLIB_RX] =
vnet_buffer (ib[0])->sw_if_index[VLIB_RX];
- u8 iv[em->ipsec_proto_main_crypto_algs[sa0->crypto_alg].iv_size];
- RAND_bytes (iv, sizeof (iv));
+ u8 *iv = vlib_buffer_get_current (ob[0]) + ip_udp_hdr_size +
+ sizeof (esp_header_t);
clib_memcpy_fast ((u8 *) vlib_buffer_get_current (ob[0]) +
ip_udp_hdr_size + sizeof (esp_header_t), iv,
- em->ipsec_proto_main_crypto_algs[sa0->
- crypto_alg].iv_size);
+ im->crypto_algs[sa0->crypto_alg].iv_size);
esp_encrypt_cbc (vm, sa0->crypto_alg,
(u8 *) vlib_buffer_get_current (ib[0]),
@@ -331,7 +322,7 @@ esp_encrypt_inline (vlib_main_t * vm,
}
ob[0]->current_length +=
- hmac_calc (sa0->integ_alg, sa0->integ_key.data,
+ hmac_calc (vm, sa0->integ_alg, sa0->integ_key.data,
sa0->integ_key.len, (u8 *) o_esp0,
ob[0]->current_length - ip_udp_hdr_size,
vlib_buffer_get_current (ob[0]) + ob[0]->current_length,
diff --git a/src/vnet/ipsec/ipsec.c b/src/vnet/ipsec/ipsec.c
index ce93f32b429..e9d13a4e717 100644
--- a/src/vnet/ipsec/ipsec.c
+++ b/src/vnet/ipsec/ipsec.c
@@ -26,24 +26,6 @@
#include <vnet/ipsec/ah.h>
ipsec_main_t ipsec_main;
-ipsec_proto_main_t ipsec_proto_main;
-
-static void
-ipsec_rand_seed (void)
-{
- struct
- {
- time_t time;
- pid_t pid;
- void *p;
- } seed_data;
-
- seed_data.time = time (NULL);
- seed_data.pid = getpid ();
- seed_data.p = (void *) &seed_data;
-
- RAND_seed ((const void *) &seed_data, sizeof (seed_data));
-}
static clib_error_t *
ipsec_check_ah_support (ipsec_sa_t * sa)
@@ -240,8 +222,7 @@ ipsec_init (vlib_main_t * vm)
{
clib_error_t *error;
ipsec_main_t *im = &ipsec_main;
-
- ipsec_rand_seed ();
+ ipsec_main_crypto_alg_t *a;
clib_memset (im, 0, sizeof (im[0]));
@@ -287,7 +268,55 @@ ipsec_init (vlib_main_t * vm)
if ((error = vlib_call_init_function (vm, ipsec_tunnel_if_init)))
return error;
- ipsec_proto_init ();
+ vec_validate (im->crypto_algs, IPSEC_CRYPTO_N_ALG - 1);
+
+ a = im->crypto_algs + IPSEC_CRYPTO_ALG_DES_CBC;
+ a->enc_op_type = VNET_CRYPTO_OP_DES_CBC_ENC;
+ a->dec_op_type = VNET_CRYPTO_OP_DES_CBC_DEC;
+ a->iv_size = a->block_size = 8;
+
+ a = im->crypto_algs + IPSEC_CRYPTO_ALG_3DES_CBC;
+ a->enc_op_type = VNET_CRYPTO_OP_3DES_CBC_ENC;
+ a->dec_op_type = VNET_CRYPTO_OP_3DES_CBC_DEC;
+ a->iv_size = a->block_size = 8;
+
+ a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_128;
+ a->enc_op_type = VNET_CRYPTO_OP_AES_128_CBC_ENC;
+ a->dec_op_type = VNET_CRYPTO_OP_AES_128_CBC_DEC;
+ a->iv_size = a->block_size = 16;
+
+ a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_192;
+ a->enc_op_type = VNET_CRYPTO_OP_AES_192_CBC_ENC;
+ a->dec_op_type = VNET_CRYPTO_OP_AES_192_CBC_DEC;
+ a->iv_size = a->block_size = 16;
+
+ a = im->crypto_algs + IPSEC_CRYPTO_ALG_AES_CBC_256;
+ a->enc_op_type = VNET_CRYPTO_OP_AES_256_CBC_ENC;
+ a->dec_op_type = VNET_CRYPTO_OP_AES_256_CBC_DEC;
+ a->iv_size = a->block_size = 16;
+
+ vec_validate (im->integ_algs, IPSEC_INTEG_N_ALG - 1);
+ ipsec_main_integ_alg_t *i;
+
+ i = &im->integ_algs[IPSEC_INTEG_ALG_SHA1_96];
+ i->op_type = VNET_CRYPTO_OP_SHA1_HMAC;
+ i->trunc_size = 12;
+
+ i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_96];
+ i->op_type = VNET_CRYPTO_OP_SHA1_HMAC;
+ i->trunc_size = 12;
+
+ i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_256_128];
+ i->op_type = VNET_CRYPTO_OP_SHA256_HMAC;
+ i->trunc_size = 16;
+
+ i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_384_192];
+ i->op_type = VNET_CRYPTO_OP_SHA384_HMAC;
+ i->trunc_size = 24;
+
+ i = &im->integ_algs[IPSEC_INTEG_ALG_SHA_512_256];
+ i->op_type = VNET_CRYPTO_OP_SHA512_HMAC;
+ i->trunc_size = 32;
return 0;
}
diff --git a/src/vnet/ipsec/ipsec.h b/src/vnet/ipsec/ipsec.h
index c877139ba12..e38a4a8771e 100644
--- a/src/vnet/ipsec/ipsec.h
+++ b/src/vnet/ipsec/ipsec.h
@@ -16,12 +16,9 @@
#define __IPSEC_H__
#include <vnet/ip/ip.h>
+#include <vnet/crypto/crypto.h>
#include <vnet/feature/feature.h>
-#include <openssl/hmac.h>
-#include <openssl/rand.h>
-#include <openssl/evp.h>
-
#include <vppinfra/types.h>
#include <vppinfra/cache.h>
@@ -69,50 +66,17 @@ typedef struct
typedef struct
{
- const EVP_CIPHER *type;
+ vnet_crypto_op_type_t enc_op_type;
+ vnet_crypto_op_type_t dec_op_type;
u8 iv_size;
u8 block_size;
-} ipsec_proto_main_crypto_alg_t;
+} ipsec_main_crypto_alg_t;
typedef struct
{
- const EVP_MD *md;
+ vnet_crypto_op_type_t op_type;
u8 trunc_size;
-} ipsec_proto_main_integ_alg_t;
-
-typedef struct
-{
- CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
- EVP_CIPHER_CTX *encrypt_ctx;
-#else
- EVP_CIPHER_CTX encrypt_ctx;
-#endif
- CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
- EVP_CIPHER_CTX *decrypt_ctx;
-#else
- EVP_CIPHER_CTX decrypt_ctx;
-#endif
- CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
- HMAC_CTX *hmac_ctx;
-#else
- HMAC_CTX hmac_ctx;
-#endif
- ipsec_crypto_alg_t last_encrypt_alg;
- ipsec_crypto_alg_t last_decrypt_alg;
- ipsec_integ_alg_t last_integ_alg;
-} ipsec_proto_main_per_thread_data_t;
-
-typedef struct
-{
- ipsec_proto_main_crypto_alg_t *ipsec_proto_main_crypto_algs;
- ipsec_proto_main_integ_alg_t *ipsec_proto_main_integ_algs;
- ipsec_proto_main_per_thread_data_t *per_thread_data;
-} ipsec_proto_main_t;
-
-extern ipsec_proto_main_t ipsec_proto_main;
+} ipsec_main_integ_alg_t;
typedef struct
{
@@ -171,6 +135,12 @@ typedef struct
u32 ah_default_backend;
/* index of default esp backend */
u32 esp_default_backend;
+
+ /* crypto alg data */
+ ipsec_main_crypto_alg_t *crypto_algs;
+
+ /* crypto integ data */
+ ipsec_main_integ_alg_t *integ_algs;
} ipsec_main_t;
extern ipsec_main_t ipsec_main;