| 2 |
theseven |
1 |
//
|
|
|
2 |
//
|
|
|
3 |
// Copyright 2009 TheSeven
|
|
|
4 |
//
|
|
|
5 |
//
|
|
|
6 |
// This file is part of the Linux4Nano toolkit.
|
|
|
7 |
//
|
|
|
8 |
// TheSeven's iBugger is free software: you can redistribute it and/or
|
|
|
9 |
// modify it under the terms of the GNU General Public License as
|
|
|
10 |
// published by the Free Software Foundation, either version 2 of the
|
|
|
11 |
// License, or (at your option) any later version.
|
|
|
12 |
//
|
|
|
13 |
// TheSeven's iBugger is distributed in the hope that it will be useful,
|
|
|
14 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
15 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
16 |
// See the GNU General Public License for more details.
|
|
|
17 |
//
|
|
|
18 |
// You should have received a copy of the GNU General Public License along
|
|
|
19 |
// with the Linux4Nano toolkit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
20 |
//
|
|
|
21 |
//
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
#include <toolkit.h>
|
|
|
25 |
#include <aes.h>
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
void aes_encrypt(uint32_t keytype, void* data, uint32_t size)
|
|
|
29 |
{
|
|
|
30 |
uint32_t ptr, i;
|
|
|
31 |
uint32_t go = 1;
|
|
|
32 |
PWRCONEXT &= ~0x400;
|
|
|
33 |
AESTYPE = keytype;
|
|
|
34 |
AESUNKREG0 = 1;
|
|
|
35 |
AESUNKREG0 = 0;
|
|
|
36 |
AESCONTROL = 1;
|
|
|
37 |
AESKEYLEN = 9;
|
|
|
38 |
AESOUTSIZE = size;
|
|
|
39 |
AESAUXSIZE = 0x10;
|
|
|
40 |
AESINSIZE = 0x10;
|
|
|
41 |
AESSIZE3 = 0x10;
|
|
|
42 |
for (ptr = 0; ptr < (size >> 2); ptr += 4)
|
|
|
43 |
{
|
|
|
44 |
AESOUTADDR = (uint32_t)data + (ptr << 2);
|
|
|
45 |
AESINADDR = (uint32_t)data + (ptr << 2);
|
|
|
46 |
AESAUXADDR = (uint32_t)data + (ptr << 2);
|
|
|
47 |
if (ptr != 0)
|
|
|
48 |
for (i = 0; i < 4; i++)
|
|
|
49 |
((uint32_t*)data)[ptr + i] ^= ((uint32_t*)data)[ptr + i - 4];
|
|
|
50 |
clean_dcache();
|
|
|
51 |
AESSTATUS = 6;
|
|
|
52 |
AESGO = go;
|
|
|
53 |
go = 3;
|
|
|
54 |
while ((AESSTATUS & 6) == 0);
|
|
|
55 |
invalidate_dcache();
|
|
|
56 |
}
|
|
|
57 |
AESCONTROL = 0;
|
|
|
58 |
PWRCONEXT |= 0x400;
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
void aes_decrypt(uint32_t keytype, void* data, uint32_t size)
|
|
|
62 |
{
|
|
|
63 |
uint32_t ptr, i;
|
|
|
64 |
uint32_t go = 1;
|
|
|
65 |
PWRCONEXT &= ~0x400;
|
|
|
66 |
AESTYPE = keytype;
|
|
|
67 |
AESUNKREG0 = 1;
|
|
|
68 |
AESUNKREG0 = 0;
|
|
|
69 |
AESCONTROL = 1;
|
|
|
70 |
AESKEYLEN = 8;
|
|
|
71 |
AESOUTSIZE = size;
|
|
|
72 |
AESAUXSIZE = 0x10;
|
|
|
73 |
AESINSIZE = 0x10;
|
|
|
74 |
AESSIZE3 = 0x10;
|
|
|
75 |
for (ptr = (size >> 2) - 4; ; ptr -= 4)
|
|
|
76 |
{
|
|
|
77 |
AESOUTADDR = (uint32_t)data + (ptr << 2);
|
|
|
78 |
AESINADDR = (uint32_t)data + (ptr << 2);
|
|
|
79 |
AESAUXADDR = (uint32_t)data + (ptr << 2);
|
|
|
80 |
clean_dcache();
|
|
|
81 |
AESSTATUS = 6;
|
|
|
82 |
AESGO = go;
|
|
|
83 |
go = 3;
|
|
|
84 |
while ((AESSTATUS & 6) == 0);
|
|
|
85 |
invalidate_dcache();
|
|
|
86 |
if (!ptr) break;
|
|
|
87 |
for (i = 0; i < 4; i++) ((uint32_t*)data)[ptr + i] ^= ((uint32_t*)data)[ptr + i - 4];
|
|
|
88 |
}
|
|
|
89 |
AESCONTROL = 0;
|
|
|
90 |
PWRCONEXT |= 0x400;
|
|
|
91 |
}
|