Debian Codegraph @

sources/ippcp/stateful_sig/xmss/wots_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 "owncp.h"
19 #include "pcpbnumisc.h"
20 #include "pcpprng.h"
21 #include "stateful_sig/xmss_internal/wots.h"
22 #include "stateful_sig/common.h"
23
24 /*
25 * A byte string can be considered as a string of base w numbers, i.e.,
26 * integers in the set {0, ... , w - 1}. The correspondence is defined
27 * by the function base_w as follows. If X
28 * is a byte string, and w is a member of the set {4, 16}, then
29 * the function outputs an array of out_len integers between 0 and w - 1.
30 *
31 * Input parameters:
32 * X byte string
33 * out_len length of basew. It is REQUIRED to be less than or equal
34 * to 8 * len_X / lg(w)
35 * params WOTS parameters (w, log2_w, n, len, len_1, hash_method)
36 *
37 * Output parameters:
38 * basew resulted out_len-byte array
39 */
40
41 /* clang-format off */
42 IPP_OWN_DEFN(void, cp_xmss_base_w, (const Ipp8u* X,
43 Ipp32s out_len,
44 Ipp8u* basew,
45 cpWOTSParams* params))
46 /* clang-format on */
47 {
48 Ipp32s in = 0;
49 Ipp32s out = 0;
50 Ipp32u total = 0;
51 Ipp32s bits = 0;
52 for (Ipp32s consumed = 0; consumed < out_len; consumed++) {
53 if (bits == 0) {
54 total = X[in];
55 in++;
56 bits += /*bit size of 1 byte*/ 8;
57 }
58 bits -= params->log2_w;
59 basew[out] = (Ipp8u)((total >> bits) & (params->w - 1));
60 out++;
61 }
62 }
63
64 /*
65 * Generates pseudorandom output of length n from an n-byte key and a 32-byte index
66 * using hash functions as follows
67 * Hash(toByte(padding_id, n) || key || msg)
68 *
69 * Returns:
70 * result of ippsHashMessage_rmf
71 *
72 * Input parameters:
73 * padding_id 32-byte value that is used as a padding value for a hash function
74 * key n-byte key for the hash function
75 * msg byte array
76 * msgLen length of msg
77 * temp_buf temporary memory (size is 2 * n + msgLen bytes at least)
78 * params WOTS parameters (w, log2_w, n, len, len_1, hash_method)
79 *
80 * Output parameters:
81 * out resulted n-byte array
82 */
83
84 /* clang-format off */
85 IPP_OWN_DEFN(IppStatus, cp_do_xmss_hash, (Ipp32u padding_id,
86 const Ipp8u* key,
87 const Ipp8u* msg,
88 Ipp32s msgLen,
89 Ipp8u* out,
90 Ipp8u* temp_buf,
91 const cpWOTSParams* params))
92 /* clang-format on */
93 {
94
95 cp_to_byte(temp_buf, params->n, padding_id);
96 CopyBlock(key, temp_buf + params->n, params->n);
97 CopyBlock(msg, temp_buf + 2 * params->n, msgLen);
98
99 return ippsHashMessage_rmf(temp_buf, 2 * params->n + msgLen, out, params->hash_method);
100 }
101
102 /*
103 * Generates pseudorandom output of length n from an n-byte key and a 32-byte index
104 * using hash functions as follows
105 * Hash(toByte(3, n) || key || msg)
106 *
107 * Input parameters:
108 * key n-byte key for the hash function
109 * msg 32-byte array
110 * temp_buf temporary memory (size is 2 * n + 32 bytes at least)
111 * params WOTS parameters (w, log2_w, n, len, len_1, hash_method)
112 *
113 * Output parameters:
114 * out resulted n-byte array
115 */
116
117 /* clang-format off */
118 IPP_OWN_DEFN(IppStatus, cp_xmss_prf, (const Ipp8u* key,
119 const Ipp8u* msg,
120 Ipp8u* out,
121 Ipp8u* temp_buf,
122 const cpWOTSParams* params))
123 /* clang-format on */
124 {
125 return cp_do_xmss_hash(/*prf function padding id*/ 3,
126 key,
127 msg,
128 /*byte size of index*/ ADRS_SIZE,
129 out,
130 temp_buf,
131 params);
132 }
133
134 /*
135 * Generates pseudorandom output of length n from an n-byte key and a 32-byte index
136 * using hash functions for key generation only as follows
137 * Hash(toByte(4, n) || key || msg)
138 *
139 * Input parameters:
140 * key n-byte key for the hash function
141 * msg byte array
142 * msgLen length of msg
143 * temp_buf temporary memory (size is 2 * n + msgLen bytes at least)
144 * params WOTS parameters (w, log2_w, n, len, len_1, hash_method)
145 *
146 * Output parameters:
147 * out resulted n-byte array
148 */
149
150 /* clang-format off */
151 IPP_OWN_DEFN(IppStatus, cp_xmss_prf_keygen, (const Ipp8u* key,
152 const Ipp8u* msg,
153 Ipp32s msgLen,
154 Ipp8u* out,
155 Ipp8u* temp_buf,
156 const cpWOTSParams* params))
157 /* clang-format on */
158 {
159 return cp_do_xmss_hash(/*prf_keygen function padding id*/ 4,
160 key,
161 msg,
162 msgLen,
163 out,
164 temp_buf,
165 params);
166 }
167
168 /*
169 * Computes an iteration of F on an n-byte X using outputs of PRF. It takes an
170 * OTS hash address (adrs) as input. This address will have the first six
171 * 32-bit words set to encode the address of this chain. In each iteration, PRF
172 * is used to generate a key for F and a bitmask that is XORed to the
173 * intermediate result before it is processed by F. In the following, pSeed is an
174 * n-byte string. To generate the keys and bitmasks, PRF is called with
175 * pSeed as key and adrs as input. The chaining function also takes as input
176 * a start index i, a number of steps s. The chaining function returns as output the array
177 * obtained by iterating F for s times on input X, using the outputs of PRF.)
178 *
179 * temp_buf size is 4 * n bytes at least.
180 */
181
182 /* clang-format off */
183 IPP_OWN_DEFN(IppStatus, cp_xmss_chain, (Ipp8u * X,
184 Ipp8u i,
185 Ipp8u s,
186 Ipp8u* pSeed,
187 Ipp8u* adrs,
188 Ipp8u* out,
189 Ipp8u* temp_buf,
190 const cpWOTSParams* params))
191 /* clang-format on */
192 {
193 IppStatus retCode = ippStsNoErr;
194 Ipp8u* bm = temp_buf;
195 Ipp8u* key = bm;
196 CopyBlock(X, out, params->n);
197
198 for (Ipp8u k = i; k < (i + s) && k < params->w; k++) {
199 cp_xmss_set_hash_address(adrs, k);
200
201 // BM = PRF(SEED, ADRS);
202 cp_xmss_set_key_and_mask(adrs, /*bitmask for bm*/ 1);
203 retCode = cp_xmss_prf(pSeed, adrs, bm, temp_buf + params->n, params);
204 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
205
206 // out = out ^ BM;
207 for (Ipp32s j = 0; j < params->n; ++j) {
208 out[j] = out[j] ^ bm[j];
209 }
210
211 // KEY = PRF(SEED, ADRS);
212 cp_xmss_set_key_and_mask(adrs, /*bitmask for key*/ 0);
213 retCode = cp_xmss_prf(pSeed, adrs, key, temp_buf + params->n, params);
214 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
215
216 // tmp = F(KEY, out);
217 retCode = cp_do_xmss_hash(/*F function padding id*/ 0,
218 key,
219 out,
220 params->n,
221 out,
222 temp_buf + params->n,
223 params);
224 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
225 }
226 cp_xmss_set_hash_address(adrs, 0);
227 return retCode;
228 }
229
230 /*
231 * In order to verify a signature sig on a message M, the verifier
232 * computes a WOTS+ public key value from the signature. This can be
233 * done by "completing" the chain computations starting from the
234 * signature values, using the base params->w values of the message hash and its
235 * checksum. An OTS hash address adrs and a seed pSeed have to be provided by the calling
236 * algorithm. This address will encode the address of the WOTS+ key
237 * pair within a greater structure. Hence, a WOTS+ algorithm MUST NOT
238 * manipulate any parts of adrs except for the last three 32-bit words.
239 * Please note that the pSeed used here is public information also
240 * available to a verifier.
241 *
242 * temp_buf size is (2 * params->len - params->len_1 + 4 * n) bytes at least.
243 *
244 * Output parameters:
245 * out resulted len * n bytes array that contains WOTS+ public key
246 *
247 */
248
249 /* clang-format off */
250 IPP_OWN_DEFN(IppStatus, cp_xmss_WOTS_pkFromSig, (const Ipp8u* M,
251 Ipp8u* sig,
252 Ipp8u* pSeed,
253 Ipp8u* adrs,
254 Ipp8u* out,
255 Ipp8u* temp_buf,
256 cpWOTSParams* params))
257 /* clang-format on */
258 {
259 IppStatus retCode = ippStsNoErr;
260 // Convert message to base w
261 Ipp32s len_2 = params->len - params->len_1;
262 Ipp8u* msg = temp_buf;
263 cp_xmss_base_w(M, params->len_1, msg, params);
264
265 // Compute checksum
266 Ipp32u csum = 0;
267 for (Ipp32s i = 0; i < params->len_1; i++) {
268 csum = csum + params->w - 1 - msg[i];
269 }
270
271 // Convert csum to base w
272 csum = csum << (8 - ((len_2 * params->log2_w) & 7));
273 Ipp32s len_2_bytes = cp_xmss_ceil((len_2 * params->log2_w) / 8.0);
274 cp_to_byte(msg + params->len, len_2_bytes, csum);
275 cp_xmss_base_w(msg + params->len, len_2, msg + params->len_1, params);
276
277 for (Ipp32s i = 0; i < params->len; i++) {
278 cp_xmss_set_chain_address(adrs, (Ipp8u)i);
279 retCode = cp_xmss_chain(sig + i * params->n,
280 msg[i],
281 (Ipp8u)(params->w - 1 - msg[i]),
282 pSeed,
283 adrs,
284 out + (i * params->n),
285 temp_buf + (params->len + len_2),
286 params);
287 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
288 }
289 // null chain address for next call
290 cp_xmss_set_chain_address(adrs, 0);
291 return retCode;
292 }
293
294 /*
295 * Generates the WOTS+ secret key.
296 *
297 * temp_buf size is (n + 32 + 2 * n + n + 32 = 4 * n + 64) bytes at least.
298 *
299 * Output parameters:
300 * out resulted n bytes array that contains WOTS+ secret key
301 *
302 */
303 /* clang-format off */
304 IPP_OWN_DEFN(IppStatus, cp_xmss_WOTS_genSK, (Ipp8u* pSecretSeed,
305 Ipp8u* pPublicSeed,
306 Ipp8u* adrs,
307 Ipp8u* out,
308 Ipp8u* temp_buf,
309 const cpWOTSParams* params))
310 /* clang-format on */
311 {
312 Ipp32s n = params->n;
313 Ipp8u* temp_for_prf = temp_buf + (n + ADRS_SIZE);
314 // sk[i] = PRFkeygen(S_XMSS, SEED || ADRS);
315 CopyBlock(pPublicSeed, temp_buf, n);
316 CopyBlock(adrs, temp_buf + n, ADRS_SIZE);
317 return cp_xmss_prf_keygen(pSecretSeed, temp_buf, n + ADRS_SIZE, out, temp_for_prf, params);
318 }
319
320 /*
321 * Generates the WOTS+ public key.
322 *
323 * temp_buf size is (7 * n + 32) bytes at least.
324 *
325 * Output parameters:
326 * pPublicKey resulted len * n bytes array that contains WOTS+ public key
327 *
328 */
329 /* clang-format off */
330 IPP_OWN_DEFN(IppStatus, cp_xmss_WOTS_genPK, (Ipp8u* pSecretSeed,
331 Ipp8u* pPublicKey,
332 Ipp8u* pPublicSeed,
333 Ipp8u* adrs,
334 Ipp8u* temp_buf,
335 const cpWOTSParams* params))
336 /* clang-format on */
337 {
338 IppStatus retCode = ippStsNoErr;
339 Ipp32s n = params->n;
340 Ipp32s len = params->len;
341 Ipp8u* sk = temp_buf;
342 for (Ipp32s i = 0; i < len; i++) {
343 // generate secret key from secret seed
344 cp_xmss_set_chain_address(adrs, (Ipp8u)i);
345 retCode = cp_xmss_WOTS_genSK(pSecretSeed,
346 pPublicSeed,
347 adrs,
348 sk,
349 temp_buf + n,
350 params); // 4 * n + 64
351 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
352
353 // chaining
354 retCode = cp_xmss_chain(sk,
355 (Ipp8u)0,
356 (Ipp8u)(params->w - 1),
357 pPublicSeed,
358 adrs,
359 pPublicKey + (i * n),
360 temp_buf + n,
361 params);
362 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
363 }
364 // null chain address for next call
365 cp_xmss_set_chain_address(adrs, 0);
366 return retCode;
367 }
368
369 /*
370 * Generates the WOTS+ signature for the message M using pSecretSeed.
371 *
372 * temp_buf size is (7 * n + len) bytes at least.
373 *
374 * Output parameters:
375 * pSignature resulted len * n bytes array that contains WOTS+ public key
376 *
377 */
378 /* clang-format off */
379 IPP_OWN_DEFN(IppStatus, cp_xmss_WOTS_sign, (const Ipp8u* M,
380 Ipp8u* pSecretSeed,
381 Ipp8u* pSignature,
382 Ipp8u* pPublicSeed,
383 Ipp8u* adrs,
384 Ipp8u* temp_buf,
385 cpWOTSParams* params))
386 /* clang-format on */
387 {
388 IppStatus retCode = ippStsNoErr;
389 // Convert message to base w
390 Ipp32s len = params->len;
391 Ipp32s len_1 = params->len_1;
392 Ipp32s len_2 = len - len_1;
393 Ipp32s n = params->n;
394 Ipp8u* msg = temp_buf;
395 cp_xmss_base_w(M, params->len_1, msg, params);
396
397 // Compute checksum
398 Ipp32u csum = 0;
399 for (Ipp32s i = 0; i < params->len_1; i++) {
400 csum = csum + params->w - 1 - msg[i];
401 }
402
403 // Convert csum to base w
404 csum = csum << (8 - ((len_2 * params->log2_w) & 7));
405 Ipp32s len_2_bytes = cp_xmss_ceil((len_2 * params->log2_w) / 8.0);
406 cp_to_byte(msg + len, len_2_bytes, csum);
407 cp_xmss_base_w(msg + len, len_2, msg + len_1, params);
408
409 Ipp8u* sk = temp_buf + len;
410 for (Ipp32s i = 0; i < len; i++) {
411 // generate secret key from secret seed
412 // sk[j] = PRFkeygen(S_XMSS, SEED || ADRS);
413 cp_xmss_set_chain_address(adrs, (Ipp8u)i);
414 retCode =
415 cp_xmss_WOTS_genSK(pSecretSeed, pPublicSeed, adrs, sk, sk + n, params); // 4 * n + 64
416 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
417
418 // chaining
419 retCode = cp_xmss_chain(sk,
420 (Ipp8u)0,
421 msg[i],
422 pPublicSeed,
423 adrs,
424 pSignature + (i * n),
425 sk + n,
426 params);
427 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
428 }
429 // null chain address for next call
430 cp_xmss_set_chain_address(adrs, 0);
431 return retCode;
432 }