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