mirror of
https://github.com/neocturne/fastd.git
synced 2025-06-05 20:25:09 +02:00
methods: avoid modifying the input buffer
Many methods only need a movable view of the input buffer.
This commit is contained in:
parent
2b6de7eddf
commit
68265164a8
7 changed files with 53 additions and 39 deletions
|
@ -123,7 +123,7 @@ static bool method_encrypt(
|
|||
|
||||
int n_blocks = block_count(in.len, sizeof(fastd_block128_t));
|
||||
|
||||
fastd_block128_t *inblocks = in.data;
|
||||
const fastd_block128_t *inblocks = in.data;
|
||||
fastd_block128_t *outblocks = out->data;
|
||||
|
||||
if (!session->cipher->crypt(
|
||||
|
@ -152,10 +152,12 @@ static bool method_decrypt(
|
|||
if (!method_session_is_valid(session))
|
||||
return false;
|
||||
|
||||
fastd_buffer_view_t in_view = fastd_buffer_get_view(&in);
|
||||
|
||||
uint8_t in_nonce[COMMON_NONCEBYTES];
|
||||
uint8_t flags;
|
||||
int64_t age;
|
||||
if (!fastd_method_handle_common_header(&session->common, &in, in_nonce, &flags, &age))
|
||||
if (!fastd_method_handle_common_header(&session->common, &in_view, in_nonce, &flags, &age))
|
||||
return false;
|
||||
|
||||
if (flags)
|
||||
|
@ -164,11 +166,11 @@ static bool method_decrypt(
|
|||
uint8_t nonce[session->method->cipher_info->iv_length ?: 1] __attribute__((aligned(8)));
|
||||
fastd_method_expand_nonce(nonce, in_nonce, sizeof(nonce));
|
||||
|
||||
*out = fastd_buffer_alloc(in.len, 0);
|
||||
*out = fastd_buffer_alloc(in_view.len, 0);
|
||||
|
||||
int n_blocks = block_count(in.len, sizeof(fastd_block128_t));
|
||||
int n_blocks = block_count(in_view.len, sizeof(fastd_block128_t));
|
||||
|
||||
fastd_block128_t *inblocks = in.data;
|
||||
const fastd_block128_t *inblocks = in_view.data;
|
||||
fastd_block128_t *outblocks = out->data;
|
||||
|
||||
if (!session->cipher->crypt(
|
||||
|
|
|
@ -120,18 +120,18 @@ fastd_method_put_common_header(fastd_buffer_t *buffer, const uint8_t nonce[COMMO
|
|||
fastd_buffer_push_from(buffer, &packet_type, 1);
|
||||
}
|
||||
|
||||
/** Removes the common header from a packet buffer */
|
||||
/** Removes the common header from a view of a packet buffer */
|
||||
static inline void
|
||||
fastd_method_take_common_header(fastd_buffer_t *buffer, uint8_t nonce[COMMON_NONCEBYTES], uint8_t *flags) {
|
||||
fastd_buffer_pull(buffer, 1);
|
||||
fastd_buffer_pull_to(buffer, flags, 1);
|
||||
fastd_buffer_pull_to(buffer, nonce, COMMON_NONCEBYTES);
|
||||
fastd_method_take_common_header(fastd_buffer_view_t *buffer, uint8_t nonce[COMMON_NONCEBYTES], uint8_t *flags) {
|
||||
fastd_buffer_view_pull(buffer, 1);
|
||||
fastd_buffer_view_pull_to(buffer, flags, 1);
|
||||
fastd_buffer_view_pull_to(buffer, nonce, COMMON_NONCEBYTES);
|
||||
}
|
||||
|
||||
/** Handles the common header of a packet */
|
||||
static inline bool fastd_method_handle_common_header(
|
||||
const fastd_method_common_t *session, fastd_buffer_t *buffer, uint8_t nonce[COMMON_NONCEBYTES], uint8_t *flags,
|
||||
int64_t *age) {
|
||||
const fastd_method_common_t *session, fastd_buffer_view_t *buffer, uint8_t nonce[COMMON_NONCEBYTES],
|
||||
uint8_t *flags, int64_t *age) {
|
||||
fastd_method_take_common_header(buffer, nonce, flags);
|
||||
return fastd_method_is_nonce_valid(session, nonce, age);
|
||||
}
|
||||
|
|
|
@ -186,7 +186,7 @@ static bool method_encrypt(
|
|||
|
||||
int n_blocks = block_count(in.len, sizeof(fastd_block128_t));
|
||||
|
||||
fastd_block128_t *inblocks = in.data;
|
||||
const fastd_block128_t *inblocks = in.data;
|
||||
fastd_block128_t *outblocks = out->data;
|
||||
fastd_block128_t tag;
|
||||
|
||||
|
@ -233,10 +233,12 @@ static bool method_decrypt(
|
|||
if (!method_session_is_valid(session))
|
||||
return false;
|
||||
|
||||
fastd_buffer_view_t in_view = fastd_buffer_get_view(&in);
|
||||
|
||||
uint8_t in_nonce[COMMON_NONCEBYTES];
|
||||
uint8_t flags;
|
||||
int64_t age;
|
||||
if (!fastd_method_handle_common_header(&session->common, &in, in_nonce, &flags, &age))
|
||||
if (!fastd_method_handle_common_header(&session->common, &in_view, in_nonce, &flags, &age))
|
||||
return false;
|
||||
|
||||
if (flags)
|
||||
|
@ -248,11 +250,11 @@ static bool method_decrypt(
|
|||
uint8_t gmac_nonce[session->method->gmac_cipher_info->iv_length] __attribute__((aligned(8)));
|
||||
fastd_method_expand_nonce(gmac_nonce, in_nonce, sizeof(gmac_nonce));
|
||||
|
||||
*out = fastd_buffer_alloc(in.len, 0);
|
||||
*out = fastd_buffer_alloc(in_view.len, 0);
|
||||
|
||||
int n_blocks = block_count(in.len, sizeof(fastd_block128_t));
|
||||
int n_blocks = block_count(in_view.len, sizeof(fastd_block128_t));
|
||||
|
||||
fastd_block128_t *inblocks = in.data;
|
||||
const fastd_block128_t *inblocks = in_view.data;
|
||||
fastd_block128_t *outblocks = out->data;
|
||||
fastd_block128_t tag;
|
||||
|
||||
|
@ -265,7 +267,7 @@ static bool method_decrypt(
|
|||
nonce))
|
||||
goto fail;
|
||||
|
||||
if (!session->ghash->digest(session->ghash_state, &tag, inblocks + 1, in.len - sizeof(fastd_block128_t)))
|
||||
if (!session->ghash->digest(session->ghash_state, &tag, inblocks + 1, in_view.len - sizeof(fastd_block128_t)))
|
||||
goto fail;
|
||||
|
||||
if (!block_equal(&tag, &outblocks[0]))
|
||||
|
|
|
@ -164,7 +164,7 @@ static bool method_encrypt(
|
|||
|
||||
int n_blocks = block_count(in.len, sizeof(fastd_block128_t));
|
||||
|
||||
fastd_block128_t *inblocks = in.data;
|
||||
const fastd_block128_t *inblocks = in.data;
|
||||
fastd_block128_t *outblocks = out->data;
|
||||
fastd_block128_t tag;
|
||||
|
||||
|
@ -211,10 +211,12 @@ static bool method_decrypt(
|
|||
if (!method_session_is_valid(session))
|
||||
return false;
|
||||
|
||||
fastd_buffer_view_t in_view = fastd_buffer_get_view(&in);
|
||||
|
||||
uint8_t in_nonce[COMMON_NONCEBYTES];
|
||||
uint8_t flags;
|
||||
int64_t age;
|
||||
if (!fastd_method_handle_common_header(&session->common, &in, in_nonce, &flags, &age))
|
||||
if (!fastd_method_handle_common_header(&session->common, &in_view, in_nonce, &flags, &age))
|
||||
return false;
|
||||
|
||||
if (flags)
|
||||
|
@ -226,11 +228,11 @@ static bool method_decrypt(
|
|||
uint8_t umac_nonce[session->method->umac_cipher_info->iv_length] __attribute__((aligned(8)));
|
||||
fastd_method_expand_nonce(umac_nonce, in_nonce, sizeof(umac_nonce));
|
||||
|
||||
*out = fastd_buffer_alloc(in.len, 0);
|
||||
*out = fastd_buffer_alloc(in_view.len, 0);
|
||||
|
||||
int n_blocks = block_count(in.len, sizeof(fastd_block128_t));
|
||||
int n_blocks = block_count(in_view.len, sizeof(fastd_block128_t));
|
||||
|
||||
fastd_block128_t *inblocks = in.data;
|
||||
const fastd_block128_t *inblocks = in_view.data;
|
||||
fastd_block128_t *outblocks = out->data;
|
||||
fastd_block128_t tag;
|
||||
|
||||
|
@ -243,7 +245,7 @@ static bool method_decrypt(
|
|||
nonce))
|
||||
goto fail;
|
||||
|
||||
if (!session->uhash->digest(session->uhash_state, &tag, inblocks + 1, in.len - sizeof(fastd_block128_t)))
|
||||
if (!session->uhash->digest(session->uhash_state, &tag, inblocks + 1, in_view.len - sizeof(fastd_block128_t)))
|
||||
goto fail;
|
||||
|
||||
if (!block_equal(&tag, &outblocks[0]))
|
||||
|
|
|
@ -158,7 +158,7 @@ static bool method_encrypt(
|
|||
|
||||
int n_blocks = block_count(in.len, sizeof(fastd_block128_t));
|
||||
|
||||
fastd_block128_t *inblocks = in.data;
|
||||
const fastd_block128_t *inblocks = in.data;
|
||||
fastd_block128_t *outblocks = out->data;
|
||||
fastd_block128_t tag;
|
||||
|
||||
|
@ -195,10 +195,12 @@ static bool method_decrypt(
|
|||
if (!method_session_is_valid(session))
|
||||
return false;
|
||||
|
||||
fastd_buffer_view_t in_view = fastd_buffer_get_view(&in);
|
||||
|
||||
uint8_t in_nonce[COMMON_NONCEBYTES];
|
||||
uint8_t flags;
|
||||
int64_t age;
|
||||
if (!fastd_method_handle_common_header(&session->common, &in, in_nonce, &flags, &age))
|
||||
if (!fastd_method_handle_common_header(&session->common, &in_view, in_nonce, &flags, &age))
|
||||
return false;
|
||||
|
||||
if (flags)
|
||||
|
@ -207,11 +209,11 @@ static bool method_decrypt(
|
|||
uint8_t nonce[session->method->cipher_info->iv_length] __attribute__((aligned(8)));
|
||||
fastd_method_expand_nonce(nonce, in_nonce, sizeof(nonce));
|
||||
|
||||
*out = fastd_buffer_alloc(in.len, 0);
|
||||
*out = fastd_buffer_alloc(in_view.len, 0);
|
||||
|
||||
int n_blocks = block_count(in.len, sizeof(fastd_block128_t));
|
||||
int n_blocks = block_count(in_view.len, sizeof(fastd_block128_t));
|
||||
|
||||
fastd_block128_t *inblocks = in.data;
|
||||
const fastd_block128_t *inblocks = in_view.data;
|
||||
fastd_block128_t *outblocks = out->data;
|
||||
fastd_block128_t tag;
|
||||
|
||||
|
@ -219,7 +221,7 @@ static bool method_decrypt(
|
|||
session->cipher_state, outblocks, inblocks, n_blocks * sizeof(fastd_block128_t), nonce))
|
||||
goto fail;
|
||||
|
||||
if (!session->ghash->digest(session->ghash_state, &tag, inblocks + 1, in.len - sizeof(fastd_block128_t)))
|
||||
if (!session->ghash->digest(session->ghash_state, &tag, inblocks + 1, in_view.len - sizeof(fastd_block128_t)))
|
||||
goto fail;
|
||||
|
||||
if (!block_equal(&tag, &outblocks[0]))
|
||||
|
|
|
@ -139,7 +139,7 @@ static bool method_encrypt(
|
|||
|
||||
int n_blocks = block_count(in.len, sizeof(fastd_block128_t));
|
||||
|
||||
fastd_block128_t *inblocks = in.data;
|
||||
const fastd_block128_t *inblocks = in.data;
|
||||
fastd_block128_t *outblocks = out->data;
|
||||
uint8_t tag[TAGBYTES] __attribute__((aligned(8)));
|
||||
|
||||
|
@ -176,10 +176,13 @@ static bool method_decrypt(
|
|||
if (!method_session_is_valid(session))
|
||||
return false;
|
||||
|
||||
|
||||
uint8_t in_nonce[COMMON_NONCEBYTES];
|
||||
uint8_t flags;
|
||||
int64_t age;
|
||||
if (!fastd_method_handle_common_header(&session->common, &in, in_nonce, &flags, &age))
|
||||
|
||||
fastd_buffer_view_t in_view = fastd_buffer_get_view(&in);
|
||||
if (!fastd_method_handle_common_header(&session->common, &in_view, in_nonce, &flags, &age))
|
||||
return false;
|
||||
|
||||
if (flags)
|
||||
|
@ -189,13 +192,14 @@ static bool method_decrypt(
|
|||
fastd_method_expand_nonce(nonce, in_nonce, sizeof(nonce));
|
||||
|
||||
uint8_t tag[TAGBYTES] __attribute__((aligned(8)));
|
||||
fastd_buffer_pull(&in, COMMON_HEADBYTES);
|
||||
fastd_buffer_pull_to(&in, tag, TAGBYTES);
|
||||
fastd_buffer_push_zero(&in, KEYBYTES);
|
||||
|
||||
*out = fastd_buffer_alloc(in.len, 0);
|
||||
|
||||
int n_blocks = block_count(in.len, sizeof(fastd_block128_t));
|
||||
fastd_block128_t *inblocks = in.data;
|
||||
const fastd_block128_t *inblocks = in.data;
|
||||
fastd_block128_t *outblocks = out->data;
|
||||
|
||||
bool ok = session->cipher->crypt(
|
||||
|
|
|
@ -138,7 +138,7 @@ static bool method_encrypt(
|
|||
|
||||
int n_blocks = block_count(in.len, sizeof(fastd_block128_t));
|
||||
|
||||
fastd_block128_t *inblocks = in.data;
|
||||
const fastd_block128_t *inblocks = in.data;
|
||||
fastd_block128_t *outblocks = out->data;
|
||||
fastd_block128_t tag;
|
||||
|
||||
|
@ -175,10 +175,12 @@ static bool method_decrypt(
|
|||
if (!method_session_is_valid(session))
|
||||
return false;
|
||||
|
||||
fastd_buffer_view_t in_view = fastd_buffer_get_view(&in);
|
||||
|
||||
uint8_t in_nonce[COMMON_NONCEBYTES];
|
||||
uint8_t flags;
|
||||
int64_t age;
|
||||
if (!fastd_method_handle_common_header(&session->common, &in, in_nonce, &flags, &age))
|
||||
if (!fastd_method_handle_common_header(&session->common, &in_view, in_nonce, &flags, &age))
|
||||
return false;
|
||||
|
||||
if (flags)
|
||||
|
@ -187,11 +189,11 @@ static bool method_decrypt(
|
|||
uint8_t nonce[session->method->cipher_info->iv_length] __attribute__((aligned(8)));
|
||||
fastd_method_expand_nonce(nonce, in_nonce, sizeof(nonce));
|
||||
|
||||
*out = fastd_buffer_alloc(in.len, 0);
|
||||
*out = fastd_buffer_alloc(in_view.len, 0);
|
||||
|
||||
int n_blocks = block_count(in.len, sizeof(fastd_block128_t));
|
||||
int n_blocks = block_count(in_view.len, sizeof(fastd_block128_t));
|
||||
|
||||
fastd_block128_t *inblocks = in.data;
|
||||
const fastd_block128_t *inblocks = in_view.data;
|
||||
fastd_block128_t *outblocks = out->data;
|
||||
fastd_block128_t tag;
|
||||
|
||||
|
@ -199,7 +201,7 @@ static bool method_decrypt(
|
|||
session->cipher_state, outblocks, inblocks, n_blocks * sizeof(fastd_block128_t), nonce))
|
||||
goto fail;
|
||||
|
||||
if (!session->uhash->digest(session->uhash_state, &tag, inblocks + 1, in.len - sizeof(fastd_block128_t)))
|
||||
if (!session->uhash->digest(session->uhash_state, &tag, inblocks + 1, in_view.len - sizeof(fastd_block128_t)))
|
||||
goto fail;
|
||||
|
||||
if (!block_equal(&tag, &outblocks[0]))
|
||||
|
|
Loading…
Add table
Reference in a new issue