Premier commit
Cette révision appartient à :
révision
aee5ad5442
118 fichiers modifiés
+16226
Aucun fichier ne correspond à votre recherche
@@ -0,0 +1,212 @@
|
||||
/* ============================================================================
|
||||
* vercors.h -- interface C du coeur VERCORS ecrit en assembleur x86-64.
|
||||
*
|
||||
* Toutes les fonctions declarees ici sont implementees en assembleur, dans le
|
||||
* repertoire asm. Il n'y a aucune implementation C de la cryptographie
|
||||
* (contrainte projet).
|
||||
*
|
||||
* Les dispositions de structures doivent rester synchronisees avec les
|
||||
* %define correspondants dans les fichiers .asm. Les _Static_assert en fin de
|
||||
* fichier cassent la compilation si l'un des deux derive.
|
||||
* ==========================================================================*/
|
||||
|
||||
#ifndef VERCORS_H
|
||||
#define VERCORS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* --------------------------------------------------------------------------
|
||||
* Constantes du protocole
|
||||
* ------------------------------------------------------------------------*/
|
||||
#define VC_DAY_SECONDS 86400u /* domaine de l'Heure Cible : 0..86399 */
|
||||
#define VC_FRAME_BYTES 64u /* taille fixe d'une trame sur l'air */
|
||||
#define VC_FRAME_MAXPAYLOAD 63u /* 64 - 1 octet de longueur */
|
||||
#define VC_KEY_BYTES 32u /* AES-256 */
|
||||
#define VC_CTR_BYTES 16u
|
||||
#define VC_GRAIN_BYTES 32u /* Grain de Sel Temporel */
|
||||
|
||||
/* --------------------------------------------------------------------------
|
||||
* SHA-256 (asm/sha256.asm)
|
||||
* ------------------------------------------------------------------------*/
|
||||
typedef struct {
|
||||
uint32_t state[8]; /* +0 */
|
||||
uint64_t bitlen; /* +32 */
|
||||
uint64_t buflen; /* +40 */
|
||||
uint8_t buf[64]; /* +48 */
|
||||
} vc_sha256_ctx; /* 112 */
|
||||
|
||||
void vc_sha256_init (vc_sha256_ctx *ctx);
|
||||
void vc_sha256_update(vc_sha256_ctx *ctx, const void *data, size_t len);
|
||||
void vc_sha256_final (vc_sha256_ctx *ctx, uint8_t out[32]);
|
||||
void vc_sha256 (const void *data, size_t len, uint8_t out[32]);
|
||||
|
||||
/* --------------------------------------------------------------------------
|
||||
* AES-256 (asm/aes256.asm) -- logiciel, pas AES-NI. Chiffrement seul : le
|
||||
* mode CTR n'a jamais besoin du dechiffrement de bloc. Voir docs/adr/0002.
|
||||
* ------------------------------------------------------------------------*/
|
||||
typedef struct {
|
||||
uint8_t rk[240]; /* 15 cles de ronde de 16 octets */
|
||||
} vc_aes_rk;
|
||||
|
||||
void vc_aes256_expand_key (const uint8_t key[32], vc_aes_rk *rk);
|
||||
void vc_aes256_encrypt_block(const vc_aes_rk *rk, const uint8_t in[16],
|
||||
uint8_t out[16]);
|
||||
/* XOR sur place avec le flot CTR ; ctr est avance et laisse a jour. */
|
||||
void vc_aes256_ctr_xor (const vc_aes_rk *rk, uint8_t ctr[16],
|
||||
uint8_t *inout, size_t len);
|
||||
|
||||
/* --------------------------------------------------------------------------
|
||||
* Primitives a temps constant (asm/const_time.asm)
|
||||
* Convention de masque : 0xFFFFFFFFFFFFFFFF = vrai, 0 = faux.
|
||||
* ------------------------------------------------------------------------*/
|
||||
uint32_t vc_ct_memcmp (const void *a, const void *b, size_t len);
|
||||
uint64_t vc_ct_mask_eq_u32(uint32_t a, uint32_t b);
|
||||
uint64_t vc_ct_mask_lt_u32(uint32_t a, uint32_t b);
|
||||
void vc_ct_cmov (void *dst, const void *src, size_t len,
|
||||
uint64_t mask);
|
||||
void vc_ct_memzero (void *p, size_t len);
|
||||
|
||||
/* --------------------------------------------------------------------------
|
||||
* CSPRNG maison (asm/csprng.asm)
|
||||
* Ne fabrique pas d'entropie : la graine vient de l'exterieur (TRNG sur
|
||||
* STM32U5, harnais de test sur x86-64).
|
||||
* ------------------------------------------------------------------------*/
|
||||
typedef struct {
|
||||
uint8_t V[32]; /* +0 */
|
||||
uint64_t C; /* +32 */
|
||||
uint64_t seeded; /* +40 */
|
||||
} vc_csprng_ctx; /* 48 */
|
||||
|
||||
void vc_csprng_seed (vc_csprng_ctx *ctx, const void *seed, size_t len);
|
||||
void vc_csprng_reseed(vc_csprng_ctx *ctx, const void *seed, size_t len);
|
||||
void vc_csprng_bytes (vc_csprng_ctx *ctx, void *out, size_t len);
|
||||
|
||||
/* --------------------------------------------------------------------------
|
||||
* Protocole VERCORS (asm/vercors_protocol.asm)
|
||||
*
|
||||
* Points [2] [4] [7] [8] : entierement specifies par le cahier des charges.
|
||||
* Les points [1] [3] [5] [6] sont plus bas, dans une section a part, car ils
|
||||
* reposent sur des hypotheses provisoires et non sur une specification.
|
||||
* ------------------------------------------------------------------------*/
|
||||
|
||||
/* [2] Heure Cible : SHA256(grain) -> 4 premiers octets big-endian -> methode
|
||||
* de rejet -> uniforme sur 0..86399. */
|
||||
uint32_t vc_derive_target_hour(const uint8_t *grain, size_t grain_len);
|
||||
|
||||
/* [4] Gating temporel, a appeler AVANT tout dechiffrement.
|
||||
* Retourne un masque tout-a-un si la trame est recevable. */
|
||||
uint64_t vc_gate_accept(uint32_t arrival_sec, uint32_t target_sec);
|
||||
|
||||
/* [7] Bourrage a taille fixe. Retourne 0, ou -1 si len > 63. */
|
||||
int vc_frame_pad (vc_csprng_ctx *rng, const uint8_t *payload, size_t len,
|
||||
uint8_t out[64]);
|
||||
/* Retourne 0, ou -1 si l'octet de longueur est incoherent. */
|
||||
int vc_frame_unpad(const uint8_t in[64], uint8_t *out, size_t *out_len);
|
||||
|
||||
/* ==========================================================================
|
||||
* Points [1] [3] [5] [6] -- asm/vercors_keysched.asm
|
||||
*
|
||||
* HYPOTHESES PROVISOIRES, A VALIDER AVANT TOUT DEPLOIEMENT REEL.
|
||||
* Le manifeste Gastinel Frame ne descend pas a ce niveau de detail. Voir
|
||||
* docs/adr/0007 a 0010 et la section correspondante du README.
|
||||
* ========================================================================*/
|
||||
|
||||
/* Separation de domaine des compteurs CTR. Sans elle, le creneau de cle et la
|
||||
* trame partageraient un flot de cle sous la meme cle du jour -- reutilisation
|
||||
* de flot, faute mortelle du mode CTR. */
|
||||
#define VC_CTR_DOMAIN_FRAME 0x00u
|
||||
#define VC_CTR_DOMAIN_KEYWRAP 0x01u
|
||||
|
||||
void vc_ctr_set_domain(uint8_t ctr[16], uint8_t domain);
|
||||
|
||||
/* --- [1] Grain de Sel Temporel ------------------------------------------
|
||||
* grain = SHA256( K_M(32) || BE16(semaine_ISO) || BE32(compteur_anti_rejeu) )
|
||||
* Ne transite jamais par la radio : chaque extremite le recalcule. */
|
||||
void vc_derive_grain(const uint8_t km[32], uint32_t week_iso,
|
||||
uint32_t replay_counter, uint8_t out[32]);
|
||||
|
||||
/* --- [6] Cle Maitresse d'Usine ------------------------------------------
|
||||
* PLACEHOLDER DE TEST sur ce prototype. Sur STM32U5, K_M vivra dans
|
||||
* l'element securise STSAFE-A110 et cette API par pointeur disparaitra. */
|
||||
const uint8_t *vc_factory_key(void);
|
||||
|
||||
/* --- [3] Fenetre de Garde Dynamique -------------------------------------- */
|
||||
typedef struct {
|
||||
uint32_t last_valid_sec; /* +0 instant du dernier paquet valide */
|
||||
int32_t clock_offset; /* +4 recalage memorise, non applique */
|
||||
uint32_t width; /* +8 demi-largeur courante (cache) */
|
||||
uint32_t flags; /* +12 bit 0 = deja synchronise */
|
||||
} vc_guard_ctx; /* 16 */
|
||||
|
||||
#define VC_GUARD_MIN_SECONDS 2u /* demi-largeur au repos (hypothese) */
|
||||
#define VC_GUARD_MAX_SECONDS 30u /* plafond fourni, a valider */
|
||||
#define VC_GUARD_DRIFT_PPM 20u /* derive quartz supposee */
|
||||
#define VC_COLDSTART_TIMEOUT 604800u /* 7 jours, periode du grain */
|
||||
|
||||
void vc_guard_init (vc_guard_ctx *g);
|
||||
/* Paquet valide : resserrement + memorisation du recalage (ref - local). */
|
||||
void vc_guard_on_valid (vc_guard_ctx *g, uint32_t local_sec,
|
||||
uint32_t ref_sec);
|
||||
/* Demi-largeur pour un temps ecoule donne : lineaire, plafonnee. */
|
||||
uint32_t vc_guard_width (uint32_t elapsed_sec);
|
||||
/* Demi-largeur courante, et mise a jour du cache. */
|
||||
uint32_t vc_guard_width_now(vc_guard_ctx *g, uint32_t now_sec);
|
||||
|
||||
/* [4] + [3] : gating avec tolerance. Seul le bord inferieur est relache --
|
||||
* le protocole prevoit que le message utile arrive APRES l'Heure Cible. */
|
||||
uint64_t vc_gate_accept_window(uint32_t arrival_sec, uint32_t target_sec,
|
||||
uint32_t width_sec);
|
||||
|
||||
/* --- [6] Cold Start ------------------------------------------------------ */
|
||||
int vc_coldstart_needed(const vc_guard_ctx *g, uint32_t now_sec);
|
||||
|
||||
/* --- [5] Bascule de cle asynchrone ---------------------------------------
|
||||
* La cle du jour suivant est chiffree sous la cle du JOUR COURANT (chainage
|
||||
* cle N+1 protegee par cle N), puis logee dans la zone de bourrage. Le creneau
|
||||
* etant du chiffre AES, il est indistinguable du bruit qui l'entoure, et la
|
||||
* trame reste a 64 octets avec ou sans bascule. */
|
||||
typedef struct {
|
||||
vc_csprng_ctx *rng; /* +0 */
|
||||
const uint8_t *payload; /* +8 */
|
||||
size_t payload_len;/* +16 */
|
||||
const uint8_t *next_key; /* +24 NULL = pas de bascule */
|
||||
const vc_aes_rk *day_rk; /* +32 cle du jour courant */
|
||||
const uint8_t *key_ctr; /* +40 domaine KEYWRAP */
|
||||
} vc_frame_req; /* 48 */
|
||||
|
||||
typedef struct {
|
||||
uint8_t payload[64]; /* +0 */
|
||||
size_t payload_len; /* +64 */
|
||||
uint64_t has_next_key; /* +72 */
|
||||
uint8_t next_key[32]; /* +80 */
|
||||
} vc_frame_out; /* 112 */
|
||||
|
||||
/* Longueurs utiles maximales : la bascule consomme les octets 32..63. */
|
||||
#define VC_FRAME_KEYSW_MAXPAYLOAD 31u
|
||||
|
||||
int vc_frame_build(const vc_frame_req *req, uint8_t out[64]);
|
||||
int vc_frame_parse(const uint8_t in[64], const vc_aes_rk *day_rk,
|
||||
const uint8_t *key_ctr, vc_frame_out *out);
|
||||
|
||||
/* --------------------------------------------------------------------------
|
||||
* Verrous de coherence ASM / C
|
||||
* ------------------------------------------------------------------------*/
|
||||
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
|
||||
_Static_assert(sizeof(vc_sha256_ctx) == 112, "vc_sha256_ctx != CTX_SIZE asm");
|
||||
_Static_assert(sizeof(vc_csprng_ctx) == 48, "vc_csprng_ctx != PRNG_SIZE asm");
|
||||
_Static_assert(sizeof(vc_aes_rk) == 240, "vc_aes_rk != AES_RK_BYTES asm");
|
||||
_Static_assert(sizeof(vc_guard_ctx) == 16, "vc_guard_ctx != GUARD_* asm");
|
||||
_Static_assert(sizeof(vc_frame_req) == 48, "vc_frame_req != REQ_* asm");
|
||||
_Static_assert(sizeof(vc_frame_out) == 112, "vc_frame_out != OUT_* asm");
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* VERCORS_H */
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur