Fix GKCrypt and add tests

This commit is contained in:
Florian Märkl 2018-11-28 16:02:25 +01:00
commit 1588e81b9e
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
3 changed files with 122 additions and 13 deletions

View file

@ -18,6 +18,7 @@
#include <munit.h>
#include <chiaki/ecdh.h>
#include <chiaki/gkcrypt.h>
static MunitResult test_ecdh(const MunitParameter params[], void *user)
{
@ -65,13 +66,114 @@ static MunitResult test_ecdh(const MunitParameter params[], void *user)
}
static MunitResult test_key_stream(const MunitParameter params[], void *user)
{
static const uint8_t handshake_key[] = { 0x83, 0xcf, 0x93, 0x1a, 0x6a, 0xa7, 0x69, 0xa6, 0xc4, 0x48, 0x5d, 0x19, 0xc1, 0x5c, 0xcc, 0x52 };
static const uint8_t ecdh_secret[] = { 0x73, 0xc8, 0xd5, 0x49, 0xc4, 0xd9, 0xdb, 0x50, 0x2e, 0xc0, 0x44, 0xea, 0x33, 0x64, 0x8c, 0x6a, 0xc9, 0xf3, 0x6c, 0x41, 0xb6, 0xa0, 0x50, 0x4f, 0xe0, 0x93, 0xde, 0xfb, 0x61, 0x9b, 0x9, 0x73 };
static const uint8_t gkcrypt_key[] = { 0x8, 0x81, 0x6f, 0xa2, 0xe5, 0x55, 0x89, 0x61, 0xd5, 0xa2, 0x86, 0xd9, 0xe, 0xec, 0x5b, 0x8c };
static const uint8_t gkcrypt_iv[] = { 0x2a, 0xe1, 0xbb, 0x3d, 0x84, 0xdc, 0x9a, 0xa9, 0xc3, 0x52, 0xa4, 0xcf, 0x3f, 0xfb, 0x8b, 0x72 };
static const uint8_t key_stream[] = { 0xf, 0x6d, 0x89, 0x85, 0x5b, 0xa7, 0x86, 0x74, 0x5b, 0xa1, 0xfe, 0x5c, 0x81, 0x19, 0x6c, 0xd5, 0x54, 0xc4, 0x1c, 0xca, 0xf6, 0xe9, 0x34, 0xa4, 0x89, 0x26, 0x98, 0xb0, 0x62, 0x12, 0xb3, 0x1a };
ChiakiLog log;
ChiakiGKCrypt gkcrypt;
ChiakiErrorCode err = chiaki_gkcrypt_init(&gkcrypt, &log, 0, 42, handshake_key, ecdh_secret);
if(err != CHIAKI_ERR_SUCCESS)
return MUNIT_ERROR;
munit_assert_memory_equal(sizeof(gkcrypt_key), gkcrypt.key, gkcrypt_key);
munit_assert_memory_equal(sizeof(gkcrypt_iv), gkcrypt.iv, gkcrypt_iv);
uint8_t key_stream_result[0x20];
err = chiaki_gkcrypt_gen_key_stream(&gkcrypt, 0x30, key_stream_result, sizeof(key_stream_result));
if(err != CHIAKI_ERR_SUCCESS)
{
chiaki_gkcrypt_fini(&gkcrypt);
return MUNIT_ERROR;
}
munit_assert_memory_equal(sizeof(key_stream), key_stream_result, key_stream);
chiaki_gkcrypt_fini(&gkcrypt);
return MUNIT_OK;
}
static MunitResult test_endecrypt(const MunitParameter params[], void *user)
{
static const uint8_t handshake_key[] = { 0x14, 0xf1, 0xe6, 0x94, 0x6c, 0x5d, 0xce, 0xa8, 0xb7, 0xaa, 0x48, 0x50, 0xf6, 0x4d, 0x21, 0xac };
static const uint8_t ecdh_secret[] = { 0xc, 0xeb, 0x77, 0x9, 0x83, 0x4d, 0x7a, 0xfc, 0x50, 0xb8, 0x46, 0x8c, 0xc6, 0x3c, 0x1e, 0x7c, 0x4e, 0x4a, 0x88, 0x93, 0x42, 0x80, 0xc1, 0x28, 0xe6, 0x1e, 0xe9, 0xd4, 0x1b, 0x8c, 0x69, 0x36 };
static const uint8_t gkcrypt_key[] = { 0x27, 0x7a, 0xa5, 0x1d, 0xac, 0xd1, 0x5f, 0xe, 0x54, 0x12, 0xfa, 0xce, 0xd, 0xc4, 0x63, 0x6a };
static const uint8_t gkcrypt_iv[] = { 0xef, 0x20, 0x40, 0xc2, 0x15, 0x3c, 0x2, 0x66, 0x32, 0x1f, 0x42, 0xbb, 0xf4, 0x50, 0x34, 0x4d };
static const uint8_t clear_data[] = { 0x4e, 0x61, 0x9f, 0x94, 0x5d, 0x4b, 0x8e, 0xbd, 0x2a, 0x15, 0x4d, 0x3, 0x6a, 0xcd, 0x49, 0x56, 0x9c, 0xc7, 0x5c, 0xe3, 0xe7, 0x0, 0x17, 0x9a, 0x38, 0xd9, 0x69, 0x53, 0x45, 0xf9, 0xc, 0xb5, 0x8c, 0x5, 0x65, 0xf, 0x70 };
static const uint8_t enc_data[] = { 0x23, 0xf4, 0x8d, 0xd8, 0xaa, 0xf9, 0x58, 0x9b, 0xb1, 0x94, 0x4f, 0xad, 0x2b, 0x8d, 0xaa, 0x8d, 0x25, 0x88, 0xfa, 0xf8, 0xb6, 0xd4, 0x17, 0xf4, 0x5f, 0x78, 0xec, 0xf5, 0x4e, 0x37, 0x20, 0xb0, 0x76, 0x81, 0x7, 0x67, 0x9a };
ChiakiLog log;
ChiakiGKCrypt gkcrypt;
ChiakiErrorCode err = chiaki_gkcrypt_init(&gkcrypt, &log, 0, 42, handshake_key, ecdh_secret);
if(err != CHIAKI_ERR_SUCCESS)
return MUNIT_ERROR;
munit_assert_memory_equal(sizeof(gkcrypt_key), gkcrypt.key, gkcrypt_key);
munit_assert_memory_equal(sizeof(gkcrypt_iv), gkcrypt.iv, gkcrypt_iv);
uint8_t key_stream_result[0x20];
err = chiaki_gkcrypt_gen_key_stream(&gkcrypt, 0x30, key_stream_result, sizeof(key_stream_result));
if(err != CHIAKI_ERR_SUCCESS)
{
chiaki_gkcrypt_fini(&gkcrypt);
return MUNIT_ERROR;
}
uint8_t buf[0x25];
memcpy(buf, clear_data, sizeof(buf));
err = chiaki_gkcrypt_encrypt(&gkcrypt, 0x11, buf, sizeof(buf));
if(err != CHIAKI_ERR_SUCCESS)
{
chiaki_gkcrypt_fini(&gkcrypt);
return MUNIT_ERROR;
}
munit_assert_memory_equal(sizeof(buf), buf, enc_data);
memcpy(buf, clear_data, sizeof(buf));
err = chiaki_gkcrypt_decrypt(&gkcrypt, 0x11, buf, sizeof(buf));
if(err != CHIAKI_ERR_SUCCESS)
{
chiaki_gkcrypt_fini(&gkcrypt);
return MUNIT_ERROR;
}
munit_assert_memory_equal(sizeof(buf), buf, enc_data);
chiaki_gkcrypt_fini(&gkcrypt);
return MUNIT_OK;
}
MunitTest tests_gkcrypt[] = {
{
"/ecdh",
test_ecdh,
NULL,
NULL,
MUNIT_TEST_OPTION_NONE,
MUNIT_TEST_OPTION_NONE,
NULL
},
{
"/key_stream",
test_key_stream,
NULL,
NULL,
MUNIT_TEST_OPTION_NONE,
NULL
},
{
"/en_decrypt",
test_key_stream,
NULL,
NULL,
MUNIT_TEST_OPTION_NONE,
NULL
},
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }