├── README ├── .gitignore ├── Makefile ├── aes.h ├── cosunpkg.c ├── norunpack.c ├── tools.h ├── types.h ├── sha1.h ├── unpkg.c ├── cospkg.c ├── ungpkg.c ├── pupunpack.c ├── puppack.c ├── bn.c ├── sceverify.c ├── pkg.c ├── ec.c ├── unself.c ├── sha1.c ├── makeself.c ├── readself.c ├── tools.c └── aes.c /README: -------------------------------------------------------------------------------- 1 | Copied from Hermes. 2 | https://github.com/hermesEOL/fail0verflow -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.o 3 | pupunpack 4 | readself 5 | unself 6 | sceverify 7 | unpkg 8 | makeself 9 | makepkg 10 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | TOOLS = readself pupunpack unself sceverify 2 | TOOLS += makeself norunpack puppack unpkg pkg 3 | TOOLS += cosunpkg cospkg ungpkg 4 | COMMON = tools.o aes.o sha1.o ec.o bn.o 5 | DEPS = Makefile tools.h types.h 6 | 7 | CC = gcc 8 | CFLAGS = -g -O2 -Wall -W 9 | LDFLAGS = -lz 10 | 11 | OBJS = $(COMMON) $(addsuffix .o, $(TOOLS)) 12 | 13 | all: $(TOOLS) 14 | 15 | $(TOOLS): %: %.o $(COMMON) $(DEPS) 16 | $(CC) $(CFLAGS) -o $@ $< $(COMMON) $(LDFLAGS) 17 | 18 | $(OBJS): %.o: %.c $(DEPS) 19 | $(CC) $(CFLAGS) -c -o $@ $< 20 | 21 | clean: 22 | -rm -f $(OBJS) $(TOOLS) 23 | -------------------------------------------------------------------------------- /aes.h: -------------------------------------------------------------------------------- 1 | #ifndef QEMU_AES_H 2 | #define QEMU_AES_H 3 | 4 | #include "tools.h" 5 | 6 | #define AES_MAXNR 14 7 | #define AES_BLOCK_SIZE 16 8 | 9 | struct aes_key_st { 10 | uint32_t rd_key[4 *(AES_MAXNR + 1)]; 11 | int rounds; 12 | }; 13 | typedef struct aes_key_st AES_KEY; 14 | 15 | int AES_set_encrypt_key(const unsigned char *userKey, const int bits, 16 | AES_KEY *key); 17 | int AES_set_decrypt_key(const unsigned char *userKey, const int bits, 18 | AES_KEY *key); 19 | 20 | void AES_encrypt(const unsigned char *in, unsigned char *out, 21 | const AES_KEY *key); 22 | void AES_decrypt(const unsigned char *in, unsigned char *out, 23 | const AES_KEY *key); 24 | 25 | #if 0 26 | void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, 27 | const unsigned long length, const AES_KEY *key, 28 | unsigned char *ivec, const int enc); 29 | #endif 30 | #endif 31 | -------------------------------------------------------------------------------- /cosunpkg.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include "tools.h" 6 | #include "types.h" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | u8 *pkg = NULL; 15 | 16 | static void unpack_file(u32 i) 17 | { 18 | u8 *ptr; 19 | u8 name[33]; 20 | u64 offset; 21 | u64 size; 22 | 23 | ptr = pkg + 0x10 + 0x30 * i; 24 | 25 | offset = be64(ptr + 0x00); 26 | size = be64(ptr + 0x08); 27 | 28 | memset(name, 0, sizeof name); 29 | strncpy((char *)name, (char *)(ptr + 0x10), 0x20); 30 | 31 | printf("unpacking %s...\n", name); 32 | memcpy_to_file((char *)name, pkg + offset, size); 33 | } 34 | 35 | static void unpack_pkg(void) 36 | { 37 | u32 n_files; 38 | u64 size; 39 | u32 i; 40 | 41 | n_files = be32(pkg + 4); 42 | size = be64(pkg + 8); 43 | 44 | for (i = 0; i < n_files; i++) 45 | unpack_file(i); 46 | } 47 | 48 | int main(int argc, char *argv[]) 49 | { 50 | if (argc != 3) 51 | fail("usage: cosunpkg filename.pkg target"); 52 | 53 | pkg = mmap_file(argv[1]); 54 | 55 | mkdir(argv[2], 0777); 56 | 57 | if (chdir(argv[2]) != 0) 58 | fail("chdir"); 59 | 60 | unpack_pkg(); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /norunpack.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "tools.h" 11 | 12 | static u8 *nor = NULL; 13 | 14 | static void new_dir(const char *n) 15 | { 16 | mkdir(n, 0777); 17 | 18 | if (chdir(n) < 0) 19 | fail("chdir"); 20 | } 21 | 22 | static void do_toc(u8 *ptr) 23 | { 24 | u32 n_entries; 25 | u32 i; 26 | u8 *p; 27 | u8 *tmp; 28 | u64 size; 29 | char name[0x20]; 30 | 31 | n_entries = be32(ptr + 0x04); 32 | p = ptr + 0x10; 33 | 34 | for(i = 0; i < n_entries; i++) { 35 | memcpy(name, p + 16, 0x20); 36 | 37 | if (strncmp(name, "asecure_loader", 0x20) == 0) { 38 | new_dir("asecure_loader"); 39 | do_toc(ptr + be64(p)); 40 | if (chdir("..") < 0) 41 | fail("chdir(..)"); 42 | } else if (strncmp(name, "ros", 3) == 0) { 43 | new_dir(name); 44 | do_toc(ptr + be64(p) + 0x10); 45 | if (chdir("..") < 0) 46 | fail("chdir(..)"); 47 | } else { 48 | tmp = ptr + be64(p); 49 | size = be64(p + 0x08); 50 | if (be32(tmp + 0x10) == 0x53434500) { 51 | tmp += 0x10; 52 | size -= 0x10; 53 | } 54 | 55 | memcpy_to_file(name, tmp, size); 56 | } 57 | p += 0x30; 58 | } 59 | } 60 | 61 | int main(int argc, char *argv[]) 62 | { 63 | if (argc != 3) 64 | fail("usage: norunpack dump.b directory"); 65 | 66 | nor = mmap_file(argv[1]); 67 | 68 | new_dir(argv[2]); 69 | 70 | do_toc(nor + 0x400); 71 | 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /tools.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Copyright 2007,2008,2010 Segher Boessenkool 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 | #ifndef TOOLS_H__ 7 | #define TOOLS_H__ 1 8 | #include 9 | 10 | #include "types.h" 11 | 12 | enum sce_key { 13 | KEY_LV0 = 0, 14 | KEY_LV1, 15 | KEY_LV2, 16 | KEY_APP, 17 | KEY_ISO, 18 | KEY_LDR, 19 | KEY_PKG, 20 | KEY_SPP 21 | }; 22 | 23 | void *mmap_file(const char *path); 24 | void memcpy_to_file(const char *fname, u8 *ptr, u64 size); 25 | const char *id2name(u32 id, struct id2name_tbl *t, const char *unk); 26 | void fail(const char *fmt, ...) __attribute__((noreturn)); 27 | void decompress(u8 *in, u64 in_len, u8 *out, u64 out_len); 28 | void get_rand(u8 *bfr, u32 size); 29 | 30 | int elf_read_hdr(u8 *hdr, struct elf_hdr *h); 31 | void elf_read_phdr(int arch64, u8 *phdr, struct elf_phdr *p); 32 | void elf_read_shdr(int arch64, u8 *shdr, struct elf_shdr *s); 33 | void elf_write_shdr(int arch64, u8 *shdr, struct elf_shdr *s); 34 | 35 | void aes256cbc(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out); 36 | void aes256cbc_enc(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out); 37 | void aes128ctr(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out); 38 | 39 | void sha1(u8 *data, u32 len, u8 *digest); 40 | void sha1_hmac(u8 *key, u8 *data, u32 len, u8 *digest); 41 | 42 | int key_get(enum sce_key type, const char *suffix, struct key *k); 43 | int key_get_simple(const char *name, u8 *bfr, u32 len); 44 | struct keylist *keys_get(enum sce_key type); 45 | 46 | int sce_decrypt_header(u8 *ptr, struct keylist *klist); 47 | int sce_encrypt_header(u8 *ptr, struct key *k); 48 | int sce_decrypt_data(u8 *ptr); 49 | int sce_encrypt_data(u8 *ptr); 50 | 51 | int ecdsa_get_params(u32 type, u8 *p, u8 *a, u8 *b, u8 *N, u8 *Gx, u8 *Gy); 52 | int ecdsa_set_curve(u32 type); 53 | void ecdsa_set_pub(u8 *Q); 54 | void ecdsa_set_priv(u8 *k); 55 | int ecdsa_verify(u8 *hash, u8 *R, u8 *S); 56 | void ecdsa_sign(u8 *hash, u8 *R, u8 *S); 57 | 58 | void bn_copy(u8 *d, u8 *a, u32 n); 59 | int bn_compare(u8 *a, u8 *b, u32 n); 60 | void bn_reduce(u8 *d, u8 *N, u32 n); 61 | void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n); 62 | void bn_sub(u8 *d, u8 *a, u8 *b, u8 *N, u32 n); 63 | void bn_to_mon(u8 *d, u8 *N, u32 n); 64 | void bn_from_mon(u8 *d, u8 *N, u32 n); 65 | void bn_mon_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n); 66 | void bn_mon_inv(u8 *d, u8 *a, u8 *N, u32 n); 67 | 68 | #define round_up(x,n) (-(-(x) & -(n))) 69 | 70 | #define array_size(x) (sizeof(x) / sizeof(*(x))) 71 | #endif 72 | -------------------------------------------------------------------------------- /types.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | #ifndef TYPES_H__ 5 | #define TYPES_H__ 6 | 7 | #include 8 | 9 | typedef unsigned long long u64; 10 | typedef unsigned int u32; 11 | typedef unsigned short u16; 12 | typedef unsigned char u8; 13 | 14 | struct elf_phdr { 15 | u32 p_type; 16 | u64 p_off; 17 | u64 p_vaddr; 18 | u64 p_paddr; 19 | u64 p_filesz; 20 | u64 p_memsz; 21 | u32 p_flags; 22 | u64 p_align; 23 | 24 | void *ptr; 25 | }; 26 | 27 | struct elf_shdr { 28 | u32 sh_name; 29 | u32 sh_type; 30 | u32 sh_flags; 31 | u64 sh_addr; 32 | u64 sh_offset; 33 | u32 sh_size; 34 | u32 sh_link; 35 | u32 sh_info; 36 | u32 sh_addralign; 37 | u32 sh_entsize; 38 | }; 39 | 40 | #define ET_NONE 0 41 | #define ET_REL 1 42 | #define ET_EXEC 2 43 | #define ET_DYN 3 44 | #define ET_CORE 4 45 | #define ET_LOOS 0xfe00 46 | #define ET_HIOS 0xfeff 47 | #define ET_LOPROC 0xff00 48 | #define ET_HIPROC 0xffff 49 | struct elf_hdr { 50 | char e_ident[16]; 51 | u16 e_type; 52 | u16 e_machine; 53 | u32 e_version; 54 | u64 e_entry; 55 | u64 e_phoff; 56 | u64 e_shoff; 57 | u32 e_flags; 58 | u16 e_ehsize; 59 | u16 e_phentsize; 60 | u16 e_phnum; 61 | u16 e_shentsize; 62 | u16 e_shnum; 63 | u16 e_shtrndx; 64 | }; 65 | 66 | 67 | struct id2name_tbl { 68 | u32 id; 69 | const char *name; 70 | }; 71 | 72 | struct key { 73 | u8 key[32]; 74 | u8 iv[16]; 75 | 76 | int pub_avail; 77 | int priv_avail; 78 | u8 pub[40]; 79 | u8 priv[21]; 80 | u32 ctype; 81 | }; 82 | 83 | struct keylist { 84 | u32 n; 85 | struct key *keys; 86 | }; 87 | 88 | static inline u8 be8(u8 *p) 89 | { 90 | return *p; 91 | } 92 | 93 | static inline u16 be16(u8 *p) 94 | { 95 | u16 a; 96 | 97 | a = p[0] << 8; 98 | a |= p[1]; 99 | 100 | return a; 101 | } 102 | 103 | static inline u32 be32(u8 *p) 104 | { 105 | u32 a; 106 | 107 | a = p[0] << 24; 108 | a |= p[1] << 16; 109 | a |= p[2] << 8; 110 | a |= p[3] << 0; 111 | 112 | return a; 113 | } 114 | 115 | static inline u64 be64(u8 *p) 116 | { 117 | u32 a, b; 118 | 119 | a = be32(p); 120 | b = be32(p + 4); 121 | 122 | return ((u64)a<<32) | b; 123 | } 124 | 125 | static inline void wbe16(u8 *p, u16 v) 126 | { 127 | p[0] = v >> 8; 128 | p[1] = v; 129 | } 130 | 131 | static inline void wbe32(u8 *p, u32 v) 132 | { 133 | p[0] = v >> 24; 134 | p[1] = v >> 16; 135 | p[2] = v >> 8; 136 | p[3] = v; 137 | } 138 | 139 | static inline void wbe64(u8 *p, u64 v) 140 | { 141 | wbe32(p + 4, v); 142 | v >>= 32; 143 | wbe32(p, v); 144 | } 145 | 146 | #endif 147 | -------------------------------------------------------------------------------- /sha1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * sha1.h 3 | * 4 | * Copyright (C) 1998 5 | * Paul E. Jones 6 | * All Rights Reserved 7 | * 8 | * This software is licensed as "freeware." Permission to distribute 9 | * this software in source and binary forms is hereby granted without 10 | * a fee. THIS SOFTWARE IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED 11 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 12 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 13 | * THE AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DAMAGES RESULTING 14 | * FROM THE USE OF THIS SOFTWARE, EITHER DIRECTLY OR INDIRECTLY, INCLUDING, 15 | * BUT NOT LIMITED TO, LOSS OF DATA OR DATA BEING RENDERED INACCURATE. 16 | * 17 | * This software is licensed as "freeware." Permission to distribute 18 | * this software in source and binary forms is hereby granted without 19 | * a fee. THIS SOFTWARE IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED 20 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 | * THE AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DAMAGES RESULTING 23 | * FROM THE USE OF THIS SOFTWARE, EITHER DIRECTLY OR INDIRECTLY, INCLUDING, 24 | * BUT NOT LIMITED TO, LOSS OF DATA OR DATA BEING RENDERED INACCURATE. 25 | * 26 | ***************************************************************************** 27 | * $Id: sha1.h,v 1.2 2004/03/27 18:00:33 paulej Exp $ 28 | ***************************************************************************** 29 | * 30 | * Description: 31 | * This class implements the Secure Hashing Standard as defined 32 | * in FIPS PUB 180-1 published April 17, 1995. 33 | * 34 | * Many of the variable names in the SHA1Context, especially the 35 | * single character names, were used because those were the names 36 | * used in the publication. 37 | * 38 | * Please read the file sha1.c for more information. 39 | * 40 | */ 41 | 42 | #ifndef _SHA1_H_ 43 | #define _SHA1_H_ 44 | 45 | /* 46 | * This structure will hold context information for the hashing 47 | * operation 48 | */ 49 | typedef struct SHA1Context 50 | { 51 | unsigned Message_Digest[5]; /* Message Digest (output) */ 52 | 53 | unsigned Length_Low; /* Message length in bits */ 54 | unsigned Length_High; /* Message length in bits */ 55 | 56 | unsigned char Message_Block[64]; /* 512-bit message blocks */ 57 | int Message_Block_Index; /* Index into message block array */ 58 | 59 | int Computed; /* Is the digest computed? */ 60 | int Corrupted; /* Is the message digest corruped? */ 61 | } SHA1Context; 62 | 63 | /* 64 | * Function Prototypes 65 | */ 66 | void SHA1Reset(SHA1Context *); 67 | int SHA1Result(SHA1Context *); 68 | void SHA1Input( SHA1Context *, 69 | const unsigned char *, 70 | unsigned); 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /unpkg.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include "tools.h" 6 | #include "types.h" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | u8 *pkg = NULL; 16 | static u64 dec_size; 17 | static u32 meta_offset; 18 | static u32 n_sections; 19 | 20 | static void unpack_content(const char *name) 21 | { 22 | u8 *tmp; 23 | u8 *decompressed; 24 | u64 offset; 25 | u64 size; 26 | u64 size_real; 27 | 28 | tmp = pkg + meta_offset + 0x80 + 0x30 * 2; 29 | 30 | offset = be64(tmp); 31 | size = be64(tmp + 8); 32 | size_real = dec_size - 0x80; 33 | 34 | if (be32(tmp + 0x2c) == 0x2) { 35 | decompressed = malloc(size_real); 36 | memset(decompressed, 0xaa, size_real); 37 | 38 | decompress(pkg + offset, size, decompressed, size_real); 39 | 40 | memcpy_to_file(name, decompressed, size_real); 41 | } else { 42 | memcpy_to_file(name, pkg + offset, size); 43 | } 44 | } 45 | 46 | static void unpack_info(u32 i) 47 | { 48 | u8 *tmp; 49 | u64 offset; 50 | u64 size; 51 | char path[256]; 52 | 53 | tmp = pkg + meta_offset + 0x80 + 0x30 * i; 54 | 55 | snprintf(path, sizeof path, "info%d", i); 56 | 57 | offset = be64(tmp); 58 | size = be64(tmp + 8); 59 | 60 | if (size != 0x40) 61 | fail("weird info size: %08x", size); 62 | 63 | memcpy_to_file(path, pkg + offset, size); 64 | } 65 | 66 | static void unpack_pkg(void) 67 | { 68 | unpack_info(0); 69 | unpack_info(1); 70 | unpack_content("content"); 71 | } 72 | 73 | static void decrypt_pkg(void) 74 | { 75 | u16 flags; 76 | u16 type; 77 | u32 hdr_len; 78 | struct keylist *k; 79 | 80 | flags = be16(pkg + 0x08); 81 | type = be16(pkg + 0x0a); 82 | hdr_len = be64(pkg + 0x10); 83 | dec_size = be64(pkg + 0x18); 84 | 85 | if (type != 3) 86 | fail("no .pkg file"); 87 | 88 | k = keys_get(KEY_PKG); 89 | 90 | if (k == NULL) 91 | fail("no key found"); 92 | 93 | if (sce_decrypt_header(pkg, k) < 0) 94 | fail("header decryption failed"); 95 | 96 | if (sce_decrypt_data(pkg) < 0) 97 | fail("data decryption failed"); 98 | 99 | meta_offset = be32(pkg + 0x0c); 100 | n_sections = be32(pkg + meta_offset + 0x60 + 0xc); 101 | 102 | if (n_sections != 3) 103 | fail("invalid section count: %d", n_sections); 104 | } 105 | 106 | int main(int argc, char *argv[]) 107 | { 108 | if (argc == 3) { 109 | pkg = mmap_file(argv[1]); 110 | 111 | mkdir(argv[2], 0777); 112 | 113 | if (chdir(argv[2]) != 0) 114 | fail("chdir"); 115 | 116 | decrypt_pkg(); 117 | unpack_pkg(); 118 | } else if (argc == 4) { 119 | if (strcmp(argv[1], "-s") != 0) 120 | fail("invalid option: %s", argv[1]); 121 | 122 | pkg = mmap_file(argv[2]); 123 | 124 | decrypt_pkg(); 125 | unpack_content(argv[3]); 126 | } else { 127 | fail("usage: unpkg [-s] filename.pkg target"); 128 | } 129 | 130 | 131 | return 0; 132 | } 133 | -------------------------------------------------------------------------------- /cospkg.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include "tools.h" 6 | #include "types.h" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #define MAX_FILES 255 14 | 15 | struct pkg_file { 16 | char name[0x20]; 17 | u8 *ptr; 18 | u64 size; 19 | u64 offset; 20 | }; 21 | 22 | static u8 *hdr = NULL; 23 | static u32 hdr_size = 0; 24 | 25 | static u32 n_files = 0; 26 | static struct pkg_file files[MAX_FILES]; 27 | 28 | static void get_files(const char *d) 29 | { 30 | DIR *dir; 31 | struct dirent *de; 32 | struct stat st; 33 | char path[256]; 34 | u32 i; 35 | u64 offset; 36 | 37 | dir = opendir(d); 38 | if (dir == NULL) 39 | fail("opendir"); 40 | 41 | offset = 0; 42 | i = 0; 43 | while ((de = readdir(dir))) { 44 | if (n_files == MAX_FILES) 45 | fail("file overflow. increase MAX_FILES"); 46 | 47 | if (strcmp(de->d_name, ".") == 0) 48 | continue; 49 | 50 | if (strcmp(de->d_name, "..") == 0) 51 | continue; 52 | 53 | if (strlen(de->d_name) > 0x20) 54 | fail("name too long: %s", de->d_name); 55 | 56 | if (de->d_type != DT_REG) 57 | fail("not a file: %s", de->d_name); 58 | 59 | snprintf(path, sizeof path, "%s/%s", d, de->d_name); 60 | 61 | memset(&files[i], 0, sizeof(*files)); 62 | strncpy(files[i].name, de->d_name, 0x19); 63 | 64 | if (stat(path, &st) < 0) 65 | fail("cannot stat %s", path); 66 | files[i].size = st.st_size; 67 | 68 | files[i].ptr = mmap_file(path); 69 | if (files[i].ptr == NULL) 70 | fail("unable to mmap %s", path); 71 | 72 | files[i].offset = offset; 73 | offset = round_up(offset + files[i].size, 0x20); 74 | 75 | i++; 76 | n_files++; 77 | } 78 | } 79 | 80 | static void build_hdr(void) 81 | { 82 | u8 *p; 83 | u32 i; 84 | u64 file_size; 85 | 86 | file_size = files[n_files - 1].offset + files[n_files - 1].size; 87 | hdr_size = 0x10 + n_files * 0x30; 88 | hdr = malloc(hdr_size); 89 | 90 | if (hdr == NULL) 91 | fail("out of memory"); 92 | 93 | memset(hdr, 0, hdr_size); 94 | p = hdr; 95 | 96 | wbe32(p + 0x00, 1); // magic 97 | wbe32(p + 0x04, n_files); 98 | wbe32(p + 0x08, hdr_size + file_size); 99 | p += 0x10; 100 | 101 | for (i = 0; i < n_files; i++) { 102 | wbe64(p + 0x00, files[i].offset + hdr_size); 103 | wbe64(p + 0x08, files[i].size); 104 | strncpy((char *)(p + 0x10), files[i].name, 0x20); 105 | p += 0x30; 106 | } 107 | } 108 | 109 | static void write_pkg(const char *n) 110 | { 111 | FILE *fp; 112 | u32 i; 113 | 114 | fp = fopen(n, "wb"); 115 | if (fp == NULL) 116 | fail("fopen(%s) failed", n); 117 | 118 | fwrite(hdr, hdr_size, 1, fp); 119 | 120 | for (i = 0; i < n_files; i++) { 121 | fseek(fp, files[i].offset + hdr_size, SEEK_SET); 122 | fwrite(files[i].ptr, files[i].size, 1, fp); 123 | } 124 | 125 | fclose(fp); 126 | } 127 | 128 | int main(int argc, char *argv[]) 129 | { 130 | if (argc != 3) 131 | fail("usage: cospkg cos.pkg dir"); 132 | 133 | get_files(argv[2]); 134 | build_hdr(); 135 | write_pkg(argv[1]); 136 | 137 | return 0; 138 | } 139 | -------------------------------------------------------------------------------- /ungpkg.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010-2011 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | // Thanks to Mathieulh for his C# retail unpacker 6 | // (http://twitter.com/#!/Mathieulh/status/23070344881381376) 7 | // Thanks to Matt_P for his python debug unpacker 8 | // (https://github.com/HACKERCHANNEL/PS3Py/blob/master/pkg.py) 9 | 10 | #include "tools.h" 11 | #include "types.h" 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | static u8 *pkg = NULL; 21 | static u64 size; 22 | static u64 offset; 23 | 24 | 25 | static void decrypt_retail_pkg(void) 26 | { 27 | u8 key[0x10]; 28 | u8 iv[0x10]; 29 | 30 | if (be16(pkg + 0x06) != 1) 31 | fail("invalid pkg type: %x", be16(pkg + 0x06)); 32 | 33 | if (key_get_simple("gpkg-key", key, 0x10) < 0) 34 | fail("failed to load the package key."); 35 | 36 | memcpy(iv, pkg + 0x70, 0x10); 37 | aes128ctr(key, iv, pkg + offset, size, pkg + offset); 38 | } 39 | 40 | static void decrypt_debug_pkg(void) 41 | { 42 | u8 key[0x40]; 43 | u8 bfr[0x1c]; 44 | u64 i; 45 | 46 | memset(key, 0, sizeof key); 47 | memcpy(key, pkg + 0x60, 8); 48 | memcpy(key + 0x08, pkg + 0x60, 8); 49 | memcpy(key + 0x10, pkg + 0x60 + 0x08, 8); 50 | memcpy(key + 0x18, pkg + 0x60 + 0x08, 8); 51 | 52 | sha1(key, sizeof key, bfr); 53 | 54 | for (i = 0; i < size; i++) { 55 | if (i != 0 && (i % 16) == 0) { 56 | wbe64(key + 0x38, be64(key + 0x38) + 1); 57 | sha1(key, sizeof key, bfr); 58 | } 59 | pkg[offset + i] ^= bfr[i & 0xf]; 60 | } 61 | } 62 | 63 | static void unpack_pkg(void) 64 | { 65 | u64 i; 66 | u64 n_files; 67 | u32 fname_len; 68 | u32 fname_off; 69 | u64 file_offset; 70 | u32 flags; 71 | char fname[256]; 72 | u8 *tmp; 73 | 74 | n_files = be32(pkg + 0x14); 75 | 76 | for (i = 0; i < n_files; i++) { 77 | tmp = pkg + offset + i*0x20; 78 | 79 | fname_off = be32(tmp) + offset; 80 | fname_len = be32(tmp + 0x04); 81 | file_offset = be64(tmp + 0x08) + offset; 82 | size = be64(tmp + 0x10); 83 | flags = be32(tmp + 0x18); 84 | 85 | if (fname_len >= sizeof fname) 86 | fail("filename too long: %s", pkg + fname_off); 87 | 88 | memset(fname, 0, sizeof fname); 89 | strncpy(fname, (char *)(pkg + fname_off), fname_len); 90 | 91 | flags &= 0xff; 92 | if (flags == 4) 93 | mkdir(fname, 0777); 94 | else if (flags == 1 || flags == 3) 95 | memcpy_to_file(fname, pkg + file_offset, size); 96 | else 97 | fail("unknown flags: %08x", flags); 98 | } 99 | } 100 | 101 | int main(int argc, char *argv[]) 102 | { 103 | char *dir; 104 | 105 | if (argc != 2 && argc != 3) 106 | fail("usage: ungpkg filename.pkg [target]"); 107 | 108 | pkg = mmap_file(argv[1]); 109 | 110 | if (argc == 2) { 111 | dir = malloc(0x31); 112 | memset(dir, 0, 0x31); 113 | memset(dir, 0, 0x30); 114 | memcpy(dir, pkg + 0x30, 0x30); 115 | } else { 116 | dir = argv[2]; 117 | } 118 | 119 | mkdir(dir, 0777); 120 | 121 | if (chdir(dir) != 0) 122 | fail("chdir(%s)", dir); 123 | 124 | offset = be64(pkg + 0x20); 125 | size = be64(pkg + 0x28); 126 | 127 | if (be16(pkg + 0x04) & 0x8000) 128 | decrypt_retail_pkg(); 129 | else 130 | decrypt_debug_pkg(); 131 | 132 | unpack_pkg(); 133 | 134 | return 0; 135 | } 136 | -------------------------------------------------------------------------------- /pupunpack.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "tools.h" 10 | 11 | static u8 *pup = NULL; 12 | static u8 pup_hmac[0x40]; 13 | static int got_hmac = -1; 14 | static u64 n_sections; 15 | static u64 hdr_size; 16 | 17 | static struct id2name_tbl t_names[] = { 18 | {0x100, "version.txt"}, 19 | {0x101, "license.txt"}, 20 | {0x102, "promo_flags.txt"}, 21 | {0x103, "update_flags.txt"}, 22 | {0x104, "patch_build.txt"}, 23 | {0x200, "ps3swu.self"}, 24 | {0x201, "vsh.tar"}, 25 | {0x202, "dots.txt"}, 26 | {0x203, "patch_data.pkg"}, 27 | {0x300, "update_files.tar"}, 28 | {0, NULL} 29 | }; 30 | 31 | static int check_hmac(u8 *hmac, u8 *bfr, u64 len) 32 | { 33 | u8 calc[0x14]; 34 | 35 | if (hmac == NULL) 36 | return 1; 37 | 38 | if (got_hmac < 0) 39 | return 1; 40 | 41 | sha1_hmac(pup_hmac, bfr, len, calc); 42 | 43 | if (memcmp(calc, hmac, sizeof calc) == 0) 44 | return 0; 45 | else 46 | return -1; 47 | } 48 | 49 | static u8 *find_hmac(u32 entry) 50 | { 51 | u8 *ptr; 52 | u32 i; 53 | 54 | ptr = pup + 0x30 + 0x20 * n_sections; 55 | 56 | for(i = 0; i < n_sections; i++) { 57 | if (be64(ptr) == entry) 58 | return ptr + 8; 59 | ptr += 0x20; 60 | } 61 | 62 | fail("not found: %d", entry); 63 | return NULL; 64 | } 65 | 66 | static void do_section(u64 i) 67 | { 68 | u8 *ptr; 69 | u64 entry; 70 | u64 offset; 71 | u64 size; 72 | int hmac_res; 73 | const char *fname; 74 | const char *hmac_status; 75 | 76 | ptr = pup + 0x30 + 0x20 * i; 77 | entry = be64(ptr); 78 | offset = be64(ptr + 0x08); 79 | size = be64(ptr + 0x10); 80 | 81 | fname = id2name(entry, t_names, NULL); 82 | if (fname == NULL) 83 | fail("unknown entry id: %08x_%08x", (u32)(entry >> 32), (u32)entry); 84 | 85 | hmac_res = check_hmac(find_hmac(i), pup + offset, size); 86 | if (hmac_res < 0) 87 | hmac_status = "FAIL"; 88 | else if (hmac_res == 0) 89 | hmac_status = "OK"; 90 | else 91 | hmac_status = "???"; 92 | 93 | printf("unpacking %s (%08x_%08x bytes; hmac: %s)...\n", fname, (u32)(size >> 32), (u32)size, hmac_status); 94 | memcpy_to_file(fname, pup + offset, size); 95 | } 96 | 97 | static void do_pup(void) 98 | { 99 | u64 data_size; 100 | u64 i; 101 | int res; 102 | 103 | n_sections = be64(pup + 0x18); 104 | hdr_size = be64(pup + 0x20); 105 | data_size = be64(pup + 0x28); 106 | 107 | printf("sections: %lld\n", n_sections); 108 | printf("hdr size: %08x_%08x\n", (u32)(hdr_size >> 32), (u32)hdr_size); 109 | printf("data size: %08x_%08x\n", (u32)(data_size >> 32), (u32)data_size); 110 | printf("header hmac: "); 111 | 112 | res = check_hmac(pup + 0x30 + 0x40 * n_sections, pup, 0x30 + 0x40 * n_sections); 113 | 114 | if (res < 0) 115 | printf("FAIL\n"); 116 | else if (res == 0) 117 | printf("OK\n"); 118 | else 119 | printf("???\n"); 120 | 121 | for (i = 0; i < n_sections; i++) 122 | do_section(i); 123 | } 124 | 125 | int main(int argc, char *argv[]) 126 | { 127 | (void)argc; 128 | 129 | if (argc < 3) 130 | fail("usage: pupunpack filename.pup directory"); 131 | 132 | got_hmac = key_get_simple("pup-hmac", pup_hmac, sizeof pup_hmac); 133 | pup = mmap_file(argv[1]); 134 | 135 | if(pup != NULL) 136 | { 137 | if (mkdir(argv[2], 0777) < 0) 138 | fail("mkdir(%s)", argv[2]); 139 | if (chdir(argv[2]) < 0) 140 | fail("chdir(%s)", argv[2]); 141 | do_pup(); 142 | } 143 | 144 | return 0; 145 | } 146 | -------------------------------------------------------------------------------- /puppack.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "tools.h" 12 | 13 | #define MAX_FILES 10 14 | 15 | static FILE *fp; 16 | static u8 pup_hmac[0x40]; 17 | static u8 hdr[0x30 + 0x40 * MAX_FILES + 0x20]; 18 | static u64 n_files; 19 | static u64 data_size; 20 | static u64 build = 0xfa11; 21 | 22 | static struct { 23 | u8 *ptr; 24 | u64 id; 25 | u64 len; 26 | u64 offset; 27 | } files[MAX_FILES]; 28 | 29 | static struct id2name_tbl t_names[] = { 30 | {0x100, "version.txt"}, 31 | {0x101, "license.txt"}, 32 | {0x102, "promo_flags.txt"}, 33 | {0x103, "update_flags.txt"}, 34 | {0x104, "patch_build.txt"}, 35 | {0x200, "ps3swu.self"}, 36 | {0x201, "vsh.tar"}, 37 | {0x202, "dots.txt"}, 38 | {0x203, "patch_data.pkg"}, 39 | {0x300, "update_files.tar"}, 40 | {0, NULL} 41 | }; 42 | 43 | 44 | static void find_files(void) 45 | { 46 | struct id2name_tbl *t; 47 | struct stat st; 48 | u64 offset; 49 | u32 i; 50 | 51 | n_files = 0; 52 | data_size = 0; 53 | 54 | t = t_names; 55 | while(t->name != NULL) { 56 | if (stat(t->name, &st) >= 0) { 57 | files[n_files].id = t->id; 58 | files[n_files].ptr = mmap_file(t->name); 59 | files[n_files].len = st.st_size; 60 | data_size += files[n_files].len; 61 | n_files++; 62 | } 63 | t++; 64 | } 65 | 66 | offset = 0x50 + 0x40 * n_files; 67 | for (i = 0; i < n_files; i++) { 68 | files[i].offset = offset; 69 | offset += files[i].len; 70 | } 71 | } 72 | 73 | static void calc_hmac(u8 *ptr, u64 len, u8 *hmac) 74 | { 75 | memset(hmac, 0, 0x20); 76 | sha1_hmac(pup_hmac, ptr, len, hmac); 77 | } 78 | 79 | static void build_header(void) 80 | { 81 | u32 i; 82 | 83 | memset(hdr, 0, sizeof hdr); 84 | memcpy(hdr, "SCEUF\0\0\0", 8); 85 | 86 | wbe64(hdr + 0x08, 1); 87 | wbe64(hdr + 0x10, build); 88 | wbe64(hdr + 0x18, n_files); 89 | wbe64(hdr + 0x20, 0x50 + n_files * 0x40); 90 | wbe64(hdr + 0x28, data_size); 91 | 92 | for (i = 0; i < n_files; i++) { 93 | wbe64(hdr + 0x30 + 0x20 * i + 0x00, files[i].id); 94 | wbe64(hdr + 0x30 + 0x20 * i + 0x08, files[i].offset); 95 | wbe64(hdr + 0x30 + 0x20 * i + 0x10, files[i].len); 96 | wbe64(hdr + 0x30 + 0x20 * i + 0x18, 0); 97 | 98 | wbe64(hdr + 0x30 + 0x20 * n_files + 0x20 * i, i); 99 | calc_hmac(files[i].ptr, files[i].len, 100 | hdr + 0x30 + 0x20 * n_files + 0x20 * i + 0x08); 101 | } 102 | 103 | calc_hmac(hdr, 0x30 + 0x40 * n_files, 104 | hdr + 0x30 + 0x40 * n_files); 105 | } 106 | 107 | static void write_pup(void) 108 | { 109 | u32 i; 110 | 111 | fseek(fp, 0, SEEK_SET); 112 | fwrite(hdr, 0x50 + 0x40 * n_files, 1, fp); 113 | 114 | for (i = 0; i < n_files; i++) { 115 | fseek(fp, files[i].offset, SEEK_SET); 116 | fwrite(files[i].ptr, files[i].len, 1, fp); 117 | } 118 | } 119 | 120 | int main(int argc, char *argv[]) 121 | { 122 | if (argc < 3) 123 | fail("usage: puppack filename.pup directory [build number]"); 124 | 125 | if (argc == 4) 126 | build = atoi(argv[3]); 127 | 128 | if (key_get_simple("pup-hmac", pup_hmac, sizeof pup_hmac) < 0) 129 | fail("pup hmac key not available"); 130 | 131 | fp = fopen(argv[1], "wb"); 132 | if (fp == NULL) 133 | fail("fopen(%s)", argv[1]); 134 | 135 | if (chdir(argv[2]) < 0) 136 | fail("chdir(%s)", argv[1]); 137 | 138 | find_files(); 139 | build_header(); 140 | write_pup(); 141 | 142 | fclose(fp); 143 | 144 | return 0; 145 | } 146 | 147 | -------------------------------------------------------------------------------- /bn.c: -------------------------------------------------------------------------------- 1 | // Copyright 2007,2008,2010 Segher Boessenkool 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | 8 | #include "tools.h" 9 | 10 | void bn_print(char *name, u8 *a, u32 n) 11 | { 12 | u32 i; 13 | 14 | printf("%s = ", name); 15 | 16 | for (i = 0; i < n; i++) 17 | printf("%02x", a[i]); 18 | 19 | printf("\n"); 20 | } 21 | 22 | static void bn_zero(u8 *d, u32 n) 23 | { 24 | memset(d, 0, n); 25 | } 26 | 27 | void bn_copy(u8 *d, u8 *a, u32 n) 28 | { 29 | memcpy(d, a, n); 30 | } 31 | 32 | int bn_compare(u8 *a, u8 *b, u32 n) 33 | { 34 | u32 i; 35 | 36 | for (i = 0; i < n; i++) { 37 | if (a[i] < b[i]) 38 | return -1; 39 | if (a[i] > b[i]) 40 | return 1; 41 | } 42 | 43 | return 0; 44 | } 45 | 46 | static u8 bn_add_1(u8 *d, u8 *a, u8 *b, u32 n) 47 | { 48 | u32 i; 49 | u32 dig; 50 | u8 c; 51 | 52 | c = 0; 53 | for (i = n - 1; i < n; i--) { 54 | dig = a[i] + b[i] + c; 55 | c = dig >> 8; 56 | d[i] = dig; 57 | } 58 | 59 | return c; 60 | } 61 | 62 | static u8 bn_sub_1(u8 *d, u8 *a, u8 *b, u32 n) 63 | { 64 | u32 i; 65 | u32 dig; 66 | u8 c; 67 | 68 | c = 1; 69 | for (i = n - 1; i < n; i--) { 70 | dig = a[i] + 255 - b[i] + c; 71 | c = dig >> 8; 72 | d[i] = dig; 73 | } 74 | 75 | return 1 - c; 76 | } 77 | 78 | void bn_reduce(u8 *d, u8 *N, u32 n) 79 | { 80 | if (bn_compare(d, N, n) >= 0) 81 | bn_sub_1(d, d, N, n); 82 | } 83 | 84 | void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n) 85 | { 86 | if (bn_add_1(d, a, b, n)) 87 | bn_sub_1(d, d, N, n); 88 | 89 | bn_reduce(d, N, n); 90 | } 91 | 92 | void bn_sub(u8 *d, u8 *a, u8 *b, u8 *N, u32 n) 93 | { 94 | if (bn_sub_1(d, a, b, n)) 95 | bn_add_1(d, d, N, n); 96 | } 97 | 98 | static const u8 inv256[0x80] = { 99 | 0x01, 0xab, 0xcd, 0xb7, 0x39, 0xa3, 0xc5, 0xef, 100 | 0xf1, 0x1b, 0x3d, 0xa7, 0x29, 0x13, 0x35, 0xdf, 101 | 0xe1, 0x8b, 0xad, 0x97, 0x19, 0x83, 0xa5, 0xcf, 102 | 0xd1, 0xfb, 0x1d, 0x87, 0x09, 0xf3, 0x15, 0xbf, 103 | 0xc1, 0x6b, 0x8d, 0x77, 0xf9, 0x63, 0x85, 0xaf, 104 | 0xb1, 0xdb, 0xfd, 0x67, 0xe9, 0xd3, 0xf5, 0x9f, 105 | 0xa1, 0x4b, 0x6d, 0x57, 0xd9, 0x43, 0x65, 0x8f, 106 | 0x91, 0xbb, 0xdd, 0x47, 0xc9, 0xb3, 0xd5, 0x7f, 107 | 0x81, 0x2b, 0x4d, 0x37, 0xb9, 0x23, 0x45, 0x6f, 108 | 0x71, 0x9b, 0xbd, 0x27, 0xa9, 0x93, 0xb5, 0x5f, 109 | 0x61, 0x0b, 0x2d, 0x17, 0x99, 0x03, 0x25, 0x4f, 110 | 0x51, 0x7b, 0x9d, 0x07, 0x89, 0x73, 0x95, 0x3f, 111 | 0x41, 0xeb, 0x0d, 0xf7, 0x79, 0xe3, 0x05, 0x2f, 112 | 0x31, 0x5b, 0x7d, 0xe7, 0x69, 0x53, 0x75, 0x1f, 113 | 0x21, 0xcb, 0xed, 0xd7, 0x59, 0xc3, 0xe5, 0x0f, 114 | 0x11, 0x3b, 0x5d, 0xc7, 0x49, 0x33, 0x55, 0xff, 115 | }; 116 | 117 | static void bn_mon_muladd_dig(u8 *d, u8 *a, u8 b, u8 *N, u32 n) 118 | { 119 | u32 dig; 120 | u32 i; 121 | 122 | u8 z = -(d[n-1] + a[n-1]*b) * inv256[N[n-1]/2]; 123 | 124 | dig = d[n-1] + a[n-1]*b + N[n-1]*z; 125 | dig >>= 8; 126 | 127 | for (i = n - 2; i < n; i--) { 128 | dig += d[i] + a[i]*b + N[i]*z; 129 | d[i+1] = dig; 130 | dig >>= 8; 131 | } 132 | 133 | d[0] = dig; 134 | dig >>= 8; 135 | 136 | if (dig) 137 | bn_sub_1(d, d, N, n); 138 | 139 | bn_reduce(d, N, n); 140 | } 141 | 142 | void bn_mon_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n) 143 | { 144 | u8 t[512]; 145 | u32 i; 146 | 147 | bn_zero(t, n); 148 | 149 | for (i = n - 1; i < n; i--) 150 | bn_mon_muladd_dig(t, a, b[i], N, n); 151 | 152 | bn_copy(d, t, n); 153 | } 154 | 155 | void bn_to_mon(u8 *d, u8 *N, u32 n) 156 | { 157 | u32 i; 158 | 159 | for (i = 0; i < 8*n; i++) 160 | bn_add(d, d, d, N, n); 161 | } 162 | 163 | void bn_from_mon(u8 *d, u8 *N, u32 n) 164 | { 165 | u8 t[512]; 166 | 167 | bn_zero(t, n); 168 | t[n-1] = 1; 169 | bn_mon_mul(d, d, t, N, n); 170 | } 171 | 172 | static void bn_mon_exp(u8 *d, u8 *a, u8 *N, u32 n, u8 *e, u32 en) 173 | { 174 | u8 t[512]; 175 | u32 i; 176 | u8 mask; 177 | 178 | bn_zero(d, n); 179 | d[n-1] = 1; 180 | bn_to_mon(d, N, n); 181 | 182 | for (i = 0; i < en; i++) 183 | for (mask = 0x80; mask != 0; mask >>= 1) { 184 | bn_mon_mul(t, d, d, N, n); 185 | if ((e[i] & mask) != 0) 186 | bn_mon_mul(d, t, a, N, n); 187 | else 188 | bn_copy(d, t, n); 189 | } 190 | } 191 | 192 | void bn_mon_inv(u8 *d, u8 *a, u8 *N, u32 n) 193 | { 194 | u8 t[512], s[512]; 195 | 196 | bn_zero(s, n); 197 | s[n-1] = 2; 198 | bn_sub_1(t, N, s, n); 199 | bn_mon_exp(d, a, N, n, t, n); 200 | } 201 | -------------------------------------------------------------------------------- /sceverify.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include "tools.h" 6 | #include "types.h" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | static u8 *ptr = NULL; 14 | 15 | static u16 type; 16 | static u16 flags; 17 | static u32 meta_offset; 18 | static u64 info_offset; 19 | static u32 app_type; 20 | static u64 filesize; 21 | static u64 header_len; 22 | static int did_fail = 0; 23 | 24 | static struct keylist *klist = NULL; 25 | 26 | static struct keylist *self_load_keys(void) 27 | { 28 | enum sce_key id; 29 | 30 | switch (app_type) { 31 | case 1: 32 | id = KEY_LV0; 33 | break; 34 | case 2: 35 | id = KEY_LV1; 36 | break; 37 | case 3: 38 | id = KEY_LV2; 39 | break; 40 | case 4: 41 | id = KEY_APP; 42 | break; 43 | case 5: 44 | id = KEY_ISO; 45 | break; 46 | case 6: 47 | id = KEY_LDR; 48 | break; 49 | default: 50 | fail("invalid type: %08x", app_type); 51 | } 52 | 53 | return keys_get(id); 54 | } 55 | 56 | static void read_self_header(void) 57 | { 58 | flags = be16(ptr + 0x08); 59 | meta_offset = be32(ptr + 0x0c); 60 | header_len = be64(ptr + 0x10); 61 | filesize = be64(ptr + 0x18); 62 | info_offset = be64(ptr + 0x28); 63 | 64 | app_type = be32(ptr + info_offset + 0x0c); 65 | 66 | klist = self_load_keys(); 67 | } 68 | 69 | static void read_pkg_header(void) 70 | { 71 | flags = be16(ptr + 0x08); 72 | meta_offset = be32(ptr + 0x0c); 73 | header_len = be64(ptr + 0x10); 74 | filesize = be64(ptr + 0x18); 75 | 76 | klist = keys_get(KEY_PKG); 77 | } 78 | 79 | static void read_spp_header(void) 80 | { 81 | flags = be16(ptr + 0x08); 82 | meta_offset = be32(ptr + 0x0c); 83 | header_len = be64(ptr + 0x10); 84 | filesize = be64(ptr + 0x18); 85 | 86 | klist = keys_get(KEY_SPP); 87 | } 88 | 89 | static void decrypt(void) 90 | { 91 | int keyid; 92 | 93 | keyid = sce_decrypt_header(ptr, klist); 94 | 95 | if (keyid < 0) 96 | fail("sce_decrypt_header failed"); 97 | 98 | if (sce_decrypt_data(ptr) < 0) 99 | fail("sce_decrypt_data failed"); 100 | 101 | if (klist->keys[keyid].pub_avail < 0) 102 | fail("no public key available"); 103 | 104 | if (ecdsa_set_curve(klist->keys[keyid].ctype) < 0) 105 | fail("ecdsa_set_curve failed"); 106 | 107 | ecdsa_set_pub(klist->keys[keyid].pub); 108 | } 109 | 110 | static void verify_signature(void) 111 | { 112 | u8 *r, *s; 113 | u8 hash[20]; 114 | u64 sig_len; 115 | 116 | sig_len = be64(ptr + meta_offset + 0x60); 117 | r = ptr + sig_len; 118 | s = r + 21; 119 | 120 | sha1(ptr, sig_len, hash); 121 | 122 | printf("Signature\n"); 123 | if (ecdsa_verify(hash, r, s)) 124 | printf(" Status: OK\n"); 125 | else 126 | printf(" Status: FAIL\n"); 127 | 128 | printf("\n"); 129 | } 130 | 131 | static int verify_hash(u8 *p, u8 *hashes) 132 | { 133 | u64 offset; 134 | u64 size; 135 | u64 id; 136 | u8 *hash, *key; 137 | u8 result[20]; 138 | 139 | offset = be64(p + 0x00); 140 | size = be64(p + 0x08); 141 | id = be32(p + 0x1c); 142 | 143 | if (id == 0xffffffff) 144 | return 0; 145 | 146 | hash = hashes + id * 0x10; 147 | key = hash + 0x20; 148 | 149 | // XXX: possible integer overflow here 150 | if (offset > (filesize + header_len)) 151 | return 1; 152 | 153 | // XXX: possible integer overflow here 154 | if ((offset + size) > (filesize + header_len)) 155 | return 1; 156 | 157 | sha1_hmac(key, ptr + offset, size, result); 158 | 159 | if (memcmp(result, hash, 20) == 0) 160 | return 0; 161 | else 162 | return -1; 163 | } 164 | 165 | static void verify_hashes(void) 166 | { 167 | u32 meta_n_hdr; 168 | u32 i; 169 | u8 *hashes; 170 | int res; 171 | 172 | meta_n_hdr = be32(ptr + meta_offset + 0x60 + 0xc); 173 | hashes = ptr + meta_offset + 0x80 + 0x30 * meta_n_hdr; 174 | 175 | printf("Hashes\n"); 176 | 177 | for (i = 0; i < meta_n_hdr; i++) { 178 | printf(" Section #%02d: ", i); 179 | res = verify_hash(ptr + meta_offset + 0x80 + 0x30 * i, hashes); 180 | if (res < 0) { 181 | did_fail = 1; 182 | printf("FAIL*\n"); 183 | } else if (res > 0) { 184 | printf("???\n"); 185 | } else { 186 | printf("OK\n"); 187 | } 188 | } 189 | 190 | printf("\n"); 191 | } 192 | 193 | int main(int argc, char *argv[]) 194 | { 195 | if (argc != 2) 196 | fail("usage: sceverify filename"); 197 | 198 | ptr = mmap_file(argv[1]); 199 | 200 | type = be16(ptr + 0x0a); 201 | 202 | if (type == 1) 203 | read_self_header(); 204 | else if(type == 3) 205 | read_pkg_header(); 206 | else if(type == 4) 207 | read_spp_header(); 208 | else 209 | fail("Unknown type: %d", type); 210 | 211 | if (flags & 0x8000) 212 | fail("devkit file; nothing to verify"); 213 | 214 | if (klist == NULL) 215 | fail("no key found"); 216 | 217 | decrypt(); 218 | verify_signature(); 219 | verify_hashes(); 220 | 221 | if (did_fail) 222 | printf(" * please not that the hash will always fail for " 223 | "unaligned non-LOAD phdrs\n"); 224 | return 0; 225 | } 226 | -------------------------------------------------------------------------------- /pkg.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include "tools.h" 6 | #include "types.h" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | static struct key k; 17 | static FILE *fp; 18 | 19 | static u8 info0[0x40]; 20 | static u8 info1[0x40]; 21 | 22 | static u8 *content; 23 | static u64 content_size_real; 24 | static u64 content_size_compressed; 25 | 26 | static u8 sce_hdr[0x20]; 27 | static u8 meta_hdr[0x2a0]; 28 | 29 | static u8 *pkg; 30 | static u64 pkg_size; 31 | 32 | static void get_file(u8 *bfr, const char *name, u64 size) 33 | { 34 | FILE *fp; 35 | 36 | fp = fopen(name, "rb"); 37 | if (fp == NULL) 38 | fail("fopen(%s) failed", name); 39 | 40 | fread(bfr, size, 1, fp); 41 | fclose(fp); 42 | } 43 | 44 | static void get_content(void) 45 | { 46 | u8 *base; 47 | uLongf size_zlib; 48 | int res; 49 | struct stat st; 50 | 51 | base = mmap_file("content"); 52 | 53 | if (stat("content", &st) < 0) 54 | fail("stat(content) failed"); 55 | 56 | content_size_real = st.st_size; 57 | content_size_compressed = compressBound(content_size_real); 58 | size_zlib = content_size_compressed; 59 | 60 | content = malloc(content_size_compressed); 61 | if (!content) 62 | fail("out of memory"); 63 | 64 | res = compress(content, &size_zlib, base, content_size_real); 65 | if (res != Z_OK) 66 | fail("compress returned %d", res); 67 | 68 | content_size_compressed = size_zlib; 69 | content = realloc(content, content_size_compressed); 70 | if (!content) 71 | fail("out of memory"); 72 | } 73 | 74 | static void get_key(const char *suffix) 75 | { 76 | if (key_get(KEY_PKG, suffix, &k) < 0) 77 | fail("key_get() failed"); 78 | 79 | if (k.pub_avail < 0) 80 | fail("no public key available"); 81 | 82 | if (k.priv_avail < 0) 83 | fail("no private key available"); 84 | 85 | if (ecdsa_set_curve(k.ctype) < 0) 86 | fail("ecdsa_set_curve failed"); 87 | 88 | ecdsa_set_pub(k.pub); 89 | ecdsa_set_priv(k.priv); 90 | } 91 | 92 | static void build_sce_hdr(void) 93 | { 94 | memset(sce_hdr, 0, sizeof sce_hdr); 95 | 96 | wbe32(sce_hdr + 0x00, 0x53434500); // magic 97 | wbe32(sce_hdr + 0x04, 2); // version 98 | wbe16(sce_hdr + 0x08, 0); // dunno, sdk type? 99 | wbe16(sce_hdr + 0x0a, 3); // SCE header type; pkg 100 | wbe32(sce_hdr + 0x0c, 0); // meta offset 101 | wbe64(sce_hdr + 0x10, sizeof sce_hdr + sizeof meta_hdr); 102 | wbe64(sce_hdr + 0x18, 0x80 + content_size_real); 103 | } 104 | 105 | static void build_meta_hdr(void) 106 | { 107 | u8 *ptr; 108 | 109 | memset(meta_hdr, 0, sizeof meta_hdr); 110 | ptr = meta_hdr; 111 | 112 | // keys for metadata encryptiomn 113 | get_rand(ptr, 0x10); 114 | get_rand(ptr + 0x20, 0x10); 115 | ptr += 0x40; 116 | 117 | // area covered by the signature 118 | wbe64(ptr + 0x00, sizeof sce_hdr + sizeof meta_hdr - 0x30); 119 | wbe32(ptr + 0x0c, 3); // number of encrypted headers 120 | wbe32(ptr + 0x10, 3 * 8); // number of keys/hashes required 121 | ptr += 0x20; 122 | 123 | // first info header 124 | wbe64(ptr + 0x00, 0x2c0); // offset 125 | wbe64(ptr + 0x08, 0x40); // size 126 | wbe32(ptr + 0x10, 1); // unknown 127 | wbe32(ptr + 0x14, 1); // index 128 | wbe32(ptr + 0x18, 2); // unknown again 129 | wbe32(ptr + 0x1c, 0); // sha index 130 | wbe32(ptr + 0x20, 1); // no encryption 131 | wbe32(ptr + 0x24, 0xffffffff); // key index 132 | wbe32(ptr + 0x28, 0xffffffff); // iv index 133 | wbe32(ptr + 0x2c, 0x1); // no compression 134 | ptr += 0x30; 135 | 136 | // second info header 137 | wbe64(ptr + 0x00, 0x300); // offset 138 | wbe64(ptr + 0x08, 0x40); // size 139 | wbe32(ptr + 0x10, 2); // unknown 140 | wbe32(ptr + 0x14, 2); // index 141 | wbe32(ptr + 0x18, 2); // unknown again 142 | wbe32(ptr + 0x1c, 8); // sha index 143 | wbe32(ptr + 0x20, 1); // no encryption 144 | wbe32(ptr + 0x24, 0xffffffff); // key index 145 | wbe32(ptr + 0x28, 0xffffffff); // iv index 146 | wbe32(ptr + 0x2c, 0x1); // no compression 147 | ptr += 0x30; 148 | 149 | // package files 150 | wbe64(ptr + 0x00, 0x340); // offset 151 | wbe64(ptr + 0x08, content_size_compressed); 152 | wbe32(ptr + 0x10, 3); // unknown 153 | wbe32(ptr + 0x14, 3); // index 154 | wbe32(ptr + 0x18, 2); // unknown again 155 | wbe32(ptr + 0x1c, 16); // sha index 156 | wbe32(ptr + 0x20, 3); // encrypted 157 | wbe32(ptr + 0x24, 22); // key index 158 | wbe32(ptr + 0x28, 23); // iv index 159 | wbe32(ptr + 0x2c, 2); // compressed 160 | ptr += 0x30; 161 | 162 | // add keys/ivs and hmac keys 163 | get_rand(ptr, 3 * 8 * 0x10); 164 | } 165 | 166 | static void fix_info_hdr(void) 167 | { 168 | wbe64(info0 + 0x18, content_size_real); 169 | wbe64(info0 + 0x20, content_size_compressed); 170 | wbe64(info1 + 0x20, content_size_compressed); 171 | } 172 | 173 | static void build_pkg(void) 174 | { 175 | pkg_size = sizeof sce_hdr + sizeof meta_hdr + 0x80; 176 | pkg_size += content_size_compressed; 177 | pkg_size = round_up(pkg_size, 0x100); 178 | 179 | pkg = malloc(pkg_size); 180 | if (!pkg) 181 | fail("out of memory"); 182 | 183 | memset(pkg, 0xaa, pkg_size); 184 | memcpy(pkg, sce_hdr, 0x20); 185 | memcpy(pkg + 0x20, meta_hdr, 0x2a0); 186 | memcpy(pkg + 0x2c0, info0, 0x40); 187 | memcpy(pkg + 0x300, info1, 0x40); 188 | memcpy(pkg + 0x340, content, content_size_compressed); 189 | } 190 | 191 | static void calculate_hash(u8 *data, u64 len, u8 *digest) 192 | { 193 | memset(digest, 0, 0x20); 194 | sha1_hmac(digest + 0x20, data, len, digest); 195 | } 196 | 197 | static void hash_pkg(void) 198 | { 199 | calculate_hash(pkg + 0x2c0, 0x40, pkg + 0x80 + 3*0x30); 200 | calculate_hash(pkg + 0x300, 0x40, pkg + 0x80 + 3*0x30 + 8*0x10); 201 | calculate_hash(pkg + 0x340, content_size_compressed, 202 | pkg + 0x80 + 3*0x30 + 16*0x10); 203 | } 204 | 205 | static void sign_pkg(void) 206 | { 207 | u8 *r, *s; 208 | u8 hash[20]; 209 | u64 sig_len; 210 | 211 | sig_len = be64(pkg + 0x60); 212 | r = pkg + sig_len; 213 | s = r + 21; 214 | 215 | sha1(pkg, sig_len, hash); 216 | 217 | ecdsa_sign(hash, r, s); 218 | } 219 | 220 | int main(int argc, char *argv[]) 221 | { 222 | if (argc != 4) 223 | fail("usage: pkg [key suffix] [contents] [filename.pkg]"); 224 | 225 | fp = fopen(argv[3], "wb"); 226 | if (fp == NULL) 227 | fail("fopen(%s) failed", argv[3]); 228 | 229 | if (chdir(argv[2]) < 0) 230 | fail("chdir"); 231 | 232 | get_key(argv[1]); 233 | get_file(info0, "info0", 0x40); 234 | get_file(info1, "info1", 0x40); 235 | get_content(); 236 | 237 | build_sce_hdr(); 238 | build_meta_hdr(); 239 | fix_info_hdr(); 240 | 241 | build_pkg(); 242 | hash_pkg(); 243 | sign_pkg(); 244 | 245 | sce_encrypt_data(pkg); 246 | sce_encrypt_header(pkg, &k); 247 | 248 | fwrite(pkg, pkg_size, 1, fp); 249 | fclose(fp); 250 | 251 | return 0; 252 | } 253 | -------------------------------------------------------------------------------- /ec.c: -------------------------------------------------------------------------------- 1 | // Copyright 2007,2008,2010 Segher Boessenkool 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | 8 | #include "tools.h" 9 | 10 | struct point { 11 | u8 x[20]; 12 | u8 y[20]; 13 | }; 14 | 15 | static u8 ec_p[20]; 16 | static u8 ec_a[20]; // mon 17 | static u8 ec_b[20]; // mon 18 | static u8 ec_N[21]; 19 | static struct point ec_G; // mon 20 | static struct point ec_Q; // mon 21 | static u8 ec_k[21]; 22 | 23 | static void elt_copy(u8 *d, u8 *a) 24 | { 25 | memcpy(d, a, 20); 26 | } 27 | 28 | static void elt_zero(u8 *d) 29 | { 30 | memset(d, 0, 20); 31 | } 32 | 33 | static int elt_is_zero(u8 *d) 34 | { 35 | u32 i; 36 | 37 | for (i = 0; i < 20; i++) 38 | if (d[i] != 0) 39 | return 0; 40 | 41 | return 1; 42 | } 43 | 44 | static void elt_add(u8 *d, u8 *a, u8 *b) 45 | { 46 | bn_add(d, a, b, ec_p, 20); 47 | } 48 | 49 | static void elt_sub(u8 *d, u8 *a, u8 *b) 50 | { 51 | bn_sub(d, a, b, ec_p, 20); 52 | } 53 | 54 | static void elt_mul(u8 *d, u8 *a, u8 *b) 55 | { 56 | bn_mon_mul(d, a, b, ec_p, 20); 57 | } 58 | 59 | static void elt_square(u8 *d, u8 *a) 60 | { 61 | elt_mul(d, a, a); 62 | } 63 | 64 | static void elt_inv(u8 *d, u8 *a) 65 | { 66 | u8 s[20]; 67 | elt_copy(s, a); 68 | bn_mon_inv(d, s, ec_p, 20); 69 | } 70 | 71 | static void point_to_mon(struct point *p) 72 | { 73 | bn_to_mon(p->x, ec_p, 20); 74 | bn_to_mon(p->y, ec_p, 20); 75 | } 76 | 77 | static void point_from_mon(struct point *p) 78 | { 79 | bn_from_mon(p->x, ec_p, 20); 80 | bn_from_mon(p->y, ec_p, 20); 81 | } 82 | 83 | #if 0 84 | static int point_is_on_curve(u8 *p) 85 | { 86 | u8 s[20], t[20]; 87 | u8 *x, *y; 88 | 89 | x = p; 90 | y = p + 20; 91 | 92 | elt_square(t, x); 93 | elt_mul(s, t, x); 94 | 95 | elt_mul(t, x, ec_a); 96 | elt_add(s, s, t); 97 | 98 | elt_add(s, s, ec_b); 99 | 100 | elt_square(t, y); 101 | elt_sub(s, s, t); 102 | 103 | return elt_is_zero(s); 104 | } 105 | #endif 106 | 107 | static void point_zero(struct point *p) 108 | { 109 | elt_zero(p->x); 110 | elt_zero(p->y); 111 | } 112 | 113 | static int point_is_zero(struct point *p) 114 | { 115 | return elt_is_zero(p->x) && elt_is_zero(p->y); 116 | } 117 | 118 | static void point_double(struct point *r, struct point *p) 119 | { 120 | u8 s[20], t[20]; 121 | struct point pp; 122 | u8 *px, *py, *rx, *ry; 123 | 124 | pp = *p; 125 | 126 | px = pp.x; 127 | py = pp.y; 128 | rx = r->x; 129 | ry = r->y; 130 | 131 | if (elt_is_zero(py)) { 132 | point_zero(r); 133 | return; 134 | } 135 | 136 | elt_square(t, px); // t = px*px 137 | elt_add(s, t, t); // s = 2*px*px 138 | elt_add(s, s, t); // s = 3*px*px 139 | elt_add(s, s, ec_a); // s = 3*px*px + a 140 | elt_add(t, py, py); // t = 2*py 141 | elt_inv(t, t); // t = 1/(2*py) 142 | elt_mul(s, s, t); // s = (3*px*px+a)/(2*py) 143 | 144 | elt_square(rx, s); // rx = s*s 145 | elt_add(t, px, px); // t = 2*px 146 | elt_sub(rx, rx, t); // rx = s*s - 2*px 147 | 148 | elt_sub(t, px, rx); // t = -(rx-px) 149 | elt_mul(ry, s, t); // ry = -s*(rx-px) 150 | elt_sub(ry, ry, py); // ry = -s*(rx-px) - py 151 | } 152 | 153 | static void point_add(struct point *r, struct point *p, struct point *q) 154 | { 155 | u8 s[20], t[20], u[20]; 156 | u8 *px, *py, *qx, *qy, *rx, *ry; 157 | struct point pp, qq; 158 | 159 | pp = *p; 160 | qq = *q; 161 | 162 | px = pp.x; 163 | py = pp.y; 164 | qx = qq.x; 165 | qy = qq.y; 166 | rx = r->x; 167 | ry = r->y; 168 | 169 | if (point_is_zero(&pp)) { 170 | elt_copy(rx, qx); 171 | elt_copy(ry, qy); 172 | return; 173 | } 174 | 175 | if (point_is_zero(&qq)) { 176 | elt_copy(rx, px); 177 | elt_copy(ry, py); 178 | return; 179 | } 180 | 181 | elt_sub(u, qx, px); 182 | 183 | if (elt_is_zero(u)) { 184 | elt_sub(u, qy, py); 185 | if (elt_is_zero(u)) 186 | point_double(r, &pp); 187 | else 188 | point_zero(r); 189 | 190 | return; 191 | } 192 | 193 | elt_inv(t, u); // t = 1/(qx-px) 194 | elt_sub(u, qy, py); // u = qy-py 195 | elt_mul(s, t, u); // s = (qy-py)/(qx-px) 196 | 197 | elt_square(rx, s); // rx = s*s 198 | elt_add(t, px, qx); // t = px+qx 199 | elt_sub(rx, rx, t); // rx = s*s - (px+qx) 200 | 201 | elt_sub(t, px, rx); // t = -(rx-px) 202 | elt_mul(ry, s, t); // ry = -s*(rx-px) 203 | elt_sub(ry, ry, py); // ry = -s*(rx-px) - py 204 | } 205 | 206 | static void point_mul(struct point *d, u8 *a, struct point *b) // a is bignum 207 | { 208 | u32 i; 209 | u8 mask; 210 | 211 | point_zero(d); 212 | 213 | for (i = 0; i < 21; i++) 214 | for (mask = 0x80; mask != 0; mask >>= 1) { 215 | point_double(d, d); 216 | if ((a[i] & mask) != 0) 217 | point_add(d, d, b); 218 | } 219 | } 220 | 221 | static void generate_ecdsa(u8 *R, u8 *S, u8 *k, u8 *hash) 222 | { 223 | u8 e[21]; 224 | u8 kk[21]; 225 | u8 m[21]; 226 | u8 minv[21]; 227 | struct point mG; 228 | FILE *fp; 229 | 230 | e[0] = 0; 231 | memcpy(e + 1, hash, 20); 232 | bn_reduce(e, ec_N, 21); 233 | 234 | try_again: 235 | fp = fopen("/dev/random", "rb"); 236 | if (fread(m, sizeof m, 1, fp) != 1) 237 | fail("reading random"); 238 | fclose(fp); 239 | m[0] = 0; 240 | if (bn_compare(m, ec_N, 21) >= 0) 241 | goto try_again; 242 | 243 | // R = (mG).x 244 | 245 | point_mul(&mG, m, &ec_G); 246 | point_from_mon(&mG); 247 | R[0] = 0; 248 | elt_copy(R+1, mG.x); 249 | 250 | // S = m**-1*(e + Rk) (mod N) 251 | 252 | bn_copy(kk, k, 21); 253 | bn_reduce(kk, ec_N, 21); 254 | bn_to_mon(m, ec_N, 21); 255 | bn_to_mon(e, ec_N, 21); 256 | bn_to_mon(R, ec_N, 21); 257 | bn_to_mon(kk, ec_N, 21); 258 | 259 | bn_mon_mul(S, R, kk, ec_N, 21); 260 | bn_add(kk, S, e, ec_N, 21); 261 | bn_mon_inv(minv, m, ec_N, 21); 262 | bn_mon_mul(S, minv, kk, ec_N, 21); 263 | 264 | bn_from_mon(R, ec_N, 21); 265 | bn_from_mon(S, ec_N, 21); 266 | } 267 | 268 | static int check_ecdsa(struct point *Q, u8 *R, u8 *S, u8 *hash) 269 | { 270 | u8 Sinv[21]; 271 | u8 e[21]; 272 | u8 w1[21], w2[21]; 273 | struct point r1, r2; 274 | u8 rr[21]; 275 | 276 | e[0] = 0; 277 | memcpy(e + 1, hash, 20); 278 | bn_reduce(e, ec_N, 21); 279 | 280 | bn_to_mon(R, ec_N, 21); 281 | bn_to_mon(S, ec_N, 21); 282 | bn_to_mon(e, ec_N, 21); 283 | 284 | bn_mon_inv(Sinv, S, ec_N, 21); 285 | 286 | bn_mon_mul(w1, e, Sinv, ec_N, 21); 287 | bn_mon_mul(w2, R, Sinv, ec_N, 21); 288 | 289 | bn_from_mon(w1, ec_N, 21); 290 | bn_from_mon(w2, ec_N, 21); 291 | 292 | point_mul(&r1, w1, &ec_G); 293 | point_mul(&r2, w2, Q); 294 | 295 | point_add(&r1, &r1, &r2); 296 | 297 | point_from_mon(&r1); 298 | 299 | rr[0] = 0; 300 | memcpy(rr + 1, r1.x, 20); 301 | bn_reduce(rr, ec_N, 21); 302 | 303 | bn_from_mon(R, ec_N, 21); 304 | bn_from_mon(S, ec_N, 21); 305 | 306 | return (bn_compare(rr, R, 21) == 0); 307 | } 308 | 309 | #if 0 310 | static void ec_priv_to_pub(u8 *k, u8 *Q) 311 | { 312 | point_mul(Q, k, ec_G); 313 | } 314 | #endif 315 | 316 | int ecdsa_set_curve(u32 type) 317 | { 318 | if (ecdsa_get_params(type, ec_p, ec_a, ec_b, ec_N, ec_G.x, ec_G.y) < 0) 319 | return -1; 320 | 321 | bn_to_mon(ec_a, ec_p, 20); 322 | bn_to_mon(ec_b, ec_p, 20); 323 | 324 | point_to_mon(&ec_G); 325 | 326 | return 0; 327 | } 328 | 329 | void ecdsa_set_pub(u8 *Q) 330 | { 331 | memcpy(ec_Q.x, Q, 20); 332 | memcpy(ec_Q.y, Q+20, 20); 333 | point_to_mon(&ec_Q); 334 | } 335 | 336 | void ecdsa_set_priv(u8 *k) 337 | { 338 | memcpy(ec_k, k, sizeof ec_k); 339 | } 340 | 341 | int ecdsa_verify(u8 *hash, u8 *R, u8 *S) 342 | { 343 | return check_ecdsa(&ec_Q, R, S, hash); 344 | } 345 | 346 | void ecdsa_sign(u8 *hash, u8 *R, u8 *S) 347 | { 348 | generate_ecdsa(R, S, ec_k, hash); 349 | } 350 | -------------------------------------------------------------------------------- /unself.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include "tools.h" 6 | #include "types.h" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #define MAX_SECTIONS 255 14 | 15 | static u8 *self = NULL; 16 | static u8 *elf = NULL; 17 | static FILE *out = NULL; 18 | 19 | static u64 info_offset; 20 | static u32 key_ver; 21 | static u64 phdr_offset; 22 | static u64 shdr_offset; 23 | static u64 sec_offset; 24 | static u64 ver_offset; 25 | static u64 version; 26 | static u64 elf_offset; 27 | static u64 meta_offset; 28 | static u64 header_len; 29 | static u64 filesize; 30 | static u32 arch64; 31 | static u32 n_sections; 32 | 33 | static struct elf_hdr ehdr; 34 | 35 | static u32 app_type; 36 | 37 | static struct { 38 | u32 offset; 39 | u32 size; 40 | u32 compressed; 41 | u32 size_uncompressed; 42 | u32 elf_offset; 43 | } self_sections[MAX_SECTIONS]; 44 | 45 | static void read_header(void) 46 | { 47 | key_ver = be16(self + 0x08); 48 | meta_offset = be32(self + 0x0c); 49 | header_len = be64(self + 0x10); 50 | filesize = be64(self + 0x18); 51 | info_offset = be64(self + 0x28); 52 | elf_offset = be64(self + 0x30); 53 | phdr_offset = be64(self + 0x38) - elf_offset; 54 | shdr_offset = be64(self + 0x40) - elf_offset; 55 | sec_offset = be64(self + 0x48); 56 | ver_offset = be64(self + 0x50); 57 | 58 | version = be64(self + info_offset + 0x10); 59 | app_type = be32(self + info_offset + 0x0c); 60 | 61 | elf = self + elf_offset; 62 | arch64 = elf_read_hdr(elf, &ehdr); 63 | } 64 | 65 | struct self_sec { 66 | u32 idx; 67 | u64 offset; 68 | u64 size; 69 | u32 compressed; 70 | u32 encrypted; 71 | u64 next; 72 | }; 73 | 74 | static void read_section(u32 i, struct self_sec *sec) 75 | { 76 | u8 *ptr; 77 | 78 | ptr = self + sec_offset + i*0x20; 79 | 80 | sec->idx = i; 81 | sec->offset = be64(ptr + 0x00); 82 | sec->size = be64(ptr + 0x08); 83 | sec->compressed = be32(ptr + 0x10) == 2 ? 1 : 0; 84 | sec->encrypted = be32(ptr + 0x20); 85 | sec->next = be64(ptr + 0x20); 86 | } 87 | 88 | static int qsort_compare(const void *a, const void *b) 89 | { 90 | const struct self_sec *sa, *sb; 91 | sa = a; 92 | sb = b; 93 | 94 | if (sa->offset > sb->offset) 95 | return 1; 96 | else if(sa->offset < sb->offset) 97 | return -1; 98 | else 99 | return 0; 100 | } 101 | 102 | static void read_sections(void) 103 | { 104 | struct self_sec s[MAX_SECTIONS]; 105 | struct elf_phdr p; 106 | u32 i; 107 | u32 j; 108 | u32 n_secs; 109 | u32 self_offset, elf_offset; 110 | 111 | memset(s, 0, sizeof s); 112 | for (i = 0, j = 0; i < ehdr.e_phnum; i++) { 113 | read_section(i, &s[j]); 114 | if (s[j].compressed) 115 | j++; 116 | } 117 | 118 | n_secs = j; 119 | qsort(s, n_secs, sizeof(*s), qsort_compare); 120 | 121 | elf_offset = 0; 122 | self_offset = header_len; 123 | j = 0; 124 | i = 0; 125 | while (elf_offset < filesize) { 126 | if (i == n_secs) { 127 | self_sections[j].offset = self_offset; 128 | self_sections[j].size = filesize - elf_offset; 129 | self_sections[j].compressed = 0; 130 | self_sections[j].size_uncompressed = filesize - elf_offset; 131 | self_sections[j].elf_offset = elf_offset; 132 | elf_offset = filesize; 133 | } else if (self_offset == s[i].offset) { 134 | self_sections[j].offset = self_offset; 135 | self_sections[j].size = s[i].size; 136 | self_sections[j].compressed = 1; 137 | elf_read_phdr(arch64, elf + phdr_offset + 138 | (ehdr.e_phentsize * s[i].idx), &p); 139 | self_sections[j].size_uncompressed = p.p_filesz; 140 | self_sections[j].elf_offset = p.p_off; 141 | 142 | elf_offset = p.p_off + p.p_filesz; 143 | self_offset = s[i].next; 144 | i++; 145 | } else { 146 | elf_read_phdr(arch64, elf + phdr_offset + 147 | (ehdr.e_phentsize * s[i].idx), &p); 148 | self_sections[j].offset = self_offset; 149 | self_sections[j].size = p.p_off - elf_offset; 150 | self_sections[j].compressed = 0; 151 | self_sections[j].size_uncompressed = self_sections[j].size; 152 | self_sections[j].elf_offset = elf_offset; 153 | 154 | elf_offset += self_sections[j].size; 155 | self_offset += s[i].offset - self_offset; 156 | } 157 | j++; 158 | } 159 | 160 | n_sections = j; 161 | } 162 | 163 | static void write_elf(void) 164 | { 165 | u32 i; 166 | u8 *bfr; 167 | u32 size; 168 | u32 offset = 0; 169 | 170 | for (i = 0; i < n_sections; i++) { 171 | fseek(out, self_sections[i].elf_offset, SEEK_SET); 172 | offset = self_sections[i].elf_offset; 173 | if (self_sections[i].compressed) { 174 | size = self_sections[i].size_uncompressed; 175 | 176 | bfr = malloc(size); 177 | if (bfr == NULL) 178 | fail("failed to allocate %d bytes", size); 179 | 180 | offset += size; 181 | 182 | decompress(self + self_sections[i].offset, 183 | self_sections[i].size, 184 | bfr, size); 185 | fwrite(bfr, size, 1, out); 186 | free(bfr); 187 | } else { 188 | bfr = self + self_sections[i].offset; 189 | size = self_sections[i].size; 190 | offset += size; 191 | 192 | fwrite(bfr, size, 1, out); 193 | } 194 | } 195 | } 196 | 197 | static void remove_shnames(u64 shdr_offset, u16 n_shdr, u64 shstrtab_offset, u32 strtab_size) 198 | { 199 | u16 i; 200 | u32 size; 201 | struct elf_shdr s; 202 | 203 | if (arch64) 204 | size = 0x40; 205 | else 206 | size = 0x28; 207 | 208 | for (i = 0; i < n_shdr; i++) { 209 | elf_read_shdr(arch64, elf + shdr_offset + i * size, &s); 210 | 211 | s.sh_name = 0; 212 | if (s.sh_type == 3) { 213 | s.sh_offset = shstrtab_offset; 214 | s.sh_size = strtab_size; 215 | } 216 | 217 | elf_write_shdr(arch64, elf + shdr_offset + i * size, &s); 218 | } 219 | } 220 | 221 | static void check_elf(void) 222 | { 223 | u8 bfr[0x48]; 224 | u64 elf_offset; 225 | u64 phdr_offset; 226 | u64 shdr_offset; 227 | u64 phdr_offset_new; 228 | u64 shdr_offset_new; 229 | u64 shstrtab_offset; 230 | u16 n_phdr; 231 | u16 n_shdr; 232 | const char shstrtab[] = ".unknown\0\0"; 233 | const char elf_magic[4] = {0x7f, 'E', 'L', 'F'}; 234 | 235 | fseek(out, 0, SEEK_SET); 236 | fread(bfr, 4, 1, out); 237 | 238 | if (memcmp(bfr, elf_magic, sizeof elf_magic) == 0) 239 | return; 240 | 241 | elf_offset = be64(self + 0x30); 242 | phdr_offset = be64(self + 0x38) - elf_offset; 243 | shdr_offset = be64(self + 0x40) - elf_offset; 244 | 245 | if (arch64) { 246 | fseek(out, 0x48, SEEK_SET); 247 | phdr_offset_new = 0x48; 248 | 249 | fseek(out, 0, SEEK_END); 250 | shdr_offset_new = ftell(out); 251 | 252 | n_phdr = be16(elf + 0x38); 253 | n_shdr = be16(elf + 0x3c); 254 | shstrtab_offset = shdr_offset_new + n_shdr * 0x40; 255 | 256 | remove_shnames(shdr_offset, n_shdr, shstrtab_offset, sizeof shstrtab); 257 | 258 | fseek(out, phdr_offset_new, SEEK_SET); 259 | fwrite(elf + phdr_offset, 0x38, n_phdr, out); 260 | 261 | fseek(out, shdr_offset_new, SEEK_SET); 262 | fwrite(elf + shdr_offset, 0x40, n_shdr, out); 263 | 264 | wbe64(elf + 0x20, phdr_offset_new); 265 | wbe64(elf + 0x28, shdr_offset_new); 266 | 267 | fseek(out, SEEK_SET, 0); 268 | fwrite(elf, 0x48, 1, out); 269 | 270 | fseek(out, shstrtab_offset, SEEK_SET); 271 | fwrite(shstrtab, sizeof shstrtab, 1, out); 272 | } else { 273 | fseek(out, 0x34, SEEK_SET); 274 | phdr_offset_new = 0x34; 275 | fseek(out, 0, SEEK_END); 276 | shdr_offset_new = ftell(out); 277 | 278 | n_phdr = be16(elf + 0x2c); 279 | n_shdr = be16(elf + 0x30); 280 | shstrtab_offset = shdr_offset_new + n_shdr * 0x40; 281 | 282 | remove_shnames(shdr_offset, n_shdr, shstrtab_offset, sizeof shstrtab); 283 | 284 | fseek(out, phdr_offset_new, SEEK_SET); 285 | fwrite(elf + phdr_offset, 0x20, n_phdr, out); 286 | 287 | fseek(out, shdr_offset_new, SEEK_SET); 288 | fwrite(elf + shdr_offset, 0x28, n_shdr, out); 289 | 290 | wbe32(elf + 0x1c, phdr_offset_new); 291 | wbe32(elf + 0x20, shdr_offset_new); 292 | 293 | fseek(out, SEEK_SET, 0); 294 | fwrite(elf, 0x34, 1, out); 295 | 296 | fseek(out, shstrtab_offset, SEEK_SET); 297 | fwrite(shstrtab, sizeof shstrtab, 1, out); 298 | } 299 | } 300 | 301 | static struct keylist *self_load_keys(void) 302 | { 303 | enum sce_key id; 304 | 305 | switch (app_type) { 306 | case 1: 307 | id = KEY_LV0; 308 | break; 309 | case 2: 310 | id = KEY_LV1; 311 | break; 312 | case 3: 313 | id = KEY_LV2; 314 | break; 315 | case 4: 316 | id = KEY_APP; 317 | break; 318 | case 5: 319 | id = KEY_ISO; 320 | break; 321 | case 6: 322 | id = KEY_LDR; 323 | break; 324 | default: 325 | fail("invalid type: %08x", app_type); 326 | } 327 | 328 | return keys_get(id); 329 | } 330 | 331 | static void self_decrypt(void) 332 | { 333 | struct keylist *klist; 334 | 335 | klist = self_load_keys(); 336 | if (klist == NULL) 337 | fail("no key found"); 338 | 339 | if (sce_decrypt_header(self, klist) < 0) 340 | fail("self_decrypt_header failed"); 341 | 342 | if (sce_decrypt_data(self) < 0) 343 | fail("self_decrypt_data failed"); 344 | } 345 | 346 | int main(int argc, char *argv[]) 347 | { 348 | if (argc != 3) 349 | fail("usage: unself in.self out.elf"); 350 | 351 | self = mmap_file(argv[1]); 352 | 353 | if (be32(self) != 0x53434500) 354 | fail("not a SELF"); 355 | 356 | read_header(); 357 | read_sections(); 358 | 359 | if (key_ver != 0x8000) 360 | self_decrypt(); 361 | 362 | out = fopen(argv[2], "w+"); 363 | 364 | write_elf(); 365 | check_elf(); 366 | 367 | fclose(out); 368 | 369 | return 0; 370 | } 371 | -------------------------------------------------------------------------------- /sha1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * sha1.c 3 | * 4 | * Copyright (C) 1998 5 | * Paul E. Jones 6 | * All Rights Reserved 7 | * 8 | * This software is licensed as "freeware." Permission to distribute 9 | * this software in source and binary forms is hereby granted without 10 | * a fee. THIS SOFTWARE IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED 11 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 12 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 13 | * THE AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DAMAGES RESULTING 14 | * FROM THE USE OF THIS SOFTWARE, EITHER DIRECTLY OR INDIRECTLY, INCLUDING, 15 | * BUT NOT LIMITED TO, LOSS OF DATA OR DATA BEING RENDERED INACCURATE. 16 | * 17 | ***************************************************************************** 18 | * $Id: sha1.c,v 1.2 2004/03/27 18:00:33 paulej Exp $ 19 | ***************************************************************************** 20 | * 21 | * Description: 22 | * This file implements the Secure Hashing Standard as defined 23 | * in FIPS PUB 180-1 published April 17, 1995. 24 | * 25 | * The Secure Hashing Standard, which uses the Secure Hashing 26 | * Algorithm (SHA), produces a 160-bit message digest for a 27 | * given data stream. In theory, it is highly improbable that 28 | * two messages will produce the same message digest. Therefore, 29 | * this algorithm can serve as a means of providing a "fingerprint" 30 | * for a message. 31 | * 32 | * Portability Issues: 33 | * SHA-1 is defined in terms of 32-bit "words". This code was 34 | * written with the expectation that the processor has at least 35 | * a 32-bit machine word size. If the machine word size is larger, 36 | * the code should still function properly. One caveat to that 37 | * is that the input functions taking characters and character 38 | * arrays assume that only 8 bits of information are stored in each 39 | * character. 40 | * 41 | * Caveats: 42 | * SHA-1 is designed to work with messages less than 2^64 bits 43 | * long. Although SHA-1 allows a message digest to be generated for 44 | * messages of any number of bits less than 2^64, this 45 | * implementation only works with messages with a length that is a 46 | * multiple of the size of an 8-bit character. 47 | * 48 | */ 49 | 50 | #include "sha1.h" 51 | 52 | /* 53 | * Define the circular shift macro 54 | */ 55 | #define SHA1CircularShift(bits,word) \ 56 | ((((word) << (bits)) & 0xFFFFFFFF) | \ 57 | ((word) >> (32-(bits)))) 58 | 59 | /* Function prototypes */ 60 | void SHA1ProcessMessageBlock(SHA1Context *); 61 | void SHA1PadMessage(SHA1Context *); 62 | 63 | /* 64 | * SHA1Reset 65 | * 66 | * Description: 67 | * This function will initialize the SHA1Context in preparation 68 | * for computing a new message digest. 69 | * 70 | * Parameters: 71 | * context: [in/out] 72 | * The context to reset. 73 | * 74 | * Returns: 75 | * Nothing. 76 | * 77 | * Comments: 78 | * 79 | */ 80 | void SHA1Reset(SHA1Context *context) 81 | { 82 | context->Length_Low = 0; 83 | context->Length_High = 0; 84 | context->Message_Block_Index = 0; 85 | 86 | context->Message_Digest[0] = 0x67452301; 87 | context->Message_Digest[1] = 0xEFCDAB89; 88 | context->Message_Digest[2] = 0x98BADCFE; 89 | context->Message_Digest[3] = 0x10325476; 90 | context->Message_Digest[4] = 0xC3D2E1F0; 91 | 92 | context->Computed = 0; 93 | context->Corrupted = 0; 94 | } 95 | 96 | /* 97 | * SHA1Result 98 | * 99 | * Description: 100 | * This function will return the 160-bit message digest into the 101 | * Message_Digest array within the SHA1Context provided 102 | * 103 | * Parameters: 104 | * context: [in/out] 105 | * The context to use to calculate the SHA-1 hash. 106 | * 107 | * Returns: 108 | * 1 if successful, 0 if it failed. 109 | * 110 | * Comments: 111 | * 112 | */ 113 | int SHA1Result(SHA1Context *context) 114 | { 115 | 116 | if (context->Corrupted) 117 | { 118 | return 0; 119 | } 120 | 121 | if (!context->Computed) 122 | { 123 | SHA1PadMessage(context); 124 | context->Computed = 1; 125 | } 126 | 127 | return 1; 128 | } 129 | 130 | /* 131 | * SHA1Input 132 | * 133 | * Description: 134 | * This function accepts an array of octets as the next portion of 135 | * the message. 136 | * 137 | * Parameters: 138 | * context: [in/out] 139 | * The SHA-1 context to update 140 | * message_array: [in] 141 | * An array of characters representing the next portion of the 142 | * message. 143 | * length: [in] 144 | * The length of the message in message_array 145 | * 146 | * Returns: 147 | * Nothing. 148 | * 149 | * Comments: 150 | * 151 | */ 152 | void SHA1Input( SHA1Context *context, 153 | const unsigned char *message_array, 154 | unsigned length) 155 | { 156 | if (!length) 157 | { 158 | return; 159 | } 160 | 161 | if (context->Computed || context->Corrupted) 162 | { 163 | context->Corrupted = 1; 164 | return; 165 | } 166 | 167 | while(length-- && !context->Corrupted) 168 | { 169 | context->Message_Block[context->Message_Block_Index++] = 170 | (*message_array & 0xFF); 171 | 172 | context->Length_Low += 8; 173 | /* Force it to 32 bits */ 174 | context->Length_Low &= 0xFFFFFFFF; 175 | if (context->Length_Low == 0) 176 | { 177 | context->Length_High++; 178 | /* Force it to 32 bits */ 179 | context->Length_High &= 0xFFFFFFFF; 180 | if (context->Length_High == 0) 181 | { 182 | /* Message is too long */ 183 | context->Corrupted = 1; 184 | } 185 | } 186 | 187 | if (context->Message_Block_Index == 64) 188 | { 189 | SHA1ProcessMessageBlock(context); 190 | } 191 | 192 | message_array++; 193 | } 194 | } 195 | 196 | /* 197 | * SHA1ProcessMessageBlock 198 | * 199 | * Description: 200 | * This function will process the next 512 bits of the message 201 | * stored in the Message_Block array. 202 | * 203 | * Parameters: 204 | * None. 205 | * 206 | * Returns: 207 | * Nothing. 208 | * 209 | * Comments: 210 | * Many of the variable names in the SHAContext, especially the 211 | * single character names, were used because those were the names 212 | * used in the publication. 213 | * 214 | * 215 | */ 216 | void SHA1ProcessMessageBlock(SHA1Context *context) 217 | { 218 | const unsigned K[] = /* Constants defined in SHA-1 */ 219 | { 220 | 0x5A827999, 221 | 0x6ED9EBA1, 222 | 0x8F1BBCDC, 223 | 0xCA62C1D6 224 | }; 225 | int t; /* Loop counter */ 226 | unsigned temp; /* Temporary word value */ 227 | unsigned W[80]; /* Word sequence */ 228 | unsigned A, B, C, D, E; /* Word buffers */ 229 | 230 | /* 231 | * Initialize the first 16 words in the array W 232 | */ 233 | for(t = 0; t < 16; t++) 234 | { 235 | W[t] = ((unsigned) context->Message_Block[t * 4]) << 24; 236 | W[t] |= ((unsigned) context->Message_Block[t * 4 + 1]) << 16; 237 | W[t] |= ((unsigned) context->Message_Block[t * 4 + 2]) << 8; 238 | W[t] |= ((unsigned) context->Message_Block[t * 4 + 3]); 239 | } 240 | 241 | for(t = 16; t < 80; t++) 242 | { 243 | W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]); 244 | } 245 | 246 | A = context->Message_Digest[0]; 247 | B = context->Message_Digest[1]; 248 | C = context->Message_Digest[2]; 249 | D = context->Message_Digest[3]; 250 | E = context->Message_Digest[4]; 251 | 252 | for(t = 0; t < 20; t++) 253 | { 254 | temp = SHA1CircularShift(5,A) + 255 | ((B & C) | ((~B) & D)) + E + W[t] + K[0]; 256 | temp &= 0xFFFFFFFF; 257 | E = D; 258 | D = C; 259 | C = SHA1CircularShift(30,B); 260 | B = A; 261 | A = temp; 262 | } 263 | 264 | for(t = 20; t < 40; t++) 265 | { 266 | temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1]; 267 | temp &= 0xFFFFFFFF; 268 | E = D; 269 | D = C; 270 | C = SHA1CircularShift(30,B); 271 | B = A; 272 | A = temp; 273 | } 274 | 275 | for(t = 40; t < 60; t++) 276 | { 277 | temp = SHA1CircularShift(5,A) + 278 | ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2]; 279 | temp &= 0xFFFFFFFF; 280 | E = D; 281 | D = C; 282 | C = SHA1CircularShift(30,B); 283 | B = A; 284 | A = temp; 285 | } 286 | 287 | for(t = 60; t < 80; t++) 288 | { 289 | temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3]; 290 | temp &= 0xFFFFFFFF; 291 | E = D; 292 | D = C; 293 | C = SHA1CircularShift(30,B); 294 | B = A; 295 | A = temp; 296 | } 297 | 298 | context->Message_Digest[0] = 299 | (context->Message_Digest[0] + A) & 0xFFFFFFFF; 300 | context->Message_Digest[1] = 301 | (context->Message_Digest[1] + B) & 0xFFFFFFFF; 302 | context->Message_Digest[2] = 303 | (context->Message_Digest[2] + C) & 0xFFFFFFFF; 304 | context->Message_Digest[3] = 305 | (context->Message_Digest[3] + D) & 0xFFFFFFFF; 306 | context->Message_Digest[4] = 307 | (context->Message_Digest[4] + E) & 0xFFFFFFFF; 308 | 309 | context->Message_Block_Index = 0; 310 | } 311 | 312 | /* 313 | * SHA1PadMessage 314 | * 315 | * Description: 316 | * According to the standard, the message must be padded to an even 317 | * 512 bits. The first padding bit must be a '1'. The last 64 318 | * bits represent the length of the original message. All bits in 319 | * between should be 0. This function will pad the message 320 | * according to those rules by filling the Message_Block array 321 | * accordingly. It will also call SHA1ProcessMessageBlock() 322 | * appropriately. When it returns, it can be assumed that the 323 | * message digest has been computed. 324 | * 325 | * Parameters: 326 | * context: [in/out] 327 | * The context to pad 328 | * 329 | * Returns: 330 | * Nothing. 331 | * 332 | * Comments: 333 | * 334 | */ 335 | void SHA1PadMessage(SHA1Context *context) 336 | { 337 | /* 338 | * Check to see if the current message block is too small to hold 339 | * the initial padding bits and length. If so, we will pad the 340 | * block, process it, and then continue padding into a second 341 | * block. 342 | */ 343 | if (context->Message_Block_Index > 55) 344 | { 345 | context->Message_Block[context->Message_Block_Index++] = 0x80; 346 | while(context->Message_Block_Index < 64) 347 | { 348 | context->Message_Block[context->Message_Block_Index++] = 0; 349 | } 350 | 351 | SHA1ProcessMessageBlock(context); 352 | 353 | while(context->Message_Block_Index < 56) 354 | { 355 | context->Message_Block[context->Message_Block_Index++] = 0; 356 | } 357 | } 358 | else 359 | { 360 | context->Message_Block[context->Message_Block_Index++] = 0x80; 361 | while(context->Message_Block_Index < 56) 362 | { 363 | context->Message_Block[context->Message_Block_Index++] = 0; 364 | } 365 | } 366 | 367 | /* 368 | * Store the message length as the last 8 octets 369 | */ 370 | context->Message_Block[56] = (context->Length_High >> 24) & 0xFF; 371 | context->Message_Block[57] = (context->Length_High >> 16) & 0xFF; 372 | context->Message_Block[58] = (context->Length_High >> 8) & 0xFF; 373 | context->Message_Block[59] = (context->Length_High) & 0xFF; 374 | context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF; 375 | context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF; 376 | context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF; 377 | context->Message_Block[63] = (context->Length_Low) & 0xFF; 378 | 379 | SHA1ProcessMessageBlock(context); 380 | } 381 | -------------------------------------------------------------------------------- /makeself.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include "tools.h" 6 | #include "types.h" 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define ALIGNMENT 0x20 16 | #define MAX_PHDR 255 17 | 18 | static u8 *elf = NULL; 19 | static u8 *self = NULL; 20 | 21 | static enum sce_key type; 22 | 23 | struct elf_hdr ehdr; 24 | struct elf_phdr phdr[MAX_PHDR]; 25 | static int arch64; 26 | 27 | static u8 sce_header[0x70]; 28 | static u8 info_header[0x20]; 29 | static u8 ctrl_header[0x70]; 30 | static u8 version_header[0x10]; 31 | 32 | static u8 *sec_header; 33 | static u32 sec_header_size; 34 | 35 | static u8 *meta_header; 36 | static u32 meta_header_size; 37 | 38 | static u64 header_size; 39 | static u32 meta_offset; 40 | static u64 elf_size; 41 | static u64 compressed_size; 42 | static u64 info_offset; 43 | static u64 version_offset; 44 | static u64 elf_offset; 45 | static u64 phdr_offset; 46 | static u64 shdr_offset; 47 | static u64 sec_offset; 48 | static u64 ctrl_offset; 49 | static u64 version; 50 | static u64 auth_id; 51 | static u64 vendor_id; 52 | static u16 sdk_type; 53 | 54 | struct key ks; 55 | 56 | static const char *elf_name = NULL; 57 | static const char *self_name = NULL; 58 | static int compression = 0; 59 | 60 | static struct { 61 | u64 offset; 62 | u64 size; 63 | u8 *ptr; 64 | int compressed; 65 | } phdr_map[MAX_PHDR]; 66 | 67 | static void get_type(const char *p) 68 | { 69 | if (strncmp(p, "lv0", 4) == 0) 70 | type = KEY_LV0; 71 | else if (strncmp(p, "lv1", 4) == 0) 72 | type = KEY_LV1; 73 | else if (strncmp(p, "lv2", 4) == 0) 74 | type = KEY_LV2; 75 | else if (strncmp(p, "iso", 4) == 0) 76 | type = KEY_ISO; 77 | else if (strncmp(p, "app", 4) == 0) 78 | type = KEY_APP; 79 | else if (strncmp(p, "ldr", 4) == 0) 80 | type = KEY_LDR; 81 | else 82 | fail("invalid type: %s", p); 83 | } 84 | 85 | static void get_keys(const char *suffix) 86 | { 87 | if (key_get(type, suffix, &ks) < 0) 88 | fail("key_get failed"); 89 | 90 | if (ks.pub_avail < 0) 91 | fail("no public key available"); 92 | 93 | if (ks.priv_avail < 0) 94 | fail("no private key available"); 95 | 96 | if (ecdsa_set_curve(ks.ctype) < 0) 97 | fail("ecdsa_set_curve failed"); 98 | 99 | ecdsa_set_pub(ks.pub); 100 | ecdsa_set_priv(ks.priv); 101 | } 102 | 103 | static void parse_elf(void) 104 | { 105 | u32 i; 106 | 107 | arch64 = elf_read_hdr(elf, &ehdr); 108 | 109 | for (i = 0; i < ehdr.e_phnum; i++) 110 | elf_read_phdr(arch64, elf + ehdr.e_phoff + i * ehdr.e_phentsize, &phdr[i]); 111 | } 112 | 113 | static void build_sce_hdr(void) 114 | { 115 | memset(sce_header, 0, sizeof sce_header); 116 | 117 | wbe32(sce_header + 0x00, 0x53434500); // magic 118 | wbe32(sce_header + 0x04, 2); // version 119 | wbe16(sce_header + 0x08, sdk_type); // dunno, sdk type? 120 | wbe16(sce_header + 0x0a, 1); // SCE header type; self 121 | wbe32(sce_header + 0x0c, meta_offset); 122 | wbe64(sce_header + 0x10, header_size); 123 | wbe64(sce_header + 0x18, round_up(elf_size, ALIGNMENT)); 124 | wbe64(sce_header + 0x20, 3); // dunno, has to be 3 125 | wbe64(sce_header + 0x28, info_offset); 126 | wbe64(sce_header + 0x30, elf_offset); 127 | wbe64(sce_header + 0x38, phdr_offset); 128 | wbe64(sce_header + 0x40, shdr_offset); 129 | wbe64(sce_header + 0x48, sec_offset); 130 | wbe64(sce_header + 0x50, version_offset); 131 | wbe64(sce_header + 0x58, ctrl_offset); 132 | wbe64(sce_header + 0x60, 0x70); // ctrl size 133 | } 134 | 135 | static void build_version_hdr(void) 136 | { 137 | memset(version_header, 0, sizeof version_header); 138 | wbe32(version_header, 1); 139 | wbe32(version_header + 0x08, 0x10); 140 | } 141 | 142 | static void build_info_hdr(void) 143 | { 144 | u32 app_type; 145 | 146 | memset(info_header, 0, sizeof info_header); 147 | 148 | switch (type) { 149 | case KEY_LV0: 150 | app_type = 1; 151 | break; 152 | case KEY_LV1: 153 | app_type = 2; 154 | break; 155 | case KEY_LV2: 156 | app_type = 3; 157 | break; 158 | case KEY_APP: 159 | app_type = 4; 160 | break; 161 | case KEY_ISO: 162 | app_type = 5; 163 | break; 164 | case KEY_LDR: 165 | app_type = 6; 166 | break; 167 | default: 168 | fail("something that should never fail failed."); 169 | } 170 | 171 | wbe64(info_header + 0x00, auth_id); 172 | wbe32(info_header + 0x08, vendor_id); 173 | wbe32(info_header + 0x0c, app_type); 174 | wbe64(info_header + 0x10, version); // version 1.0.0 175 | } 176 | 177 | static void build_ctrl_hdr(void) 178 | { 179 | memset(ctrl_header, 0, sizeof ctrl_header); 180 | 181 | wbe32(ctrl_header + 0x00, 1); // type: control flags 182 | wbe32(ctrl_header + 0x04, 0x30); // length 183 | // flags are all zero here 184 | 185 | wbe32(ctrl_header + 0x30, 2); // type: digest 186 | wbe32(ctrl_header + 0x34, 0x40); // length 187 | } 188 | 189 | static void build_sec_hdr(void) 190 | { 191 | u32 i; 192 | u8 *ptr; 193 | 194 | sec_header_size = ehdr.e_phnum * 0x20; 195 | sec_header = malloc(sec_header_size); 196 | 197 | memset(sec_header, 0, sec_header_size); 198 | 199 | for (i = 0; i < ehdr.e_phnum; i++) { 200 | ptr = sec_header + i * 0x20; 201 | 202 | wbe64(ptr + 0x00, phdr_map[i].offset); 203 | wbe64(ptr + 0x08, phdr_map[i].size); 204 | 205 | if (phdr_map[i].compressed == 1) 206 | wbe32(ptr + 0x10, 2); 207 | else 208 | wbe32(ptr + 0x10, 1); 209 | 210 | wbe32(ptr + 0x14, 0); // unknown 211 | wbe32(ptr + 0x18, 0); // unknown 212 | 213 | if (phdr[i].p_type == 1) 214 | wbe32(ptr + 0x1c, 1); // encrypted LOAD phdr 215 | else 216 | wbe32(ptr + 0x1c, 0); // no loadable phdr 217 | } 218 | } 219 | 220 | static void meta_add_phdr(u8 *ptr, u32 i) 221 | { 222 | wbe64(ptr + 0x00, phdr_map[i].offset); 223 | wbe64(ptr + 0x08, phdr_map[i].size); 224 | 225 | // unknown 226 | wbe32(ptr + 0x10, 2); 227 | wbe32(ptr + 0x14, i); // phdr index maybe? 228 | wbe32(ptr + 0x18, 2); 229 | 230 | wbe32(ptr + 0x1c, i*8); // sha index 231 | wbe32(ptr + 0x20, 1); // not encpryted 232 | wbe32(ptr + 0x24, 0xffffffff); // no key 233 | wbe32(ptr + 0x28, 0xffffffff); // no iv 234 | wbe32(ptr + 0x2c, 1); // not compressed 235 | } 236 | 237 | static void meta_add_load(u8 *ptr, u32 i) 238 | { 239 | wbe64(ptr + 0x00, phdr_map[i].offset); 240 | wbe64(ptr + 0x08, phdr_map[i].size); 241 | 242 | // unknown 243 | wbe32(ptr + 0x10, 2); 244 | wbe32(ptr + 0x14, i); // phdr index maybe? 245 | wbe32(ptr + 0x18, 2); 246 | 247 | wbe32(ptr + 0x1c, i*8); // sha index 248 | wbe32(ptr + 0x20, 3); // phdr is encrypted 249 | wbe32(ptr + 0x24, (i*8) + 6); // key index 250 | wbe32(ptr + 0x28, (i*8) + 7); // iv index 251 | 252 | if (phdr_map[i].compressed == 1) 253 | wbe32(ptr + 0x2c, 2); 254 | else 255 | wbe32(ptr + 0x2c, 1); 256 | } 257 | 258 | static void build_meta_hdr(void) 259 | { 260 | u32 i; 261 | u8 *ptr; 262 | 263 | meta_header_size = 0x80 + ehdr.e_phnum * (0x30 + 0x20 + 0x60) + 0x30; 264 | meta_header = malloc(meta_header_size); 265 | memset(meta_header, 0, meta_header_size); 266 | 267 | ptr = meta_header + 0x20; 268 | 269 | // aes keys for meta encryption 270 | get_rand(ptr, 0x10); 271 | get_rand(ptr + 0x20, 0x10); 272 | ptr += 0x40; 273 | 274 | // area covered by the signature 275 | wbe64(ptr + 0x00, meta_offset + meta_header_size - 0x30); 276 | 277 | wbe32(ptr + 0x08, 1); 278 | wbe32(ptr + 0x0c, ehdr.e_phnum); // number of encrypted headers 279 | wbe32(ptr + 0x10, ehdr.e_phnum * 8); // number of keys/hashes required 280 | wbe32(ptr + 0x14, meta_header_size / 0x10); 281 | ptr += 0x20; 282 | 283 | // add encrypted phdr information 284 | for (i = 0; i < ehdr.e_phnum; i++) { 285 | if (phdr[i].p_type == 1) 286 | meta_add_load(ptr, i); 287 | else 288 | meta_add_phdr(ptr, i); 289 | 290 | ptr += 0x30; 291 | } 292 | 293 | // add keys/ivs and hmac keys 294 | get_rand(ptr, ehdr.e_phnum * 8 * 0x10); 295 | } 296 | 297 | static void calculate_hashes(void) 298 | { 299 | u32 i; 300 | u8 *keys; 301 | 302 | keys = self + meta_offset + 0x80 + (0x30 * ehdr.e_phnum); 303 | 304 | for (i = 0; i < ehdr.e_phnum; i++) { 305 | memset(keys + (i * 8 * 0x10), 0, 0x20); 306 | sha1_hmac(keys + ((i * 8) + 2) * 0x10, 307 | self + phdr_map[i].offset, 308 | phdr_map[i].size, 309 | keys + (i * 8) * 0x10 310 | ); 311 | } 312 | } 313 | 314 | static void build_hdr(void) 315 | { 316 | memcpy(self, sce_header, sizeof sce_header); 317 | memcpy(self + info_offset, info_header, sizeof info_header); 318 | memcpy(self + version_offset, version_header, sizeof version_header); 319 | memcpy(self + ctrl_offset, ctrl_header, sizeof ctrl_header); 320 | memcpy(self + sec_offset, sec_header, sec_header_size); 321 | memcpy(self + phdr_offset, elf + ehdr.e_phoff, ehdr.e_phnum * ehdr.e_phentsize); 322 | // memcpy(self + shdr_offset, elf + ehdr.e_shoff, ehdr.e_shnum * ehdr.e_shentsize); 323 | memcpy(self + meta_offset, meta_header, meta_header_size); 324 | memcpy(self + elf_offset, elf, ehdr.e_ehsize); 325 | } 326 | 327 | static void write_elf(void) 328 | { 329 | u32 i; 330 | 331 | if (compression) { 332 | for (i = 0; i < ehdr.e_phnum; i++) { 333 | memcpy(self + phdr_map[i].offset, 334 | phdr_map[i].ptr, 335 | phdr_map[i].size); 336 | } 337 | memcpy(self + shdr_offset, elf + ehdr.e_shoff, ehdr.e_shnum * ehdr.e_shentsize); 338 | } else { 339 | memcpy(self + header_size, elf, elf_size); 340 | } 341 | } 342 | 343 | static void compress_elf(void) 344 | { 345 | u32 i; 346 | u64 offset; 347 | uLongf size_zlib; 348 | int res; 349 | u64 size_compressed; 350 | 351 | offset = header_size; 352 | 353 | for (i = 0; i < ehdr.e_phnum; i++) { 354 | phdr_map[i].offset = offset; 355 | 356 | if (phdr[i].p_type != 1) { 357 | phdr_map[i].ptr = elf + phdr[i].p_off; 358 | phdr_map[i].size = phdr[i].p_filesz; 359 | phdr_map[i].compressed = 0; 360 | offset = round_up(offset + phdr[i].p_filesz, 0x20); 361 | continue; 362 | } 363 | 364 | size_compressed = compressBound(phdr[i].p_filesz); 365 | size_zlib = size_compressed; 366 | 367 | phdr_map[i].ptr = malloc(size_compressed); 368 | if (!phdr_map[i].ptr) 369 | fail("out of memory"); 370 | 371 | res = compress(phdr_map[i].ptr, &size_zlib, 372 | elf + phdr[i].p_off, phdr[i].p_filesz); 373 | 374 | if (size_zlib >= phdr[i].p_filesz) { 375 | free(phdr_map[i].ptr); 376 | phdr_map[i].ptr = elf + phdr[i].p_off; 377 | phdr_map[i].size = phdr[i].p_filesz; 378 | phdr_map[i].compressed = 0; 379 | offset = round_up(offset + phdr[i].p_filesz, ALIGNMENT); 380 | } else { 381 | phdr_map[i].ptr = realloc(phdr_map[i].ptr, size_zlib); 382 | if (phdr_map[i].ptr == NULL) 383 | fail("out of memory"); 384 | 385 | phdr_map[i].size = size_zlib; 386 | phdr_map[i].compressed = 1; 387 | offset = round_up(offset + phdr_map[i].size, ALIGNMENT); 388 | } 389 | } 390 | 391 | compressed_size = phdr_map[i - 1].offset + phdr_map[i - 1].size; 392 | shdr_offset = compressed_size; 393 | compressed_size += ehdr.e_shentsize * ehdr.e_shnum; 394 | } 395 | 396 | static void fill_phdr_map(void) 397 | { 398 | u32 i; 399 | 400 | memset(phdr_map, 0, sizeof phdr_map); 401 | 402 | for (i = 0; i < ehdr.e_phnum; i++) { 403 | phdr_map[i].offset = phdr[i].p_off + header_size; 404 | phdr_map[i].size = phdr[i].p_filesz; 405 | phdr_map[i].compressed = 0; 406 | phdr[i].ptr = NULL; 407 | } 408 | 409 | compressed_size = elf_size; 410 | shdr_offset = ehdr.e_shoff + header_size; 411 | } 412 | 413 | static void sign_hdr(void) 414 | { 415 | u8 *r, *s; 416 | u8 hash[20]; 417 | u64 sig_len; 418 | 419 | sig_len = be64(self + meta_offset + 0x60); 420 | r = self + sig_len; 421 | s = r + 21; 422 | 423 | sha1(self, sig_len, hash); 424 | 425 | ecdsa_sign(hash, r, s); 426 | } 427 | 428 | static u64 get_filesize(const char *path) 429 | { 430 | struct stat st; 431 | 432 | stat(path, &st); 433 | 434 | return st.st_size; 435 | } 436 | 437 | static void get_version(const char *v) 438 | { 439 | u8 *ptr; 440 | u32 i; 441 | u32 maj, min, rev; 442 | u32 tmp; 443 | 444 | i = 0; 445 | maj = min = rev = tmp = 0; 446 | ptr = (u8 *)v; 447 | while (*ptr) { 448 | if (i > 2) { 449 | fprintf(stderr, "WARNING: invalid sdk_version, using 1.0.0\n"); 450 | version = 1ULL << 48; 451 | return; 452 | } 453 | 454 | if (*ptr == '.') { 455 | if (i == 0) 456 | maj = tmp; 457 | else if (i == 1) 458 | min = tmp; 459 | else if (i == 2) 460 | rev = tmp; 461 | i++; 462 | ptr++; 463 | tmp = 0; 464 | continue; 465 | } 466 | 467 | if (*ptr >= '0' && *ptr <= '9') { 468 | tmp <<= 4; 469 | tmp += *ptr - '0'; 470 | ptr++; 471 | continue; 472 | } 473 | 474 | fprintf(stderr, "WARNING: invalid sdk_version, using 1.0.0\n"); 475 | version = 1ULL << 48; 476 | return; 477 | } 478 | 479 | if (i == 2) 480 | rev = tmp; 481 | 482 | version = ((u64)maj & 0xffff) << 48; 483 | version |= ((u64)min & 0xffff) << 32; 484 | version |= rev; 485 | } 486 | 487 | static void get_vendor(char *v) 488 | { 489 | vendor_id = strtoull(v, NULL, 16); 490 | } 491 | 492 | static void get_auth(char *a) 493 | { 494 | auth_id = strtoull(a, NULL, 16); 495 | } 496 | 497 | static void get_sdktype(char * t) 498 | { 499 | sdk_type = strtoul(t, NULL, 10); 500 | } 501 | 502 | static void get_args(int argc, char *argv[]) 503 | { 504 | u32 i; 505 | 506 | if (argc != 9 && argc != 10) 507 | fail("usage: makeself [-c] [type] [version suffix] [version] [vendor id] [auth id] [sdk type] [elf] [self]"); 508 | 509 | i = 1; 510 | 511 | if (argc == 10) { 512 | if (strcmp(argv[1], "-c") != 0) 513 | fail("invalid option: %s", argv[1]); 514 | compression = 1; 515 | i++; 516 | } 517 | 518 | get_type(argv[i++]); 519 | get_keys(argv[i++]); 520 | get_version(argv[i++]); 521 | get_vendor(argv[i++]); 522 | get_auth(argv[i++]); 523 | get_sdktype(argv[i++]); 524 | 525 | elf_name = argv[i++]; 526 | self_name = argv[i++]; 527 | 528 | if (compression) { 529 | if (type == KEY_ISO) 530 | fail("no compression support for isolated modules"); 531 | if (type == KEY_LDR) 532 | fail("no compression support for secure loaders"); 533 | } 534 | } 535 | 536 | 537 | int main(int argc, char *argv[]) 538 | { 539 | FILE *fp; 540 | u8 bfr[ALIGNMENT]; 541 | 542 | get_args(argc, argv); 543 | 544 | elf_size = get_filesize(elf_name); 545 | elf = mmap_file(elf_name); 546 | 547 | parse_elf(); 548 | 549 | meta_header_size = 0x80 + ehdr.e_phnum * (0x30 + 0x20 + 0x60) + 0x30; 550 | info_offset = 0x70; 551 | elf_offset = 0x90; 552 | phdr_offset = elf_offset + ehdr.e_ehsize; 553 | sec_offset = round_up(phdr_offset + ehdr.e_phentsize * ehdr.e_phnum, ALIGNMENT); 554 | version_offset = round_up(sec_offset + ehdr.e_phnum * 0x20, ALIGNMENT); 555 | ctrl_offset = round_up(version_offset + 0x10, ALIGNMENT); 556 | meta_offset = round_up(ctrl_offset + 0x70, ALIGNMENT); 557 | header_size = round_up(meta_offset + meta_header_size, 0x80); 558 | 559 | if (compression) 560 | compress_elf(); 561 | else 562 | fill_phdr_map(); 563 | 564 | build_sce_hdr(); 565 | build_info_hdr(); 566 | build_ctrl_hdr(); 567 | build_sec_hdr(); 568 | build_version_hdr(); 569 | build_meta_hdr(); 570 | 571 | self = malloc(header_size + elf_size); 572 | memset(self, 0, header_size + elf_size); 573 | 574 | build_hdr(); 575 | write_elf(); 576 | calculate_hashes(); 577 | sign_hdr(); 578 | 579 | sce_encrypt_data(self); 580 | sce_encrypt_header(self, &ks); 581 | 582 | fp = fopen(self_name, "wb"); 583 | if (fp == NULL) 584 | fail("fopen(%s) failed", self_name); 585 | 586 | if (fwrite(self, header_size + compressed_size, 1, fp) != 1) 587 | fail("unable to write self"); 588 | 589 | memset(bfr, 0, sizeof bfr); 590 | fwrite(bfr, round_up(compressed_size, ALIGNMENT) - compressed_size, 1, fp); 591 | 592 | fclose(fp); 593 | 594 | return 0; 595 | } 596 | -------------------------------------------------------------------------------- /readself.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | // 6 | // Thanks to xorloser for his selftool! 7 | // (see xorloser.com) 8 | // 9 | 10 | 11 | #include 12 | #include 13 | #include 14 | #include "tools.h" 15 | 16 | static u8 *self; 17 | static u8 *elf; 18 | 19 | static struct elf_hdr ehdr; 20 | 21 | static int arch64; 22 | static u32 meta_offset; 23 | static u64 elf_offset; 24 | static u64 header_len; 25 | static u64 phdr_offset; 26 | static u64 shdr_offset; 27 | static u64 filesize; 28 | static u32 vendorid; 29 | static u64 authid; 30 | static u64 app_version; 31 | static u32 app_type; 32 | static u16 sdk_type; 33 | static u64 info_offset; 34 | static u64 sec_offset; 35 | static u64 ver_info; 36 | static u64 ctrl_offset; 37 | static u64 ctrl_size; 38 | 39 | static int decrypted = -1; 40 | 41 | struct id2name_tbl t_sdk_type[] = { 42 | {0, "Retail (Type 0)"}, 43 | {1, "Retail"}, 44 | {2, "Retail (Type 1)"}, 45 | {0x8000, "Devkit"}, 46 | {0, NULL} 47 | }; 48 | 49 | struct id2name_tbl t_app_type[] = { 50 | {1, "level 0"}, 51 | {2, "level 1"}, 52 | {3, "level 2"}, 53 | {4, "application"}, 54 | {5, "isolated SPU module"}, 55 | {6, "secure loader"}, 56 | {8, "NP-DRM application"}, 57 | {0, NULL} 58 | }; 59 | 60 | static struct id2name_tbl t_shdr_type[] = { 61 | {0, "NULL"}, 62 | {1, "PROGBITS"}, 63 | {2, "SYMTAB"}, 64 | {3, "STRTAB"}, 65 | {4, "RELA"}, 66 | {5, "HASH"}, 67 | {6, "DYNAMIC"}, 68 | {7, "NOTE"}, 69 | {8, "NOBITS"}, 70 | {9, "REL"}, 71 | {10, "SHLIB"}, 72 | {11, "DYNSYM"}, 73 | {12, NULL}, 74 | }; 75 | 76 | static struct id2name_tbl t_elf_type[] = { 77 | {ET_NONE, "None"}, 78 | {ET_REL, "Relocatable file"}, 79 | {ET_EXEC, "Executable file"}, 80 | {ET_DYN, "Shared object file"}, 81 | {ET_CORE, "Core file"}, 82 | {0, NULL} 83 | }; 84 | 85 | static struct id2name_tbl t_elf_machine[] = { 86 | {20, "PowerPC"}, 87 | {21, "PowerPC64"}, 88 | {23, "SPE"}, 89 | {0, NULL} 90 | }; 91 | 92 | 93 | static struct id2name_tbl t_phdr_type[] = { 94 | {0, "NULL"}, 95 | {1, "LOAD"}, 96 | {2, "DYN"}, 97 | {3, "INTPR"}, 98 | {4, "NOTE"}, 99 | {5, "SHLIB"}, 100 | {6, "PHDR"}, 101 | {0, NULL} 102 | }; 103 | 104 | static struct id2name_tbl t_compressed[] = { 105 | {1, "[NO ]"}, 106 | {2, "[YES]"}, 107 | {0, NULL} 108 | }; 109 | 110 | static struct id2name_tbl t_encrypted[] = { 111 | {0, "[N/A]"}, 112 | {1, "[YES]"}, 113 | {2, "[NO ]"}, 114 | {0, NULL} 115 | }; 116 | 117 | static void parse_self(void) 118 | { 119 | sdk_type = be16(self + 0x08); 120 | meta_offset = be32(self + 0x0c); 121 | header_len = be64(self + 0x10); 122 | filesize = be64(self + 0x18); 123 | info_offset = be64(self + 0x28); 124 | elf_offset = be64(self + 0x30); 125 | phdr_offset = be64(self + 0x38) - elf_offset; 126 | shdr_offset = be64(self + 0x40) - elf_offset; 127 | sec_offset = be64(self + 0x48); 128 | ver_info = be64(self + 0x50); 129 | ctrl_offset = be64(self + 0x58); 130 | ctrl_size = be64(self + 0x60); 131 | 132 | vendorid = be32(self + info_offset + 0x08); 133 | authid = be64(self + info_offset + 0x00); 134 | app_type = be32(self + info_offset + 0x0c); 135 | app_version = be64(self + info_offset + 0x10); 136 | 137 | elf = self + elf_offset; 138 | arch64 = elf_read_hdr(elf, &ehdr); 139 | } 140 | 141 | 142 | static struct keylist *self_load_keys(void) 143 | { 144 | enum sce_key id; 145 | 146 | switch (app_type) { 147 | case 1: 148 | id = KEY_LV0; 149 | break; 150 | case 2: 151 | id = KEY_LV1; 152 | break; 153 | case 3: 154 | id = KEY_LV2; 155 | break; 156 | case 4: 157 | id = KEY_APP; 158 | break; 159 | case 5: 160 | id = KEY_ISO; 161 | break; 162 | case 6: 163 | id = KEY_LDR; 164 | break; 165 | case 8: 166 | return NULL; 167 | break; 168 | default: 169 | fail("invalid type: %08x", app_type); 170 | } 171 | 172 | return keys_get(id); 173 | } 174 | 175 | static void decrypt_header(void) 176 | { 177 | struct keylist *klist; 178 | 179 | klist = self_load_keys(); 180 | if (klist == NULL) 181 | return; 182 | 183 | decrypted = sce_decrypt_header(self, klist); 184 | free(klist->keys); 185 | free(klist); 186 | } 187 | 188 | 189 | static const char *get_auth_type(void) 190 | { 191 | return "Unknown"; 192 | } 193 | 194 | static void show_self_header(void) 195 | { 196 | printf("SELF header\n"); 197 | printf(" elf #1 offset: %08x_%08x\n", (u32)(elf_offset>>32), (u32)elf_offset); 198 | printf(" header len: %08x_%08x\n", (u32)(header_len>>32), (u32)header_len); 199 | printf(" meta offset: %08x_%08x\n", 0, meta_offset); 200 | printf(" phdr offset: %08x_%08x\n", (u32)(phdr_offset>>32), (u32)phdr_offset); 201 | printf(" shdr offset: %08x_%08x\n", (u32)(shdr_offset>>32), (u32)shdr_offset); 202 | printf(" file size: %08x_%08x\n", (u32)(filesize>>32), (u32)filesize); 203 | printf(" auth id: %08x_%08x (%s)\n", (u32)(authid>>32), (u32)authid, get_auth_type()); 204 | printf(" vendor id: %08x\n", vendorid); 205 | printf(" info offset: %08x_%08x\n", (u32)(info_offset >> 32), (u32)info_offset); 206 | printf(" sinfo offset: %08x_%08x\n", (u32)(sec_offset >> 32), (u32)sec_offset); 207 | printf(" version offset: %08x_%08x\n", (u32)(ver_info >> 32), (u32)ver_info); 208 | printf(" control info: %08x_%08x (%08x_%08x bytes)\n", 209 | (u32)(ctrl_offset >> 32), (u32)ctrl_offset, 210 | (u32)(ctrl_size >> 32), (u32)ctrl_size); 211 | 212 | printf(" app version: %x.%x.%x\n", (u16)(app_version >> 48), (u16)(app_version >> 32), (u32)app_version); 213 | printf(" SDK type: %s\n", id2name(sdk_type, t_sdk_type, "unknown")); 214 | printf(" app type: %s\n", id2name(app_type, t_app_type, "unknown")); 215 | 216 | 217 | printf("\n"); 218 | } 219 | 220 | static void print_hash(u8 *ptr, u32 len) 221 | { 222 | while(len--) 223 | printf(" %02x", *ptr++); 224 | } 225 | 226 | 227 | static void show_ctrl(void) 228 | { 229 | u32 i, j; 230 | u32 type, length; 231 | 232 | printf("Control info\n"); 233 | 234 | for (i = 0; i < ctrl_size; ) { 235 | type = be32(self + ctrl_offset + i); 236 | length = be32(self + ctrl_offset + i + 0x04); 237 | switch (type) { 238 | case 1: 239 | if (length == 0x30) { 240 | printf(" control flags:\n "); 241 | print_hash(self + ctrl_offset + i + 0x10, 0x10); 242 | printf("\n"); 243 | break; 244 | } 245 | case 2: 246 | if (length == 0x40) { 247 | printf(" file digest:\n "); 248 | print_hash(self + ctrl_offset + i + 0x10, 0x14); 249 | printf("\n "); 250 | print_hash(self + ctrl_offset + i + 0x24, 0x14); 251 | printf("\n"); 252 | break; 253 | } 254 | if (length == 0x30) { 255 | printf(" file digest:\n "); 256 | print_hash(self + ctrl_offset + i + 0x10, 0x14); 257 | printf("\n"); 258 | break; 259 | } 260 | case 3: 261 | if (length == 0x90) { 262 | 263 | char id[0x31]; 264 | memset(id, 0, 0x31); 265 | memcpy(id, self + ctrl_offset + i + 0x20, 0x30); 266 | 267 | printf(" NPDRM info:\n"); 268 | printf(" magic: %08x\n", be32(self + ctrl_offset + i + 0x10)); 269 | printf(" unk0 : %08x\n", be32(self + ctrl_offset + i + 0x14)); 270 | printf(" unk1 : %08x\n", be32(self + ctrl_offset + i + 0x18)); 271 | printf(" unk2 : %08x\n", be32(self + ctrl_offset + i + 0x1c)); 272 | printf(" content_id: %s\n", id); 273 | printf(" digest: "); 274 | print_hash(self + ctrl_offset + i + 0x50, 0x10); 275 | printf("\n invdigest: "); 276 | print_hash(self + ctrl_offset + i + 0x60, 0x10); 277 | printf("\n xordigest: "); 278 | print_hash(self + ctrl_offset + i + 0x70, 0x10); 279 | printf("\n"); 280 | break; 281 | } 282 | default: 283 | printf(" unknown:\n"); 284 | for(j = 0; j < length; j++) { 285 | if ((j % 16) == 0) 286 | printf(" "); 287 | printf(" %02x", be8(self + ctrl_offset + i + j)); 288 | if ((j % 16) == 15 || (j == length - 1)) 289 | printf("\n"); 290 | } 291 | break; 292 | } 293 | i += length; 294 | } 295 | printf("\n"); 296 | } 297 | 298 | static void show_sinfo(void) 299 | { 300 | u32 i; 301 | u64 offset, size; 302 | u32 compressed, encrypted; 303 | u32 unk1, unk2; 304 | 305 | printf("Section header\n"); 306 | 307 | printf(" offset size compressed unk1" 308 | " unk2 encrypted\n"); 309 | 310 | for (i = 0; i < ehdr.e_phnum; i++) { 311 | offset = be64(self + sec_offset + i*0x20 + 0x00); 312 | size = be64(self + sec_offset + i*0x20 + 0x08); 313 | compressed = be32(self + sec_offset + i*0x20 + 0x10); 314 | unk1 = be32(self + sec_offset + i*0x20 + 0x14); 315 | unk2 = be32(self + sec_offset + i*0x20 + 0x18); 316 | encrypted = be32(self + sec_offset + i*0x20 + 0x1c); 317 | printf(" %08x_%08x %08x_%08x %s %08x %08x %s\n", 318 | (u32)(offset >> 32), (u32)offset, 319 | (u32)(size >> 32), (u32)size, 320 | id2name(compressed, t_compressed, "[???]"), 321 | unk1, unk2, 322 | id2name(encrypted, t_encrypted, "[???]") 323 | ); 324 | } 325 | 326 | printf("\n"); 327 | } 328 | 329 | static void show_meta(void) 330 | { 331 | u32 meta_len; 332 | u32 meta_n_hdr; 333 | u32 meta_n_keys; 334 | u32 i; 335 | u64 offset, size; 336 | u8 *tmp; 337 | 338 | printf("Encrypted Metadata\n"); 339 | 340 | if (sdk_type == 0x8000) { 341 | printf(" no encrypted metadata in fselfs.\n\n"); 342 | return; 343 | } 344 | 345 | if (decrypted < 0) { 346 | printf(" unable to decrypt metadata\n\n"); 347 | return; 348 | } 349 | 350 | meta_len = be32(self + meta_offset + 0x60 + 0x4); 351 | meta_n_hdr = be32(self + meta_offset + 0x60 + 0xc); 352 | meta_n_keys = be32(self + meta_offset + 0x60 + 0x10); 353 | 354 | printf(" Key: "); 355 | print_hash(self + meta_offset + 0x20, 0x10); 356 | printf("\n"); 357 | 358 | printf(" IV : "); 359 | print_hash(self + meta_offset + 0x40, 0x10); 360 | printf("\n"); 361 | 362 | printf(" Signature end %08x\n", meta_len); 363 | printf(" Sections %d\n", meta_n_hdr); 364 | printf(" Keys %d\n", meta_n_keys); 365 | printf("\n"); 366 | 367 | printf(" Sections\n"); 368 | printf(" Offset Length Key IV SHA1\n"); 369 | for (i = 0; i < meta_n_hdr; i++) { 370 | tmp = self + meta_offset + 0x80 + 0x30*i; 371 | offset = be64(tmp); 372 | size = be64(tmp + 8); 373 | printf(" %08x_%08x %08x_%08x %03d %03d %03d\n", 374 | (u32)(offset >> 32), (u32)offset, (u32)(size >> 32), (u32)size, 375 | be32(tmp + 0x24), be32(tmp + 0x28), be32(tmp + 0x1c)); 376 | } 377 | printf("\n"); 378 | 379 | printf(" Keys\n"); 380 | printf(" Idx Data\n"); 381 | tmp = self + meta_offset + 0x80 + 0x30*meta_n_hdr; 382 | for (i = 0; i < meta_n_keys; i++) { 383 | printf(" %03d ", i); 384 | print_hash(tmp + i*0x10, 0x10); 385 | printf("\n"); 386 | } 387 | printf("\n"); 388 | 389 | printf("\n"); 390 | 391 | } 392 | 393 | static void show_elf_header(void) 394 | { 395 | printf("ELF header\n"); 396 | 397 | printf(" type: %s\n", id2name(ehdr.e_type, t_elf_type, "unknown")); 398 | printf(" machine: %s\n", id2name(ehdr.e_machine, t_elf_machine, "unknown")); 399 | printf(" version: %d\n", ehdr.e_version); 400 | 401 | if (arch64) { 402 | printf(" phdr offset: %08x_%08x\n", 403 | (u32)(ehdr.e_phoff>>32), (u32)ehdr.e_phoff); 404 | printf(" shdr offset: %08x_%08x\n", 405 | (u32)(ehdr.e_phoff>>32), (u32)ehdr.e_shoff); 406 | printf(" entry: %08x_%08x\n", 407 | (u32)(ehdr.e_entry>>32), (u32)ehdr.e_entry); 408 | } else { 409 | printf(" phdr offset: %08x\n", 410 | (u32)ehdr.e_phoff); 411 | printf(" shdr offset: %08x\n", 412 | (u32)ehdr.e_shoff); 413 | printf(" entry: %08x\n", 414 | (u32)ehdr.e_entry); 415 | } 416 | 417 | printf(" flags: %08x\n", ehdr.e_flags); 418 | printf(" header size: %08x\n", ehdr.e_ehsize); 419 | printf(" program header size: %08x\n", 420 | ehdr.e_phentsize); 421 | printf(" program headers: %d\n", ehdr.e_phnum); 422 | printf(" section header size: %08x\n", 423 | ehdr.e_shentsize); 424 | printf(" section headers: %d\n", ehdr.e_shnum); 425 | printf(" section header string table index: %d\n", ehdr.e_shtrndx); 426 | 427 | printf("\n"); 428 | } 429 | 430 | static void get_flags(u32 flags, char *ptr) 431 | { 432 | memset(ptr, '-', 3); 433 | ptr[3] = 0; 434 | 435 | if (flags & 4) 436 | ptr[0] = 'r'; 437 | if (flags & 2) 438 | ptr[1] = 'w'; 439 | if (flags & 1) 440 | ptr[2] = 'x'; 441 | } 442 | 443 | static void show_phdr(unsigned int idx) 444 | { 445 | struct elf_phdr p; 446 | char ppc[4], spe[4], rsx[4]; 447 | 448 | elf_read_phdr(arch64, elf + phdr_offset + (ehdr.e_phentsize * idx), &p); 449 | 450 | get_flags(p.p_flags, ppc); 451 | get_flags(p.p_flags >> 20, spe); 452 | get_flags(p.p_flags >> 24, rsx); 453 | 454 | if (arch64) { 455 | printf(" %5s %08x_%08x %08x_%08x %08x_%08x\n" 456 | " %08x_%08x %08x_%08x" 457 | " %s %s %s %08x_%08x\n", 458 | id2name(p.p_type, t_phdr_type, "?????"), 459 | (u32)(p.p_off >> 32) , (u32)p.p_off, 460 | (u32)(p.p_vaddr >> 32) , (u32)p.p_vaddr, 461 | (u32)(p.p_paddr >> 32) , (u32)p.p_paddr, 462 | (u32)(p.p_memsz >> 32) , (u32)p.p_memsz, 463 | (u32)(p.p_filesz >> 32) , (u32)p.p_filesz, 464 | ppc, spe, rsx, 465 | (u32)(p.p_align >> 32) , (u32)p.p_align 466 | ); 467 | } else { 468 | printf(" %5s %08x %08x %08x " 469 | "%08x %08x %s %s %s %08x\n", 470 | id2name(p.p_type, t_phdr_type, "?????"), 471 | (u32)p.p_off, (u32)p.p_vaddr, 472 | (u32)p.p_paddr, (u32)p.p_memsz, (u32)p.p_filesz, 473 | ppc, spe, rsx, (u32)p.p_align); 474 | } 475 | } 476 | 477 | static void get_shdr_flags(u32 flags, char *ptr) 478 | { 479 | memset(ptr, ' ', 3); 480 | ptr[3] = 0; 481 | 482 | if (flags & 4) 483 | ptr[0] = 'w'; 484 | if (flags & 2) 485 | ptr[1] = 'a'; 486 | if (flags & 1) 487 | ptr[2] = 'e'; 488 | } 489 | 490 | static void show_shdr(unsigned int idx) 491 | { 492 | struct elf_shdr s; 493 | char flags[4]; 494 | 495 | elf_read_shdr(arch64, elf + shdr_offset + (ehdr.e_shentsize * idx), &s); 496 | get_shdr_flags(s.sh_flags, flags); 497 | 498 | if (arch64) { 499 | printf(" [%02d] %-15s %-9s %08x_%08x" 500 | " %02d %-3s %02d %03d %02d\n" 501 | " %08x_%08x %08x_%08x\n", 502 | idx, "", 503 | id2name(s.sh_type, t_shdr_type, "????"), 504 | (u32)(s.sh_addr >> 32), (u32)s.sh_addr, 505 | s.sh_entsize, flags, s.sh_link, s.sh_info, 506 | s.sh_addralign, 507 | (u32)(s.sh_offset >> 32), (u32)s.sh_offset, 508 | 0, (u32)s.sh_size 509 | ); 510 | } else { 511 | printf(" [%02d] %-15s %-9s %08x" 512 | " %08x %08x %02d %-3s %02d %02d %02d\n", 513 | idx, "", 514 | id2name(s.sh_type, t_shdr_type, "????"), 515 | (u32)s.sh_addr, (u32)s.sh_offset, 516 | s.sh_size, s.sh_entsize, 517 | flags, s.sh_link, s.sh_info, s.sh_addralign); 518 | 519 | } 520 | } 521 | 522 | static void show_phdrs(void) 523 | { 524 | unsigned int i; 525 | 526 | printf("Program headers\n"); 527 | 528 | if (ehdr.e_phnum == 0) { 529 | printf("No program headers in this file.\n"); 530 | } else { 531 | if (arch64) 532 | printf(" type offset vaddr " 533 | "paddr\n memsize filesize" 534 | " PPU SPE RSX align\n"); 535 | else 536 | printf(" type offset vaddr paddr " 537 | "memsize filesize PPU SPE RSX align\n"); 538 | for (i = 0; i < ehdr.e_phnum; i++) 539 | show_phdr(i); 540 | } 541 | 542 | printf("\n"); 543 | } 544 | 545 | static void show_shdrs(void) 546 | { 547 | unsigned int i; 548 | 549 | printf("Section headers\n"); 550 | 551 | if (ehdr.e_shnum == 0) { 552 | printf("No section headers in this file.\n"); 553 | } else { 554 | if (arch64) 555 | printf(" [Nr] Name Type Addr" 556 | " ES Flg Lk Inf Al\n" 557 | " Off Size\n"); 558 | else 559 | printf(" [Nr] Name Type Addr" 560 | " Off Size ES Flg Lk Inf Al\n"); 561 | for (i = 0; i < ehdr.e_shnum; i++) 562 | show_shdr(i); 563 | } 564 | 565 | printf("\n"); 566 | } 567 | 568 | int main(int argc, char *argv[]) 569 | { 570 | if (argc != 2) 571 | fail("usage: readself file.self"); 572 | 573 | self = mmap_file(argv[1]); 574 | 575 | parse_self(); 576 | decrypt_header(); 577 | 578 | show_self_header(); 579 | show_ctrl(); 580 | show_sinfo(); 581 | show_meta(); 582 | show_elf_header(); 583 | show_phdrs(); 584 | show_shdrs(); 585 | 586 | return 0; 587 | } 588 | -------------------------------------------------------------------------------- /tools.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Copyright 2007,2008,2010 Segher Boessenkool 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 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 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 | {0, NULL} 381 | }; 382 | 383 | static int key_build_path(char *ptr) 384 | { 385 | char *home = NULL; 386 | char *dir = NULL; 387 | 388 | memset(ptr, 0, 256); 389 | 390 | dir = getenv("SONY_KEYS"); 391 | if (dir != NULL) { 392 | strncpy(ptr, dir, 256); 393 | return 0; 394 | } 395 | 396 | home = getenv("HOME"); 397 | if (home == NULL) 398 | return -1; 399 | 400 | snprintf(ptr, 256, "%s/.ps3/", home); 401 | 402 | return 0; 403 | } 404 | 405 | static int key_read(const char *path, u32 len, u8 *dst) 406 | { 407 | FILE *fp = NULL; 408 | u32 read; 409 | int ret = -1; 410 | 411 | fp = fopen(path, "r"); 412 | if (fp == NULL) 413 | goto fail; 414 | 415 | read = fread(dst, len, 1, fp); 416 | 417 | if (read != 1) 418 | goto fail; 419 | 420 | ret = 0; 421 | 422 | fail: 423 | if (fp != NULL) 424 | fclose(fp); 425 | 426 | return ret; 427 | } 428 | 429 | struct keylist *keys_get(enum sce_key type) 430 | { 431 | const char *name = NULL; 432 | char base[256]; 433 | char path[256]; 434 | void *tmp = NULL; 435 | char *id; 436 | DIR *dp; 437 | struct dirent *dent; 438 | struct keylist *klist; 439 | u8 bfr[4]; 440 | 441 | klist = malloc(sizeof *klist); 442 | if (klist == NULL) 443 | goto fail; 444 | 445 | memset(klist, 0, sizeof *klist); 446 | 447 | name = id2name(type, t_key2file, NULL); 448 | if (name == NULL) 449 | goto fail; 450 | 451 | if (key_build_path(base) < 0) 452 | goto fail; 453 | 454 | dp = opendir(base); 455 | if (dp == NULL) 456 | goto fail; 457 | 458 | while ((dent = readdir(dp)) != NULL) { 459 | if (strncmp(dent->d_name, name, strlen(name)) == 0 && 460 | strstr(dent->d_name, "key") != NULL) { 461 | tmp = realloc(klist->keys, (klist->n + 1) * sizeof(struct key)); 462 | if (tmp == NULL) 463 | goto fail; 464 | 465 | id = strrchr(dent->d_name, '-'); 466 | if (id != NULL) 467 | id++; 468 | 469 | klist->keys = tmp; 470 | memset(&klist->keys[klist->n], 0, sizeof(struct key)); 471 | 472 | snprintf(path, sizeof path, "%s/%s-key-%s", base, name, id); 473 | key_read(path, 32, klist->keys[klist->n].key); 474 | 475 | snprintf(path, sizeof path, "%s/%s-iv-%s", base, name, id); 476 | key_read(path, 16, klist->keys[klist->n].iv); 477 | 478 | klist->keys[klist->n].pub_avail = -1; 479 | klist->keys[klist->n].priv_avail = -1; 480 | 481 | snprintf(path, sizeof path, "%s/%s-pub-%s", base, name, id); 482 | if (key_read(path, 40, klist->keys[klist->n].pub) == 0) { 483 | snprintf(path, sizeof path, "%s/%s-ctype-%s", base, name, id); 484 | key_read(path, 4, bfr); 485 | 486 | klist->keys[klist->n].pub_avail = 1; 487 | klist->keys[klist->n].ctype = be32(bfr); 488 | } 489 | 490 | snprintf(path, sizeof path, "%s/%s-priv-%s", base, name, id); 491 | if (key_read(path, 21, klist->keys[klist->n].priv) == 0) 492 | klist->keys[klist->n].priv_avail = 1; 493 | 494 | 495 | klist->n++; 496 | } 497 | } 498 | 499 | return klist; 500 | 501 | fail: 502 | if (klist != NULL) { 503 | if (klist->keys != NULL) 504 | free(klist->keys); 505 | free(klist); 506 | } 507 | klist = NULL; 508 | 509 | return NULL; 510 | } 511 | 512 | int key_get_simple(const char *name, u8 *bfr, u32 len) 513 | { 514 | char base[256]; 515 | char path[256]; 516 | 517 | if (key_build_path(base) < 0) 518 | return -1; 519 | 520 | snprintf(path, sizeof path, "%s/%s", base, name); 521 | if (key_read(path, len, bfr) < 0) 522 | return -1; 523 | 524 | return 0; 525 | } 526 | 527 | int key_get(enum sce_key type, const char *suffix, struct key *k) 528 | { 529 | const char *name; 530 | char base[256]; 531 | char path[256]; 532 | u8 tmp[4]; 533 | 534 | if (key_build_path(base) < 0) 535 | return -1; 536 | 537 | name = id2name(type, t_key2file, NULL); 538 | if (name == NULL) 539 | return -1; 540 | 541 | snprintf(path, sizeof path, "%s/%s-key-%s", base, name, suffix); 542 | if (key_read(path, 32, k->key) < 0) 543 | return -1; 544 | 545 | snprintf(path, sizeof path, "%s/%s-iv-%s", base, name, suffix); 546 | if (key_read(path, 16, k->iv) < 0) 547 | return -1; 548 | 549 | k->pub_avail = k->priv_avail = 1; 550 | 551 | snprintf(path, sizeof path, "%s/%s-ctype-%s", base, name, suffix); 552 | if (key_read(path, 4, tmp) < 0) { 553 | k->pub_avail = k->priv_avail = -1; 554 | return 0; 555 | } 556 | 557 | k->ctype = be32(tmp); 558 | 559 | snprintf(path, sizeof path, "%s/%s-pub-%s", base, name, suffix); 560 | if (key_read(path, 40, k->pub) < 0) 561 | k->pub_avail = -1; 562 | 563 | snprintf(path, sizeof path, "%s/%s-priv-%s", base, name, suffix); 564 | if (key_read(path, 21, k->priv) < 0) 565 | k->priv_avail = -1; 566 | 567 | return 0; 568 | } 569 | 570 | static void memcpy_inv(u8 *dst, u8 *src, u32 len) 571 | { 572 | u32 j; 573 | 574 | for (j = 0; j < len; j++) 575 | dst[j] = ~src[j]; 576 | } 577 | 578 | int ecdsa_get_params(u32 type, u8 *p, u8 *a, u8 *b, u8 *N, u8 *Gx, u8 *Gy) 579 | { 580 | static u8 tbl[64 * 121]; 581 | char path[256]; 582 | u32 offset; 583 | 584 | if (type >= 64) 585 | return -1; 586 | 587 | if (key_build_path(path) < 0) 588 | return -1; 589 | 590 | strncat(path, "/curves", sizeof path); 591 | 592 | if (key_read(path, sizeof tbl, tbl) < 0) 593 | return -1; 594 | 595 | offset = type * 121; 596 | 597 | memcpy_inv(p, tbl + offset + 0, 20); 598 | memcpy_inv(a, tbl + offset + 20, 20); 599 | memcpy_inv(b, tbl + offset + 40, 20); 600 | memcpy_inv(N, tbl + offset + 60, 21); 601 | memcpy_inv(Gx, tbl + offset + 81, 20); 602 | memcpy_inv(Gy, tbl + offset + 101, 20); 603 | 604 | return 0; 605 | } 606 | 607 | int sce_decrypt_header(u8 *ptr, struct keylist *klist) 608 | { 609 | u32 meta_offset; 610 | u32 meta_len; 611 | u64 header_len; 612 | u32 i, j; 613 | u8 tmp[0x40]; 614 | int success = 0; 615 | 616 | 617 | meta_offset = be32(ptr + 0x0c); 618 | header_len = be64(ptr + 0x10); 619 | 620 | for (i = 0; i < klist->n; i++) { 621 | aes256cbc(klist->keys[i].key, 622 | klist->keys[i].iv, 623 | ptr + meta_offset + 0x20, 624 | 0x40, 625 | tmp); 626 | 627 | success = 1; 628 | for (j = 0x10; j < (0x10 + 0x10); j++) 629 | if (tmp[j] != 0) 630 | success = 0; 631 | 632 | for (j = 0x30; j < (0x30 + 0x10); j++) 633 | if (tmp[j] != 0) 634 | success = 0; 635 | 636 | if (success == 1) { 637 | memcpy(ptr + meta_offset + 0x20, tmp, 0x40); 638 | break; 639 | } 640 | } 641 | 642 | if (success != 1) 643 | return -1; 644 | 645 | memcpy(tmp, ptr + meta_offset + 0x40, 0x10); 646 | aes128ctr(ptr + meta_offset + 0x20, 647 | tmp, 648 | ptr + meta_offset + 0x60, 649 | 0x20, 650 | ptr + meta_offset + 0x60); 651 | 652 | meta_len = header_len - meta_offset; 653 | 654 | aes128ctr(ptr + meta_offset + 0x20, 655 | tmp, 656 | ptr + meta_offset + 0x80, 657 | meta_len - 0x80, 658 | ptr + meta_offset + 0x80); 659 | 660 | return i; 661 | } 662 | 663 | int sce_encrypt_header(u8 *ptr, struct key *k) 664 | { 665 | u32 meta_offset; 666 | u32 meta_len; 667 | u64 header_len; 668 | u8 iv[16]; 669 | 670 | meta_offset = be32(ptr + 0x0c); 671 | header_len = be64(ptr + 0x10); 672 | meta_len = header_len - meta_offset; 673 | 674 | memcpy(iv, ptr + meta_offset + 0x40, 0x10); 675 | aes128ctr(ptr + meta_offset + 0x20, 676 | iv, 677 | ptr + meta_offset + 0x60, 678 | meta_len - 0x60, 679 | ptr + meta_offset + 0x60); 680 | 681 | aes256cbc_enc(k->key, k->iv, 682 | ptr + meta_offset + 0x20, 683 | 0x40, 684 | ptr + meta_offset + 0x20); 685 | 686 | 687 | return 0; 688 | } 689 | 690 | int sce_decrypt_data(u8 *ptr) 691 | { 692 | u64 meta_offset; 693 | u32 meta_len; 694 | u32 meta_n_hdr; 695 | u64 header_len; 696 | u32 i; 697 | 698 | u64 offset; 699 | u64 size; 700 | u32 keyid; 701 | u32 ivid; 702 | u8 *tmp; 703 | 704 | u8 iv[16]; 705 | 706 | meta_offset = be32(ptr + 0x0c); 707 | header_len = be64(ptr + 0x10); 708 | meta_len = header_len - meta_offset; 709 | meta_n_hdr = be32(ptr + meta_offset + 0x60 + 0xc); 710 | 711 | for (i = 0; i < meta_n_hdr; i++) { 712 | tmp = ptr + meta_offset + 0x80 + 0x30*i; 713 | offset = be64(tmp); 714 | size = be64(tmp + 8); 715 | keyid = be32(tmp + 0x24); 716 | ivid = be32(tmp + 0x28); 717 | 718 | if (keyid == 0xffffffff || ivid == 0xffffffff) 719 | continue; 720 | 721 | memcpy(iv, ptr + meta_offset + 0x80 + 0x30 * meta_n_hdr + ivid * 0x10, 0x10); 722 | aes128ctr(ptr + meta_offset + 0x80 + 0x30 * meta_n_hdr + keyid * 0x10, 723 | iv, 724 | ptr + offset, 725 | size, 726 | ptr + offset); 727 | } 728 | 729 | return 0; 730 | } 731 | 732 | int sce_encrypt_data(u8 *ptr) 733 | { 734 | return sce_decrypt_data(ptr); 735 | } 736 | -------------------------------------------------------------------------------- /aes.c: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * aes.c - integrated in QEMU by Fabrice Bellard from the OpenSSL project. 4 | */ 5 | /* 6 | * rijndael-alg-fst.c 7 | * 8 | * @version 3.0 (December 2000) 9 | * 10 | * Optimised ANSI C code for the Rijndael cipher (now AES) 11 | * 12 | * @author Vincent Rijmen 13 | * @author Antoon Bosselaers 14 | * @author Paulo Barreto 15 | * 16 | * This code is hereby placed in the public domain. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS 19 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 25 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 27 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 28 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | 32 | #include "tools.h" 33 | #include "aes.h" 34 | 35 | #define MAXKC (256/32) 36 | #define MAXKB (256/8) 37 | #define MAXNR 14 38 | 39 | /* This controls loop-unrolling in aes_core.c */ 40 | #undef FULL_UNROLL 41 | # define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3])) 42 | # define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); (ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); } 43 | 44 | /* 45 | Te0[x] = S [x].[02, 01, 01, 03]; 46 | Te1[x] = S [x].[03, 02, 01, 01]; 47 | Te2[x] = S [x].[01, 03, 02, 01]; 48 | Te3[x] = S [x].[01, 01, 03, 02]; 49 | Te4[x] = S [x].[01, 01, 01, 01]; 50 | 51 | Td0[x] = Si[x].[0e, 09, 0d, 0b]; 52 | Td1[x] = Si[x].[0b, 0e, 09, 0d]; 53 | Td2[x] = Si[x].[0d, 0b, 0e, 09]; 54 | Td3[x] = Si[x].[09, 0d, 0b, 0e]; 55 | Td4[x] = Si[x].[01, 01, 01, 01]; 56 | */ 57 | 58 | static const u32 Te0[256] = { 59 | 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU, 60 | 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U, 61 | 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU, 62 | 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU, 63 | 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U, 64 | 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU, 65 | 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU, 66 | 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU, 67 | 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU, 68 | 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU, 69 | 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U, 70 | 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU, 71 | 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU, 72 | 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U, 73 | 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU, 74 | 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU, 75 | 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU, 76 | 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU, 77 | 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU, 78 | 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U, 79 | 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU, 80 | 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU, 81 | 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU, 82 | 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU, 83 | 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U, 84 | 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U, 85 | 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U, 86 | 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U, 87 | 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU, 88 | 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U, 89 | 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U, 90 | 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU, 91 | 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU, 92 | 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U, 93 | 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U, 94 | 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U, 95 | 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU, 96 | 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U, 97 | 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU, 98 | 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U, 99 | 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU, 100 | 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U, 101 | 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U, 102 | 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU, 103 | 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U, 104 | 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U, 105 | 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U, 106 | 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U, 107 | 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U, 108 | 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U, 109 | 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U, 110 | 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U, 111 | 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU, 112 | 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U, 113 | 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U, 114 | 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U, 115 | 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U, 116 | 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U, 117 | 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U, 118 | 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU, 119 | 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U, 120 | 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U, 121 | 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U, 122 | 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU, 123 | }; 124 | static const u32 Te1[256] = { 125 | 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU, 126 | 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U, 127 | 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU, 128 | 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U, 129 | 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU, 130 | 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U, 131 | 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU, 132 | 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U, 133 | 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U, 134 | 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU, 135 | 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U, 136 | 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U, 137 | 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U, 138 | 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU, 139 | 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U, 140 | 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U, 141 | 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU, 142 | 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U, 143 | 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U, 144 | 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U, 145 | 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU, 146 | 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU, 147 | 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U, 148 | 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU, 149 | 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU, 150 | 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U, 151 | 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU, 152 | 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U, 153 | 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU, 154 | 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U, 155 | 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U, 156 | 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U, 157 | 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU, 158 | 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U, 159 | 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU, 160 | 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U, 161 | 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU, 162 | 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U, 163 | 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U, 164 | 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU, 165 | 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU, 166 | 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU, 167 | 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U, 168 | 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U, 169 | 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU, 170 | 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U, 171 | 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU, 172 | 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U, 173 | 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU, 174 | 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U, 175 | 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU, 176 | 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU, 177 | 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U, 178 | 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU, 179 | 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U, 180 | 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU, 181 | 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U, 182 | 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U, 183 | 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U, 184 | 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU, 185 | 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU, 186 | 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U, 187 | 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU, 188 | 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U, 189 | }; 190 | static const u32 Te2[256] = { 191 | 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU, 192 | 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U, 193 | 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU, 194 | 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U, 195 | 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU, 196 | 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U, 197 | 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU, 198 | 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U, 199 | 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U, 200 | 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU, 201 | 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U, 202 | 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U, 203 | 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U, 204 | 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU, 205 | 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U, 206 | 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U, 207 | 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU, 208 | 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U, 209 | 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U, 210 | 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U, 211 | 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU, 212 | 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU, 213 | 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U, 214 | 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU, 215 | 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU, 216 | 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U, 217 | 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU, 218 | 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U, 219 | 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU, 220 | 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U, 221 | 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U, 222 | 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U, 223 | 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU, 224 | 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U, 225 | 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU, 226 | 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U, 227 | 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU, 228 | 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U, 229 | 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U, 230 | 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU, 231 | 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU, 232 | 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU, 233 | 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U, 234 | 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U, 235 | 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU, 236 | 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U, 237 | 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU, 238 | 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U, 239 | 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU, 240 | 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U, 241 | 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU, 242 | 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU, 243 | 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U, 244 | 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU, 245 | 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U, 246 | 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU, 247 | 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U, 248 | 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U, 249 | 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U, 250 | 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU, 251 | 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU, 252 | 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U, 253 | 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU, 254 | 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U, 255 | }; 256 | static const u32 Te3[256] = { 257 | 258 | 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U, 259 | 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U, 260 | 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U, 261 | 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU, 262 | 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU, 263 | 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU, 264 | 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U, 265 | 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU, 266 | 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU, 267 | 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U, 268 | 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U, 269 | 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU, 270 | 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU, 271 | 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU, 272 | 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU, 273 | 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU, 274 | 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U, 275 | 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU, 276 | 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU, 277 | 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U, 278 | 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U, 279 | 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U, 280 | 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U, 281 | 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U, 282 | 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU, 283 | 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U, 284 | 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU, 285 | 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU, 286 | 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U, 287 | 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U, 288 | 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U, 289 | 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU, 290 | 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U, 291 | 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU, 292 | 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU, 293 | 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U, 294 | 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U, 295 | 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU, 296 | 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U, 297 | 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU, 298 | 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U, 299 | 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U, 300 | 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U, 301 | 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U, 302 | 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU, 303 | 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U, 304 | 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU, 305 | 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U, 306 | 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU, 307 | 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U, 308 | 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU, 309 | 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU, 310 | 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU, 311 | 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU, 312 | 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U, 313 | 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U, 314 | 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U, 315 | 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U, 316 | 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U, 317 | 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U, 318 | 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU, 319 | 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U, 320 | 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU, 321 | 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU, 322 | }; 323 | static const u32 Te4[256] = { 324 | 0x63636363U, 0x7c7c7c7cU, 0x77777777U, 0x7b7b7b7bU, 325 | 0xf2f2f2f2U, 0x6b6b6b6bU, 0x6f6f6f6fU, 0xc5c5c5c5U, 326 | 0x30303030U, 0x01010101U, 0x67676767U, 0x2b2b2b2bU, 327 | 0xfefefefeU, 0xd7d7d7d7U, 0xababababU, 0x76767676U, 328 | 0xcacacacaU, 0x82828282U, 0xc9c9c9c9U, 0x7d7d7d7dU, 329 | 0xfafafafaU, 0x59595959U, 0x47474747U, 0xf0f0f0f0U, 330 | 0xadadadadU, 0xd4d4d4d4U, 0xa2a2a2a2U, 0xafafafafU, 331 | 0x9c9c9c9cU, 0xa4a4a4a4U, 0x72727272U, 0xc0c0c0c0U, 332 | 0xb7b7b7b7U, 0xfdfdfdfdU, 0x93939393U, 0x26262626U, 333 | 0x36363636U, 0x3f3f3f3fU, 0xf7f7f7f7U, 0xccccccccU, 334 | 0x34343434U, 0xa5a5a5a5U, 0xe5e5e5e5U, 0xf1f1f1f1U, 335 | 0x71717171U, 0xd8d8d8d8U, 0x31313131U, 0x15151515U, 336 | 0x04040404U, 0xc7c7c7c7U, 0x23232323U, 0xc3c3c3c3U, 337 | 0x18181818U, 0x96969696U, 0x05050505U, 0x9a9a9a9aU, 338 | 0x07070707U, 0x12121212U, 0x80808080U, 0xe2e2e2e2U, 339 | 0xebebebebU, 0x27272727U, 0xb2b2b2b2U, 0x75757575U, 340 | 0x09090909U, 0x83838383U, 0x2c2c2c2cU, 0x1a1a1a1aU, 341 | 0x1b1b1b1bU, 0x6e6e6e6eU, 0x5a5a5a5aU, 0xa0a0a0a0U, 342 | 0x52525252U, 0x3b3b3b3bU, 0xd6d6d6d6U, 0xb3b3b3b3U, 343 | 0x29292929U, 0xe3e3e3e3U, 0x2f2f2f2fU, 0x84848484U, 344 | 0x53535353U, 0xd1d1d1d1U, 0x00000000U, 0xededededU, 345 | 0x20202020U, 0xfcfcfcfcU, 0xb1b1b1b1U, 0x5b5b5b5bU, 346 | 0x6a6a6a6aU, 0xcbcbcbcbU, 0xbebebebeU, 0x39393939U, 347 | 0x4a4a4a4aU, 0x4c4c4c4cU, 0x58585858U, 0xcfcfcfcfU, 348 | 0xd0d0d0d0U, 0xefefefefU, 0xaaaaaaaaU, 0xfbfbfbfbU, 349 | 0x43434343U, 0x4d4d4d4dU, 0x33333333U, 0x85858585U, 350 | 0x45454545U, 0xf9f9f9f9U, 0x02020202U, 0x7f7f7f7fU, 351 | 0x50505050U, 0x3c3c3c3cU, 0x9f9f9f9fU, 0xa8a8a8a8U, 352 | 0x51515151U, 0xa3a3a3a3U, 0x40404040U, 0x8f8f8f8fU, 353 | 0x92929292U, 0x9d9d9d9dU, 0x38383838U, 0xf5f5f5f5U, 354 | 0xbcbcbcbcU, 0xb6b6b6b6U, 0xdadadadaU, 0x21212121U, 355 | 0x10101010U, 0xffffffffU, 0xf3f3f3f3U, 0xd2d2d2d2U, 356 | 0xcdcdcdcdU, 0x0c0c0c0cU, 0x13131313U, 0xececececU, 357 | 0x5f5f5f5fU, 0x97979797U, 0x44444444U, 0x17171717U, 358 | 0xc4c4c4c4U, 0xa7a7a7a7U, 0x7e7e7e7eU, 0x3d3d3d3dU, 359 | 0x64646464U, 0x5d5d5d5dU, 0x19191919U, 0x73737373U, 360 | 0x60606060U, 0x81818181U, 0x4f4f4f4fU, 0xdcdcdcdcU, 361 | 0x22222222U, 0x2a2a2a2aU, 0x90909090U, 0x88888888U, 362 | 0x46464646U, 0xeeeeeeeeU, 0xb8b8b8b8U, 0x14141414U, 363 | 0xdedededeU, 0x5e5e5e5eU, 0x0b0b0b0bU, 0xdbdbdbdbU, 364 | 0xe0e0e0e0U, 0x32323232U, 0x3a3a3a3aU, 0x0a0a0a0aU, 365 | 0x49494949U, 0x06060606U, 0x24242424U, 0x5c5c5c5cU, 366 | 0xc2c2c2c2U, 0xd3d3d3d3U, 0xacacacacU, 0x62626262U, 367 | 0x91919191U, 0x95959595U, 0xe4e4e4e4U, 0x79797979U, 368 | 0xe7e7e7e7U, 0xc8c8c8c8U, 0x37373737U, 0x6d6d6d6dU, 369 | 0x8d8d8d8dU, 0xd5d5d5d5U, 0x4e4e4e4eU, 0xa9a9a9a9U, 370 | 0x6c6c6c6cU, 0x56565656U, 0xf4f4f4f4U, 0xeaeaeaeaU, 371 | 0x65656565U, 0x7a7a7a7aU, 0xaeaeaeaeU, 0x08080808U, 372 | 0xbabababaU, 0x78787878U, 0x25252525U, 0x2e2e2e2eU, 373 | 0x1c1c1c1cU, 0xa6a6a6a6U, 0xb4b4b4b4U, 0xc6c6c6c6U, 374 | 0xe8e8e8e8U, 0xddddddddU, 0x74747474U, 0x1f1f1f1fU, 375 | 0x4b4b4b4bU, 0xbdbdbdbdU, 0x8b8b8b8bU, 0x8a8a8a8aU, 376 | 0x70707070U, 0x3e3e3e3eU, 0xb5b5b5b5U, 0x66666666U, 377 | 0x48484848U, 0x03030303U, 0xf6f6f6f6U, 0x0e0e0e0eU, 378 | 0x61616161U, 0x35353535U, 0x57575757U, 0xb9b9b9b9U, 379 | 0x86868686U, 0xc1c1c1c1U, 0x1d1d1d1dU, 0x9e9e9e9eU, 380 | 0xe1e1e1e1U, 0xf8f8f8f8U, 0x98989898U, 0x11111111U, 381 | 0x69696969U, 0xd9d9d9d9U, 0x8e8e8e8eU, 0x94949494U, 382 | 0x9b9b9b9bU, 0x1e1e1e1eU, 0x87878787U, 0xe9e9e9e9U, 383 | 0xcecececeU, 0x55555555U, 0x28282828U, 0xdfdfdfdfU, 384 | 0x8c8c8c8cU, 0xa1a1a1a1U, 0x89898989U, 0x0d0d0d0dU, 385 | 0xbfbfbfbfU, 0xe6e6e6e6U, 0x42424242U, 0x68686868U, 386 | 0x41414141U, 0x99999999U, 0x2d2d2d2dU, 0x0f0f0f0fU, 387 | 0xb0b0b0b0U, 0x54545454U, 0xbbbbbbbbU, 0x16161616U, 388 | }; 389 | static const u32 Td0[256] = { 390 | 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U, 391 | 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U, 392 | 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U, 393 | 0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU, 394 | 0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U, 395 | 0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U, 396 | 0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU, 397 | 0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U, 398 | 0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU, 399 | 0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U, 400 | 0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U, 401 | 0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U, 402 | 0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U, 403 | 0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU, 404 | 0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U, 405 | 0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU, 406 | 0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U, 407 | 0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU, 408 | 0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U, 409 | 0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U, 410 | 0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U, 411 | 0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU, 412 | 0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U, 413 | 0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU, 414 | 0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U, 415 | 0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU, 416 | 0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U, 417 | 0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU, 418 | 0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU, 419 | 0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U, 420 | 0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU, 421 | 0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U, 422 | 0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU, 423 | 0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U, 424 | 0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U, 425 | 0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U, 426 | 0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU, 427 | 0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U, 428 | 0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U, 429 | 0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU, 430 | 0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U, 431 | 0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U, 432 | 0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U, 433 | 0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U, 434 | 0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U, 435 | 0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU, 436 | 0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U, 437 | 0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U, 438 | 0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U, 439 | 0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U, 440 | 0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U, 441 | 0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU, 442 | 0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU, 443 | 0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU, 444 | 0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU, 445 | 0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U, 446 | 0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U, 447 | 0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU, 448 | 0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU, 449 | 0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U, 450 | 0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU, 451 | 0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U, 452 | 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U, 453 | 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U, 454 | }; 455 | static const u32 Td1[256] = { 456 | 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU, 457 | 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U, 458 | 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU, 459 | 0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U, 460 | 0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U, 461 | 0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U, 462 | 0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U, 463 | 0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U, 464 | 0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U, 465 | 0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU, 466 | 0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU, 467 | 0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU, 468 | 0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U, 469 | 0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU, 470 | 0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U, 471 | 0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U, 472 | 0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U, 473 | 0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU, 474 | 0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU, 475 | 0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U, 476 | 0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU, 477 | 0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U, 478 | 0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU, 479 | 0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU, 480 | 0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U, 481 | 0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U, 482 | 0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U, 483 | 0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU, 484 | 0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U, 485 | 0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU, 486 | 0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U, 487 | 0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U, 488 | 0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U, 489 | 0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU, 490 | 0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U, 491 | 0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U, 492 | 0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U, 493 | 0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U, 494 | 0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U, 495 | 0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U, 496 | 0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU, 497 | 0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU, 498 | 0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U, 499 | 0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU, 500 | 0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U, 501 | 0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU, 502 | 0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU, 503 | 0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U, 504 | 0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU, 505 | 0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U, 506 | 0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U, 507 | 0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U, 508 | 0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U, 509 | 0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U, 510 | 0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U, 511 | 0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U, 512 | 0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU, 513 | 0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U, 514 | 0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U, 515 | 0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU, 516 | 0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U, 517 | 0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U, 518 | 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U, 519 | 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U, 520 | }; 521 | static const u32 Td2[256] = { 522 | 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U, 523 | 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U, 524 | 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U, 525 | 0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U, 526 | 0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU, 527 | 0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U, 528 | 0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U, 529 | 0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U, 530 | 0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U, 531 | 0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU, 532 | 0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U, 533 | 0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U, 534 | 0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU, 535 | 0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U, 536 | 0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U, 537 | 0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U, 538 | 0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U, 539 | 0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U, 540 | 0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U, 541 | 0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU, 542 | 543 | 0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U, 544 | 0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U, 545 | 0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U, 546 | 0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U, 547 | 0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U, 548 | 0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU, 549 | 0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU, 550 | 0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U, 551 | 0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU, 552 | 0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U, 553 | 0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU, 554 | 0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU, 555 | 0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU, 556 | 0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU, 557 | 0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U, 558 | 0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U, 559 | 0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U, 560 | 0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U, 561 | 0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U, 562 | 0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U, 563 | 0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U, 564 | 0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU, 565 | 0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU, 566 | 0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U, 567 | 0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U, 568 | 0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU, 569 | 0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU, 570 | 0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U, 571 | 0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U, 572 | 0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U, 573 | 0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U, 574 | 0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U, 575 | 0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U, 576 | 0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U, 577 | 0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU, 578 | 0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U, 579 | 0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U, 580 | 0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U, 581 | 0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U, 582 | 0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U, 583 | 0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U, 584 | 0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU, 585 | 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U, 586 | 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U, 587 | }; 588 | static const u32 Td3[256] = { 589 | 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU, 590 | 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU, 591 | 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U, 592 | 0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U, 593 | 0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU, 594 | 0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU, 595 | 0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U, 596 | 0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU, 597 | 0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U, 598 | 0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU, 599 | 0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U, 600 | 0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U, 601 | 0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U, 602 | 0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U, 603 | 0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U, 604 | 0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU, 605 | 0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU, 606 | 0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U, 607 | 0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U, 608 | 0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU, 609 | 0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU, 610 | 0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U, 611 | 0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U, 612 | 0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U, 613 | 0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U, 614 | 0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU, 615 | 0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U, 616 | 0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U, 617 | 0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU, 618 | 0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU, 619 | 0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U, 620 | 0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U, 621 | 0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U, 622 | 0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU, 623 | 0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U, 624 | 0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U, 625 | 0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U, 626 | 0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U, 627 | 0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U, 628 | 0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U, 629 | 0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U, 630 | 0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU, 631 | 0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U, 632 | 0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U, 633 | 0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU, 634 | 0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU, 635 | 0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U, 636 | 0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU, 637 | 0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U, 638 | 0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U, 639 | 0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U, 640 | 0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U, 641 | 0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U, 642 | 0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U, 643 | 0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU, 644 | 0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU, 645 | 0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU, 646 | 0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU, 647 | 0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U, 648 | 0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U, 649 | 0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U, 650 | 0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU, 651 | 0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U, 652 | 0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U, 653 | }; 654 | static const u32 Td4[256] = { 655 | 0x52525252U, 0x09090909U, 0x6a6a6a6aU, 0xd5d5d5d5U, 656 | 0x30303030U, 0x36363636U, 0xa5a5a5a5U, 0x38383838U, 657 | 0xbfbfbfbfU, 0x40404040U, 0xa3a3a3a3U, 0x9e9e9e9eU, 658 | 0x81818181U, 0xf3f3f3f3U, 0xd7d7d7d7U, 0xfbfbfbfbU, 659 | 0x7c7c7c7cU, 0xe3e3e3e3U, 0x39393939U, 0x82828282U, 660 | 0x9b9b9b9bU, 0x2f2f2f2fU, 0xffffffffU, 0x87878787U, 661 | 0x34343434U, 0x8e8e8e8eU, 0x43434343U, 0x44444444U, 662 | 0xc4c4c4c4U, 0xdedededeU, 0xe9e9e9e9U, 0xcbcbcbcbU, 663 | 0x54545454U, 0x7b7b7b7bU, 0x94949494U, 0x32323232U, 664 | 0xa6a6a6a6U, 0xc2c2c2c2U, 0x23232323U, 0x3d3d3d3dU, 665 | 0xeeeeeeeeU, 0x4c4c4c4cU, 0x95959595U, 0x0b0b0b0bU, 666 | 0x42424242U, 0xfafafafaU, 0xc3c3c3c3U, 0x4e4e4e4eU, 667 | 0x08080808U, 0x2e2e2e2eU, 0xa1a1a1a1U, 0x66666666U, 668 | 0x28282828U, 0xd9d9d9d9U, 0x24242424U, 0xb2b2b2b2U, 669 | 0x76767676U, 0x5b5b5b5bU, 0xa2a2a2a2U, 0x49494949U, 670 | 0x6d6d6d6dU, 0x8b8b8b8bU, 0xd1d1d1d1U, 0x25252525U, 671 | 0x72727272U, 0xf8f8f8f8U, 0xf6f6f6f6U, 0x64646464U, 672 | 0x86868686U, 0x68686868U, 0x98989898U, 0x16161616U, 673 | 0xd4d4d4d4U, 0xa4a4a4a4U, 0x5c5c5c5cU, 0xccccccccU, 674 | 0x5d5d5d5dU, 0x65656565U, 0xb6b6b6b6U, 0x92929292U, 675 | 0x6c6c6c6cU, 0x70707070U, 0x48484848U, 0x50505050U, 676 | 0xfdfdfdfdU, 0xededededU, 0xb9b9b9b9U, 0xdadadadaU, 677 | 0x5e5e5e5eU, 0x15151515U, 0x46464646U, 0x57575757U, 678 | 0xa7a7a7a7U, 0x8d8d8d8dU, 0x9d9d9d9dU, 0x84848484U, 679 | 0x90909090U, 0xd8d8d8d8U, 0xababababU, 0x00000000U, 680 | 0x8c8c8c8cU, 0xbcbcbcbcU, 0xd3d3d3d3U, 0x0a0a0a0aU, 681 | 0xf7f7f7f7U, 0xe4e4e4e4U, 0x58585858U, 0x05050505U, 682 | 0xb8b8b8b8U, 0xb3b3b3b3U, 0x45454545U, 0x06060606U, 683 | 0xd0d0d0d0U, 0x2c2c2c2cU, 0x1e1e1e1eU, 0x8f8f8f8fU, 684 | 0xcacacacaU, 0x3f3f3f3fU, 0x0f0f0f0fU, 0x02020202U, 685 | 0xc1c1c1c1U, 0xafafafafU, 0xbdbdbdbdU, 0x03030303U, 686 | 0x01010101U, 0x13131313U, 0x8a8a8a8aU, 0x6b6b6b6bU, 687 | 0x3a3a3a3aU, 0x91919191U, 0x11111111U, 0x41414141U, 688 | 0x4f4f4f4fU, 0x67676767U, 0xdcdcdcdcU, 0xeaeaeaeaU, 689 | 0x97979797U, 0xf2f2f2f2U, 0xcfcfcfcfU, 0xcecececeU, 690 | 0xf0f0f0f0U, 0xb4b4b4b4U, 0xe6e6e6e6U, 0x73737373U, 691 | 0x96969696U, 0xacacacacU, 0x74747474U, 0x22222222U, 692 | 0xe7e7e7e7U, 0xadadadadU, 0x35353535U, 0x85858585U, 693 | 0xe2e2e2e2U, 0xf9f9f9f9U, 0x37373737U, 0xe8e8e8e8U, 694 | 0x1c1c1c1cU, 0x75757575U, 0xdfdfdfdfU, 0x6e6e6e6eU, 695 | 0x47474747U, 0xf1f1f1f1U, 0x1a1a1a1aU, 0x71717171U, 696 | 0x1d1d1d1dU, 0x29292929U, 0xc5c5c5c5U, 0x89898989U, 697 | 0x6f6f6f6fU, 0xb7b7b7b7U, 0x62626262U, 0x0e0e0e0eU, 698 | 0xaaaaaaaaU, 0x18181818U, 0xbebebebeU, 0x1b1b1b1bU, 699 | 0xfcfcfcfcU, 0x56565656U, 0x3e3e3e3eU, 0x4b4b4b4bU, 700 | 0xc6c6c6c6U, 0xd2d2d2d2U, 0x79797979U, 0x20202020U, 701 | 0x9a9a9a9aU, 0xdbdbdbdbU, 0xc0c0c0c0U, 0xfefefefeU, 702 | 0x78787878U, 0xcdcdcdcdU, 0x5a5a5a5aU, 0xf4f4f4f4U, 703 | 0x1f1f1f1fU, 0xddddddddU, 0xa8a8a8a8U, 0x33333333U, 704 | 0x88888888U, 0x07070707U, 0xc7c7c7c7U, 0x31313131U, 705 | 0xb1b1b1b1U, 0x12121212U, 0x10101010U, 0x59595959U, 706 | 0x27272727U, 0x80808080U, 0xececececU, 0x5f5f5f5fU, 707 | 0x60606060U, 0x51515151U, 0x7f7f7f7fU, 0xa9a9a9a9U, 708 | 0x19191919U, 0xb5b5b5b5U, 0x4a4a4a4aU, 0x0d0d0d0dU, 709 | 0x2d2d2d2dU, 0xe5e5e5e5U, 0x7a7a7a7aU, 0x9f9f9f9fU, 710 | 0x93939393U, 0xc9c9c9c9U, 0x9c9c9c9cU, 0xefefefefU, 711 | 0xa0a0a0a0U, 0xe0e0e0e0U, 0x3b3b3b3bU, 0x4d4d4d4dU, 712 | 0xaeaeaeaeU, 0x2a2a2a2aU, 0xf5f5f5f5U, 0xb0b0b0b0U, 713 | 0xc8c8c8c8U, 0xebebebebU, 0xbbbbbbbbU, 0x3c3c3c3cU, 714 | 0x83838383U, 0x53535353U, 0x99999999U, 0x61616161U, 715 | 0x17171717U, 0x2b2b2b2bU, 0x04040404U, 0x7e7e7e7eU, 716 | 0xbabababaU, 0x77777777U, 0xd6d6d6d6U, 0x26262626U, 717 | 0xe1e1e1e1U, 0x69696969U, 0x14141414U, 0x63636363U, 718 | 0x55555555U, 0x21212121U, 0x0c0c0c0cU, 0x7d7d7d7dU, 719 | }; 720 | static const u32 rcon[] = { 721 | 0x01000000, 0x02000000, 0x04000000, 0x08000000, 722 | 0x10000000, 0x20000000, 0x40000000, 0x80000000, 723 | 0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */ 724 | }; 725 | 726 | /** 727 | * Expand the cipher key into the encryption key schedule. 728 | */ 729 | int AES_set_encrypt_key(const unsigned char *userKey, const int bits, 730 | AES_KEY *key) { 731 | 732 | u32 *rk; 733 | int i = 0; 734 | u32 temp; 735 | 736 | if (!userKey || !key) 737 | return -1; 738 | if (bits != 128 && bits != 192 && bits != 256) 739 | return -2; 740 | 741 | rk = key->rd_key; 742 | 743 | if (bits==128) 744 | key->rounds = 10; 745 | else if (bits==192) 746 | key->rounds = 12; 747 | else 748 | key->rounds = 14; 749 | 750 | rk[0] = GETU32(userKey ); 751 | rk[1] = GETU32(userKey + 4); 752 | rk[2] = GETU32(userKey + 8); 753 | rk[3] = GETU32(userKey + 12); 754 | if (bits == 128) { 755 | while (1) { 756 | temp = rk[3]; 757 | rk[4] = rk[0] ^ 758 | (Te4[(temp >> 16) & 0xff] & 0xff000000) ^ 759 | (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ 760 | (Te4[(temp ) & 0xff] & 0x0000ff00) ^ 761 | (Te4[(temp >> 24) ] & 0x000000ff) ^ 762 | rcon[i]; 763 | rk[5] = rk[1] ^ rk[4]; 764 | rk[6] = rk[2] ^ rk[5]; 765 | rk[7] = rk[3] ^ rk[6]; 766 | if (++i == 10) { 767 | return 0; 768 | } 769 | rk += 4; 770 | } 771 | } 772 | rk[4] = GETU32(userKey + 16); 773 | rk[5] = GETU32(userKey + 20); 774 | if (bits == 192) { 775 | while (1) { 776 | temp = rk[ 5]; 777 | rk[ 6] = rk[ 0] ^ 778 | (Te4[(temp >> 16) & 0xff] & 0xff000000) ^ 779 | (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ 780 | (Te4[(temp ) & 0xff] & 0x0000ff00) ^ 781 | (Te4[(temp >> 24) ] & 0x000000ff) ^ 782 | rcon[i]; 783 | rk[ 7] = rk[ 1] ^ rk[ 6]; 784 | rk[ 8] = rk[ 2] ^ rk[ 7]; 785 | rk[ 9] = rk[ 3] ^ rk[ 8]; 786 | if (++i == 8) { 787 | return 0; 788 | } 789 | rk[10] = rk[ 4] ^ rk[ 9]; 790 | rk[11] = rk[ 5] ^ rk[10]; 791 | rk += 6; 792 | } 793 | } 794 | rk[6] = GETU32(userKey + 24); 795 | rk[7] = GETU32(userKey + 28); 796 | if (bits == 256) { 797 | while (1) { 798 | temp = rk[ 7]; 799 | rk[ 8] = rk[ 0] ^ 800 | (Te4[(temp >> 16) & 0xff] & 0xff000000) ^ 801 | (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ 802 | (Te4[(temp ) & 0xff] & 0x0000ff00) ^ 803 | (Te4[(temp >> 24) ] & 0x000000ff) ^ 804 | rcon[i]; 805 | rk[ 9] = rk[ 1] ^ rk[ 8]; 806 | rk[10] = rk[ 2] ^ rk[ 9]; 807 | rk[11] = rk[ 3] ^ rk[10]; 808 | if (++i == 7) { 809 | return 0; 810 | } 811 | temp = rk[11]; 812 | rk[12] = rk[ 4] ^ 813 | (Te4[(temp >> 24) ] & 0xff000000) ^ 814 | (Te4[(temp >> 16) & 0xff] & 0x00ff0000) ^ 815 | (Te4[(temp >> 8) & 0xff] & 0x0000ff00) ^ 816 | (Te4[(temp ) & 0xff] & 0x000000ff); 817 | rk[13] = rk[ 5] ^ rk[12]; 818 | rk[14] = rk[ 6] ^ rk[13]; 819 | rk[15] = rk[ 7] ^ rk[14]; 820 | 821 | rk += 8; 822 | } 823 | } 824 | return 0; 825 | } 826 | 827 | /** 828 | * Expand the cipher key into the decryption key schedule. 829 | */ 830 | int AES_set_decrypt_key(const unsigned char *userKey, const int bits, 831 | AES_KEY *key) { 832 | 833 | u32 *rk; 834 | int i, j, status; 835 | u32 temp; 836 | 837 | /* first, start with an encryption schedule */ 838 | status = AES_set_encrypt_key(userKey, bits, key); 839 | if (status < 0) 840 | return status; 841 | 842 | rk = key->rd_key; 843 | 844 | /* invert the order of the round keys: */ 845 | for (i = 0, j = 4*(key->rounds); i < j; i += 4, j -= 4) { 846 | temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp; 847 | temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp; 848 | temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp; 849 | temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp; 850 | } 851 | /* apply the inverse MixColumn transform to all round keys but the first and the last: */ 852 | for (i = 1; i < (key->rounds); i++) { 853 | rk += 4; 854 | rk[0] = 855 | Td0[Te4[(rk[0] >> 24) ] & 0xff] ^ 856 | Td1[Te4[(rk[0] >> 16) & 0xff] & 0xff] ^ 857 | Td2[Te4[(rk[0] >> 8) & 0xff] & 0xff] ^ 858 | Td3[Te4[(rk[0] ) & 0xff] & 0xff]; 859 | rk[1] = 860 | Td0[Te4[(rk[1] >> 24) ] & 0xff] ^ 861 | Td1[Te4[(rk[1] >> 16) & 0xff] & 0xff] ^ 862 | Td2[Te4[(rk[1] >> 8) & 0xff] & 0xff] ^ 863 | Td3[Te4[(rk[1] ) & 0xff] & 0xff]; 864 | rk[2] = 865 | Td0[Te4[(rk[2] >> 24) ] & 0xff] ^ 866 | Td1[Te4[(rk[2] >> 16) & 0xff] & 0xff] ^ 867 | Td2[Te4[(rk[2] >> 8) & 0xff] & 0xff] ^ 868 | Td3[Te4[(rk[2] ) & 0xff] & 0xff]; 869 | rk[3] = 870 | Td0[Te4[(rk[3] >> 24) ] & 0xff] ^ 871 | Td1[Te4[(rk[3] >> 16) & 0xff] & 0xff] ^ 872 | Td2[Te4[(rk[3] >> 8) & 0xff] & 0xff] ^ 873 | Td3[Te4[(rk[3] ) & 0xff] & 0xff]; 874 | } 875 | return 0; 876 | } 877 | 878 | #ifndef AES_ASM 879 | /* 880 | * Encrypt a single block 881 | * in and out can overlap 882 | */ 883 | void AES_encrypt(const unsigned char *in, unsigned char *out, 884 | const AES_KEY *key) { 885 | 886 | const u32 *rk; 887 | u32 s0, s1, s2, s3, t0, t1, t2, t3; 888 | #ifndef FULL_UNROLL 889 | int r; 890 | #endif /* ?FULL_UNROLL */ 891 | 892 | // assert(in && out && key); 893 | rk = key->rd_key; 894 | 895 | /* 896 | * map byte array block to cipher state 897 | * and add initial round key: 898 | */ 899 | s0 = GETU32(in ) ^ rk[0]; 900 | s1 = GETU32(in + 4) ^ rk[1]; 901 | s2 = GETU32(in + 8) ^ rk[2]; 902 | s3 = GETU32(in + 12) ^ rk[3]; 903 | #ifdef FULL_UNROLL 904 | /* round 1: */ 905 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[ 4]; 906 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[ 5]; 907 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[ 6]; 908 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[ 7]; 909 | /* round 2: */ 910 | s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[ 8]; 911 | s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[ 9]; 912 | s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[10]; 913 | s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[11]; 914 | /* round 3: */ 915 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[12]; 916 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[13]; 917 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[14]; 918 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[15]; 919 | /* round 4: */ 920 | s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[16]; 921 | s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[17]; 922 | s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[18]; 923 | s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[19]; 924 | /* round 5: */ 925 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[20]; 926 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[21]; 927 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[22]; 928 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[23]; 929 | /* round 6: */ 930 | s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[24]; 931 | s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[25]; 932 | s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[26]; 933 | s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[27]; 934 | /* round 7: */ 935 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[28]; 936 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[29]; 937 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[30]; 938 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[31]; 939 | /* round 8: */ 940 | s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[32]; 941 | s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[33]; 942 | s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[34]; 943 | s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[35]; 944 | /* round 9: */ 945 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[36]; 946 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[37]; 947 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[38]; 948 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[39]; 949 | if (key->rounds > 10) { 950 | /* round 10: */ 951 | s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[40]; 952 | s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[41]; 953 | s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[42]; 954 | s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[43]; 955 | /* round 11: */ 956 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[44]; 957 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[45]; 958 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[46]; 959 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[47]; 960 | if (key->rounds > 12) { 961 | /* round 12: */ 962 | s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[48]; 963 | s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[49]; 964 | s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[50]; 965 | s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[51]; 966 | /* round 13: */ 967 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[52]; 968 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[53]; 969 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[54]; 970 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[55]; 971 | } 972 | } 973 | rk += key->rounds << 2; 974 | #else /* !FULL_UNROLL */ 975 | /* 976 | * Nr - 1 full rounds: 977 | */ 978 | r = key->rounds >> 1; 979 | for (;;) { 980 | t0 = 981 | Te0[(s0 >> 24) ] ^ 982 | Te1[(s1 >> 16) & 0xff] ^ 983 | Te2[(s2 >> 8) & 0xff] ^ 984 | Te3[(s3 ) & 0xff] ^ 985 | rk[4]; 986 | t1 = 987 | Te0[(s1 >> 24) ] ^ 988 | Te1[(s2 >> 16) & 0xff] ^ 989 | Te2[(s3 >> 8) & 0xff] ^ 990 | Te3[(s0 ) & 0xff] ^ 991 | rk[5]; 992 | t2 = 993 | Te0[(s2 >> 24) ] ^ 994 | Te1[(s3 >> 16) & 0xff] ^ 995 | Te2[(s0 >> 8) & 0xff] ^ 996 | Te3[(s1 ) & 0xff] ^ 997 | rk[6]; 998 | t3 = 999 | Te0[(s3 >> 24) ] ^ 1000 | Te1[(s0 >> 16) & 0xff] ^ 1001 | Te2[(s1 >> 8) & 0xff] ^ 1002 | Te3[(s2 ) & 0xff] ^ 1003 | rk[7]; 1004 | 1005 | rk += 8; 1006 | if (--r == 0) { 1007 | break; 1008 | } 1009 | 1010 | s0 = 1011 | Te0[(t0 >> 24) ] ^ 1012 | Te1[(t1 >> 16) & 0xff] ^ 1013 | Te2[(t2 >> 8) & 0xff] ^ 1014 | Te3[(t3 ) & 0xff] ^ 1015 | rk[0]; 1016 | s1 = 1017 | Te0[(t1 >> 24) ] ^ 1018 | Te1[(t2 >> 16) & 0xff] ^ 1019 | Te2[(t3 >> 8) & 0xff] ^ 1020 | Te3[(t0 ) & 0xff] ^ 1021 | rk[1]; 1022 | s2 = 1023 | Te0[(t2 >> 24) ] ^ 1024 | Te1[(t3 >> 16) & 0xff] ^ 1025 | Te2[(t0 >> 8) & 0xff] ^ 1026 | Te3[(t1 ) & 0xff] ^ 1027 | rk[2]; 1028 | s3 = 1029 | Te0[(t3 >> 24) ] ^ 1030 | Te1[(t0 >> 16) & 0xff] ^ 1031 | Te2[(t1 >> 8) & 0xff] ^ 1032 | Te3[(t2 ) & 0xff] ^ 1033 | rk[3]; 1034 | } 1035 | #endif /* ?FULL_UNROLL */ 1036 | /* 1037 | * apply last round and 1038 | * map cipher state to byte array block: 1039 | */ 1040 | s0 = 1041 | (Te4[(t0 >> 24) ] & 0xff000000) ^ 1042 | (Te4[(t1 >> 16) & 0xff] & 0x00ff0000) ^ 1043 | (Te4[(t2 >> 8) & 0xff] & 0x0000ff00) ^ 1044 | (Te4[(t3 ) & 0xff] & 0x000000ff) ^ 1045 | rk[0]; 1046 | PUTU32(out , s0); 1047 | s1 = 1048 | (Te4[(t1 >> 24) ] & 0xff000000) ^ 1049 | (Te4[(t2 >> 16) & 0xff] & 0x00ff0000) ^ 1050 | (Te4[(t3 >> 8) & 0xff] & 0x0000ff00) ^ 1051 | (Te4[(t0 ) & 0xff] & 0x000000ff) ^ 1052 | rk[1]; 1053 | PUTU32(out + 4, s1); 1054 | s2 = 1055 | (Te4[(t2 >> 24) ] & 0xff000000) ^ 1056 | (Te4[(t3 >> 16) & 0xff] & 0x00ff0000) ^ 1057 | (Te4[(t0 >> 8) & 0xff] & 0x0000ff00) ^ 1058 | (Te4[(t1 ) & 0xff] & 0x000000ff) ^ 1059 | rk[2]; 1060 | PUTU32(out + 8, s2); 1061 | s3 = 1062 | (Te4[(t3 >> 24) ] & 0xff000000) ^ 1063 | (Te4[(t0 >> 16) & 0xff] & 0x00ff0000) ^ 1064 | (Te4[(t1 >> 8) & 0xff] & 0x0000ff00) ^ 1065 | (Te4[(t2 ) & 0xff] & 0x000000ff) ^ 1066 | rk[3]; 1067 | PUTU32(out + 12, s3); 1068 | } 1069 | 1070 | /* 1071 | * Decrypt a single block 1072 | * in and out can overlap 1073 | */ 1074 | void AES_decrypt(const unsigned char *in, unsigned char *out, 1075 | const AES_KEY *key) { 1076 | 1077 | const u32 *rk; 1078 | u32 s0, s1, s2, s3, t0, t1, t2, t3; 1079 | #ifndef FULL_UNROLL 1080 | int r; 1081 | #endif /* ?FULL_UNROLL */ 1082 | 1083 | // assert(in && out && key); 1084 | rk = key->rd_key; 1085 | 1086 | /* 1087 | * map byte array block to cipher state 1088 | * and add initial round key: 1089 | */ 1090 | s0 = GETU32(in ) ^ rk[0]; 1091 | s1 = GETU32(in + 4) ^ rk[1]; 1092 | s2 = GETU32(in + 8) ^ rk[2]; 1093 | s3 = GETU32(in + 12) ^ rk[3]; 1094 | #ifdef FULL_UNROLL 1095 | /* round 1: */ 1096 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[ 4]; 1097 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[ 5]; 1098 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[ 6]; 1099 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[ 7]; 1100 | /* round 2: */ 1101 | s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[ 8]; 1102 | s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[ 9]; 1103 | s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[10]; 1104 | s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[11]; 1105 | /* round 3: */ 1106 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[12]; 1107 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[13]; 1108 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[14]; 1109 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[15]; 1110 | /* round 4: */ 1111 | s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[16]; 1112 | s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[17]; 1113 | s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[18]; 1114 | s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[19]; 1115 | /* round 5: */ 1116 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[20]; 1117 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[21]; 1118 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[22]; 1119 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[23]; 1120 | /* round 6: */ 1121 | s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[24]; 1122 | s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[25]; 1123 | s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[26]; 1124 | s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[27]; 1125 | /* round 7: */ 1126 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[28]; 1127 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[29]; 1128 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[30]; 1129 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[31]; 1130 | /* round 8: */ 1131 | s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[32]; 1132 | s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[33]; 1133 | s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[34]; 1134 | s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[35]; 1135 | /* round 9: */ 1136 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[36]; 1137 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[37]; 1138 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[38]; 1139 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[39]; 1140 | if (key->rounds > 10) { 1141 | /* round 10: */ 1142 | s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[40]; 1143 | s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[41]; 1144 | s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[42]; 1145 | s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[43]; 1146 | /* round 11: */ 1147 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[44]; 1148 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[45]; 1149 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[46]; 1150 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[47]; 1151 | if (key->rounds > 12) { 1152 | /* round 12: */ 1153 | s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[48]; 1154 | s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[49]; 1155 | s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[50]; 1156 | s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[51]; 1157 | /* round 13: */ 1158 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[52]; 1159 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[53]; 1160 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[54]; 1161 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[55]; 1162 | } 1163 | } 1164 | rk += key->rounds << 2; 1165 | #else /* !FULL_UNROLL */ 1166 | /* 1167 | * Nr - 1 full rounds: 1168 | */ 1169 | r = key->rounds >> 1; 1170 | for (;;) { 1171 | t0 = 1172 | Td0[(s0 >> 24) ] ^ 1173 | Td1[(s3 >> 16) & 0xff] ^ 1174 | Td2[(s2 >> 8) & 0xff] ^ 1175 | Td3[(s1 ) & 0xff] ^ 1176 | rk[4]; 1177 | t1 = 1178 | Td0[(s1 >> 24) ] ^ 1179 | Td1[(s0 >> 16) & 0xff] ^ 1180 | Td2[(s3 >> 8) & 0xff] ^ 1181 | Td3[(s2 ) & 0xff] ^ 1182 | rk[5]; 1183 | t2 = 1184 | Td0[(s2 >> 24) ] ^ 1185 | Td1[(s1 >> 16) & 0xff] ^ 1186 | Td2[(s0 >> 8) & 0xff] ^ 1187 | Td3[(s3 ) & 0xff] ^ 1188 | rk[6]; 1189 | t3 = 1190 | Td0[(s3 >> 24) ] ^ 1191 | Td1[(s2 >> 16) & 0xff] ^ 1192 | Td2[(s1 >> 8) & 0xff] ^ 1193 | Td3[(s0 ) & 0xff] ^ 1194 | rk[7]; 1195 | 1196 | rk += 8; 1197 | if (--r == 0) { 1198 | break; 1199 | } 1200 | 1201 | s0 = 1202 | Td0[(t0 >> 24) ] ^ 1203 | Td1[(t3 >> 16) & 0xff] ^ 1204 | Td2[(t2 >> 8) & 0xff] ^ 1205 | Td3[(t1 ) & 0xff] ^ 1206 | rk[0]; 1207 | s1 = 1208 | Td0[(t1 >> 24) ] ^ 1209 | Td1[(t0 >> 16) & 0xff] ^ 1210 | Td2[(t3 >> 8) & 0xff] ^ 1211 | Td3[(t2 ) & 0xff] ^ 1212 | rk[1]; 1213 | s2 = 1214 | Td0[(t2 >> 24) ] ^ 1215 | Td1[(t1 >> 16) & 0xff] ^ 1216 | Td2[(t0 >> 8) & 0xff] ^ 1217 | Td3[(t3 ) & 0xff] ^ 1218 | rk[2]; 1219 | s3 = 1220 | Td0[(t3 >> 24) ] ^ 1221 | Td1[(t2 >> 16) & 0xff] ^ 1222 | Td2[(t1 >> 8) & 0xff] ^ 1223 | Td3[(t0 ) & 0xff] ^ 1224 | rk[3]; 1225 | } 1226 | #endif /* ?FULL_UNROLL */ 1227 | /* 1228 | * apply last round and 1229 | * map cipher state to byte array block: 1230 | */ 1231 | s0 = 1232 | (Td4[(t0 >> 24) ] & 0xff000000) ^ 1233 | (Td4[(t3 >> 16) & 0xff] & 0x00ff0000) ^ 1234 | (Td4[(t2 >> 8) & 0xff] & 0x0000ff00) ^ 1235 | (Td4[(t1 ) & 0xff] & 0x000000ff) ^ 1236 | rk[0]; 1237 | PUTU32(out , s0); 1238 | s1 = 1239 | (Td4[(t1 >> 24) ] & 0xff000000) ^ 1240 | (Td4[(t0 >> 16) & 0xff] & 0x00ff0000) ^ 1241 | (Td4[(t3 >> 8) & 0xff] & 0x0000ff00) ^ 1242 | (Td4[(t2 ) & 0xff] & 0x000000ff) ^ 1243 | rk[1]; 1244 | PUTU32(out + 4, s1); 1245 | s2 = 1246 | (Td4[(t2 >> 24) ] & 0xff000000) ^ 1247 | (Td4[(t1 >> 16) & 0xff] & 0x00ff0000) ^ 1248 | (Td4[(t0 >> 8) & 0xff] & 0x0000ff00) ^ 1249 | (Td4[(t3 ) & 0xff] & 0x000000ff) ^ 1250 | rk[2]; 1251 | PUTU32(out + 8, s2); 1252 | s3 = 1253 | (Td4[(t3 >> 24) ] & 0xff000000) ^ 1254 | (Td4[(t2 >> 16) & 0xff] & 0x00ff0000) ^ 1255 | (Td4[(t1 >> 8) & 0xff] & 0x0000ff00) ^ 1256 | (Td4[(t0 ) & 0xff] & 0x000000ff) ^ 1257 | rk[3]; 1258 | PUTU32(out + 12, s3); 1259 | } 1260 | 1261 | #endif /* AES_ASM */ 1262 | 1263 | #if 0 1264 | void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, 1265 | const unsigned long length, const AES_KEY *key, 1266 | unsigned char *ivec, const int enc) 1267 | { 1268 | 1269 | unsigned long n; 1270 | unsigned long len = length; 1271 | unsigned char tmp[AES_BLOCK_SIZE]; 1272 | 1273 | assert(in && out && key && ivec); 1274 | 1275 | if (enc) { 1276 | while (len >= AES_BLOCK_SIZE) { 1277 | for(n=0; n < AES_BLOCK_SIZE; ++n) 1278 | tmp[n] = in[n] ^ ivec[n]; 1279 | AES_encrypt(tmp, out, key); 1280 | memcpy(ivec, out, AES_BLOCK_SIZE); 1281 | len -= AES_BLOCK_SIZE; 1282 | in += AES_BLOCK_SIZE; 1283 | out += AES_BLOCK_SIZE; 1284 | } 1285 | if (len) { 1286 | for(n=0; n < len; ++n) 1287 | tmp[n] = in[n] ^ ivec[n]; 1288 | for(n=len; n < AES_BLOCK_SIZE; ++n) 1289 | tmp[n] = ivec[n]; 1290 | AES_encrypt(tmp, tmp, key); 1291 | memcpy(out, tmp, AES_BLOCK_SIZE); 1292 | memcpy(ivec, tmp, AES_BLOCK_SIZE); 1293 | } 1294 | } else { 1295 | while (len >= AES_BLOCK_SIZE) { 1296 | memcpy(tmp, in, AES_BLOCK_SIZE); 1297 | AES_decrypt(in, out, key); 1298 | for(n=0; n < AES_BLOCK_SIZE; ++n) 1299 | out[n] ^= ivec[n]; 1300 | memcpy(ivec, tmp, AES_BLOCK_SIZE); 1301 | len -= AES_BLOCK_SIZE; 1302 | in += AES_BLOCK_SIZE; 1303 | out += AES_BLOCK_SIZE; 1304 | } 1305 | if (len) { 1306 | memcpy(tmp, in, AES_BLOCK_SIZE); 1307 | AES_decrypt(tmp, tmp, key); 1308 | for(n=0; n < len; ++n) 1309 | out[n] = tmp[n] ^ ivec[n]; 1310 | memcpy(ivec, tmp, AES_BLOCK_SIZE); 1311 | } 1312 | } 1313 | } 1314 | #endif 1315 | --------------------------------------------------------------------------------