Received: by 2002:a05:7412:ba23:b0:fa:4c10:6cad with SMTP id jp35csp2201434rdb; Sun, 21 Jan 2024 11:49:18 -0800 (PST) X-Google-Smtp-Source: AGHT+IFkALXzy6kGhqNsQzlerV/ApcwD1By5GTHttl5XToQ76xJfA/BgHp3dMpchFIFJEKdjykoT X-Received: by 2002:a05:6214:2608:b0:67f:a0a6:4cfa with SMTP id gu8-20020a056214260800b0067fa0a64cfamr5112568qvb.112.1705866557743; Sun, 21 Jan 2024 11:49:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705866557; cv=pass; d=google.com; s=arc-20160816; b=IeTkNkbOfVe6n+ILN4bLkLlnJVIGYhW9nbKMZDnT8ze4R5A+rqadbwVD56GbSdw1Pf HSFTd4Acpkp5fxYST67QNjbTQHEb1dr27xYMLzMFfZ+UB5C+xfJoEfweUum2Qe8UJADD pYB5wQbnWmEHmVqfjKcjs6MB9hI2ZdrU4SWa22DWCMNKb6/7nBJa3YR56wzuipFidNMq Yt48O6dxjm96Q2dkSxhmcjselJ03fdnPOgsbrMx8rQnp5AouHzl9zyZ0l1bzdyXl/HHw O9tRd3NeM8a8Wi0ewDGEx0Sa48sglviPi8iEmxF1YnuBCMy6HXX+4I2OceKc0ADplVvh LCGA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=dM1ESK56KVU61kC4b7LibKAzfXd3yisiHn9dOSDVSU0=; fh=3rCpEkNiu0V72hzuVHhV3IWPrMk1LCkIPHU5FUQjHq8=; b=CCCsD5Va+R+oeel9I7h9yvHiHXJbK7aNAL2qcTanQQe/Tt93PmDOKjDrYoZi+Rs/tP QKOnBgqWUAi+XStvtY57Is3Y41im7WAX3OObgHSqNWwiZAJSmYwspqaS7iqleNgFs24G BSNIXqvpJzlV7X1qaH4lKezqX57zaZi/8zToJc5Q0cONLNyyTdngsDKjhRuooqrJBkiQ eGbMZmk8A32lQLuVu856GFqKPDTmO9O3iTSxFIQC0zmC45DI6Wmm5DG2+NySi89zLYEv i81lY3KWBcZgXz575MtcpQyvLpPdVtQLtr2im5ooR7G7Uhsyv/RiKhhUsOP0flC6IjQT 6dJw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@jvdsn.com header.s=mail header.b=vXvqKi5x; arc=pass (i=1 spf=pass spfdomain=jvdsn.com dkim=pass dkdomain=jvdsn.com dmarc=pass fromdomain=jvdsn.com); spf=pass (google.com: domain of linux-crypto+bounces-1520-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-crypto+bounces-1520-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=jvdsn.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id z14-20020a0cda8e000000b0067f483722c0si4079156qvj.208.2024.01.21.11.49.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Jan 2024 11:49:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto+bounces-1520-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@jvdsn.com header.s=mail header.b=vXvqKi5x; arc=pass (i=1 spf=pass spfdomain=jvdsn.com dkim=pass dkdomain=jvdsn.com dmarc=pass fromdomain=jvdsn.com); spf=pass (google.com: domain of linux-crypto+bounces-1520-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-crypto+bounces-1520-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=jvdsn.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 6AAF41C20BFB for ; Sun, 21 Jan 2024 19:49:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 02F3138398; Sun, 21 Jan 2024 19:49:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=jvdsn.com header.i=@jvdsn.com header.b="vXvqKi5x" X-Original-To: linux-crypto@vger.kernel.org Received: from smtp.jvdsn.com (smtp.jvdsn.com [129.153.194.31]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5769F3838D for ; Sun, 21 Jan 2024 19:49:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=129.153.194.31 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705866553; cv=none; b=rnmYYV4+EBNcB+D+Ac/dklZJHziNAVGgtb2hbpPd+8xtqvAJsWFyRJXeKd3S9sou8FrzKhrPU5MAPFhaJ7eD3f8zlayazaMQIVzvs10ooS8xXHhb9eknNMsoo8M5OCW3PX3tdq5MxY9GpWVUL/yodBeWlVtrK6hNj7sBDf3D4U0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705866553; c=relaxed/simple; bh=Io8CZinL8nPZ8HMB8amdcvs5wzCPD4xojiPD9RtJ8Ac=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=qqA1ilNeStl+k2O+Nb2nxLN+6sY09WT3aWu7fClnY3gmeIOir5swjIqtL6pW9bpE50k9k9JndETbknbv+jrkphs4kWEdMupqtF8p2Zf42k+AA4OHelJTXKR1PZ66XtvjzWyZdD0g2fDM8xw2PYM+M1qt1d9/1RYQxZDnEbPX5+U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=jvdsn.com; spf=pass smtp.mailfrom=jvdsn.com; dkim=pass (2048-bit key) header.d=jvdsn.com header.i=@jvdsn.com header.b=vXvqKi5x; arc=none smtp.client-ip=129.153.194.31 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=jvdsn.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=jvdsn.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=jvdsn.com; s=mail; t=1705866551; bh=Io8CZinL8nPZ8HMB8amdcvs5wzCPD4xojiPD9RtJ8Ac=; h=From:To:Cc:Subject:Date; b=vXvqKi5xA75THLzKTY73FRgEos5YiEJ8NtqkXA6FsfZPIUv9Fk10mw+Fp0VdWKTvy W0/8Xtks9Jb2qdNu8Eytth14zbPRNc6bF/PV+FKEjB52zBDmHIGEeemioSBOCvrJRn AUwBFkzF8rICSZBBduYeY3OjFTWkEnXZYeF1ydddfL1qnI9pBWOTPf6LAbPlVRwdDy m3m+ELjGrOCVJnxMf4VNfrPO9npIiRba8K+7A3+9Vmjoqjex8Gz474oCa238ZPy65G RlvAxuyhyfBZ4e+aZSec5+z7+Zq2QsFJmWufprBSAgH+24xeUSjFfKCN/jhES6e3+i vCejpwwfgcgJg== From: Joachim Vandersmissen To: linux-crypto@vger.kernel.org, Herbert Xu Cc: Joachim Vandersmissen Subject: [PATCH] crypto: rsa - restrict plaintext/ciphertext values more in FIPS mode Date: Sun, 21 Jan 2024 13:49:00 -0600 Message-ID: <20240121194901.344206-1-git@jvdsn.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SP 800-56Br2, Section 7.1.1 [1] specifies that: 1. If m does not satisfy 1 < m < (n – 1), output an indication that m is out of range, and exit without further processing. Similarly, Section 7.1.2 of the same standard specifies that: 1. If the ciphertext c does not satisfy 1 < c < (n – 1), output an indication that the ciphertext is out of range, and exit without further processing. [1] https://doi.org/10.6028/NIST.SP.800-56Br2 Signed-off-by: Joachim Vandersmissen --- crypto/rsa.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/crypto/rsa.c b/crypto/rsa.c index b9cd11fb7d36..b5c67e6f8774 100644 --- a/crypto/rsa.c +++ b/crypto/rsa.c @@ -24,12 +24,36 @@ struct rsa_mpi_key { MPI qinv; }; +static int rsa_check_payload_fips(MPI x, MPI n) +{ + MPI n1; + + if (mpi_cmp_ui(x, 1) <= 0) + return -EINVAL; + + n1 = mpi_alloc(0); + if (!n1) + return -ENOMEM; + + if (mpi_sub_ui(n1, n, 1) || mpi_cmp(x, n1) >= 0) { + mpi_free(n1); + return -EINVAL; + } + + mpi_free(n1); + return 0; +} + /* * RSAEP function [RFC3447 sec 5.1.1] * c = m^e mod n; */ static int _rsa_enc(const struct rsa_mpi_key *key, MPI c, MPI m) { + /* For FIPS, SP 800-56Br2, Section 7.1.1 requires 1 < m < n - 1 */ + if (fips_enabled && rsa_check_payload_fips(m, key->n)) + return -EINVAL; + /* (1) Validate 0 <= m < n */ if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0) return -EINVAL; @@ -50,6 +74,11 @@ static int _rsa_dec_crt(const struct rsa_mpi_key *key, MPI m_or_m1_or_h, MPI c) MPI m2, m12_or_qh; int ret = -ENOMEM; + /* For FIPS, SP 800-56Br2, Section 7.1.2 requires 1 < c < n - 1 */ + if (fips_enabled && rsa_check_payload_fips(c, key->n)) + return -EINVAL; + + /* (1) Validate 0 <= c < n */ if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0) return -EINVAL; -- 2.43.0