Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 theseven 1
//
2
//
143 theseven 3
//    Copyright 2010 TheSeven
2 theseven 4
//
5
//
143 theseven 6
//    This file is part of emBIOS.
2 theseven 7
//
143 theseven 8
//    emBIOS is free software: you can redistribute it and/or
2 theseven 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
//
143 theseven 13
//    emBIOS is distributed in the hope that it will be useful,
2 theseven 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
143 theseven 19
//    with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
2 theseven 20
//
21
//
22
 
23
 
143 theseven 24
#include "global.h"
25
#include "hwkeyaes.h"
26
#include "s5l8701.h"
27
#include "thread.h"
2 theseven 28
 
29
 
143 theseven 30
void hwkeyaes(enum hwkeyaes_direction direction, uint32_t keyidx, void* data, uint32_t size)
2 theseven 31
{
32
    uint32_t ptr, i;
33
    uint32_t go = 1;
143 theseven 34
    PWRCON(1) &= ~0x400;
35
    AESTYPE = keyidx;
2 theseven 36
    AESUNKREG0 = 1;
37
    AESUNKREG0 = 0;
38
    AESCONTROL = 1;
143 theseven 39
    AESKEYLEN = direction == HWKEYAES_ENCRYPT ? 9 : 8;
2 theseven 40
    AESOUTSIZE = size;
41
    AESAUXSIZE = 0x10;
42
    AESINSIZE = 0x10;
43
    AESSIZE3 = 0x10;
143 theseven 44
    ptr = direction == HWKEYAES_ENCRYPT ? 0 : (size >> 2) - 4;
45
    while (true)
2 theseven 46
    {
143 theseven 47
        if (direction == HWKEYAES_ENCRYPT)
48
        {
49
            if (ptr >= (size >> 2)) break;
50
            if (ptr != 0)
51
                for (i = 0; i < 4; i++)
52
                    ((uint32_t*)data)[ptr + i] ^= ((uint32_t*)data)[ptr + i - 4];
53
        }
2 theseven 54
        AESOUTADDR = (uint32_t)data + (ptr << 2);
55
        AESINADDR = (uint32_t)data + (ptr << 2);
56
        AESAUXADDR = (uint32_t)data + (ptr << 2);
156 theseven 57
	    clean_dcache();
2 theseven 58
        AESSTATUS = 6;
59
        AESGO = go;
60
        go = 3;
143 theseven 61
        while ((AESSTATUS & 6) == 0) yield();
156 theseven 62
	    invalidate_dcache();
143 theseven 63
        if (direction == HWKEYAES_DECRYPT)
156 theseven 64
		{
65
			if (!ptr) break;
143 theseven 66
            for (i = 0; i < 4; i++)
67
                ((uint32_t*)data)[ptr + i] ^= ((uint32_t*)data)[ptr + i - 4];
156 theseven 68
		}
143 theseven 69
        ptr += direction == HWKEYAES_ENCRYPT ? 4 : -4;
2 theseven 70
    }
71
    AESCONTROL = 0;
143 theseven 72
    PWRCON(1) |= 0x400;
2 theseven 73
}