Premier commit
Cette révision appartient à :
révision
aee5ad5442
118 fichiers modifiés
+16226
Aucun fichier ne correspond à votre recherche
@@ -0,0 +1,660 @@
|
||||
/* ============================================================================
|
||||
* test_vercors.c -- harnais de validation du coeur VERCORS assembleur.
|
||||
*
|
||||
* Deux niveaux :
|
||||
* 1. vecteurs officiels sur les primitives (FIPS 180-4, FIPS 197,
|
||||
* NIST SP 800-38A) -- si ceux-la passent, l'assembleur est juste ;
|
||||
* 2. cycle protocolaire complet sur la radio bouclee.
|
||||
*
|
||||
* Le cycle demande par le prompt s'arrete a la bascule de cle : ce point [5]
|
||||
* n'est pas specifie sans le manifeste Gastinel Frame et n'a donc pas ete
|
||||
* devine. Le test le signale explicitement plutot que de le declarer reussi.
|
||||
* ==========================================================================*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "vercors.h"
|
||||
#include "vc_radio.h"
|
||||
#include "vc_imu.h"
|
||||
#include "vc_baro.h"
|
||||
|
||||
/* Points de controle exposes par les stubs, absents des interfaces perennes. */
|
||||
void vc_radio_stub_set_clock(uint32_t seconds_since_midnight);
|
||||
void vc_imu_stub_arm_fall(void);
|
||||
|
||||
static int g_fail;
|
||||
|
||||
static void ok(const char *name, int cond)
|
||||
{
|
||||
printf(" [%s] %s\n", cond ? "PASS" : "FAIL", name);
|
||||
if (!cond) g_fail++;
|
||||
}
|
||||
|
||||
static void from_hex(uint8_t *d, const char *h)
|
||||
{
|
||||
size_t n = strlen(h) / 2, i;
|
||||
for (i = 0; i < n; i++) {
|
||||
unsigned v;
|
||||
sscanf(h + 2 * i, "%2x", &v);
|
||||
d[i] = (uint8_t)v;
|
||||
}
|
||||
}
|
||||
|
||||
static int hex_is(const uint8_t *d, size_t n, const char *want)
|
||||
{
|
||||
char s[257];
|
||||
size_t i;
|
||||
for (i = 0; i < n; i++) sprintf(s + 2 * i, "%02x", d[i]);
|
||||
return strcmp(s, want) == 0;
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
* 1. Primitives
|
||||
* ========================================================================*/
|
||||
|
||||
static void test_sha256(void)
|
||||
{
|
||||
uint8_t out[32];
|
||||
char *big;
|
||||
|
||||
puts("\n-- SHA-256 (FIPS 180-4) --");
|
||||
|
||||
vc_sha256("", 0, out);
|
||||
ok("chaine vide", hex_is(out, 32,
|
||||
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"));
|
||||
|
||||
vc_sha256("abc", 3, out);
|
||||
ok("abc", hex_is(out, 32,
|
||||
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"));
|
||||
|
||||
vc_sha256("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56, out);
|
||||
ok("448 bits (2 blocs)", hex_is(out, 32,
|
||||
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"));
|
||||
|
||||
big = malloc(1000000);
|
||||
memset(big, 'a', 1000000);
|
||||
vc_sha256(big, 1000000, out);
|
||||
ok("1 million de 'a'", hex_is(out, 32,
|
||||
"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"));
|
||||
free(big);
|
||||
|
||||
/* Le chemin incremental doit donner le meme resultat que le one-shot :
|
||||
* c'est le code de bufferisation partielle qui est teste ici. */
|
||||
{
|
||||
vc_sha256_ctx c;
|
||||
uint8_t inc[32], one[32];
|
||||
const char *m = "Gastinel Frame / VERCORS / 169 MHz";
|
||||
size_t i, l = strlen(m);
|
||||
vc_sha256_init(&c);
|
||||
for (i = 0; i < l; i++) vc_sha256_update(&c, m + i, 1);
|
||||
vc_sha256_final(&c, inc);
|
||||
vc_sha256(m, l, one);
|
||||
ok("incremental == one-shot", memcmp(inc, one, 32) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_aes256(void)
|
||||
{
|
||||
uint8_t k[32], b[64], ctr[16], p[37], c[37];
|
||||
vc_aes_rk rk;
|
||||
int i;
|
||||
|
||||
puts("\n-- AES-256 logiciel (FIPS 197 / SP 800-38A) --");
|
||||
|
||||
from_hex(k, "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
|
||||
vc_aes256_expand_key(k, &rk);
|
||||
from_hex(b, "00112233445566778899aabbccddeeff");
|
||||
vc_aes256_encrypt_block(&rk, b, b);
|
||||
ok("FIPS 197 C.3 bloc", hex_is(b, 16, "8ea2b7ca516745bfeafc49904b496089"));
|
||||
|
||||
from_hex(k, "603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
|
||||
vc_aes256_expand_key(k, &rk);
|
||||
from_hex(ctr, "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
|
||||
from_hex(b, "6bc1bee22e409f96e93d7e117393172a"
|
||||
"ae2d8a571e03ac9c9eb76fac45af8e51"
|
||||
"30c81c46a35ce411e5fbc1191a0a52ef"
|
||||
"f69f2445df4f9b17ad2b417be66c3710");
|
||||
vc_aes256_ctr_xor(&rk, ctr, b, 64);
|
||||
ok("SP 800-38A F.5.5 CTR", hex_is(b, 64,
|
||||
"601ec313775789a5b7a7f504bbf3d228"
|
||||
"f443e3ca4d62b59aca84e990cacaf5c5"
|
||||
"2b0930daa23de94ce87017ba2d84988d"
|
||||
"dfc9c58db67aada613c2dd08457941a6"));
|
||||
|
||||
for (i = 0; i < 37; i++) p[i] = (uint8_t)(i * 7 + 1);
|
||||
memcpy(c, p, 37);
|
||||
from_hex(ctr, "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
|
||||
vc_aes256_ctr_xor(&rk, ctr, c, 37);
|
||||
from_hex(ctr, "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
|
||||
vc_aes256_ctr_xor(&rk, ctr, c, 37);
|
||||
ok("CTR aller-retour, longueur non alignee", memcmp(p, c, 37) == 0);
|
||||
|
||||
from_hex(ctr, "ffffffffffffffffffffffffffffffff");
|
||||
memset(b, 0, 16);
|
||||
vc_aes256_ctr_xor(&rk, ctr, b, 16);
|
||||
ok("compteur : propagation de retenue",
|
||||
hex_is(ctr, 16, "00000000000000000000000000000000"));
|
||||
}
|
||||
|
||||
static void test_const_time(void)
|
||||
{
|
||||
uint8_t a[32], b[32], d[8];
|
||||
|
||||
puts("\n-- Primitives a temps constant --");
|
||||
|
||||
memset(a, 0xA5, 32);
|
||||
memcpy(b, a, 32);
|
||||
ok("memcmp : zones egales", vc_ct_memcmp(a, b, 32) == 0);
|
||||
|
||||
b[31] ^= 0x01; /* difference sur le dernier octet */
|
||||
ok("memcmp : ecart en fin", vc_ct_memcmp(a, b, 32) == 1);
|
||||
memcpy(b, a, 32);
|
||||
b[0] ^= 0x80; /* difference sur le premier octet */
|
||||
ok("memcmp : ecart en tete", vc_ct_memcmp(a, b, 32) == 1);
|
||||
|
||||
ok("mask_eq vrai", vc_ct_mask_eq_u32(42, 42) == 0xFFFFFFFFFFFFFFFFull);
|
||||
ok("mask_eq faux", vc_ct_mask_eq_u32(42, 43) == 0);
|
||||
ok("mask_lt vrai", vc_ct_mask_lt_u32(41, 42) == 0xFFFFFFFFFFFFFFFFull);
|
||||
ok("mask_lt faux", vc_ct_mask_lt_u32(42, 42) == 0);
|
||||
/* Bornes non signees : 0xFFFFFFFF ne doit pas etre vu comme negatif. */
|
||||
ok("mask_lt non signe", vc_ct_mask_lt_u32(0xFFFFFFFFu, 1) == 0);
|
||||
|
||||
memset(d, 0x11, 8);
|
||||
vc_ct_cmov(d, "ABCDEFGH", 8, 0);
|
||||
ok("cmov masque nul : inchange", d[0] == 0x11 && d[7] == 0x11);
|
||||
vc_ct_cmov(d, "ABCDEFGH", 8, 0xFFFFFFFFFFFFFFFFull);
|
||||
ok("cmov masque plein : copie", memcmp(d, "ABCDEFGH", 8) == 0);
|
||||
|
||||
memset(a, 0xFF, 32);
|
||||
vc_ct_memzero(a, 32);
|
||||
{
|
||||
int i, z = 1;
|
||||
for (i = 0; i < 32; i++) if (a[i]) z = 0;
|
||||
ok("memzero", z);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_csprng(void)
|
||||
{
|
||||
vc_csprng_ctx r1, r2;
|
||||
uint8_t o1[100], o2[100], o3[100];
|
||||
int i, zeros;
|
||||
|
||||
puts("\n-- CSPRNG maison (DRBG SHA-256) --");
|
||||
|
||||
vc_csprng_seed(&r1, "graine-de-test", 14);
|
||||
vc_csprng_seed(&r2, "graine-de-test", 14);
|
||||
vc_csprng_bytes(&r1, o1, 100);
|
||||
vc_csprng_bytes(&r2, o2, 100);
|
||||
ok("deterministe a graine egale", memcmp(o1, o2, 100) == 0);
|
||||
|
||||
vc_csprng_bytes(&r1, o3, 100);
|
||||
ok("blocs successifs differents", memcmp(o1, o3, 100) != 0);
|
||||
|
||||
vc_csprng_seed(&r2, "autre-graine", 12);
|
||||
vc_csprng_bytes(&r2, o2, 100);
|
||||
ok("graines differentes divergent", memcmp(o1, o2, 100) != 0);
|
||||
|
||||
/* 100 octets tous nuls signalerait un generateur mort. */
|
||||
for (i = 0, zeros = 0; i < 100; i++) if (o1[i] == 0) zeros++;
|
||||
ok("sortie non degeneree", zeros < 10);
|
||||
|
||||
/* Le decoupage en blocs de 32 ne doit pas creer de discontinuite : une
|
||||
* demande de 100 octets doit prefixer une demande de 40. */
|
||||
vc_csprng_seed(&r1, "coupe", 5);
|
||||
vc_csprng_bytes(&r1, o1, 100);
|
||||
vc_csprng_seed(&r2, "coupe", 5);
|
||||
vc_csprng_bytes(&r2, o2, 40);
|
||||
ok("prefixe coherent (40 sur 100)", memcmp(o1, o2, 40) == 0);
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
* 2. Heure Cible [2]
|
||||
* ========================================================================*/
|
||||
|
||||
static void test_target_hour(void)
|
||||
{
|
||||
uint8_t grain[32];
|
||||
uint32_t h, h2;
|
||||
int i;
|
||||
unsigned long buckets[24];
|
||||
int all_in_range = 1, any_bucket_empty = 0;
|
||||
|
||||
puts("\n-- [2] Heure Cible --");
|
||||
|
||||
memset(grain, 0, 32);
|
||||
h = vc_derive_target_hour(grain, 32);
|
||||
h2 = vc_derive_target_hour(grain, 32);
|
||||
ok("deterministe : meme grain, meme heure", h == h2);
|
||||
ok("dans 0..86399", h < VC_DAY_SECONDS);
|
||||
|
||||
/* Vecteurs de reference calcules independamment de l'assembleur.
|
||||
* Grain de 32 zeros : SHA256 = 66687aad..., x = 0x66687aad = 1718123181,
|
||||
* inferieur au seuil de rejet donc accepte au premier tirage,
|
||||
* 1718123181 mod 86400 = 59181.
|
||||
* Grain de 32 octets 0xff : meme procedure, 40182.
|
||||
* Ces deux valeurs figent a la fois la lecture big-endian des 4 premiers
|
||||
* octets et la reduction modulo : tout implementeur d'une autre extremite
|
||||
* doit retrouver exactement ces nombres. */
|
||||
ok("vecteur de reference : grain nul", h == 59181u);
|
||||
{
|
||||
uint8_t gf[32];
|
||||
memset(gf, 0xff, 32);
|
||||
ok("vecteur de reference : grain 0xff",
|
||||
vc_derive_target_hour(gf, 32) == 40182u);
|
||||
}
|
||||
|
||||
/* Deux grains voisins doivent donner des heures sans rapport. */
|
||||
grain[0] = 1;
|
||||
ok("sensibilite au grain", vc_derive_target_hour(grain, 32) != h);
|
||||
|
||||
/* Repartition : 4096 grains sur 24 tranches horaires. Avec un modulo nu,
|
||||
* le biais serait de l'ordre de 5e-6, invisible ici -- ce test ne prouve
|
||||
* donc pas l'absence de biais, il detecte un generateur franchement casse
|
||||
* (heures concentrees, tranches vides). L'absence de biais tient a la
|
||||
* methode de rejet, qui est verifiee par construction dans le code. */
|
||||
memset(buckets, 0, sizeof buckets);
|
||||
for (i = 0; i < 4096; i++) {
|
||||
uint8_t g[32];
|
||||
memset(g, 0, 32);
|
||||
g[0] = (uint8_t)(i & 0xFF);
|
||||
g[1] = (uint8_t)(i >> 8);
|
||||
{
|
||||
uint32_t t = vc_derive_target_hour(g, 32);
|
||||
if (t >= VC_DAY_SECONDS) all_in_range = 0;
|
||||
buckets[t / 3600]++;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < 24; i++) if (buckets[i] == 0) any_bucket_empty = 1;
|
||||
ok("4096 tirages tous dans le domaine", all_in_range);
|
||||
ok("les 24 tranches horaires sont couvertes", !any_bucket_empty);
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
* 3. Trame : bourrage [7] et chiffrement [8]
|
||||
* ========================================================================*/
|
||||
|
||||
static void test_frame(void)
|
||||
{
|
||||
vc_csprng_ctx rng;
|
||||
uint8_t f1[64], f2[64], out[64];
|
||||
size_t n;
|
||||
const char *msg = "ALT1483 T+4.2 BAT87";
|
||||
int i, zeros;
|
||||
|
||||
puts("\n-- [7] Bourrage a taille fixe / [8] chiffrement --");
|
||||
|
||||
vc_csprng_seed(&rng, "graine-trame", 12);
|
||||
|
||||
ok("charge trop longue refusee",
|
||||
vc_frame_pad(&rng, (const uint8_t *)msg, 64, f1) == -1);
|
||||
|
||||
ok("bourrage accepte",
|
||||
vc_frame_pad(&rng, (const uint8_t *)msg, strlen(msg), f1) == 0);
|
||||
|
||||
ok("aller-retour bourrage", vc_frame_unpad(f1, out, &n) == 0
|
||||
&& n == strlen(msg)
|
||||
&& memcmp(out, msg, n) == 0);
|
||||
|
||||
/* Le remplissage doit etre du bruit, pas des zeros : sous CTR, une queue
|
||||
* de zeros exposerait le flot de cle en clair. */
|
||||
for (i = (int)strlen(msg) + 1, zeros = 0; i < 64; i++)
|
||||
if (f1[i] == 0) zeros++;
|
||||
ok("remplissage non nul", zeros < 4);
|
||||
|
||||
/* Deux trames de meme contenu doivent differer : le bourrage varie. */
|
||||
vc_frame_pad(&rng, (const uint8_t *)msg, strlen(msg), f2);
|
||||
ok("deux trames identiques different par le bruit",
|
||||
memcmp(f1, f2, 64) != 0);
|
||||
|
||||
/* Chiffrement CTR d'une trame complete, puis dechiffrement. */
|
||||
{
|
||||
uint8_t key[32], ctr[16], ctr_save[16], cipher[64];
|
||||
vc_aes_rk rk;
|
||||
|
||||
vc_csprng_bytes(&rng, key, 32);
|
||||
vc_csprng_bytes(&rng, ctr, 16);
|
||||
memcpy(ctr_save, ctr, 16);
|
||||
vc_aes256_expand_key(key, &rk);
|
||||
|
||||
memcpy(cipher, f1, 64);
|
||||
vc_aes256_ctr_xor(&rk, ctr, cipher, 64);
|
||||
ok("le chiffre differe du clair", memcmp(cipher, f1, 64) != 0);
|
||||
|
||||
memcpy(ctr, ctr_save, 16);
|
||||
vc_aes256_ctr_xor(&rk, ctr, cipher, 64);
|
||||
ok("dechiffrement restitue la trame", memcmp(cipher, f1, 64) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
/* ==========================================================================
|
||||
* 4. Cycle complet sur radio bouclee : gating temporel [4]
|
||||
* ========================================================================*/
|
||||
|
||||
static void test_full_cycle(void)
|
||||
{
|
||||
vc_csprng_ctx rng;
|
||||
vc_aes_rk rk;
|
||||
uint8_t grain[32], key[32], ctr[16], ctr_tx[16];
|
||||
uint8_t frame[64], rx[64], plain[64];
|
||||
uint32_t target, arrival;
|
||||
size_t n;
|
||||
const char *msg = "ALT1483 T+4.2 BAT87";
|
||||
int aes_calls_avoided;
|
||||
|
||||
puts("\n-- Cycle complet : grain -> heure cible -> gating -> trame --");
|
||||
|
||||
vc_radio_init();
|
||||
vc_imu_init();
|
||||
vc_baro_init();
|
||||
|
||||
/* [1] Le Grain de Sel est ici FOURNI, pas derive : sa generation isolee
|
||||
* hebdomadaire n'est pas specifiee sans le manifeste. Les deux
|
||||
* extremites partagent donc simplement le meme tableau. */
|
||||
vc_csprng_seed(&rng, "semaine-2026-36", 15);
|
||||
vc_csprng_bytes(&rng, grain, 32);
|
||||
|
||||
/* [2] Chaque cote calcule l'Heure Cible de son cote, sans rien emettre. */
|
||||
target = vc_derive_target_hour(grain, 32);
|
||||
printf(" Heure Cible = %u s (%02u:%02u:%02u)\n",
|
||||
target, target / 3600, (target / 60) % 60, target % 60);
|
||||
ok("heure cible dans la journee", target < VC_DAY_SECONDS);
|
||||
|
||||
vc_csprng_bytes(&rng, key, 32);
|
||||
vc_csprng_bytes(&rng, ctr, 16);
|
||||
vc_aes256_expand_key(key, &rk);
|
||||
|
||||
/* --- emission --- */
|
||||
vc_frame_pad(&rng, (const uint8_t *)msg, strlen(msg), frame);
|
||||
memcpy(ctr_tx, ctr, 16);
|
||||
{
|
||||
uint8_t on_air[64];
|
||||
memcpy(on_air, frame, 64);
|
||||
vc_aes256_ctr_xor(&rk, ctr_tx, on_air, 64);
|
||||
ok("emission", vc_radio_send(on_air, 64) == VC_HAL_OK);
|
||||
}
|
||||
|
||||
/* --- reception AVANT l'Heure Cible : rejet attendu, sans AES --- */
|
||||
aes_calls_avoided = 0;
|
||||
arrival = (target == 0) ? 0 : target - 1;
|
||||
vc_radio_stub_set_clock(arrival);
|
||||
ok("trame recue", vc_radio_recv(rx, sizeof rx) == 64);
|
||||
|
||||
if (target > 0) {
|
||||
uint64_t accept = vc_gate_accept(vc_radio_last_rx_seconds(), target);
|
||||
ok("gating : trame trop tot rejetee", accept == 0);
|
||||
if (accept == 0) aes_calls_avoided++; /* AES n'est pas lance */
|
||||
ok("aucun dechiffrement tente avant l'heure cible",
|
||||
aes_calls_avoided == 1);
|
||||
} else {
|
||||
puts(" [SKIP] heure cible a 0 s, pas d'instant anterieur a tester");
|
||||
}
|
||||
|
||||
/* --- reception APRES l'Heure Cible : acceptation et dechiffrement --- */
|
||||
{
|
||||
uint8_t on_air[64];
|
||||
uint8_t ctr_rx[16];
|
||||
|
||||
memcpy(on_air, frame, 64);
|
||||
memcpy(ctr_tx, ctr, 16);
|
||||
vc_aes256_ctr_xor(&rk, ctr_tx, on_air, 64);
|
||||
vc_radio_send(on_air, 64);
|
||||
|
||||
arrival = target + 30;
|
||||
if (arrival >= VC_DAY_SECONDS) arrival = VC_DAY_SECONDS - 1;
|
||||
vc_radio_stub_set_clock(arrival);
|
||||
vc_radio_recv(rx, sizeof rx);
|
||||
|
||||
ok("gating : trame a l'heure acceptee",
|
||||
vc_gate_accept(vc_radio_last_rx_seconds(), target)
|
||||
== 0xFFFFFFFFFFFFFFFFull);
|
||||
|
||||
memcpy(ctr_rx, ctr, 16);
|
||||
memcpy(plain, rx, 64);
|
||||
vc_aes256_ctr_xor(&rk, ctr_rx, plain, 64);
|
||||
|
||||
ok("dechiffrement puis retrait du bourrage",
|
||||
vc_frame_unpad(plain, (uint8_t *)rx, &n) == 0
|
||||
&& n == strlen(msg)
|
||||
&& memcmp(rx, msg, n) == 0);
|
||||
}
|
||||
|
||||
/* [5] est couvert par sa propre suite de tests, plus bas. */
|
||||
|
||||
/* HAL : verification que les stubs repondent au contrat. */
|
||||
{
|
||||
vc_imu_accel_t a;
|
||||
uint32_t pa;
|
||||
int32_t t;
|
||||
ok("HAL IMU", vc_imu_read_accel(&a) == VC_HAL_OK && a.z == 1000);
|
||||
vc_imu_stub_arm_fall();
|
||||
ok("HAL IMU drapeau de chute",
|
||||
vc_imu_fall_detected() == 1 && vc_imu_fall_detected() == 0);
|
||||
ok("HAL barometre", vc_baro_read_pressure(&pa) == VC_HAL_OK
|
||||
&& vc_baro_read_temp(&t) == VC_HAL_OK
|
||||
&& pa > 50000 && pa < 110000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* ==========================================================================
|
||||
* 5. Points [1] [3] [5] [6] -- hypotheses provisoires
|
||||
* ========================================================================*/
|
||||
|
||||
static void test_grain(void)
|
||||
{
|
||||
uint8_t g1[32], g2[32], g3[32];
|
||||
const uint8_t *km = vc_factory_key();
|
||||
|
||||
puts("\n-- [1] Grain de Sel Temporel (hypothese) --");
|
||||
|
||||
/* La cle de test doit crier ce qu'elle est. */
|
||||
ok("K_M de test explicitement marquee", memcmp(km, "PLACEHOLDER", 11) == 0);
|
||||
|
||||
/* Les deux extremites, partageant K_M et la semaine, derivent le meme
|
||||
* grain sans rien echanger : propriete centrale du protocole. */
|
||||
vc_derive_grain(km, 36, 0, g1);
|
||||
vc_derive_grain(km, 36, 0, g2);
|
||||
ok("derivation isolee : deux cotes, meme grain", memcmp(g1, g2, 32) == 0);
|
||||
|
||||
vc_derive_grain(km, 37, 0, g3);
|
||||
ok("semaine differente -> grain different", memcmp(g1, g3, 32) != 0);
|
||||
|
||||
vc_derive_grain(km, 36, 1, g3);
|
||||
ok("compteur anti-rejeu different -> grain different", memcmp(g1, g3, 32) != 0);
|
||||
|
||||
{
|
||||
uint8_t km2[32];
|
||||
memcpy(km2, km, 32);
|
||||
km2[0] ^= 0x01;
|
||||
vc_derive_grain(km2, 36, 0, g3);
|
||||
ok("K_M differente -> grain different", memcmp(g1, g3, 32) != 0);
|
||||
}
|
||||
|
||||
/* Vecteur de reference : fige l'encodage BE16 de la semaine et BE32 du
|
||||
* compteur. Toute autre implementation doit retrouver ce grain. */
|
||||
{
|
||||
vc_sha256_ctx c;
|
||||
uint8_t ref[32], tail[6];
|
||||
tail[0] = 0; tail[1] = 36;
|
||||
tail[2] = 0; tail[3] = 0; tail[4] = 0; tail[5] = 7;
|
||||
vc_sha256_init(&c);
|
||||
vc_sha256_update(&c, km, 32);
|
||||
vc_sha256_update(&c, tail, 6);
|
||||
vc_sha256_final(&c, ref);
|
||||
vc_derive_grain(km, 36, 7, g1);
|
||||
ok("encodage conforme (BE16 semaine, BE32 compteur)",
|
||||
memcmp(g1, ref, 32) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_guard(void)
|
||||
{
|
||||
vc_guard_ctx g;
|
||||
|
||||
puts("\n-- [3] Fenetre de Garde Dynamique (hypothese) --");
|
||||
|
||||
ok("largeur au repos", vc_guard_width(0) == VC_GUARD_MIN_SECONDS);
|
||||
|
||||
/* Croissance lineaire calee sur la derive du quartz : 20 ppm, soit
|
||||
* environ 1,73 s par jour, tronque a 1 s en arithmetique entiere. */
|
||||
ok("elargissement apres un jour", vc_guard_width(86400) == 3);
|
||||
ok("monotone", vc_guard_width(3600) <= vc_guard_width(7200));
|
||||
|
||||
/* Le plafond doit tenir meme apres un silence de plusieurs jours, sinon
|
||||
* le gating temporel finirait par ne plus rien filtrer. */
|
||||
ok("plafonnee a 30 jours de silence",
|
||||
vc_guard_width(30u * 86400u) == VC_GUARD_MAX_SECONDS);
|
||||
ok("plafonnee a 10 ans de silence",
|
||||
vc_guard_width(3650u * 86400u) == VC_GUARD_MAX_SECONDS);
|
||||
|
||||
vc_guard_init(&g);
|
||||
g.last_valid_sec = 0;
|
||||
ok("fenetre large avant resynchro",
|
||||
vc_guard_width_now(&g, 20u * 86400u) == VC_GUARD_MAX_SECONDS);
|
||||
vc_guard_on_valid(&g, 20u * 86400u, 20u * 86400u + 5u);
|
||||
ok("resserrement immediat des reception valide",
|
||||
vc_guard_width_now(&g, 20u * 86400u) == VC_GUARD_MIN_SECONDS);
|
||||
ok("recalage d'horloge memorise", g.clock_offset == 5);
|
||||
ok("horloge en arriere bornee",
|
||||
vc_guard_width_now(&g, 20u * 86400u - 100u) == VC_GUARD_MIN_SECONDS);
|
||||
|
||||
/* Gating avec tolerance : seul le bord inferieur est relache. */
|
||||
ok("trop tot au-dela de la tolerance : rejete",
|
||||
vc_gate_accept_window(1000, 1100, 30) == 0);
|
||||
ok("un peu tot mais dans la tolerance : accepte",
|
||||
vc_gate_accept_window(1080, 1100, 30) == 0xFFFFFFFFFFFFFFFFull);
|
||||
ok("apres l'heure cible : accepte",
|
||||
vc_gate_accept_window(5000, 1100, 30) == 0xFFFFFFFFFFFFFFFFull);
|
||||
/* Heure Cible proche de minuit : target - width doit saturer a zero, pas
|
||||
* boucler vers quatre milliards. */
|
||||
ok("saturation a zero pres de minuit",
|
||||
vc_gate_accept_window(5, 10, 30) == 0xFFFFFFFFFFFFFFFFull);
|
||||
}
|
||||
|
||||
static void test_coldstart(void)
|
||||
{
|
||||
vc_guard_ctx g;
|
||||
|
||||
puts("\n-- [6] Cold Start (hypothese) --");
|
||||
|
||||
vc_guard_init(&g);
|
||||
ok("apres reboot : resynchro exigee", vc_coldstart_needed(&g, 0) == 1);
|
||||
|
||||
vc_guard_on_valid(&g, 1000, 1000);
|
||||
ok("apres un paquet valide : pas de resynchro",
|
||||
vc_coldstart_needed(&g, 1000) == 0);
|
||||
ok("silence de 6 jours : pas encore",
|
||||
vc_coldstart_needed(&g, 1000 + 6u * 86400u) == 0);
|
||||
ok("silence de 7 jours : resynchro exigee",
|
||||
vc_coldstart_needed(&g, 1000 + 7u * 86400u) == 1);
|
||||
}
|
||||
|
||||
static void test_key_switch(void)
|
||||
{
|
||||
vc_csprng_ctx rng;
|
||||
vc_aes_rk day_rk;
|
||||
vc_frame_req req;
|
||||
vc_frame_out out;
|
||||
uint8_t day_key[32], next_key[32], key_ctr[16], frame[64], frame2[64];
|
||||
const char *msg = "RELEVE 12H";
|
||||
int i, zeros;
|
||||
|
||||
puts("\n-- [5] Bascule de cle steganographique (hypothese) --");
|
||||
|
||||
vc_csprng_seed(&rng, "graine-bascule", 14);
|
||||
vc_csprng_bytes(&rng, day_key, 32);
|
||||
vc_csprng_bytes(&rng, next_key, 32);
|
||||
vc_csprng_bytes(&rng, key_ctr, 16);
|
||||
vc_ctr_set_domain(key_ctr, VC_CTR_DOMAIN_KEYWRAP);
|
||||
vc_aes256_expand_key(day_key, &day_rk);
|
||||
|
||||
memset(&req, 0, sizeof req);
|
||||
req.rng = &rng;
|
||||
req.payload = (const uint8_t *)msg;
|
||||
req.payload_len = strlen(msg);
|
||||
req.next_key = next_key;
|
||||
req.day_rk = &day_rk;
|
||||
req.key_ctr = key_ctr;
|
||||
|
||||
ok("trame avec bascule construite", vc_frame_build(&req, frame) == 0);
|
||||
|
||||
/* La cle N+1 est chiffree sous la cle du jour avant d'etre logee : elle ne
|
||||
* doit pas apparaitre en clair, meme avant le chiffrement de la trame. */
|
||||
ok("cle N+1 absente en clair de la trame",
|
||||
memcmp(frame + 32, next_key, 32) != 0);
|
||||
|
||||
ok("lecture de la trame",
|
||||
vc_frame_parse(frame, &day_rk, key_ctr, &out) == 0);
|
||||
ok("bascule signalee", out.has_next_key == 1);
|
||||
ok("charge utile intacte", out.payload_len == strlen(msg)
|
||||
&& memcmp(out.payload, msg, out.payload_len) == 0);
|
||||
ok("cle N+1 restituee", memcmp(out.next_key, next_key, 32) == 0);
|
||||
|
||||
req.next_key = NULL;
|
||||
ok("trame sans bascule construite", vc_frame_build(&req, frame2) == 0);
|
||||
ok("meme taille avec et sans bascule", sizeof frame == sizeof frame2);
|
||||
ok("drapeau absent sans bascule", (frame2[0] & 0x80) == 0);
|
||||
ok("lecture sans bascule",
|
||||
vc_frame_parse(frame2, &day_rk, key_ctr, &out) == 0
|
||||
&& out.has_next_key == 0 && out.payload_len == strlen(msg));
|
||||
|
||||
for (i = (int)strlen(msg) + 1, zeros = 0; i < 32; i++)
|
||||
if (frame[i] == 0) zeros++;
|
||||
ok("bourrage residuel non nul", zeros < 4);
|
||||
|
||||
/* Budget : la bascule consomme les octets 32 a 63. */
|
||||
req.next_key = next_key;
|
||||
req.payload_len = VC_FRAME_KEYSW_MAXPAYLOAD + 1;
|
||||
ok("charge trop longue avec bascule refusee",
|
||||
vc_frame_build(&req, frame) == -1);
|
||||
req.payload_len = VC_FRAME_KEYSW_MAXPAYLOAD;
|
||||
ok("charge maximale avec bascule acceptee",
|
||||
vc_frame_build(&req, frame) == 0);
|
||||
|
||||
/* Separation de domaine : creneau de cle et trame ne doivent jamais
|
||||
* partager un flot de cle sous la meme cle du jour. */
|
||||
{
|
||||
uint8_t frame_ctr[16];
|
||||
memcpy(frame_ctr, key_ctr, 16);
|
||||
vc_ctr_set_domain(frame_ctr, VC_CTR_DOMAIN_FRAME);
|
||||
ok("domaines de compteur distincts",
|
||||
memcmp(frame_ctr, key_ctr, 16) != 0
|
||||
&& frame_ctr[0] == 0x00 && key_ctr[0] == 0x01);
|
||||
}
|
||||
}
|
||||
|
||||
/* ========================================================================*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
puts("=========================================");
|
||||
puts(" VERCORS -- validation du coeur x86-64");
|
||||
puts("=========================================");
|
||||
|
||||
test_sha256();
|
||||
test_aes256();
|
||||
test_const_time();
|
||||
test_csprng();
|
||||
test_target_hour();
|
||||
test_frame();
|
||||
test_full_cycle();
|
||||
test_grain();
|
||||
test_guard();
|
||||
test_coldstart();
|
||||
test_key_switch();
|
||||
|
||||
puts("\n=========================================");
|
||||
if (g_fail == 0) {
|
||||
puts(" RESULTAT : tous les tests passent");
|
||||
puts(" Points [1] [3] [5] [6] : implementes sur HYPOTHESES");
|
||||
puts(" provisoires -- a valider avant deploiement reel.");
|
||||
puts(" Point ouvert : aucune authentification de trame.");
|
||||
} else {
|
||||
printf(" RESULTAT : %d echec(s)\n", g_fail);
|
||||
}
|
||||
puts("=========================================");
|
||||
return g_fail != 0;
|
||||
}
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur