From patchwork Tue Jan 2 22:00:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Github ODP bot X-Patchwork-Id: 123228 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp9452964qgn; Tue, 2 Jan 2018 14:01:06 -0800 (PST) X-Google-Smtp-Source: ACJfBotteXN3Xqm6EsPYhOcPXnZcl4TP9mdKb25igH5H20rCx2OHpac+XPSczVjIPyNZp6OmSQcD X-Received: by 10.200.37.203 with SMTP id f11mr65825283qtf.230.1514930466694; Tue, 02 Jan 2018 14:01:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514930466; cv=none; d=google.com; s=arc-20160816; b=KNbGO+zJHjHJKiXDu4g4e15GNFCtHNoqGA87lmKGrfhtvmCueK5Ag/8NhGfiBU7zK2 cEJJ81oxqMk2UtNB93cV7UiuUZvYx1122X1NElrBYO9SAjFoz2pTI6DlEbNGRpQYAiu2 OgIEVbVRqikybiMSYh5cINRSveDmEEuwauxl//y4mVxjCmqwdEz9CKZvsnsLlDlHOOas BNPv+adiJZRN1d/SzcOLFOrvSuH1VLpaXQG6gJtaBV1aguFnbTe3yJVd4aI2in2mL+0q vankuB/NLEJR3kHxke6EJmMR8971LtFO7TaLGrsnqvWCfrxibW6H3L10SXb89Ci27jiS qi3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:github-pr-num :references:in-reply-to:message-id:date:to:from:delivered-to :arc-authentication-results; bh=kEm0JV+xmkfao/aA2PxrlLDHV53aE89JGS1yrCwqzP0=; b=c8b0WF14w2w/+oKsBp2nyfz+TPRVk2A/yPNW/UdLtU5gaOz/RQfInFHLI5ROencwas kwKFRgYYcI37gRgmcbPg9gZ3VMGm9kqFxIscS3bAeZn8jRTgHrPyxwa3p8IGD28UfMrS Ozm8CBD0M05NHQCJy4dlTnjZz4t5Sdv6hV3YAb9fImYjM5kXoQC+LBoH3p95j88+fHue G3s06lSMGt5emeIBkiWvPz1vAFBF/8ijw8H8tOTjpLDV4dkrfHYRQ4tqvqUUZQr8JQpo kk+hygvLqz3pygbaY5LP8u7dZZJTXNJZFB7nL+kBmybAa6TQviLfDGNXsw4uYi8ETcYG PXFA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.197.127.237 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Return-Path: Received: from lists.linaro.org (ec2-54-197-127-237.compute-1.amazonaws.com. [54.197.127.237]) by mx.google.com with ESMTP id g41si13841731qtc.18.2018.01.02.14.01.06; Tue, 02 Jan 2018 14:01:06 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.197.127.237 as permitted sender) client-ip=54.197.127.237; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.197.127.237 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Received: by lists.linaro.org (Postfix, from userid 109) id 66CF66084A; Tue, 2 Jan 2018 22:01:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 1B8516087F; Tue, 2 Jan 2018 22:00:30 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 1DB666084A; Tue, 2 Jan 2018 22:00:23 +0000 (UTC) Received: from forward103p.mail.yandex.net (forward103p.mail.yandex.net [77.88.28.106]) by lists.linaro.org (Postfix) with ESMTPS id BF5D56071D for ; Tue, 2 Jan 2018 22:00:20 +0000 (UTC) Received: from mxback11g.mail.yandex.net (mxback11g.mail.yandex.net [IPv6:2a02:6b8:0:1472:2741:0:8b7:90]) by forward103p.mail.yandex.net (Yandex) with ESMTP id 88F9121822DE for ; Wed, 3 Jan 2018 01:00:19 +0300 (MSK) Received: from smtp1j.mail.yandex.net (smtp1j.mail.yandex.net [2a02:6b8:0:801::ab]) by mxback11g.mail.yandex.net (nwsmtp/Yandex) with ESMTP id dqMixIZUOD-0JZqOadp; Wed, 03 Jan 2018 01:00:19 +0300 Received: by smtp1j.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id VdGnHvf6yY-0IWGUhlI; Wed, 03 Jan 2018 01:00:18 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) From: Github ODP bot To: lng-odp@lists.linaro.org Date: Wed, 3 Jan 2018 01:00:15 +0300 Message-Id: <1514930417-24831-2-git-send-email-odpbot@yandex.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1514930417-24831-1-git-send-email-odpbot@yandex.ru> References: <1514930417-24831-1-git-send-email-odpbot@yandex.ru> Github-pr-num: 342 Subject: [lng-odp] [PATCH v11 1/3] performance: add AES tests to crypto performance tests X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" From: Dmitry Eremin-Solenikov Signed-off-by: Dmitry Eremin-Solenikov --- /** Email created from pull request 342 (lumag:openssl-ctx) ** https://github.com/Linaro/odp/pull/342 ** Patch: https://github.com/Linaro/odp/pull/342.patch ** Base sha: 49ebafae0edebbc750742d8874ad0a7588286dea ** Merge commit sha: a7b33af3346ca9716c2d6d2f9f7952377cedeebf **/ test/performance/odp_crypto.c | 71 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/test/performance/odp_crypto.c b/test/performance/odp_crypto.c index 0cbc2754e..b1c1b79fe 100644 --- a/test/performance/odp_crypto.c +++ b/test/performance/odp_crypto.c @@ -30,7 +30,7 @@ */ #define POOL_NUM_PKT 64 -static uint8_t test_iv[8] = "01234567"; +static uint8_t test_iv[16] = "0123456789abcdef"; static uint8_t test_key16[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, @@ -38,6 +38,12 @@ static uint8_t test_key16[16] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x10, }; +static uint8_t test_key20[20] = { 0x01, 0x02, 0x03, 0x04, 0x05, + 0x06, 0x07, 0x08, 0x09, 0x0a, + 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, +}; + static uint8_t test_key24[24] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, @@ -226,6 +232,69 @@ static crypto_alg_config_t algs_config[] = { .auth_digest_len = 12, }, }, + { + .name = "aes-cbc-null", + .session = { + .cipher_alg = ODP_CIPHER_ALG_AES_CBC, + .cipher_key = { + .data = test_key16, + .length = sizeof(test_key16) + }, + .iv = { + .data = test_iv, + .length = 16, + }, + .auth_alg = ODP_AUTH_ALG_NULL + }, + }, + { + .name = "aes-cbc-hmac-sha1-96", + .session = { + .cipher_alg = ODP_CIPHER_ALG_AES_CBC, + .cipher_key = { + .data = test_key16, + .length = sizeof(test_key16) + }, + .iv = { + .data = test_iv, + .length = 16, + }, + .auth_alg = ODP_AUTH_ALG_SHA1_HMAC, + .auth_key = { + .data = test_key20, + .length = sizeof(test_key20) + }, + .auth_digest_len = 12, + }, + }, + { + .name = "null-hmac-sha1-96", + .session = { + .cipher_alg = ODP_CIPHER_ALG_NULL, + .auth_alg = ODP_AUTH_ALG_SHA1_HMAC, + .auth_key = { + .data = test_key20, + .length = sizeof(test_key20) + }, + .auth_digest_len = 12, + }, + }, + { + .name = "aes-gcm", + .session = { + .cipher_alg = ODP_CIPHER_ALG_AES_GCM, + .cipher_key = { + .data = test_key16, + .length = sizeof(test_key16) + }, + .iv = { + .data = test_iv, + .length = 12, + }, + .auth_alg = ODP_AUTH_ALG_AES_GCM, + .auth_digest_len = 16, + }, + }, }; /** From patchwork Tue Jan 2 22:00:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Github ODP bot X-Patchwork-Id: 123229 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp9454010qgn; Tue, 2 Jan 2018 14:01:55 -0800 (PST) X-Google-Smtp-Source: ACJfBovmn94lCac/KbizTWcrPQ2wptj4i9hmTjW4sYTkFlPgKJ8NbEcGn+Gnl8XYIT/UCTZ4FH2G X-Received: by 10.237.37.162 with SMTP id x31mr64600046qtc.58.1514930515257; Tue, 02 Jan 2018 14:01:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1514930515; cv=none; d=google.com; s=arc-20160816; b=QgQ4fM2jyf08bQWWFpmX69CuGTSRgJ++kEeBiiNaC5g/v7Ayf3mTvcgn/EvPGzECYS 1ObliAAaNq1zn1P4gmG9alTb0urwygjH+1XdWnJ0Ka6oUqPDWh447HR7dqGOE/XPqHa6 cgMsU7gAyMOPEaRinAWZXY4PSr1/idMNsbKeulyduzXkufXynUTFnfXrOmqzRKh9ehf5 KKWbJ8jLUZL4SQuI4OYhVZegXsiSeuWhsw+awdoVnBF4dwyjo0RCkIeXROKuszuhnodM 8k+5F6PqX5JuA69q853/ENCqGFxLL43lSNSnr9FJcSSnZ3OaFVcewl86dAVyrkWT0jrV sOXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:github-pr-num :references:in-reply-to:message-id:date:to:from:delivered-to :arc-authentication-results; bh=MORh3rYZUoGTUA6im5r1ONiTDreRWKXq6yaZ8oRgFAI=; b=KHHyQ7yOaaa1jBvakoYqb0lUECIIaMzzG1ofvgZgK9fyrkXpmBIxebCCTSOEwoNtsr 6DwkJ1YWS776y1DQmRUCXn1MmiMvH3a8sfNgM0jf2tZEUaqxK/uAwRG9tFrDC27Tx9Is lJvdnNTyOi3Wnp/4ufN5iSvSju02CUq4XRnus3Wpr2zYlsFpAQQHPiNJOfWXAnlmXJJ+ vRT/tQ2fUwA6szwmN6Q2LRHNFrbLNNmrPzSXcEJ4UlXHcn/41GZufq6m6a28DRj0Ow5i K4+XaHNGCrRwye0vGin6WQBvzFu8wq+ftpOr/WF2o7YAyx5+dJQsuVrOKzXfCP+DYtZd zgRg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.197.127.237 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Return-Path: Received: from lists.linaro.org (ec2-54-197-127-237.compute-1.amazonaws.com. [54.197.127.237]) by mx.google.com with ESMTP id u38si1281934qtu.453.2018.01.02.14.01.55; Tue, 02 Jan 2018 14:01:55 -0800 (PST) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.197.127.237 as permitted sender) client-ip=54.197.127.237; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.197.127.237 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Received: by lists.linaro.org (Postfix, from userid 109) id EF679608D9; Tue, 2 Jan 2018 22:01:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 8F42B60905; Tue, 2 Jan 2018 22:00:39 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 8EFDE6088B; Tue, 2 Jan 2018 22:00:28 +0000 (UTC) Received: from forward101o.mail.yandex.net (forward101o.mail.yandex.net [37.140.190.181]) by lists.linaro.org (Postfix) with ESMTPS id 9BC9E606A3 for ; Tue, 2 Jan 2018 22:00:22 +0000 (UTC) Received: from mxback3j.mail.yandex.net (mxback3j.mail.yandex.net [IPv6:2a02:6b8:0:1619::10c]) by forward101o.mail.yandex.net (Yandex) with ESMTP id 3C22E1342E16 for ; Wed, 3 Jan 2018 01:00:21 +0300 (MSK) Received: from smtp1j.mail.yandex.net (smtp1j.mail.yandex.net [2a02:6b8:0:801::ab]) by mxback3j.mail.yandex.net (nwsmtp/Yandex) with ESMTP id nOtkK7vttl-0LceF5ls; Wed, 03 Jan 2018 01:00:21 +0300 Received: by smtp1j.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id VdGnHvf6yY-0KWOCZpD; Wed, 03 Jan 2018 01:00:20 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) From: Github ODP bot To: lng-odp@lists.linaro.org Date: Wed, 3 Jan 2018 01:00:17 +0300 Message-Id: <1514930417-24831-4-git-send-email-odpbot@yandex.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1514930417-24831-1-git-send-email-odpbot@yandex.ru> References: <1514930417-24831-1-git-send-email-odpbot@yandex.ru> Github-pr-num: 342 Subject: [lng-odp] [PATCH v11 3/3] linux-gen: crypto: significant speedup of all operations X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" From: Dmitry Eremin-Solenikov Per idea of Janne Peltonen, do not allocate/free crypto contexts for each operation, providing significant speed increase. Each thread on startup allocates hmac+cipher contexts pair for each crypto session. Then they are initialized on demand, when session is first executed using this thread. Signed-off-by: Dmitry Eremin-Solenikov --- /** Email created from pull request 342 (lumag:openssl-ctx) ** https://github.com/Linaro/odp/pull/342 ** Patch: https://github.com/Linaro/odp/pull/342.patch ** Base sha: 49ebafae0edebbc750742d8874ad0a7588286dea ** Merge commit sha: a7b33af3346ca9716c2d6d2f9f7952377cedeebf **/ platform/linux-generic/include/odp_internal.h | 2 + platform/linux-generic/odp_crypto.c | 277 +++++++++++++++++--------- platform/linux-generic/odp_init.c | 13 ++ 3 files changed, 201 insertions(+), 91 deletions(-) diff --git a/platform/linux-generic/include/odp_internal.h b/platform/linux-generic/include/odp_internal.h index 54f95b8b3..4fd721449 100644 --- a/platform/linux-generic/include/odp_internal.h +++ b/platform/linux-generic/include/odp_internal.h @@ -110,6 +110,8 @@ int odp_queue_term_global(void); int odp_crypto_init_global(void); int odp_crypto_term_global(void); +int _odp_crypto_init_local(void); +int _odp_crypto_term_local(void); int odp_timer_init_global(const odp_init_t *params); int odp_timer_term_global(void); diff --git a/platform/linux-generic/odp_crypto.c b/platform/linux-generic/odp_crypto.c index 3b110c191..8bef7a859 100644 --- a/platform/linux-generic/odp_crypto.c +++ b/platform/linux-generic/odp_crypto.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -20,6 +19,7 @@ #include #include #include +#include #include #include @@ -129,6 +129,8 @@ struct odp_crypto_generic_session_t { }; crypto_func_t func; } auth; + + unsigned idx; }; typedef struct odp_crypto_global_s odp_crypto_global_t; @@ -137,15 +139,56 @@ struct odp_crypto_global_s { odp_spinlock_t lock; odp_crypto_generic_session_t *free; odp_crypto_generic_session_t sessions[MAX_SESSIONS]; + + /* These bitfields are cleared at alloc_session() + * together with the rest of data */ + bitset_t hmac_valid[ODP_THREAD_COUNT_MAX] + [(MAX_SESSIONS + ATOM_BITSET_SIZE - 1) / ATOM_BITSET_SIZE]; + bitset_t cipher_valid[ODP_THREAD_COUNT_MAX] + [(MAX_SESSIONS + ATOM_BITSET_SIZE - 1) / ATOM_BITSET_SIZE]; + bitset_t mac_cipher_valid[ODP_THREAD_COUNT_MAX] + [(MAX_SESSIONS + ATOM_BITSET_SIZE - 1) / ATOM_BITSET_SIZE]; + odp_ticketlock_t openssl_lock[0]; }; static odp_crypto_global_t *global; +typedef struct crypto_local_t { + HMAC_CTX *hmac_ctx[MAX_SESSIONS]; + EVP_CIPHER_CTX *cipher_ctx[MAX_SESSIONS]; + EVP_CIPHER_CTX *mac_cipher_ctx[MAX_SESSIONS]; + bitset_t *hmac_valid; + bitset_t *cipher_valid; + bitset_t *mac_cipher_valid; +} crypto_local_t; + +static __thread crypto_local_t local; + +typedef enum { KIND_HMAC, KIND_CIPHER, KIND_MAC_CIPHER } crypto_kind_t; + +static inline int crypto_should_init(odp_crypto_generic_session_t *session, + crypto_kind_t kind) +{ + bitset_t *ptr = kind == KIND_HMAC ? local.hmac_valid : + kind == KIND_CIPHER ? local.cipher_valid : + local.mac_cipher_valid; + bitset_t *cv = ptr + (session->idx / ATOM_BITSET_SIZE); + bitset_t cur = atom_bitset_load(cv, __ATOMIC_ACQUIRE); + + if (bitset_is_set(cur, session->idx % ATOM_BITSET_SIZE)) + return false; + + atom_bitset_set(cv, session->idx % ATOM_BITSET_SIZE, __ATOMIC_RELEASE); + + return true; +} + static odp_crypto_generic_session_t *alloc_session(void) { odp_crypto_generic_session_t *session = NULL; + unsigned i; odp_spinlock_lock(&global->lock); session = global->free; @@ -155,6 +198,19 @@ odp_crypto_generic_session_t *alloc_session(void) } odp_spinlock_unlock(&global->lock); + session->idx = session - global->sessions; + + for (i = 0; i < ODP_THREAD_COUNT_MAX; i++) { + bitset_t *cv; + + cv = global->hmac_valid[i] + (session->idx / ATOM_BITSET_SIZE); + atom_bitset_clr(cv, session->idx % ATOM_BITSET_SIZE, __ATOMIC_ACQ_REL); + cv = global->cipher_valid[i] + (session->idx / ATOM_BITSET_SIZE); + atom_bitset_clr(cv, session->idx % ATOM_BITSET_SIZE, __ATOMIC_ACQ_REL); + cv = global->mac_cipher_valid[i] + (session->idx / ATOM_BITSET_SIZE); + atom_bitset_clr(cv, session->idx % ATOM_BITSET_SIZE, __ATOMIC_ACQ_REL); + } + return session; } @@ -175,24 +231,51 @@ null_crypto_routine(odp_packet_t pkt ODP_UNUSED, return ODP_CRYPTO_ALG_ERR_NONE; } +/* Mimic new OpenSSL 1.1.y API */ +#if OPENSSL_VERSION_NUMBER < 0x10100000L +static HMAC_CTX *HMAC_CTX_new(void) +{ + HMAC_CTX *ctx = malloc(sizeof(*ctx)); + + HMAC_CTX_init(ctx); + return ctx; +} + +static void HMAC_CTX_free(HMAC_CTX *ctx) +{ + HMAC_CTX_cleanup(ctx); + free(ctx); +} +#endif + static -void packet_hmac_calculate(HMAC_CTX *ctx, - odp_packet_t pkt, - const odp_crypto_packet_op_param_t *param, - odp_crypto_generic_session_t *session, - uint8_t *hash) +void packet_hmac(odp_packet_t pkt, + const odp_crypto_packet_op_param_t *param, + odp_crypto_generic_session_t *session, + uint8_t *hash) { + HMAC_CTX *ctx = local.hmac_ctx[session->idx]; uint32_t offset = param->auth_range.offset; uint32_t len = param->auth_range.length; ODP_ASSERT(offset + len <= odp_packet_len(pkt)); - HMAC_Init_ex(ctx, - session->auth.key, - session->auth.key_length, - session->auth.evp_md, - NULL); + if (crypto_should_init(session, KIND_HMAC)) { + HMAC_Init_ex(ctx, + session->auth.key, + session->auth.key_length, + session->auth.evp_md, + NULL); + } else { + /* Reinitialize HMAC calculation without resetting the key */ + HMAC_Init_ex(ctx, + NULL, + 0, + NULL, + NULL); + } + /* Hash it */ while (len > 0) { uint32_t seglen = 0; /* GCC */ void *mapaddr = odp_packet_offset(pkt, offset, &seglen, NULL); @@ -206,36 +289,6 @@ void packet_hmac_calculate(HMAC_CTX *ctx, HMAC_Final(ctx, hash, NULL); } -#if OPENSSL_VERSION_NUMBER < 0x10100000L -static -void packet_hmac(odp_packet_t pkt, - const odp_crypto_packet_op_param_t *param, - odp_crypto_generic_session_t *session, - uint8_t *hash) -{ - HMAC_CTX ctx; - - /* Hash it */ - HMAC_CTX_init(&ctx); - packet_hmac_calculate(&ctx, pkt, param, session, hash); - HMAC_CTX_cleanup(&ctx); -} -#else -static -void packet_hmac(odp_packet_t pkt, - const odp_crypto_packet_op_param_t *param, - odp_crypto_generic_session_t *session, - uint8_t *hash) -{ - HMAC_CTX *ctx; - - /* Hash it */ - ctx = HMAC_CTX_new(); - packet_hmac_calculate(ctx, pkt, param, session, hash); - HMAC_CTX_free(ctx); -} -#endif - static odp_crypto_alg_err_t auth_gen(odp_packet_t pkt, const odp_crypto_packet_op_param_t *param, @@ -430,7 +483,7 @@ odp_crypto_alg_err_t cipher_encrypt(odp_packet_t pkt, const odp_crypto_packet_op_param_t *param, odp_crypto_generic_session_t *session) { - EVP_CIPHER_CTX *ctx; + EVP_CIPHER_CTX *ctx = local.cipher_ctx[session->idx]; void *iv_ptr; int ret; @@ -442,16 +495,15 @@ odp_crypto_alg_err_t cipher_encrypt(odp_packet_t pkt, return ODP_CRYPTO_ALG_ERR_IV_INVALID; /* Encrypt it */ - ctx = EVP_CIPHER_CTX_new(); - EVP_EncryptInit_ex(ctx, session->cipher.evp_cipher, NULL, - session->cipher.key_data, NULL); + if (crypto_should_init(session, false)) { + EVP_EncryptInit_ex(ctx, session->cipher.evp_cipher, NULL, + session->cipher.key_data, NULL); + EVP_CIPHER_CTX_set_padding(ctx, 0); + } EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv_ptr); - EVP_CIPHER_CTX_set_padding(ctx, 0); ret = internal_encrypt(ctx, pkt, param); - EVP_CIPHER_CTX_free(ctx); - return ret <= 0 ? ODP_CRYPTO_ALG_ERR_DATA_SIZE : ODP_CRYPTO_ALG_ERR_NONE; } @@ -461,7 +513,7 @@ odp_crypto_alg_err_t cipher_decrypt(odp_packet_t pkt, const odp_crypto_packet_op_param_t *param, odp_crypto_generic_session_t *session) { - EVP_CIPHER_CTX *ctx; + EVP_CIPHER_CTX *ctx = local.cipher_ctx[session->idx]; void *iv_ptr; int ret; @@ -473,16 +525,15 @@ odp_crypto_alg_err_t cipher_decrypt(odp_packet_t pkt, return ODP_CRYPTO_ALG_ERR_IV_INVALID; /* Decrypt it */ - ctx = EVP_CIPHER_CTX_new(); - EVP_DecryptInit_ex(ctx, session->cipher.evp_cipher, NULL, - session->cipher.key_data, NULL); + if (crypto_should_init(session, KIND_CIPHER)) { + EVP_DecryptInit_ex(ctx, session->cipher.evp_cipher, NULL, + session->cipher.key_data, NULL); + EVP_CIPHER_CTX_set_padding(ctx, 0); + } EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv_ptr); - EVP_CIPHER_CTX_set_padding(ctx, 0); ret = internal_decrypt(ctx, pkt, param); - EVP_CIPHER_CTX_free(ctx); - return ret <= 0 ? ODP_CRYPTO_ALG_ERR_DATA_SIZE : ODP_CRYPTO_ALG_ERR_NONE; } @@ -519,7 +570,7 @@ odp_crypto_alg_err_t aes_gcm_encrypt(odp_packet_t pkt, const odp_crypto_packet_op_param_t *param, odp_crypto_generic_session_t *session) { - EVP_CIPHER_CTX *ctx; + EVP_CIPHER_CTX *ctx = local.cipher_ctx[session->idx]; const uint8_t *aad_head = param->aad_ptr; uint32_t aad_len = session->p.auth_aad_len; void *iv_ptr; @@ -535,13 +586,14 @@ odp_crypto_alg_err_t aes_gcm_encrypt(odp_packet_t pkt, return ODP_CRYPTO_ALG_ERR_IV_INVALID; /* Encrypt it */ - ctx = EVP_CIPHER_CTX_new(); - EVP_EncryptInit_ex(ctx, session->cipher.evp_cipher, NULL, - session->cipher.key_data, NULL); - EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, - session->p.iv.length, NULL); + if (crypto_should_init(session, KIND_CIPHER)) { + EVP_EncryptInit_ex(ctx, session->cipher.evp_cipher, NULL, + session->cipher.key_data, NULL); + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, + session->p.iv.length, NULL); + EVP_CIPHER_CTX_set_padding(ctx, 0); + } EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv_ptr); - EVP_CIPHER_CTX_set_padding(ctx, 0); /* Authenticate header data (if any) without encrypting them */ if (aad_len > 0) @@ -555,8 +607,6 @@ odp_crypto_alg_err_t aes_gcm_encrypt(odp_packet_t pkt, odp_packet_copy_from_mem(pkt, param->hash_result_offset, session->p.auth_digest_len, block); - EVP_CIPHER_CTX_free(ctx); - return ret <= 0 ? ODP_CRYPTO_ALG_ERR_DATA_SIZE : ODP_CRYPTO_ALG_ERR_NONE; } @@ -566,7 +616,7 @@ odp_crypto_alg_err_t aes_gcm_decrypt(odp_packet_t pkt, const odp_crypto_packet_op_param_t *param, odp_crypto_generic_session_t *session) { - EVP_CIPHER_CTX *ctx; + EVP_CIPHER_CTX *ctx = local.cipher_ctx[session->idx]; const uint8_t *aad_head = param->aad_ptr; uint32_t aad_len = session->p.auth_aad_len; int dummy_len = 0; @@ -582,13 +632,14 @@ odp_crypto_alg_err_t aes_gcm_decrypt(odp_packet_t pkt, return ODP_CRYPTO_ALG_ERR_IV_INVALID; /* Decrypt it */ - ctx = EVP_CIPHER_CTX_new(); - EVP_DecryptInit_ex(ctx, session->cipher.evp_cipher, NULL, - session->cipher.key_data, NULL); - EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, - session->p.iv.length, NULL); + if (crypto_should_init(session, KIND_CIPHER)) { + EVP_DecryptInit_ex(ctx, session->cipher.evp_cipher, NULL, + session->cipher.key_data, NULL); + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, + session->p.iv.length, NULL); + EVP_CIPHER_CTX_set_padding(ctx, 0); + } EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv_ptr); - EVP_CIPHER_CTX_set_padding(ctx, 0); odp_packet_copy_to_mem(pkt, param->hash_result_offset, session->p.auth_digest_len, block); @@ -602,8 +653,6 @@ odp_crypto_alg_err_t aes_gcm_decrypt(odp_packet_t pkt, ret = internal_decrypt(ctx, pkt, param); - EVP_CIPHER_CTX_free(ctx); - return ret <= 0 ? ODP_CRYPTO_ALG_ERR_ICV_CHECK : ODP_CRYPTO_ALG_ERR_NONE; } @@ -635,7 +684,7 @@ odp_crypto_alg_err_t aes_gmac_gen(odp_packet_t pkt, const odp_crypto_packet_op_param_t *param, odp_crypto_generic_session_t *session) { - EVP_CIPHER_CTX *ctx; + EVP_CIPHER_CTX *ctx = local.mac_cipher_ctx[session->idx]; void *iv_ptr; uint8_t block[EVP_MAX_MD_SIZE]; int ret; @@ -648,13 +697,14 @@ odp_crypto_alg_err_t aes_gmac_gen(odp_packet_t pkt, return ODP_CRYPTO_ALG_ERR_IV_INVALID; /* Encrypt it */ - ctx = EVP_CIPHER_CTX_new(); - EVP_EncryptInit_ex(ctx, session->auth.evp_cipher, NULL, - session->auth.key, NULL); - EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, - session->p.iv.length, NULL); + if (crypto_should_init(session, KIND_MAC_CIPHER)) { + EVP_EncryptInit_ex(ctx, session->auth.evp_cipher, NULL, + session->auth.key, NULL); + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, + session->p.iv.length, NULL); + EVP_CIPHER_CTX_set_padding(ctx, 0); + } EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv_ptr); - EVP_CIPHER_CTX_set_padding(ctx, 0); ret = internal_aad(ctx, pkt, param); @@ -663,8 +713,6 @@ odp_crypto_alg_err_t aes_gmac_gen(odp_packet_t pkt, odp_packet_copy_from_mem(pkt, param->hash_result_offset, session->p.auth_digest_len, block); - EVP_CIPHER_CTX_free(ctx); - return ret <= 0 ? ODP_CRYPTO_ALG_ERR_DATA_SIZE : ODP_CRYPTO_ALG_ERR_NONE; } @@ -674,7 +722,7 @@ odp_crypto_alg_err_t aes_gmac_check(odp_packet_t pkt, const odp_crypto_packet_op_param_t *param, odp_crypto_generic_session_t *session) { - EVP_CIPHER_CTX *ctx; + EVP_CIPHER_CTX *ctx = local.mac_cipher_ctx[session->idx]; void *iv_ptr; uint8_t block[EVP_MAX_MD_SIZE]; int ret; @@ -687,13 +735,14 @@ odp_crypto_alg_err_t aes_gmac_check(odp_packet_t pkt, return ODP_CRYPTO_ALG_ERR_IV_INVALID; /* Decrypt it */ - ctx = EVP_CIPHER_CTX_new(); - EVP_DecryptInit_ex(ctx, session->auth.evp_cipher, NULL, - session->auth.key, NULL); - EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, - session->p.iv.length, NULL); + if (crypto_should_init(session, KIND_MAC_CIPHER)) { + EVP_DecryptInit_ex(ctx, session->auth.evp_cipher, NULL, + session->auth.key, NULL); + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, + session->p.iv.length, NULL); + EVP_CIPHER_CTX_set_padding(ctx, 0); + } EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv_ptr); - EVP_CIPHER_CTX_set_padding(ctx, 0); odp_packet_copy_to_mem(pkt, param->hash_result_offset, session->p.auth_digest_len, block); @@ -704,8 +753,6 @@ odp_crypto_alg_err_t aes_gmac_check(odp_packet_t pkt, ret = internal_aad(ctx, pkt, param); - EVP_CIPHER_CTX_free(ctx); - return ret <= 0 ? ODP_CRYPTO_ALG_ERR_ICV_CHECK : ODP_CRYPTO_ALG_ERR_NONE; } @@ -1216,6 +1263,54 @@ int odp_crypto_term_global(void) return rc; } +int _odp_crypto_init_local(void) +{ + unsigned i; + int id; + + memset(&local, 0, sizeof(local)); + + for (i = 0; i < MAX_SESSIONS; i++) { + local.hmac_ctx[i] = HMAC_CTX_new(); + local.cipher_ctx[i] = EVP_CIPHER_CTX_new(); + local.mac_cipher_ctx[i] = EVP_CIPHER_CTX_new(); + + if (local.hmac_ctx[i] == NULL || + local.cipher_ctx[i] == NULL || + local.mac_cipher_ctx[i] == NULL) { + _odp_crypto_term_local(); + return -1; + } + } + + /* Clear local valid bits */ + id = odp_thread_id(); + local.hmac_valid = global->hmac_valid[id]; + local.cipher_valid = global->cipher_valid[id]; + local.mac_cipher_valid = global->mac_cipher_valid[id]; + memset(local.hmac_valid, 0, sizeof(global->hmac_valid[0])); + memset(local.cipher_valid, 0, sizeof(global->cipher_valid[0])); + memset(local.mac_cipher_valid, 0, sizeof(global->mac_cipher_valid[0])); + + return 0; +} + +int _odp_crypto_term_local(void) +{ + unsigned i; + + for (i = 0; i < MAX_SESSIONS; i++) { + if (local.hmac_ctx[i] != NULL) + HMAC_CTX_free(local.hmac_ctx[i]); + if (local.cipher_ctx[i] != NULL) + EVP_CIPHER_CTX_free(local.cipher_ctx[i]); + if (local.mac_cipher_ctx[i] != NULL) + EVP_CIPHER_CTX_free(local.mac_cipher_ctx[i]); + } + + return 0; +} + odp_random_kind_t odp_random_max_kind(void) { return ODP_RANDOM_CRYPTO; diff --git a/platform/linux-generic/odp_init.c b/platform/linux-generic/odp_init.c index bff8303ee..bbcbd8dd3 100644 --- a/platform/linux-generic/odp_init.c +++ b/platform/linux-generic/odp_init.c @@ -327,6 +327,12 @@ int odp_init_local(odp_instance_t instance, odp_thread_type_t thr_type) } stage = PKTIO_INIT; + if (_odp_crypto_init_local()) { + ODP_ERR("ODP crypto local init failed.\n"); + goto init_fail; + } + stage = CRYPTO_INIT; + if (odp_pool_init_local()) { ODP_ERR("ODP pool local init failed.\n"); goto init_fail; @@ -379,6 +385,13 @@ int _odp_term_local(enum init_stage stage) } /* Fall through */ + case CRYPTO_INIT: + if (_odp_crypto_term_local()) { + ODP_ERR("ODP crypto local term failed.\n"); + rc = -1; + } + /* Fall through */ + case POOL_INIT: if (odp_pool_term_local()) { ODP_ERR("ODP buffer pool local term failed.\n");