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
37cx_err_t 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 cx_err_t error;
47
48 if (total <= sizeof(buf)) {
49 (void) memcpy(buf, in1, in1len);
50 (void) memcpy(buf + in1len, in2, in2len);
51 error = cx_shake256_hash(buf, total, out, outlen);
52 explicit_bzero(buf, total);
53 }
54 else {
55 cx_sha3_t ctx = {0};
56 error = cx_shake256_init_no_throw(&ctx, outlen * 8U);
57 if (error != CX_OK) {
58 return error;
59 }
60 error = cx_hash_no_throw((cx_hash_t *) &ctx, 0, in1, in1len, NULL, 0);
61 if (error != CX_OK) {
62 return error;
63 }
64 error = cx_hash_no_throw((cx_hash_t *) &ctx, CX_LAST, in2, in2len, out, outlen);
65 }
66 return error;
67}
68
69cx_err_t MLDSA_UTIL_shake256_three(uint8_t *out,
70 size_t outlen,
71 const uint8_t *in1,
72 size_t in1len,
73 const uint8_t *in2,
74 size_t in2len,
75 const uint8_t *in3,
76 size_t in3len)
77{
78 cx_sha3_t ctx = {0};
79 cx_err_t error;
80
81 error = cx_shake256_init_no_throw(&ctx, outlen * 8U);
82 if (error != CX_OK) {
83 return error;
84 }
85 error = cx_hash_no_throw((cx_hash_t *) &ctx, 0, in1, in1len, NULL, 0);
86 if (error != CX_OK) {
87 return error;
88 }
89 error = cx_hash_no_throw((cx_hash_t *) &ctx, 0, in2, in2len, NULL, 0);
90 if (error != CX_OK) {
91 return error;
92 }
93 return cx_hash_no_throw((cx_hash_t *) &ctx, CX_LAST, in3, in3len, out, outlen);
94}
95
97 size_t outlen,
98 const uint8_t *seed,
99 size_t seedlen,
100 uint16_t nonce)
101{
102 uint8_t buf[MLDSA_SEEDBYTES + 2U] = {0};
103 if (seedlen > MLDSA_SEEDBYTES) {
104 seedlen = MLDSA_SEEDBYTES;
105 }
106 memcpy(buf, seed, seedlen);
107 buf[seedlen] = (uint8_t) (nonce & 0xFFU);
108 buf[seedlen + 1U] = (uint8_t) (nonce >> 8U);
109 cx_shake128_hash(buf, seedlen + 2U, out, outlen);
110 explicit_bzero(buf, sizeof(buf));
111}
112
114 size_t outlen,
115 const uint8_t *seed,
116 size_t seedlen,
117 uint16_t nonce)
118{
119 uint8_t buf[MLDSA_CRHBYTES + 2U] = {0};
120 if (seedlen > MLDSA_CRHBYTES) {
121 seedlen = MLDSA_CRHBYTES;
122 }
123 memcpy(buf, seed, seedlen);
124 buf[seedlen] = (uint8_t) (nonce & 0xFFU);
125 buf[seedlen + 1U] = (uint8_t) (nonce >> 8U);
126 cx_shake256_hash(buf, seedlen + 2U, out, outlen);
127 explicit_bzero(buf, sizeof(buf));
128}
void MLDSA_UTIL_shake256(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen)
SHAKE256 hash wrapper.
cx_err_t 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_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.
cx_err_t 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)