| 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 |
|
| 17 |
#include "owndefs.h" |
| 18 |
#include "stateful_sig/lms_internal/lms.h" |
| 19 |
|
| 20 |
/*F* |
| 21 |
// Name: ippsLMSBufferGetSize |
| 22 |
// |
| 23 |
// Purpose: Get the LMS temporary buffer size (bytes). |
| 24 |
// |
| 25 |
// Returns: Reason: |
| 26 |
// ippStsNullPtrErr pSize == NULL |
| 27 |
// ippStsBadArgErr lmsType.lmotsOIDAlgo > LMOTS_SHA256_N24_W8 |
| 28 |
// lmsType.lmotsOIDAlgo < LMOTS_SHA256_N32_W1 |
| 29 |
// lmsType.lmsOIDAlgo > LMS_SHA256_M24_H25 |
| 30 |
// lmsType.lmsOIDAlgo < LMS_SHA256_M32_H5 |
| 31 |
// ippStsLengthErr maxMessageLength < 1 |
| 32 |
// maxMessageLength > (Ipp32s)(IPP_MAX_32S) - |
| 33 |
// - (byteSizeI + 4(q byteSize) + 2(D_MESG byteSize) + n(C byteSize)) |
| 34 |
// ippStsNoErr no errors |
| 35 |
// |
| 36 |
// Parameters: |
| 37 |
// pSize pointer to the work buffer's byte size |
| 38 |
// maxMessageLength maximum length of the processing message |
| 39 |
// lmsType structure with LMS parameters lmotsOIDAlgo and lmsOIDAlgo |
| 40 |
// |
| 41 |
*F*/ |
| 42 |
|
| 43 |
/* clang-format off */ |
| 44 |
IPPFUN(IppStatus, ippsLMSBufferGetSize, (Ipp32s* pSize, |
| 45 |
Ipp32s maxMessageLength, |
| 46 |
const IppsLMSAlgoType lmsType)) |
| 47 |
/* clang-format on */ |
| 48 |
{ |
| 49 |
IppStatus ippcpSts = ippStsErr; |
| 50 |
|
| 51 |
/* Input parameters check */ |
| 52 |
IPP_BAD_PTR1_RET(pSize); |
| 53 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo >= LMS_MAX, ippStsBadArgErr); |
| 54 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo <= LMS_MIN, ippStsBadArgErr); |
| 55 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo >= LMOTS_MAX, ippStsBadArgErr); |
| 56 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo <= LMOTS_MIN, ippStsBadArgErr); |
| 57 |
|
| 58 |
/* Set LMOTS and LMS parameters */ |
| 59 |
cpLMOTSParams lmotsParams; |
| 60 |
ippcpSts = setLMOTSParams(lmsType.lmotsOIDAlgo, &lmotsParams); |
| 61 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 62 |
cpLMSParams lmsParams; |
| 63 |
ippcpSts = setLMSParams(lmsType.lmsOIDAlgo, &lmsParams); |
| 64 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 65 |
|
| 66 |
/* Check message length */ |
| 67 |
IPP_BADARG_RET(maxMessageLength < 1, ippStsLengthErr); |
| 68 |
// this restriction is needed to avoid overflow of Ipp32s |
| 69 |
// maxMessageLength must be less than IPP_MAX_32S - (CP_PK_I_BYTESIZE + q + D_MESG + C ) |
| 70 |
IPP_BADARG_RET(maxMessageLength > |
| 71 |
(Ipp32s)((IPP_MAX_32S) - (CP_PK_I_BYTESIZE + 4 + 2 + lmotsParams.n)), |
| 72 |
ippStsLengthErr); |
| 73 |
/* clang-format off */ |
| 74 |
/* Calculate the maximum Set LMOTS and LMS parameters */ |
| 75 |
// pubKey->I || q || D_MESG || C || pMsg |
| 76 |
Ipp32u lenBufQ = CP_PK_I_BYTESIZE + 4 + 2 + lmotsParams.n + (Ipp32u)maxMessageLength; |
| 77 |
// pubKey->I || q || i || j || Y[i] |
| 78 |
Ipp32u lenBufTmp = CP_PK_I_BYTESIZE + 4 + 2 + 1 + lmotsParams.n; |
| 79 |
// pubKey->I || node_num || D_LEAF || Kc |
| 80 |
Ipp32u lenBufTc = CP_PK_I_BYTESIZE + 4 + 2 + lmotsParams.n; |
| 81 |
// pubKey->I || node_num/2 || D_INTR || path[i] || tmp |
| 82 |
Ipp32u lenBufIntr = CP_PK_I_BYTESIZE + 4 + 2 + lmotsParams.n + lmotsParams.n; |
| 83 |
Ipp32u lenBufZ = (lmotsParams.p + 1) * lmotsParams.n; |
| 84 |
// lenBufQZ = lenBufQ + (lenBufZ - /* shift to reuse Q */ (Ipp32u)maxMessageLength + 1); |
| 85 |
Ipp32u lenBufQZ = CP_PK_I_BYTESIZE + 4 + 2 + lmotsParams.n + lenBufZ + 1; |
| 86 |
/* clang-format on */ |
| 87 |
|
| 88 |
*pSize = (Ipp32s)IPP_MAX(IPP_MAX(IPP_MAX(IPP_MAX(lenBufQZ, lenBufQ), lenBufTmp), lenBufTc), |
| 89 |
lenBufIntr); |
| 90 |
|
| 91 |
return ippcpSts; |
| 92 |
} |
| 93 |
|
| 94 |
/*F* |
| 95 |
// Name: ippsLMSVerifyBufferGetSize |
| 96 |
// |
| 97 |
// Purpose: Get the size of temporary buffer required for LMS verification (bytes). |
| 98 |
// |
| 99 |
// Returns: Reason: |
| 100 |
// ippStsNullPtrErr pSize == NULL |
| 101 |
// ippStsBadArgErr lmsType.lmotsOIDAlgo > LMOTS_SHA256_N24_W8 |
| 102 |
// lmsType.lmotsOIDAlgo < LMOTS_SHA256_N32_W1 |
| 103 |
// lmsType.lmsOIDAlgo > LMS_SHA256_M24_H25 |
| 104 |
// lmsType.lmsOIDAlgo < LMS_SHA256_M32_H5 |
| 105 |
// ippStsLengthErr maxMessageLength < 1 |
| 106 |
// maxMessageLength > (Ipp32s)(IPP_MAX_32S) - |
| 107 |
// - (byteSizeI + 4(q byteSize) + 2(D_MESG byteSize) + n(C byteSize)) |
| 108 |
// ippStsNoErr no errors |
| 109 |
// |
| 110 |
// Parameters: |
| 111 |
// pSize pointer to the work buffer's byte size |
| 112 |
// maxMessageLength maximum length of the processing message |
| 113 |
// lmsType structure with LMS parameters lmotsOIDAlgo and lmsOIDAlgo |
| 114 |
// |
| 115 |
*F*/ |
| 116 |
/* clang-format off */ |
| 117 |
IPPFUN(IppStatus, ippsLMSVerifyBufferGetSize, (Ipp32s* pSize, |
| 118 |
Ipp32s maxMessageLength, |
| 119 |
const IppsLMSAlgoType lmsType)) |
| 120 |
/* clang-format on */ |
| 121 |
{ |
| 122 |
return ippsLMSBufferGetSize(pSize, maxMessageLength, lmsType); |
| 123 |
} |
| 124 |
|
| 125 |
/*F* |
| 126 |
// Name: ippsLMSSignatureStateGetSize |
| 127 |
// |
| 128 |
// Purpose: Get the LMS signature state size (bytes). |
| 129 |
// |
| 130 |
// Returns: Reason: |
| 131 |
// ippStsNullPtrErr pSize == NULL |
| 132 |
// ippStsBadArgErr lmsType.lmotsOIDAlgo > LMOTS_SHA256_N24_W8 |
| 133 |
// lmsType.lmotsOIDAlgo < LMOTS_SHA256_N32_W1 |
| 134 |
// lmsType.lmsOIDAlgo > LMS_SHA256_M24_H25 |
| 135 |
// lmsType.lmsOIDAlgo < LMS_SHA256_M32_H5 |
| 136 |
// ippStsNoErr no errors |
| 137 |
// |
| 138 |
// Parameters: |
| 139 |
// pSize pointer to the size |
| 140 |
// lmsType structure with LMS parameters lmotsOIDAlgo and lmsOIDAlgo |
| 141 |
// |
| 142 |
*F*/ |
| 143 |
|
| 144 |
IPPFUN(IppStatus, ippsLMSSignatureStateGetSize, (Ipp32s * pSize, const IppsLMSAlgoType lmsType)) |
| 145 |
{ |
| 146 |
IppStatus ippcpSts = ippStsErr; |
| 147 |
|
| 148 |
IPP_BAD_PTR1_RET(pSize); |
| 149 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo >= LMS_MAX, ippStsBadArgErr); |
| 150 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo <= LMS_MIN, ippStsBadArgErr); |
| 151 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo >= LMOTS_MAX, ippStsBadArgErr); |
| 152 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo <= LMOTS_MIN, ippStsBadArgErr); |
| 153 |
|
| 154 |
/* Set LMOTS and LMS parameters */ |
| 155 |
cpLMOTSParams lmotsParams; |
| 156 |
ippcpSts = setLMOTSParams(lmsType.lmotsOIDAlgo, &lmotsParams); |
| 157 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 158 |
cpLMSParams lmsParams; |
| 159 |
ippcpSts = setLMSParams(lmsType.lmsOIDAlgo, &lmsParams); |
| 160 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 161 |
|
| 162 |
Ipp32s lmots_sign_size = (Ipp32s)(lmotsParams.n // C |
| 163 |
+ lmotsParams.n * lmotsParams.p) // Y |
| 164 |
+ CP_LMS_ALIGNMENT; |
| 165 |
*pSize = (Ipp32s)(sizeof(IppsLMSSignatureState) + lmsParams.m * lmsParams.h) // path |
| 166 |
+ lmots_sign_size; // lmots_sig |
| 167 |
|
| 168 |
return ippcpSts; |
| 169 |
} |
| 170 |
|
| 171 |
/*F* |
| 172 |
// Name: ippsLMSPublicKeyStateGetSize |
| 173 |
// |
| 174 |
// Purpose: Provides the LMS public key state size (bytes). |
| 175 |
// |
| 176 |
// Returns: Reason: |
| 177 |
// ippStsNullPtrErr pSize == NULL |
| 178 |
// ippStsBadArgErr lmsType.lmotsOIDAlgo > LMOTS_SHA256_N24_W8 |
| 179 |
// lmsType.lmotsOIDAlgo < LMOTS_SHA256_N32_W1 |
| 180 |
// lmsType.lmsOIDAlgo > LMS_SHA256_M24_H25 |
| 181 |
// lmsType.lmsOIDAlgo < LMS_SHA256_M32_H5 |
| 182 |
// ippStsNoErr no errors |
| 183 |
// |
| 184 |
// Parameters: |
| 185 |
// pSize pointer to the size |
| 186 |
// lmsType structure with LMS parameters lmotsOIDAlgo and lmsOIDAlgo |
| 187 |
// |
| 188 |
*F*/ |
| 189 |
IPPFUN(IppStatus, ippsLMSPublicKeyStateGetSize, (Ipp32s * pSize, const IppsLMSAlgoType lmsType)) |
| 190 |
{ |
| 191 |
IppStatus ippcpSts = ippStsErr; |
| 192 |
|
| 193 |
IPP_BAD_PTR1_RET(pSize); |
| 194 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo >= LMS_MAX, ippStsBadArgErr); |
| 195 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo <= LMS_MIN, ippStsBadArgErr); |
| 196 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo >= LMOTS_MAX, ippStsBadArgErr); |
| 197 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo <= LMOTS_MIN, ippStsBadArgErr); |
| 198 |
|
| 199 |
/* Set LMS parameters */ |
| 200 |
cpLMSParams lmsParams; |
| 201 |
ippcpSts = setLMSParams(lmsType.lmsOIDAlgo, &lmsParams); |
| 202 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 203 |
|
| 204 |
*pSize = (Ipp32s)sizeof(IppsLMSPublicKeyState) + |
| 205 |
/* T1 */ (Ipp32s)lmsParams.m; |
| 206 |
|
| 207 |
return ippcpSts; |
| 208 |
} |
| 209 |
|
| 210 |
/*F* |
| 211 |
// Name: ippsLMSSetPublicKeyState |
| 212 |
// |
| 213 |
// Purpose: Set LMS public key. |
| 214 |
// |
| 215 |
// Returns: Reason: |
| 216 |
// ippStsNullPtrErr pI == NULL |
| 217 |
// pK == NULL |
| 218 |
// pState == NULL |
| 219 |
// ippStsBadArgErr lmsType.lmotsOIDAlgo > LMOTS_SHA256_N24_W8 |
| 220 |
// lmsType.lmotsOIDAlgo < LMOTS_SHA256_N32_W1 |
| 221 |
// lmsType.lmsOIDAlgo > LMS_SHA256_M24_H25 |
| 222 |
// lmsType.lmsOIDAlgo < LMS_SHA256_M32_H5 |
| 223 |
// ippStsNoErr no errors |
| 224 |
// |
| 225 |
// Parameters: |
| 226 |
// lmsType structure with LMS parameters lmotsOIDAlgo and lmsOIDAlgo |
| 227 |
// pI pointer to the LMS private key identifier |
| 228 |
// pK pointer to the LMS public key |
| 229 |
// pState pointer to the LMS public key state |
| 230 |
// |
| 231 |
*F*/ |
| 232 |
/* clang-format off */ |
| 233 |
IPPFUN(IppStatus, ippsLMSSetPublicKeyState, (const IppsLMSAlgoType lmsType, |
| 234 |
const Ipp8u* pI, |
| 235 |
const Ipp8u* pK, |
| 236 |
IppsLMSPublicKeyState* pState)) |
| 237 |
/* clang-format on */ |
| 238 |
{ |
| 239 |
IppStatus ippcpSts = ippStsErr; |
| 240 |
|
| 241 |
IPP_BAD_PTR3_RET(pI, pK, pState); |
| 242 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo >= LMS_MAX, ippStsBadArgErr); |
| 243 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo <= LMS_MIN, ippStsBadArgErr); |
| 244 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo >= LMOTS_MAX, ippStsBadArgErr); |
| 245 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo <= LMOTS_MIN, ippStsBadArgErr); |
| 246 |
|
| 247 |
/* Set context id to prevent its copying */ |
| 248 |
CP_LMS_SET_PUB_KEY_CTX_ID(pState); |
| 249 |
|
| 250 |
/* Set LMS parameters */ |
| 251 |
cpLMSParams lmsParams; |
| 252 |
ippcpSts = setLMSParams(lmsType.lmsOIDAlgo, &lmsParams); |
| 253 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 254 |
|
| 255 |
/* Fill in the structure */ |
| 256 |
pState->lmsOIDAlgo = lmsType.lmsOIDAlgo; |
| 257 |
pState->lmotsOIDAlgo = lmsType.lmotsOIDAlgo; |
| 258 |
CopyBlock(pI, pState->I, CP_PK_I_BYTESIZE); |
| 259 |
// Set pointer to T1 right to the end of the context |
| 260 |
pState->T1 = (Ipp8u*)pState + sizeof(IppsLMSPublicKeyState); |
| 261 |
CopyBlock(pK, pState->T1, (cpSize)lmsParams.m); |
| 262 |
|
| 263 |
return ippcpSts; |
| 264 |
} |
| 265 |
|
| 266 |
/*F* |
| 267 |
// Name: ippsLMSSetSignatureState |
| 268 |
// |
| 269 |
// Purpose: Set LMS signature. |
| 270 |
// |
| 271 |
// Returns: Reason: |
| 272 |
// ippStsNullPtrErr pC == NULL |
| 273 |
// pY == NULL |
| 274 |
// pAuthPath == NULL |
| 275 |
// pState == NULL |
| 276 |
// ippStsBadArgErr lmsType.lmotsOIDAlgo > LMOTS_SHA256_N24_W8 |
| 277 |
// lmsType.lmotsOIDAlgo < LMOTS_SHA256_N32_W1 |
| 278 |
// lmsType.lmsOIDAlgo > LMS_SHA256_M24_H25 |
| 279 |
// lmsType.lmsOIDAlgo < LMS_SHA256_M32_H5 |
| 280 |
// q is incorrect |
| 281 |
// ippStsNoErr no errors |
| 282 |
// |
| 283 |
// Parameters: |
| 284 |
// lmsType structure with LMS parameters lmotsOIDAlgo and lmsOIDAlgo |
| 285 |
// q index of LMS leaf |
| 286 |
// pC pointer to the C LM-OTS value |
| 287 |
// pY pointer to the y LM-OTS value |
| 288 |
// pAuthPath pointer to the LMS authorization path |
| 289 |
// pState pointer to the LMS signature state |
| 290 |
// |
| 291 |
*F*/ |
| 292 |
|
| 293 |
/* clang-format off */ |
| 294 |
IPPFUN(IppStatus, ippsLMSSetSignatureState, (const IppsLMSAlgoType lmsType, |
| 295 |
Ipp32u q, |
| 296 |
const Ipp8u* pC, |
| 297 |
const Ipp8u* pY, |
| 298 |
const Ipp8u* pAuthPath, |
| 299 |
IppsLMSSignatureState* pState)) |
| 300 |
/* clang-format on */ |
| 301 |
{ |
| 302 |
IPP_BAD_PTR4_RET(pC, pY, pAuthPath, pState); |
| 303 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo >= LMS_MAX, ippStsBadArgErr); |
| 304 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo <= LMS_MIN, ippStsBadArgErr); |
| 305 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo >= LMOTS_MAX, ippStsBadArgErr); |
| 306 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo <= LMOTS_MIN, ippStsBadArgErr); |
| 307 |
|
| 308 |
IppStatus ippcpSts = ippStsErr; |
| 309 |
|
| 310 |
/* Set LMOTS and LMS parameters */ |
| 311 |
cpLMOTSParams lmotsParams; |
| 312 |
ippcpSts = setLMOTSParams(lmsType.lmotsOIDAlgo, &lmotsParams); |
| 313 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 314 |
cpLMSParams lmsParams; |
| 315 |
ippcpSts = setLMSParams(lmsType.lmsOIDAlgo, &lmsParams); |
| 316 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 317 |
|
| 318 |
/* Set context id to prevent its copying */ |
| 319 |
CP_LMS_SET_SIGN_CTX_ID(pState); |
| 320 |
|
| 321 |
/* Check q value before set */ |
| 322 |
Ipp32u qLimit = 1 << lmsParams.h; |
| 323 |
IPP_BADARG_RET(q >= qLimit, ippStsBadArgErr); |
| 324 |
|
| 325 |
pState->_q = q; |
| 326 |
pState->_lmsOIDAlgo = lmsType.lmsOIDAlgo; |
| 327 |
|
| 328 |
_cpLMOTSSignatureState* locLMOTSSig = &(pState->_lmotsSig); |
| 329 |
locLMOTSSig->_lmotsOIDAlgo = lmsType.lmotsOIDAlgo; |
| 330 |
|
| 331 |
// Copy auth path data |
| 332 |
Ipp32s authPathSize = (Ipp32s)(lmsParams.h * lmotsParams.n); |
| 333 |
pState->_pAuthPath = (Ipp8u*)pState + sizeof(IppsLMSSignatureState); |
| 334 |
CopyBlock(pAuthPath, pState->_pAuthPath, authPathSize); |
| 335 |
|
| 336 |
// Copy C data |
| 337 |
Ipp32s cSize = (Ipp32s)lmotsParams.n; |
| 338 |
locLMOTSSig->pC = (Ipp8u*)pState->_pAuthPath + authPathSize; |
| 339 |
CopyBlock(pC, locLMOTSSig->pC, cSize); |
| 340 |
|
| 341 |
// Copy Y data |
| 342 |
Ipp32s ySize = (Ipp32s)(lmotsParams.n * lmotsParams.p); |
| 343 |
locLMOTSSig->pY = (Ipp8u*)pState->_pAuthPath + authPathSize + cSize; |
| 344 |
CopyBlock(pY, locLMOTSSig->pY, ySize); |
| 345 |
|
| 346 |
return ippcpSts; |
| 347 |
} |
| 348 |
|
| 349 |
/*F* |
| 350 |
// Name: ippsLMSKeyGenBufferGetSize |
| 351 |
// |
| 352 |
// Purpose: Get the LMS temporary buffer size needed for ippsLMSKeyGen (bytes). |
| 353 |
// |
| 354 |
// Returns: Reason: |
| 355 |
// ippStsNullPtrErr pSize == NULL |
| 356 |
// ippStsBadArgErr lmsType.lmotsOIDAlgo > LMOTS_SHA256_N24_W8 |
| 357 |
// lmsType.lmotsOIDAlgo < LMOTS_SHA256_N32_W1 |
| 358 |
// lmsType.lmsOIDAlgo > LMS_SHA256_M24_H25 |
| 359 |
// lmsType.lmsOIDAlgo < LMS_SHA256_M32_H5 |
| 360 |
// ippStsNoErr no errors |
| 361 |
// |
| 362 |
// Parameters: |
| 363 |
// pSize pointer to the work buffer's byte size |
| 364 |
// lmsType structure with LMS parameters lmotsOIDAlgo and lmsOIDAlgo |
| 365 |
// |
| 366 |
*F*/ |
| 367 |
|
| 368 |
IPPFUN(IppStatus, ippsLMSKeyGenBufferGetSize, (Ipp32s * pSize, const IppsLMSAlgoType lmsType)) |
| 369 |
{ |
| 370 |
IppStatus ippcpSts = ippStsErr; |
| 371 |
|
| 372 |
/* Input parameters check */ |
| 373 |
IPP_BAD_PTR1_RET(pSize); |
| 374 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo >= LMS_MAX, ippStsBadArgErr); |
| 375 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo <= LMS_MIN, ippStsBadArgErr); |
| 376 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo >= LMOTS_MAX, ippStsBadArgErr); |
| 377 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo <= LMOTS_MIN, ippStsBadArgErr); |
| 378 |
|
| 379 |
/* Set LMOTS and LMS parameters */ |
| 380 |
cpLMOTSParams lmotsParams; |
| 381 |
ippcpSts = setLMOTSParams(lmsType.lmotsOIDAlgo, &lmotsParams); |
| 382 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 383 |
cpLMSParams lmsParams; |
| 384 |
ippcpSts = setLMSParams(lmsType.lmsOIDAlgo, &lmsParams); |
| 385 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 386 |
|
| 387 |
Ipp32u n = lmotsParams.n; |
| 388 |
Ipp32u p = lmotsParams.p; |
| 389 |
Ipp32u h = lmsParams.h; |
| 390 |
|
| 391 |
*pSize = (Ipp32s)(2 * CP_PK_I_BYTESIZE + 4 + 2 + n + n + h + 1 + h * n + n * p + n + |
| 392 |
(4 + 2 + 1 + n + n * p)); |
| 393 |
|
| 394 |
return ippcpSts; |
| 395 |
} |
| 396 |
|
| 397 |
/*F* |
| 398 |
// Name: ippsLMSSignBufferGetSize |
| 399 |
// |
| 400 |
// Purpose: Get the LMS temporary buffer size needed for ippsLMSSign (bytes). |
| 401 |
// |
| 402 |
// Returns: Reason: |
| 403 |
// ippStsNullPtrErr pSize == NULL |
| 404 |
// ippStsBadArgErr lmsType.lmotsOIDAlgo > LMOTS_SHA256_N24_W8 |
| 405 |
// lmsType.lmotsOIDAlgo < LMOTS_SHA256_N32_W1 |
| 406 |
// ippStsLengthErr maxMessageLength < 1 |
| 407 |
// maxMessageLength > (Ipp32s)(IPP_MAX_32S) - |
| 408 |
// - (byteSizeI + 4(q byteSize) + 2(D_MESG byteSize) + n(C byteSize)) |
| 409 |
// ippStsNoErr no errors |
| 410 |
// |
| 411 |
// Parameters: |
| 412 |
// pSize pointer to the work buffer's byte size |
| 413 |
// maxMessageLength maximum length of the processing message |
| 414 |
// lmsType structure with LMS parameters lmotsOIDAlgo and lmsOIDAlgo |
| 415 |
// |
| 416 |
*F*/ |
| 417 |
|
| 418 |
IPPFUN(IppStatus, |
| 419 |
ippsLMSSignBufferGetSize, |
| 420 |
(Ipp32s * pSize, Ipp32s maxMessageLength, const IppsLMSAlgoType lmsType)) |
| 421 |
{ |
| 422 |
IppStatus ippcpSts = ippStsErr; |
| 423 |
|
| 424 |
/* Input parameters check */ |
| 425 |
IPP_BAD_PTR1_RET(pSize); |
| 426 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo >= LMOTS_MAX, ippStsBadArgErr); |
| 427 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo <= LMOTS_MIN, ippStsBadArgErr); |
| 428 |
|
| 429 |
/* Set LMOTS parameters */ |
| 430 |
cpLMOTSParams lmotsParams; |
| 431 |
ippcpSts = setLMOTSParams(lmsType.lmotsOIDAlgo, &lmotsParams); |
| 432 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 433 |
|
| 434 |
Ipp32u n = lmotsParams.n; // LMOTS n |
| 435 |
Ipp32s key_gen_temp_size = 0; |
| 436 |
|
| 437 |
/* Check message length */ |
| 438 |
IPP_BADARG_RET(maxMessageLength < 1, ippStsLengthErr); |
| 439 |
// this restriction is needed to avoid overflow of Ipp32s |
| 440 |
// maxMessageLength must be less than IPP_MAX_32S - (CP_PK_I_BYTESIZE + q + D_MESG + C ) |
| 441 |
IPP_BADARG_RET(maxMessageLength > (Ipp32s)((IPP_MAX_32S) - (CP_PK_I_BYTESIZE + 4 + 2 + n)), |
| 442 |
ippStsLengthErr); |
| 443 |
|
| 444 |
ippcpSts = ippsLMSKeyGenBufferGetSize(&key_gen_temp_size, lmsType); |
| 445 |
|
| 446 |
*pSize = |
| 447 |
(Ipp32s)(IPP_MAX(CP_PK_I_BYTESIZE + 4 + 2 + n + IPP_MAX((Ipp32u)maxMessageLength, 1 + n), |
| 448 |
(Ipp32u)key_gen_temp_size)); |
| 449 |
return ippcpSts; |
| 450 |
} |
| 451 |
|
| 452 |
/*F* |
| 453 |
// Name: ippsLMSPrivateKeyStateGetSize |
| 454 |
// |
| 455 |
// Purpose: Provides the LMS private key state size (bytes). |
| 456 |
// |
| 457 |
// Returns: Reason: |
| 458 |
// ippStsNullPtrErr pSize == NULL |
| 459 |
// ippStsBadArgErr lmsType.lmotsOIDAlgo > LMOTS_SHA256_N24_W8 |
| 460 |
// lmsType.lmotsOIDAlgo < LMOTS_SHA256_N32_W1 |
| 461 |
// ippStsLengthErr extraBufSize < 0 |
| 462 |
// ippStsSizeWrn extraBufSize % lmsParams.m != 0 |
| 463 |
// ippStsNoErr no errors |
| 464 |
// |
| 465 |
// Parameters: |
| 466 |
// pSize pointer to the size |
| 467 |
// lmsType structure with LMS parameters lmotsOIDAlgo and lmsOIDAlgo |
| 468 |
// extraBufSize size of the extra buffer for the private key |
| 469 |
// |
| 470 |
*F*/ |
| 471 |
|
| 472 |
IPPFUN(IppStatus, |
| 473 |
ippsLMSPrivateKeyStateGetSize, |
| 474 |
(Ipp32s * pSize, const IppsLMSAlgoType lmsType, Ipp32s extraBufSize)) |
| 475 |
{ |
| 476 |
IppStatus ippcpSts = ippStsErr; |
| 477 |
|
| 478 |
/* Input parameters check */ |
| 479 |
IPP_BAD_PTR1_RET(pSize); |
| 480 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo >= LMS_MAX, ippStsBadArgErr); |
| 481 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo <= LMS_MIN, ippStsBadArgErr); |
| 482 |
IPP_BADARG_RET(extraBufSize < 0, ippStsLengthErr); |
| 483 |
|
| 484 |
/* Set LMS parameters */ |
| 485 |
cpLMOTSParams lmotsParams; |
| 486 |
ippcpSts = setLMOTSParams(lmsType.lmotsOIDAlgo, &lmotsParams); |
| 487 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 488 |
|
| 489 |
// Data are kept in the extra buffer by chunks equal lmotsParams.n. |
| 490 |
// There is no sense to keep space for less than lmotsParams.n bytes |
| 491 |
IPP_BADARG_RET((extraBufSize % (Ipp32s)lmotsParams.n) != 0, ippStsSizeWrn); |
| 492 |
// rounding to lmotsParams.n |
| 493 |
extraBufSize = (extraBufSize / (Ipp32s)lmotsParams.n) * (Ipp32s)lmotsParams.n; |
| 494 |
|
| 495 |
*pSize = (Ipp32s)(sizeof(IppsLMSPrivateKeyState) + lmotsParams.n + CP_PK_I_BYTESIZE) + |
| 496 |
extraBufSize + 2 * CP_LMS_ALIGNMENT; |
| 497 |
|
| 498 |
return ippcpSts; |
| 499 |
} |
| 500 |
|
| 501 |
/*F* |
| 502 |
// Name: ippsLMSInitKeyPair |
| 503 |
// |
| 504 |
// Purpose: Init LMS public and private keys states. |
| 505 |
// |
| 506 |
// Returns: Reason: |
| 507 |
// ippStsNullPtrErr pPrvKey == NULL or pPubKey == NULL |
| 508 |
// ippStsBadArgErr lmsType.lmotsOIDAlgo > Max value for IppsLMOTSAlgo |
| 509 |
// lmsType.lmotsOIDAlgo < Min value for IppsLMOTSAlgo |
| 510 |
// lmsType.lmsOIDAlgo > Max value for IppsLMSAlgo |
| 511 |
// lmsType.lmsOIDAlgo < Min value for IppsLMSAlgo |
| 512 |
// ippStsLengthErr extraBufSize < 0 |
| 513 |
// ippStsNoErr no errors |
| 514 |
// |
| 515 |
// Parameters: |
| 516 |
// lmsType id of LMS set of parameters (algorithm) |
| 517 |
// pPrvKey pointer to the LMS private key state |
| 518 |
// pPubKey pointer to the LMS public key state |
| 519 |
// extraBufSize size of the extra buffer for the private key (bytes) |
| 520 |
// |
| 521 |
*F*/ |
| 522 |
|
| 523 |
IPPFUN(IppStatus, |
| 524 |
ippsLMSInitKeyPair, |
| 525 |
(const IppsLMSAlgoType lmsType, |
| 526 |
Ipp32s extraBufSize, |
| 527 |
IppsLMSPrivateKeyState* pPrvKey, |
| 528 |
IppsLMSPublicKeyState* pPubKey)) |
| 529 |
{ |
| 530 |
IppStatus ippcpSts = ippStsErr; |
| 531 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo >= LMS_MAX, ippStsBadArgErr); |
| 532 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo <= LMS_MIN, ippStsBadArgErr); |
| 533 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo >= LMOTS_MAX, ippStsBadArgErr); |
| 534 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo <= LMOTS_MIN, ippStsBadArgErr); |
| 535 |
IPP_BADARG_RET(extraBufSize < 0, ippStsLengthErr); |
| 536 |
|
| 537 |
/* Input parameters check */ |
| 538 |
IPP_BAD_PTR2_RET(pPrvKey, pPubKey); |
| 539 |
|
| 540 |
/* Set LMS parameters */ |
| 541 |
cpLMOTSParams lmotsParams; |
| 542 |
ippcpSts = setLMOTSParams(lmsType.lmotsOIDAlgo, &lmotsParams); |
| 543 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 544 |
|
| 545 |
cpLMSParams lmsParams; |
| 546 |
ippcpSts = setLMSParams(lmsType.lmsOIDAlgo, &lmsParams); |
| 547 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 548 |
|
| 549 |
CP_LMS_SET_PRIV_KEY_CTX_ID(pPrvKey); |
| 550 |
pPrvKey->lmsOIDAlgo = lmsType.lmsOIDAlgo; |
| 551 |
pPrvKey->lmotsOIDAlgo = lmsType.lmotsOIDAlgo; |
| 552 |
|
| 553 |
Ipp8u* ptr = (Ipp8u*)pPrvKey; |
| 554 |
|
| 555 |
/* allocate internal contexts */ |
| 556 |
ptr += sizeof(IppsLMSPrivateKeyState); |
| 557 |
|
| 558 |
pPrvKey->pSecretSeed = (Ipp8u*)(IPP_ALIGNED_PTR((ptr), CP_LMS_ALIGNMENT)); |
| 559 |
ptr += lmotsParams.n; |
| 560 |
|
| 561 |
pPrvKey->pI = (Ipp8u*)(IPP_ALIGNED_PTR((ptr), CP_LMS_ALIGNMENT)); |
| 562 |
ptr += CP_PK_I_BYTESIZE; |
| 563 |
|
| 564 |
pPrvKey->pExtraBuf = ptr; |
| 565 |
// rounding to lmsParams.m |
| 566 |
extraBufSize = (extraBufSize / (Ipp32s)lmsParams.m) * (Ipp32s)lmsParams.m; |
| 567 |
pPrvKey->extraBufSize = extraBufSize; |
| 568 |
|
| 569 |
CP_LMS_SET_PUB_KEY_CTX_ID(pPubKey); |
| 570 |
pPubKey->lmsOIDAlgo = lmsType.lmsOIDAlgo; |
| 571 |
pPubKey->lmotsOIDAlgo = lmsType.lmotsOIDAlgo; |
| 572 |
|
| 573 |
ptr = (Ipp8u*)pPubKey; |
| 574 |
ptr += sizeof(IppsLMSPublicKeyState); |
| 575 |
|
| 576 |
pPubKey->T1 = ptr; |
| 577 |
|
| 578 |
return ippcpSts; |
| 579 |
} |
| 580 |
|
| 581 |
/*F* |
| 582 |
// Name: ippsLMSInitSignature |
| 583 |
// |
| 584 |
// Purpose: Init the LMS signature state. |
| 585 |
// |
| 586 |
// Returns: Reason: |
| 587 |
// ippStsNullPtrErr pSign == NULL |
| 588 |
// ippStsBadArgErr lmsType.lmotsOIDAlgo > Max value for IppsLMOTSAlgo |
| 589 |
// ippStsBadArgErr lmsType.lmotsOIDAlgo < Min value for IppsLMOTSAlgo |
| 590 |
// ippStsBadArgErr lmsType.lmsOIDAlgo > Max value for IppsLMSAlgo |
| 591 |
// ippStsBadArgErr lmsType.lmsOIDAlgo < Min value for IppsLMSAlgo |
| 592 |
// ippStsNoErr no errors |
| 593 |
// |
| 594 |
// Parameters: |
| 595 |
// lmsType id of LMS set of parameters (algorithm) |
| 596 |
// pSign pointer to the LMS signature state |
| 597 |
// |
| 598 |
*F*/ |
| 599 |
|
| 600 |
IPPFUN(IppStatus, |
| 601 |
ippsLMSInitSignature, |
| 602 |
(const IppsLMSAlgoType lmsType, IppsLMSSignatureState* pSign)) |
| 603 |
{ |
| 604 |
IppStatus ippcpSts = ippStsErr; |
| 605 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo >= LMS_MAX, ippStsBadArgErr); |
| 606 |
IPP_BADARG_RET(lmsType.lmsOIDAlgo <= LMS_MIN, ippStsBadArgErr); |
| 607 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo >= LMOTS_MAX, ippStsBadArgErr); |
| 608 |
IPP_BADARG_RET(lmsType.lmotsOIDAlgo <= LMOTS_MIN, ippStsBadArgErr); |
| 609 |
|
| 610 |
IPP_BAD_PTR1_RET(pSign); |
| 611 |
|
| 612 |
/* Set LMOTS parameters */ |
| 613 |
cpLMOTSParams lmotsParams; |
| 614 |
ippcpSts = setLMOTSParams(lmsType.lmotsOIDAlgo, &lmotsParams); |
| 615 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 616 |
|
| 617 |
CP_LMS_SET_SIGN_CTX_ID(pSign); |
| 618 |
pSign->_lmsOIDAlgo = lmsType.lmsOIDAlgo; |
| 619 |
pSign->_q = 0; |
| 620 |
|
| 621 |
Ipp8u* ptr = (Ipp8u*)pSign; |
| 622 |
|
| 623 |
/* allocate internal contexts */ |
| 624 |
ptr += sizeof(IppsLMSSignatureState); |
| 625 |
|
| 626 |
pSign->_lmotsSig._lmotsOIDAlgo = lmsType.lmotsOIDAlgo; |
| 627 |
pSign->_lmotsSig.pC = (Ipp8u*)(IPP_ALIGNED_PTR((ptr), CP_LMS_ALIGNMENT)); |
| 628 |
ptr += lmotsParams.n; |
| 629 |
|
| 630 |
pSign->_lmotsSig.pY = ptr; |
| 631 |
ptr += lmotsParams.n * lmotsParams.p; |
| 632 |
|
| 633 |
pSign->_pAuthPath = ptr; |
| 634 |
|
| 635 |
return ippcpSts; |
| 636 |
} |