Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp4599104ybi; Tue, 30 Jul 2019 05:00:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqyMM6iX1ru3OsVuMIOK88IH28CIbzsgRrHpZ6Sy+bOLqCVjiNMuDYqqVoVAAUX7puPu8dHh X-Received: by 2002:a17:902:d715:: with SMTP id w21mr77058990ply.261.1564488006087; Tue, 30 Jul 2019 05:00:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564488006; cv=none; d=google.com; s=arc-20160816; b=qEd/ZdJ25GypkdgzcqTX8r1QSde/sMmNFEvmDCnKZADzubbDi0qgyUQQk9csNyt+6s hsy0MDeUA/dd0JxJY25kU7fUI+Irhe+nzEolqkDDqyO6NWIkcEx30CNHss3wiiombAFV a5fdNOS/bKYCWO1CRWTulgve4zhkvMgkCo2UTk7exirTd/DakWTZbn1KQ/K9m9t3OVDi PBslE18PEdbDyNQIHfZnI7mfw/Tly4XdmoATVzTej3rFVfEee52YgIIZni/vBjsJqVGb R6EB0+InATIoWc4zbHEjnLSAv69Vb6X866bIX0vnw6WgP+x1cy8V/D6/q3PvUKho8L+j yMyQ== 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=sqlfyKjlDXZV3DwbPy2A8wN4POP9DRHVu+cg2E135DY=; b=VYDxICrpduooLVoRtQmgkIDwn9OIzlRdLaOH2MPVt9p05pCtc7IvW9kJiMbhPrIIuU jmgSygnzh7cJ0d1vJ9EWfqJgV+XQhtStE1m0sVSEuGD0Wvw3gFwZnh/lpSy2We4kwtHU 7re/rD7TitGpYTshIVpo9p+5HI8j/DM/al6EBpL5yr08dIORvMulNFGAhLkEHW6EY7iM lI/Ks4aTUKLcRnGxbxG0VydKVHUOvn8OLNVqChKS7+Cq0n3b2Tjzu3C7kFoUAJc/BZA4 Pn6oIET4T1jqCxekOCmolBR/cMUf1U5DYFHiuE5+ZM17HclJAukkO91WfgJ/F9pf5+FT p12A== 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 w10si7851001pgs.529.2019.07.30.04.59.52; Tue, 30 Jul 2019 05:00:06 -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 S1729411AbfG3Kdw (ORCPT + 99 others); Tue, 30 Jul 2019 06:33:52 -0400 Received: from inva021.nxp.com ([92.121.34.21]:39938 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729536AbfG3Kdv (ORCPT ); Tue, 30 Jul 2019 06:33:51 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 6363420064C; Tue, 30 Jul 2019 12:33:48 +0200 (CEST) Received: from inva024.eu-rdc02.nxp.com (inva024.eu-rdc02.nxp.com [134.27.226.22]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 561CF200661; Tue, 30 Jul 2019 12:33:48 +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 00F13204D6; Tue, 30 Jul 2019 12:33:47 +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 v2 1/2] crypto: gcm - helper functions for assoclen/authsize check Date: Tue, 30 Jul 2019 13:33:43 +0300 Message-Id: <1564482824-26581-2-git-send-email-iuliana.prodan@nxp.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1564482824-26581-1-git-send-email-iuliana.prodan@nxp.com> References: <1564482824-26581-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..0a2f21e 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