Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
cx_mldsa_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 *****************************************************************************/
21#include <string.h>
22#include "lcx_mldsa.h"
23#include "cx_mldsa_util.h"
24#include "lcx_sha3.h"
25#include "lcx_hash.h"
26
27void MLDSA_UTIL_shake256(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen)
28{
29 cx_shake256_hash(in, inlen, out, outlen);
30}
31
32void MLDSA_UTIL_shake128(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen)
33{
34 cx_shake128_hash(in, inlen, out, outlen);
35}
36
37void MLDSA_UTIL_shake256_two(uint8_t *out,
38 size_t outlen,
39 const uint8_t *in1,
40 size_t in1len,
41 const uint8_t *in2,
42 size_t in2len)
43{
44 uint8_t buf[256] = {0};
45 size_t total = in1len + in2len;
46
47 if (total <= sizeof(buf)) {
48 (void) memcpy(buf, in1, in1len);
49 (void) memcpy(buf + in1len, in2, in2len);
50 cx_shake256_hash(buf, total, out, outlen);
51 explicit_bzero(buf, total);
52 }
53 else {
54 cx_sha3_t ctx = {0};
55 cx_shake256_init_no_throw(&ctx, outlen * 8U);
56 cx_hash_no_throw((cx_hash_t *) &ctx, 0, in1, in1len, NULL, 0);
57 cx_hash_no_throw((cx_hash_t *) &ctx, CX_LAST, in2, in2len, out, outlen);
58 }
59}
60
61void MLDSA_UTIL_shake256_three(uint8_t *out,
62 size_t outlen,
63 const uint8_t *in1,
64 size_t in1len,
65 const uint8_t *in2,
66 size_t in2len,
67 const uint8_t *in3,
68 size_t in3len)
69{
70 cx_sha3_t ctx = {0};
71 cx_shake256_init_no_throw(&ctx, outlen * 8U);
72 cx_hash_no_throw((cx_hash_t *) &ctx, 0, in1, in1len, NULL, 0);
73 cx_hash_no_throw((cx_hash_t *) &ctx, 0, in2, in2len, NULL, 0);
74 cx_hash_no_throw((cx_hash_t *) &ctx, CX_LAST, in3, in3len, out, outlen);
75}
76
78 size_t outlen,
79 const uint8_t *seed,
80 size_t seedlen,
81 uint16_t nonce)
82{
83 uint8_t buf[MLDSA_SEEDBYTES + 2U] = {0};
84 if (seedlen > MLDSA_SEEDBYTES) {
85 seedlen = MLDSA_SEEDBYTES;
86 }
87 memcpy(buf, seed, seedlen);
88 buf[seedlen] = (uint8_t) (nonce & 0xFFU);
89 buf[seedlen + 1U] = (uint8_t) (nonce >> 8U);
90 cx_shake128_hash(buf, seedlen + 2U, out, outlen);
91 explicit_bzero(buf, sizeof(buf));
92}
93
95 size_t outlen,
96 const uint8_t *seed,
97 size_t seedlen,
98 uint16_t nonce)
99{
100 uint8_t buf[MLDSA_CRHBYTES + 2U] = {0};
101 if (seedlen > MLDSA_CRHBYTES) {
102 seedlen = MLDSA_CRHBYTES;
103 }
104 memcpy(buf, seed, seedlen);
105 buf[seedlen] = (uint8_t) (nonce & 0xFFU);
106 buf[seedlen + 1U] = (uint8_t) (nonce >> 8U);
107 cx_shake256_hash(buf, seedlen + 2U, out, outlen);
108 explicit_bzero(buf, sizeof(buf));
109}
void MLDSA_UTIL_shake256(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen)
SHAKE256 hash wrapper.
void MLDSA_UTIL_shake128(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen)
SHAKE128 hash wrapper.
void MLDSA_UTIL_shake256_seed_nonce(uint8_t *out, size_t outlen, const uint8_t *seed, size_t seedlen, uint16_t nonce)
SHAKE256 with seed || uint16_t nonce.
void MLDSA_UTIL_shake128_seed_nonce(uint8_t *out, size_t outlen, const uint8_t *seed, size_t seedlen, uint16_t nonce)
SHAKE128 with seed || uint16_t nonce.
void MLDSA_UTIL_shake256_three(uint8_t *out, size_t outlen, const uint8_t *in1, size_t in1len, const uint8_t *in2, size_t in2len, const uint8_t *in3, size_t in3len)
SHAKE256 with three inputs concatenated.
void MLDSA_UTIL_shake256_two(uint8_t *out, size_t outlen, const uint8_t *in1, size_t in1len, const uint8_t *in2, size_t in2len)
SHAKE256 with two inputs concatenated.
#define MLDSA_SEEDBYTES
Definition lcx_mldsa.h:40
#define MLDSA_CRHBYTES
Definition lcx_mldsa.h:41
#define CX_LAST
Definition lcx_common.h:115
Hash functions.
ML-DSA (Module-Lattice Digital Signature Algorithm) public API.
SHA-3 (Secure Hash Algorithm 3)