Skip to content

Commit 96cad0c

Browse files
committed
Expand and unify example READMEs
Signed-off-by: Hanno Becker <beckphan@amazon.co.uk>
1 parent 12d5ac1 commit 96cad0c

File tree

14 files changed

+465
-1387
lines changed

14 files changed

+465
-1387
lines changed

examples/basic/README.md

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
11
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
22

3-
# Building mlkem-native
3+
# Basic build
44

5-
This directory contains a minimal example for how to build mlkem-native.
5+
This directory contains a minimal example for how to build mlkem-native for a single security level.
6+
7+
## Use Case
8+
9+
Use this approach when:
10+
- You need only one ML-KEM parameter set (512, 768, or 1024)
11+
- You want to build the mlkem-native C files separately, not as a single compilation unit.
12+
- You're using C only, no native backends.
613

714
## Components
815

9-
An application using mlkem-native as-is needs to include the following components:
16+
1. mlkem-native source tree: [`mlkem/src/`](../../mlkem/src) and [`mlkem/src/fips202/`](../../mlkem/src/fips202)
17+
2. A secure random number generator implementing [`randombytes.h`](../../mlkem/src/randombytes.h)
18+
3. Your application source code
19+
20+
## Configuration
1021

11-
1. mlkem-native source tree, including [`mlkem/src/`](../../mlkem/src) and [`mlkem/src/fips202/`](../../mlkem/src/fips202).
12-
2. A secure pseudo random number generator, implementing [`randombytes.h`](../../mlkem/src/randombytes.h).
13-
3. The application source code
22+
The configuration file [mlkem_native_config.h](mlkem_native/mlkem_native_config.h) sets:
23+
- `MLK_CONFIG_PARAMETER_SET`: Security level (512, 768, or 1024). Default is 768.
24+
- `MLK_CONFIG_NAMESPACE_PREFIX`: Symbol prefix for the API. Set to `mlkem` in this example.
1425

15-
**WARNING:** The `randombytes()` implementation used here is for TESTING ONLY. You MUST NOT use this implementation
16-
outside of testing.
26+
To change the security level, modify `MLK_CONFIG_PARAMETER_SET` in the config file or pass it via CFLAGS:
27+
```bash
28+
make build CFLAGS="-DMLK_CONFIG_PARAMETER_SET=512"
29+
```
1730

1831
## Usage
1932

20-
Build this example with `make build`, run with `make run`.
33+
```bash
34+
make build # Build the example
35+
make run # Run the example
36+
```
37+
38+
## Warning
39+
40+
The `randombytes()` implementation in `test_only_rng/` is for TESTING ONLY.
41+
You MUST provide a cryptographically secure RNG for production use.
Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
22

3-
# Building mlkem-native
3+
# Basic derandomized-only build
44

5-
This directory contains a minimal example showing how to build **mlkem-native** for use cases only requiring the deterministic key generation and encapsulation APIs (`crypto_kem_keypair_derand` and `crypto_kem_enc_derand`). In that case, no implementation of `randombytes()` has to be provided.
5+
This directory contains a minimal example for building mlkem-native using only the deterministic API,
6+
without requiring a `randombytes()` implementation.
67

7-
The configuration file [mlkem_native_config.h](mlkem_native/mlkem_native_config.h) sets `MLK_CONFIG_NO_RANDOMIZED_API`
8-
to disable the randomized API functions.
8+
## Use Case
9+
10+
Use this approach when:
11+
- Your application manages its own entropy/randomness externally
12+
- You only need `crypto_kem_keypair_derand` and `crypto_kem_enc_derand` (deterministic variants)
913

1014
## Components
1115

12-
An application using mlkem-native as-is needs to include the following components:
16+
1. mlkem-native source tree: [`mlkem/src/`](../../mlkem/src) and [`mlkem/src/fips202/`](../../mlkem/src/fips202)
17+
2. Your application source code
18+
19+
No `randombytes()` implementation is required.
20+
21+
## Configuration
22+
23+
The configuration file [mlkem_native_config.h](mlkem_native/mlkem_native_config.h) sets:
24+
- `MLK_CONFIG_NO_RANDOMIZED_API`: Disables `crypto_kem_keypair` and `crypto_kem_enc`
25+
- `MLK_CONFIG_PARAMETER_SET`: Security level (default 768)
26+
- `MLK_CONFIG_NAMESPACE_PREFIX`: Symbol prefix (set to `mlkem`)
1327

