Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp5958489ybi; Wed, 31 Jul 2019 06:06:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqyeeMrelWvWSbjVhSZ92EJdKmJrdoz74X4N1nBhqNBq7PC3M7iC8VorHE2B1m+WLaj0jZzY X-Received: by 2002:a17:902:694a:: with SMTP id k10mr119296043plt.255.1564578401752; Wed, 31 Jul 2019 06:06:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564578401; cv=none; d=google.com; s=arc-20160816; b=XuP3xIQFjtg/ONsunu/hbUxglKymjP7kes9pii1y+F22upSdulpr7z3hobby/7qF0s ZonmPshXMdGnhkUhHDzi6GU3ROpeHw4TQcKoe2StMDje4lsB13mI/RGCu1SOQfgUkILB gqA8pEMvRJwjuuLmZZJcWrl4z5AOSbEMRlnkCfd0x+iyeNSPZknMBwalieZ5T/PpIWSX ZQf/H4h77z7u/yVspuLIhcfZ1/vN0mbWUJJ4r0FNgN7sBL/dBDTxkG9NYT835+v1ifjw TAUlFMS+kqn6gxPxWFOVRzqOaweljd5eoAfAKT4oXAlrWSzJhpHFBakJUEwzDw85cqPR 2/uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=c0Cqy/hS8kHuFHQ70Sf7iLOZCuZJCRXsY8/iLKl15eY=; b=WflK/55BBtTGqtD3T1XTVP9sVcwCUd8SFjoscrtnTk9JuxWx8A6hnGwxCuYLbxq61l AIEMPntuMNAMvSeHa9F7Vo++gASVSqdwnyhkxSvl38GsoDzJbWrVxFT2w0z3rbGBR1VG ea9hwSlpHxC63J8VAI3Dt4qMILoo0j0ovag9O877Dmn/0zXNm0u8qHW+n6PIi6VthWOS I+jgxsyY4iRLLFBGc7Gv+2dPHjEupUBxLo9rIk5330QDMq/5ySeYIHbQ94pziA53MaeP 5Dqu6SIk79S0OEwlALMo8DBw93oENsfrlaZ9Skc4syAzgr8KvO3eCBsLlgdu67oTfZYI KZ4g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b5si31394222pfd.273.2019.07.31.06.06.19; Wed, 31 Jul 2019 06:06:41 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-crypto-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387865AbfGaNGI (ORCPT + 99 others); Wed, 31 Jul 2019 09:06:08 -0400 Received: from inva020.nxp.com ([92.121.34.13]:33810 "EHLO inva020.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387726AbfGaNGH (ORCPT ); Wed, 31 Jul 2019 09:06:07 -0400 Received: from inva020.nxp.com (localhost [127.0.0.1]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id C0C8D1A040D; Wed, 31 Jul 2019 15:06:04 +0200 (CEST) Received: from inva024.eu-rdc02.nxp.com (inva024.eu-rdc02.nxp.com [134.27.226.22]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id B3DB01A01AC; Wed, 31 Jul 2019 15:06:04 +0200 (CEST) Received: from lorenz.ea.freescale.net (lorenz.ea.freescale.net [10.171.71.5]) by inva024.eu-rdc02.nxp.com (Postfix) with ESMTP id 65C9B205F3; Wed, 31 Jul 2019 15:06:04 +0200 (CEST) From: Iuliana Prodan To: Herbert Xu , "David S. Miller" Cc: linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, linux-imx Subject: [PATCH v3 1/2] crypto: gcm - helper functions for assoclen/authsize check Date: Wed, 31 Jul 2019 16:05:54 +0300 Message-Id: <1564578355-9639-2-git-send-email-iuliana.prodan@nxp.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1564578355-9639-1-git-send-email-iuliana.prodan@nxp.com> References: <1564578355-9639-1-git-send-email-iuliana.prodan@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Added inline helper functions to check authsize and assoclen for gcm, rfc4106 and rfc4543. These are used in the generic implementation of gcm, rfc4106 and rfc4543. Signed-off-by: Iuliana Prodan --- crypto/gcm.c | 41 ++++++++++++++------------------------- include/crypto/gcm.h | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 26 deletions(-) diff --git a/crypto/gcm.c b/crypto/gcm.c index f254e2d..2f3b50f 100644 --- a/crypto/gcm.c +++ b/crypto/gcm.c @@ -152,20 +152,7 @@ static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key, static int crypto_gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize) { - switch (authsize) { - case 4: - case 8: - case 12: - case 13: - case 14: - case 15: - case 16: - break; - default: - return -EINVAL; - } - - return 0; + return crypto_gcm_check_authsize(authsize); } static void crypto_gcm_init_common(struct aead_request *req) @@ -762,15 +749,11 @@ static int crypto_rfc4106_setauthsize(struct crypto_aead *parent, unsigned int authsize) { struct crypto_rfc4106_ctx *ctx = crypto_aead_ctx(parent); + int err; - switch (authsize) { - case 8: - case 12: - case 16: - break; - default: - return -EINVAL; - } + err = crypto_rfc4106_check_authsize(authsize); + if (err) + return err; return crypto_aead_setauthsize(ctx->child, authsize); } @@ -818,8 +801,11 @@ static struct aead_request *crypto_rfc4106_crypt(struct aead_request *req) static int crypto_rfc4106_encrypt(struct aead_request *req) { - if (req->assoclen != 16 && req->assoclen != 20) - return -EINVAL; + int err; + + err = crypto_ipsec_check_assoclen(req->assoclen); + if (err) + return err; req = crypto_rfc4106_crypt(req); @@ -828,8 +814,11 @@ static int crypto_rfc4106_encrypt(struct aead_request *req) static int crypto_rfc4106_decrypt(struct aead_request *req) { - if (req->assoclen != 16 && req->assoclen != 20) - return -EINVAL; + int err; + + err = crypto_ipsec_check_assoclen(req->assoclen); + if (err) + return err; req = crypto_rfc4106_crypt(req); diff --git a/include/crypto/gcm.h b/include/crypto/gcm.h index c50e057..9d7eff0 100644 --- a/include/crypto/gcm.h +++ b/include/crypto/gcm.h @@ -1,8 +1,63 @@ #ifndef _CRYPTO_GCM_H #define _CRYPTO_GCM_H +#include + #define GCM_AES_IV_SIZE 12 #define GCM_RFC4106_IV_SIZE 8 #define GCM_RFC4543_IV_SIZE 8 +/* + * validate authentication tag for GCM + */ +static inline int crypto_gcm_check_authsize(unsigned int authsize) +{ + switch (authsize) { + case 4: + case 8: + case 12: + case 13: + case 14: + case 15: + case 16: + break; + default: + return -EINVAL; + } + + return 0; +} + +/* + * validate authentication tag for RFC4106 + */ +static inline int crypto_rfc4106_check_authsize(unsigned int authsize) +{ + switch (authsize) { + case 8: + case 12: + case 16: + break; + default: + return -EINVAL; + } + + return 0; +} + +/* + * validate assoclen for RFC4106/RFC4543 + */ +static inline int crypto_ipsec_check_assoclen(unsigned int assoclen) +{ + switch (assoclen) { + case 16: + case 20: + break; + default: + return -EINVAL; + } + + return 0; +} #endif -- 2.1.0