summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/plugins/crypto_openssl/main.c68
-rw-r--r--src/plugins/unittest/CMakeLists.txt1
-rw-r--r--src/plugins/unittest/crypto/sha.c92
-rw-r--r--src/plugins/unittest/crypto_test.c13
-rw-r--r--src/vnet/crypto/crypto.c27
-rw-r--r--src/vnet/crypto/crypto.h40
6 files changed, 226 insertions, 15 deletions
diff --git a/src/plugins/crypto_openssl/main.c b/src/plugins/crypto_openssl/main.c
index e93f476f30a..48846b14483 100644
--- a/src/plugins/crypto_openssl/main.c
+++ b/src/plugins/crypto_openssl/main.c
@@ -18,6 +18,7 @@
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/rand.h>
+#include <openssl/sha.h>
#include <vlib/vlib.h>
#include <vnet/plugin/plugin.h>
@@ -29,6 +30,7 @@ typedef struct
CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
EVP_CIPHER_CTX *evp_cipher_ctx;
HMAC_CTX *hmac_ctx;
+ EVP_MD_CTX *hash_ctx;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
HMAC_CTX _hmac_ctx;
#endif
@@ -67,6 +69,13 @@ static openssl_per_thread_data_t *per_thread_data = 0;
#define EVP_CTRL_AEAD_SET_TAG EVP_CTRL_GCM_SET_TAG
#endif
+#define foreach_openssl_hash_op \
+ _ (SHA1, EVP_sha1) \
+ _ (SHA224, EVP_sha224) \
+ _ (SHA256, EVP_sha256) \
+ _ (SHA384, EVP_sha384) \
+ _ (SHA512, EVP_sha512)
+
#define foreach_openssl_hmac_op \
_(MD5, EVP_md5) \
_(SHA1, EVP_sha1) \
@@ -320,6 +329,40 @@ openssl_ops_dec_chacha20_poly1305 (vlib_main_t *vm, vnet_crypto_op_t *ops[],
}
static_always_inline u32
+openssl_ops_hash (vlib_main_t *vm, vnet_crypto_op_t *ops[],
+ vnet_crypto_op_chunk_t *chunks, u32 n_ops, const EVP_MD *md)
+{
+ openssl_per_thread_data_t *ptd =
+ vec_elt_at_index (per_thread_data, vm->thread_index);
+ EVP_MD_CTX *ctx = ptd->hash_ctx;
+ vnet_crypto_op_chunk_t *chp;
+ u32 md_len, i, j, n_fail = 0;
+
+ for (i = 0; i < n_ops; i++)
+ {
+ vnet_crypto_op_t *op = ops[i];
+
+ EVP_DigestInit_ex (ctx, md, NULL);
+ if (op->flags & VNET_CRYPTO_OP_FLAG_CHAINED_BUFFERS)
+ {
+ chp = chunks + op->chunk_index;
+ for (j = 0; j < op->n_chunks; j++)
+ {
+ EVP_DigestUpdate (ctx, chp->src, chp->len);
+ chp += 1;
+ }
+ }
+ else
+ EVP_DigestUpdate (ctx, op->src, op->len);
+
+ EVP_DigestFinal_ex (ctx, op->digest, &md_len);
+ op->digest_len = md_len;
+ op->status = VNET_CRYPTO_OP_STATUS_COMPLETED;
+ }
+ return n_ops - n_fail;
+}
+
+static_always_inline u32
openssl_ops_hmac (vlib_main_t * vm, vnet_crypto_op_t * ops[],
vnet_crypto_op_chunk_t * chunks, u32 n_ops,
const EVP_MD * md)
@@ -397,6 +440,22 @@ openssl_ops_hmac (vlib_main_t * vm, vnet_crypto_op_t * ops[],
foreach_openssl_evp_op;
#undef _
+#define _(a, b) \
+ static u32 openssl_ops_hash_##a (vlib_main_t *vm, vnet_crypto_op_t *ops[], \
+ u32 n_ops) \
+ { \
+ return openssl_ops_hash (vm, ops, 0, n_ops, b ()); \
+ } \
+ static u32 openssl_ops_hash_chained_##a ( \
+ vlib_main_t *vm, vnet_crypto_op_t *ops[], vnet_crypto_op_chunk_t *chunks, \
+ u32 n_ops) \
+ { \
+ return openssl_ops_hash (vm, ops, chunks, n_ops, b ()); \
+ }
+
+foreach_openssl_hash_op;
+#undef _
+
#define _(a, b) \
static u32 \
openssl_ops_hmac_##a (vlib_main_t * vm, vnet_crypto_op_t * ops[], u32 n_ops) \
@@ -440,6 +499,14 @@ crypto_openssl_init (vlib_main_t * vm)
foreach_openssl_hmac_op;
#undef _
+#define _(a, b) \
+ vnet_crypto_register_ops_handlers (vm, eidx, VNET_CRYPTO_OP_##a##_HASH, \
+ openssl_ops_hash_##a, \
+ openssl_ops_hash_chained_##a);
+
+ foreach_openssl_hash_op;
+#undef _
+
vec_validate_aligned (per_thread_data, tm->n_vlib_mains - 1,
CLIB_CACHE_LINE_BYTES);
@@ -448,6 +515,7 @@ crypto_openssl_init (vlib_main_t * vm)
ptd->evp_cipher_ctx = EVP_CIPHER_CTX_new ();
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
ptd->hmac_ctx = HMAC_CTX_new ();
+ ptd->hash_ctx = EVP_MD_CTX_create ();
#else
HMAC_CTX_init (&(ptd->_hmac_ctx));
ptd->hmac_ctx = &ptd->_hmac_ctx;
diff --git a/src/plugins/unittest/CMakeLists.txt b/src/plugins/unittest/CMakeLists.txt
index b6322cddf09..6276a92d749 100644
--- a/src/plugins/unittest/CMakeLists.txt
+++ b/src/plugins/unittest/CMakeLists.txt
@@ -29,6 +29,7 @@ add_vpp_plugin(unittest
crypto/rfc2202_hmac_md5.c
crypto/rfc2202_hmac_sha1.c
crypto/rfc4231.c
+ crypto/sha.c
crypto_test.c
fib_test.c
interface_test.c
diff --git a/src/plugins/unittest/crypto/sha.c b/src/plugins/unittest/crypto/sha.c
new file mode 100644
index 00000000000..384e359980d
--- /dev/null
+++ b/src/plugins/unittest/crypto/sha.c
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2021 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 <vnet/crypto/crypto.h>
+#include <unittest/crypto/crypto.h>
+
+static char sha_data[3] = "abc";
+
+static u8 sha1_tc1_digest[20] = { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81,
+ 0x6a, 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50,
+ 0xc2, 0x6c, 0x9c, 0xd0, 0xd8, 0x9d };
+
+static u8 sha224_digest[] = {
+ 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22, 0x86, 0x42,
+ 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3, 0x2A, 0xAD, 0xBC, 0xE4,
+ 0xBD, 0xA0, 0xB3, 0xF7, 0xE3, 0x6C, 0x9D, 0xA7,
+};
+
+static u8 sha256_digest[] = { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
+ 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
+ 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
+ 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD };
+
+static u8 sha384_digest[] = { 0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B,
+ 0xB5, 0xA0, 0x3D, 0x69, 0x9A, 0xC6, 0x50, 0x07,
+ 0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63,
+ 0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED,
+ 0x80, 0x86, 0x07, 0x2B, 0xA1, 0xE7, 0xCC, 0x23,
+ 0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7 };
+
+static u8 sha512_digest[] = {
+ 0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA, 0xCC, 0x41, 0x73, 0x49, 0xAE,
+ 0x20, 0x41, 0x31, 0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2, 0x0A, 0x9E,
+ 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A, 0x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1,
+ 0xA8, 0x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD, 0x45, 0x4D, 0x44, 0x23,
+ 0x64, 0x3C, 0xE8, 0x0E, 0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F
+};
+
+UNITTEST_REGISTER_CRYPTO_TEST (nist_sha1_tc1) = {
+ .name = "NIST SHA-1 TC1",
+ .alg = VNET_CRYPTO_ALG_HASH_SHA1,
+ .plaintext = TEST_DATA (sha_data),
+ .digest = TEST_DATA (sha1_tc1_digest),
+};
+
+UNITTEST_REGISTER_CRYPTO_TEST (nist_sha224_tc1) = {
+ .name = "NIST SHA-224 TC1",
+ .alg = VNET_CRYPTO_ALG_HASH_SHA224,
+ .plaintext = TEST_DATA (sha_data),
+ .digest = TEST_DATA (sha224_digest),
+};
+
+UNITTEST_REGISTER_CRYPTO_TEST (nist_sha256_tc1) = {
+ .name = "NIST SHA-256 TC1",
+ .alg = VNET_CRYPTO_ALG_HASH_SHA256,
+ .plaintext = TEST_DATA (sha_data),
+ .digest = TEST_DATA (sha256_digest),
+};
+
+UNITTEST_REGISTER_CRYPTO_TEST (nist_sha384_tc1) = {
+ .name = "NIST SHA-384 TC1",
+ .alg = VNET_CRYPTO_ALG_HASH_SHA384,
+ .plaintext = TEST_DATA (sha_data),
+ .digest = TEST_DATA (sha384_digest),
+};
+
+UNITTEST_REGISTER_CRYPTO_TEST (nist_sha512_tc1) = {
+ .name = "NIST SHA-512 TC1",
+ .alg = VNET_CRYPTO_ALG_HASH_SHA512,
+ .plaintext = TEST_DATA (sha_data),
+ .digest = TEST_DATA (sha512_digest),
+};
+
+/*
+ * 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
index f08b253dd19..ed21e86b8d7 100644
--- a/src/plugins/unittest/crypto_test.c
+++ b/src/plugins/unittest/crypto_test.c
@@ -75,6 +75,9 @@ print_results (vlib_main_t * vm, unittest_crypto_test_registration_t ** rv,
case VNET_CRYPTO_OP_TYPE_HMAC:
exp_digest = &r->digest;
break;
+ case VNET_CRYPTO_OP_TYPE_HASH:
+ exp_digest = &r->digest;
+ break;
default:
ASSERT (0);
}
@@ -629,6 +632,12 @@ test_crypto_static (vlib_main_t * vm, crypto_test_main_t * tm,
op->len = r->plaintext.length;
}
break;
+ case VNET_CRYPTO_OP_TYPE_HASH:
+ op->digest = computed_data + computed_data_total_len;
+ computed_data_total_len += r->digest.length;
+ op->src = r->plaintext.data;
+ op->len = r->plaintext.length;
+ break;
default:
break;
};
@@ -802,6 +811,10 @@ test_crypto (vlib_main_t * vm, crypto_test_main_t * tm)
n_ops_static += 1;
}
break;
+ case VNET_CRYPTO_OP_TYPE_HASH:
+ computed_data_total_len += r->digest.length;
+ n_ops_static += 1;
+ break;
default:
break;
};
diff --git a/src/vnet/crypto/crypto.c b/src/vnet/crypto/crypto.c
index 74f945e8382..3b1505ad448 100644
--- a/src/vnet/crypto/crypto.c
+++ b/src/vnet/crypto/crypto.c
@@ -343,7 +343,13 @@ vnet_crypto_key_len_check (vnet_crypto_alg_t alg, u16 length)
#define _(n, s) \
case VNET_CRYPTO_ALG_HMAC_##n: \
return 1;
- foreach_crypto_hmac_alg
+ foreach_crypto_hmac_alg
+#undef _
+
+#define _(n, s) \
+ case VNET_CRYPTO_ALG_HASH_##n: \
+ return 1;
+ foreach_crypto_hash_alg
#undef _
}
@@ -669,6 +675,20 @@ vnet_crypto_init_cipher_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t eid,
}
static void
+vnet_crypto_init_hash_data (vnet_crypto_alg_t alg, vnet_crypto_op_id_t id,
+ char *name)
+{
+ vnet_crypto_main_t *cm = &crypto_main;
+ cm->algs[alg].name = name;
+ cm->algs[alg].op_by_type[VNET_CRYPTO_OP_TYPE_HASH] = id;
+ cm->opt_data[id].alg = alg;
+ cm->opt_data[id].active_engine_index_simple = ~0;
+ cm->opt_data[id].active_engine_index_chained = ~0;
+ cm->opt_data[id].type = VNET_CRYPTO_OP_TYPE_HASH;
+ hash_set_mem (cm->alg_index_by_name, name, alg);
+}
+
+static void
vnet_crypto_init_hmac_data (vnet_crypto_alg_t alg,
vnet_crypto_op_id_t id, char *name)
{
@@ -740,6 +760,11 @@ vnet_crypto_init (vlib_main_t * vm)
VNET_CRYPTO_OP_##n##_HMAC, "hmac-" s);
foreach_crypto_hmac_alg;
#undef _
+#define _(n, s) \
+ vnet_crypto_init_hash_data (VNET_CRYPTO_ALG_HASH_##n, \
+ VNET_CRYPTO_OP_##n##_HASH, s);
+ foreach_crypto_hash_alg;
+#undef _
#define _(n, s, k, t, a) \
vnet_crypto_init_async_data (VNET_CRYPTO_ALG_##n##_TAG##t##_AAD##a, \
VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_ENC, \
diff --git a/src/vnet/crypto/crypto.h b/src/vnet/crypto/crypto.h
index a44c8910555..71978b64835 100644
--- a/src/vnet/crypto/crypto.h
+++ b/src/vnet/crypto/crypto.h
@@ -39,6 +39,13 @@
_(AES_256_GCM, "aes-256-gcm", 32) \
_(CHACHA20_POLY1305, "chacha20-poly1305", 32)
+#define foreach_crypto_hash_alg \
+ _ (SHA1, "sha-1") \
+ _ (SHA224, "sha-224") \
+ _ (SHA256, "sha-256") \
+ _ (SHA384, "sha-384") \
+ _ (SHA512, "sha-512")
+
#define foreach_crypto_hmac_alg \
_(MD5, "md5") \
_(SHA1, "sha-1") \
@@ -47,12 +54,13 @@
_(SHA384, "sha-384") \
_(SHA512, "sha-512")
-#define foreach_crypto_op_type \
- _(ENCRYPT, "encrypt") \
- _(DECRYPT, "decrypt") \
- _(AEAD_ENCRYPT, "aead-encrypt") \
- _(AEAD_DECRYPT, "aead-decrypt") \
- _(HMAC, "hmac")
+#define foreach_crypto_op_type \
+ _ (ENCRYPT, "encrypt") \
+ _ (DECRYPT, "decrypt") \
+ _ (AEAD_ENCRYPT, "aead-encrypt") \
+ _ (AEAD_DECRYPT, "aead-decrypt") \
+ _ (HMAC, "hmac") \
+ _ (HASH, "hash")
typedef enum
{
@@ -138,13 +146,15 @@ typedef enum
{
VNET_CRYPTO_ALG_NONE = 0,
#define _(n, s, l) VNET_CRYPTO_ALG_##n,
- foreach_crypto_cipher_alg
- foreach_crypto_aead_alg
+ foreach_crypto_cipher_alg foreach_crypto_aead_alg
#undef _
#define _(n, s) VNET_CRYPTO_ALG_HMAC_##n,
- foreach_crypto_hmac_alg
+ foreach_crypto_hmac_alg
+#undef _
+#define _(n, s) VNET_CRYPTO_ALG_HASH_##n,
+ foreach_crypto_hash_alg
#undef _
- VNET_CRYPTO_N_ALGS,
+ VNET_CRYPTO_N_ALGS,
} vnet_crypto_alg_t;
typedef enum
@@ -210,13 +220,15 @@ typedef enum
{
VNET_CRYPTO_OP_NONE = 0,
#define _(n, s, l) VNET_CRYPTO_OP_##n##_ENC, VNET_CRYPTO_OP_##n##_DEC,
- foreach_crypto_cipher_alg
- foreach_crypto_aead_alg
+ foreach_crypto_cipher_alg foreach_crypto_aead_alg
#undef _
#define _(n, s) VNET_CRYPTO_OP_##n##_HMAC,
- foreach_crypto_hmac_alg
+ foreach_crypto_hmac_alg
+#undef _
+#define _(n, s) VNET_CRYPTO_OP_##n##_HASH,
+ foreach_crypto_hash_alg
#undef _
- VNET_CRYPTO_N_OP_IDS,
+ VNET_CRYPTO_N_OP_IDS,
} vnet_crypto_op_id_t;
/* *INDENT-ON* */