mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-06 16:36:16 +08:00
Add LMS key_update callback
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user