Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
cx_mlkem_internal.c
Go to the documentation of this file.
1/*****************************************************************************
2 * (c) 2026 Ledger SAS.
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 *****************************************************************************/
24#include <string.h>
25
26#include "cx_mlkem_internal.h"
27#include "cx_mlkem_indcpa.h"
28#include "cx_mlkem_util.h"
29#include "cx_utils.h"
30
32 size_t pk_len,
33 uint8_t *sk,
34 size_t sk_len,
35 const uint8_t coins[2 * MLKEM_SYMBYTES],
36 MLKEM_param_t param)
37{
38 cx_err_t error = CX_INTERNAL_ERROR;
39
40 if ((pk == NULL) || (sk == NULL)) {
41 error = CX_INVALID_PARAMETER;
42 goto end;
43 }
44
45 if (param > MLKEM_1024) {
46 error = CX_INVALID_PARAMETER_VALUE;
47 goto end;
48 }
49
50 const MLKEM_param_info_t *p = &MLKEM_PARAM[param];
51
52 if (p == NULL) {
53 error = CX_INVALID_PARAMETER;
54 goto end;
55 }
56
57 if ((pk_len < p->pk_bytes) || (sk_len < p->sk_bytes)) {
58 error = CX_INVALID_PARAMETER_SIZE;
59 goto end;
60 }
61
62 if ((p->sk_bytes < (2U * MLKEM_SYMBYTES)) || (p->indcpa_sk_bytes + p->pk_bytes > sk_len)) {
63 error = CX_INVALID_PARAMETER_SIZE;
64 goto end;
65 }
66
67 CX_CHECK(MLKEM_INDCPA_keypair_derand(pk, p->pk_bytes, sk, p->sk_bytes, coins, p));
68
69 memcpy(&sk[p->indcpa_sk_bytes], pk, p->pk_bytes);
70 MLKEM_UTIL_hash_h(&sk[p->sk_bytes - (2U * MLKEM_SYMBYTES)], pk, p->pk_bytes);
71 memcpy(&sk[p->sk_bytes - MLKEM_SYMBYTES], &coins[MLKEM_SYMBYTES], MLKEM_SYMBYTES);
72
73end:
74 return error;
75}
76
77cx_err_t MLKEM_crypto_kem_enc_derand(uint8_t *ct,
78 size_t ct_len,
79 uint8_t *ss,
80 size_t ss_len,
81 const uint8_t *pk,
82 size_t pk_len,
83 const uint8_t coins[2 * MLKEM_SYMBYTES],
84 MLKEM_param_t param)
85{
86 cx_err_t error = CX_INTERNAL_ERROR;
87 uint8_t buf[2U * MLKEM_SYMBYTES] = {0};
88 uint8_t kr[2U * MLKEM_SYMBYTES] = {0};
89
90 if ((ct == NULL) || (ss == NULL) || (pk == NULL)) {
91 error = CX_INVALID_PARAMETER;
92 goto end;
93 }
94
95 if (param > MLKEM_1024) {
96 error = CX_INVALID_PARAMETER_VALUE;
97 goto end;
98 }
99
100 const MLKEM_param_info_t *p = &MLKEM_PARAM[param];
101
102 if (p == NULL) {
103 error = CX_INVALID_PARAMETER;
104 goto end;
105 }
106
107 if ((pk_len < p->pk_bytes) || (ct_len < p->ct_bytes)) {
108 error = CX_INVALID_PARAMETER_SIZE;
109 goto end;
110 }
111
112 // FIPS 203 §7.2: validate encapsulation key modulus
113 CX_CHECK(MLKEM_UTIL_check_ek(pk, p));
114
115 memcpy(buf, coins, MLKEM_SYMBYTES);
117 MLKEM_UTIL_hash_g(kr, buf, 2U * MLKEM_SYMBYTES);
118
119 CX_CHECK(MLKEM_INDCPA_enc(
120 ct, p->ct_bytes, buf, sizeof(buf), pk, p->pk_bytes, &kr[MLKEM_SYMBYTES], p));
121
122 if (ss_len < MLKEM_SYMBYTES) {
123 error = CX_INVALID_PARAMETER_SIZE;
124 goto end;
125 }
126
127 memcpy(ss, kr, MLKEM_SYMBYTES);
128
129end:
130 explicit_bzero(buf, sizeof(buf));
131 explicit_bzero(kr, sizeof(kr));
132 return error;
133}
cx_err_t MLKEM_INDCPA_keypair_derand(uint8_t *pk, size_t pk_len, uint8_t *sk, size_t sk_len, const uint8_t coins[2 *MLKEM_SYMBYTES], const MLKEM_param_info_t *p)
cx_err_t MLKEM_INDCPA_enc(uint8_t *c, size_t c_len, const uint8_t *m, size_t m_len, const uint8_t *pk, size_t pk_len, const uint8_t coins[2 *MLKEM_SYMBYTES], const MLKEM_param_info_t *p)
cx_err_t MLKEM_crypto_kem_enc_derand(uint8_t *ct, size_t ct_len, uint8_t *ss, size_t ss_len, const uint8_t *pk, size_t pk_len, const uint8_t coins[2 *MLKEM_SYMBYTES], MLKEM_param_t param)
Deterministic ML-KEM encapsulation.
cx_err_t MLKEM_crypto_kem_keypair_derand(uint8_t *pk, size_t pk_len, uint8_t *sk, size_t sk_len, const uint8_t coins[2 *MLKEM_SYMBYTES], MLKEM_param_t param)
Deterministic ML-KEM key pair generation.
Internal ML-KEM deterministic routines (not part of the public API).
void MLKEM_UTIL_hash_g(uint8_t *out, const uint8_t *in, size_t in_len)
Computes the G hash function (SHA3-512) as defined in FIPS 203.
void MLKEM_UTIL_hash_h(uint8_t out[CX_SHA3_256_SIZE], const uint8_t *in, size_t in_len)
Computes the H hash function (SHA3-256) as defined in FIPS 203.
cx_err_t MLKEM_UTIL_check_ek(const uint8_t *ek, const MLKEM_param_info_t *p)
Validates encapsulation key modulus (FIPS 203 §7.2).
#define MLKEM_SYMBYTES
Definition lcx_mlkem.h:38
enum MLKEM_param_e MLKEM_param_t
ML-KEM parameter set selector.
const MLKEM_param_info_t MLKEM_PARAM[MLKEM_NUM_PARAM_SETS]
Lookup table of ML-KEM parameter sets indexed by MLKEM_param_t.
@ MLKEM_1024
Definition lcx_mlkem.h:162
ML-KEM parameter set descriptor holding all derived sizes.
Definition lcx_mlkem.h:168
uint16_t indcpa_sk_bytes
Definition lcx_mlkem.h:178