diff --git a/lib/include/chiaki/common.h b/lib/include/chiaki/common.h index 8827dcc..071f42c 100644 --- a/lib/include/chiaki/common.h +++ b/lib/include/chiaki/common.h @@ -28,17 +28,17 @@ extern "C" { typedef enum { CHIAKI_ERR_SUCCESS = 0, - CHIAKI_ERR_PARSE_ADDR = 1, - CHIAKI_ERR_THREAD = 2, - CHIAKI_ERR_MEMORY = 3, - CHIAKI_ERR_NETWORK = 4, - CHIAKI_ERR_INVALID_DATA = 5, - CHIAKI_ERR_BUF_TOO_SMALL = 6 + CHIAKI_ERR_UNKNOWN, + CHIAKI_ERR_PARSE_ADDR, + CHIAKI_ERR_THREAD, + CHIAKI_ERR_MEMORY, + CHIAKI_ERR_NETWORK, + CHIAKI_ERR_INVALID_DATA, + CHIAKI_ERR_BUF_TOO_SMALL } ChiakiErrorCode; CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code); - #ifdef __cplusplus } #endif diff --git a/lib/include/chiaki/rpcrypt.h b/lib/include/chiaki/rpcrypt.h index 0d0e72a..d0d3f1f 100644 --- a/lib/include/chiaki/rpcrypt.h +++ b/lib/include/chiaki/rpcrypt.h @@ -30,11 +30,16 @@ extern "C" { typedef struct chiaki_rpcrypt_t { - + uint8_t bright[CHIAKI_KEY_BYTES]; + uint8_t ambassador[CHIAKI_KEY_BYTES]; + struct hmac_ctx_st *hmac_ctx; } ChiakiRPCrypt; CHIAKI_EXPORT void chiaki_rpcrypt_bright_ambassador(uint8_t *bright, uint8_t *ambassador, const uint8_t *nonce, const uint8_t *morning); +CHIAKI_EXPORT void chiaki_rpcrypt_init(ChiakiRPCrypt *rpcrypt, const uint8_t *nonce, const uint8_t *morning); +CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_generate_iv(ChiakiRPCrypt *rpcrypt, uint8_t *iv, uint64_t counter); + #ifdef __cplusplus } #endif diff --git a/lib/src/common.c b/lib/src/common.c index 42b5a60..45f34cc 100644 --- a/lib/src/common.c +++ b/lib/src/common.c @@ -17,6 +17,7 @@ #include + CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code) { switch(code) @@ -36,4 +37,4 @@ CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code) default: return "Unknown"; } -} \ No newline at end of file +} diff --git a/lib/src/rpcrypt.c b/lib/src/rpcrypt.c index e838251..6b6c2b3 100644 --- a/lib/src/rpcrypt.c +++ b/lib/src/rpcrypt.c @@ -17,6 +17,10 @@ #include +#include +#include + +#include CHIAKI_EXPORT void chiaki_rpcrypt_bright_ambassador(uint8_t *bright, uint8_t *ambassador, const uint8_t *nonce, const uint8_t *morning) @@ -42,4 +46,37 @@ CHIAKI_EXPORT void chiaki_rpcrypt_bright_ambassador(uint8_t *bright, uint8_t *am v ^= nonce[i]; bright[i] = v; } +} + + +CHIAKI_EXPORT void chiaki_rpcrypt_init(ChiakiRPCrypt *rpcrypt, const uint8_t *nonce, const uint8_t *morning) +{ + chiaki_rpcrypt_bright_ambassador(rpcrypt->bright, rpcrypt->ambassador, nonce, morning); +} + +CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_generate_iv(ChiakiRPCrypt *rpcrypt, uint8_t *iv, uint64_t counter) +{ + uint8_t hmac_key[] = { 0xac, 0x07, 0x88, 0x83, 0xc8, 0x3a, 0x1f, 0xe8, 0x11, 0x46, 0x3a, 0xf3, 0x9e, 0xe3, 0xe3, 0x77 }; + + uint8_t buf[CHIAKI_KEY_BYTES + 8]; + memcpy(buf, rpcrypt->ambassador, CHIAKI_KEY_BYTES); + buf[CHIAKI_KEY_BYTES + 0] = (uint8_t)((counter >> 0x38) & 0xff); + buf[CHIAKI_KEY_BYTES + 1] = (uint8_t)((counter >> 0x30) & 0xff); + buf[CHIAKI_KEY_BYTES + 2] = (uint8_t)((counter >> 0x28) & 0xff); + buf[CHIAKI_KEY_BYTES + 3] = (uint8_t)((counter >> 0x20) & 0xff); + buf[CHIAKI_KEY_BYTES + 4] = (uint8_t)((counter >> 0x18) & 0xff); + buf[CHIAKI_KEY_BYTES + 5] = (uint8_t)((counter >> 0x10) & 0xff); + buf[CHIAKI_KEY_BYTES + 6] = (uint8_t)((counter >> 0x08) & 0xff); + buf[CHIAKI_KEY_BYTES + 7] = (uint8_t)((counter >> 0x00) & 0xff); + + uint8_t hmac[32]; + unsigned int hmac_len = 0; + if(!HMAC(EVP_sha256(), hmac_key, CHIAKI_KEY_BYTES, buf, sizeof(buf), hmac, &hmac_len)) + return CHIAKI_ERR_UNKNOWN; + + if(hmac_len < CHIAKI_KEY_BYTES) + return CHIAKI_ERR_UNKNOWN; + + memcpy(iv, hmac, CHIAKI_KEY_BYTES); + return CHIAKI_ERR_SUCCESS; } \ No newline at end of file diff --git a/test/rpcrypt.c b/test/rpcrypt.c index d02ee93..6fdf2de 100644 --- a/test/rpcrypt.c +++ b/test/rpcrypt.c @@ -19,10 +19,13 @@ #include + +static const uint8_t nonce[] = { 0x43, 0x9, 0x67, 0xae, 0x36, 0x4b, 0x1c, 0x45, 0x26, 0x62, 0x37, 0x7a, 0xbf, 0x3f, 0xe9, 0x39 }; +static const uint8_t morning[] = { 0xd2, 0x78, 0x9f, 0x51, 0x85, 0xa7, 0x99, 0xa2, 0x44, 0x52, 0x77, 0x9c, 0x2b, 0x83, 0xcf, 0x7 }; + + static MunitResult test_bright_ambassador(const MunitParameter params[], void *user) { - static const uint8_t nonce[] = { 0x43, 0x9, 0x67, 0xae, 0x36, 0x4b, 0x1c, 0x45, 0x26, 0x62, 0x37, 0x7a, 0xbf, 0x3f, 0xe9, 0x39 }; - static const uint8_t morning[] = { 0xd2, 0x78, 0x9f, 0x51, 0x85, 0xa7, 0x99, 0xa2, 0x44, 0x52, 0x77, 0x9c, 0x2b, 0x83, 0xcf, 0x7 }; static const uint8_t bright_expected[] = { 0xa4, 0x4e, 0x2a, 0x16, 0x5e, 0x20, 0xd3, 0xf, 0xaa, 0x11, 0x8b, 0xc7, 0x7c, 0xa7, 0xdc, 0x11 }; static const uint8_t ambassador_expected[] = { 0x1d, 0xa8, 0xb9, 0x1f, 0x6e, 0x26, 0x64, 0x2e, 0xbc, 0x8, 0x8b, 0x0, 0x4f, 0x1, 0x5b, 0x52 }; @@ -36,6 +39,43 @@ static MunitResult test_bright_ambassador(const MunitParameter params[], void *u return MUNIT_OK; } +static MunitResult test_iv(const MunitParameter params[], void *user) +{ + static const uint8_t iv_a_expected[] = { 0x6, 0x29, 0xbe, 0x4, 0xe9, 0x91, 0x1c, 0x48, 0xb4, 0x5c, 0x2, 0x6d, 0xb7, 0xb7, 0x88, 0x46 }; + static const uint8_t iv_b_expected[] = { 0x3f, 0xd0, 0x83, 0xa, 0xc7, 0x30, 0xfc, 0x56, 0x75, 0x2d, 0xbe, 0xb8, 0x2c, 0x68, 0xa7, 0x4 }; + + ChiakiRPCrypt rpcrypt; + ChiakiErrorCode err; + + chiaki_rpcrypt_init(&rpcrypt, nonce, morning); + + uint8_t iv[CHIAKI_KEY_BYTES]; + + err = chiaki_rpcrypt_generate_iv(&rpcrypt, iv, 0); + if(err != CHIAKI_ERR_SUCCESS) + return MUNIT_ERROR; + munit_assert_memory_equal(CHIAKI_KEY_BYTES, iv, iv_a_expected); + + err = chiaki_rpcrypt_generate_iv(&rpcrypt, iv, 0); + if(err != CHIAKI_ERR_SUCCESS) + return MUNIT_ERROR; + munit_assert_memory_equal(CHIAKI_KEY_BYTES, iv, iv_a_expected); + + err = chiaki_rpcrypt_generate_iv(&rpcrypt, iv, 0x0102030405060708); + if(err != CHIAKI_ERR_SUCCESS) + return MUNIT_ERROR; + munit_assert_memory_equal(CHIAKI_KEY_BYTES, iv, iv_b_expected); + + err = chiaki_rpcrypt_generate_iv(&rpcrypt, iv, 0x0102030405060708); + if(err != CHIAKI_ERR_SUCCESS) + return MUNIT_ERROR; + munit_assert_memory_equal(CHIAKI_KEY_BYTES, iv, iv_b_expected); + + return MUNIT_OK; +} + + + MunitTest tests_rpcrypt[] = { { "/bright_ambassador", @@ -45,5 +85,13 @@ MunitTest tests_rpcrypt[] = { MUNIT_TEST_OPTION_NONE, NULL }, + { + "/iv", + test_iv, + NULL, + NULL, + MUNIT_TEST_OPTION_NONE, + NULL + }, { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; \ No newline at end of file