Skip to content

Commit a68fe84

Browse files
authored
Merge pull request #436 from philljj/update_lms_xmss_examples
Update lms xmss examples
2 parents 2326995 + 1252dd6 commit a68fe84

4 files changed

Lines changed: 144 additions & 114 deletions

File tree

pq/stateful_hash_sig/Makefile

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
# PQ Stateful Hash-Based Signature Examples Makefile
22
#
3-
# XMSS_INC, XMSS_LIB, and WOLF_STATIC_LIB are only required if
4-
# building with --with-libxmss.
3+
# HSS_INC, HSS_LIB, XMSS_INC, XMSS_LIB, and WOLF_STATIC_LIB are only
4+
# required if building with --with-libxmss or --with-liblms.
55
#
66
CC = gcc
77
WOLFSSL_INSTALL_DIR = /usr/local
8-
HSS_INC =
9-
XMSS_INC =
108
CFLAGS = -Wall -I$(WOLFSSL_INSTALL_DIR)/include
119
LIBS = -L$(WOLFSSL_INSTALL_DIR)/lib -lm
1210

1311
# option variables
1412
WOLF_DYN_LIB = -lwolfssl
1513
WOLF_STATIC_LIB = $(WOLFSSL_INSTALL_DIR)/lib/libwolfssl.a
16-
HSS_LIB =
17-
XMSS_LIB =
14+
15+
# These are only needed if building with the external
16+
# integration (ext_lms, ext_xmss).
17+
# HSS_INC = path to hash-sigs src dir
18+
# XMSS_INC = path to xmss-reference src dir
19+
# HSS_LIB = path to hash-sigs hss_lib.a
20+
# XMSS_LIB = path to xmss-reference xmss_lib.a
21+
22+
# Options
1823
DEBUG_FLAGS = -g -DDEBUG
1924
DEBUG_INC_PATHS = -MD
2025
OPTIMIZE = -Os
21-
22-
# Options
2326
#CFLAGS+=$(DEBUG_FLAGS)
2427
#CFLAGS+=$(OPTIMIZE)
2528

@@ -36,19 +39,18 @@ debug: all
3639

3740
# build template
3841
lms_example: lms_example.c
39-
$(CC) -o $@ $< $(CFLAGS) -I$(HSS_INC) $(LIBS) $(WOLF_STATIC_LIB) $(HSS_LIB)
42+
# If building with wc_lms (--enable-lms):
43+
$(CC) -o $@ $< $(CFLAGS) -DWOLFSSL_WC_LMS $(LIBS) $(WOLF_DYN_LIB)
44+
# If building with ext_lms (--enable-lms --with-liblms=<path>):
45+
# $(CC) -o $@ $< $(CFLAGS) -I$(HSS_INC) $(LIBS) $(WOLF_STATIC_LIB) $(HSS_LIB)
4046

4147
xmss_example: xmss_example.c
42-
# If building with --enable-xmss=wolfssl:
43-
$(CC) -o $@ $< $(CFLAGS) -I$(XMSS_INC) $(LIBS) $(WOLF_STATIC_LIB) $(XMSS_LIB)
44-
# If building with --enable-xmss --with-libxmss=<path>:
45-
# $(CC) -o $@ $< $(CFLAGS) $(LIBS) $(WOLF_DYN_LIB)
46-
47-
xmss_example_verifyonly: xmss_example.c
48-
$(CC) -o $@ $< $(CFLAGS) -I$(XMSS_INC) -DWOLFSSL_XMSS_VERIFY_ONLY $(LIBS) $(WOLF_STATIC_LIB) $(XMSS_LIB)
48+
# If building with wc_xmss (--enable-xmss):
49+
$(CC) -o $@ $< $(CFLAGS) -DWOLFSSL_WC_XMSS $(LIBS) $(WOLF_DYN_LIB)
50+
# If building with ext_xmss (--enable-xmss --with-libxmss=<path>):
51+
# $(CC) -o $@ $< $(CFLAGS) -I$(XMSS_INC) $(LIBS) $(WOLF_STATIC_LIB) $(XMSS_LIB)
4952

5053
clean:
5154
rm -f $(TARGETS)
52-
rm -f xmss_example_verifyonly
5355
rm -f lms_example.key
5456
rm -f xmss_example.key

pq/stateful_hash_sig/README.md

