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

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