Debian Codegraph @

sources/ippcp/stateful_sig/xmss/xmss_internal.c

raw
1 /*************************************************************************
2 * Copyright (C) 2023 Intel Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *************************************************************************/
16
17 #include "owndefs.h"
18 #include "stateful_sig/xmss_internal/xmss.h"
19 #include "stateful_sig/common.h"
20
21 /*
22 * Does the randomized hashing in the tree
23 *
24 * Input parameters:
25 * left left half of the hash function input (n-byte array)
26 * right right half of the hash function input (n-byte array)
27 * seed key for the prf function (n-byte array)
28 * adrs address ADRS of the hash function call
29 * temp_buf temporary memory (size is 6 * n bytes at least)
30 * params WOTS parameters (w, log2_w, n, len, len_1, hash_method)
31 *
32 * Output parameters:
33 * out resulted n-byte array that contains hash
34 */
35
36 /* clang-format off */
37 IPP_OWN_DEFN(IppStatus, cp_xmss_rand_hash, (Ipp8u* left,
38 Ipp8u* right,
39 Ipp8u* seed,
40 Ipp8u* adrs,
41 Ipp8u* out,
42 Ipp8u* temp_buf,
43 const cpWOTSParams* params))
44 /* clang-format on */
45 {
46 IppStatus retCode = ippStsNoErr;
47 Ipp8u* pMsg = temp_buf;
48 Ipp8u* pKey = temp_buf + 2 * params->n;
49 Ipp8u* temp = temp_buf + 3 * params->n;
50
51 cp_xmss_set_key_and_mask(adrs, /*key bitmask*/ 0);
52 retCode = cp_xmss_prf(seed, adrs, pKey, temp, params);
53 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
54
55 cp_xmss_set_key_and_mask(adrs, /*left bitmask*/ 1);
56 retCode = cp_xmss_prf(seed, adrs, pMsg, temp, params);
57 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
58
59 cp_xmss_set_key_and_mask(adrs, /*right bitmask*/ 2);
60 retCode = cp_xmss_prf(seed, adrs, pMsg + params->n, temp, params);
61 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
62
63 // null adrs
64 cp_xmss_set_key_and_mask(adrs, 0);
65
66 // (LEFT XOR BM_0) || (RIGHT XOR BM_1)
67 for (Ipp32s i = 0; i < params->n; ++i) {
68 pMsg[i] = left[i] ^ pMsg[i];
69 pMsg[i + params->n] = right[i] ^ pMsg[i + params->n];
70 }
71
72 //H(KEY, pMsg);
73 retCode = cp_do_xmss_hash(/*H padding id*/ 1, pKey, pMsg, 2 * params->n, out, temp, params);
74 return retCode;
75 }
76
77 /*
78 * Compute the leaves of the binary hash tree, a so-called L-tree.
79 * An L-tree is an unbalanced binary hash tree, distinct but
80 * similar to the main XMSS binary hash tree. The function takes as input a
81 * WOTS+ public key pk and compresses it to a single n-byte value pk[0].
82 * It also takes as input an L-tree address adrs that encodes the address
83 * of the L-tree and seed
84 *
85 * temp_buf size is 6 * n bytes at least.
86 *
87 */
88
89 /* clang-format off */
90 IPP_OWN_DEFN(IppStatus, cp_xmss_ltree, (Ipp8u* pk,
91 Ipp8u* seed,
92 Ipp8u* adrs,
93 Ipp8u* temp_buf,
94 const cpWOTSParams* params))
95 /* clang-format on */
96 {
97 IppStatus retCode = ippStsNoErr;
98 Ipp32s len_ = params->len;
99 Ipp32s n_ = params->n;
100
101 // tree height is 0 for now
102 cp_xmss_set_tree_height(adrs, 0);
103
104 while (len_ > 1) {
105 for (Ipp32s i = 0; i < len_ / 2; i++) {
106 cp_xmss_set_tree_index_8(adrs, (Ipp8u)i);
107
108 retCode = cp_xmss_rand_hash(pk + (2 * i * n_),
109 pk + ((2 * i * n_) + n_),
110 seed,
111 adrs,
112 pk + (i * n_),
113 temp_buf,
114 params);
115 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
116 }
117 if ((len_ & 1) == 1) {
118 CopyBlock(pk + (n_ * (len_ - 1)), pk + (n_ * (len_ / 2)), n_);
119 len_ = (len_ >> 1) + 1;
120 } else {
121 len_ = len_ >> 1;
122 }
123 // increase the tree height
124 cp_xmss_set_tree_height(adrs, cp_xmss_get_tree_height(adrs) + 1);
125 }
126
127 // null tree height and tree index
128 cp_xmss_set_tree_height(adrs, 0);
129 cp_xmss_set_tree_index_8(adrs, (Ipp8u)0);
130 return retCode;
131 }
132
133 /*
134 * Builds the binary hash tree, a so-called L-tree.
135 * For the height of a node within a tree, counting starts with the leaves at height zero.
136 * The treeHash algorithm returns the root node of a tree (out).
137 * The treeHash algorithm described here uses a stack holding up to (h - 1) nodes.
138 * We furthermore assume that the height of a node is stored alongside
139 * a node’s value (an n-byte string) on the stack.
140 *
141 * temp_buf size is (h + 1) * (n + 1) + 2 * len * n + 7 * n + 32 bytes at least.
142 *
143 */
144 /* clang-format off */
145 IPP_OWN_DEFN(IppStatus, cp_xmss_tree_hash, (Ipp8u isKeyGen,
146 IppsXMSSPrivateKeyState* pSecretKey,
147 Ipp8u* adrs,
148 Ipp8u* out,
149 Ipp32u idx_leaf,
150 Ipp8u* temp_buf,
151 Ipp32s h,
152 const cpWOTSParams* params))
153 /* clang-format on */
154 {
155 IppStatus retCode = ippStsNoErr;
156 Ipp8u* heights = temp_buf;
157 Ipp8u* stack = heights + (h + 1);
158
159 Ipp32s len = params->len;
160 Ipp32s n = params->n;
161 Ipp32s len_n = len * n;
162 Ipp32s stack_size = 0;
163 Ipp8u *node, *temp_node;
164 // Note: there is no overflow since the maximum value for h is 20 according to the Spec
165 for (Ipp32u i = 0; i < (Ipp32u)(1 << h); ++i) {
166 // generate OTS public key
167 cp_to_byte(adrs, ADRS_SIZE, 0);
168 cp_xmss_set_tree_type(adrs, /*OTS hash*/ 0);
169 cp_xmss_set_ots_address(adrs, i);
170 node = stack + (h + 1) * n; // size: len * n
171 temp_node = node + len_n;
172 retCode = cp_xmss_WOTS_genPK(pSecretKey->pSecretSeed,
173 node,
174 pSecretKey->pPublicSeed,
175 adrs,
176 temp_node,
177 params); // size: 7 * n + 32
178 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
179
180 // call ltree
181 cp_to_byte(adrs, ADRS_SIZE, 0);
182 cp_xmss_set_tree_type(adrs, /*L-tree*/ 1);
183 cp_xmss_set_ltree_address(adrs, i);
184 retCode =
185 cp_xmss_ltree(node, pSecretKey->pPublicSeed, adrs, temp_node, params); // size: 7 * n
186 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
187
188 if (isKeyGen == 0 && (idx_leaf ^ 1) == i) {
189 CopyBlock(node, out, n);
190 }
191
192 // calculate a root of sub-tree
193 cp_to_byte(adrs, ADRS_SIZE, 0);
194 cp_xmss_set_tree_type(adrs, /*hash tree*/ 2);
195 cp_xmss_set_tree_height(adrs, 0);
196 cp_xmss_set_tree_index_32(adrs, i);
197 heights[stack_size] = 0;
198 while (stack_size > 0 && heights[stack_size - 1] == heights[stack_size]) {
199 Ipp32u idx = cp_xmss_get_tree_index(adrs);
200 idx = (idx - 1) / 2;
201 cp_xmss_set_tree_index_32(adrs, idx);
202 stack_size--; // stack.pop
203
204 retCode = cp_xmss_rand_hash(stack + (stack_size * n),
205 node,
206 pSecretKey->pPublicSeed,
207 adrs,
208 node,
209 temp_node,
210 params);
211 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
212
213 heights[stack_size]++;
214 cp_xmss_set_tree_height(adrs, heights[stack_size]);
215
216 if (isKeyGen == 0 && ((idx_leaf >> heights[stack_size]) ^ 1) == idx) {
217 CopyBlock(node, out + heights[stack_size] * n, n);
218 }
219 }
220 CopyBlock(node, stack + (stack_size * n), n);
221 stack_size++; // stack.push
222 }
223 if (isKeyGen == 1) {
224 CopyBlock(stack, out, n);
225 }
226
227 return retCode;
228 }