Commit 5eb86ffe authored by cRTrn13's avatar cRTrn13
Browse files

Debug logging code + AACS fixes. libaacs tested working until mk.

parent 6b204f33
......@@ -2,12 +2,13 @@
#include "configfile.h"
#include "file.h"
#include "../util/macro.h"
#include "../util/logging.h"
uint8_t *configfile_record(CONFIGFILE *kf, enum configfile_types type, uint16_t *entries, size_t *entry_len)
{
size_t pos = 0, len = 0;
while (pos + 4 <= len) {
while (pos + 4 <= kf->size) {
len = MKINT_BE24(kf->buf + pos + 1);
if (entries) {
......@@ -18,8 +19,11 @@ uint8_t *configfile_record(CONFIGFILE *kf, enum configfile_types type, uint16_t
*entry_len = MKINT_BE32(kf->buf + pos + 6);
}
if (kf->buf[pos] == type)
if (kf->buf[pos] == type) {
DEBUG(DBG_CONFIGFILE, "CONFIGFILE record 0x%02x retrieved (0x%08x)\n", type, kf->buf + pos + 10);
return kf->buf + pos + 10; // only return ptr to first byte of entry
}
pos += len;
}
......@@ -32,6 +36,8 @@ CONFIGFILE *configfile_open(const char *path)
FILE_H *fp = NULL;
CONFIGFILE *kf = malloc(sizeof(CONFIGFILE));
DEBUG(DBG_CONFIGFILE, "Opening configfile %s... (0x%08x)\n", path, kf);
if ((fp = file_open(path, "rb"))) {
file_seek(fp, 0, SEEK_END);
kf->size = file_tell(fp);
......@@ -43,8 +49,6 @@ CONFIGFILE *configfile_open(const char *path)
file_close(fp);
X_FREE(fp);
return kf;
}
......@@ -53,6 +57,8 @@ CONFIGFILE *configfile_open(const char *path)
void configfile_close(CONFIGFILE *kf)
{
DEBUG(DBG_CONFIGFILE, "configfile closed (0x%08x)\n", kf);
X_FREE(kf->buf);
X_FREE(kf);
}
......@@ -4,6 +4,7 @@
#include "file.h"
#include "../util/macro.h"
#include "../util/logging.h"
FILE_H *file_open_linux(const char* filename, const char *mode);
void file_close_linux(FILE_H *file);
......@@ -16,6 +17,9 @@ int file_write_linux(FILE_H *file, uint8_t *buf, int64_t size);
void file_close_linux(FILE_H *file)
{
fclose((FILE *)file->internal);
DEBUG(DBG_FILE, "Closed LINUX file (0x%08x)\n", file);
X_FREE(file);
}
......@@ -44,6 +48,7 @@ FILE_H *file_open_linux(const char* filename, const char *mode)
FILE *fp = NULL;
FILE_H *file = malloc(sizeof(FILE_H));
DEBUG(DBG_CONFIGFILE, "Opening LINUX file %s... (0x%08x)\n", filename, file);
file->close = file_close_linux;
file->seek = file_seek_linux;
file->read = file_read_linux;
......@@ -56,5 +61,9 @@ FILE_H *file_open_linux(const char* filename, const char *mode)
return file;
}
DEBUG(DBG_FILE, "Error opening file! (0x%08x)\n", file);
X_FREE(file);
return NULL;
}
......@@ -7,6 +7,7 @@
#include "crypto.h"
#include "mmc.h"
#include "../util/macro.h"
#include "../util/logging.h"
#include "../file/file.h"
int _calc_pk(AACS_KEYS *aacs);
......@@ -16,47 +17,52 @@ int _calc_uks(AACS_KEYS *aacs, const char *path);
int _validate_pk(uint8_t *pk, uint8_t *cvalue, uint8_t *uv, uint8_t *vd, uint8_t *mk);
int _verify_ts(uint8_t *buf, size_t size);
int _calc_mk(AACS_KEYS *aacs, const char *path)
{
DEBUG(DBG_AACS, "Calculate media key...\n");
int a, num_uvs = 0;
char f_name[100];
size_t len;
uint8_t *buf = NULL, *rec, *uvs, *key_pos, *pks;
uint16_t num_pks;
MKB *mkb = NULL;
snprintf(f_name, 100, "%s/AACS/MKB_RO.inf", path);
mkb = mkb_open(f_name);
uvs = mkb_subdiff_records(mkb, &len);
rec = uvs;
while (rec < buf + len) {
if (rec[0] & 0xc0)
break;
rec += 5;
num_uvs++;
}
if ((mkb = mkb_open(path))) {
DEBUG(DBG_AACS, "Get UVS...\n");
uvs = mkb_subdiff_records(mkb, &len);
rec = uvs;
while (rec < uvs + len) {
if (rec[0] & 0xc0)
break;
rec += 5;
num_uvs++;
}
rec = mkb_cvalues(mkb, &len);
pks = configfile_record(aacs->kf, KF_PK_ARRAY, &num_pks, NULL);
key_pos = pks;
while (key_pos < pks + num_pks * 16) {
memcpy(aacs->pk, key_pos, 16);
for (a = 0; a < num_uvs; a++)
if (_validate_pk(aacs->pk, rec + a * 16, uvs + 1 + a * 5, mkb_mk_dv(mkb), aacs->mk)) {
mkb_close(mkb);
X_FREE(buf);
return 1;
DEBUG(DBG_AACS, "Get cvalues...\n");
rec = mkb_cvalues(mkb, &len);
if ((pks = configfile_record(aacs->kf, KF_PK_ARRAY, &num_pks, NULL))) {
key_pos = pks;
while (key_pos < pks + num_pks * 16) {
memcpy(aacs->pk, key_pos, 16);
DEBUG(DBG_AACS, "Trying processing key...\n");
for (a = 0; a < num_uvs; a++) {
if (_validate_pk(aacs->pk, rec + a * 16, uvs + 1 + a * 5, mkb_mk_dv(mkb), aacs->mk)) {
mkb_close(mkb);
X_FREE(buf);
return 1;
}
}
key_pos += 16;
}
}
key_pos += 16;
mkb_close(mkb);
X_FREE(buf);
}
mkb_close(mkb);
X_FREE(buf);
return 0;
}
......@@ -97,7 +103,7 @@ int _calc_uks(AACS_KEYS *aacs, const char *path)
FILE_H *fp = NULL;
unsigned char buf[16];
char f_name[100];
off_t f_pos;
uint64_t f_pos;
snprintf(f_name, 100, "/%s/AACS/Unit_Key_RO.inf", path);
......@@ -126,6 +132,12 @@ int _validate_pk(uint8_t *pk, uint8_t *cvalue, uint8_t *uv, uint8_t *vd, uint8_t
AES_KEY aes;
uint8_t dec_vd[16];
DEBUG(DBG_AACS, "Validate processing key %s...\n", print_hex(pk, 16));
DEBUG(DBG_AACS, " Using:\n");
DEBUG(DBG_AACS, " UV: %s\n", print_hex(uv, 4));
DEBUG(DBG_AACS, " cvalue: %s\n", print_hex(cvalue, 16));
DEBUG(DBG_AACS, " Verification data: %s\n", print_hex(vd, 16));
AES_set_decrypt_key(pk, 128, &aes);
AES_decrypt(cvalue, mk, &aes);
......@@ -137,6 +149,7 @@ int _validate_pk(uint8_t *pk, uint8_t *cvalue, uint8_t *uv, uint8_t *vd, uint8_t
AES_decrypt(vd, dec_vd, &aes);
if (!memcmp(dec_vd, "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8)) {
DEBUG(DBG_AACS, "Processing key is valid!\n");
return 1;
}
......@@ -149,12 +162,16 @@ AACS_KEYS *aacs_open(const char *path, const char *configfile_path)
aacs->kf = NULL;
if ((aacs->kf = configfile_open(configfile_path))) {
_calc_pk(aacs);
_calc_mk(aacs, path);
_calc_vuk(aacs, path);
_calc_uks(aacs, path);
return aacs;
DEBUG(DBG_AACS, "Starting AACS waterfall...\n");
//_calc_pk(aacs);
if (_calc_mk(aacs, path)) {
if (_calc_vuk(aacs, path)) {
if (_calc_uks(aacs, path)) {
DEBUG(DBG_AACS, "AACS initialized (0x%08x)!\n", aacs);
return aacs;
}
}
}
}
return NULL;
......
......@@ -16,7 +16,7 @@
typedef struct aacs_keys AACS_KEYS;
struct aacs_keys {
uint8_t *pk, *mk, *vuk;
uint8_t pk[16], mk[16], vuk[16];
uint8_t *uks; // unit key array (size = 16 * num_uks, each key is at 16-byte offset)
uint16_t num_uks; // number of unit keys
uint8_t iv[16];
......
......@@ -2,8 +2,9 @@
#include <malloc.h>
#include "mkb.h"
#include "../util/macro.h"
#include "../file/file.h"
#include "../util/macro.h"
#include "../util/logging.h"
uint8_t *_record(MKB *mkb, uint8_t type, size_t *rec_len); // returns ptr to requests MKB record
......@@ -11,15 +12,18 @@ uint8_t *_record(MKB *mkb, uint8_t type, size_t *rec_len)
{
size_t pos = 0, len = 0;
while (pos + 4 <= len) {
while (pos + 4 <= mkb->size) {
len = MKINT_BE24(mkb->buf + pos + 1);
if (rec_len) {
*rec_len = len;
}
if (mkb->buf[pos] == type)
if (mkb->buf[pos] == type) {
DEBUG(DBG_MKB, "Retrieved MKB record 0x%02x (0x%08x)\n", type, mkb->buf + pos);
return mkb->buf + pos;
}
pos += len;
}
......@@ -34,6 +38,7 @@ MKB *mkb_open(const char *path)
MKB *mkb = malloc(sizeof(MKB));
snprintf(f_name, 100, "%s/AACS/MKB_RO.inf", path);
DEBUG(DBG_MKB, "Opening MKB %s... (0x%08x)\n", f_name, mkb);
if ((fp = file_open(f_name, "rb"))) {
file_seek(fp, 0, SEEK_END);
......@@ -44,13 +49,15 @@ MKB *mkb_open(const char *path)
file_read(fp, mkb->buf, mkb->size);
file_close(fp);
X_FREE(fp);
DEBUG(DBG_MKB, "MKB size: %d (0x%08x)\n", mkb->size, mkb);
DEBUG(DBG_MKB, "MKB version: %d (0x%08x)\n", mkb_version(mkb), mkb);
file_close(fp);
return mkb;
}
DEBUG(DBG_MKB, "Error opening MKB! (0x%08x)\n", mkb);
return NULL;
}
......
#include <string.h>
#include "logging.h"
const uint32_t master_mask = 0xffff; // this is only temporary
char out[512];
#define HEX_PRINT(X,Y) { int zz; for(zz = 0; zz < Y; zz++) fprintf(stderr, "%02X", X[zz]); fprintf(stderr, "\n"); }
char *print_hex(uint8_t *buf, int count)
{
memset(out, 0, count);
int zz;
for(zz = 0; zz < count; zz++) {
sprintf(out + (zz * 2), "%02X", buf[zz]);
}
return out;
}
void debug(char *file, int line, uint32_t mask, const char *format, ...)
{
uint32_t type = (mask & master_mask) & 0xfffe,
verbose = !((!(master_mask & 1)) & (mask & 1));
if (type && verbose) {
char buffer[512];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
fprintf(stderr, "%s:%d: %s", file, line, buffer);
}
}
#ifndef LOGGING_H_
#define LOGGING_H_
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#define DEBUG(X,Y,...) debug(__FILE__,__LINE__,X,Y,##__VA_ARGS__)
enum {
DBG_CONFIGFILE = 2,
DBG_FILE = 4,
DBG_AACS = 8,
DBG_MKB = 16,
} debug_mask;
char *print_hex(uint8_t *str, int count);
void debug(char *file, int line, uint32_t mask, const char *format, ...);
#endif /* LOGGING_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment