forked from fail0verflow/mini
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrypto.c
More file actions
123 lines (102 loc) · 2.2 KB
/
Copy pathcrypto.c
File metadata and controls
123 lines (102 loc) · 2.2 KB
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
/*
mini - a Free Software replacement for the Nintendo/BroadOn IOS.
crypto hardware support
Copyright (C) 2008, 2009 Haxx Enterprises <bushing@gmail.com>
Copyright (C) 2008, 2009 Sven Peter <svenpeter@gmail.com>
# This code is licensed to you under the terms of the GNU GPL, version 2;
# see file COPYING or http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*/
#include "crypto.h"
#include "hollywood.h"
#include "utils.h"
#include "memory.h"
#include "irq.h"
#include "gecko.h"
#include "string.h"
#include "seeprom.h"
#define AES_CMD_RESET 0
#define AES_CMD_DECRYPT 0x9800
otp_t otp;
seeprom_t seeprom;
void crypto_read_otp(void)
{
u32 *otpd = (u32*)&otp;
int i;
for (i=0; i< 0x20; i++) {
write32(HW_OTPCMD,0x80000000|i);
*otpd++ = read32(HW_OTPDATA);
}
}
void crypto_read_seeprom(void)
{
seeprom_read(&seeprom, 0, sizeof(seeprom) / 2);
}
void crypto_initialize(void)
{
crypto_read_otp();
crypto_read_seeprom();
write32(AES_CMD, 0);
while (read32(AES_CMD) != 0);
irq_enable(IRQ_AES);
}
static int _aes_irq = 0;
void aes_irq(void)
{
_aes_irq = 1;
}
static inline void aes_command(u16 cmd, u8 iv_keep, u32 blocks)
{
if (blocks != 0)
blocks--;
_aes_irq = 0;
write32(AES_CMD, (cmd << 16) | (iv_keep ? 0x1000 : 0) | (blocks&0x7f));
while (read32(AES_CMD) & 0x80000000);
}
void aes_reset(void)
{
write32(AES_CMD, 0);
while (read32(AES_CMD) != 0);
}
void aes_set_iv(u8 *iv)
{
int i;
for(i = 0; i < 4; i++) {
write32(AES_IV, *(u32 *)iv);
iv += 4;
}
}
void aes_empty_iv(void)
{
int i;
for(i = 0; i < 4; i++)
write32(AES_IV, 0);
}
void aes_set_key(u8 *key)
{
int i;
for(i = 0; i < 4; i++) {
write32(AES_KEY, *(u32 *)key);
key += 4;
}
}
void aes_decrypt(u8 *src, u8 *dst, u32 blocks, u8 keep_iv)
{
int this_blocks = 0;
while(blocks > 0) {
this_blocks = blocks;
if (this_blocks > 0x80)
this_blocks = 0x80;
write32(AES_SRC, dma_addr(src));
write32(AES_DEST, dma_addr(dst));
dc_flushrange(src, blocks * 16);
dc_invalidaterange(dst, blocks * 16);
ahb_flush_to(AHB_AES);
aes_command(AES_CMD_DECRYPT, keep_iv, this_blocks);
ahb_flush_from(AHB_AES);
ahb_flush_to(AHB_STARLET);
blocks -= this_blocks;
src += this_blocks<<4;
dst += this_blocks<<4;
keep_iv = 1;
}
}