Debian Codegraph @

sources/ippcp/stateful_sig/xmss/xmss_sign.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: ippsXMSSSign
23 //
24 // Purpose: XMSS signature generation.
25 //
26 // Returns: Reason:
27 // ippStsNullPtrErr pMsg == NULL
28 // pPrvKey == NULL
29 // pSign == NULL
30 // pBuffer == NULL
31 // ippStsLengthErr msgLen < 1
32 // ippStsLengthErr msgLen > IPP_MAX_32S - (n + 5 * n + len + key_gen_size)
33 // ippStsOutOfRangeErr index of current secret key is greater then number of keys
34 // ippStsNoErr no errors
35 //
36 // Parameters:
37 // pMsg pointer to the message data buffer
38 // msgLen message buffer length
39 // pPrvKey pointer to the XMSS private key
40 // pSign pointer to the XMSS signature
41 // pBuffer pointer to the temporary memory
42 //
43 *F*/
44 /* clang-format off */
45 IPPFUN(IppStatus, ippsXMSSSign, (const Ipp8u* pMsg,
46 const Ipp32s msgLen,
47 IppsXMSSPrivateKeyState* pPrvKey,
48 IppsXMSSSignatureState* pSign,
49 Ipp8u* pBuffer))
50 /* clang-format on */
51 {
52 IppStatus retCode = ippStsNoErr;
53
54 /* Check if any of input pointers are NULL */
55 IPP_BAD_PTR3_RET(pMsg, pPrvKey, pSign)
56 /* Check if temporary buffer is NULL */
57 IPP_BAD_PTR1_RET(pBuffer)
58 /* Check msg length */
59 IPP_BADARG_RET(msgLen < 1, ippStsLengthErr)
60
61 /* Parameters of the current XMSS */
62 Ipp32s h = 0;
63 cpWOTSParams params;
64 retCode = cp_xmss_set_params(pPrvKey->OIDAlgo, &h, &params);
65 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
66 Ipp32s len = params.len;
67 Ipp32s n = params.n;
68
69 Ipp32s key_gen_size;
70 retCode = ippsXMSSKeyGenBufferGetSize(&key_gen_size, pPrvKey->OIDAlgo);
71 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
72
73 IPP_BADARG_RET(msgLen > (Ipp32s)(IPP_MAX_32S) - (n + 5 * n + len + key_gen_size),
74 ippStsLengthErr);
75
76 Ipp32s pBufferSize;
77 retCode = ippsXMSSSignBufferGetSize(&pBufferSize, msgLen, pPrvKey->OIDAlgo);
78 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
79
80 // description of internals for OTS Hash / L-tree / Hash tree address is following
81 // +-----------------------------------------------------+
82 // | layer address (32 bits)|
83 // +-----------------------------------------------------+
84 // | tree address (64 bits)|
85 // +-----------------------------------------------------+
86 // | type = 0 / 1 / 2 (32 bits)|
87 // +-----------------------------------------------------+
88 // | OTS address / L-tree address / Padding = 0 (32 bits)|
89 // +-----------------------------------------------------+
90 // | chain address / tree height (32 bits)|
91 // +-----------------------------------------------------+
92 // | hash address / tree index (32 bits)|
93 // +-----------------------------------------------------+
94 // | keyAndMask (32 bits)|
95 // +-----------------------------------------------------+
96 Ipp8u adrs[ADRS_SIZE] = {
97 0, 0, 0, 0, // 0; 4
98 0, 0, 0, 0, 0, 0, 0, 0, // 4; 12
99 0, 0, 0, 0, // 12; 16
100 0, 0, 0, 0, // 16; 20
101 0, 0, 0, 0, // 20; 24
102 0, 0, 0, 0, // 24; 28
103 0, 0, 0, 0 // 28; 32
104 };
105 // pass the error if we are out of secret keys
106 // Note: there is no overflow since the maximum value for h is 20 according to the Spec
107 if (pPrvKey->idx >= (Ipp32u)(1 << h)) {
108 return ippStsOutOfRangeErr;
109 }
110
111 // idx
112 pSign->idx = pPrvKey->idx;
113
114 // fill r
115 Ipp8u* idx_buf = pBuffer;
116 cp_to_byte(idx_buf, n, pPrvKey->idx);
117 retCode = cp_xmss_prf(pPrvKey->pSK_PRF, idx_buf, pSign->r, idx_buf + n, &params);
118 if (ippStsNoErr != retCode) {
119 PurgeBlock(pBuffer, pBufferSize);
120 return retCode;
121 }
122
123 // byte[n] M_ = H_msg(r || getRoot(PK) || (toByte(idx_sig, n)), M);
124 Ipp8u* pMsg_ = pBuffer;
125 Ipp8u* temp_buf = pMsg_ + n;
126
127 cp_to_byte(temp_buf, n, /*h_msg padding id*/ 2);
128 CopyBlock(pSign->r, temp_buf + n, n);
129 CopyBlock(pPrvKey->pRoot, temp_buf + 2 * n, n);
130 cp_to_byte(temp_buf + 3 * n, n, pPrvKey->idx);
131 CopyBlock(pMsg, temp_buf + 4 * n, msgLen);
132
133 retCode = ippsHashMessage_rmf(temp_buf, 4 * n + msgLen, pMsg_, params.hash_method);
134 if (ippStsNoErr != retCode) {
135 PurgeBlock(pBuffer, pBufferSize);
136 return retCode;
137 }
138
139 // pAuthPath
140 retCode = cp_xmss_tree_hash(/*isKeyGen*/ 0,
141 pPrvKey,
142 adrs,
143 pSign->pAuthPath,
144 pPrvKey->idx,
145 temp_buf,
146 h,
147 &params);
148 if (ippStsNoErr != retCode) {
149 PurgeBlock(pBuffer, pBufferSize);
150 return retCode;
151 }
152
153 // pOTSSign
154 cp_to_byte(adrs, ADRS_SIZE, 0);
155 cp_xmss_set_tree_type(adrs, /*OTS hash*/ 0);
156 cp_xmss_set_ots_address(adrs, /*setOTSAddress*/ pPrvKey->idx);
157 retCode = cp_xmss_WOTS_sign(pMsg_,
158 pPrvKey->pSecretSeed,
159 pSign->pOTSSign,
160 pPrvKey->pPublicSeed,
161 adrs,
162 temp_buf,
163 &params);
164
165 // zeroize the temporary memory if everything else was successful
166 PurgeBlock(pBuffer, pBufferSize);
167 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
168
169 // during the next call another private key should be used to sign
170 pPrvKey->idx++;
171
172 // pass the error if we are out of secret keys
173 // Note: there is no overflow since the maximum value for h is 20 according to the Spec
174 if (pPrvKey->idx >= (Ipp32u)(1 << h)) {
175 return ippStsOutOfRangeErr;
176 }
177
178 return retCode;
179 }