API参考
载入中...
搜索中...
未找到
xmss_tree.h
1/*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16#ifndef XMSS_TREE_H
17#define XMSS_TREE_H
18
19#include "hitls_build.h"
20#if defined(HITLS_CRYPTO_XMSS) || defined(HITLS_CRYPTO_SLH_DSA)
21
22#include <stdint.h>
23#include <stddef.h>
24#include "xmss_common.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/*
31 * Generic Tree Context
32 * This structure encapsulates the parameters needed for tree operations
33 * Used by both XMSS and SLH-DSA for common tree operations
34 */
35typedef struct {
36 /* Algorithm parameters */
37 uint32_t n; // Hash output length
38 uint32_t hp; // Tree height per layer
39 uint32_t d; // Number of layers
40 uint32_t wotsLen; // WOTS+ chain length
41
42 /* Key material */
43 const uint8_t *pubSeed; // Public seed
44 const uint8_t *skSeed; // Private seed
45 const uint8_t *root; // Tree root (used for verification)
46
47 /* Generic hash function table */
48 const CryptHashFuncs *hashFuncs;
49
50 /* Generic address operation table */
51 const CryptAdrsOps *adrsOps;
52
53 /* Original context (for hash function callbacks) */
54 void *originalCtx;
55 bool isXmss;
56} TreeCtx;
57
58/*
59 * Compute an internal tree node
60 *
61 * Recursively computes a node in the XMSS Merkle tree.
62 * - If height == 0: computes WOTS+ public key (leaf node)
63 * - Otherwise: recursively computes children and hashes them
64 *
65 * @param node Output node (n bytes)
66 * @param idx Node index at the given height
67 * @param height Node height in the tree (0 = leaf)
68 * @param adrs Address for domain separation
69 * @param ctx Tree context
70 * @param authPath Output authentication path (optional, can be NULL)
71 * @param leafIdx Leaf index for which to build auth path
72 *
73 * @return CRYPT_SUCCESS on success
74 */
75int32_t XmssTree_ComputeNode(uint8_t *node, uint32_t idx, uint32_t height, void *adrs, const TreeCtx *ctx,
76 uint8_t *authPath, uint32_t leafIdx);
77
78/*
79 * Generate XMSS signature
80 *
81 * Generates a WOTS+ signature plus authentication path for a message.
82 *
83 * @param msg Message to sign (n bytes - already hashed)
84 * @param msgLen Length of message (must be n)
85 * @param idx Leaf index to sign
86 * @param adrs Address for domain separation (void* for polymorphism)
87 * @param ctx Tree context
88 * @param sig Output signature (WOTS+ sig + auth path)
89 * @param sigLen Input: buffer size, Output: actual signature length
90 * @param root Output tree root (n bytes)
91 *
92 * @return CRYPT_SUCCESS on success
93 */
94int32_t XmssTree_Sign(const uint8_t *msg, uint32_t msgLen, uint32_t idx, void *adrs, const TreeCtx *ctx, uint8_t *sig,
95 uint32_t *sigLen, uint8_t *root);
96
97/*
98 * Compute public key from XMSS signature
99 *
100 * Verifies an XMSS signature and computes the resulting tree root.
101 *
102 * @param msg Message that was signed (n bytes)
103 * @param msgLen Length of message (must be n)
104 * @param sig XMSS signature (WOTS+ sig + auth path)
105 * @param sigLen Length of signature
106 * @param idx Leaf index that was signed
107 * @param adrs Address for domain separation (void* for polymorphism)
108 * @param ctx Tree context
109 * @param pk Output public key / tree root (n bytes)
110 *
111 * @return CRYPT_SUCCESS on success
112 */
113int32_t XmssTree_Verify(const uint8_t *msg, uint32_t msgLen, const uint8_t *sig, uint32_t sigLen, uint32_t idx,
114 void *adrs, const TreeCtx *ctx, uint8_t *pk);
115
116/*
117 * Verify XMSS Hypertree signature (internal)
118 *
119 * Verifies a hypertree signature by iterating through all layers.
120 * For XMSS (d=1), this is just a single tree verification.
121 * For XMSSMT or SLH-DSA (d>1), this traverses the multi-layer tree structure.
122 *
123 * Execution flow:
124 * 1. Validates input parameters and signature length
125 * 2. Iterates through each layer (0 to d-1):
126 * a. For layer > 0: extract tree and leaf indices from treeIdx
127 * b. Set layer address and tree address in ADRS
128 * c. Verify current layer using XmssTree_Verify
129 * d. Use computed root as message for next layer
130 * 3. Compare final computed root with ctx->root
131 *
132 * @param msg Message digest to verify (n bytes)
133 * @param msgLen Length of message (n bytes)
134 * @param sig Signature buffer (contains auth paths for all layers)
135 * @param sigLen Length of signature
136 * @param treeIdx Tree index (for layer > 0)
137 * @param leafIdx Leaf index in the current layer
138 * @param ctx Tree context (contains expected root in ctx->root)
139 *
140 * @return CRYPT_SUCCESS on success
141 * CRYPT_XMSS_ERR_VERIFY_FAIL on verification failure
142 */
143int32_t HyperTree_Verify(const uint8_t *msg, uint32_t msgLen, const uint8_t *sig, uint32_t sigLen, uint64_t treeIdx,
144 uint32_t leafIdx, const TreeCtx *ctx);
145
146/*
147 * Sign using XMSS Hypertree (internal)
148 *
149 * Generates a signature for a hypertree structure by iterating through all layers.
150 * For XMSS (d=1), this is just a single tree signature.
151 * For XMSSMT (d>1), this traverses the multi-layer tree structure.
152 *
153 * This function encapsulates the multi-layer signing logic that is common
154 * to both XMSS and XMSSMT, making the code more maintainable and reusable.
155 *
156 * Execution flow:
157 * 1. Validates input parameters
158 * 2. Iterates through each layer (0 to d-1):
159 * a. For layer > 0: extract tree and leaf indices from treeIdx
160 * b. Set layer address and tree address in ADRS
161 * c. Sign current layer using XmssTree_Sign
162 * d. Use computed root as message for next layer
163 * 3. Returns the final signature containing all layer signatures
164 *
165 * @param msg Message digest to sign (n bytes)
166 * @param msgLen Length of message (n bytes)
167 * @param treeIdx Tree index (for layer > 0)
168 * @param leafIdx Leaf index in the current layer
169 * @param ctx Tree context (generic, works with both XMSS and SLH-DSA)
170 * @param sig Output signature buffer
171 * @param sigLen Input: buffer size, Output: actual signature length
172 *
173 * @return CRYPT_SUCCESS on success
174 */
175int32_t HyperTree_Sign(const uint8_t *msg, uint32_t msgLen, uint64_t treeIdx, uint32_t leafIdx, const TreeCtx *ctx,
176 uint8_t *sig, uint32_t *sigLen);
177#ifdef __cplusplus
178}
179#endif
180
181#endif // HITLS_CRYPTO_XMSS || HITLS_CRYPTO_SLH_DSA
182#endif // XMSS_TREE_H