diff --git a/src/proto/handshake.c b/src/proto/handshake.c new file mode 100644 index 0000000..c2a0df5 --- /dev/null +++ b/src/proto/handshake.c @@ -0,0 +1,2 @@ +// handshake.c +// TODO \ No newline at end of file diff --git a/src/proto/handshake.h b/src/proto/handshake.h new file mode 100644 index 0000000..f1480db --- /dev/null +++ b/src/proto/handshake.h @@ -0,0 +1,92 @@ +// handshake.h +// The Bravais handshake - Noise IK extended with an ML-KEM-768 exchange. +// +// The *Handshake* (tm) +// <- s (responder's static key, known from config) +// -> random, e, ekem, es, s, ss, payload (message 1, "init") +// <- random, e, ee, se, kemct, payload (message 2, "response") +// +// e/ekem are fresh X25519 and ML-KEM-768 keys; kemct encapsulates a secret to ekem, which is mixed into the key +// schedule next to the X25519 results. Every field is hashed into the transcript, so altering anything makes the +// next AEAD fail. +// +// The transport mode is bound into the Noise prologue - a message 1 captured on one transport fails auth on the other. +// +// This module produces and consumes logical messages (basically transport-independent). Packet headers, Crypto framing, +// and the encoding I "borrowed" from TLS (the general shape at least) used in QUIC mode are the wire layer's job. + +#ifndef BRAVAIS_HANDSHAKE_H +#define BRAVAIS_HANDSHAKE_H + +#include +#include + +#include "bravais.h" +#include "bravais_internal.h" +#include "crypto/noise.h" + +#define BRAVAIS_HANDSHAKE_RANDOM_LENGTH 32 + +#define BRAVAIS_HANDSHAKE_INIT_LENGTH (BRAVAIS_HANDSHAKE_RANDOM_LENGTH \ + + BRAVAIS_X25519_KEY_LENGTH \ + + BRAVAIS_ML_KEM_768_PUBLIC_KEY_LENGTH \ + + BRAVAIS_X25519_KEY_LENGTH + BRAVAIS_AEAD_TAG_LENGTH \ + + BRAVAIS_AEAD_TAG_LENGTH) // 1312 + +#define BRAVAIS_HANDSHAKE_RESPONSE_LENGTH (BRAVAIS_HANDSHAKE_RANDOM_LENGTH \ + + BRAVAIS_X25519_KEY_LENGTH \ + + BRAVAIS_ML_KEM_768_CIPHERTEXT_LENGTH \ + + BRAVAIS_AEAD_TAG_LENGTH) // 1168 + +typedef enum { + BRAVAIS_HANDSHAKE_STATE_NONE = 0, + BRAVAIS_HANDSHAKE_STATE_INITIATOR_START, // Ready to write message 1 + BRAVAIS_HANDSHAKE_STATE_INITIATOR_SENT, // Waiting for message 2 + BRAVAIS_HANDSHAKE_STATE_RESPONDER_START, // Waiting for message 1 + BRAVAIS_HANDSHAKE_STATE_RESPONDER_READ, // Message 1 accepted; remote identity known, ready to respond + BRAVAIS_HANDSHAKE_STATE_DONE // Secrets wiped; only public keys remain +} bravais_handshake_state_t; + +// Output of a completed handshake, already oriented for this side. +typedef struct { + uint8_t sendKey[BRAVAIS_CHACHA20_POLY1305_KEY_LENGTH]; + uint8_t recvKey[BRAVAIS_CHACHA20_POLY1305_KEY_LENGTH]; + uint8_t handshakeHash[BRAVAIS_HASH_LENGTH]; // Final transcript hash, usable for channel binding + bool confirmed; // Initiator: true; Responder: false until the first packet under recvKey authenticates. +} bravais_session_keys_t; + +typedef struct { + bravais_handshake_state_t state; + bravais_noise_state_t noise; + uint8_t localStaticPrivate[BRAVAIS_X25519_KEY_LENGTH]; + uint8_t localStaticPublic[BRAVAIS_X25519_KEY_LENGTH]; + uint8_t remoteStaticPublic[BRAVAIS_X25519_KEY_LENGTH]; // Initiator: from config; Responder: from message 1 + uint8_t localEphemeralPrivate[BRAVAIS_X25519_KEY_LENGTH]; + uint8_t remoteEphemeralPublic[BRAVAIS_X25519_KEY_LENGTH]; + union { + uint8_t kemPrivateKey[BRAVAIS_ML_KEM_768_PRIVATE_KEY_LENGTH]; // Initiator + uint8_t remoteKemPublicKey[BRAVAIS_ML_KEM_768_PUBLIC_KEY_LENGTH]; // Responder + }; +} bravais_handshake_t; + +bravais_result_t bravais_handshake_init_initiator(bravais_handshake_t* handshake, bravais_transport_mode_t transportMode, const uint8_t* localStaticPrivate, const uint8_t* remoteStaticPublic); +bravais_result_t bravais_handshake_init_responder(bravais_handshake_t* handshake, bravais_transport_mode_t transportMode, const uint8_t* localStaticPrivate); + +// Initiator - write message 1 (BRAVAIS_HANDSHAKE_INIT_LENGTH bytes) +bravais_result_t bravais_handshake_write_init(bravais_handshake_t* handshake, uint8_t* out, size_t outCap, size_t* outLen); + +// Responder - read message 1. On success the initiator's auth'd static key is in handshake->remoteStaticPublic; check it +// against the whitelist before calling write_response, which is where the expensive ML-KEM crap happens. +// On any failure, the handshake is left as it was. +bravais_result_t bravais_handshake_read_init(bravais_handshake_t* handshake, const uint8_t* msg, size_t msgLen); + +// Responder - read message 2 (BRAVAIS_HANDSHAKE_RESPONSE_LENGTH bytes) and finish. +bravais_result_t bravais_handshake_write_response(bravais_handshake_t* handshake, uint8_t* out, size_t outCap, size_t* outLen, bravais_session_keys_t* outKeys); + +// Initiator - read message 2 and finish. Again, any failure and the handshake is left alone (forged response can't kill a real handshake) +bravais_result_t bravais_handshake_read_response(bravais_handshake_t* handshake, const uint8_t* msg, size_t msgLen, bravais_session_keys_t* outKeys); + +// Wipe everything +void bravais_handshake_wipe(bravais_handshake_t* handshake); + +#endif // BRAVAIS_HANDSHAKE_H \ No newline at end of file diff --git a/src/proto/session.c b/src/proto/session.c new file mode 100644 index 0000000..3eb1a02 --- /dev/null +++ b/src/proto/session.c @@ -0,0 +1,62 @@ +// session.c +// impl + +#include "proto/session.h" +#include "crypto/aead.h" +#include "util/hygiene.h" + +void bravais_session_init(bravais_session_t* session, const bravais_session_keys_t* keys) { + session->keys = *keys; + session->nextSendCounter = 0; +} + +bravais_result_t bravais_session_seal(bravais_session_t* session, const uint8_t* ad, size_t adLen, const uint8_t* pt, size_t ptLen, uint8_t* out, size_t outCap, size_t* outLen, uint64_t* outCounter) { + if (!session || !out || !outLen || !outCounter || (!pt && ptLen != 0) || (!ad && adLen != 0)) { return BRAVAIS_ERR_INVALID_ARG; } + if (ptLen > SIZE_MAX - BRAVAIS_AEAD_TAG_LENGTH) { return BRAVAIS_ERR_INVALID_ARG; } + + size_t needed = ptLen + BRAVAIS_AEAD_TAG_LENGTH; + if (outCap < needed) { + *outLen = needed; + return BRAVAIS_ERR_BUFFER_TOO_SMALL; + } + + if (session->nextSendCounter >= BRAVAIS_SESSION_COUNTER_LIMIT) { return BRAVAIS_ERR_LIMIT; } + + // Random rant, intellisense trolls so much, why the hell is nextSendCounter just not showing, like wtf, + // this stupid editor makes no sense sometimes + uint64_t counter = session->nextSendCounter++; + bravais_aead_seal(session->keys.sendKey, counter, ad, adLen, pt, ptLen, out); + + *outLen = needed; + *outCounter = counter; + + return BRAVAIS_OK; +} + +bravais_result_t bravais_session_open(bravais_session_t* session, uint64_t counter, const uint8_t* ad, size_t adLen, const uint8_t* ct, size_t ctLen, uint8_t* out, size_t outCap, size_t* outLen) { + if (!session || !ct || !out || !outLen || (!ad && adLen != 0)) { return BRAVAIS_ERR_INVALID_ARG; } + if (ctLen < BRAVAIS_AEAD_TAG_LENGTH) { return BRAVAIS_ERR_MALFORMED; } + + size_t ptLen = ctLen - BRAVAIS_AEAD_TAG_LENGTH; + if (outCap < ptLen) { + *outLen = ptLen; + return BRAVAIS_ERR_BUFFER_TOO_SMALL; + } + + bravais_result_t result = bravais_aead_open(session->keys.recvKey, counter, ad, adLen, ct, ctLen, out); + if (result != BRAVAIS_OK) { return result; } + + session->keys.confirmed = true; + *outLen = ptLen; + + return BRAVAIS_OK; +} + +bool bravais_session_is_confirmed(const bravais_session_t* session) { + return session->keys.confirmed; +} + +void bravais_session_wipe(bravais_session_t* session) { + if (!session) { return; } + bravais_hygiene_zero(session, sizeof(*session)); +} diff --git a/src/proto/session.h b/src/proto/session.h new file mode 100644 index 0000000..a4f6df6 --- /dev/null +++ b/src/proto/session.h @@ -0,0 +1,38 @@ +// session.h +// Transport encryption with handshake-produced keys +// +// The counter is just a packet number (goes into the header in the clear and forms the AEAD nonce on both sides). +// Each direction has its own key - two sides shouldn't have their counters collide. +// Replay protection isn't here, caller should check open() against a replay window (proto/replay) after auth succeeds. + +#ifndef BRAVAIS_SESSION_H +#define BRAVAIS_SESSION_H + +#include +#include + +#include "bravais.h" +#include "proto/handshake.h" + +// Stop sealing well before nonces could wrap - a session needs to rekey wayyyyyyyyy before this crap hits. +#define BRAVAIS_SESSION_COUNTER_LIMIT (UINT64_C(1) << 60) + +typedef struct { + bravais_session_keys_t keys; + uint64_t nextSendCounter; +} bravais_session_t; + +void bravais_session_init(bravais_session_t* session, const bravais_session_keys_t* keys); + +// Encrypt with the next counter - *out* receives *ptLen* + BRAVAIS_AEAD_TAG_LENGTH bytes; may equal *pt* +// BRAVAIS_ERR_LIMIT once the counter limit is reached +bravais_result_t bravais_session_seal(bravais_session_t* session, const uint8_t* ad, size_t adLen, const uint8_t* pt, size_t ptLen, uint8_t* out, size_t outCap, size_t* outLen, uint64_t* outCounter); + +// Decrypt a packet sent with the given counter - *out* may equal *ct* +// The first success confirms an unconfirmed (aka responder) session. +bravais_result_t bravais_session_open(bravais_session_t* session, uint64_t counter, const uint8_t* ad, size_t adLen, const uint8_t* ct, size_t ctLen, uint8_t* out, size_t outCap, size_t* outLen); + +bool bravais_session_is_confirmed(const bravais_session_t* session); +void bravais_session_wipe(bravais_session_t* session); + +#endif // BRAVAIS_SESSION_H