Skip to content

Commit 9488128

Browse files
committed
Add LMS key_update callback
1 parent 47639a9 commit 9488128

File tree

9 files changed

+344
-363
lines changed

9 files changed

+344
-363
lines changed

include/gmssl/lms.h

Lines changed: 120 additions & 128 deletions
Large diffs are not rendered by default.

src/lms.c

Lines changed: 138 additions & 196 deletions
Large diffs are not rendered by default.

src/x509_key.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ int x509_public_key_print(FILE *fp, int fmt, int ind, const char *label, const X
162162
}
163163
break;
164164
case OID_lms_hashsig:
165-
if (lms_public_key_print(fp, fmt, ind, label, &key->u.lms_key.public_key) != 1) {
165+
if (lms_public_key_print(fp, fmt, ind, label, &key->u.lms_key) != 1) {
166166
error_print();
167167
return -1;
168168
}

tests/lmstest.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ static int test_lms_key_generate(void)
294294
error_print();
295295
return -1;
296296
}
297-
//lms_key_print(stdout, 0, 0, "lms_key", &lms_key);
297+
lms_private_key_print(stdout, 0, 0, "lms_private_key", &lms_key);
298298

299299
printf("%s() ok\n", __FUNCTION__);
300300
return 1;
@@ -341,13 +341,13 @@ static int test_lms_key_to_bytes(void)
341341
error_print();
342342
return -1;
343343
}
344-
lms_key_print(stdout, 0, 4, "lms_public_key", &key);
344+
lms_public_key_print(stdout, 0, 4, "lms_public_key", &key);
345345

346346
if (lms_private_key_from_bytes(&key, &cp, &len) != 1) {
347347
error_print();
348348
return -1;
349349
}
350-
lms_key_print(stdout, 0, 4, "lms_private_key", &key);
350+
lms_private_key_print(stdout, 0, 4, "lms_private_key", &key);
351351
if (len != 0) {
352352
error_print();
353353
return -1;
@@ -539,7 +539,7 @@ static int test_hss_key_generate(void)
539539
}
540540

541541
hss_public_key_print(stdout, 0, 4, "hss_public_key", &key);
542-
hss_key_print(stdout, 0, 4, "hss_key", &key);
542+
hss_private_key_print(stdout, 0, 4, "hss_key", &key);
543543

544544
printf("%s() ok\n", __FUNCTION__);
545545
return 1;
@@ -799,7 +799,7 @@ static int test_hss_key_to_bytes(void)
799799
error_print();
800800
return -1;
801801
}
802-
hss_key_print(stdout, 0, 4, "lms_private_key", &key);
802+
hss_private_key_print(stdout, 0, 4, "lms_private_key", &key);
803803
if (len != 0) {
804804
error_print();
805805
return -1;
@@ -868,7 +868,7 @@ static int test_hss_sign_level2(void)
868868
error_print();
869869
return -1;
870870
}
871-
hss_key_print(stderr, 0, 4, "hss_key", &key);
871+
hss_private_key_print(stderr, 0, 4, "hss_key", &key);
872872

873873

874874
if (hss_sign_init(&ctx, &key) != 1) {
@@ -916,7 +916,7 @@ static int test_hss_sign(void)
916916
error_print();
917917
return -1;
918918
}
919-
hss_key_print(stderr, 0, 4, "hss_key", &key);
919+
hss_private_key_print(stderr, 0, 4, "hss_key", &key);
920920

921921

922922
if (hss_sign_init(&ctx, &key) != 1) {
@@ -951,6 +951,7 @@ static int test_hss_sign(void)
951951
return 1;
952952
}
953953

954+
/*
954955
static int test_hss_public_key_algor(void)
955956
{
956957
int lms_types[] = {
@@ -1025,7 +1026,7 @@ static int test_hss_public_key_algor(void)
10251026
return 1;
10261027
10271028
}
1028-
1029+
*/
10291030

10301031
int main(void)
10311032
{
@@ -1048,7 +1049,7 @@ int main(void)
10481049
if (test_hss_sign_level1() != 1) goto err;
10491050
if (test_hss_sign_level2() != 1) goto err;
10501051
if (test_hss_sign() != 1) goto err;
1051-
if (test_hss_public_key_algor() != 1) goto err;
1052+
// if (test_hss_public_key_algor() != 1) goto err;
10521053

10531054
printf("%s all tests passed\n", __FILE__);
10541055
return 0;

tools/gmssl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ extern int tls12_client_main(int argc, char **argv);
6464
extern int tls12_server_main(int argc, char **argv);
6565
extern int tls13_client_main(int argc, char **argv);
6666
extern int tls13_server_main(int argc, char **argv);
67-
#ifdef ENABLE_LMS_HSS
67+
#ifdef ENABLE_LMS
6868
extern int lmskeygen_main(int argc, char **argv);
6969
extern int lmssign_main(int argc, char **argv);
7070
extern int lmsverify_main(int argc, char **argv);
@@ -154,7 +154,7 @@ static const char *options =
154154
" cmsdecrypt Decrypt CMS EnvelopedData\n"
155155
" cmssign Generate CMS SignedData\n"
156156
" cmsverify Verify CMS SignedData\n"
157-
#ifdef ENABLE_LMS_HSS
157+
#ifdef ENABLE_LMS
158158
" lmskeygen Generate LMS-SM3 (Leighton-Micali Signature) keypair\n"
159159
" lmssign Generate LMS-SM3 signature\n"
160160
" lmsverify Verify LMS-SM3 signature\n"
@@ -334,7 +334,7 @@ int main(int argc, char **argv)
334334
return tls13_client_main(argc, argv);
335335
} else if (!strcmp(*argv, "tls13_server")) {
336336
return tls13_server_main(argc, argv);
337-
#ifdef ENABLE_LMS_HSS
337+
#ifdef ENABLE_LMS
338338
} else if (!strcmp(*argv, "lmskeygen")) {
339339
return lmskeygen_main(argc, argv);
340340
} else if (!strcmp(*argv, "lmssign")) {

tools/hsssign.c

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2025 The GmSSL Project. All Rights Reserved.
2+
* Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the License); you may
55
* not use this file except in compliance with the License.
@@ -26,6 +26,36 @@ static const char *options =
2626
" -verbose Print public key and signature\n"
2727
"\n";
2828

29+
static int key_update_cb(HSS_KEY *key)
30+
{
31+
FILE *fp;
32+
uint8_t buf[HSS_PRIVATE_KEY_MAX_SIZE];
33+
uint8_t *p = buf;
34+
size_t len = 0;
35+
36+
if (!key->update_param) {
37+
error_print();
38+
return -1;
39+
}
40+
fp = (FILE *)key->update_param;
41+
42+
if (hss_private_key_to_bytes(key, &p, &len) != 1) {
43+
error_print();
44+
return -1;
45+
}
46+
rewind(fp);
47+
if (fwrite(buf, 1, len, fp) != len
48+
|| fflush(fp) != 0) {
49+
gmssl_secure_clear(buf, sizeof(buf));
50+
error_print();
51+
return -1;
52+
}
53+
// TODO: need fsync to make sure data is written to disk
54+
// but fsync need <unistd.h>, not std C
55+
gmssl_secure_clear(buf, sizeof(buf));
56+
return 1;
57+
}
58+
2959
int hsssign_main(int argc, char **argv)
3060
{
3161
int ret = 1;
@@ -112,28 +142,21 @@ int hsssign_main(int argc, char **argv)
112142
}
113143
if (keylen) {
114144
error_print();
115-
return -1;
145+
goto end;
116146
}
117147

118148
if (verbose) {
119149
hss_public_key_print(stderr, 0, 0, "hss_public_key", &key);
120150
}
121151

122-
if (hss_sign_init(&ctx, &key) != 1) {
152+
if (hss_key_set_update_callback(&key, key_update_cb, keyfp) != 1) {
123153
error_print();
124154
goto end;
125155
}
126156

127-
// write updated key back to file
128-
// TODO: write back `q` only
129-
if (hss_private_key_to_bytes(&key, &p, &keylen) != 1) {
130-
error_print();
131-
return -1;
132-
}
133-
rewind(keyfp);
134-
if (fwrite(keybuf, 1, keylen, keyfp) != keylen) {
157+
if (hss_sign_init(&ctx, &key) != 1) {
135158
error_print();
136-
return -1;
159+
goto end;
137160
}
138161

139162
while (1) {

tools/lmskeygen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ int lmskeygen_main(int argc, char **argv)
115115
return -1;
116116
}
117117
if (verbose) {
118-
lms_public_key_print(stderr, 0, 0, "lms_public_key", &key.public_key);
118+
lms_public_key_print(stderr, 0, 0, "lms_public_key", &key);
119119
}
120120

121121
if (lms_private_key_to_bytes(&key, &pout, &outlen) != 1) {

tools/lmssign.c

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2025 The GmSSL Project. All Rights Reserved.
2+
* Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the License); you may
55
* not use this file except in compliance with the License.
@@ -26,6 +26,36 @@ static const char *options =
2626
" -verbose Print public key and signature\n"
2727
"\n";
2828

29+
static int key_update_cb(LMS_KEY *key)
30+
{
31+
FILE *fp;
32+
uint8_t buf[LMS_PRIVATE_KEY_SIZE];
33+
uint8_t *p = buf;
34+
size_t len = 0;
35+
36+
if (!key->update_param) {
37+
error_print();
38+
return -1;
39+
}
40+
fp = (FILE *)key->update_param;
41+
42+
if (lms_private_key_to_bytes(key, &p, &len) != 1) {
43+
error_print();
44+
return -1;
45+
}
46+
rewind(fp);
47+
if (fwrite(buf, 1, len, fp) != len
48+
|| fflush(fp) != 0) {
49+
gmssl_secure_clear(buf, sizeof(buf));
50+
error_print();
51+
return -1;
52+
}
53+
// TODO: need fsync to make sure data is written to disk
54+
// but fsync need <unistd.h>, not std C
55+
gmssl_secure_clear(buf, sizeof(buf));
56+
return 1;
57+
}
58+
2959
int lmssign_main(int argc, char **argv)
3060
{
3161
int ret = 1;
@@ -116,24 +146,17 @@ int lmssign_main(int argc, char **argv)
116146
}
117147

118148
if (verbose) {
119-
lms_public_key_print(stderr, 0, 0, "lms_public_key", &key.public_key);
149+
lms_public_key_print(stderr, 0, 0, "lms_public_key", &key);
120150
}
121151

122-
if (lms_sign_init(&ctx, &key) != 1) {
152+
if (lms_key_set_update_callback(&key, key_update_cb, keyfp) != 1) {
123153
error_print();
124154
goto end;
125155
}
126156

127-
// write updated key back to file
128-
// TODO: write back `q` only
129-
if (lms_private_key_to_bytes(&key, &p, &keylen) != 1) {
130-
error_print();
131-
return -1;
132-
}
133-
rewind(keyfp);
134-
if (fwrite(keybuf, 1, keylen, keyfp) != keylen) {
157+
if (lms_sign_init(&ctx, &key) != 1) {
135158
error_print();
136-
return -1;
159+
goto end;
137160
}
138161

139162
while (1) {

tools/lmsverify.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ int lmsverify_main(int argc, char **argv)
113113
goto end;
114114
}
115115
if (verbose) {
116-
lms_public_key_print(stderr, 0, 0, "lms_public_key", &key.public_key);
116+
lms_public_key_print(stderr, 0, 0, "lms_public_key", &key);
117117
}
118118

119119
// read signature even if signature not compatible with the public key

0 commit comments

Comments
 (0)