Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/ml_kem/ml_kem.c
Line
Count
Source
1
/*
2
 * Copyright 2024-2026 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <openssl/byteorder.h>
11
#include <openssl/rand.h>
12
#include <openssl/proverr.h>
13
#include "crypto/ml_kem.h"
14
#include "internal/common.h"
15
#include "internal/constant_time.h"
16
#include "internal/sha3.h"
17
18
#if defined(OPENSSL_CONSTANT_TIME_VALIDATION)
19
#include <valgrind/memcheck.h>
20
#endif
21
22
#if ML_KEM_SEED_BYTES != ML_KEM_SHARED_SECRET_BYTES + ML_KEM_RANDOM_BYTES
23
#error "ML-KEM keygen seed length != shared secret + random bytes length"
24
#endif
25
#if ML_KEM_SHARED_SECRET_BYTES != ML_KEM_RANDOM_BYTES
26
#error "Invalid unequal lengths of ML-KEM shared secret and random inputs"
27
#endif
28
29
#if UINT_MAX < UINT32_MAX
30
#error "Unsupported compiler: sizeof(unsigned int) < sizeof(uint32_t)"
31
#endif
32
33
/* Handy function-like bit-extraction macros */
34
67.2M
#define bit0(b) ((b) & 1)
35
470M
#define bitn(n, b) (((b) >> n) & 1)
36
37
/*
38
 * 12 bits are sufficient to losslessly represent values in [0, q-1].
39
 * INVERSE_DEGREE is (n/2)^-1 mod q; used in inverse NTT.
40
 */
41
4.75M
#define DEGREE ML_KEM_DEGREE
42
#define INVERSE_DEGREE (ML_KEM_PRIME - 2 * 13)
43
#define LOG2PRIME 12
44
#define BARRETT_SHIFT (2 * LOG2PRIME)
45
46
#ifdef SHA3_BLOCKSIZE
47
#define SHAKE128_BLOCKSIZE SHA3_BLOCKSIZE(128)
48
#endif
49
50
/*
51
 * Return whether a value that can only be 0 or 1 is non-zero, in constant time
52
 * in practice!  The return value is a mask that is all ones if true, and all
53
 * zeros otherwise (twos-complement arithmetic assumed for unsigned values).
54
 *
55
 * Although this is used in constant-time selects, we omit a value barrier
56
 * here.  Value barriers impede auto-vectorization (likely because it forces
57
 * the value to transit through a general-purpose register). On AArch64, this
58
 * is a difference of 2x.
59
 *
60
 * We usually add value barriers to selects because Clang turns consecutive
61
 * selects with the same condition into a branch instead of CMOV/CSEL. This
62
 * condition does not occur in Kyber, so omitting it seems to be safe so far,
63
 * but see |cbd_2|, |cbd_3|, where reduction needs to be specialised to the
64
 * sign of the input, rather than adding |q| in advance, and using the generic
65
 * |reduce_once|.  (David Benjamin, Chromium)
66
 */
67
#if 0
68
#define constish_time_non_zero(b) (~constant_time_is_zero(b));
69
#else
70
1.42G
#define constish_time_non_zero(b) (0u - (b))
71
#endif
72
73
/*
74
 * The scalar rejection-sampling buffer size needs to be a multiple of 12, but
75
 * is otherwise arbitrary, the preferred block size matches the internal buffer
76
 * size of SHAKE128, avoiding internal buffering and copying in SHAKE128. That
77
 * block size of (1600 - 256)/8 bytes, or 168, just happens to divide by 12!
78
 *
79
 * If the blocksize is unknown, or is not divisible by 12, 168 is used as a
80
 * fallback.
81
 */
82
#if defined(SHAKE128_BLOCKSIZE) && (SHAKE128_BLOCKSIZE) % 12 == 0
83
#define SCALAR_SAMPLING_BUFSIZE (SHAKE128_BLOCKSIZE)
84
#else
85
#define SCALAR_SAMPLING_BUFSIZE 168
86
#endif
87
88
/*
89
 * Structure of keys
90
 */
91
typedef struct ossl_ml_kem_scalar_st {
92
    /* On every function entry and exit, 0 <= c[i] < ML_KEM_PRIME. */
93
    uint16_t c[ML_KEM_DEGREE];
94
} scalar;
95
96
/* Key material allocation layout */
97
#define DECLARE_ML_KEM_PUBKEYDATA(name, rank)                  \
98
    struct name##_alloc {                                      \
99
        /* Public vector |t| */                                \
100
        scalar tbuf[(rank)];                                   \
101
        /* Pre-computed matrix |m| (FIPS 203 |A| transpose) */ \
102
        scalar mbuf[(rank) * (rank)];                          \
103
    }
104
105
#define DECLARE_ML_KEM_PRVKEYDATA(name, rank)  \
106
    struct name##_alloc {                      \
107
        scalar sbuf[rank];                     \
108
        uint8_t zbuf[2 * ML_KEM_RANDOM_BYTES]; \
109
    }
110
111
/* Declare variant-specific public and private storage */
112
#define DECLARE_ML_KEM_VARIANT_KEYDATA(bits)                        \
113
    DECLARE_ML_KEM_PUBKEYDATA(pubkey_##bits, ML_KEM_##bits##_RANK); \
114
    DECLARE_ML_KEM_PRVKEYDATA(prvkey_##bits, ML_KEM_##bits##_RANK)
115
116
DECLARE_ML_KEM_VARIANT_KEYDATA(512);
117
DECLARE_ML_KEM_VARIANT_KEYDATA(768);
118
DECLARE_ML_KEM_VARIANT_KEYDATA(1024);
119
#undef DECLARE_ML_KEM_VARIANT_KEYDATA
120
#undef DECLARE_ML_KEM_PUBKEYDATA
121
#undef DECLARE_ML_KEM_PRVKEYDATA
122
123
typedef __owur int (*CBD_FUNC)(scalar *out, uint8_t in[ML_KEM_RANDOM_BYTES + 1],
124
    EVP_MD_CTX *mdctx, const ML_KEM_KEY *key);
125
static void scalar_encode(uint8_t *out, const scalar *s, int bits);
126
127
/*
128
 * The wire-form of a losslessly encoded vector uses 12-bits per element.
129
 *
130
 * The wire-form public key consists of the lossless encoding of the public
131
 * vector |t|, followed by the public seed |rho|.
132
 *
133
 * Our serialised private key concatenates serialisations of the private vector
134
 * |s|, the public key, the public key hash, and the failure secret |z|.
135
 */
136
#define VECTOR_BYTES(b) ((3 * DEGREE / 2) * ML_KEM_##b##_RANK)
137
#define PUBKEY_BYTES(b) (VECTOR_BYTES(b) + ML_KEM_RANDOM_BYTES)
138
#define PRVKEY_BYTES(b) (2 * PUBKEY_BYTES(b) + ML_KEM_PKHASH_BYTES)
139
140
/*
141
 * Encapsulation produces a vector "u" and a scalar "v", whose coordinates
142
 * (numbers modulo the ML-KEM prime "q") are lossily encoded using as "du" and
143
 * "dv" bits, respectively.  This encoding is the ciphertext input for
144
 * decapsulation.
145
 */
146
#define U_VECTOR_BYTES(b) ((DEGREE / 8) * ML_KEM_##b##_DU * ML_KEM_##b##_RANK)
147
#define V_SCALAR_BYTES(b) ((DEGREE / 8) * ML_KEM_##b##_DV)
148
#define CTEXT_BYTES(b) (U_VECTOR_BYTES(b) + V_SCALAR_BYTES(b))
149
150
#if defined(OPENSSL_CONSTANT_TIME_VALIDATION)
151
152
/*
153
 * CONSTTIME_SECRET takes a pointer and a number of bytes and marks that region
154
 * of memory as secret. Secret data is tracked as it flows to registers and
155
 * other parts of a memory. If secret data is used as a condition for a branch,
156
 * or as a memory index, it will trigger warnings in valgrind.
157
 */
158
#define CONSTTIME_SECRET(ptr, len) VALGRIND_MAKE_MEM_UNDEFINED(ptr, len)
159
160
/*
161
 * CONSTTIME_DECLASSIFY takes a pointer and a number of bytes and marks that
162
 * region of memory as public. Public data is not subject to constant-time
163
 * rules.
164
 */
165
#define CONSTTIME_DECLASSIFY(ptr, len) VALGRIND_MAKE_MEM_DEFINED(ptr, len)
166
167
#else
168
169
#define CONSTTIME_SECRET(ptr, len)
170
#define CONSTTIME_DECLASSIFY(ptr, len)
171
172
#endif
173
174
/*
175
 * Indices of slots in the vinfo tables below
176
 */
177
94.2k
#define ML_KEM_512_VINFO 0
178
245k
#define ML_KEM_768_VINFO 1
179
82.9k
#define ML_KEM_1024_VINFO 2
180
181
/*
182
 * Per-variant fixed parameters
183
 */
184
static const ML_KEM_VINFO vinfo_map[3] = {
185
    { "ML-KEM-512",
186
        PRVKEY_BYTES(512),
187
        sizeof(struct prvkey_512_alloc),
188
        PUBKEY_BYTES(512),
189
        sizeof(struct pubkey_512_alloc),
190
        CTEXT_BYTES(512),
191
        VECTOR_BYTES(512),
192
        U_VECTOR_BYTES(512),
193
        EVP_PKEY_ML_KEM_512,
194
        ML_KEM_512_BITS,
195
        ML_KEM_512_RANK,
196
        ML_KEM_512_DU,
197
        ML_KEM_512_DV,
198
        ML_KEM_512_SECBITS,
199
        ML_KEM_512_SECURITY_CATEGORY },
200
    { "ML-KEM-768",
201
        PRVKEY_BYTES(768),
202
        sizeof(struct prvkey_768_alloc),
203
        PUBKEY_BYTES(768),
204
        sizeof(struct pubkey_768_alloc),
205
        CTEXT_BYTES(768),
206
        VECTOR_BYTES(768),
207
        U_VECTOR_BYTES(768),
208
        EVP_PKEY_ML_KEM_768,
209
        ML_KEM_768_BITS,
210
        ML_KEM_768_RANK,
211
        ML_KEM_768_DU,
212
        ML_KEM_768_DV,
213
        ML_KEM_768_SECBITS,
214
        ML_KEM_768_SECURITY_CATEGORY },
215
    { "ML-KEM-1024",
216
        PRVKEY_BYTES(1024),
217
        sizeof(struct prvkey_1024_alloc),
218
        PUBKEY_BYTES(1024),
219
        sizeof(struct pubkey_1024_alloc),
220
        CTEXT_BYTES(1024),
221
        VECTOR_BYTES(1024),
222
        U_VECTOR_BYTES(1024),
223
        EVP_PKEY_ML_KEM_1024,
224
        ML_KEM_1024_BITS,
225
        ML_KEM_1024_RANK,
226
        ML_KEM_1024_DU,
227
        ML_KEM_1024_DV,
228
        ML_KEM_1024_SECBITS,
229
        ML_KEM_1024_SECURITY_CATEGORY }
230
};
231
232
/*
233
 * Remainders modulo `kPrime`, for sufficiently small inputs, are computed in
234
 * constant time via Barrett reduction, and a final call to reduce_once(),
235
 * which reduces inputs that are at most 2*kPrime and is also constant-time.
236
 */
237
static const int kPrime = ML_KEM_PRIME;
238
static const unsigned int kBarrettShift = BARRETT_SHIFT;
239
static const size_t kBarrettMultiplier = (1 << BARRETT_SHIFT) / ML_KEM_PRIME;
240
static const uint16_t kHalfPrime = (ML_KEM_PRIME - 1) / 2;
241
static const uint16_t kInverseDegree = INVERSE_DEGREE;
242
243
/*
244
 * Python helper:
245
 *
246
 * p = 3329
247
 * def bitreverse(i):
248
 *     ret = 0
249
 *     for n in range(7):
250
 *         bit = i & 1
251
 *         ret <<= 1
252
 *         ret |= bit
253
 *         i >>= 1
254
 *     return ret
255
 */
256
257
/*-
258
 * First precomputed array from Appendix A of FIPS 203, or else Python:
259
 * kNTTRoots = [pow(17, bitreverse(i), p) for i in range(128)]
260
 */
261
static const uint16_t kNTTRoots[128] = {
262
    0x001, 0x6c1, 0xa14, 0xcd9, 0xa52, 0x276, 0x769, 0x350,
263
    0x426, 0x77f, 0x0c1, 0x31d, 0xae2, 0xcbc, 0x239, 0x6d2,
264
    0x128, 0x98f, 0x53b, 0x5c4, 0xbe6, 0x038, 0x8c0, 0x535,
265
    0x592, 0x82e, 0x217, 0xb42, 0x959, 0xb3f, 0x7b6, 0x335,
266
    0x121, 0x14b, 0xcb5, 0x6dc, 0x4ad, 0x900, 0x8e5, 0x807,
267
    0x28a, 0x7b9, 0x9d1, 0x278, 0xb31, 0x021, 0x528, 0x77b,
268
    0x90f, 0x59b, 0x327, 0x1c4, 0x59e, 0xb34, 0x5fe, 0x962,
269
    0xa57, 0xa39, 0x5c9, 0x288, 0x9aa, 0xc26, 0x4cb, 0x38e,
270
    0x011, 0xac9, 0x247, 0xa59, 0x665, 0x2d3, 0x8f0, 0x44c,
271
    0x581, 0xa66, 0xcd1, 0x0e9, 0x2f4, 0x86c, 0xbc7, 0xbea,
272
    0x6a7, 0x673, 0xae5, 0x6fd, 0x737, 0x3b8, 0x5b5, 0xa7f,
273
    0x3ab, 0x904, 0x985, 0x954, 0x2dd, 0x921, 0x10c, 0x281,
274
    0x630, 0x8fa, 0x7f5, 0xc94, 0x177, 0x9f5, 0x82a, 0x66d,
275
    0x427, 0x13f, 0xad5, 0x2f5, 0x833, 0x231, 0x9a2, 0xa22,
276
    0xaf4, 0x444, 0x193, 0x402, 0x477, 0x866, 0xad7, 0x376,
277
    0x6ba, 0x4bc, 0x752, 0x405, 0x83e, 0xb77, 0x375, 0x86a
278
};
279
280
/*
281
 * InverseNTTRoots = [pow(17, -bitreverse(i), p) for i in range(128)]
282
 * Listed in order of use in the inverse NTT loop (index 0 is skipped):
283
 *
284
 *  0, 64, 65, ..., 127, 32, 33, ..., 63, 16, 17, ..., 31, 8, 9, ...
285
 */
286
static const uint16_t kInverseNTTRoots[128] = {
287
    0x001, 0x497, 0x98c, 0x18a, 0x4c3, 0x8fc, 0x5af, 0x845,
288
    0x647, 0x98b, 0x22a, 0x49b, 0x88a, 0x8ff, 0xb6e, 0x8bd,
289
    0x20d, 0x2df, 0x35f, 0xad0, 0x4ce, 0xa0c, 0x22c, 0xbc2,
290
    0x8da, 0x694, 0x4d7, 0x30c, 0xb8a, 0x06d, 0x50c, 0x407,
291
    0x6d1, 0xa80, 0xbf5, 0x3e0, 0xa24, 0x3ad, 0x37c, 0x3fd,
292
    0x956, 0x282, 0x74c, 0x949, 0x5ca, 0x604, 0x21c, 0x68e,
293
    0x65a, 0x117, 0x13a, 0x495, 0xa0d, 0xc18, 0x030, 0x29b,
294
    0x780, 0x8b5, 0x411, 0xa2e, 0x69c, 0x2a8, 0xaba, 0x238,
295
    0xcf0, 0x973, 0x836, 0x0db, 0x357, 0xa79, 0x738, 0x2c8,
296
    0x2aa, 0x39f, 0x703, 0x1cd, 0x763, 0xb3d, 0x9da, 0x766,
297
    0x3f2, 0x586, 0x7d9, 0xce0, 0x1d0, 0xa89, 0x330, 0x548,
298
    0xa77, 0x4fa, 0x41c, 0x401, 0x854, 0x625, 0x04c, 0xbb6,
299
    0xbe0, 0x9cc, 0x54b, 0x1c2, 0x3a8, 0x1bf, 0xaea, 0x4d3,
300
    0x76f, 0x7cc, 0x441, 0xcc9, 0x11b, 0x73d, 0x7c6, 0x372,
301
    0xbd9, 0x62f, 0xac8, 0x045, 0x21f, 0x9e4, 0xc40, 0x582,
302
    0x8db, 0x9b1, 0x598, 0xa8b, 0x2af, 0x028, 0x2ed, 0x640
303
};
304
305
/*
306
 * Second precomputed array from Appendix A of FIPS 203 (normalised positive),
307
 * or else Python:
308
 * ModRoots = [pow(17, 2*bitreverse(i) + 1, p) for i in range(128)]
309
 */
310
static const uint16_t kModRoots[128] = {
311
    0x011, 0xcf0, 0xac9, 0x238, 0x247, 0xaba, 0xa59, 0x2a8,
312
    0x665, 0x69c, 0x2d3, 0xa2e, 0x8f0, 0x411, 0x44c, 0x8b5,
313
    0x581, 0x780, 0xa66, 0x29b, 0xcd1, 0x030, 0x0e9, 0xc18,
314
    0x2f4, 0xa0d, 0x86c, 0x495, 0xbc7, 0x13a, 0xbea, 0x117,
315
    0x6a7, 0x65a, 0x673, 0x68e, 0xae5, 0x21c, 0x6fd, 0x604,
316
    0x737, 0x5ca, 0x3b8, 0x949, 0x5b5, 0x74c, 0xa7f, 0x282,
317
    0x3ab, 0x956, 0x904, 0x3fd, 0x985, 0x37c, 0x954, 0x3ad,
318
    0x2dd, 0xa24, 0x921, 0x3e0, 0x10c, 0xbf5, 0x281, 0xa80,
319
    0x630, 0x6d1, 0x8fa, 0x407, 0x7f5, 0x50c, 0xc94, 0x06d,
320
    0x177, 0xb8a, 0x9f5, 0x30c, 0x82a, 0x4d7, 0x66d, 0x694,
321
    0x427, 0x8da, 0x13f, 0xbc2, 0xad5, 0x22c, 0x2f5, 0xa0c,
322
    0x833, 0x4ce, 0x231, 0xad0, 0x9a2, 0x35f, 0xa22, 0x2df,
323
    0xaf4, 0x20d, 0x444, 0x8bd, 0x193, 0xb6e, 0x402, 0x8ff,
324
    0x477, 0x88a, 0x866, 0x49b, 0xad7, 0x22a, 0x376, 0x98b,
325
    0x6ba, 0x647, 0x4bc, 0x845, 0x752, 0x5af, 0x405, 0x8fc,
326
    0x83e, 0x4c3, 0xb77, 0x18a, 0x375, 0x98c, 0x86a, 0x497
327
};
328
329
/*
330
 * single_keccak hashes |inlen| bytes from |in| and writes |outlen| bytes of
331
 * output to |out|. If the |md| specifies a fixed-output function, like
332
 * SHA3-256, then |outlen| must be the correct length for that function.
333
 */
334
static __owur int single_keccak(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen,
335
    EVP_MD_CTX *mdctx)
336
611k
{
337
611k
    unsigned int sz = (unsigned int)outlen;
338
339
611k
    if (!EVP_DigestUpdate(mdctx, in, inlen))
340
0
        return 0;
341
611k
    if (EVP_MD_xof(EVP_MD_CTX_get0_md(mdctx)))
342
523k
        return EVP_DigestFinalXOF(mdctx, out, outlen);
343
87.6k
    return EVP_DigestFinal_ex(mdctx, out, &sz)
344
87.6k
        && ossl_assert((size_t)sz == outlen);
345
611k
}
346
347
/*
348
 * FIPS 203, Section 4.1, equation (4.3): PRF. Takes 32+1 input bytes, and uses
349
 * SHAKE256 to produce the input to SamplePolyCBD_eta: FIPS 203, algorithm 8.
350
 */
351
static __owur int prf(uint8_t *out, size_t len, const uint8_t in[ML_KEM_RANDOM_BYTES + 1],
352
    EVP_MD_CTX *mdctx, const ML_KEM_KEY *key)
353
523k
{
354
523k
    return EVP_DigestInit_ex(mdctx, key->shake256_md, NULL)
355
523k
        && single_keccak(out, len, in, ML_KEM_RANDOM_BYTES + 1, mdctx);
356
523k
}
357
358
/*
359
 * FIPS 203, Section 4.1, equation (4.4): H.  SHA3-256 hash of a variable
360
 * length input, producing 32 bytes of output.
361
 */
362
static __owur int hash_h(uint8_t out[ML_KEM_PKHASH_BYTES], const uint8_t *in, size_t len,
363
    EVP_MD_CTX *mdctx, const ML_KEM_KEY *key)
364
458
{
365
458
    return EVP_DigestInit_ex(mdctx, key->sha3_256_md, NULL)
366
458
        && single_keccak(out, ML_KEM_PKHASH_BYTES, in, len, mdctx);
367
458
}
368
369
/* Incremental hash_h of expanded public key */
370
static int
371
hash_h_pubkey(uint8_t pkhash[ML_KEM_PKHASH_BYTES],
372
    EVP_MD_CTX *mdctx, ML_KEM_KEY *key)
373
86.8k
{
374
86.8k
    const ML_KEM_VINFO *vinfo = key->vinfo;
375
86.8k
    const scalar *t = key->t, *end = t + vinfo->rank;
376
86.8k
    unsigned int sz;
377
378
86.8k
    if (!EVP_DigestInit_ex(mdctx, key->sha3_256_md, NULL))
379
0
        return 0;
380
381
260k
    do {
382
260k
        uint8_t buf[3 * DEGREE / 2];
383
384
260k
        scalar_encode(buf, t++, 12);
385
260k
        if (!EVP_DigestUpdate(mdctx, buf, sizeof(buf)))
386
0
            return 0;
387
260k
    } while (t < end);
388
389
86.8k
    if (!EVP_DigestUpdate(mdctx, key->rho, ML_KEM_RANDOM_BYTES))
390
0
        return 0;
391
86.8k
    return EVP_DigestFinal_ex(mdctx, pkhash, &sz)
392
86.8k
        && ossl_assert(sz == ML_KEM_PKHASH_BYTES);
393
86.8k
}
394
395
/*
396
 * FIPS 203, Section 4.1, equation (4.5): G.  SHA3-512 hash of a variable
397
 * length input, producing 64 bytes of output, in particular the seeds
398
 * (d,z) for key generation.
399
 */
400
static __owur int hash_g(uint8_t out[ML_KEM_SEED_BYTES], const uint8_t *in, size_t len,
401
    EVP_MD_CTX *mdctx, const ML_KEM_KEY *key)
402
87.2k
{
403
87.2k
    return EVP_DigestInit_ex(mdctx, key->sha3_512_md, NULL)
404
87.2k
        && single_keccak(out, ML_KEM_SEED_BYTES, in, len, mdctx);
405
87.2k
}
406
407
/*
408
 * FIPS 203, Section 4.1, equation (4.4): J. SHAKE256 taking a variable length
409
 * input to compute a 32-byte implicit rejection shared secret, of the same
410
 * length as the expected shared secret.  (Computed even on success to avoid
411
 * side-channel leaks).
412
 */
413
static __owur int kdf(uint8_t out[ML_KEM_SHARED_SECRET_BYTES],
414
    const uint8_t z[ML_KEM_RANDOM_BYTES],
415
    const uint8_t *ctext, size_t len,
416
    EVP_MD_CTX *mdctx, const ML_KEM_KEY *key)
417
234
{
418
234
    return EVP_DigestInit_ex(mdctx, key->shake256_md, NULL)
419
234
        && EVP_DigestUpdate(mdctx, z, ML_KEM_RANDOM_BYTES)
420
234
        && EVP_DigestUpdate(mdctx, ctext, len)
421
234
        && EVP_DigestFinalXOF(mdctx, out, ML_KEM_SHARED_SECRET_BYTES);
422
234
}
423
424
/*
425
 * FIPS 203, Section 4.2.2, Algorithm 7: "SampleNTT" (steps 3-17, steps 1, 2
426
 * are performed by the caller). Rejection-samples a Keccak stream to get
427
 * uniformly distributed elements in the range [0,q). This is used for matrix
428
 * expansion and only operates on public inputs.
429
 */
430
static __owur int sample_scalar(scalar *out, EVP_MD_CTX *mdctx)
431
784k
{
432
784k
    uint16_t *curr = out->c, *endout = curr + DEGREE;
433
784k
    uint8_t buf[SCALAR_SAMPLING_BUFSIZE], *in;
434
784k
    uint8_t *endin = buf + sizeof(buf);
435
784k
    uint16_t d;
436
784k
    uint8_t b1, b2, b3;
437
438
2.35M
    do {
439
2.35M
        if (!EVP_DigestSqueeze(mdctx, in = buf, sizeof(buf)))
440
0
            return 0;
441
122M
        do {
442
122M
            b1 = *in++;
443
122M
            b2 = *in++;
444
122M
            b3 = *in++;
445
446
122M
            if (curr >= endout)
447
263k
                break;
448
122M
            if ((d = ((b2 & 0x0f) << 8) + b1) < kPrime)
449
101M
                *curr++ = d;
450
122M
            if (curr >= endout)
451
521k
                break;
452
122M
            if ((d = (b3 << 4) + (b2 >> 4)) < kPrime)
453
99.5M
                *curr++ = d;
454
122M
        } while (in < endin);
455
2.35M
    } while (curr < endout);
456
784k
    return 1;
457
784k
}
458
459
/*-
460
 * reduce_once reduces 0 <= x < 2*kPrime, mod kPrime.
461
 *
462
 * Subtract |q| if the input is larger, without exposing a side-channel,
463
 * avoiding the "clangover" attack.  See |constish_time_non_zero| for a
464
 * discussion on why the value barrier is by default omitted.
465
 */
466
static __owur uint16_t reduce_once(uint16_t x)
467
1.29G
{
468
1.29G
    const uint16_t subtracted = x - kPrime;
469
1.29G
    uint16_t mask = constish_time_non_zero(subtracted >> 15);
470
471
1.29G
    return (mask & x) | (~mask & subtracted);
472
1.29G
}
473
474
/*
475
 * Constant-time reduce x mod kPrime using Barrett reduction. x must be less
476
 * than kPrime + 2 * kPrime^2.  This is sufficient to reduce a product of
477
 * two already reduced u_int16 values, in fact it is sufficient for each
478
 * to be less than 2^12, because (kPrime * (2 * kPrime + 1)) > 2^24.
479
 */
480
static __owur uint16_t reduce(uint32_t x)
481
584M
{
482
584M
    uint64_t product = (uint64_t)x * kBarrettMultiplier;
483
584M
    uint32_t quotient = (uint32_t)(product >> kBarrettShift);
484
584M
    uint32_t remainder = x - quotient * kPrime;
485
486
584M
    return reduce_once(remainder);
487
584M
}
488
489
/* Multiply a scalar by a constant. */
490
static void scalar_mult_const(scalar *s, uint16_t a)
491
1.69k
{
492
1.69k
    uint16_t *curr = s->c, *end = curr + DEGREE, tmp;
493
494
433k
    do {
495
433k
        tmp = reduce(*curr * a);
496
433k
        *curr++ = tmp;
497
433k
    } while (curr < end);
498
1.69k
}
499
500
/*-
501
 * FIPS 203, Section 4.3, Algorithm 9: "NTT".
502
 * In-place number theoretic transform of a given scalar.  Note that ML-KEM's
503
 * kPrime 3329 does not have a 512th root of unity, so this transform leaves
504
 * off the last iteration of the usual FFT code, with the 128 relevant roots of
505
 * unity being stored in NTTRoots.  This means the output should be seen as 128
506
 * elements in GF(3329^2), with the coefficients of the elements being
507
 * consecutive entries in |s->c|.
508
 */
509
static void scalar_ntt(scalar *s)
510
394k
{
511
394k
    const uint16_t *roots = kNTTRoots;
512
394k
    uint16_t *end = s->c + DEGREE;
513
394k
    int offset = DEGREE / 2;
514
515
2.76M
    do {
516
2.76M
        uint16_t *curr = s->c, *peer;
517
518
50.1M
        do {
519
50.1M
            uint16_t *pause = curr + offset, even, odd;
520
50.1M
            uint32_t zeta = *++roots;
521
522
50.1M
            peer = pause;
523
353M
            do {
524
353M
                even = *curr;
525
353M
                odd = reduce(*peer * zeta);
526
353M
                *peer++ = reduce_once(even - odd + kPrime);
527
353M
                *curr++ = reduce_once(odd + even);
528
353M
            } while (curr < pause);
529
50.1M
        } while ((curr = peer) < end);
530
2.76M
    } while ((offset >>= 1) >= 2);
531
394k
}
532
533
/*-
534
 * FIPS 203, Section 4.3, Algorithm 10: "NTT^(-1)".
535
 * In-place inverse number theoretic transform of a given scalar, with pairs of
536
 * entries of s->v being interpreted as elements of GF(3329^2). Just as with
537
 * the number theoretic transform, this leaves off the first step of the normal
538
 * iFFT to account for the fact that 3329 does not have a 512th root of unity,
539
 * using the precomputed 128 roots of unity stored in InverseNTTRoots.
540
 */
541
static void scalar_inverse_ntt(scalar *s)
542
1.69k
{
543
1.69k
    const uint16_t *roots = kInverseNTTRoots;
544
1.69k
    uint16_t *end = s->c + DEGREE;
545
1.69k
    int offset = 2;
546
547
11.8k
    do {
548
11.8k
        uint16_t *curr = s->c, *peer;
549
550
214k
        do {
551
214k
            uint16_t *pause = curr + offset, even, odd;
552
214k
            uint32_t zeta = *++roots;
553
554
214k
            peer = pause;
555
1.51M
            do {
556
1.51M
                even = *curr;
557
1.51M
                odd = *peer;
558
1.51M
                *peer++ = reduce(zeta * (even - odd + kPrime));
559
1.51M
                *curr++ = reduce_once(odd + even);
560
1.51M
            } while (curr < pause);
561
214k
        } while ((curr = peer) < end);
562
11.8k
    } while ((offset <<= 1) < DEGREE);
563
1.69k
    scalar_mult_const(s, kInverseDegree);
564
1.69k
}
565
566
/* Addition updating the LHS scalar in-place. */
567
static void scalar_add(scalar *lhs, const scalar *rhs)
568
1.74k
{
569
1.74k
    int i;
570
571
447k
    for (i = 0; i < DEGREE; i++)
572
445k
        lhs->c[i] = reduce_once(lhs->c[i] + rhs->c[i]);
573
1.74k
}
574
575
/* Subtraction updating the LHS scalar in-place. */
576
static void scalar_sub(scalar *lhs, const scalar *rhs)
577
234
{
578
234
    int i;
579
580
60.1k
    for (i = 0; i < DEGREE; i++)
581
59.9k
        lhs->c[i] = reduce_once(lhs->c[i] - rhs->c[i] + kPrime);
582
234
}
583
584
/*
585
 * Multiplying two scalars in the number theoretically transformed state. Since
586
 * 3329 does not have a 512th root of unity, this means we have to interpret
587
 * the 2*ith and (2*i+1)th entries of the scalar as elements of
588
 * GF(3329)[X]/(X^2 - 17^(2*bitreverse(i)+1)).
589
 *
590
 * The value of 17^(2*bitreverse(i)+1) mod 3329 is stored in the precomputed
591
 * ModRoots table. Note that our Barrett transform only allows us to multiply
592
 * two reduced numbers together, so we need some intermediate reduction steps,
593
 * even if an uint64_t could hold 3 multiplied numbers.
594
 */
595
static void scalar_mult(scalar *out, const scalar *lhs,
596
    const scalar *rhs)
597
1.69k
{
598
1.69k
    uint16_t *curr = out->c, *end = curr + DEGREE;
599
1.69k
    const uint16_t *lc = lhs->c, *rc = rhs->c;
600
1.69k
    const uint16_t *roots = kModRoots;
601
602
216k
    do {
603
216k
        uint32_t l0 = *lc++, r0 = *rc++;
604
216k
        uint32_t l1 = *lc++, r1 = *rc++;
605
216k
        uint32_t zetapow = *roots++;
606
607
216k
        *curr++ = reduce(l0 * r0 + reduce(l1 * r1) * zetapow);
608
216k
        *curr++ = reduce(l0 * r1 + l1 * r0);
609
216k
    } while (curr < end);
610
1.69k
}
611
612
/* Above, but add the result to an existing scalar */
613
static ossl_inline void scalar_mult_add(scalar *out, const scalar *lhs,
614
    const scalar *rhs)
615
593k
{
616
593k
    uint16_t *curr = out->c, *end = curr + DEGREE;
617
593k
    const uint16_t *lc = lhs->c, *rc = rhs->c;
618
593k
    const uint16_t *roots = kModRoots;
619
620
75.9M
    do {
621
75.9M
        uint32_t l0 = *lc++, r0 = *rc++;
622
75.9M
        uint32_t l1 = *lc++, r1 = *rc++;
623
75.9M
        uint16_t *c0 = curr++;
624
75.9M
        uint16_t *c1 = curr++;
625
75.9M
        uint32_t zetapow = *roots++;
626
627
75.9M
        *c0 = reduce(*c0 + l0 * r0 + reduce(l1 * r1) * zetapow);
628
75.9M
        *c1 = reduce(*c1 + l0 * r1 + l1 * r0);
629
75.9M
    } while (curr < end);
630
593k
}
631
632
/*-
633
 * FIPS 203, Section 4.2.1, Algorithm 5: "ByteEncode_d", for 2<=d<=12.
634
 * Here |bits| is |d|.  For efficiency, we handle the d=1 case separately.
635
 */
636
static void scalar_encode(uint8_t *out, const scalar *s, int bits)
637
527k
{
638
527k
    const uint16_t *curr = s->c, *end = curr + DEGREE;
639
527k
    uint64_t accum = 0, element;
640
527k
    int used = 0;
641
642
134M
    do {
643
134M
        element = *curr++;
644
134M
        if (used + bits < 64) {
645
109M
            accum |= element << used;
646
109M
            used += bits;
647
109M
        } else if (used + bits > 64) {
648
16.8M
            out = OPENSSL_store_u64_le(out, accum | (element << used));
649
16.8M
            accum = element >> (64 - used);
650
16.8M
            used = (used + bits) - 64;
651
16.8M
        } else {
652
8.42M
            out = OPENSSL_store_u64_le(out, accum | (element << used));
653
8.42M
            accum = 0;
654
8.42M
            used = 0;
655
8.42M
        }
656
134M
    } while (curr < end);
657
527k
}
658
659
/*
660
 * scalar_encode_1 is |scalar_encode| specialised for |bits| == 1.
661
 */
662
static void scalar_encode_1(uint8_t out[DEGREE / 8], const scalar *s)
663
234
{
664
234
    int i, j;
665
234
    uint8_t out_byte;
666
667
7.72k
    for (i = 0; i < DEGREE; i += 8) {
668
7.48k
        out_byte = 0;
669
67.3k
        for (j = 0; j < 8; j++)
670
59.9k
            out_byte |= bit0(s->c[i + j]) << j;
671
7.48k
        *out = out_byte;
672
7.48k
        out++;
673
7.48k
    }
674
234
}
675
676
/*-
677
 * FIPS 203, Section 4.2.1, Algorithm 6: "ByteDecode_d", for 2<=d<12.
678
 * Here |bits| is |d|.  For efficiency, we handle the d=1 and d=12 cases
679
 * separately.
680
 *
681
 * scalar_decode parses |DEGREE * bits| bits from |in| into |DEGREE| values in
682
 * |out|.
683
 */
684
static void scalar_decode(scalar *out, const uint8_t *in, int bits)
685
896
{
686
896
    uint16_t *curr = out->c, *end = curr + DEGREE;
687
896
    uint64_t accum = 0;
688
896
    int accum_bits = 0, todo = bits;
689
896
    uint16_t bitmask = (((uint16_t)1) << bits) - 1, mask = bitmask;
690
896
    uint16_t element = 0;
691
692
252k
    do {
693
252k
        if (accum_bits == 0) {
694
31.1k
            in = OPENSSL_load_u64_le(&accum, in);
695
31.1k
            accum_bits = 64;
696
31.1k
        }
697
252k
        if (todo == bits && accum_bits >= bits) {
698
            /* No partial "element", and all the required bits available */
699
206k
            *curr++ = ((uint16_t)accum) & mask;
700
206k
            accum >>= bits;
701
206k
            accum_bits -= bits;
702
206k
        } else if (accum_bits >= todo) {
703
            /* A partial "element", and all the required bits available */
704
23.3k
            *curr++ = element | ((((uint16_t)accum) & mask) << (bits - todo));
705
23.3k
            accum >>= todo;
706
23.3k
            accum_bits -= todo;
707
23.3k
            element = 0;
708
23.3k
            todo = bits;
709
23.3k
            mask = bitmask;
710
23.3k
        } else {
711
            /*
712
             * Only some of the requisite bits accumulated, store |accum_bits|
713
             * of these in |element|.  The accumulated bitcount becomes 0, but
714
             * as soon as we have more bits we'll want to merge accum_bits
715
             * fewer of them into the final |element|.
716
             *
717
             * Note that with a 64-bit accumulator and |bits| always 12 or
718
             * less, if we're here, the previous iteration had all the
719
             * requisite bits, and so there are no kept bits in |element|.
720
             */
721
23.3k
            element = ((uint16_t)accum) & mask;
722
23.3k
            todo -= accum_bits;
723
23.3k
            mask = bitmask >> accum_bits;
724
23.3k
            accum_bits = 0;
725
23.3k
        }
726
252k
    } while (curr < end);
727
896
}
728
729
static __owur int scalar_decode_12(scalar *out, const uint8_t in[3 * DEGREE / 2])
730
1.72k
{
731
1.72k
    int i;
732
1.72k
    uint16_t *c = out->c;
733
734
175k
    for (i = 0; i < DEGREE / 2; ++i) {
735
174k
        uint8_t b1 = *in++;
736
174k
        uint8_t b2 = *in++;
737
174k
        uint8_t b3 = *in++;
738
174k
        int outOfRange1 = (*c++ = b1 | ((b2 & 0x0f) << 8)) >= kPrime;
739
174k
        int outOfRange2 = (*c++ = (b2 >> 4) | (b3 << 4)) >= kPrime;
740
741
174k
        if (outOfRange1 | outOfRange2)
742
449
            return 0;
743
174k
    }
744
1.27k
    return 1;
745
1.72k
}
746
747
/*-
748
 * scalar_decode_decompress_add is a combination of decoding and decompression
749
 * both specialised for |bits| == 1, with the result added (and sum reduced) to
750
 * the output scalar.
751
 *
752
 * NOTE: this function MUST not leak an input-data-depedennt timing signal.
753
 * A timing leak in a related function in the reference Kyber implementation
754
 * made the "clangover" attack (CVE-2024-37880) possible, giving key recovery
755
 * for ML-KEM-512 in minutes, provided the attacker has access to precise
756
 * timing of a CPU performing chosen-ciphertext decap.  Admittedly this is only
757
 * a risk when private keys are reused (perhaps KEMTLS servers).
758
 */
759
static void
760
scalar_decode_decompress_add(scalar *out, const uint8_t in[DEGREE / 8])
761
455
{
762
455
    static const uint16_t half_q_plus_1 = (ML_KEM_PRIME >> 1) + 1;
763
455
    uint16_t *curr = out->c, *end = curr + DEGREE;
764
455
    uint16_t mask;
765
455
    uint8_t b;
766
767
    /*
768
     * Add |half_q_plus_1| if the bit is set, without exposing a side-channel,
769
     * avoiding the "clangover" attack.  See |constish_time_non_zero| for a
770
     * discussion on why the value barrier is by default omitted.
771
     */
772
455
#define decode_decompress_add_bit                        \
773
116k
    mask = constish_time_non_zero(bit0(b));              \
774
116k
    *curr = reduce_once(*curr + (mask & half_q_plus_1)); \
775
116k
    curr++;                                              \
776
116k
    b >>= 1
777
778
    /* Unrolled to process each byte in one iteration */
779
14.5k
    do {
780
14.5k
        b = *in++;
781
14.5k
        decode_decompress_add_bit;
782
14.5k
        decode_decompress_add_bit;
783
14.5k
        decode_decompress_add_bit;
784
14.5k
        decode_decompress_add_bit;
785
786
14.5k
        decode_decompress_add_bit;
787
14.5k
        decode_decompress_add_bit;
788
14.5k
        decode_decompress_add_bit;
789
14.5k
        decode_decompress_add_bit;
790
14.5k
    } while (curr < end);
791
455
#undef decode_decompress_add_bit
792
455
}
793
794
/*
795
 * FIPS 203, Section 4.2.1, Equation (4.7): Compress_d.
796
 *
797
 * Compresses (lossily) an input |x| mod 3329 into |bits| many bits by grouping
798
 * numbers close to each other together. The formula used is
799
 * round(2^|bits|/kPrime*x) mod 2^|bits|.
800
 * Uses Barrett reduction to achieve constant time. Since we need both the
801
 * remainder (for rounding) and the quotient (as the result), we cannot use
802
 * |reduce| here, but need to do the Barrett reduction directly.
803
 */
804
static __owur uint16_t compress(uint16_t x, int bits)
805
505k
{
806
505k
    uint32_t shifted = (uint32_t)x << bits;
807
505k
    uint64_t product = (uint64_t)shifted * kBarrettMultiplier;
808
505k
    uint32_t quotient = (uint32_t)(product >> kBarrettShift);
809
505k
    uint32_t remainder = shifted - quotient * kPrime;
810
811
    /*
812
     * Adjust the quotient to round correctly:
813
     *   0 <= remainder <= kHalfPrime round to 0
814
     *   kHalfPrime < remainder <= kPrime + kHalfPrime round to 1
815
     *   kPrime + kHalfPrime < remainder < 2 * kPrime round to 2
816
     */
817
505k
    quotient += 1 & constant_time_lt_32(kHalfPrime, remainder);
818
505k
    quotient += 1 & constant_time_lt_32(kPrime + kHalfPrime, remainder);
819
505k
    return quotient & ((1 << bits) - 1);
820
505k
}
821
822
/*
823
 * FIPS 203, Section 4.2.1, Equation (4.8): Decompress_d.
824
825
 * Decompresses |x| by using a close equi-distant representative. The formula
826
 * is round(kPrime/2^|bits|*x). Note that 2^|bits| being the divisor allows us
827
 * to implement this logic using only bit operations.
828
 */
829
static __owur uint16_t decompress(uint16_t x, int bits)
830
229k
{
831
229k
    uint32_t product = (uint32_t)x * kPrime;
832
229k
    uint32_t power = 1 << bits;
833
    /* This is |product| % power, since |power| is a power of 2. */
834
229k
    uint32_t remainder = product & (power - 1);
835
    /* This is |product| / power, since |power| is a power of 2. */
836
229k
    uint32_t lower = product >> bits;
837
838
    /*
839
     * The rounding logic works since the first half of numbers mod |power|
840
     * have a 0 as first bit, and the second half has a 1 as first bit, since
841
     * |power| is a power of 2. As a 12 bit number, |remainder| is always
842
     * positive, so we will shift in 0s for a right shift.
843
     */
844
229k
    return lower + (remainder >> (bits - 1));
845
229k
}
846
847
/*-
848
 * FIPS 203, Section 4.2.1, Equation (4.7): "Compress_d".
849
 * In-place lossy rounding of scalars to 2^d bits.
850
 */
851
static void scalar_compress(scalar *s, int bits)
852
1.97k
{
853
1.97k
    int i;
854
855
507k
    for (i = 0; i < DEGREE; i++)
856
505k
        s->c[i] = compress(s->c[i], bits);
857
1.97k
}
858
859
/*
860
 * FIPS 203, Section 4.2.1, Equation (4.8): "Decompress_d".
861
 * In-place approximate recovery of scalars from 2^d bit compression.
862
 */
863
static void scalar_decompress(scalar *s, int bits)
864
896
{
865
896
    int i;
866
867
230k
    for (i = 0; i < DEGREE; i++)
868
229k
        s->c[i] = decompress(s->c[i], bits);
869
896
}
870
871
/* Addition updating the LHS vector in-place. */
872
static void vector_add(scalar *lhs, const scalar *rhs, int rank)
873
455
{
874
1.28k
    do {
875
1.28k
        scalar_add(lhs++, rhs++);
876
1.28k
    } while (--rank > 0);
877
455
}
878
879
/*
880
 * Encodes an entire vector into 32*|rank|*|bits| bytes. Note that since 256
881
 * (DEGREE) is divisible by 8, the individual vector entries will always fill a
882
 * whole number of bytes, so we do not need to worry about bit packing here.
883
 */
884
static void vector_encode(uint8_t *out, const scalar *a, int bits, int rank)
885
88.8k
{
886
88.8k
    int stride = bits * DEGREE / 8;
887
888
355k
    for (; rank-- > 0; out += stride)
889
266k
        scalar_encode(out, a++, bits);
890
88.8k
}
891
892
/*
893
 * Decodes 32*|rank|*|bits| bytes from |in| into |out|. It returns early
894
 * if any parsed value is >= |ML_KEM_PRIME|.  The resulting scalars are
895
 * then decompressed and transformed via the NTT.
896
 *
897
 * Note: Used only in decrypt_cpa(), which returns void and so does not check
898
 * the return value of this function.  Side-channels are fine when the input
899
 * ciphertext to decap() is simply syntactically invalid.
900
 */
901
static void
902
vector_decode_decompress_ntt(scalar *out, const uint8_t *in, int bits, int rank)
903
234
{
904
234
    int stride = bits * DEGREE / 8;
905
906
896
    for (; rank-- > 0; in += stride, ++out) {
907
662
        scalar_decode(out, in, bits);
908
662
        scalar_decompress(out, bits);
909
662
        scalar_ntt(out);
910
662
    }
911
234
}
912
913
/* vector_decode(), specialised to bits == 12. */
914
static __owur int vector_decode_12(scalar *out, const uint8_t in[3 * DEGREE / 2], int rank)
915
929
{
916
929
    int stride = 3 * DEGREE / 2;
917
918
2.20k
    for (; rank-- > 0; in += stride)
919
1.72k
        if (!scalar_decode_12(out++, in))
920
449
            return 0;
921
480
    return 1;
922
929
}
923
924
/* In-place compression of each scalar component */
925
static void vector_compress(scalar *a, int bits, int rank)
926
455
{
927
1.28k
    do {
928
1.28k
        scalar_compress(a++, bits);
929
1.28k
    } while (--rank > 0);
930
455
}
931
932
/* The output scalar must not overlap with the inputs */
933
static void inner_product(scalar *out, const scalar *lhs, const scalar *rhs,
934
    int rank)
935
599
{
936
599
    scalar_mult(out, lhs, rhs);
937
1.68k
    while (--rank > 0)
938
1.08k
        scalar_mult_add(out, ++lhs, ++rhs);
939
599
}
940
941
/*
942
 * Here, the output vector must not overlap with the inputs, the result is
943
 * directly subjected to inverse NTT.
944
 */
945
static void
946
matrix_mult_intt(scalar *out, const scalar *m, const scalar *a, int rank)
947
389
{
948
389
    const scalar *ar;
949
389
    int i, j;
950
951
1.48k
    for (i = rank; i-- > 0; ++out) {
952
1.09k
        scalar_mult(out, m++, ar = a);
953
3.29k
        for (j = rank - 1; j > 0; --j)
954
2.19k
            scalar_mult_add(out, m++, ++ar);
955
1.09k
        scalar_inverse_ntt(out);
956
1.09k
    }
957
389
}
958
959
/* Here, the output vector must not overlap with the inputs */
960
static void
961
matrix_mult_transpose_add(scalar *out, const scalar *m, const scalar *a, int rank)
962
65.5k
{
963
65.5k
    const scalar *mc = m, *mr, *ar;
964
65.5k
    int i, j;
965
966
262k
    for (i = rank; i-- > 0; ++out) {
967
196k
        scalar_mult_add(out, mr = mc++, ar = a);
968
589k
        for (j = rank; --j > 0;)
969
393k
            scalar_mult_add(out, (mr += rank), ++ar);
970
196k
    }
971
65.5k
}
972
973
/*-
974
 * Expands the matrix from a seed for key generation and for encaps-CPA.
975
 * NOTE: FIPS 203 matrix "A" is the transpose of this matrix, computed
976
 * by appending the (i,j) indices to the seed in the opposite order!
977
 *
978
 * Where FIPS 203 computes t = A * s + e, we use the transpose of "m".
979
 */
980
static __owur int matrix_expand(EVP_MD_CTX *mdctx, ML_KEM_KEY *key)
981
87.3k
{
982
87.3k
    scalar *out = key->m;
983
87.3k
    uint8_t input[ML_KEM_RANDOM_BYTES + 2];
984
87.3k
    int rank = key->vinfo->rank;
985
87.3k
    int i, j;
986
987
    /*
988
     * The seeds derived below and the sampling buffers in sample_scalar()
989
     * are not cleansed: per FIPS 203 section 3.3 the matrix A is easily
990
     * computed from the public encapsulation key and does not require any
991
     * special protections.
992
     */
993
87.3k
    memcpy(input, key->rho, ML_KEM_RANDOM_BYTES);
994
348k
    for (i = 0; i < rank; i++) {
995
1.04M
        for (j = 0; j < rank; j++) {
996
784k
            input[ML_KEM_RANDOM_BYTES] = i;
997
784k
            input[ML_KEM_RANDOM_BYTES + 1] = j;
998
784k
            if (!EVP_DigestInit_ex(mdctx, key->shake128_md, NULL)
999
784k
                || !EVP_DigestUpdate(mdctx, input, sizeof(input))
1000
784k
                || !sample_scalar(out++, mdctx))
1001
0
                return 0;
1002
784k
        }
1003
261k
    }
1004
87.3k
    return 1;
1005
87.3k
}
1006
1007
/*
1008
 * Algorithm 7 from the spec, with eta fixed to two and the PRF call
1009
 * included. Creates binominally distributed elements by sampling 2*|eta| bits,
1010
 * and setting the coefficient to the count of the first bits minus the count of
1011
 * the second bits, resulting in a centered binomial distribution. Since eta is
1012
 * two this gives -2/2 with a probability of 1/16, -1/1 with probability 1/4,
1013
 * and 0 with probability 3/8.
1014
 */
1015
static __owur int cbd_2(scalar *out, uint8_t in[ML_KEM_RANDOM_BYTES + 1],
1016
    EVP_MD_CTX *mdctx, const ML_KEM_KEY *key)
1017
521k
{
1018
521k
    uint16_t *curr = out->c, *end = curr + DEGREE;
1019
521k
    uint8_t randbuf[4 * DEGREE / 8], *r = randbuf; /* 64 * eta slots */
1020
521k
    uint16_t value, mask;
1021
521k
    uint8_t b;
1022
1023
521k
    if (!prf(randbuf, sizeof(randbuf), in, mdctx, key)) {
1024
0
        OPENSSL_cleanse((void *)randbuf, sizeof(randbuf));
1025
0
        return 0;
1026
0
    }
1027
1028
66.8M
    do {
1029
66.8M
        b = *r++;
1030
1031
        /*
1032
         * Add |kPrime| if |value| underflowed.  See |constish_time_non_zero|
1033
         * for a discussion on why the value barrier is by default omitted.
1034
         * While this could have been written reduce_once(value + kPrime), this
1035
         * is one extra addition and small range of |value| tempts some
1036
         * versions of Clang to emit a branch.
1037
         */
1038
66.8M
        value = bit0(b) + bitn(1, b);
1039
66.8M
        value -= bitn(2, b) + bitn(3, b);
1040
66.8M
        mask = constish_time_non_zero(value >> 15);
1041
66.8M
        *curr++ = value + (kPrime & mask);
1042
1043
66.8M
        value = bitn(4, b) + bitn(5, b);
1044
66.8M
        value -= bitn(6, b) + bitn(7, b);
1045
66.8M
        mask = constish_time_non_zero(value >> 15);
1046
66.8M
        *curr++ = value + (kPrime & mask);
1047
66.8M
    } while (curr < end);
1048
1049
521k
    OPENSSL_cleanse((void *)randbuf, sizeof(randbuf));
1050
521k
    return 1;
1051
521k
}
1052
1053
/*
1054
 * Algorithm 7 from the spec, with eta fixed to three and the PRF call
1055
 * included. Creates binominally distributed elements by sampling 3*|eta| bits,
1056
 * and setting the coefficient to the count of the first bits minus the count of
1057
 * the second bits, resulting in a centered binomial distribution.
1058
 */
1059
static __owur int cbd_3(scalar *out, uint8_t in[ML_KEM_RANDOM_BYTES + 1],
1060
    EVP_MD_CTX *mdctx, const ML_KEM_KEY *key)
1061
1.98k
{
1062
1.98k
    uint16_t *curr = out->c, *end = curr + DEGREE;
1063
1.98k
    uint8_t randbuf[6 * DEGREE / 8], *r = randbuf; /* 64 * eta slots */
1064
1.98k
    uint8_t b1, b2, b3;
1065
1.98k
    uint16_t value, mask;
1066
1067
1.98k
    if (!prf(randbuf, sizeof(randbuf), in, mdctx, key)) {
1068
0
        OPENSSL_cleanse((void *)randbuf, sizeof(randbuf));
1069
0
        return 0;
1070
0
    }
1071
1072
126k
    do {
1073
126k
        b1 = *r++;
1074
126k
        b2 = *r++;
1075
126k
        b3 = *r++;
1076
1077
        /*
1078
         * Add |kPrime| if |value| underflowed.  See |constish_time_non_zero|
1079
         * for a discussion on why the value barrier is by default omitted.
1080
         * While this could have been written reduce_once(value + kPrime), this
1081
         * is one extra addition and small range of |value| tempts some
1082
         * versions of Clang to emit a branch.
1083
         */
1084
126k
        value = bit0(b1) + bitn(1, b1) + bitn(2, b1);
1085
126k
        value -= bitn(3, b1) + bitn(4, b1) + bitn(5, b1);
1086
126k
        mask = constish_time_non_zero(value >> 15);
1087
126k
        *curr++ = value + (kPrime & mask);
1088
1089
126k
        value = bitn(6, b1) + bitn(7, b1) + bit0(b2);
1090
126k
        value -= bitn(1, b2) + bitn(2, b2) + bitn(3, b2);
1091
126k
        mask = constish_time_non_zero(value >> 15);
1092
126k
        *curr++ = value + (kPrime & mask);
1093
1094
126k
        value = bitn(4, b2) + bitn(5, b2) + bitn(6, b2);
1095
126k
        value -= bitn(7, b2) + bit0(b3) + bitn(1, b3);
1096
126k
        mask = constish_time_non_zero(value >> 15);
1097
126k
        *curr++ = value + (kPrime & mask);
1098
1099
126k
        value = bitn(2, b3) + bitn(3, b3) + bitn(4, b3);
1100
126k
        value -= bitn(5, b3) + bitn(6, b3) + bitn(7, b3);
1101
126k
        mask = constish_time_non_zero(value >> 15);
1102
126k
        *curr++ = value + (kPrime & mask);
1103
126k
    } while (curr < end);
1104
1105
1.98k
    OPENSSL_cleanse((void *)randbuf, sizeof(randbuf));
1106
1.98k
    return 1;
1107
1.98k
}
1108
1109
/*
1110
 * Generates a secret vector by using |cbd| with the given seed to generate
1111
 * scalar elements and incrementing |counter| for each slot of the vector.
1112
 */
1113
static __owur int gencbd_vector(scalar *out, CBD_FUNC cbd, uint8_t *counter,
1114
    const uint8_t seed[ML_KEM_RANDOM_BYTES], int rank,
1115
    EVP_MD_CTX *mdctx, const ML_KEM_KEY *key)
1116
455
{
1117
455
    uint8_t input[ML_KEM_RANDOM_BYTES + 1];
1118
455
    int ret = 0;
1119
1120
455
    memcpy(input, seed, ML_KEM_RANDOM_BYTES);
1121
1.28k
    do {
1122
1.28k
        input[ML_KEM_RANDOM_BYTES] = (*counter)++;
1123
1.28k
        if (!cbd(out++, input, mdctx, key))
1124
0
            goto end;
1125
1.28k
    } while (--rank > 0);
1126
455
    ret = 1;
1127
1128
455
end:
1129
455
    OPENSSL_cleanse((void *)input, sizeof(input));
1130
455
    return ret;
1131
455
}
1132
1133
/*
1134
 * As above plus NTT transform.
1135
 */
1136
static __owur int gencbd_vector_ntt(scalar *out, CBD_FUNC cbd, uint8_t *counter,
1137
    const uint8_t seed[ML_KEM_RANDOM_BYTES], int rank,
1138
    EVP_MD_CTX *mdctx, const ML_KEM_KEY *key)
1139
174k
{
1140
174k
    uint8_t input[ML_KEM_RANDOM_BYTES + 1];
1141
174k
    int ret = 0;
1142
1143
174k
    memcpy(input, seed, ML_KEM_RANDOM_BYTES);
1144
522k
    do {
1145
522k
        input[ML_KEM_RANDOM_BYTES] = (*counter)++;
1146
522k
        if (!cbd(out, input, mdctx, key))
1147
0
            goto end;
1148
522k
        scalar_ntt(out++);
1149
522k
    } while (--rank > 0);
1150
174k
    ret = 1;
1151
1152
174k
end:
1153
174k
    OPENSSL_cleanse((void *)input, sizeof(input));
1154
174k
    return ret;
1155
174k
}
1156
1157
/* The |ETA1| value for ML-KEM-512 is 3, the rest and all ETA2 values are 2. */
1158
55.7k
#define CBD1(evp_type) ((evp_type) == EVP_PKEY_ML_KEM_512 ? cbd_3 : cbd_2)
1159
1160
/*
1161
 * FIPS 203, Section 5.2, Algorithm 14: K-PKE.Encrypt.
1162
 *
1163
 * Encrypts a message with given randomness to the ciphertext in |out|. Without
1164
 * applying the Fujisaki-Okamoto transform this would not result in a CCA
1165
 * secure scheme, since lattice schemes are vulnerable to decryption failure
1166
 * oracles.
1167
 *
1168
 * The steps are re-ordered to make more efficient/localised use of storage.
1169
 *
1170
 * Note also that the input public key is assumed to hold a precomputed matrix
1171
 * |A| (our key->m, with the public key holding an expanded (16-bit per scalar
1172
 * coefficient) key->t vector).
1173
 *
1174
 * Caller passes storage in |tmp| for for two temporary vectors.
1175
 */
1176
static __owur int encrypt_cpa(uint8_t out[ML_KEM_SHARED_SECRET_BYTES],
1177
    const uint8_t message[DEGREE / 8],
1178
    const uint8_t r[ML_KEM_RANDOM_BYTES], scalar *tmp,
1179
    EVP_MD_CTX *mdctx, const ML_KEM_KEY *key)
1180
455
{
1181
455
    const ML_KEM_VINFO *vinfo = key->vinfo;
1182
455
    CBD_FUNC cbd_1 = CBD1(vinfo->evp_type);
1183
455
    int rank = vinfo->rank;
1184
    /* We can use tmp[0..rank-1] as storage for |y|, then |e1|, ... */
1185
455
    scalar *y = &tmp[0], *e1 = y, *e2 = y;
1186
    /* We can use tmp[rank]..tmp[2*rank - 1] for |u| */
1187
455
    scalar *u = &tmp[rank];
1188
455
    scalar v;
1189
455
    uint8_t input[ML_KEM_RANDOM_BYTES + 1];
1190
455
    uint8_t counter = 0;
1191
455
    int du = vinfo->du;
1192
455
    int dv = vinfo->dv;
1193
455
    int ret = 0;
1194
1195
    /* FIPS 203 "y" vector */
1196
455
    if (!gencbd_vector_ntt(y, cbd_1, &counter, r, rank, mdctx, key))
1197
0
        goto end;
1198
    /* FIPS 203 "v" scalar */
1199
455
    inner_product(&v, key->t, y, rank);
1200
455
    scalar_inverse_ntt(&v);
1201
    /* FIPS 203 "u" vector */
1202
455
    matrix_mult_intt(u, key->m, y, rank);
1203
1204
    /* All done with |y|, now free to reuse tmp[0] for FIPS 203 |e1| */
1205
455
    if (!gencbd_vector(e1, cbd_2, &counter, r, rank, mdctx, key))
1206
0
        goto end;
1207
455
    vector_add(u, e1, rank);
1208
455
    vector_compress(u, du, rank);
1209
455
    vector_encode(out, u, du, rank);
1210
1211
    /* All done with |e1|, now free to reuse tmp[0] for FIPS 203 |e2| */
1212
455
    memcpy(input, r, ML_KEM_RANDOM_BYTES);
1213
455
    input[ML_KEM_RANDOM_BYTES] = counter;
1214
455
    if (!cbd_2(e2, input, mdctx, key))
1215
0
        goto end;
1216
455
    scalar_add(&v, e2);
1217
1218
    /* Combine message with |v| */
1219
455
    scalar_decode_decompress_add(&v, message);
1220
455
    scalar_compress(&v, dv);
1221
455
    scalar_encode(out + vinfo->u_vector_bytes, &v, dv);
1222
455
    ret = 1;
1223
1224
455
end:
1225
455
    OPENSSL_cleanse((void *)input, sizeof(input));
1226
455
    OPENSSL_cleanse((void *)&v, sizeof(v));
1227
455
    return ret;
1228
455
}
1229
1230
/*
1231
 * FIPS 203, Section 5.3, Algorithm 15: K-PKE.Decrypt.
1232
 */
1233
static void
1234
decrypt_cpa(uint8_t out[ML_KEM_SHARED_SECRET_BYTES],
1235
    const uint8_t *ctext, scalar *u, const ML_KEM_KEY *key)
1236
234
{
1237
234
    const ML_KEM_VINFO *vinfo = key->vinfo;
1238
234
    scalar v, mask;
1239
234
    int rank = vinfo->rank;
1240
234
    int du = vinfo->du;
1241
234
    int dv = vinfo->dv;
1242
1243
234
    vector_decode_decompress_ntt(u, ctext, du, rank);
1244
234
    scalar_decode(&v, ctext + vinfo->u_vector_bytes, dv);
1245
234
    scalar_decompress(&v, dv);
1246
234
    inner_product(&mask, key->s, u, rank);
1247
234
    scalar_inverse_ntt(&mask);
1248
234
    scalar_sub(&v, &mask);
1249
234
    scalar_compress(&v, 1);
1250
234
    scalar_encode_1(out, &v);
1251
1252
234
    OPENSSL_cleanse((void *)&v, sizeof(v));
1253
234
    OPENSSL_cleanse((void *)&mask, sizeof(mask));
1254
234
}
1255
1256
/*-
1257
 * FIPS 203, Section 7.1, Algorithm 19: "ML-KEM.KeyGen".
1258
 * FIPS 203, Section 7.2, Algorithm 20: "ML-KEM.Encaps".
1259
 *
1260
 * Fills the |out| buffer with the |ek| output of "ML-KEM.KeyGen", or,
1261
 * equivalently, the |ek| input of "ML-KEM.Encaps", i.e. returns the
1262
 * wire-format of an ML-KEM public key.
1263
 */
1264
static void encode_pubkey(uint8_t *out, const ML_KEM_KEY *key)
1265
88.2k
{
1266
88.2k
    const uint8_t *rho = key->rho;
1267
88.2k
    const ML_KEM_VINFO *vinfo = key->vinfo;
1268
1269
88.2k
    vector_encode(out, key->t, 12, vinfo->rank);
1270
88.2k
    memcpy(out + vinfo->vector_bytes, rho, ML_KEM_RANDOM_BYTES);
1271
88.2k
}
1272
1273
/*-
1274
 * FIPS 203, Section 7.1, Algorithm 19: "ML-KEM.KeyGen".
1275
 *
1276
 * Fills the |out| buffer with the |dk| output of "ML-KEM.KeyGen".
1277
 * This matches the input format of parse_prvkey() below.
1278
 */
1279
static void encode_prvkey(uint8_t *out, const ML_KEM_KEY *key)
1280
173
{
1281
173
    const ML_KEM_VINFO *vinfo = key->vinfo;
1282
1283
173
    vector_encode(out, key->s, 12, vinfo->rank);
1284
173
    out += vinfo->vector_bytes;
1285
173
    encode_pubkey(out, key);
1286
173
    out += vinfo->pubkey_bytes;
1287
173
    memcpy(out, key->pkhash, ML_KEM_PKHASH_BYTES);
1288
173
    out += ML_KEM_PKHASH_BYTES;
1289
173
    memcpy(out, key->z, ML_KEM_RANDOM_BYTES);
1290
173
}
1291
1292
/*-
1293
 * FIPS 203, Section 7.1, Algorithm 19: "ML-KEM.KeyGen".
1294
 * FIPS 203, Section 7.2, Algorithm 20: "ML-KEM.Encaps".
1295
 *
1296
 * This function parses the |in| buffer as the |ek| output of "ML-KEM.KeyGen",
1297
 * or, equivalently, the |ek| input of "ML-KEM.Encaps", i.e. decodes the
1298
 * wire-format of the ML-KEM public key.
1299
 */
1300
static int parse_pubkey(const uint8_t *in, EVP_MD_CTX *mdctx, ML_KEM_KEY *key)
1301
792
{
1302
792
    const ML_KEM_VINFO *vinfo = key->vinfo;
1303
1304
    /* Decode and check |t| */
1305
792
    if (!vector_decode_12(key->t, in, vinfo->rank)) {
1306
334
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
1307
334
            "%s invalid public 't' vector",
1308
334
            vinfo->algorithm_name);
1309
334
        return 0;
1310
334
    }
1311
    /* Save the matrix |m| recovery seed |rho| */
1312
458
    memcpy(key->rho, in + vinfo->vector_bytes, ML_KEM_RANDOM_BYTES);
1313
    /*
1314
     * Pre-compute the public key hash, needed for both encap and decap.
1315
     * Also pre-compute the matrix expansion, stored with the public key.
1316
     */
1317
458
    if (!hash_h(key->pkhash, in, vinfo->pubkey_bytes, mdctx, key)
1318
458
        || !matrix_expand(mdctx, key)) {
1319
0
        ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR,
1320
0
            "internal error while parsing %s public key",
1321
0
            vinfo->algorithm_name);
1322
0
        return 0;
1323
0
    }
1324
458
    return 1;
1325
458
}
1326
1327
/*
1328
 * FIPS 203, Section 7.1, Algorithm 19: "ML-KEM.KeyGen".
1329
 *
1330
 * Parses the |in| buffer as a |dk| output of "ML-KEM.KeyGen".
1331
 * This matches the output format of encode_prvkey() above.
1332
 */
