Add LMS/HSS CL

This commit is contained in:
Zhi Guan
2026-06-24 10:23:18 +08:00
parent dadd2a3e0d
commit e7c0a28f9b
10 changed files with 1677 additions and 20 deletions

View File

@@ -15,6 +15,9 @@
#include <gmssl/mem.h>
#include <gmssl/error.h>
#include <gmssl/lms.h>
#ifdef ENABLE_LMS_CL
#include <gmssl/lms_cl.h>
#endif
static const char *usage = "-lms_types types -out file [-pubout file] [-verbose]\n";
@@ -55,8 +58,15 @@ int hsskeygen_main(int argc, char **argv)
uint8_t *pout = out;
uint8_t *ppubout = pubout;
size_t outlen = 0, puboutlen = 0;
#ifdef ENABLE_LMS_CL
LMS_CL_CTX cl_ctx;
int cl_initialized = 0;
#endif
memset(&key, 0, sizeof(key));
#ifdef ENABLE_LMS_CL
memset(&cl_ctx, 0, sizeof(cl_ctx));
#endif
argc--;
argv++;
@@ -129,10 +139,22 @@ bad:
goto end;
}
#ifdef ENABLE_LMS_CL
if (lms_cl_init(&cl_ctx) != 1) {
error_print();
goto end;
}
cl_initialized = 1;
if (hss_cl_key_generate(&cl_ctx, &key, lms_types_val, levels) != 1) {
error_print();
goto end;
}
#else
if (hss_key_generate(&key, lms_types_val, levels) != 1) {
error_print();
return -1;
}
#endif
if (verbose) {
hss_public_key_print(stderr, 0, 0, "hss_public_key", &key);
}
@@ -157,6 +179,9 @@ bad:
ret = 0;
end:
#ifdef ENABLE_LMS_CL
if (cl_initialized) lms_cl_cleanup(&cl_ctx);
#endif
hss_key_cleanup(&key);
gmssl_secure_clear(out, outlen);
if (outfile && outfp) fclose(outfp);

View File

