summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2013-11-30 07:28:56 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2013-11-30 07:28:56 +0100
commit53be4c96b3508a4e19e89d16662329b0ce303df6 (patch)
tree93ca9c2cfa19d2e93cae5aadf107331f777c5858
parent9dacff2507a1e69cecc0ec888d49d296bd9c91b0 (diff)
downloadfastd-53be4c96b3508a4e19e89d16662329b0ce303df6.tar
fastd-53be4c96b3508a4e19e89d16662329b0ce303df6.zip
cipher-test: use the new common header helpers
-rw-r--r--src/methods/cipher_test/cipher_test.c38
1 files changed, 11 insertions, 27 deletions
diff --git a/src/methods/cipher_test/cipher_test.c b/src/methods/cipher_test/cipher_test.c
index 2516164..3241acf 100644
--- a/src/methods/cipher_test/cipher_test.c
+++ b/src/methods/cipher_test/cipher_test.c
@@ -117,13 +117,8 @@ static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fast
if (tail_len)
memset(in.data+in.len, 0, tail_len);
- size_t iv_length = session->method->cipher_info->iv_length;
- uint8_t nonce[iv_length];
- if (iv_length) {
- memset(nonce, 0, iv_length);
- memcpy(nonce, session->common.send_nonce, min_size_t(COMMON_NONCEBYTES, iv_length));
- nonce[iv_length-1] = 1;
- }
+ uint8_t nonce[session->method->cipher_info->iv_length];
+ fastd_method_expand_nonce(nonce, session->common.send_nonce, sizeof(nonce));
int n_blocks = block_count(in.len, sizeof(fastd_block128_t));
@@ -139,13 +134,9 @@ static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fast
fastd_buffer_free(in);
- fastd_buffer_pull_head(ctx, out, COMMON_HEADBYTES);
-
- memcpy(out->data, session->common.send_nonce, COMMON_NONCEBYTES);
+ fastd_method_put_common_header(ctx, out, session->common.send_nonce, 0);
fastd_method_increment_nonce(&session->common);
- ((uint8_t*)out->data)[COMMON_NONCEBYTES] = 0; /* flags */
-
return true;
}
@@ -156,24 +147,17 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho
if (!method_session_is_valid(ctx, session))
return false;
- const uint8_t *common_nonce = in.data;
-
- if (common_nonce[COMMON_NONCEBYTES]) /* flags */
+ uint8_t in_nonce[COMMON_NONCEBYTES];
+ uint8_t flags;
+ int64_t age;
+ if (!fastd_method_handle_common_header(ctx, &session->common, &in, in_nonce, &flags, &age))
return false;
- size_t iv_length = session->method->cipher_info->iv_length;
- uint8_t nonce[iv_length];
- if (iv_length) {
- memset(nonce, 0, iv_length);
- memcpy(nonce, common_nonce, min_size_t(COMMON_NONCEBYTES, iv_length));
- nonce[iv_length-1] = 1;
- }
-
- int64_t age;
- if (!fastd_method_is_nonce_valid(ctx, &session->common, in.data, &age))
+ if (flags)
return false;
- fastd_buffer_push_head(ctx, &in, COMMON_HEADBYTES);
+ uint8_t nonce[session->method->cipher_info->iv_length];
+ fastd_method_expand_nonce(nonce, in_nonce, sizeof(nonce));
size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len;
*out = fastd_buffer_alloc(ctx, in.len, 0, tail_len);
@@ -190,7 +174,7 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho
return false;
}
- if (!fastd_method_reorder_check(ctx, peer, &session->common, common_nonce, age)) {
+ if (!fastd_method_reorder_check(ctx, peer, &session->common, in_nonce, age)) {
fastd_buffer_free(*out);
*out = fastd_buffer_alloc(ctx, 0, 0, 0);
}