Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug where framefmt serialize returns a wrong ciphertext size #385

Merged
merged 6 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions source/framefmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,9 @@ int aws_cryptosdk_serialize_frame(
// lets us avoid having to pass in a redundant argument, avoids needing to
// take a branch in serde_framed, and does not impact the serialized
// output.
state.max_frame_size = plaintext_size;
state.plaintext_size = plaintext_size;
// Currently all supported algorithms have plaintext = ciphertext size
state.ciphertext_size = plaintext_size;
state.max_frame_size = plaintext_size;
state.plaintext_size = plaintext_size;
state.ciphertext_size = 0;

state.alg_props = alg_props;
state.u.buffer = *ciphertext_buf;
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ aws_add_test(header ${VALGRIND} ${CMAKE_CURRENT_BINARY_DIR}/unit-test-suite head
aws_add_test(materials ${VALGRIND} ${CMAKE_CURRENT_BINARY_DIR}/unit-test-suite materials)
aws_add_test(enc_ctx ${VALGRIND} ${CMAKE_CURRENT_BINARY_DIR}/unit-test-suite enc_ctx)
aws_add_test(encrypt ${VALGRIND} ${CMAKE_CURRENT_BINARY_DIR}/unit-test-suite encrypt)
aws_add_test(framefmt ${VALGRIND} ${CMAKE_CURRENT_BINARY_DIR}/unit-test-suite framefmt)
aws_add_test(hkdf ${VALGRIND} ${CMAKE_CURRENT_BINARY_DIR}/unit-test-suite hkdf)
aws_add_test(raw_aes_keyring ${VALGRIND} ${CMAKE_CURRENT_BINARY_DIR}/unit-test-suite raw_aes_keyring)
aws_add_test(raw_rsa_keyring ${VALGRIND} ${CMAKE_CURRENT_BINARY_DIR}/unit-test-suite raw_rsa_keyring)
Expand Down
1 change: 1 addition & 0 deletions tests/unit/runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct test_case *test_groups[] = { header_test_cases,
materials_test_cases,
enc_ctx_test_cases,
encrypt_test_cases,
framefmt_test_cases,
hkdf_test_cases,
raw_aes_keyring_decrypt_test_cases,
raw_aes_keyring_encrypt_test_cases,
Expand Down
64 changes: 64 additions & 0 deletions tests/unit/t_framefmt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <aws/cryptosdk/default_cmm.h>
#include <aws/cryptosdk/private/cipher.h>
#include <aws/cryptosdk/private/framefmt.h>
#include <aws/cryptosdk/session.h>
#include <stdlib.h>
#include "counting_keyring.h"
#include "testing.h"
#include "testutil.h"
#include "zero_keyring.h"

int test_serialize_return_ciphertext_size() {
// Arguments to the function
size_t ciphertext_buf_capacity = 10000;
size_t plaintext_size = 100;
enum aws_cryptosdk_alg_id id = ALG_AES128_GCM_IV12_TAG16_HKDF_SHA256_ECDSA_P256;

struct aws_allocator *alloc = aws_default_allocator();

// Alocate the frame
struct aws_cryptosdk_frame out_frame;
out_frame.type = FRAME_TYPE_SINGLE;
out_frame.sequence_number = 1;

// Allocate the byte buffer
struct aws_byte_buf ciphertext_buf;
TEST_ASSERT_SUCCESS(aws_byte_buf_init(&ciphertext_buf, alloc, ciphertext_buf_capacity));

// Increase the length of the ciphertext buffer to simulate already having data in it
ciphertext_buf.len += 1000;

// Allocate and initialize the alg_properties
const struct aws_cryptosdk_alg_properties *alg_props = aws_cryptosdk_alg_props(id);
size_t ciphertext_size;

size_t old_ciphertext_buf_len = ciphertext_buf.len;

TEST_ASSERT_SUCCESS(
aws_cryptosdk_serialize_frame(&out_frame, &ciphertext_size, plaintext_size, &ciphertext_buf, alg_props));
// Assert that the returned size is correct
TEST_ASSERT(old_ciphertext_buf_len + ciphertext_size == ciphertext_buf.len);
angelhof marked this conversation as resolved.
Show resolved Hide resolved

aws_byte_buf_clean_up(&ciphertext_buf);

return 0;
}

struct test_case framefmt_test_cases[] = {
{ "framefmt", "test_serialize_return_ciphertext_size", test_serialize_return_ciphertext_size }, { NULL }
};
1 change: 1 addition & 0 deletions tests/unit/testing.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extern struct test_case cipher_test_cases[];
extern struct test_case materials_test_cases[];
extern struct test_case enc_ctx_test_cases[];
extern struct test_case encrypt_test_cases[];
extern struct test_case framefmt_test_cases[];
extern struct test_case hkdf_test_cases[];
extern struct test_case raw_aes_keyring_provider_info_test_cases[];
extern struct test_case raw_aes_keyring_decrypt_test_cases[];
Expand Down