Advertisement
xerpi

Untitled

Aug 30th, 2014
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1. #include "3dstypes.h"
  2. #include "3ds.h"
  3. #include "draw.h"
  4. #include "libc.h"
  5. #include "crypto.h"
  6. #include "FS.h"
  7.  
  8. extern void *start_addr;
  9. extern void *end_addr;
  10.  
  11. /*
  12. 4 bytes number of entries
  13. entry:
  14.     16 bytes CTR
  15.     16 bytes keyY
  16.     4 bytes length in megabytes(round up)
  17.     64 bytes output filename in utf16(could use something like "/ncchcode1.romfs.xorpad")
  18.  
  19. #define entry_ctr 0+4 //+4 because first int in the array is not an entry
  20. #define entry_keyY 16+4
  21. #define entry_megabytes 32+4
  22. #define entry_filename 36+4
  23. */
  24.  
  25. struct ncch_info_entry {
  26.     uint8_t  CTR[16];
  27.     uint8_t  keyY[16];
  28.     uint32_t size_mb;
  29.     wchar_t  filename[32];
  30. };
  31.  
  32. struct ncch_info {
  33.     uint32_t n_entries;
  34.     struct ncch_info_entry entries[20];
  35. };
  36.  
  37. void ncch_info_createpad(struct ncch_info_entry *info)
  38. {
  39.     #define BUFFER_ADDR ((volatile uint8_t*)0x20000000)
  40.     #define BLOCK_SIZE  (0x10000)
  41.    
  42.     uint32_t handle = FileOpen(info->filename);
  43.     setup_aeskey(0x2C, AES_BIG_INPUT|AES_NORMAL_INPUT, info->keyY);
  44.     use_aeskey(0x2C);
  45.    
  46.     uint8_t ctr[16];
  47.     memcpy(ctr, info->CTR, 16);
  48.    
  49.     uint8_t zero_buf[16];
  50.     uint32_t size_bytes = info->size_mb*1024*1024;
  51.     uint32_t i, j;
  52.     for (i = 0; i < size_bytes; i += BLOCK_SIZE) {
  53.         for (j = 0; (j < BLOCK_SIZE) && (i+j < size_bytes); j+= 16) {
  54.             memset(zero_buf, 0, 16);
  55.             set_ctr(AES_BIG_INPUT|AES_NORMAL_INPUT, ctr);
  56.             aes_decrypt(zero_buf, (void*)BUFFER_ADDR+j, ctr, 1);
  57.             add_ctr(ctr, 1);
  58.         }
  59.         FlushProcessDataCache(0xFFFF8001, (void*)BUFFER_ADDR, BLOCK_SIZE);
  60.         FileWriteOffset(handle, (void*)BUFFER_ADDR, BLOCK_SIZE, i);
  61.     }
  62. }
  63.  
  64. static volatile u32 runOnce = 1;
  65. int main()
  66. {
  67.     while(!runOnce) {};
  68.     runOnce = 0;
  69.     FlushProcessDataCache(0xFFFF8001, (void*)start_addr, start_addr-end_addr);
  70.     ClearScreen(0x0);
  71.     DEBUG("3DS CTR DECRYPTOR by VOiD, refactored by xerpi");
  72.    
  73.     struct ncch_info info;
  74.    
  75.     DEBUG("Opening /ncchinfo.bin ...");
  76.     u32 handle = FileOpen(L"/ncchinfo.bin");
  77.     FileRead(handle, &info, 4); //Read number of entries
  78.     DEBUG("\tDone!");
  79.  
  80.     if (!info.n_entries || info.n_entries > 20) {
  81.         ERROR("Nothing to do.(%i) :/", info.n_entries);
  82.         return 0;
  83.     }
  84.     FileRead(handle, &info, sizeof(struct ncch_info));
  85.     DEBUG("Found %i entries", info.n_entries);
  86.    
  87.     int i;
  88.     for(i = 0; i < info.n_entries; i++) {
  89.         /*createpad(info.entries[i].CTR,
  90.                   info.entries[i].keyY,
  91.                   info.entries[i].filename,
  92.                   info.entries[i].size_mb,
  93.                   i+1);*/
  94.         DEBUG("Creating pad %i, size: %iMB ...", i+1, info.entries[i].filename);
  95.         ncch_info_createpad(&info.entries[i]);
  96.         DEBUG("\tDone!");
  97.     }
  98.  
  99.     DEBUG("Finished!");
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement