diff options
author | Johan Hedberg <johan.hedberg@intel.com> | 2014-05-30 18:38:06 +0300 |
---|---|---|
committer | Johan Hedberg <johan.hedberg@intel.com> | 2014-12-03 16:32:47 +0200 |
commit | c8293fdbf16e4efe1428d21dd6e25de0acef0e58 (patch) | |
tree | 361ff3cca69a92bb0ea5684df5c7eb82af63a516 /unit/test-ecc.c | |
parent | 19e6c21ab62e9ac03d98093d210a1ac84b854c7b (diff) | |
download | bluez-c8293fdbf16e4efe1428d21dd6e25de0acef0e58.tar.gz |
unit/test-ecc: Add sample data from core spec
Diffstat (limited to 'unit/test-ecc.c')
-rw-r--r-- | unit/test-ecc.c | 151 |
1 files changed, 149 insertions, 2 deletions
diff --git a/unit/test-ecc.c b/unit/test-ecc.c index 59475f4a4..da7470618 100644 --- a/unit/test-ecc.c +++ b/unit/test-ecc.c @@ -48,7 +48,7 @@ static void vli_print(uint8_t *vli, size_t size) #define PAIR_COUNT 200 -static void test_basic(void) +static void test_multi(void) { uint8_t public1[64], public2[64]; uint8_t private1[32], private2[32]; @@ -88,11 +88,158 @@ static void test_basic(void) printf("\n"); } +static void print_buf(const char *label, uint8_t *buf, size_t len) +{ + size_t i; + + printf("%s ", label); + + for (i = 0; i < len; i++) + printf("%02x", buf[i]); + + printf("\n"); +} + +static void test_sample_1(void) +{ + uint8_t priv_a[32] = { 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58, + 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a, + 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74, + 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f, + }; + uint8_t priv_b[32] = { 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b, + 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59, + 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90, + 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55, + }; + uint8_t pub_a[64] = { 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc, + 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef, + 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e, + 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20, + + 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74, + 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76, + 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63, + 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc, + }; + uint8_t pub_b[64] = { 0x90, 0xa1, 0xaa, 0x2f, 0xb2, 0x77, 0x90, 0x55, + 0x9f, 0xa6, 0x15, 0x86, 0xfd, 0x8a, 0xb5, 0x47, + 0x00, 0x4c, 0x9e, 0xf1, 0x84, 0x22, 0x59, 0x09, + 0x96, 0x1d, 0xaf, 0x1f, 0xf0, 0xf0, 0xa1, 0x1e, + + 0x4a, 0x21, 0xb1, 0x15, 0xf9, 0xaf, 0x89, 0x5f, + 0x76, 0x36, 0x8e, 0xe2, 0x30, 0x11, 0x2d, 0x47, + 0x60, 0x51, 0xb8, 0x9a, 0x3a, 0x70, 0x56, 0x73, + 0x37, 0xad, 0x9d, 0x42, 0x3e, 0xf3, 0x55, 0x4c, + }; + uint8_t dhkey[32] = { 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86, + 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99, + 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34, + 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec, + }; + uint8_t dhkey_a[32], dhkey_b[32]; + int fails = 0; + + ecdh_shared_secret(pub_b, priv_a, dhkey_a); + ecdh_shared_secret(pub_a, priv_b, dhkey_b); + + printf("\n"); + + print_buf("DHKey ", dhkey, 32); + print_buf("DHKey A", dhkey_a, 32); + print_buf("DHKey B", dhkey_b, 32); + + if (memcmp(dhkey_a, dhkey, 32)) { + printf("DHKey A doesn't match!\n"); + fails++; + } else { + printf("DHKey A matches :)\n"); + } + + if (memcmp(dhkey_b, dhkey, 32)) { + printf("DHKey B doesn't match!\n"); + fails++; + } else { + printf("DHKey B matches :)\n"); + } + + g_assert(fails == 0); +} + +static void test_sample_2(void) +{ + uint8_t priv_a[32] = { 0x63, 0x76, 0x45, 0xd0, 0xf7, 0x73, 0xac, 0xb7, + 0xff, 0xdd, 0x03, 0x72, 0xb9, 0x72, 0x85, 0xb4, + 0x41, 0xb6, 0x5d, 0x0c, 0x5d, 0x54, 0x84, 0x60, + 0x1a, 0xa3, 0x9a, 0x3c, 0x69, 0x16, 0xa5, 0x06, + }; + uint8_t priv_b[32] = { 0xba, 0x30, 0x55, 0x50, 0x19, 0xa2, 0xca, 0xa3, + 0xa5, 0x29, 0x08, 0xc6, 0xb5, 0x03, 0x88, 0x7e, + 0x03, 0x2b, 0x50, 0x73, 0xd4, 0x2e, 0x50, 0x97, + 0x64, 0xcd, 0x72, 0x0d, 0x67, 0xa0, 0x9a, 0x52, + }; + uint8_t pub_a[64] = { 0xdd, 0x78, 0x5c, 0x74, 0x03, 0x9b, 0x7e, 0x98, + 0xcb, 0x94, 0x87, 0x4a, 0xad, 0xfa, 0xf8, 0xd5, + 0x43, 0x3e, 0x5c, 0xaf, 0xea, 0xb5, 0x4c, 0xf4, + 0x9e, 0x80, 0x79, 0x57, 0x7b, 0xa4, 0x31, 0x2c, + + 0x4f, 0x5d, 0x71, 0x43, 0x77, 0x43, 0xf8, 0xea, + 0xd4, 0x3e, 0xbd, 0x17, 0x91, 0x10, 0x21, 0xd0, + 0x1f, 0x87, 0x43, 0x8e, 0x40, 0xe2, 0x52, 0xcd, + 0xbe, 0xdf, 0x98, 0x38, 0x18, 0x12, 0x95, 0x91, + }; + uint8_t pub_b[64] = { 0xcc, 0x00, 0x65, 0xe1, 0xf5, 0x6c, 0x0d, 0xcf, + 0xec, 0x96, 0x47, 0x20, 0x66, 0xc9, 0xdb, 0x84, + 0x81, 0x75, 0xa8, 0x4d, 0xc0, 0xdf, 0xc7, 0x9d, + 0x1b, 0x3f, 0x3d, 0xf2, 0x3f, 0xe4, 0x65, 0xf4, + + 0x79, 0xb2, 0xec, 0xd8, 0xca, 0x55, 0xa1, 0xa8, + 0x43, 0x4d, 0x6b, 0xca, 0x10, 0xb0, 0xc2, 0x01, + 0xc2, 0x33, 0x4e, 0x16, 0x24, 0xc4, 0xef, 0xee, + 0x99, 0xd8, 0xbb, 0xbc, 0x48, 0xd0, 0x01, 0x02, + }; + uint8_t dhkey[32] = { 0x69, 0xeb, 0x21, 0x32, 0xf2, 0xc6, 0x05, 0x41, + 0x60, 0x19, 0xcd, 0x5e, 0x94, 0xe1, 0xe6, 0x5f, + 0x33, 0x07, 0xe3, 0x38, 0x4b, 0x68, 0xe5, 0x62, + 0x3f, 0x88, 0x6d, 0x2f, 0x3a, 0x84, 0x85, 0xab, + }; + uint8_t dhkey_a[32], dhkey_b[32]; + int fails = 0; + + ecdh_shared_secret(pub_b, priv_a, dhkey_a); + ecdh_shared_secret(pub_a, priv_b, dhkey_b); + + printf("\n"); + + print_buf("DHKey ", dhkey, 32); + print_buf("DHKey A", dhkey_a, 32); + print_buf("DHKey B", dhkey_b, 32); + + if (memcmp(dhkey_a, dhkey, 32)) { + printf("DHKey A doesn't match!\n"); + fails++; + } else { + printf("DHKey A matches :)\n"); + } + + if (memcmp(dhkey_b, dhkey, 32)) { + printf("DHKey B doesn't match!\n"); + fails++; + } else { + printf("DHKey B matches :)\n"); + } + + g_assert(fails == 0); +} + int main(int argc, char *argv[]) { g_test_init(&argc, &argv, NULL); - g_test_add_func("/ecdh", test_basic); + g_test_add_func("/ecdh/multi", test_multi); + + g_test_add_func("/ecdh/sample/1", test_sample_1); + g_test_add_func("/ecdh/sample/2", test_sample_2); return g_test_run(); } |