From bf89c757ddee815ebdd42bba91413daa96d50b75 Mon Sep 17 00:00:00 2001 From: Cástor Muñoz Date: Fri, 5 Feb 2016 00:50:53 +0100 Subject: 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 --- firmware/target/arm/s5l8702/crypto-s5l8702.c | 102 +++++++++++++++++++++++++++ firmware/target/arm/s5l8702/crypto-s5l8702.h | 46 ++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 firmware/target/arm/s5l8702/crypto-s5l8702.c create mode 100644 firmware/target/arm/s5l8702/crypto-s5l8702.h (limited to 'firmware/target/arm') 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 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright © 2009 Michael Sparmann + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#include +#include +#include + +#include "config.h" +#include "system.h" + +#include "s5l8702.h" +#include "clocking-s5l8702.h" +#include "crypto-s5l8702.h" + +void hwkeyaes(enum hwkeyaes_direction direction, uint32_t keyidx, void* data, uint32_t size) +{ + int i; + clockgate_enable(CLOCKGATE_AES, true); + for (i = 0; i < 4; i++) AESIV[i] = 0; + AESUNKREG0 = 1; + AESUNKREG0 = 0; + AESCONTROL = 1; + AESUNKREG1 = 0; + AESTYPE = keyidx; + AESTYPE2 = ~AESTYPE; + AESUNKREG2 = 0; + AESKEYLEN = direction == HWKEYAES_ENCRYPT ? 9 : 8; + AESOUTSIZE = size; + AESOUTADDR = data; + AESINSIZE = size; + AESINADDR = data; + AESAUXSIZE = size; + AESAUXADDR = data; + AESSIZE3 = size; + commit_discard_dcache(); + AESGO = 1; + while (!(AESSTATUS & 0xf)); + clockgate_enable(CLOCKGATE_AES, false); +} + +void sha1(void* data, uint32_t size, void* hash) +{ + int i, space; + bool done = false; + uint32_t tmp32[16]; + uint8_t* tmp8 = (uint8_t*)tmp32; + uint32_t* databuf = (uint32_t*)data; + uint32_t* hashbuf = (uint32_t*)hash; + clockgate_enable(CLOCKGATE_SHA, true); + SHA1RESET = 1; + while (SHA1CONFIG & 1); + SHA1RESET = 0; + SHA1CONFIG = 0; + while (!done) + { + space = ((uint32_t)databuf) - ((uint32_t)data) - size + 64; + if (space > 0) + { + for (i = 0; i < 16; i++) tmp32[i] = 0; + if (space <= 64) + { + for (i = 0; i < 64 - space; i++) + tmp8[i] = ((uint8_t*)databuf)[i]; + tmp8[64 - space] = 0x80; + } + if (space >= 8) + { + tmp8[0x3b] = (size >> 29) & 0xff; + tmp8[0x3c] = (size >> 21) & 0xff; + tmp8[0x3d] = (size >> 13) & 0xff; + tmp8[0x3e] = (size >> 5) & 0xff; + tmp8[0x3f] = (size << 3) & 0xff; + done = true; + } + for (i = 0; i < 16; i++) SHA1DATAIN[i] = tmp32[i]; + } + else + for (i = 0; i < 16; i++) SHA1DATAIN[i] = *databuf++; + SHA1CONFIG |= 2; + while (SHA1CONFIG & 1); + SHA1CONFIG |= 8; + } + for (i = 0; i < 5; i++) *hashbuf++ = SHA1RESULT[i]; + clockgate_enable(CLOCKGATE_SHA, false); +} 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 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id: + * + * Copyright © 2009 Michael Sparmann + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +#ifndef __CRYPTO_S5L8702_H__ +#define __CRYPTO_S5L8702_H__ + +#include + +#include "config.h" + +#define SHA1_SZ 20 /* bytes */ + +enum hwkeyaes_direction +{ + HWKEYAES_DECRYPT = 0, + HWKEYAES_ENCRYPT = 1 +}; + +enum hwkeyaes_keyidx +{ + HWKEYAES_GKEY = 1, /* device model key */ + HWKEYAES_UKEY = 2 /* device unique key */ +}; + +void hwkeyaes(enum hwkeyaes_direction direction, + uint32_t keyidx, void* data, uint32_t size); +void sha1(void* data, uint32_t size, void* hash); + +#endif /* __CRYPTO_S5L8702_H__ */ -- cgit v1.2.3