Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp1284490rdb; Fri, 2 Feb 2024 23:29:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IHjEzBeIbQ5x349t2T0PEOq4vziVoCyVtUandqBDYfU9/IytkhscJl7Czjg0A3Y5X1VZkM1 X-Received: by 2002:ac8:5e49:0:b0:42b:e5e0:5db with SMTP id i9-20020ac85e49000000b0042be5e005dbmr685877qtx.11.1706945383094; Fri, 02 Feb 2024 23:29:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706945383; cv=pass; d=google.com; s=arc-20160816; b=aX9rDuTGljIrExq8lv/3vwJajuyBWQiabX+nJyCz43Azn049mW1bBwLWrG5pPikQC8 1wifEKqLcU/KSb91VErmunlBc/BvhpZ2/4VRAY+0JOj/6Tgr2aTik5wWoHBsHtD94NLt 5aS9uWUaIWS5Irf8sCwNNjkiQ9Ss0EMqxSFMP0f709KZubStXY/+ysTx78a37PNvwLMt e310QtBmYd5tOVFoBRb/h8tXwMWFol5X/pdfqCjMK/glXiFDonrl490BdRbZy+fh750y 59+YM/ir1fjNGXQYO4zUQd/vggfQmCmPzat/WR8hIsep4/8sbv1/R/1Gp5lfK/1k6TJf Rg7g== 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=BlrFHP6S/gsoKwB3YWpP90rupX3Z8bOBhVDD7g6wOeI=; fh=FaIpxpnuxuz/T+gF+WfMr84AkiP0EdJdrIrDbh84G14=; b=uQQHf3tyIvO2OwYzwjHuRSayYob5o5ZczNPFHLL5hQ0ZMX3dqrmyNzhnSD511x9REY zi+nytPw3mzRQioIf2gvNPlTk4EHehcrXi0i8+gMCz2oZqWlz7c8g2l7IyUyC/7v4f6f DFWktk967Ea4A/62HCGjH0enhgsmtQiKEZX+c45Lqv0awfRGz+kPZwIL2VGND8exvQ4x 8KPMuGlg5NrffzsOn3CZES7j3faLfS6llDu8hC7xySIwl8MajH0ESCLMftK12EajZsEG HZC1ipy0mkQjaCexELVDDs89tniR6OmJE3SW6ox3LfM8pu75LDQc3IUbWOGjQvpITSsf jVvA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@jvdsn.com header.s=mail header.b=O9Ki0kuH; 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-1824-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-crypto+bounces-1824-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=jvdsn.com X-Forwarded-Encrypted: i=1; AJvYcCXy/AGYWWZfZm1VzYq3SUpIJ2PZ8djqw9YpdhQUdxsNKA8pSwWJ9Dd2aKpUFr7E/WlZRI/h6CutK74Lkd13ipzG/3UJjNwUxyyZzXmS2Q== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id h26-20020ac846da000000b0042c00df5929si3226300qto.124.2024.02.02.23.29.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Feb 2024 23:29:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto+bounces-1824-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=O9Ki0kuH; 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-1824-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-crypto+bounces-1824-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 8902A1C26A35 for ; Sat, 3 Feb 2024 07:29:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 055A94EB2A; Sat, 3 Feb 2024 07:29:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=jvdsn.com header.i=@jvdsn.com header.b="O9Ki0kuH" 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 21DB34EB25 for ; Sat, 3 Feb 2024 07:29:37 +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=1706945379; cv=none; b=UzoiOeT8QGbkHNUHynmZU9SGhsx/q0MQJB75ics6C+D+He8VwyoKcW+tmLKpM0gbbuauFNaVtJtYcNRMeklpPcNsPn8FloLCaNqfWhtg8vH6vwGe0e1CXkX58NEuzTmlysMXmr7Fy9w5ag2aIpm8bUCNquuBNuPn+L9S/+52JNw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706945379; c=relaxed/simple; bh=PTOpCCEM3IxbhvuSFFypmEAY+7vzYF1umsDvuUrgiXI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=rF5Hc+WcyM/U7B2AMwqZ1MHP+isc3nBRDE2lLmuSjh5HEbdzv3F+6tLIjrWIDVDKJmzqJ3fhwFDO0wyDw3eHVxh52qI2+KQp8v3GSGoO5t4EaaF13+MVDctPozkz03qDeUrhCuOL+uTui7Ccgk1wD0qPM6M0bxOp31vWgz7+2g4= 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=O9Ki0kuH; 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=1706944814; bh=PTOpCCEM3IxbhvuSFFypmEAY+7vzYF1umsDvuUrgiXI=; h=From:To:Cc:Subject:Date; b=O9Ki0kuHyXgz6hB8GZG2xrVRL5oWLrqyVfO171cWUMsxxrB6I/OV4T1My0XFwZ0P+ J5WZu8f8zTeL4rUWAHf7U1nNJNEmA4ROLjWlORjKhQjfN5gTrOyVJa9gU/II/bY4at Juv14sn6g31bgNvikWLl9HlQNhtraNaUh+ltk2f3i05S6jEmwdMgW7VsF6SSd+ANLb hLpnp4PYC19WzFyvjAsp9U7EO7Xou9MYbzcgUJL/yrrvVMUWQh2qCr3aHyQgnPZ5PF JnLRQJ6Whzrr7J5VtDYB8dGF3s66NTNPVlianCNKYsN0O0Ual163IEFFA0G1vHhwc8 LukaM9EAPy5fQ== From: Joachim Vandersmissen To: linux-crypto@vger.kernel.org, Herbert Xu Cc: Joachim Vandersmissen Subject: [PATCH v2] crypto: rsa - restrict plaintext/ciphertext values more Date: Sat, 3 Feb 2024 01:19:59 -0600 Message-ID: <20240203071959.239363-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 Hi Herbert, As requested, I replaced the existing check with the new SP 800-56Br2 check. I verified that the restriction is now applied in both FIPS and non-FIPS mode. I tried to make it clear in the comments why the code is now deviating from RFC3447. ---8<--- 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. This range is slightly more conservative than RFC3447, as it also excludes RSA fixed points 0, 1, and n - 1. [1] https://doi.org/10.6028/NIST.SP.800-56Br2 Signed-off-by: Joachim Vandersmissen --- crypto/rsa.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/crypto/rsa.c b/crypto/rsa.c index b9cd11fb7d36..d9be9e86097e 100644 --- a/crypto/rsa.c +++ b/crypto/rsa.c @@ -24,14 +24,38 @@ struct rsa_mpi_key { MPI qinv; }; +static int rsa_check_payload(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) { - /* (1) Validate 0 <= m < n */ - if (mpi_cmp_ui(m, 0) < 0 || mpi_cmp(m, key->n) >= 0) + /* + * Even though (1) in RFC3447 only requires 0 <= m <= n - 1, we are + * slightly more conservative and require 1 < m < n - 1. This is in line + * with SP 800-56Br2, Section 7.1.1. + */ + if (rsa_check_payload(m, key->n)) return -EINVAL; /* (2) c = m^e mod n */ @@ -50,8 +74,12 @@ 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; - /* (1) Validate 0 <= c < n */ - if (mpi_cmp_ui(c, 0) < 0 || mpi_cmp(c, key->n) >= 0) + /* + * Even though (1) in RFC3447 only requires 0 <= c <= n - 1, we are + * slightly more conservative and require 1 < c < n - 1. This is in line + * with SP 800-56Br2, Section 7.1.2. + */ + if (rsa_check_payload(c, key->n)) return -EINVAL; m2 = mpi_alloc(0); -- 2.43.0