Files
n_signer/firmware/teensy41/signer/tests/host_test_mlkem768.c
T
Laan Tungir ac2e6347a2 v0.1.6 - Teensy 4.1 SD-card OTP pad: real implementation working on hardware
- otppad_embedded: bit-compatible port of libotppad (2386/2386 host tests pass)
- otp_pad_sd: SdFat-direct SD card pad reader (FAT-only, ASCII armor + binary .otp)
- pad_gen.ino: TRNG-sourced 1 MB pad generator using i.MX RT1062 TRNG registers
- Linker script: moved .rodata from DTCM to FLASH (EXCLUDE_FILE ed25519),
  reclaiming 124 KB DTCM, free stack 5.9 KB -> 130.9 KB
- check_stack.sh: build-time FlexRAM stack gauge, wired into build_signer.sh
- test_otp_sd.py: 8/9 hardware tests pass (ASCII + binary round-trips,
  offset advance, tamper detection; 10 KB plaintext times out on perf)
- test_classical.py: 16/16 pass with new memory layout (ed25519 OK)
- Memory evaluation document: plans/teensy41_memory_evaluation.md
2026-07-30 17:10:50 -04:00

88 lines
2.9 KiB
C

/* host_test_mlkem768.c — host-side end-to-end test of ML-KEM-768
* keygen + encapsulate + decapsulate.
*
* Reproduces the test_signer.py encapsulate/decapsulate flow on host:
* 1. crypto_kem_keypair()
* 2. crypto_kem_enc() -> (ct, ss_enc)
* 3. crypto_kem_dec() -> ss_dec
* 4. Check ss_enc == ss_dec
*
* Build (from the repo root):
* cc -O2 -Wall -Wextra -D HOST_TEST -o host_test_mlkem768 \
* -I firmware/teensy41/signer/src/pqclean/crypto_kem/ml-kem-768 \
* -I firmware/teensy41/signer/src/pqclean/common \
* firmware/teensy41/signer/src/pqclean/crypto_kem/ml-kem-768/*.c \
* firmware/teensy41/signer/src/pqclean/common/fips202.c \
* firmware/teensy41/signer/src/pqclean/common/sha2.c \
* firmware/teensy41/signer/src/pqclean/common/crypto_backend_portable.c \
* firmware/teensy41/signer/tests/host_test_mlkem768.c -lm
* ./host_test_mlkem768
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "api.h"
static uint64_t rng_state = 0x2545F4914F6CDD1DULL;
int randombytes(uint8_t *buf, size_t len) {
size_t i;
for (i = 0; i < len; i++) {
rng_state = rng_state * 6364136223846793005ULL + 1442695040888963407ULL;
buf[i] = (uint8_t)(rng_state >> 56);
}
return 0;
}
int main(void) {
uint8_t pk[ML_KEM_768_CRYPTO_PUBLICKEYBYTES];
uint8_t sk[ML_KEM_768_CRYPTO_SECRETKEYBYTES];
uint8_t ct[ML_KEM_768_CRYPTO_CIPHERTEXTBYTES];
uint8_t ss_enc[ML_KEM_768_CRYPTO_BYTES];
uint8_t ss_dec[ML_KEM_768_CRYPTO_BYTES];
int trial, fails = 0;
const int NTRIALS = 10;
printf("== ML-KEM-768 keygen+encaps+decaps host test (%d trials) ==\n", NTRIALS);
for (trial = 0; trial < NTRIALS; trial++) {
rng_state = 0x2545F4914F6CDD1DULL ^ ((uint64_t)(trial + 1) * 0x9E3779B97F4A7C15ULL);
if (crypto_kem_keypair(pk, sk) != 0) {
printf("FAIL [trial %d] keygen\n", trial);
return 1;
}
if (crypto_kem_enc(ct, ss_enc, pk) != 0) {
printf("FAIL [trial %d] encaps\n", trial);
fails++;
continue;
}
if (crypto_kem_dec(ss_dec, ct, sk) != 0) {
printf("FAIL [trial %d] decaps (rc != 0)\n", trial);
fails++;
continue;
}
if (memcmp(ss_enc, ss_dec, ML_KEM_768_CRYPTO_BYTES) != 0) {
printf("FAIL [trial %d] shared secret mismatch:\n enc: ", trial);
for (int i = 0; i < 32; i++) printf("%02x", ss_enc[i]);
printf("\n dec: ");
for (int i = 0; i < 32; i++) printf("%02x", ss_dec[i]);
printf("\n");
fails++;
continue;
}
printf("PASS [trial %d] keygen+encaps+decaps (ss match)\n", trial);
}
if (fails == 0) {
printf("\nALL ML-KEM-768 TESTS PASSED\n");
return 0;
}
printf("\n%d TEST(S) FAILED\n", fails);
return 1;
}