1333
static int parse_prvkey(const uint8_t *in, EVP_MD_CTX *mdctx, ML_KEM_KEY *key)
1334
137
{
1335
137
    const ML_KEM_VINFO *vinfo = key->vinfo;
1336
1337
    /* Decode and check |s|. */
1338
137
    if (!vector_decode_12(key->s, in, vinfo->rank)) {
1339
115
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
1340
115
            "%s invalid private 's' vector",
1341
115
            vinfo->algorithm_name);
1342
115
        return 0;
1343
115
    }
1344
22
    in += vinfo->vector_bytes;
1345
1346
22
    if (!parse_pubkey(in, mdctx, key))
1347
13
        return 0;
1348
9
    in += vinfo->pubkey_bytes;
1349
1350
    /* Check public key hash. */
1351
9
    if (memcmp(key->pkhash, in, ML_KEM_PKHASH_BYTES) != 0) {
1352
9
        ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,
1353
9
            "%s public key hash mismatch",
1354
9
            vinfo->algorithm_name);
1355
9
        return 0;
1356
9
    }
1357
0
    in += ML_KEM_PKHASH_BYTES;
1358
1359
0
    memcpy(key->z, in, ML_KEM_RANDOM_BYTES);
1360
0
    return 1;
1361
9
}
1362
1363
/*
1364
 * FIPS 203, Section 6.1, Algorithm 16: "ML-KEM.KeyGen_internal".
1365
 *
1366
 * The implementation of Section 5.1, Algorithm 13, "K-PKE.KeyGen(d)" is
1367
 * inlined.
1368
 *
1369
 * The caller MUST pass a pre-allocated digest context that is not shared with
1370
 * any concurrent computation.
1371
 *
1372
 * This function optionally outputs the serialised wire-form |ek| public key
1373
 * into the provided |pubenc| buffer, and generates the content of the |rho|,
1374
 * |pkhash|, |t|, |m|, |s| and |z| components of the private |key| (which must
1375
 * have preallocated space for these).
1376
 *
1377
 * Keys are computed from a 32-byte random |d| plus the 1 byte rank for
1378
 * domain separation.  These are concatenated and hashed to produce a pair of
1379
 * 32-byte seeds public "rho", used to generate the matrix, and private "sigma",
1380
 * used to generate the secret vector |s|.
1381
 *
1382
 * The second random input |z| is copied verbatim into the Fujisaki-Okamoto
1383
 * (FO) transform "implicit-rejection" secret (the |z| component of the private
1384
 * key), which thwarts chosen-ciphertext attacks, provided decap() runs in
1385
 * constant time, with no side channel leaks, on all well-formed (valid length,
1386
 * and correctly encoded) ciphertext inputs.
1387
 */
1388
static __owur int genkey(const uint8_t seed[ML_KEM_SEED_BYTES],
1389
    EVP_MD_CTX *mdctx, uint8_t *pubenc, ML_KEM_KEY *key)
1390
55.3k
{
1391
55.3k
    uint8_t hashed[2 * ML_KEM_RANDOM_BYTES];
1392
55.3k
    const uint8_t *const sigma = hashed + ML_KEM_RANDOM_BYTES;
1393
55.3k
    uint8_t augmented_seed[ML_KEM_RANDOM_BYTES + 1];
1394
55.3k
    const ML_KEM_VINFO *vinfo = key->vinfo;
1395
55.3k
    CBD_FUNC cbd_1 = CBD1(vinfo->evp_type);
1396
55.3k
    int rank = vinfo->rank;
1397
55.3k
    uint8_t counter = 0;
1398
55.3k
    int ret = 0;
1399
1400
    /*
1401
     * Use the "d" seed salted with the rank to derive the public and private
1402
     * seeds rho and sigma.
1403
     */
1404
55.3k
    memcpy(augmented_seed, seed, ML_KEM_RANDOM_BYTES);
1405
55.3k
    augmented_seed[ML_KEM_RANDOM_BYTES] = (uint8_t)rank;
1406
55.3k
    if (!hash_g(hashed, augmented_seed, sizeof(augmented_seed), mdctx, key))
1407
0
        goto end;
1408
55.3k
    memcpy(key->rho, hashed, ML_KEM_RANDOM_BYTES);
1409
    /* The |rho| matrix seed is public */
1410
55.3k
    CONSTTIME_DECLASSIFY(key->rho, ML_KEM_RANDOM_BYTES);
1411
1412
    /* FIPS 203 |e| vector is initial value of key->t */
1413
55.3k
    if (!matrix_expand(mdctx, key)
1414
55.3k
        || !gencbd_vector_ntt(key->s, cbd_1, &counter, sigma, rank, mdctx, key)
1415
55.3k
        || !gencbd_vector_ntt(key->t, cbd_1, &counter, sigma, rank, mdctx, key))
1416
0
        goto end;
1417
1418
    /* To |e| we now add the product of transpose |m| and |s|, giving |t|. */
1419
55.3k
    matrix_mult_transpose_add(key->t, key->m, key->s, rank);
1420
    /* The |t| vector is public */
1421
55.3k
    CONSTTIME_DECLASSIFY(key->t, vinfo->rank * sizeof(scalar));
1422
1423
55.3k
    if (pubenc == NULL) {
1424
        /* Incremental digest of public key without in-full serialisation. */
1425
55.3k
        if (!hash_h_pubkey(key->pkhash, mdctx, key))
1426
0
            goto end;
1427
55.3k
    } else {
1428
0
        encode_pubkey(pubenc, key);
1429
0
        if (!hash_h(key->pkhash, pubenc, vinfo->pubkey_bytes, mdctx, key))
1430
0
            goto end;
1431
0
    }
1432
1433
    /* Save |z| portion of seed for "implicit rejection" on failure. */
1434
55.3k
    memcpy(key->z, seed + ML_KEM_RANDOM_BYTES, ML_KEM_RANDOM_BYTES);
1435
1436
    /* Save the |d| portion of the seed */
1437
55.3k
    key->d = key->z + ML_KEM_RANDOM_BYTES;
1438
55.3k
    memcpy(key->d, seed, ML_KEM_RANDOM_BYTES);
1439
1440
55.3k
    ret = 1;
1441
55.3k
end:
1442
55.3k
    OPENSSL_cleanse((void *)augmented_seed, sizeof(augmented_seed));
1443
55.3k
    OPENSSL_cleanse((void *)hashed, sizeof(hashed));
1444
55.3k
    if (ret == 0) {
1445
0
        ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR,
1446
0
            "internal error while generating %s private key",
1447
0
            vinfo->algorithm_name);
1448
0
    }
