From patchwork Thu Aug 22 11:01:44 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: 2848207 Return-Path: X-Original-To: patchwork-linux-pm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 769A29F271 for ; Thu, 22 Aug 2013 11:09:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id BEFA720623 for ; Thu, 22 Aug 2013 11:09:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1159F20620 for ; Thu, 22 Aug 2013 11:09:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752960Ab3HVLEK (ORCPT ); Thu, 22 Aug 2013 07:04:10 -0400 Received: from mail-pa0-f51.google.com ([209.85.220.51]:48046 "EHLO mail-pa0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752918Ab3HVLEF (ORCPT ); Thu, 22 Aug 2013 07:04:05 -0400 Received: by mail-pa0-f51.google.com with SMTP id lf1so1383796pab.10 for ; Thu, 22 Aug 2013 04:04:05 -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=Yh53CVMIPAhkGNW+w3ai2VjBYOKqMUPuok5FOO8KUV8=; b=tPaTLvtyGnQnwHPGTYQM2pgHRY30WW2doRw84ioY34Q+lsZpN3nX8UBSwkX5NyW2Yp MJ0LKLm4omzOrP5TAmn4bv5VvzQU3vOy0qN+dL2IqdUD5+JmCgbyJkS0s9+nLqRimuGm /WEC++E1ewb9T0Zb/tZqL7DZjSUZAJMFfJQ1sH7F+XJ1n3oltQ/yG8GDKBpV7KzBvrnM jcNJbqsG4tFmJDI6nBhDDUpfvsze1bYeSUAHFvqbMaUvRJ9ia7EOAbe/+28hZPm2aKDJ sXxwc6+hFNNUxPJp1GPQq//8voO/9gzxTytA5z+lM5Jq/5h44Cv7Z9hkJmTw77+rVRN7 q7KA== X-Received: by 10.66.169.172 with SMTP id af12mr14814428pac.23.1377169444912; Thu, 22 Aug 2013 04:04:04 -0700 (PDT) Received: from localhost.localdomain ([130.57.30.250]) by mx.google.com with ESMTPSA id ht5sm14182603pbb.29.1969.12.31.16.00.00 (version=TLSv1 cipher=RC4-SHA bits=128/128); Thu, 22 Aug 2013 04:04:04 -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 05/18] asymmetric keys: implement RSASP1 Date: Thu, 22 Aug 2013 19:01:44 +0800 Message-Id: <1377169317-5959-6-git-send-email-jlee@suse.com> X-Mailer: git-send-email 1.6.0.2 In-Reply-To: <1377169317-5959-1-git-send-email-jlee@suse.com> References: <1377169317-5959-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=-9.6 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable 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 Implement RSASP1 and fill-in the following data to public key signature structure: signature length (pkcs->k), signature octet strings (pks->S) and MPI of signature (pks->rsa.s). Reviewed-by: Jiri Kosina Signed-off-by: Lee, Chun-Yi --- crypto/asymmetric_keys/rsa.c | 47 +++++++++++++++++++++++++++++++++++++++-- 1 files changed, 44 insertions(+), 3 deletions(-) diff --git a/crypto/asymmetric_keys/rsa.c b/crypto/asymmetric_keys/rsa.c index 0862018..e60defe 100644 --- a/crypto/asymmetric_keys/rsa.c +++ b/crypto/asymmetric_keys/rsa.c @@ -86,6 +86,39 @@ static const struct { }; /* + * RSASP1() function [RFC3447 sec 5.2.1] + */ +static int RSASP1(const struct private_key *key, MPI m, MPI *_s) +{ + MPI s; + int ret; + + /* (1) Validate 0 <= m < n */ + if (mpi_cmp_ui(m, 0) < 0) { + kleave(" = -EBADMSG [m < 0]"); + return -EBADMSG; + } + if (mpi_cmp(m, key->rsa.n) >= 0) { + kleave(" = -EBADMSG [m >= n]"); + return -EBADMSG; + } + + s = mpi_alloc(0); + if (!s) + return -ENOMEM; + + /* (2) s = m^d mod n */ + ret = mpi_powm(s, m, key->rsa.d, key->rsa.n); + if (ret < 0) { + mpi_free(s); + return ret; + } + + *_s = s; + return 0; +} + +/* * RSAVP1() function [RFC3447 sec 5.2.2] */ static int RSAVP1(const struct public_key *key, MPI s, MPI *_m) @@ -173,9 +206,12 @@ static int RSA_I2OSP(MPI x, size_t xLen, u8 **_X) static int RSA_OS2IP(u8 *X, size_t XLen, MPI *_x) { MPI x; + int ret; x = mpi_alloc((XLen + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB); - mpi_set_buffer(x, X, XLen, 0); + ret = mpi_set_buffer(x, X, XLen, 0); + if (ret < 0) + return ret; *_x = x; return 0; @@ -453,8 +489,13 @@ static struct public_key_signature *RSA_generate_signature( if (ret < 0) goto error_v1_5_encode; - /* TODO 3): s = RSASP1 (K, m) */ - s = m; + /* 3): s = RSASP1 (K, m) */ + RSASP1(key, m, &s); + + pks->rsa.s = s; + pks->nr_mpi = 1; + pks->k = mpi_get_nbits(s); + pks->k = (pks->k + 7) / 8; /* 4): S = I2OSP (s, k) */ _RSA_I2OSP(s, &X_size, &pks->S);