| 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 encapsulation key and randomness to generate a key and an associated ciphertext. |
| 28 |
* |
| 29 |
* K - output pointer to the generated shared secret key K of size 32 bytes |
| 30 |
* ciphertext - output pointer to the ciphertext of size 32*(d_{u}*k + d_{v})) bytes |
| 31 |
* inpEncKey - input pointer to the encapsulation key (public key) of size 384*k + 32 bytes |
| 32 |
* m - input parameter with generated randomness m of size 32 bytes |
| 33 |
* mlkemCtx - input pointer to ML KEM context |
| 34 |
*/ |
| 35 |
/* clang-format off */ |
| 36 |
IPP_OWN_DEFN(IppStatus, cp_MLKEMencaps_internal, (Ipp8u K[CP_SHARED_SECRET_BYTES], |
| 37 |
Ipp8u* ciphertext, |
| 38 |
const Ipp8u* inpEncKey, |
| 39 |
const Ipp8u m[CP_RAND_DATA_BYTES], |
| 40 |
IppsMLKEMState* mlkemCtx)) |
| 41 |
/* clang-format on */ |
| 42 |
{ |
| 43 |
IppStatus sts = ippStsNoErr; |
| 44 |
|
| 45 |
/* (K,𝑟) <- G(m||H(ek)) */ |
| 46 |
Ipp8u r_N[33]; |
| 47 |
Ipp8u concatData[64]; |
| 48 |
|
| 49 |
const Ipp32s ekByteSize = 384 * mlkemCtx->params.k + 32; |
| 50 |
const Ipp32s ek_pkeByteSize = ekByteSize; |
| 51 |
|
| 52 |
/* H(ek) */ |
| 53 |
sts = ippsHashMessage_rmf(inpEncKey, ek_pkeByteSize, r_N, ippsHashMethod_SHA3_256()); |
| 54 |
IPP_BADARG_RET((sts != ippStsNoErr), sts); |
| 55 |
/* m||H(ek) */ |
| 56 |
CopyBlock(m, concatData, CP_RAND_DATA_BYTES); |
| 57 |
CopyBlock(r_N, concatData + 32, 32); |
| 58 |
/* G(m||H(ek)) */ |
| 59 |
sts = ippsHashMessage_rmf(concatData, 64, concatData, ippsHashMethod_SHA3_512()); |
| 60 |
IPP_BADARG_RET((sts != ippStsNoErr), sts); |
| 61 |
|
| 62 |
CopyBlock(concatData, K, CP_SHARED_SECRET_BYTES); |
| 63 |
CopyBlock(concatData + CP_SHARED_SECRET_BYTES, r_N, 32); |
| 64 |
|
| 65 |
/* c <- K-PKE.Encrypt(ek, m, r) */ |
| 66 |
sts = cp_KPKE_Encrypt(ciphertext, inpEncKey, m, r_N, mlkemCtx); |
| 67 |
|
| 68 |
/* Clear the copy of the secret */ |
| 69 |
PurgeBlock(concatData, sizeof(concatData)); |
| 70 |
|
| 71 |
return sts; |
| 72 |
} |