Subversion Repositories freemyipod

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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