diff options
author | Cástor Muñoz <cmvidal@gmail.com> | 2016-02-05 00:50:53 +0100 |
---|---|---|
committer | Cástor Muñoz <cmvidal@gmail.com> | 2016-08-19 00:15:44 +0200 |
commit | bf89c757ddee815ebdd42bba91413daa96d50b75 (patch) | |
tree | cdb4f95963e5b85d7fd3608bf44aa857b9e2016d | |
parent | 57a44854e30e5a6b082b3816c0ca0ff83240474f (diff) | |
download | rockbox-bf89c757ddee815ebdd42bba91413daa96d50b75.tar.gz rockbox-bf89c757ddee815ebdd42bba91413daa96d50b75.zip |
iPod Classic: hardware AES and SHA1 for bootloader
Based on emCORE.
Low level functions that do not depend on Rockbox kernel,
intended to be used by the bootloader, dualboot-installer,
RB drivers or other .dfu tools.
Change-Id: I3c616ded42260c6626bda23b7e580791981df61d
-rw-r--r-- | firmware/SOURCES | 1 | ||||
-rw-r--r-- | firmware/target/arm/s5l8702/crypto-s5l8702.c | 102 | ||||
-rw-r--r-- | firmware/target/arm/s5l8702/crypto-s5l8702.h | 46 |
3 files changed, 149 insertions, 0 deletions
diff --git a/firmware/SOURCES b/firmware/SOURCES index ec37b707de..391b9af185 100644 --- a/firmware/SOURCES +++ b/firmware/SOURCES | |||
@@ -1629,6 +1629,7 @@ target/arm/s5l8702/ipod6g/audio-ipod6g.c | |||
1629 | target/arm/s5l8702/ipod6g/adc-ipod6g.c | 1629 | target/arm/s5l8702/ipod6g/adc-ipod6g.c |
1630 | #else | 1630 | #else |
1631 | target/arm/s5l8702/spi-s5l8702.c | 1631 | target/arm/s5l8702/spi-s5l8702.c |
1632 | target/arm/s5l8702/crypto-s5l8702.c | ||
1632 | #endif | 1633 | #endif |
1633 | #endif | 1634 | #endif |
1634 | 1635 | ||
diff --git a/firmware/target/arm/s5l8702/crypto-s5l8702.c b/firmware/target/arm/s5l8702/crypto-s5l8702.c new file mode 100644 index 0000000000..e81711add2 --- /dev/null +++ b/firmware/target/arm/s5l8702/crypto-s5l8702.c | |||
@@ -0,0 +1,102 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright © 2009 Michael Sparmann | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include <stdint.h> | ||
22 | #include <stdbool.h> | ||
23 | #include <string.h> | ||
24 | |||
25 | #include "config.h" | ||
26 | #include "system.h" | ||
27 | |||
28 | #include "s5l8702.h" | ||
29 | #include "clocking-s5l8702.h" | ||
30 | #include "crypto-s5l8702.h" | ||
31 | |||
32 | void hwkeyaes(enum hwkeyaes_direction direction, uint32_t keyidx, void* data, uint32_t size) | ||
33 | { | ||
34 | int i; | ||
35 | clockgate_enable(CLOCKGATE_AES, true); | ||
36 | for (i = 0; i < 4; i++) AESIV[i] = 0; | ||
37 | AESUNKREG0 = 1; | ||
38 | AESUNKREG0 = 0; | ||
39 | AESCONTROL = 1; | ||
40 | AESUNKREG1 = 0; | ||
41 | AESTYPE = keyidx; | ||
42 | AESTYPE2 = ~AESTYPE; | ||
43 | AESUNKREG2 = 0; | ||
44 | AESKEYLEN = direction == HWKEYAES_ENCRYPT ? 9 : 8; | ||
45 | AESOUTSIZE = size; | ||
46 | AESOUTADDR = data; | ||
47 | AESINSIZE = size; | ||
48 | AESINADDR = data; | ||
49 | AESAUXSIZE = size; | ||
50 | AESAUXADDR = data; | ||
51 | AESSIZE3 = size; | ||
52 | commit_discard_dcache(); | ||
53 | AESGO = 1; | ||
54 | while (!(AESSTATUS & 0xf)); | ||
55 | clockgate_enable(CLOCKGATE_AES, false); | ||
56 | } | ||
57 | |||
58 | void sha1(void* data, uint32_t size, void* hash) | ||
59 | { | ||
60 | int i, space; | ||
61 | bool done = false; | ||
62 | uint32_t tmp32[16]; | ||
63 | uint8_t* tmp8 = (uint8_t*)tmp32; | ||
64 | uint32_t* databuf = (uint32_t*)data; | ||
65 | uint32_t* hashbuf = (uint32_t*)hash; | ||
66 | clockgate_enable(CLOCKGATE_SHA, true); | ||
67 | SHA1RESET = 1; | ||
68 | while (SHA1CONFIG & 1); | ||
69 | SHA1RESET = 0; | ||
70 | SHA1CONFIG = 0; | ||
71 | while (!done) | ||
72 | { | ||
73 | space = ((uint32_t)databuf) - ((uint32_t)data) - size + 64; | ||
74 | if (space > 0) | ||
75 | { | ||
76 | for (i = 0; i < 16; i++) tmp32[i] = 0; | ||
77 | if (space <= 64) | ||
78 | { | ||
79 | for (i = 0; i < 64 - space; i++) | ||
80 | tmp8[i] = ((uint8_t*)databuf)[i]; | ||
81 | tmp8[64 - space] = 0x80; | ||
82 | } | ||
83 | if (space >= 8) | ||
84 | { | ||
85 | tmp8[0x3b] = (size >> 29) & 0xff; | ||
86 | tmp8[0x3c] = (size >> 21) & 0xff; | ||
87 | tmp8[0x3d] = (size >> 13) & 0xff; | ||
88 | tmp8[0x3e] = (size >> 5) & 0xff; | ||
89 | tmp8[0x3f] = (size << 3) & 0xff; | ||
90 | done = true; | ||
91 | } | ||
92 | for (i = 0; i < 16; i++) SHA1DATAIN[i] = tmp32[i]; | ||
93 | } | ||
94 | else | ||
95 | for (i = 0; i < 16; i++) SHA1DATAIN[i] = *databuf++; | ||
96 | SHA1CONFIG |= 2; | ||
97 | while (SHA1CONFIG & 1); | ||
98 | SHA1CONFIG |= 8; | ||
99 | } | ||
100 | for (i = 0; i < 5; i++) *hashbuf++ = SHA1RESULT[i]; | ||
101 | clockgate_enable(CLOCKGATE_SHA, false); | ||
102 | } | ||
diff --git a/firmware/target/arm/s5l8702/crypto-s5l8702.h b/firmware/target/arm/s5l8702/crypto-s5l8702.h new file mode 100644 index 0000000000..541a67889c --- /dev/null +++ b/firmware/target/arm/s5l8702/crypto-s5l8702.h | |||
@@ -0,0 +1,46 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id: | ||
9 | * | ||
10 | * Copyright © 2009 Michael Sparmann | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #ifndef __CRYPTO_S5L8702_H__ | ||
22 | #define __CRYPTO_S5L8702_H__ | ||
23 | |||
24 | #include <stdint.h> | ||
25 | |||
26 | #include "config.h" | ||
27 | |||
28 | #define SHA1_SZ 20 /* bytes */ | ||
29 | |||
30 | enum hwkeyaes_direction | ||
31 | { | ||
32 | HWKEYAES_DECRYPT = 0, | ||
33 | HWKEYAES_ENCRYPT = 1 | ||
34 | }; | ||
35 | |||
36 | enum hwkeyaes_keyidx | ||
37 | { | ||
38 | HWKEYAES_GKEY = 1, /* device model key */ | ||
39 | HWKEYAES_UKEY = 2 /* device unique key */ | ||
40 | }; | ||
41 | |||
42 | void hwkeyaes(enum hwkeyaes_direction direction, | ||
43 | uint32_t keyidx, void* data, uint32_t size); | ||
44 | void sha1(void* data, uint32_t size, void* hash); | ||
45 | |||
46 | #endif /* __CRYPTO_S5L8702_H__ */ | ||