summaryrefslogtreecommitdiff
path: root/src/crypto.c
blob: f6e2cd81ff25dd556358ebb8f428c230703c9d0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* This file is part of Nekopack.

   Copyright (C) 2017 Jakob Tsar-Fox, All Rights Reserved.

   Nekopack is free software: you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation, either version 3 of the License, or (at
   your option) any later version.

   Nekopack is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Nekopack. If not, see <http://www.gnu.org/licenses/>. */

#include "cli.h"
#include "crypto.h"


/* Returns the encryption keys for a given game value. */
key get_encryption_key(game_type game) {
    key encryption_key;
    switch (game) {
        case NEKOPARA_VOLUME_0:
            encryption_key.master_key = 0x1548e29c;
            encryption_key.initial_fallback_key = 0x9c;
            encryption_key.primary_fallback_key = 0xd7;
            encryption_key.uses_initial_key = 1;
            break;
        case NEKOPARA_VOLUME_0_STEAM:
            encryption_key.master_key = 0x44528b87;
            encryption_key.initial_fallback_key = 0x87;
            encryption_key.primary_fallback_key = 0x23;
            encryption_key.uses_initial_key = 1;
            break;
        case NEKOPARA_VOLUME_1:
            encryption_key.master_key = 0x1548e29c;
            encryption_key.initial_fallback_key = 0x00;
            encryption_key.primary_fallback_key = 0xd7;
            encryption_key.uses_initial_key = 0;
            break;
        case NEKOPARA_VOLUME_1_STEAM:
            encryption_key.master_key = 0x44528b87;
            encryption_key.initial_fallback_key = 0x00;
            encryption_key.primary_fallback_key = 0x23;
            encryption_key.uses_initial_key = 0;
            break;
        default:
            encryption_key.master_key = 0x00000000;
            encryption_key.initial_fallback_key = 0x00;
            encryption_key.primary_fallback_key = 0x00;
            encryption_key.uses_initial_key = 0;
    }
    return encryption_key;
}


/* Decrypts the contents of a buffer according to a file key. */
void decrypt_buffer(Bytef *encrypted_buffer, uint64_t buffer_length,
                    key encryption_key, uint32_t file_key) {
    uint32_t xor_key = file_key ^ encryption_key.master_key;
    uint8_t initial_key = xor_key & 0xff;
    uint8_t primary_key = (xor_key >> 24 ^ xor_key >> 16 ^ \
                           xor_key >> 8 ^ xor_key) & 0xff;
    if (xor_key == 1 && initial_key == 0)
        initial_key = encryption_key.initial_fallback_key;
    else if (primary_key == 0)
        primary_key = encryption_key.primary_fallback_key;
    if (encryption_key.uses_initial_key)
        encrypted_buffer[0] ^= initial_key;
    for (uint64_t i = 0; i < buffer_length; i++) {
        encrypted_buffer[i] ^= primary_key;
    }
}