Debian Codegraph @

sources/ippcp/ml_kem/internal/k_pke/k_pke_encrypt.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_Encrypt
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 the encryption key to encrypt a plaintext message using the randomness r.
28 *
29 * ciphertext - output pointer to the generated ciphertext of size 32*(d_{u}*k+d_{v})) bytes
30 * inpEncKey - input pointer to the encryption key of size 384*k + 32 bytes
31 * m - input parameter with the generated plaintext message of size 32 bytes
32 * r_N - input parameter with the generated randomness of size 32 bytes + 1 byte to store N
33 * mlkemCtx - input pointer to the ML KEM context
34 */
35 /* clang-format off */
36 IPP_OWN_DEFN(IppStatus, cp_KPKE_Encrypt, (Ipp8u * ciphertext,
37 const Ipp8u* inpEncKey,
38 const Ipp8u m[CP_RAND_DATA_BYTES],
39 Ipp8u r_N[CP_RAND_DATA_BYTES + 1],
40 IppsMLKEMState* mlkemCtx))
41 /* clang-format on */
42 {
43 IppStatus sts = ippStsNoErr;
44 const Ipp8u k = mlkemCtx->params.k;
45 const Ipp16u d_u = mlkemCtx->params.d_u;
46 const Ipp8u d_v = mlkemCtx->params.d_v;
47 const Ipp8u eta1 = mlkemCtx->params.eta1;
48 const Ipp8u eta2 = mlkemCtx->params.eta2;
49 _cpMLKEMStorage* pStorage = &mlkemCtx->storage;
50
51 /* Allocate memory for temporary objects */
52 CP_ML_ALLOCATE_ALIGNED_POLYVEC(t, k, pStorage)
53 CP_ML_ALLOCATE_ALIGNED_POLYVEC(vectorY, k, pStorage)
54 CP_ML_ALLOCATE_ALIGNED_POLY(polyE2, pStorage)
55 CP_ML_ALLOCATE_ALIGNED_POLYVEC(vectorE1, k, pStorage)
56 CP_ML_ALLOCATE_ALIGNED_POLYVEC(u, k, pStorage)
57 CP_ML_ALLOCATE_ALIGNED_POLY(polyMu, pStorage)
58 CP_ML_ALLOCATE_ALIGNED_POLY(v, pStorage)
59
60 /* 1: N <- 0, iterated in the range [0, 7) */
61 Ipp8u N = 0;
62
63 /* 2: Decode t <- cp_byteDecode_{12}(ekPKE[0 : 384*k]), t is an element of (Z_{q}^{256})^{k} */
64 for (Ipp8u i = 0; i < k; i++) {
65 sts = cp_byteDecode(&t[i], 12, inpEncKey + 384 * i, 384 * (k - i));
66 IPP_BADARG_RET((sts != ippStsNoErr), sts);
67 }
68
69 /* 3: rho <- ekPKE[384*k : 384*k+32] */
70 Ipp8u rho_j_i[34];
71 CopyBlock(inpEncKey + 384 * k, rho_j_i, 32);
72
73 /* 4-8: Generate transposed matrix A */
74 Ipp16sPoly* matrixA = (Ipp16sPoly*)(mlkemCtx->pA);
75 cp_matrixAGen(matrixA, rho_j_i, matrixATransposed, mlkemCtx);
76
77 /* 9-12: Generate vector y */
78 cp_polyVecGen(vectorY, r_N, &N, eta1, mlkemCtx, nttTransform);
79
80 /* 13-16: Generate vector e1 */
81 cp_polyVecGen(vectorE1, r_N, &N, eta2, mlkemCtx, noNttTransform);
82
83 /* 17: Generate polynomial e2 */
84 cp_polyGen(polyE2, r_N, &N, eta2, mlkemCtx, noNttTransform);
85
86 /* 19: u <- cp_NTT^{-1}(A^{T} * y`) + e1 */
87 CP_ML_ALLOCATE_ALIGNED_POLY(tmpPoly, pStorage)
88 for (Ipp8u i = 0; i < k; i++) {
89 cp_multiplyNTT(CP_MATRIX_A_GET_I_J(matrixA, i, 0), &vectorY[0], &u[i]);
90
91 for (Ipp8u j = 1; j < k; j++) {
92 cp_multiplyNTT(CP_MATRIX_A_GET_I_J(matrixA, i, j), &vectorY[j], tmpPoly);
93 cp_polyAdd(tmpPoly, &u[i], &u[i]);
94 }
95 cp_inverseNTT(&u[i]);
96 cp_polyAdd(&vectorE1[i], &u[i], &u[i]);
97 }
98
99 /* 20: mu <- Decompress_{1}(cp_byteDecode_{1}(m)) */
100 sts = cp_byteDecode(polyMu, 1, m, CP_RAND_DATA_BYTES);
101 IPP_BADARG_RET((sts != ippStsNoErr), sts);
102
103 for (Ipp32u i = 0; i < 256; i++) {
104 sts = cp_Decompress((Ipp16u*)&polyMu->values[i], polyMu->values[i], 1);
105 IPP_BADARG_RET((sts != ippStsNoErr), sts);
106 }
107
108 /* 21: v <- cp_NTT^{-1}(t^{T} * y) + e2 + mu */
109 cp_multiplyNTT(&t[0], &vectorY[0], v);
110 for (Ipp8u j = 1; j < k; j++) {
111 cp_multiplyNTT(&t[j], &vectorY[j], tmpPoly);
112 cp_polyAdd(tmpPoly, v, v);
113 }
114 CP_ML_RELEASE_ALIGNED_POLY(pStorage, sts) // Ipp16sPoly tmpPoly
115 cp_inverseNTT(v);
116 cp_polyAdd(polyE2, v, v);
117 cp_polyAdd(polyMu, v, v);
118
119 /* Set up c1 and c2 pointers */
120 Ipp8u* c1 = ciphertext;
121 Ipp8u* c2 = ciphertext + (32 * d_u * k);
122
123 /* 22: c1 <- cp_byteEncode_{d_{u}}(Compress_{d_{u}}(u)) */
124 for (Ipp8u i = 0; i < k; i++) {
125 for (Ipp32u j = 0; j < 256; j++) {
126 sts = cp_Compress((Ipp16u*)&u[i].values[j], u[i].values[j], d_u);
127 IPP_BADARG_RET((sts != ippStsNoErr), sts);
128 }
129 sts = cp_byteEncode(c1 + i * 32 * d_u, d_u, &u[i]);
130 IPP_BADARG_RET((sts != ippStsNoErr), sts);
131 }
132
133 /* 23: c2 <- cp_byteEncode_{d_{v}}(Compress_{d_{v}}(v)) */
134 for (Ipp32u j = 0; j < 256; j++) {
135 sts = cp_Compress((Ipp16u*)&v->values[j], v->values[j], d_v);
136 IPP_BADARG_RET((sts != ippStsNoErr), sts);
137 }
138 sts = cp_byteEncode(c2, d_v, v);
139 IPP_BADARG_RET((sts != ippStsNoErr), sts);
140
141 /* Release locally used storage */
142 CP_ML_RELEASE_ALIGNED_POLYVEC(k, pStorage, sts) // Ipp16sPoly t[k]
143 CP_ML_RELEASE_ALIGNED_POLYVEC(k, pStorage, sts) // Ipp16sPoly vectorY[k]
144 CP_ML_RELEASE_ALIGNED_POLY(pStorage, sts) // Ipp16sPoly polyE2
145 CP_ML_RELEASE_ALIGNED_POLYVEC(k, pStorage, sts) // Ipp16sPoly vectorE1[k]
146 CP_ML_RELEASE_ALIGNED_POLYVEC(k, pStorage, sts) // Ipp16sPoly u[k]
147 CP_ML_RELEASE_ALIGNED_POLY(pStorage, sts) // Ipp16sPoly polyMu
148 CP_ML_RELEASE_ALIGNED_POLY(pStorage, sts) // Ipp16sPoly v
149
150 return sts;
151 }