Debian Codegraph @

sources/ippcp/stateful_sig/lms/lms_keygen.c

raw
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: ippsLMSKeyGen
21 //
22 // Purpose: LMS key pair generation.
23 //
24 // Returns: Reason:
25 // ippStsNullPtrErr pPrvKey == NULL
26 // pPubKey == NULL
27 // pBuffer == NULL
28 // ippStsBadArgErr wrong LMS or LMOTS parameters
29 // inside pPrvKey
30 // ippStsContextMatchErr pPrvKey or pPubKey contexts are invalid
31 // ippStsNoErr no errors
32 //
33 // Parameters:
34 // pPrvKey pointer to the private key state
35 // pPubKey pointer to the public key state
36 // rndFunc function pointer to generate random numbers. It can be NULL
37 // Security strength must be 8*n bits
38 // pRndParam parameters for rndFunc. It can be NULL
39 // pBuffer pointer to the temporary memory
40 //
41 *F*/
42
43 /* clang-format off */
44 IPPFUN(IppStatus, ippsLMSKeyGen, (IppsLMSPrivateKeyState* pPrvKey,
45 IppsLMSPublicKeyState* pPubKey,
46 IppBitSupplier rndFunc,
47 void* pRndParam,
48 Ipp8u* pBuffer))
49 /* clang-format on */
50 {
51 IppStatus ippcpSts = ippStsErr;
52
53 /* Check if any of input pointers are NULL */
54 IPP_BAD_PTR3_RET(pPrvKey, pPubKey, pBuffer)
55 IPP_BADARG_RET(!CP_LMS_VALID_PRIV_KEY_CTX_ID(pPrvKey), ippStsContextMatchErr);
56 IPP_BADARG_RET(!CP_LMS_VALID_PUB_KEY_CTX_ID(pPubKey), ippStsContextMatchErr);
57 IPP_BADARG_RET(pPrvKey->lmsOIDAlgo >= LMS_MAX, ippStsBadArgErr);
58 IPP_BADARG_RET(pPrvKey->lmsOIDAlgo <= LMS_MIN, ippStsBadArgErr);
59 IPP_BADARG_RET(pPrvKey->lmotsOIDAlgo >= LMOTS_MAX, ippStsBadArgErr);
60 IPP_BADARG_RET(pPrvKey->lmotsOIDAlgo <= LMOTS_MIN, ippStsBadArgErr);
61
62 // Set LMOTS and LMS parameters
63 cpLMSParams lmsParams;
64 cpLMOTSParams lmotsParams;
65 ippcpSts = setLMSParams(pPrvKey->lmsOIDAlgo, &lmsParams);
66 IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts)
67 ippcpSts = setLMOTSParams(pPrvKey->lmotsOIDAlgo, &lmotsParams);
68 IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts)
69 Ipp32u n = lmotsParams.n;
70
71 Ipp32s pBufferSize;
72 IppsLMSAlgoType algoTypes = { pPrvKey->lmotsOIDAlgo, pPrvKey->lmsOIDAlgo };
73
74 ippcpSts = ippsLMSKeyGenBufferGetSize(&pBufferSize, algoTypes);
75 IPP_BADARG_RET((ippStsNoErr != ippcpSts), ippcpSts)
76
77 // fill private key fields
78 pPrvKey->q = 0;
79
80 ippcpSts = cp_rand_num((Ipp32u*)pPrvKey->pSecretSeed, (Ipp32s)n, rndFunc, pRndParam);
81 if (ippStsNoErr != ippcpSts) {
82 PurgeBlock(pPrvKey->pSecretSeed, (Ipp32s)n); // zeroize the secret seed if error occurs
83 return ippcpSts;
84 }
85
86 ippcpSts = cp_rand_num((Ipp32u*)pPrvKey->pI, CP_PK_I_BYTESIZE, rndFunc, pRndParam);
87 if (ippStsNoErr != ippcpSts) {
88 PurgeBlock(pPrvKey->pSecretSeed, (Ipp32s)n); // zeroize the secret seed if error occurs
89 PurgeBlock(pPrvKey->pI, CP_PK_I_BYTESIZE); // zeroize the I if error occurs
90 return ippcpSts;
91 }
92 // calculate the public key as a root of the LMS tree
93 ippcpSts = cp_lms_tree_hash(/*isKeyGen*/ 1,
94 pPrvKey->pSecretSeed,
95 pPrvKey->pI,
96 pPubKey->T1,
97 /*empty*/ 0,
98 pBuffer,
99 pPrvKey->pExtraBuf,
100 pPrvKey->extraBufSize,
101 &lmsParams,
102 &lmotsParams);
103 PurgeBlock(pBuffer, pBufferSize); // zeroize the temporary memory
104 if (ippStsNoErr != ippcpSts) {
105 PurgeBlock(pPrvKey->pSecretSeed, (Ipp32s)n); // zeroize the secret seed if error occurs
106 PurgeBlock(pPrvKey->pI, CP_PK_I_BYTESIZE); // zeroize the I if error occurs
107 return ippcpSts;
108 }
109 // copy I from private key to public
110 CopyBlock(pPrvKey->pI, pPubKey->I, CP_PK_I_BYTESIZE);
111
112 return ippcpSts;
113 }