14-
1. mlkem-native source tree, including [`mlkem/src/`](../../mlkem/src) and [`mlkem/src/fips202/`](../../mlkem/src/fips202).
15-
2. The application source code
28+
## Notes
1629

30+
- This is incompatible with `MLK_CONFIG_KEYGEN_PCT` (pairwise consistency test)
1731

1832
## Usage
1933

20-
Build this example with `make build`, run with `make run`.
34+
```bash
35+
make build # Build the example
36+
make run # Run the example
37+
```
Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,56 @@
11
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
22

3-
# Bring your own FIPS-202
3+
# Bring Your Own FIPS-202
44

5-
This directory contains a minimal example for how to use mlkem-native as a code package, with a custom FIPS-202
5+
This directory contains a minimal example for using mlkem-native with a custom FIPS-202 (SHA-3/SHAKE)
66
implementation. We use tiny_sha3[^tiny_sha3] as an example.
77

8+
## Use Case
9+
10+
Use this approach when:
11+
- You need only one ML-KEM parameter set (512, 768, or 1024)
12+
- Your application already has a FIPS-202 software/hardware implementation you want to reuse
13+
814
## Components
915

10-
An application using mlkem-native with a custom FIPS-202 implementation needs the following:
16+
1. Arithmetic part of mlkem-native: [`mlkem/src/`](../../mlkem/src) (excluding `fips202/`)
17+
2. A secure random number generator implementing [`randombytes.h`](../../mlkem/src/randombytes.h)
18+
3. Custom FIPS-202 implementation with headers compatible with:
19+
- [`fips202.h`](../../mlkem/src/fips202/fips202.h)
20+
- [`fips202x4.h`](../../mlkem/src/fips202/fips202x4.h)
21+
4. Your application source code
22+
23+
## Configuration
24+
25+
The configuration file [mlkem_native_config.h](mlkem_native/mlkem_native_config.h) sets:
26+
- `MLK_CONFIG_FIPS202_CUSTOM_HEADER`: Path to your custom `fips202.h`
27+
- `MLK_CONFIG_FIPS202X4_CUSTOM_HEADER`: Path to your custom `fips202x4.h`
1128

12-
1. Arithmetic part of the mlkem-native source tree: [`mlkem/src/`](../../mlkem/src)
13-
2. A secure pseudo random number generator, implementing [`randombytes.h`](../../mlkem/src/randombytes.h).
14-
3. A custom FIPS-202 with `fips202.h` and `fips202x4.h` headers compatible with
15-
[`mlkem/src/fips202/fips202.h`](../../mlkem/src/fips202/fips202.h) and [`mlkem/src/fips202/fips202x4.h`](../../mlkem/src/fips202/fips202x4.h).
16-
4. The application source code
29+
Your custom FIPS-202 implementation must provide:
30+
- `mlk_shake128_absorb_once()`, `mlk_shake128_squeezeblocks()`, `mlk_shake128_release()`
31+
- `mlk_shake256()`, `mlk_sha3_256()`, `mlk_sha3_512()`
32+
- `mlk_shake256x4()`
33+
- `mlk_shake128x4_absorb_once()`, `mlk_shake128x4_squeezeblocks()`, `mlk_shake128x4_release()`
34+
- Structure definitions for `mlk_shake128ctx` and `mlk_shake128x4ctx`
1735

18-
The configuration file [mlkem_native_config.h](mlkem_native/mlkem_native_config.h) sets
19-
`MLK_CONFIG_FIPS202_CUSTOM_HEADER` and `MLK_CONFIG_FIPS202X4_CUSTOM_HEADER` to point to the custom FIPS-202 headers.
36+
See [FIPS202.md](../../FIPS202.md) for the complete API specification.
2037

21-
**WARNING:** The `randombytes()` implementation used here is for TESTING ONLY. You MUST NOT use this implementation
22-
outside of testing.
38+
## Notes
39+
40+
- The 4x batched functions (`x4`) can fall back to 4 sequential calls if batching isn't available
41+
- Structure definitions may differ from mlkem-native's defaults (e.g., for incremental hashing)
2342

2443
## Usage
2544

26-
Build this example with `make build`, run with `make run`.
45+
```bash
46+
make build # Build the example
47+
make run # Run the example
48+
```
49+
50+
## Warning
51+
52+
The `randombytes()` implementation in `test_only_rng/` is for TESTING ONLY.
53+
You MUST provide a cryptographically secure RNG for production use.
2754

