| 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 |
/*F* |
| 20 |
// Name: ippsLMSSign |
| 21 |
// |
| 22 |
// Purpose: LMS signature generation. |
| 23 |
// |
| 24 |
// Returns: Reason: |
| 25 |
// ippStsNullPtrErr pMsg == NULL |
| 26 |
// pPrvKey == NULL |
| 27 |
// pSign == NULL |
| 28 |
// pBuffer == NULL |
| 29 |
// ippStsBadArgErr wrong LMS or LMOTS parameters |
| 30 |
// inside pPrvKey |
| 31 |
// ippStsContextMatchErr pSign or pPrvKey contexts are invalid |
| 32 |
// ippStsLengthErr msgLen < 1 |
| 33 |
// ippStsLengthErr msgLen > IPP_MAX_32S - (CP_PK_I_BYTESIZE + 4 + 2 + n) |
| 34 |
// ippStsNoErr no errors |
| 35 |
// ippStsOutOfRangeErr private key will not be valid any longer after executing the function |
| 36 |
// |
| 37 |
// Parameters: |
| 38 |
// pMsg pointer to the message data buffer |
| 39 |
// msgLen message buffer length |
| 40 |
// pPrvKey pointer to the private key |
| 41 |
// pSign pointer to the signature |
| 42 |
// rndFunc function pointer to generate random numbers. It can be NULL |
| 43 |
// Security strength must be 8*n bits |
| 44 |
// pRndParam parameters for rndFunc. It can be NULL |
| 45 |
// pBuffer pointer to the temporary memory |
| 46 |
// |
| 47 |
*F*/ |
| 48 |
|
| 49 |
/* clang-format off */ |
| 50 |
IPPFUN(IppStatus, ippsLMSSign, (const Ipp8u* pMsg, |
| 51 |
const Ipp32s msgLen, |
| 52 |
IppsLMSPrivateKeyState* pPrvKey, |
| 53 |
IppsLMSSignatureState* pSign, |
| 54 |
IppBitSupplier rndFunc, |
| 55 |
void* pRndParam, |
| 56 |
Ipp8u* pBuffer)) |
| 57 |
/* clang-format on */ |
| 58 |
{ |
| 59 |
IppStatus ippcpSts = ippStsErr; |
| 60 |
|
| 61 |
/* Check if any of input pointers are NULL */ |
| 62 |
IPP_BAD_PTR4_RET(pMsg, pPrvKey, pSign, pBuffer) |
| 63 |
/* Check msg length */ |
| 64 |
IPP_BADARG_RET(msgLen < 1, ippStsLengthErr) |
| 65 |
IPP_BADARG_RET(!CP_LMS_VALID_PRIV_KEY_CTX_ID(pPrvKey), ippStsContextMatchErr); |
| 66 |
IPP_BADARG_RET(!CP_LMS_VALID_SIGN_CTX_ID(pSign), ippStsContextMatchErr); |
| 67 |
IPP_BADARG_RET(pPrvKey->lmsOIDAlgo >= LMS_MAX, ippStsBadArgErr); |
| 68 |
IPP_BADARG_RET(pPrvKey->lmsOIDAlgo <= LMS_MIN, ippStsBadArgErr); |
| 69 |
IPP_BADARG_RET(pPrvKey->lmotsOIDAlgo >= LMOTS_MAX, ippStsBadArgErr); |
| 70 |
IPP_BADARG_RET(pPrvKey->lmotsOIDAlgo <= LMOTS_MIN, ippStsBadArgErr); |
| 71 |
|
| 72 |
// Set LMOTS and LMS parameters |
| 73 |
cpLMSParams lmsParams; |
| 74 |
cpLMOTSParams lmotsParams; |
| 75 |
ippcpSts = setLMSParams(pPrvKey->lmsOIDAlgo, &lmsParams); |
| 76 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 77 |
ippcpSts = setLMOTSParams(pPrvKey->lmotsOIDAlgo, &lmotsParams); |
| 78 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 79 |
Ipp32u n = lmotsParams.n; |
| 80 |
Ipp32u h = lmsParams.h; |
| 81 |
IPP_BADARG_RET(msgLen > (Ipp32s)((IPP_MAX_32S) - (CP_PK_I_BYTESIZE + 4 + 2 + n)), |
| 82 |
ippStsLengthErr); |
| 83 |
|
| 84 |
Ipp32s pBufferSize; |
| 85 |
IppsLMSAlgoType algoTypes = { pPrvKey->lmotsOIDAlgo, pPrvKey->lmsOIDAlgo }; |
| 86 |
ippcpSts = ippsLMSSignBufferGetSize(&pBufferSize, msgLen, algoTypes); |
| 87 |
IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts) |
| 88 |
|
| 89 |
// set q of current private key |
| 90 |
// pass the error if we are out of secret keys |
| 91 |
// Note: there is no overflow since the maximum value for h is 25 according to the Spec |
| 92 |
if (pPrvKey->q >= (Ipp32u)(1 << h)) { |
| 93 |
return ippStsOutOfRangeErr; |
| 94 |
} |
| 95 |
pSign->_q = pPrvKey->q; |
| 96 |
|
| 97 |
// fill _lmotsSig |
| 98 |
ippcpSts = cp_rand_num((Ipp32u*)pSign->_lmotsSig.pC, (Ipp32s)n, rndFunc, pRndParam); |
| 99 |
if (ippStsNoErr != ippcpSts) { |
| 100 |
PurgeBlock(pSign->_lmotsSig.pC, (Ipp32s)n); // zeroize C if error occurs |
| 101 |
return ippcpSts; |
| 102 |
} |
| 103 |
|
| 104 |
ippcpSts = cp_lms_OTS_sign( |
| 105 |
pMsg, |
| 106 |
msgLen, |
| 107 |
pPrvKey->pSecretSeed, |
| 108 |
pPrvKey->pI, |
| 109 |
pSign->_q, |
| 110 |
pSign->_lmotsSig.pC, |
| 111 |
pSign->_lmotsSig.pY, |
| 112 |
pBuffer, |
| 113 |
&lmotsParams); // temp size: CP_PK_I_BYTESIZE + 4 + 2 + n + max(msgLen, 1 + n) |
| 114 |
if (ippStsNoErr != ippcpSts) { |
| 115 |
PurgeBlock(pSign->_lmotsSig.pC, (Ipp32s)n); // zeroize C if error occurs |
| 116 |
PurgeBlock(pBuffer, pBufferSize); |
| 117 |
return ippcpSts; |
| 118 |
} |
| 119 |
|
| 120 |
// pAuthPath |
| 121 |
ippcpSts = cp_lms_tree_hash(/*isKeyGen*/ 0, |
| 122 |
pPrvKey->pSecretSeed, |
| 123 |
pPrvKey->pI, |
| 124 |
pSign->_pAuthPath, |
| 125 |
pPrvKey->q, |
| 126 |
pBuffer, |
| 127 |
pPrvKey->pExtraBuf, |
| 128 |
pPrvKey->extraBufSize, |
| 129 |
&lmsParams, |
| 130 |
&lmotsParams); |
| 131 |
if (ippStsNoErr != ippcpSts) { |
| 132 |
PurgeBlock(pSign->_lmotsSig.pC, (Ipp32s)n); // zeroize C if error occurs |
| 133 |
PurgeBlock(pBuffer, pBufferSize); |
| 134 |
return ippcpSts; |
| 135 |
} |
| 136 |
|
| 137 |
// zeroize the temporary memory if everything else was successful |
| 138 |
PurgeBlock(pBuffer, pBufferSize); |
| 139 |
|
| 140 |
// during the next call another private key should be used to sign |
| 141 |
pPrvKey->q++; |
| 142 |
|
| 143 |
// pass the error if we are out of secret keys |
| 144 |
// Note: there is no overflow since the maximum value for h is 25 according to the Spec |
| 145 |
if (pPrvKey->q >= (Ipp32u)(1 << h)) { |
| 146 |
return ippStsOutOfRangeErr; |
| 147 |
} |
| 148 |
|
| 149 |
return ippcpSts; |
| 150 |
} |