mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-09-23 05:44:07 +08:00
Add LMS/HSS CL
This commit is contained in:
@@ -112,6 +112,7 @@ option(ENABLE_SM3_SSE "Enable SM3 SSE assembly implementation" ${GMSSL_DEFAULT_E
|
|||||||
|
|
||||||
option(ENABLE_SM4_CTR_AESNI_AVX "Enable SM4 CTR AESNI+AVX assembly implementation" OFF)
|
option(ENABLE_SM4_CTR_AESNI_AVX "Enable SM4 CTR AESNI+AVX assembly implementation" OFF)
|
||||||
option(ENABLE_SM4_CL "Enable SM4 OpenCL" OFF)
|
option(ENABLE_SM4_CL "Enable SM4 OpenCL" OFF)
|
||||||
|
option(ENABLE_LMS_CL "Enable LMS OpenCL" OFF)
|
||||||
|
|
||||||
|
|
||||||
option(ENABLE_INTEL_RDRAND "Enable Intel RDRAND instructions" OFF)
|
option(ENABLE_INTEL_RDRAND "Enable Intel RDRAND instructions" OFF)
|
||||||
@@ -538,6 +539,19 @@ if (ENABLE_LMS)
|
|||||||
list(APPEND tests lms)
|
list(APPEND tests lms)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if (ENABLE_LMS_CL)
|
||||||
|
if (NOT ENABLE_LMS)
|
||||||
|
message(FATAL_ERROR "ENABLE_LMS_CL requires ENABLE_LMS")
|
||||||
|
endif()
|
||||||
|
message(STATUS "ENABLE_LMS_CL is ON")
|
||||||
|
add_definitions(-DENABLE_LMS_CL)
|
||||||
|
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
|
||||||
|
add_definitions(-DMACOS) # to include <OpenCL/OpenCL.h>
|
||||||
|
endif()
|
||||||
|
list(APPEND src src/lms_cl.c)
|
||||||
|
list(APPEND tests lms_cl)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
if (ENABLE_XMSS)
|
if (ENABLE_XMSS)
|
||||||
message(STATUS "ENABLE_XMSS is ON")
|
message(STATUS "ENABLE_XMSS is ON")
|
||||||
@@ -761,10 +775,6 @@ elseif (APPLE)
|
|||||||
target_link_libraries(gmssl dl)
|
target_link_libraries(gmssl dl)
|
||||||
endif()
|
endif()
|
||||||
target_link_libraries(gmssl "-framework Security")
|
target_link_libraries(gmssl "-framework Security")
|
||||||
if (ENABLE_SM4_CL)
|
|
||||||
# FIXME: different rules for cl and OpenCL framework
|
|
||||||
target_link_libraries(gmssl "-framework OpenCL")
|
|
||||||
endif()
|
|
||||||
#target_link_libraries(gmssl "-framework CoreFoundation") # rand_apple.c CFRelease()
|
#target_link_libraries(gmssl "-framework CoreFoundation") # rand_apple.c CFRelease()
|
||||||
elseif (MINGW)
|
elseif (MINGW)
|
||||||
target_link_libraries(gmssl PRIVATE wsock32)
|
target_link_libraries(gmssl PRIVATE wsock32)
|
||||||
@@ -774,6 +784,16 @@ else()
|
|||||||
endif()
|
endif()
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if (ENABLE_SM4_CL OR ENABLE_LMS_CL)
|
||||||
|
if (APPLE)
|
||||||
|
target_link_libraries(gmssl "-framework OpenCL")
|
||||||
|
else()
|
||||||
|
find_package(OpenCL REQUIRED)
|
||||||
|
target_include_directories(gmssl PRIVATE ${OpenCL_INCLUDE_DIRS})
|
||||||
|
target_link_libraries(gmssl ${OpenCL_LIBRARIES})
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
|
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
|
||||||
|
|
||||||
@@ -947,7 +967,7 @@ endif()
|
|||||||
#
|
#
|
||||||
set(CPACK_PACKAGE_NAME "GmSSL")
|
set(CPACK_PACKAGE_NAME "GmSSL")
|
||||||
set(CPACK_PACKAGE_VENDOR "GmSSL develop team")
|
set(CPACK_PACKAGE_VENDOR "GmSSL develop team")
|
||||||
set(CPACK_PACKAGE_VERSION "3.3.0-dev.1163")
|
set(CPACK_PACKAGE_VERSION "3.3.0-dev.1164")
|
||||||
set(CPACK_PACKAGE_DESCRIPTION_FILE ${PROJECT_SOURCE_DIR}/README.md)
|
set(CPACK_PACKAGE_DESCRIPTION_FILE ${PROJECT_SOURCE_DIR}/README.md)
|
||||||
set(CPACK_NSIS_MODIFY_PATH ON)
|
set(CPACK_NSIS_MODIFY_PATH ON)
|
||||||
include(CPack)
|
include(CPack)
|
||||||
|
|||||||
63
include/gmssl/lms_cl.h
Normal file
63
include/gmssl/lms_cl.h
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GMSSL_LMS_CL_H
|
||||||
|
#define GMSSL_LMS_CL_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <gmssl/lms.h>
|
||||||
|
#ifdef MACOS
|
||||||
|
#include <OpenCL/OpenCL.h>
|
||||||
|
#else
|
||||||
|
#include <CL/cl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
cl_context context;
|
||||||
|
cl_command_queue queue;
|
||||||
|
cl_program program;
|
||||||
|
cl_kernel leafs_tree_kernel;
|
||||||
|
cl_kernel leafs_compact_kernel;
|
||||||
|
cl_kernel internal_nodes_kernel;
|
||||||
|
size_t local_work_size;
|
||||||
|
size_t max_leaf_batch;
|
||||||
|
} LMS_CL_CTX;
|
||||||
|
|
||||||
|
|
||||||
|
int lms_cl_init(LMS_CL_CTX *ctx);
|
||||||
|
void lms_cl_cleanup(LMS_CL_CTX *ctx);
|
||||||
|
|
||||||
|
int lms_cl_derive_merkle_tree(LMS_CL_CTX *ctx,
|
||||||
|
const lms_sm3_digest_t seed, const uint8_t I[16], int height, lms_sm3_digest_t *tree);
|
||||||
|
int lms_cl_derive_merkle_root(LMS_CL_CTX *ctx,
|
||||||
|
const lms_sm3_digest_t seed, const uint8_t I[16], int height, lms_sm3_digest_t root);
|
||||||
|
|
||||||
|
int lms_cl_key_generate_ex(LMS_CL_CTX *ctx, LMS_KEY *key, int lms_type,
|
||||||
|
const lms_sm3_digest_t seed, const uint8_t I[16], int cache_tree);
|
||||||
|
int lms_cl_key_generate(LMS_CL_CTX *ctx, LMS_KEY *key, int lms_type);
|
||||||
|
int lms_cl_private_key_from_bytes(LMS_CL_CTX *ctx, LMS_KEY *key, const uint8_t **in, size_t *inlen);
|
||||||
|
|
||||||
|
int hss_cl_key_generate(LMS_CL_CTX *ctx, HSS_KEY *key, const int *lms_types, size_t levels);
|
||||||
|
int hss_cl_private_key_from_bytes(LMS_CL_CTX *ctx, HSS_KEY *key, const uint8_t **in, size_t *inlen);
|
||||||
|
int hss_cl_key_update(LMS_CL_CTX *ctx, HSS_KEY *key);
|
||||||
|
int hss_cl_sign_init(LMS_CL_CTX *ctx, HSS_SIGN_CTX *sign_ctx, HSS_KEY *key);
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
@@ -18,7 +18,7 @@ extern "C" {
|
|||||||
|
|
||||||
|
|
||||||
#define GMSSL_VERSION_NUM 30300
|
#define GMSSL_VERSION_NUM 30300
|
||||||
#define GMSSL_VERSION_STR "GmSSL 3.3.0-dev.1163"
|
#define GMSSL_VERSION_STR "GmSSL 3.3.0-dev.1164"
|
||||||
|
|
||||||
int gmssl_version_num(void);
|
int gmssl_version_num(void);
|
||||||
const char *gmssl_version_str(void);
|
const char *gmssl_version_str(void);
|
||||||
|
|||||||
1168
src/lms_cl.c
Normal file
1168
src/lms_cl.c
Normal file
File diff suppressed because it is too large
Load Diff
38
src/tlcp.c
38
src/tlcp.c
@@ -91,22 +91,32 @@ static int tlcp_cipher_suite_is_ecdhe(int cipher_suite)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
ServerKeyExchange
|
服务器Certificate
|
||||||
|
|
||||||
|
常规采用ECC_, ECDHE_ 套件时,服务器证书是双证书证书链
|
||||||
|
|
||||||
|
opaque ASN.1Cert<1..2^24-1>;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
ASN.1Cert certificate<0..2^24-1>;
|
||||||
|
} Certificate;
|
||||||
|
|
||||||
|
但是采用IBC_ 套件时,服务器证书消息变为
|
||||||
|
|
||||||
|
opaque ASN1.1IBCParam<1..2^24-1>;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
opaque ibc_id<1..2^16-1>;
|
||||||
|
ASN1.1IBCParam ibc_parameter;
|
||||||
|
} Certificate;
|
||||||
|
|
||||||
|
其中ibc_id的负载数据为服务器的域名
|
||||||
|
ibc_parameter 是SM9_ENC_MASTER_PUBLIC_KEY 的der编码
|
||||||
|
即sm9_enc_master_public_key_to_der的编码
|
||||||
|
|
||||||
|
客户端密钥交换的方式和ECC_套件类似,用sm9_encrypt(ibc_parameter, ibc_id)加密pre_master_secret,然后把密文放到ClientKeyExchange中.
|
||||||
|
|
||||||
select (KeyExchangeAlgorithm) {
|
|
||||||
case ECC:
|
|
||||||
digitall-signed struct {
|
|
||||||
opaque client_random[32];
|
|
||||||
opaque server_random[32];
|
|
||||||
opaque ASN1.Cert<1..2^24-1>;
|
|
||||||
} signed_params;
|
|
||||||
|
|
||||||
case ECDHE:
|
|
||||||
ServerECDHEParams params;
|
|
||||||
digitally-signed struct {
|
|
||||||
opaque client_random[32];
|
|
||||||
opaque server_random[32];
|
|
||||||
ServerECDHEParams params;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
289
tests/lms_cltest.c
Normal file
289
tests/lms_cltest.c
Normal file
@@ -0,0 +1,289 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#else
|
||||||
|
#include <sys/time.h>
|
||||||
|
#endif
|
||||||
|
#include <gmssl/lms.h>
|
||||||
|
#include <gmssl/lms_cl.h>
|
||||||
|
#include <gmssl/error.h>
|
||||||
|
|
||||||
|
|
||||||
|
static int test_lms_cl_derive_merkle_tree(void)
|
||||||
|
{
|
||||||
|
LMS_CL_CTX ctx;
|
||||||
|
lms_sm3_digest_t seed = {0};
|
||||||
|
uint8_t I[16] = {0};
|
||||||
|
int h = 5;
|
||||||
|
size_t n = (size_t)1 << h;
|
||||||
|
lms_sm3_digest_t *tree = NULL;
|
||||||
|
lms_sm3_digest_t *cl_tree = NULL;
|
||||||
|
lms_sm3_digest_t root;
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
if (lms_cl_init(&ctx) != 1) {
|
||||||
|
fprintf(stderr, "%s: OpenCL unavailable, skipped\n", __FUNCTION__);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (!(tree = (lms_sm3_digest_t *)malloc(sizeof(lms_sm3_digest_t) * (2*n - 1)))) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (!(cl_tree = (lms_sm3_digest_t *)malloc(sizeof(lms_sm3_digest_t) * (2*n - 1)))) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
lms_derive_merkle_tree(seed, I, h, tree);
|
||||||
|
if (lms_cl_derive_merkle_tree(&ctx, seed, I, h, cl_tree) != 1) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (memcmp(tree, cl_tree, sizeof(lms_sm3_digest_t) * (2*n - 1)) != 0) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (lms_cl_derive_merkle_root(&ctx, seed, I, h, root) != 1) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (memcmp(tree[0], root, 32) != 0) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s() ok\n", __FUNCTION__);
|
||||||
|
ret = 1;
|
||||||
|
end:
|
||||||
|
if (tree) free(tree);
|
||||||
|
if (cl_tree) free(cl_tree);
|
||||||
|
lms_cl_cleanup(&ctx);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int test_lms_cl_key_generate(void)
|
||||||
|
{
|
||||||
|
LMS_CL_CTX ctx;
|
||||||
|
lms_sm3_digest_t seed = {1};
|
||||||
|
uint8_t I[16] = {2};
|
||||||
|
LMS_KEY key;
|
||||||
|
LMS_KEY cl_key;
|
||||||
|
LMS_KEY key2;
|
||||||
|
uint8_t keybuf[LMS_PRIVATE_KEY_SIZE];
|
||||||
|
uint8_t *p = keybuf;
|
||||||
|
const uint8_t *cp = keybuf;
|
||||||
|
size_t keylen = 0;
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
memset(&key, 0, sizeof(key));
|
||||||
|
memset(&cl_key, 0, sizeof(cl_key));
|
||||||
|
memset(&key2, 0, sizeof(key2));
|
||||||
|
|
||||||
|
if (lms_cl_init(&ctx) != 1) {
|
||||||
|
fprintf(stderr, "%s: OpenCL unavailable, skipped\n", __FUNCTION__);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (lms_key_generate_ex(&key, LMS_SM3_M32_H5, seed, I, 1) != 1) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (lms_cl_key_generate_ex(&ctx, &cl_key, LMS_SM3_M32_H5, seed, I, 1) != 1) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (memcmp(&key.public_key, &cl_key.public_key, sizeof(LMS_PUBLIC_KEY)) != 0) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (lms_private_key_to_bytes(&key, &p, &keylen) != 1) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (keylen != LMS_PRIVATE_KEY_SIZE) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (lms_cl_private_key_from_bytes(&ctx, &key2, &cp, &keylen) != 1 || keylen != 0) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (memcmp(&key.public_key, &key2.public_key, sizeof(LMS_PUBLIC_KEY)) != 0
|
||||||
|
|| memcmp(key.tree, key2.tree, sizeof(lms_sm3_digest_t) * ((1 << 5)*2 - 1)) != 0) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
printf("%s() ok\n", __FUNCTION__);
|
||||||
|
ret = 1;
|
||||||
|
end:
|
||||||
|
lms_key_cleanup(&key);
|
||||||
|
lms_key_cleanup(&cl_key);
|
||||||
|
lms_key_cleanup(&key2);
|
||||||
|
lms_cl_cleanup(&ctx);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int test_hss_cl_key_generate(void)
|
||||||
|
{
|
||||||
|
LMS_CL_CTX ctx;
|
||||||
|
int lms_types[] = {
|
||||||
|
LMS_SM3_M32_H5,
|
||||||
|
LMS_SM3_M32_H5,
|
||||||
|
};
|
||||||
|
HSS_KEY key;
|
||||||
|
HSS_KEY key2;
|
||||||
|
HSS_SIGN_CTX sign_ctx;
|
||||||
|
HSS_SIGN_CTX verify_ctx;
|
||||||
|
uint8_t keybuf[HSS_PRIVATE_KEY_MAX_SIZE];
|
||||||
|
uint8_t sig[HSS_SIGNATURE_MAX_SIZE];
|
||||||
|
uint8_t *p = keybuf;
|
||||||
|
const uint8_t *cp = keybuf;
|
||||||
|
uint8_t msg[] = "abc";
|
||||||
|
size_t keylen = 0;
|
||||||
|
size_t siglen = 0;
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
memset(&key, 0, sizeof(key));
|
||||||
|
memset(&key2, 0, sizeof(key2));
|
||||||
|
memset(&sign_ctx, 0, sizeof(sign_ctx));
|
||||||
|
memset(&verify_ctx, 0, sizeof(verify_ctx));
|
||||||
|
|
||||||
|
if (lms_cl_init(&ctx) != 1) {
|
||||||
|
fprintf(stderr, "%s: OpenCL unavailable, skipped\n", __FUNCTION__);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (hss_cl_key_generate(&ctx, &key, lms_types, sizeof(lms_types)/sizeof(lms_types[0])) != 1) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (hss_private_key_to_bytes(&key, &p, &keylen) != 1) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (hss_cl_private_key_from_bytes(&ctx, &key2, &cp, &keylen) != 1 || keylen != 0) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (hss_public_key_equ(&key, &key2) != 1) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
key2.lms_key[1].q = 31;
|
||||||
|
if (hss_cl_sign_init(&ctx, &sign_ctx, &key2) != 1
|
||||||
|
|| hss_sign_update(&sign_ctx, msg, sizeof(msg)) != 1
|
||||||
|
|| hss_sign_finish(&sign_ctx, sig, &siglen) != 1) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (key2.lms_key[0].q != 2 || key2.lms_key[1].q != 0) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if (hss_verify_init(&verify_ctx, &key, sig, siglen) != 1
|
||||||
|
|| hss_verify_update(&verify_ctx, msg, sizeof(msg)) != 1
|
||||||
|
|| hss_verify_finish(&verify_ctx) != 1) {
|
||||||
|
error_print();
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s() ok\n", __FUNCTION__);
|
||||||
|
ret = 1;
|
||||||
|
end:
|
||||||
|
hss_key_cleanup(&key);
|
||||||
|
hss_key_cleanup(&key2);
|
||||||
|
lms_cl_cleanup(&ctx);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if ENABLE_TEST_SPEED
|
||||||
|
static double get_seconds(void)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
LARGE_INTEGER freq;
|
||||||
|
LARGE_INTEGER count;
|
||||||
|
|
||||||
|
QueryPerformanceFrequency(&freq);
|
||||||
|
QueryPerformanceCounter(&count);
|
||||||
|
return (double)count.QuadPart/(double)freq.QuadPart;
|
||||||
|
#else
|
||||||
|
struct timeval tv;
|
||||||
|
|
||||||
|
gettimeofday(&tv, NULL);
|
||||||
|
return (double)tv.tv_sec + (double)tv.tv_usec/1000000;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static int speed_lms_cl_derive_merkle_root(void)
|
||||||
|
{
|
||||||
|
LMS_CL_CTX ctx;
|
||||||
|
lms_sm3_digest_t seed = {0};
|
||||||
|
uint8_t I[16] = {0};
|
||||||
|
lms_sm3_digest_t root;
|
||||||
|
lms_sm3_digest_t cl_root;
|
||||||
|
double begin;
|
||||||
|
double seconds;
|
||||||
|
int h = 10;
|
||||||
|
int cl_h = 15;
|
||||||
|
|
||||||
|
if (lms_cl_init(&ctx) != 1) {
|
||||||
|
fprintf(stderr, "%s: OpenCL unavailable, skipped\n", __FUNCTION__);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
begin = get_seconds();
|
||||||
|
lms_derive_merkle_root(seed, I, h, root);
|
||||||
|
seconds = get_seconds() - begin;
|
||||||
|
fprintf(stderr, "%s: CPU H%d %.3f seconds\n", __FUNCTION__, h, seconds);
|
||||||
|
|
||||||
|
begin = get_seconds();
|
||||||
|
if (lms_cl_derive_merkle_root(&ctx, seed, I, h, cl_root) != 1) {
|
||||||
|
error_print();
|
||||||
|
lms_cl_cleanup(&ctx);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
seconds = get_seconds() - begin;
|
||||||
|
fprintf(stderr, "%s: OpenCL H%d %.3f seconds\n", __FUNCTION__, h, seconds);
|
||||||
|
if (memcmp(root, cl_root, 32) != 0) {
|
||||||
|
error_print();
|
||||||
|
lms_cl_cleanup(&ctx);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
begin = get_seconds();
|
||||||
|
if (lms_cl_derive_merkle_root(&ctx, seed, I, cl_h, cl_root) != 1) {
|
||||||
|
error_print();
|
||||||
|
lms_cl_cleanup(&ctx);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
seconds = get_seconds() - begin;
|
||||||
|
fprintf(stderr, "%s: OpenCL H%d %.3f seconds\n", __FUNCTION__, cl_h, seconds);
|
||||||
|
lms_cl_cleanup(&ctx);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
if (test_lms_cl_derive_merkle_tree() != 1) goto err;
|
||||||
|
if (test_lms_cl_key_generate() != 1) goto err;
|
||||||
|
if (test_hss_cl_key_generate() != 1) goto err;
|
||||||
|
#if ENABLE_TEST_SPEED
|
||||||
|
if (speed_lms_cl_derive_merkle_root() != 1) goto err;
|
||||||
|
#endif
|
||||||
|
printf("%s all tests passed\n", __FILE__);
|
||||||
|
return 0;
|
||||||
|
err:
|
||||||
|
error_print();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@@ -15,6 +15,9 @@
|
|||||||
#include <gmssl/mem.h>
|
#include <gmssl/mem.h>
|
||||||
#include <gmssl/error.h>
|
#include <gmssl/error.h>
|
||||||
#include <gmssl/lms.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";
|
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 *pout = out;
|
||||||
uint8_t *ppubout = pubout;
|
uint8_t *ppubout = pubout;
|
||||||
size_t outlen = 0, puboutlen = 0;
|
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));
|
memset(&key, 0, sizeof(key));
|
||||||
|
#ifdef ENABLE_LMS_CL
|
||||||
|
memset(&cl_ctx, 0, sizeof(cl_ctx));
|
||||||
|
#endif
|
||||||
|
|
||||||
argc--;
|
argc--;
|
||||||
argv++;
|
argv++;
|
||||||
@@ -129,10 +139,22 @@ bad:
|
|||||||
goto end;
|
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) {
|
if (hss_key_generate(&key, lms_types_val, levels) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
hss_public_key_print(stderr, 0, 0, "hss_public_key", &key);
|
hss_public_key_print(stderr, 0, 0, "hss_public_key", &key);
|
||||||
}
|
}
|
||||||
@@ -157,6 +179,9 @@ bad:
|
|||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
end:
|
end:
|
||||||
|
#ifdef ENABLE_LMS_CL
|
||||||
|
if (cl_initialized) lms_cl_cleanup(&cl_ctx);
|
||||||
|
#endif
|
||||||
hss_key_cleanup(&key);
|
hss_key_cleanup(&key);
|
||||||
gmssl_secure_clear(out, outlen);
|
gmssl_secure_clear(out, outlen);
|
||||||
if (outfile && outfp) fclose(outfp);
|
if (outfile && outfp) fclose(outfp);
|
||||||
|
|||||||
@@ -15,6 +15,9 @@
|
|||||||
#include <gmssl/mem.h>
|
#include <gmssl/mem.h>
|
||||||
#include <gmssl/error.h>
|
#include <gmssl/error.h>
|
||||||
#include <gmssl/lms.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";
|
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;
|
HSS_SIGN_CTX ctx;
|
||||||
uint8_t sig[HSS_SIGNATURE_MAX_SIZE];
|
uint8_t sig[HSS_SIGNATURE_MAX_SIZE];
|
||||||
size_t siglen;
|
size_t siglen;
|
||||||
|
#ifdef ENABLE_LMS_CL
|
||||||
|
LMS_CL_CTX cl_ctx;
|
||||||
|
int cl_initialized = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
memset(&key, 0, sizeof(key));
|
memset(&key, 0, sizeof(key));
|
||||||
|
#ifdef ENABLE_LMS_CL
|
||||||
|
memset(&cl_ctx, 0, sizeof(cl_ctx));
|
||||||
|
#endif
|
||||||
|
|
||||||
argc--;
|
argc--;
|
||||||
argv++;
|
argv++;
|
||||||
@@ -136,10 +146,22 @@ bad:
|
|||||||
fprintf(stderr, "%s: read private key failure\n", prog);
|
fprintf(stderr, "%s: read private key failure\n", prog);
|
||||||
goto end;
|
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) {
|
if (hss_private_key_from_bytes(&key, &cp, &keylen) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
if (keylen) {
|
if (keylen) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
@@ -154,10 +176,17 @@ bad:
|
|||||||
goto end;
|
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) {
|
if (hss_sign_init(&ctx, &key) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
uint8_t buf[1024];
|
uint8_t buf[1024];
|
||||||
@@ -185,6 +214,9 @@ bad:
|
|||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
end:
|
end:
|
||||||
|
#ifdef ENABLE_LMS_CL
|
||||||
|
if (cl_initialized) lms_cl_cleanup(&cl_ctx);
|
||||||
|
#endif
|
||||||
hss_key_cleanup(&key);
|
hss_key_cleanup(&key);
|
||||||
gmssl_secure_clear(keybuf, sizeof(keybuf));
|
gmssl_secure_clear(keybuf, sizeof(keybuf));
|
||||||
gmssl_secure_clear(&ctx, sizeof(ctx));
|
gmssl_secure_clear(&ctx, sizeof(ctx));
|
||||||
|
|||||||
@@ -15,6 +15,9 @@
|
|||||||
#include <gmssl/mem.h>
|
#include <gmssl/mem.h>
|
||||||
#include <gmssl/error.h>
|
#include <gmssl/error.h>
|
||||||
#include <gmssl/lms.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";
|
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 *pout = out;
|
||||||
uint8_t *ppubout = pubout;
|
uint8_t *ppubout = pubout;
|
||||||
size_t outlen = 0, puboutlen = 0;
|
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));
|
memset(&key, 0, sizeof(key));
|
||||||
|
#ifdef ENABLE_LMS_CL
|
||||||
|
memset(&cl_ctx, 0, sizeof(cl_ctx));
|
||||||
|
#endif
|
||||||
|
|
||||||
argc--;
|
argc--;
|
||||||
argv++;
|
argv++;
|
||||||
@@ -110,10 +120,22 @@ bad:
|
|||||||
goto end;
|
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) {
|
if (lms_key_generate(&key, lms_type_val) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
if (verbose) {
|
if (verbose) {
|
||||||
lms_public_key_print(stderr, 0, 0, "lms_public_key", &key);
|
lms_public_key_print(stderr, 0, 0, "lms_public_key", &key);
|
||||||
}
|
}
|
||||||
@@ -138,6 +160,9 @@ bad:
|
|||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
end:
|
end:
|
||||||
|
#ifdef ENABLE_LMS_CL
|
||||||
|
if (cl_initialized) lms_cl_cleanup(&cl_ctx);
|
||||||
|
#endif
|
||||||
lms_key_cleanup(&key);
|
lms_key_cleanup(&key);
|
||||||
gmssl_secure_clear(out, outlen);
|
gmssl_secure_clear(out, outlen);
|
||||||
if (outfile && outfp) fclose(outfp);
|
if (outfile && outfp) fclose(outfp);
|
||||||
|
|||||||
@@ -15,6 +15,9 @@
|
|||||||
#include <gmssl/mem.h>
|
#include <gmssl/mem.h>
|
||||||
#include <gmssl/error.h>
|
#include <gmssl/error.h>
|
||||||
#include <gmssl/lms.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";
|
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;
|
LMS_SIGN_CTX ctx;
|
||||||
uint8_t sig[LMS_SIGNATURE_MAX_SIZE];
|
uint8_t sig[LMS_SIGNATURE_MAX_SIZE];
|
||||||
size_t siglen;
|
size_t siglen;
|
||||||
|
#ifdef ENABLE_LMS_CL
|
||||||
|
LMS_CL_CTX cl_ctx;
|
||||||
|
int cl_initialized = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
memset(&key, 0, sizeof(key));
|
memset(&key, 0, sizeof(key));
|
||||||
|
#ifdef ENABLE_LMS_CL
|
||||||
|
memset(&cl_ctx, 0, sizeof(cl_ctx));
|
||||||
|
#endif
|
||||||
|
|
||||||
argc--;
|
argc--;
|
||||||
argv++;
|
argv++;
|
||||||
@@ -136,10 +146,22 @@ bad:
|
|||||||
fprintf(stderr, "%s: read private key failure\n", prog);
|
fprintf(stderr, "%s: read private key failure\n", prog);
|
||||||
goto end;
|
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) {
|
if (lms_private_key_from_bytes(&key, &cp, &keylen) != 1) {
|
||||||
error_print();
|
error_print();
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
if (keylen) {
|
if (keylen) {
|
||||||
error_print();
|
error_print();
|
||||||
return -1;
|
return -1;
|
||||||
@@ -185,6 +207,9 @@ bad:
|
|||||||
ret = 0;
|
ret = 0;
|
||||||
|
|
||||||
end:
|
end:
|
||||||
|
#ifdef ENABLE_LMS_CL
|
||||||
|
if (cl_initialized) lms_cl_cleanup(&cl_ctx);
|
||||||
|
#endif
|
||||||
lms_key_cleanup(&key);
|
lms_key_cleanup(&key);
|
||||||
gmssl_secure_clear(keybuf, sizeof(keybuf));
|
gmssl_secure_clear(keybuf, sizeof(keybuf));
|
||||||
gmssl_secure_clear(&ctx, sizeof(ctx));
|
gmssl_secure_clear(&ctx, sizeof(ctx));
|
||||||
|
|||||||
Reference in New Issue
Block a user