lib/rsa: Make fdt_add_bignum() available outside of RSA code
authorAlexandru Gagniuc <mr.nuke.me@gmail.com>
Fri, 19 Feb 2021 18:45:11 +0000 (12:45 -0600)
committerTom Rini <trini@konsulko.com>
Wed, 14 Apr 2021 19:06:08 +0000 (15:06 -0400)
fdt_add_bignum() is useful for algorithms other than just RSA. To
allow its use for ECDSA, move it to a common file under lib/.

The new file is suffixed with '-libcrypto' because it has a direct
dependency on openssl. This is due to the use of the "BIGNUM *" type.

Signed-off-by: Alexandru Gagniuc <mr.nuke.me@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
include/u-boot/fdt-libcrypto.h [new file with mode: 0644]
lib/fdt-libcrypto.c [new file with mode: 0644]
lib/rsa/rsa-sign.c
tools/Makefile

diff --git a/include/u-boot/fdt-libcrypto.h b/include/u-boot/fdt-libcrypto.h
new file mode 100644 (file)
index 0000000..5142f37
--- /dev/null
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * Copyright (c) 2020, Alexandru Gagniuc <mr.nuke.me@gmail.com>
+ * Copyright (c) 2013, Google Inc.
+ */
+
+#ifndef _FDT_LIBCRYPTO_H
+#define _FDT_LIBCRYPTO_H
+
+#include <openssl/bn.h>
+
+/**
+ * fdt_add_bignum() - Write a libcrypto BIGNUM as an FDT property
+ *
+ * Convert a libcrypto BIGNUM * into a big endian array of integers.
+ *
+ * @blob:      FDT blob to modify
+ * @noffset:   Offset of the FDT node
+ * @prop_name: What to call the property in the FDT
+ * @num:       pointer to a libcrypto big number
+ * @num_bits:  How big is 'num' in bits?
+ * @return 0 if all good all working, -ve on horror
+ */
+int fdt_add_bignum(void *blob, int noffset, const char *prop_name,
+                  BIGNUM *num, int num_bits);
+
+#endif /* _FDT_LIBCRYPTO_H */
diff --git a/lib/fdt-libcrypto.c b/lib/fdt-libcrypto.c
new file mode 100644 (file)
index 0000000..ecb0344
--- /dev/null
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2020, Alexandru Gagniuc <mr.nuke.me@gmail.com>
+ * Copyright (c) 2013, Google Inc.
+ */
+
+#include <libfdt.h>
+#include <u-boot/fdt-libcrypto.h>
+
+int fdt_add_bignum(void *blob, int noffset, const char *prop_name,
+                  BIGNUM *num, int num_bits)
+{
+       int nwords = num_bits / 32;
+       int size;
+       uint32_t *buf, *ptr;
+       BIGNUM *tmp, *big2, *big32, *big2_32;
+       BN_CTX *ctx;
+       int ret;
+
+       tmp = BN_new();
+       big2 = BN_new();
+       big32 = BN_new();
+       big2_32 = BN_new();
+
+       /*
+        * Note: This code assumes that all of the above succeed, or all fail.
+        * In practice memory allocations generally do not fail (unless the
+        * process is killed), so it does not seem worth handling each of these
+        * as a separate case. Technicaly this could leak memory on failure,
+        * but a) it won't happen in practice, and b) it doesn't matter as we
+        * will immediately exit with a failure code.
+        */
+       if (!tmp || !big2 || !big32 || !big2_32) {
+               fprintf(stderr, "Out of memory (bignum)\n");
+               return -ENOMEM;
+       }
+       ctx = BN_CTX_new();
+       if (!ctx) {
+               fprintf(stderr, "Out of memory (bignum context)\n");
+               return -ENOMEM;
+       }
+       BN_set_word(big2, 2L);
+       BN_set_word(big32, 32L);
+       BN_exp(big2_32, big2, big32, ctx); /* B = 2^32 */
+
+       size = nwords * sizeof(uint32_t);
+       buf = malloc(size);
+       if (!buf) {
+               fprintf(stderr, "Out of memory (%d bytes)\n", size);
+               return -ENOMEM;
+       }
+
+       /* Write out modulus as big endian array of integers */
+       for (ptr = buf + nwords - 1; ptr >= buf; ptr--) {
+               BN_mod(tmp, num, big2_32, ctx); /* n = N mod B */
+               *ptr = cpu_to_fdt32(BN_get_word(tmp));
+               BN_rshift(num, num, 32); /*  N = N/B */
+       }
+
+       /*
+        * We try signing with successively increasing size values, so this
+        * might fail several times
+        */
+       ret = fdt_setprop(blob, noffset, prop_name, buf, size);
+       free(buf);
+       BN_free(tmp);
+       BN_free(big2);
+       BN_free(big32);
+       BN_free(big2_32);
+
+       return ret ? -FDT_ERR_NOSPACE : 0;
+}
index 1f0d81b..557c690 100644 (file)
@@ -9,6 +9,7 @@
 #include <string.h>
 #include <image.h>
 #include <time.h>
