diff --git a/source/nand/aes.c b/source/nand/aes.c index cf2c1f3..6bad542 100644 --- a/source/nand/aes.c +++ b/source/nand/aes.c @@ -85,7 +85,7 @@ void add_ctr(void* ctr, uint32_t carry) uint32_t sum; int32_t i; - for(i = 0; i < 4; i++) { + for(i=0; i < 4; i++) { counter[i] = ((uint32_t)outctr[i*4+0]<<24) | ((uint32_t)outctr[i*4+1]<<16) | ((uint32_t)outctr[i*4+2]<<8) | ((uint32_t)outctr[i*4+3]<<0); } @@ -110,6 +110,46 @@ void add_ctr(void* ctr, uint32_t carry) } } +void ctr_decrypt_boffset(void *inbuf, void *outbuf, size_t size, size_t off, uint32_t mode, uint8_t *ctr) +{ + size_t bytes_left = size; + size_t off_fix = off % AES_BLOCK_SIZE; + uint8_t temp[AES_BLOCK_SIZE]; + uint8_t ctr_local[16]; + uint8_t *in = inbuf; + uint8_t *out = outbuf; + uint32_t i; + + for (i=0; i<16; i++) // setup local ctr + ctr_local[i] = ctr[i]; + add_ctr(ctr_local, off / AES_BLOCK_SIZE); + + if (off_fix) // handle misaligned offset (at beginning) + { + for (i=off_fix; ictr, 16); - add_ctr(ctr, off / 16); - - // setup the key - setup_aeskeyY(0x34, info->keyy); - use_aeskey(0x34); - - // handle misaligned offset (at beginning) - if (off % 16) { - memcpy(buff16 + (off % 16), buffer, 16 - (off % 16)); - ctr_decrypt(buff16, buff16, 1, mode, ctr); - memcpy(buffer, buff16 + (off % 16), 16 - (off % 16)); - buffer += 16 - (off % 16); - bt -= 16 - (off % 16); - } - - // de/encrypt the data - ctr_decrypt(buffer, buffer, bt / 16, mode, ctr); - buffer += 16 * (UINT) (bt / 16); - bt -= 16 * (UINT) (bt / 16); - - // handle misaligned offset (at end) - if (bt) { - memcpy(buff16, buffer, bt); - ctr_decrypt(buff16, buff16, 1, mode, ctr); - memcpy(buffer, buff16, bt); - buffer += bt; - bt = 0; - } -} - FRESULT fx_open (FIL* fp, const TCHAR* path, BYTE mode) { int num = alias_num(path); FilCryptInfo* info = fx_find_cryptinfo(fp); @@ -122,8 +84,11 @@ FRESULT fx_read (FIL* fp, void* buff, UINT btr, UINT* br) { FilCryptInfo* info = fx_find_cryptinfo(fp); FSIZE_t off = f_tell(fp); FRESULT res = f_read(fp, buff, btr, br); - if (info && info->fptr) - fx_crypt(info, buff, off, btr); + if (info && info->fptr) { + setup_aeskeyY(0x34, info->keyy); + use_aeskey(0x34); + ctr_decrypt_boffset(buff, buff, btr, off, AES_CNT_CTRNAND_MODE, info->ctr); + } return res; } @@ -132,12 +97,14 @@ FRESULT fx_write (FIL* fp, const void* buff, UINT btw, UINT* bw) { FSIZE_t off = f_tell(fp); FRESULT res = FR_OK; if (info && info->fptr) { + setup_aeskeyY(0x34, info->keyy); + use_aeskey(0x34); *bw = 0; for (UINT p = 0; (p < btw) && (res == FR_OK); p += SDCRYPT_BUFFER_SIZE) { UINT pcount = min(SDCRYPT_BUFFER_SIZE, (btw - p)); UINT bwl = 0; memcpy(SDCRYPT_BUFFER, (u8*) buff + p, pcount); - fx_crypt(info, SDCRYPT_BUFFER, off + p, pcount); + ctr_decrypt_boffset(SDCRYPT_BUFFER, SDCRYPT_BUFFER, pcount, off + p, AES_CNT_CTRNAND_MODE, info->ctr); res = f_write(fp, (const void*) SDCRYPT_BUFFER, pcount, &bwl); *bw += bwl; }