diff --git a/src/crypto/aead.c b/src/crypto/aead.c new file mode 100644 index 0000000..a752336 --- /dev/null +++ b/src/crypto/aead.c @@ -0,0 +1,42 @@ +// aead.c + +#include "crypto/aead.h" +#include "util/hygiene.h" + +#include +#include + +static_assert(crypto_aead_chacha20poly1305_ietf_KEYBYTES == BRAVAIS_CHACHA20_POLY1305_KEY_LENGTH, "ChaCha20-Poly1305 key size"); +static_assert(crypto_aead_chacha20poly1305_ietf_NPUBBYTES == BRAVAIS_NONCE_LENGTH, "ChaCha20-Poly1305 nonce size"); +static_assert(crypto_aead_chacha20poly1305_ietf_ABYTES == BRAVAIS_AEAD_TAG_LENGTH, "Poly1305 tag size"); + +static void make_nonce(uint64_t counter, uint8_t* nonce) { + for (size_t i = 0; i < 4; i++) { + nonce[i] = 0; + } + + for (size_t i = 0; i < 8; i++) { + nonce[4 + i] = (uint8_t)(counter >> (8 * i)); + } +} + +void bravais_aead_seal(const uint8_t* key, uint64_t counter, const uint8_t* ad, size_t adLen, const uint8_t* pt, size_t ptLen, uint8_t* out) { + uint8_t nonce[BRAVAIS_NONCE_LENGTH]; + make_nonce(counter, nonce); + + crypto_aead_chacha20poly1305_ietf_encrypt(out, nullptr, pt, ptLen, ad, adLen, nullptr, nonce, key); +} + +bravais_result_t bravais_aead_open(const uint8_t* key, uint64_t counter, const uint8_t* ad, size_t adLen, const uint8_t* ct, size_t ctLen, uint8_t* out) { + if (ctLen < BRAVAIS_AEAD_TAG_LENGTH) { return BRAVAIS_ERR_MALFORMED; } + + uint8_t nonce[BRAVAIS_NONCE_LENGTH]; + make_nonce(counter, nonce); + + if (crypto_aead_chacha20poly1305_ietf_decrypt(out, nullptr, nullptr, ct, ctLen, ad, adLen, nonce, key) != 0) { + bravais_hygiene_zero(out, ctLen - BRAVAIS_AEAD_TAG_LENGTH); + return BRAVAIS_ERR_AUTH; + } + + return BRAVAIS_OK; +} diff --git a/src/crypto/aead.h b/src/crypto/aead.h new file mode 100644 index 0000000..ae83bae --- /dev/null +++ b/src/crypto/aead.h @@ -0,0 +1,23 @@ +// aead.h +// ChaCha20-Poly1305 (RFC 8439) with Noise-style nonces. +// +// The 96-bit nonce is 32 zero bits followed by a 64-bit little-endian counter (Noise spec, section 12.3). +// Every key in this library is used in one direction only, so a counter never repeats under a key. + +#ifndef BRAVAIS_AEAD_H +#define BRAVAIS_AEAD_H + +#include +#include + +#include "bravais.h" +#include "bravais_internal.h" + +// Encrypt. out receives ptLen + BRAVAIS_AEAD_TAG_LENGTH bytes and may equal pt (in place). +void bravais_aead_seal(const uint8_t* key, uint64_t counter, const uint8_t* ad, size_t adLen, const uint8_t* pt, size_t ptLen, uint8_t* out); + +// Decrypt and verify. out receives ctLen - BRAVAIS_AEAD_TAG_LENGTH bytes and may equal ct (in place). +// BRAVAIS_ERR_AUTH on a bad tag (out is zeroed), BRAVAIS_ERR_MALFORMED if ctLen is shorter than a tag. +bravais_result_t bravais_aead_open(const uint8_t* key, uint64_t counter, const uint8_t* ad, size_t adLen, const uint8_t* ct, size_t ctLen, uint8_t* out); + +#endif // BRAVAIS_AEAD_H diff --git a/src/crypto/hash.c b/src/crypto/hash.c new file mode 100644 index 0000000..b0cfb25 --- /dev/null +++ b/src/crypto/hash.c @@ -0,0 +1,55 @@ +// hash.c + +#include "crypto/hash.h" +#include "util/hygiene.h" + +#include +#include + +static_assert(crypto_hash_sha256_BYTES == BRAVAIS_HASH_LENGTH, "SHA-256 output size"); +static_assert(crypto_auth_hmacsha256_BYTES == BRAVAIS_HASH_LENGTH, "HMAC-SHA-256 output size"); + +void bravais_sha256(const uint8_t* data, size_t dataLen, uint8_t* out) { + crypto_hash_sha256(out, data, dataLen); +} + +void bravais_sha256_two(const uint8_t* data1, size_t data1Len, const uint8_t* data2, size_t data2Len, uint8_t* out) { + crypto_hash_sha256_state state; + + crypto_hash_sha256_init(&state); + crypto_hash_sha256_update(&state, data1, data1Len); + if (data2Len != 0) { + crypto_hash_sha256_update(&state, data2, data2Len); + } + crypto_hash_sha256_final(&state, out); +} + +void bravais_hmac_sha256(const uint8_t* key, size_t keyLen, const uint8_t* data1, size_t data1Len, const uint8_t* data2, size_t data2Len, uint8_t* out) { + crypto_auth_hmacsha256_state state; + + crypto_auth_hmacsha256_init(&state, key, keyLen); + crypto_auth_hmacsha256_update(&state, data1, data1Len); + if (data2Len != 0) { + crypto_auth_hmacsha256_update(&state, data2, data2Len); + } + crypto_auth_hmacsha256_final(&state, out); + + bravais_hygiene_zero(&state, sizeof(state)); +} + +void bravais_noise_hkdf(const uint8_t* chainingKey, const uint8_t* ikm, size_t ikmLen, uint8_t* out1, uint8_t* out2, uint8_t* out3) { + static const uint8_t one = 0x01; + static const uint8_t two = 0x02; + static const uint8_t three = 0x03; + + uint8_t tempKey[BRAVAIS_HASH_LENGTH]; + bravais_hmac_sha256(chainingKey, BRAVAIS_HASH_LENGTH, ikm, ikmLen, nullptr, 0, tempKey); + + bravais_hmac_sha256(tempKey, sizeof(tempKey), &one, 1, nullptr, 0, out1); + bravais_hmac_sha256(tempKey, sizeof(tempKey), out1, BRAVAIS_HASH_LENGTH, &two, 1, out2); + if (out3) { + bravais_hmac_sha256(tempKey, sizeof(tempKey), out2, BRAVAIS_HASH_LENGTH, &three, 1, out3); + } + + bravais_hygiene_zero(tempKey, sizeof(tempKey)); +} diff --git a/src/crypto/hash.h b/src/crypto/hash.h new file mode 100644 index 0000000..5745f75 --- /dev/null +++ b/src/crypto/hash.h @@ -0,0 +1,26 @@ +// hash.h +// SHA-256, HMAC-SHA-256 and the Noise HKDF. +// +// SHA-256 is the only hash in the library: Noise uses it here, and QUIC's Initial key schedule needs it anyway. + +#ifndef BRAVAIS_HASH_H +#define BRAVAIS_HASH_H + +#include +#include + +#define BRAVAIS_HASH_LENGTH 32 + +void bravais_sha256(const uint8_t* data, size_t dataLen, uint8_t* out); + +// SHA-256 over the concatenation data1 || data2, without building it. +void bravais_sha256_two(const uint8_t* data1, size_t data1Len, const uint8_t* data2, size_t data2Len, uint8_t* out); + +// HMAC-SHA-256 over the concatenation data1 || data2. data2 may be nullptr with data2Len 0. +void bravais_hmac_sha256(const uint8_t* key, size_t keyLen, const uint8_t* data1, size_t data1Len, const uint8_t* data2, size_t data2Len, uint8_t* out); + +// Noise HKDF (spec section 4.3): two or three BRAVAIS_HASH_LENGTH outputs from a chaining key and input key material. +// out3 may be nullptr for the two-output form. +void bravais_noise_hkdf(const uint8_t* chainingKey, const uint8_t* ikm, size_t ikmLen, uint8_t* out1, uint8_t* out2, uint8_t* out3); + +#endif // BRAVAIS_HASH_H diff --git a/src/crypto/kem.c b/src/crypto/kem.c new file mode 100644 index 0000000..4bdd3e9 --- /dev/null +++ b/src/crypto/kem.c @@ -0,0 +1,37 @@ +// kem.c + +#include "crypto/kem.h" + +#include +#include + +static_assert(OQS_KEM_ml_kem_768_length_public_key == BRAVAIS_ML_KEM_768_PUBLIC_KEY_LENGTH, "ML-KEM-768 public key size"); +static_assert(OQS_KEM_ml_kem_768_length_secret_key == BRAVAIS_ML_KEM_768_PRIVATE_KEY_LENGTH, "ML-KEM-768 private key size"); +static_assert(OQS_KEM_ml_kem_768_length_ciphertext == BRAVAIS_ML_KEM_768_CIPHERTEXT_LENGTH, "ML-KEM-768 ciphertext size"); +static_assert(OQS_KEM_ml_kem_768_length_shared_secret == BRAVAIS_ML_KEM_768_SHARED_SECRET_LENGTH, "ML-KEM-768 shared secret size"); +static_assert(OQS_KEM_ml_kem_768_length_keypair_seed == BRAVAIS_ML_KEM_768_SEED_LENGTH, "ML-KEM-768 seed size"); + +bravais_result_t bravais_kem_keypair(uint8_t* outPublicKey, uint8_t* outPrivateKey) { + if (OQS_KEM_ml_kem_768_keypair(outPublicKey, outPrivateKey) != OQS_SUCCESS) { + return BRAVAIS_ERR_UNKNOWN; + } + + return BRAVAIS_OK; +} + +bravais_result_t bravais_kem_encaps(const uint8_t* publicKey, uint8_t* outCiphertext, uint8_t* outShared) { + // Fails on a public key that doesn't pass the FIPS 203 modulus check, i.e. a malformed one + if (OQS_KEM_ml_kem_768_encaps(outCiphertext, outShared, publicKey) != OQS_SUCCESS) { + return BRAVAIS_ERR_MALFORMED; + } + + return BRAVAIS_OK; +} + +bravais_result_t bravais_kem_decaps(const uint8_t* privateKey, const uint8_t* ciphertext, uint8_t* outShared) { + if (OQS_KEM_ml_kem_768_decaps(outShared, ciphertext, privateKey) != OQS_SUCCESS) { + return BRAVAIS_ERR_UNKNOWN; + } + + return BRAVAIS_OK; +} diff --git a/src/crypto/kem.h b/src/crypto/kem.h new file mode 100644 index 0000000..f17b086 --- /dev/null +++ b/src/crypto/kem.h @@ -0,0 +1,23 @@ +// kem.h +// ML-KEM-768 (FIPS 203) + +#ifndef BRAVAIS_KEM_H +#define BRAVAIS_KEM_H + +#include + +#include "bravais.h" +#include "bravais_internal.h" + +// Fresh keypair: encapsulation (public) key and expanded decapsulation (private) key. +bravais_result_t bravais_kem_keypair(uint8_t* outPublicKey, uint8_t* outPrivateKey); + +// Encapsulate to a public key: a ciphertext to send, and the shared secret it carries. +bravais_result_t bravais_kem_encaps(const uint8_t* publicKey, uint8_t* outCiphertext, uint8_t* outShared); + +// Recover the shared secret from a ciphertext. +// A tampered ciphertext does not fail here: FIPS 203 implicit rejection yields an unrelated secret, +// which then fails the next AEAD check. Callers must not treat BRAVAIS_OK as authentication. +bravais_result_t bravais_kem_decaps(const uint8_t* privateKey, const uint8_t* ciphertext, uint8_t* outShared); + +#endif // BRAVAIS_KEM_H diff --git a/src/crypto/noise.c b/src/crypto/noise.c new file mode 100644 index 0000000..80fad3c --- /dev/null +++ b/src/crypto/noise.c @@ -0,0 +1,89 @@ +// noise.c + +#include "crypto/noise.h" +#include "crypto/aead.h" +#include "util/hygiene.h" + +#include + +// The spec reserves 2^64 - 1; a CipherState that reaches it must not encrypt again +#define BRAVAIS_NOISE_NONCE_LIMIT UINT64_MAX + +void bravais_noise_init(bravais_noise_state_t* state, const char* protocolName, const uint8_t* prologue, size_t prologueLen) { + *state = (bravais_noise_state_t){}; + + size_t nameLen = strlen(protocolName); + if (nameLen <= BRAVAIS_HASH_LENGTH) { + memcpy(state->hash, protocolName, nameLen); // Zero-padded to HASHLEN + } else { + bravais_sha256((const uint8_t*)protocolName, nameLen, state->hash); + } + + memcpy(state->chainingKey, state->hash, BRAVAIS_HASH_LENGTH); + bravais_noise_mix_hash(state, prologue, prologueLen); +} + +void bravais_noise_mix_hash(bravais_noise_state_t* state, const uint8_t* data, size_t dataLen) { + bravais_sha256_two(state->hash, BRAVAIS_HASH_LENGTH, data, dataLen, state->hash); +} + +void bravais_noise_mix_key(bravais_noise_state_t* state, const uint8_t* ikm, size_t ikmLen) { + uint8_t newChainingKey[BRAVAIS_HASH_LENGTH]; + uint8_t tempKey[BRAVAIS_HASH_LENGTH]; + + bravais_noise_hkdf(state->chainingKey, ikm, ikmLen, newChainingKey, tempKey, nullptr); + + memcpy(state->chainingKey, newChainingKey, BRAVAIS_HASH_LENGTH); + memcpy(state->key, tempKey, sizeof(state->key)); // HASHLEN is already 32, so no truncation + state->hasKey = true; + state->nonce = 0; + + bravais_hygiene_zero(newChainingKey, sizeof(newChainingKey)); + bravais_hygiene_zero(tempKey, sizeof(tempKey)); +} + +bravais_result_t bravais_noise_encrypt_and_hash(bravais_noise_state_t* state, const uint8_t* pt, size_t ptLen, uint8_t* out) { + size_t outLen = ptLen; + + if (state->hasKey) { + if (state->nonce == BRAVAIS_NOISE_NONCE_LIMIT) { return BRAVAIS_ERR_LIMIT; } + + bravais_aead_seal(state->key, state->nonce, state->hash, BRAVAIS_HASH_LENGTH, pt, ptLen, out); + state->nonce++; + outLen += BRAVAIS_AEAD_TAG_LENGTH; + } else if (out != pt && ptLen != 0) { + memmove(out, pt, ptLen); + } + + bravais_noise_mix_hash(state, out, outLen); + return BRAVAIS_OK; +} + +bravais_result_t bravais_noise_decrypt_and_hash(bravais_noise_state_t* state, const uint8_t* ct, size_t ctLen, uint8_t* out) { + // The new hash covers the ciphertext, so compute it before an in-place decrypt destroys it + uint8_t newHash[BRAVAIS_HASH_LENGTH]; + bravais_sha256_two(state->hash, BRAVAIS_HASH_LENGTH, ct, ctLen, newHash); + + if (state->hasKey) { + if (state->nonce == BRAVAIS_NOISE_NONCE_LIMIT) { return BRAVAIS_ERR_LIMIT; } + + // The old hash is the associated data + bravais_result_t result = bravais_aead_open(state->key, state->nonce, state->hash, BRAVAIS_HASH_LENGTH, ct, ctLen, out); + if (result != BRAVAIS_OK) { return result; } + + state->nonce++; + } else if (out != ct && ctLen != 0) { + memmove(out, ct, ctLen); + } + + memcpy(state->hash, newHash, BRAVAIS_HASH_LENGTH); + return BRAVAIS_OK; +} + +void bravais_noise_split(const bravais_noise_state_t* state, uint8_t* out1, uint8_t* out2) { + bravais_noise_hkdf(state->chainingKey, nullptr, 0, out1, out2, nullptr); +} + +void bravais_noise_wipe(bravais_noise_state_t* state) { + bravais_hygiene_zero(state, sizeof(*state)); +} diff --git a/src/crypto/noise.h b/src/crypto/noise.h new file mode 100644 index 0000000..6336e8b --- /dev/null +++ b/src/crypto/noise.h @@ -0,0 +1,40 @@ +// noise.h +// The Noise Protocol Framework's SymmetricState and CipherState (spec sections 5.1 and 5.2), +// fixed to ChaChaPoly and SHA-256. Handshake patterns live in proto/handshake. + +#ifndef BRAVAIS_NOISE_H +#define BRAVAIS_NOISE_H + +#include +#include + +#include "bravais.h" +#include "bravais_internal.h" +#include "crypto/hash.h" + +typedef struct { + uint8_t chainingKey[BRAVAIS_HASH_LENGTH]; // ck + uint8_t hash[BRAVAIS_HASH_LENGTH]; // h, the running transcript hash + uint8_t key[BRAVAIS_CHACHA20_POLY1305_KEY_LENGTH]; // k + bool hasKey; + uint64_t nonce; // n +} bravais_noise_state_t; + +// InitializeSymmetric(protocolName), then MixHash(prologue). +void bravais_noise_init(bravais_noise_state_t* state, const char* protocolName, const uint8_t* prologue, size_t prologueLen); + +void bravais_noise_mix_hash(bravais_noise_state_t* state, const uint8_t* data, size_t dataLen); +void bravais_noise_mix_key(bravais_noise_state_t* state, const uint8_t* ikm, size_t ikmLen); + +// Output length is ptLen, plus BRAVAIS_AEAD_TAG_LENGTH once a key exists. out may equal pt. +bravais_result_t bravais_noise_encrypt_and_hash(bravais_noise_state_t* state, const uint8_t* pt, size_t ptLen, uint8_t* out); + +// BRAVAIS_ERR_AUTH on a bad tag; the state is then left unchanged. out may equal ct. +bravais_result_t bravais_noise_decrypt_and_hash(bravais_noise_state_t* state, const uint8_t* ct, size_t ctLen, uint8_t* out); + +// Split(): the two transport keys. out1 is initiator to responder, out2 the reverse. +void bravais_noise_split(const bravais_noise_state_t* state, uint8_t* out1, uint8_t* out2); + +void bravais_noise_wipe(bravais_noise_state_t* state); + +#endif // BRAVAIS_NOISE_H diff --git a/src/crypto/rng.c b/src/crypto/rng.c new file mode 100644 index 0000000..31bb69f --- /dev/null +++ b/src/crypto/rng.c @@ -0,0 +1,20 @@ +// rng.c + +#include "crypto/rng.h" + +#include + +bravais_result_t bravais_crypto_init(void) { + // 0 = initialized now, 1 = already initialized, -1 = failure + if (sodium_init() < 0) { + return BRAVAIS_ERR_UNKNOWN; + } + + return BRAVAIS_OK; +} + +void bravais_random_bytes(uint8_t* buf, size_t len) { + if (!buf || len == 0) { return; } + + randombytes_buf(buf, len); +} diff --git a/src/crypto/rng.h b/src/crypto/rng.h new file mode 100644 index 0000000..dabd9f9 --- /dev/null +++ b/src/crypto/rng.h @@ -0,0 +1,20 @@ +// rng.h +// Library initialization and randomness. +// +// The one documented sans-IO exception: randomness comes from the OS through libsodium. + +#ifndef BRAVAIS_RNG_H +#define BRAVAIS_RNG_H + +#include +#include + +#include "bravais.h" + +// Initialize the crypto backends. Idempotent and thread-safe; call before any other crypto/ function. +bravais_result_t bravais_crypto_init(void); + +// Fill buf with len cryptographically secure random bytes. Cannot fail once initialized. +void bravais_random_bytes(uint8_t* buf, size_t len); + +#endif // BRAVAIS_RNG_H diff --git a/src/crypto/x25519.c b/src/crypto/x25519.c new file mode 100644 index 0000000..292ae58 --- /dev/null +++ b/src/crypto/x25519.c @@ -0,0 +1,27 @@ +// x25519.c + +#include "crypto/x25519.h" + +#include +#include + +static_assert(crypto_scalarmult_BYTES == BRAVAIS_X25519_KEY_LENGTH, "X25519 shared secret size"); +static_assert(crypto_scalarmult_SCALARBYTES == BRAVAIS_X25519_KEY_LENGTH, "X25519 private key size"); + +void bravais_x25519_keypair(uint8_t* outPrivateKey, uint8_t* outPublicKey) { + randombytes_buf(outPrivateKey, BRAVAIS_X25519_KEY_LENGTH); + crypto_scalarmult_base(outPublicKey, outPrivateKey); +} + +void bravais_x25519_public_key(const uint8_t* privateKey, uint8_t* outPublicKey) { + crypto_scalarmult_base(outPublicKey, privateKey); +} + +bravais_result_t bravais_x25519_dh(const uint8_t* privateKey, const uint8_t* publicKey, uint8_t* outShared) { + // libsodium returns -1 when the output is all zeros, i.e. the peer sent a low-order point + if (crypto_scalarmult(outShared, privateKey, publicKey) != 0) { + return BRAVAIS_ERR_AUTH; + } + + return BRAVAIS_OK; +} diff --git a/src/crypto/x25519.h b/src/crypto/x25519.h new file mode 100644 index 0000000..19750da --- /dev/null +++ b/src/crypto/x25519.h @@ -0,0 +1,21 @@ +// x25519.h +// X25519 key agreement (RFC 7748) + +#ifndef BRAVAIS_X25519_H +#define BRAVAIS_X25519_H + +#include + +#include "bravais.h" + +// Generate a fresh keypair. +void bravais_x25519_keypair(uint8_t* outPrivateKey, uint8_t* outPublicKey); + +// Derive the public key belonging to a private key. +void bravais_x25519_public_key(const uint8_t* privateKey, uint8_t* outPublicKey); + +// Shared secret between our private key and their public key. +// BRAVAIS_ERR_AUTH if the result is all zeros, which happens only for a low-order (malicious) public key. +bravais_result_t bravais_x25519_dh(const uint8_t* privateKey, const uint8_t* publicKey, uint8_t* outShared); + +#endif // BRAVAIS_X25519_H