44 const uint8_t *info,
int info_len,
45 uint8_t *out,
int out_len)
56 for (uint8_t block = 1; (block - 1) * digest_size < out_len; block++)
65 t_prev_len = digest_size;
68 int out_offset = (block - 1) * digest_size;
69 int copylen =
min_int(digest_size, out_len - out_offset);
71 memcpy(out + out_offset, t_prev, copylen);
79 const uint8_t *label,
size_t label_len,
80 const uint8_t *
context,
size_t context_len,
81 uint8_t *out, uint16_t out_len)
83 if (secret_len != 32 || label_len > 250 || context_len > 255
96 const uint8_t *label_prefix = (
const uint8_t *) (
"ovpn ");
99 int hkdf_label_len = 2 + prefix_len + 1 + label_len + 1 + context_len;
132 const uint8_t epoch_update_label[] =
"datakey upd";
134 const size_t epoch_update_label_len =
sizeof(epoch_update_label) - 1;
138 epoch_update_label, epoch_update_label_len,
157 const uint8_t epoch_data_key_label[] =
"data_key";
159 const size_t epoch_data_key_label_len =
sizeof(epoch_data_key_label) - 1;
162 epoch_data_key_label, epoch_data_key_label_len,
166 const uint8_t epoch_data_iv_label[] =
"data_iv";
168 const size_t epoch_data_iv_label_len =
sizeof(epoch_data_iv_label) - 1;
171 epoch_data_iv_label, epoch_data_iv_label_len,
184 char name[32] = { 0 };
242 uint16_t current_highest_key = highest_future_key->
epoch ? highest_future_key->
epoch : 1;
244 uint16_t num_keys_generate = desired_highest_key - current_highest_key;
250 for (uint16_t i = 0; i < num_keys_generate; i++)
299 ASSERT(fki < co->epoch_data_keys_future_count);
331 memset(new_ctx, 0,
sizeof(
struct key_ctx));
356 uint16_t future_key_count)
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
static bool buf_write_u16(struct buffer *dest, uint16_t data)
static uint8_t * buf_bptr(const struct buffer *buf)
#define ALLOC_ARRAY_CLEAR(dptr, type, n)
static int buf_len(const struct buffer *buf)
static bool buf_write(struct buffer *dest, const void *src, size_t size)
static bool buf_write_u8(struct buffer *dest, uint8_t data)
static void gc_free(struct gc_arena *a)
static struct gc_arena gc_new(void)
void init_key_bi_ctx_send(struct key_ctx *ctx, const struct key_parameters *key_params, const struct key_type *kt, const char *name)
uint64_t cipher_get_aead_limits(const char *ciphername)
Check if the cipher is an AEAD cipher and needs to be limited to a certain number of number of blocks...
void free_key_ctx(struct key_ctx *ctx)
void init_key_bi_ctx_recv(struct key_ctx *ctx, const struct key_parameters *key_params, const struct key_type *kt, const char *name)
Data Channel Cryptography Module.
static bool cipher_decrypt_verify_fail_warn(const struct key_ctx *ctx)
Check if the number of failed decryption is approaching the limit and we should try to move to a new ...
static bool aead_usage_limit_reached(const uint64_t limit, const struct key_ctx *key_ctx, int64_t higest_pid)
Checks if the usage limit for an AEAD cipher is reached.
Data Channel Cryptography SSL library-specific backend interface.
void hmac_ctx_update(hmac_ctx_t *ctx, const uint8_t *src, int src_len)
hmac_ctx_t * hmac_ctx_new(void)
void hmac_ctx_reset(hmac_ctx_t *ctx)
void hmac_ctx_init(hmac_ctx_t *ctx, const uint8_t *key, const char *mdname)
void hmac_ctx_final(hmac_ctx_t *ctx, uint8_t *dst)
void hmac_ctx_free(hmac_ctx_t *ctx)
int cipher_kt_iv_size(const char *ciphername)
Returns the size of the IV used by the cipher, in bytes, or 0 if no IV is used.
int cipher_kt_key_size(const char *ciphername)
Returns the size of keys used by the cipher, in bytes.
void hmac_ctx_cleanup(hmac_ctx_t *ctx)
bool ovpn_expand_label(const uint8_t *secret, size_t secret_len, const uint8_t *label, size_t label_len, const uint8_t *context, size_t context_len, uint8_t *out, uint16_t out_len)
Variant of the RFC 8446 TLS 1.3 HKDF-Expand-Label function with the following differences/restriction...
void epoch_generate_future_receive_keys(struct crypto_options *co)
Generates and fills the epoch_data_keys_future with next valid future keys in crypto_options using th...
void epoch_replace_update_recv_key(struct crypto_options *co, uint16_t new_epoch)
This is called when the peer uses a new send key that is not the default key.
static void epoch_key_iterate(struct epoch_key *epoch_key)
Iterates the epoch key to make it E_n+1, ie increase the epoch by one and derive the new key material...
static void epoch_init_send_key_ctx(struct crypto_options *co)
void free_epoch_key_ctx(struct crypto_options *co)
Frees the extra data structures used by epoch keys in crypto_options.
void epoch_check_send_iterate(struct crypto_options *opt)
Checks if we need to iterate the send epoch key.
static void epoch_init_recv_key(struct key_ctx *ctx, struct crypto_options *co)
struct key_ctx * epoch_lookup_decrypt_key(struct crypto_options *opt, uint16_t epoch)
Using an epoch, this function will try to retrieve a decryption key context that matches that epoch f...
void epoch_init_key_ctx(struct crypto_options *co, const struct key_type *key_type, const struct epoch_key *e1_send, const struct epoch_key *e1_recv, uint16_t future_key_count)
Initialises data channel keys and internal structures for epoch data keys using the provided E0 epoch...
void epoch_data_key_derive(struct key_parameters *key, const struct epoch_key *epoch_key, const struct key_type *kt)
Generate a data channel key pair from the epoch key.
void ovpn_hkdf_expand(const uint8_t *secret, const uint8_t *info, int info_len, uint8_t *out, int out_len)
Implementation of the RFC5869 HKDF-Expand function with the following restrictions.
void epoch_iterate_send_key(struct crypto_options *co)
Updates the send key and send_epoch_key in cryptio_options->key_ctx_bi to use the next epoch.
mbedtls_md_context_t hmac_ctx_t
Generic HMAC context.
#define SHA256_DIGEST_LENGTH
static int min_int(int x, int y)
void packet_id_move_recv(struct packet_id_rec *dest, struct packet_id_rec *src)
Move the packet id recv structure from src to dest.
static void reset_packet_id_send(struct packet_id_send *p)
Reset the current send packet id to its initial state.
#define PACKET_ID_EPOCH_MAX
Wrapper structure for dynamically allocated memory.
int len
Length in bytes of the actual content within the allocated memory.
Contains all state information for one tunnel.
Security parameter state for processing data channel packets.
struct epoch_key epoch_key_send
last epoch_key used for generation of the current send data keys.
struct key_type epoch_key_type
the key_type that is used to generate the epoch keys
struct key_ctx epoch_retiring_data_receive_key
The old key before the sender switched to a new epoch data key.
uint64_t aead_usage_limit
The limit for AEAD cipher, this is the sum of packets + blocks that are allowed to be used.
struct key_ctx * epoch_data_keys_future
Keeps the future epoch data keys for decryption.
struct key_ctx_bi key_ctx_bi
OpenSSL cipher and HMAC contexts for both sending and receiving directions.
uint16_t epoch_data_keys_future_count
number of keys stored in epoch_data_keys_future
struct packet_id_rec epoch_retiring_key_pid_recv
struct epoch_key epoch_key_recv
epoch_key used for the highest receive epoch keys
struct packet_id packet_id
Current packet ID state for both sending and receiving directions.
uint8_t epoch_key[SHA256_DIGEST_LENGTH]
Garbage collection arena used to keep track of dynamically allocated memory.
Container for two sets of OpenSSL cipher and/or HMAC contexts for both sending and receiving directio...
struct key_ctx decrypt
cipher and/or HMAC contexts for receiving direction.
struct key_ctx encrypt
Cipher and/or HMAC contexts for sending direction.
Container for one set of cipher and/or HMAC contexts.
uint16_t epoch
OpenVPN data channel epoch, this variable holds the epoch number this key belongs to.
internal structure similar to struct key that holds key information but is not represented on wire an...
const char * cipher
const name of the cipher
Container for unidirectional cipher and HMAC key material.
uint8_t cipher[MAX_CIPHER_KEY_LENGTH]
Key material for cipher operations.
uint8_t hmac[MAX_HMAC_KEY_LENGTH]
Key material for HMAC operations.
struct seq_list * seq_list
struct packet_id_send send