| 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_Decrypt |
| 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 decryption key to decrypt a ciphertext. |
| 28 |
* |
| 29 |
* message - output pointer to the generated message of size 32 bytes |
| 30 |
* pPKE_DecKey - input pointer to the decryption key of size 384*k bytes |
| 31 |
* ciphertext - input pointer to the ciphertext of size 32*(d_{u}*k+d_{v})) bytes |
| 32 |
* mlkemCtx - input pointer to the ML KEM context |
| 33 |
*/ |
| 34 |
/* clang-format off */ |
| 35 |
IPP_OWN_DEFN(IppStatus, cp_KPKE_Decrypt, (Ipp8u * message, |
| 36 |
const Ipp8u* pPKE_DecKey, |
| 37 |
const Ipp8u* ciphertext, |
| 38 |
IppsMLKEMState* mlkemCtx)) |
| 39 |
/* clang-format on */ |
| 40 |
{ |
| 41 |
IppStatus sts = ippStsNoErr; |
| 42 |
const Ipp8u k = mlkemCtx->params.k; |
| 43 |
const Ipp16u d_u = mlkemCtx->params.d_u; |
| 44 |
const Ipp8u d_v = mlkemCtx->params.d_v; |
| 45 |
_cpMLKEMStorage* pStorage = &mlkemCtx->storage; |
| 46 |
|
| 47 |
/* Allocate memory for temporary objects */ |
| 48 |
CP_ML_ALLOCATE_ALIGNED_POLYVEC(u, k, pStorage) |
| 49 |
CP_ML_ALLOCATE_ALIGNED_POLY(v, pStorage) |
| 50 |
CP_ML_ALLOCATE_ALIGNED_POLYVEC(s, k, pStorage) |
| 51 |
CP_ML_ALLOCATE_ALIGNED_POLY(w, pStorage) |
| 52 |
|
| 53 |
/* 1: c1 <- c[0 : 32*d_{u}*k] */ |
| 54 |
Ipp8u* c1 = (Ipp8u*)ciphertext; |
| 55 |
/* 2: c2 <- c[32*d_{u}*k : 32(d_{u}*k + d_{v})] */ |
| 56 |
Ipp8u* c2 = c1 + (32 * d_u * k); |
| 57 |
|
| 58 |
/* 3: u` <- Decompress_{d_{u}}(cp_byteDecode_{d_{u}}(c1)) */ |
| 59 |
for (Ipp8u i = 0; i < k; i++) { |
| 60 |
sts = cp_byteDecode(&u[i], d_u, c1 + i * 32 * d_u, 32 * d_u * (k - i)); |
| 61 |
IPP_BADARG_RET((sts != ippStsNoErr), sts); |
| 62 |
|
| 63 |
for (Ipp32u j = 0; j < 256; j++) { |
| 64 |
sts = cp_Decompress((Ipp16u*)&u[i].values[j], u[i].values[j], d_u); |
| 65 |
IPP_BADARG_RET((sts != ippStsNoErr), sts); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/* 4: v` <- Decompress_{d_{v}}(cp_byteDecode_{d_{v}}(c2)) */ |
| 70 |
sts = cp_byteDecode(v, d_v, c2, 32 * d_v); |
| 71 |
IPP_BADARG_RET((sts != ippStsNoErr), sts); |
| 72 |
for (Ipp32u j = 0; j < 256; j++) { |
| 73 |
sts = cp_Decompress((Ipp16u*)&v->values[j], v->values[j], d_v); |
| 74 |
IPP_BADARG_RET((sts != ippStsNoErr), sts); |
| 75 |
} |
| 76 |
|
| 77 |
/* 5: s` <- cp_byteDecode_{12}(dk_{pke}) */ |
| 78 |
for (Ipp8u i = 0; i < k; i++) { |
| 79 |
sts = cp_byteDecode(&s[i], 12, pPKE_DecKey + 384 * i, 384 * (k - i)); |
| 80 |
IPP_BADARG_RET((sts != ippStsNoErr), sts); |
| 81 |
} |
| 82 |
|
| 83 |
/* 6: w <- v` - cp_NTT^{-1}(s`^{T} * cp_NTT(u`)) */ |
| 84 |
cp_NTT(&u[0]); |
| 85 |
cp_multiplyNTT(&s[0], &u[0], w); |
| 86 |
CP_ML_ALLOCATE_ALIGNED_POLY(tmpPoly, pStorage) |
| 87 |
for (Ipp8u i = 1; i < k; i++) { |
| 88 |
cp_NTT(&u[i]); |
| 89 |
cp_multiplyNTT(&s[i], &u[i], tmpPoly); |
| 90 |
cp_polyAdd(tmpPoly, w, w); |
| 91 |
} |
| 92 |
CP_ML_RELEASE_ALIGNED_POLY(pStorage, sts) // Ipp16sPoly tmpPoly |
| 93 |
cp_inverseNTT(w); |
| 94 |
cp_polySub(v, w, w); |
| 95 |
|
| 96 |
/* 7: m <- cp_byteEncode_{1}(Compress_{1}(w)) */ |
| 97 |
for (Ipp32u j = 0; j < 256; j++) { |
| 98 |
sts = cp_Compress((Ipp16u*)&w->values[j], w->values[j], 1); |
| 99 |
IPP_BADARG_RET((sts != ippStsNoErr), sts); |
| 100 |
} |
| 101 |
sts = cp_byteEncode(message, 1, w); |
| 102 |
IPP_BADARG_RET((sts != ippStsNoErr), sts); |
| 103 |
|
| 104 |
/* Release locally used storage */ |
| 105 |
CP_ML_RELEASE_ALIGNED_POLYVEC(k, pStorage, sts) // Ipp16sPoly u[k] |
| 106 |
CP_ML_RELEASE_ALIGNED_POLY(pStorage, sts) // Ipp16sPoly v |
| 107 |
CP_ML_RELEASE_ALIGNED_POLYVEC(k, pStorage, sts) // Ipp16sPoly s[k] |
| 108 |
CP_ML_RELEASE_ALIGNED_POLY(pStorage, sts) // Ipp16sPoly w |
| 109 |
|
| 110 |
return sts; |
| 111 |
} |