1449
55.3k
    return ret;
1450
55.3k
}
1451
1452
/*-
1453
 * FIPS 203, Section 6.2, Algorithm 17: "ML-KEM.Encaps_internal".
1454
 * This is the deterministic version with randomness supplied externally.
1455
 *
1456
 * The caller must pass space for two vectors in |tmp|.
1457
 * The |ctext| buffer have space for the ciphertext of the ML-KEM variant
1458
 * of the provided key.
1459
 */
1460
static int encap(uint8_t *ctext, uint8_t secret[ML_KEM_SHARED_SECRET_BYTES],
1461
    const uint8_t entropy[ML_KEM_RANDOM_BYTES],
1462
    scalar *tmp, EVP_MD_CTX *mdctx, const ML_KEM_KEY *key)
1463
221
{
1464
221
    uint8_t input[ML_KEM_RANDOM_BYTES + ML_KEM_PKHASH_BYTES];
1465
221
    uint8_t Kr[ML_KEM_SHARED_SECRET_BYTES + ML_KEM_RANDOM_BYTES];
1466
221
    uint8_t *r = Kr + ML_KEM_SHARED_SECRET_BYTES;
1467
221
    int ret;
1468
1469
221
    memcpy(input, entropy, ML_KEM_RANDOM_BYTES);
1470
221
    memcpy(input + ML_KEM_RANDOM_BYTES, key->pkhash, ML_KEM_PKHASH_BYTES);
1471
221
    ret = hash_g(Kr, input, sizeof(input), mdctx, key)
1472
221
        && encrypt_cpa(ctext, entropy, r, tmp, mdctx, key);
1473
221
    OPENSSL_cleanse((void *)input, sizeof(input));
1474
1475
221
    if (ret)
1476
221
        memcpy(secret, Kr, ML_KEM_SHARED_SECRET_BYTES);
1477
0
    else
1478
0
        ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR,
1479
0
            "internal error while performing %s encapsulation",
1480
0
            key->vinfo->algorithm_name);
