Debian Codegraph @

sources/ippcp/stateful_sig/lms/lmots_internal.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 #include "stateful_sig/lms_internal/lmots.h"
17 #include "stateful_sig/common.h"
18
19 /*
20 * Does the randomized hashing for OTS (H function in the Spec)
21 * I_q size: I_q_len + val1Len + val2Len + msgLen
22 *
23 * Input parameters:
24 * I_q merged buffer consisting of I and the q value
25 * I_q_len size of I_q
26 * val1 1st value passing to the function
27 * val1Len size of val1
28 * val2 2nd value passing to the function
29 * val2Len size of val2
30 * pMsg pointer to message
31 * msgLen size of pMsg
32 * hash_method Crypto library hash method
33 *
34 * Output parameters:
35 * out resulted n-byte array that contains hash
36 */
37 /* clang-format off */
38 IPP_OWN_DEFN(IppStatus, cp_lms_H, (Ipp8u* I_q, const Ipp32s I_q_len,
39 Ipp32u val1, const Ipp32s val1Len,
40 Ipp8u* val2, const Ipp32s val2Len,
41 const Ipp8u* pMsg, const Ipp32s msgLen,
42 Ipp8u* out, const IppsHashMethod* hash_method))
43 /* clang-format on */
44 {
45 int total_size = I_q_len;
46
47 cp_to_byte(I_q + total_size, val1Len, val1);
48 total_size += val1Len;
49
50 CopyBlock(val2, I_q + total_size, val2Len);
51 total_size += val2Len;
52
53 if (msgLen > 0) {
54 CopyBlock(pMsg, I_q + total_size, msgLen);
55 total_size += msgLen;
56 }
57
58 return ippsHashMessage_rmf(I_q, total_size, out, hash_method);
59 }
60
61 /*
62 * Generates OTS public key
63 *
64 * Input parameters:
65 * secret_seed random seed to generate secret key
66 * I pointer to I buffer (I from the spec)
67 * q q value from the spec
68 * temp_buf temporary memory (size is CP_PK_I_BYTESIZE + 4 + 2 + 1 + n + n * p bytes at least)
69 * params OTS parameters
70 *
71 * Output parameters:
72 * out resulted n-byte array that contains OTS public key
73 */
74 /* clang-format off */
75 IPP_OWN_DEFN(IppStatus, cp_lms_OTS_genPK, (
76 Ipp8u* secret_seed, Ipp8u* pI, Ipp32u q,
77 Ipp8u* out, Ipp8u* temp_buf, const cpLMOTSParams* params))
78 /* clang-format on */
79 {
80 IppStatus retCode = ippStsErr;
81 const Ipp32s nParam = (Ipp32s)params->n;
82 const Ipp32u wParam = params->w;
83 const Ipp32u pParam = params->p;
84 const Ipp32u two_w = (1 << wParam);
85 Ipp8u D_PRIV_ = D_PRIV;
86
87 Ipp8u* I_q = temp_buf;
88 CopyBlock(pI, I_q, CP_PK_I_BYTESIZE);
89 cp_to_byte(I_q + CP_PK_I_BYTESIZE, /*q byteLen*/ 4, q);
90
91 Ipp8u* sk_begin = (I_q + CP_PK_I_BYTESIZE + 4 + 2 + 1 + nParam);
92 Ipp8u* sk = sk_begin; // size = n * p
93 for (Ipp32u i = 0; i < pParam; i++) {
94 // generate secret key from secret seed
95 // x_q[i] = cp_lms_H(I || u32str(q) || u16str(i) || u8str(0xff) || SEED)
96 retCode = cp_lms_H(I_q,
97 CP_PK_I_BYTESIZE + /*q byteLen*/ 4,
98 i,
99 /*i byteLen*/ 2,
100 &D_PRIV_,
101 /*D_PRIV byteLen*/ 1,
102 secret_seed,
103 nParam,
104 sk,
105 params->hash_method);
106 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
107
108 // chaining
109 for (Ipp8u j = 0; j < two_w - 1; j++) {
110 // sk = cp_lms_H(I || u32str(q) || u16str(i) || u8str(j) || sk)
111 retCode = cp_lms_H(I_q,
112 CP_PK_I_BYTESIZE + /*q byteLen*/ 4,
113 i,
114 /*i byteLen*/ 2,
115 &j,
116 /*j byteLen*/ 1,
117 sk,
118 nParam,
119 sk,
120 params->hash_method);
121 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
122 }
123 sk += nParam;
124 }
125 // public_key = cp_lms_H(I || u32str(q) || u16str(D_PBLC) || sk)
126 retCode = cp_lms_H(I_q,
127 CP_PK_I_BYTESIZE + /*q byteLen*/ 4,
128 D_PBLC,
129 /*D_PBLC byteLen*/ 2,
130 sk_begin,
131 nParam * (Ipp32s)pParam,
132 NULL,
133 0,
134 out,
135 params->hash_method);
136 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
137
138 return retCode;
139 }
140
141 /*
142 * Generates OTS signature
143 *
144 * Input parameters:
145 * pMsg pointer to message
146 * msgLen size of pMsg
147 * secret_seed random seed to generate secret key
148 * pI pointer to I buffer (I from the spec)
149 * q q value from the spec
150 * pC pointer to C buffer (C from the spec)
151 * temp_buf temporary memory (size is CP_PK_I_BYTESIZE + 4 + 2 + n + max(msgLen, 1 + n) bytes at least)
152 * params OTS parameters
153 *
154 * Output parameters:
155 * pY pointer to Y buffer (Y from the spec)
156 */
157 /* clang-format off */
158 IPP_OWN_DEFN(IppStatus, cp_lms_OTS_sign, (
159 const Ipp8u* pMsg, const Ipp32s msgLen,
160 Ipp8u* secret_seed, Ipp8u* pI, Ipp32u q, Ipp8u* pC,
161 Ipp8u* pY,
162 Ipp8u* temp_buf, const cpLMOTSParams* params))
163 /* clang-format on */
164 {
165 IppStatus retCode = ippStsErr;
166 const Ipp32s nParam = (Ipp32s)params->n;
167 const Ipp32u wParam = params->w;
168 const Ipp32u pParam = params->p;
169 Ipp8u D_PRIV_ = D_PRIV;
170
171 Ipp8u Q[CP_LMS_MAX_HASH_BYTESIZE + CP_CKSM_BYTESIZE];
172 Ipp8u* I_q = temp_buf;
173 // Q = cp_lms_H(I || u32str(q) || u16str(D_MESG) || pC || pMsg)
174 // I_q size: CP_PK_I_BYTESIZE + 4 + 2 + nParam + msgLen
175 CopyBlock(pI, I_q, CP_PK_I_BYTESIZE);
176 cp_to_byte(I_q + CP_PK_I_BYTESIZE, /*q byteLen*/ 4, q);
177 retCode = cp_lms_H(I_q,
178 CP_PK_I_BYTESIZE + /*q byteLen*/ 4,
179 D_MESG,
180 /*D_MESG byteLen*/ 2,
181 pC,
182 nParam,
183 pMsg,
184 msgLen,
185 Q,
186 params->hash_method);
187 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
188
189 /* Calculate checksum Cksm(Q) and append it to Q */
190 Ipp32u cksmQ = cpCksm(Q, *params);
191 // Q || Cksm(Q)
192 cp_to_byte(Q + nParam, /*cksmQ byteLen*/ 2, cksmQ);
193
194 Ipp8u* sk = (temp_buf + CP_PK_I_BYTESIZE + 4 + 2 + 1 + nParam);
195 for (Ipp32u i = 0; i < pParam; i++) {
196 // generate secret key from secret seed
197 // x_q[i] = cp_lms_H(I || u32str(q) || u16str(i) || u8str(0xff) || SEED)
198 retCode = cp_lms_H(I_q,
199 CP_PK_I_BYTESIZE + /*q byteLen*/ 4,
200 i,
201 /*i byteLen*/ 2,
202 &D_PRIV_,
203 /*D_PRIV byteLen*/ 1,
204 secret_seed,
205 nParam,
206 sk,
207 params->hash_method);
208 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
209
210 // a = coef(Q || Cksm(Q), i, w)
211 const Ipp32u a = cpCoef(Q, i, wParam);
212
213 // chaining
214 for (Ipp8u j = 0; j < a; j++) {
215 // sk = cp_lms_H(I || u32str(q) || u16str(i) || u8str(j) || sk)
216 retCode = cp_lms_H(I_q,
217 CP_PK_I_BYTESIZE + /*q byteLen*/ 4,
218 i,
219 /*i byteLen*/ 2,
220 &j,
221 /*j byteLen*/ 1,
222 sk,
223 nParam,
224 sk,
225 params->hash_method);
226 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
227 }
228 CopyBlock(sk, pY + i * (Ipp32u)nParam, nParam);
229 }
230 return retCode;
231 }