Skip to content

Commit

Permalink
LMS support: verify only support, fix build on nrf52.
Browse files Browse the repository at this point in the history
  • Loading branch information
philljj committed Aug 30, 2023
1 parent f2339e1 commit e734e3e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 34 deletions.
30 changes: 22 additions & 8 deletions docs/PQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
10 changes: 5 additions & 5 deletions options.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 6 additions & 16 deletions src/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions tools/keytools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions tools/keytools/keygen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down

0 comments on commit e734e3e

Please sign in to comment.