diff --git a/docs/PQ.md b/docs/PQ.md index 0fbdb055c..41349c696 100644 --- a/docs/PQ.md +++ b/docs/PQ.md @@ -16,11 +16,13 @@ of cryptographically relevant quantum computers. ### Building with LMS Support LMS/HSS support in wolfCrypt requires the hash-sigs library ( https://github.com/cisco/hash-sigs ). -The hash-sigs repo should be present here: +The hash-sigs repo should be cloned to the `src` dir here + ``` -$ls lib/ -CMakeLists.txt hash-sigs wolfssl wolfTPM +$ls lib/hash-sigs/ +lib src ``` + and checked out at this commit ``` b0631b8891295bf2929e68761205337b7c031726 @@ -31,14 +33,26 @@ In the file `sha256.h` update the `USE_OPENSSL` define to #define USE_OPENSSL 0 ``` -Build hash-sigs with: +Build hash-sigs with +``` +$make hss_lib.a +$make hss_verify.a +``` + +and place the built static libs here: ``` -make hss_lib.a +$ls lib/hash-sigs/lib/ +hss_lib.a hss_verify.a ``` -Note: hash-sigs only builds static libraries. `hss_lib.a` is the -single-threaded version, and `hss_lib_thread.a` multi-threaded. At the moment -wolfBoot LMS support is using single-threaded `hss_lib.a`. +Note: the hash-sigs project only builds static libraries: +- hss_verify.a: a single-threaded verify-only static lib. +- hss_lib.a: a single-threaded static lib. +- hss_lib_thread.a: a multi-threaded static lib. + +The keytools utility links against `hss_lib.a` as it needs full +keygen, signing, and verifying functionality, while wolfBoot +links with `hss_verify.a` as it needs verify functionality only. ### Config diff --git a/options.mk b/options.mk index 8d08d8e93..856729359 100644 --- a/options.mk +++ b/options.mk @@ -316,17 +316,17 @@ ifeq ($(SIGN),LMS) LMSDIR = lib/hash-sigs KEYGEN_OPTIONS+=--lms SIGN_OPTIONS+=--lms - LIBS += $(LMSDIR)/hss_lib.a + LIBS += $(LMSDIR)/lib/hss_verify.a WOLFCRYPT_OBJS+= \ ./lib/wolfssl/wolfcrypt/src/ext_lms.o \ - ./lib/wolfssl/wolfcrypt/src/hash.o \ ./lib/wolfssl/wolfcrypt/src/memory.o \ - ./lib/wolfssl/wolfcrypt/src/wc_port.o + ./lib/wolfssl/wolfcrypt/src/wc_port.o \ + ./lib/wolfssl/wolfcrypt/src/hash.o CFLAGS+=-D"WOLFBOOT_SIGN_LMS" -D"WOLFSSL_HAVE_LMS" -D"HAVE_LIBLMS" \ -D"LMS_LEVELS=$(LMS_LEVELS)" -D"LMS_HEIGHT=$(LMS_HEIGHT)" \ - -D"LMS_WINTERNITZ=$(LMS_WINTERNITZ)" -I"$(LMSDIR)" \ + -D"LMS_WINTERNITZ=$(LMS_WINTERNITZ)" -I$(LMSDIR)/src \ -D"IMAGE_SIGNATURE_SIZE"=$(IMAGE_SIGNATURE_SIZE) \ - -D"PRINTF_ENABLED" + -D"LMS_VERIFY_ONLY" ifeq ($(WOLFBOOT_SMALL_STACK),1) $(error WOLFBOOT_SMALL_STACK with LMS not supported) else diff --git a/src/image.c b/src/image.c index b2a0a53ce..b8bf629f9 100644 --- a/src/image.c +++ b/src/image.c @@ -436,21 +436,11 @@ static void wolfBoot_verify_signature(uint8_t key_slot, LMS_HEIGHT, LMS_WINTERNITZ); /* Set the public key. */ - XMEMCPY(lms.pub, pubkey, KEYSTORE_PUBKEY_SIZE); - - ret = wc_LmsKey_GetPubLen(&lms, &pub_len); - + ret = wc_LmsKey_ImportPubRaw(&lms, pubkey, KEYSTORE_PUBKEY_SIZE); if (ret != 0) { /* Something is wrong with the pub key or LMS parameters. */ - wolfBoot_printf("error: wc_LmsKey_GetPubLen %d\n", ret); - return; - } - - if (pub_len != KEYSTORE_PUBKEY_SIZE) { - /* Something is wrong with the pub key or LMS parameters. */ - wolfBoot_printf("error: wc_LmsKey_GetPubLen mismatch: "\ - " got %d, expected %d\n", pub_len, - KEYSTORE_PUBKEY_SIZE); + wolfBoot_printf("error: wc_LmsKey_ImportPubRaw" \ + " returned %d\n", ret); return; } @@ -1269,15 +1259,15 @@ int wolfBoot_open_image_address(struct wolfBoot_image *img, uint8_t *image) uint32_t *magic = (uint32_t *)(image); if (*magic != WOLFBOOT_MAGIC) { wolfBoot_printf("Boot header magic 0x%08x invalid at %p\n", - *magic, image); + (unsigned int)*magic, image); return -1; } img->fw_size = wolfBoot_image_size(image); - wolfBoot_printf("Image size %d\n", img->fw_size); + wolfBoot_printf("Image size %d\n", (unsigned int)img->fw_size); #ifdef WOLFBOOT_FIXED_PARTITIONS if (img->fw_size > (WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE)) { wolfBoot_printf("Image size %d > max %d\n", - img->fw_size, (WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE)); + (unsigned int)img->fw_size, (WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE)); img->fw_size = 0; return -1; } diff --git a/tools/keytools/Makefile b/tools/keytools/Makefile index 499404a3e..ebb498a7a 100644 --- a/tools/keytools/Makefile +++ b/tools/keytools/Makefile @@ -18,9 +18,9 @@ OBJDIR = ./ LIBS = ifeq ($(SIGN),LMS) - LMSDIR = $(WOLFBOOTDIR)/lib/hash-sigs/ - LIBS += $(LMSDIR)/hss_lib.a - CFLAGS +=-DWOLFBOOT_SIGN_LMS -DWOLFSSL_HAVE_LMS -DHAVE_LIBLMS -I$(LMSDIR) \ + LMSDIR = $(WOLFBOOTDIR)/lib/hash-sigs + LIBS += $(LMSDIR)/lib/hss_lib.a + CFLAGS +=-DWOLFBOOT_SIGN_LMS -DWOLFSSL_HAVE_LMS -DHAVE_LIBLMS -I$(LMSDIR)/src \ -D"LMS_LEVELS=$(LMS_LEVELS)" -D"LMS_HEIGHT=$(LMS_HEIGHT)" \ -D"LMS_WINTERNITZ=$(LMS_WINTERNITZ)" endif diff --git a/tools/keytools/keygen.c b/tools/keytools/keygen.c index 97af74030..600152843 100644 --- a/tools/keytools/keygen.c +++ b/tools/keytools/keygen.c @@ -492,6 +492,8 @@ static void keygen_lms(const char *priv_fname) FILE * fpriv; LmsKey key; int ret; + byte lms_pub[HSS_MAX_PUBLIC_KEY_LEN]; + word32 pub_len = sizeof(lms_pub); ret = wc_LmsKey_Init(&key, NULL, INVALID_DEVID); if (ret != 0) { @@ -534,6 +536,18 @@ static void keygen_lms(const char *priv_fname) exit(1); } + ret = wc_LmsKey_ExportPubRaw(&key, lms_pub, &pub_len); + if (ret != 0) { + fprintf(stderr, "error: wc_LmsKey_ExportPubRaw returned %d\n", ret); + exit(1); + } + + if (pub_len != sizeof(lms_pub)) { + fprintf(stderr, "error: wc_LmsKey_ExportPubRaw returned pub_len=%d\n" \ + ", expected %zu\n", pub_len, sizeof(lms_pub)); + exit(1); + } + /* Append the public key to the private keyfile. */ fpriv = fopen(priv_fname, "r+"); if (!fpriv) { @@ -543,10 +557,10 @@ static void keygen_lms(const char *priv_fname) } fseek(fpriv, 64, SEEK_SET); - fwrite(key.pub, KEYSTORE_PUBKEY_SIZE_LMS, 1, fpriv); + fwrite(lms_pub, KEYSTORE_PUBKEY_SIZE_LMS, 1, fpriv); fclose(fpriv); - keystore_add(KEYGEN_LMS, key.pub, KEYSTORE_PUBKEY_SIZE_LMS, priv_fname); + keystore_add(KEYGEN_LMS, lms_pub, KEYSTORE_PUBKEY_SIZE_LMS, priv_fname); wc_LmsKey_Free(&key); }