Lines changed: 102 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,21 @@
22

33
This directory contains:
44

5-
- A simple example that uses wolfCrypt LMS/HSS hooks to sign and verify a message
6-
with configurable LMS/HSS parameters. Requires wolfssl with `--enable-lms=yes`
7-
and `--with-liblms=<path to hash-sigs install>`.
5+
- An example that uses wolfCrypt LMS/HSS to sign and verify a
6+
message with configurable LMS/HSS parameters. Requires wolfssl with
7+
`--enable-lms`.
88

9-
- An example that uses wolfCrypt XMSS/XMSS^MT hooks to sign and verify a message
10-
with a configurable XMSS/XMSS^MT parameter string. Requires wolfssl with `--enable-xmss=yes`
11-
and `--with-libxmss=<path to patched xmss-reference install>`, or wolfssl
12-
with `--enable-xmss=wolfssl`.
9+
- An example that uses wolfCrypt XMSS/XMSS^MT to sign and verify a
10+
message with a configurable XMSS/XMSS^MT parameter string. Requires wolfssl
11+
with `--enable-xmss`.
1312

14-
# Prerequisites
13+
By default these examples use the wolfCrypt LMS and XMSS implementations
14+
(`wc_lms.c`, `wc_lms_impl.c`, `wc_xmss.c`, `wc_xmss_impl.c`), which are more
15+
performant and configurable. Also, these implementations benefit significantly
16+
from `--enable-intelasm` and `--enable-armasm`.
1517

16-
The LMS/HSS sign verify example requires that hash-sigs has been built, and
17-
wolfSSL has been built with LMS/HSS support enabled. Please see Item 17
18-
in the wolfSSL repo's INSTALL file.
19-
20-
https://github.com/wolfSSL/wolfssl/blob/master/INSTALL
21-
22-
If building with `--with-libxmss=<path>`, the XMSS/XMSS^MT example requires
23-
that the xmss-reference repository has been cloned, patched, and built. Please
24-
see item 20 in the wolfSSL repo's INSTALL file.
25-
26-
The patch to use is `0001-Patch-to-support-wolfSSL-xmss-reference-integration.patch` from this XMSS/XMSS^MT example.
27-
This patch includes an addendum readme, `patch_readme.md`, that lists all changes made and explains their rationale.
28-
29-
# Building the LMS/HSS example
30-
31-
Configure the Makefile to point to your hash-sigs install:
32-
33-
```
34-
HSS_INC = <path to hss install>
35-
```
36-
37-
```
38-
HSS_LIB = <path to hss_lib_thread.a>
39-
```
40-
41-
Then build:
42-
43-
```
44-
$ make lms_example
45-
```
18+
If you want to use the old external integrations (`ext_lms.c`, `ext_xmss.c`),
19+
see the section "Building the External Integration examples".
4620

4721
## Signing and Verifying a Message with LMS/HSS
4822

