Debian Codegraph @

sources/ippcp/stateful_sig/xmss/xmss_verify.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 "stateful_sig/xmss_internal/xmss.h"
19 #include "stateful_sig/common.h"
20
21 /*F*
22 // Name: ippsXMSSVerify
23 //
24 // Purpose: XMSS signature verification.
25 //
26 // Returns: Reason:
27 // ippStsNullPtrErr pMsg == NULL
28 // pSign == NULL
29 // pIsSignValid == NULL
30 // pKey == NULL
31 // pBuffer == NULL
32 // ippStsLengthErr msgLen < 1
33 // ippStsLengthErr msgLen > IPP_MAX_32S - (numTempBufs + len) * n
34 // ippStsNoErr no errors
35 //
36 // Parameters:
37 // pMsg pointer to the message data buffer
38 // msgLen message buffer length
39 // pSign pointer to the XMSS signature
40 // pIsSignValid 1 if signature is valid, 0 - vice versa
41 // pKey pointer to the XMSS public key
42 // pBuffer pointer to the temporary memory
43 //
44 *F*/
45 /* clang-format off */
46 IPPFUN(IppStatus, ippsXMSSVerify, (const Ipp8u* pMsg,
47 const Ipp32s msgLen,
48 const IppsXMSSSignatureState* pSign,
49 int* pIsSignValid,
50 const IppsXMSSPublicKeyState* pKey,
51 Ipp8u* pBuffer))
52 /* clang-format on */
53 {
54 IppStatus retCode = ippStsNoErr;
55
56 /* Check if any of input pointers are NULL */
57 IPP_BAD_PTR4_RET(pMsg, pSign, pIsSignValid, pKey)
58 /* Check if temporary buffer is NULL */
59 IPP_BAD_PTR1_RET(pBuffer)
60 /* Check msg length */
61 IPP_BADARG_RET(msgLen < 1, ippStsLengthErr)
62 *pIsSignValid = 0;
63
64 /* Parameters of the current XMSS */
65 Ipp32s h = 0;
66 cpWOTSParams params;
67 retCode = cp_xmss_set_params(pKey->OIDAlgo, &h, &params);
68 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
69 Ipp32s len = params.len;
70 Ipp32s n = params.n;
71 const Ipp32s numTempBufs = 10;
72 IPP_BADARG_RET(msgLen > (Ipp32s)(IPP_MAX_32S) - (numTempBufs + len) * n, ippStsLengthErr);
73
74 // description of internals for OTS Hash / L-tree / Hash tree address is following
75 // +-----------------------------------------------------+
76 // | layer address (32 bits)|
77 // +-----------------------------------------------------+
78 // | tree address (64 bits)|
79 // +-----------------------------------------------------+
80 // | type = 0 / 1 / 2 (32 bits)|
81 // +-----------------------------------------------------+
82 // | OTS address / L-tree address / Padding = 0 (32 bits)|
83 // +-----------------------------------------------------+
84 // | chain address / tree height (32 bits)|
85 // +-----------------------------------------------------+
86 // | hash address / tree index (32 bits)|
87 // +-----------------------------------------------------+
88 // | keyAndMask (32 bits)|
89 // +-----------------------------------------------------+
90 Ipp8u adrs[ADRS_SIZE] = {
91 0, 0, 0, 0, // 0; 4
92 0, 0, 0, 0, 0, 0, 0, 0, // 4; 12
93 0, 0, 0, 0, // 12; 16
94 0, 0, 0, 0, // 16; 20
95 0, 0, 0, 0, // 20; 24
96 0, 0, 0, 0, // 24; 28
97 0, 0, 0, 0 // 28; 32
98 };
99
100 Ipp32u idx = pSign->idx;
101
102 Ipp8u* pMsg_ = pBuffer;
103 Ipp8u* temp_key = pBuffer + n;
104 Ipp8u* temp_buf = pBuffer + n + (len * n);
105
106 // byte[n] M_ = H_msg(r || getRoot(PK) || (toByte(idx_sig, n)), M);
107 cp_to_byte(temp_buf, n, /*h_msg padding id*/ 2);
108 CopyBlock(pSign->r, temp_buf + n, n);
109 CopyBlock(pKey->pRoot, temp_buf + 2 * n, n);
110 cp_to_byte(temp_buf + 3 * n, n, idx);
111 CopyBlock(pMsg, temp_buf + 4 * n, msgLen);
112
113 retCode = ippsHashMessage_rmf(temp_buf, 4 * n + msgLen, pMsg_, params.hash_method);
114
115 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
116 cp_xmss_set_ots_address(adrs, idx);
117
118 // 1. get ots public key working with msg and ots signature
119 retCode = cp_xmss_WOTS_pkFromSig(pMsg_,
120 pSign->pOTSSign,
121 pKey->pSeed,
122 adrs,
123 temp_key,
124 temp_buf,
125 &params);
126 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
127
128 cp_xmss_set_tree_type(adrs, /*L-tree*/ 1);
129 retCode = cp_xmss_ltree(temp_key, pKey->pSeed, adrs, temp_buf, &params);
130 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
131
132 // 2. do hashing using authorization path
133 cp_xmss_set_tree_type(adrs, /*hash tree*/ 2);
134
135 cp_xmss_set_ots_address(adrs, 0);
136
137 for (Ipp32s i = 0; i < h; ++i) {
138 cp_xmss_set_tree_height(adrs, (Ipp8u)i);
139 // if we are the left child
140 if (((pSign->idx / (1 << i)) & 1) == 0) {
141 // leaf || auth_path
142 CopyBlock(temp_key, temp_buf, n);
143 CopyBlock(pSign->pAuthPath + (i * n), temp_buf + n, n);
144 } else {
145 // auth_path || leaf
146 CopyBlock(pSign->pAuthPath + (i * n), temp_buf, n);
147 CopyBlock(temp_key, temp_buf + n, n);
148 }
149
150 idx /= 2;
151 cp_xmss_set_tree_index_32(adrs, idx);
152
153 retCode = cp_xmss_rand_hash(temp_buf,
154 temp_buf + n,
155 pKey->pSeed,
156 adrs,
157 temp_key,
158 temp_buf + 2 * n,
159 &params);
160 IPP_BADARG_RET((ippStsNoErr != retCode), retCode)
161 }
162
163 // 3. verify with public key
164 BNU_CHUNK_T is_equal = cpIsEquBlock_ct(pKey->pRoot, temp_key, n);
165 if (is_equal) {
166 *pIsSignValid = 1;
167 }
168 return retCode;
169 }