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