From patchwork Sun Sep 15 00:56:49 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chun-Yi Lee X-Patchwork-Id: 2894241 Return-Path: X-Original-To: patchwork-linux-pm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id B3001BFF05 for ; Sun, 15 Sep 2013 01:01:27 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id CEEC6201DA for ; Sun, 15 Sep 2013 01:01:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id ECB9E201D5 for ; Sun, 15 Sep 2013 01:01:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932465Ab3IOBBY (ORCPT ); Sat, 14 Sep 2013 21:01:24 -0400 Received: from mail-bk0-f44.google.com ([209.85.214.44]:64295 "EHLO mail-bk0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932518Ab3IOA5E (ORCPT ); Sat, 14 Sep 2013 20:57:04 -0400 Received: by mail-bk0-f44.google.com with SMTP id mz10so1015917bkb.3 for ; Sat, 14 Sep 2013 17:57:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=eVxJeEUHEkIWTk/q69jRPAUAilcYO2EuESMgRPXRzsk=; b=BEERsPCiDbgVvQmn+J9tOWDZ3F4GR/jIhDJYg21YCnghkTXPsqO5dFAOx6tecD61s3 zvvyFUQ/L2aFcl/vd/7Ft0SCG1yc55jsA6psMmQlQNFwEty/BwRHsnPZpr/5OMnKAXmE 57df09S1GtDiMchGUYvu+2GBv1R43mkmFL3BhBHuGqLsqZnElNHpzLyqF9tAMvWtpDGD 3bCefnB0Qsow9zUs2wmtD7T0cGHC5wd/R3NuRczEes8uK4uzBZZAg6FHrLy7IDvVe3Y1 o6Ez3tE9WrNQsX8nyUPZaHefmuC4WsjeBBIwmwrym5XsuhxC+ZpGYEG//8UrJAZQpUVL aOlg== X-Received: by 10.205.36.70 with SMTP id sz6mr17666446bkb.12.1379206622581; Sat, 14 Sep 2013 17:57:02 -0700 (PDT) Received: from localhost.localdomain ([124.11.22.254]) by mx.google.com with ESMTPSA id zl3sm4941613bkb.4.1969.12.31.16.00.00 (version=TLSv1 cipher=RC4-SHA bits=128/128); Sat, 14 Sep 2013 17:57:02 -0700 (PDT) From: "Lee, Chun-Yi" To: linux-kernel@vger.kernel.org Cc: linux-security-module@vger.kernel.org, linux-efi@vger.kernel.org, linux-pm@vger.kernel.org, linux-crypto@vger.kernel.org, opensuse-kernel@opensuse.org, David Howells , "Rafael J. Wysocki" , Matthew Garrett , Len Brown , Pavel Machek , Josh Boyer , Vojtech Pavlik , Matt Fleming , James Bottomley , Greg KH , JKosina@suse.com, Rusty Russell , Herbert Xu , "David S. Miller" , "H. Peter Anvin" , Michal Marek , Gary Lin , Vivek Goyal , "Lee, Chun-Yi" Subject: [PATCH V4 03/15] asymmetric keys: separate the length checking of octet string from RSA_I2OSP Date: Sun, 15 Sep 2013 08:56:49 +0800 Message-Id: <1379206621-18639-4-git-send-email-jlee@suse.com> X-Mailer: git-send-email 1.6.0.2 In-Reply-To: <1379206621-18639-1-git-send-email-jlee@suse.com> References: <1379206621-18639-1-git-send-email-jlee@suse.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, KHOP_BIG_TO_CC, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Due to RSA_I2OSP is not only used by signature verification path but also used in signature generation path. So, separate the length checking of octet string because it's not for generate 0x00 0x01 leading string when used in signature generation. The naming of _RSA_I2OSP and the variables used in this function accord PKCS#1 spec but not follow kernel naming convention, it useful when look at them with spec. Reference: ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1v2/pkcs1ietffinal.txt Reference: http://www.emc.com/collateral/white-papers/h11300-pkcs-1v2-2-rsa-cryptography-standard-wp.pdf Cc: Pavel Machek Reviewed-by: Jiri Kosina Signed-off-by: Lee, Chun-Yi --- crypto/asymmetric_keys/rsa.c | 33 ++++++++++++++++++++++++--------- 1 files changed, 24 insertions(+), 9 deletions(-) diff --git a/crypto/asymmetric_keys/rsa.c b/crypto/asymmetric_keys/rsa.c index 352ba45..aac8b77 100644 --- a/crypto/asymmetric_keys/rsa.c +++ b/crypto/asymmetric_keys/rsa.c @@ -121,12 +121,30 @@ static int RSAVP1(const struct public_key *key, MPI s, MPI *_m) /* * Integer to Octet String conversion [RFC3447 sec 4.1] */ -static int RSA_I2OSP(MPI x, size_t xLen, u8 **_X) +static int _RSA_I2OSP(MPI x, unsigned *X_size, u8 **_X) { - unsigned X_size, x_size; int X_sign; u8 *X; + X = mpi_get_buffer(x, X_size, &X_sign); + if (!X) + return -ENOMEM; + if (X_sign < 0) { + kfree(X); + return -EBADMSG; + } + + *_X = X; + return 0; +} + +static int RSA_I2OSP(MPI x, size_t xLen, u8 **_X) +{ + unsigned x_size; + unsigned X_size; + u8 *X = NULL; + int ret; + /* Make sure the string is the right length. The number should begin * with { 0x00, 0x01, ... } so we have to account for 15 leading zero * bits not being reported by MPI. @@ -136,13 +154,10 @@ static int RSA_I2OSP(MPI x, size_t xLen, u8 **_X) if (x_size != xLen * 8 - 15) return -ERANGE; - X = mpi_get_buffer(x, &X_size, &X_sign); - if (!X) - return -ENOMEM; - if (X_sign < 0) { - kfree(X); - return -EBADMSG; - } + ret = _RSA_I2OSP(x, &X_size, &X); + if (ret < 0) + return ret; + if (X_size != xLen - 1) { kfree(X); return -EBADMSG;