Subversion Repositories freemyipod

Rev

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
//
427 farthen 6
//    This file is part of emCORE.
2 theseven 7
//
427 farthen 8
//    emCORE 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
//
427 farthen 13
//    emCORE 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
427 farthen 19
//    with emCORE.  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
 
387 theseven 30
struct mutex hwkeyaes_mutex;
31
 
32
 
143 theseven 33
void hwkeyaes(enum hwkeyaes_direction direction, uint32_t keyidx, void* data, uint32_t size)
2 theseven 34
{
35
    uint32_t ptr, i;
36
    uint32_t go = 1;
387 theseven 37
    mutex_lock(&hwkeyaes_mutex, TIMEOUT_BLOCK);
143 theseven 38
    PWRCON(1) &= ~0x400;
39
    AESTYPE = keyidx;
2 theseven 40
    AESUNKREG0 = 1;
41
    AESUNKREG0 = 0;
42
    AESCONTROL = 1;
143 theseven 43
    AESKEYLEN = direction == HWKEYAES_ENCRYPT ? 9 : 8;
2 theseven 44
    AESOUTSIZE = size;
45
    AESAUXSIZE = 0x10;
46
    AESINSIZE = 0x10;
47
    AESSIZE3 = 0x10;
143 theseven 48
    ptr = direction == HWKEYAES_ENCRYPT ? 0 : (size >> 2) - 4;
49
    while (true)
2 theseven 50
    {
143 theseven 51
        if (direction == HWKEYAES_ENCRYPT)
52
        {
53
            if (ptr >= (size >> 2)) break;
54
            if (ptr != 0)
55
                for (i = 0; i < 4; i++)
56
                    ((uint32_t*)data)[ptr + i] ^= ((uint32_t*)data)[ptr + i - 4];
57
        }
265 theseven 58
        AESOUTADDR = (void*)((uint32_t)data + (ptr << 2));
59
        AESINADDR = (void*)((uint32_t)data + (ptr << 2));
60
        AESAUXADDR = (void*)((uint32_t)data + (ptr << 2));
636 theseven 61
	    invalidate_dcache();
2 theseven 62
        AESSTATUS = 6;
63
        AESGO = go;
64
        go = 3;
598 theseven 65
        while ((AESSTATUS & 6) == 0);
143 theseven 66
        if (direction == HWKEYAES_DECRYPT)
156 theseven 67
		{
68
			if (!ptr) break;
143 theseven 69
            for (i = 0; i < 4; i++)
70
                ((uint32_t*)data)[ptr + i] ^= ((uint32_t*)data)[ptr + i - 4];
156 theseven 71
		}
143 theseven 72
        ptr += direction == HWKEYAES_ENCRYPT ? 4 : -4;
2 theseven 73
    }
74
    AESCONTROL = 0;
143 theseven 75
    PWRCON(1) |= 0x400;
387 theseven 76
    mutex_unlock(&hwkeyaes_mutex);
2 theseven 77
}