@@ -15,6 +15,9 @@
#include <gmssl/mem.h>
#include <gmssl/error.h>
#include <gmssl/lms.h>
#ifdef ENABLE_LMS_CL
#include <gmssl/lms_cl.h>
#endif
static const char *usage = "-key file [-in file] [-out file] [-verbose]\n";
@@ -75,8 +78,15 @@ int hsssign_main(int argc, char **argv)
HSS_SIGN_CTX ctx;
uint8_t sig[HSS_SIGNATURE_MAX_SIZE];
size_t siglen;
#ifdef ENABLE_LMS_CL
LMS_CL_CTX cl_ctx;
int cl_initialized = 0;
#endif
memset(&key, 0, sizeof(key));
#ifdef ENABLE_LMS_CL
memset(&cl_ctx, 0, sizeof(cl_ctx));
#endif
argc--;
argv++;
@@ -136,10 +146,22 @@ bad:
fprintf(stderr, "%s: read private key failure\n", prog);
goto end;
}
#ifdef ENABLE_LMS_CL
if (lms_cl_init(&cl_ctx) != 1) {
error_print();
goto end;
}
cl_initialized = 1;
if (hss_cl_private_key_from_bytes(&cl_ctx, &key, &cp, &keylen) != 1) {
error_print();
goto end;
}
#else
if (hss_private_key_from_bytes(&key, &cp, &keylen) != 1) {
error_print();
goto end;
}
#endif
if (keylen) {
error_print();
goto end;
@@ -154,10 +176,17 @@ bad:
goto end;
}
#ifdef ENABLE_LMS_CL
if (hss_cl_sign_init(&cl_ctx, &ctx, &key) != 1) {
error_print();
goto end;
}
#else
if (hss_sign_init(&ctx, &key) != 1) {
error_print();
goto end;
}
#endif
while (1) {
uint8_t buf[1024];
@@ -185,6 +214,9 @@ bad:
ret = 0;
end:
#ifdef ENABLE_LMS_CL
if (cl_initialized) lms_cl_cleanup(&cl_ctx);
#endif
hss_key_cleanup(&key);
gmssl_secure_clear(keybuf, sizeof(keybuf));
gmssl_secure_clear(&ctx, sizeof(ctx));

View File

@@ -15,6 +15,9 @@
#include <gmssl/mem.h>
#include <gmssl/error.h>
#include <gmssl/lms.h>
#ifdef ENABLE_LMS_CL
#include <gmssl/lms_cl.h>
#endif
static const char *usage = "-lms_type type -out file [-pubout file] [-verbose]\n";
@@ -49,8 +52,15 @@ int lmskeygen_main(int argc, char **argv)
uint8_t *pout = out;
uint8_t *ppubout = pubout;
size_t outlen = 0, puboutlen = 0;
#ifdef ENABLE_LMS_CL
LMS_CL_CTX cl_ctx;
int cl_initialized = 0;
#endif
memset(&key, 0, sizeof(key));
#ifdef ENABLE_LMS_CL
memset(&cl_ctx, 0, sizeof(cl_ctx));
#endif
argc--;
argv++;
@@ -110,10 +120,22 @@ bad:
goto end;
}
#ifdef ENABLE_LMS_CL
if (lms_cl_init(&cl_ctx) != 1) {
error_print();
goto end;
}
cl_initialized = 1;
if (lms_cl_key_generate(&cl_ctx, &key, lms_type_val) != 1) {
error_print();
goto end;
}
#else
if (lms_key_generate(&key, lms_type_val) != 1) {
error_print();
return -1;
}
#endif
if (verbose) {
lms_public_key_print(stderr, 0, 0, "lms_public_key", &key);
}
@@ -138,6 +160,9 @@ bad:
ret = 0;
end:
#ifdef ENABLE_LMS_CL
if (cl_initialized) lms_cl_cleanup(&cl_ctx);
#endif
lms_key_cleanup(&key);
gmssl_secure_clear(out, outlen);
if (outfile && outfp) fclose(outfp);

View File

@@ -15,6 +15,9 @@
#include <gmssl/mem.h>
#include <gmssl/error.h>
#include <gmssl/lms.h>
#ifdef ENABLE_LMS_CL
#include <gmssl/lms_cl.h>
#endif
static const char *usage = "-key file [-in file] [-out file] [-verbose]\n";
@@ -75,8 +78,15 @@ int lmssign_main(int argc, char **argv)
LMS_SIGN_CTX ctx;
uint8_t sig[LMS_SIGNATURE_MAX_SIZE];
size_t siglen;
#ifdef ENABLE_LMS_CL
LMS_CL_CTX cl_ctx;
int cl_initialized = 0;
#endif
memset(&key, 0, sizeof(key));
#ifdef ENABLE_LMS_CL
memset(&cl_ctx, 0, sizeof(cl_ctx));
#endif
argc--;
argv++;
@@ -136,10 +146,22 @@ bad:
fprintf(stderr, "%s: read private key failure\n", prog);
goto end;
}
#ifdef ENABLE_LMS_CL
if (lms_cl_init(&cl_ctx) != 1) {
error_print();
goto end;
}
cl_initialized = 1;
if (lms_cl_private_key_from_bytes(&cl_ctx, &key, &cp, &keylen) != 1) {
error_print();
goto end;
}
#else
if (lms_private_key_from_bytes(&key, &cp, &keylen) != 1) {
error_print();
goto end;
}
#endif
if (keylen) {
error_print();
return -1;
@@ -185,6 +207,9 @@ bad:
ret = 0;
end:
#ifdef ENABLE_LMS_CL
if (cl_initialized) lms_cl_cleanup(&cl_ctx);
#endif
lms_key_cleanup(&key);
gmssl_secure_clear(keybuf, sizeof(keybuf));
gmssl_secure_clear(&ctx, sizeof(ctx));