Blame SOURCES/0356-pgp-factor-out-rsa_pad.patch

3efed6
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
3efed6
From: Daniel Axtens <dja@axtens.net>
3efed6
Date: Thu, 1 Oct 2020 20:23:48 +1000
3efed6
Subject: [PATCH] pgp: factor out rsa_pad
3efed6
3efed6
rsa_pad does the PKCS#1 v1.5 padding for the RSA signature scheme.
3efed6
We want to use it in other RSA signature verification applications.
3efed6
3efed6
I considered and rejected putting it in lib/crypto.c. That file doesn't
3efed6
currently require any MPI functions, but rsa_pad does. That's not so
3efed6
much of a problem for the grub kernel and modules, but crypto.c also
3efed6
gets built into all the grub utilities. So - despite the utils not
3efed6
using any asymmetric ciphers -  we would need to built the entire MPI
3efed6
infrastructure in to them.
3efed6
3efed6
A better and simpler solution is just to spin rsa_pad out into its own
3efed6
PKCS#1 v1.5 module.
3efed6
3efed6
Signed-off-by: Daniel Axtens <dja@axtens.net>
3efed6
---
3efed6
 grub-core/Makefile.core.def |  8 ++++++
3efed6
 grub-core/commands/pgp.c    | 28 ++-------------------
3efed6
 grub-core/lib/pkcs1_v15.c   | 59 +++++++++++++++++++++++++++++++++++++++++++++
3efed6
 include/grub/pkcs1_v15.h    | 27 +++++++++++++++++++++
3efed6
 4 files changed, 96 insertions(+), 26 deletions(-)
3efed6
 create mode 100644 grub-core/lib/pkcs1_v15.c
3efed6
 create mode 100644 include/grub/pkcs1_v15.h
3efed6
3efed6
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
3efed6
index 809f11feaef..99615c07b94 100644
3efed6
--- a/grub-core/Makefile.core.def
3efed6
+++ b/grub-core/Makefile.core.def
3efed6
@@ -2387,6 +2387,14 @@ module = {
3efed6
   cppflags = '$(CPPFLAGS_GCRY)';
3efed6
 };
3efed6
 
