Debian Codegraph @

sources/ippcp/ml_kem/internal/internal_decaps.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 //-------------------------------//
18 // Level 1 functions
19 //-------------------------------//
20
21 #include "owncp.h"
22 #include "owndefs.h"
23 #include "ippcpdefs.h"
24 #include "stateless_pqc/ml_kem_internal/ml_kem.h"
25
26 /*
27 * Uses the decapsulation key to produce a shared secret key from a ciphertext.
28 *
29 * K - output pointer to the generated shared secret key K of size 32 bytes
30 * ciphertext - input pointer to the ciphertext of size 32*(d_{u}*k + d_{v})) bytes
31 * inpDecKey - input pointer to the decapsulation key (private key) of size 768*k + 96 bytes
32 * mlkemCtx - input pointer to ML KEM context
33 */
34 /* clang-format off */
35 IPP_OWN_DEFN(IppStatus, cp_MLKEMdecaps_internal, (Ipp8u K[CP_SHARED_SECRET_BYTES],
36 const Ipp8u* ciphertext,
37 const Ipp8u* inpDecKey,
38 IppsMLKEMState* mlkemCtx))
39 /* clang-format on */
40 {
41 const Ipp8u k = mlkemCtx->params.k;
42 _cpMLKEMStorage* pStorage = &mlkemCtx->storage;
43
44 int ciphertext_size = 0;
45 switch (k) {
46 case 2: //IPPCP_ML_KEM_512
47 ciphertext_size = 768;
48 break;
49 case 3: // IPPCP_ML_KEM_768
50 ciphertext_size = 1088;
51 break;
52 case 4: // IPPCP_ML_KEM_1024
53 ciphertext_size = 1568;
54 break;
55 default: // Invalid k value
56 return ippStsBadArgErr;
57 }
58
59 IppStatus sts = ippStsNoErr;
60
61 /* 1: dkPKE <- dk[0 : 384*k] */
62 const Ipp8u* pPKE_DecKey = inpDecKey;
63 /* 2: ekPKE <- dk[384*k : 768*k+32] */
64 const Ipp8u* pPKE_EncKey = inpDecKey + 384 * k;
65 /* 3: h <- dk[768*k+32 : 768*k+64] */
66 const Ipp8u* h = inpDecKey + 768 * k + 32;
67 /* 4: z <- dk[768*k+64 : 768*k+96] */
68 const Ipp8u* z = inpDecKey + 768 * k + 64;
69
70 /* 5: m` <- K-PKE.Decrypt(dkPKE, c) */
71 Ipp8u message[32];
72 IppStatus decryptSts = cp_KPKE_Decrypt(message, pPKE_DecKey, ciphertext, mlkemCtx);
73
74 /* 6: (K`, r`) <- G(m`||h) */
75
76 // stores 32 bytes of K1, 32 bytes of r and 1 byte of N
77 Ipp8u K1_r_N[CP_SHARED_SECRET_BYTES + 32 + 1];
78 Ipp8u* K1 = K1_r_N;
79 Ipp8u* r_N = K1_r_N + 32;
80
81 /* m`||h */
82 CopyBlock(message, K1_r_N, 32);
83 CopyBlock(h, K1_r_N + 32, 32);
84 /* G(m`||h) */
85 sts = ippsHashMessage_rmf(K1_r_N, 64, K1_r_N, ippsHashMethod_SHA3_512());
86 CP_CHECK_FREE_RET(sts != ippStsNoErr, sts, pStorage);
87
88 /* 7: K`` <- J(z||c) */
89 Ipp8u K2[CP_SHARED_SECRET_BYTES];
90 const IppsHashMethod* hash_method = ippsHashMethod_SHAKE256(CP_SHARED_SECRET_BYTES * 8);
91 int hash_size = 0;
92 sts = ippsHashGetSizeOptimal_rmf(&hash_size, hash_method);
93 CP_CHECK_FREE_RET(sts != ippStsNoErr, sts, pStorage);
94
95 IppsHashState_rmf* hash_state =
96 (IppsHashState_rmf*)cp_mlStorageAllocate(pStorage, hash_size + CP_ML_KEM_ALIGNMENT);
97 CP_CHECK_FREE_RET(hash_state == NULL, ippStsMemAllocErr, pStorage);
98 hash_state = IPP_ALIGNED_PTR(hash_state, CP_ML_KEM_ALIGNMENT);
99
100 sts = ippsHashInit_rmf(hash_state, hash_method);
101 CP_CHECK_FREE_RET(sts != ippStsNoErr, sts, pStorage);
102 sts = ippsHashUpdate_rmf(z, 32, hash_state);
103 CP_CHECK_FREE_RET(sts != ippStsNoErr, sts, pStorage);
104 sts = ippsHashUpdate_rmf(ciphertext, ciphertext_size, hash_state);
105 CP_CHECK_FREE_RET(sts != ippStsNoErr, sts, pStorage);
106 sts = ippsHashFinal_rmf(K2, hash_state);
107 CP_CHECK_FREE_RET(sts != ippStsNoErr, sts, pStorage);
108
109 /* 8: c` <- K-PKE.Encrypt(ekPKE, m`, r`) */
110 Ipp8u* ciphertext1 = cp_mlStorageAllocate(pStorage, ciphertext_size + CP_ML_KEM_ALIGNMENT);
111 CP_CHECK_FREE_RET(ciphertext1 == NULL, ippStsMemAllocErr, pStorage);
112 ciphertext1 = IPP_ALIGNED_PTR(ciphertext1, CP_ML_KEM_ALIGNMENT);
113 sts = cp_KPKE_Encrypt(ciphertext1, pPKE_EncKey, message, r_N, mlkemCtx);
114 CP_CHECK_FREE_RET(sts != ippStsNoErr, sts, pStorage);
115
116 /* 9-10: if c != c` then K` <- K`` */
117 BNU_CHUNK_T is_equal = cpIsEquBlock_ct(ciphertext, ciphertext1, ciphertext_size);
118 MASKED_COPY_BNU(K, is_equal, K1, K2, CP_SHARED_SECRET_BYTES);
119
120 /* Release locally used storage */
121 sts = cp_mlStorageRelease(pStorage, hash_size + CP_ML_KEM_ALIGNMENT); // hash_state
122 sts |= cp_mlStorageRelease(pStorage,
123 ciphertext_size + CP_ML_KEM_ALIGNMENT); // ciphertext1
124
125 return sts | decryptSts;
126 }