diff --git a/src/proto/handshake.c b/src/proto/handshake.c index c2a0df5..9806215 100644 --- a/src/proto/handshake.c +++ b/src/proto/handshake.c @@ -1,2 +1,322 @@ // handshake.c -// TODO \ No newline at end of file + +#include "proto/handshake.h" +#include "crypto/kem.h" +#include "crypto/rng.h" +#include "crypto/x25519.h" +#include "util/hygiene.h" + +#include + +// This is hashed into the inital state; bump with any change to the pattern. +static const char protocolName[] = "Bravais_IK_25519+MLKEM768_ChaChaPoly_SHA256"; + +// "bravais/1" followed by one byte of transport mode +static const uint8_t prologueTag[] = { 'b', 'r', 'a', 'v', 'a', 'i', 's', '/', '1' }; + +/* HELPERS */ +static bool transport_mode_is_valid(bravais_transport_mode_t transportMode) { + return transportMode == BRAVAIS_TRANSPORT_MODE_RAW || transportMode == BRAVAIS_TRANSPORT_MODE_QUIC; +} + +static void init_common(bravais_handshake_t* handshake, bravais_transport_mode_t transportMode, const uint8_t* localStaticPrivate) { + *handshake = (bravais_handshake_t){}; + + uint8_t prologue[sizeof(prologueTag) + 1]; + memcpy(prologue, prologueTag, sizeof(prologueTag)); + prologue[sizeof(prologueTag)] = (uint8_t)transportMode; + + bravais_noise_init(&handshake->noise, protocolName, prologue, sizeof(prologue)); + + memcpy(handshake->localStaticPrivate, localStaticPrivate, BRAVAIS_X25519_KEY_LENGTH); + bravais_x25519_public_key(localStaticPrivate, handshake->localStaticPublic); +} + +// DH then MixKey so the shared secret doesn't outlive the call +static bravais_result_t mix_dh(bravais_handshake_t* handshake, const uint8_t* privateKey, const uint8_t* publicKey) { + uint8_t shared[BRAVAIS_X25519_KEY_LENGTH]; + + bravais_result_t result = bravais_x25519(privateKey, publicKey, shared); + if (result == BRAVAIS_OK) { + bravais_noise_mix_key(&handshake->noise, shared, sizeof(shared)); + } + + bravais_hygiene_zero(shared, sizeof(shared)); + + return result; +} + +// Split, oriented for this side, then wipe every secret (keep public keys) +static void finish(bravais_handshake_t* handshake, bool isInitiator, bravais_session_keys_t* outKeys) { + uint8_t initToResp[BRAVAIS_HASH_LENGTH]; + uint8_t respToInit[BRAVAIS_HASH_LENGTH]; + + bravais_noise_split(&handshake->noise, initToResp, respToInit); + + if (isInitiator) { + memcpy(outKeys->sendKey, initToResp, sizeof(outKeys->sendKey)); + memcpy(outKeys->recvKey, respToInit, sizeof(outKeys->recvKey)); + } else { + memcpy(outKeys->sendKey, respToInit, sizeof(outKeys->sendKey)); + memcpy(outKeys->recvKey, initToResp, sizeof(outKeys->recvKey)); + } + memcpy(outKeys->handshakeHash, handshake->noise.hash, sizeof(outKeys->handshakeHash)); + + // The responder has no proof (yet) that the initiator derived the same keys, so message 1 may be a replay. + // Message 2's tag gives the initiator proof about the responder. + outKeys->confirmed = isInitiator; + + bravais_hygiene_zero(initToResp, sizeof(initToResp)); + bravais_hygiene_zero(respToInit, sizeof(respToInit)); + + bravais_noise_wipe(&handshake->noise); + bravais_hygiene_zero(handshake->localStaticPrivate, sizeof(handshake->localStaticPrivate)); + bravais_hygiene_zero(handshake->localEphemeralPrivate, sizeof(handshake->localEphemeralPrivate)); + bravais_hygiene_zero(handshake->kemPrivateKey, sizeof(handshake->kemPrivateKey)); // Larger union member + handshake->state = BRAVAIS_HANDSHAKE_STATE_DONE; +} + +/* SETUP */ + +bravais_result_t bravais_handshake_init_initiator(bravais_handshake_t* handshake, bravais_transport_mode_t transportMode, const uint8_t* localStaticPrivate, const uint8_t* remoteStaticPublic) { + if (!handshake || !localStaticPrivate || !remoteStaticPublic || !transport_mode_is_valid(transportMode)) { return BRAVAIS_ERR_INVALID_ARG; } + + init_common(handshake, transportMode, localStaticPrivate); + + // IK pre-message "<- s" - initiator knows the responder's key in advance + memcpy(handshake->remoteStaticPublic, remoteStaticPublic, BRAVAIS_X25519_KEY_LENGTH); + bravais_noise_mix_hash(&handshake->noise, remoteStaticPublic, BRAVAIS_X25519_KEY_LENGTH); + + handshake->state = BRAVAIS_HANDSHAKE_STATE_INITIATOR_START; + + return BRAVAIS_OK; +} + +bravais_result_t bravais_handshake_init_responder(bravais_handshake_t* handshake, bravais_transport_mode_t transportMode, const uint8_t* localStaticPrivate) { + if (!handshake || !localStaticPrivate || !transport_mode_is_valid(transportMode)) { return BRAVAIS_ERR_INVALID_ARG; } + + init_common(handshake, transportMode, localStaticPrivate); + + // Same thing from the other side + bravais_noise_mix_hash(&handshake->noise, handshake->localStaticPublic, BRAVAIS_X25519_KEY_LENGTH); + + handshake->state = BRAVAIS_HANDSHAKE_STATE_RESPONDER_START; + + return BRAVAIS_OK; +} + +/* MESSAGE 1 */ +bravais_result_t bravais_handshake_write_init(bravais_handshake_t* handshake, uint8_t* out, size_t outCap, size_t* outLen) { + if (!handshake || !out || !outLen) { return BRAVAIS_ERR_INVALID_ARG; } + if (handshake->state != BRAVAIS_HANDSHAKE_STATE_INITIATOR_START) { return BRAVAIS_ERR_STATE; } + if (outCap < BRAVAIS_HANDSHAKE_INIT_LENGTH) { + *outLen = BRAVAIS_HANDSHAKE_INIT_LENGTH; + return BRAVAIS_ERR_BUFFER_TOO_SMALL; + } + + bravais_result_t result = BRAVAIS_OK; + size_t pos = 0; + + // random + bravais_random_bytes(out + pos, BRAVAIS_HANDSHAKE_RANDOM_LENGTH); + bravais_noise_mix_hash(&handshake->noise, out + pos, BRAVAIS_HANDSHAKE_RANDOM_LENGTH); + pos += BRAVAIS_HANDSHAKE_RANDOM_LENGTH; + + // e + bravais_x25519_keypair(handshake->localEphemeralPrivate, out + pos); + bravais_noise_mix_hash(&handshake->noise, out + pos, BRAVAIS_X25519_KEY_LENGTH); + pos += BRAVAIS_X25519_KEY_LENGTH; + + // ekem + result = bravais_kem_keypair(out + pos, handshake->kemPrivateKey); + if (result != BRAVAIS_OK) { goto fail; } + bravais_noise_mix_hash(&handshake->noise, out + pos, BRAVAIS_ML_KEM_768_PUBLIC_KEY_LENGTH); + pos += BRAVAIS_ML_KEM_768_PUBLIC_KEY_LENGTH; + + // es + result = mix_dh(handshake, handshake->localEphemeralPrivate, handshake->remoteStaticPublic); + if (result != BRAVAIS_OK) { goto fail; } + + // s (our identity, encrypted) + result = bravais_noise_encrypt_and_hash(&handshake->noise, handshake->localStaticPublic, BRAVAIS_X25519_KEY_LENGTH, out + pos); + if (result != BRAVAIS_OK) { goto fail; } + pos += BRAVAIS_X25519_KEY_LENGTH + BRAVAIS_AEAD_TAG_LENGTH; + + // ss + result = mix_dh(handshake, handshake->localStaticPrivate, handshake->remoteStaticPublic); + if (result != BRAVAIS_OK) { goto fail; } + + // payload (empty, prives we hold the private half of s) + result = bravais_noise_encrypt_and_hash(&handshake->noise, nullptr, 0, out + pos); + if (result != BRAVAIS_OK) { goto fail; } + pos += BRAVAIS_AEAD_TAG_LENGTH; + + *outLen = pos; + handshake->state = BRAVAIS_HANDSHAKE_STATE_INITIATOR_SENT; + + return BRAVAIS_OK; + +fail: + bravais_hygiene_zero(out, outCap); + bravais_handshake_wipe(handshake); + return result; +} + +bravais_result_t bravais_handshake_read_init(bravais_handshake_t* handshake, const uint8_t* msg, size_t msgLen) { + // Work on a copy, basically a copy of the above thing but *reading* (hype) + if (!handshake || !msg) { return BRAVAIS_ERR_INVALID_ARG; } + if (handshake->state != BRAVAIS_HANDSHAKE_STATE_RESPONDER_START) { return BRAVAIS_ERR_STATE; } + if (msgLen != BRAVAIS_HANDSHAKE_INIT_LENGTH) { return BRAVAIS_ERR_MALFORMED; } + + // Make the copy (commit on success) + bravais_handshake_t work = *handshake; + bravais_result_t result = BRAVAIS_OK; + size_t pos = 0; + + // random + bravais_noise_mix_hash(&work.noise, msg + pos, BRAVAIS_HANDSHAKE_RANDOM_LENGTH); + pos += BRAVAIS_HANDSHAKE_RANDOM_LENGTH; + + // e + memcpy(work.remoteKemPublicKey, msg + pos, BRAVAIS_X25519_KEY_LENGTH); + bravais_noise_mix_hash(&work.noise, work.remoteEphemeralPublic, BRAVAIS_X25519_KEY_LENGTH); + pos += BRAVAIS_X25519_KEY_LENGTH; + + // ekem + memcpy(work.remoteEphemeralPublic, msg + pos, BRAVAIS_ML_KEM_768_PUBLIC_KEY_LENGTH); + bravais_noise_mix_hash(&work.noise, work.remoteKemPublicKey, BRAVAIS_ML_KEM_768_PUBLIC_KEY_LENGTH); + pos += BRAVAIS_ML_KEM_768_PUBLIC_KEY_LENGTH; + + // es + result = mix_dh(&work, work.localStaticPrivate, work.remoteEphemeralPublic); + if (result != BRAVAIS_OK) { goto done; } + + // s + result = bravais_noise_decrypt_and_hash(&work.noise, msg + pos, BRAVAIS_X25519_KEY_LENGTH + BRAVAIS_AEAD_TAG_LENGTH, work.remoteStaticPublic); + if (result != BRAVAIS_OK) { goto done; } + pos += BRAVAIS_X25519_KEY_LENGTH + BRAVAIS_AEAD_TAG_LENGTH; + + // ss + result = mix_dh(&work, work.localStaticPrivate, work.remoteStaticPublic); + if (result != BRAVAIS_OK) { goto done; } + + // payload + result = bravais_noise_decrypt_and_hash(&work.noise, msg + pos, BRAVAIS_AEAD_TAG_LENGTH, nullptr); + if (result != BRAVAIS_OK) { goto done; } + + work.state = BRAVAIS_HANDSHAKE_STATE_RESPONDER_READ; + *handshake = work; // commit + +done: + bravais_handshake_wipe(&work); + return result; +} + +/* MESSAGE 2 */ + +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) { + if (!handshake || !out || !outLen || !outKeys) { return BRAVAIS_ERR_INVALID_ARG; } + if (handshake->state != BRAVAIS_HANDSHAKE_STATE_RESPONDER_READ) { return BRAVAIS_ERR_STATE; } + if (outCap < BRAVAIS_HANDSHAKE_RESPONSE_LENGTH) { + *outLen = BRAVAIS_HANDSHAKE_RESPONSE_LENGTH; + return BRAVAIS_ERR_BUFFER_TOO_SMALL; + } + + bravais_result_t result = BRAVAIS_OK; + uint8_t kemShared[BRAVAIS_ML_KEM_768_SHARED_SECRET_LENGTH]; + size_t pos = 0; + + // random + bravais_random_bytes(out + pos, BRAVAIS_HANDSHAKE_RANDOM_LENGTH); + bravais_noise_mix_hash(&handshake->noise, out + pos, BRAVAIS_HANDSHAKE_RANDOM_LENGTH); + pos += BRAVAIS_HANDSHAKE_RANDOM_LENGTH; + + // e + bravais_x25519_keypair(handshake->localEphemeralPrivate, out + pos); + bravais_noise_mix_hash(&handshake->noise, out + pos, BRAVAIS_X25519_KEY_LENGTH); + pos += BRAVAIS_X25519_KEY_LENGTH; + + // ee + result = mix_dh(handshake, handshake->localEphemeralPrivate, handshake->remoteEphemeralPublic); + if (result != BRAVAIS_OK) { goto fail; } + + // se: initiator's static with our ephemeral + result = mix_dh(handshake, handshake->localEphemeralPrivate, handshake->remoteStaticPublic); + if (result != BRAVAIS_OK) { goto fail; } + + // kemct - sent in the clear, like a real ServerHello key_share, its secret goes into the key + result = bravais_kem_encaps(handshake->remoteKemPublicKey, out + pos, kemShared); + if (result != BRAVAIS_OK) { goto fail; } + bravais_noise_mix_hash(&handshake->noise, out + pos, BRAVAIS_ML_KEM_768_CIPHERTEXT_LENGTH); + bravais_noise_mix_key(&handshake->noise, kemShared, sizeof(kemShared)); + pos += BRAVAIS_ML_KEM_768_CIPHERTEXT_LENGTH; + + // payload: its tag proves we derived the same keys + result = bravais_noise_encrypt_and_hash(&handshake->noise, nullptr, 0, out + pos); + if (result != BRAVAIS_OK) { goto fail; } + pos += BRAVAIS_AEAD_TAG_LENGTH; + + bravais_hygiene_zero(kemShared, sizeof(kemShared)); + finish(handshake, false, outKeys); + *outLen = pos; + + return BRAVAIS_OK; + +fail: + bravais_hygiene_zero(kemShared, sizeof(kemShared)); + bravais_hygiene_zero(out, outCap); + bravais_handshake_wipe(handshake); + return result; +} + +bravais_result_t bravais_handshake_read_response(bravais_handshake_t* handshake, const uint8_t* msg, size_t msgLen, bravais_session_keys_t* outKeys) { + if (!handshake || !msg || !outKeys) { return BRAVAIS_ERR_INVALID_ARG; } + if (handshake->state != BRAVAIS_HANDSHAKE_STATE_INITIATOR_SENT) { return BRAVAIS_ERR_STATE; } + if (msgLen != BRAVAIS_HANDSHAKE_RESPONSE_LENGTH) { return BRAVAIS_ERR_MALFORMED; } + + bravais_handshake_t work = *handshake; + bravais_result_t result = BRAVAIS_OK; + uint8_t kemShared[BRAVAIS_ML_KEM_768_SHARED_SECRET_LENGTH]; + size_t pos = 0; + + // random + bravais_noise_mix_hash(&work.noise, msg + pos, BRAVAIS_HANDSHAKE_RANDOM_LENGTH); + pos += BRAVAIS_HANDSHAKE_RANDOM_LENGTH; + + // e + memcpy(work.remoteEphemeralPublic, msg + pos, BRAVAIS_X25519_KEY_LENGTH); + bravais_noise_mix_hash(&work.noise, work.remoteEphemeralPublic, BRAVAIS_X25519_KEY_LENGTH); + pos += BRAVAIS_X25519_KEY_LENGTH; + + // ee + result = mix_dh(&work, work.localEphemeralPrivate, work.remoteEphemeralPublic); + if (result != BRAVAIS_OK) { goto done; } + + // se: our static with their ephemeral + result = mix_dh(&work, work.localStaticPrivate, work.remoteEphemeralPublic); + if (result != BRAVAIS_OK) { goto done; } + + // kemct + bravais_noise_mix_hash(&work.noise, msg + pos, BRAVAIS_ML_KEM_768_CIPHERTEXT_LENGTH); + result = bravais_kem_decaps(work.kemPrivateKey, msg + pos, kemShared); + if (result != BRAVAIS_OK) { goto done; } + bravais_noise_mix_key(&work.noise, kemShared, sizeof(kemShared)); + pos += BRAVAIS_ML_KEM_768_CIPHERTEXT_LENGTH; + + // payload - the first real authentication of everything above, including the KEM secret + result = bravais_noise_decrypt_and_hash(&work.noise, msg + pos, BRAVAIS_AEAD_TAG_LENGTH, nullptr); + if (result != BRAVAIS_OK) { goto done; } + + finish(&work, true, outKeys); + *handshake = work; + +done: + bravais_hygiene_zero(kemShared, sizeof(kemShared)); + bravais_handshake_wipe(&work); + return result; +} + +void bravais_handshake_wipe(bravais_handshake_t* handshake) { + if (!handshake) { return; } + bravais_hygiene_zero(handshake, sizeof(*handshake)); +}