From patchwork Wed Nov 8 18:00:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Github ODP bot X-Patchwork-Id: 118305 Delivered-To: patch@linaro.org Received: by 10.140.22.164 with SMTP id 33csp5603592qgn; Wed, 8 Nov 2017 10:07:28 -0800 (PST) X-Google-Smtp-Source: AGs4zMZI5gb6XrL4jMZJH8r5YdvSqD2/i/huQq1nAsrCRTFR8b9DQjcqQonaUmR53O0jzI3IHfnU X-Received: by 10.55.72.201 with SMTP id v192mr2158010qka.333.1510164448817; Wed, 08 Nov 2017 10:07:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510164448; cv=none; d=google.com; s=arc-20160816; b=PaBM5NYCCP1cMIBGtn5LjQVBUQ01L0Dm5jB3ak7Nk/MkA8T9DrEQwMI5xDeGAfPK2z MF9flWKFix+jiBd39CXWF/wrA0AqEM4tv0UQa0avd2kZgDq2qqfT0E/qAhVFg/AmDu3R vloWKezmSv5M+IlBqd5m1pDgpBZnKNI8pEbo7/NBBGEVxRWLFqUnA6D7Rzmn24rTMWeL UlGUFPZWGiiIP+NY3/88UCwnIJ83ysYoERSWxWIhprz7sLHslBfhOD4olSWp7YI3uOK5 q21yKMbhFci79ZFT7pfYzZtHxRPCp1NahtCNmpi+xL8uL5V837g6/ajWKYC3k5r4mRf9 roYQ== 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=xqwcMJo5wI8LimHoCHpDip68L9Px36imRH0DC0yBank=; b=nbP+H4aZ5L6iRHPpUqnVvOZMK6TV+0KGI8SXkQe+Mf8t+G+9AASA66FsM0dq5Gf+mK DBSb/MMdSAHHRrb4UnOtd+R7H7WmlBz1aF90y0rXCqwj8LkzwM+eoCcrJJriuhcqoNOp 2YCvhQXeBeigYq3Synmwx5RcuSRcSUexH6MIkJX0Q4TIk36/f4no1pQxlskgfuQVvfeu 3zR00qfaUX2ypkID5e9a7FFosOxvOKzEtmn5McFb0rq7J6/+nmOhIG4kmjDW+64yhhZv VcXa5IMHui9cSnbf+DtEb+As25V0frxNRDM4MvtPDju/asrUy7RZskDEnwMit17Mfjot 3E+g== 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 w73si4331389qkb.23.2017.11.08.10.07.28; Wed, 08 Nov 2017 10:07:28 -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 872A760C27; Wed, 8 Nov 2017 18:07:28 +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_H3,RCVD_IN_MSPIKE_WL,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 5F6AD61001; Wed, 8 Nov 2017 18:01:17 +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 317146069F; Wed, 8 Nov 2017 18:00:26 +0000 (UTC) Received: from forward104o.mail.yandex.net (forward104o.mail.yandex.net [37.140.190.179]) by lists.linaro.org (Postfix) with ESMTPS id 08D396069D for ; Wed, 8 Nov 2017 18:00:17 +0000 (UTC) Received: from mxback2o.mail.yandex.net (mxback2o.mail.yandex.net [IPv6:2a02:6b8:0:1a2d::1c]) by forward104o.mail.yandex.net (Yandex) with ESMTP id 873FC70238E for ; Wed, 8 Nov 2017 21:00:15 +0300 (MSK) Received: from smtp2p.mail.yandex.net (smtp2p.mail.yandex.net [2a02:6b8:0:1472:2741:0:8b6:7]) by mxback2o.mail.yandex.net (nwsmtp/Yandex) with ESMTP id LWd6gH2aBJ-0FDeF3f2; Wed, 08 Nov 2017 21:00:15 +0300 Received: by smtp2p.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id unJA9Y8tSc-0F3mQ32o; Wed, 08 Nov 2017 21:00:15 +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, 8 Nov 2017 21:00:09 +0300 Message-Id: <1510164012-14286-6-git-send-email-odpbot@yandex.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1510164012-14286-1-git-send-email-odpbot@yandex.ru> References: <1510164012-14286-1-git-send-email-odpbot@yandex.ru> Github-pr-num: 288 Subject: [lng-odp] [PATCH API-NEXT v1 5/8] linux-gen: crypto: add AES-GMAC implementation 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 Implement AES-GMAC on top of OpenSSL AES-GCM with all text going into AAD part. Signed-off-by: Dmitry Eremin-Solenikov --- /** Email created from pull request 288 (lumag:gmac) ** https://github.com/Linaro/odp/pull/288 ** Patch: https://github.com/Linaro/odp/pull/288.patch ** Base sha: d22c949cc466bf28de559855a1cb525740578137 ** Merge commit sha: 569b57eebedebdf2d3148ca06f481cb94bea52e4 **/ .../linux-generic/include/odp_crypto_internal.h | 5 +- platform/linux-generic/odp_crypto.c | 149 ++++++++++++++++++++- 2 files changed, 152 insertions(+), 2 deletions(-) diff --git a/platform/linux-generic/include/odp_crypto_internal.h b/platform/linux-generic/include/odp_crypto_internal.h index 12d1720b7..21174daa4 100644 --- a/platform/linux-generic/include/odp_crypto_internal.h +++ b/platform/linux-generic/include/odp_crypto_internal.h @@ -51,7 +51,10 @@ struct odp_crypto_generic_session { uint8_t key[EVP_MAX_KEY_LENGTH]; uint32_t key_length; uint32_t bytes; - const EVP_MD *evp_md; + union { + const EVP_MD *evp_md; + const EVP_CIPHER *evp_cipher; + }; crypto_func_t func; } auth; }; diff --git a/platform/linux-generic/odp_crypto.c b/platform/linux-generic/odp_crypto.c index 3174feee0..ac0fa337b 100644 --- a/platform/linux-generic/odp_crypto.c +++ b/platform/linux-generic/odp_crypto.c @@ -38,7 +38,9 @@ * Keep sorted: first by key length, then by IV length */ static const odp_crypto_cipher_capability_t cipher_capa_null[] = { -{.key_len = 0, .iv_len = 0} }; +{.key_len = 0, .iv_len = 0}, +/* Special case for GMAC */ +{.key_len = 0, .iv_len = 12} }; static const odp_crypto_cipher_capability_t cipher_capa_des[] = { {.key_len = 24, .iv_len = 8} }; @@ -83,6 +85,9 @@ static const odp_crypto_auth_capability_t auth_capa_sha512_hmac[] = { static const odp_crypto_auth_capability_t auth_capa_aes_gcm[] = { {.digest_len = 16, .key_len = 0, .aad_len = {.min = 8, .max = 12, .inc = 4} } }; +static const odp_crypto_auth_capability_t auth_capa_aes_gmac[] = { +{.digest_len = 16, .key_len = 16, .aad_len = {.min = 0, .max = 0, .inc = 0} } }; + typedef struct odp_crypto_global_s odp_crypto_global_t; struct odp_crypto_global_s { @@ -235,6 +240,33 @@ odp_crypto_alg_err_t auth_check(odp_packet_t pkt, } static +int internal_aad(EVP_CIPHER_CTX *ctx, + odp_packet_t pkt, + const odp_crypto_packet_op_param_t *param) +{ + uint32_t offset = param->auth_range.offset; + uint32_t len = param->auth_range.length; + int dummy_len; + int ret; + + ODP_ASSERT(offset + len <= odp_packet_len(pkt)); + + while (len > 0) { + uint32_t seglen = 0; /* GCC */ + void *mapaddr = odp_packet_offset(pkt, offset, &seglen, NULL); + uint32_t maclen = len > seglen ? seglen : len; + + EVP_EncryptUpdate(ctx, NULL, &dummy_len, mapaddr, maclen); + offset += maclen; + len -= maclen; + } + + ret = EVP_EncryptFinal_ex(ctx, NULL, &dummy_len); + + return ret; +} + +static int internal_encrypt(EVP_CIPHER_CTX *ctx, odp_packet_t pkt, const odp_crypto_packet_op_param_t *param) @@ -555,6 +587,106 @@ static int process_aes_gcm_param(odp_crypto_generic_session_t *session, return 0; } +static +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; + void *iv_ptr; + uint8_t block[EVP_MAX_MD_SIZE]; + int ret; + + if (param->override_iv_ptr) + iv_ptr = param->override_iv_ptr; + else if (session->p.iv.data) + iv_ptr = session->cipher.iv_data; + else + 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); + EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv_ptr); + EVP_CIPHER_CTX_set_padding(ctx, 0); + + ret = internal_aad(ctx, pkt, param); + + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, + session->p.auth_digest_len, block); + 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; +} + +static +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; + void *iv_ptr; + uint8_t block[EVP_MAX_MD_SIZE]; + int ret; + + if (param->override_iv_ptr) + iv_ptr = param->override_iv_ptr; + else if (session->p.iv.data) + iv_ptr = session->cipher.iv_data; + else + 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); + 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); + EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, + session->p.auth_digest_len, block); + + 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; +} + +static int process_aes_gmac_param(odp_crypto_generic_session_t *session, + const EVP_CIPHER *cipher) +{ + /* Verify Key len is valid */ + if ((uint32_t)EVP_CIPHER_key_length(cipher) != + session->p.auth_key.length) + return -1; + + memcpy(session->auth.key, session->p.auth_key.data, + session->p.auth_key.length); + + session->auth.evp_cipher = cipher; + + /* Set function */ + if (ODP_CRYPTO_OP_ENCODE == session->p.op) + session->auth.func = aes_gmac_gen; + else + session->auth.func = aes_gmac_check; + + return 0; +} + static int process_auth_param(odp_crypto_generic_session_t *session, uint32_t key_length, const EVP_MD *evp_md) @@ -600,6 +732,7 @@ int odp_crypto_capability(odp_crypto_capability_t *capa) capa->auths.bit.sha256_hmac = 1; capa->auths.bit.sha512_hmac = 1; capa->auths.bit.aes_gcm = 1; + capa->auths.bit.aes_gmac = 1; #if ODP_DEPRECATED_API capa->ciphers.bit.aes128_cbc = 1; @@ -687,6 +820,10 @@ int odp_crypto_auth_capability(odp_auth_alg_t auth, src = auth_capa_aes_gcm; num = sizeof(auth_capa_aes_gcm) / size; break; + case ODP_AUTH_ALG_AES_GMAC: + src = auth_capa_aes_gmac; + num = sizeof(auth_capa_aes_gmac) / size; + break; default: return -1; } @@ -849,6 +986,16 @@ odp_crypto_session_create(odp_crypto_session_param_t *param, rc = -1; } break; + case ODP_AUTH_ALG_AES_GMAC: + if (param->auth_key.length == 16) + rc = process_aes_gmac_param(session, EVP_aes_128_gcm()); + else if (param->auth_key.length == 24) + rc = process_aes_gmac_param(session, EVP_aes_192_gcm()); + else if (param->auth_key.length == 32) + rc = process_aes_gmac_param(session, EVP_aes_256_gcm()); + else + rc = -1; + break; default: rc = -1; }