Debian Codegraph @

sources/ippcp/stateful_sig/lms/lms_verify.c

raw
1 /*************************************************************************
2 * Copyright (C) 2024 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/lms.h"
17 #include "stateful_sig/common.h"
18
19 /*F*
20 // Name: ippsLMSVerify
21 //
22 // Purpose: LMS signature verification.
23 //
24 // Returns: Reason:
25 // ippStsNullPtrErr pMsg == NULL
26 // pSign == NULL
27 // pIsSignValid == NULL
28 // pKey == NULL
29 // pBuffer == NULL
30 // ippStsBadArgErr wrong LMS or LMOTS parameters
31 // inside pSign and pKey
32 // OR q is incorrect
33 // ippStsContextMatchErr pSign or pKey contexts are invalid
34 // ippStsLengthErr msgLen < 1
35 // ippStsNoErr no errors
36 //
37 // Parameters:
38 // pMsg pointer to the message data buffer
39 // msgLen message buffer length, bytes
40 // pSign pointer to the LMS signature state
41 // pIsSignValid 1 if signature is valid, 0 - vice versa
42 // pKey pointer to the LMS public key state
43 // pBuffer pointer to the temporary memory
44 //
45 *F*/
46
47 /* clang-format off */
48 IPPFUN(IppStatus, ippsLMSVerify, (const Ipp8u* pMsg,
49 const Ipp32s msgLen,
50 const IppsLMSSignatureState* pSign,
51 int* pIsSignValid,
52 const IppsLMSPublicKeyState* pKey,
53 Ipp8u* pBuffer))
54 /* clang-format on */
55 {
56 IppStatus ippcpSts = ippStsErr;
57
58 /* Check if any of input pointers are NULL */
59 IPP_BAD_PTR4_RET(pMsg, pSign, pIsSignValid, pKey)
60 /* Check if temporary buffer is NULL */
61 IPP_BAD_PTR1_RET(pBuffer)
62 /* Check msg length */
63 IPP_BADARG_RET(msgLen < 1, ippStsLengthErr)
64 IPP_BADARG_RET(!CP_LMS_VALID_SIGN_CTX_ID(pSign), ippStsContextMatchErr);
65 IPP_BADARG_RET(!CP_LMS_VALID_PUB_KEY_CTX_ID(pKey), ippStsContextMatchErr);
66 *pIsSignValid = 0;
67
68 /* Parse public key(Pk) */
69 /* --------------------------------------------- */
70 IppsLMSAlgo lmsTypePk = pKey->lmsOIDAlgo;
71 IppsLMOTSAlgo lmotsTypePk = pKey->lmotsOIDAlgo;
72
73 // Set LMOTS and LMS parameters
74 cpLMOTSParams lmotsParams;
75 cpLMSParams lmsParams;
76 ippcpSts = setLMOTSParams(lmotsTypePk, &lmotsParams);
77 IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts)
78 ippcpSts = setLMSParams(lmsTypePk, &lmsParams);
79 IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts)
80 Ipp32u nParam = lmotsParams.n;
81 Ipp32u wParam = lmotsParams.w;
82 Ipp32u pParam = lmotsParams.p;
83 Ipp32u mParam = lmsParams.m;
84 Ipp32u hParam = lmsParams.h;
85
86 /* Parse signature */
87 /* ---------------------------------------------------- */
88 Ipp32u q = pSign->_q;
89 _cpLMOTSSignatureState lmotsSig = pSign->_lmotsSig;
90 IppsLMOTSAlgo lmotsTypeSig = lmotsSig._lmotsOIDAlgo;
91 IppsLMSAlgo lmsTypeSig = pSign->_lmsOIDAlgo;
92 Ipp8u* pAuthPath = pSign->_pAuthPath;
93
94 // Check the validity of the parsed signature parameters
95 Ipp32u qLimit = 1 << hParam;
96 if ((lmsTypePk != lmsTypeSig) || (lmotsTypePk != lmotsTypeSig) || (q >= qLimit)) {
97 return ippStsBadArgErr;
98 }
99
100 /* Compute LMS pub key candidate (Algorithms 6a and 4b) */
101 /* ---------------------------------------------------- */
102 Ipp8u* tmpQBuf = pBuffer;
103 Ipp32u total_size = 0;
104 // Buffer's invariant for alg correctness - first 16 bytes is always pubKey->I
105 CopyBlock(pKey->I, tmpQBuf, CP_PK_I_BYTESIZE);
106 total_size += CP_PK_I_BYTESIZE;
107 cp_to_byte(tmpQBuf + total_size, /*q byteLen*/ 4, q);
108 total_size += /*q byteLen*/ 4;
109 cp_to_byte(tmpQBuf + total_size, /*D_MESG byteLen*/ 2, D_MESG);
110 total_size += /*D_MESG byteLen*/ 2;
111 CopyBlock(lmotsSig.pC, tmpQBuf + total_size, (cpSize)nParam);
112 total_size += nParam;
113 CopyBlock(pMsg, tmpQBuf + total_size, msgLen);
114 total_size += (Ipp32u)msgLen;
115
116 // Q = H(I || u32str(q) || u16str(D_MESG) || C || message)
117 Ipp8u Q_CksmQ[CP_LMS_MAX_HASH_BYTESIZE + CP_CKSM_BYTESIZE];
118 ippcpSts = ippsHashMessage_rmf(tmpQBuf, (int)total_size, Q_CksmQ, lmsParams.hash_method);
119 IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts)
120
121 /* Calculate checksum Cksm(Q) and append it to Q */
122 Ipp32u cksmQ = cpCksm(Q_CksmQ, lmotsParams);
123 cp_to_byte(Q_CksmQ + nParam, /*cksmQ byteLen*/ 2, cksmQ);
124
125 Ipp8u* pZ = pBuffer + (total_size - (Ipp32u)msgLen + 1); // size = (pParam + 1) * nParam //z[0];
126
127 Ipp8u tmp[CP_LMS_MAX_HASH_BYTESIZE];
128 for (Ipp32u i = 0; i < pParam; i++) {
129 // a = coef(Q || Cksm(Q), i, w)
130 Ipp32u a = cpCoef(Q_CksmQ, i, wParam);
131 //tmp = y[i]
132 CopyBlock(lmotsSig.pY + i * nParam, tmp, (cpSize)nParam);
133
134 // I || u32str(q)
135 Ipp8u* tmpBuff = pBuffer;
136 // I || u32str(q) || u16str(i)
137 cp_to_byte(tmpBuff + CP_PK_I_BYTESIZE + /*q byteLen*/ 4, /*i byteLen*/ 2, i);
138 for (Ipp32u j = a; j < (Ipp32u)((1 << wParam) - 1); j++) {
139 // I || u32str(q) || u16str(i) || u8str(j)
140 cp_to_byte(tmpBuff + CP_PK_I_BYTESIZE + /*q byteLen*/ 4 + /*i byteLen*/ 2,
141 /*j byteLen*/ 1,
142 j);
143 // I || u32str(q) || u16str(i) || u8str(j) || tmp
144 CopyBlock(tmp,
145 tmpBuff + CP_PK_I_BYTESIZE + /*q byteLen*/ 4 + /*i byteLen*/ 2 +
146 /*j byteLen*/ 1,
147 (cpSize)nParam);
148 // tmp = H(I || u32str(q) || u16str(i) || u8str(j) || tmp)
149 ippcpSts = ippsHashMessage_rmf(tmpBuff,
150 (int)(CP_PK_I_BYTESIZE + /*q byteLen*/ 4 +
151 /*i byteLen*/ 2 + /*j byteLen*/ 1 + nParam),
152 tmp,
153 lmsParams.hash_method);
154 IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts)
155 }
156 CopyBlock(tmp, pZ + (i + 1) * nParam, (cpSize)nParam);
157 }
158 // I u32str(q) u16str(D_PBLC)
159 Ipp32s zStartOffset = (Ipp32s)(nParam - (CP_PK_I_BYTESIZE + 4 + 2));
160 // I u16str(D_PBLC)
161 CopyBlock(tmpQBuf, pZ + zStartOffset, CP_PK_I_BYTESIZE + 4);
162 // Conduct operation u16str(D_PBLC)
163 cp_to_byte(pZ + nParam - /*D_PBLC byteLen*/ 2, /*D_PBLC byteLen*/ 2, D_PBLC);
164 // tmp = Kc = H(I || u32str(q) || u16str(D_PBLC) || z[0] || z[1] || ... || z[p-1])
165 Ipp8u* Kc = tmp;
166 ippcpSts = ippsHashMessage_rmf(
167 pZ + zStartOffset,
168 (int)(pParam * nParam + CP_PK_I_BYTESIZE + /*q byteLen*/ 4 + /*D_PBLC byteLen*/ 2),
169 Kc,
170 lmsParams.hash_method);
171 IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts)
172
173 /* Compute the candidate LMS root value Tc */
174 /* --------------------------------------------- */
175 Ipp32u node_num = (1 << hParam) + q;
176 Ipp8u* tmpBuffKc = pBuffer;
177 // I || u32str(node_num)
178 cp_to_byte(tmpBuffKc + CP_PK_I_BYTESIZE, /*node_num byteLen*/ 4, node_num);
179 // I || u32str(node_num) || u16str(D_LEAF)
180 cp_to_byte(tmpBuffKc + CP_PK_I_BYTESIZE + /*node_num byteLen*/ 4, /*D_LEAF byteLen*/ 2, D_LEAF);
181 // I || u32str(node_num) || u16str(D_LEAF) || Kc
182 CopyBlock(Kc,
183 tmpBuffKc + CP_PK_I_BYTESIZE + /*node_num byteLen*/ 4 + /*D_LEAF byteLen*/ 2,
184 (cpSize)mParam);
185
186 ippcpSts = ippsHashMessage_rmf(
187 tmpBuffKc,
188 (int)(CP_PK_I_BYTESIZE + /*node_num byteLen*/ 4 + /*D_LEAF byteLen*/ 2 + mParam),
189 tmp,
190 lmsParams.hash_method);
191 IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts)
192
193 Ipp32u i = 0;
194 Ipp8u* locTmp = pBuffer;
195 // I || u32str(node_num/2) || u16str(D_INTR)
196 cp_to_byte(locTmp + CP_PK_I_BYTESIZE + /*node_num byteLen*/ 4, /*D_INTR byteLen*/ 2, D_INTR);
197 while (node_num > 1) {
198 // I || u32str(node_num/2)
199 cp_to_byte(locTmp + CP_PK_I_BYTESIZE, /*node_num byteLen*/ 4, node_num / 2);
200
201 if ((node_num & 1) == 1) {
202 // I || u32str(node_num/2) || u16str(D_INTR) || path[i]
203 CopyBlock(pAuthPath + i * mParam,
204 locTmp + CP_PK_I_BYTESIZE + /*node_num byteLen*/ 4 + /*D_INTR byteLen*/ 2,
205 (cpSize)mParam);
206 // I || u32str(node_num/2) || u16str(D_INTR) || path[i] || tmp
207 CopyBlock(tmp,
208 locTmp + CP_PK_I_BYTESIZE + /*node_num byteLen*/ 4 + /*D_INTR byteLen*/ 2 +
209 mParam,
210 (cpSize)mParam);
211 } else {
212 // I || u32str(node_num/2) || u16str(D_INTR) || tmp
213 CopyBlock(tmp,
214 locTmp + CP_PK_I_BYTESIZE + /*node_num byteLen*/ 4 + /*D_INTR byteLen*/ 2,
215 (cpSize)mParam);
216 // I || u32str(node_num/2) || u16str(D_INTR) || tmp || path[i]
217 CopyBlock(pAuthPath + i * mParam,
218 locTmp + CP_PK_I_BYTESIZE + /*node_num byteLen*/ 4 + /*D_INTR byteLen*/ 2 +
219 mParam,
220 (cpSize)mParam);
221 }
222
223 ippcpSts = ippsHashMessage_rmf(
224 locTmp,
225 (int)(CP_PK_I_BYTESIZE + /*node_num byteLen*/ 4 + /*D_INTR byteLen*/ 2 + 2 * mParam),
226 tmp,
227 lmotsParams.hash_method);
228 IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts)
229
230 node_num = node_num >> 1;
231 i++;
232 }
233
234 /* Verify with given public key */
235 /* --------------------------------------------- */
236 BNU_CHUNK_T is_equal = cpIsEquBlock_ct(pKey->T1, tmp, (int)mParam);
237 if (is_equal) {
238 *pIsSignValid = 1;
239 }
240
241 return ippcpSts;
242 }