Debian Codegraph @

sources/ippcp/stateful_sig/xmss/xmss_keygen.c

raw
1 /*************************************************************************
2 * Copyright (C) 2025 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 /*F*
22 // Name: ippsXMSSKeyGen
23 //
24 // Purpose: XMSS public and private keys generation.
25 //
26 // Returns: Reason:
27 // ippStsNullPtrErr pPrvKey == NULL
28 // pPubKey == NULL
29 // pBuffer == NULL
30 // ippStsBadArgErr pPrvKey->OIDAlgo > Max value for IppsXMSSAlgo
31 // ippStsBadArgErr pPrvKey->OIDAlgo <= 0
32 // ippStsNoErr no errors
33 //
34 // Parameters:
35 // pPrvKey pointer to the private key state
36 // pPubKey pointer to the public key state
37 // rndFunc function pointer to generate random numbers. It can be NULL
38 // pRndParam parameters for rndFunc. It can be NULL
39 // pBuffer pointer to the temporary memory
40 //
41 *F*/
42 /* clang-format off */
43 IPPFUN(IppStatus, ippsXMSSKeyGen, (IppsXMSSPrivateKeyState* pPrvKey,
44 IppsXMSSPublicKeyState* pPubKey,
45 IppBitSupplier rndFunc,
46 void* pRndParam,
47 Ipp8u* pBuffer))
48 /* clang-format on */
49 {
50 IppStatus retCode = ippStsNoErr;
51
52 /* Check if any of input pointers are NULL */
53 IPP_BAD_PTR1_RET(pPrvKey)
54 IPP_BAD_PTR1_RET(pPubKey)
55 /* Check if temporary buffer is NULL */
56 IPP_BAD_PTR1_RET(pBuffer)
57 IPP_BADARG_RET(pPrvKey->OIDAlgo > XMSS_SHA2_20_512, ippStsBadArgErr);
58 IPP_BADARG_RET(pPrvKey->OIDAlgo < XMSS_SHA2_10_256, ippStsBadArgErr);
59
60 /* Parameters of the current XMSS */
61 Ipp32s h = 0;
62 cpWOTSParams params;
63 retCode = cp_xmss_set_params(pPubKey->OIDAlgo, &h, &params);
64 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
65 Ipp32s n = params.n;
66
67 // description of internals for OTS Hash / L-tree / Hash tree address is following
68 // +-----------------------------------------------------+
69 // | layer address (32 bits)|
70 // +-----------------------------------------------------+
71 // | tree address (64 bits)|
72 // +-----------------------------------------------------+
73 // | type = 0 / 1 / 2 (32 bits)|
74 // +-----------------------------------------------------+
75 // | OTS address / L-tree address / Padding = 0 (32 bits)|
76 // +-----------------------------------------------------+
77 // | chain address / tree height (32 bits)|
78 // +-----------------------------------------------------+
79 // | hash address / tree index (32 bits)|
80 // +-----------------------------------------------------+
81 // | keyAndMask (32 bits)|
82 // +-----------------------------------------------------+
83 Ipp8u adrs[ADRS_SIZE] = {
84 0, 0, 0, 0, // 0; 4
85 0, 0, 0, 0, 0, 0, 0, 0, // 4; 12
86 0, 0, 0, 0, // 12; 16
87 0, 0, 0, 0, // 16; 20
88 0, 0, 0, 0, // 20; 24
89 0, 0, 0, 0, // 24; 28
90 0, 0, 0, 0 // 28; 32
91 };
92
93 // fill private key fields
94 pPrvKey->idx = 0;
95
96 retCode = cp_rand_num((Ipp32u*)pPrvKey->pSecretSeed, n, rndFunc, pRndParam);
97 if (ippStsNoErr != retCode) {
98 PurgeBlock(pPrvKey->pSecretSeed, n); // zeroize the secret seed if error occurs
99 return retCode;
100 }
101
102 retCode = cp_rand_num((Ipp32u*)pPrvKey->pSK_PRF, n, rndFunc, pRndParam);
103 if (ippStsNoErr != retCode) {
104 PurgeBlock(pPrvKey->pSecretSeed, n); // zeroize the secret seed if error occurs
105 PurgeBlock(pPrvKey->pSK_PRF, n); // zeroize the SK_PRF if error occurs
106 return retCode;
107 }
108
109 retCode = cp_rand_num((Ipp32u*)pPrvKey->pPublicSeed, n, rndFunc, pRndParam);
110 if (ippStsNoErr != retCode) {
111 PurgeBlock(pPrvKey->pSecretSeed, n); // zeroize the secret seed if error occurs
112 PurgeBlock(pPrvKey->pSK_PRF, n); // zeroize the SK_PRF if error occurs
113 PurgeBlock(pPrvKey->pPublicSeed, n); // zeroize the public seed if error occurs
114 return retCode;
115 }
116
117 Ipp32s pBufferSize;
118 retCode = ippsXMSSKeyGenBufferGetSize(&pBufferSize, pPrvKey->OIDAlgo);
119 if (ippStsNoErr != retCode) {
120 PurgeBlock(pPrvKey->pSecretSeed, n); // zeroize the secret seed if error occurs
121 PurgeBlock(pPrvKey->pSK_PRF, n); // zeroize the SK_PRF if error occurs
122 PurgeBlock(pPrvKey->pPublicSeed, n); // zeroize the public seed if error occurs
123 return retCode;
124 }
125
126 retCode = cp_xmss_tree_hash(/*isKeyGen*/ 1,
127 pPrvKey,
128 adrs,
129 pPrvKey->pRoot,
130 /*empty*/ 0,
131 pBuffer,
132 h,
133 &params);
134 PurgeBlock(pBuffer, pBufferSize); // zeroize the temporary memory
135 if (ippStsNoErr != retCode) {
136 PurgeBlock(pPrvKey->pSecretSeed, n); // zeroize the secret seed if error occurs
137 PurgeBlock(pPrvKey->pSK_PRF, n); // zeroize the SK_PRF if error occurs
138 PurgeBlock(pPrvKey->pPublicSeed, n); // zeroize the public seed if error occurs
139 return retCode;
140 }
141
142 // fill public key fields
143 CopyBlock(pPrvKey->pRoot, pPubKey->pRoot, n);
144 CopyBlock(pPrvKey->pPublicSeed, pPubKey->pSeed, n);
145
146 return retCode;
147 }