aacs.c 2.7 KB
Newer Older
cRTrn13's avatar
cRTrn13 committed
1 2

#include <stdio.h>
cRTrn13's avatar
cRTrn13 committed
3
#include <malloc.h>
cRTrn13's avatar
openssl  
cRTrn13 committed
4 5
#include <string.h>
#include <openssl/aes.h>
cRTrn13's avatar
cRTrn13 committed
6 7

#include "aacs.h"
8
#include "crypto.h"
cRTrn13's avatar
cRTrn13 committed
9
#include "../util/macro.h"
10
#include "../file/file.h"
cRTrn13's avatar
cRTrn13 committed
11

12 13 14
int _calc_pk(uint8_t *key);
int _calc_mk(uint8_t *key);
int _calc_vuk(uint8_t *key, const char *path);
cRTrn13's avatar
openssl  
cRTrn13 committed
15 16
int _calc_uks(AACS *aacs, uint8_t *vuk, const char *path);
int _validate_pk(uint8_t *pk, uint8_t *cvalue, uint8_t *uv, uint8_t *vd, uint8_t *mk);
17 18 19
int _verify_ts(uint8_t *buf);


cRTrn13's avatar
cRTrn13 committed
20

cRTrn13's avatar
openssl  
cRTrn13 committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
int _calc_vuk(uint8_t *key, const char *path)
{
    int a;
    AES_KEY aes;
    uint8_t vid[16];
    /*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);

            for (a = 0; a < 16; a++) {
                key[a] ^= vid[a];
            }
/*
            mmc_close(drive);

            return 1;
        }

        mmc_close(drive);
    }*/

    return 0;
}

int _calc_uks(AACS *aacs, uint8_t *vuk, const char *path)
{
    AES_KEY aes;
51
    FILE_H *fp = NULL;
cRTrn13's avatar
openssl  
cRTrn13 committed
52 53 54 55 56 57
    unsigned char buf[16];
    char f_name[100];
    off_t f_pos;

    snprintf(f_name, 100, "/%s/AACS/Unit_Key_RO.inf", path);

58 59
    if ((fp = file_open(f_name, "rb"))) {
        file_read(fp, buf, 4);
cRTrn13's avatar
openssl  
cRTrn13 committed
60 61 62

        f_pos = MKINT_BE32(buf) + 48;

63 64
        file_seek(fp, f_pos, SEEK_SET);
        file_read(fp, buf, 16);
cRTrn13's avatar
openssl  
cRTrn13 committed
65 66 67 68

        AES_set_decrypt_key(vuk, 128, &aes);
        AES_decrypt(buf, aacs->uks, &aes);

69
        file_close(fp);
cRTrn13's avatar
openssl  
cRTrn13 committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

        return 1;
    }

    return 0;
}

int _validate_pk(uint8_t *pk, uint8_t *cvalue, uint8_t *uv, uint8_t *vd, uint8_t *mk)
{
    int a;
    AES_KEY aes;
    uint8_t dec_vd[16];

    AES_set_decrypt_key(pk, 128, &aes);
    AES_decrypt(cvalue, mk, &aes);

    for (a = 0; a < 4; a++) {
        mk[a + 12] ^= uv[a];
    }

    AES_set_decrypt_key(mk, 128, &aes);
    AES_decrypt(vd, dec_vd, &aes);

    if (!memcmp(dec_vd, "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8)) {
        return 1;
    }

    return 0;
}

cRTrn13's avatar
cRTrn13 committed
100 101
AACS *aacs_open(const char *path)
{
102
    uint8_t key[16];
cRTrn13's avatar
cRTrn13 committed
103
    AACS *aacs = malloc(sizeof(AACS));
104 105 106 107 108

    // perform aacs waterfall
    _calc_pk(key);
    _calc_mk(key);
    _calc_vuk(key, path);
cRTrn13's avatar
openssl  
cRTrn13 committed
109
    _calc_uks(aacs, key, path);
cRTrn13's avatar
cRTrn13 committed
110 111

    return aacs;
cRTrn13's avatar
cRTrn13 committed
112 113 114 115 116 117 118
}

void aacs_close(AACS *aacs)
{
    X_FREE(aacs);
}

119 120
int aacs_decrypt_unit(AACS *aacs, uint8_t *buf)
{
cRTrn13's avatar
openssl  
cRTrn13 committed
121 122
    int a;
    AES_KEY aes;
123 124
    uint8_t seed[16], iv[] = { 0x0b, 0xa0, 0xf8, 0xdd, 0xfe, 0xa6, 0x1f, 0xb3, 0xd8, 0xdf, 0x9f, 0x56, 0x6a, 0x05, 0x0f, 0x78 };

cRTrn13's avatar
openssl  
cRTrn13 committed
125 126
    AES_set_encrypt_key(aacs->uks, 128, &aes);
    AES_encrypt(buf, seed, &aes);
127

cRTrn13's avatar
openssl  
cRTrn13 committed
128
    for (a = 0; a < 16; a++) {
129
        seed[a] ^= buf[a];
cRTrn13's avatar
openssl  
cRTrn13 committed
130
    }
131

cRTrn13's avatar
openssl  
cRTrn13 committed
132 133
    AES_set_decrypt_key( seed, 128, &aes );
    AES_cbc_encrypt(buf + 16,buf + 16, 6144 - 16, &aes, iv, 0);
cRTrn13's avatar
cRTrn13 committed
134 135

    return 1;
136
}