mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-20 21:43:12 -07:00
GMAC working
This commit is contained in:
parent
f128dd6426
commit
d689f9e029
8 changed files with 218 additions and 28 deletions
|
@ -39,7 +39,8 @@ typedef enum
|
||||||
CHIAKI_ERR_MUTEX_LOCKED,
|
CHIAKI_ERR_MUTEX_LOCKED,
|
||||||
CHIAKI_ERR_CANCELED,
|
CHIAKI_ERR_CANCELED,
|
||||||
CHIAKI_ERR_TIMEOUT,
|
CHIAKI_ERR_TIMEOUT,
|
||||||
CHIAKI_ERR_INVALID_RESPONSE
|
CHIAKI_ERR_INVALID_RESPONSE,
|
||||||
|
CHIAKI_ERR_UNINITIALIZED
|
||||||
} ChiakiErrorCode;
|
} ChiakiErrorCode;
|
||||||
|
|
||||||
CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code);
|
CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code);
|
||||||
|
|
|
@ -30,12 +30,17 @@ extern "C" {
|
||||||
|
|
||||||
#define CHIAKI_GKCRYPT_BLOCK_SIZE 0x10
|
#define CHIAKI_GKCRYPT_BLOCK_SIZE 0x10
|
||||||
#define CHIAKI_GKCRYPT_GMAC_SIZE 4
|
#define CHIAKI_GKCRYPT_GMAC_SIZE 4
|
||||||
|
#define CHIAKI_GKCRYPT_GMAC_KEY_REFRESH_KEY_POS 45000
|
||||||
|
#define CHIAKI_GKCRYPT_GMAC_KEY_REFRESH_IV_OFFSET 44910
|
||||||
|
|
||||||
typedef struct chiaki_gkcrypt_t {
|
typedef struct chiaki_gkcrypt_t {
|
||||||
uint8_t *key_buf;
|
uint8_t *key_buf;
|
||||||
size_t key_buf_size;
|
size_t key_buf_size;
|
||||||
uint8_t key[CHIAKI_GKCRYPT_BLOCK_SIZE];
|
|
||||||
uint8_t iv[CHIAKI_GKCRYPT_BLOCK_SIZE];
|
uint8_t iv[CHIAKI_GKCRYPT_BLOCK_SIZE];
|
||||||
|
uint8_t key_base[CHIAKI_GKCRYPT_BLOCK_SIZE];
|
||||||
|
uint8_t key_gmac_base[CHIAKI_GKCRYPT_BLOCK_SIZE];
|
||||||
|
uint8_t key_gmac_current[CHIAKI_GKCRYPT_BLOCK_SIZE];
|
||||||
|
uint64_t key_gmac_index_next;
|
||||||
ChiakiLog *log;
|
ChiakiLog *log;
|
||||||
} ChiakiGKCrypt;
|
} ChiakiGKCrypt;
|
||||||
|
|
||||||
|
@ -46,7 +51,9 @@ CHIAKI_EXPORT void chiaki_gkcrypt_fini(ChiakiGKCrypt *gkcrypt);
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_gen_key_stream(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size);
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_gen_key_stream(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size);
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_decrypt(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size);
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_decrypt(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size);
|
||||||
static inline ChiakiErrorCode chiaki_gkcrypt_encrypt(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size) { return chiaki_gkcrypt_decrypt(gkcrypt, key_pos, buf, buf_size); }
|
static inline ChiakiErrorCode chiaki_gkcrypt_encrypt(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size) { return chiaki_gkcrypt_decrypt(gkcrypt, key_pos, buf, buf_size); }
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_gmac(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size, uint8_t *gmac_out);
|
CHIAKI_EXPORT void chiaki_gkcrypt_gen_gmac_key(uint64_t index, const uint8_t *key_base, const uint8_t *iv, uint8_t *key_out);
|
||||||
|
CHIAKI_EXPORT void chiaki_gkcrypt_gen_new_gmac_key(ChiakiGKCrypt *gkcrypt, uint64_t index);
|
||||||
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_gmac(ChiakiGKCrypt *gkcrypt, size_t key_pos, const uint8_t *buf, size_t buf_size, uint8_t *gmac_out);
|
||||||
|
|
||||||
static inline ChiakiGKCrypt *chiaki_gkcrypt_new(ChiakiLog *log, size_t key_buf_blocks, uint8_t index, const uint8_t *handshake_key, const uint8_t *ecdh_secret)
|
static inline ChiakiGKCrypt *chiaki_gkcrypt_new(ChiakiLog *log, size_t key_buf_blocks, uint8_t index, const uint8_t *handshake_key, const uint8_t *ecdh_secret)
|
||||||
{
|
{
|
||||||
|
|
|
@ -32,6 +32,7 @@ extern "C" {
|
||||||
|
|
||||||
typedef void (*ChiakiTakionDataCallback)(uint8_t *buf, size_t buf_size, void *user);
|
typedef void (*ChiakiTakionDataCallback)(uint8_t *buf, size_t buf_size, void *user);
|
||||||
typedef void (*ChiakiTakionAVCallback)(uint8_t *buf, size_t buf_size, uint8_t base_type, uint32_t key_pos, void *user);
|
typedef void (*ChiakiTakionAVCallback)(uint8_t *buf, size_t buf_size, uint8_t base_type, uint32_t key_pos, void *user);
|
||||||
|
typedef ChiakiErrorCode (*ChiakiTakionMACCallback)(uint8_t *buf, size_t buf_size, size_t key_pos, uint8_t *mac_out, void *user);
|
||||||
|
|
||||||
|
|
||||||
typedef struct chiaki_takion_connect_info_t
|
typedef struct chiaki_takion_connect_info_t
|
||||||
|
@ -43,6 +44,8 @@ typedef struct chiaki_takion_connect_info_t
|
||||||
void *data_cb_user;
|
void *data_cb_user;
|
||||||
ChiakiTakionAVCallback av_cb;
|
ChiakiTakionAVCallback av_cb;
|
||||||
void *av_cb_user;
|
void *av_cb_user;
|
||||||
|
ChiakiTakionMACCallback mac_cb;
|
||||||
|
void *mac_cb_user;
|
||||||
} ChiakiTakionConnectInfo;
|
} ChiakiTakionConnectInfo;
|
||||||
|
|
||||||
|
|
||||||
|
@ -53,6 +56,8 @@ typedef struct chiaki_takion_t
|
||||||
void *data_cb_user;
|
void *data_cb_user;
|
||||||
ChiakiTakionAVCallback av_cb;
|
ChiakiTakionAVCallback av_cb;
|
||||||
void *av_cb_user;
|
void *av_cb_user;
|
||||||
|
ChiakiTakionMACCallback mac_cb;
|
||||||
|
void *mac_cb_user;
|
||||||
int sock;
|
int sock;
|
||||||
ChiakiThread thread;
|
ChiakiThread thread;
|
||||||
int stop_pipe[2];
|
int stop_pipe[2];
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
#include <openssl/evp.h>
|
#include <openssl/evp.h>
|
||||||
#include <openssl/hmac.h>
|
#include <openssl/hmac.h>
|
||||||
|
#include <openssl/sha.h>
|
||||||
|
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
|
@ -46,6 +47,10 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_init(ChiakiGKCrypt *gkcrypt, Chiaki
|
||||||
return CHIAKI_ERR_UNKNOWN;
|
return CHIAKI_ERR_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
chiaki_gkcrypt_gen_gmac_key(0, gkcrypt->key_base, gkcrypt->iv, gkcrypt->key_gmac_base);
|
||||||
|
gkcrypt->key_gmac_index_next = 1;
|
||||||
|
memcpy(gkcrypt->key_gmac_current, gkcrypt->key_gmac_base, sizeof(gkcrypt->key_gmac_current));
|
||||||
|
|
||||||
return CHIAKI_ERR_SUCCESS;
|
return CHIAKI_ERR_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,18 +77,18 @@ static ChiakiErrorCode gkcrypt_gen_key_iv(ChiakiGKCrypt *gkcrypt, uint8_t index,
|
||||||
|
|
||||||
assert(hmac_size == sizeof(hmac));
|
assert(hmac_size == sizeof(hmac));
|
||||||
|
|
||||||
memcpy(gkcrypt->key, hmac, CHIAKI_GKCRYPT_BLOCK_SIZE);
|
memcpy(gkcrypt->key_base, hmac, CHIAKI_GKCRYPT_BLOCK_SIZE);
|
||||||
memcpy(gkcrypt->iv, hmac + CHIAKI_GKCRYPT_BLOCK_SIZE, CHIAKI_GKCRYPT_BLOCK_SIZE);
|
memcpy(gkcrypt->iv, hmac + CHIAKI_GKCRYPT_BLOCK_SIZE, CHIAKI_GKCRYPT_BLOCK_SIZE);
|
||||||
|
|
||||||
return CHIAKI_ERR_SUCCESS;
|
return CHIAKI_ERR_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void counter_add(uint8_t *out, const uint8_t *base, int v)
|
static inline void counter_add(uint8_t *out, const uint8_t *base, uint64_t v)
|
||||||
{
|
{
|
||||||
size_t i=0;
|
size_t i=0;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
int r = (int)base[i] + v;
|
uint64_t r = base[i] + v;
|
||||||
out[i] = (uint8_t)(r & 0xff);
|
out[i] = (uint8_t)(r & 0xff);
|
||||||
v = r >> 8;
|
v = r >> 8;
|
||||||
i++;
|
i++;
|
||||||
|
@ -93,6 +98,22 @@ static inline void counter_add(uint8_t *out, const uint8_t *base, int v)
|
||||||
memcpy(out + i, base + i, CHIAKI_GKCRYPT_BLOCK_SIZE - i);
|
memcpy(out + i, base + i, CHIAKI_GKCRYPT_BLOCK_SIZE - i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CHIAKI_EXPORT void chiaki_gkcrypt_gen_gmac_key(uint64_t index, const uint8_t *key_base, const uint8_t *iv, uint8_t *key_out)
|
||||||
|
{
|
||||||
|
uint8_t data[0x20];
|
||||||
|
memcpy(data, key_base, 0x10);
|
||||||
|
counter_add(data + 0x10, iv, index * CHIAKI_GKCRYPT_GMAC_KEY_REFRESH_IV_OFFSET);
|
||||||
|
uint8_t md[0x20];
|
||||||
|
SHA256(data, 0x20, md);
|
||||||
|
xor_bytes(md, md + 0x10, 0x10);
|
||||||
|
memcpy(key_out, md, CHIAKI_GKCRYPT_BLOCK_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
CHIAKI_EXPORT void chiaki_gkcrypt_gen_new_gmac_key(ChiakiGKCrypt *gkcrypt, uint64_t index)
|
||||||
|
{
|
||||||
|
chiaki_gkcrypt_gen_gmac_key(index, gkcrypt->key_gmac_base, gkcrypt->iv, gkcrypt->key_gmac_current);
|
||||||
|
}
|
||||||
|
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_gen_key_stream(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size)
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_gen_key_stream(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size)
|
||||||
{
|
{
|
||||||
assert(key_pos % CHIAKI_GKCRYPT_BLOCK_SIZE == 0);
|
assert(key_pos % CHIAKI_GKCRYPT_BLOCK_SIZE == 0);
|
||||||
|
@ -102,7 +123,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_gen_key_stream(ChiakiGKCrypt *gkcry
|
||||||
if(!ctx)
|
if(!ctx)
|
||||||
return CHIAKI_ERR_UNKNOWN;
|
return CHIAKI_ERR_UNKNOWN;
|
||||||
|
|
||||||
if(!EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, gkcrypt->key, NULL))
|
if(!EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, gkcrypt->key_base, NULL))
|
||||||
{
|
{
|
||||||
EVP_CIPHER_CTX_free(ctx);
|
EVP_CIPHER_CTX_free(ctx);
|
||||||
return CHIAKI_ERR_UNKNOWN;
|
return CHIAKI_ERR_UNKNOWN;
|
||||||
|
@ -153,30 +174,50 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_decrypt(ChiakiGKCrypt *gkcrypt, siz
|
||||||
return CHIAKI_ERR_SUCCESS;
|
return CHIAKI_ERR_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_gmac(ChiakiGKCrypt *gkcrypt, size_t key_pos, uint8_t *buf, size_t buf_size, uint8_t *gmac_out)
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_gmac(ChiakiGKCrypt *gkcrypt, size_t key_pos, const uint8_t *buf, size_t buf_size, uint8_t *gmac_out)
|
||||||
{
|
{
|
||||||
uint8_t iv[CHIAKI_GKCRYPT_BLOCK_SIZE];
|
uint8_t iv[CHIAKI_GKCRYPT_BLOCK_SIZE];
|
||||||
counter_add(iv, gkcrypt->iv, (int)key_pos);
|
counter_add(iv, gkcrypt->iv, key_pos / 0x10);
|
||||||
|
|
||||||
uint8_t *key = gkcrypt->key; // TODO: calculate new key? (conditions?)
|
if(key_pos > gkcrypt->key_gmac_index_next * CHIAKI_GKCRYPT_GMAC_KEY_REFRESH_KEY_POS)
|
||||||
|
{
|
||||||
|
uint64_t key_index_new = key_pos / CHIAKI_GKCRYPT_GMAC_KEY_REFRESH_KEY_POS;
|
||||||
|
chiaki_gkcrypt_gen_new_gmac_key(gkcrypt, key_index_new);
|
||||||
|
gkcrypt->key_gmac_index_next = key_index_new+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: key_pos smaller than current index
|
||||||
|
|
||||||
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
|
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
|
||||||
if(!ctx)
|
if(!ctx)
|
||||||
return CHIAKI_ERR_MEMORY;
|
return CHIAKI_ERR_MEMORY;
|
||||||
|
|
||||||
if(!EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, key, iv))
|
if(!EVP_CipherInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL, 1))
|
||||||
{
|
{
|
||||||
EVP_CIPHER_CTX_free(ctx);
|
EVP_CIPHER_CTX_free(ctx);
|
||||||
return CHIAKI_ERR_UNKNOWN;
|
return CHIAKI_ERR_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!EVP_EncryptUpdate(ctx, NULL, NULL, buf, (int)buf_size))
|
if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, CHIAKI_GKCRYPT_BLOCK_SIZE, NULL))
|
||||||
{
|
{
|
||||||
EVP_CIPHER_CTX_free(ctx);
|
EVP_CIPHER_CTX_free(ctx);
|
||||||
return CHIAKI_ERR_UNKNOWN;
|
return CHIAKI_ERR_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!EVP_EncryptFinal_ex(ctx, NULL, NULL))
|
if(!EVP_CipherInit_ex(ctx, NULL, NULL, gkcrypt->key_gmac_current, iv, 1))
|
||||||
|
{
|
||||||
|
EVP_CIPHER_CTX_free(ctx);
|
||||||
|
return CHIAKI_ERR_UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
|
int len;
|
||||||
|
if(!EVP_EncryptUpdate(ctx, NULL, &len, buf, (int)buf_size))
|
||||||
|
{
|
||||||
|
EVP_CIPHER_CTX_free(ctx);
|
||||||
|
return CHIAKI_ERR_UNKNOWN;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!EVP_EncryptFinal_ex(ctx, NULL, &len))
|
||||||
{
|
{
|
||||||
EVP_CIPHER_CTX_free(ctx);
|
EVP_CIPHER_CTX_free(ctx);
|
||||||
return CHIAKI_ERR_UNKNOWN;
|
return CHIAKI_ERR_UNKNOWN;
|
||||||
|
|
|
@ -59,6 +59,7 @@ static void nagare_takion_data_expect_bang(ChiakiNagare *nagare, uint8_t *buf, s
|
||||||
static void nagare_takion_data_expect_streaminfo(ChiakiNagare *nagare, uint8_t *buf, size_t buf_size);
|
static void nagare_takion_data_expect_streaminfo(ChiakiNagare *nagare, uint8_t *buf, size_t buf_size);
|
||||||
static ChiakiErrorCode nagare_send_streaminfo_ack(ChiakiNagare *nagare);
|
static ChiakiErrorCode nagare_send_streaminfo_ack(ChiakiNagare *nagare);
|
||||||
static void nagare_takion_av(uint8_t *buf, size_t buf_size, uint8_t base_type, uint32_t key_pos, void *user);
|
static void nagare_takion_av(uint8_t *buf, size_t buf_size, uint8_t base_type, uint32_t key_pos, void *user);
|
||||||
|
static ChiakiErrorCode nagare_takion_mac(uint8_t *buf, size_t buf_size, size_t key_pos, uint8_t *mac_out, void *user);
|
||||||
|
|
||||||
|
|
||||||
CHIAKI_EXPORT ChiakiErrorCode chiaki_nagare_run(ChiakiSession *session)
|
CHIAKI_EXPORT ChiakiErrorCode chiaki_nagare_run(ChiakiSession *session)
|
||||||
|
@ -90,6 +91,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_nagare_run(ChiakiSession *session)
|
||||||
takion_info.data_cb_user = nagare;
|
takion_info.data_cb_user = nagare;
|
||||||
takion_info.av_cb = nagare_takion_av;
|
takion_info.av_cb = nagare_takion_av;
|
||||||
takion_info.av_cb_user = nagare;
|
takion_info.av_cb_user = nagare;
|
||||||
|
takion_info.mac_cb = nagare_takion_mac;
|
||||||
|
takion_info.mac_cb_user = nagare;
|
||||||
|
|
||||||
err = chiaki_takion_connect(&nagare->takion, &takion_info);
|
err = chiaki_takion_connect(&nagare->takion, &takion_info);
|
||||||
free(takion_info.sa);
|
free(takion_info.sa);
|
||||||
|
@ -511,3 +514,12 @@ static void nagare_takion_av(uint8_t *buf, size_t buf_size, uint8_t base_type, u
|
||||||
//CHIAKI_LOGD(nagare->log, "Nagare AV %lu\n", buf_size);
|
//CHIAKI_LOGD(nagare->log, "Nagare AV %lu\n", buf_size);
|
||||||
//chiaki_log_hexdump(nagare->log, CHIAKI_LOG_DEBUG, buf, buf_size);
|
//chiaki_log_hexdump(nagare->log, CHIAKI_LOG_DEBUG, buf, buf_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static ChiakiErrorCode nagare_takion_mac(uint8_t *buf, size_t buf_size, size_t key_pos, uint8_t *mac_out, void *user)
|
||||||
|
{
|
||||||
|
ChiakiNagare *nagare = user;
|
||||||
|
if(!nagare->gkcrypt_b)
|
||||||
|
return CHIAKI_ERR_UNINITIALIZED;
|
||||||
|
return chiaki_gkcrypt_gmac(nagare->gkcrypt_b, key_pos, buf, buf_size, mac_out);
|
||||||
|
}
|
|
@ -73,6 +73,9 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_senkusha_run(ChiakiSession *session)
|
||||||
takion_info.data_cb = senkusha_takion_data;
|
takion_info.data_cb = senkusha_takion_data;
|
||||||
takion_info.data_cb_user = &senkusha;
|
takion_info.data_cb_user = &senkusha;
|
||||||
|
|
||||||
|
takion_info.mac_cb = NULL; // TODO: Do we need the MAC here?
|
||||||
|
takion_info.mac_cb_user = NULL;
|
||||||
|
|
||||||
err = chiaki_takion_connect(&senkusha.takion, &takion_info);
|
err = chiaki_takion_connect(&senkusha.takion, &takion_info);
|
||||||
free(takion_info.sa);
|
free(takion_info.sa);
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
|
|
|
@ -107,6 +107,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_connect(ChiakiTakion *takion, Chiaki
|
||||||
takion->data_cb_user = info->data_cb_user;
|
takion->data_cb_user = info->data_cb_user;
|
||||||
takion->av_cb = info->av_cb;
|
takion->av_cb = info->av_cb;
|
||||||
takion->av_cb_user = info->av_cb_user;
|
takion->av_cb_user = info->av_cb_user;
|
||||||
|
takion->mac_cb = info->mac_cb;
|
||||||
|
takion->mac_cb_user = info->mac_cb_user;
|
||||||
takion->something = TAKION_LOCAL_SOMETHING;
|
takion->something = TAKION_LOCAL_SOMETHING;
|
||||||
|
|
||||||
takion->tag_local = 0x4823; // "random" tag
|
takion->tag_local = 0x4823; // "random" tag
|
||||||
|
@ -376,11 +378,11 @@ static void takion_handle_packet(ChiakiTakion *takion, uint8_t *buf, size_t buf_
|
||||||
switch(base_type)
|
switch(base_type)
|
||||||
{
|
{
|
||||||
case TAKION_PACKET_TYPE_MESSAGE:
|
case TAKION_PACKET_TYPE_MESSAGE:
|
||||||
takion_handle_packet_message(takion, buf+1, buf_size-1);
|
takion_handle_packet_message(takion, buf, buf_size);
|
||||||
break;
|
break;
|
||||||
case TAKION_PACKET_TYPE_2:
|
case TAKION_PACKET_TYPE_2:
|
||||||
case TAKION_PACKET_TYPE_3:
|
case TAKION_PACKET_TYPE_3:
|
||||||
takion_handle_packet_av(takion, base_type, buf+1, buf_size-1);
|
takion_handle_packet_av(takion, base_type, buf, buf_size);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
CHIAKI_LOGW(takion->log, "Takion packet with unknown type %#x received\n", buf[0]);
|
CHIAKI_LOGW(takion->log, "Takion packet with unknown type %#x received\n", buf[0]);
|
||||||
|
@ -392,7 +394,7 @@ static void takion_handle_packet(ChiakiTakion *takion, uint8_t *buf, size_t buf_
|
||||||
static void takion_handle_packet_message(ChiakiTakion *takion, uint8_t *buf, size_t buf_size)
|
static void takion_handle_packet_message(ChiakiTakion *takion, uint8_t *buf, size_t buf_size)
|
||||||
{
|
{
|
||||||
TakionMessage msg;
|
TakionMessage msg;
|
||||||
ChiakiErrorCode err = takion_parse_message(takion, buf, buf_size, &msg);
|
ChiakiErrorCode err = takion_parse_message(takion, buf+1, buf_size-1, &msg);
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -648,24 +650,61 @@ static void takion_handle_packet_av(ChiakiTakion *takion, uint8_t base_type, uin
|
||||||
{
|
{
|
||||||
// HHIxIIx
|
// HHIxIIx
|
||||||
|
|
||||||
if(buf_size < AV_HEADER_SIZE + 1)
|
//CHIAKI_LOGD(takion->log, "calculating GMAC for buf:\n");
|
||||||
|
//chiaki_log_hexdump(takion->log, CHIAKI_LOG_DEBUG, buf, buf_size);
|
||||||
|
|
||||||
|
size_t av_size = buf_size-1;
|
||||||
|
|
||||||
|
if(av_size < AV_HEADER_SIZE + 1)
|
||||||
{
|
{
|
||||||
CHIAKI_LOGE(takion->log, "Takion received AV packet smaller than av header size + 1\n");
|
CHIAKI_LOGE(takion->log, "Takion received AV packet smaller than av header size + 1\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t packet_index = ntohs(*((uint16_t *)(buf + 0)));
|
uint8_t *av = buf+1;
|
||||||
uint16_t frame_index = ntohs(*((uint16_t *)(buf + 2)));
|
|
||||||
uint32_t dword_2 = ntohl(*((uint32_t *)(buf + 4)));
|
uint16_t packet_index = ntohs(*((uint16_t *)(av + 0)));
|
||||||
uint8_t *gmac = buf + 9; // uint8_t[4]
|
uint16_t frame_index = ntohs(*((uint16_t *)(av + 2)));
|
||||||
uint32_t key_pos = ntohl(*((uint32_t *)(buf + 0xd)));
|
uint32_t dword_2 = ntohl(*((uint32_t *)(av + 4)));
|
||||||
uint8_t unknown_1 = buf[0x11];
|
|
||||||
|
uint8_t gmac[4];
|
||||||
|
memcpy(gmac, av + 9, sizeof(gmac));
|
||||||
|
memset(av + 9, 0, sizeof(gmac));
|
||||||
|
|
||||||
|
uint32_t key_pos = ntohl(*((uint32_t *)(av + 0xd)));
|
||||||
|
|
||||||
|
uint8_t unknown_1 = av[0x11];
|
||||||
|
|
||||||
|
if(takion->mac_cb)
|
||||||
|
{
|
||||||
|
uint8_t gmac_expected[4];
|
||||||
|
memset(gmac_expected, 0, sizeof(gmac_expected));
|
||||||
|
|
||||||
|
//CHIAKI_LOGD(takion->log, "calculating GMAC for:\n");
|
||||||
|
//chiaki_log_hexdump(takion->log, CHIAKI_LOG_DEBUG, buf, buf_size);
|
||||||
|
|
||||||
|
if(takion->mac_cb(buf, buf_size, key_pos, gmac_expected, takion->mac_cb_user) != CHIAKI_ERR_SUCCESS)
|
||||||
|
{
|
||||||
|
CHIAKI_LOGE(takion->log, "Takion failed to calculate MAC\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//CHIAKI_LOGD(takion->log, "GMAC:\n");
|
||||||
|
//chiaki_log_hexdump(takion->log, CHIAKI_LOG_DEBUG, gmac, sizeof(gmac));
|
||||||
|
//CHIAKI_LOGD(takion->log, "GMAC expected:\n");
|
||||||
|
//chiaki_log_hexdump(takion->log, CHIAKI_LOG_DEBUG, gmac_expected, sizeof(gmac_expected));
|
||||||
|
if(memcmp(gmac_expected, gmac, sizeof(gmac)) != 0)
|
||||||
|
{
|
||||||
|
CHIAKI_LOGE(takion->log, "Takion packet MAC mismatch for key_pos %#lx\n", key_pos);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
CHIAKI_LOGD(takion->log, "Takion packet MAC correct for key pos %#lx\n", key_pos);
|
||||||
|
}
|
||||||
|
|
||||||
//CHIAKI_LOGD(takion->log, "packet index %u, frame index %u\n", packet_index, frame_index);
|
//CHIAKI_LOGD(takion->log, "packet index %u, frame index %u\n", packet_index, frame_index);
|
||||||
//chiaki_log_hexdump(takion->log, CHIAKI_LOG_DEBUG, buf, buf_size);
|
//chiaki_log_hexdump(takion->log, CHIAKI_LOG_DEBUG, buf, buf_size);
|
||||||
|
|
||||||
uint8_t *data = buf + AV_HEADER_SIZE;
|
uint8_t *data = av + AV_HEADER_SIZE;
|
||||||
size_t data_size = buf_size - AV_HEADER_SIZE;
|
size_t data_size = av_size - AV_HEADER_SIZE;
|
||||||
|
|
||||||
if(takion->av_cb)
|
if(takion->av_cb)
|
||||||
takion->av_cb(data, data_size, base_type, key_pos, takion->av_cb_user);
|
takion->av_cb(data, data_size, base_type, key_pos, takion->av_cb_user);
|
||||||
|
|
|
@ -81,7 +81,7 @@ static MunitResult test_key_stream(const MunitParameter params[], void *user)
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
return MUNIT_ERROR;
|
return MUNIT_ERROR;
|
||||||
|
|
||||||
munit_assert_memory_equal(sizeof(gkcrypt_key), gkcrypt.key, gkcrypt_key);
|
munit_assert_memory_equal(sizeof(gkcrypt_key), gkcrypt.key_base, gkcrypt_key);
|
||||||
munit_assert_memory_equal(sizeof(gkcrypt_iv), gkcrypt.iv, gkcrypt_iv);
|
munit_assert_memory_equal(sizeof(gkcrypt_iv), gkcrypt.iv, gkcrypt_iv);
|
||||||
|
|
||||||
uint8_t key_stream_result[0x20];
|
uint8_t key_stream_result[0x20];
|
||||||
|
@ -115,7 +115,7 @@ static MunitResult test_endecrypt(const MunitParameter params[], void *user)
|
||||||
if(err != CHIAKI_ERR_SUCCESS)
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
return MUNIT_ERROR;
|
return MUNIT_ERROR;
|
||||||
|
|
||||||
munit_assert_memory_equal(sizeof(gkcrypt_key), gkcrypt.key, gkcrypt_key);
|
munit_assert_memory_equal(sizeof(gkcrypt_key), gkcrypt.key_base, gkcrypt_key);
|
||||||
munit_assert_memory_equal(sizeof(gkcrypt_iv), gkcrypt.iv, gkcrypt_iv);
|
munit_assert_memory_equal(sizeof(gkcrypt_iv), gkcrypt.iv, gkcrypt_iv);
|
||||||
|
|
||||||
uint8_t key_stream_result[0x20];
|
uint8_t key_stream_result[0x20];
|
||||||
|
@ -150,6 +150,72 @@ static MunitResult test_endecrypt(const MunitParameter params[], void *user)
|
||||||
return MUNIT_OK;
|
return MUNIT_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static MunitResult test_gmac(const MunitParameter params[], void *user)
|
||||||
|
{
|
||||||
|
static const uint8_t gkcrypt_key[] = { 0xb6, 0x4b, 0x1e, 0x65, 0x3f, 0xbb, 0xa7, 0xab, 0x80, 0xb3, 0x1e, 0x5a, 0x32, 0x4d, 0xec, 0xc0 };
|
||||||
|
static const uint8_t gkcrypt_iv[] = { 0x7c, 0xc8, 0xd0, 0x19, 0x9e, 0xdf, 0xd8, 0xc3, 0xb5, 0x0f, 0x32, 0xee, 0x36, 0x33, 0x6a, 0x5a };
|
||||||
|
|
||||||
|
static const uint8_t buf[] = { 0x03, 0x00, 0x18, 0x00, 0x19, 0x00, 0x02, 0x50, 0x21, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x69, 0xa0, 0x00, 0xf9, 0xf8, 0x98, 0x24, 0xda, 0x56, 0x0b, 0x74, 0xec, 0x68, 0x15, 0xd3, 0x1f,
|
||||||
|
0x85, 0x43, 0xaa, 0xd3, 0xcd, 0xb8, 0x50, 0x2e, 0x90, 0xa4, 0xb0, 0x85, 0xf3, 0xbe, 0x9a, 0x47,
|
||||||
|
0x97, 0xc1, 0xab, 0x5f, 0xa5, 0xfd, 0x62, 0x5c, 0x26, 0x1a, 0x54, 0x36, 0x72, 0x30, 0xc3, 0x2b,
|
||||||
|
0xe0, 0x2c, 0x5a, 0x86, 0x07, 0xf5, 0xdc, 0x12, 0x55, 0xb4, 0x8e, 0x17, 0x5b, 0xe4, 0x7f, 0x96,
|
||||||
|
0x55, 0x86, 0xf0, 0xfb, 0x45, 0xe2, 0xf8, 0xe8, 0x7d, 0x5f, 0x3a, 0x8b, 0xb0, 0x64, 0x6c, 0x54,
|
||||||
|
0xf6, 0x8c, 0x4e, 0xa4, 0x12, 0xa3, 0x26, 0x7f, 0x22, 0x5b, 0x19, 0x98, 0xba, 0xd4, 0xad, 0x9f,
|
||||||
|
0x5f, 0x63, 0xaf, 0x1f, 0x97, 0x8c, 0x17, 0x49, 0xa3, 0x30, 0x71, 0xd2, 0xb0, 0x49, 0x2c, 0x75,
|
||||||
|
0xa2, 0x89, 0xda, 0x50, 0xfd, 0x9b, 0x08, 0x41, 0x98, 0x2d, 0x8c, 0x0b, 0x92, 0x79, 0xae, 0x60,
|
||||||
|
0x23, 0x83, 0xef, 0x5c, 0xda, 0xce, 0x71, 0x08, 0x2d, 0x0b, 0x17, 0x62, 0x56, 0x48, 0xc5, 0xe2,
|
||||||
|
0x0f, 0x22, 0x1d, 0xdc, 0xcc, 0xc2, 0x82, 0xa6, 0x8e, 0xec, 0x1e, 0x04, 0x0c, 0x08, 0xaa, 0xa2,
|
||||||
|
0x80, 0x6c, 0x4f, 0xd3, 0x6f, 0xc1, 0x9b, 0x76, 0x9a, 0x58, 0xaa, 0x25, 0x56, 0xef, 0xf2, 0xfd,
|
||||||
|
0x77, 0xf0, 0x32, 0x4e, 0x83, 0xe2, 0x30, 0x25, 0xa5, 0x5a, 0xf1, 0xdf, 0xc8, 0x75, 0xa2, 0xb6,
|
||||||
|
0x2b, 0xfd, 0xac, 0xbc, 0x63, 0x09, 0xa1, 0xcc, 0x2b, 0xf5, 0x46, 0xef, 0x49, 0x83, 0x8f, 0x0b,
|
||||||
|
0xc1, 0x47, 0xac, 0x5a, 0x00, 0x2e, 0xec, 0x1c, 0xd0, 0x0a, 0x66, 0xb5, 0xee, 0x9e, 0xad, 0xc9,
|
||||||
|
0x64, 0x2d, 0xcb, 0x98, 0x0f, 0x43, 0xa6, 0xe1, 0x4c, 0xe7, 0x9f, 0x2d, 0xab, 0x94, 0x01, 0xd7,
|
||||||
|
0x52, 0x84, 0x19 };
|
||||||
|
|
||||||
|
static const size_t key_pos = 0x69a0;
|
||||||
|
|
||||||
|
static const uint8_t gmac_expected[] = { 0x6f, 0x81, 0x10, 0x97 };
|
||||||
|
|
||||||
|
ChiakiGKCrypt gkcrypt;
|
||||||
|
memset(&gkcrypt, 0, sizeof(gkcrypt));
|
||||||
|
|
||||||
|
memcpy(gkcrypt.key_gmac_current, gkcrypt_key, sizeof(gkcrypt.key_gmac_current));
|
||||||
|
memcpy(gkcrypt.iv, gkcrypt_iv, sizeof(gkcrypt.iv));
|
||||||
|
gkcrypt.key_buf = NULL;
|
||||||
|
gkcrypt.key_buf_size = 0;
|
||||||
|
gkcrypt.key_gmac_index_next = 1;
|
||||||
|
|
||||||
|
uint8_t gmac[CHIAKI_GKCRYPT_GMAC_SIZE];
|
||||||
|
ChiakiErrorCode err = chiaki_gkcrypt_gmac(&gkcrypt, key_pos, buf, sizeof(buf), gmac);
|
||||||
|
if(err != CHIAKI_ERR_SUCCESS)
|
||||||
|
return MUNIT_ERROR;
|
||||||
|
|
||||||
|
munit_assert_memory_equal(sizeof(gmac), gmac, gmac_expected);
|
||||||
|
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static MunitResult test_gen_gmac_key(const MunitParameter params[], void *user)
|
||||||
|
{
|
||||||
|
static const uint8_t key_initial[] = { 0xbe, 0xeb, 0xa0, 0xf0, 0x3d, 0x05, 0x70, 0x7d, 0x3a, 0xc7, 0x3c, 0xd7, 0x32, 0xb9, 0x48, 0x01 };
|
||||||
|
static const uint8_t iv[] = { 0xe8, 0x71, 0x87, 0xe7, 0x63, 0xe0, 0xdf, 0x46, 0x3d, 0xc2, 0x02, 0x4a, 0x2c, 0xd2, 0x9c, 0x45 };
|
||||||
|
static const uint8_t key_result[] = { 0xe3, 0xdb, 0x92, 0xd9, 0xdd, 0xd3, 0x68, 0x99, 0xae, 0xfd, 0x9b, 0x15, 0xe1, 0xa6, 0x87, 0x8b };
|
||||||
|
|
||||||
|
ChiakiGKCrypt gkcrypt;
|
||||||
|
memset(&gkcrypt, 0, sizeof(gkcrypt));
|
||||||
|
memcpy(gkcrypt.key_gmac_base, key_initial, sizeof(gkcrypt.key_gmac_base));
|
||||||
|
memcpy(gkcrypt.iv, iv, sizeof(gkcrypt.iv));
|
||||||
|
|
||||||
|
uint8_t key_out[CHIAKI_GKCRYPT_BLOCK_SIZE];
|
||||||
|
chiaki_gkcrypt_gen_gmac_key(1, key_initial, iv, key_out);
|
||||||
|
|
||||||
|
munit_assert_memory_equal(sizeof(key_out), key_out, key_result);
|
||||||
|
|
||||||
|
return MUNIT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
MunitTest tests_gkcrypt[] = {
|
MunitTest tests_gkcrypt[] = {
|
||||||
{
|
{
|
||||||
|
@ -170,7 +236,23 @@ MunitTest tests_gkcrypt[] = {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"/en_decrypt",
|
"/en_decrypt",
|
||||||
test_key_stream,
|
test_endecrypt,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
MUNIT_TEST_OPTION_NONE,
|
||||||
|
NULL
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/gmac",
|
||||||
|
test_gmac,
|
||||||
|
NULL,
|
||||||
|
NULL,
|
||||||
|
MUNIT_TEST_OPTION_NONE,
|
||||||
|
NULL
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"/gen_gmac_key",
|
||||||
|
test_gen_gmac_key,
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
NULL,
|
||||||
MUNIT_TEST_OPTION_NONE,
|
MUNIT_TEST_OPTION_NONE,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue