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

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