1515#include <string.h>
1616#include <stdint.h>
1717#include <gmssl/sm3.h>
18- #include <gmssl/hash256.h>
1918#ifdef ENABLE_SHA2
2019#include <gmssl/sha2.h>
2120#endif
2524extern "C" {
2625#endif
2726
27+ typedef uint8_t xmss_hash256_t [32 ];
28+
2829
2930// Crosscheck with data from xmss-reference (SHA-256), except the XMSS signature.
30- #if defined(ENABLE_XMSS_CROSSCHECK ) && defined(ENABLE_SHA2 )
31- # define HASH256_CTX SHA256_CTX
32- # define hash256_init sha256_init
33- # define hash256_update sha256_update
34- # define hash256_finish sha256_finish
31+ #if defined(ENABLE_XMSS_CROSSCHECK ) && defined(ENABLE_SHA2 ) && !defined(HASH256_CTX )
32+ # define XMSS_HASH256_CTX SHA256_CTX
33+ # define xmss_hash256_init sha256_init
34+ # define xmss_hash256_update sha256_update
35+ # define xmss_hash256_finish sha256_finish
36+ # define XMSS_HASH256_BLOCK_SIZE SHA256_BLOCK_SIZE
3537#else
36- # define HASH256_CTX SM3_CTX
37- # define hash256_init sm3_init
38- # define hash256_update sm3_update
39- # define hash256_finish sm3_finish
38+ # define XMSS_HASH256_CTX SM3_CTX
39+ # define xmss_hash256_init sm3_init
40+ # define xmss_hash256_update sm3_update
41+ # define xmss_hash256_finish sm3_finish
42+ # define XMSS_HASH256_BLOCK_SIZE SM3_BLOCK_SIZE
4043#endif
4144
4245
@@ -85,65 +88,65 @@ typedef struct {
8588
8689typedef uint8_t xmss_adrs_t [32 ];
8790
88- void adrs_copy_layer_address (xmss_adrs_t dst , const xmss_adrs_t src );
89- void adrs_copy_tree_address (xmss_adrs_t dst , const xmss_adrs_t src );
90- void adrs_copy_type (xmss_adrs_t dst , const xmss_adrs_t src );
91- void adrs_copy_ots_address (xmss_adrs_t dst , const xmss_adrs_t src );
92- void adrs_copy_ltree_address (xmss_adrs_t dst , const xmss_adrs_t src );
93- void adrs_copy_padding (xmss_adrs_t dst , const xmss_adrs_t src );
94- void adrs_copy_chain_address (xmss_adrs_t dst , const xmss_adrs_t src );
95- void adrs_copy_tree_height (xmss_adrs_t dst , const xmss_adrs_t src );
96- void adrs_copy_hash_address (xmss_adrs_t dst , const xmss_adrs_t src );
97- void adrs_copy_tree_index (xmss_adrs_t dst , const xmss_adrs_t src );
98- void adrs_copy_key_and_mask (xmss_adrs_t dst , const xmss_adrs_t src );
99-
100- void adrs_set_layer_address (xmss_adrs_t adrs , uint32_t layer );
101- void adrs_set_tree_address (xmss_adrs_t adrs , uint64_t tree_addr );
102- void adrs_set_type (xmss_adrs_t adrs , uint32_t type );
103- void adrs_set_ots_address (xmss_adrs_t adrs , uint32_t address );
104- void adrs_set_ltree_address (xmss_adrs_t adrs , uint32_t address );
105- void adrs_set_padding (xmss_adrs_t adrs , uint32_t padding );
106- void adrs_set_chain_address (xmss_adrs_t adrs , uint32_t address );
107- void adrs_set_tree_height (xmss_adrs_t adrs , uint32_t height );
108- void adrs_set_hash_address (xmss_adrs_t adrs , uint32_t address );
109- void adrs_set_tree_index (xmss_adrs_t adrs , uint32_t index );
110- void adrs_set_key_and_mask (xmss_adrs_t adrs , uint32_t key_and_mask );
111-
112- int xmss_adrs_print (FILE * fp , int fmt , int ind , const char * label , const hash256_t adrs );
91+ void xmss_adrs_copy_layer_address (xmss_adrs_t dst , const xmss_adrs_t src );
92+ void xmss_adrs_copy_tree_address (xmss_adrs_t dst , const xmss_adrs_t src );
93+ void xmss_adrs_copy_type (xmss_adrs_t dst , const xmss_adrs_t src );
94+ void xmss_adrs_copy_ots_address (xmss_adrs_t dst , const xmss_adrs_t src );
95+ void xmss_adrs_copy_ltree_address (xmss_adrs_t dst , const xmss_adrs_t src );
96+ void xmss_adrs_copy_padding (xmss_adrs_t dst , const xmss_adrs_t src );
97+ void xmss_adrs_copy_chain_address (xmss_adrs_t dst , const xmss_adrs_t src );
98+ void xmss_adrs_copy_tree_height (xmss_adrs_t dst , const xmss_adrs_t src );
99+ void xmss_adrs_copy_hash_address (xmss_adrs_t dst , const xmss_adrs_t src );
100+ void xmss_adrs_copy_tree_index (xmss_adrs_t dst , const xmss_adrs_t src );
101+ void xmss_adrs_copy_key_and_mask (xmss_adrs_t dst , const xmss_adrs_t src );
102+
103+ void xmss_adrs_set_layer_address (xmss_adrs_t adrs , uint32_t layer );
104+ void xmss_adrs_set_tree_address (xmss_adrs_t adrs , uint64_t tree_addr );
105+ void xmss_adrs_set_type (xmss_adrs_t adrs , uint32_t type );
106+ void xmss_adrs_set_ots_address (xmss_adrs_t adrs , uint32_t address );
107+ void xmss_adrs_set_ltree_address (xmss_adrs_t adrs , uint32_t address );
108+ void xmss_adrs_set_padding (xmss_adrs_t adrs , uint32_t padding );
109+ void xmss_adrs_set_chain_address (xmss_adrs_t adrs , uint32_t address );
110+ void xmss_adrs_set_tree_height (xmss_adrs_t adrs , uint32_t height );
111+ void xmss_adrs_set_hash_address (xmss_adrs_t adrs , uint32_t address );
112+ void xmss_adrs_set_tree_index (xmss_adrs_t adrs , uint32_t index );
113+ void xmss_adrs_set_key_and_mask (xmss_adrs_t adrs , uint32_t key_and_mask );
114+
115+ int xmss_adrs_print (FILE * fp , int fmt , int ind , const char * label , const xmss_hash256_t adrs );
113116
114117// WOTS+ with SM3/SHA256
115118
116- #define WOTS_WINTERNITZ_W 16 // rfc 8391 named algors only support w = 2^4 = 16
117- #define WOTS_NUM_CHAINS 67
118-
119- typedef hash256_t wots_key_t [ WOTS_NUM_CHAINS ];
120- typedef hash256_t wots_sig_t [ WOTS_NUM_CHAINS ];
121-
122-
123- void wots_derive_sk (const hash256_t secret ,
124- const hash256_t seed , const xmss_adrs_t adrs ,
125- wots_key_t sk );
126- void wots_chain (const hash256_t x ,
127- const hash256_t seed , const xmss_adrs_t adrs ,
128- int start , int steps , hash256_t y );
129- void wots_sk_to_pk (const wots_key_t sk ,
130- const hash256_t seed , const xmss_adrs_t adrs ,
131- wots_key_t pk );
132- void wots_sign (const wots_key_t sk ,
133- const hash256_t seed , const xmss_adrs_t adrs ,
134- const hash256_t dgst , wots_sig_t sig );
135- void wots_sig_to_pk (const wots_sig_t sig ,
136- const hash256_t seed , const xmss_adrs_t adrs ,
137- const hash256_t dgst , wots_key_t pk );
138- void wots_pk_to_root (const wots_key_t pk ,
139- const hash256_t seed , const xmss_adrs_t adrs ,
140- hash256_t wots_root );
141- void wots_derive_root (const hash256_t secret ,
142- const hash256_t seed , const xmss_adrs_t adrs ,
143- hash256_t wots_root );
144- int wots_verify (const hash256_t wots_root ,
145- const hash256_t seed , const xmss_adrs_t adrs ,
146- const hash256_t dgst , const wots_sig_t sig );
119+ #define XMSS_WOTS_WINTERNITZ_W 16 // rfc 8391 named algors only support w = 2^4 = 16
120+ #define XMSS_WOTS_NUM_CHAINS 67
121+
122+ typedef xmss_hash256_t xmss_wots_key_t [ XMSS_WOTS_NUM_CHAINS ];
123+ typedef xmss_hash256_t xmss_wots_sig_t [ XMSS_WOTS_NUM_CHAINS ];
124+
125+
126+ void xmss_wots_derive_sk (const xmss_hash256_t secret ,
127+ const xmss_hash256_t seed , const xmss_adrs_t adrs ,
128+ xmss_wots_key_t sk );
129+ void xmss_wots_chain (const xmss_hash256_t x ,
130+ const xmss_hash256_t seed , const xmss_adrs_t adrs ,
131+ int start , int steps , xmss_hash256_t y );
132+ void xmss_wots_sk_to_pk (const xmss_wots_key_t sk ,
133+ const xmss_hash256_t seed , const xmss_adrs_t adrs ,
134+ xmss_wots_key_t pk );
135+ void xmss_wots_sign (const xmss_wots_key_t sk ,
136+ const xmss_hash256_t seed , const xmss_adrs_t adrs ,
137+ const xmss_hash256_t dgst , xmss_wots_sig_t sig );
138+ void xmss_wots_sig_to_pk (const xmss_wots_sig_t sig ,
139+ const xmss_hash256_t seed , const xmss_adrs_t adrs ,
140+ const xmss_hash256_t dgst , xmss_wots_key_t pk );
141+ void xmss_wots_pk_to_root (const xmss_wots_key_t pk ,
142+ const xmss_hash256_t seed , const xmss_adrs_t adrs ,
143+ xmss_hash256_t wots_root );
144+ void xmss_wots_derive_root (const xmss_hash256_t secret ,
145+ const xmss_hash256_t seed , const xmss_adrs_t adrs ,
146+ xmss_hash256_t wots_root );
147+ int xmss_wots_verify (const xmss_hash256_t wots_root ,
148+ const xmss_hash256_t seed , const xmss_adrs_t adrs ,
149+ const xmss_hash256_t dgst , const xmss_wots_sig_t sig );
147150
148151
149152
@@ -185,31 +188,31 @@ uint32_t xmss_type_from_name(const char *name);
185188int xmss_type_to_height (uint32_t xmss_type , size_t * height );
186189
187190size_t xmss_num_tree_nodes (size_t height );
188- void xmss_build_tree (const hash256_t secret ,
189- const hash256_t seed , const xmss_adrs_t adrs ,
190- size_t height , hash256_t * tree ); // tree[xmss_num_tree_nodes(height)]
191- void xmss_build_auth_path (const hash256_t * tree , size_t height ,
192- uint32_t index , hash256_t * auth_path ); // auth_path[height]
193- void xmss_build_root (const hash256_t wots_root , uint32_t index ,
194- const hash256_t seed , const xmss_adrs_t adrs ,
195- const hash256_t * auth_path , size_t height ,
196- hash256_t xmss_root );
191+ void xmss_build_tree (const xmss_hash256_t secret ,
192+ const xmss_hash256_t seed , const xmss_adrs_t adrs ,
193+ size_t height , xmss_hash256_t * tree ); // tree[xmss_num_tree_nodes(height)]
194+ void xmss_build_auth_path (const xmss_hash256_t * tree , size_t height ,
195+ uint32_t index , xmss_hash256_t * auth_path ); // auth_path[height]
196+ void xmss_build_root (const xmss_hash256_t wots_root , uint32_t index ,
197+ const xmss_hash256_t seed , const xmss_adrs_t adrs ,
198+ const xmss_hash256_t * auth_path , size_t height ,
199+ xmss_hash256_t xmss_root );
197200
198201
199202typedef struct {
200203 uint32_t xmss_type ;
201- hash256_t seed ;
202- hash256_t root ;
204+ xmss_hash256_t seed ;
205+ xmss_hash256_t root ;
203206} XMSS_PUBLIC_KEY ;
204207
205208#define XMSS_PUBLIC_KEY_SIZE (4 + 32 + 32) // = 68
206209
207210typedef struct {
208211 XMSS_PUBLIC_KEY public_key ;
209212 uint32_t index ;
210- hash256_t secret ;
211- hash256_t sk_prf ;
212- hash256_t * tree ; // hash256_t [2^(h + 1) - 1]
213+ xmss_hash256_t secret ;
214+ xmss_hash256_t sk_prf ;
215+ xmss_hash256_t * tree ; // xmss_hash256_t [2^(h + 1) - 1]
213216} XMSS_KEY ;
214217
215218// XMSS_SHA2_10_256: 65,640
@@ -232,9 +235,9 @@ void xmss_key_cleanup(XMSS_KEY *key);
232235
233236typedef struct {
234237 uint32_t index ; // < 2^(XMSS_MAX_HEIGHT) = 2^20, always encode to 4 bytes
235- hash256_t random ;
236- wots_sig_t wots_sig ;
237- hash256_t auth_path [XMSS_MAX_HEIGHT ];
238+ xmss_hash256_t random ;
239+ xmss_wots_sig_t wots_sig ;
240+ xmss_hash256_t auth_path [XMSS_MAX_HEIGHT ];
238241} XMSS_SIGNATURE ;
239242
240243// XMSS_SM3_10_256 2500 bytes
@@ -243,6 +246,7 @@ typedef struct {
243246#define XMSS_SIGNATURE_MIN_SIZE (4 + 32 + 32*67 + 32 * XMSS_MIN_HEIGHT) // = 2500 bytes
244247#define XMSS_SIGNATURE_MAX_SIZE (4 + 32 + 32*67 + 32 * XMSS_MAX_HEIGHT) // = 2820 bytes
245248int xmss_signature_size (uint32_t xmss_type , size_t * siglen );
249+ int xmss_key_get_signature_size (const XMSS_KEY * key , size_t * siglen );
246250int xmss_signature_to_bytes (const XMSS_SIGNATURE * sig , uint32_t xmss_type , uint8_t * * out , size_t * outlen );
247251int xmss_signature_from_bytes (XMSS_SIGNATURE * sig , uint32_t xmss_type , const uint8_t * * in , size_t * inlen );
248252int xmss_signature_print (FILE * fp , int fmt , int ind , const char * label , const uint8_t * in , size_t inlen );
@@ -251,7 +255,7 @@ int xmss_signature_print_ex(FILE *fp, int fmt, int ind, const char *label, const
251255typedef struct {
252256 XMSS_PUBLIC_KEY xmss_public_key ;
253257 XMSS_SIGNATURE xmss_sig ;
254- HASH256_CTX hash256_ctx ;
258+ XMSS_HASH256_CTX hash256_ctx ;
255259} XMSS_SIGN_CTX ;
256260
257261int xmss_sign_init (XMSS_SIGN_CTX * ctx , XMSS_KEY * key );
@@ -335,19 +339,19 @@ size_t xmssmt_num_trees_nodes(size_t height, size_t layers);
335339
336340typedef struct {
337341 uint32_t xmssmt_type ;
338- hash256_t seed ;
339- hash256_t root ;
342+ xmss_hash256_t seed ;
343+ xmss_hash256_t root ;
340344} XMSSMT_PUBLIC_KEY ;
341345
342- #define XMSSMT_PUBLIC_KEY_SIZE (4 + sizeof(hash256_t ) + sizeof(hash256_t )) // = 68 bytes
346+ #define XMSSMT_PUBLIC_KEY_SIZE (4 + sizeof(xmss_hash256_t ) + sizeof(xmss_hash256_t )) // = 68 bytes
343347
344348typedef struct {
345349 XMSSMT_PUBLIC_KEY public_key ;
346350 uint64_t index ; // in [0, 2^60 - 1]
347- hash256_t secret ;
348- hash256_t sk_prf ;
349- hash256_t * trees ;
350- wots_sig_t wots_sigs [XMSSMT_MAX_LAYERS - 1 ];
351+ xmss_hash256_t secret ;
352+ xmss_hash256_t sk_prf ;
353+ xmss_hash256_t * trees ;
354+ xmss_wots_sig_t wots_sigs [XMSSMT_MAX_LAYERS - 1 ];
351355} XMSSMT_KEY ;
352356
353357/*
@@ -361,7 +365,7 @@ typedef struct {
361365 XMSSMT_SM3_60_12_256: 47,916 bytes
362366*/
363367int xmssmt_private_key_size (uint32_t xmssmt_type , size_t * len );
364- int xmssmt_build_auth_path (const hash256_t * tree , size_t height , size_t layers , uint64_t index , hash256_t * auth_path );
368+ int xmssmt_build_auth_path (const xmss_hash256_t * tree , size_t height , size_t layers , uint64_t index , xmss_hash256_t * auth_path );
365369
366370int xmssmt_key_generate (XMSSMT_KEY * key , uint32_t xmssmt_type );
367371int xmssmt_key_update (XMSSMT_KEY * key );
@@ -376,17 +380,18 @@ void xmssmt_key_cleanup(XMSSMT_KEY *key);
376380
377381typedef struct {
378382 uint64_t index ;
379- hash256_t random ;
380- wots_sig_t wots_sigs [XMSSMT_MAX_LAYERS ];
381- hash256_t auth_path [XMSSMT_MAX_HEIGHT ];
383+ xmss_hash256_t random ;
384+ xmss_wots_sig_t wots_sigs [XMSSMT_MAX_LAYERS ];
385+ xmss_hash256_t auth_path [XMSSMT_MAX_HEIGHT ];
382386} XMSSMT_SIGNATURE ;
383387
384388int xmssmt_index_to_bytes (uint64_t index , uint32_t xmssmt_type , uint8_t * * out , size_t * outlen );
385389int xmssmt_index_from_bytes (uint64_t * index , uint32_t xmssmt_type , const uint8_t * * in , size_t * inlen );
386390
387391#define XMSSMT_SIGNATURE_MAX_SIZE \
388- (sizeof(uint64_t) + sizeof(hash256_t ) + sizeof(wots_sig_t )*XMSSMT_MAX_LAYERS + sizeof(hash256_t )*XMSSMT_MAX_HEIGHT) // = 27688 bytes
392+ (sizeof(uint64_t) + sizeof(xmss_hash256_t ) + sizeof(xmss_wots_sig_t )*XMSSMT_MAX_LAYERS + sizeof(xmss_hash256_t )*XMSSMT_MAX_HEIGHT) // = 27688 bytes
389393
394+ int xmssmt_key_get_signature_size (const XMSSMT_KEY * key , size_t * siglen );
390395int xmssmt_signature_size (uint32_t xmssmt_type , size_t * siglen );
391396int xmssmt_signature_to_bytes (const XMSSMT_SIGNATURE * sig , uint32_t xmssmt_type , uint8_t * * out , size_t * outlen );
392397int xmssmt_signature_from_bytes (XMSSMT_SIGNATURE * sig , uint32_t xmssmt_type , const uint8_t * * in , size_t * inlen );
@@ -397,7 +402,7 @@ int xmssmt_signature_print(FILE *fp, int fmt, int ind, const char *label, const
397402typedef struct {
398403 XMSSMT_PUBLIC_KEY xmssmt_public_key ;
399404 XMSSMT_SIGNATURE xmssmt_sig ;
400- HASH256_CTX hash256_ctx ;
405+ XMSS_HASH256_CTX hash256_ctx ;
401406} XMSSMT_SIGN_CTX ;
402407
403408int xmssmt_sign_init (XMSSMT_SIGN_CTX * ctx , XMSSMT_KEY * key );
0 commit comments