@@ -78,31 +52,6 @@ examples:
7852
description:
7953
...
8054
```
81-
82-
# Building the XMSS/XMSS^MT example
83-
84-
If building with `--with-libxmss=<path>`, configure the Makefile to point to
85-
your xmss install:
86-
87-
```
88-
XMSS_INC = <path to patched xmss install>
89-
```
90-
91-
```
92-
XMSS_LIB = <path to xmss_lib.a or xmss_verify_lib.a>
93-
```
94-
95-
Then build:
96-
97-
```
98-
$ make xmss_example
99-
```
100-
101-
Build the verify-only example with
102-
```
103-
$ make xmss_example_verifyonly
104-
```
105-
10655
## Signing and Verifying a Message with XMSS/XMSS^MT
10756

10857
To see the help and usage, run the program without options:
@@ -144,64 +93,76 @@ number of levels in the hyper-tree. The number of signatures available
14493
is `N = 2 ** (h)`.
14594

14695
The main contributor to key generation time is the ratio `h/d`.
147-
Not surprisingly, be aware that `XMSS-SHA2_20_256`, and `XMSSMT-SHA2_60/3_256`, are particularly
148-
CPU intensive because of the large number of hash operations involved, and
149-
may take a long time. E.g. on an Intel i7 linux system these examples took
150-
approximately 24 min, and 1 hour, respectively.
96+
Not surprisingly, be aware that `XMSS-SHA2_20_256`, and `XMSSMT-SHA2_60/3_256`,
97+
are particularly CPU intensive because of the large number of hash operations
98+
involved, and may take a long time.
15199

152100
The other examples will be much faster.
153101

154102
An interesting facet of XMSS/XMSS^MT is that the private key format
155-
and size is implementation specific. The wolfSSL XMSS/XMSS^MT hooks
156-
feature uses the "fast" implementation from xmss-reference, which
157-
has larger private key sizes.
103+
and size is implementation specific. The wolfSSL XMSS/XMSS^MT default
104+
implementation has larger private key sizes for greater signing
105+
performance.
158106

159-
For example:
107+
For example this is obtained with the default `-enable-xmss` with
108+
`--enable-intelasm` (on an Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz):
160109

161110
```
162-
$ ./xmss_example "XMSSMT-SHA2_20/2_256" 200
111+
$time ./xmss_example "XMSSMT-SHA2_20/2_256" 200
163112
using parameters: XMSSMT-SHA2_20/2_256
164113
signature length: 4963
165-
priv key length: 6002
114+
priv key length: 5780
166115
pub key length: 68
116+
making key with XMSSMT-SHA2_20/2_256 parameters...
167117
...done!
168118
signing and verifying 200 signatures...
169119
...done!
170120
finished
171121
122+
real 0m2.115s
123+
user 0m2.104s
124+
sys 0m0.007s
172125
```
173126

127+
Versus the same with `--enable-xmss=small` instead:
128+
174129
```
175-
$ ./xmss_example "XMSSMT-SHA2_40/4_256" 200
176-
using parameters: XMSSMT-SHA2_40/4_256
177-
signature length: 9893
178-
priv key length: 15256
130+
$time ./xmss_example "XMSSMT-SHA2_20/2_256" 200
131+
using parameters: XMSSMT-SHA2_20/2_256
132+
signature length: 4963
133+
priv key length: 135
179134
pub key length: 68
135+
making key with XMSSMT-SHA2_20/2_256 parameters...
180136
...done!
181137
signing and verifying 200 signatures...
182138
...done!
183139
finished
140+
141+
real 6m57.413s
142+
user 6m56.337s
143+
sys 0m0.058s
184144
```
185145

186146
## Using the verify-only XMSS/XMSS^MT example
187147

188-
The usage for the verify-only example is:
148+
The verify-only XMSS example requires that wolfSSL has been built with
149+
`--enable-xmss=verify-only`. The usage for the verify-only example is:
189150
```
190-
$ ./xmss_example_verifyonly
151+
$ ./xmss_example
191152
usage:
192-
./xmss_example_verifyonly <param string> <pub file> <sig file> <msg file>
153+
./xmss_example <param string> <pub file> <sig file> <msg file>
193154
194155
For simplicity message is assumed to be 32 bytes in size.
195156
196157
examples:
197-
./xmss_example_verifyonly XMSSMT-SHA2_20/4_256 xmss_pub.key xmss_sig.bin msg.bin
198-
./xmss_example_verifyonly XMSSMT-SHA2_60/6_256 xmss_pub.key xmss_sig.bin msg.bin
199-
./xmss_example_verifyonly XMSS-SHA2_10_256 xmss_pub.key xmss_sig.bin msg.bin
158+
./xmss_example XMSSMT-SHA2_20/4_256 xmss_pub.key xmss_sig.bin msg.bin
159+
./xmss_example XMSSMT-SHA2_60/6_256 xmss_pub.key xmss_sig.bin msg.bin
160+
./xmss_example XMSS-SHA2_10_256 xmss_pub.key xmss_sig.bin msg.bin
200161
```
201162

202163
An example:
203164
```
204-
$./xmss_example_verifyonly XMSSMT-SHA2_20/2_256 pk.bin sig.bin msg.bin
165+
$./xmss_example XMSSMT-SHA2_20/2_256 pk.bin sig.bin msg.bin
205166
using parameters: XMSSMT-SHA2_20/2_256
206167
pub:
207168
0x00 0x00 0x00 0x01 0x2B 0xC1 0xA4 0x8D
@@ -223,3 +184,60 @@ pub key length: 68
223184
Verify good!
224185
finished
225186
```
187+
188+
189+
# Building the External Integration examples
190+
191+
By default wolfssl uses the wolfCrypt LMS/XMSS implementations. However
192+
the previous external integrations are still supported.
193+
194+
If building with `--with-liblms=<path>`, the LMS/HSS example requires
195+
that hash-sigs has been built. Please see Item 17 in the wolfSSL repo's INSTALL file.
196+
197+
https://github.com/wolfSSL/wolfssl/blob/master/INSTALL
198+
199+
If building with `--with-libxmss=<path>`, the XMSS/XMSS^MT example requires
200+
that the xmss-reference repository has been cloned, patched, and built. Please
201+
see item 20 in the wolfSSL repo's INSTALL file.
202+
203+
The patch to use is `0001-Patch-to-support-wolfSSL-xmss-reference-integration.patch`
204+
from this XMSS/XMSS^MT example. This patch includes an addendum readme,
205+
`patch_readme.md`, that lists all changes made and explains their rationale.
206+
207+
## Building the external LMS/HSS example
208+
209+
If building with `--with-liblms=<path>`, configure the Makefile to point
210+
to your hash-sigs install:
211+
212+
```
213+
HSS_INC = <path to hss install>
214+
```
215+
216+
```
217+
HSS_LIB = <path to hss_lib_thread.a>
218+
```
219+
220+
Then build:
221+
222+
```
223+
$ make lms_example
224+
```
225+
226+
## Building the external XMSS/XMSS^MT example
227+
228+
If building with `--with-libxmss=<path>`, configure the Makefile to point to
229+
your xmss install:
230+
231+
```
232+
XMSS_INC = <path to patched xmss install>
233+
```
234+
235+
```
236+
XMSS_LIB = <path to xmss_lib.a or xmss_verify_lib.a>
237+
```
238+
239+
Then build:
240+
241+
```
242+
$ make xmss_example
243+
```

pq/stateful_hash_sig/lms_example.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@
2424
#include <wolfssl/options.h>
2525
#include <wolfssl/wolfcrypt/error-crypt.h>
2626

27-
#ifdef HAVE_LIBLMS
27+
#ifdef WOLFSSL_HAVE_LMS
2828

2929
#include <wolfssl/wolfcrypt/lms.h>
30-
#include <wolfssl/wolfcrypt/ext_lms.h>
30+
#ifdef HAVE_LIBLMS
31+
#include <wolfssl/wolfcrypt/ext_lms.h>
32+
#else
33+
#include <wolfssl/wolfcrypt/wc_lms.h>
34+
#endif
3135

3236
static void print_usage(void);
3337
static int write_key_file(const byte * priv, word32 privSz, void * context);
3438
static int read_key_file(byte * priv, word32 privSz, void * context);
3539
static int do_lms_example(int levels, int height, int winternitz,
3640
size_t sigs_to_do);
37-
static void dump_hex(const char * what, const uint8_t * buf, size_t len);
41+
static void dump_hex(const char * what, const byte * buf, size_t len);
3842

3943
static WC_RNG rng;
4044

@@ -248,6 +252,12 @@ do_lms_example(int levels,
248252
goto exit_lms_example;
249253
}
250254

255+
ret = wc_LmsKey_Init(&verifyKey, NULL, 0);
256+
if (ret) {
257+
fprintf(stderr, "error: wc_LmsKey_Init returned %d\n", ret);
258+
goto exit_lms_example;
259+
}
260+
251261
ret = wc_LmsKey_SetParameters(&signingKey, levels, height, winternitz);
252262
if (ret) {
253263
fprintf(stderr, "error: wc_LmsKey_SetParameters(%d, %d, %d) returned %d\n",
@@ -374,9 +384,9 @@ do_lms_example(int levels,
374384
}
375385

376386
static void
377-
dump_hex(const char * what,
378-
const uint8_t * buf,
379-
size_t len)
387+
dump_hex(const char * what,
388+
const byte * buf,
389+
size_t len)
380390
{
381391
printf("%s\n", what);
382392
for (size_t i = 0; i < len; ++i) {
@@ -400,5 +410,5 @@ int main(int argc, char** argv) {
400410
printf("This requires the --with-liblms flag.\n");
401411
return 0;
402412
}
403-
#endif /* WITH_LILMS */
413+
#endif /* WOLFSSL_HAVE_LMS */
404414

0 commit comments

Comments
 (0)