+#include <u-boot/fdt-libcrypto.h>
 #include <openssl/bn.h>
 #include <openssl/rsa.h>
 #include <openssl/pem.h>
@@ -680,70 +681,6 @@ int rsa_get_params(RSA *key, uint64_t *exponent, uint32_t *n0_invp,
        return ret;
 }
 
-static int fdt_add_bignum(void *blob, int noffset, const char *prop_name,
-                         BIGNUM *num, int num_bits)
-{
-       int nwords = num_bits / 32;
-       int size;
-       uint32_t *buf, *ptr;
-       BIGNUM *tmp, *big2, *big32, *big2_32;
-       BN_CTX *ctx;
-       int ret;
-
-       tmp = BN_new();
-       big2 = BN_new();
-       big32 = BN_new();
-       big2_32 = BN_new();
-
-       /*
-        * Note: This code assumes that all of the above succeed, or all fail.
-        * In practice memory allocations generally do not fail (unless the
-        * process is killed), so it does not seem worth handling each of these
-        * as a separate case. Technicaly this could leak memory on failure,
-        * but a) it won't happen in practice, and b) it doesn't matter as we
-        * will immediately exit with a failure code.
-        */
-       if (!tmp || !big2 || !big32 || !big2_32) {
-               fprintf(stderr, "Out of memory (bignum)\n");
-               return -ENOMEM;
-       }
-       ctx = BN_CTX_new();
-       if (!ctx) {
-               fprintf(stderr, "Out of memory (bignum context)\n");
-               return -ENOMEM;
-       }
-       BN_set_word(big2, 2L);
-       BN_set_word(big32, 32L);
-       BN_exp(big2_32, big2, big32, ctx); /* B = 2^32 */
-
-       size = nwords * sizeof(uint32_t);
-       buf = malloc(size);
-       if (!buf) {
-               fprintf(stderr, "Out of memory (%d bytes)\n", size);
-               return -ENOMEM;
-       }
-
-       /* Write out modulus as big endian array of integers */
-       for (ptr = buf + nwords - 1; ptr >= buf; ptr--) {
-               BN_mod(tmp, num, big2_32, ctx); /* n = N mod B */
-               *ptr = cpu_to_fdt32(BN_get_word(tmp));
-               BN_rshift(num, num, 32); /*  N = N/B */
-       }
-
-       /*
-        * We try signing with successively increasing size values, so this
-        * might fail several times
-        */
-       ret = fdt_setprop(blob, noffset, prop_name, buf, size);
-       free(buf);
-       BN_free(tmp);
-       BN_free(big2);
-       BN_free(big32);
-       BN_free(big2_32);
-
-       return ret ? -FDT_ERR_NOSPACE : 0;
-}
-
 int rsa_add_verify_data(struct image_sign_info *info, void *keydest)
 {
        BIGNUM *modulus, *r_squared;
index e968b20..3b8e7ac 100644 (file)
@@ -73,6 +73,10 @@ RSA_OBJS-$(CONFIG_FIT_SIGNATURE) := $(addprefix lib/rsa/, \
 AES_OBJS-$(CONFIG_FIT_CIPHER) := $(addprefix lib/aes/, \
                                        aes-encrypt.o aes-decrypt.o)
 
+# Cryptographic helpers that depend on openssl/libcrypto
+LIBCRYPTO_OBJS-$(CONFIG_FIT_SIGNATURE) := $(addprefix lib/, \
+                                       fdt-libcrypto.o)
+
 ROCKCHIP_OBS = lib/rc4.o rkcommon.o rkimage.o rksd.o rkspi.o
 
 # common objs for dumpimage and mkimage
@@ -115,6 +119,7 @@ dumpimage-mkimage-objs := aisimage.o \
                        zynqimage.o \
                        zynqmpimage.o \
                        zynqmpbif.o \
+                       $(LIBCRYPTO_OBJS-y) \
                        $(LIBFDT_OBJS) \
                        gpimage.o \
                        gpimage-common.o \