Advertisement
dcjoedog

New Tolls.C file

Oct 21st, 2011
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.63 KB | None | 0 0
  1. // Copyright 2010            Sven Peter <svenpeter@gmail.com>
  2. // Copyright 2007,2008,2010  Segher Boessenkool  <segher@kernel.crashing.org>
  3. // Licensed under the terms of the GNU GPL, version 2
  4. // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
  5.  
  6. #include <sys/types.h>
  7. #include <sys/mman.h>
  8. #include <stdio.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <sys/stat.h>
  12. #include <string.h>
  13. #include <stdarg.h>
  14. #include <stdlib.h>
  15. #include <zlib.h>
  16. #include <dirent.h>
  17.  
  18. #include "tools.h"
  19. #include "aes.h"
  20. #include "sha1.h"
  21.  
  22. //
  23. // misc
  24. //
  25. void *mmap_file(const char *path)
  26. {
  27.     int fd;
  28.     struct stat st;
  29.     void *ptr;
  30.  
  31.     fd = open(path, O_RDONLY);
  32.     if(fd == -1)
  33.         fail("open %s", path);
  34.     if(fstat(fd, &st) != 0)
  35.         fail("fstat %s", path);
  36.  
  37.     ptr = mmap(0, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  38.     if(ptr==NULL)
  39.         fail("mmap");
  40.     close(fd);
  41.  
  42.     return ptr;
  43. }
  44.  
  45. void memcpy_to_file(const char *fname, u8 *ptr, u64 size)
  46. {
  47.     FILE *fp;
  48.  
  49.     fp = fopen(fname, "w");
  50.     fwrite(ptr, size, 1, fp);
  51.     fclose(fp);
  52. }
  53.  
  54. void fail(const char *a, ...)
  55. {
  56.     char msg[1024];
  57.     va_list va;
  58.  
  59.     va_start(va, a);
  60.     vsnprintf(msg, sizeof msg, a, va);
  61.     fprintf(stderr, "%s\n", msg);
  62.     perror("perror");
  63.  
  64.     exit(1);
  65. }
  66.  
  67. void decompress(u8 *in, u64 in_len, u8 *out, u64 out_len)
  68. {
  69.     z_stream s;
  70.     int ret;
  71.  
  72.     memset(&s, 0, sizeof(s));
  73.  
  74.     s.zalloc = Z_NULL;
  75.     s.zfree = Z_NULL;
  76.     s.opaque = Z_NULL;
  77.  
  78.     ret = inflateInit(&s);
  79.     if (ret != Z_OK)
  80.         fail("inflateInit returned %d", ret);
  81.  
  82.     s.avail_in = in_len;
  83.     s.next_in = in;
  84.  
  85.     s.avail_out = out_len;
  86.     s.next_out = out;
  87.  
  88.     ret = inflate(&s, Z_FINISH);
  89.     if (ret != Z_OK && ret != Z_STREAM_END)
  90.         fail("inflate returned %d", ret);
  91.  
  92.     inflateEnd(&s);
  93. }
  94.  
  95. const char *id2name(u32 id, struct id2name_tbl *t, const char *unk)
  96. {
  97.     while (t->name != NULL) {
  98.         if (id == t->id)
  99.             return t->name;
  100.         t++;
  101.     }
  102.     return unk;
  103. }
  104.  
  105. void get_rand(u8 *bfr, u32 size)
  106. {
  107.     FILE *fp;
  108.  
  109.     fp = fopen("/dev/urandom", "r");
  110.     if (fp == NULL)
  111.         fail("unable to open random");
  112.  
  113.     if (fread(bfr, size, 1, fp) != 1)
  114.         fail("unable to read random numbers");
  115.  
  116.     fclose(fp);
  117. }
  118.  
  119. //
  120. // ELF helpers
  121. //
  122. int elf_read_hdr(u8 *hdr, struct elf_hdr *h)
  123. {
  124.     int arch64;
  125.     memcpy(h->e_ident, hdr, 16);
  126.     hdr += 16;
  127.  
  128.     arch64 = h->e_ident[4] == 2;
  129.  
  130.     h->e_type = be16(hdr);
  131.     hdr += 2;
  132.     h->e_machine = be16(hdr);
  133.     hdr += 2;
  134.     h->e_version = be32(hdr);
  135.     hdr += 4;
  136.    
  137.     if (arch64) {
  138.         h->e_entry = be64(hdr);
  139.         h->e_phoff = be64(hdr + 8);
  140.         h->e_shoff = be64(hdr + 16);
  141.         hdr += 24;
  142.     } else {
  143.         h->e_entry = be32(hdr);
  144.         h->e_phoff = be32(hdr + 4);
  145.         h->e_shoff = be32(hdr + 8);
  146.         hdr += 12;
  147.     }
  148.  
  149.     h->e_flags = be32(hdr);
  150.     hdr += 4;
  151.  
  152.     h->e_ehsize = be16(hdr);
  153.     hdr += 2;
  154.     h->e_phentsize = be16(hdr);
  155.     hdr += 2;
  156.     h->e_phnum = be16(hdr);
  157.     hdr += 2;
  158.     h->e_shentsize = be16(hdr);
  159.     hdr += 2;
  160.     h->e_shnum = be16(hdr);
  161.     hdr += 2;
  162.     h->e_shtrndx = be16(hdr);
  163.  
  164.     return arch64;
  165. }
  166.  
  167. void elf_read_phdr(int arch64, u8 *phdr, struct elf_phdr *p)
  168. {
  169.     if (arch64) {
  170.         p->p_type =   be32(phdr + 0);
  171.         p->p_flags =  be32(phdr + 4);
  172.         p->p_off =    be64(phdr + 1*8);
  173.         p->p_vaddr =  be64(phdr + 2*8);
  174.         p->p_paddr =  be64(phdr + 3*8);
  175.         p->p_filesz = be64(phdr + 4*8);
  176.         p->p_memsz =  be64(phdr + 5*8);
  177.         p->p_align =  be64(phdr + 6*8);
  178.     } else {   
  179.         p->p_type =   be32(phdr + 0*4);
  180.         p->p_off =    be32(phdr + 1*4);
  181.         p->p_vaddr =  be32(phdr + 2*4);
  182.         p->p_paddr =  be32(phdr + 3*4);
  183.         p->p_filesz = be32(phdr + 4*4);
  184.         p->p_memsz =  be32(phdr + 5*4);
  185.         p->p_flags =  be32(phdr + 6*4);
  186.         p->p_align =  be32(phdr + 7*4);
  187.     }
  188. }
  189.  
  190. void elf_read_shdr(int arch64, u8 *shdr, struct elf_shdr *s)
  191. {
  192.     if (arch64) {
  193.         s->sh_name =      be32(shdr + 0*4);
  194.         s->sh_type =      be32(shdr + 1*4);
  195.         s->sh_flags =     be64(shdr + 2*4);
  196.         s->sh_addr =      be64(shdr + 2*4 + 1*8);
  197.         s->sh_offset =    be64(shdr + 2*4 + 2*8);
  198.         s->sh_size =      be64(shdr + 2*4 + 3*8);
  199.         s->sh_link =      be32(shdr + 2*4 + 4*8);
  200.         s->sh_info =      be32(shdr + 3*4 + 4*8);
  201.         s->sh_addralign = be64(shdr + 4*4 + 4*8);
  202.         s->sh_entsize =   be64(shdr + 4*4 + 5*8);
  203.     } else {
  204.         s->sh_name =      be32(shdr + 0*4);
  205.         s->sh_type =      be32(shdr + 1*4);
  206.         s->sh_flags =     be32(shdr + 2*4);
  207.         s->sh_addr =      be32(shdr + 3*4);
  208.         s->sh_offset =    be32(shdr + 4*4);
  209.         s->sh_size =      be32(shdr + 5*4);
  210.         s->sh_link =      be32(shdr + 6*4);
  211.         s->sh_info =      be32(shdr + 7*4);
  212.         s->sh_addralign = be32(shdr + 8*4);
  213.         s->sh_entsize =   be32(shdr + 9*4);
  214.     }
  215. }
  216.  
  217. void elf_write_shdr(int arch64, u8 *shdr, struct elf_shdr *s)
  218. {
  219.     if (arch64) {
  220.         wbe32(shdr + 0*4, s->sh_name);
  221.         wbe32(shdr + 1*4, s->sh_type);
  222.         wbe64(shdr + 2*4, s->sh_flags);
  223.         wbe64(shdr + 2*4 + 1*8, s->sh_addr);
  224.         wbe64(shdr + 2*4 + 2*8, s->sh_offset);
  225.         wbe64(shdr + 2*4 + 3*8, s->sh_size);
  226.         wbe32(shdr + 2*4 + 4*8, s->sh_link);
  227.         wbe32(shdr + 3*4 + 4*8, s->sh_info);
  228.         wbe64(shdr + 4*4 + 4*8, s->sh_addralign);
  229.         wbe64(shdr + 4*4 + 5*8, s->sh_entsize);
  230.     } else {
  231.         wbe32(shdr + 0*4, s->sh_name);
  232.         wbe32(shdr + 1*4, s->sh_type);
  233.         wbe32(shdr + 2*4, s->sh_flags);
  234.         wbe32(shdr + 3*4, s->sh_addr);
  235.         wbe32(shdr + 4*4, s->sh_offset);
  236.         wbe32(shdr + 5*4, s->sh_size);
  237.         wbe32(shdr + 6*4, s->sh_link);
  238.         wbe32(shdr + 7*4, s->sh_info);
  239.         wbe32(shdr + 8*4, s->sh_addralign);
  240.         wbe32(shdr + 9*4, s->sh_entsize);
  241.     }
  242. }
  243.  
  244. //
  245. // crypto
  246. //
  247. void aes256cbc(u8 *key, u8 *iv_in, u8 *in, u64 len, u8 *out)
  248. {
  249.     AES_KEY k;
  250.     u32 i;
  251.     u8 tmp[16];
  252.     u8 iv[16];
  253.  
  254.     memcpy(iv, iv_in, 16);
  255.     memset(&k, 0, sizeof k);
  256.     AES_set_decrypt_key(key, 256, &k);
  257.  
  258.     while (len > 0) {
  259.         memcpy(tmp, in, 16);
  260.         AES_decrypt(in, out, &k);
  261.  
  262.         for (i = 0; i < 16; i++)
  263.             out[i] ^= iv[i];
  264.  
  265.         memcpy(iv, tmp, 16);
  266.  
  267.         out += 16;
  268.         in += 16;
  269.         len -= 16;
  270.  
  271.     }
  272. }
  273.  
  274. void aes256cbc_enc(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out)
  275. {
  276.     AES_KEY k;
  277.     u32 i;
  278.     u8 tmp[16];
  279.  
  280.     memcpy(tmp, iv, 16);
  281.     memset(&k, 0, sizeof k);
  282.     AES_set_encrypt_key(key, 256, &k);
  283.  
  284.     while (len > 0) {
  285.         for (i = 0; i < 16; i++)
  286.             tmp[i] ^= *in++;
  287.  
  288.         AES_encrypt(tmp, out, &k);
  289.         memcpy(tmp, out, 16);
  290.  
  291.         out += 16;
  292.         len -= 16;
  293.     }
  294. }
  295.  
  296. void aes128ctr(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out)
  297. {
  298.     AES_KEY k;
  299.     u32 i;
  300.     u8 ctr[16];
  301.     u64 tmp;
  302.  
  303.     memset(ctr, 0, 16);
  304.     memset(&k, 0, sizeof k);
  305.  
  306.     AES_set_encrypt_key(key, 128, &k);
  307.  
  308.     for (i = 0; i < len; i++) {
  309.         if ((i & 0xf) == 0) {
  310.             AES_encrypt(iv, ctr, &k);
  311.    
  312.             // increase nonce
  313.             tmp = be64(iv + 8) + 1;
  314.             wbe64(iv + 8, tmp);
  315.             if (tmp == 0)
  316.                 wbe64(iv, be64(iv) + 1);
  317.         }
  318.         *out++ = *in++ ^ ctr[i & 0x0f];
  319.     }
  320. }
  321.  
  322.  
  323. // FIXME: use a non-broken sha1.c *sigh*
  324. static void sha1_fixup(struct SHA1Context *ctx, u8 *digest)
  325. {
  326.     u32 i;
  327.  
  328.     for(i = 0; i < 5; i++) {
  329.         *digest++ = ctx->Message_Digest[i] >> 24 & 0xff;
  330.         *digest++ = ctx->Message_Digest[i] >> 16 & 0xff;
  331.         *digest++ = ctx->Message_Digest[i] >> 8 & 0xff;
  332.         *digest++ = ctx->Message_Digest[i] & 0xff;
  333.     }
  334. }
  335.  
  336. void sha1(u8 *data, u32 len, u8 *digest)
  337. {
  338.     struct SHA1Context ctx;
  339.  
  340.     SHA1Reset(&ctx);
  341.     SHA1Input(&ctx, data, len);
  342.     SHA1Result(&ctx);
  343.  
  344.     sha1_fixup(&ctx, digest);
  345. }
  346.  
  347. void sha1_hmac(u8 *key, u8 *data, u32 len, u8 *digest)
  348. {
  349.     struct SHA1Context ctx;
  350.     u32 i;
  351.     u8 ipad[0x40];
  352.     u8 tmp[0x40 + 0x14]; // opad + hash(ipad + message)
  353.  
  354.     SHA1Reset(&ctx);
  355.  
  356.     for (i = 0; i < sizeof ipad; i++) {
  357.         tmp[i] = key[i] ^ 0x5c; // opad
  358.         ipad[i] = key[i] ^ 0x36;
  359.     }
  360.  
  361.     SHA1Input(&ctx, ipad, sizeof ipad);
  362.     SHA1Input(&ctx, data, len);
  363.     SHA1Result(&ctx);
  364.  
  365.     sha1_fixup(&ctx, tmp + 0x40);
  366.  
  367.     sha1(tmp, sizeof tmp, digest);
  368.  
  369. }
  370.    
  371. static struct id2name_tbl t_key2file[] = {
  372.     {KEY_LV0, "lv0"},
  373.     {KEY_LV1, "lv1"},
  374.     {KEY_LV2, "lv2"},
  375.     {KEY_APP, "app"},
  376.     {KEY_ISO, "iso"},
  377.     {KEY_LDR, "ldr"},
  378.     {KEY_PKG, "pkg"},
  379.     {KEY_SPP, "spp"},
  380.     {KEY_DRM, "drm"},
  381.     {KEY_RVK, "rvk"},
  382.     {0, NULL}
  383. };
  384.  
  385. static int key_build_path(char *ptr)
  386. {
  387.     char *home = NULL;
  388.     char *dir = NULL;
  389.  
  390.     memset(ptr, 0, 256);
  391.  
  392.     dir = getenv("SONY_KEYS");
  393.     if (dir != NULL) {
  394.         strncpy(ptr, dir, 256);
  395.         return 0;
  396.     }
  397.  
  398.     home = getenv("HOME");
  399.     if (home == NULL)
  400.         return -1;
  401.  
  402.     snprintf(ptr, 256, "%s/.ps3/", home);
  403.  
  404.     return 0;
  405. }
  406.  
  407. static int key_read(const char *path, u32 len, u8 *dst)
  408. {
  409.     FILE *fp = NULL;
  410.     u32 read;
  411.     int ret = -1;
  412.  
  413.     fp = fopen(path, "r");
  414.     if (fp == NULL)
  415.         goto fail;
  416.  
  417.     read = fread(dst, len, 1, fp);
  418.  
  419.     if (read != 1)
  420.         goto fail;
  421.  
  422.     ret = 0;
  423.  
  424. fail:
  425.     if (fp != NULL)
  426.         fclose(fp);
  427.  
  428.     return ret;
  429. }
  430.  
  431. struct keylist *keys_get(enum sce_key type)
  432. {
  433.     const char *name = NULL;
  434.     char base[256];
  435.     char path[256];
  436.     void *tmp = NULL;
  437.     char *id;
  438.     DIR *dp;
  439.     struct dirent *dent;
  440.     struct keylist *klist;
  441.     u8 bfr[4];
  442.  
  443.     klist = malloc(sizeof *klist);
  444.     if (klist == NULL)
  445.         goto fail;
  446.  
  447.     memset(klist, 0, sizeof *klist);
  448.  
  449.     name = id2name(type, t_key2file, NULL);
  450.     if (name == NULL)
  451.         goto fail;
  452.  
  453.     if (key_build_path(base) < 0)
  454.         goto fail;
  455.  
  456.     dp = opendir(base);
  457.     if (dp == NULL)
  458.         goto fail;
  459.  
  460.     while ((dent = readdir(dp)) != NULL) {
  461.         if (strncmp(dent->d_name, name, strlen(name)) == 0 &&
  462.             strstr(dent->d_name, "key") != NULL) {
  463.             tmp = realloc(klist->keys, (klist->n + 1) * sizeof(struct key));
  464.             if (tmp == NULL)
  465.                 goto fail;
  466.  
  467.             id = strrchr(dent->d_name, '-');
  468.             if (id != NULL)
  469.                 id++;
  470.  
  471.             klist->keys = tmp;
  472.             memset(&klist->keys[klist->n], 0, sizeof(struct key));
  473.  
  474.             snprintf(path, sizeof path, "%s/%s-key-%s", base, name, id);
  475.             key_read(path, 32, klist->keys[klist->n].key);
  476.    
  477.             snprintf(path, sizeof path, "%s/%s-iv-%s", base, name, id);
  478.             key_read(path, 16, klist->keys[klist->n].iv);
  479.    
  480.             klist->keys[klist->n].pub_avail = -1;
  481.             klist->keys[klist->n].priv_avail = -1;
  482.  
  483.             snprintf(path, sizeof path, "%s/%s-pub-%s", base, name, id);
  484.             if (key_read(path, 40, klist->keys[klist->n].pub) == 0) {
  485.                 snprintf(path, sizeof path, "%s/%s-ctype-%s", base, name, id);
  486.                 key_read(path, 4, bfr);
  487.  
  488.                 klist->keys[klist->n].pub_avail = 1;
  489.                 klist->keys[klist->n].ctype = be32(bfr);
  490.             }
  491.  
  492.             snprintf(path, sizeof path, "%s/%s-priv-%s", base, name, id);
  493.             if (key_read(path, 21, klist->keys[klist->n].priv) == 0)
  494.                 klist->keys[klist->n].priv_avail = 1;
  495.  
  496.  
  497.             klist->n++;
  498.         }
  499.     }
  500.  
  501.     return klist;
  502.  
  503. fail:
  504.     if (klist != NULL) {
  505.         if (klist->keys != NULL)
  506.             free(klist->keys);
  507.         free(klist);
  508.     }
  509.     klist = NULL;
  510.  
  511.     return NULL;
  512. }
  513.  
  514. int key_get_simple(const char *name, u8 *bfr, u32 len)
  515. {
  516.     char base[256];
  517.     char path[256];
  518.  
  519.     if (key_build_path(base) < 0)
  520.         return -1;
  521.  
  522.     snprintf(path, sizeof path, "%s/%s", base, name);
  523.     if (key_read(path, len, bfr) < 0)
  524.         return -1;
  525.  
  526.     return 0;
  527. }
  528.  
  529. int key_get(enum sce_key type, const char *suffix, struct key *k)
  530. {
  531.     const char *name;
  532.     char base[256];
  533.     char path[256];
  534.     u8 tmp[4];
  535.  
  536.     if (key_build_path(base) < 0)
  537.         return -1;
  538.  
  539.     name = id2name(type, t_key2file, NULL);
  540.     if (name == NULL)
  541.         return -1;
  542.  
  543.     snprintf(path, sizeof path, "%s/%s-key-%s", base, name, suffix);
  544.     if (key_read(path, 32, k->key) < 0)
  545.         return -1;
  546.    
  547.     snprintf(path, sizeof path, "%s/%s-iv-%s", base, name, suffix);
  548.     if (key_read(path, 16, k->iv) < 0)
  549.         return -1;
  550.  
  551.     k->pub_avail = k->priv_avail = 1;
  552.  
  553.     snprintf(path, sizeof path, "%s/%s-ctype-%s", base, name, suffix);
  554.     if (key_read(path, 4, tmp) < 0) {
  555.         k->pub_avail = k->priv_avail = -1;
  556.         return 0;
  557.     }
  558.  
  559.     k->ctype = be32(tmp);
  560.  
  561.     snprintf(path, sizeof path, "%s/%s-pub-%s", base, name, suffix);
  562.     if (key_read(path, 40, k->pub) < 0)
  563.         k->pub_avail = -1;
  564.  
  565.     snprintf(path, sizeof path, "%s/%s-priv-%s", base, name, suffix);
  566.     if (key_read(path, 21, k->priv) < 0)
  567.         k->priv_avail = -1;
  568.  
  569.     return 0;
  570. }  
  571.  
  572. static void memcpy_inv(u8 *dst, u8 *src, u32 len)
  573. {
  574.     u32 j;
  575.  
  576.     for (j = 0; j < len; j++)
  577.         dst[j] = ~src[j];
  578. }
  579.  
  580. int ecdsa_get_params(u32 type, u8 *p, u8 *a, u8 *b, u8 *N, u8 *Gx, u8 *Gy)
  581. {
  582.     static u8 tbl[64 * 121];
  583.     char path[256];
  584.     u32 offset;
  585.  
  586.     if (type >= 64)
  587.         return -1;
  588.  
  589.     if (key_build_path(path) < 0)
  590.         return -1;
  591.  
  592.     strncat(path, "/curves", sizeof path);
  593.  
  594.     if (key_read(path, sizeof tbl, tbl) < 0)
  595.         return -1;
  596.  
  597.     offset = type * 121;
  598.  
  599.     memcpy_inv(p, tbl + offset + 0, 20);
  600.     memcpy_inv(a, tbl + offset + 20, 20);
  601.     memcpy_inv(b, tbl + offset + 40, 20);
  602.     memcpy_inv(N, tbl + offset + 60, 21);
  603.     memcpy_inv(Gx, tbl + offset + 81, 20);
  604.     memcpy_inv(Gy, tbl + offset + 101, 20);
  605.  
  606.     return 0;
  607. }
  608.  
  609. int sce_decrypt_header(u8 *ptr, struct keylist *klist)
  610. {
  611.     u32 meta_offset;
  612.     u32 meta_len;
  613.     u64 header_len;
  614.     u32 i, j;
  615.     u8 tmp[0x40];
  616.     int success = 0;
  617.  
  618.  
  619.     meta_offset = be32(ptr + 0x0c);
  620.     header_len  = be64(ptr + 0x10);
  621.  
  622.     for (i = 0; i < klist->n; i++) {
  623.         aes256cbc(klist->keys[i].key,
  624.               klist->keys[i].iv,
  625.               ptr + meta_offset + 0x20,
  626.               0x40,
  627.               tmp);
  628.  
  629.         success = 1;
  630.         for (j = 0x10; j < (0x10 + 0x10); j++)
  631.             if (tmp[j] != 0)
  632.                 success = 0;
  633.    
  634.         for (j = 0x30; j < (0x30 + 0x10); j++)
  635.             if (tmp[j] != 0)
  636.                    success = 0;
  637.  
  638.         if (success == 1) {
  639.             memcpy(ptr + meta_offset + 0x20, tmp, 0x40);
  640.             break;
  641.         }
  642.     }
  643.  
  644.     if (success != 1)
  645.         return -1;
  646.  
  647.     memcpy(tmp, ptr + meta_offset + 0x40, 0x10);
  648.     aes128ctr(ptr + meta_offset + 0x20,
  649.           tmp,
  650.           ptr + meta_offset + 0x60,
  651.           0x20,
  652.           ptr + meta_offset + 0x60);
  653.  
  654.     meta_len = header_len - meta_offset;
  655.  
  656.     aes128ctr(ptr + meta_offset + 0x20,
  657.           tmp,
  658.           ptr + meta_offset + 0x80,
  659.           meta_len - 0x80,
  660.           ptr + meta_offset + 0x80);
  661.  
  662.     return i;
  663. }
  664.  
  665. int sce_encrypt_header(u8 *ptr, struct key *k)
  666. {
  667.     u32 meta_offset;
  668.     u32 meta_len;
  669.     u64 header_len;
  670.     u8 iv[16];
  671.  
  672.     meta_offset = be32(ptr + 0x0c);
  673.     header_len  = be64(ptr + 0x10);
  674.     meta_len = header_len - meta_offset;
  675.  
  676.     memcpy(iv, ptr + meta_offset + 0x40, 0x10);
  677.     aes128ctr(ptr + meta_offset + 0x20,
  678.           iv,
  679.           ptr + meta_offset + 0x60,
  680.           meta_len - 0x60,
  681.           ptr + meta_offset + 0x60);
  682.  
  683.     aes256cbc_enc(k->key, k->iv,
  684.                   ptr + meta_offset + 0x20,
  685.               0x40,
  686.               ptr + meta_offset + 0x20);
  687.  
  688.  
  689.     return 0;
  690. }
  691.  
  692. int sce_decrypt_data(u8 *ptr)
  693. {
  694.     u64 meta_offset;
  695.     u32 meta_len;
  696.     u32 meta_n_hdr;
  697.     u64 header_len;
  698.     u32 i;
  699.  
  700.     u64 offset;
  701.     u64 size;
  702.     u32 keyid;
  703.     u32 ivid;
  704.     u8 *tmp;
  705.  
  706.     u8 iv[16];
  707.  
  708.     meta_offset = be32(ptr + 0x0c);
  709.     header_len  = be64(ptr + 0x10);
  710.     meta_len = header_len - meta_offset;
  711.     meta_n_hdr = be32(ptr + meta_offset + 0x60 + 0xc);
  712.  
  713.     for (i = 0; i < meta_n_hdr; i++) {
  714.         tmp = ptr + meta_offset + 0x80 + 0x30*i;
  715.         offset = be64(tmp);
  716.         size = be64(tmp + 8);
  717.         keyid = be32(tmp + 0x24);
  718.         ivid = be32(tmp + 0x28);
  719.  
  720.         if (keyid == 0xffffffff || ivid == 0xffffffff)
  721.             continue;
  722.  
  723.         memcpy(iv, ptr + meta_offset + 0x80 + 0x30 * meta_n_hdr + ivid * 0x10, 0x10);
  724.         aes128ctr(ptr + meta_offset + 0x80 + 0x30 * meta_n_hdr + keyid * 0x10,
  725.                   iv,
  726.                   ptr + offset,
  727.               size,
  728.               ptr + offset);
  729.     }
  730.  
  731.     return 0;
  732. }
  733.  
  734. int sce_encrypt_data(u8 *ptr)
  735. {
  736.     return sce_decrypt_data(ptr);
  737. }
  738.  
  739.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement