Add LMS key_update callback

This commit is contained in:
Zhi Guan
2026-01-18 12:12:45 +08:00
parent 47639a9e23
commit 9488128154
9 changed files with 355 additions and 374 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2025 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
@@ -26,6 +26,36 @@ static const char *options =
" -verbose Print public key and signature\n"
"\n";
static int key_update_cb(HSS_KEY *key)
{
FILE *fp;
uint8_t buf[HSS_PRIVATE_KEY_MAX_SIZE];
uint8_t *p = buf;
size_t len = 0;
if (!key->update_param) {
error_print();
return -1;
}
fp = (FILE *)key->update_param;
if (hss_private_key_to_bytes(key, &p, &len) != 1) {
error_print();
return -1;
}
rewind(fp);
if (fwrite(buf, 1, len, fp) != len
|| fflush(fp) != 0) {
gmssl_secure_clear(buf, sizeof(buf));
error_print();
return -1;
}
// TODO: need fsync to make sure data is written to disk
// but fsync need <unistd.h>, not std C
gmssl_secure_clear(buf, sizeof(buf));
return 1;
}
int hsssign_main(int argc, char **argv)
{
int ret = 1;
@@ -112,28 +142,21 @@ bad:
}
if (keylen) {
error_print();
return -1;
goto end;
}
if (verbose) {
hss_public_key_print(stderr, 0, 0, "hss_public_key", &key);
}
if (hss_sign_init(&ctx, &key) != 1) {
if (hss_key_set_update_callback(&key, key_update_cb, keyfp) != 1) {
error_print();
goto end;
}
// write updated key back to file
// TODO: write back `q` only
if (hss_private_key_to_bytes(&key, &p, &keylen) != 1) {
if (hss_sign_init(&ctx, &key) != 1) {
error_print();
return -1;
}
rewind(keyfp);
if (fwrite(keybuf, 1, keylen, keyfp) != keylen) {
error_print();
return -1;
goto end;
}
while (1) {