Debian Codegraph @

sources/ippcp/ml_kem/internal/k_pke/k_pke_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 /*
17 * Level 2(K-PKE) function cp_KPKE_KeyGen
18 */
19
20 #include "owncp.h"
21 #include "owndefs.h"
22 #include "ippcpdefs.h"
23 #include "stateless_pqc/ml_kem_internal/ml_kem.h"
24 #include "hash/pcphash_rmf.h"
25
26 /*
27 * Uses randomness to generate an encryption key and a corresponding decryption key.
28 * outEncKey - output pointer to the encryption key of size 384*k + 32 bytes
29 * outDecKey - output pointer to the decryption key of size 384*k
30 * d_k - input parameter with the generated randomness of size 32 bytes + 1 byte for k
31 * mlkemCtx - input pointer to the ML KEM context
32 */
33 /* clang-format off */
34 IPP_OWN_DEFN(IppStatus, cp_KPKE_KeyGen, (Ipp8u* outEncKey,
35 Ipp8u* outDecKey,
36 const Ipp8u d_k[33],
37 IppsMLKEMState* mlkemCtx))
38 /* clang-format on */
39 {
40 IppStatus sts = ippStsNoErr;
41 const Ipp8u k = mlkemCtx->params.k;
42 const Ipp8u eta1 = mlkemCtx->params.eta1;
43 _cpMLKEMStorage* pStorage = &mlkemCtx->storage;
44
45 /* Allocate memory for temporary objects */
46 CP_ML_ALLOCATE_ALIGNED_POLYVEC(vectorS, k, pStorage)
47 CP_ML_ALLOCATE_ALIGNED_POLYVEC(vectorE, k, pStorage)
48 CP_ML_ALLOCATE_ALIGNED_POLYVEC(t, k, pStorage)
49
50 /* (rho,sigma) <- G(d||k) */
51 // stores 32 bytes of rho, 32 bytes of sigma and 1 byte of N
52 Ipp8u rho_sigma_N[65];
53 Ipp8u* pRho = rho_sigma_N;
54 Ipp8u* pSigma_N = rho_sigma_N + 32;
55
56 sts = ippsHashMessage_rmf(d_k, 33, rho_sigma_N, ippsHashMethod_SHA3_512());
57 IPP_BADARG_RET((sts != ippStsNoErr), sts);
58
59 // N is iterated in the range [0, 7)
60 Ipp8u N = 0;
61
62 /* Generate matrix A */
63 Ipp8u rho_j_i[34];
64 CopyBlock(pRho, rho_j_i, 32);
65 Ipp16sPoly* matrixA = (Ipp16sPoly*)(mlkemCtx->pA); // vectors accessing pointer
66 cp_matrixAGen(matrixA, rho_j_i, matrixAOrigin, mlkemCtx);
67
68 /* Generate vector s and write to output */
69 cp_polyVecGen(vectorS, pSigma_N, &N, eta1, mlkemCtx, nttTransform);
70 for (Ipp8u i = 0; i < k; i++) {
71 sts = cp_byteEncode(outDecKey + i * 384, 12, &vectorS[i]);
72 IPP_BADARG_RET((sts != ippStsNoErr), sts);
73 }
74
75 /* Generate vector e */
76 cp_polyVecGen(vectorE, pSigma_N, &N, eta1, mlkemCtx, nttTransform);
77
78 /* t` = A` * s` + e` */
79 CP_ML_ALLOCATE_ALIGNED_POLY(tmpPoly, pStorage)
80 for (Ipp8u i = 0; i < k; i++) {
81 cp_multiplyNTT(CP_MATRIX_A_GET_I_J(matrixA, i, 0), &vectorS[0], &t[i]);
82
83 for (Ipp8u j = 1; j < k; j++) {
84 cp_multiplyNTT(CP_MATRIX_A_GET_I_J(matrixA, i, j), &vectorS[j], tmpPoly);
85 cp_polyAdd(tmpPoly, &t[i], &t[i]);
86 }
87 cp_polyAdd(&vectorE[i], &t[i], &t[i]);
88 }
89 CP_ML_RELEASE_ALIGNED_POLY(pStorage, sts) // Ipp16sPoly tmpPoly
90
91 for (Ipp8u i = 0; i < k; i++) {
92 sts = cp_byteEncode(outEncKey + i * 384, 12, &t[i]);
93 IPP_BADARG_RET((sts != ippStsNoErr), sts);
94 }
95 CopyBlock(pRho, outEncKey + 384 * k, 32);
96
97 /* Release locally used storage */
98 CP_ML_RELEASE_ALIGNED_POLYVEC(k, pStorage, sts) // Ipp16sPoly vectorS[k]
99 CP_ML_RELEASE_ALIGNED_POLYVEC(k, pStorage, sts) // Ipp16sPoly vectorE[k]
100 CP_ML_RELEASE_ALIGNED_POLYVEC(k, pStorage, sts) // Ipp16sPoly t[k]
101
102 return sts;
103 }