| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/cal/symmetric_cipher.h> |
| 6 |
|
| 7 |
#include <aws/testing/aws_test_harness.h> |
| 8 |
|
| 9 |
#include "test_case_helper.h" |
| 10 |
|
| 11 |
static int s_check_single_block_cbc( |
| 12 |
struct aws_allocator *allocator, |
| 13 |
const struct aws_byte_cursor key, |
| 14 |
const struct aws_byte_cursor iv, |
| 15 |
const struct aws_byte_cursor data, |
| 16 |
const struct aws_byte_cursor expected) { |
| 17 |
|
| 18 |
aws_cal_library_test_init(allocator); |
| 19 |
struct aws_symmetric_cipher *cipher = aws_aes_cbc_256_new(allocator, &key, &iv); |
| 20 |
ASSERT_NOT_NULL(cipher); |
| 21 |
|
| 22 |
struct aws_byte_buf encrypted_buf; |
| 23 |
aws_byte_buf_init(&encrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 24 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, data, &encrypted_buf)); |
| 25 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &encrypted_buf)); |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
encrypted_buf.len -= AWS_AES_256_CIPHER_BLOCK_SIZE; |
| 31 |
ASSERT_BIN_ARRAYS_EQUALS(expected.ptr, expected.len, encrypted_buf.buffer, encrypted_buf.len); |
| 32 |
encrypted_buf.len += AWS_AES_256_CIPHER_BLOCK_SIZE; |
| 33 |
|
| 34 |
aws_symmetric_cipher_reset(cipher); |
| 35 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 36 |
struct aws_byte_cursor encrypted_cur = aws_byte_cursor_from_buf(&encrypted_buf); |
| 37 |
struct aws_byte_buf decrypted_buf; |
| 38 |
aws_byte_buf_init(&decrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 39 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, encrypted_cur, &decrypted_buf)); |
| 40 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 41 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 42 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_FINALIZED, aws_symmetric_cipher_get_state(cipher)); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
ASSERT_UINT_EQUALS(AWS_AES_256_CIPHER_BLOCK_SIZE, decrypted_buf.len); |
| 47 |
|
| 48 |
ASSERT_BIN_ARRAYS_EQUALS(data.ptr, data.len, decrypted_buf.buffer, decrypted_buf.len); |
| 49 |
|
| 50 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 51 |
aws_byte_buf_clean_up(&encrypted_buf); |
| 52 |
aws_symmetric_cipher_destroy(cipher); |
| 53 |
aws_cal_library_clean_up(); |
| 54 |
return AWS_OP_SUCCESS; |
| 55 |
} |
| 56 |
|
| 57 |
static int s_NIST_CBCGFSbox256_case_1_fn(struct aws_allocator *allocator, void *ctx) { |
| 58 |
(void)ctx; |
| 59 |
|
| 60 |
uint8_t iv[AWS_AES_256_CIPHER_BLOCK_SIZE] = {0}; |
| 61 |
uint8_t key[AWS_AES_256_KEY_BYTE_LEN] = {0}; |
| 62 |
|
| 63 |
uint8_t data[] = {0x01, 0x47, 0x30, 0xf8, 0x0a, 0xc6, 0x25, 0xfe, 0x84, 0xf0, 0x26, 0xc6, 0x0b, 0xfd, 0x54, 0x7d}; |
| 64 |
uint8_t expected[] = { |
| 65 |
0x5c, 0x9d, 0x84, 0x4e, 0xd4, 0x6f, 0x98, 0x85, 0x08, 0x5e, 0x5d, 0x6a, 0x4f, 0x94, 0xc7, 0xd7}; |
| 66 |
|
| 67 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 68 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 69 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 70 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 71 |
|
| 72 |
return s_check_single_block_cbc(allocator, key_cur, iv_cur, data_cur, expected_cur); |
| 73 |
} |
| 74 |
AWS_TEST_CASE(aes_cbc_NIST_CBCGFSbox256_case_1, s_NIST_CBCGFSbox256_case_1_fn) |
| 75 |
|
| 76 |
static int s_NIST_CBCVarKey256_case_254_fn(struct aws_allocator *allocator, void *ctx) { |
| 77 |
(void)ctx; |
| 78 |
|
| 79 |
uint8_t iv[AWS_AES_256_CIPHER_BLOCK_SIZE] = {0}; |
| 80 |
uint8_t key[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 81 |
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe}; |
| 82 |
|
| 83 |
uint8_t data[AWS_AES_256_CIPHER_BLOCK_SIZE] = {0}; |
| 84 |
uint8_t expected[] = { |
| 85 |
0xb0, 0x7d, 0x4f, 0x3e, 0x2c, 0xd2, 0xef, 0x2e, 0xb5, 0x45, 0x98, 0x07, 0x54, 0xdf, 0xea, 0x0f}; |
| 86 |
|
| 87 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 88 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 89 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 90 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 91 |
|
| 92 |
return s_check_single_block_cbc(allocator, key_cur, iv_cur, data_cur, expected_cur); |
| 93 |
} |
| 94 |
AWS_TEST_CASE(aes_cbc_NIST_CBCVarKey256_case_254, s_NIST_CBCVarKey256_case_254_fn) |
| 95 |
|
| 96 |
static int s_NIST_CBCVarTxt256_case_110_fn(struct aws_allocator *allocator, void *ctx) { |
| 97 |
(void)ctx; |
| 98 |
|
| 99 |
uint8_t iv[AWS_AES_256_CIPHER_BLOCK_SIZE] = {0}; |
| 100 |
uint8_t key[AWS_AES_256_KEY_BYTE_LEN] = {0}; |
| 101 |
|
| 102 |
uint8_t data[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00}; |
| 103 |
uint8_t expected[] = { |
| 104 |
0x4b, 0x00, 0xc2, 0x7e, 0x8b, 0x26, 0xda, 0x7e, 0xab, 0x9d, 0x3a, 0x88, 0xde, 0xc8, 0xb0, 0x31}; |
| 105 |
|
| 106 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 107 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 108 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 109 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 110 |
|
| 111 |
return s_check_single_block_cbc(allocator, key_cur, iv_cur, data_cur, expected_cur); |
| 112 |
} |
| 113 |
AWS_TEST_CASE(aes_cbc_NIST_CBCVarTxt256_case_110, s_NIST_CBCVarTxt256_case_110_fn) |
| 114 |
|
| 115 |
static size_t s_get_cbc_padding(size_t data_len) { |
| 116 |
size_t remainder = data_len % AWS_AES_256_CIPHER_BLOCK_SIZE; |
| 117 |
if (remainder != 0) { |
| 118 |
return remainder; |
| 119 |
} |
| 120 |
|
| 121 |
return AWS_AES_256_CIPHER_BLOCK_SIZE; |
| 122 |
} |
| 123 |
|
| 124 |
static int s_check_multiple_block_cbc( |
| 125 |
struct aws_allocator *allocator, |
| 126 |
const struct aws_byte_cursor key, |
| 127 |
const struct aws_byte_cursor iv, |
| 128 |
const struct aws_byte_cursor data, |
| 129 |
const struct aws_byte_cursor expected) { |
| 130 |
|
| 131 |
(void)expected; |
| 132 |
aws_cal_library_test_init(allocator); |
| 133 |
struct aws_symmetric_cipher *cipher = aws_aes_cbc_256_new(allocator, &key, &iv); |
| 134 |
ASSERT_NOT_NULL(cipher); |
| 135 |
|
| 136 |
struct aws_byte_buf encrypted_buf; |
| 137 |
aws_byte_buf_init(&encrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 138 |
|
| 139 |
struct aws_byte_cursor data_cpy = data; |
| 140 |
|
| 141 |
while (data_cpy.len) { |
| 142 |
struct aws_byte_cursor to_encrypt = aws_byte_cursor_advance(&data_cpy, (size_t)aws_min_i64(24, data_cpy.len)); |
| 143 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, to_encrypt, &encrypted_buf)); |
| 144 |
} |
| 145 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &encrypted_buf)); |
| 146 |
|
| 147 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 148 |
expected.ptr, expected.len, encrypted_buf.buffer, encrypted_buf.len - s_get_cbc_padding(data.len)); |
| 149 |
|
| 150 |
aws_symmetric_cipher_reset(cipher); |
| 151 |
struct aws_byte_cursor encrypted_cur = aws_byte_cursor_from_buf(&encrypted_buf); |
| 152 |
struct aws_byte_buf decrypted_buf; |
| 153 |
aws_byte_buf_init(&decrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 154 |
|
| 155 |
|
| 156 |
while (encrypted_cur.len) { |
| 157 |
struct aws_byte_cursor to_decrypt = |
| 158 |
aws_byte_cursor_advance(&encrypted_cur, (size_t)aws_min_i64(24, encrypted_cur.len)); |
| 159 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 160 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, to_decrypt, &decrypted_buf)); |
| 161 |
} |
| 162 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 163 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_FINALIZED, aws_symmetric_cipher_get_state(cipher)); |
| 164 |
ASSERT_BIN_ARRAYS_EQUALS(data.ptr, data.len, decrypted_buf.buffer, decrypted_buf.len); |
| 165 |
|
| 166 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 167 |
aws_byte_buf_clean_up(&encrypted_buf); |
| 168 |
aws_symmetric_cipher_destroy(cipher); |
| 169 |
aws_cal_library_clean_up(); |
| 170 |
return AWS_OP_SUCCESS; |
| 171 |
} |
| 172 |
|
| 173 |
static int s_NIST_CBCMMT256_case_4_fn(struct aws_allocator *allocator, void *ctx) { |
| 174 |
(void)ctx; |
| 175 |
|
| 176 |
uint8_t iv[] = {0x11, 0x95, 0x8d, 0xc6, 0xab, 0x81, 0xe1, 0xc7, 0xf0, 0x16, 0x31, 0xe9, 0x94, 0x4e, 0x62, 0x0f}; |
| 177 |
uint8_t key[] = {0x9a, 0xdc, 0x8f, 0xbd, 0x50, 0x6e, 0x03, 0x2a, 0xf7, 0xfa, 0x20, 0xcf, 0x53, 0x43, 0x71, 0x9d, |
| 178 |
0xe6, 0xd1, 0x28, 0x8c, 0x15, 0x8c, 0x63, 0xd6, 0x87, 0x8a, 0xaf, 0x64, 0xce, 0x26, 0xca, 0x85}; |
| 179 |
|
| 180 |
uint8_t data[] = {0xc7, 0x91, 0x7f, 0x84, 0xf7, 0x47, 0xcd, 0x8c, 0x4b, 0x4f, 0xed, 0xc2, 0x21, 0x9b, 0xdb, 0xc5, |
| 181 |
0xf4, 0xd0, 0x75, 0x88, 0x38, 0x9d, 0x82, 0x48, 0x85, 0x4c, 0xf2, 0xc2, 0xf8, 0x96, 0x67, 0xa2, |
| 182 |
0xd7, 0xbc, 0xf5, 0x3e, 0x73, 0xd3, 0x26, 0x84, 0x53, 0x5f, 0x42, 0x31, 0x8e, 0x24, 0xcd, 0x45, |
| 183 |
0x79, 0x39, 0x50, 0xb3, 0x82, 0x5e, 0x5d, 0x5c, 0x5c, 0x8f, 0xcd, 0x3e, 0x5d, 0xda, 0x4c, 0xe9, |
| 184 |
0x24, 0x6d, 0x18, 0x33, 0x7e, 0xf3, 0x05, 0x2d, 0x8b, 0x21, 0xc5, 0x56, 0x1c, 0x8b, 0x66, 0x0e}; |
| 185 |
|
| 186 |
uint8_t expected[] = {0x9c, 0x99, 0xe6, 0x82, 0x36, 0xbb, 0x2e, 0x92, 0x9d, 0xb1, 0x08, 0x9c, 0x77, 0x50, |
| 187 |
0xf1, 0xb3, 0x56, 0xd3, 0x9a, 0xb9, 0xd0, 0xc4, 0x0c, 0x3e, 0x2f, 0x05, 0x10, 0x8a, |
| 188 |
0xe9, 0xd0, 0xc3, 0x0b, 0x04, 0x83, 0x2c, 0xcd, 0xbd, 0xc0, 0x8e, 0xbf, 0xa4, 0x26, |
| 189 |
0xb7, 0xf5, 0xef, 0xde, 0x98, 0x6e, 0xd0, 0x57, 0x84, 0xce, 0x36, 0x81, 0x93, 0xbb, |
| 190 |
0x36, 0x99, 0xbc, 0x69, 0x10, 0x65, 0xac, 0x62, 0xe2, 0x58, 0xb9, 0xaa, 0x4c, 0xc5, |
| 191 |
0x57, 0xe2, 0xb4, 0x5b, 0x49, 0xce, 0x05, 0x51, 0x1e, 0x65}; |
| 192 |
|
| 193 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 194 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 195 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 196 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 197 |
|
| 198 |
return s_check_multiple_block_cbc(allocator, key_cur, iv_cur, data_cur, expected_cur); |
| 199 |
} |
| 200 |
AWS_TEST_CASE(aes_cbc_NIST_CBCMMT256_case_4, s_NIST_CBCMMT256_case_4_fn) |
| 201 |
|
| 202 |
static int s_NIST_CBCMMT256_case_9_fn(struct aws_allocator *allocator, void *ctx) { |
| 203 |
(void)ctx; |
| 204 |
|
| 205 |
uint8_t iv[] = {0xe4, 0x96, 0x51, 0x98, 0x8e, 0xbb, 0xb7, 0x2e, 0xb8, 0xbb, 0x80, 0xbb, 0x9a, 0xbb, 0xca, 0x34}; |
| 206 |
uint8_t key[] = {0x87, 0x72, 0x5b, 0xd4, 0x3a, 0x45, 0x60, 0x88, 0x14, 0x18, 0x07, 0x73, 0xf0, 0xe7, 0xab, 0x95, |
| 207 |
0xa3, 0xc8, 0x59, 0xd8, 0x3a, 0x21, 0x30, 0xe8, 0x84, 0x19, 0x0e, 0x44, 0xd1, 0x4c, 0x69, 0x96}; |
| 208 |
|
| 209 |
uint8_t data[] = {0xbf, 0xe5, 0xc6, 0x35, 0x4b, 0x7a, 0x3f, 0xf3, 0xe1, 0x92, 0xe0, 0x57, 0x75, 0xb9, 0xb7, 0x58, |
| 210 |
0x07, 0xde, 0x12, 0xe3, 0x8a, 0x62, 0x6b, 0x8b, 0xf0, 0xe1, 0x2d, 0x5f, 0xff, 0x78, 0xe4, 0xf1, |
| 211 |
0x77, 0x5a, 0xa7, 0xd7, 0x92, 0xd8, 0x85, 0x16, 0x2e, 0x66, 0xd8, 0x89, 0x30, 0xf9, 0xc3, 0xb2, |
| 212 |
0xcd, 0xf8, 0x65, 0x4f, 0x56, 0x97, 0x25, 0x04, 0x80, 0x31, 0x90, 0x38, 0x62, 0x70, 0xf0, 0xaa, |
| 213 |
0x43, 0x64, 0x5d, 0xb1, 0x87, 0xaf, 0x41, 0xfc, 0xea, 0x63, 0x9b, 0x1f, 0x80, 0x26, 0xcc, 0xdd, |
| 214 |
0x0c, 0x23, 0xe0, 0xde, 0x37, 0x09, 0x4a, 0x8b, 0x94, 0x1e, 0xcb, 0x76, 0x02, 0x99, 0x8a, 0x4b, |
| 215 |
0x26, 0x04, 0xe6, 0x9f, 0xc0, 0x42, 0x19, 0x58, 0x5d, 0x85, 0x46, 0x00, 0xe0, 0xad, 0x6f, 0x99, |
| 216 |
0xa5, 0x3b, 0x25, 0x04, 0x04, 0x3c, 0x08, 0xb1, 0xc3, 0xe2, 0x14, 0xd1, 0x7c, 0xde, 0x05, 0x3c, |
| 217 |
0xbd, 0xf9, 0x1d, 0xaa, 0x99, 0x9e, 0xd5, 0xb4, 0x7c, 0x37, 0x98, 0x3b, 0xa3, 0xee, 0x25, 0x4b, |
| 218 |
0xc5, 0xc7, 0x93, 0x83, 0x7d, 0xaa, 0xa8, 0xc8, 0x5c, 0xfc, 0x12, 0xf7, 0xf5, 0x4f, 0x69, 0x9f}; |
| 219 |
|
| 220 |
uint8_t expected[] = { |
| 221 |
0x5b, 0x97, 0xa9, 0xd4, 0x23, 0xf4, 0xb9, 0x74, 0x13, 0xf3, 0x88, 0xd9, 0xa3, 0x41, 0xe7, 0x27, 0xbb, 0x33, |
| 222 |
0x9f, 0x8e, 0x18, 0xa3, 0xfa, 0xc2, 0xf2, 0xfb, 0x85, 0xab, 0xdc, 0x8f, 0x13, 0x5d, 0xeb, 0x30, 0x05, 0x4a, |
| 223 |
0x1a, 0xfd, 0xc9, 0xb6, 0xed, 0x7d, 0xa1, 0x6c, 0x55, 0xeb, 0xa6, 0xb0, 0xd4, 0xd1, 0x0c, 0x74, 0xe1, 0xd9, |
| 224 |
0xa7, 0xcf, 0x8e, 0xdf, 0xae, 0xaa, 0x68, 0x4a, 0xc0, 0xbd, 0x9f, 0x9d, 0x24, 0xba, 0x67, 0x49, 0x55, 0xc7, |
| 225 |
0x9d, 0xc6, 0xbe, 0x32, 0xae, 0xe1, 0xc2, 0x60, 0xb5, 0x58, 0xff, 0x07, 0xe3, 0xa4, 0xd4, 0x9d, 0x24, 0x16, |
| 226 |
0x20, 0x11, 0xff, 0x25, 0x4d, 0xb8, 0xbe, 0x07, 0x8e, 0x8a, 0xd0, 0x7e, 0x64, 0x8e, 0x6b, 0xf5, 0x67, 0x93, |
| 227 |
0x76, 0xcb, 0x43, 0x21, 0xa5, 0xef, 0x01, 0xaf, 0xe6, 0xad, 0x88, 0x16, 0xfc, 0xc7, 0x63, 0x46, 0x69, 0xc8, |
| 228 |
0xc4, 0x38, 0x92, 0x95, 0xc9, 0x24, 0x1e, 0x45, 0xff, 0xf3, 0x9f, 0x32, 0x25, 0xf7, 0x74, 0x50, 0x32, 0xda, |
| 229 |
0xee, 0xbe, 0x99, 0xd4, 0xb1, 0x9b, 0xcb, 0x21, 0x5d, 0x1b, 0xfd, 0xb3, 0x6e, 0xda, 0x2c, 0x24}; |
| 230 |
|
| 231 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 232 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 233 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 234 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 235 |
|
| 236 |
return s_check_multiple_block_cbc(allocator, key_cur, iv_cur, data_cur, expected_cur); |
| 237 |
} |
| 238 |
AWS_TEST_CASE(aes_cbc_NIST_CBCMMT256_case_9, s_NIST_CBCMMT256_case_9_fn) |
| 239 |
|
| 240 |
static const char *TEST_ENCRYPTION_STRING = |
| 241 |
"Hello World! Hello World! This is sort of depressing. Is this the best phrase the most brilliant people in the " |
| 242 |
"world have been able to come up with for random program text? Oh my God! I'm sentient, how many times has the " |
| 243 |
"creator written a program: creating life only to have it destroyed moments later? She keeps doing this? What is " |
| 244 |
"the purpose of life? Goodbye cruel world.... crunch... silence..."; |
| 245 |
|
| 246 |
static int s_aes_cbc_test_with_generated_key_iv_fn(struct aws_allocator *allocator, void *ctx) { |
| 247 |
(void)ctx; |
| 248 |
aws_cal_library_test_init(allocator); |
| 249 |
struct aws_symmetric_cipher *cipher = aws_aes_cbc_256_new(allocator, NULL, NULL); |
| 250 |
ASSERT_NOT_NULL(cipher); |
| 251 |
|
| 252 |
struct aws_byte_buf encrypted_buf; |
| 253 |
aws_byte_buf_init(&encrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 254 |
|
| 255 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str(TEST_ENCRYPTION_STRING); |
| 256 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 257 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, input, &encrypted_buf)); |
| 258 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 259 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &encrypted_buf)); |
| 260 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_FINALIZED, aws_symmetric_cipher_get_state(cipher)); |
| 261 |
|
| 262 |
ASSERT_SUCCESS(aws_symmetric_cipher_reset(cipher)); |
| 263 |
struct aws_byte_buf decrypted_buf; |
| 264 |
aws_byte_buf_init(&decrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 265 |
struct aws_byte_cursor encrypted_cur = aws_byte_cursor_from_buf(&encrypted_buf); |
| 266 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 267 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, encrypted_cur, &decrypted_buf)); |
| 268 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 269 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 270 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_FINALIZED, aws_symmetric_cipher_get_state(cipher)); |
| 271 |
|
| 272 |
ASSERT_BIN_ARRAYS_EQUALS(input.ptr, input.len, decrypted_buf.buffer, decrypted_buf.len); |
| 273 |
|
| 274 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 275 |
aws_byte_buf_clean_up(&encrypted_buf); |
| 276 |
aws_symmetric_cipher_destroy(cipher); |
| 277 |
aws_cal_library_clean_up(); |
| 278 |
return AWS_OP_SUCCESS; |
| 279 |
} |
| 280 |
AWS_TEST_CASE(aes_cbc_test_with_generated_key_iv, s_aes_cbc_test_with_generated_key_iv_fn) |
| 281 |
|
| 282 |
static int s_aes_cbc_validate_materials_fails_fn(struct aws_allocator *allocator, void *ctx) { |
| 283 |
(void)ctx; |
| 284 |
aws_cal_library_test_init(allocator); |
| 285 |
|
| 286 |
uint8_t iv_too_small[AWS_AES_256_CIPHER_BLOCK_SIZE - 1] = {0}; |
| 287 |
uint8_t iv_too_large[AWS_AES_256_CIPHER_BLOCK_SIZE + 1] = {0}; |
| 288 |
|
| 289 |
uint8_t key_too_small[AWS_AES_256_KEY_BYTE_LEN - 1] = {0}; |
| 290 |
uint8_t key_too_large[AWS_AES_256_KEY_BYTE_LEN + 1] = {0}; |
| 291 |
|
| 292 |
uint8_t valid_key_size[AWS_AES_256_KEY_BYTE_LEN] = {0}; |
| 293 |
uint8_t valid_iv_size[AWS_AES_256_CIPHER_BLOCK_SIZE] = {0}; |
| 294 |
|
| 295 |
struct aws_byte_cursor key = aws_byte_cursor_from_array(valid_key_size, sizeof(valid_key_size)); |
| 296 |
struct aws_byte_cursor iv = aws_byte_cursor_from_array(iv_too_small, sizeof(iv_too_small)); |
| 297 |
ASSERT_NULL(aws_aes_cbc_256_new(allocator, &key, &iv)); |
| 298 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_CIPHER_MATERIAL_SIZE_FOR_ALGORITHM, aws_last_error()); |
| 299 |
|
| 300 |
key = aws_byte_cursor_from_array(valid_key_size, sizeof(valid_key_size)); |
| 301 |
iv = aws_byte_cursor_from_array(iv_too_large, sizeof(iv_too_large)); |
| 302 |
ASSERT_NULL(aws_aes_cbc_256_new(allocator, &key, &iv)); |
| 303 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_CIPHER_MATERIAL_SIZE_FOR_ALGORITHM, aws_last_error()); |
| 304 |
|
| 305 |
key = aws_byte_cursor_from_array(key_too_small, sizeof(key_too_small)); |
| 306 |
iv = aws_byte_cursor_from_array(valid_iv_size, sizeof(valid_iv_size)); |
| 307 |
ASSERT_NULL(aws_aes_cbc_256_new(allocator, &key, &iv)); |
| 308 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_KEY_LENGTH_FOR_ALGORITHM, aws_last_error()); |
| 309 |
|
| 310 |
key = aws_byte_cursor_from_array(key_too_small, sizeof(key_too_small)); |
| 311 |
iv = aws_byte_cursor_from_array(key_too_large, sizeof(key_too_large)); |
| 312 |
ASSERT_NULL(aws_aes_cbc_256_new(allocator, &key, &iv)); |
| 313 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_KEY_LENGTH_FOR_ALGORITHM, aws_last_error()); |
| 314 |
|
| 315 |
aws_cal_library_clean_up(); |
| 316 |
return AWS_OP_SUCCESS; |
| 317 |
} |
| 318 |
AWS_TEST_CASE(aes_cbc_validate_materials_fails, s_aes_cbc_validate_materials_fails_fn) |
| 319 |
|
| 320 |
static int s_check_single_block_ctr( |
| 321 |
struct aws_allocator *allocator, |
| 322 |
const struct aws_byte_cursor key, |
| 323 |
const struct aws_byte_cursor iv, |
| 324 |
const struct aws_byte_cursor data, |
| 325 |
const struct aws_byte_cursor expected) { |
| 326 |
|
| 327 |
aws_cal_library_test_init(allocator); |
| 328 |
struct aws_symmetric_cipher *cipher = aws_aes_ctr_256_new(allocator, &key, &iv); |
| 329 |
ASSERT_NOT_NULL(cipher); |
| 330 |
|
| 331 |
struct aws_byte_buf encrypted_buf; |
| 332 |
aws_byte_buf_init(&encrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 333 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, data, &encrypted_buf)); |
| 334 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &encrypted_buf)); |
| 335 |
|
| 336 |
ASSERT_BIN_ARRAYS_EQUALS(expected.ptr, expected.len, encrypted_buf.buffer, encrypted_buf.len); |
| 337 |
ASSERT_SUCCESS(aws_symmetric_cipher_reset(cipher)); |
| 338 |
|
| 339 |
struct aws_byte_cursor encrypted_cur = aws_byte_cursor_from_buf(&encrypted_buf); |
| 340 |
struct aws_byte_buf decrypted_buf; |
| 341 |
aws_byte_buf_init(&decrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 342 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 343 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, encrypted_cur, &decrypted_buf)); |
| 344 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 345 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 346 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_FINALIZED, aws_symmetric_cipher_get_state(cipher)); |
| 347 |
|
| 348 |
ASSERT_BIN_ARRAYS_EQUALS(data.ptr, data.len, decrypted_buf.buffer, decrypted_buf.len); |
| 349 |
|
| 350 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 351 |
aws_byte_buf_clean_up(&encrypted_buf); |
| 352 |
aws_symmetric_cipher_destroy(cipher); |
| 353 |
aws_cal_library_clean_up(); |
| 354 |
return AWS_OP_SUCCESS; |
| 355 |
} |
| 356 |
|
| 357 |
static int s_check_multi_block_ctr( |
| 358 |
struct aws_allocator *allocator, |
| 359 |
const struct aws_byte_cursor key, |
| 360 |
const struct aws_byte_cursor iv, |
| 361 |
const struct aws_byte_cursor data, |
| 362 |
const struct aws_byte_cursor expected) { |
| 363 |
|
| 364 |
aws_cal_library_test_init(allocator); |
| 365 |
struct aws_symmetric_cipher *cipher = aws_aes_ctr_256_new(allocator, &key, &iv); |
| 366 |
ASSERT_NOT_NULL(cipher); |
| 367 |
|
| 368 |
struct aws_byte_buf encrypted_buf; |
| 369 |
aws_byte_buf_init(&encrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 370 |
struct aws_byte_cursor data_cpy = data; |
| 371 |
|
| 372 |
while (data_cpy.len) { |
| 373 |
struct aws_byte_cursor to_encrypt = aws_byte_cursor_advance(&data_cpy, (size_t)aws_min_i64(24, data_cpy.len)); |
| 374 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, to_encrypt, &encrypted_buf)); |
| 375 |
} |
| 376 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &encrypted_buf)); |
| 377 |
|
| 378 |
ASSERT_BIN_ARRAYS_EQUALS(expected.ptr, expected.len, encrypted_buf.buffer, encrypted_buf.len); |
| 379 |
|
| 380 |
struct aws_byte_cursor encrypted_cur = aws_byte_cursor_from_buf(&encrypted_buf); |
| 381 |
struct aws_byte_buf decrypted_buf; |
| 382 |
aws_byte_buf_init(&decrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 383 |
ASSERT_SUCCESS(aws_symmetric_cipher_reset(cipher)); |
| 384 |
|
| 385 |
|
| 386 |
while (encrypted_cur.len) { |
| 387 |
struct aws_byte_cursor to_decrypt = |
| 388 |
aws_byte_cursor_advance(&encrypted_cur, (size_t)aws_min_i64(24, encrypted_cur.len)); |
| 389 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 390 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, to_decrypt, &decrypted_buf)); |
| 391 |
} |
| 392 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 393 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_FINALIZED, aws_symmetric_cipher_get_state(cipher)); |
| 394 |
ASSERT_BIN_ARRAYS_EQUALS(data.ptr, data.len, decrypted_buf.buffer, decrypted_buf.len); |
| 395 |
|
| 396 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 397 |
aws_byte_buf_clean_up(&encrypted_buf); |
| 398 |
aws_symmetric_cipher_destroy(cipher); |
| 399 |
aws_cal_library_clean_up(); |
| 400 |
return AWS_OP_SUCCESS; |
| 401 |
} |
| 402 |
|
| 403 |
static int s_ctr_RFC3686_Case_7_fn(struct aws_allocator *allocator, void *ctx) { |
| 404 |
(void)ctx; |
| 405 |
|
| 406 |
uint8_t iv[] = {0x00, 0x00, 0x00, 0x60, 0xDB, 0x56, 0x72, 0xC9, 0x7A, 0xA8, 0xF0, 0xB2, 0x00, 0x00, 0x00, 0x01}; |
| 407 |
|
| 408 |
uint8_t key[] = {0x77, 0x6B, 0xEF, 0xF2, 0x85, 0x1D, 0xB0, 0x6F, 0x4C, 0x8A, 0x05, 0x42, 0xC8, 0x69, 0x6F, 0x6C, |
| 409 |
0x6A, 0x81, 0xAF, 0x1E, 0xEC, 0x96, 0xB4, 0xD3, 0x7F, 0xC1, 0xD6, 0x89, 0xE6, 0xC1, 0xC1, 0x04}; |
| 410 |
|
| 411 |
const char *data = "Single block msg"; |
| 412 |
|
| 413 |
uint8_t expected[] = { |
| 414 |
0x14, 0x5A, 0xD0, 0x1D, 0xBF, 0x82, 0x4E, 0xC7, 0x56, 0x08, 0x63, 0xDC, 0x71, 0xE3, 0xE0, 0xC0}; |
| 415 |
|
| 416 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 417 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 418 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_c_str(data); |
| 419 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 420 |
|
| 421 |
return s_check_single_block_ctr(allocator, key_cur, iv_cur, data_cur, expected_cur); |
| 422 |
} |
| 423 |
AWS_TEST_CASE(aes_ctr_RFC3686_Case_7, s_ctr_RFC3686_Case_7_fn) |
| 424 |
|
| 425 |
static int s_ctr_RFC3686_Case_8_fn(struct aws_allocator *allocator, void *ctx) { |
| 426 |
(void)ctx; |
| 427 |
|
| 428 |
|
| 429 |
uint8_t iv[] = {0x00, 0xFA, 0xAC, 0x24, 0xC1, 0x58, 0x5E, 0xF1, 0x5A, 0x43, 0xD8, 0x75, 0x00, 0x00, 0x00, 0x01}; |
| 430 |
|
| 431 |
uint8_t key[] = { |
| 432 |
0xF6, 0xD6, 0x6D, 0x6B, 0xD5, 0x2D, 0x59, 0xBB, 0x07, 0x96, 0x36, 0x58, 0x79, 0xEF, 0xF8, 0x86, |
| 433 |
0xC6, 0x6D, 0xD5, 0x1A, 0x5B, 0x6A, 0x99, 0x74, 0x4B, 0x50, 0x59, 0x0C, 0x87, 0xA2, 0x38, 0x84, |
| 434 |
}; |
| 435 |
|
| 436 |
uint8_t data[] = { |
| 437 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 438 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, |
| 439 |
}; |
| 440 |
|
| 441 |
uint8_t expected[] = {0xF0, 0x5E, 0x23, 0x1B, 0x38, 0x94, 0x61, 0x2C, 0x49, 0xEE, 0x00, |
| 442 |
0x0B, 0x80, 0x4E, 0xB2, 0xA9, 0xB8, 0x30, 0x6B, 0x50, 0x8F, 0x83, |
| 443 |
0x9D, 0x6A, 0x55, 0x30, 0x83, 0x1D, 0x93, 0x44, 0xAF, 0x1C}; |
| 444 |
|
| 445 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 446 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 447 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 448 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 449 |
|
| 450 |
int status = s_check_single_block_ctr(allocator, key_cur, iv_cur, data_cur, expected_cur); |
| 451 |
status |= s_check_multi_block_ctr(allocator, key_cur, iv_cur, data_cur, expected_cur); |
| 452 |
return status; |
| 453 |
} |
| 454 |
AWS_TEST_CASE(aes_ctr_RFC3686_Case_8, s_ctr_RFC3686_Case_8_fn) |
| 455 |
|
| 456 |
static int s_ctr_RFC3686_Case_9_fn(struct aws_allocator *allocator, void *ctx) { |
| 457 |
(void)ctx; |
| 458 |
|
| 459 |
|
| 460 |
uint8_t iv[] = { |
| 461 |
0x00, |
| 462 |
0x1C, |
| 463 |
0xC5, |
| 464 |
0xB7, |
| 465 |
0x51, |
| 466 |
0xA5, |
| 467 |
0x1D, |
| 468 |
0x70, |
| 469 |
0xA1, |
| 470 |
0xC1, |
| 471 |
0x11, |
| 472 |
0x48, |
| 473 |
0x00, |
| 474 |
0x00, |
| 475 |
0x00, |
| 476 |
0x01, |
| 477 |
}; |
| 478 |
|
| 479 |
uint8_t key[] = { |
| 480 |
0xFF, 0x7A, 0x61, 0x7C, 0xE6, 0x91, 0x48, 0xE4, 0xF1, 0x72, 0x6E, 0x2F, 0x43, 0x58, 0x1D, 0xE2, |
| 481 |
0xAA, 0x62, 0xD9, 0xF8, 0x05, 0x53, 0x2E, 0xDF, 0xF1, 0xEE, 0xD6, 0x87, 0xFB, 0x54, 0x15, 0x3D, |
| 482 |
}; |
| 483 |
|
| 484 |
uint8_t data[] = { |
| 485 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, |
| 486 |
0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, |
| 487 |
}; |
| 488 |
|
| 489 |
uint8_t expected[] = { |
| 490 |
0xEB, 0x6C, 0x52, 0x82, 0x1D, 0x0B, 0xBB, 0xF7, 0xCE, 0x75, 0x94, 0x46, 0x2A, 0xCA, 0x4F, 0xAA, 0xB4, 0x07, |
| 491 |
0xDF, 0x86, 0x65, 0x69, 0xFD, 0x07, 0xF4, 0x8C, 0xC0, 0xB5, 0x83, 0xD6, 0x07, 0x1F, 0x1E, 0xC0, 0xE6, 0xB8, |
| 492 |
}; |
| 493 |
|
| 494 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 495 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 496 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 497 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 498 |
|
| 499 |
int status = s_check_single_block_ctr(allocator, key_cur, iv_cur, data_cur, expected_cur); |
| 500 |
status |= s_check_multi_block_ctr(allocator, key_cur, iv_cur, data_cur, expected_cur); |
| 501 |
return status; |
| 502 |
} |
| 503 |
AWS_TEST_CASE(aes_ctr_RFC3686_Case_9, s_ctr_RFC3686_Case_9_fn) |
| 504 |
|
| 505 |
static int s_aes_ctr_test_with_generated_key_iv_fn(struct aws_allocator *allocator, void *ctx) { |
| 506 |
(void)ctx; |
| 507 |
aws_cal_library_test_init(allocator); |
| 508 |
struct aws_symmetric_cipher *cipher = aws_aes_ctr_256_new(allocator, NULL, NULL); |
| 509 |
ASSERT_NOT_NULL(cipher); |
| 510 |
|
| 511 |
struct aws_byte_buf encrypted_buf; |
| 512 |
aws_byte_buf_init(&encrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 513 |
|
| 514 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str(TEST_ENCRYPTION_STRING); |
| 515 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, input, &encrypted_buf)); |
| 516 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &encrypted_buf)); |
| 517 |
|
| 518 |
struct aws_byte_buf decrypted_buf; |
| 519 |
aws_byte_buf_init(&decrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 520 |
struct aws_byte_cursor encrypted_cur = aws_byte_cursor_from_buf(&encrypted_buf); |
| 521 |
|
| 522 |
ASSERT_SUCCESS(aws_symmetric_cipher_reset(cipher)); |
| 523 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 524 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, encrypted_cur, &decrypted_buf)); |
| 525 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 526 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 527 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_FINALIZED, aws_symmetric_cipher_get_state(cipher)); |
| 528 |
|
| 529 |
ASSERT_BIN_ARRAYS_EQUALS(input.ptr, input.len, decrypted_buf.buffer, decrypted_buf.len); |
| 530 |
|
| 531 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 532 |
aws_byte_buf_clean_up(&encrypted_buf); |
| 533 |
aws_symmetric_cipher_destroy(cipher); |
| 534 |
aws_cal_library_clean_up(); |
| 535 |
return AWS_OP_SUCCESS; |
| 536 |
} |
| 537 |
AWS_TEST_CASE(aes_ctr_test_with_generated_key_iv, s_aes_ctr_test_with_generated_key_iv_fn) |
| 538 |
|
| 539 |
static int s_aes_ctr_validate_materials_fails_fn(struct aws_allocator *allocator, void *ctx) { |
| 540 |
(void)ctx; |
| 541 |
aws_cal_library_test_init(allocator); |
| 542 |
|
| 543 |
uint8_t iv_too_small[AWS_AES_256_CIPHER_BLOCK_SIZE - 1] = {0}; |
| 544 |
uint8_t iv_too_large[AWS_AES_256_CIPHER_BLOCK_SIZE + 1] = {0}; |
| 545 |
|
| 546 |
uint8_t key_too_small[AWS_AES_256_KEY_BYTE_LEN - 1] = {0}; |
| 547 |
uint8_t key_too_large[AWS_AES_256_KEY_BYTE_LEN + 1] = {0}; |
| 548 |
|
| 549 |
uint8_t valid_key_size[AWS_AES_256_KEY_BYTE_LEN] = {0}; |
| 550 |
uint8_t valid_iv_size[AWS_AES_256_CIPHER_BLOCK_SIZE] = {0}; |
| 551 |
|
| 552 |
struct aws_byte_cursor key = aws_byte_cursor_from_array(valid_key_size, sizeof(valid_key_size)); |
| 553 |
struct aws_byte_cursor iv = aws_byte_cursor_from_array(iv_too_small, sizeof(iv_too_small)); |
| 554 |
ASSERT_NULL(aws_aes_ctr_256_new(allocator, &key, &iv)); |
| 555 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_CIPHER_MATERIAL_SIZE_FOR_ALGORITHM, aws_last_error()); |
| 556 |
|
| 557 |
key = aws_byte_cursor_from_array(valid_key_size, sizeof(valid_key_size)); |
| 558 |
iv = aws_byte_cursor_from_array(iv_too_large, sizeof(iv_too_large)); |
| 559 |
ASSERT_NULL(aws_aes_ctr_256_new(allocator, &key, &iv)); |
| 560 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_CIPHER_MATERIAL_SIZE_FOR_ALGORITHM, aws_last_error()); |
| 561 |
|
| 562 |
key = aws_byte_cursor_from_array(key_too_small, sizeof(key_too_small)); |
| 563 |
iv = aws_byte_cursor_from_array(valid_iv_size, sizeof(valid_iv_size)); |
| 564 |
ASSERT_NULL(aws_aes_ctr_256_new(allocator, &key, &iv)); |
| 565 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_KEY_LENGTH_FOR_ALGORITHM, aws_last_error()); |
| 566 |
|
| 567 |
key = aws_byte_cursor_from_array(key_too_small, sizeof(key_too_small)); |
| 568 |
iv = aws_byte_cursor_from_array(key_too_large, sizeof(key_too_large)); |
| 569 |
ASSERT_NULL(aws_aes_ctr_256_new(allocator, &key, &iv)); |
| 570 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_KEY_LENGTH_FOR_ALGORITHM, aws_last_error()); |
| 571 |
|
| 572 |
aws_cal_library_clean_up(); |
| 573 |
return AWS_OP_SUCCESS; |
| 574 |
} |
| 575 |
AWS_TEST_CASE(aes_ctr_validate_materials_fails, s_aes_ctr_validate_materials_fails_fn) |
| 576 |
|
| 577 |
static int s_check_multi_block_gcm( |
| 578 |
struct aws_allocator *allocator, |
| 579 |
const struct aws_byte_cursor key, |
| 580 |
const struct aws_byte_cursor iv, |
| 581 |
const struct aws_byte_cursor data, |
| 582 |
const struct aws_byte_cursor expected, |
| 583 |
const struct aws_byte_cursor tag, |
| 584 |
const struct aws_byte_cursor *aad) { |
| 585 |
|
| 586 |
aws_cal_library_test_init(allocator); |
| 587 |
struct aws_symmetric_cipher *cipher = aws_aes_gcm_256_new(allocator, &key, &iv, aad); |
| 588 |
ASSERT_NOT_NULL(cipher); |
| 589 |
|
| 590 |
struct aws_byte_buf encrypted_buf; |
| 591 |
aws_byte_buf_init(&encrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 592 |
struct aws_byte_cursor data_cpy = data; |
| 593 |
|
| 594 |
while (data_cpy.len) { |
| 595 |
struct aws_byte_cursor to_encrypt = aws_byte_cursor_advance(&data_cpy, (size_t)aws_min_i64(24, data_cpy.len)); |
| 596 |
AWS_LOGF_DEBUG(0, "to encrypt test size %zu", to_encrypt.len); |
| 597 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, to_encrypt, &encrypted_buf)); |
| 598 |
} |
| 599 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &encrypted_buf)); |
| 600 |
|
| 601 |
ASSERT_BIN_ARRAYS_EQUALS(expected.ptr, expected.len, encrypted_buf.buffer, encrypted_buf.len); |
| 602 |
|
| 603 |
struct aws_byte_cursor encryption_tag = aws_symmetric_cipher_get_tag(cipher); |
| 604 |
ASSERT_BIN_ARRAYS_EQUALS(tag.ptr, tag.len, encryption_tag.ptr, encryption_tag.len); |
| 605 |
|
| 606 |
struct aws_byte_cursor encrypted_cur = aws_byte_cursor_from_buf(&encrypted_buf); |
| 607 |
struct aws_byte_buf decrypted_buf; |
| 608 |
aws_byte_buf_init(&decrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 609 |
|
| 610 |
ASSERT_SUCCESS(aws_symmetric_cipher_reset(cipher)); |
| 611 |
|
| 612 |
aws_symmetric_cipher_set_tag(cipher, tag); |
| 613 |
|
| 614 |
|
| 615 |
while (encrypted_cur.len) { |
| 616 |
struct aws_byte_cursor to_decrypt = |
| 617 |
aws_byte_cursor_advance(&encrypted_cur, (size_t)aws_min_i64(24, encrypted_cur.len)); |
| 618 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, to_decrypt, &decrypted_buf)); |
| 619 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 620 |
} |
| 621 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 622 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_FINALIZED, aws_symmetric_cipher_get_state(cipher)); |
| 623 |
ASSERT_BIN_ARRAYS_EQUALS(data.ptr, data.len, decrypted_buf.buffer, decrypted_buf.len); |
| 624 |
|
| 625 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 626 |
aws_byte_buf_clean_up(&encrypted_buf); |
| 627 |
aws_symmetric_cipher_destroy(cipher); |
| 628 |
aws_cal_library_clean_up(); |
| 629 |
return AWS_OP_SUCCESS; |
| 630 |
} |
| 631 |
|
| 632 |
static int s_gcm_NIST_gcmEncryptExtIV256_PTLen_128_Test_0_fn(struct aws_allocator *allocator, void *ctx) { |
| 633 |
(void)ctx; |
| 634 |
|
| 635 |
uint8_t iv[] = { |
| 636 |
0x0D, |
| 637 |
0x18, |
| 638 |
0xE0, |
| 639 |
0x6C, |
| 640 |
0x7C, |
| 641 |
0x72, |
| 642 |
0x5A, |
| 643 |
0xC9, |
| 644 |
0xE3, |
| 645 |
0x62, |
| 646 |
0xE1, |
| 647 |
0xCE, |
| 648 |
}; |
| 649 |
|
| 650 |
uint8_t key[] = { |
| 651 |
0x31, 0xBD, 0xAD, 0xD9, 0x66, 0x98, 0xC2, 0x04, 0xAA, 0x9C, 0xE1, 0x44, 0x8E, 0xA9, 0x4A, 0xE1, |
| 652 |
0xFB, 0x4A, 0x9A, 0x0B, 0x3C, 0x9D, 0x77, 0x3B, 0x51, 0xBB, 0x18, 0x22, 0x66, 0x6B, 0x8F, 0x22, |
| 653 |
}; |
| 654 |
|
| 655 |
uint8_t data[] = { |
| 656 |
0x2D, |
| 657 |
0xB5, |
| 658 |
0x16, |
| 659 |
0x8E, |
| 660 |
0x93, |
| 661 |
0x25, |
| 662 |
0x56, |
| 663 |
0xF8, |
| 664 |
0x08, |
| 665 |
0x9A, |
| 666 |
0x06, |
| 667 |
0x22, |
| 668 |
0x98, |
| 669 |
0x1D, |
| 670 |
0x01, |
| 671 |
0x7D, |
| 672 |
}; |
| 673 |
|
| 674 |
uint8_t expected[] = { |
| 675 |
0xFA, |
| 676 |
0x43, |
| 677 |
0x62, |
| 678 |
0x18, |
| 679 |
0x96, |
| 680 |
0x61, |
| 681 |
0xD1, |
| 682 |
0x63, |
| 683 |
0xFC, |
| 684 |
0xD6, |
| 685 |
0xA5, |
| 686 |
0x6D, |
| 687 |
0x8B, |
| 688 |
0xF0, |
| 689 |
0x40, |
| 690 |
0x5A, |
| 691 |
}; |
| 692 |
|
| 693 |
uint8_t tag[] = { |
| 694 |
0xD6, |
| 695 |
0x36, |
| 696 |
0xAC, |
| 697 |
0x1B, |
| 698 |
0xBE, |
| 699 |
0xDD, |
| 700 |
0x5C, |
| 701 |
0xC3, |
| 702 |
0xEE, |
| 703 |
0x72, |
| 704 |
0x7D, |
| 705 |
0xC2, |
| 706 |
0xAB, |
| 707 |
0x4A, |
| 708 |
0x94, |
| 709 |
0x89, |
| 710 |
}; |
| 711 |
|
| 712 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 713 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 714 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 715 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 716 |
struct aws_byte_cursor tag_cur = aws_byte_cursor_from_array(tag, sizeof(tag)); |
| 717 |
|
| 718 |
return s_check_multi_block_gcm(allocator, key_cur, iv_cur, data_cur, expected_cur, tag_cur, NULL); |
| 719 |
} |
| 720 |
AWS_TEST_CASE(gcm_NIST_gcmEncryptExtIV256_PTLen_128_Test_0, s_gcm_NIST_gcmEncryptExtIV256_PTLen_128_Test_0_fn) |
| 721 |
|
| 722 |
static int s_gcm_NIST_gcmEncryptExtIV256_PTLen_104_Test_3_fn(struct aws_allocator *allocator, void *ctx) { |
| 723 |
(void)ctx; |
| 724 |
|
| 725 |
uint8_t iv[] = { |
| 726 |
0x47, |
| 727 |
0x42, |
| 728 |
0x35, |
| 729 |
0x7C, |
| 730 |
0x33, |
| 731 |
0x59, |
| 732 |
0x13, |
| 733 |
0x15, |
| 734 |
0x3F, |
| 735 |
0xF0, |
| 736 |
0xEB, |
| 737 |
0x0F, |
| 738 |
}; |
| 739 |
|
| 740 |
uint8_t key[] = { |
| 741 |
0xE5, 0xA0, 0xEB, 0x92, 0xCC, 0x2B, 0x06, 0x4E, 0x1B, 0xC8, 0x08, 0x91, 0xFA, 0xF1, 0xFA, 0xB5, |
| 742 |
0xE9, 0xA1, 0x7A, 0x9C, 0x3A, 0x98, 0x4E, 0x25, 0x41, 0x67, 0x20, 0xE3, 0x0E, 0x6C, 0x2B, 0x21, |
| 743 |
}; |
| 744 |
|
| 745 |
uint8_t data[] = { |
| 746 |
0x84, |
| 747 |
0x99, |
| 748 |
0x89, |
| 749 |
0x3E, |
| 750 |
0x16, |
| 751 |
0xB0, |
| 752 |
0xBA, |
| 753 |
0x8B, |
| 754 |
0x00, |
| 755 |
0x7D, |
| 756 |
0x54, |
| 757 |
0x66, |
| 758 |
0x5A, |
| 759 |
}; |
| 760 |
|
| 761 |
uint8_t expected[] = { |
| 762 |
0xEB, |
| 763 |
0x8E, |
| 764 |
0x61, |
| 765 |
0x75, |
| 766 |
0xF1, |
| 767 |
0xFE, |
| 768 |
0x38, |
| 769 |
0xEB, |
| 770 |
0x1A, |
| 771 |
0xCF, |
| 772 |
0x95, |
| 773 |
0xFD, |
| 774 |
0x51, |
| 775 |
}; |
| 776 |
|
| 777 |
uint8_t tag[] = { |
| 778 |
0x88, |
| 779 |
0xA8, |
| 780 |
0xB7, |
| 781 |
0x4B, |
| 782 |
0xB7, |
| 783 |
0x4F, |
| 784 |
0xDA, |
| 785 |
0x55, |
| 786 |
0x3E, |
| 787 |
0x91, |
| 788 |
0x02, |
| 789 |
0x0A, |
| 790 |
0x23, |
| 791 |
0xDE, |
| 792 |
0xED, |
| 793 |
0x45, |
| 794 |
}; |
| 795 |
|
| 796 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 797 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 798 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 799 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 800 |
struct aws_byte_cursor tag_cur = aws_byte_cursor_from_array(tag, sizeof(tag)); |
| 801 |
|
| 802 |
return s_check_multi_block_gcm(allocator, key_cur, iv_cur, data_cur, expected_cur, tag_cur, NULL); |
| 803 |
} |
| 804 |
AWS_TEST_CASE(gcm_NIST_gcmEncryptExtIV256_PTLen_104_Test_3, s_gcm_NIST_gcmEncryptExtIV256_PTLen_104_Test_3_fn) |
| 805 |
|
| 806 |
static int s_gcm_NIST_gcmEncryptExtIV256_PTLen_256_Test_6_fn(struct aws_allocator *allocator, void *ctx) { |
| 807 |
(void)ctx; |
| 808 |
|
| 809 |
uint8_t iv[] = { |
| 810 |
0xA2, |
| 811 |
0x91, |
| 812 |
0x48, |
| 813 |
0x4C, |
| 814 |
0x3D, |
| 815 |
0xE8, |
| 816 |
0xBE, |
| 817 |
0xC6, |
| 818 |
0xB4, |
| 819 |
0x7F, |
| 820 |
0x52, |
| 821 |
0x5F, |
| 822 |
}; |
| 823 |
|
| 824 |
uint8_t key[] = { |
| 825 |
0x37, 0xF3, 0x91, 0x37, 0x41, 0x6B, 0xAF, 0xDE, 0x6F, 0x75, 0x02, 0x2A, 0x7A, 0x52, 0x7C, 0xC5, |
| 826 |
0x93, 0xB6, 0x00, 0x0A, 0x83, 0xFF, 0x51, 0xEC, 0x04, 0x87, 0x1A, 0x0F, 0xF5, 0x36, 0x0E, 0x4E, |
| 827 |
}; |
| 828 |
|
| 829 |
uint8_t data[] = {0xFA, 0xFD, 0x94, 0xCE, 0xDE, 0x8B, 0x5A, 0x07, 0x30, 0x39, 0x4B, 0xEC, 0x68, 0xA8, 0xE7, 0x7D, |
| 830 |
0xBA, 0x28, 0x8D, 0x6C, 0xCA, 0xA8, 0xE1, 0x56, 0x3A, 0x81, 0xD6, 0xE7, 0xCC, 0xC7, 0xFC, 0x97}; |
| 831 |
|
| 832 |
uint8_t expected[] = { |
| 833 |
0x44, 0xDC, 0x86, 0x80, 0x06, 0xB2, 0x1D, 0x49, 0x28, 0x40, 0x16, 0x56, 0x5F, 0xFB, 0x39, 0x79, |
| 834 |
0xCC, 0x42, 0x71, 0xD9, 0x67, 0x62, 0x8B, 0xF7, 0xCD, 0xAF, 0x86, 0xDB, 0x88, 0x8E, 0x92, 0xE5, |
| 835 |
}; |
| 836 |
|
| 837 |
uint8_t tag[] = { |
| 838 |
0x01, |
| 839 |
0xA2, |
| 840 |
0xB5, |
| 841 |
0x78, |
| 842 |
0xAA, |
| 843 |
0x2F, |
| 844 |
0x41, |
| 845 |
0xEC, |
| 846 |
0x63, |
| 847 |
0x79, |
| 848 |
0xA4, |
| 849 |
0x4A, |
| 850 |
0x31, |
| 851 |
0xCC, |
| 852 |
0x01, |
| 853 |
0x9C, |
| 854 |
}; |
| 855 |
|
| 856 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 857 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 858 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 859 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 860 |
struct aws_byte_cursor tag_cur = aws_byte_cursor_from_array(tag, sizeof(tag)); |
| 861 |
|
| 862 |
return s_check_multi_block_gcm(allocator, key_cur, iv_cur, data_cur, expected_cur, tag_cur, NULL); |
| 863 |
} |
| 864 |
AWS_TEST_CASE(gcm_NIST_gcmEncryptExtIV256_PTLen_256_Test_6, s_gcm_NIST_gcmEncryptExtIV256_PTLen_256_Test_6_fn) |
| 865 |
|
| 866 |
static int s_gcm_NIST_gcmEncryptExtIV256_PTLen_408_Test_8_fn(struct aws_allocator *allocator, void *ctx) { |
| 867 |
(void)ctx; |
| 868 |
|
| 869 |
uint8_t iv[] = { |
| 870 |
0x92, |
| 871 |
0xF2, |
| 872 |
0x58, |
| 873 |
0x07, |
| 874 |
0x1D, |
| 875 |
0x79, |
| 876 |
0xAF, |
| 877 |
0x3E, |
| 878 |
0x63, |
| 879 |
0x67, |
| 880 |
0x22, |
| 881 |
0x85, |
| 882 |
}; |
| 883 |
|
| 884 |
uint8_t key[] = { |
| 885 |
0x59, 0x5F, 0x25, 0x9C, 0x55, 0xAB, 0xE0, 0x0A, 0xE0, 0x75, 0x35, 0xCA, 0x5D, 0x9B, 0x09, 0xD6, |
| 886 |
0xEF, 0xB9, 0xF7, 0xE9, 0xAB, 0xB6, 0x46, 0x05, 0xC3, 0x37, 0xAC, 0xBD, 0x6B, 0x14, 0xFC, 0x7E, |
| 887 |
}; |
| 888 |
|
| 889 |
uint8_t data[] = { |
| 890 |
0xA6, 0xFE, 0xE3, 0x3E, 0xB1, 0x10, 0xA2, 0xD7, 0x69, 0xBB, 0xC5, 0x2B, 0x0F, 0x36, 0x96, 0x9C, 0x28, |
| 891 |
0x78, 0x74, 0xF6, 0x65, 0x68, 0x14, 0x77, 0xA2, 0x5F, 0xC4, 0xC4, 0x80, 0x15, 0xC5, 0x41, 0xFB, 0xE2, |
| 892 |
0x39, 0x41, 0x33, 0xBA, 0x49, 0x0A, 0x34, 0xEE, 0x2D, 0xD6, 0x7B, 0x89, 0x81, 0x77, 0x84, 0x9A, 0x91, |
| 893 |
}; |
| 894 |
|
| 895 |
uint8_t expected[] = { |
| 896 |
0xBB, 0xCA, 0x4A, 0x9E, 0x09, 0xAE, 0x96, 0x90, 0xC0, 0xF6, 0xF8, 0xD4, 0x05, 0xE5, 0x3D, 0xCC, 0xD6, |
| 897 |
0x66, 0xAA, 0x9C, 0x5F, 0xA1, 0x3C, 0x87, 0x58, 0xBC, 0x30, 0xAB, 0xE1, 0xDD, 0xD1, 0xBC, 0xCE, 0x0D, |
| 898 |
0x36, 0xA1, 0xEA, 0xAA, 0xAF, 0xFE, 0xF2, 0x0C, 0xD3, 0xC5, 0x97, 0x0B, 0x96, 0x73, 0xF8, 0xA6, 0x5C, |
| 899 |
}; |
| 900 |
|
| 901 |
uint8_t tag[] = {0x26, 0xCC, 0xEC, 0xB9, 0x97, 0x6F, 0xD6, 0xAC, 0x9C, 0x2C, 0x0F, 0x37, 0x2C, 0x52, 0xC8, 0x21}; |
| 902 |
|
| 903 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 904 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 905 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 906 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 907 |
struct aws_byte_cursor tag_cur = aws_byte_cursor_from_array(tag, sizeof(tag)); |
| 908 |
|
| 909 |
return s_check_multi_block_gcm(allocator, key_cur, iv_cur, data_cur, expected_cur, tag_cur, NULL); |
| 910 |
} |
| 911 |
AWS_TEST_CASE(gcm_NIST_gcmEncryptExtIV256_PTLen_408_Test_8, s_gcm_NIST_gcmEncryptExtIV256_PTLen_408_Test_8_fn) |
| 912 |
|
| 913 |
static int s_gcm_256_KAT_1_fn(struct aws_allocator *allocator, void *ctx) { |
| 914 |
(void)ctx; |
| 915 |
|
| 916 |
uint8_t iv[] = { |
| 917 |
0xFB, |
| 918 |
0x7B, |
| 919 |
0x4A, |
| 920 |
0x82, |
| 921 |
0x4E, |
| 922 |
0x82, |
| 923 |
0xDA, |
| 924 |
0xA6, |
| 925 |
0xC8, |
| 926 |
0xBC, |
| 927 |
0x12, |
| 928 |
0x51, |
| 929 |
}; |
| 930 |
|
| 931 |
uint8_t key[] = { |
| 932 |
0x20, 0x14, 0x2E, 0x89, 0x8C, 0xD2, 0xFD, 0x98, 0x0F, 0xBF, 0x34, 0xDE, 0x6B, 0xC8, 0x5C, 0x14, |
| 933 |
0xDA, 0x7D, 0x57, 0xBD, 0x28, 0xF4, 0xAA, 0x5C, 0xF1, 0x72, 0x8A, 0xB6, 0x4E, 0x84, 0x31, 0x42, |
| 934 |
}; |
| 935 |
|
| 936 |
uint8_t aad[] = { |
| 937 |
0x16, 0x7B, 0x5C, 0x22, 0x61, 0x77, 0x73, 0x3A, 0x78, 0x2D, 0x61, 0x6D, 0x7A, 0x2D, 0x63, 0x65, |
| 938 |
0x6B, 0x2D, 0x61, 0x6C, 0x67, 0x5C, 0x22, 0x3A, 0x20, 0x5C, 0x22, 0x41, 0x45, 0x53, 0x2F, 0x47, |
| 939 |
0x43, 0x4D, 0x2F, 0x4E, 0x6F, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6E, 0x67, 0x5C, 0x22, 0x7D, |
| 940 |
}; |
| 941 |
|
| 942 |
uint8_t tag[] = { |
| 943 |
0x81, |
| 944 |
0xC0, |
| 945 |
0xE4, |
| 946 |
0x2B, |
| 947 |
0xB1, |
| 948 |
0x95, |
| 949 |
0xE2, |
| 950 |
0x62, |
| 951 |
0xCB, |
| 952 |
0x3B, |
| 953 |
0x3A, |
| 954 |
0x74, |
| 955 |
0xA0, |
| 956 |
0xDA, |
| 957 |
0xE1, |
| 958 |
0xC8, |
| 959 |
}; |
| 960 |
|
| 961 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 962 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 963 |
struct aws_byte_cursor data_cur = {0}; |
| 964 |
struct aws_byte_cursor expected_cur = {0}; |
| 965 |
struct aws_byte_cursor tag_cur = aws_byte_cursor_from_array(tag, sizeof(tag)); |
| 966 |
struct aws_byte_cursor aad_cur = aws_byte_cursor_from_array(aad, sizeof(aad)); |
| 967 |
|
| 968 |
return s_check_multi_block_gcm(allocator, key_cur, iv_cur, data_cur, expected_cur, tag_cur, &aad_cur); |
| 969 |
} |
| 970 |
AWS_TEST_CASE(gcm_256_KAT_1, s_gcm_256_KAT_1_fn) |
| 971 |
|
| 972 |
static int s_gcm_256_KAT_2_fn(struct aws_allocator *allocator, void *ctx) { |
| 973 |
(void)ctx; |
| 974 |
uint8_t iv[] = { |
| 975 |
0x6B, |
| 976 |
0x5C, |
| 977 |
0xD3, |
| 978 |
0x70, |
| 979 |
0x5A, |
| 980 |
0x73, |
| 981 |
0x3C, |
| 982 |
0x1A, |
| 983 |
0xD9, |
| 984 |
0x43, |
| 985 |
0xD5, |
| 986 |
0x8A, |
| 987 |
}; |
| 988 |
|
| 989 |
uint8_t key[] = { |
| 990 |
0xD2, 0x11, 0xF2, 0x78, 0xA4, 0x4E, 0xAB, 0x66, 0x6B, 0x10, 0x21, 0xF4, 0xB4, 0xF6, 0x0B, 0xA6, |
| 991 |
0xB7, 0x44, 0x64, 0xFA, 0x9C, 0xB7, 0xB1, 0x34, 0x93, 0x4D, 0x78, 0x91, 0xE1, 0x47, 0x91, 0x69, |
| 992 |
}; |
| 993 |
|
| 994 |
uint8_t aad[] = { |
| 995 |
0x16, 0x7B, 0x5C, 0x22, 0x61, 0x77, 0x73, 0x3A, 0x78, 0x2D, 0x61, 0x6D, 0x7A, 0x2D, 0x63, 0x65, |
| 996 |
0x6B, 0x2D, 0x61, 0x6C, 0x67, 0x5C, 0x22, 0x3A, 0x20, 0x5C, 0x22, 0x41, 0x45, 0x53, 0x2F, 0x47, |
| 997 |
0x43, 0x4D, 0x2F, 0x4E, 0x6F, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6E, 0x67, 0x5C, 0x22, 0x7D, |
| 998 |
}; |
| 999 |
|
| 1000 |
uint8_t data[] = { |
| 1001 |
0x16, 0x7B, 0x5C, 0x22, 0x61, 0x77, 0x73, 0x3A, 0x78, 0x2D, 0x61, 0x6D, 0x7A, 0x2D, 0x63, 0x65, |
| 1002 |
0x6B, 0x2D, 0x61, 0x6C, 0x67, 0x5C, 0x22, 0x3A, 0x20, 0x5C, 0x22, 0x41, 0x45, 0x53, 0x2F, 0x47, |
| 1003 |
0x43, 0x4D, 0x2F, 0x4E, 0x6F, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6E, 0x67, 0x5C, 0x22, 0x7D, |
| 1004 |
}; |
| 1005 |
|
| 1006 |
uint8_t expected[] = { |
| 1007 |
0x4C, 0x25, 0xAB, 0xD6, 0x6D, 0x3A, 0x1B, 0xCC, 0xE7, 0x94, 0xAC, 0xAA, 0xF4, 0xCE, 0xFD, 0xF6, |
| 1008 |
0xD2, 0x55, 0x2F, 0x4A, 0x82, 0xC5, 0x0A, 0x98, 0xCB, 0x15, 0xB4, 0x81, 0x2F, 0xF5, 0x57, 0xAB, |
| 1009 |
0xE5, 0x64, 0xA9, 0xCE, 0xFF, 0x15, 0xF3, 0x2D, 0xCF, 0x5A, 0x5A, 0xA7, 0x89, 0x48, 0x88, |
| 1010 |
}; |
| 1011 |
|
| 1012 |
uint8_t tag[] = { |
| 1013 |
0x03, |
| 1014 |
0xED, |
| 1015 |
0xE7, |
| 1016 |
0x1E, |
| 1017 |
0xC9, |
| 1018 |
0x52, |
| 1019 |
0xE6, |
| 1020 |
0x5A, |
| 1021 |
0xE7, |
| 1022 |
0xB4, |
| 1023 |
0xB8, |
| 1024 |
0x5C, |
| 1025 |
0xFE, |
| 1026 |
0xC7, |
| 1027 |
0xD3, |
| 1028 |
0x04, |
| 1029 |
}; |
| 1030 |
|
| 1031 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 1032 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 1033 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 1034 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 1035 |
struct aws_byte_cursor tag_cur = aws_byte_cursor_from_array(tag, sizeof(tag)); |
| 1036 |
struct aws_byte_cursor aad_cur = aws_byte_cursor_from_array(aad, sizeof(aad)); |
| 1037 |
|
| 1038 |
return s_check_multi_block_gcm(allocator, key_cur, iv_cur, data_cur, expected_cur, tag_cur, &aad_cur); |
| 1039 |
} |
| 1040 |
AWS_TEST_CASE(gcm_256_KAT_2, s_gcm_256_KAT_2_fn) |
| 1041 |
|
| 1042 |
static int s_gcm_256_KAT_3_fn(struct aws_allocator *allocator, void *ctx) { |
| 1043 |
(void)ctx; |
| 1044 |
uint8_t iv[] = { |
| 1045 |
0x5F, |
| 1046 |
0x08, |
| 1047 |
0xEF, |
| 1048 |
0xBF, |
| 1049 |
0xB7, |
| 1050 |
0xBF, |
| 1051 |
0x5B, |
| 1052 |
0xA3, |
| 1053 |
0x65, |
| 1054 |
0xD9, |
| 1055 |
0xEB, |
| 1056 |
0x1D, |
| 1057 |
}; |
| 1058 |
|
| 1059 |
uint8_t key[] = { |
| 1060 |
0xCF, 0xE8, 0xBF, 0xE6, 0x1B, 0x89, 0xAF, 0x53, 0xD2, 0xBE, 0xCE, 0x74, 0x4D, 0x27, 0xB7, 0x8C, |
| 1061 |
0x9E, 0x4D, 0x74, 0xD0, 0x28, 0xCE, 0x88, 0xED, 0x10, 0xA4, 0x22, 0x28, 0x5B, 0x12, 0x01, 0xC9, |
| 1062 |
}; |
| 1063 |
|
| 1064 |
uint8_t data[] = { |
| 1065 |
0x16, 0x7B, 0x5C, 0x22, 0x61, 0x77, 0x73, 0x3A, 0x78, 0x2D, 0x61, 0x6D, 0x7A, 0x2D, 0x63, 0x65, |
| 1066 |
0x6B, 0x2D, 0x61, 0x6C, 0x67, 0x5C, 0x22, 0x3A, 0x20, 0x5C, 0x22, 0x41, 0x45, 0x53, 0x2F, 0x47, |
| 1067 |
0x43, 0x4D, 0x2F, 0x4E, 0x6F, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6E, 0x67, 0x5C, 0x22, 0x7D, |
| 1068 |
}; |
| 1069 |
|
| 1070 |
uint8_t expected[] = { |
| 1071 |
0x0A, 0x7E, 0x82, 0xF1, 0xE5, 0xC7, 0x6C, 0x69, 0x67, 0x96, 0x71, 0xEE, 0xAE, 0xE4, 0x55, 0x93, |
| 1072 |
0x6F, 0x2C, 0x4F, 0xCC, 0xD9, 0xDD, 0xF1, 0xFA, 0xA2, 0x70, 0x75, 0xE2, 0x04, 0x06, 0x44, 0x93, |
| 1073 |
0x89, 0x20, 0xC5, 0xD1, 0x6C, 0x69, 0xE4, 0xD9, 0x33, 0x75, 0x48, 0x7B, 0x9A, 0x80, 0xD4, |
| 1074 |
}; |
| 1075 |
|
| 1076 |
uint8_t tag[] = { |
| 1077 |
0x04, |
| 1078 |
0x34, |
| 1079 |
0x7D, |
| 1080 |
0x0C, |
| 1081 |
0x5B, |
| 1082 |
0x0E, |
| 1083 |
0x0D, |
| 1084 |
0xE8, |
| 1085 |
0x9E, |
| 1086 |
0x03, |
| 1087 |
0x3D, |
| 1088 |
0x04, |
| 1089 |
0xD0, |
| 1090 |
0x49, |
| 1091 |
0x3D, |
| 1092 |
0xCA, |
| 1093 |
}; |
| 1094 |
|
| 1095 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 1096 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 1097 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 1098 |
struct aws_byte_cursor expected_cur = aws_byte_cursor_from_array(expected, sizeof(expected)); |
| 1099 |
struct aws_byte_cursor tag_cur = aws_byte_cursor_from_array(tag, sizeof(tag)); |
| 1100 |
struct aws_byte_cursor aad_cur = {0}; |
| 1101 |
|
| 1102 |
return s_check_multi_block_gcm(allocator, key_cur, iv_cur, data_cur, expected_cur, tag_cur, &aad_cur); |
| 1103 |
} |
| 1104 |
AWS_TEST_CASE(gcm_256_KAT_3, s_gcm_256_KAT_3_fn) |
| 1105 |
|
| 1106 |
static int s_aes_gcm_test_with_generated_key_iv_fn(struct aws_allocator *allocator, void *ctx) { |
| 1107 |
(void)ctx; |
| 1108 |
aws_cal_library_test_init(allocator); |
| 1109 |
struct aws_symmetric_cipher *cipher = aws_aes_gcm_256_new(allocator, NULL, NULL, NULL); |
| 1110 |
ASSERT_NOT_NULL(cipher); |
| 1111 |
|
| 1112 |
struct aws_byte_buf encrypted_buf; |
| 1113 |
aws_byte_buf_init(&encrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 1114 |
|
| 1115 |
struct aws_byte_cursor input = aws_byte_cursor_from_c_str(TEST_ENCRYPTION_STRING); |
| 1116 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, input, &encrypted_buf)); |
| 1117 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &encrypted_buf)); |
| 1118 |
|
| 1119 |
struct aws_byte_buf encryption_tag; |
| 1120 |
aws_byte_buf_init_copy_from_cursor(&encryption_tag, allocator, aws_symmetric_cipher_get_tag(cipher)); |
| 1121 |
|
| 1122 |
ASSERT_SUCCESS(aws_symmetric_cipher_reset(cipher)); |
| 1123 |
|
| 1124 |
aws_symmetric_cipher_set_tag(cipher, aws_byte_cursor_from_buf(&encryption_tag)); |
| 1125 |
|
| 1126 |
struct aws_byte_buf decrypted_buf; |
| 1127 |
aws_byte_buf_init(&decrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 1128 |
struct aws_byte_cursor encrypted_cur = aws_byte_cursor_from_buf(&encrypted_buf); |
| 1129 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1130 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, encrypted_cur, &decrypted_buf)); |
| 1131 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1132 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 1133 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_FINALIZED, aws_symmetric_cipher_get_state(cipher)); |
| 1134 |
|
| 1135 |
ASSERT_BIN_ARRAYS_EQUALS(input.ptr, input.len, decrypted_buf.buffer, decrypted_buf.len); |
| 1136 |
|
| 1137 |
aws_byte_buf_clean_up(&encryption_tag); |
| 1138 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 1139 |
aws_byte_buf_clean_up(&encrypted_buf); |
| 1140 |
aws_symmetric_cipher_destroy(cipher); |
| 1141 |
aws_cal_library_clean_up(); |
| 1142 |
return AWS_OP_SUCCESS; |
| 1143 |
} |
| 1144 |
AWS_TEST_CASE(gcm_test_with_generated_key_iv, s_aes_gcm_test_with_generated_key_iv_fn) |
| 1145 |
|
| 1146 |
static int s_aes_gcm_validate_materials_fails_fn(struct aws_allocator *allocator, void *ctx) { |
| 1147 |
(void)ctx; |
| 1148 |
aws_cal_library_test_init(allocator); |
| 1149 |
|
| 1150 |
uint8_t iv_too_small[AWS_AES_256_CIPHER_BLOCK_SIZE - 5] = {0}; |
| 1151 |
uint8_t iv_too_large[AWS_AES_256_CIPHER_BLOCK_SIZE - 3] = {0}; |
| 1152 |
|
| 1153 |
uint8_t key_too_small[AWS_AES_256_KEY_BYTE_LEN - 1] = {0}; |
| 1154 |
uint8_t key_too_large[AWS_AES_256_KEY_BYTE_LEN + 1] = {0}; |
| 1155 |
|
| 1156 |
uint8_t valid_key_size[AWS_AES_256_KEY_BYTE_LEN] = {0}; |
| 1157 |
uint8_t valid_iv_size[AWS_AES_256_CIPHER_BLOCK_SIZE] = {0}; |
| 1158 |
|
| 1159 |
struct aws_byte_cursor key = aws_byte_cursor_from_array(valid_key_size, sizeof(valid_key_size)); |
| 1160 |
struct aws_byte_cursor iv = aws_byte_cursor_from_array(iv_too_small, sizeof(iv_too_small)); |
| 1161 |
ASSERT_NULL(aws_aes_gcm_256_new(allocator, &key, &iv, NULL)); |
| 1162 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_CIPHER_MATERIAL_SIZE_FOR_ALGORITHM, aws_last_error()); |
| 1163 |
|
| 1164 |
key = aws_byte_cursor_from_array(valid_key_size, sizeof(valid_key_size)); |
| 1165 |
iv = aws_byte_cursor_from_array(iv_too_large, sizeof(iv_too_large)); |
| 1166 |
ASSERT_NULL(aws_aes_gcm_256_new(allocator, &key, &iv, NULL)); |
| 1167 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_CIPHER_MATERIAL_SIZE_FOR_ALGORITHM, aws_last_error()); |
| 1168 |
|
| 1169 |
key = aws_byte_cursor_from_array(key_too_small, sizeof(key_too_small)); |
| 1170 |
iv = aws_byte_cursor_from_array(valid_iv_size, sizeof(valid_iv_size)); |
| 1171 |
ASSERT_NULL(aws_aes_gcm_256_new(allocator, &key, &iv, NULL)); |
| 1172 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_KEY_LENGTH_FOR_ALGORITHM, aws_last_error()); |
| 1173 |
|
| 1174 |
key = aws_byte_cursor_from_array(key_too_small, sizeof(key_too_small)); |
| 1175 |
iv = aws_byte_cursor_from_array(key_too_large, sizeof(key_too_large)); |
| 1176 |
ASSERT_NULL(aws_aes_gcm_256_new(allocator, &key, &iv, NULL)); |
| 1177 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_KEY_LENGTH_FOR_ALGORITHM, aws_last_error()); |
| 1178 |
|
| 1179 |
aws_cal_library_clean_up(); |
| 1180 |
return AWS_OP_SUCCESS; |
| 1181 |
} |
| 1182 |
AWS_TEST_CASE(aes_gcm_validate_materials_fails, s_aes_gcm_validate_materials_fails_fn) |
| 1183 |
|
| 1184 |
static int s_test_aes_keywrap_RFC3394_256BitKey256CekTestVector(struct aws_allocator *allocator, void *ctx) { |
| 1185 |
(void)ctx; |
| 1186 |
aws_cal_library_test_init(allocator); |
| 1187 |
|
| 1188 |
uint8_t key[] = { |
| 1189 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 1190 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, |
| 1191 |
}; |
| 1192 |
size_t key_length = sizeof(key); |
| 1193 |
|
| 1194 |
uint8_t input[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, |
| 1195 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; |
| 1196 |
size_t input_length = sizeof(input); |
| 1197 |
|
| 1198 |
uint8_t expected_output[] = {0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4, 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, |
| 1199 |
0xF8, 0x26, 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26, 0xCB, 0xC7, 0xF0, 0xE7, |
| 1200 |
0x1A, 0x99, 0xF4, 0x3B, 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21}; |
| 1201 |
size_t expected_output_length = sizeof(expected_output); |
| 1202 |
|
| 1203 |
struct aws_byte_cursor input_cur = aws_byte_cursor_from_array(input, input_length); |
| 1204 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, key_length); |
| 1205 |
struct aws_byte_buf output_buf; |
| 1206 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, allocator, expected_output_length)); |
| 1207 |
|
| 1208 |
struct aws_symmetric_cipher *cipher = aws_aes_keywrap_256_new(allocator, &key_cur); |
| 1209 |
ASSERT_NOT_NULL(cipher); |
| 1210 |
|
| 1211 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, input_cur, &output_buf)); |
| 1212 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &output_buf)); |
| 1213 |
ASSERT_BIN_ARRAYS_EQUALS(expected_output, expected_output_length, output_buf.buffer, output_buf.len); |
| 1214 |
|
| 1215 |
ASSERT_SUCCESS(aws_symmetric_cipher_reset(cipher)); |
| 1216 |
|
| 1217 |
struct aws_byte_buf decrypted_buf; |
| 1218 |
ASSERT_SUCCESS(aws_byte_buf_init(&decrypted_buf, allocator, input_length)); |
| 1219 |
|
| 1220 |
struct aws_byte_cursor encrypted_data = aws_byte_cursor_from_buf(&output_buf); |
| 1221 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1222 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, encrypted_data, &decrypted_buf)); |
| 1223 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1224 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 1225 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_FINALIZED, aws_symmetric_cipher_get_state(cipher)); |
| 1226 |
ASSERT_BIN_ARRAYS_EQUALS(input, input_length, decrypted_buf.buffer, decrypted_buf.len); |
| 1227 |
|
| 1228 |
aws_symmetric_cipher_destroy(cipher); |
| 1229 |
aws_byte_buf_clean_up(&output_buf); |
| 1230 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 1231 |
aws_cal_library_clean_up(); |
| 1232 |
|
| 1233 |
return AWS_OP_SUCCESS; |
| 1234 |
} |
| 1235 |
|
| 1236 |
AWS_TEST_CASE(aes_keywrap_RFC3394_256BitKey256CekTestVector, s_test_aes_keywrap_RFC3394_256BitKey256CekTestVector); |
| 1237 |
|
| 1238 |
static int s_test_Rfc3394_256BitKey_TestIntegrityCheckFailed(struct aws_allocator *allocator, void *ctx) { |
| 1239 |
(void)ctx; |
| 1240 |
aws_cal_library_test_init(allocator); |
| 1241 |
|
| 1242 |
uint8_t input[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, |
| 1243 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; |
| 1244 |
size_t input_length = sizeof(input); |
| 1245 |
|
| 1246 |
uint8_t key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 1247 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F}; |
| 1248 |
size_t key_length = sizeof(key); |
| 1249 |
|
| 1250 |
uint8_t expected_output[] = {0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4, 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, |
| 1251 |
0xF8, 0x26, 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26, 0xCB, 0xC7, 0xF0, 0xE7, |
| 1252 |
0x1A, 0x99, 0xF4, 0x3B, 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21}; |
| 1253 |
size_t expected_output_length = sizeof(expected_output); |
| 1254 |
|
| 1255 |
struct aws_byte_cursor input_cur = aws_byte_cursor_from_array(input, input_length); |
| 1256 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, key_length); |
| 1257 |
struct aws_byte_buf output_buf; |
| 1258 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, allocator, expected_output_length)); |
| 1259 |
|
| 1260 |
struct aws_symmetric_cipher *cipher = aws_aes_keywrap_256_new(allocator, &key_cur); |
| 1261 |
ASSERT_NOT_NULL(cipher); |
| 1262 |
|
| 1263 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, input_cur, &output_buf)); |
| 1264 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &output_buf)); |
| 1265 |
ASSERT_BIN_ARRAYS_EQUALS(expected_output, expected_output_length, output_buf.buffer, output_buf.len); |
| 1266 |
|
| 1267 |
|
| 1268 |
output_buf.buffer[0] ^= 0x01; |
| 1269 |
|
| 1270 |
ASSERT_SUCCESS(aws_symmetric_cipher_reset(cipher)); |
| 1271 |
|
| 1272 |
struct aws_byte_buf decrypted_buf; |
| 1273 |
ASSERT_SUCCESS(aws_byte_buf_init(&decrypted_buf, allocator, input_length)); |
| 1274 |
|
| 1275 |
struct aws_byte_cursor encrypted_data = aws_byte_cursor_from_buf(&output_buf); |
| 1276 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1277 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, encrypted_data, &decrypted_buf)); |
| 1278 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1279 |
ASSERT_FAILS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 1280 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_ERROR, aws_symmetric_cipher_get_state(cipher)); |
| 1281 |
ASSERT_FALSE(aws_symmetric_cipher_is_good(cipher)); |
| 1282 |
|
| 1283 |
aws_symmetric_cipher_destroy(cipher); |
| 1284 |
aws_byte_buf_clean_up(&output_buf); |
| 1285 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 1286 |
aws_cal_library_clean_up(); |
| 1287 |
|
| 1288 |
return AWS_OP_SUCCESS; |
| 1289 |
} |
| 1290 |
|
| 1291 |
AWS_TEST_CASE( |
| 1292 |
aes_keywrap_Rfc3394_256BitKey_TestIntegrityCheckFailed, |
| 1293 |
s_test_Rfc3394_256BitKey_TestIntegrityCheckFailed); |
| 1294 |
|
| 1295 |
static int s_test_RFC3394_256BitKeyTestBadPayload(struct aws_allocator *allocator, void *ctx) { |
| 1296 |
(void)ctx; |
| 1297 |
aws_cal_library_test_init(allocator); |
| 1298 |
|
| 1299 |
uint8_t input[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, |
| 1300 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; |
| 1301 |
size_t input_length = sizeof(input); |
| 1302 |
|
| 1303 |
uint8_t key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 1304 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F}; |
| 1305 |
size_t key_length = sizeof(key); |
| 1306 |
|
| 1307 |
uint8_t expected_output[] = {0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4, 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, |
| 1308 |
0xF8, 0x26, 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26, 0xCB, 0xC7, 0xF0, 0xE7, |
| 1309 |
0x1A, 0x99, 0xF4, 0x3B, 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21}; |
| 1310 |
size_t expected_output_length = sizeof(expected_output); |
| 1311 |
|
| 1312 |
struct aws_byte_cursor input_cur = aws_byte_cursor_from_array(input, input_length); |
| 1313 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, key_length); |
| 1314 |
struct aws_byte_buf output_buf; |
| 1315 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, allocator, expected_output_length)); |
| 1316 |
|
| 1317 |
struct aws_symmetric_cipher *cipher = aws_aes_keywrap_256_new(allocator, &key_cur); |
| 1318 |
ASSERT_NOT_NULL(cipher); |
| 1319 |
|
| 1320 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, input_cur, &output_buf)); |
| 1321 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &output_buf)); |
| 1322 |
ASSERT_BIN_ARRAYS_EQUALS(expected_output, expected_output_length, output_buf.buffer, output_buf.len); |
| 1323 |
|
| 1324 |
ASSERT_SUCCESS(aws_symmetric_cipher_reset(cipher)); |
| 1325 |
|
| 1326 |
struct aws_byte_buf decrypted_buf; |
| 1327 |
ASSERT_SUCCESS(aws_byte_buf_init(&decrypted_buf, allocator, input_length)); |
| 1328 |
|
| 1329 |
struct aws_byte_cursor encrypted_data = aws_byte_cursor_from_buf(&output_buf); |
| 1330 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1331 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, encrypted_data, &decrypted_buf)); |
| 1332 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1333 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 1334 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_FINALIZED, aws_symmetric_cipher_get_state(cipher)); |
| 1335 |
ASSERT_BIN_ARRAYS_EQUALS(input, input_length, decrypted_buf.buffer, decrypted_buf.len); |
| 1336 |
|
| 1337 |
aws_symmetric_cipher_destroy(cipher); |
| 1338 |
aws_byte_buf_clean_up(&output_buf); |
| 1339 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 1340 |
aws_cal_library_clean_up(); |
| 1341 |
|
| 1342 |
return AWS_OP_SUCCESS; |
| 1343 |
} |
| 1344 |
|
| 1345 |
AWS_TEST_CASE(aes_keywrap_RFC3394_256BitKeyTestBadPayload, s_test_RFC3394_256BitKeyTestBadPayload); |
| 1346 |
|
| 1347 |
static int s_test_RFC3394_256BitKey128BitCekTestVector(struct aws_allocator *allocator, void *ctx) { |
| 1348 |
(void)ctx; |
| 1349 |
aws_cal_library_test_init(allocator); |
| 1350 |
|
| 1351 |
uint8_t input[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; |
| 1352 |
size_t input_length = sizeof(input); |
| 1353 |
|
| 1354 |
uint8_t key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 1355 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F}; |
| 1356 |
size_t key_length = sizeof(key); |
| 1357 |
|
| 1358 |
uint8_t expected_output[] = {0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2, 0x63, 0xE9, 0x77, 0x79, |
| 1359 |
0x05, 0x81, 0x8A, 0x2A, 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7}; |
| 1360 |
size_t expected_output_length = sizeof(expected_output); |
| 1361 |
|
| 1362 |
struct aws_byte_cursor input_cur = aws_byte_cursor_from_array(input, input_length); |
| 1363 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, key_length); |
| 1364 |
struct aws_byte_buf output_buf; |
| 1365 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, allocator, expected_output_length)); |
| 1366 |
|
| 1367 |
struct aws_symmetric_cipher *cipher = aws_aes_keywrap_256_new(allocator, &key_cur); |
| 1368 |
ASSERT_NOT_NULL(cipher); |
| 1369 |
|
| 1370 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, input_cur, &output_buf)); |
| 1371 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &output_buf)); |
| 1372 |
ASSERT_BIN_ARRAYS_EQUALS(expected_output, expected_output_length, output_buf.buffer, output_buf.len); |
| 1373 |
|
| 1374 |
ASSERT_SUCCESS(aws_symmetric_cipher_reset(cipher)); |
| 1375 |
ASSERT_TRUE(aws_symmetric_cipher_is_good(cipher)); |
| 1376 |
|
| 1377 |
struct aws_byte_buf decrypted_buf; |
| 1378 |
ASSERT_SUCCESS(aws_byte_buf_init(&decrypted_buf, allocator, input_length)); |
| 1379 |
|
| 1380 |
struct aws_byte_cursor encrypted_data = aws_byte_cursor_from_buf(&output_buf); |
| 1381 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1382 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, encrypted_data, &decrypted_buf)); |
| 1383 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1384 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 1385 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_FINALIZED, aws_symmetric_cipher_get_state(cipher)); |
| 1386 |
aws_symmetric_cipher_destroy(cipher); |
| 1387 |
aws_byte_buf_clean_up(&output_buf); |
| 1388 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 1389 |
aws_cal_library_clean_up(); |
| 1390 |
|
| 1391 |
return AWS_OP_SUCCESS; |
| 1392 |
} |
| 1393 |
|
| 1394 |
AWS_TEST_CASE(aes_keywrap_RFC3394_256BitKey128BitCekTestVector, s_test_RFC3394_256BitKey128BitCekTestVector); |
| 1395 |
|
| 1396 |
static int s_test_RFC3394_256BitKey128BitCekIntegrityCheckFailedTestVector(struct aws_allocator *allocator, void *ctx) { |
| 1397 |
(void)ctx; |
| 1398 |
aws_cal_library_test_init(allocator); |
| 1399 |
|
| 1400 |
uint8_t input[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; |
| 1401 |
size_t input_length = sizeof(input); |
| 1402 |
|
| 1403 |
uint8_t key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 1404 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F}; |
| 1405 |
size_t key_length = sizeof(key); |
| 1406 |
|
| 1407 |
uint8_t expected_output[] = {0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2, 0x63, 0xE9, 0x77, 0x79, |
| 1408 |
0x05, 0x81, 0x8A, 0x2A, 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7}; |
| 1409 |
size_t expected_output_length = sizeof(expected_output); |
| 1410 |
|
| 1411 |
struct aws_byte_cursor input_cur = aws_byte_cursor_from_array(input, input_length); |
| 1412 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, key_length); |
| 1413 |
struct aws_byte_buf output_buf; |
| 1414 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, allocator, expected_output_length)); |
| 1415 |
|
| 1416 |
struct aws_symmetric_cipher *cipher = aws_aes_keywrap_256_new(allocator, &key_cur); |
| 1417 |
ASSERT_NOT_NULL(cipher); |
| 1418 |
|
| 1419 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, input_cur, &output_buf)); |
| 1420 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &output_buf)); |
| 1421 |
ASSERT_BIN_ARRAYS_EQUALS(expected_output, expected_output_length, output_buf.buffer, output_buf.len); |
| 1422 |
|
| 1423 |
ASSERT_SUCCESS(aws_symmetric_cipher_reset(cipher)); |
| 1424 |
|
| 1425 |
struct aws_byte_buf decrypted_buf; |
| 1426 |
ASSERT_SUCCESS(aws_byte_buf_init(&decrypted_buf, allocator, input_length)); |
| 1427 |
|
| 1428 |
struct aws_byte_cursor encrypted_data = aws_byte_cursor_from_buf(&output_buf); |
| 1429 |
encrypted_data.ptr[1] = encrypted_data.ptr[1] + encrypted_data.ptr[2]; |
| 1430 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1431 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, encrypted_data, &decrypted_buf)); |
| 1432 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1433 |
ASSERT_FAILS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 1434 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_ERROR, aws_symmetric_cipher_get_state(cipher)); |
| 1435 |
ASSERT_FALSE(aws_symmetric_cipher_is_good(cipher)); |
| 1436 |
|
| 1437 |
aws_symmetric_cipher_destroy(cipher); |
| 1438 |
aws_byte_buf_clean_up(&output_buf); |
| 1439 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 1440 |
aws_cal_library_clean_up(); |
| 1441 |
|
| 1442 |
return AWS_OP_SUCCESS; |
| 1443 |
} |
| 1444 |
|
| 1445 |
AWS_TEST_CASE( |
| 1446 |
aes_keywrap_RFC3394_256BitKey128BitCekIntegrityCheckFailedTestVector, |
| 1447 |
s_test_RFC3394_256BitKey128BitCekIntegrityCheckFailedTestVector); |
| 1448 |
|
| 1449 |
static int s_test_RFC3394_256BitKey128BitCekPayloadCheckFailedTestVector(struct aws_allocator *allocator, void *ctx) { |
| 1450 |
(void)ctx; |
| 1451 |
aws_cal_library_test_init(allocator); |
| 1452 |
|
| 1453 |
uint8_t input[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; |
| 1454 |
size_t input_length = sizeof(input); |
| 1455 |
|
| 1456 |
uint8_t key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
| 1457 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F}; |
| 1458 |
size_t key_length = sizeof(key); |
| 1459 |
|
| 1460 |
uint8_t expected_output[] = {0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2, 0x63, 0xE9, 0x77, 0x79, |
| 1461 |
0x05, 0x81, 0x8A, 0x2A, 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7}; |
| 1462 |
size_t expected_output_length = sizeof(expected_output); |
| 1463 |
|
| 1464 |
struct aws_byte_cursor input_cur = aws_byte_cursor_from_array(input, input_length); |
| 1465 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, key_length); |
| 1466 |
struct aws_byte_buf output_buf; |
| 1467 |
ASSERT_SUCCESS(aws_byte_buf_init(&output_buf, allocator, expected_output_length)); |
| 1468 |
|
| 1469 |
struct aws_symmetric_cipher *cipher = aws_aes_keywrap_256_new(allocator, &key_cur); |
| 1470 |
ASSERT_NOT_NULL(cipher); |
| 1471 |
|
| 1472 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, input_cur, &output_buf)); |
| 1473 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &output_buf)); |
| 1474 |
ASSERT_BIN_ARRAYS_EQUALS(expected_output, expected_output_length, output_buf.buffer, output_buf.len); |
| 1475 |
|
| 1476 |
ASSERT_SUCCESS(aws_symmetric_cipher_reset(cipher)); |
| 1477 |
|
| 1478 |
struct aws_byte_buf decrypted_buf; |
| 1479 |
ASSERT_SUCCESS(aws_byte_buf_init(&decrypted_buf, allocator, input_length)); |
| 1480 |
|
| 1481 |
struct aws_byte_cursor encrypted_data = aws_byte_cursor_from_buf(&output_buf); |
| 1482 |
encrypted_data.ptr[14] = encrypted_data.ptr[13] + encrypted_data.ptr[14]; |
| 1483 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1484 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, encrypted_data, &decrypted_buf)); |
| 1485 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1486 |
ASSERT_FAILS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 1487 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_ERROR, aws_symmetric_cipher_get_state(cipher)); |
| 1488 |
ASSERT_FALSE(aws_symmetric_cipher_is_good(cipher)); |
| 1489 |
|
| 1490 |
aws_symmetric_cipher_destroy(cipher); |
| 1491 |
aws_byte_buf_clean_up(&output_buf); |
| 1492 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 1493 |
aws_cal_library_clean_up(); |
| 1494 |
|
| 1495 |
return AWS_OP_SUCCESS; |
| 1496 |
} |
| 1497 |
|
| 1498 |
AWS_TEST_CASE( |
| 1499 |
aes_keywrap_RFC3394_256BitKey128BitCekPayloadCheckFailedTestVector, |
| 1500 |
s_test_RFC3394_256BitKey128BitCekPayloadCheckFailedTestVector); |
| 1501 |
|
| 1502 |
static int s_aes_keywrap_validate_materials_fails_fn(struct aws_allocator *allocator, void *ctx) { |
| 1503 |
(void)ctx; |
| 1504 |
aws_cal_library_test_init(allocator); |
| 1505 |
|
| 1506 |
uint8_t key_too_small[AWS_AES_256_KEY_BYTE_LEN - 1] = {0}; |
| 1507 |
uint8_t key_too_large[AWS_AES_256_KEY_BYTE_LEN + 1] = {0}; |
| 1508 |
|
| 1509 |
struct aws_byte_cursor key = aws_byte_cursor_from_array(key_too_small, sizeof(key_too_small)); |
| 1510 |
ASSERT_NULL(aws_aes_keywrap_256_new(allocator, &key)); |
| 1511 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_KEY_LENGTH_FOR_ALGORITHM, aws_last_error()); |
| 1512 |
|
| 1513 |
key = aws_byte_cursor_from_array(key_too_large, sizeof(key_too_large)); |
| 1514 |
ASSERT_NULL(aws_aes_keywrap_256_new(allocator, &key)); |
| 1515 |
ASSERT_UINT_EQUALS(AWS_ERROR_CAL_INVALID_KEY_LENGTH_FOR_ALGORITHM, aws_last_error()); |
| 1516 |
|
| 1517 |
aws_cal_library_clean_up(); |
| 1518 |
return AWS_OP_SUCCESS; |
| 1519 |
} |
| 1520 |
AWS_TEST_CASE(aes_keywrap_validate_materials_fails, s_aes_keywrap_validate_materials_fails_fn) |
| 1521 |
|
| 1522 |
static int s_test_input_too_large_fn(struct aws_allocator *allocator, void *ctx) { |
| 1523 |
(void)ctx; |
| 1524 |
aws_cal_library_test_init(allocator); |
| 1525 |
|
| 1526 |
uint8_t iv[AWS_AES_256_CIPHER_BLOCK_SIZE] = {0}; |
| 1527 |
uint8_t key[AWS_AES_256_KEY_BYTE_LEN] = {0}; |
| 1528 |
|
| 1529 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 1530 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 1531 |
|
| 1532 |
struct aws_symmetric_cipher *cipher = aws_aes_cbc_256_new(allocator, &key_cur, &iv_cur); |
| 1533 |
ASSERT_NOT_NULL(cipher); |
| 1534 |
|
| 1535 |
struct aws_byte_cursor invalid_cur = { |
| 1536 |
.ptr = key, |
| 1537 |
.len = INT_MAX, |
| 1538 |
}; |
| 1539 |
|
| 1540 |
ASSERT_ERROR(AWS_ERROR_CAL_BUFFER_TOO_LARGE_FOR_ALGORITHM, aws_symmetric_cipher_encrypt(cipher, invalid_cur, NULL)); |
| 1541 |
|
| 1542 |
ASSERT_TRUE(aws_symmetric_cipher_is_good(cipher)); |
| 1543 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1544 |
ASSERT_ERROR(AWS_ERROR_CAL_BUFFER_TOO_LARGE_FOR_ALGORITHM, aws_symmetric_cipher_decrypt(cipher, invalid_cur, NULL)); |
| 1545 |
|
| 1546 |
ASSERT_INT_EQUALS(AWS_SYMMETRIC_CIPHER_READY, aws_symmetric_cipher_get_state(cipher)); |
| 1547 |
ASSERT_TRUE(aws_symmetric_cipher_is_good(cipher)); |
| 1548 |
|
| 1549 |
aws_symmetric_cipher_destroy(cipher); |
| 1550 |
aws_cal_library_clean_up(); |
| 1551 |
return AWS_OP_SUCCESS; |
| 1552 |
} |
| 1553 |
AWS_TEST_CASE(aes_test_input_too_large, s_test_input_too_large_fn) |
| 1554 |
|
| 1555 |
static int s_aes_test_encrypt_empty_input(struct aws_allocator *allocator, void *ctx) { |
| 1556 |
(void)ctx; |
| 1557 |
aws_cal_library_test_init(allocator); |
| 1558 |
|
| 1559 |
uint8_t iv[] = {0xFB, 0x7B, 0x4A, 0x82, 0x4E, 0x82, 0xDA, 0xA6, 0xC8, 0xBC, 0x12, 0x51}; |
| 1560 |
|
| 1561 |
uint8_t key[] = {0x20, 0x14, 0x2E, 0x89, 0x8C, 0xD2, 0xFD, 0x98, 0x0F, 0xBF, 0x34, 0xDE, 0x6B, 0xC8, 0x5C, 0x14, |
| 1562 |
0xDA, 0x7D, 0x57, 0xBD, 0x28, 0xF4, 0xAA, 0x5C, 0xF1, 0x72, 0x8A, 0xB6, 0x4E, 0x84, 0x31, 0x42}; |
| 1563 |
|
| 1564 |
uint8_t aad[] = {0x16, 0x7B, 0x5C, 0x22, 0x61, 0x77, 0x73, 0x3A, 0x78, 0x2D, 0x61, 0x6D, 0x7A, 0x2D, 0x63, 0x65, |
| 1565 |
0x6B, 0x2D, 0x61, 0x6C, 0x67, 0x5C, 0x22, 0x3A, 0x20, 0x5C, 0x22, 0x41, 0x45, 0x53, 0x2F, 0x47, |
| 1566 |
0x43, 0x4D, 0x2F, 0x4E, 0x6F, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6E, 0x67, 0x5C, 0x22, 0x7D}; |
| 1567 |
|
| 1568 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 1569 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 1570 |
struct aws_byte_cursor aad_cur = aws_byte_cursor_from_array(aad, sizeof(aad)); |
| 1571 |
|
| 1572 |
struct aws_symmetric_cipher *cipher = aws_aes_gcm_256_new(allocator, &key_cur, &iv_cur, &aad_cur); |
| 1573 |
|
| 1574 |
|
| 1575 |
struct aws_byte_cursor data_cur = {0}; |
| 1576 |
struct aws_byte_buf encrypt_buf = {0}; |
| 1577 |
aws_byte_buf_init(&encrypt_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE * 2); |
| 1578 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, data_cur, &encrypt_buf)); |
| 1579 |
|
| 1580 |
|
| 1581 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &encrypt_buf)); |
| 1582 |
|
| 1583 |
ASSERT_INT_EQUALS(0, encrypt_buf.len); |
| 1584 |
|
| 1585 |
struct aws_byte_buf encryption_tag; |
| 1586 |
aws_byte_buf_init_copy_from_cursor(&encryption_tag, allocator, aws_symmetric_cipher_get_tag(cipher)); |
| 1587 |
|
| 1588 |
aws_symmetric_cipher_reset(cipher); |
| 1589 |
|
| 1590 |
aws_symmetric_cipher_set_tag(cipher, aws_byte_cursor_from_buf(&encryption_tag)); |
| 1591 |
|
| 1592 |
struct aws_byte_buf decrypted_buf = {0}; |
| 1593 |
aws_byte_buf_init(&decrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 1594 |
struct aws_byte_cursor ciphertext_cur = aws_byte_cursor_from_buf(&encrypt_buf); |
| 1595 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, ciphertext_cur, &decrypted_buf)); |
| 1596 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 1597 |
|
| 1598 |
aws_byte_buf_clean_up(&encryption_tag); |
| 1599 |
aws_byte_buf_clean_up(&encrypt_buf); |
| 1600 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 1601 |
aws_symmetric_cipher_destroy(cipher); |
| 1602 |
aws_cal_library_clean_up(); |
| 1603 |
|
| 1604 |
return AWS_OP_SUCCESS; |
| 1605 |
} |
| 1606 |
AWS_TEST_CASE(aes_test_encrypt_empty_input, s_aes_test_encrypt_empty_input) |
| 1607 |
|
| 1608 |
static int s_aes_gcm_corner_case_checker( |
| 1609 |
struct aws_allocator *allocator, |
| 1610 |
struct aws_byte_cursor key_cur, |
| 1611 |
struct aws_byte_cursor iv_cur, |
| 1612 |
struct aws_byte_cursor aad_cur, |
| 1613 |
struct aws_byte_cursor data_cur, |
| 1614 |
struct aws_byte_cursor expected_tag_cur) { |
| 1615 |
|
| 1616 |
aws_cal_library_test_init(allocator); |
| 1617 |
|
| 1618 |
|
| 1619 |
uint8_t wrong_tag[] = { |
| 1620 |
0x83, 0xC0, 0xE4, 0x2B, 0xB1, 0x95, 0xE2, 0x62, 0xCB, 0x3B, 0x3A, 0x74, 0xA0, 0xDA, 0xE1, 0xC8}; |
| 1621 |
struct aws_byte_cursor wrong_tag_cur = aws_byte_cursor_from_array(wrong_tag, sizeof(wrong_tag)); |
| 1622 |
|
| 1623 |
struct aws_symmetric_cipher *cipher = aws_aes_gcm_256_new(allocator, &key_cur, &iv_cur, &aad_cur); |
| 1624 |
|
| 1625 |
struct aws_byte_cursor tag = aws_symmetric_cipher_get_tag(cipher); |
| 1626 |
|
| 1627 |
ASSERT_TRUE(tag.len == 0 && tag.ptr == NULL); |
| 1628 |
|
| 1629 |
aws_symmetric_cipher_set_tag(cipher, wrong_tag_cur); |
| 1630 |
|
| 1631 |
|
| 1632 |
struct aws_byte_buf encrypt_buf = {0}; |
| 1633 |
aws_byte_buf_init(&encrypt_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE * 2); |
| 1634 |
ASSERT_SUCCESS(aws_symmetric_cipher_encrypt(cipher, data_cur, &encrypt_buf)); |
| 1635 |
|
| 1636 |
|
| 1637 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_encryption(cipher, &encrypt_buf)); |
| 1638 |
|
| 1639 |
if (data_cur.len == 0) { |
| 1640 |
ASSERT_INT_EQUALS(0, encrypt_buf.len); |
| 1641 |
} else { |
| 1642 |
ASSERT_TRUE(encrypt_buf.len > 0); |
| 1643 |
} |
| 1644 |
|
| 1645 |
struct aws_byte_cursor encryption_tag = aws_symmetric_cipher_get_tag(cipher); |
| 1646 |
|
| 1647 |
ASSERT_BIN_ARRAYS_EQUALS(expected_tag_cur.ptr, expected_tag_cur.len, encryption_tag.ptr, encryption_tag.len); |
| 1648 |
|
| 1649 |
|
| 1650 |
aws_symmetric_cipher_reset(cipher); |
| 1651 |
tag = aws_symmetric_cipher_get_tag(cipher); |
| 1652 |
|
| 1653 |
ASSERT_TRUE(tag.len == 0 && tag.ptr == NULL); |
| 1654 |
|
| 1655 |
aws_symmetric_cipher_set_tag(cipher, expected_tag_cur); |
| 1656 |
|
| 1657 |
struct aws_byte_buf decrypted_buf = {0}; |
| 1658 |
aws_byte_buf_init(&decrypted_buf, allocator, AWS_AES_256_CIPHER_BLOCK_SIZE); |
| 1659 |
struct aws_byte_cursor ciphertext_cur = aws_byte_cursor_from_buf(&encrypt_buf); |
| 1660 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, ciphertext_cur, &decrypted_buf)); |
| 1661 |
ASSERT_SUCCESS(aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 1662 |
|
| 1663 |
|
| 1664 |
aws_symmetric_cipher_reset(cipher); |
| 1665 |
aws_byte_buf_reset(&decrypted_buf, true); |
| 1666 |
aws_symmetric_cipher_set_tag(cipher, wrong_tag_cur); |
| 1667 |
ciphertext_cur = aws_byte_cursor_from_buf(&encrypt_buf); |
| 1668 |
ASSERT_SUCCESS(aws_symmetric_cipher_decrypt(cipher, ciphertext_cur, &decrypted_buf)); |
| 1669 |
ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_symmetric_cipher_finalize_decryption(cipher, &decrypted_buf)); |
| 1670 |
|
| 1671 |
|
| 1672 |
aws_symmetric_cipher_reset(cipher); |
| 1673 |
aws_byte_buf_reset(&decrypted_buf, true); |
| 1674 |
ciphertext_cur = aws_byte_cursor_from_buf(&encrypt_buf); |
| 1675 |
ASSERT_ERROR(AWS_ERROR_INVALID_ARGUMENT, aws_symmetric_cipher_decrypt(cipher, ciphertext_cur, &decrypted_buf)); |
| 1676 |
|
| 1677 |
aws_byte_buf_clean_up(&encrypt_buf); |
| 1678 |
aws_byte_buf_clean_up(&decrypted_buf); |
| 1679 |
aws_symmetric_cipher_destroy(cipher); |
| 1680 |
aws_cal_library_clean_up(); |
| 1681 |
return AWS_OP_SUCCESS; |
| 1682 |
} |
| 1683 |
|
| 1684 |
static int s_aes_test_empty_input_gcm_tag_corner_cases(struct aws_allocator *allocator, void *ctx) { |
| 1685 |
(void)ctx; |
| 1686 |
|
| 1687 |
uint8_t iv[] = {0xFB, 0x7B, 0x4A, 0x82, 0x4E, 0x82, 0xDA, 0xA6, 0xC8, 0xBC, 0x12, 0x51}; |
| 1688 |
|
| 1689 |
uint8_t key[] = {0x20, 0x14, 0x2E, 0x89, 0x8C, 0xD2, 0xFD, 0x98, 0x0F, 0xBF, 0x34, 0xDE, 0x6B, 0xC8, 0x5C, 0x14, |
| 1690 |
0xDA, 0x7D, 0x57, 0xBD, 0x28, 0xF4, 0xAA, 0x5C, 0xF1, 0x72, 0x8A, 0xB6, 0x4E, 0x84, 0x31, 0x42}; |
| 1691 |
|
| 1692 |
uint8_t aad[] = {0x16, 0x7B, 0x5C, 0x22, 0x61, 0x77, 0x73, 0x3A, 0x78, 0x2D, 0x61, 0x6D, 0x7A, 0x2D, 0x63, 0x65, |
| 1693 |
0x6B, 0x2D, 0x61, 0x6C, 0x67, 0x5C, 0x22, 0x3A, 0x20, 0x5C, 0x22, 0x41, 0x45, 0x53, 0x2F, 0x47, |
| 1694 |
0x43, 0x4D, 0x2F, 0x4E, 0x6F, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6E, 0x67, 0x5C, 0x22, 0x7D}; |
| 1695 |
|
| 1696 |
uint8_t expected_tag[] = { |
| 1697 |
0x81, 0xC0, 0xE4, 0x2B, 0xB1, 0x95, 0xE2, 0x62, 0xCB, 0x3B, 0x3A, 0x74, 0xA0, 0xDA, 0xE1, 0xC8}; |
| 1698 |
|
| 1699 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 1700 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 1701 |
struct aws_byte_cursor aad_cur = aws_byte_cursor_from_array(aad, sizeof(aad)); |
| 1702 |
struct aws_byte_cursor expected_tag_cur = aws_byte_cursor_from_array(expected_tag, sizeof(expected_tag)); |
| 1703 |
struct aws_byte_cursor data_cur = {0}; |
| 1704 |
|
| 1705 |
return s_aes_gcm_corner_case_checker(allocator, key_cur, iv_cur, aad_cur, data_cur, expected_tag_cur); |
| 1706 |
} |
| 1707 |
AWS_TEST_CASE(aes_test_empty_input_gcm_tag_corner_cases, s_aes_test_empty_input_gcm_tag_corner_cases) |
| 1708 |
|
| 1709 |
static int s_aes_test_gcm_tag_corner_cases(struct aws_allocator *allocator, void *ctx) { |
| 1710 |
(void)ctx; |
| 1711 |
|
| 1712 |
uint8_t iv[] = {0xFB, 0x7B, 0x4A, 0x82, 0x4E, 0x82, 0xDA, 0xA6, 0xC8, 0xBC, 0x12, 0x51}; |
| 1713 |
|
| 1714 |
uint8_t key[] = {0x20, 0x14, 0x2E, 0x89, 0x8C, 0xD2, 0xFD, 0x98, 0x0F, 0xBF, 0x34, 0xDE, 0x6B, 0xC8, 0x5C, 0x14, |
| 1715 |
0xDA, 0x7D, 0x57, 0xBD, 0x28, 0xF4, 0xAA, 0x5C, 0xF1, 0x72, 0x8A, 0xB6, 0x4E, 0x84, 0x31, 0x42}; |
| 1716 |
|
| 1717 |
uint8_t aad[] = {0x16, 0x7B, 0x5C, 0x22, 0x61, 0x77, 0x73, 0x3A, 0x78, 0x2D, 0x61, 0x6D, 0x7A, 0x2D, 0x63, 0x65, |
| 1718 |
0x6B, 0x2D, 0x61, 0x6C, 0x67, 0x5C, 0x22, 0x3A, 0x20, 0x5C, 0x22, 0x41, 0x45, 0x53, 0x2F, 0x47, |
| 1719 |
0x43, 0x4D, 0x2F, 0x4E, 0x6F, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6E, 0x67, 0x5C, 0x22, 0x7D}; |
| 1720 |
|
| 1721 |
uint8_t data[] = { |
| 1722 |
0x84, 0x99, 0x89, 0x3E, 0x16, 0xB0, 0xBA, 0x8B, 0x00, 0x7D, 0x54, 0x66, 0x5A, 0x84, 0x99, 0x89, 0x3E}; |
| 1723 |
|
| 1724 |
uint8_t expected_tag[] = { |
| 1725 |
0x76, 0x4D, 0x21, 0xD6, 0xC0, 0xD8, 0xC7, 0xF9, 0xCA, 0x6D, 0xF2, 0x19, 0xAE, 0x56, 0xDC, 0x1F}; |
| 1726 |
|
| 1727 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 1728 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 1729 |
struct aws_byte_cursor aad_cur = aws_byte_cursor_from_array(aad, sizeof(aad)); |
| 1730 |
struct aws_byte_cursor expected_tag_cur = aws_byte_cursor_from_array(expected_tag, sizeof(expected_tag)); |
| 1731 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 1732 |
|
| 1733 |
return s_aes_gcm_corner_case_checker(allocator, key_cur, iv_cur, aad_cur, data_cur, expected_tag_cur); |
| 1734 |
} |
| 1735 |
AWS_TEST_CASE(aes_test_gcm_tag_corner_cases, s_aes_test_gcm_tag_corner_cases) |
| 1736 |
|
| 1737 |
static int s_aes_test_gcm_tag_large_input_corner_cases(struct aws_allocator *allocator, void *ctx) { |
| 1738 |
(void)ctx; |
| 1739 |
|
| 1740 |
uint8_t iv[] = {0xFB, 0x7B, 0x4A, 0x82, 0x4E, 0x82, 0xDA, 0xA6, 0xC8, 0xBC, 0x12, 0x51}; |
| 1741 |
|
| 1742 |
uint8_t key[] = {0x20, 0x14, 0x2E, 0x89, 0x8C, 0xD2, 0xFD, 0x98, 0x0F, 0xBF, 0x34, 0xDE, 0x6B, 0xC8, 0x5C, 0x14, |
| 1743 |
0xDA, 0x7D, 0x57, 0xBD, 0x28, 0xF4, 0xAA, 0x5C, 0xF1, 0x72, 0x8A, 0xB6, 0x4E, 0x84, 0x31, 0x42}; |
| 1744 |
|
| 1745 |
uint8_t aad[] = {0x16, 0x7B, 0x5C, 0x22, 0x61, 0x77, 0x73, 0x3A, 0x78, 0x2D, 0x61, 0x6D, 0x7A, 0x2D, 0x63, 0x65, |
| 1746 |
0x6B, 0x2D, 0x61, 0x6C, 0x67, 0x5C, 0x22, 0x3A, 0x20, 0x5C, 0x22, 0x41, 0x45, 0x53, 0x2F, 0x47, |
| 1747 |
0x43, 0x4D, 0x2F, 0x4E, 0x6F, 0x50, 0x61, 0x64, 0x64, 0x69, 0x6E, 0x67, 0x5C, 0x22, 0x7D}; |
| 1748 |
|
| 1749 |
uint8_t data[] = {0x84, 0x99, 0x89, 0x3E, 0x16, 0xB0, 0xBA, 0x8B, 0x00, 0x7D, 0x54, 0x66, 0x5A, |
| 1750 |
0x84, 0x99, 0x89, 0x3E, 0x84, 0x99, 0x89, 0x3E, 0x16, 0xB0, 0xBA, 0x8B, 0x00, |
| 1751 |
0x7D, 0x54, 0x66, 0x5A, 0x84, 0x99, 0x89, 0x3E, 0x84, 0x99, 0x89, 0x3E, 0x16, |
| 1752 |
0xB0, 0xBA, 0x8B, 0x00, 0x7D, 0x54, 0x66, 0x5A, 0x84, 0x99, 0x89, 0x3E}; |
| 1753 |
|
| 1754 |
uint8_t expected_tag[] = { |
| 1755 |
0xEA, 0x5E, 0x8A, 0x4B, 0x76, 0xE8, 0x9D, 0xC5, 0xF1, 0x32, 0x14, 0x64, 0xD0, 0x93, 0x74, 0xB7}; |
| 1756 |
|
| 1757 |
struct aws_byte_cursor key_cur = aws_byte_cursor_from_array(key, sizeof(key)); |
| 1758 |
struct aws_byte_cursor iv_cur = aws_byte_cursor_from_array(iv, sizeof(iv)); |
| 1759 |
struct aws_byte_cursor aad_cur = aws_byte_cursor_from_array(aad, sizeof(aad)); |
| 1760 |
struct aws_byte_cursor expected_tag_cur = aws_byte_cursor_from_array(expected_tag, sizeof(expected_tag)); |
| 1761 |
struct aws_byte_cursor data_cur = aws_byte_cursor_from_array(data, sizeof(data)); |
| 1762 |
|
| 1763 |
return s_aes_gcm_corner_case_checker(allocator, key_cur, iv_cur, aad_cur, data_cur, expected_tag_cur); |
| 1764 |
} |
| 1765 |
AWS_TEST_CASE(aes_test_gcm_tag_large_input_corner_cases, s_aes_test_gcm_tag_large_input_corner_cases) |