|
1 | 1 | [//]: # (SPDX-License-Identifier: CC-BY-4.0) |
2 | 2 |
|
3 | | -# Using a custom configuration and FIPS-202 backend |
| 3 | +# Custom FIPS-202 Backend |
4 | 4 |
|
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 want to replace the low-level Keccak-f1600 permutation |
| 12 | +- You want to keep mlkem-native's FIPS-202 frontend (absorb/squeeze logic) |
| 13 | + |
| 14 | +This differs from `bring_your_own_fips202` in that you only replace the *backend* (Keccak permutation), |
| 15 | +not the entire FIPS-202 implementation. |
7 | 16 |
|
8 | 17 | ## Components |
9 | 18 |
|
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. |
| 19 | +1. Arithmetic part of mlkem-native: [`mlkem/src/`](../../mlkem/src) |
| 20 | +2. FIPS-202 frontend: [`mlkem/src/fips202/`](../../mlkem/src/fips202) (can remove existing backends) |
| 21 | +3. A secure random number generator implementing [`randombytes.h`](../../mlkem/src/randombytes.h) |
| 22 | +4. Custom FIPS-202 backend (see below) |
| 23 | +5. Your application source code |
| 24 | + |
| 25 | +## Configuration |
| 26 | + |
| 27 | +The configuration file [mlkem_native_config.h](mlkem_native/mlkem_native_config.h) sets: |
| 28 | +- `MLK_CONFIG_USE_NATIVE_BACKEND_FIPS202`: Enables native FIPS-202 backend |
| 29 | +- `MLK_CONFIG_FIPS202_BACKEND_FILE`: Path to your custom backend metadata file |
| 30 | + |
| 31 | +A custom backend consists of: |
| 32 | +1. A metadata header (e.g., [custom.h](mlkem_native/src/fips202/native/custom/custom.h)) that: |
| 33 | + - Sets `MLK_USE_FIPS202_X1_NATIVE` (and/or `X2`/`X4`) to indicate which functions are replaced |
| 34 | + - Includes the implementation header |
| 35 | +2. An implementation providing `mlk_keccakf1600_native()` (and/or batched variants) |
| 36 | + |
| 37 | +Example backend metadata file: |
| 38 | +```c |
| 39 | +#ifndef CUSTOM_FIPS202_BACKEND_H |
| 40 | +#define CUSTOM_FIPS202_BACKEND_H |
| 41 | + |
| 42 | +/* Indicate we're replacing 1-fold Keccak-f1600 */ |
| 43 | +#define MLK_USE_FIPS202_X1_NATIVE |
| 44 | + |
| 45 | +/* Include the implementation */ |
| 46 | +#include "custom/src/keccak_impl.h" |
| 47 | + |
| 48 | +#endif |
| 49 | +``` |
| 50 | + |
| 51 | +## Notes |
| 52 | + |
| 53 | +- The tiny_sha3 code uses byte-reversed Keccak state on big-endian targets; this example removes |
| 54 | + that reversal since mlkem-native's frontend assumes standard byte order |
32 | 55 |
|
33 | 56 | ## Usage |
34 | 57 |
|
35 | | -Build this example with `make build`, run with `make run`. |
| 58 | +```bash |
| 59 | +make build # Build the example |
| 60 | +make run # Run the example |
| 61 | +``` |
| 62 | + |
| 63 | +## Warning |
| 64 | + |
| 65 | +The `randombytes()` implementation in `test_only_rng/` is for TESTING ONLY. |
| 66 | +You MUST provide a cryptographically secure RNG for production use. |
36 | 67 |
|
37 | 68 | <!--- bibliography ---> |
38 | 69 | [^tiny_sha3]: Markku-Juhani O. Saarinen: tiny_sha3, [https://github.com/mjosaarinen/tiny_sha3](https://github.com/mjosaarinen/tiny_sha3) |
0 commit comments