Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
cx_mlkem_util.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 "lcx_mlkem.h"
27#include "cx_mlkem_util.h"
28#include "lcx_sha3.h"
29
30void MLKEM_UTIL_hash_h(uint8_t out[CX_SHA3_256_SIZE], const uint8_t *in, size_t in_len)
31{
32 cx_sha3_256_hash(in, in_len, out);
33}
34
35void MLKEM_UTIL_hash_g(uint8_t *out, const uint8_t *in, size_t in_len)
36{
37 cx_sha3_512_hash(in, in_len, out);
38}
39
40void MLKEM_UTIL_hash_j(uint8_t *out, const uint8_t *in, size_t inlen)
41{
42 cx_shake256_hash(in, inlen, out, MLKEM_SYMBYTES);
43}
44
45void MLKEM_UTIL_prf(uint8_t *out, size_t outlen, const uint8_t *key, uint8_t nonce)
46{
47 uint8_t extkey[MLKEM_SYMBYTES + 1U] = {0};
48 (void) memcpy(extkey, key, MLKEM_SYMBYTES);
49 extkey[MLKEM_SYMBYTES] = nonce;
50 cx_shake256_hash(extkey, MLKEM_SYMBYTES + 1U, out, outlen);
51 explicit_bzero(extkey, sizeof(extkey));
52}
53
54void MLKEM_UTIL_xof_squeeze(uint8_t *out, size_t outlen, const uint8_t *seed, uint8_t x, uint8_t y)
55{
56 uint8_t extseed[MLKEM_SYMBYTES + 2U] = {0};
57 (void) memcpy(extseed, seed, MLKEM_SYMBYTES);
58 extseed[MLKEM_SYMBYTES] = x;
59 extseed[MLKEM_SYMBYTES + 1U] = y;
60 cx_shake128_hash(extseed, MLKEM_SYMBYTES + 2U, out, outlen);
61}
62
63void MLKEM_UTIL_ct_cmov(uint8_t *dst, const uint8_t *src, size_t len, uint8_t b)
64{
65 uint8_t mask = (uint8_t) (-(int8_t) (b != 0U));
66 for (size_t i = 0U; i < len; i++) {
67 dst[i] ^= mask & (dst[i] ^ src[i]);
68 }
69}
70
71cx_err_t MLKEM_UTIL_check_ek(const uint8_t *ek, const MLKEM_param_info_t *p)
72{
73 if ((p == NULL) || (ek == NULL)) {
74 return CX_INVALID_PARAMETER;
75 }
76 for (uint32_t i = 0U; i < p->k; i++) {
77 const uint8_t *a = &ek[i * MLKEM_POLYBYTES];
78 for (uint32_t j = 0U; j < (MLKEM_N / 2U); j++) {
79 uint16_t c0 = (uint16_t) (a[3U * j] | (((uint16_t) a[(3U * j) + 1U] << 8U) & 0xFFFU));
80 uint16_t c1
81 = (uint16_t) ((a[(3U * j) + 1U] >> 4U) | ((uint16_t) a[(3U * j) + 2U] << 4U));
82 if ((c0 >= MLKEM_Q) || (c1 >= MLKEM_Q)) {
83 return CX_INVALID_PARAMETER;
84 }
85 }
86 }
87 return CX_OK;
88}
void MLKEM_UTIL_hash_j(uint8_t *out, const uint8_t *in, size_t inlen)
void MLKEM_UTIL_ct_cmov(uint8_t *dst, const uint8_t *src, size_t len, uint8_t b)
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_prf(uint8_t *out, size_t outlen, const uint8_t *key, uint8_t nonce)
Computes the PRF function (SHAKE256) as defined in FIPS 203.
void MLKEM_UTIL_xof_squeeze(uint8_t *out, size_t outlen, const uint8_t *seed, uint8_t x, uint8_t y)
Computes the XOF function (SHAKE128) 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_Q
Definition lcx_mlkem.h:36
#define MLKEM_POLYBYTES
Definition lcx_mlkem.h:40
#define MLKEM_N
Definition lcx_mlkem.h:35
#define MLKEM_SYMBYTES
Definition lcx_mlkem.h:38
ML-KEM (Module-Lattice Key Encapsulation Mechanism) public API.
SHA-3 (Secure Hash Algorithm 3)
ML-KEM parameter set descriptor holding all derived sizes.
Definition lcx_mlkem.h:168