From 640889eea3345416643bd9c5ed78bf157686f086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Sat, 17 Nov 2018 18:57:47 +0100 Subject: [PATCH] Simplify RPCrypt --- lib/include/chiaki/rpcrypt.h | 4 +-- lib/src/ctrl.c | 8 +++++- lib/src/rpcrypt.c | 52 +++++++++--------------------------- test/rpcrypt.c | 16 +++++------ 4 files changed, 29 insertions(+), 51 deletions(-) diff --git a/lib/include/chiaki/rpcrypt.h b/lib/include/chiaki/rpcrypt.h index d71e620..f3ae5f7 100644 --- a/lib/include/chiaki/rpcrypt.h +++ b/lib/include/chiaki/rpcrypt.h @@ -40,8 +40,8 @@ CHIAKI_EXPORT void chiaki_rpcrypt_bright_ambassador(uint8_t *bright, uint8_t *am 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); -CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_encrypt(ChiakiRPCrypt *rpcrypt, uint64_t counter, uint8_t *buf, size_t buf_size); -CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_decrypt(ChiakiRPCrypt *rpcrypt, uint64_t counter, uint8_t *buf, size_t buf_size); +CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_encrypt(ChiakiRPCrypt *rpcrypt, uint64_t counter, uint8_t *in, uint8_t *out, size_t sz); +CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_decrypt(ChiakiRPCrypt *rpcrypt, uint64_t counter, uint8_t *in, uint8_t *out, size_t sz); #ifdef __cplusplus } diff --git a/lib/src/ctrl.c b/lib/src/ctrl.c index 443b978..f106d71 100644 --- a/lib/src/ctrl.c +++ b/lib/src/ctrl.c @@ -21,6 +21,7 @@ #include #include #include +#include #define SESSION_CTRL_PORT 9295 @@ -78,13 +79,14 @@ static ChiakiErrorCode ctrl_thread_connect(ChiakiCtrl *ctrl) CHIAKI_LOGI(&session->log, "Connected to %s:%d\n", session->connect_info.hostname, SESSION_CTRL_PORT); + char request[512]; static const char request_fmt[] = "GET /sce/rp/session/ctrl HTTP/1.1\r\n" "Host: %s:%d\r\n" "User-Agent: remoteplay Windows\r\n" "Connection: keep-alive\r\n" "Content-Length: 0\r\n" - "RP-Auth: %s\r\n" + "RP-Auth: %s\r\n" "RP-Version: 8.0\r\n" "RP-Did: %s\r\n" "RP-ControllerType: 3\r\n" @@ -92,6 +94,10 @@ static ChiakiErrorCode ctrl_thread_connect(ChiakiCtrl *ctrl) "RP-OSType: %s\r\n" "RP-ConPath: 1\r\n\r\n"; + uint8_t auth_enc[CHIAKI_KEY_BYTES]; + + + close(sock); return CHIAKI_ERR_SUCCESS; } \ No newline at end of file diff --git a/lib/src/rpcrypt.c b/lib/src/rpcrypt.c index 27e9401..e58747f 100644 --- a/lib/src/rpcrypt.c +++ b/lib/src/rpcrypt.c @@ -82,7 +82,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_generate_iv(ChiakiRPCrypt *rpcrypt, return CHIAKI_ERR_SUCCESS; } -static ChiakiErrorCode chiaki_rpcrypt_crypt(ChiakiRPCrypt *rpcrypt, uint64_t counter, uint8_t *buf, size_t buf_size, bool encrypt) +static ChiakiErrorCode chiaki_rpcrypt_crypt(ChiakiRPCrypt *rpcrypt, uint64_t counter, uint8_t *in, uint8_t *out, size_t sz, bool encrypt) { EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); if(!ctx) @@ -109,60 +109,32 @@ static ChiakiErrorCode chiaki_rpcrypt_crypt(ChiakiRPCrypt *rpcrypt, uint64_t cou if(!EVP_CIPHER_CTX_set_padding(ctx, 0)) FAIL(CHIAKI_ERR_UNKNOWN); - if(buf_size % CHIAKI_KEY_BYTES) + int outl; + if(encrypt) { - size_t padded_size = ((buf_size + CHIAKI_KEY_BYTES - 1) / CHIAKI_KEY_BYTES) * CHIAKI_KEY_BYTES; - uint8_t *tmp = malloc(padded_size); - if(!tmp) - FAIL(CHIAKI_ERR_MEMORY); - memcpy(tmp, buf, buf_size); - memset(tmp + buf_size, 0, padded_size - buf_size); - int outl = (int)padded_size; - - int success; - if(encrypt) - success = EVP_EncryptUpdate(ctx, tmp, &outl, tmp, outl); - else - success = EVP_DecryptUpdate(ctx, tmp, &outl, tmp, outl); - - if(!success || outl != (int)padded_size) - { - free(tmp); + if(!EVP_EncryptUpdate(ctx, out, &outl, in, (int)sz)) FAIL(CHIAKI_ERR_UNKNOWN); - } - - memcpy(buf, tmp, buf_size); - free(tmp); } else { - int outl = (int)buf_size; - if(encrypt) - { - if(!EVP_EncryptUpdate(ctx, buf, &outl, buf, outl)) - FAIL(CHIAKI_ERR_UNKNOWN); - } - else - { - if(!EVP_DecryptUpdate(ctx, buf, &outl, buf, outl)) - FAIL(CHIAKI_ERR_UNKNOWN); - } - - if(outl != (int)buf_size) + if(!EVP_DecryptUpdate(ctx, out, &outl, in, (int)sz)) FAIL(CHIAKI_ERR_UNKNOWN); } + if(outl != (int)sz) + FAIL(CHIAKI_ERR_UNKNOWN); + #undef FAIL EVP_CIPHER_CTX_free(ctx); return CHIAKI_ERR_SUCCESS; } -CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_encrypt(ChiakiRPCrypt *rpcrypt, uint64_t counter, uint8_t *buf, size_t buf_size) +CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_encrypt(ChiakiRPCrypt *rpcrypt, uint64_t counter, uint8_t *in, uint8_t *out, size_t sz) { - return chiaki_rpcrypt_crypt(rpcrypt, counter, buf, buf_size, true); + return chiaki_rpcrypt_crypt(rpcrypt, counter, in, out, sz, true); } -CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_decrypt(ChiakiRPCrypt *rpcrypt, uint64_t counter, uint8_t *buf, size_t buf_size) +CHIAKI_EXPORT ChiakiErrorCode chiaki_rpcrypt_decrypt(ChiakiRPCrypt *rpcrypt, uint64_t counter, uint8_t *in, uint8_t *out, size_t sz) { - return chiaki_rpcrypt_crypt(rpcrypt, counter, buf, buf_size, false); + return chiaki_rpcrypt_crypt(rpcrypt, counter, in, out, sz, false); } diff --git a/test/rpcrypt.c b/test/rpcrypt.c index 864eb02..9acd808 100644 --- a/test/rpcrypt.c +++ b/test/rpcrypt.c @@ -86,7 +86,7 @@ static MunitResult test_encrypt(const MunitParameter params[], void *user) // less than block size uint8_t buf_a[] = { 0x13, 0x37, 0xc0, 0xff, 0xee }; static const uint8_t cipher_expected_a[] = { 0x40, 0x48, 0x63, 0xeb, 0xb4 }; - err = chiaki_rpcrypt_encrypt(&rpcrypt, 0x0102030405060708, buf_a, sizeof(buf_a)); + err = chiaki_rpcrypt_encrypt(&rpcrypt, 0x0102030405060708, buf_a, buf_a, sizeof(buf_a)); if(err != CHIAKI_ERR_SUCCESS) return MUNIT_ERROR; munit_assert_memory_equal(sizeof(buf_a), buf_a, cipher_expected_a); @@ -94,7 +94,7 @@ static MunitResult test_encrypt(const MunitParameter params[], void *user) // 1x block size uint8_t buf_b[] = { 0xdf, 0xa8, 0xa3, 0xd2, 0x6a, 0xd7, 0xf3, 0x4c, 0x4f, 0x87, 0x4c, 0xeb, 0x8c, 0x57, 0xfb, 0x3f }; static const uint8_t cipher_expected_b[] = { 0x8c, 0xd7, 0x0, 0xc6, 0x30, 0x25, 0x3a, 0x18, 0x15, 0x20, 0xeb, 0x26, 0xf7, 0xed, 0xab, 0x15 }; - err = chiaki_rpcrypt_encrypt(&rpcrypt, 0x0102030405060708, buf_b, sizeof(buf_b)); + err = chiaki_rpcrypt_encrypt(&rpcrypt, 0x0102030405060708, buf_b, buf_b, sizeof(buf_b)); if(err != CHIAKI_ERR_SUCCESS) return MUNIT_ERROR; munit_assert_memory_equal(sizeof(buf_b), buf_b, cipher_expected_b); @@ -102,7 +102,7 @@ static MunitResult test_encrypt(const MunitParameter params[], void *user) // more than block size, but not dividable by block size uint8_t buf_c[] = { 0x8, 0xd, 0x80, 0xc7, 0xb2, 0x2b, 0x9f, 0xf3, 0xe2, 0xd7, 0xc8, 0xa5, 0xb0, 0x92, 0xd5, 0x0, 0x8d, 0xe6, 0xd7, 0x74 }; static const uint8_t cipher_expected_c[] = { 0x5b, 0x72, 0x23, 0xd3, 0xe8, 0xd9, 0x56, 0xa7, 0xb8, 0x70, 0x6f, 0x68, 0xcb, 0x28, 0x85, 0x2a, 0x6, 0xa5, 0xd2, 0xe0 }; - err = chiaki_rpcrypt_encrypt(&rpcrypt, 0x0102030405060708, buf_c, sizeof(buf_c)); + err = chiaki_rpcrypt_encrypt(&rpcrypt, 0x0102030405060708, buf_c, buf_c, sizeof(buf_c)); if(err != CHIAKI_ERR_SUCCESS) return MUNIT_ERROR; munit_assert_memory_equal(sizeof(buf_c), buf_c, cipher_expected_c); @@ -110,7 +110,7 @@ static MunitResult test_encrypt(const MunitParameter params[], void *user) // 2x block size uint8_t buf_d[] = { 0xa1, 0x6, 0x91, 0x1b, 0x49, 0x74, 0xe0, 0x73, 0x3, 0xe8, 0x47, 0x42, 0x8, 0x4d, 0x83, 0x4e, 0xf3, 0x26, 0x14, 0x7b, 0xde, 0x2d, 0xf6, 0x7d, 0x47, 0x96, 0x8c, 0x3b, 0x66, 0x95, 0x7e, 0x5d }; static const uint8_t cipher_expected_d[] = { 0xf2, 0x79, 0x32, 0xf, 0x13, 0x86, 0x29, 0x27, 0x59, 0x4f, 0xe0, 0x8f, 0x73, 0xf7, 0xd3, 0x64, 0x9d, 0x80, 0x7e, 0x90, 0xd1, 0xf5, 0xd8, 0x18, 0xe7, 0xbe, 0x16, 0x29, 0xfb, 0x48, 0x2b, 0xf8 }; - err = chiaki_rpcrypt_encrypt(&rpcrypt, 0x0102030405060708, buf_d, sizeof(buf_d)); + err = chiaki_rpcrypt_encrypt(&rpcrypt, 0x0102030405060708, buf_d, buf_d, sizeof(buf_d)); if(err != CHIAKI_ERR_SUCCESS) return MUNIT_ERROR; munit_assert_memory_equal(sizeof(buf_d), buf_d, cipher_expected_d); @@ -128,7 +128,7 @@ static MunitResult test_decrypt(const MunitParameter params[], void *user) // less than block size uint8_t buf_a[] = { 0x8d, 0xd2, 0x1d, 0xfb }; static const uint8_t expected_a[] = { 0xde, 0xad, 0xbe, 0xef }; - err = chiaki_rpcrypt_decrypt(&rpcrypt, 0x0102030405060708, buf_a, sizeof(buf_a)); + err = chiaki_rpcrypt_decrypt(&rpcrypt, 0x0102030405060708, buf_a, buf_a, sizeof(buf_a)); if(err != CHIAKI_ERR_SUCCESS) return MUNIT_ERROR; munit_assert_memory_equal(sizeof(buf_a), buf_a, expected_a); @@ -136,7 +136,7 @@ static MunitResult test_decrypt(const MunitParameter params[], void *user) // 1x block size uint8_t buf_b[] = { 0xeb, 0x22, 0x4e, 0xb7, 0x73, 0x94, 0x6a, 0x31, 0x6e, 0xdd, 0xe5, 0x87, 0x29, 0xdc, 0xd5, 0x6b }; static const uint8_t expected_b[] = { 0xb8, 0x5d, 0xed, 0xa3, 0x29, 0x66, 0xa3, 0x65, 0x34, 0x7a, 0x42, 0x4a, 0x52, 0x66, 0x85, 0x41 }; - err = chiaki_rpcrypt_decrypt(&rpcrypt, 0x0102030405060708, buf_b, sizeof(buf_b)); + err = chiaki_rpcrypt_decrypt(&rpcrypt, 0x0102030405060708, buf_b, buf_b, sizeof(buf_b)); if(err != CHIAKI_ERR_SUCCESS) return MUNIT_ERROR; munit_assert_memory_equal(sizeof(buf_b), buf_b, expected_b); @@ -144,7 +144,7 @@ static MunitResult test_decrypt(const MunitParameter params[], void *user) // more than block size, but not dividable by block size uint8_t buf_c[] = { 0x2b, 0xd8, 0x1d, 0x86, 0x39, 0xe, 0x2d, 0xd7, 0xde, 0x2d, 0xb5, 0xbc, 0xba, 0x5e, 0xe9, 0x78, 0xee, 0xc9, 0x3b, 0x18 }; static const uint8_t expected_c[] = { 0x78, 0xa7, 0xbe, 0x92, 0x63, 0xfc, 0xe4, 0x83, 0x84, 0x8a, 0x12, 0x71, 0xc1, 0xe4, 0xb9, 0x52, 0xf2, 0xbb, 0xcd, 0x39 }; - err = chiaki_rpcrypt_decrypt(&rpcrypt, 0x0102030405060708, buf_c, sizeof(buf_c)); + err = chiaki_rpcrypt_decrypt(&rpcrypt, 0x0102030405060708, buf_c, buf_c, sizeof(buf_c)); if(err != CHIAKI_ERR_SUCCESS) return MUNIT_ERROR; munit_assert_memory_equal(sizeof(buf_c), buf_c, expected_c); @@ -152,7 +152,7 @@ static MunitResult test_decrypt(const MunitParameter params[], void *user) // 2x block size uint8_t buf_d[] = { 0x2b, 0x80, 0xda, 0xa0, 0x5e, 0x8d, 0x2e, 0x1d, 0xe1, 0x95, 0x16, 0xb1, 0xed, 0xd2, 0xc3, 0x8a, 0xb8, 0xcc, 0x6c, 0x42, 0x57, 0x8d, 0xc5, 0x7d, 0xf1, 0x5c, 0x4, 0x42, 0x97, 0x25, 0x3a, 0x91 }; static const uint8_t expected_d[] = { 0x78, 0xff, 0x79, 0xb4, 0x4, 0x7f, 0xe7, 0x49, 0xbb, 0x32, 0xb1, 0x7c, 0x96, 0x68, 0x93, 0xa0, 0x86, 0xa6, 0x56, 0x38, 0x79, 0x2a, 0xa8, 0x3, 0x67, 0x9b, 0xf2, 0x82, 0x2d, 0x1a, 0x8, 0x29 }; - err = chiaki_rpcrypt_decrypt(&rpcrypt, 0x0102030405060708, buf_d, sizeof(buf_d)); + err = chiaki_rpcrypt_decrypt(&rpcrypt, 0x0102030405060708, buf_d, buf_d, sizeof(buf_d)); if(err != CHIAKI_ERR_SUCCESS) return MUNIT_ERROR; munit_assert_memory_equal(sizeof(buf_d), buf_d, expected_d);