1481
221
    OPENSSL_cleanse((void *)Kr, sizeof(Kr));
1482
221
    return ret;
1483
221
}
1484
1485
/*
1486
 * Hash the input message |m'| and public key digest |h|
1487
 * to obtain |K| and |r|.
1488
 */
1489
static int hash_kr(uint8_t *out, uint8_t *in,
1490
    EVP_MD_CTX *mdctx, const ML_KEM_KEY *key)
1491
135
{
1492
135
    unsigned int sz, wanted;
1493
1494
135
    wanted = ML_KEM_SHARED_SECRET_BYTES + ML_KEM_RANDOM_BYTES;
1495
135
    return (EVP_DigestInit_ex(mdctx, key->sha3_512_md, NULL)
1496
135
        && EVP_DigestUpdate(mdctx, in, ML_KEM_RANDOM_BYTES)
1497
135
        && EVP_DigestUpdate(mdctx, key->pkhash, ML_KEM_PKHASH_BYTES)
1498
135
        && EVP_DigestFinal_ex(mdctx, out, &sz)
1499
135
        && ossl_assert(sz == wanted));
1500
135
}
1501
1502
/*-
1503
 * Decap needs space for: Kbar | K | r | m'
1504
 * We slice up a single buffer to hold them all.
1505
 * We don't need to cleanse the public pkhash value.
1506
 */
1507
37
#define DECAP_BUFFER_SZ (2 * ML_KEM_SHARED_SECRET_BYTES + 2 * ML_KEM_RANDOM_BYTES)
1508
1509
/*
1510
 * FIPS 203, Section 6.3, Algorithm 18: ML-KEM.Decaps_internal
1511
 *
1512
 * Barring failure of the supporting SHA3/SHAKE primitives, this is fully
1513
 * deterministic, the randomness for the FO transform is extracted during
1514
 * private key generation.
1515
 *
1516
 * The caller must pass space for two vectors in |tmp|.
1517
 * The |ctext| and |tmp_ctext| buffers must each have space for the ciphertext
1518
 * of the key's ML-KEM variant.
1519
 */
1520
static int decap(uint8_t secret[ML_KEM_SHARED_SECRET_BYTES],
1521
    const uint8_t *ctext, uint8_t *tmp_ctext, scalar *tmp,
1522
    EVP_MD_CTX *mdctx, const ML_KEM_KEY *key)
1523
37
{
1524
37
    uint8_t buf[DECAP_BUFFER_SZ];
1525
37
    uint8_t *failure_key = buf; /* Kbar */
1526
37
    uint8_t *Kr = failure_key + ML_KEM_SHARED_SECRET_BYTES;
1527
37
    uint8_t *r = Kr + ML_KEM_SHARED_SECRET_BYTES;
1528
37
    uint8_t *m = r + ML_KEM_RANDOM_BYTES; /* m' */
1529
37
    const ML_KEM_VINFO *vinfo = key->vinfo;
1530
37
    int i;
1531
37
    uint8_t mask;
1532
1533
    /*
1534
     * If our KDF is unavailable, fail early! Otherwise, keep going ignoring
1535
     * any further errors, returning success, and whatever we got for a shared
1536
     * secret.  The decrypt_cpa() function is just arithmetic on secret data,
1537
     * so should not be subject to failure that makes its output predictable.
1538
     *
1539
     * We guard against "should never happen" catastrophic failure of the
1540
     * "pure" function |hash_g| by overwriting the shared secret with the
1541
     * content of the failure key and returning early, if nevertheless hash_g
1542
     * fails.  This is not constant-time, but a failure of |hash_g| already
1543
     * implies loss of side-channel resistance.
1544
     *
1545
     * The same action is taken, if also |encrypt_cpa| should catastrophically
1546
     * fail, due to failure of the |PRF| underlying the CBD functions.
1547
     */
1548
37
    if (!kdf(failure_key, key->z, ctext, vinfo->ctext_bytes, mdctx, key)) {
1549
0
        ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR,
1550
0
            "internal error while performing %s decapsulation",
1551
0
            vinfo->algorithm_name);
1552
0
        OPENSSL_cleanse(buf, DECAP_BUFFER_SZ);
1553
0
        return 0;
1554
0
    }
1555
37
    decrypt_cpa(m, ctext, tmp, key);
1556
37
    if (!hash_kr(Kr, m, mdctx, key)
1557
37
        || !encrypt_cpa(tmp_ctext, m, r, tmp, mdctx, key)) {
1558
0
        memcpy(secret, failure_key, ML_KEM_SHARED_SECRET_BYTES);
1559
0
        goto end;
1560
0
    }
1561
37
    mask = constant_time_eq_int_8(0,
1562
37
        CRYPTO_memcmp(ctext, tmp_ctext, vinfo->ctext_bytes));
1563
1.22k
    for (i = 0; i < ML_KEM_SHARED_SECRET_BYTES; i++)
1564
1.18k
        secret[i] = constant_time_select_8(mask, Kr[i], failure_key[i]);
1565
37
end:
1566
37
    OPENSSL_cleanse(buf, DECAP_BUFFER_SZ);
