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

aacs_keys struct

parent beee3383
......@@ -6,46 +6,46 @@
#include "aacs.h"
#include "crypto.h"
#include "mmc.h"
#include "../util/macro.h"
#include "../file/file.h"
int _calc_pk(uint8_t *key);
int _calc_mk(uint8_t *key);
int _calc_vuk(uint8_t *key, const char *path);
int _calc_uks(AACS *aacs, uint8_t *vuk, const char *path);
int _calc_pk(AACS_KEYS *aacs);
int _calc_mk(AACS_KEYS *aacs);
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 _calc_vuk(uint8_t *key, const char *path)
int _calc_vuk(AACS_KEYS *aacs, const char *path)
{
int a;
AES_KEY aes;
uint8_t vid[16];
/*MMC* mmc = NULL;
MMC* mmc = NULL;
if ((mmc = mmc_open(path))) {
if (mmc_read_vid(mmc, vid)) {*/
AES_set_decrypt_key(key, 128, &aes);
AES_decrypt(vid, key, &aes);
if ((mmc = mmc_open(path, aacs->host_priv_key, aacs->host_cert, aacs->host_nonce, aacs->host_key_point))) {
if (mmc_read_vid(mmc)) {
AES_set_decrypt_key(aacs->mk, 128, &aes);
AES_decrypt(vid, aacs->vuk, &aes);
for (a = 0; a < 16; a++) {
key[a] ^= vid[a];
aacs->vuk[a] ^= vid[a];
}
/*
mmc_close(drive);
mmc_close(mmc);
return 1;
}
mmc_close(drive);
}*/
mmc_close(mmc);
}
return 0;
}
int _calc_uks(AACS *aacs, uint8_t *vuk, const char *path)
int _calc_uks(AACS_KEYS *aacs, const char *path)
{
AES_KEY aes;
FILE_H *fp = NULL;
......@@ -63,7 +63,7 @@ int _calc_uks(AACS *aacs, uint8_t *vuk, const char *path)
file_seek(fp, f_pos, SEEK_SET);
file_read(fp, buf, 16);
AES_set_decrypt_key(vuk, 128, &aes);
AES_set_decrypt_key(aacs->vuk, 128, &aes);
AES_decrypt(buf, aacs->uks, &aes);
file_close(fp);
......@@ -97,26 +97,25 @@ int _validate_pk(uint8_t *pk, uint8_t *cvalue, uint8_t *uv, uint8_t *vd, uint8_t
return 0;
}
AACS *aacs_open(const char *path)
AACS_KEYS *aacs_open(const char *path)
{
uint8_t key[16];
AACS *aacs = malloc(sizeof(AACS));
AACS_KEYS *aacs = malloc(sizeof(AACS_KEYS));
// perform aacs waterfall
_calc_pk(key);
_calc_mk(key);
_calc_vuk(key, path);
_calc_uks(aacs, key, path);
_calc_pk(aacs);
_calc_mk(aacs);
_calc_vuk(aacs, path);
_calc_uks(aacs, path);
return aacs;
}
void aacs_close(AACS *aacs)
void aacs_close(AACS_KEYS *aacs)
{
X_FREE(aacs);
}
int aacs_decrypt_unit(AACS *aacs, uint8_t *buf)
int aacs_decrypt_unit(AACS_KEYS *aacs, uint8_t *buf)
{
int a;
AES_KEY aes;
......
......@@ -12,14 +12,15 @@
#include "mkb.h"
typedef struct aacs AACS;
struct aacs {
uint8_t *uks; // unit key array (size = 16 * num_uks, each key is at 16-byte offset)
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)
uint16_t num_uks; // number of unit keys
uint8_t host_priv_key[10], host_cert[92], host_nonce[20], host_key_point[40]; // host keys
};
AACS *aacs_open(const char *path);
void aacs_close(AACS *aacs);
int aacs_decrypt_unit(AACS *aacs, uint8_t *buf);
AACS_KEYS *aacs_open(const char *path);
void aacs_close(AACS_KEYS *aacs);
int aacs_decrypt_unit(AACS_KEYS *aacs, uint8_t *buf);
#endif /* AACS_H_ */
......@@ -23,20 +23,24 @@ void _aesg3(const uint8_t *src_key, uint8_t *dst_key, uint8_t inc)
AES_set_decrypt_key(src_key, 128, &aes);
AES_decrypt(seed, dst_key, &aes);
for (a = 0; a < 16; a++)
for (a = 0; a < 16; a++) {
dst_key[a] ^= seed[a];
}
}
void crypto_aesg3(const uint8_t *D, uint8_t *lsubk, uint8_t* rsubk, uint8_t *pk)
{
if (lsubk)
if (lsubk) {
_aesg3(D, lsubk, 0);
}
if (pk)
if (pk) {
_aesg3(D, pk, 1);
}
if (rsubk)
if (rsubk) {
_aesg3(D, rsubk, 2);
}
}
void crypto_aacs_sign(const uint8_t *c, const uint8_t *pubk, uint8_t *sig, uint8_t *n, const uint8_t *dhp)
......
......@@ -128,7 +128,7 @@ void mmc_close(MMC *mmc)
X_FREE(mmc);
}
void mmc_read_vid(MMC *mmc, int patched)
int mmc_read_vid(MMC *mmc)
{
int a;
uint8_t agid, buf[116], cmd[16], hkp[40], hks[40], dn[20], dc[92], dkp[40], dks[40];
......@@ -144,6 +144,7 @@ void mmc_read_vid(MMC *mmc, int patched)
_mmc_report_key(mmc, 0, 0, 0, 0, buf, 8);
agid = (buf[7] & 0xff) >> 6;
int patched = 0; //todo: get rid of this
if (!patched) {
memset(buf, 0, 116);
buf[1] = 0x72;
......@@ -179,6 +180,11 @@ void mmc_read_vid(MMC *mmc, int patched)
cmd[9] = 0x24;
cmd[10] = (agid << 6) & 0xc0;
if (_mmc_send_cmd(mmc, cmd, buf, 0, 36) == 0)
if (_mmc_send_cmd(mmc, cmd, buf, 0, 36) == 0) {
memcpy(mmc->vid, buf + 4, 16);
return 1;
}
return 0;
}
......@@ -15,6 +15,6 @@ struct mmc {
MMC *mmc_open(const char *path, uint8_t *host_priv_key, uint8_t *host_cert, uint8_t *host_nonce, uint8_t *host_key_point);
void mmc_close(MMC *drive);
void mmc_read_vid(MMC *drive, int patched);
int mmc_read_vid(MMC *drive);
#endif /* MMC_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