GMAC working

This commit is contained in:
Florian Märkl 2019-03-26 22:36:02 +01:00
commit d689f9e029
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
8 changed files with 218 additions and 28 deletions

View file

@ -39,7 +39,8 @@ typedef enum
CHIAKI_ERR_MUTEX_LOCKED,
CHIAKI_ERR_CANCELED,
CHIAKI_ERR_TIMEOUT,
CHIAKI_ERR_INVALID_RESPONSE
CHIAKI_ERR_INVALID_RESPONSE,
CHIAKI_ERR_UNINITIALIZED
} ChiakiErrorCode;
CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code);

View file

@ -30,12 +30,17 @@ extern "C" {
#define CHIAKI_GKCRYPT_BLOCK_SIZE 0x10
#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 {
uint8_t *key_buf;
size_t key_buf_size;
uint8_t key[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;
} 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_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); }
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)
{

View file

@ -32,6 +32,7 @@ extern "C" {
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 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
@ -43,6 +44,8 @@ typedef struct chiaki_takion_connect_info_t
void *data_cb_user;
ChiakiTakionAVCallback av_cb;
void *av_cb_user;
ChiakiTakionMACCallback mac_cb;
void *mac_cb_user;
} ChiakiTakionConnectInfo;
@ -53,6 +56,8 @@ typedef struct chiaki_takion_t
void *data_cb_user;
ChiakiTakionAVCallback av_cb;
void *av_cb_user;
ChiakiTakionMACCallback mac_cb;
void *mac_cb_user;
int sock;
ChiakiThread thread;
int stop_pipe[2];

View file

@ -23,6 +23,7 @@
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/sha.h>
#include "utils.h"
@ -46,6 +47,10 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_init(ChiakiGKCrypt *gkcrypt, Chiaki
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;
}
@ -72,18 +77,18 @@ static ChiakiErrorCode gkcrypt_gen_key_iv(ChiakiGKCrypt *gkcrypt, uint8_t index,
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);
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;
do
{
int r = (int)base[i] + v;
uint64_t r = base[i] + v;
out[i] = (uint8_t)(r & 0xff);
v = r >> 8;
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);
}
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)
{
assert(key_pos % CHIAKI_GKCRYPT_BLOCK_SIZE == 0);
@ -102,7 +123,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_gen_key_stream(ChiakiGKCrypt *gkcry
if(!ctx)
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);
return CHIAKI_ERR_UNKNOWN;
@ -153,30 +174,50 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_gkcrypt_decrypt(ChiakiGKCrypt *gkcrypt, siz
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];
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();
if(!ctx)
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);
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);
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);
return CHIAKI_ERR_UNKNOWN;

View file

@ -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 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 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)
@ -90,6 +91,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_nagare_run(ChiakiSession *session)
takion_info.data_cb_user = nagare;
takion_info.av_cb = nagare_takion_av;
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);
free(takion_info.sa);
@ -510,4 +513,13 @@ 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_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);
}

View file

@ -73,6 +73,9 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_senkusha_run(ChiakiSession *session)
takion_info.data_cb = senkusha_takion_data;
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);
free(takion_info.sa);
if(err != CHIAKI_ERR_SUCCESS)

View file

@ -107,6 +107,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_takion_connect(ChiakiTakion *takion, Chiaki
takion->data_cb_user = info->data_cb_user;
takion->av_cb = info->av_cb;
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->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)
{
case TAKION_PACKET_TYPE_MESSAGE:
takion_handle_packet_message(takion, buf+1, buf_size-1);
takion_handle_packet_message(takion, buf, buf_size);
break;
case TAKION_PACKET_TYPE_2:
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;
default:
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)
{
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)
return;
@ -648,24 +650,61 @@ static void takion_handle_packet_av(ChiakiTakion *takion, uint8_t base_type, uin
{
// 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");
return;
}
uint16_t packet_index = ntohs(*((uint16_t *)(buf + 0)));
uint16_t frame_index = ntohs(*((uint16_t *)(buf + 2)));
uint32_t dword_2 = ntohl(*((uint32_t *)(buf + 4)));
uint8_t *gmac = buf + 9; // uint8_t[4]
uint32_t key_pos = ntohl(*((uint32_t *)(buf + 0xd)));
uint8_t unknown_1 = buf[0x11];
uint8_t *av = buf+1;
uint16_t packet_index = ntohs(*((uint16_t *)(av + 0)));
uint16_t frame_index = ntohs(*((uint16_t *)(av + 2)));
uint32_t dword_2 = ntohl(*((uint32_t *)(av + 4)));
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_log_hexdump(takion->log, CHIAKI_LOG_DEBUG, buf, buf_size);
uint8_t *data = buf + AV_HEADER_SIZE;
size_t data_size = buf_size - AV_HEADER_SIZE;
uint8_t *data = av + AV_HEADER_SIZE;
size_t data_size = av_size - AV_HEADER_SIZE;
if(takion->av_cb)
takion->av_cb(data, data_size, base_type, key_pos, takion->av_cb_user);