Commit 712be3aa authored by cRTrn13's avatar cRTrn13
Browse files

keyfile

parent 5e344931
#include "keyfile.h"
#include "file.h"
#include "../util/macro.h"
uint8_t *_record(KEYFILE *kf, uint8_t type, size_t *rec_len);
uint8_t *_record(KEYFILE *kf, uint8_t type, size_t *rec_len)
{
return NULL;
}
KEYFILE *keyfile_open(const char *path)
{
FILE_H *fp = NULL;
KEYFILE *kf = malloc(sizeof(KEYFILE));
if ((fp = file_open(path, "rb"))) {
file_seek(fp, 0, SEEK_END);
kf->size = file_tell(fp);
file_seek(fp, 0, SEEK_SET);
kf->buf = malloc(kf->size);
file_read(fp, kf->buf, kf->size);
file_close(fp);
X_FREE(fp);
return kf;
}
return NULL;
}
void keyfile_close(KEYFILE *kf)
{
X_FREE(kf->buf);
X_FREE(kf);
}
#ifndef KEYFILE_H_
#define KEYFILE_H_
#include <stdint.h>
#include <unistd.h>
typedef struct keyfile KEYFILE;
struct keyfile {
size_t size;
uint8_t *buf;
};
KEYFILE *keyfile_open(const char *path);
void keyfile_close(KEYFILE *kf);
#endif /* KEYFILE_H_ */
......@@ -11,12 +11,53 @@
#include "../file/file.h"
int _calc_pk(AACS_KEYS *aacs);
int _calc_mk(AACS_KEYS *aacs);
int _calc_mk(AACS_KEYS *aacs, const char *path);
int _calc_vuk(AACS_KEYS *aacs, const char *path);
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);
int _verify_ts(uint8_t *buf, size_t size);
int _calc_mk(AACS_KEYS *aacs, const char *path)
{
int a, num_uvs = 0;
char f_name[100];
size_t len;
uint8_t *buf = NULL, *rec, *uvs, *key_pos;
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++;
}
rec = mkb_cvalues(mkb, &len);
key_pos = aacs->pks;
while (key_pos < aacs->pks + aacs->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;
}
key_pos += 16;
}
mkb_close(mkb);
X_FREE(buf);
return 0;
}
int _calc_vuk(AACS_KEYS *aacs, const char *path)
{
......@@ -97,13 +138,13 @@ int _validate_pk(uint8_t *pk, uint8_t *cvalue, uint8_t *uv, uint8_t *vd, uint8_t
return 0;
}
AACS_KEYS *aacs_open(const char *path)
AACS_KEYS *aacs_open(const char *path, const char *keyfile_path)
{
AACS_KEYS *aacs = malloc(sizeof(AACS_KEYS));
// perform aacs waterfall
_calc_pk(aacs);
_calc_mk(aacs);
_calc_mk(aacs, path);
_calc_vuk(aacs, path);
_calc_uks(aacs, path);
......@@ -119,17 +160,17 @@ int aacs_decrypt_unit(AACS_KEYS *aacs, uint8_t *buf)
{
int a;
AES_KEY aes;
uint8_t seed[16], iv[] = { 0x0b, 0xa0, 0xf8, 0xdd, 0xfe, 0xa6, 0x1f, 0xb3, 0xd8, 0xdf, 0x9f, 0x56, 0x6a, 0x05, 0x0f, 0x78 };
uint8_t key[16], iv[] = { 0x0b, 0xa0, 0xf8, 0xdd, 0xfe, 0xa6, 0x1f, 0xb3, 0xd8, 0xdf, 0x9f, 0x56, 0x6a, 0x05, 0x0f, 0x78 };
AES_set_encrypt_key(aacs->uks, 128, &aes);
AES_encrypt(buf, seed, &aes);
AES_encrypt(buf, key, &aes);
for (a = 0; a < 16; a++) {
seed[a] ^= buf[a];
key[a] ^= buf[a];
}
AES_set_decrypt_key( seed, 128, &aes );
AES_cbc_encrypt(buf + 16,buf + 16, 6144 - 16, &aes, iv, 0);
AES_set_decrypt_key(key, 128, &aes);
AES_cbc_encrypt(buf + 16, buf + 16, 6144 - 16, &aes, iv, 0);
return 1;
}
......@@ -12,14 +12,25 @@
#include "mkb.h"
enum aacs_key_type {
PK_ARRAY,
HOST_PRIV_KEY,
HOST_CERT,
HOST_NONCE,
HOST_KEY_POINT
};
typedef struct aacs_keys AACS_KEYS;
struct aacs_keys {
uint8_t *pk, *mk, *vuk, *uks; // unit key array (size = 16 * num_uks, each key is at 16-byte offset)
uint8_t *pk, *mk, *vuk;
uint8_t *pks, // processing key array (size = 16 * num_pks, each key is at 16-byte offset)
*uks; // unit key array (size = 16 * num_uks, each key is at 16-byte offset)
uint16_t num_uks; // number of unit keys
uint16_t num_pks; // number of processing keys
uint8_t host_priv_key[10], host_cert[92], host_nonce[20], host_key_point[40]; // host keys
};
AACS_KEYS *aacs_open(const char *path);
AACS_KEYS *aacs_open(const char *path, const char *keyfile_path);
void aacs_close(AACS_KEYS *aacs);
int aacs_decrypt_unit(AACS_KEYS *aacs, uint8_t *buf);
......
......@@ -27,7 +27,7 @@ uint8_t *_record(MKB *mkb, uint8_t type, size_t *rec_len)
return NULL;
}
struct mkb *mkb_open(const char *path)
MKB *mkb_open(const char *path)
{
FILE_H *fp = NULL;
char f_name[100];
......
......@@ -3,6 +3,7 @@
#define MACRO_H_
#include <stdio.h>
#include <malloc.h>
#define HEX_PRINT(X,Y) { int zz; for(zz = 0; zz < Y; zz++) fprintf(stderr, "%02X", X[zz]); fprintf(stderr, "\n"); }
#define MKINT_BE16(X) ( (X)[0] << 8 | (X)[1] )
......
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