Add XMSS CL

This commit is contained in:
Zhi Guan
2026-06-24 10:56:50 +08:00
parent e7c0a28f9b
commit 03c8276501
9 changed files with 1418 additions and 3 deletions

View File

@@ -15,6 +15,9 @@
#include <gmssl/mem.h>
#include <gmssl/error.h>
#include <gmssl/xmss.h>
#ifdef ENABLE_XMSS_CL
#include <gmssl/xmss_cl.h>
#endif
static const char *usage = "-xmss_type type -out file [-pubout file] [-verbose]\n";
@@ -48,8 +51,15 @@ int xmsskeygen_main(int argc, char **argv)
uint8_t pubout[XMSS_PUBLIC_KEY_SIZE];
uint8_t *ppubout;
size_t puboutlen ;
#ifdef ENABLE_XMSS_CL
XMSS_CL_CTX cl_ctx;
int cl_inited = 0;
#endif
memset(&key, 0, sizeof(key));
#ifdef ENABLE_XMSS_CL
memset(&cl_ctx, 0, sizeof(cl_ctx));
#endif
argc--;
argv++;
@@ -109,10 +119,22 @@ bad:
goto end;
}
#ifdef ENABLE_XMSS_CL
if (xmss_cl_init(&cl_ctx) != 1) {
error_print();
goto end;
}
cl_inited = 1;
if (xmss_cl_key_generate(&cl_ctx, &key, xmss_type_val) != 1) {
error_print();
goto end;
}
#else
if (xmss_key_generate(&key, xmss_type_val) != 1) {
error_print();
return -1;
}
#endif
if (verbose) {
xmss_public_key_print(stderr, 0, 0, "xmss_public_key", &key);
}
@@ -154,6 +176,9 @@ bad:
ret = 0;
end:
#ifdef ENABLE_XMSS_CL
if (cl_inited) xmss_cl_cleanup(&cl_ctx);
#endif
xmss_key_cleanup(&key);
if (out) {
gmssl_secure_clear(out, outlen);

View File

@@ -15,6 +15,9 @@
#include <gmssl/mem.h>
#include <gmssl/error.h>
#include <gmssl/xmss.h>
#ifdef ENABLE_XMSS_CL
#include <gmssl/xmss_cl.h>
#endif
static const char *usage = "-xmssmt_type type -out file [-pubout file] [-verbose]\n";
@@ -52,8 +55,15 @@ int xmssmtkeygen_main(int argc, char **argv)
uint8_t *pout;
uint8_t *ppubout = pubout;
size_t outlen = 0, puboutlen = 0;
#ifdef ENABLE_XMSS_CL
XMSS_CL_CTX cl_ctx;
int cl_inited = 0;
#endif
memset(&key, 0, sizeof(key));
#ifdef ENABLE_XMSS_CL
memset(&cl_ctx, 0, sizeof(cl_ctx));
#endif
argc--;
argv++;
@@ -113,10 +123,22 @@ bad:
goto end;
}
#ifdef ENABLE_XMSS_CL
if (xmss_cl_init(&cl_ctx) != 1) {
error_print();
goto end;
}
cl_inited = 1;
if (xmssmt_cl_key_generate(&cl_ctx, &key, xmssmt_type_val) != 1) {
error_print();
goto end;
}
#else
if (xmssmt_key_generate(&key, xmssmt_type_val) != 1) {
error_print();
return -1;
}
#endif
if (verbose) {
xmssmt_public_key_print(stderr, 0, 0, "xmssmt_public_key", &key);
}
@@ -151,6 +173,9 @@ bad:
ret = 0;
end:
#ifdef ENABLE_XMSS_CL
if (cl_inited) xmss_cl_cleanup(&cl_ctx);
#endif
xmssmt_key_cleanup(&key);
if (out) {
gmssl_secure_clear(out, outlen);

View File

@@ -15,6 +15,9 @@
#include <gmssl/mem.h>
#include <gmssl/error.h>
#include <gmssl/xmss.h>
#ifdef ENABLE_XMSS_CL
#include <gmssl/xmss_cl.h>
#endif
static const char *usage = "-key file [-in file] [-out file] [-verbose]\n";
@@ -69,8 +72,15 @@ int xmssmtsign_main(int argc, char **argv)
XMSSMT_SIGN_CTX ctx;
uint8_t sig[XMSSMT_SIGNATURE_MAX_SIZE];
size_t siglen;
#ifdef ENABLE_XMSS_CL
XMSS_CL_CTX cl_ctx;
int cl_inited = 0;
#endif
memset(&key, 0, sizeof(key));
#ifdef ENABLE_XMSS_CL
memset(&cl_ctx, 0, sizeof(cl_ctx));
#endif
argc--;
argv++;
@@ -126,6 +136,14 @@ bad:
goto end;
}
#ifdef ENABLE_XMSS_CL
if (xmss_cl_init(&cl_ctx) != 1) {
error_print();
goto end;
}
cl_inited = 1;
#endif
if (xmssmt_private_key_from_file(&key, keyfp) != 1) {
fprintf(stderr, "%s: read private key failure\n", prog);
goto end;
@@ -140,10 +158,17 @@ bad:
goto end;
}
#ifdef ENABLE_XMSS_CL
if (xmssmt_cl_sign_init(&cl_ctx, &ctx, &key) != 1) {
error_print();
goto end;
}
#else
if (xmssmt_sign_init(&ctx, &key) != 1) {
error_print();
goto end;
}
#endif
while (1) {
uint8_t buf[1024];
@@ -171,6 +196,9 @@ bad:
ret = 0;
end:
#ifdef ENABLE_XMSS_CL
if (cl_inited) xmss_cl_cleanup(&cl_ctx);
#endif
xmssmt_key_cleanup(&key);
gmssl_secure_clear(&ctx, sizeof(ctx));
if (keyfp) fclose(keyfp);

View File

@@ -16,6 +16,9 @@
#include <gmssl/error.h>
#include <gmssl/endian.h>
#include <gmssl/xmss.h>
#ifdef ENABLE_XMSS_CL
#include <gmssl/xmss_cl.h>
#endif
static const char *usage = "-key file [-in file] [-out file] [-verbose]\n";
@@ -68,8 +71,17 @@ int xmsssign_main(int argc, char **argv)
XMSS_SIGN_CTX ctx;
uint8_t sig[XMSS_SIGNATURE_MAX_SIZE];
size_t siglen;
#ifdef ENABLE_XMSS_CL
XMSS_CL_CTX cl_ctx;
uint8_t *keybuf = NULL;
size_t keylen = 0;
int cl_inited = 0;
#endif
memset(&key, 0, sizeof(key));
#ifdef ENABLE_XMSS_CL
memset(&cl_ctx, 0, sizeof(cl_ctx));
#endif
argc--;
argv++;
@@ -125,10 +137,40 @@ bad:
goto end;
}
#ifdef ENABLE_XMSS_CL
if (xmss_cl_init(&cl_ctx) != 1) {
error_print();
goto end;
}
cl_inited = 1;
if (fseek(keyfp, 0, SEEK_END) != 0
|| (keylen = (size_t)ftell(keyfp)) == (size_t)-1
|| fseek(keyfp, 0, SEEK_SET) != 0) {
error_print();
goto end;
}
if (!(keybuf = malloc(keylen))) {
error_print();
goto end;
}
if (fread(keybuf, 1, keylen, keyfp) != keylen) {
fprintf(stderr, "%s: read private key failure\n", prog);
goto end;
}
{
const uint8_t *cp = keybuf;
size_t len = keylen;
if (xmss_cl_private_key_from_bytes(&cl_ctx, &key, &cp, &len) != 1 || len) {
fprintf(stderr, "%s: read private key failure\n", prog);
goto end;
}
}
#else
if (xmss_private_key_from_file(&key, keyfp) != 1) {
fprintf(stderr, "%s: read private key failure\n", prog);
goto end;
}
#endif
if (verbose) {
xmss_public_key_print(stderr, 0, 0, "lms_public_key", &key);
@@ -170,6 +212,13 @@ bad:
ret = 0;
end:
#ifdef ENABLE_XMSS_CL
if (cl_inited) xmss_cl_cleanup(&cl_ctx);
if (keybuf) {
gmssl_secure_clear(keybuf, keylen);
free(keybuf);
}
#endif
xmss_key_cleanup(&key);
gmssl_secure_clear(&ctx, sizeof(ctx));
if (keyfp) fclose(keyfp);