2855
<!--- bibliography --->
2956
[^tiny_sha3]: Markku-Juhani O. Saarinen: tiny_sha3, [https://github.com/mjosaarinen/tiny_sha3](https://github.com/mjosaarinen/tiny_sha3)
Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,55 @@
11
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
22

3-
# Bring your own FIPS-202 (Static State Variant)
3+
# Bring Your Own FIPS-202 (Static State Variant)
44

5-
This directory contains a minimal example for how to use mlkem-native with external FIPS202
6-
HW/SW-implementations that use a single global state (for example, some hardware accelerators).
7-
Specifically, this example demonstrates the use of the serial-only FIPS-202 configuration
8-
`MLK_CONFIG_SERIAL_FIPS202_ONLY`.
5+
This directory contains a minimal example for using mlkem-native with a custom FIPS-202 implementation
6+
that uses a single global state. This is common for hardware accelerators that can only hold one
7+
Keccak state at a time.
8+
9+
## Use Case
10+
11+
Use this approach when:
12+
- You need only one ML-KEM parameter set (512, 768, or 1024)
13+
- Your application already has a FIPS-202 software/hardware implementation you want to reuse
14+
- Your FIPS-202 implementation does not support multiple active SHA3/SHAKE computations.
915

1016
## Components
1117

12-
An application using mlkem-native with a custom FIPS-202 implementation needs the following:
18+
1. Arithmetic part of mlkem-native: [`mlkem/src/`](../../mlkem/src) (excluding `fips202/`)
19+
2. A secure random number generator implementing [`randombytes.h`](../../mlkem/src/randombytes.h)
20+
3. Custom FIPS-202 implementation with headers compatible with [`fips202.h`](../../mlkem/src/fips202/fips202.h)
21+
and [`fips202x4.h`](../../mlkem/src/fips202/fips202x4.h)
22+
4. Your application source code
23+
24+
## Configuration
25+
26+
The configuration file [mlkem_native_config.h](mlkem_native/mlkem_native_config.h) sets:
27+
- `MLK_CONFIG_SERIAL_FIPS202_ONLY`: Disables batched Keccak; matrix entries generated one at a time
28+
- `MLK_CONFIG_FIPS202_CUSTOM_HEADER`: Path to your custom `fips202.h`
29+
- `MLK_CONFIG_FIPS202X4_CUSTOM_HEADER`: Path to stub `fips202x4.h`
1330

14-
1. Arithmetic part of the mlkem-native source tree: [`mlkem/src/`](../../mlkem/src)
15-
2. A secure pseudo random number generator, implementing [`randombytes.h`](../../mlkem/src/randombytes.h).
16-
2. A custom FIPS202 with `fips202.h` header compatible with [`mlkem/src/fips202/fips202.h`](../../mlkem/src/fips202/fips202.h).
17-
The FIPS202x4 header `fips202x4.h` is unused with `MLK_CONFIG_SERIAL_FIPS202_ONLY` and can be filled with stubs.
18-
3. The application source code
31+
Your custom FIPS-202 implementation must provide:
32+
- `mlk_shake128_absorb_once()`, `mlk_shake128_squeezeblocks()`, `mlk_shake128_release()`
33+
- `mlk_shake256()`, `mlk_sha3_256()`, `mlk_sha3_512()`
34+
- Structure definition for `mlk_shake128ctx`
1935

20-
**WARNING:** The `randombytes()` implementation used here is for TESTING ONLY. You MUST NOT use this implementation
21-
outside of testing.
36+
## Notes
37+
38+
- `MLK_CONFIG_SERIAL_FIPS202_ONLY` may reduce performance on CPUs with SIMD support
39+
- Matrix generation becomes sequential instead of batched (4 entries at a time)
40+
- Only enable this when your hardware requires it
2241

2342
## Usage
2443

25-
Build this example with `make build`, run with `make run`.
44+
```bash
45+
make build # Build the example
46+
make run # Run the example
47+
```
48+
49+
## Warning
50+
51+
The `randombytes()` implementation in `test_only_rng/` is for TESTING ONLY.
52+
You MUST provide a cryptographically secure RNG for production use.
2653

2754
<!--- bibliography --->
2855
[^tiny_sha3]: Markku-Juhani O. Saarinen: tiny_sha3, [https://github.com/mjosaarinen/tiny_sha3](https://github.com/mjosaarinen/tiny_sha3)

examples/custom_backend/README.md

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,70 @@
11
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
22

3-
# Using a custom configuration and FIPS-202 backend
3+
# Custom FIPS-202 Backend
44

5-
This directory contains a minimal example for how to use mlkem-native as a code package, with a custom FIPS-202
6-
backend and a custom configuration. We use [^tiny_sha3] as an example.
5+
This directory contains a minimal example for using mlkem-native with a custom FIPS-202 *backend*
6+
(as opposed to a complete custom FIPS-202 implementation). We use tiny_sha3[^tiny_sha3] as an example.
7+
8+
## Use Case
9+
10+
Use this approach when:
11+
- You need only one ML-KEM parameter set (512, 768, or 1024)
12+
- You want to replace the low-level Keccak-f1600 permutation
13+
- You want to keep mlkem-native's FIPS-202 frontend (absorb/squeeze logic)
14+
15+
This differs from `bring_your_own_fips202` in that you only replace the *backend* (Keccak permutation),
16+
not the entire FIPS-202 implementation.
717

818
## Components
919

10-
An application using mlkem-native with a custom FIPS-202 backend and custom configuration needs the following:
11-
12-
1. Arithmetic part of the mlkem-native source tree: [`mlkem/src/`](../../mlkem/src). In this example, we disable arithmetic
13-
backends, hence it is safe to remove the entire `native` subfolder.
14-
2. A secure pseudo random number generator, implementing [`randombytes.h`](../../mlkem/src/randombytes.h). **WARNING:** The
15-
`randombytes()` implementation used here is for TESTING ONLY. You MUST NOT use this implementation outside of testing.
16-
3. FIPS-202 part of the mlkem-native source tree, [`fips202/`](../../mlkem/src/fips202). If you only want to use your backend,
17-
you can remove all existing backends; that's what this example does.
18-
4. A custom FIPS-202 backend. In this example, the backend file is
19-
[custom.h](mlkem_native/src/fips202/native/custom/custom.h), wrapping
20-
[sha3.c](mlkem_native/src/fips202/native/custom/src/sha3.c) and setting `MLK_USE_FIPS202_X1_NATIVE` to indicate that we
21-
replace 1-fold Keccak-F1600.
22-
5. Either modify the existing [mlkem_native_config.h](mlkem_native/mlkem_native_config.h), or register a new config.
23-
In this example, we modify [mlkem_native_config.h](mlkem_native/mlkem_native_config.h) directly. For the sake of
24-
demonstration, we set a custom namespace. We set `MLK_CONFIG_FIPS202_BACKEND_FILE` to point to our custom FIPS-202
25-
backend, but leave `MLK_CONFIG_USE_NATIVE_BACKEND_ARITH` undefined to indicate that we wish to use the C backend.
26-
27-
## Note
28-
29-
The tiny_sha3 code uses a byte-reversed presentation of the Keccakf1600 state for big-endian targets. Since
30-
mlkem-native's FIPS202 frontend assumes a standard presentation, the corresponding byte-reversal in
31-
[sha3.c](mlkem_native/src/fips202/native/custom/src/sha3.c) is removed.
20+
1. Arithmetic part of mlkem-native: [`mlkem/src/`](../../mlkem/src)
21+
2. FIPS-202 frontend: [`mlkem/src/fips202/`](../../mlkem/src/fips202) (can remove existing backends)
22+
3. A secure random number generator implementing [`randombytes.h`](../../mlkem/src/randombytes.h)
23+
4. Custom FIPS-202 backend (see below)
24+
5. Your application source code
25+
26+
## Configuration
27+
28+
The configuration file [mlkem_native_config.h](mlkem_native/mlkem_native_config.h) sets:
29+
- `MLK_CONFIG_USE_NATIVE_BACKEND_FIPS202`: Enables native FIPS-202 backend
30+
- `MLK_CONFIG_FIPS202_BACKEND_FILE`: Path to your custom backend metadata file
31+
32+
A custom backend consists of:
33+
1. A metadata header (e.g., [custom.h](mlkem_native/src/fips202/native/custom/custom.h)) that:
34+
- Sets `MLK_USE_FIPS202_X1_NATIVE` (and/or `X2`/`X4`) to indicate which functions are replaced
35+
- Includes the implementation header
36+
2. An implementation providing `mlk_keccakf1600_native()` (and/or batched variants)
37+
38+
Example backend metadata file:
39+
```c
40+
#ifndef CUSTOM_FIPS202_BACKEND_H
41+
#define CUSTOM_FIPS202_BACKEND_H
42+
43+
/* Indicate we're replacing 1-fold Keccak-f1600 */
44+
#define MLK_USE_FIPS202_X1_NATIVE
45+
46+
/* Include the implementation */
47+
#include "custom/src/keccak_impl.h"
48+
49+
#endif
50+
```
51+
52+
## Notes
53+
54+
- The tiny_sha3 code uses byte-reversed Keccak state on big-endian targets; this example removes
55+
that reversal since mlkem-native's frontend assumes standard byte order
3256

3357
## Usage
3458

35-
Build this example with `make build`, run with `make run`.
59+
```bash
60+
make build # Build the example
61+
make run # Run the example
62+
```
63+
64+
## Warning
65+
66+
The `randombytes()` implementation in `test_only_rng/` is for TESTING ONLY.
67+
You MUST provide a cryptographically secure RNG for production use.
3668

3769
<!--- bibliography --->
3870
[^tiny_sha3]: Markku-Juhani O. Saarinen: tiny_sha3, [https://github.com/mjosaarinen/tiny_sha3](https://github.com/mjosaarinen/tiny_sha3)
Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,50 @@
11
[//]: # (SPDX-License-Identifier: CC-BY-4.0)
22

3-
# Single-level mlkem-native in a single compilation unit
3+
# Monolithic Build (C Backend)
44

5-
This directory contains a minimal example for how to build a single instance of mlkem-native in a single compilation
6-
unit. Only the C-backend is exercised.
5+
This directory contains a minimal example for building mlkem-native for a single
6+
parameter set of ML-KEM as a single compilation unit using the auto-generated
7+
`mlkem_native.c` file.
78

8-
The auto-generated source file [mlkem_native.c](mlkem_native/mlkem_native.c) includes all mlkem-native C source
9-
files. Moreover, it clears all `#define`s clauses set by mlkem-native at the end, and is hence amenable to multiple
10-
inclusion in another compilation unit. It exposes the API [mlkem_native.h](mlkem_native/mlkem_native.h).
9+
## Use Case
1110

12-
The configuration file [mlkem_native_config.h](mlkem_native/mlkem_native_config.h) sets
13-
`MLK_CONFIG_INTERNAL_API_QUALIFIER` to `static`, making all internal functions static for the single-CU build.
11+
Use this approach when:
12+
- You want the simplest possible build integration (one `.c` file)
13+
- You're using only C (no native backends)
14+
- You need only one ML-KEM parameter set (512, 768, or 1024)
15+
16+
## Components
17+
18+
1. Source tree [mlkem_native/*](mlkem_native), including top-level compilation unit
19+
[mlkem_native.c](mlkem_native/mlkem_native.c) (gathering all C sources)
20+
and the mlkem-native API [mlkem_native.h](mlkem_native/mlkem_native.h).
21+
2. A secure random number generator implementing [`randombytes.h`](../../mlkem/src/randombytes.h)
22+
3. Your application source code
23+
24+
## Configuration
25+
26+
The configuration file [mlkem_native_config.h](mlkem_native/mlkem_native_config.h) sets:
27+
- `MLK_CONFIG_PARAMETER_SET`: Security level (default 768)
28+
- `MLK_CONFIG_NAMESPACE_PREFIX`: Symbol prefix (set to `mlkem`)
29+
- `MLK_CONFIG_INTERNAL_API_QUALIFIER=static`: Makes internal functions static for single-CU builds
30+
31+
The auto-generated `mlkem_native.c`:
32+
- Includes all mlkem-native C source files
33+
- Clears all internal `#define`s at the end, allowing multiple inclusion
34+
35+
## Notes
36+
37+
- The monolithic `.c` file is auto-generated by `scripts/autogen`
38+
- Internal functions become `static`, enabling better compiler optimization
1439

1540
## Usage
1641

17-
Build this example with `make build`, run with `make run`.
42+
```bash
43+
make build # Build the example
44+
make run # Run the example
45+
```
46+
47+
## Warning
1848

19-
**WARNING:** The `randombytes()` implementation used here is for TESTING ONLY. You MUST NOT use this implementation
20-
outside of testing.
49+
The `randombytes()` implementation in `test_only_rng/` is for TESTING ONLY.
50+
You MUST provide a cryptographically secure RNG for production use.

0 commit comments

Comments
 (0)