3efed6
+module = {
3efed6
+  name = pkcs1_v15;
3efed6
+  common = lib/pkcs1_v15.c;
3efed6
+
3efed6
+  cflags = '$(CFLAGS_GCRY) -Wno-redundant-decls -Wno-sign-compare';
3efed6
+  cppflags = '$(CPPFLAGS_GCRY)';
3efed6
+};
3efed6
+
3efed6
 module = {
3efed6
   name = all_video;
3efed6
   common = lib/fake_module.c;
3efed6
diff --git a/grub-core/commands/pgp.c b/grub-core/commands/pgp.c
3efed6
index d39846d8cfe..bb6543819f0 100644
3efed6
--- a/grub-core/commands/pgp.c
3efed6
+++ b/grub-core/commands/pgp.c
3efed6
@@ -24,6 +24,7 @@
3efed6
 #include <grub/file.h>
3efed6
 #include <grub/command.h>
3efed6
 #include <grub/crypto.h>
3efed6
+#include <grub/pkcs1_v15.h>
3efed6
 #include <grub/i18n.h>
3efed6
 #include <grub/gcrypt/gcrypt.h>
3efed6
 #include <grub/pubkey.h>
3efed6
@@ -411,32 +412,7 @@ static int
3efed6
 rsa_pad (gcry_mpi_t *hmpi, grub_uint8_t *hval,
3efed6
 	 const gcry_md_spec_t *hash, struct grub_public_subkey *sk)
3efed6
 {
3efed6
-  grub_size_t tlen, emlen, fflen;
3efed6
-  grub_uint8_t *em, *emptr;
3efed6
-  unsigned nbits = gcry_mpi_get_nbits (sk->mpis[0]);
3efed6
-  int ret;
3efed6
-  tlen = hash->mdlen + hash->asnlen;
3efed6
-  emlen = (nbits + 7) / 8;
3efed6
-  if (emlen < tlen + 11)
3efed6
-    return 1;
3efed6
-
3efed6
-  em = grub_malloc (emlen);
3efed6
-  if (!em)
3efed6
-    return 1;
3efed6
-
3efed6
-  em[0] = 0x00;
3efed6
-  em[1] = 0x01;
3efed6
-  fflen = emlen - tlen - 3;
3efed6
-  for (emptr = em + 2; emptr < em + 2 + fflen; emptr++)
3efed6
-    *emptr = 0xff;
3efed6
-  *emptr++ = 0x00;
3efed6
-  grub_memcpy (emptr, hash->asnoid, hash->asnlen);
3efed6
-  emptr += hash->asnlen;
3efed6
-  grub_memcpy (emptr, hval, hash->mdlen);
3efed6
-
3efed6
-  ret = gcry_mpi_scan (hmpi, GCRYMPI_FMT_USG, em, emlen, 0);
3efed6
-  grub_free (em);
3efed6
-  return ret;
3efed6
+  return grub_crypto_rsa_pad(hmpi, hval, hash, sk->mpis[0]);
3efed6
 }
3efed6
 
3efed6
 struct grub_pubkey_context
3efed6
diff --git a/grub-core/lib/pkcs1_v15.c b/grub-core/lib/pkcs1_v15.c
3efed6
new file mode 100644
3efed6
index 00000000000..dbacd563d01
3efed6
--- /dev/null
3efed6
+++ b/grub-core/lib/pkcs1_v15.c
3efed6
@@ -0,0 +1,59 @@
3efed6
+/*
3efed6
+ *  GRUB  --  GRand Unified Bootloader
3efed6
+ *  Copyright (C) 2013  Free Software Foundation, Inc.
3efed6
+ *
3efed6
+ *  GRUB is free software: you can redistribute it and/or modify
3efed6
+ *  it under the terms of the GNU General Public License as published by
3efed6
+ *  the Free Software Foundation, either version 3 of the License, or
3efed6
+ *  (at your option) any later version.
3efed6
+ *
3efed6
+ *  GRUB is distributed in the hope that it will be useful,
3efed6
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
3efed6
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3efed6
+ *  GNU General Public License for more details.
3efed6
+ *
3efed6
+ *  You should have received a copy of the GNU General Public License
3efed6
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
3efed6
+ */
3efed6
+
3efed6
+#include <grub/dl.h>
3efed6
+#include <grub/gcrypt/gcrypt.h>
3efed6
+
3efed6
+GRUB_MOD_LICENSE ("GPLv3+");
3efed6
+
3efed6
+/*
3efed6
+ * Given a hash value 'hval', of hash specification 'hash', perform
3efed6
+ * the EMSA-PKCS1-v1_5 padding suitable for a key with modulus 'mod'
3efed6
+ * (see RFC 8017 s 9.2) and place the result in 'hmpi'.
3efed6
+ */
3efed6
+gcry_err_code_t
3efed6
+grub_crypto_rsa_pad (gcry_mpi_t * hmpi, grub_uint8_t * hval,
3efed6
+		     const gcry_md_spec_t * hash, gcry_mpi_t mod)
3efed6
+{
3efed6
+  grub_size_t tlen, emlen, fflen;
3efed6
+  grub_uint8_t *em, *emptr;
3efed6
+  unsigned nbits = gcry_mpi_get_nbits (mod);
3efed6
+  int ret;
3efed6
+  tlen = hash->mdlen + hash->asnlen;
3efed6
+  emlen = (nbits + 7) / 8;
3efed6
+  if (emlen < tlen + 11)
3efed6
+    return GPG_ERR_TOO_SHORT;
3efed6
+
3efed6
+  em = grub_malloc (emlen);
3efed6
+  if (!em)
3efed6
+    return 1;
3efed6
+
3efed6
+  em[0] = 0x00;
3efed6
+  em[1] = 0x01;
3efed6
+  fflen = emlen - tlen - 3;
3efed6
+  for (emptr = em + 2; emptr < em + 2 + fflen; emptr++)
3efed6
+    *emptr = 0xff;
3efed6
+  *emptr++ = 0x00;
3efed6
+  grub_memcpy (emptr, hash->asnoid, hash->asnlen);
3efed6
+  emptr += hash->asnlen;
3efed6
+  grub_memcpy (emptr, hval, hash->mdlen);
3efed6
+
3efed6
+  ret = gcry_mpi_scan (hmpi, GCRYMPI_FMT_USG, em, emlen, 0);
3efed6
+  grub_free (em);
3efed6
+  return ret;
3efed6
+}
3efed6
diff --git a/include/grub/pkcs1_v15.h b/include/grub/pkcs1_v15.h
3efed6
new file mode 100644
3efed6
index 00000000000..5c338c84a15
3efed6
--- /dev/null
3efed6
+++ b/include/grub/pkcs1_v15.h
3efed6
@@ -0,0 +1,27 @@
3efed6
+/*
3efed6
+ *  GRUB  --  GRand Unified Bootloader
3efed6
+ *  Copyright (C) 2013  Free Software Foundation, Inc.
3efed6
+ *
3efed6
+ *  GRUB is free software: you can redistribute it and/or modify
3efed6
+ *  it under the terms of the GNU General Public License as published by
3efed6
+ *  the Free Software Foundation, either version 3 of the License, or
3efed6
+ *  (at your option) any later version.
3efed6
+ *
3efed6
+ *  GRUB is distributed in the hope that it will be useful,
3efed6
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
3efed6
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3efed6
+ *  GNU General Public License for more details.
3efed6
+ *
3efed6
+ *  You should have received a copy of the GNU General Public License
3efed6
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
3efed6
+ */
3efed6
+
3efed6
+/*
3efed6
+ * Given a hash value 'hval', of hash specification 'hash', perform
3efed6
+ * the EMSA-PKCS1-v1_5 padding suitable for a key with modulus 'mod'
3efed6
+ * (See RFC 8017 s 9.2)
3efed6
+ */
3efed6
+gcry_err_code_t
3efed6
+grub_crypto_rsa_pad (gcry_mpi_t * hmpi, grub_uint8_t * hval,
3efed6
+		     const gcry_md_spec_t * hash, gcry_mpi_t mod);
3efed6
+