From patchwork Tue Jan 30 17: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: 126294 Delivered-To: patch@linaro.org Received: by 10.46.84.92 with SMTP id y28csp3550523ljd; Tue, 30 Jan 2018 09:19:28 -0800 (PST) X-Google-Smtp-Source: AH8x225sCRq9iJ1KPxp7Zpd6JjFYohkX2/VDBrrLDIB2RfzZB+qy/3T5PWpuPHpOMBnaXGzRCvzp X-Received: by 10.55.125.197 with SMTP id y188mr34761140qkc.111.1517332768026; Tue, 30 Jan 2018 09:19:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517332768; cv=none; d=google.com; s=arc-20160816; b=G0WGyKjYlUGTvgOO18avxEFu0QZUA0XSBChPuBwy5PJ4xA/YHaIiVZyb6wXbGFgkxb pee36o1o3fI+wAWsAdnYqqFcvUOw0pnW19XT02l5w1IphuYYL1QjfSJMt/OfkbMl0iB6 nZXa83qQHBISC0inLFa2tO4z74IKX3USsCN7ZEKVWummIDrYDZxZ2ukiOavM10xYA3ra tEQF7CNNySjTL/r2z4osBkLT8J12ghSiDQf1VgoIkSCk0widl2QZPg6MEeYLitDFjSob V0gSUZJqQX6eG+nI5Q+WlG7t9nj8xW/A5txHCzjn+c+Z7W1WrCZpWFCsazV2YVLzYtZS /cdg== 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=RGSPaxvFYK5AgT5fmjzViIpvtzACCntINzbOJGDS2SE=; b=pgDvUOw69pcLAdSfpKYRnMBn9wZde3HfD1kc/Ru2YrijsGf8MRUpxd1JvdekLdVjy1 iqsrExJPFK0bcu3PHxODdXpdV7S5gu2sGTQI8qKAY1uyiY1CCVHiWq0YY8htaGstApFA IeOrXAW5zHzEHSd7OuPxmBCcYh2GMvvmAfx4okpm7+pwV0xWItd8aPygufoar5PvdonB uHGW+eiGCBj6k533cF/DB7vl+ivklYI2xA7qWjNV/4XGD5MBC97ay/Uz2+QXoQXijDyq 465p+EStCPB4TrdwIIYD7v16v6uarrjnxo42Zq5W3TLOwbOKSCGdCpUTMGn/+E7CP6Mi 3O4A== 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 o33si66490qta.203.2018.01.30.09.19.27; Tue, 30 Jan 2018 09:19:27 -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 B2BDE6176D; Tue, 30 Jan 2018 17:19:27 +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 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 B926C617A1; Tue, 30 Jan 2018 17:03:59 +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 82C7F609C7; Tue, 30 Jan 2018 17:03:14 +0000 (UTC) Received: from forward105o.mail.yandex.net (forward105o.mail.yandex.net [37.140.190.183]) by lists.linaro.org (Postfix) with ESMTPS id 67EEB60881 for ; Tue, 30 Jan 2018 17:00:44 +0000 (UTC) Received: from mxback11j.mail.yandex.net (mxback11j.mail.yandex.net [IPv6:2a02:6b8:0:1619::84]) by forward105o.mail.yandex.net (Yandex) with ESMTP id 39AAA4444E9B for ; Tue, 30 Jan 2018 20:00:43 +0300 (MSK) Received: from smtp1p.mail.yandex.net (smtp1p.mail.yandex.net [2a02:6b8:0:1472:2741:0:8b6:6]) by mxback11j.mail.yandex.net (nwsmtp/Yandex) with ESMTP id wKBXmBACom-0hjGn4xr; Tue, 30 Jan 2018 20:00:43 +0300 Received: by smtp1p.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id 3C5hSSv4xg-0gre5PSt; Tue, 30 Jan 2018 20:00:42 +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: Tue, 30 Jan 2018 20:00:17 +0300 Message-Id: <1517331619-18755-14-git-send-email-odpbot@yandex.ru> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1517331619-18755-1-git-send-email-odpbot@yandex.ru> References: <1517331619-18755-1-git-send-email-odpbot@yandex.ru> Github-pr-num: 434 Subject: [lng-odp] [PATCH API-NEXT v4 13/15] linux-gen: crypto: implement AES-CMAC 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 434 (lumag:crypto-upd) ** https://github.com/Linaro/odp/pull/434 ** Patch: https://github.com/Linaro/odp/pull/434.patch ** Base sha: 5718327018debbb02aacb464493504c95fbe57a3 ** Merge commit sha: c5d9389a8d3b1a532e290aff4508756a920173ee **/ platform/linux-generic/odp_crypto.c | 147 ++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/platform/linux-generic/odp_crypto.c b/platform/linux-generic/odp_crypto.c index faa415cd0..5579ade5b 100644 --- a/platform/linux-generic/odp_crypto.c +++ b/platform/linux-generic/odp_crypto.c @@ -25,6 +25,7 @@ #include #include +#include #include #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(OPENSSL_NO_POLY1305) @@ -108,6 +109,14 @@ 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}, .iv_len = 12 } }; +static const odp_crypto_auth_capability_t auth_capa_aes_cmac[] = { +{.digest_len = 12, .key_len = 16, .aad_len = {.min = 0, .max = 0, .inc = 0} }, +{.digest_len = 16, .key_len = 16, .aad_len = {.min = 0, .max = 0, .inc = 0} }, +{.digest_len = 12, .key_len = 24, .aad_len = {.min = 0, .max = 0, .inc = 0} }, +{.digest_len = 16, .key_len = 24, .aad_len = {.min = 0, .max = 0, .inc = 0} }, +{.digest_len = 12, .key_len = 32, .aad_len = {.min = 0, .max = 0, .inc = 0} }, +{.digest_len = 16, .key_len = 32, .aad_len = {.min = 0, .max = 0, .inc = 0} } }; + #if _ODP_HAVE_CHACHA20_POLY1305 static const odp_crypto_auth_capability_t auth_capa_chacha20_poly1305[] = { {.digest_len = 16, .key_len = 0, .aad_len = {.min = 8, .max = 12, .inc = 4} } }; @@ -177,6 +186,7 @@ static odp_crypto_global_t *global; typedef struct crypto_local_t { HMAC_CTX *hmac_ctx[MAX_SESSIONS]; + CMAC_CTX *cmac_ctx[MAX_SESSIONS]; EVP_CIPHER_CTX *cipher_ctx[MAX_SESSIONS]; EVP_CIPHER_CTX *mac_cipher_ctx[MAX_SESSIONS]; uint8_t *ctx_valid; @@ -344,6 +354,93 @@ odp_crypto_alg_err_t auth_hmac_check(odp_packet_t pkt, return ODP_CRYPTO_ALG_ERR_NONE; } +static void +auth_cmac_init(odp_crypto_generic_session_t *session) +{ + CMAC_CTX *ctx = local.cmac_ctx[session->idx]; + + CMAC_Init(ctx, + session->auth.key, + session->p.auth_key.length, + session->auth.evp_cipher, + NULL); +} + +static +void packet_cmac(odp_packet_t pkt, + const odp_crypto_packet_op_param_t *param, + odp_crypto_generic_session_t *session, + uint8_t *hash) +{ + CMAC_CTX *ctx = local.cmac_ctx[session->idx]; + uint32_t offset = param->auth_range.offset; + uint32_t len = param->auth_range.length; + size_t outlen; + + ODP_ASSERT(offset + len <= odp_packet_len(pkt)); + + /* Reinitialize CMAC calculation without resetting the key */ + CMAC_Init(ctx, NULL, 0, NULL, NULL); + + while (len > 0) { + uint32_t seglen = 0; /* GCC */ + void *mapaddr = odp_packet_offset(pkt, offset, &seglen, NULL); + uint32_t maclen = len > seglen ? seglen : len; + + CMAC_Update(ctx, mapaddr, maclen); + offset += maclen; + len -= maclen; + } + + CMAC_Final(ctx, hash, &outlen); +} + +static +odp_crypto_alg_err_t auth_cmac_gen(odp_packet_t pkt, + const odp_crypto_packet_op_param_t *param, + odp_crypto_generic_session_t *session) +{ + uint8_t hash[EVP_MAX_MD_SIZE]; + + /* Hash it */ + packet_cmac(pkt, param, session, hash); + + /* Copy to the output location */ + odp_packet_copy_from_mem(pkt, + param->hash_result_offset, + session->p.auth_digest_len, + hash); + + return ODP_CRYPTO_ALG_ERR_NONE; +} + +static +odp_crypto_alg_err_t auth_cmac_check(odp_packet_t pkt, + const odp_crypto_packet_op_param_t *param, + odp_crypto_generic_session_t *session) +{ + uint32_t bytes = session->p.auth_digest_len; + uint8_t hash_in[EVP_MAX_MD_SIZE]; + uint8_t hash_out[EVP_MAX_MD_SIZE]; + + /* Copy current value out and clear it before authentication */ + odp_packet_copy_to_mem(pkt, param->hash_result_offset, + bytes, hash_in); + + _odp_packet_set_data(pkt, param->hash_result_offset, + 0, bytes); + + /* Hash it */ + packet_cmac(pkt, param, session, hash_out); + + /* Verify match */ + if (0 != memcmp(hash_in, hash_out, bytes)) + return ODP_CRYPTO_ALG_ERR_ICV_CHECK; + + /* Matched */ + return ODP_CRYPTO_ALG_ERR_NONE; +} + static int internal_aad(EVP_CIPHER_CTX *ctx, odp_packet_t pkt, @@ -1001,6 +1098,35 @@ static int process_auth_hmac_param(odp_crypto_generic_session_t *session, return 0; } +static int process_auth_cmac_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; + + /* Set function */ + if (ODP_CRYPTO_OP_ENCODE == session->p.op) + session->auth.func = auth_cmac_gen; + else + session->auth.func = auth_cmac_check; + session->auth.init = auth_cmac_init; + + session->auth.evp_cipher = cipher; + + /* Number of valid bytes */ + if (session->p.auth_digest_len < + (unsigned)EVP_CIPHER_block_size(cipher) / 2) + return -1; + + /* Convert keys */ + memcpy(session->auth.key, session->p.auth_key.data, + session->p.auth_key.length); + + return 0; +} + int odp_crypto_capability(odp_crypto_capability_t *capa) { if (NULL == capa) @@ -1030,6 +1156,7 @@ int odp_crypto_capability(odp_crypto_capability_t *capa) capa->auths.bit.aes_gcm = 1; capa->auths.bit.aes_ccm = 1; capa->auths.bit.aes_gmac = 1; + capa->auths.bit.aes_cmac = 1; #if _ODP_HAVE_CHACHA20_POLY1305 capa->auths.bit.chacha20_poly1305 = 1; #endif @@ -1138,6 +1265,10 @@ int odp_crypto_auth_capability(odp_auth_alg_t auth, src = auth_capa_aes_ccm; num = sizeof(auth_capa_aes_ccm) / size; break; + case ODP_AUTH_ALG_AES_CMAC: + src = auth_capa_aes_cmac; + num = sizeof(auth_capa_aes_cmac) / size; + break; #if _ODP_HAVE_CHACHA20_POLY1305 case ODP_AUTH_ALG_CHACHA20_POLY1305: src = auth_capa_chacha20_poly1305; @@ -1374,6 +1505,19 @@ odp_crypto_session_create(odp_crypto_session_param_t *param, rc = -1; } break; + case ODP_AUTH_ALG_AES_CMAC: + if (param->auth_key.length == 16) + rc = process_auth_cmac_param(session, + EVP_aes_128_cbc()); + else if (param->auth_key.length == 24) + rc = process_auth_cmac_param(session, + EVP_aes_192_cbc()); + else if (param->auth_key.length == 32) + rc = process_auth_cmac_param(session, + EVP_aes_256_cbc()); + else + rc = -1; + break; #if _ODP_HAVE_CHACHA20_POLY1305 case ODP_AUTH_ALG_CHACHA20_POLY1305: /* ChaCha20_Poly1305 requires to do both auth and @@ -1561,6 +1705,7 @@ int _odp_crypto_init_local(void) for (i = 0; i < MAX_SESSIONS; i++) { local.hmac_ctx[i] = HMAC_CTX_new(); + local.cmac_ctx[i] = CMAC_CTX_new(); local.cipher_ctx[i] = EVP_CIPHER_CTX_new(); local.mac_cipher_ctx[i] = EVP_CIPHER_CTX_new(); @@ -1584,6 +1729,8 @@ int _odp_crypto_term_local(void) unsigned i; for (i = 0; i < MAX_SESSIONS; i++) { + if (local.cmac_ctx[i] != NULL) + CMAC_CTX_free(local.cmac_ctx[i]); if (local.hmac_ctx[i] != NULL) HMAC_CTX_free(local.hmac_ctx[i]); if (local.cipher_ctx[i] != NULL)