1567
37
    return 1;
1568
37
}
1569
1570
/*
1571
 * After allocating storage for public or private key data, update the key
1572
 * component pointers to reference that storage.
1573
 *
1574
 * The caller should only store private data in `priv` *after* a successful
1575
 * (non-zero) return from this function.
1576
 */
1577
static __owur int add_storage(scalar *pub, scalar *priv,
1578
    int private, int dup, ML_KEM_KEY *key)
1579
72.3k
{
1580
72.3k
    int rank = key->vinfo->rank;
1581
1582
72.3k
    if (pub == NULL || (private && priv == NULL)) {
1583
        /*
1584
         * One of these could be allocated correctly. It is legal to call free with a NULL
1585
         * pointer, so always attempt to free both allocations here
1586
         */
1587
0
        OPENSSL_free(pub);
1588
0
        OPENSSL_secure_free(priv);
1589
0
        return 0;
1590
0
    }
1591
1592
    /*
1593
     * We're adding key material, set up rho and pkhash to point to the
1594
     * rho_pkhash buffer.  Zero the key hash when creating fresh keys.
1595
     */
1596
72.3k
    if (dup == 0)
1597
72.2k
        memset(key->rho_pkhash, 0, sizeof(key->rho_pkhash));
1598
72.3k
    key->rho = key->rho_pkhash;
1599
72.3k
    key->pkhash = key->rho_pkhash + ML_KEM_RANDOM_BYTES;
1600
72.3k
    key->d = key->z = NULL;
1601
1602
    /* A public key needs space for |t| and |m| */
1603
72.3k
    key->m = (key->t = pub) + rank;
1604
1605
    /*
1606
     * A private key also needs space for |s| and |z|.
1607
     * The |z| buffer always includes additional space for |d|, but a key's |d|
1608
     * pointer is left NULL when parsed from the NIST format, which omits that
1609
     * information.  Only keys generated from a (d, z) seed pair will have a
1610
     * non-NULL |d| pointer.
1611
     */
1612
72.3k
    if (private)
1613
71.5k
        key->z = (uint8_t *)(rank + (key->s = priv));
1614
72.3k
    return 1;
1615
72.3k
}
1616
1617
/*
1618
 * After freeing the storage associated with a key that failed to be
1619
 * constructed, reset the internal pointers back to NULL.
1620
 */
1621
void ossl_ml_kem_key_reset(ML_KEM_KEY *key)
1622
72.9k
{
1623
    /*
1624
     * seedbuf can be allocated and contain |z| and |d| if the key is
1625
     * being created from a private key encoding.  Similarly a pending
1626
     * serialised (encoded) private key may be queued up to load.
1627
     * Clear and free that data now.
1628
     */
1629
72.9k
    if (key->seedbuf != NULL)
1630
24
        OPENSSL_secure_clear_free(key->seedbuf, ML_KEM_SEED_BYTES);
1631
72.9k
    if (ossl_ml_kem_have_dkenc(key))
1632
0
        OPENSSL_secure_clear_free(key->encoded_dk, key->vinfo->prvkey_bytes);
1633
1634
    /*-
1635
     * Cleanse any sensitive data:
1636
     * - The private vector |s| is immediately followed by the FO failure
1637
     *   secret |z|, and seed |d|, we can cleanse all three in one call.
1638
     */
1639
72.9k
    if (key->t != NULL) {
1640
72.3k
        if (ossl_ml_kem_have_prvkey(key))
1641
71.5k
            OPENSSL_secure_clear_free(key->s, key->vinfo->prvalloc);
1642
72.3k
        OPENSSL_free(key->t);
1643
72.3k
    }
1644
72.9k
    key->d = key->z = key->seedbuf = key->encoded_dk = (uint8_t *)(key->s = key->m = key->t = NULL);
1645
72.9k
}
1646
1647
/*
1648
 * ----- API exported to the provider
1649
 *
1650
 * Parameters with an implicit fixed length in the internal static API of each
1651
 * variant have an explicit checked length argument at this layer.
1652
 */
1653
1654
/* Retrieve the parameters of one of the ML-KEM variants */
1655
const ML_KEM_VINFO *ossl_ml_kem_get_vinfo(int evp_type)
1656
422k
{
1657
422k
    switch (evp_type) {
1658
94.2k
    case EVP_PKEY_ML_KEM_512:
1659
94.2k
        return &vinfo_map[ML_KEM_512_VINFO];
1660
245k
    case EVP_PKEY_ML_KEM_768:
1661
245k
        return &vinfo_map[ML_KEM_768_VINFO];
1662
82.9k
    case EVP_PKEY_ML_KEM_1024:
1663
82.9k
        return &vinfo_map[ML_KEM_1024_VINFO];
1664
422k
    }
1665
0
    return NULL;
1666
422k
}
1667
1668
ML_KEM_KEY *ossl_ml_kem_key_new(OSSL_LIB_CTX *libctx, const char *properties,
1669
    int evp_type)
1670
46.7k
{
1671
46.7k
    const ML_KEM_VINFO *vinfo = ossl_ml_kem_get_vinfo(evp_type);
1672
46.7k
    ML_KEM_KEY *key;
1673
1674
46.7k
    if (vinfo == NULL) {
1675
0
        ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT,
1676
0
            "unsupported ML-KEM key type: %d", evp_type);
1677
0
        return NULL;
1678
0
    }
1679
1680
46.7k
    if ((key = OPENSSL_malloc(sizeof(*key))) == NULL)
1681
0
        return NULL;
1682
1683
46.7k
    key->vinfo = vinfo;
1684
46.7k
    key->libctx = libctx;
1685
46.7k
    key->prov_flags = ML_KEM_KEY_PROV_FLAGS_DEFAULT;
1686
46.7k
    key->shake128_md = EVP_MD_fetch(libctx, "SHAKE128", properties);
1687
46.7k
    key->shake256_md = EVP_MD_fetch(libctx, "SHAKE256", properties);
1688
46.7k
    key->sha3_256_md = EVP_MD_fetch(libctx, "SHA3-256", properties);
1689
46.7k
    key->sha3_512_md = EVP_MD_fetch(libctx, "SHA3-512", properties);
1690
46.7k
    key->d = key->z = key->rho = key->pkhash = key->encoded_dk = key->seedbuf = NULL;
1691
46.7k
    key->s = key->m = key->t = NULL;
1692
1693
46.7k
    if (key->shake128_md != NULL
1694
46.7k
        && key->shake256_md != NULL
1695
46.7k
        && key->sha3_256_md != NULL
1696
46.7k
        && key->sha3_512_md != NULL)
1697
46.7k
        return key;
1698
1699
0
    ossl_ml_kem_key_free(key);
1700
0
    ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR,
1701
0
        "missing SHA3 digest algorithms while creating %s key",
1702
0
        vinfo->algorithm_name);
1703
0
    return NULL;
1704
46.7k
}
1705
1706
ML_KEM_KEY *ossl_ml_kem_key_dup(const ML_KEM_KEY *key, int selection)
1707
102
{
1708
102
    int ok = 0;
1709
102
    ML_KEM_KEY *ret;
1710
1711
102
    if (key == NULL)
1712
0
        return NULL;
1713
    /*
1714
     * Partially decoded keys, not yet imported or loaded, should never be
1715
     * duplicated.
1716
     */
1717
102
    if (ossl_ml_kem_decoded_key(key))
1718
0
        return NULL;
1719
1720
102
    else if ((ret = OPENSSL_memdup(key, sizeof(*key))) == NULL)
1721
0
        return NULL;
1722
1723
102
    ret->d = ret->z = ret->rho = ret->pkhash = NULL;
1724
102
    ret->s = ret->m = ret->t = NULL;
1725
1726
    /* Clear selection bits we can't fulfill */
1727
102
    if (!ossl_ml_kem_have_pubkey(key))
1728
0
        selection = 0;
1729
102
    else if (!ossl_ml_kem_have_prvkey(key))
1730
102
        selection &= ~OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
1731
0
    else if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
1732
0
        selection &= ~OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
1733
1734
102
    switch (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) {
1735
0
    case 0:
1736
0
        ok = 1;
1737
0
        break;
1738
102
    case OSSL_KEYMGMT_SELECT_PUBLIC_KEY:
1739
102
        ok = add_storage(OPENSSL_memdup(key->t, key->vinfo->puballoc), NULL, 0, 1, ret);
1740
102
        break;
1741
0
    case OSSL_KEYMGMT_SELECT_PRIVATE_KEY:
1742
        /* Frees both and returns 0 if either is NULL */
1743
0
        ok = add_storage(OPENSSL_memdup(key->t, key->vinfo->puballoc),
1744
0
            OPENSSL_secure_malloc(key->vinfo->prvalloc), 1, 1, ret);
1745
0
        if (ok) {
1746
0
            memcpy(ret->s, key->s, key->vinfo->prvalloc);
1747
1748
            /* Duplicated keys retain |d|, if available */
1749
0
            if (key->d != NULL)
1750
0
                ret->d = ret->z + ML_KEM_RANDOM_BYTES;
1751
0
        }
1752
0
        break;
1753
102
    }
1754
1755
102
    if (!ok) {
1756
0
        OPENSSL_free(ret);
1757
0
        return NULL;
1758
0
    }
1759
1760
102
    EVP_MD_up_ref(ret->shake128_md);
1761
102
    EVP_MD_up_ref(ret->shake256_md);
1762
102
    EVP_MD_up_ref(ret->sha3_256_md);
1763
102
    EVP_MD_up_ref(ret->sha3_512_md);
1764
1765
102
    return ret;
1766
102
}
1767
1768
void ossl_ml_kem_key_free(ML_KEM_KEY *key)
1769
309k
{
1770
309k
    if (key == NULL)
1771
237k
        return;
1772
1773
72.5k
    EVP_MD_free(key->shake128_md);
1774
72.5k
    EVP_MD_free(key->shake256_md);
1775
72.5k
    EVP_MD_free(key->sha3_256_md);
1776
72.5k
    EVP_MD_free(key->sha3_512_md);
1777
1778
72.5k
    ossl_ml_kem_key_reset(key);
1779
72.5k
    OPENSSL_free(key);
1780
72.5k
}
1781
1782
/* Serialise the public component of an ML-KEM key */
1783
int ossl_ml_kem_encode_public_key(uint8_t *out, size_t len,
1784
    const ML_KEM_KEY *key)
1785
88.0k
{
1786
88.0k
    if (!ossl_ml_kem_have_pubkey(key)
1787
88.0k
        || len != key->vinfo->pubkey_bytes)
1788
0
        return 0;
1789
88.0k
    encode_pubkey(out, key);
1790
88.0k
    return 1;
1791
88.0k
}
1792
1793
/* Serialise an ML-KEM private key */
1794
int ossl_ml_kem_encode_private_key(uint8_t *out, size_t len,
1795
    const ML_KEM_KEY *key)
1796
173
{
1797
173
    if (!ossl_ml_kem_have_prvkey(key)
1798
173
        || len != key->vinfo->prvkey_bytes)
1799
0
        return 0;
1800
173
    encode_prvkey(out, key);
1801
173
    return 1;
1802
173
}
1803
1804
int ossl_ml_kem_encode_seed(uint8_t *out, size_t len,
1805
    const ML_KEM_KEY *key)
1806
315
{
1807
315
    if (key == NULL || key->d == NULL || len != ML_KEM_SEED_BYTES)
1808
51
        return 0;
1809
    /*
1810
     * Both in the seed buffer, and in the allocated storage, the |d| component
1811
     * of the seed is stored last, so we must copy each separately.
1812
     */
1813
264
    memcpy(out, key->d, ML_KEM_RANDOM_BYTES);
1814
264
    out += ML_KEM_RANDOM_BYTES;
1815
264
    memcpy(out, key->z, ML_KEM_RANDOM_BYTES);
1816
264
    return 1;
1817
315
}
1818
1819
/*
1820
 * Stash the seed without (yet) performing a keygen, used during decoding, to
1821
 * avoid an extra keygen if we're only going to export the key again to load
1822
 * into another provider.
1823
 */
1824
ML_KEM_KEY *ossl_ml_kem_set_seed(const uint8_t *seed, size_t seedlen, ML_KEM_KEY *key)
1825
24
{
1826
24
    if (key == NULL
1827
24
        || ossl_ml_kem_have_pubkey(key)
1828
24
        || ossl_ml_kem_have_seed(key)
1829
24
        || seedlen != ML_KEM_SEED_BYTES)
1830
0
        return NULL;
1831
1832
24
    if (key->seedbuf == NULL) {
1833
24
        key->seedbuf = OPENSSL_secure_malloc(seedlen);
1834
24
        if (key->seedbuf == NULL)
1835
0
            return NULL;
1836
24
    }
1837
1838
24
    key->z = key->seedbuf;
1839
24
    key->d = key->z + ML_KEM_RANDOM_BYTES;
1840
24
    memcpy(key->d, seed, ML_KEM_RANDOM_BYTES);
1841
24
    seed += ML_KEM_RANDOM_BYTES;
1842
24
    memcpy(key->z, seed, ML_KEM_RANDOM_BYTES);
1843
24
    return key;
1844
24
}
1845
1846
/* Parse input as a public key */
1847
int ossl_ml_kem_parse_public_key(const uint8_t *in, size_t len, ML_KEM_KEY *key)
1848
770
{
1849
770
    EVP_MD_CTX *mdctx = NULL;
1850
770
    const ML_KEM_VINFO *vinfo;
1851
770
    int ret = 0;
1852
1853
    /* Keys with key material are immutable */
1854
770
    if (key == NULL
1855
770
        || ossl_ml_kem_have_pubkey(key)
1856
770
        || ossl_ml_kem_have_dkenc(key))
1857
0
        return 0;
1858
770
    vinfo = key->vinfo;
1859
1860
770
    if (len != vinfo->pubkey_bytes
1861
770
        || (mdctx = EVP_MD_CTX_new()) == NULL)
1862
0
        return 0;
1863
1864
770
    if (add_storage(OPENSSL_malloc(vinfo->puballoc), NULL, 0, 0, key))
1865
770
        ret = parse_pubkey(in, mdctx, key);
1866
1867
770
    if (!ret)
1868
321
        ossl_ml_kem_key_reset(key);
1869
770
    EVP_MD_CTX_free(mdctx);
1870
770
    return ret;
1871
770
}
1872
1873
/* Parse input as a new private key */
1874
int ossl_ml_kem_parse_private_key(const uint8_t *in, size_t len,
1875
    ML_KEM_KEY *key)
