Skip to content

Commit

Permalink
rxtools: lib/media/nand: Remove dependency on CTRDecryptor
Browse files Browse the repository at this point in the history
rxtools: lib/media/nand: Remove dependency on CTRDecryptor
rxtools: lib/media/crypto: Add aesCtr
  • Loading branch information
173210 committed Feb 29, 2016
1 parent 9ead299 commit a762bc1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
1 change: 1 addition & 0 deletions rxtools/source/include/lib/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ void aesSelKey(uint32_t keyno);
void aesSetCtr(void* iv);
void aesAddCtr(void* ctr, uint32_t carry);
void aesDecrypt(void *dst, const void *src, uint16_t blocks, uint32_t mode);
void aesCtr(void *dst, const void *src, size_t n, void *ctr);
40 changes: 32 additions & 8 deletions rxtools/source/lib/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ static void fifo(const void* inbuf, void* outbuf, size_t blocks)
}
}

static void start(uint32_t mode)
{
*REG_AESCNT = mode |
AES_CNT_START |
AES_CNT_INPUT_ORDER |
AES_CNT_OUTPUT_ORDER |
AES_CNT_INPUT_ENDIAN |
AES_CNT_OUTPUT_ENDIAN |
AES_CNT_FLUSH_READ |
AES_CNT_FLUSH_WRITE;
}

void aesSetKeyX(uint8_t keyslot, void* keyx)
{
uint32_t * _keyx = (uint32_t*)keyx;
Expand Down Expand Up @@ -178,13 +190,25 @@ void aesDecrypt(void *dst, const void *src, uint16_t blocks, uint32_t mode)
{
*REG_AESCNT = 0;
*REG_AESBLKCNT = blocks << 16;
*REG_AESCNT = mode |
AES_CNT_START |
AES_CNT_INPUT_ORDER |
AES_CNT_OUTPUT_ORDER |
AES_CNT_INPUT_ENDIAN |
AES_CNT_OUTPUT_ENDIAN |
AES_CNT_FLUSH_READ |
AES_CNT_FLUSH_WRITE;
start(mode);
fifo(src, dst, blocks);
}

void aesCtr(void *dst, const void *src, size_t n, void *ctr)
{
*REG_AESBLKCNT = UINT16_MAX << 16;
while (n >= UINT16_MAX) {
aesSetCtr(ctr);
start(AES_CTR_MODE);
fifo(src, dst, UINT16_MAX);
src = (void *)((uintptr_t)src + UINT16_MAX * AES_BLOCK_SIZE);
dst = (void *)((uintptr_t)dst + UINT16_MAX * AES_BLOCK_SIZE);
n -= UINT16_MAX;
aesAddCtr(ctr, UINT16_MAX);
}

*REG_AESBLKCNT = n << 16;
aesSetCtr(ctr);
start(AES_CTR_MODE);
fifo(src, dst, n);
}
34 changes: 19 additions & 15 deletions rxtools/source/lib/media/nand.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/

#include <lib/mpcore.h>
#include <features/decryption.h>
#include <lib/crypto.h>
#include <lib/media/tmio.h>
#include <lib/media/nand.h>
Expand Down Expand Up @@ -95,42 +94,47 @@ void GetNANDCTR(uint8_t *ctr) {
for (int i = 0; i < 16; i++) { *(ctr + i) = NANDCTR[i]; }
}

static void cryptNand(enum nandPart part, uint32_t sector, void *p, uint32_t n)
static int cryptNand(enum nandPart part, uint32_t sector, void *p, uint32_t n)
{
PartitionInfo info;
const uint32_t sectorBlocks = 512 / AES_BLOCK_SIZE;
uint8_t ctr[AES_BLOCK_SIZE];
uint32_t key;

GetNANDCTR(ctr);
aesAddCtr(ctr, part / 16 + sector * 0x20);
info.ctr = ctr;
info.buffer = p;
info.size = n * 0x200;
info.keyY = NULL;
switch (part) {
case TWLN:
case TWLP:
info.keyslot = 0x3;
key = 0x3;
break;

case AGB_SAVE:
info.keyslot = 0x7;
key = 0x7;
break;

case FIRM0:
case FIRM1:
info.keyslot = 0x6;
key = 0x6;
break;

case CTRNAND:
info.keyslot = 0x4;
key = 0x4;
break;

case KTR_CTRNAND:
info.keyslot = 0x5;
key = 0x5;
break;

default:
return -1;
}

DecryptPartition(&info);
aesSelKey(key);

GetNANDCTR(ctr);
aesAddCtr(ctr, part / 16 + sector * sectorBlocks);

aesCtr(p, p, n * sectorBlocks, ctr);

return 0;
}

void nand_readsectors(uint32_t sector_no, uint32_t numsectors, uint8_t *out, unsigned int partition) {
Expand Down

0 comments on commit a762bc1

Please sign in to comment.