mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-06 16:36:16 +08:00
Update SPHINCS+
This commit is contained in:
@@ -133,6 +133,8 @@ void sphincs_adrs_set_hash_address(sphincs_adrs_t adrs, const uint32_t address);
|
||||
void sphincs_adrs_set_tree_height(sphincs_adrs_t adrs, uint32_t height);
|
||||
void sphincs_adrs_set_tree_index(sphincs_adrs_t adrs, uint32_t index);
|
||||
|
||||
int sphincs_adrs_print(FILE *fp, int fmt, int ind, const char *label, const sphincs_adrs_t adrs);
|
||||
|
||||
typedef struct {
|
||||
uint8_t layer_address;
|
||||
uint64_t tree_address;
|
||||
@@ -160,43 +162,37 @@ typedef struct {
|
||||
size_t siglen;
|
||||
} SPHINCS_PARAMS;
|
||||
|
||||
// sizeof(sphincs_secret_t) == n, when sm3/sha256, n == 16
|
||||
typedef uint8_t sphincs_secret_t[16];
|
||||
// sizeof(sphincs_hash128_t) == n, when sm3/sha256, n == 16
|
||||
typedef uint8_t sphincs_hash128_t[16];
|
||||
|
||||
typedef sphincs_secret_t sphincs_wots_key_t[35];
|
||||
typedef sphincs_secret_t sphincs_wots_sig_t[35];
|
||||
|
||||
|
||||
#define SPHINCS_WOTS_NUM_CHAINS 35
|
||||
|
||||
typedef sphincs_hash128_t sphincs_wots_key_t[35];
|
||||
typedef sphincs_hash128_t sphincs_wots_sig_t[35];
|
||||
|
||||
int sphincs_wots_key_print(FILE *fp, int fmt, int ind, const char *label, const sphincs_wots_key_t key);
|
||||
int sphincs_wots_sig_print(FILE *fp, int fmt, int ind, const char *label, const sphincs_wots_sig_t sig);
|
||||
|
||||
void sphincs_wots_derive_sk(const sphincs_secret_t secret,
|
||||
const sphincs_secret_t seed, const sphincs_adrs_t in_adrs,
|
||||
void sphincs_wots_derive_sk(const sphincs_hash128_t secret,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t in_adrs,
|
||||
sphincs_wots_key_t sk);
|
||||
void sphincs_wots_chain(const sphincs_secret_t x,
|
||||
const sphincs_secret_t seed, const sphincs_adrs_t ots_adrs,
|
||||
int start, int steps, sphincs_secret_t y);
|
||||
void sphincs_wots_chain(const sphincs_hash128_t x,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t ots_adrs,
|
||||
int start, int steps, sphincs_hash128_t y);
|
||||
void sphincs_wots_sk_to_pk(const sphincs_wots_key_t sk,
|
||||
const sphincs_secret_t seed, const sphincs_adrs_t ots_adrs,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t ots_adrs,
|
||||
sphincs_wots_key_t pk);
|
||||
void sphincs_wots_pk_to_root(const sphincs_wots_key_t pk,
|
||||
const sphincs_secret_t seed, const sphincs_adrs_t in_adrs,
|
||||
sphincs_secret_t root);
|
||||
void sphincs_base_w_and_checksum(const sphincs_secret_t dgst, int steps[35]);
|
||||
void sphincs_wots_sign(const sphincs_wots_key_t sk,
|
||||
const sphincs_secret_t seed, const sphincs_adrs_t ots_adrs,
|
||||
const sphincs_secret_t dgst, sphincs_wots_sig_t sig);
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t ots_adrs,
|
||||
const sphincs_hash128_t dgst, sphincs_wots_sig_t sig);
|
||||
void sphincs_wots_sig_to_pk(const sphincs_wots_sig_t sig,
|
||||
const sphincs_secret_t seed, const sphincs_adrs_t ots_adrs,
|
||||
const sphincs_secret_t dgst, sphincs_wots_key_t pk);
|
||||
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
uint32_t index;
|
||||
sphincs_wots_sig_t wots_sig;
|
||||
sphincs_secret_t auth_path[22]; // sphincs+_128f height = 22
|
||||
} SPHINCS_XMSS_SIGNATURE;
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t ots_adrs,
|
||||
const sphincs_hash128_t dgst, sphincs_wots_key_t pk);
|
||||
void sphincs_wots_pk_to_root(const sphincs_wots_key_t pk,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t in_adrs,
|
||||
sphincs_hash128_t root);
|
||||
|
||||
|
||||
#if 1 // SPHINCS+_128s
|
||||
@@ -212,16 +208,138 @@ typedef struct {
|
||||
# define SPHINCS_FORS_NUM_TREES 33
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#define SPHINCS_FORS_TREE_HEIGHT 12
|
||||
#define SPHINCS_FORS_TREE_NUM_NODES ((1 << (SPHINCS_FORS_TREE_HEIGHT + 1)) - 1)
|
||||
|
||||
|
||||
#define SPHINCS_FORS_NUM_NODES (SPHINCS_FORS_TREE_NUM_NODES * SPHINCS_FORS_NUM_TRESS + 1)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// FORS (Forest Of Random Subsets)
|
||||
|
||||
// fors_tree
|
||||
// fors_tree_height
|
||||
// fors_tree_root
|
||||
// fors_forest
|
||||
// fors_num_trees
|
||||
// fors_root
|
||||
|
||||
#define SPHINCS_XMSS_HEIGHT (SPHINCS_HYPERTREE_HEIGHT/SPHINCS_HYPERTREE_LAYERS)
|
||||
#define SPHINCS_XMSS_NUM_NODES ((1 << (SPHINCS_XMSS_HEIGHT + 1)) - 1)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
SPHINCS+_128s/SM3
|
||||
|
||||
|
||||
H_msg(R, PK.seed, PK.root, M)
|
||||
= MGF1-SM3(R
|
||||
||PK.seed
|
||||
||SM3(R||PK.seed||PK.root||M),
|
||||
m),
|
||||
|
||||
|
||||
1. fors_index: 12 * 14 = 168 bits = 21 bytes
|
||||
2. tree_index: 63 - 63/7 = 54 bits = 7 bytes
|
||||
3. leaf_index: 9 bits = 2 bytes
|
||||
total: 30 bytes, 1 MGF1-SM3 output
|
||||
|
||||
|
||||
|
||||
SPHINCS+_128f/SM3
|
||||
|
||||
1. fors_index: 6 * 33 = 198 bits = 25 bytes
|
||||
2. tree_address: 66 - 66/22 = 63 bits = 8 bytes
|
||||
3. keypair_address (leaf_index): 3 bits = 1 byte
|
||||
total: 34 bytes, so need 2 MGF1-SM3 output
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
void sphincs_xmss_tree_hash(
|
||||
const sphincs_hash128_t left_child, const sphincs_hash128_t right_child,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t adrs,
|
||||
hash256_t parent);
|
||||
void sphincs_xmss_build_tree(const sphincs_hash128_t secret,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t adrs,
|
||||
sphincs_hash128_t tree[SPHINCS_XMSS_NUM_NODES]);
|
||||
void sphincs_xmss_build_auth_path(const sphincs_hash128_t tree[SPHINCS_XMSS_NUM_NODES],
|
||||
uint32_t tree_index, sphincs_hash128_t auth_path[SPHINCS_XMSS_HEIGHT]);
|
||||
void sphincs_xmss_build_root(const sphincs_hash128_t wots_root, uint32_t tree_index,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t adrs,
|
||||
const sphincs_hash128_t auth_path[SPHINCS_XMSS_HEIGHT],
|
||||
hash256_t root);
|
||||
|
||||
|
||||
typedef struct {
|
||||
sphincs_secret_t fors_sk[SPHINCS_FORS_HEIGHT];
|
||||
sphincs_secret_t auth_path[SPHINCS_FORS_NUM_TREES][SPHINCS_FORS_HEIGHT];
|
||||
sphincs_wots_sig_t wots_sig;
|
||||
sphincs_hash128_t auth_path[22]; // sphincs+_128f height = 22
|
||||
} SPHINCS_XMSS_SIGNATURE;
|
||||
|
||||
void sphincs_xmss_sign(const sphincs_hash128_t secret,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t adrs, uint32_t keypair_address,
|
||||
const sphincs_hash128_t tbs_root, // to be signed xmss_root or fors_forest_root
|
||||
SPHINCS_XMSS_SIGNATURE *sig);
|
||||
void sphincs_xmss_sig_to_root(const SPHINCS_XMSS_SIGNATURE *sig,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t adrs, uint32_t keypair_address,
|
||||
const sphincs_hash128_t tbs_root, // to be signed xmss_root or fors_forest_root
|
||||
sphincs_hash128_t xmss_root);
|
||||
|
||||
|
||||
void sphincs_hypertree_derive_root(const sphincs_hash128_t secret, const sphincs_hash128_t seed,
|
||||
sphincs_hash128_t root);
|
||||
void sphincs_hypertree_sign(const sphincs_hash128_t secret,
|
||||
const sphincs_hash128_t seed, uint64_t tree_address, uint32_t keypair_address,
|
||||
const sphincs_hash128_t tbs_fors_forest_root,
|
||||
SPHINCS_XMSS_SIGNATURE sig[SPHINCS_HYPERTREE_LAYERS]);
|
||||
int sphincs_hypertree_verify(const sphincs_hash128_t top_xmss_root,
|
||||
const sphincs_hash128_t seed, uint64_t tree_address, uint32_t keypair_address,
|
||||
const sphincs_hash128_t tbs_fors_forest_root,
|
||||
const SPHINCS_XMSS_SIGNATURE sig[SPHINCS_HYPERTREE_LAYERS]);
|
||||
|
||||
|
||||
|
||||
typedef uint8_t sphincs_fors_digest_t[21];
|
||||
|
||||
|
||||
void sphincs_fors_derive_sk(const sphincs_hash128_t secret,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t in_adrs,
|
||||
uint32_t fors_index, sphincs_hash128_t sk);
|
||||
|
||||
void sphincs_fors_build_tree(const sphincs_hash128_t secret,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t in_adrs, int tree_addr,
|
||||
sphincs_hash128_t tree[SPHINCS_FORS_TREE_NUM_NODES]);;
|
||||
void sphincs_fors_derive_root(const sphincs_hash128_t secret,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t in_adrs,
|
||||
sphincs_hash128_t root);
|
||||
|
||||
|
||||
typedef struct {
|
||||
sphincs_hash128_t fors_sk[SPHINCS_FORS_NUM_TREES];
|
||||
sphincs_hash128_t auth_path[SPHINCS_FORS_NUM_TREES][SPHINCS_FORS_HEIGHT];
|
||||
} SPHINCS_FORS_SIGNATURE;
|
||||
|
||||
void sphincs_fors_sign(const sphincs_hash128_t secret,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t in_adrs,
|
||||
const uint8_t dgst[21],
|
||||
SPHINCS_FORS_SIGNATURE *sig);
|
||||
void sphincs_fors_sig_to_root(const SPHINCS_FORS_SIGNATURE *sig,
|
||||
const sphincs_hash128_t seed, const sphincs_adrs_t adrs,
|
||||
const uint8_t dgst[21], sphincs_hash128_t root);
|
||||
|
||||
|
||||
#define SPHINCS_FORS_SIGNATURE_SIZE sizeof(SPHINCS_FORS_SIGNATURE)
|
||||
int sphincs_fors_signature_to_bytes(const SPHINCS_FORS_SIGNATURE *sig, uint8_t **out, size_t *outlen);
|
||||
int sphincs_fors_signature_from_bytes(SPHINCS_FORS_SIGNATURE *sig, const uint8_t **in, size_t *inlen);
|
||||
@@ -230,14 +348,14 @@ int sphincs_fors_signature_print(FILE *fp, int fmt, int ind, const char *label,
|
||||
|
||||
|
||||
typedef struct {
|
||||
sphincs_secret_t seed;
|
||||
sphincs_secret_t root;
|
||||
sphincs_hash128_t seed;
|
||||
sphincs_hash128_t root;
|
||||
} SPHINCS_PUBLIC_KEY;
|
||||
|
||||
typedef struct {
|
||||
SPHINCS_PUBLIC_KEY public_key;
|
||||
sphincs_secret_t secret;
|
||||
sphincs_secret_t sk_prf;
|
||||
sphincs_hash128_t secret;
|
||||
sphincs_hash128_t sk_prf;
|
||||
} SPHINCS_KEY;
|
||||
|
||||
#define SPHINCS_PUBLIC_KEY_SIZE sizeof(SPHINCS_PUBLIC_KEY)
|
||||
@@ -253,7 +371,7 @@ void sphincs_key_cleanup(SPHINCS_KEY *key);
|
||||
|
||||
|
||||
typedef struct {
|
||||
sphincs_secret_t random;
|
||||
sphincs_hash128_t random;
|
||||
SPHINCS_FORS_SIGNATURE fors_sig;
|
||||
SPHINCS_XMSS_SIGNATURE xmss_sigs[SPHINCS_HYPERTREE_LAYERS];
|
||||
} SPHINCS_SIGNATURE;
|
||||
|
||||
933
src/sphincs.c
933
src/sphincs.c
File diff suppressed because it is too large
Load Diff
@@ -17,16 +17,26 @@
|
||||
#include <gmssl/sphincs.h>
|
||||
|
||||
|
||||
// 这个应该是用值去验证的
|
||||
static int test_sphincs_wots_derive_sk(void)
|
||||
{
|
||||
sphincs_secret_t secret;
|
||||
sphincs_secret_t seed;
|
||||
sphincs_adrs_t adrs;
|
||||
sphincs_hash128_t secret;
|
||||
sphincs_hash128_t seed;
|
||||
sphincs_adrs_t adrs = {0};
|
||||
sphincs_wots_key_t wots_sk;
|
||||
|
||||
sphincs_wots_derive_sk(secret, seed, adrs, wots_sk);
|
||||
memset(secret, 0, sizeof(secret));
|
||||
memset(seed, 0, sizeof(seed));
|
||||
sphincs_adrs_set_layer_address(adrs, 0);
|
||||
sphincs_adrs_set_tree_address(adrs, 0);
|
||||
sphincs_adrs_set_type(adrs, SPHINCS_ADRS_TYPE_WOTS_PRF);
|
||||
sphincs_adrs_set_keypair_address(adrs, 0);
|
||||
|
||||
format_bytes(stderr, 0, 4, "secret", secret, sizeof(secret));
|
||||
format_bytes(stderr, 0, 4, "seed", seed, sizeof(seed));
|
||||
sphincs_adrs_print(stderr, 0, 4, "adrs", adrs);
|
||||
|
||||
memset(wots_sk, 0, sizeof(wots_sk));
|
||||
sphincs_wots_derive_sk(secret, seed, adrs, wots_sk);
|
||||
sphincs_wots_key_print(stderr, 0, 4, "wots_sk", wots_sk);
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
@@ -35,13 +45,15 @@ static int test_sphincs_wots_derive_sk(void)
|
||||
|
||||
static int test_sphincs_wots_chain(void)
|
||||
{
|
||||
sphincs_secret_t x;
|
||||
sphincs_secret_t seed;
|
||||
sphincs_adrs_t adrs;
|
||||
sphincs_secret_t y;
|
||||
sphincs_hash128_t seed = {0};
|
||||
sphincs_adrs_t adrs = {0};
|
||||
sphincs_hash128_t x = {0};
|
||||
sphincs_hash128_t y = {0};
|
||||
int start = 0;
|
||||
int steps = 15;
|
||||
|
||||
format_bytes(stderr, 0, 4, "seed", seed, sizeof(seed));
|
||||
|
||||
sphincs_wots_chain(x, seed, adrs, start, steps, y);
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
@@ -51,7 +63,7 @@ static int test_sphincs_wots_chain(void)
|
||||
static int test_sphincs_wots_sk_to_pk(void)
|
||||
{
|
||||
sphincs_wots_key_t wots_sk;
|
||||
sphincs_secret_t seed;
|
||||
sphincs_hash128_t seed;
|
||||
sphincs_adrs_t adrs;
|
||||
sphincs_wots_key_t wots_pk;
|
||||
|
||||
@@ -66,9 +78,9 @@ static int test_sphincs_wots_sk_to_pk(void)
|
||||
static int test_sphincs_wots_pk_to_root(void)
|
||||
{
|
||||
sphincs_wots_key_t wots_pk;
|
||||
sphincs_secret_t seed;
|
||||
sphincs_hash128_t seed;
|
||||
sphincs_adrs_t adrs;
|
||||
sphincs_secret_t wots_root;
|
||||
sphincs_hash128_t wots_root;
|
||||
|
||||
sphincs_wots_pk_to_root(wots_pk, seed, adrs, wots_root);
|
||||
|
||||
@@ -79,9 +91,9 @@ static int test_sphincs_wots_pk_to_root(void)
|
||||
static int test_sphincs_wots_sign(void)
|
||||
{
|
||||
sphincs_wots_key_t wots_sk;
|
||||
sphincs_secret_t seed;
|
||||
sphincs_hash128_t seed;
|
||||
sphincs_adrs_t adrs;
|
||||
sphincs_secret_t dgst;
|
||||
sphincs_hash128_t dgst;
|
||||
sphincs_wots_sig_t wots_sig;
|
||||
|
||||
sphincs_wots_sign(wots_sk, seed, adrs, dgst, wots_sig);
|
||||
@@ -93,9 +105,9 @@ static int test_sphincs_wots_sign(void)
|
||||
static int test_sphincs_wots_sig_to_pk(void)
|
||||
{
|
||||
sphincs_wots_sig_t wots_sig;
|
||||
sphincs_secret_t seed;
|
||||
sphincs_hash128_t seed;
|
||||
sphincs_adrs_t adrs;
|
||||
sphincs_secret_t dgst;
|
||||
sphincs_hash128_t dgst;
|
||||
sphincs_wots_key_t wots_pk;
|
||||
|
||||
sphincs_wots_sig_to_pk(wots_sig, seed, adrs, dgst, wots_pk);
|
||||
@@ -104,41 +116,356 @@ static int test_sphincs_wots_sig_to_pk(void)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int test_sphincs_wots_sign_verify(void)
|
||||
{
|
||||
sphincs_hash128_t secret = {0};
|
||||
sphincs_hash128_t seed = {0};
|
||||
sphincs_adrs_t adrs = {0};
|
||||
sphincs_hash128_t dgst = {0};
|
||||
|
||||
sphincs_wots_key_t wots_sk;
|
||||
sphincs_wots_key_t wots_pk;
|
||||
sphincs_secret_t seed;
|
||||
sphincs_adrs_t adrs;
|
||||
sphincs_secret_t dgst;
|
||||
sphincs_wots_sig_t wots_sig;
|
||||
sphincs_wots_key_t wots_pk2;
|
||||
sphincs_hash128_t wots_root;
|
||||
int i;
|
||||
|
||||
sphincs_adrs_set_layer_address(adrs, 0);
|
||||
sphincs_adrs_set_tree_address(adrs, 0);
|
||||
sphincs_adrs_set_type(adrs, SPHINCS_ADRS_TYPE_WOTS_PRF);
|
||||
sphincs_adrs_set_keypair_address(adrs, 0);
|
||||
|
||||
sphincs_wots_derive_sk(secret, seed, adrs, wots_sk);
|
||||
|
||||
format_print(stderr, 0, 4, "wots_sk\n");
|
||||
for (i = 0; i < 35; i++) {
|
||||
format_print(stderr, 0, 8, "%d", i);
|
||||
format_bytes(stderr, 0, 0, "", wots_sk[i], 16);
|
||||
}
|
||||
|
||||
sphincs_wots_sk_to_pk(wots_sk, seed, adrs, wots_pk);
|
||||
|
||||
format_print(stderr, 0, 4, "wots_pk\n");
|
||||
for (i = 0; i < 35; i++) {
|
||||
format_print(stderr, 0, 8, "%d", i);
|
||||
format_bytes(stderr, 0, 0, "", wots_pk[i], 16);
|
||||
}
|
||||
|
||||
|
||||
sphincs_wots_pk_to_root(wots_pk, seed, adrs, wots_root);
|
||||
format_bytes(stderr, 0, 4, "wots_root (from pk)", wots_root, 16);
|
||||
|
||||
|
||||
|
||||
sphincs_wots_sign(wots_sk, seed, adrs, dgst, wots_sig);
|
||||
|
||||
sphincs_wots_sig_to_pk(wots_sig, seed, adrs, dgst, wots_pk2);
|
||||
format_print(stderr, 0, 4, "wots_pk\n");
|
||||
for (i = 0; i < 35; i++) {
|
||||
// format_print(stderr, 0, 8, "%d", i);
|
||||
// format_bytes(stderr, 0, 0, "", wots_pk[i], 16);
|
||||
}
|
||||
|
||||
sphincs_wots_pk_to_root(wots_pk2, seed, adrs, wots_root);
|
||||
format_bytes(stderr, 0, 4, "wots_root (from sig)", wots_root, 16);
|
||||
|
||||
|
||||
|
||||
if (memcmp(wots_pk2, wots_pk, sizeof(sphincs_wots_key_t)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int test_sphincs_xmss_build_tree(void)
|
||||
{
|
||||
sphincs_hash128_t secret = {0};
|
||||
sphincs_hash128_t seed = {0};
|
||||
sphincs_adrs_t adrs = {0};
|
||||
sphincs_hash128_t tree[SPHINCS_XMSS_NUM_NODES] = {0};
|
||||
int i;
|
||||
|
||||
sphincs_xmss_build_tree(secret, seed, adrs, tree);
|
||||
|
||||
// for sphincs+_128s, height==9, num_nodes==1023
|
||||
format_print(stderr, 0, 4, "xmss_tree\n");
|
||||
for (i = 0; i < SPHINCS_XMSS_NUM_NODES; i++) {
|
||||
format_print(stderr, 0, 8, "%d", i);
|
||||
format_bytes(stderr, 0, 0, "", tree[i], sizeof(sphincs_hash128_t));
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sphincs_xmss_build_auth_path(void)
|
||||
{
|
||||
sphincs_hash128_t secret = {0};
|
||||
sphincs_hash128_t seed = {0};
|
||||
sphincs_adrs_t adrs = {0};
|
||||
sphincs_hash128_t tree[SPHINCS_XMSS_NUM_NODES] = {0};
|
||||
uint32_t tree_index = 0;
|
||||
sphincs_hash128_t auth_path[SPHINCS_XMSS_HEIGHT];
|
||||
int i;
|
||||
|
||||
sphincs_xmss_build_tree(secret, seed, adrs, tree);
|
||||
|
||||
sphincs_xmss_build_auth_path(tree, tree_index, auth_path);
|
||||
|
||||
format_print(stderr, 0, 4, "auth_path\n");
|
||||
for (i = 0; i < SPHINCS_XMSS_HEIGHT; i++) {
|
||||
format_print(stderr, 0, 8, "%d", i);
|
||||
format_bytes(stderr, 0, 0, "", auth_path[i], sizeof(sphincs_hash128_t));
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sphincs_xmss_build_root(void)
|
||||
{
|
||||
sphincs_hash128_t secret = {0};
|
||||
sphincs_hash128_t seed = {0};
|
||||
sphincs_adrs_t adrs = {0};
|
||||
sphincs_hash128_t tree[SPHINCS_XMSS_NUM_NODES] = {0};
|
||||
uint32_t tree_index = 0;
|
||||
sphincs_hash128_t auth_path[SPHINCS_XMSS_HEIGHT];
|
||||
sphincs_hash128_t root;
|
||||
|
||||
sphincs_xmss_build_tree(secret, seed, adrs, tree);
|
||||
|
||||
sphincs_xmss_build_auth_path(tree, tree_index, auth_path);
|
||||
|
||||
sphincs_xmss_build_root(tree[tree_index], tree_index, seed, adrs, auth_path, root);
|
||||
|
||||
if (memcmp(root, tree[SPHINCS_XMSS_NUM_NODES - 1], sizeof(root)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sphincs_xmss_sign(void)
|
||||
{
|
||||
sphincs_hash128_t secret = {0};
|
||||
sphincs_hash128_t seed = {0};
|
||||
sphincs_adrs_t adrs = {0};
|
||||
uint32_t keypair_address = 0;
|
||||
|
||||
sphincs_hash128_t xmss_tree[SPHINCS_XMSS_NUM_NODES];
|
||||
sphincs_hash128_t dgst = {0};
|
||||
SPHINCS_XMSS_SIGNATURE sig;
|
||||
sphincs_hash128_t xmss_root;
|
||||
sphincs_hash128_t auth_path[SPHINCS_XMSS_HEIGHT];
|
||||
|
||||
|
||||
sphincs_xmss_build_tree(secret, seed, adrs, xmss_tree);
|
||||
|
||||
sphincs_xmss_build_auth_path(xmss_tree, keypair_address, auth_path);
|
||||
|
||||
sphincs_xmss_build_root(xmss_tree[keypair_address], keypair_address, seed, adrs, auth_path, xmss_root);
|
||||
|
||||
|
||||
format_bytes(stderr, 0, 4, "tree[0]", xmss_tree[0], 16);
|
||||
format_bytes(stderr, 0, 4, "tree_root", xmss_tree[SPHINCS_XMSS_NUM_NODES-1], 16);
|
||||
|
||||
|
||||
sphincs_xmss_sign(secret, seed, adrs, keypair_address, dgst, &sig);
|
||||
|
||||
sphincs_xmss_sig_to_root(&sig, seed, adrs, keypair_address, dgst, xmss_root);
|
||||
|
||||
if (memcmp(xmss_root, xmss_tree[SPHINCS_XMSS_NUM_NODES - 1], sizeof(sphincs_hash128_t)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sphincs_xmss_sig_to_root(void)
|
||||
{
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sphincs_hypertree(void)
|
||||
{
|
||||
sphincs_hash128_t secret = {0};
|
||||
sphincs_hash128_t seed = {0};
|
||||
uint64_t tree_address = 0;
|
||||
uint32_t keypair_address = 0;
|
||||
sphincs_hash128_t forest_root = {0};
|
||||
int i;
|
||||
|
||||
sphincs_adrs_t adrs = {0};
|
||||
|
||||
sphincs_hash128_t trees[SPHINCS_XMSS_NUM_NODES * SPHINCS_HYPERTREE_LAYERS];
|
||||
sphincs_hash128_t *tree = trees;
|
||||
|
||||
|
||||
|
||||
for (i = 0; i < SPHINCS_HYPERTREE_LAYERS; i++) {
|
||||
sphincs_adrs_set_layer_address(adrs, i);
|
||||
sphincs_adrs_set_tree_address(adrs, 0);
|
||||
|
||||
sphincs_xmss_build_tree(secret, seed, adrs, tree);
|
||||
|
||||
char label[64];
|
||||
snprintf(label, sizeof(label), "leaf[%d]", i);
|
||||
|
||||
format_bytes(stderr, 0, 4, label, tree[0], 16);
|
||||
|
||||
snprintf(label, sizeof(label), "root[%d]", i);
|
||||
format_bytes(stderr, 0, 4, label, tree[1022], 16);
|
||||
|
||||
|
||||
tree += SPHINCS_XMSS_NUM_NODES;
|
||||
}
|
||||
|
||||
SPHINCS_XMSS_SIGNATURE sig[SPHINCS_HYPERTREE_LAYERS];
|
||||
|
||||
sphincs_adrs_set_layer_address(adrs, 0);
|
||||
sphincs_adrs_set_tree_address(adrs, keypair_address);
|
||||
sphincs_xmss_sign(secret, seed, adrs, keypair_address, forest_root, &sig[0]);
|
||||
|
||||
sphincs_hash128_t xmss_root;
|
||||
sphincs_xmss_sig_to_root(&sig[0], seed, adrs, keypair_address, forest_root, xmss_root);
|
||||
|
||||
format_bytes(stderr, 0, 4, "xmss_root[0]", xmss_root, 16);
|
||||
|
||||
for (i = 1; i < SPHINCS_HYPERTREE_LAYERS; i++) {
|
||||
// layer +1 keypair_address is the lowest xmss_height bits of tree_address
|
||||
keypair_address = tree_address & ((1 << SPHINCS_XMSS_HEIGHT) - 1);
|
||||
tree_address >>= SPHINCS_XMSS_HEIGHT;
|
||||
|
||||
sphincs_adrs_set_layer_address(adrs, i);
|
||||
sphincs_adrs_set_tree_address(adrs, tree_address);
|
||||
// sign xmss_root with layer +1 xmss_keypair
|
||||
sphincs_xmss_sign(secret, seed, adrs, keypair_address, xmss_root, &sig[i]);
|
||||
// xmss_sig => xmss_root, to be signed by next layer +1
|
||||
sphincs_xmss_sig_to_root(&sig[i], seed, adrs, keypair_address, xmss_root, xmss_root);
|
||||
|
||||
|
||||
|
||||
format_print(stderr, 0, 4, "xmss_root[%d]", i);
|
||||
format_bytes(stderr, 0, 0, "", xmss_root, 16);
|
||||
}
|
||||
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int test_sphincs_hypertree_sign(void)
|
||||
{
|
||||
sphincs_hash128_t secret = {0};
|
||||
sphincs_hash128_t seed = {0};
|
||||
uint64_t tree_address = 0;
|
||||
uint32_t keypair_address = 0;
|
||||
sphincs_hash128_t forest_root = {0};
|
||||
|
||||
sphincs_hash128_t ht_root;
|
||||
SPHINCS_XMSS_SIGNATURE ht_sig[SPHINCS_HYPERTREE_LAYERS];
|
||||
|
||||
sphincs_hypertree_derive_root(secret, seed, ht_root);
|
||||
format_bytes(stderr, 0, 4, "hypertree_root", ht_root, 16);
|
||||
|
||||
|
||||
sphincs_hypertree_sign(secret, seed, tree_address, keypair_address, forest_root, ht_sig);
|
||||
|
||||
|
||||
if (sphincs_hypertree_verify(ht_root, seed, tree_address, keypair_address, forest_root, ht_sig) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
static int test_sphincs_fors_sign(void)
|
||||
{
|
||||
sphincs_hash128_t secret = {0};
|
||||
sphincs_hash128_t seed = {0};
|
||||
sphincs_adrs_t adrs = {0};
|
||||
const uint8_t dgst[21] = {0};
|
||||
|
||||
sphincs_hash128_t root;
|
||||
sphincs_hash128_t sig_to_root;
|
||||
SPHINCS_FORS_SIGNATURE sig;
|
||||
|
||||
|
||||
|
||||
sphincs_fors_derive_root(secret, seed, adrs, root);
|
||||
|
||||
sphincs_fors_sign(secret, seed, adrs, dgst, &sig);
|
||||
|
||||
sphincs_fors_sig_to_root(&sig, seed, adrs, dgst, sig_to_root);
|
||||
|
||||
if (memcmp(sig_to_root, root, 16) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sphincs_sign(void)
|
||||
{
|
||||
SPHINCS_KEY key;
|
||||
sphincs_adrs_t adrs = {0};
|
||||
sphincs_hash128_t random;
|
||||
|
||||
|
||||
uint32_t tree_index;
|
||||
uint32_t leaf_index;
|
||||
|
||||
sphincs_adrs_set_layer_address(adrs, 0);
|
||||
sphincs_adrs_set_tree_address(adrs, tree_index);
|
||||
sphincs_adrs_set_type(adrs, SPHINCS_ADRS_TYPE_FORS_TREE);
|
||||
sphincs_adrs_set_keypair_address(adrs, leaf_index);
|
||||
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if (test_sphincs_fors_sign() != 1) goto err;
|
||||
//if (test_sphincs_xmss_build_tree() != 1) goto err;
|
||||
//if (test_sphincs_hypertree() != 1) goto err;
|
||||
// if (test_sphincs_hypertree_sign() != 1) goto err;
|
||||
//if (test_sphincs_wots_sign_verify() != 1) goto err;
|
||||
/*
|
||||
if (test_sphincs_wots_derive_sk() != 1) goto err;
|
||||
if (test_sphincs_wots_chain() != 1) goto err;
|
||||
if (test_sphincs_wots_sk_to_pk() != 1) goto err;
|
||||
if (test_sphincs_wots_pk_to_root() != 1) goto err;
|
||||
if (test_sphincs_wots_sign() != 1) goto err;
|
||||
if (test_sphincs_wots_sig_to_pk() != 1) goto err;
|
||||
if (test_sphincs_wots_sign_verify() != 1) goto err;
|
||||
|
||||
if (test_sphincs_xmss_build_auth_path() != 1) goto err;
|
||||
*/
|
||||
|
||||
//if (test_sphincs_xmss_build_root() != 1) goto err;
|
||||
//if (test_sphincs_xmss_sign() != 1) goto err;
|
||||
|
||||
|
||||
printf("%s all tests passed\n", __FILE__);
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user