1876
137
{
1877
137
    EVP_MD_CTX *mdctx = NULL;
1878
137
    const ML_KEM_VINFO *vinfo;
1879
137
    int ret = 0;
1880
1881
    /* Keys with key material are immutable */
1882
137
    if (key == NULL
1883
137
        || ossl_ml_kem_have_pubkey(key)
1884
137
        || ossl_ml_kem_have_dkenc(key))
1885
0
        return 0;
1886
137
    vinfo = key->vinfo;
1887
1888
137
    if (len != vinfo->prvkey_bytes
1889
137
        || (mdctx = EVP_MD_CTX_new()) == NULL)
1890
0
        return 0;
1891
1892
    /* Clear any unused seed */
1893
137
    ossl_ml_kem_key_reset(key);
1894
1895
137
    if (add_storage(OPENSSL_malloc(vinfo->puballoc),
1896
137
            OPENSSL_secure_malloc(vinfo->prvalloc), 1, 0, key))
1897
137
        ret = parse_prvkey(in, mdctx, key);
1898
1899
137
    if (!ret)
1900
137
        ossl_ml_kem_key_reset(key);
1901
137
    EVP_MD_CTX_free(mdctx);
1902
137
    return ret;
1903
137
}
1904
1905
/*
1906
 * Generate a new keypair, either from the saved seed (when non-null), or from
1907
 * the RNG.
1908
 */
1909
int ossl_ml_kem_genkey(uint8_t *pubenc, size_t publen, ML_KEM_KEY *key)
1910
86.8k
{
1911
86.8k
    uint8_t seed[ML_KEM_SEED_BYTES];
1912
86.8k
    EVP_MD_CTX *mdctx = NULL;
1913
86.8k
    const ML_KEM_VINFO *vinfo;
1914
86.8k
    int ret = 0;
1915
1916
86.8k
    if (key == NULL
1917
86.8k
        || ossl_ml_kem_have_pubkey(key)
1918
86.8k
        || ossl_ml_kem_have_dkenc(key))
1919
0
        return 0;
1920
86.8k
    vinfo = key->vinfo;
1921
1922
86.8k
    if (pubenc != NULL && publen != vinfo->pubkey_bytes)
1923
0
        return 0;
1924
1925
86.8k
    if (key->seedbuf != NULL) {
1926
91
        if (!ossl_ml_kem_encode_seed(seed, sizeof(seed), key))
1927
0
            return 0;
1928
91
        ossl_ml_kem_key_reset(key);
1929
86.7k
    } else if (RAND_priv_bytes_ex(key->libctx, seed, sizeof(seed),
1930
86.7k
                   key->vinfo->secbits)
1931
86.7k
        <= 0) {
1932
0
        return 0;
1933
0
    }
1934
1935
86.8k
    if ((mdctx = EVP_MD_CTX_new()) == NULL)
1936
0
        return 0;
1937
1938
    /*
1939
     * Data derived from (d, z) defaults secret, and to avoid side-channel
1940
     * leaks should not influence control flow.
1941
     */
1942
86.8k
    CONSTTIME_SECRET(seed, ML_KEM_SEED_BYTES);
1943
1944
86.8k
    if (add_storage(OPENSSL_malloc(vinfo->puballoc),
1945
86.8k
            OPENSSL_secure_malloc(vinfo->prvalloc), 1, 0, key))
1946
86.8k
        ret = genkey(seed, mdctx, pubenc, key);
1947
86.8k
    OPENSSL_cleanse(seed, sizeof(seed));
1948
1949
    /* Declassify secret inputs and derived outputs before returning control */
1950
86.8k
    CONSTTIME_DECLASSIFY(seed, ML_KEM_SEED_BYTES);
1951
1952
86.8k
    EVP_MD_CTX_free(mdctx);
1953
86.8k
    if (!ret) {
1954
        /* Erase any partial public key output */
1955
0
        if (pubenc != NULL)
1956
0
            OPENSSL_cleanse(pubenc, vinfo->pubkey_bytes);
1957
0
        ossl_ml_kem_key_reset(key);
1958
0
        return 0;
1959
0
    }
1960
1961
    /* The public components are already declassified */
1962
86.8k
    CONSTTIME_DECLASSIFY(key->s, vinfo->rank * sizeof(scalar));
1963
86.8k
    CONSTTIME_DECLASSIFY(key->z, 2 * ML_KEM_RANDOM_BYTES);
1964
86.8k
    return 1;
1965
86.8k
}
1966
1967
/*
1968
 * FIPS 203, Section 6.2, Algorithm 17: ML-KEM.Encaps_internal
1969
 * This is the deterministic version with randomness supplied externally.
1970
 */
1971
int ossl_ml_kem_encap_seed(uint8_t *ctext, size_t clen,
1972
    uint8_t *shared_secret, size_t slen,
1973
    const uint8_t *entropy, size_t elen,
1974
    const ML_KEM_KEY *key)
1975
221
{
1976
221
    const ML_KEM_VINFO *vinfo;
1977
221
    EVP_MD_CTX *mdctx;
1978
221
    int ret = 0;
1979
1980
221
    if (key == NULL || !ossl_ml_kem_have_pubkey(key))
1981
0
        return 0;
1982
221
    vinfo = key->vinfo;
1983
1984
221
    if (ctext == NULL || clen != vinfo->ctext_bytes
1985
221
        || shared_secret == NULL || slen != ML_KEM_SHARED_SECRET_BYTES
1986
221
        || entropy == NULL || elen != ML_KEM_RANDOM_BYTES
1987
221
        || (mdctx = EVP_MD_CTX_new()) == NULL)
1988
0
        return 0;
1989
    /*
1990
     * Data derived from the encap entropy defaults secret, and to avoid
1991
     * side-channel leaks should not influence control flow.
1992
     */
1993
221
    CONSTTIME_SECRET(entropy, elen);
1994
1995
    /*-
1996
     * This avoids the need to handle allocation failures for two (max 2KB
1997
     * each) vectors, that are never retained on return from this function.
1998
     * We stack-allocate these.
1999
     */
2000
221
#define case_encap_seed(bits)                                        \
2001
221
    {                                                                \
2002
221
        scalar tmp[2 * ML_KEM_##bits##_RANK];                        \
2003
221
                                                                     \
2004
221
        ret = encap(ctext, shared_secret, entropy, tmp, mdctx, key); \
2005
221
        OPENSSL_cleanse((void *)tmp, sizeof(tmp));                   \
2006
221
    }
2007
221
    switch (vinfo->evp_type) {
2008
85
    case EVP_PKEY_ML_KEM_512:
2009
85
        case_encap_seed(512);
2010
85
        break;
2011
91
    case EVP_PKEY_ML_KEM_768:
2012
91
        case_encap_seed(768);
2013
91
        break;
2014
45
    case EVP_PKEY_ML_KEM_1024:
2015
45
        case_encap_seed(1024);
2016
45
        break;
2017
221
    }
2018
221
#undef case_encap_seed
2019
2020
    /* Erase any partial ciphertext output on failure */
2021
221
    if (!ret)
2022
0
        OPENSSL_cleanse(ctext, clen);
2023
2024
    /* Declassify secret inputs and derived outputs before returning control */
2025
221
    CONSTTIME_DECLASSIFY(entropy, elen);
2026
221
    CONSTTIME_DECLASSIFY(ctext, clen);
2027
221
    CONSTTIME_DECLASSIFY(shared_secret, slen);
2028
2029
221
    EVP_MD_CTX_free(mdctx);
2030
221
    return ret;
2031
221
}
2032
2033
int ossl_ml_kem_encap_rand(uint8_t *ctext, size_t clen,
2034
    uint8_t *shared_secret, size_t slen,
2035
    const ML_KEM_KEY *key)
2036
221
{
2037
221
    uint8_t r[ML_KEM_RANDOM_BYTES];
2038
221
    int ret;
2039
2040
221
    if (key == NULL)
2041
0
        return 0;
2042
2043
221
    if (RAND_bytes_ex(key->libctx, r, ML_KEM_RANDOM_BYTES,
2044
221
            key->vinfo->secbits)
2045
221
        < 1)
2046
0
        return 0;
2047
2048
221
    ret = ossl_ml_kem_encap_seed(ctext, clen, shared_secret, slen,
2049
221
        r, sizeof(r), key);
2050
2051
221
    OPENSSL_cleanse((void *)r, sizeof(r));
2052
221
    return ret;
2053
221
}
2054
2055
int ossl_ml_kem_decap(uint8_t *shared_secret, size_t slen,
2056
    const uint8_t *ctext, size_t clen,
2057
    const ML_KEM_KEY *key)
2058
234
{
2059
234
    const ML_KEM_VINFO *vinfo;
2060
234
    EVP_MD_CTX *mdctx;
2061
234
    int ret = 0;
2062
#if defined(OPENSSL_CONSTANT_TIME_VALIDATION)
2063
    int classify_bytes = 2 * sizeof(scalar) + ML_KEM_RANDOM_BYTES;
2064
#endif
2065
2066
    /* Need a private key here */
2067
234
    if (!ossl_ml_kem_have_prvkey(key)
2068
234
        || shared_secret == NULL
2069
234
        || slen < ML_KEM_SHARED_SECRET_BYTES)
2070
0
        return 0;
2071
234
    vinfo = key->vinfo;
2072
2073
234
    if (slen != ML_KEM_SHARED_SECRET_BYTES
2074
234
        || ctext == NULL || clen != vinfo->ctext_bytes
2075
234
        || (mdctx = EVP_MD_CTX_new()) == NULL) {
2076
0
        (void)RAND_bytes_ex(key->libctx, shared_secret,
2077
0
            ML_KEM_SHARED_SECRET_BYTES, vinfo->secbits);
2078
0
        return 0;
2079
0
    }
2080
    /*
2081
     * Data derived from |s| and |z| defaults secret, and to avoid side-channel
2082
     * leaks should not influence control flow.
2083
     */
2084
234
    CONSTTIME_SECRET(key->s, classify_bytes);
2085
2086
    /*-
2087
     * This avoids the need to handle allocation failures for two (max 2KB
2088
     * each) vectors and an encoded ciphertext (max 1568 bytes), that are never
2089
     * retained on return from this function.
2090
     * We stack-allocate these.
2091
     */
2092
234
#define case_decap(bits)                                          \
2093
234
    {                                                             \
2094
234
        uint8_t cbuf[CTEXT_BYTES(bits)];                          \
2095
234
        scalar tmp[2 * ML_KEM_##bits##_RANK];                     \
2096
234
                                                                  \
2097
234
        ret = decap(shared_secret, ctext, cbuf, tmp, mdctx, key); \
2098
234
        OPENSSL_cleanse((void *)tmp, sizeof(tmp));                \
2099
234
        OPENSSL_cleanse((void *)cbuf, sizeof(cbuf));              \
2100
234
    }
2101
234
    switch (vinfo->evp_type) {
2102
85
    case EVP_PKEY_ML_KEM_512:
2103
85
        case_decap(512);
2104
85
        break;
2105
104
    case EVP_PKEY_ML_KEM_768:
2106
104
        case_decap(768);
2107
104
        break;
2108
45
    case EVP_PKEY_ML_KEM_1024:
2109
45
        case_decap(1024);
2110
45
        break;
2111
234
    }
2112
234
#undef case_decap
2113
2114
    /* Declassify secret inputs and derived outputs before returning control */
2115
234
    CONSTTIME_DECLASSIFY(key->s, classify_bytes);
2116
234
    CONSTTIME_DECLASSIFY(shared_secret, slen);
2117
234
    EVP_MD_CTX_free(mdctx);
2118
2119
234
    return ret;
2120
234
}
2121
2122
int ossl_ml_kem_pubkey_cmp(const ML_KEM_KEY *key1, const ML_KEM_KEY *key2)
2123
204
{
2124
    /*
2125
     * This handles any unexpected differences in the ML-KEM variant rank,
2126
     * giving different key component structures, barring SHA3-256 hash
2127
     * collisions, the keys are the same size.
2128
     */
2129
204
    if (ossl_ml_kem_have_pubkey(key1) && ossl_ml_kem_have_pubkey(key2))
2130
204
        return memcmp(key1->pkhash, key2->pkhash, ML_KEM_PKHASH_BYTES) == 0;
2131
2132
    /*
2133
     * No match if just one of the public keys is not available, otherwise both
2134
     * are unavailable, and for now such keys are considered equal.
2135
     */
2136
0
    return (!(ossl_ml_kem_have_pubkey(key1) ^ ossl_ml_kem_have_pubkey(key2)));
2137
204
}