| 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/lms.h" |
| 17 |
#include "stateful_sig/common.h" |
| 18 |
|
| 19 |
/* |
| 20 |
* Does the randomized hashing for the tree out of OTS (H function in LMS Spec) |
| 21 |
* I size: 16 + 4 + val2Len + val3Len + msgLen |
| 22 |
* |
| 23 |
* Input parameters: |
| 24 |
* I pointer to I buffer (I from the spec) |
| 25 |
* val1 1st value passing to the function |
| 26 |
* val1Len size of val1 |
| 27 |
* val2 2nd value passing to the function |
| 28 |
* val2Len size of val2 |
| 29 |
* val3 3rd value passing to the function |
| 30 |
* val3Len size of val3 |
| 31 |
* pMsg pointer to message |
| 32 |
* msgLen size of pMsg |
| 33 |
* hash_method Crypto library hash method |
| 34 |
* |
| 35 |
* Output parameters: |
| 36 |
* out resulted n-byte array that contains hash |
| 37 |
*/ |
| 38 |
/* clang-format off */ |
| 39 |
IPP_OWN_DEFN(IppStatus, cp_lms_H_tree, ( |
| 40 |
Ipp8u* I, |
| 41 |
Ipp32u val1, |
| 42 |
Ipp32u val2, const Ipp32s val2Len, |
| 43 |
Ipp8u* val3, const Ipp32s val3Len, |
| 44 |
Ipp8u* pMsg, const Ipp32s msgLen, |
| 45 |
Ipp8u* out, const IppsHashMethod* hash_method)) |
| 46 |
/* clang-format on */ |
| 47 |
{ |
| 48 |
int total_size = CP_PK_I_BYTESIZE; |
| 49 |
|
| 50 |
cp_to_byte(I + total_size, /*val1 byteLen*/ 4, val1); |
| 51 |
total_size += 4; |
| 52 |
|
| 53 |
cp_to_byte(I + total_size, val2Len, val2); |
| 54 |
total_size += val2Len; |
| 55 |
|
| 56 |
CopyBlock(val3, I + total_size, val3Len); |
| 57 |
total_size += val3Len; |
| 58 |
|
| 59 |
if (msgLen > 0) { |
| 60 |
CopyBlock(pMsg, I + total_size, msgLen); |
| 61 |
total_size += msgLen; |
| 62 |
} |
| 63 |
|
| 64 |
return ippsHashMessage_rmf(I, total_size, out, hash_method); |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
* Builds the LMS tree. The function calls from keygen or sign functions |
| 69 |
* The algorithm use auxiliary memory to store nodes of the built tree. |
| 70 |
* It significantly reduces calculations on the sign stage and significantly |
| 71 |
* improves the performance by storing already calculated tree nodes. |
| 72 |
* |
| 73 |
* Input parameters: |
| 74 |
* isKeyGen parameter == 1 if the function is called from keygen function. |
| 75 |
* Otherwise it equals to 0 |
| 76 |
* secret_seed random seed to generate secret key |
| 77 |
* pI pointer to I buffer (I from the spec) |
| 78 |
* idx_leaf index of leaf for which the authentication path is built. |
| 79 |
* In case of calling from keygen function parameter is ignored |
| 80 |
* temp_buf temporary memory (size is (((((CP_PK_I_BYTESIZE + 4 + 2 + n + n) + h + 1) + h * n) + n * p) + n) + (CP_PK_I_BYTESIZE + 4 + 2 + 1 + n + n * p) bytes at least) |
| 81 |
* pAuxiliaryMem auxiliary memory to store nodes of the tree. |
| 82 |
* If isKeyGen == 1, nodes are written to pAuxiliaryMem |
| 83 |
* If isKeyGen == 0, nodes are read from pAuxiliaryMem |
| 84 |
* aux_size size of pAuxiliaryMem (bytes) |
| 85 |
* lmsParams LMS parameters |
| 86 |
* lmotsParams OTS parameters |
| 87 |
* |
| 88 |
* Output parameters: |
| 89 |
* out resulted n-byte array that contains the public key |
| 90 |
*/ |
| 91 |
/* clang-format off */ |
| 92 |
IPP_OWN_DEFN(IppStatus, cp_lms_tree_hash, (Ipp8u isKeyGen, |
| 93 |
Ipp8u* pSecretSeed, |
| 94 |
Ipp8u* pI, |
| 95 |
Ipp8u* out, |
| 96 |
Ipp32u idx_leaf, |
| 97 |
Ipp8u* temp_buf, |
| 98 |
Ipp8u* pAuxiliaryMem, |
| 99 |
Ipp32s aux_size, |
| 100 |
const cpLMSParams* lmsParams, |
| 101 |
const cpLMOTSParams* lmotsParams)) |
| 102 |
/* clang-format on */ |
| 103 |
{ |
| 104 |
IppStatus retCode = ippStsErr; |
| 105 |
const Ipp32u n = lmotsParams->n; |
| 106 |
const Ipp32s n_s = (Ipp32s)n; |
| 107 |
const Ipp32u h = lmsParams->h; |
| 108 |
const Ipp32s h_s = (Ipp32s)h; |
| 109 |
|
| 110 |
Ipp8u* I_r = temp_buf; |
| 111 |
CopyBlock(pI, I_r, CP_PK_I_BYTESIZE); |
| 112 |
|
| 113 |
Ipp8u* heights = I_r + (CP_PK_I_BYTESIZE + 4 + 2 + n_s + n_s); // size: h |
| 114 |
Ipp8u* stack = heights + (h + 1); |
| 115 |
|
| 116 |
Ipp32s stack_size = 0; |
| 117 |
Ipp8u *node, *temp_node; |
| 118 |
// Note: there is no overflow since the maximum value for h is 25 according to the Spec |
| 119 |
for (Ipp32u i = 0; i < (Ipp32u)(1 << h); i++) { |
| 120 |
if ((isKeyGen == 0) && (aux_size > 0) && (idx_leaf != i)) { |
| 121 |
Ipp8u b = 0; |
| 122 |
Ipp32u h_local = 0; |
| 123 |
Ipp32u j_local = i; |
| 124 |
Ipp32u idx_local = idx_leaf; |
| 125 |
while (h_local < h - 1) { |
| 126 |
if ((idx_local ^ 1) == j_local) { |
| 127 |
Ipp32s aux_idx = (Ipp32s)(((1 << (h - h_local)) - 2) + j_local); |
| 128 |
if (aux_idx * n_s < aux_size - n_s) { |
| 129 |
/* The node was pre-calculated on KeyGen step */ |
| 130 |
b = 1; |
| 131 |
} |
| 132 |
break; |
| 133 |
} |
| 134 |
h_local++; |
| 135 |
j_local >>= 1; |
| 136 |
idx_local >>= 1; |
| 137 |
} |
| 138 |
if (b) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
Ipp32u r = (1 << h) + i; // r = 2^h + i |
| 144 |
node = stack + h * n; // size: n |
| 145 |
// 2*2^0 + 2*2^1 + 2*2^2 +... + 2*2^(h-1) = 2 * ((1 << h) - 1) |
| 146 |
Ipp32s aux_idx = (Ipp32s)(((1 << h) - 2) + i); |
| 147 |
|
| 148 |
if ((isKeyGen == 0) && (aux_idx * n_s < aux_size - n_s)) { |
| 149 |
CopyBlock(pAuxiliaryMem + aux_idx * n_s, node, n_s); |
| 150 |
} else { |
| 151 |
temp_node = node + n_s; |
| 152 |
// public key generation |
| 153 |
retCode = cp_lms_OTS_genPK( |
| 154 |
pSecretSeed, |
| 155 |
pI, |
| 156 |
i, |
| 157 |
node, |
| 158 |
temp_node, |
| 159 |
lmotsParams); // temp_node size: CP_PK_I_BYTESIZE + 4 + 2 + 1 + n + n * p |
| 160 |
IPP_BADARG_RET((ippStsNoErr != retCode), retCode) |
| 161 |
// hash public key in leaf node |
| 162 |
retCode = cp_lms_H_tree(I_r, |
| 163 |
r, |
| 164 |
D_LEAF, |
| 165 |
2, |
| 166 |
node, |
| 167 |
n_s, |
| 168 |
NULL, |
| 169 |
0, |
| 170 |
node, |
| 171 |
lmsParams->hash_method); // size: n |
| 172 |
IPP_BADARG_RET((ippStsNoErr != retCode), retCode) |
| 173 |
} |
| 174 |
|
| 175 |
if (isKeyGen == 0 && (idx_leaf ^ 1) == i) { |
| 176 |
CopyBlock(node, out, n_s); |
| 177 |
} |
| 178 |
if ((isKeyGen == 1) && (aux_idx * n_s < aux_size - n_s)) { |
| 179 |
CopyBlock(node, pAuxiliaryMem + aux_idx * n_s, n_s); |
| 180 |
} |
| 181 |
|
| 182 |
// calculate a root of sub-tree |
| 183 |
heights[stack_size] = 0; |
| 184 |
Ipp32u j = i; |
| 185 |
while (stack_size > 0 && heights[stack_size - 1] == heights[stack_size]) { |
| 186 |
stack_size--; // stack.pop |
| 187 |
|
| 188 |
j >>= 1; // j = j / 2 |
| 189 |
r >>= 1; // r = r / 2 |
| 190 |
|
| 191 |
aux_idx = (Ipp32s)(((1 << (h - heights[stack_size] - 1)) - 2) + j); |
| 192 |
if ((isKeyGen == 0) && (aux_idx >= 0) && (aux_idx * n_s <= aux_size - n_s)) { |
| 193 |
CopyBlock(pAuxiliaryMem + aux_idx * n_s, node, n_s); |
| 194 |
} else { |
| 195 |
retCode = cp_lms_H_tree(I_r, |
| 196 |
r, |
| 197 |
D_INTR, |
| 198 |
2, |
| 199 |
stack + (stack_size * n_s), |
| 200 |
n_s, // left |
| 201 |
node, |
| 202 |
n_s, // right |
| 203 |
node, // out |
| 204 |
lmsParams->hash_method); |
| 205 |
IPP_BADARG_RET((ippStsNoErr != retCode), retCode) |
| 206 |
} |
| 207 |
if (isKeyGen == 1 && (aux_idx >= 0) && (aux_idx * n_s <= aux_size - n_s)) { |
| 208 |
CopyBlock(node, pAuxiliaryMem + aux_idx * n_s, n_s); |
| 209 |
} |
| 210 |
|
| 211 |
heights[stack_size]++; |
| 212 |
|
| 213 |
if ((isKeyGen == 0) && (((idx_leaf >> heights[stack_size]) ^ 1) == j)) { |
| 214 |
CopyBlock(node, out + (heights[stack_size]) * n_s, n_s); |
| 215 |
} |
| 216 |
} |
| 217 |
CopyBlock(node, stack + (stack_size * n_s), n_s); |
| 218 |
stack_size++; // stack.push |
| 219 |
} |
| 220 |
|
| 221 |
// fill the output |
| 222 |
if (isKeyGen == 1) { |
| 223 |
CopyBlock(stack, out, n_s); |
| 224 |
} else if (aux_size > 0) { |
| 225 |
for (Ipp32s h_local = 0; h_local < h_s; h_local++) { |
| 226 |
Ipp32u j_local = (idx_leaf >> h_local) ^ 1; |
| 227 |
Ipp32s aux_idx = (Ipp32s)(((1 << (h_s - h_local)) - 2) + j_local); |
| 228 |
if ((aux_idx >= 0) && (aux_idx * n_s < aux_size - n_s)) { |
| 229 |
CopyBlock(pAuxiliaryMem + (aux_idx * n_s), out + (h_local * n_s), n_s); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
return retCode; |
| 235 |
} |