| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/cal/cal.h> |
| 6 |
#include <aws/cal/ecc.h> |
| 7 |
#include <aws/cal/hash.h> |
| 8 |
#include <aws/common/byte_buf.h> |
| 9 |
#include <aws/common/encoding.h> |
| 10 |
#include <aws/common/string.h> |
| 11 |
#include <aws/testing/aws_test_harness.h> |
| 12 |
|
| 13 |
#include "test_case_helper.h" |
| 14 |
|
| 15 |
static int s_test_key_derivation( |
| 16 |
struct aws_allocator *allocator, |
| 17 |
enum aws_ecc_curve_name curve_name, |
| 18 |
struct aws_byte_cursor private_key, |
| 19 |
struct aws_byte_cursor expected_pub_x, |
| 20 |
struct aws_byte_cursor expected_pub_y) { |
| 21 |
|
| 22 |
aws_cal_library_test_init(allocator); |
| 23 |
|
| 24 |
struct aws_ecc_key_pair *private_key_pair = |
| 25 |
aws_ecc_key_pair_new_from_private_key(allocator, curve_name, &private_key); |
| 26 |
|
| 27 |
ASSERT_NOT_NULL(private_key_pair); |
| 28 |
|
| 29 |
int error = aws_ecc_key_pair_derive_public_key(private_key_pair); |
| 30 |
|
| 31 |
|
| 32 |
if (error) { |
| 33 |
ASSERT_INT_EQUALS(AWS_ERROR_UNSUPPORTED_OPERATION, aws_last_error()); |
| 34 |
goto complete; |
| 35 |
} |
| 36 |
|
| 37 |
struct aws_byte_cursor pub_x; |
| 38 |
struct aws_byte_cursor pub_y; |
| 39 |
|
| 40 |
aws_ecc_key_pair_get_public_key(private_key_pair, &pub_x, &pub_y); |
| 41 |
ASSERT_BIN_ARRAYS_EQUALS(expected_pub_x.ptr, expected_pub_x.len, pub_x.ptr, pub_x.len); |
| 42 |
|
| 43 |
ASSERT_BIN_ARRAYS_EQUALS(expected_pub_y.ptr, expected_pub_y.len, pub_y.ptr, pub_y.len); |
| 44 |
|
| 45 |
complete: |
| 46 |
aws_ecc_key_pair_release(private_key_pair); |
| 47 |
|
| 48 |
aws_cal_library_clean_up(); |
| 49 |
return AWS_OP_SUCCESS; |
| 50 |
} |
| 51 |
|
| 52 |
static int s_ecdsa_p256_test_pub_key_derivation_fn(struct aws_allocator *allocator, void *ctx) { |
| 53 |
(void)ctx; |
| 54 |
|
| 55 |
uint8_t d[] = { |
| 56 |
0xc9, 0x80, 0x68, 0x98, 0xa0, 0x33, 0x49, 0x16, 0xc8, 0x60, 0x74, 0x88, 0x80, 0xa5, 0x41, 0xf0, |
| 57 |
0x93, 0xb5, 0x79, 0xa9, 0xb1, 0xf3, 0x29, 0x34, 0xd8, 0x6c, 0x36, 0x3c, 0x39, 0x80, 0x03, 0x57, |
| 58 |
}; |
| 59 |
|
| 60 |
struct aws_byte_cursor private_key = aws_byte_cursor_from_array(d, sizeof(d)); |
| 61 |
|
| 62 |
uint8_t x[] = { |
| 63 |
0xd0, 0x72, 0x0d, 0xc6, 0x91, 0xaa, 0x80, 0x09, 0x6b, 0xa3, 0x2f, 0xed, 0x1c, 0xb9, 0x7c, 0x2b, |
| 64 |
0x62, 0x06, 0x90, 0xd0, 0x6d, 0xe0, 0x31, 0x7b, 0x86, 0x18, 0xd5, 0xce, 0x65, 0xeb, 0x72, 0x8f, |
| 65 |
}; |
| 66 |
|
| 67 |
struct aws_byte_cursor pub_x = aws_byte_cursor_from_array(x, sizeof(x)); |
| 68 |
|
| 69 |
uint8_t y[] = { |
| 70 |
0x96, 0x81, 0xb5, 0x17, 0xb1, 0xcd, 0xa1, 0x7d, 0x0d, 0x83, 0xd3, 0x35, 0xd9, 0xc4, 0xa8, 0xa9, |
| 71 |
0xa9, 0xb0, 0xb1, 0xb3, 0xc7, 0x10, 0x6d, 0x8f, 0x3c, 0x72, 0xbc, 0x50, 0x93, 0xdc, 0x27, 0x5f, |
| 72 |
}; |
| 73 |
|
| 74 |
struct aws_byte_cursor pub_y = aws_byte_cursor_from_array(y, sizeof(y)); |
| 75 |
|
| 76 |
return s_test_key_derivation(allocator, AWS_CAL_ECDSA_P256, private_key, pub_x, pub_y); |
| 77 |
} |
| 78 |
|
| 79 |
AWS_TEST_CASE(ecdsa_p256_test_pub_key_derivation, s_ecdsa_p256_test_pub_key_derivation_fn) |
| 80 |
|
| 81 |
static int s_ecdsa_p384_test_pub_key_derivation_fn(struct aws_allocator *allocator, void *ctx) { |
| 82 |
(void)ctx; |
| 83 |
|
| 84 |
uint8_t d[] = { |
| 85 |
0x53, 0x94, 0xf7, 0x97, 0x3e, 0xa8, 0x68, 0xc5, 0x2b, 0xf3, 0xff, 0x8d, 0x8c, 0xee, 0xb4, 0xdb, |
| 86 |
0x90, 0xa6, 0x83, 0x65, 0x3b, 0x12, 0x48, 0x5d, 0x5f, 0x62, 0x7c, 0x3c, 0xe5, 0xab, 0xd8, 0x97, |
| 87 |
0x8f, 0xc9, 0x67, 0x3d, 0x14, 0xa7, 0x1d, 0x92, 0x57, 0x47, 0x93, 0x16, 0x62, 0x49, 0x3c, 0x37, |
| 88 |
}; |
| 89 |
|
| 90 |
struct aws_byte_cursor private_key = aws_byte_cursor_from_array(d, sizeof(d)); |
| 91 |
|
| 92 |
uint8_t x[] = { |
| 93 |
0xfd, 0x3c, 0x84, 0xe5, 0x68, 0x9b, 0xed, 0x27, 0x0e, 0x60, 0x1b, 0x3d, 0x80, 0xf9, 0x0d, 0x67, |
| 94 |
0xa9, 0xae, 0x45, 0x1c, 0xce, 0x89, 0x0f, 0x53, 0xe5, 0x83, 0x22, 0x9a, 0xd0, 0xe2, 0xee, 0x64, |
| 95 |
0x56, 0x11, 0xfa, 0x99, 0x36, 0xdf, 0xa4, 0x53, 0x06, 0xec, 0x18, 0x06, 0x67, 0x74, 0xaa, 0x24, |
| 96 |
}; |
| 97 |
|
| 98 |
struct aws_byte_cursor pub_x = aws_byte_cursor_from_array(x, sizeof(x)); |
| 99 |
|
| 100 |
uint8_t y[] = { |
| 101 |
0xb8, 0x3c, 0xa4, 0x12, 0x6c, 0xfc, 0x4c, 0x4d, 0x1d, 0x18, 0xa4, 0xb6, 0xc2, 0x1c, 0x7f, 0x69, |
| 102 |
0x9d, 0x51, 0x23, 0xdd, 0x9c, 0x24, 0xf6, 0x6f, 0x83, 0x38, 0x46, 0xee, 0xb5, 0x82, 0x96, 0x19, |
| 103 |
0x6b, 0x42, 0xec, 0x06, 0x42, 0x5d, 0xb5, 0xb7, 0x0a, 0x4b, 0x81, 0xb7, 0xfc, 0xf7, 0x05, 0xa0, |
| 104 |
}; |
| 105 |
|
| 106 |
struct aws_byte_cursor pub_y = aws_byte_cursor_from_array(y, sizeof(y)); |
| 107 |
|
| 108 |
return s_test_key_derivation(allocator, AWS_CAL_ECDSA_P384, private_key, pub_x, pub_y); |
| 109 |
} |
| 110 |
|
| 111 |
AWS_TEST_CASE(ecdsa_p384_test_pub_key_derivation, s_ecdsa_p384_test_pub_key_derivation_fn) |
| 112 |
|
| 113 |
static int s_test_known_signing_value( |
| 114 |
struct aws_allocator *allocator, |
| 115 |
enum aws_ecc_curve_name curve_name, |
| 116 |
struct aws_byte_cursor private_key, |
| 117 |
struct aws_byte_cursor pub_x, |
| 118 |
struct aws_byte_cursor pub_y) { |
| 119 |
|
| 120 |
aws_cal_library_test_init(allocator); |
| 121 |
|
| 122 |
struct aws_ecc_key_pair *signing_key = aws_ecc_key_pair_new_from_private_key(allocator, curve_name, &private_key); |
| 123 |
ASSERT_NOT_NULL(signing_key); |
| 124 |
|
| 125 |
struct aws_ecc_key_pair *verifying_key = |
| 126 |
aws_ecc_key_pair_new_from_public_key(allocator, curve_name, &pub_x, &pub_y); |
| 127 |
ASSERT_NOT_NULL(verifying_key); |
| 128 |
|
| 129 |
uint8_t message[] = { |
| 130 |
0x59, 0x05, 0x23, 0x88, 0x77, 0xc7, 0x74, 0x21, 0xf7, 0x3e, 0x43, 0xee, 0x3d, 0xa6, 0xf2, 0xd9, |
| 131 |
0xe2, 0xcc, 0xad, 0x5f, 0xc9, 0x42, 0xdc, 0xec, 0x0c, 0xbd, 0x25, 0x48, 0x29, 0x35, 0xfa, 0xaf, |
| 132 |
0x41, 0x69, 0x83, 0xfe, 0x16, 0x5b, 0x1a, 0x04, 0x5e, 0xe2, 0xbc, 0xd2, 0xe6, 0xdc, 0xa3, 0xbd, |
| 133 |
0xf4, 0x6c, 0x43, 0x10, 0xa7, 0x46, 0x1f, 0x9a, 0x37, 0x96, 0x0c, 0xa6, 0x72, 0xd3, 0xfe, 0xb5, |
| 134 |
0x47, 0x3e, 0x25, 0x36, 0x05, 0xfb, 0x1d, 0xdf, 0xd2, 0x80, 0x65, 0xb5, 0x3c, 0xb5, 0x85, 0x8a, |
| 135 |
0x8a, 0xd2, 0x81, 0x75, 0xbf, 0x9b, 0xd3, 0x86, 0xa5, 0xe4, 0x71, 0xea, 0x7a, 0x65, 0xc1, 0x7c, |
| 136 |
0xc9, 0x34, 0xa9, 0xd7, 0x91, 0xe9, 0x14, 0x91, 0xeb, 0x37, 0x54, 0xd0, 0x37, 0x99, 0x79, 0x0f, |
| 137 |
0xe2, 0xd3, 0x08, 0xd1, 0x61, 0x46, 0xd5, 0xc9, 0xb0, 0xd0, 0xde, 0xbd, 0x97, 0xd7, 0x9c, 0xe8, |
| 138 |
}; |
| 139 |
struct aws_byte_cursor message_input = aws_byte_cursor_from_array(message, sizeof(message)); |
| 140 |
uint8_t hash[AWS_SHA256_LEN]; |
| 141 |
AWS_ZERO_ARRAY(hash); |
| 142 |
struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash)); |
| 143 |
aws_sha256_compute(allocator, &message_input, &hash_value, 0); |
| 144 |
|
| 145 |
size_t signature_size = aws_ecc_key_pair_signature_length(signing_key); |
| 146 |
|
| 147 |
struct aws_byte_buf signature_buf; |
| 148 |
AWS_ZERO_STRUCT(signature_buf); |
| 149 |
aws_byte_buf_init(&signature_buf, allocator, signature_size); |
| 150 |
|
| 151 |
struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value); |
| 152 |
ASSERT_SUCCESS(aws_ecc_key_pair_sign_message(signing_key, &hash_cur, &signature_buf)); |
| 153 |
|
| 154 |
struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature_buf); |
| 155 |
ASSERT_SUCCESS(aws_ecc_key_pair_verify_signature(verifying_key, &hash_cur, &signature_cur)); |
| 156 |
|
| 157 |
aws_byte_buf_clean_up(&signature_buf); |
| 158 |
aws_ecc_key_pair_release(verifying_key); |
| 159 |
aws_ecc_key_pair_release(signing_key); |
| 160 |
|
| 161 |
aws_cal_library_clean_up(); |
| 162 |
|
| 163 |
return AWS_OP_SUCCESS; |
| 164 |
} |
| 165 |
|
| 166 |
static int s_ecdsa_p256_test_known_signing_value_fn(struct aws_allocator *allocator, void *ctx) { |
| 167 |
(void)ctx; |
| 168 |
|
| 169 |
uint8_t d[] = { |
| 170 |
0x51, 0x9b, 0x42, 0x3d, 0x71, 0x5f, 0x8b, 0x58, 0x1f, 0x4f, 0xa8, 0xee, 0x59, 0xf4, 0x77, 0x1a, |
| 171 |
0x5b, 0x44, 0xc8, 0x13, 0x0b, 0x4e, 0x3e, 0xac, 0xca, 0x54, 0xa5, 0x6d, 0xda, 0x72, 0xb4, 0x64, |
| 172 |
}; |
| 173 |
|
| 174 |
struct aws_byte_cursor private_key = aws_byte_cursor_from_array(d, sizeof(d)); |
| 175 |
|
| 176 |
uint8_t x[] = { |
| 177 |
0x1c, 0xcb, 0xe9, 0x1c, 0x07, 0x5f, 0xc7, 0xf4, 0xf0, 0x33, 0xbf, 0xa2, 0x48, 0xdb, 0x8f, 0xcc, |
| 178 |
0xd3, 0x56, 0x5d, 0xe9, 0x4b, 0xbf, 0xb1, 0x2f, 0x3c, 0x59, 0xff, 0x46, 0xc2, 0x71, 0xbf, 0x83, |
| 179 |
}; |
| 180 |
|
| 181 |
uint8_t y[] = { |
| 182 |
0xce, 0x40, 0x14, 0xc6, 0x88, 0x11, 0xf9, 0xa2, 0x1a, 0x1f, 0xdb, 0x2c, 0x0e, 0x61, 0x13, 0xe0, |
| 183 |
0x6d, 0xb7, 0xca, 0x93, 0xb7, 0x40, 0x4e, 0x78, 0xdc, 0x7c, 0xcd, 0x5c, 0xa8, 0x9a, 0x4c, 0xa9, |
| 184 |
}; |
| 185 |
|
| 186 |
struct aws_byte_cursor pub_x = aws_byte_cursor_from_array(x, sizeof(x)); |
| 187 |
struct aws_byte_cursor pub_y = aws_byte_cursor_from_array(y, sizeof(y)); |
| 188 |
|
| 189 |
return s_test_known_signing_value(allocator, AWS_CAL_ECDSA_P256, private_key, pub_x, pub_y); |
| 190 |
} |
| 191 |
|
| 192 |
AWS_TEST_CASE(ecdsa_p256_test_known_signing_value, s_ecdsa_p256_test_known_signing_value_fn) |
| 193 |
|
| 194 |
static int s_ecdsa_p384_test_known_signing_value_fn(struct aws_allocator *allocator, void *ctx) { |
| 195 |
(void)ctx; |
| 196 |
|
| 197 |
uint8_t d[] = { |
| 198 |
0x53, 0x94, 0xf7, 0x97, 0x3e, 0xa8, 0x68, 0xc5, 0x2b, 0xf3, 0xff, 0x8d, 0x8c, 0xee, 0xb4, 0xdb, |
| 199 |
0x90, 0xa6, 0x83, 0x65, 0x3b, 0x12, 0x48, 0x5d, 0x5f, 0x62, 0x7c, 0x3c, 0xe5, 0xab, 0xd8, 0x97, |
| 200 |
0x8f, 0xc9, 0x67, 0x3d, 0x14, 0xa7, 0x1d, 0x92, 0x57, 0x47, 0x93, 0x16, 0x62, 0x49, 0x3c, 0x37, |
| 201 |
}; |
| 202 |
|
| 203 |
struct aws_byte_cursor private_key = aws_byte_cursor_from_array(d, sizeof(d)); |
| 204 |
|
| 205 |
uint8_t x[] = { |
| 206 |
0xfd, 0x3c, 0x84, 0xe5, 0x68, 0x9b, 0xed, 0x27, 0x0e, 0x60, 0x1b, 0x3d, 0x80, 0xf9, 0x0d, 0x67, |
| 207 |
0xa9, 0xae, 0x45, 0x1c, 0xce, 0x89, 0x0f, 0x53, 0xe5, 0x83, 0x22, 0x9a, 0xd0, 0xe2, 0xee, 0x64, |
| 208 |
0x56, 0x11, 0xfa, 0x99, 0x36, 0xdf, 0xa4, 0x53, 0x06, 0xec, 0x18, 0x06, 0x67, 0x74, 0xaa, 0x24, |
| 209 |
}; |
| 210 |
|
| 211 |
struct aws_byte_cursor pub_x = aws_byte_cursor_from_array(x, sizeof(x)); |
| 212 |
|
| 213 |
uint8_t y[] = { |
| 214 |
0xb8, 0x3c, 0xa4, 0x12, 0x6c, 0xfc, 0x4c, 0x4d, 0x1d, 0x18, 0xa4, 0xb6, 0xc2, 0x1c, 0x7f, 0x69, |
| 215 |
0x9d, 0x51, 0x23, 0xdd, 0x9c, 0x24, 0xf6, 0x6f, 0x83, 0x38, 0x46, 0xee, 0xb5, 0x82, 0x96, 0x19, |
| 216 |
0x6b, 0x42, 0xec, 0x06, 0x42, 0x5d, 0xb5, 0xb7, 0x0a, 0x4b, 0x81, 0xb7, 0xfc, 0xf7, 0x05, 0xa0, |
| 217 |
}; |
| 218 |
|
| 219 |
struct aws_byte_cursor pub_y = aws_byte_cursor_from_array(y, sizeof(y)); |
| 220 |
|
| 221 |
return s_test_known_signing_value(allocator, AWS_CAL_ECDSA_P384, private_key, pub_x, pub_y); |
| 222 |
} |
| 223 |
|
| 224 |
AWS_TEST_CASE(ecdsa_p384_test_known_signing_value, s_ecdsa_p384_test_known_signing_value_fn) |
| 225 |
|
| 226 |
static int s_ecdsa_test_invalid_signature_fn(struct aws_allocator *allocator, void *ctx) { |
| 227 |
(void)ctx; |
| 228 |
|
| 229 |
aws_cal_library_test_init(allocator); |
| 230 |
|
| 231 |
struct aws_ecc_key_pair *key_pair = aws_ecc_key_pair_new_generate_random(allocator, AWS_CAL_ECDSA_P256); |
| 232 |
ASSERT_NOT_NULL(key_pair); |
| 233 |
|
| 234 |
uint8_t message[] = { |
| 235 |
0x59, 0x05, 0x23, 0x88, 0x77, 0xc7, 0x74, 0x21, 0xf7, 0x3e, 0x43, 0xee, 0x3d, 0xa6, 0xf2, 0xd9, |
| 236 |
0xe2, 0xcc, 0xad, 0x5f, 0xc9, 0x42, 0xdc, 0xec, 0x0c, 0xbd, 0x25, 0x48, 0x29, 0x35, 0xfa, 0xaf, |
| 237 |
0x41, 0x69, 0x83, 0xfe, 0x16, 0x5b, 0x1a, 0x04, 0x5e, 0xe2, 0xbc, 0xd2, 0xe6, 0xdc, 0xa3, 0xbd, |
| 238 |
0xf4, 0x6c, 0x43, 0x10, 0xa7, 0x46, 0x1f, 0x9a, 0x37, 0x96, 0x0c, 0xa6, 0x72, 0xd3, 0xfe, 0xb5, |
| 239 |
0x47, 0x3e, 0x25, 0x36, 0x05, 0xfb, 0x1d, 0xdf, 0xd2, 0x80, 0x65, 0xb5, 0x3c, 0xb5, 0x85, 0x8a, |
| 240 |
0x8a, 0xd2, 0x81, 0x75, 0xbf, 0x9b, 0xd3, 0x86, 0xa5, 0xe4, 0x71, 0xea, 0x7a, 0x65, 0xc1, 0x7c, |
| 241 |
0xc9, 0x34, 0xa9, 0xd7, 0x91, 0xe9, 0x14, 0x91, 0xeb, 0x37, 0x54, 0xd0, 0x37, 0x99, 0x79, 0x0f, |
| 242 |
0xe2, 0xd3, 0x08, 0xd1, 0x61, 0x46, 0xd5, 0xc9, 0xb0, 0xd0, 0xde, 0xbd, 0x97, 0xd7, 0x9c, 0xe8, |
| 243 |
}; |
| 244 |
struct aws_byte_cursor message_input = aws_byte_cursor_from_array(message, sizeof(message)); |
| 245 |
uint8_t hash[AWS_SHA256_LEN]; |
| 246 |
AWS_ZERO_ARRAY(hash); |
| 247 |
struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash)); |
| 248 |
aws_sha256_compute(allocator, &message_input, &hash_value, 0); |
| 249 |
|
| 250 |
size_t signature_size = aws_ecc_key_pair_signature_length(key_pair); |
| 251 |
|
| 252 |
struct aws_byte_buf signature_buf; |
| 253 |
AWS_ZERO_STRUCT(signature_buf); |
| 254 |
aws_byte_buf_init(&signature_buf, allocator, signature_size); |
| 255 |
|
| 256 |
struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value); |
| 257 |
ASSERT_SUCCESS(aws_ecc_key_pair_sign_message(key_pair, &hash_cur, &signature_buf)); |
| 258 |
|
| 259 |
struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature_buf); |
| 260 |
|
| 261 |
uint8_t value_to_flip = signature_buf.buffer[15]; |
| 262 |
signature_buf.buffer[15] = value_to_flip == 0 ? 0x0a : ~(value_to_flip); |
| 263 |
ASSERT_ERROR( |
| 264 |
AWS_ERROR_CAL_SIGNATURE_VALIDATION_FAILED, |
| 265 |
aws_ecc_key_pair_verify_signature(key_pair, &hash_cur, &signature_cur)); |
| 266 |
|
| 267 |
aws_byte_buf_clean_up(&signature_buf); |
| 268 |
aws_ecc_key_pair_release(key_pair); |
| 269 |
|
| 270 |
aws_cal_library_clean_up(); |
| 271 |
|
| 272 |
return AWS_OP_SUCCESS; |
| 273 |
} |
| 274 |
|
| 275 |
AWS_TEST_CASE(ecdsa_test_invalid_signature, s_ecdsa_test_invalid_signature_fn) |
| 276 |
|
| 277 |
static int s_test_key_gen(struct aws_allocator *allocator, enum aws_ecc_curve_name curve_name) { |
| 278 |
aws_cal_library_test_init(allocator); |
| 279 |
|
| 280 |
struct aws_ecc_key_pair *key_pair = aws_ecc_key_pair_new_generate_random(allocator, curve_name); |
| 281 |
|
| 282 |
struct aws_byte_cursor pub_x; |
| 283 |
struct aws_byte_cursor pub_y; |
| 284 |
|
| 285 |
aws_ecc_key_pair_get_public_key(key_pair, &pub_x, &pub_y); |
| 286 |
ASSERT_TRUE(pub_x.len > 0); |
| 287 |
ASSERT_TRUE(pub_y.len > 0); |
| 288 |
|
| 289 |
struct aws_byte_cursor priv_d; |
| 290 |
aws_ecc_key_pair_get_private_key(key_pair, &priv_d); |
| 291 |
ASSERT_TRUE(priv_d.len > 0); |
| 292 |
|
| 293 |
uint8_t message[] = { |
| 294 |
0x59, 0x05, 0x23, 0x88, 0x77, 0xc7, 0x74, 0x21, 0xf7, 0x3e, 0x43, 0xee, 0x3d, 0xa6, 0xf2, 0xd9, |
| 295 |
0xe2, 0xcc, 0xad, 0x5f, 0xc9, 0x42, 0xdc, 0xec, 0x0c, 0xbd, 0x25, 0x48, 0x29, 0x35, 0xfa, 0xaf, |
| 296 |
0x41, 0x69, 0x83, 0xfe, 0x16, 0x5b, 0x1a, 0x04, 0x5e, 0xe2, 0xbc, 0xd2, 0xe6, 0xdc, 0xa3, 0xbd, |
| 297 |
0xf4, 0x6c, 0x43, 0x10, 0xa7, 0x46, 0x1f, 0x9a, 0x37, 0x96, 0x0c, 0xa6, 0x72, 0xd3, 0xfe, 0xb5, |
| 298 |
0x47, 0x3e, 0x25, 0x36, 0x05, 0xfb, 0x1d, 0xdf, 0xd2, 0x80, 0x65, 0xb5, 0x3c, 0xb5, 0x85, 0x8a, |
| 299 |
0x8a, 0xd2, 0x81, 0x75, 0xbf, 0x9b, 0xd3, 0x86, 0xa5, 0xe4, 0x71, 0xea, 0x7a, 0x65, 0xc1, 0x7c, |
| 300 |
0xc9, 0x34, 0xa9, 0xd7, 0x91, 0xe9, 0x14, 0x91, 0xeb, 0x37, 0x54, 0xd0, 0x37, 0x99, 0x79, 0x0f, |
| 301 |
0xe2, 0xd3, 0x08, 0xd1, 0x61, 0x46, 0xd5, 0xc9, 0xb0, 0xd0, 0xde, 0xbd, 0x97, 0xd7, 0x9c, 0xe8, |
| 302 |
}; |
| 303 |
|
| 304 |
struct aws_byte_cursor message_input = aws_byte_cursor_from_array(message, sizeof(message)); |
| 305 |
uint8_t hash[AWS_SHA256_LEN]; |
| 306 |
AWS_ZERO_ARRAY(hash); |
| 307 |
struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash)); |
| 308 |
aws_sha256_compute(allocator, &message_input, &hash_value, 0); |
| 309 |
|
| 310 |
size_t signature_size = aws_ecc_key_pair_signature_length(key_pair); |
| 311 |
|
| 312 |
struct aws_byte_buf signature_buf; |
| 313 |
AWS_ZERO_STRUCT(signature_buf); |
| 314 |
aws_byte_buf_init(&signature_buf, allocator, signature_size); |
| 315 |
|
| 316 |
struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value); |
| 317 |
ASSERT_SUCCESS(aws_ecc_key_pair_sign_message(key_pair, &hash_cur, &signature_buf)); |
| 318 |
|
| 319 |
struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature_buf); |
| 320 |
ASSERT_SUCCESS(aws_ecc_key_pair_verify_signature(key_pair, &hash_cur, &signature_cur)); |
| 321 |
|
| 322 |
aws_byte_buf_clean_up(&signature_buf); |
| 323 |
aws_ecc_key_pair_release(key_pair); |
| 324 |
|
| 325 |
aws_cal_library_clean_up(); |
| 326 |
|
| 327 |
return AWS_OP_SUCCESS; |
| 328 |
} |
| 329 |
|
| 330 |
static int s_ecdsa_p256_test_key_gen_fn(struct aws_allocator *allocator, void *ctx) { |
| 331 |
(void)ctx; |
| 332 |
|
| 333 |
return s_test_key_gen(allocator, AWS_CAL_ECDSA_P256); |
| 334 |
} |
| 335 |
|
| 336 |
AWS_TEST_CASE(ecdsa_p256_test_key_gen, s_ecdsa_p256_test_key_gen_fn) |
| 337 |
|
| 338 |
static int s_ecdsa_p384_test_key_gen_fn(struct aws_allocator *allocator, void *ctx) { |
| 339 |
(void)ctx; |
| 340 |
|
| 341 |
return s_test_key_gen(allocator, AWS_CAL_ECDSA_P384); |
| 342 |
} |
| 343 |
|
| 344 |
AWS_TEST_CASE(ecdsa_p384_test_key_gen, s_ecdsa_p384_test_key_gen_fn) |
| 345 |
|
| 346 |
static int s_test_key_gen_export(struct aws_allocator *allocator, enum aws_ecc_curve_name curve_name) { |
| 347 |
|
| 348 |
aws_cal_library_test_init(allocator); |
| 349 |
|
| 350 |
struct aws_ecc_key_pair *key_pair = aws_ecc_key_pair_new_generate_random(allocator, curve_name); |
| 351 |
|
| 352 |
struct aws_byte_cursor pub_x; |
| 353 |
struct aws_byte_cursor pub_y; |
| 354 |
|
| 355 |
aws_ecc_key_pair_get_public_key(key_pair, &pub_x, &pub_y); |
| 356 |
ASSERT_TRUE(pub_x.len > 0); |
| 357 |
ASSERT_TRUE(pub_y.len > 0); |
| 358 |
|
| 359 |
struct aws_byte_cursor priv_d; |
| 360 |
aws_ecc_key_pair_get_private_key(key_pair, &priv_d); |
| 361 |
ASSERT_TRUE(priv_d.len > 0); |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
size_t curve_private_key_length = aws_ecc_key_coordinate_byte_size_from_curve_name(curve_name); |
| 368 |
struct aws_byte_buf padded_priv_d; |
| 369 |
aws_byte_buf_init(&padded_priv_d, allocator, curve_private_key_length); |
| 370 |
aws_byte_buf_secure_zero(&padded_priv_d); |
| 371 |
|
| 372 |
if (priv_d.len < curve_private_key_length) { |
| 373 |
padded_priv_d.len = curve_private_key_length - priv_d.len; |
| 374 |
} |
| 375 |
|
| 376 |
ASSERT_SUCCESS(aws_byte_buf_append(&padded_priv_d, &priv_d)); |
| 377 |
|
| 378 |
struct aws_byte_cursor padded_priv_d_cursor = aws_byte_cursor_from_buf(&padded_priv_d); |
| 379 |
struct aws_ecc_key_pair *signing_key = |
| 380 |
aws_ecc_key_pair_new_from_private_key(allocator, curve_name, &padded_priv_d_cursor); |
| 381 |
ASSERT_NOT_NULL(signing_key); |
| 382 |
|
| 383 |
uint8_t message[] = { |
| 384 |
0x59, 0x05, 0x23, 0x88, 0x77, 0xc7, 0x74, 0x21, 0xf7, 0x3e, 0x43, 0xee, 0x3d, 0xa6, 0xf2, 0xd9, |
| 385 |
0xe2, 0xcc, 0xad, 0x5f, 0xc9, 0x42, 0xdc, 0xec, 0x0c, 0xbd, 0x25, 0x48, 0x29, 0x35, 0xfa, 0xaf, |
| 386 |
0x41, 0x69, 0x83, 0xfe, 0x16, 0x5b, 0x1a, 0x04, 0x5e, 0xe2, 0xbc, 0xd2, 0xe6, 0xdc, 0xa3, 0xbd, |
| 387 |
0xf4, 0x6c, 0x43, 0x10, 0xa7, 0x46, 0x1f, 0x9a, 0x37, 0x96, 0x0c, 0xa6, 0x72, 0xd3, 0xfe, 0xb5, |
| 388 |
0x47, 0x3e, 0x25, 0x36, 0x05, 0xfb, 0x1d, 0xdf, 0xd2, 0x80, 0x65, 0xb5, 0x3c, 0xb5, 0x85, 0x8a, |
| 389 |
0x8a, 0xd2, 0x81, 0x75, 0xbf, 0x9b, 0xd3, 0x86, 0xa5, 0xe4, 0x71, 0xea, 0x7a, 0x65, 0xc1, 0x7c, |
| 390 |
0xc9, 0x34, 0xa9, 0xd7, 0x91, 0xe9, 0x14, 0x91, 0xeb, 0x37, 0x54, 0xd0, 0x37, 0x99, 0x79, 0x0f, |
| 391 |
0xe2, 0xd3, 0x08, 0xd1, 0x61, 0x46, 0xd5, 0xc9, 0xb0, 0xd0, 0xde, 0xbd, 0x97, 0xd7, 0x9c, 0xe8, |
| 392 |
}; |
| 393 |
|
| 394 |
struct aws_byte_cursor message_input = aws_byte_cursor_from_array(message, sizeof(message)); |
| 395 |
uint8_t hash[AWS_SHA256_LEN]; |
| 396 |
AWS_ZERO_ARRAY(hash); |
| 397 |
struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash)); |
| 398 |
aws_sha256_compute(allocator, &message_input, &hash_value, 0); |
| 399 |
|
| 400 |
size_t signature_size = aws_ecc_key_pair_signature_length(key_pair); |
| 401 |
|
| 402 |
struct aws_byte_buf signature_buf; |
| 403 |
AWS_ZERO_STRUCT(signature_buf); |
| 404 |
aws_byte_buf_init(&signature_buf, allocator, signature_size); |
| 405 |
|
| 406 |
struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value); |
| 407 |
ASSERT_SUCCESS(aws_ecc_key_pair_sign_message(signing_key, &hash_cur, &signature_buf)); |
| 408 |
|
| 409 |
struct aws_ecc_key_pair *verifying_key = |
| 410 |
aws_ecc_key_pair_new_from_public_key(allocator, curve_name, &pub_x, &pub_y); |
| 411 |
ASSERT_NOT_NULL(verifying_key); |
| 412 |
|
| 413 |
struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature_buf); |
| 414 |
ASSERT_SUCCESS(aws_ecc_key_pair_verify_signature(verifying_key, &hash_cur, &signature_cur)); |
| 415 |
|
| 416 |
aws_byte_buf_clean_up(&signature_buf); |
| 417 |
aws_byte_buf_clean_up(&padded_priv_d); |
| 418 |
aws_ecc_key_pair_release(key_pair); |
| 419 |
aws_ecc_key_pair_release(signing_key); |
| 420 |
aws_ecc_key_pair_release(verifying_key); |
| 421 |
|
| 422 |
aws_cal_library_clean_up(); |
| 423 |
|
| 424 |
return AWS_OP_SUCCESS; |
| 425 |
} |
| 426 |
|
| 427 |
static int s_ecdsa_p256_test_key_gen_export_fn(struct aws_allocator *allocator, void *ctx) { |
| 428 |
(void)ctx; |
| 429 |
|
| 430 |
return s_test_key_gen_export(allocator, AWS_CAL_ECDSA_P256); |
| 431 |
} |
| 432 |
|
| 433 |
AWS_TEST_CASE(ecdsa_p256_test_key_gen_export, s_ecdsa_p256_test_key_gen_export_fn) |
| 434 |
|
| 435 |
static int s_ecdsa_p384_test_key_gen_export_fn(struct aws_allocator *allocator, void *ctx) { |
| 436 |
(void)ctx; |
| 437 |
|
| 438 |
return s_test_key_gen_export(allocator, AWS_CAL_ECDSA_P256); |
| 439 |
} |
| 440 |
|
| 441 |
AWS_TEST_CASE(ecdsa_p384_test_key_gen_export, s_ecdsa_p384_test_key_gen_export_fn) |
| 442 |
|
| 443 |
static int s_ecdsa_test_import_asn1_key_pair( |
| 444 |
struct aws_allocator *allocator, |
| 445 |
struct aws_byte_cursor asn1_cur, |
| 446 |
enum aws_ecc_curve_name expected_curve_name) { |
| 447 |
|
| 448 |
aws_cal_library_test_init(allocator); |
| 449 |
|
| 450 |
struct aws_ecc_key_pair *imported_key = aws_ecc_key_pair_new_from_asn1(allocator, &asn1_cur); |
| 451 |
ASSERT_NOT_NULL(imported_key); |
| 452 |
|
| 453 |
ASSERT_INT_EQUALS(expected_curve_name, imported_key->curve_name); |
| 454 |
|
| 455 |
uint8_t message[] = { |
| 456 |
0x59, 0x05, 0x23, 0x88, 0x77, 0xc7, 0x74, 0x21, 0xf7, 0x3e, 0x43, 0xee, 0x3d, 0xa6, 0xf2, 0xd9, |
| 457 |
0xe2, 0xcc, 0xad, 0x5f, 0xc9, 0x42, 0xdc, 0xec, 0x0c, 0xbd, 0x25, 0x48, 0x29, 0x35, 0xfa, 0xaf, |
| 458 |
0x41, 0x69, 0x83, 0xfe, 0x16, 0x5b, 0x1a, 0x04, 0x5e, 0xe2, 0xbc, 0xd2, 0xe6, 0xdc, 0xa3, 0xbd, |
| 459 |
0xf4, 0x6c, 0x43, 0x10, 0xa7, 0x46, 0x1f, 0x9a, 0x37, 0x96, 0x0c, 0xa6, 0x72, 0xd3, 0xfe, 0xb5, |
| 460 |
0x47, 0x3e, 0x25, 0x36, 0x05, 0xfb, 0x1d, 0xdf, 0xd2, 0x80, 0x65, 0xb5, 0x3c, 0xb5, 0x85, 0x8a, |
| 461 |
0x8a, 0xd2, 0x81, 0x75, 0xbf, 0x9b, 0xd3, 0x86, 0xa5, 0xe4, 0x71, 0xea, 0x7a, 0x65, 0xc1, 0x7c, |
| 462 |
0xc9, 0x34, 0xa9, 0xd7, 0x91, 0xe9, 0x14, 0x91, 0xeb, 0x37, 0x54, 0xd0, 0x37, 0x99, 0x79, 0x0f, |
| 463 |
0xe2, 0xd3, 0x08, 0xd1, 0x61, 0x46, 0xd5, 0xc9, 0xb0, 0xd0, 0xde, 0xbd, 0x97, 0xd7, 0x9c, 0xe8, |
| 464 |
}; |
| 465 |
struct aws_byte_cursor message_input = aws_byte_cursor_from_array(message, sizeof(message)); |
| 466 |
uint8_t hash[AWS_SHA256_LEN]; |
| 467 |
AWS_ZERO_ARRAY(hash); |
| 468 |
struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash)); |
| 469 |
aws_sha256_compute(allocator, &message_input, &hash_value, 0); |
| 470 |
|
| 471 |
size_t signature_size = aws_ecc_key_pair_signature_length(imported_key); |
| 472 |
|
| 473 |
struct aws_byte_buf signature_buf; |
| 474 |
AWS_ZERO_STRUCT(signature_buf); |
| 475 |
aws_byte_buf_init(&signature_buf, allocator, signature_size); |
| 476 |
|
| 477 |
struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value); |
| 478 |
ASSERT_SUCCESS(aws_ecc_key_pair_sign_message(imported_key, &hash_cur, &signature_buf)); |
| 479 |
|
| 480 |
struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature_buf); |
| 481 |
ASSERT_SUCCESS(aws_ecc_key_pair_verify_signature(imported_key, &hash_cur, &signature_cur)); |
| 482 |
|
| 483 |
aws_byte_buf_clean_up(&signature_buf); |
| 484 |
aws_ecc_key_pair_release(imported_key); |
| 485 |
|
| 486 |
aws_cal_library_clean_up(); |
| 487 |
|
| 488 |
return AWS_OP_SUCCESS; |
| 489 |
} |
| 490 |
|
| 491 |
static int s_ecdsa_p256_test_import_asn1_key_pair_fn(struct aws_allocator *allocator, void *ctx) { |
| 492 |
(void)ctx; |
| 493 |
|
| 494 |
uint8_t asn1_encoded_key_raw[] = { |
| 495 |
0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x78, 0xed, 0xed, 0xcf, 0x95, 0x9e, 0x42, 0x24, 0x37, 0xa4, 0x56, |
| 496 |
0xed, 0x08, 0x19, 0x3c, 0x53, 0x4b, 0x6f, 0xff, 0x40, 0x64, 0x48, 0x6a, 0x49, 0x86, 0x0c, 0xb7, 0x0a, 0xe5, |
| 497 |
0x2d, 0xbd, 0xd6, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, |
| 498 |
0x42, 0x00, 0x04, 0xbf, 0x61, 0x63, 0x46, 0x93, 0x2d, 0x00, 0x33, 0x19, 0xe3, 0x3a, 0x19, 0xc6, 0xc8, 0x55, |
| 499 |
0xf5, 0xc8, 0x44, 0x91, 0xe9, 0x9b, 0x83, 0x36, 0x67, 0x5d, 0x25, 0x0d, 0x7b, 0xe0, 0xc0, 0xf1, 0xd2, 0xaa, |
| 500 |
0x5c, 0xdf, 0xfb, 0xa9, 0x37, 0x19, 0x8d, 0x82, 0x47, 0x28, 0x88, 0xbe, 0x46, 0x7f, 0x3c, 0xcd, 0x41, 0xaa, |
| 501 |
0x08, 0x9a, 0x37, 0x0d, 0x61, 0x7f, 0x5f, 0xeb, 0x9f, 0x55, 0xf7, 0x54, 0xda, |
| 502 |
}; |
| 503 |
|
| 504 |
struct aws_byte_cursor asn1_encoded_key = |
| 505 |
aws_byte_cursor_from_array(asn1_encoded_key_raw, sizeof(asn1_encoded_key_raw)); |
| 506 |
|
| 507 |
return s_ecdsa_test_import_asn1_key_pair(allocator, asn1_encoded_key, AWS_CAL_ECDSA_P256); |
| 508 |
} |
| 509 |
AWS_TEST_CASE(ecdsa_p256_test_import_asn1_key_pair, s_ecdsa_p256_test_import_asn1_key_pair_fn) |
| 510 |
|
| 511 |
static int s_ecdsa_p256_test_import_asn1_pkcs8_key_pair_fn(struct aws_allocator *allocator, void *ctx) { |
| 512 |
(void)ctx; |
| 513 |
|
| 514 |
uint8_t asn1_encoded_key_raw[] = { |
| 515 |
0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, |
| 516 |
0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x04, 0x6D, 0x30, 0x6B, 0x02, 0x01, 0x01, 0x04, 0x20, |
| 517 |
0x18, 0xCD, 0x6B, 0x61, 0x25, 0xB5, 0x37, 0x61, 0xB8, 0xF4, 0x6A, 0xBA, 0x42, 0xAF, 0xA9, 0x70, 0x14, 0x9D, |
| 518 |
0x72, 0x40, 0x6D, 0x84, 0x00, 0xA8, 0x40, 0x02, 0x13, 0x86, 0x8C, 0xA9, 0x2D, 0x63, 0xA1, 0x44, 0x03, 0x42, |
| 519 |
0x00, 0x04, 0x95, 0x4E, 0x22, 0xE2, 0xDB, 0x79, 0xCC, 0xA7, 0x56, 0x6F, 0xC2, 0x29, 0xA1, 0x0B, 0x05, 0x96, |
| 520 |
0x22, 0x66, 0x06, 0xC3, 0x6D, 0x0A, 0xD1, 0xD8, 0x57, 0x82, 0x02, 0x19, 0x74, 0xCD, 0x52, 0x4D, 0x5A, 0x57, |
| 521 |
0x18, 0xCA, 0xDF, 0xF5, 0x81, 0x9B, 0x9D, 0x0C, 0xBF, 0x2E, 0xB9, 0x91, 0xD2, 0x1A, 0xBB, 0x76, 0x8A, 0x06, |
| 522 |
0x66, 0xB3, 0xBF, 0x7C, 0x6C, 0x30, 0xF6, 0xBF, 0x8C, 0x84, 0x73, 0x96, |
| 523 |
}; |
| 524 |
|
| 525 |
struct aws_byte_cursor asn1_encoded_key = |
| 526 |
aws_byte_cursor_from_array(asn1_encoded_key_raw, sizeof(asn1_encoded_key_raw)); |
| 527 |
|
| 528 |
return s_ecdsa_test_import_asn1_key_pair(allocator, asn1_encoded_key, AWS_CAL_ECDSA_P256); |
| 529 |
} |
| 530 |
AWS_TEST_CASE(ecdsa_p256_test_import_asn1_pkcs8_key_pair, s_ecdsa_p256_test_import_asn1_pkcs8_key_pair_fn) |
| 531 |
|
| 532 |
static int s_ecdsa_p384_test_import_asn1_key_pair_fn(struct aws_allocator *allocator, void *ctx) { |
| 533 |
(void)ctx; |
| 534 |
|
| 535 |
uint8_t asn1_encoded_key_raw[] = { |
| 536 |
0x30, 0x81, 0xa4, 0x02, 0x01, 0x01, 0x04, 0x30, 0xa4, 0x4e, 0x2c, 0xf8, 0x6a, 0xfd, 0x42, 0x0e, 0xd4, |
| 537 |
0xbb, 0x2d, 0x08, 0xe2, 0x35, 0xe7, 0xb2, 0xc7, 0x87, 0x37, 0xbc, 0x92, 0xc2, 0x9a, 0x84, 0x39, 0x99, |
| 538 |
0x24, 0xe3, 0xa3, 0x01, 0x8c, 0xa0, 0xc1, 0x34, 0xd7, 0x8d, 0x86, 0xa1, 0x8c, 0xe1, 0xe4, 0x3e, 0xd1, |
| 539 |
0xe3, 0xff, 0x8b, 0xa4, 0x1d, 0xa0, 0x07, 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x22, 0xa1, 0x64, 0x03, |
| 540 |
0x62, 0x00, 0x04, 0x89, 0x64, 0x99, 0x1e, 0x1e, 0xa3, 0x6e, 0x30, 0x5e, 0xb1, 0x00, 0xef, 0x51, 0x0e, |
| 541 |
0x78, 0xc6, 0x7a, 0x2f, 0x1d, 0x21, 0x65, 0xe2, 0x68, 0xfa, 0x22, 0x5f, 0x1c, 0x8e, 0x00, 0xdc, 0x74, |
| 542 |
0xa6, 0x97, 0x7d, 0x73, 0xb3, 0x05, 0x00, 0xfd, 0xf5, 0x52, 0x85, 0xd8, 0x81, 0x62, 0x6c, 0x0a, 0x04, |
| 543 |
0xf4, 0xaa, 0x39, 0xe0, 0x2b, 0x05, 0x29, 0xd1, 0x70, 0x8a, 0x42, 0x44, 0x9a, 0xe7, 0xed, 0xec, 0x3a, |
| 544 |
0x52, 0x06, 0x24, 0x67, 0x35, 0x27, 0x6b, 0x80, 0x7c, 0xda, 0xc5, 0xe9, 0x97, 0xfa, 0x1d, 0xd4, 0x0c, |
| 545 |
0x27, 0x54, 0xed, 0x97, 0xe6, 0xbd, 0xe0, 0xaf, 0xff, 0xaa, 0xb9, 0x63, 0xf3, 0x21, |
| 546 |
}; |
| 547 |
|
| 548 |
struct aws_byte_cursor asn1_encoded_key = |
| 549 |
aws_byte_cursor_from_array(asn1_encoded_key_raw, sizeof(asn1_encoded_key_raw)); |
| 550 |
|
| 551 |
return s_ecdsa_test_import_asn1_key_pair(allocator, asn1_encoded_key, AWS_CAL_ECDSA_P384); |
| 552 |
} |
| 553 |
|
| 554 |
AWS_TEST_CASE(ecdsa_p384_test_import_asn1_key_pair, s_ecdsa_p384_test_import_asn1_key_pair_fn) |
| 555 |
|
| 556 |
static int s_ecdsa_test_import_asn1_key_pair_public_only_fn(struct aws_allocator *allocator, void *ctx) { |
| 557 |
(void)ctx; |
| 558 |
|
| 559 |
aws_cal_library_test_init(allocator); |
| 560 |
|
| 561 |
uint8_t asn1_encoded_full_key_raw[] = { |
| 562 |
0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x99, 0x16, 0x2a, 0x5b, 0x4e, 0x63, 0x86, 0x4c, 0x5f, 0x8e, 0x37, |
| 563 |
0xf7, 0x2b, 0xbd, 0x97, 0x1d, 0x5c, 0x68, 0x80, 0x18, 0xc3, 0x91, 0x0f, 0xb3, 0xc3, 0xf9, 0x3a, 0xc9, 0x7a, |
| 564 |
0x4b, 0xa3, 0xf6, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, |
| 565 |
0x42, 0x00, 0x04, 0xec, 0x6c, 0xd7, 0x4b, 0xdc, 0x33, 0xc2, 0x56, 0x32, 0xad, 0x52, 0x56, 0xac, 0xf5, 0xf0, |
| 566 |
0xe6, 0x28, 0x99, 0x84, 0x83, 0xaf, 0x73, 0x6f, 0xfe, 0xd7, 0x83, 0x3b, 0x42, 0x81, 0x5d, 0x2e, 0xe0, 0xdb, |
| 567 |
0xf6, 0xac, 0xa4, 0xc6, 0x16, 0x7e, 0x3e, 0xe0, 0xff, 0x7b, 0x43, 0xe8, 0xa1, 0x36, 0x50, 0x92, 0x83, 0x06, |
| 568 |
0x94, 0xb3, 0xd4, 0x93, 0x06, 0xde, 0x63, 0x8a, 0xa1, 0x1c, 0x3f, 0xb2, 0x57, |
| 569 |
}; |
| 570 |
|
| 571 |
uint8_t asn1_encoded_pub_key_raw[] = { |
| 572 |
0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, |
| 573 |
0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xec, 0x6c, 0xd7, 0x4b, 0xdc, |
| 574 |
0x33, 0xc2, 0x56, 0x32, 0xad, 0x52, 0x56, 0xac, 0xf5, 0xf0, 0xe6, 0x28, 0x99, 0x84, 0x83, 0xaf, |
| 575 |
0x73, 0x6f, 0xfe, 0xd7, 0x83, 0x3b, 0x42, 0x81, 0x5d, 0x2e, 0xe0, 0xdb, 0xf6, 0xac, 0xa4, 0xc6, |
| 576 |
0x16, 0x7e, 0x3e, 0xe0, 0xff, 0x7b, 0x43, 0xe8, 0xa1, 0x36, 0x50, 0x92, 0x83, 0x06, 0x94, 0xb3, |
| 577 |
0xd4, 0x93, 0x06, 0xde, 0x63, 0x8a, 0xa1, 0x1c, 0x3f, 0xb2, 0x57, |
| 578 |
}; |
| 579 |
|
| 580 |
struct aws_byte_cursor full_key_asn1 = |
| 581 |
aws_byte_cursor_from_array(asn1_encoded_full_key_raw, sizeof(asn1_encoded_full_key_raw)); |
| 582 |
struct aws_byte_cursor pub_key_asn1 = |
| 583 |
aws_byte_cursor_from_array(asn1_encoded_pub_key_raw, sizeof(asn1_encoded_pub_key_raw)); |
| 584 |
|
| 585 |
struct aws_ecc_key_pair *signing_key = aws_ecc_key_pair_new_from_asn1(allocator, &full_key_asn1); |
| 586 |
ASSERT_NOT_NULL(signing_key); |
| 587 |
|
| 588 |
struct aws_ecc_key_pair *verifying_key = aws_ecc_key_pair_new_from_asn1(allocator, &pub_key_asn1); |
| 589 |
ASSERT_NOT_NULL(verifying_key); |
| 590 |
|
| 591 |
uint8_t message[] = { |
| 592 |
0x59, 0x05, 0x23, 0x88, 0x77, 0xc7, 0x74, 0x21, 0xf7, 0x3e, 0x43, 0xee, 0x3d, 0xa6, 0xf2, 0xd9, |
| 593 |
0xe2, 0xcc, 0xad, 0x5f, 0xc9, 0x42, 0xdc, 0xec, 0x0c, 0xbd, 0x25, 0x48, 0x29, 0x35, 0xfa, 0xaf, |
| 594 |
0x41, 0x69, 0x83, 0xfe, 0x16, 0x5b, 0x1a, 0x04, 0x5e, 0xe2, 0xbc, 0xd2, 0xe6, 0xdc, 0xa3, 0xbd, |
| 595 |
0xf4, 0x6c, 0x43, 0x10, 0xa7, 0x46, 0x1f, 0x9a, 0x37, 0x96, 0x0c, 0xa6, 0x72, 0xd3, 0xfe, 0xb5, |
| 596 |
0x47, 0x3e, 0x25, 0x36, 0x05, 0xfb, 0x1d, 0xdf, 0xd2, 0x80, 0x65, 0xb5, 0x3c, 0xb5, 0x85, 0x8a, |
| 597 |
0x8a, 0xd2, 0x81, 0x75, 0xbf, 0x9b, 0xd3, 0x86, 0xa5, 0xe4, 0x71, 0xea, 0x7a, 0x65, 0xc1, 0x7c, |
| 598 |
0xc9, 0x34, 0xa9, 0xd7, 0x91, 0xe9, 0x14, 0x91, 0xeb, 0x37, 0x54, 0xd0, 0x37, 0x99, 0x79, 0x0f, |
| 599 |
0xe2, 0xd3, 0x08, 0xd1, 0x61, 0x46, 0xd5, 0xc9, 0xb0, 0xd0, 0xde, 0xbd, 0x97, 0xd7, 0x9c, 0xe8, |
| 600 |
}; |
| 601 |
|
| 602 |
struct aws_byte_cursor message_input = aws_byte_cursor_from_array(message, sizeof(message)); |
| 603 |
uint8_t hash[AWS_SHA256_LEN]; |
| 604 |
AWS_ZERO_ARRAY(hash); |
| 605 |
struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash)); |
| 606 |
aws_sha256_compute(allocator, &message_input, &hash_value, 0); |
| 607 |
|
| 608 |
size_t signature_size = aws_ecc_key_pair_signature_length(signing_key); |
| 609 |
|
| 610 |
struct aws_byte_buf signature_buf; |
| 611 |
AWS_ZERO_STRUCT(signature_buf); |
| 612 |
aws_byte_buf_init(&signature_buf, allocator, signature_size); |
| 613 |
|
| 614 |
struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value); |
| 615 |
ASSERT_SUCCESS(aws_ecc_key_pair_sign_message(signing_key, &hash_cur, &signature_buf)); |
| 616 |
|
| 617 |
struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature_buf); |
| 618 |
ASSERT_SUCCESS(aws_ecc_key_pair_verify_signature(verifying_key, &hash_cur, &signature_cur)); |
| 619 |
|
| 620 |
aws_byte_buf_clean_up(&signature_buf); |
| 621 |
aws_ecc_key_pair_release(verifying_key); |
| 622 |
aws_ecc_key_pair_release(signing_key); |
| 623 |
|
| 624 |
aws_cal_library_clean_up(); |
| 625 |
|
| 626 |
return AWS_OP_SUCCESS; |
| 627 |
} |
| 628 |
|
| 629 |
AWS_TEST_CASE(ecdsa_test_import_asn1_key_pair_public_only, s_ecdsa_test_import_asn1_key_pair_public_only_fn) |
| 630 |
|
| 631 |
static int s_ecdsa_test_import_asn1_key_pair_invalid_fails_fn(struct aws_allocator *allocator, void *ctx) { |
| 632 |
(void)ctx; |
| 633 |
|
| 634 |
aws_cal_library_test_init(allocator); |
| 635 |
|
| 636 |
|
| 637 |
uint8_t bad_asn1_encoded_full_key_raw_1[] = { |
| 638 |
0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x99, 0x16, 0x2a, 0x5b, 0x4e, 0x63, 0x86, 0x4c, 0x5f, 0x8e, 0x37, |
| 639 |
0xf7, 0x2b, 0xbd, 0x97, 0x1d, 0x5c, 0x68, 0x80, 0x18, 0xc3, 0x91, 0x0f, 0xb3, 0xc3, 0xf9, 0x3a, 0xc9, 0x7a, |
| 640 |
0x4b, 0xa3, 0xf6, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x8a, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, |
| 641 |
0x42, 0x00, 0x04, 0xec, 0x6c, 0xd7, 0x4b, 0xdc, 0x33, 0xc2, 0x56, 0x32, 0xad, 0x52, 0x56, 0xac, 0xf5, 0xf0, |
| 642 |
0xe6, 0x28, 0x99, 0x84, 0x83, 0xaf, 0x73, 0x6f, 0xfe, 0xd7, 0x83, 0x3b, 0x42, 0x81, 0x5d, 0x2e, 0xe0, 0xdb, |
| 643 |
0xf6, 0xac, 0xa4, 0xc6, 0x16, 0x7e, 0x3e, 0xe0, 0xff, 0x7b, 0x43, 0xe8, 0xa1, 0x36, 0x50, 0x92, 0x83, 0x06, |
| 644 |
0x94, 0xb3, 0xd4, 0x93, 0x06, 0xde, 0x63, 0x8a, 0xa1, 0x1c, 0x3f, 0xb2, 0x57, |
| 645 |
}; |
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
uint8_t bad_asn1_encoded_full_key_raw_2[] = { |
| 650 |
0x2b, 0x20, 0x6, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 651 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x0, |
| 652 |
}; |
| 653 |
|
| 654 |
struct aws_byte_cursor bad_full_key_asn1_1 = |
| 655 |
aws_byte_cursor_from_array(bad_asn1_encoded_full_key_raw_1, sizeof(bad_asn1_encoded_full_key_raw_1)); |
| 656 |
|
| 657 |
struct aws_ecc_key_pair *signing_key = aws_ecc_key_pair_new_from_asn1(allocator, &bad_full_key_asn1_1); |
| 658 |
ASSERT_NULL(signing_key); |
| 659 |
ASSERT_INT_EQUALS(AWS_ERROR_CAL_UNSUPPORTED_KEY_FORMAT, aws_last_error()); |
| 660 |
|
| 661 |
struct aws_byte_cursor bad_full_key_asn1_2 = |
| 662 |
aws_byte_cursor_from_array(bad_asn1_encoded_full_key_raw_2, sizeof(bad_asn1_encoded_full_key_raw_2)); |
| 663 |
|
| 664 |
signing_key = aws_ecc_key_pair_new_from_asn1(allocator, &bad_full_key_asn1_2); |
| 665 |
ASSERT_NULL(signing_key); |
| 666 |
ASSERT_INT_EQUALS(AWS_ERROR_CAL_UNSUPPORTED_KEY_FORMAT, aws_last_error()); |
| 667 |
aws_cal_library_clean_up(); |
| 668 |
|
| 669 |
return AWS_OP_SUCCESS; |
| 670 |
} |
| 671 |
|
| 672 |
AWS_TEST_CASE(ecdsa_test_import_asn1_key_pair_invalid_fails, s_ecdsa_test_import_asn1_key_pair_invalid_fails_fn) |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
static int s_ecdsa_test_signature_format_fn(struct aws_allocator *allocator, void *ctx) { |
| 682 |
(void)ctx; |
| 683 |
|
| 684 |
aws_cal_library_test_init(allocator); |
| 685 |
|
| 686 |
uint8_t asn1_encoded_signature_raw[] = { |
| 687 |
0x30, 0x45, 0x02, 0x21, 0x00, 0xd7, 0xc5, 0xb9, 0x9e, 0x0b, 0xb1, 0x1a, 0x1f, 0x32, 0xda, 0x66, 0xe0, 0xff, |
| 688 |
0x59, 0xb7, 0x8a, 0x5e, 0xb3, 0x94, 0x9c, 0x23, 0xb3, 0xfc, 0x1f, 0x18, 0xcc, 0xf6, 0x61, 0x67, 0x8b, 0xf1, |
| 689 |
0xc1, 0x02, 0x20, 0x26, 0x4d, 0x8b, 0x7c, 0xaa, 0x52, 0x4c, 0xc0, 0x2e, 0x5f, 0xf6, 0x7e, 0x24, 0x82, 0xe5, |
| 690 |
0xfb, 0xcb, 0xc7, 0x9b, 0x83, 0x0d, 0x19, 0x7e, 0x7a, 0x40, 0x37, 0x87, 0xdd, 0x1c, 0x93, 0x13, 0xc4, |
| 691 |
}; |
| 692 |
|
| 693 |
uint8_t x[] = { |
| 694 |
0x1c, 0xcb, 0xe9, 0x1c, 0x07, 0x5f, 0xc7, 0xf4, 0xf0, 0x33, 0xbf, 0xa2, 0x48, 0xdb, 0x8f, 0xcc, |
| 695 |
0xd3, 0x56, 0x5d, 0xe9, 0x4b, 0xbf, 0xb1, 0x2f, 0x3c, 0x59, 0xff, 0x46, 0xc2, 0x71, 0xbf, 0x83, |
| 696 |
}; |
| 697 |
|
| 698 |
uint8_t y[] = { |
| 699 |
0xce, 0x40, 0x14, 0xc6, 0x88, 0x11, 0xf9, 0xa2, 0x1a, 0x1f, 0xdb, 0x2c, 0x0e, 0x61, 0x13, 0xe0, |
| 700 |
0x6d, 0xb7, 0xca, 0x93, 0xb7, 0x40, 0x4e, 0x78, 0xdc, 0x7c, 0xcd, 0x5c, 0xa8, 0x9a, 0x4c, 0xa9, |
| 701 |
}; |
| 702 |
|
| 703 |
struct aws_byte_cursor pub_x = aws_byte_cursor_from_array(x, sizeof(x)); |
| 704 |
struct aws_byte_cursor pub_y = aws_byte_cursor_from_array(y, sizeof(y)); |
| 705 |
struct aws_ecc_key_pair *verifying_key = |
| 706 |
aws_ecc_key_pair_new_from_public_key(allocator, AWS_CAL_ECDSA_P256, &pub_x, &pub_y); |
| 707 |
ASSERT_NOT_NULL(verifying_key); |
| 708 |
|
| 709 |
uint8_t message[] = { |
| 710 |
0x59, 0x05, 0x23, 0x88, 0x77, 0xc7, 0x74, 0x21, 0xf7, 0x3e, 0x43, 0xee, 0x3d, 0xa6, 0xf2, 0xd9, |
| 711 |
0xe2, 0xcc, 0xad, 0x5f, 0xc9, 0x42, 0xdc, 0xec, 0x0c, 0xbd, 0x25, 0x48, 0x29, 0x35, 0xfa, 0xaf, |
| 712 |
0x41, 0x69, 0x83, 0xfe, 0x16, 0x5b, 0x1a, 0x04, 0x5e, 0xe2, 0xbc, 0xd2, 0xe6, 0xdc, 0xa3, 0xbd, |
| 713 |
0xf4, 0x6c, 0x43, 0x10, 0xa7, 0x46, 0x1f, 0x9a, 0x37, 0x96, 0x0c, 0xa6, 0x72, 0xd3, 0xfe, 0xb5, |
| 714 |
0x47, 0x3e, 0x25, 0x36, 0x05, 0xfb, 0x1d, 0xdf, 0xd2, 0x80, 0x65, 0xb5, 0x3c, 0xb5, 0x85, 0x8a, |
| 715 |
0x8a, 0xd2, 0x81, 0x75, 0xbf, 0x9b, 0xd3, 0x86, 0xa5, 0xe4, 0x71, 0xea, 0x7a, 0x65, 0xc1, 0x7c, |
| 716 |
0xc9, 0x34, 0xa9, 0xd7, 0x91, 0xe9, 0x14, 0x91, 0xeb, 0x37, 0x54, 0xd0, 0x37, 0x99, 0x79, 0x0f, |
| 717 |
0xe2, 0xd3, 0x08, 0xd1, 0x61, 0x46, 0xd5, 0xc9, 0xb0, 0xd0, 0xde, 0xbd, 0x97, 0xd7, 0x9c, 0xe8, |
| 718 |
}; |
| 719 |
|
| 720 |
struct aws_byte_cursor message_input = aws_byte_cursor_from_array(message, sizeof(message)); |
| 721 |
uint8_t hash[AWS_SHA256_LEN]; |
| 722 |
AWS_ZERO_ARRAY(hash); |
| 723 |
struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash)); |
| 724 |
aws_sha256_compute(allocator, &message_input, &hash_value, 0); |
| 725 |
struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value); |
| 726 |
struct aws_byte_buf signature_buf = |
| 727 |
aws_byte_buf_from_array(asn1_encoded_signature_raw, sizeof(asn1_encoded_signature_raw)); |
| 728 |
|
| 729 |
struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature_buf); |
| 730 |
ASSERT_SUCCESS(aws_ecc_key_pair_verify_signature(verifying_key, &hash_cur, &signature_cur)); |
| 731 |
|
| 732 |
aws_ecc_key_pair_release(verifying_key); |
| 733 |
|
| 734 |
aws_cal_library_clean_up(); |
| 735 |
|
| 736 |
return AWS_OP_SUCCESS; |
| 737 |
} |
| 738 |
|
| 739 |
AWS_TEST_CASE(ecdsa_test_signature_format, s_ecdsa_test_signature_format_fn) |
| 740 |
|
| 741 |
enum aws_ecc_key_check_flags { |
| 742 |
AWS_ECC_KCF_PUBLIC = 1, |
| 743 |
AWS_ECC_KCF_PRIVATE = 2, |
| 744 |
}; |
| 745 |
|
| 746 |
static int s_check_cursor_data(struct aws_byte_cursor *cursor) { |
| 747 |
ASSERT_TRUE(cursor->ptr != NULL && cursor->len > 0 && (*cursor->ptr == 0 || *cursor->ptr != 0)); |
| 748 |
|
| 749 |
return AWS_OP_SUCCESS; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
static int s_test_key_ref_counting(struct aws_ecc_key_pair *key_pair, enum aws_ecc_key_check_flags flags) { |
| 757 |
|
| 758 |
aws_ecc_key_pair_acquire(key_pair); |
| 759 |
aws_ecc_key_pair_release(key_pair); |
| 760 |
aws_ecc_key_pair_acquire(key_pair); |
| 761 |
aws_ecc_key_pair_acquire(key_pair); |
| 762 |
aws_ecc_key_pair_release(key_pair); |
| 763 |
aws_ecc_key_pair_release(key_pair); |
| 764 |
|
| 765 |
if (flags & AWS_ECC_KCF_PRIVATE) { |
| 766 |
struct aws_byte_cursor private_key_cursor; |
| 767 |
AWS_ZERO_STRUCT(private_key_cursor); |
| 768 |
|
| 769 |
aws_ecc_key_pair_get_private_key(key_pair, &private_key_cursor); |
| 770 |
ASSERT_SUCCESS(s_check_cursor_data(&private_key_cursor)); |
| 771 |
} |
| 772 |
|
| 773 |
if (flags & AWS_ECC_KCF_PUBLIC) { |
| 774 |
struct aws_byte_cursor pub_x; |
| 775 |
AWS_ZERO_STRUCT(pub_x); |
| 776 |
struct aws_byte_cursor pub_y; |
| 777 |
AWS_ZERO_STRUCT(pub_y); |
| 778 |
|
| 779 |
aws_ecc_key_pair_get_public_key(key_pair, &pub_x, &pub_y); |
| 780 |
ASSERT_SUCCESS(s_check_cursor_data(&pub_x)); |
| 781 |
ASSERT_SUCCESS(s_check_cursor_data(&pub_y)); |
| 782 |
} |
| 783 |
|
| 784 |
aws_ecc_key_pair_release(key_pair); |
| 785 |
|
| 786 |
return AWS_OP_SUCCESS; |
| 787 |
} |
| 788 |
|
| 789 |
static int s_ecc_key_pair_random_ref_count_test(struct aws_allocator *allocator, void *ctx) { |
| 790 |
(void)ctx; |
| 791 |
|
| 792 |
aws_cal_library_test_init(allocator); |
| 793 |
|
| 794 |
struct aws_ecc_key_pair *key_pair = aws_ecc_key_pair_new_generate_random(allocator, AWS_CAL_ECDSA_P256); |
| 795 |
ASSERT_NOT_NULL(key_pair); |
| 796 |
|
| 797 |
int result = s_test_key_ref_counting(key_pair, AWS_ECC_KCF_PUBLIC | AWS_ECC_KCF_PRIVATE); |
| 798 |
|
| 799 |
aws_cal_library_clean_up(); |
| 800 |
|
| 801 |
return result; |
| 802 |
} |
| 803 |
|
| 804 |
AWS_TEST_CASE(ecc_key_pair_random_ref_count_test, s_ecc_key_pair_random_ref_count_test) |
| 805 |
|
| 806 |
static int s_ecc_key_pair_public_ref_count_test(struct aws_allocator *allocator, void *ctx) { |
| 807 |
(void)ctx; |
| 808 |
|
| 809 |
aws_cal_library_test_init(allocator); |
| 810 |
|
| 811 |
uint8_t x[] = { |
| 812 |
0x1c, 0xcb, 0xe9, 0x1c, 0x07, 0x5f, 0xc7, 0xf4, 0xf0, 0x33, 0xbf, 0xa2, 0x48, 0xdb, 0x8f, 0xcc, |
| 813 |
0xd3, 0x56, 0x5d, 0xe9, 0x4b, 0xbf, 0xb1, 0x2f, 0x3c, 0x59, 0xff, 0x46, 0xc2, 0x71, 0xbf, 0x83, |
| 814 |
}; |
| 815 |
|
| 816 |
uint8_t y[] = { |
| 817 |
0xce, 0x40, 0x14, 0xc6, 0x88, 0x11, 0xf9, 0xa2, 0x1a, 0x1f, 0xdb, 0x2c, 0x0e, 0x61, 0x13, 0xe0, |
| 818 |
0x6d, 0xb7, 0xca, 0x93, 0xb7, 0x40, 0x4e, 0x78, 0xdc, 0x7c, 0xcd, 0x5c, 0xa8, 0x9a, 0x4c, 0xa9, |
| 819 |
}; |
| 820 |
|
| 821 |
struct aws_byte_cursor pub_x = aws_byte_cursor_from_array(x, sizeof(x)); |
| 822 |
struct aws_byte_cursor pub_y = aws_byte_cursor_from_array(y, sizeof(y)); |
| 823 |
struct aws_ecc_key_pair *key_pair = |
| 824 |
aws_ecc_key_pair_new_from_public_key(allocator, AWS_CAL_ECDSA_P256, &pub_x, &pub_y); |
| 825 |
ASSERT_NOT_NULL(key_pair); |
| 826 |
|
| 827 |
int result = s_test_key_ref_counting(key_pair, AWS_ECC_KCF_PUBLIC); |
| 828 |
|
| 829 |
aws_cal_library_clean_up(); |
| 830 |
|
| 831 |
return result; |
| 832 |
} |
| 833 |
|
| 834 |
AWS_TEST_CASE(ecc_key_pair_public_ref_count_test, s_ecc_key_pair_public_ref_count_test) |
| 835 |
|
| 836 |
static int s_ecc_key_pair_asn1_ref_count_test(struct aws_allocator *allocator, void *ctx) { |
| 837 |
(void)ctx; |
| 838 |
|
| 839 |
aws_cal_library_test_init(allocator); |
| 840 |
|
| 841 |
uint8_t asn1_encoded_full_key_raw[] = { |
| 842 |
0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x99, 0x16, 0x2a, 0x5b, 0x4e, 0x63, 0x86, 0x4c, 0x5f, 0x8e, 0x37, |
| 843 |
0xf7, 0x2b, 0xbd, 0x97, 0x1d, 0x5c, 0x68, 0x80, 0x18, 0xc3, 0x91, 0x0f, 0xb3, 0xc3, 0xf9, 0x3a, 0xc9, 0x7a, |
| 844 |
0x4b, 0xa3, 0xf6, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, |
| 845 |
0x42, 0x00, 0x04, 0xec, 0x6c, 0xd7, 0x4b, 0xdc, 0x33, 0xc2, 0x56, 0x32, 0xad, 0x52, 0x56, 0xac, 0xf5, 0xf0, |
| 846 |
0xe6, 0x28, 0x99, 0x84, 0x83, 0xaf, 0x73, 0x6f, 0xfe, 0xd7, 0x83, 0x3b, 0x42, 0x81, 0x5d, 0x2e, 0xe0, 0xdb, |
| 847 |
0xf6, 0xac, 0xa4, 0xc6, 0x16, 0x7e, 0x3e, 0xe0, 0xff, 0x7b, 0x43, 0xe8, 0xa1, 0x36, 0x50, 0x92, 0x83, 0x06, |
| 848 |
0x94, 0xb3, 0xd4, 0x93, 0x06, 0xde, 0x63, 0x8a, 0xa1, 0x1c, 0x3f, 0xb2, 0x57, |
| 849 |
}; |
| 850 |
|
| 851 |
struct aws_byte_cursor full_key_asn1 = |
| 852 |
aws_byte_cursor_from_array(asn1_encoded_full_key_raw, sizeof(asn1_encoded_full_key_raw)); |
| 853 |
|
| 854 |
struct aws_ecc_key_pair *key_pair = aws_ecc_key_pair_new_from_asn1(allocator, &full_key_asn1); |
| 855 |
ASSERT_NOT_NULL(key_pair); |
| 856 |
|
| 857 |
int result = s_test_key_ref_counting(key_pair, AWS_ECC_KCF_PUBLIC | AWS_ECC_KCF_PRIVATE); |
| 858 |
|
| 859 |
aws_cal_library_clean_up(); |
| 860 |
|
| 861 |
return result; |
| 862 |
} |
| 863 |
|
| 864 |
AWS_TEST_CASE(ecc_key_pair_asn1_ref_count_test, s_ecc_key_pair_asn1_ref_count_test) |
| 865 |
|
| 866 |
static int s_ecc_key_pair_private_ref_count_test(struct aws_allocator *allocator, void *ctx) { |
| 867 |
(void)ctx; |
| 868 |
|
| 869 |
aws_cal_library_test_init(allocator); |
| 870 |
|
| 871 |
uint8_t d[] = { |
| 872 |
0xc9, 0x80, 0x68, 0x98, 0xa0, 0x33, 0x49, 0x16, 0xc8, 0x60, 0x74, 0x88, 0x80, 0xa5, 0x41, 0xf0, |
| 873 |
0x93, 0xb5, 0x79, 0xa9, 0xb1, 0xf3, 0x29, 0x34, 0xd8, 0x6c, 0x36, 0x3c, 0x39, 0x80, 0x03, 0x57, |
| 874 |
}; |
| 875 |
|
| 876 |
struct aws_byte_cursor private_key_cursor = aws_byte_cursor_from_array(d, sizeof(d)); |
| 877 |
|
| 878 |
struct aws_ecc_key_pair *key_pair = |
| 879 |
aws_ecc_key_pair_new_from_private_key(allocator, AWS_CAL_ECDSA_P256, &private_key_cursor); |
| 880 |
ASSERT_NOT_NULL(key_pair); |
| 881 |
|
| 882 |
int result = s_test_key_ref_counting(key_pair, AWS_ECC_KCF_PRIVATE); |
| 883 |
|
| 884 |
aws_cal_library_clean_up(); |
| 885 |
|
| 886 |
return result; |
| 887 |
} |
| 888 |
|
| 889 |
AWS_TEST_CASE(ecc_key_pair_private_ref_count_test, s_ecc_key_pair_private_ref_count_test) |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
AWS_STATIC_STRING_FROM_LITERAL(s_hex_message, "a8ddb188e516d365ba275c2b6d55ead851e89ab66f162adf29614f37cd3403c9"); |
| 897 |
|
| 898 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 899 |
s_signature_value, |
| 900 |
"3044021f7cfd51af2b722f8d1fa1afb65b4d5486ed59a67bcf9f3acc62aad6ddd37db10221009d4c9f9a37104fc01a8daffc9a6bd1056b7b43" |
| 901 |
"c1196edde0b52878b759628f8c"); |
| 902 |
|
| 903 |
AWS_STATIC_STRING_FROM_LITERAL(s_pub_x, "b6618f6a65740a99e650b33b6b4b5bd0d43b176d721a3edfea7e7d2d56d936b1"); |
| 904 |
|
| 905 |
AWS_STATIC_STRING_FROM_LITERAL(s_pub_y, "865ed22a7eadc9c5cb9d2cbaca1b3699139fedc5043dc6661864218330c8e518"); |
| 906 |
|
| 907 |
static int s_validate_message_signature( |
| 908 |
struct aws_allocator *allocator, |
| 909 |
struct aws_ecc_key_pair *ecc_key, |
| 910 |
struct aws_byte_cursor hex_message_cursor, |
| 911 |
struct aws_byte_cursor signature_value_cursor) { |
| 912 |
|
| 913 |
size_t binary_length = 0; |
| 914 |
if (aws_hex_compute_decoded_len(signature_value_cursor.len, &binary_length)) { |
| 915 |
return AWS_OP_ERR; |
| 916 |
} |
| 917 |
|
| 918 |
int result = AWS_OP_ERR; |
| 919 |
|
| 920 |
struct aws_byte_buf binary_signature; |
| 921 |
AWS_ZERO_STRUCT(binary_signature); |
| 922 |
|
| 923 |
struct aws_byte_buf message_buffer; |
| 924 |
AWS_ZERO_STRUCT(message_buffer); |
| 925 |
|
| 926 |
if (aws_byte_buf_init(&binary_signature, allocator, binary_length) || |
| 927 |
aws_byte_buf_init(&message_buffer, allocator, AWS_SHA256_LEN)) { |
| 928 |
goto done; |
| 929 |
} |
| 930 |
|
| 931 |
if (aws_hex_decode(&signature_value_cursor, &binary_signature)) { |
| 932 |
goto done; |
| 933 |
} |
| 934 |
|
| 935 |
if (aws_hex_decode(&hex_message_cursor, &message_buffer)) { |
| 936 |
goto done; |
| 937 |
} |
| 938 |
|
| 939 |
struct aws_byte_cursor binary_signature_cursor = |
| 940 |
aws_byte_cursor_from_array(binary_signature.buffer, binary_signature.len); |
| 941 |
struct aws_byte_cursor digest_cursor = aws_byte_cursor_from_buf(&message_buffer); |
| 942 |
if (aws_ecc_key_pair_verify_signature(ecc_key, &digest_cursor, &binary_signature_cursor)) { |
| 943 |
goto done; |
| 944 |
} |
| 945 |
|
| 946 |
result = AWS_OP_SUCCESS; |
| 947 |
|
| 948 |
done: |
| 949 |
|
| 950 |
aws_byte_buf_clean_up(&binary_signature); |
| 951 |
aws_byte_buf_clean_up(&message_buffer); |
| 952 |
|
| 953 |
return result; |
| 954 |
} |
| 955 |
|
| 956 |
static int s_ecdsa_p256_test_small_coordinate_verification(struct aws_allocator *allocator, void *ctx) { |
| 957 |
(void)ctx; |
| 958 |
aws_cal_library_test_init(allocator); |
| 959 |
|
| 960 |
struct aws_ecc_key_pair *key = aws_ecc_key_new_from_hex_coordinates( |
| 961 |
allocator, AWS_CAL_ECDSA_P256, aws_byte_cursor_from_string(s_pub_x), aws_byte_cursor_from_string(s_pub_y)); |
| 962 |
|
| 963 |
ASSERT_SUCCESS(s_validate_message_signature( |
| 964 |
allocator, key, aws_byte_cursor_from_string(s_hex_message), aws_byte_cursor_from_string(s_signature_value))); |
| 965 |
|
| 966 |
aws_ecc_key_pair_release(key); |
| 967 |
|
| 968 |
aws_cal_library_clean_up(); |
| 969 |
return AWS_OP_SUCCESS; |
| 970 |
} |
| 971 |
AWS_TEST_CASE(ecdsa_p256_test_small_coordinate_verification, s_ecdsa_p256_test_small_coordinate_verification); |
| 972 |
|
| 973 |
static int s_ecdsa_signature_encode_helper_roundtrip(struct aws_allocator *allocator, void *ctx) { |
| 974 |
(void)ctx; |
| 975 |
aws_cal_library_test_init(allocator); |
| 976 |
|
| 977 |
struct aws_byte_cursor signature_value_cursor = aws_byte_cursor_from_string(s_signature_value); |
| 978 |
size_t binary_length = 0; |
| 979 |
if (aws_hex_compute_decoded_len(signature_value_cursor.len, &binary_length)) { |
| 980 |
return AWS_OP_ERR; |
| 981 |
} |
| 982 |
struct aws_byte_buf binary_signature; |
| 983 |
AWS_ZERO_STRUCT(binary_signature); |
| 984 |
|
| 985 |
aws_byte_buf_init(&binary_signature, allocator, binary_length); |
| 986 |
|
| 987 |
ASSERT_SUCCESS(aws_hex_decode(&signature_value_cursor, &binary_signature)); |
| 988 |
|
| 989 |
struct aws_byte_cursor r; |
| 990 |
AWS_ZERO_STRUCT(r); |
| 991 |
struct aws_byte_cursor s; |
| 992 |
AWS_ZERO_STRUCT(s); |
| 993 |
struct aws_byte_cursor bin_cursor = aws_byte_cursor_from_buf(&binary_signature); |
| 994 |
ASSERT_SUCCESS(aws_ecc_decode_signature_der_to_raw(allocator, bin_cursor, &r, &s)); |
| 995 |
|
| 996 |
uint8_t expected_r[] = {0x7c, 0xfd, 0x51, 0xaf, 0x2b, 0x72, 0x2f, 0x8d, 0x1f, 0xa1, 0xaf, |
| 997 |
0xb6, 0x5b, 0x4d, 0x54, 0x86, 0xed, 0x59, 0xa6, 0x7b, 0xcf, 0x9f, |
| 998 |
0x3a, 0xcc, 0x62, 0xaa, 0xd6, 0xdd, 0xd3, 0x7d, 0xb1}; |
| 999 |
struct aws_byte_cursor expected_r_cur = aws_byte_cursor_from_array(expected_r, sizeof(expected_r)); |
| 1000 |
uint8_t expected_s[] = {0x9d, 0x4c, 0x9f, 0x9a, 0x37, 0x10, 0x4f, 0xc0, 0x1a, 0x8d, 0xaf, |
| 1001 |
0xfc, 0x9a, 0x6b, 0xd1, 0x05, 0x6b, 0x7b, 0x43, 0xc1, 0x19, 0x6e, |
| 1002 |
0xdd, 0xe0, 0xb5, 0x28, 0x78, 0xb7, 0x59, 0x62, 0x8f, 0x8c}; |
| 1003 |
struct aws_byte_cursor expected_s_cur = aws_byte_cursor_from_array(expected_s, sizeof(expected_s)); |
| 1004 |
|
| 1005 |
ASSERT_BIN_ARRAYS_EQUALS(expected_r_cur.ptr, expected_r_cur.len, r.ptr, r.len); |
| 1006 |
ASSERT_BIN_ARRAYS_EQUALS(expected_s_cur.ptr, expected_s_cur.len, s.ptr, s.len); |
| 1007 |
|
| 1008 |
struct aws_byte_buf encoded_sig; |
| 1009 |
aws_byte_buf_init(&encoded_sig, allocator, 128); |
| 1010 |
ASSERT_SUCCESS(aws_ecc_encode_signature_raw_to_der(allocator, r, s, &encoded_sig)); |
| 1011 |
|
| 1012 |
ASSERT_BIN_ARRAYS_EQUALS(binary_signature.buffer, binary_signature.len, encoded_sig.buffer, encoded_sig.len); |
| 1013 |
|
| 1014 |
aws_byte_buf_clean_up(&binary_signature); |
| 1015 |
aws_byte_buf_clean_up(&encoded_sig); |
| 1016 |
|
| 1017 |
aws_cal_library_clean_up(); |
| 1018 |
return AWS_OP_SUCCESS; |
| 1019 |
} |
| 1020 |
AWS_TEST_CASE(ecdsa_signature_encode_helper_roundtrip, s_ecdsa_signature_encode_helper_roundtrip); |
| 1021 |
|
| 1022 |
AWS_STATIC_STRING_FROM_LITERAL( |
| 1023 |
s_signature_value_short, |
| 1024 |
"3042021f2d2aadcebc1b3f78ecd112539ec0e3447b375f6a99ca0b27b54c31da0e6c5e021f47fb3dbdffb858f4ba8a03e7b483e6b8c946a80a" |
| 1025 |
"d846fa800ad8cac53f8ebd"); |
| 1026 |
|
| 1027 |
static int s_ecdsa_signature_decode_helper(struct aws_allocator *allocator, void *ctx) { |
| 1028 |
(void)ctx; |
| 1029 |
aws_cal_library_test_init(allocator); |
| 1030 |
|
| 1031 |
struct aws_byte_cursor signature_value_cursor = aws_byte_cursor_from_string(s_signature_value_short); |
| 1032 |
size_t binary_length = 0; |
| 1033 |
if (aws_hex_compute_decoded_len(signature_value_cursor.len, &binary_length)) { |
| 1034 |
return AWS_OP_ERR; |
| 1035 |
} |
| 1036 |
struct aws_byte_buf binary_signature; |
| 1037 |
AWS_ZERO_STRUCT(binary_signature); |
| 1038 |
|
| 1039 |
aws_byte_buf_init(&binary_signature, allocator, binary_length); |
| 1040 |
|
| 1041 |
ASSERT_SUCCESS(aws_hex_decode(&signature_value_cursor, &binary_signature)); |
| 1042 |
|
| 1043 |
struct aws_byte_cursor r; |
| 1044 |
AWS_ZERO_STRUCT(r); |
| 1045 |
struct aws_byte_cursor s; |
| 1046 |
AWS_ZERO_STRUCT(s); |
| 1047 |
struct aws_byte_cursor bin_cursor = aws_byte_cursor_from_buf(&binary_signature); |
| 1048 |
|
| 1049 |
uint8_t decoded[64] = {0}; |
| 1050 |
struct aws_byte_buf decoded_buf = aws_byte_buf_from_empty_array(decoded, 64); |
| 1051 |
|
| 1052 |
ASSERT_SUCCESS(aws_ecc_decode_signature_der_to_raw_padded(allocator, bin_cursor, &decoded_buf, 32)); |
| 1053 |
|
| 1054 |
uint8_t expected_out[] = {0x00, 0x2d, 0x2a, 0xad, 0xce, 0xbc, 0x1b, 0x3f, 0x78, 0xec, 0xd1, 0x12, 0x53, |
| 1055 |
0x9e, 0xc0, 0xe3, 0x44, 0x7b, 0x37, 0x5f, 0x6a, 0x99, 0xca, 0x0b, 0x27, 0xb5, |
| 1056 |
0x4c, 0x31, 0xda, 0x0e, 0x6c, 0x5e, 0x00, 0x47, 0xfb, 0x3d, 0xbd, 0xff, 0xb8, |
| 1057 |
0x58, 0xf4, 0xba, 0x8a, 0x03, 0xe7, 0xb4, 0x83, 0xe6, 0xb8, 0xc9, 0x46, 0xa8, |
| 1058 |
0x0a, 0xd8, 0x46, 0xfa, 0x80, 0x0a, 0xd8, 0xca, 0xc5, 0x3f, 0x8e, 0xbd}; |
| 1059 |
struct aws_byte_cursor expected_out_cur = aws_byte_cursor_from_array(expected_out, sizeof(expected_out)); |
| 1060 |
|
| 1061 |
ASSERT_BIN_ARRAYS_EQUALS(expected_out_cur.ptr, expected_out_cur.len, decoded_buf.buffer, decoded_buf.len); |
| 1062 |
|
| 1063 |
aws_byte_buf_clean_up(&decoded_buf); |
| 1064 |
aws_byte_buf_clean_up(&binary_signature); |
| 1065 |
|
| 1066 |
aws_cal_library_clean_up(); |
| 1067 |
return AWS_OP_SUCCESS; |
| 1068 |
} |
| 1069 |
AWS_TEST_CASE(ecdsa_signature_decode_helper, s_ecdsa_signature_decode_helper); |
| 1070 |
|
| 1071 |
static int s_ecdsa_export_sec1_roundtrip(struct aws_allocator *allocator, void *ctx) { |
| 1072 |
(void)ctx; |
| 1073 |
aws_cal_library_test_init(allocator); |
| 1074 |
|
| 1075 |
uint8_t asn1_encoded_key_raw[] = { |
| 1076 |
0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x78, 0xed, 0xed, 0xcf, 0x95, 0x9e, 0x42, 0x24, 0x37, 0xa4, 0x56, |
| 1077 |
0xed, 0x08, 0x19, 0x3c, 0x53, 0x4b, 0x6f, 0xff, 0x40, 0x64, 0x48, 0x6a, 0x49, 0x86, 0x0c, 0xb7, 0x0a, 0xe5, |
| 1078 |
0x2d, 0xbd, 0xd6, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, |
| 1079 |
0x42, 0x00, 0x04, 0xbf, 0x61, 0x63, 0x46, 0x93, 0x2d, 0x00, 0x33, 0x19, 0xe3, 0x3a, 0x19, 0xc6, 0xc8, 0x55, |
| 1080 |
0xf5, 0xc8, 0x44, 0x91, 0xe9, 0x9b, 0x83, 0x36, 0x67, 0x5d, 0x25, 0x0d, 0x7b, 0xe0, 0xc0, 0xf1, 0xd2, 0xaa, |
| 1081 |
0x5c, 0xdf, 0xfb, 0xa9, 0x37, 0x19, 0x8d, 0x82, 0x47, 0x28, 0x88, 0xbe, 0x46, 0x7f, 0x3c, 0xcd, 0x41, 0xaa, |
| 1082 |
0x08, 0x9a, 0x37, 0x0d, 0x61, 0x7f, 0x5f, 0xeb, 0x9f, 0x55, 0xf7, 0x54, 0xda, |
| 1083 |
}; |
| 1084 |
|
| 1085 |
struct aws_byte_cursor asn1_encoded_key = |
| 1086 |
aws_byte_cursor_from_array(asn1_encoded_key_raw, sizeof(asn1_encoded_key_raw)); |
| 1087 |
|
| 1088 |
struct aws_ecc_key_pair *imported_key = aws_ecc_key_pair_new_from_asn1(allocator, &asn1_encoded_key); |
| 1089 |
ASSERT_NOT_NULL(imported_key); |
| 1090 |
|
| 1091 |
struct aws_byte_buf encoded_buf; |
| 1092 |
aws_byte_buf_init(&encoded_buf, allocator, 256); |
| 1093 |
ASSERT_SUCCESS(aws_ecc_key_pair_export(imported_key, AWS_CAL_ECC_KEY_EXPORT_PRIVATE_SEC1, &encoded_buf)); |
| 1094 |
|
| 1095 |
ASSERT_BIN_ARRAYS_EQUALS(asn1_encoded_key.ptr, asn1_encoded_key.len, encoded_buf.buffer, encoded_buf.len); |
| 1096 |
|
| 1097 |
aws_byte_buf_clean_up(&encoded_buf); |
| 1098 |
aws_ecc_key_pair_release(imported_key); |
| 1099 |
|
| 1100 |
aws_cal_library_clean_up(); |
| 1101 |
return AWS_OP_SUCCESS; |
| 1102 |
} |
| 1103 |
AWS_TEST_CASE(ecdsa_export_sec1_roundtrip, s_ecdsa_export_sec1_roundtrip); |
| 1104 |
|
| 1105 |
static int s_ecdsa_export_pkcs8_roundtrip(struct aws_allocator *allocator, void *ctx) { |
| 1106 |
(void)ctx; |
| 1107 |
aws_cal_library_test_init(allocator); |
| 1108 |
|
| 1109 |
uint8_t asn1_encoded_key_raw[] = { |
| 1110 |
0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, |
| 1111 |
0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x04, 0x6D, 0x30, 0x6B, 0x02, 0x01, 0x01, 0x04, 0x20, |
| 1112 |
0x18, 0xCD, 0x6B, 0x61, 0x25, 0xB5, 0x37, 0x61, 0xB8, 0xF4, 0x6A, 0xBA, 0x42, 0xAF, 0xA9, 0x70, 0x14, 0x9D, |
| 1113 |
0x72, 0x40, 0x6D, 0x84, 0x00, 0xA8, 0x40, 0x02, 0x13, 0x86, 0x8C, 0xA9, 0x2D, 0x63, 0xA1, 0x44, 0x03, 0x42, |
| 1114 |
0x00, 0x04, 0x95, 0x4E, 0x22, 0xE2, 0xDB, 0x79, 0xCC, 0xA7, 0x56, 0x6F, 0xC2, 0x29, 0xA1, 0x0B, 0x05, 0x96, |
| 1115 |
0x22, 0x66, 0x06, 0xC3, 0x6D, 0x0A, 0xD1, 0xD8, 0x57, 0x82, 0x02, 0x19, 0x74, 0xCD, 0x52, 0x4D, 0x5A, 0x57, |
| 1116 |
0x18, 0xCA, 0xDF, 0xF5, 0x81, 0x9B, 0x9D, 0x0C, 0xBF, 0x2E, 0xB9, 0x91, 0xD2, 0x1A, 0xBB, 0x76, 0x8A, 0x06, |
| 1117 |
0x66, 0xB3, 0xBF, 0x7C, 0x6C, 0x30, 0xF6, 0xBF, 0x8C, 0x84, 0x73, 0x96, |
| 1118 |
}; |
| 1119 |
|
| 1120 |
struct aws_byte_cursor asn1_encoded_key = |
| 1121 |
aws_byte_cursor_from_array(asn1_encoded_key_raw, sizeof(asn1_encoded_key_raw)); |
| 1122 |
|
| 1123 |
struct aws_ecc_key_pair *imported_key = aws_ecc_key_pair_new_from_asn1(allocator, &asn1_encoded_key); |
| 1124 |
ASSERT_NOT_NULL(imported_key); |
| 1125 |
|
| 1126 |
struct aws_byte_buf encoded_buf; |
| 1127 |
aws_byte_buf_init(&encoded_buf, allocator, 256); |
| 1128 |
ASSERT_SUCCESS(aws_ecc_key_pair_export(imported_key, AWS_CAL_ECC_KEY_EXPORT_PRIVATE_PKCS8, &encoded_buf)); |
| 1129 |
|
| 1130 |
ASSERT_BIN_ARRAYS_EQUALS(asn1_encoded_key.ptr, asn1_encoded_key.len, encoded_buf.buffer, encoded_buf.len); |
| 1131 |
|
| 1132 |
aws_byte_buf_clean_up(&encoded_buf); |
| 1133 |
aws_ecc_key_pair_release(imported_key); |
| 1134 |
|
| 1135 |
aws_cal_library_clean_up(); |
| 1136 |
return AWS_OP_SUCCESS; |
| 1137 |
} |
| 1138 |
AWS_TEST_CASE(ecdsa_export_pkcs8_roundtrip, s_ecdsa_export_pkcs8_roundtrip); |
| 1139 |
|
| 1140 |
static int s_ecdsa_export_spki_roundtrip(struct aws_allocator *allocator, void *ctx) { |
| 1141 |
(void)ctx; |
| 1142 |
aws_cal_library_test_init(allocator); |
| 1143 |
|
| 1144 |
uint8_t asn1_encoded_key_raw[] = { |
| 1145 |
0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, |
| 1146 |
0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xec, 0x6c, 0xd7, 0x4b, 0xdc, |
| 1147 |
0x33, 0xc2, 0x56, 0x32, 0xad, 0x52, 0x56, 0xac, 0xf5, 0xf0, 0xe6, 0x28, 0x99, 0x84, 0x83, 0xaf, |
| 1148 |
0x73, 0x6f, 0xfe, 0xd7, 0x83, 0x3b, 0x42, 0x81, 0x5d, 0x2e, 0xe0, 0xdb, 0xf6, 0xac, 0xa4, 0xc6, |
| 1149 |
0x16, 0x7e, 0x3e, 0xe0, 0xff, 0x7b, 0x43, 0xe8, 0xa1, 0x36, 0x50, 0x92, 0x83, 0x06, 0x94, 0xb3, |
| 1150 |
0xd4, 0x93, 0x06, 0xde, 0x63, 0x8a, 0xa1, 0x1c, 0x3f, 0xb2, 0x57, |
| 1151 |
}; |
| 1152 |
|
| 1153 |
struct aws_byte_cursor asn1_encoded_key = |
| 1154 |
aws_byte_cursor_from_array(asn1_encoded_key_raw, sizeof(asn1_encoded_key_raw)); |
| 1155 |
|
| 1156 |
struct aws_ecc_key_pair *imported_key = aws_ecc_key_pair_new_from_asn1(allocator, &asn1_encoded_key); |
| 1157 |
ASSERT_NOT_NULL(imported_key); |
| 1158 |
|
| 1159 |
struct aws_byte_buf encoded_buf; |
| 1160 |
aws_byte_buf_init(&encoded_buf, allocator, 256); |
| 1161 |
ASSERT_SUCCESS(aws_ecc_key_pair_export(imported_key, AWS_CAL_ECC_KEY_EXPORT_PUBLIC_SPKI, &encoded_buf)); |
| 1162 |
|
| 1163 |
ASSERT_BIN_ARRAYS_EQUALS(asn1_encoded_key.ptr, asn1_encoded_key.len, encoded_buf.buffer, encoded_buf.len); |
| 1164 |
|
| 1165 |
aws_byte_buf_clean_up(&encoded_buf); |
| 1166 |
aws_ecc_key_pair_release(imported_key); |
| 1167 |
|
| 1168 |
aws_cal_library_clean_up(); |
| 1169 |
return AWS_OP_SUCCESS; |
| 1170 |
} |
| 1171 |
AWS_TEST_CASE(ecdsa_export_spki_roundtrip, s_ecdsa_export_spki_roundtrip); |
| 1172 |
|
| 1173 |
#ifdef AWS_OS_APPLE |
| 1174 |
|
| 1175 |
static int s_test_key_gen_from_private_fuzz( |
| 1176 |
struct aws_allocator *allocator, |
| 1177 |
enum aws_ecc_curve_name curve_name, |
| 1178 |
size_t number_loop) { |
| 1179 |
|
| 1180 |
uint8_t message[] = { |
| 1181 |
0x59, 0x05, 0x23, 0x88, 0x77, 0xc7, 0x74, 0x21, 0xf7, 0x3e, 0x43, 0xee, 0x3d, 0xa6, 0xf2, 0xd9, |
| 1182 |
0xe2, 0xcc, 0xad, 0x5f, 0xc9, 0x42, 0xdc, 0xec, 0x0c, 0xbd, 0x25, 0x48, 0x29, 0x35, 0xfa, 0xaf, |
| 1183 |
0x41, 0x69, 0x83, 0xfe, 0x16, 0x5b, 0x1a, 0x04, 0x5e, 0xe2, 0xbc, 0xd2, 0xe6, 0xdc, 0xa3, 0xbd, |
| 1184 |
0xf4, 0x6c, 0x43, 0x10, 0xa7, 0x46, 0x1f, 0x9a, 0x37, 0x96, 0x0c, 0xa6, 0x72, 0xd3, 0xfe, 0xb5, |
| 1185 |
0x47, 0x3e, 0x25, 0x36, 0x05, 0xfb, 0x1d, 0xdf, 0xd2, 0x80, 0x65, 0xb5, 0x3c, 0xb5, 0x85, 0x8a, |
| 1186 |
0x8a, 0xd2, 0x81, 0x75, 0xbf, 0x9b, 0xd3, 0x86, 0xa5, 0xe4, 0x71, 0xea, 0x7a, 0x65, 0xc1, 0x7c, |
| 1187 |
0xc9, 0x34, 0xa9, 0xd7, 0x91, 0xe9, 0x14, 0x91, 0xeb, 0x37, 0x54, 0xd0, 0x37, 0x99, 0x79, 0x0f, |
| 1188 |
0xe2, 0xd3, 0x08, 0xd1, 0x61, 0x46, 0xd5, 0xc9, 0xb0, 0xd0, 0xde, 0xbd, 0x97, 0xd7, 0x9c, 0xe8, |
| 1189 |
}; |
| 1190 |
struct aws_byte_cursor message_input = aws_byte_cursor_from_array(message, sizeof(message)); |
| 1191 |
uint8_t hash[AWS_SHA256_LEN]; |
| 1192 |
AWS_ZERO_ARRAY(hash); |
| 1193 |
struct aws_byte_buf hash_value = aws_byte_buf_from_empty_array(hash, sizeof(hash)); |
| 1194 |
struct aws_byte_cursor hash_cur = aws_byte_cursor_from_buf(&hash_value); |
| 1195 |
aws_sha256_compute(allocator, &message_input, &hash_value, 0); |
| 1196 |
|
| 1197 |
for (size_t i = 0; i < number_loop; i++) { |
| 1198 |
struct aws_ecc_key_pair *key_pair = aws_ecc_key_pair_new_generate_random(allocator, curve_name); |
| 1199 |
struct aws_byte_cursor priv_d; |
| 1200 |
aws_ecc_key_pair_get_private_key(key_pair, &priv_d); |
| 1201 |
ASSERT_TRUE(priv_d.len > 0); |
| 1202 |
|
| 1203 |
struct aws_ecc_key_pair *key_pair_private = |
| 1204 |
aws_ecc_key_pair_new_from_private_key(allocator, curve_name, &priv_d); |
| 1205 |
ASSERT_NOT_NULL(key_pair_private); |
| 1206 |
struct aws_byte_cursor pub_x; |
| 1207 |
struct aws_byte_cursor pub_y; |
| 1208 |
aws_ecc_key_pair_get_public_key(key_pair_private, &pub_x, &pub_y); |
| 1209 |
ASSERT_UINT_EQUALS(0, pub_x.len); |
| 1210 |
ASSERT_UINT_EQUALS(0, pub_y.len); |
| 1211 |
|
| 1212 |
size_t signature_size = aws_ecc_key_pair_signature_length(key_pair_private); |
| 1213 |
|
| 1214 |
struct aws_byte_buf signature_buf; |
| 1215 |
AWS_ZERO_STRUCT(signature_buf); |
| 1216 |
aws_byte_buf_init(&signature_buf, allocator, signature_size); |
| 1217 |
|
| 1218 |
|
| 1219 |
ASSERT_SUCCESS(aws_ecc_key_pair_sign_message(key_pair_private, &hash_cur, &signature_buf)); |
| 1220 |
struct aws_byte_cursor signature_cur = aws_byte_cursor_from_buf(&signature_buf); |
| 1221 |
ASSERT_SUCCESS(aws_ecc_key_pair_verify_signature(key_pair, &hash_cur, &signature_cur)); |
| 1222 |
|
| 1223 |
aws_ecc_key_pair_release(key_pair); |
| 1224 |
aws_ecc_key_pair_release(key_pair_private); |
| 1225 |
aws_byte_buf_clean_up(&signature_buf); |
| 1226 |
} |
| 1227 |
aws_byte_buf_clean_up(&hash_value); |
| 1228 |
|
| 1229 |
return AWS_OP_SUCCESS; |
| 1230 |
} |
| 1231 |
|
| 1232 |
static int s_ecc_key_gen_from_private_fuzz_test(struct aws_allocator *allocator, void *ctx) { |
| 1233 |
(void)ctx; |
| 1234 |
|
| 1235 |
aws_cal_library_test_init(allocator); |
| 1236 |
ASSERT_SUCCESS(s_test_key_gen_from_private_fuzz(allocator, AWS_CAL_ECDSA_P256, 1000)); |
| 1237 |
ASSERT_SUCCESS(s_test_key_gen_from_private_fuzz(allocator, AWS_CAL_ECDSA_P384, 1000)); |
| 1238 |
aws_cal_library_clean_up(); |
| 1239 |
return AWS_OP_SUCCESS; |
| 1240 |
} |
| 1241 |
|
| 1242 |
AWS_TEST_CASE(ecc_key_gen_from_private_fuzz_test, s_ecc_key_gen_from_private_fuzz_test) |
| 1243 |
|
| 1244 |
#else |
| 1245 |
|
| 1246 |
static int s_ecc_key_gen_from_private_fuzz_test(struct aws_allocator *allocator, void *ctx) { |
| 1247 |
(void)ctx; |
| 1248 |
(void)allocator; |
| 1249 |
return AWS_OP_SUCCESS; |
| 1250 |
} |
| 1251 |
|
| 1252 |
AWS_TEST_CASE(ecc_key_gen_from_private_fuzz_test, s_ecc_key_gen_from_private_fuzz_test) |
| 1253 |
#endif |