| 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 randomness to generate an encapsulation key and a corresponding decapsulation key. |
| 28 |
* outEncKey - output pointer to the output encapsulation key (public key) |
| 29 |
* outDecKey - output pointer to the output decapsulation key (private key) |
| 30 |
* d_k - input parameter with generated randomness d appended by k |
| 31 |
* z - input parameter with generated randomness z |
| 32 |
* mlkemCtx - input pointer to ML KEM context |
| 33 |
*/ |
| 34 |
/* clang-format off */ |
| 35 |
IPP_OWN_DEFN(IppStatus, cp_MLKEMkeyGen_internal, (Ipp8u * outEncKey, |
| 36 |
Ipp8u* outDecKey, |
| 37 |
const Ipp8u d_k[CP_RAND_DATA_BYTES+1], |
| 38 |
const Ipp8u z[CP_RAND_DATA_BYTES], |
| 39 |
IppsMLKEMState* mlkemCtx)) |
| 40 |
/* clang-format on */ |
| 41 |
{ |
| 42 |
IppStatus sts = ippStsNoErr; |
| 43 |
|
| 44 |
/* (ekPKE,dkPKE) <- K-PKE.KeyGen(d) */ |
| 45 |
sts = cp_KPKE_KeyGen(outEncKey, outDecKey, d_k, mlkemCtx); |
| 46 |
IPP_BADARG_RET((sts != ippStsNoErr), sts); |
| 47 |
|
| 48 |
const Ipp8u k = mlkemCtx->params.k; |
| 49 |
const cpSize ekByteSize = 384 * k + 32; |
| 50 |
const cpSize ek_pkeByteSize = ekByteSize; |
| 51 |
const cpSize dk_pkeByteSize = 384 * k; |
| 52 |
|
| 53 |
/* dk <- (dkPKE||ek||H(ek)||z) */ |
| 54 |
CopyBlock(outEncKey, outDecKey + dk_pkeByteSize, ek_pkeByteSize); |
| 55 |
sts = ippsHashMessage_rmf(outEncKey, |
| 56 |
ek_pkeByteSize, |
| 57 |
outDecKey + dk_pkeByteSize + ek_pkeByteSize, |
| 58 |
ippsHashMethod_SHA3_256()); |
| 59 |
IPP_BADARG_RET((sts != ippStsNoErr), sts); |
| 60 |
CopyBlock(z, outDecKey + dk_pkeByteSize + ek_pkeByteSize + 32, CP_RAND_DATA_BYTES); |
| 61 |
|
| 62 |
return sts; |
| 63 |
} |