-
Notifications
You must be signed in to change notification settings - Fork 42
/
aes.c
135 lines (114 loc) · 3.04 KB
/
aes.c
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* GreenPois0n Cynanide - aes.c
* Copyright (C) 2010 Chronic-Dev Team
* Copyright (C) 2010 Joshua Hill
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aes.h"
#include "common.h"
#include "commands.h"
#include "functions.h"
int(*aes_crypto_cmd)(AesOption option, void* input, void* output, unsigned int size, AesMode mode, void* iv, void* key) = NULL;
int aes_init() {
//printf("Initializing aes\n");
aes_crypto_cmd = find_function("aes_crypto_cmd", TARGET_BASEADDR, TARGET_BASEADDR);
if(aes_crypto_cmd == NULL) {
puts("Unable to find aes_crypto_cmd\n");
} else {
printf("Found aes_crypto_cmd at 0x%x\n", aes_crypto_cmd);
cmd_add("aes", &aes_cmd, "encrypt/decrypt kbag aes keys using gid");
}
return 0;
}
int aes_cmd(int argc, CmdArg* argv) {
int i = 0;
char* kbag = NULL;
char* action = NULL;
unsigned int size = 0;
unsigned char* key = NULL;
if(argc != 3) {
puts("usage: aes <enc/dec> [data]\n");
return 0;
}
kbag = argv[2].string;
action = argv[1].string;
key = (unsigned char*) malloc(kAesSizeMax);
if(!strcmp(action, "dec")) {
size = aes_decrypt_key(kbag, &key);
} else if(!strcmp(action, "enc")) {
size = aes_encrypt_key(kbag, &key);
} else {
free(key);
return -1;
}
// print iv
enter_critical_section();
printf("-iv ");
for(i = 0; i < 16; i++) {
printf("%02x", key[i]);
}
// and key
printf(" -k ");
for(i = 16; i < size; i++) {
printf("%02x", key[i]);
}
printf("\n");
exit_critical_section();
if(key) free(key);
return 0;
}
unsigned int aes_decrypt_key(unsigned char* in, unsigned char** out) {
int i = 0;
unsigned int size = 0;
unsigned int byte = 0;
unsigned char* data = *out;
if(data == NULL) {
return 0;
}
size = strlen(in) / 2;
if(size > kAesSizeMax || size < kAesSizeMin) {
return 0;
}
for(i = 0; i < size; i++) {
sscanf(in, "%02x", &byte);
data[i] = byte;
in += 2;
}
aes_crypto_cmd(kAesDecrypt, data, data, size, kAesTypeGid, 0, 0);
return size;
}
unsigned int aes_encrypt_key(unsigned char* in, unsigned char** out) {
int i = 0;
unsigned int size = 0;
unsigned int byte = 0;
unsigned char* data = *out;
if(data == NULL) {
return 0;
}
size = strlen(in) / 2;
if(size > kAesSizeMax || size < kAesSizeMin) {
return 0;
}
for(i = 0; i < size; i++) {
sscanf(in, "%02x", &byte);
data[i] = byte;
in += 2;
}
aes_crypto_cmd(kAesEncrypt, data, data, size, kAesTypeGid, 0, 0);
return size;
}