├── wudecrypt.bkl ├── TODO ├── .gitignore ├── .travis.yml ├── config.h ├── aes.h ├── functions.h ├── NOTICE ├── struct.h ├── README.md ├── sha1.h ├── sha1.c ├── utarray.h ├── main.c ├── functions.c ├── aes.c └── LICENSE /wudecrypt.bkl: -------------------------------------------------------------------------------- 1 | toolsets = gnu; 2 | program wudecrypt { 3 | sources { 4 | main.c 5 | functions.c 6 | aes.c 7 | sha1.c 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | Entries are in the order of importance (most important on top): 2 | 3 | * Put comments in the code, it's currently hard to understand and a mess for everybody else than me. 4 | * Optimize the code as much as possible. It's currently very slow. 5 | 6 | Possible features that will most probably never be implemented: 7 | 8 | * Multi-threading during file decryption (at least more than one partition at once could give a great speedup). 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | 31 | # Debug files 32 | *.dSYM/ 33 | 34 | # Custom debug and program output files 35 | SI*/ 36 | GI*/ 37 | UP*/ 38 | GM*/ 39 | debug/ 40 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | 3 | matrix: 4 | fast_finish: true 5 | include: 6 | - os: linux 7 | compiler: gcc 8 | - os: linux 9 | compiler: clang 10 | - os: osx 11 | osx_image: xcode7.3 12 | compiler: gcc 13 | - os: osx 14 | osx_image: xcode7.3 15 | compiler: clang 16 | - os: osx 17 | osx_image: xcode7.1 18 | compiler: gcc 19 | - os: osx 20 | osx_image: xcode7.1 21 | compiler: clang 22 | 23 | before_script: 24 | - wget https://github.com/vslavik/bakefile/releases/download/v1.2.5.1/bakefile-1.2.5.1_beta-bin.tar.bz2 -O /tmp/bakefile.tar.bz2 25 | - tar xjvf /tmp/bakefile.tar.bz2 26 | - export PATH=$PATH:$PWD/bakefile-1.2.5.1_beta 27 | script: 28 | - bkl wudecrypt.bkl 29 | - make CC=$CC CXX=$CXX 30 | after_success: 31 | - cat GNUmakefile 32 | 33 | notifications: 34 | email: 35 | recipients: 36 | - michael@armbrust.me 37 | on_success: change 38 | on_failure: always 39 | -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONFIG_H_ 2 | #define _CONFIG_H_ 3 | #include 4 | 5 | #ifndef _WIN32 6 | #include 7 | #define makedir(dir) mkdir(dir, 0777) 8 | #else 9 | #include 10 | #define makedir(dir) _mkdir(dir) 11 | #endif 12 | 13 | #define PTOC_SIZE 0x80 14 | 15 | static const char* APP_VERSION = "0.1.1"; 16 | 17 | static const size_t KEY_LENGTH = 16; 18 | static const size_t GAME_SERIAL_LENGTH = 10; 19 | static const size_t GAME_VER_LENGTH = 2; 20 | static const size_t SYS_VER_LENGTH = 3; 21 | static const size_t REGION_LENGTH = 3; 22 | 23 | static const uint8_t MAGIC_BYTES[4] = { 0x57, 0x55, 0x50, 0x2D }; 24 | 25 | static const unsigned long long int WIIU_DECRYPTED_AREA_OFFSET = 0x18000; 26 | static const unsigned long int PARTITION_TOC_OFFSET = 0x800; 27 | static const unsigned long int PARTITION_TOC_ENTRY_SIZE = PTOC_SIZE; 28 | 29 | static const uint8_t DECRYPTED_AREA_SIGNATURE[4] = { 0xCC, 0xA6, 0xE6, 0x7B }; 30 | static const uint8_t PARTITION_FILE_TABLE_SIGNATURE[4] = { 0x46, 0x53, 0x54, 0x00 }; 31 | 32 | static const char* TITLE_TICKET_FILE = "TITLE.TIK"; 33 | #endif // _CONFIG_H_ 34 | -------------------------------------------------------------------------------- /aes.h: -------------------------------------------------------------------------------- 1 | #ifndef _AES_H_ 2 | #define _AES_H_ 3 | 4 | #include 5 | 6 | 7 | // #define the macros below to 1/0 to enable/disable the mode of operation. 8 | // 9 | // CBC enables AES128 encryption in CBC-mode of operation and handles 0-padding. 10 | // ECB enables the basic ECB 16-byte block algorithm. Both can be enabled simultaneously. 11 | 12 | // The #ifndef-guard allows it to be configured before #include'ing or at compile time. 13 | #ifndef CBC 14 | #define CBC 1 15 | #endif 16 | 17 | #ifndef ECB 18 | #define ECB 0 19 | #endif 20 | 21 | 22 | 23 | #if defined(ECB) && ECB 24 | 25 | void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t *output); 26 | void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output); 27 | 28 | #endif // #if defined(ECB) && ECB 29 | 30 | 31 | #if defined(CBC) && CBC 32 | 33 | void AES128_CBC_encrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv); 34 | void AES128_CBC_decrypt_buffer(uint8_t* output, uint8_t* input, uint32_t length, const uint8_t* key, const uint8_t* iv); 35 | 36 | #endif // #if defined(CBC) && CBC 37 | 38 | 39 | 40 | #endif //_AES_H_ 41 | -------------------------------------------------------------------------------- /functions.h: -------------------------------------------------------------------------------- 1 | #ifndef _FUNCTIONS_H_ 2 | #define _FUNCTIONS_H_ 3 | #include 4 | #include "struct.h" 5 | 6 | uint8_t* loadKeyFile(FILE* file); 7 | uint8_t* loadKey(char* filename); 8 | 9 | void* readFileOffset(uint64_t offset, size_t size, size_t count, FILE* file); 10 | void* readFile(size_t size, size_t count, FILE* file); 11 | 12 | uint8_t* readEncryptedOffset(uint8_t* key, uint64_t offset, size_t size, FILE* file); 13 | uint8_t* readVolumeEncryptedOffset(uint8_t* key, int64_t volume_offset, int64_t cluster_offset, int64_t file_offset, size_t size, FILE* file); 14 | 15 | struct partition_entry* create_partition_entry(uint8_t* raw_entry); 16 | struct file* create_file(char* parent, char* filename, int64_t volume_base_offset, int64_t data_section_offset, int64_t lba, int64_t size, struct partition* source_partition, uint32_t entry_id); 17 | struct directory* create_directory(struct partition* source_partition, uint32_t* current_index, char* parent); 18 | 19 | void extract_all(FILE* infile, struct directory* root_directory, char* outputdir); 20 | void extract_dir(FILE* infile, struct directory* dir, char* outputdir); 21 | void extract_file(FILE* infile, struct file* file, char* outputdir); 22 | 23 | void extract_file_hashed(FILE* infile, char* outputpath, char* volumename, int64_t volume_offset, int64_t cluster_offset, int64_t file_offset, int64_t size, uint8_t* key, uint8_t* iv, uint16_t cluster_id); 24 | void extract_file_unhashed(FILE* infile, char* outputpath, char* volumename, int64_t volume_offset, int64_t cluster_offset, int64_t file_offset, int64_t size, uint8_t* key, uint8_t* iv); 25 | 26 | int strincmp(const char* s1, const char* s2, int n); 27 | int titlekeycmp(const void* e1, const void* e2); 28 | 29 | uint16_t bytesToUShortBE(uint8_t* bytes); 30 | uint32_t bytesToUIntBE(uint8_t* bytes); 31 | #endif // _FUNCTIONS_H_ 32 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | wudecrypt uses the following libraries according to their license: 2 | 3 | 4 | utarray 5 | 6 | Copyright (c) 2003-2014, Troy D. Hanson http://troydhanson.github.com/uthash/ 7 | All rights reserved. 8 | 9 | Redistribution and use in source and binary forms, with or without 10 | modification, are permitted provided that the following conditions are met: 11 | 12 | * Redistributions of source code must retain the above copyright 13 | notice, this list of conditions and the following disclaimer. 14 | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 16 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 18 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 19 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 24 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | 27 | 28 | sha1 29 | - Removed some library references because we don't use the whole library 30 | - Removed self-test code 31 | 32 | Licensed under the Apache License, Version 2.0 (the "License"); you may 33 | not use this file except in compliance with the License. 34 | You may obtain a copy of the License at 35 | 36 | http://www.apache.org/licenses/LICENSE-2.0 37 | 38 | Unless required by applicable law or agreed to in writing, software 39 | distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 40 | WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 41 | See the License for the specific language governing permissions and 42 | limitations under the License. 43 | 44 | This file is part of mbed TLS (https://tls.mbed.org) 45 | -------------------------------------------------------------------------------- /struct.h: -------------------------------------------------------------------------------- 1 | #ifndef _STRUCT_H_ 2 | #define _STRUCT_H_ 3 | #include "config.h" 4 | #include "utarray.h" 5 | 6 | struct partition_cluster { 7 | uint64_t offset; 8 | uint64_t size; 9 | 10 | uint32_t unknown1; 11 | uint32_t unknown2; 12 | }; 13 | 14 | struct partition_entry { 15 | uint8_t is_directory; 16 | uint64_t name_offset; 17 | // Allow names with a size of up to 512 bytes, should be enough hopefully 18 | char entry_name[512]; 19 | 20 | uint64_t offset_in_cluster; 21 | 22 | uint32_t last_row_in_dir; 23 | uint64_t size; 24 | 25 | uint16_t unknown; 26 | uint16_t starting_cluster; 27 | }; 28 | 29 | static const UT_icd partition_entry_icd = { sizeof(struct partition_entry), NULL, NULL, NULL }; 30 | 31 | struct partition { 32 | uint64_t offset; 33 | uint8_t identifier[25]; 34 | char name[PTOC_SIZE]; 35 | 36 | uint8_t key[16]; 37 | uint8_t iv[16]; 38 | 39 | uint32_t cluster_count; 40 | struct partition_cluster* clusters; 41 | 42 | UT_array* entries; 43 | }; 44 | 45 | struct titlekey { 46 | char name[19]; 47 | uint8_t encryptedKey[16]; 48 | uint8_t decryptedKey[16]; 49 | uint8_t iv[16]; 50 | }; 51 | 52 | static const UT_icd titlekey_icd = { sizeof(struct titlekey), NULL, NULL, NULL }; 53 | 54 | struct directory { 55 | int64_t record_offset; 56 | char parent[512]; 57 | 58 | UT_array* subdirs; 59 | UT_array* files; 60 | 61 | char directory_name[512]; 62 | }; 63 | 64 | static const UT_icd directory_icd = { sizeof(struct directory), NULL, NULL, NULL }; 65 | 66 | struct file { 67 | char parent[512]; 68 | char filename[512]; 69 | 70 | int64_t volume_base_offset; 71 | int64_t data_section_offset; 72 | int64_t lba; 73 | int64_t size; 74 | 75 | struct partition* parent_partition; 76 | uint32_t entry_id; 77 | }; 78 | 79 | static const UT_icd file_icd = { sizeof(struct file), NULL, NULL, NULL }; 80 | 81 | struct volume { 82 | struct partition* source; 83 | 84 | char identifier[PTOC_SIZE]; 85 | int64_t volume_base_offset; 86 | int64_t data_offset; 87 | 88 | struct directory* root_directory; 89 | }; 90 | 91 | struct block { 92 | int64_t number; 93 | int64_t offset; 94 | }; 95 | #endif // _STRUCT_H_ 96 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wudecrypt [![Build Status](https://travis-ci.org/maki-chan/wudecrypt.svg?branch=master)](https://travis-ci.org/maki-chan/wudecrypt) [![Coverity Scan Build Status](https://scan.coverity.com/projects/8817/badge.svg)](https://scan.coverity.com/projects/maki-chan-wudecrypt) 2 | 3 | ## What is wudecrypt? 4 | wudecrypt (sometimes also written WUDecrypt) is a tool written in C, fully cross-platform compatible, to decrypt Wii U Disk images (.wud). 5 | 6 | **NOTE: In its current state, wudecrypt is pre-alpha quality. It could crash during the extraction of WUD images, it's not well optimized (will run slowly) and other unwanted side effects could occur. I'm not responsible for any damage wudecrypt will cause on your system.** 7 | 8 | ## How to build 9 | Get the sources from Github and use [Bakefile](http://bakefile.org) to generate a GNU makefile that should work natively on OSX and Linux, but also via MinGW for Windows builds. You could also try to use bakefile to generate project files for Visual Studio on Windows, though I did not test VS building. 10 | 11 | For example, to get a working build, you could run the following commands in a shell (assuming you have bakefile working as a global command): 12 | ``` 13 | $ git clone https://github.com/maki-chan/wudecrypt.git 14 | $ cd wudecrypt 15 | $ bkl wudecrypt.bkl 16 | $ make 17 | ``` 18 | 19 | This will create a working `wudecrypt` executable. 20 | 21 | ## How to use 22 | For wudecrypt to work, you will need a WUD image, the corresponding disc key and the Wii U common key. If you have all of these files, you can run wudecrypt via the following command: 23 | ``` 24 | wudecrypt path/to/image.wud /path/to/output /path/to/commonkey.bin /path/to/disckey.bin 25 | ``` 26 | 27 | wudecrypt has a fifth optional argument which can be `SI`, `UP`, `GI` or `GM` depending on which partition types you want to extract. To play the decrypted image, extracting only the `GM` type partitions should be enough. I mostly introduced this function as the extraction takes a very long time and it tries to avoid a whole lot of data you won't need. 28 | 29 | ## License 30 | wudecrypt is released under the GNU AGPLv3 license. More information can be found in the LICENSE file or on the [original license page](https://www.gnu.org/licenses/agpl-3.0.txt). 31 | 32 | wudecrypt uses [utarray](http://troydhanson.github.com/uthash/), published by Troy D. Hanson, licensed under the [revised BSD license](https://troydhanson.github.io/uthash/license.html) 33 | 34 | wudecrypt uses the [SHA-1 implementation from mbed TLS](https://tls.mbed.org/sha-1-source-code), published by ARM Limited, licensed under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) 35 | -------------------------------------------------------------------------------- /sha1.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file sha1.h 3 | * 4 | * \brief SHA-1 cryptographic hash function 5 | * 6 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 7 | * SPDX-License-Identifier: Apache-2.0 8 | * 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 10 | * not use this file except in compliance with the License. 11 | * You may obtain a copy of the License at 12 | * 13 | * http://www.apache.org/licenses/LICENSE-2.0 14 | * 15 | * Unless required by applicable law or agreed to in writing, software 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | * See the License for the specific language governing permissions and 19 | * limitations under the License. 20 | * 21 | * This file is part of mbed TLS (https://tls.mbed.org) 22 | */ 23 | #ifndef MBEDTLS_SHA1_H 24 | #define MBEDTLS_SHA1_H 25 | 26 | #include 27 | #include 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | /** 34 | * \brief SHA-1 context structure 35 | */ 36 | typedef struct 37 | { 38 | uint32_t total[2]; /*!< number of bytes processed */ 39 | uint32_t state[5]; /*!< intermediate digest state */ 40 | unsigned char buffer[64]; /*!< data block being processed */ 41 | } 42 | mbedtls_sha1_context; 43 | 44 | /** 45 | * \brief Initialize SHA-1 context 46 | * 47 | * \param ctx SHA-1 context to be initialized 48 | */ 49 | void mbedtls_sha1_init( mbedtls_sha1_context *ctx ); 50 | 51 | /** 52 | * \brief Clear SHA-1 context 53 | * 54 | * \param ctx SHA-1 context to be cleared 55 | */ 56 | void mbedtls_sha1_free( mbedtls_sha1_context *ctx ); 57 | 58 | /** 59 | * \brief Clone (the state of) a SHA-1 context 60 | * 61 | * \param dst The destination context 62 | * \param src The context to be cloned 63 | */ 64 | void mbedtls_sha1_clone( mbedtls_sha1_context *dst, 65 | const mbedtls_sha1_context *src ); 66 | 67 | /** 68 | * \brief SHA-1 context setup 69 | * 70 | * \param ctx context to be initialized 71 | */ 72 | void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ); 73 | 74 | /** 75 | * \brief SHA-1 process buffer 76 | * 77 | * \param ctx SHA-1 context 78 | * \param input buffer holding the data 79 | * \param ilen length of the input data 80 | */ 81 | void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ); 82 | 83 | /** 84 | * \brief SHA-1 final digest 85 | * 86 | * \param ctx SHA-1 context 87 | * \param output SHA-1 checksum result 88 | */ 89 | void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ); 90 | 91 | /* Internal use */ 92 | void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] ); 93 | 94 | #ifdef __cplusplus 95 | } 96 | #endif 97 | 98 | #ifdef __cplusplus 99 | extern "C" { 100 | #endif 101 | 102 | /** 103 | * \brief Output = SHA-1( input buffer ) 104 | * 105 | * \param input buffer holding the data 106 | * \param ilen length of the input data 107 | * \param output SHA-1 checksum result 108 | */ 109 | void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ); 110 | 111 | #ifdef __cplusplus 112 | } 113 | #endif 114 | 115 | #endif /* mbedtls_sha1.h */ 116 | -------------------------------------------------------------------------------- /sha1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * FIPS-180-1 compliant SHA-1 implementation 3 | * 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 5 | * SPDX-License-Identifier: Apache-2.0 6 | * 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may 8 | * not use this file except in compliance with the License. 9 | * You may obtain a copy of the License at 10 | * 11 | * http://www.apache.org/licenses/LICENSE-2.0 12 | * 13 | * Unless required by applicable law or agreed to in writing, software 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 | * See the License for the specific language governing permissions and 17 | * limitations under the License. 18 | * 19 | * This file is part of mbed TLS (https://tls.mbed.org) 20 | */ 21 | /* 22 | * The SHA-1 standard was published by NIST in 1993. 23 | * 24 | * http://www.itl.nist.gov/fipspubs/fip180-1.htm 25 | */ 26 | 27 | #include 28 | #include "sha1.h" 29 | 30 | /* Implementation that should never be optimized out by the compiler */ 31 | static void mbedtls_zeroize( void *v, size_t n ) { 32 | volatile unsigned char *p = v; while( n-- ) *p++ = 0; 33 | } 34 | 35 | /* 36 | * 32-bit integer manipulation macros (big endian) 37 | */ 38 | #ifndef GET_UINT32_BE 39 | #define GET_UINT32_BE(n,b,i) \ 40 | { \ 41 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ 42 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ 43 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ 44 | | ( (uint32_t) (b)[(i) + 3] ); \ 45 | } 46 | #endif 47 | 48 | #ifndef PUT_UINT32_BE 49 | #define PUT_UINT32_BE(n,b,i) \ 50 | { \ 51 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ 52 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ 53 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ 54 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ 55 | } 56 | #endif 57 | 58 | void mbedtls_sha1_init( mbedtls_sha1_context *ctx ) 59 | { 60 | memset( ctx, 0, sizeof( mbedtls_sha1_context ) ); 61 | } 62 | 63 | void mbedtls_sha1_free( mbedtls_sha1_context *ctx ) 64 | { 65 | if( ctx == NULL ) 66 | return; 67 | 68 | mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) ); 69 | } 70 | 71 | void mbedtls_sha1_clone( mbedtls_sha1_context *dst, 72 | const mbedtls_sha1_context *src ) 73 | { 74 | *dst = *src; 75 | } 76 | 77 | /* 78 | * SHA-1 context setup 79 | */ 80 | void mbedtls_sha1_starts( mbedtls_sha1_context *ctx ) 81 | { 82 | ctx->total[0] = 0; 83 | ctx->total[1] = 0; 84 | 85 | ctx->state[0] = 0x67452301; 86 | ctx->state[1] = 0xEFCDAB89; 87 | ctx->state[2] = 0x98BADCFE; 88 | ctx->state[3] = 0x10325476; 89 | ctx->state[4] = 0xC3D2E1F0; 90 | } 91 | 92 | void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] ) 93 | { 94 | uint32_t temp, W[16], A, B, C, D, E; 95 | 96 | GET_UINT32_BE( W[ 0], data, 0 ); 97 | GET_UINT32_BE( W[ 1], data, 4 ); 98 | GET_UINT32_BE( W[ 2], data, 8 ); 99 | GET_UINT32_BE( W[ 3], data, 12 ); 100 | GET_UINT32_BE( W[ 4], data, 16 ); 101 | GET_UINT32_BE( W[ 5], data, 20 ); 102 | GET_UINT32_BE( W[ 6], data, 24 ); 103 | GET_UINT32_BE( W[ 7], data, 28 ); 104 | GET_UINT32_BE( W[ 8], data, 32 ); 105 | GET_UINT32_BE( W[ 9], data, 36 ); 106 | GET_UINT32_BE( W[10], data, 40 ); 107 | GET_UINT32_BE( W[11], data, 44 ); 108 | GET_UINT32_BE( W[12], data, 48 ); 109 | GET_UINT32_BE( W[13], data, 52 ); 110 | GET_UINT32_BE( W[14], data, 56 ); 111 | GET_UINT32_BE( W[15], data, 60 ); 112 | 113 | #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) 114 | 115 | #define R(t) \ 116 | ( \ 117 | temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \ 118 | W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \ 119 | ( W[t & 0x0F] = S(temp,1) ) \ 120 | ) 121 | 122 | #define P(a,b,c,d,e,x) \ 123 | { \ 124 | e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \ 125 | } 126 | 127 | A = ctx->state[0]; 128 | B = ctx->state[1]; 129 | C = ctx->state[2]; 130 | D = ctx->state[3]; 131 | E = ctx->state[4]; 132 | 133 | #define F(x,y,z) (z ^ (x & (y ^ z))) 134 | #define K 0x5A827999 135 | 136 | P( A, B, C, D, E, W[0] ); 137 | P( E, A, B, C, D, W[1] ); 138 | P( D, E, A, B, C, W[2] ); 139 | P( C, D, E, A, B, W[3] ); 140 | P( B, C, D, E, A, W[4] ); 141 | P( A, B, C, D, E, W[5] ); 142 | P( E, A, B, C, D, W[6] ); 143 | P( D, E, A, B, C, W[7] ); 144 | P( C, D, E, A, B, W[8] ); 145 | P( B, C, D, E, A, W[9] ); 146 | P( A, B, C, D, E, W[10] ); 147 | P( E, A, B, C, D, W[11] ); 148 | P( D, E, A, B, C, W[12] ); 149 | P( C, D, E, A, B, W[13] ); 150 | P( B, C, D, E, A, W[14] ); 151 | P( A, B, C, D, E, W[15] ); 152 | P( E, A, B, C, D, R(16) ); 153 | P( D, E, A, B, C, R(17) ); 154 | P( C, D, E, A, B, R(18) ); 155 | P( B, C, D, E, A, R(19) ); 156 | 157 | #undef K 158 | #undef F 159 | 160 | #define F(x,y,z) (x ^ y ^ z) 161 | #define K 0x6ED9EBA1 162 | 163 | P( A, B, C, D, E, R(20) ); 164 | P( E, A, B, C, D, R(21) ); 165 | P( D, E, A, B, C, R(22) ); 166 | P( C, D, E, A, B, R(23) ); 167 | P( B, C, D, E, A, R(24) ); 168 | P( A, B, C, D, E, R(25) ); 169 | P( E, A, B, C, D, R(26) ); 170 | P( D, E, A, B, C, R(27) ); 171 | P( C, D, E, A, B, R(28) ); 172 | P( B, C, D, E, A, R(29) ); 173 | P( A, B, C, D, E, R(30) ); 174 | P( E, A, B, C, D, R(31) ); 175 | P( D, E, A, B, C, R(32) ); 176 | P( C, D, E, A, B, R(33) ); 177 | P( B, C, D, E, A, R(34) ); 178 | P( A, B, C, D, E, R(35) ); 179 | P( E, A, B, C, D, R(36) ); 180 | P( D, E, A, B, C, R(37) ); 181 | P( C, D, E, A, B, R(38) ); 182 | P( B, C, D, E, A, R(39) ); 183 | 184 | #undef K 185 | #undef F 186 | 187 | #define F(x,y,z) ((x & y) | (z & (x | y))) 188 | #define K 0x8F1BBCDC 189 | 190 | P( A, B, C, D, E, R(40) ); 191 | P( E, A, B, C, D, R(41) ); 192 | P( D, E, A, B, C, R(42) ); 193 | P( C, D, E, A, B, R(43) ); 194 | P( B, C, D, E, A, R(44) ); 195 | P( A, B, C, D, E, R(45) ); 196 | P( E, A, B, C, D, R(46) ); 197 | P( D, E, A, B, C, R(47) ); 198 | P( C, D, E, A, B, R(48) ); 199 | P( B, C, D, E, A, R(49) ); 200 | P( A, B, C, D, E, R(50) ); 201 | P( E, A, B, C, D, R(51) ); 202 | P( D, E, A, B, C, R(52) ); 203 | P( C, D, E, A, B, R(53) ); 204 | P( B, C, D, E, A, R(54) ); 205 | P( A, B, C, D, E, R(55) ); 206 | P( E, A, B, C, D, R(56) ); 207 | P( D, E, A, B, C, R(57) ); 208 | P( C, D, E, A, B, R(58) ); 209 | P( B, C, D, E, A, R(59) ); 210 | 211 | #undef K 212 | #undef F 213 | 214 | #define F(x,y,z) (x ^ y ^ z) 215 | #define K 0xCA62C1D6 216 | 217 | P( A, B, C, D, E, R(60) ); 218 | P( E, A, B, C, D, R(61) ); 219 | P( D, E, A, B, C, R(62) ); 220 | P( C, D, E, A, B, R(63) ); 221 | P( B, C, D, E, A, R(64) ); 222 | P( A, B, C, D, E, R(65) ); 223 | P( E, A, B, C, D, R(66) ); 224 | P( D, E, A, B, C, R(67) ); 225 | P( C, D, E, A, B, R(68) ); 226 | P( B, C, D, E, A, R(69) ); 227 | P( A, B, C, D, E, R(70) ); 228 | P( E, A, B, C, D, R(71) ); 229 | P( D, E, A, B, C, R(72) ); 230 | P( C, D, E, A, B, R(73) ); 231 | P( B, C, D, E, A, R(74) ); 232 | P( A, B, C, D, E, R(75) ); 233 | P( E, A, B, C, D, R(76) ); 234 | P( D, E, A, B, C, R(77) ); 235 | P( C, D, E, A, B, R(78) ); 236 | P( B, C, D, E, A, R(79) ); 237 | 238 | #undef K 239 | #undef F 240 | 241 | ctx->state[0] += A; 242 | ctx->state[1] += B; 243 | ctx->state[2] += C; 244 | ctx->state[3] += D; 245 | ctx->state[4] += E; 246 | } 247 | 248 | /* 249 | * SHA-1 process buffer 250 | */ 251 | void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen ) 252 | { 253 | size_t fill; 254 | uint32_t left; 255 | 256 | if( ilen == 0 ) 257 | return; 258 | 259 | left = ctx->total[0] & 0x3F; 260 | fill = 64 - left; 261 | 262 | ctx->total[0] += (uint32_t) ilen; 263 | ctx->total[0] &= 0xFFFFFFFF; 264 | 265 | if( ctx->total[0] < (uint32_t) ilen ) 266 | ctx->total[1]++; 267 | 268 | if( left && ilen >= fill ) 269 | { 270 | memcpy( (void *) (ctx->buffer + left), input, fill ); 271 | mbedtls_sha1_process( ctx, ctx->buffer ); 272 | input += fill; 273 | ilen -= fill; 274 | left = 0; 275 | } 276 | 277 | while( ilen >= 64 ) 278 | { 279 | mbedtls_sha1_process( ctx, input ); 280 | input += 64; 281 | ilen -= 64; 282 | } 283 | 284 | if( ilen > 0 ) 285 | memcpy( (void *) (ctx->buffer + left), input, ilen ); 286 | } 287 | 288 | static const unsigned char sha1_padding[64] = 289 | { 290 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 291 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 292 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 293 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 294 | }; 295 | 296 | /* 297 | * SHA-1 final digest 298 | */ 299 | void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] ) 300 | { 301 | uint32_t last, padn; 302 | uint32_t high, low; 303 | unsigned char msglen[8]; 304 | 305 | high = ( ctx->total[0] >> 29 ) 306 | | ( ctx->total[1] << 3 ); 307 | low = ( ctx->total[0] << 3 ); 308 | 309 | PUT_UINT32_BE( high, msglen, 0 ); 310 | PUT_UINT32_BE( low, msglen, 4 ); 311 | 312 | last = ctx->total[0] & 0x3F; 313 | padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); 314 | 315 | mbedtls_sha1_update( ctx, sha1_padding, padn ); 316 | mbedtls_sha1_update( ctx, msglen, 8 ); 317 | 318 | PUT_UINT32_BE( ctx->state[0], output, 0 ); 319 | PUT_UINT32_BE( ctx->state[1], output, 4 ); 320 | PUT_UINT32_BE( ctx->state[2], output, 8 ); 321 | PUT_UINT32_BE( ctx->state[3], output, 12 ); 322 | PUT_UINT32_BE( ctx->state[4], output, 16 ); 323 | } 324 | 325 | /* 326 | * output = SHA-1( input buffer ) 327 | */ 328 | void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ) 329 | { 330 | mbedtls_sha1_context ctx; 331 | 332 | mbedtls_sha1_init( &ctx ); 333 | mbedtls_sha1_starts( &ctx ); 334 | mbedtls_sha1_update( &ctx, input, ilen ); 335 | mbedtls_sha1_finish( &ctx, output ); 336 | mbedtls_sha1_free( &ctx ); 337 | } 338 | -------------------------------------------------------------------------------- /utarray.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2008-2014, Troy D. Hanson http://troydhanson.github.com/uthash/ 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 12 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 13 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 14 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 15 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 16 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 17 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 18 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 19 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 20 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 | */ 23 | 24 | /* a dynamic array implementation using macros 25 | */ 26 | #ifndef UTARRAY_H 27 | #define UTARRAY_H 28 | 29 | #define UTARRAY_VERSION 1.9.9 30 | 31 | #ifdef __GNUC__ 32 | #define _UNUSED_ __attribute__ ((__unused__)) 33 | #else 34 | #define _UNUSED_ 35 | #endif 36 | 37 | #include /* size_t */ 38 | #include /* memset, etc */ 39 | #include /* exit */ 40 | 41 | #ifndef oom 42 | #define oom() exit(-1) 43 | #endif 44 | 45 | typedef void (ctor_f)(void *dst, const void *src); 46 | typedef void (dtor_f)(void *elt); 47 | typedef void (init_f)(void *elt); 48 | typedef struct { 49 | size_t sz; 50 | init_f *init; 51 | ctor_f *copy; 52 | dtor_f *dtor; 53 | } UT_icd; 54 | 55 | typedef struct { 56 | unsigned i,n;/* i: index of next available slot, n: num slots */ 57 | UT_icd icd; /* initializer, copy and destructor functions */ 58 | char *d; /* n slots of size icd->sz*/ 59 | } UT_array; 60 | 61 | #define utarray_init(a,_icd) do { \ 62 | memset(a,0,sizeof(UT_array)); \ 63 | (a)->icd=*_icd; \ 64 | } while(0) 65 | 66 | #define utarray_done(a) do { \ 67 | if ((a)->n) { \ 68 | if ((a)->icd.dtor) { \ 69 | size_t _ut_i; \ 70 | for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \ 71 | (a)->icd.dtor(utarray_eltptr(a,_ut_i)); \ 72 | } \ 73 | } \ 74 | free((a)->d); \ 75 | } \ 76 | (a)->n=0; \ 77 | } while(0) 78 | 79 | #define utarray_new(a,_icd) do { \ 80 | a=(UT_array*)malloc(sizeof(UT_array)); \ 81 | if (a == NULL) oom(); \ 82 | utarray_init(a,_icd); \ 83 | } while(0) 84 | 85 | #define utarray_free(a) do { \ 86 | utarray_done(a); \ 87 | free(a); \ 88 | } while(0) 89 | 90 | #define utarray_reserve(a,by) do { \ 91 | if (((a)->i+(by)) > ((a)->n)) { \ 92 | char *utarray_tmp; \ 93 | while(((a)->i+(by)) > ((a)->n)) { (a)->n = ((a)->n ? (2*(a)->n) : 8); } \ 94 | utarray_tmp=(char*)realloc((a)->d, (a)->n*(a)->icd.sz); \ 95 | if (utarray_tmp == NULL) oom(); \ 96 | (a)->d=utarray_tmp; \ 97 | } \ 98 | } while(0) 99 | 100 | #define utarray_push_back(a,p) do { \ 101 | utarray_reserve(a,1); \ 102 | if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,(a)->i++), p); } \ 103 | else { memcpy(_utarray_eltptr(a,(a)->i++), p, (a)->icd.sz); }; \ 104 | } while(0) 105 | 106 | #define utarray_pop_back(a) do { \ 107 | if ((a)->icd.dtor) { (a)->icd.dtor( _utarray_eltptr(a,--((a)->i))); } \ 108 | else { (a)->i--; } \ 109 | } while(0) 110 | 111 | #define utarray_extend_back(a) do { \ 112 | utarray_reserve(a,1); \ 113 | if ((a)->icd.init) { (a)->icd.init(_utarray_eltptr(a,(a)->i)); } \ 114 | else { memset(_utarray_eltptr(a,(a)->i),0,(a)->icd.sz); } \ 115 | (a)->i++; \ 116 | } while(0) 117 | 118 | #define utarray_len(a) ((a)->i) 119 | 120 | #define utarray_eltptr(a,j) (((j) < (a)->i) ? _utarray_eltptr(a,j) : NULL) 121 | #define _utarray_eltptr(a,j) ((char*)((a)->d + ((a)->icd.sz*(j) ))) 122 | 123 | #define utarray_insert(a,p,j) do { \ 124 | if (j > (a)->i) utarray_resize(a,j); \ 125 | utarray_reserve(a,1); \ 126 | if ((j) < (a)->i) { \ 127 | memmove( _utarray_eltptr(a,(j)+1), _utarray_eltptr(a,j), \ 128 | ((a)->i - (j))*((a)->icd.sz)); \ 129 | } \ 130 | if ((a)->icd.copy) { (a)->icd.copy( _utarray_eltptr(a,j), p); } \ 131 | else { memcpy(_utarray_eltptr(a,j), p, (a)->icd.sz); }; \ 132 | (a)->i++; \ 133 | } while(0) 134 | 135 | #define utarray_inserta(a,w,j) do { \ 136 | if (utarray_len(w) == 0) break; \ 137 | if (j > (a)->i) utarray_resize(a,j); \ 138 | utarray_reserve(a,utarray_len(w)); \ 139 | if ((j) < (a)->i) { \ 140 | memmove(_utarray_eltptr(a,(j)+utarray_len(w)), \ 141 | _utarray_eltptr(a,j), \ 142 | ((a)->i - (j))*((a)->icd.sz)); \ 143 | } \ 144 | if ((a)->icd.copy) { \ 145 | size_t _ut_i; \ 146 | for(_ut_i=0;_ut_i<(w)->i;_ut_i++) { \ 147 | (a)->icd.copy(_utarray_eltptr(a,j+_ut_i), _utarray_eltptr(w,_ut_i)); \ 148 | } \ 149 | } else { \ 150 | memcpy(_utarray_eltptr(a,j), _utarray_eltptr(w,0), \ 151 | utarray_len(w)*((a)->icd.sz)); \ 152 | } \ 153 | (a)->i += utarray_len(w); \ 154 | } while(0) 155 | 156 | #define utarray_resize(dst,num) do { \ 157 | size_t _ut_i; \ 158 | if (dst->i > (size_t)(num)) { \ 159 | if ((dst)->icd.dtor) { \ 160 | for(_ut_i=num; _ut_i < dst->i; _ut_i++) { \ 161 | (dst)->icd.dtor(utarray_eltptr(dst,_ut_i)); \ 162 | } \ 163 | } \ 164 | } else if (dst->i < (size_t)(num)) { \ 165 | utarray_reserve(dst,num-dst->i); \ 166 | if ((dst)->icd.init) { \ 167 | for(_ut_i=dst->i; _ut_i < num; _ut_i++) { \ 168 | (dst)->icd.init(utarray_eltptr(dst,_ut_i)); \ 169 | } \ 170 | } else { \ 171 | memset(_utarray_eltptr(dst,dst->i),0,(dst)->icd.sz*(num-dst->i)); \ 172 | } \ 173 | } \ 174 | dst->i = num; \ 175 | } while(0) 176 | 177 | #define utarray_concat(dst,src) do { \ 178 | utarray_inserta((dst),(src),utarray_len(dst)); \ 179 | } while(0) 180 | 181 | #define utarray_erase(a,pos,len) do { \ 182 | if ((a)->icd.dtor) { \ 183 | size_t _ut_i; \ 184 | for(_ut_i=0; _ut_i < len; _ut_i++) { \ 185 | (a)->icd.dtor(utarray_eltptr((a),pos+_ut_i)); \ 186 | } \ 187 | } \ 188 | if ((a)->i > (pos+len)) { \ 189 | memmove( _utarray_eltptr((a),pos), _utarray_eltptr((a),pos+len), \ 190 | (((a)->i)-(pos+len))*((a)->icd.sz)); \ 191 | } \ 192 | (a)->i -= (len); \ 193 | } while(0) 194 | 195 | #define utarray_renew(a,u) do { \ 196 | if (a) utarray_clear(a); \ 197 | else utarray_new((a),(u)); \ 198 | } while(0) 199 | 200 | #define utarray_clear(a) do { \ 201 | if ((a)->i > 0) { \ 202 | if ((a)->icd.dtor) { \ 203 | size_t _ut_i; \ 204 | for(_ut_i=0; _ut_i < (a)->i; _ut_i++) { \ 205 | (a)->icd.dtor(utarray_eltptr(a,_ut_i)); \ 206 | } \ 207 | } \ 208 | (a)->i = 0; \ 209 | } \ 210 | } while(0) 211 | 212 | #define utarray_sort(a,cmp) do { \ 213 | qsort((a)->d, (a)->i, (a)->icd.sz, cmp); \ 214 | } while(0) 215 | 216 | #define utarray_find(a,v,cmp) bsearch((v),(a)->d,(a)->i,(a)->icd.sz,cmp) 217 | 218 | #define utarray_front(a) (((a)->i) ? (_utarray_eltptr(a,0)) : NULL) 219 | #define utarray_next(a,e) (((e)==NULL) ? utarray_front(a) : ((((a)->i) > (utarray_eltidx(a,e)+1)) ? _utarray_eltptr(a,utarray_eltidx(a,e)+1) : NULL)) 220 | #define utarray_prev(a,e) (((e)==NULL) ? utarray_back(a) : ((utarray_eltidx(a,e) > 0) ? _utarray_eltptr(a,utarray_eltidx(a,e)-1) : NULL)) 221 | #define utarray_back(a) (((a)->i) ? (_utarray_eltptr(a,(a)->i-1)) : NULL) 222 | #define utarray_eltidx(a,e) (((char*)(e) >= (char*)((a)->d)) ? (((char*)(e) - (char*)((a)->d))/(size_t)(a)->icd.sz) : -1) 223 | 224 | /* last we pre-define a few icd for common utarrays of ints and strings */ 225 | static void utarray_str_cpy(void *dst, const void *src) { 226 | char **_src = (char**)src, **_dst = (char**)dst; 227 | *_dst = (*_src == NULL) ? NULL : strdup(*_src); 228 | } 229 | static void utarray_str_dtor(void *elt) { 230 | char **eltc = (char**)elt; 231 | if (*eltc) free(*eltc); 232 | } 233 | static const UT_icd ut_str_icd _UNUSED_ = {sizeof(char*),NULL,utarray_str_cpy,utarray_str_dtor}; 234 | static const UT_icd ut_int_icd _UNUSED_ = {sizeof(int),NULL,NULL,NULL}; 235 | static const UT_icd ut_ptr_icd _UNUSED_ = {sizeof(void*),NULL,NULL,NULL}; 236 | 237 | 238 | #endif /* UTARRAY_H */ 239 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "config.h" 6 | #include "utarray.h" 7 | #include "struct.h" 8 | #include "functions.h" 9 | #include "aes.h" 10 | 11 | int main(int argc, char* argv[]) { 12 | int i, j, c; 13 | uint32_t partition_count, current_ft_size, current_dir_index; 14 | uint64_t cluster_start, entries_offset, total_entries, name_table_offset, current_entry_offset, current_name_offset; 15 | char* gameserial; 16 | char* gameversion; 17 | char* gameregion; 18 | char* sysversion; 19 | char partition_hash_name[19]; 20 | char calculated_name[19]; 21 | char outputdir[1024]; 22 | uint8_t* commonkey; 23 | uint8_t* disckey; 24 | uint8_t* partition_toc; 25 | uint8_t* partition_block; 26 | uint8_t* decrypted_data; 27 | uint8_t* decrypted_data2; 28 | uint8_t* titleid; 29 | uint8_t raw_entry[16]; 30 | struct partition_entry* entry; 31 | struct partition* partitions; 32 | struct volume* volumes; 33 | struct titlekey* titlekey; 34 | struct titlekey* newtitlekey; 35 | UT_array* titlekeys; 36 | FILE* wudimage; 37 | 38 | // Initialize array right at the start 39 | utarray_new(titlekeys, &titlekey_icd); 40 | 41 | printf("WUDecrypt v%s by makikatze\n", APP_VERSION); 42 | printf("Licensed under GNU AGPLv3\n\n"); 43 | 44 | if (argc < 5 || argc > 6) { 45 | printf("Usage: %s []\n", argv[0]); 46 | exit(EXIT_FAILURE); 47 | } 48 | 49 | commonkey = loadKey(argv[3]); 50 | if (commonkey == NULL) { 51 | fprintf(stderr, "Error while loading common key\n"); 52 | exit(EXIT_FAILURE); 53 | } 54 | 55 | disckey = loadKey(argv[4]); 56 | if (disckey == NULL) { 57 | fprintf(stderr, "Error while loading disc key\n"); 58 | exit(EXIT_FAILURE); 59 | } 60 | 61 | wudimage = fopen(argv[1], "r"); 62 | if (wudimage == NULL) { 63 | fprintf(stderr, "Could not open WUD image\n"); 64 | exit(EXIT_FAILURE); 65 | } 66 | 67 | gameserial = (char*)readFileOffset(0, sizeof(char), GAME_SERIAL_LENGTH, wudimage); 68 | if (gameserial == NULL) { 69 | fprintf(stderr, "Couldn't read game serial from image\n"); 70 | fclose(wudimage); 71 | exit(EXIT_FAILURE); 72 | } 73 | if (memcmp(gameserial, MAGIC_BYTES, 4) != 0) { 74 | fprintf(stderr, "WARNING: Most probably no valid WUD image\nTrying to continue anyways, although errors are expected!\n\n"); 75 | } 76 | 77 | if (fseek(wudimage, 1, SEEK_CUR) != 0) { 78 | fprintf(stderr, "Error: Could not seek in WUD image\n"); 79 | fclose(wudimage); 80 | exit(EXIT_FAILURE); 81 | } 82 | gameversion = (char*)readFile(sizeof(char), GAME_VER_LENGTH, wudimage); 83 | if (gameversion == NULL) { 84 | fprintf(stderr, "Couldn't read game version from image\n"); 85 | fclose(wudimage); 86 | exit(EXIT_FAILURE); 87 | } 88 | 89 | if (fseek(wudimage, 1, SEEK_CUR) != 0) { 90 | fprintf(stderr, "Error: Could not seek in WUD image\n"); 91 | fclose(wudimage); 92 | exit(EXIT_FAILURE); 93 | } 94 | sysversion = (char*)readFile(sizeof(char), SYS_VER_LENGTH, wudimage); 95 | if (sysversion == NULL) { 96 | fprintf(stderr, "Couldn't read system version from image\n"); 97 | fclose(wudimage); 98 | exit(EXIT_FAILURE); 99 | } 100 | gameregion = (char*)readFile(sizeof(char), REGION_LENGTH, wudimage); 101 | if (gameregion == NULL) { 102 | fprintf(stderr, "Couldn't read game region from image\n"); 103 | fclose(wudimage); 104 | exit(EXIT_FAILURE); 105 | } 106 | 107 | // Print information about game 108 | printf("Game Serial: %.*s\n", (int)GAME_SERIAL_LENGTH, gameserial); 109 | printf("Game Revision: %.*s\n", (int)GAME_VER_LENGTH, gameversion); 110 | printf("System Version: %c.%c.%c\n", sysversion[0], sysversion[1], sysversion[2]); 111 | printf("Game Region: %.*s\n\n", (int)REGION_LENGTH, gameregion); 112 | 113 | partition_toc = readEncryptedOffset(disckey, WIIU_DECRYPTED_AREA_OFFSET, 0x8000, wudimage); 114 | if (partition_toc == NULL || memcmp(partition_toc, DECRYPTED_AREA_SIGNATURE, 4) != 0) { 115 | fprintf(stderr, "Couldn't decrypt partition table\n"); 116 | fclose(wudimage); 117 | exit(EXIT_FAILURE); 118 | } 119 | 120 | partition_count = bytesToUIntBE(partition_toc + 0x1C); 121 | printf("Partition count: %d\n", partition_count); 122 | 123 | partitions = (struct partition*)malloc(partition_count * sizeof(struct partition)); 124 | volumes = (struct volume*)malloc(partition_count * sizeof(struct volume)); 125 | for (i = 0; i < partition_count; i++) { 126 | memcpy(partitions[i].identifier, partition_toc + PARTITION_TOC_OFFSET + (i * PARTITION_TOC_ENTRY_SIZE), 0x19); 127 | memcpy(partitions[i].name, partition_toc + PARTITION_TOC_OFFSET + (i * PARTITION_TOC_ENTRY_SIZE), PARTITION_TOC_ENTRY_SIZE); 128 | partitions[i].offset = (uint64_t)bytesToUIntBE(partition_toc + PARTITION_TOC_OFFSET + (i * PARTITION_TOC_ENTRY_SIZE) + 0x20); 129 | partitions[i].offset *= 0x8000; 130 | partitions[i].offset -= 0x10000; 131 | 132 | printf("\nPartition %d:\n", i + 1); 133 | printf("\tPartition ID: %.*s\n", 0x19, partitions[i].identifier); 134 | printf("\tPartition Name: %s\n", partitions[i].name); 135 | printf("\tPartition Offset: 0x%llX\n", (unsigned long long int)partitions[i].offset); 136 | 137 | strncpy(partition_hash_name, partitions[i].name, 18); 138 | partition_hash_name[18] = '\0'; 139 | for (titlekey = (struct titlekey*)utarray_front(titlekeys); titlekey != NULL; titlekey = (struct titlekey*)utarray_next(titlekeys, titlekey)) { 140 | if (strncmp(titlekey->name, partition_hash_name, 18) == 0) { 141 | break; 142 | } 143 | } 144 | if (strncmp((char*)partitions[i].name, "SI", 2) == 0 145 | || strncmp((char*)partitions[i].name, "UP", 2) == 0 146 | || strncmp((char*)partitions[i].name, "GI", 2) == 0 147 | || titlekey != NULL) { 148 | if (titlekey == NULL) { 149 | memcpy(partitions[i].key, disckey, 16); 150 | memset(partitions[i].iv, 0, 16); 151 | } else { 152 | memcpy(partitions[i].key, titlekey->decryptedKey, 16); 153 | memcpy(partitions[i].iv, titlekey->iv, 16); 154 | } 155 | 156 | printf("\tPartition Key: "); 157 | for (c = 0; c < 12; c++) { 158 | printf("%02X", partitions[i].key[c]); 159 | } 160 | printf("********\n\n"); 161 | 162 | current_ft_size = 0x8000; 163 | 164 | partition_block = readEncryptedOffset(partitions[i].key, WIIU_DECRYPTED_AREA_OFFSET + partitions[i].offset, current_ft_size, wudimage); 165 | 166 | if (memcmp(partition_block, PARTITION_FILE_TABLE_SIGNATURE, 4) != 0) { 167 | fprintf(stderr, "Decrypted partition %s has no valid file table signature\n", partitions[i].name); 168 | fclose(wudimage); 169 | exit(EXIT_FAILURE); 170 | } 171 | 172 | partitions[i].cluster_count = bytesToUIntBE(partition_block + 8); 173 | partitions[i].clusters = (struct partition_cluster*)malloc(partitions[i].cluster_count * sizeof(struct partition_cluster)); 174 | for (c = 0; c < partitions[i].cluster_count; c++) { 175 | cluster_start = (uint64_t)(bytesToUIntBE(partition_block + 0x20 + (0x20 * c))) * 0x8000; 176 | partitions[i].clusters[c].unknown1 = bytesToUIntBE(partition_block + 0x20 + (0x20 * c) + 0x10); 177 | partitions[i].clusters[c].unknown2 = bytesToUIntBE(partition_block + 0x20 + (0x20 * c) + 0x14); 178 | 179 | if (cluster_start > 0) { 180 | partitions[i].clusters[c].offset = cluster_start - 0x8000; 181 | } else { 182 | partitions[i].clusters[c].offset = 0; 183 | } 184 | 185 | partitions[i].clusters[c].size = (uint64_t)(bytesToUIntBE(partition_block + 0x20 + (0x20 * c) + 4)) * 0x8000; 186 | } 187 | 188 | entries_offset = ((uint64_t)bytesToUIntBE(partition_block + 4) * bytesToUIntBE(partition_block + 8)) + 0x20; 189 | 190 | memcpy(raw_entry, partition_block + entries_offset, 16); 191 | entry = create_partition_entry(raw_entry); 192 | 193 | total_entries = entry->last_row_in_dir; 194 | name_table_offset = entries_offset + (total_entries * 0x10); 195 | 196 | strncpy(entry->entry_name, (char*)(partition_block + name_table_offset + entry->name_offset), 0x200); 197 | 198 | utarray_new(partitions[i].entries, &partition_entry_icd); 199 | utarray_push_back(partitions[i].entries, entry); 200 | 201 | for(j = 1; j < total_entries; j++) { 202 | current_entry_offset = entries_offset + (j * 0x10); 203 | 204 | while((current_entry_offset + 0x10) > current_ft_size) { 205 | current_ft_size += 0x8000; 206 | free(partition_block); 207 | partition_block = readEncryptedOffset(partitions[i].key, WIIU_DECRYPTED_AREA_OFFSET + partitions[i].offset, current_ft_size, wudimage); 208 | } 209 | 210 | memcpy(raw_entry, partition_block + current_entry_offset, 16); 211 | entry = create_partition_entry(raw_entry); 212 | 213 | current_name_offset = name_table_offset + entry->name_offset; 214 | 215 | while((current_name_offset + 0x200) > current_ft_size) { 216 | current_ft_size += 0x8000; 217 | free(partition_block); 218 | partition_block = readEncryptedOffset(partitions[i].key, WIIU_DECRYPTED_AREA_OFFSET + partitions[i].offset, current_ft_size, wudimage); 219 | } 220 | 221 | strncpy(entry->entry_name, (char*)(partition_block + current_name_offset), 0x200); 222 | 223 | utarray_push_back(partitions[i].entries, entry); 224 | } 225 | 226 | if (strncmp((char*)partitions[i].name, "SI", 2) == 0 227 | || strncmp((char*)partitions[i].name, "GI", 2) == 0) { 228 | for (entry = (struct partition_entry*)utarray_front(partitions[i].entries); entry != NULL; entry = (struct partition_entry*)utarray_next(partitions[i].entries, entry)) { 229 | if (entry->is_directory != 1 && strincmp(entry->entry_name, TITLE_TICKET_FILE, strlen(TITLE_TICKET_FILE)) == 0) { 230 | decrypted_data = readVolumeEncryptedOffset(partitions[i].key, partitions[i].offset, partitions[i].clusters[entry->starting_cluster].offset, entry->offset_in_cluster + 0x1BF, 0x10, wudimage); 231 | titleid = readVolumeEncryptedOffset(partitions[i].key, partitions[i].offset, partitions[i].clusters[entry->starting_cluster].offset, entry->offset_in_cluster + 0x1DC, 8, wudimage); 232 | 233 | // Using raw_entry as iv here because its size is suitable 234 | memset(raw_entry, 0, 16); 235 | memcpy(raw_entry, titleid, 8); 236 | decrypted_data2 = (uint8_t*)malloc(0x10 * sizeof(uint8_t)); 237 | AES128_CBC_decrypt_buffer(decrypted_data2, decrypted_data, 0x10, commonkey, raw_entry); 238 | 239 | sprintf(calculated_name, "GM%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX", titleid[0], titleid[1], titleid[2], titleid[3], titleid[4], titleid[5], titleid[6], titleid[7]); 240 | newtitlekey = (struct titlekey*)malloc(sizeof(struct titlekey)); 241 | memcpy(newtitlekey->encryptedKey, decrypted_data, 0x10); 242 | memcpy(newtitlekey->decryptedKey, decrypted_data2, 0x10); 243 | memcpy(newtitlekey->iv, raw_entry, 0x10); 244 | strncpy(newtitlekey->name, calculated_name, 0x12); 245 | newtitlekey->name[18] = '\0'; 246 | 247 | utarray_sort(titlekeys, titlekeycmp); 248 | if(utarray_find(titlekeys, newtitlekey, titlekeycmp) == NULL) { 249 | utarray_push_back(titlekeys, newtitlekey); 250 | } 251 | 252 | free(newtitlekey); 253 | } 254 | } 255 | } 256 | 257 | if ((argc >= 6 && strncmp((char*)partitions[i].name, argv[5], 2) == 0) 258 | || argc < 6) { 259 | volumes[i].source = &(partitions[i]); 260 | volumes[i].volume_base_offset = partitions[i].offset; 261 | strncpy(volumes[i].identifier, partitions[i].name, PARTITION_TOC_ENTRY_SIZE - 1); 262 | volumes[i].identifier[127] = '\0'; 263 | current_dir_index = 0; 264 | volumes[i].root_directory = create_directory(volumes[i].source, ¤t_dir_index, ""); 265 | 266 | strncpy(outputdir, argv[2], 1023); 267 | outputdir[1023] = '\0'; 268 | if(outputdir[strlen(outputdir) - 1] == '/') { 269 | outputdir[strlen(outputdir) - 1] = '\0'; 270 | } 271 | 272 | extract_all(wudimage, volumes[i].root_directory, outputdir); 273 | } 274 | } else { 275 | fprintf(stderr, "WARNING: Partition %s has no matching key and cannot be decrypted\n\n", partitions[i].name); 276 | } 277 | } 278 | 279 | fclose(wudimage); 280 | return EXIT_SUCCESS; 281 | } 282 | -------------------------------------------------------------------------------- /functions.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "config.h" 7 | #include "struct.h" 8 | #include "functions.h" 9 | #include "aes.h" 10 | #include "sha1.h" 11 | 12 | uint8_t* loadKeyFile(FILE* file) { 13 | uint8_t* key; 14 | 15 | // Check for errors with the given file pointer 16 | if (file == NULL) { 17 | fprintf(stderr, "Given file pointer was a null reference\n"); 18 | return NULL; 19 | } 20 | 21 | // Allocate the memory where we'll store the key 22 | key = (uint8_t*)malloc(KEY_LENGTH * sizeof(uint8_t)); 23 | if (key == NULL) { 24 | fprintf(stderr, "Could not allocate enough bytes for key\n"); 25 | return NULL; 26 | } 27 | 28 | // Read the key from the given file 29 | if (fread(key, sizeof(uint8_t), KEY_LENGTH, file) != KEY_LENGTH) { 30 | free(key); 31 | fprintf(stderr, "Could not read the key from file\n"); 32 | return NULL; 33 | } 34 | 35 | return key; 36 | } 37 | 38 | uint8_t* loadKey(char* filename) { 39 | FILE* file; 40 | uint8_t* key = NULL; 41 | 42 | file = fopen(filename, "r"); 43 | if (file != NULL) { 44 | key = loadKeyFile(file); 45 | fclose(file); 46 | } 47 | 48 | return key; 49 | } 50 | 51 | void* readFileOffset(uint64_t offset, size_t size, size_t count, FILE* file) { 52 | void* output; 53 | if (fseek(file, offset, SEEK_SET) != 0) { 54 | fprintf(stderr, "Error while seeking in file\n"); 55 | return NULL; 56 | } 57 | output = malloc(size * count); 58 | if (output == NULL) { 59 | fprintf(stderr, "Could not allocate enough bytes to read into memory\n"); 60 | return NULL; 61 | } 62 | if (fread(output, size, count, file) != count) { 63 | fprintf(stderr, "WARNING: Could not read as many bytes as requested from file\n"); 64 | } 65 | return output; 66 | } 67 | 68 | void* readFile(size_t size, size_t count, FILE* file) { 69 | void* output; 70 | output = malloc(size * count); 71 | if (output == NULL) { 72 | fprintf(stderr, "Could not allocate enough bytes to read into memory\n"); 73 | return NULL; 74 | } 75 | if (fread(output, size, count, file) != count) { 76 | fprintf(stderr, "WARNING: Could not read as many bytes as requested from file\n"); 77 | } 78 | return output; 79 | } 80 | 81 | uint8_t* readEncryptedOffset(uint8_t* key, uint64_t offset, size_t count, FILE* file) { 82 | uint8_t iv[16]; 83 | uint8_t* encrypted_chunk = (uint8_t*)readFileOffset(offset, sizeof(uint8_t), count, file); 84 | uint8_t* decrypted_chunk; 85 | if (encrypted_chunk == NULL) { 86 | fprintf(stderr, "Could not read encrypted chunk from file\n"); 87 | return NULL; 88 | } 89 | decrypted_chunk = (uint8_t*)malloc(count * sizeof(uint8_t)); 90 | if (decrypted_chunk == NULL) { 91 | fprintf(stderr, "Could not allocate enough memory to decrypt chunk\n"); 92 | free(encrypted_chunk); 93 | return NULL; 94 | } 95 | 96 | memset(iv, 0, 16); 97 | AES128_CBC_decrypt_buffer(decrypted_chunk, encrypted_chunk, count, key, iv); 98 | 99 | free(encrypted_chunk); 100 | return decrypted_chunk; 101 | } 102 | 103 | uint8_t* readVolumeEncryptedOffset(uint8_t* key, int64_t volume_offset, int64_t cluster_offset, int64_t file_offset, size_t size, FILE* file) { 104 | uint8_t iv[16]; 105 | uint8_t* encrypted_chunk; 106 | uint8_t* decrypted_chunk; 107 | uint8_t* output = (uint8_t*)malloc(size * sizeof(uint8_t)); 108 | int64_t buffer_location = 0; 109 | int64_t max_copy_size, copy_size, read_offset; 110 | struct block blockstruct; 111 | 112 | while (size > 0) { 113 | blockstruct.number = file_offset / 0x8000; 114 | blockstruct.offset = file_offset % 0x8000; 115 | 116 | read_offset = WIIU_DECRYPTED_AREA_OFFSET + volume_offset + cluster_offset + (blockstruct.number * 0x8000); 117 | encrypted_chunk = (uint8_t*)readFileOffset(read_offset, sizeof(uint8_t), 0x8000, file); 118 | if (encrypted_chunk == NULL) { 119 | fprintf(stderr, "Could not read encrypted chunk from file\n"); 120 | free(output); 121 | return NULL; 122 | } 123 | 124 | decrypted_chunk = (uint8_t*)malloc(0x8000 * sizeof(uint8_t)); 125 | if (decrypted_chunk == NULL) { 126 | fprintf(stderr, "Could not allocate enough memory to decrypt chunk\n"); 127 | free(encrypted_chunk); 128 | free(output); 129 | return NULL; 130 | } 131 | 132 | memset(iv, 0, 16); 133 | AES128_CBC_decrypt_buffer(decrypted_chunk, encrypted_chunk, 0x8000, key, iv); 134 | free(encrypted_chunk); 135 | 136 | max_copy_size = 0x8000 - blockstruct.offset; 137 | copy_size = (size > max_copy_size) ? max_copy_size : size; 138 | 139 | memcpy(output + buffer_location, decrypted_chunk + blockstruct.offset, copy_size); 140 | free(decrypted_chunk); 141 | 142 | size -= copy_size; 143 | buffer_location += copy_size; 144 | file_offset += copy_size; 145 | } 146 | 147 | return output; 148 | } 149 | 150 | struct partition_entry* create_partition_entry(uint8_t* raw_entry) { 151 | struct partition_entry* entry = (struct partition_entry*)malloc(sizeof(struct partition_entry)); 152 | 153 | if(raw_entry[0] == 1) { 154 | entry->is_directory = 1; 155 | entry->last_row_in_dir = bytesToUIntBE(raw_entry + 8); 156 | } else { 157 | entry->is_directory = 0; 158 | entry->size = bytesToUIntBE(raw_entry + 8); 159 | } 160 | 161 | entry->name_offset = bytesToUIntBE(raw_entry) & 0x00FFFFFF; 162 | 163 | entry->offset_in_cluster = (uint64_t)bytesToUIntBE(raw_entry + 4); 164 | entry->offset_in_cluster <<= 5; 165 | 166 | entry->unknown = bytesToUShortBE(raw_entry + 0x0C); 167 | entry->starting_cluster = bytesToUShortBE(raw_entry + 0x0E); 168 | 169 | return entry; 170 | } 171 | 172 | struct file* create_file(char* parent, char* filename, int64_t volume_base_offset, int64_t data_section_offset, int64_t lba, int64_t size, struct partition* source_partition, uint32_t entry_id) { 173 | struct file* file = (struct file*)malloc(sizeof(struct file)); 174 | 175 | strncpy(file->parent, parent, 511); 176 | strncpy(file->filename, filename, 511); 177 | file->parent[511] = '\0'; 178 | file->filename[511] = '\0'; 179 | file->volume_base_offset = volume_base_offset; 180 | file->data_section_offset = data_section_offset; 181 | file->lba = lba; 182 | file->size = size; 183 | file->parent_partition = source_partition; 184 | file->entry_id = entry_id; 185 | 186 | return file; 187 | } 188 | 189 | struct directory* create_directory(struct partition* source_partition, uint32_t* current_index, char* parent) { 190 | struct directory* dir = (struct directory*)malloc(sizeof(struct directory)); 191 | struct partition_entry* entry; 192 | struct directory* subdir; 193 | struct file* file; 194 | char next_dir[512]; 195 | uint32_t last_row_in_dir; 196 | 197 | utarray_new(dir->subdirs, &directory_icd); 198 | utarray_new(dir->files, &file_icd); 199 | 200 | if (strlen(parent) == 0) { 201 | strncpy(dir->parent, source_partition->name, PARTITION_TOC_ENTRY_SIZE); 202 | } else { 203 | strncpy(dir->parent, parent, 511); 204 | } 205 | dir->parent[511] = '\0'; 206 | 207 | strncpy(dir->directory_name, ((struct partition_entry*)utarray_eltptr(source_partition->entries, *current_index))->entry_name, 511); 208 | dir->directory_name[511] = '\0'; 209 | if (dir->parent[strlen(dir->parent) - 1] != '/') { 210 | sprintf(next_dir, "%s/%s", dir->parent, dir->directory_name); 211 | } else { 212 | sprintf(next_dir, "%s%s", dir->parent, dir->directory_name); 213 | } 214 | 215 | last_row_in_dir = ((struct partition_entry*)utarray_eltptr(source_partition->entries, *current_index))->last_row_in_dir; 216 | for (++(*current_index); *current_index < last_row_in_dir; (*current_index)++) { 217 | if (((struct partition_entry*)utarray_eltptr(source_partition->entries, *current_index))->is_directory) { 218 | subdir = create_directory(source_partition, current_index, next_dir); 219 | utarray_push_back(dir->subdirs, subdir); 220 | free(subdir); 221 | (*current_index)--; 222 | } else { 223 | entry = (struct partition_entry*)utarray_eltptr(source_partition->entries, *current_index); 224 | file = create_file(next_dir, entry->entry_name, source_partition->offset, source_partition->clusters[entry->starting_cluster].offset, entry->offset_in_cluster, entry->size, source_partition, *current_index); 225 | utarray_push_back(dir->files, file); 226 | free(file); 227 | } 228 | } 229 | 230 | return dir; 231 | } 232 | 233 | void extract_all(FILE* infile, struct directory* root_directory, char* outputdir) { 234 | if (makedir(outputdir) != 0) { 235 | if (errno != EEXIST) { 236 | fprintf(stderr, "Error: Output directory does not exist, cannot continue\n"); 237 | return; 238 | } 239 | } 240 | 241 | extract_dir(infile, root_directory, outputdir); 242 | } 243 | 244 | void extract_dir(FILE* infile, struct directory* dir, char* outputdir) { 245 | char fullout[1024]; 246 | struct directory* subdir; 247 | struct file* file; 248 | 249 | sprintf(fullout, "%s/%s/%s", outputdir, dir->parent, dir->directory_name); 250 | 251 | if (makedir(fullout) != 0) { 252 | if (errno != EEXIST) { 253 | fprintf(stderr, "Error: Could not create a directory, cannot continue\n"); 254 | return; 255 | } 256 | } 257 | 258 | for (file = (struct file*)utarray_front(dir->files); file != NULL; file = (struct file*)utarray_next(dir->files, file)) { 259 | extract_file(infile, file, outputdir); 260 | } 261 | for (subdir = (struct directory*)utarray_front(dir->subdirs); subdir != NULL; subdir = (struct directory*)utarray_next(dir->subdirs, subdir)) { 262 | extract_dir(infile, subdir, outputdir); 263 | } 264 | } 265 | 266 | void extract_file(FILE* infile, struct file* file, char* outputdir) { 267 | char fullout[1024]; 268 | uint8_t first_iv[16]; 269 | uint8_t* cluster_id; 270 | struct partition_entry* entry; 271 | 272 | sprintf(fullout, "%s/%s/%s", outputdir, file->parent, file->filename); 273 | printf("%s\n", fullout); 274 | 275 | memset(first_iv, 0, 16); 276 | entry = (struct partition_entry*)utarray_eltptr(file->parent_partition->entries, file->entry_id); 277 | cluster_id = (uint8_t*)(&(entry->starting_cluster)); 278 | first_iv[0] = cluster_id[1]; 279 | first_iv[1] = cluster_id[0]; 280 | 281 | if (entry->unknown == 0x0400 282 | || entry->unknown == 0x0040 283 | || (file->parent_partition->clusters[entry->starting_cluster].unknown1 == 0x00000400 284 | && file->parent_partition->clusters[entry->starting_cluster].unknown2 == 0x02000000)) { 285 | extract_file_hashed(infile, fullout, file->parent_partition->name, file->volume_base_offset, file->data_section_offset, file->lba, file->size, file->parent_partition->key, first_iv, entry->starting_cluster); 286 | } else { 287 | extract_file_unhashed(infile, fullout, file->parent_partition->name, file->volume_base_offset, file->data_section_offset, file->lba, file->size, file->parent_partition->key, first_iv); 288 | } 289 | } 290 | 291 | void extract_file_hashed(FILE* infile, char* outputpath, char* volumename, int64_t volume_offset, int64_t cluster_offset, int64_t file_offset, int64_t size, uint8_t* key, uint8_t* iv, uint16_t cluster_id) { 292 | uint8_t* encrypted_cluster; 293 | uint8_t* decrypted_header; 294 | uint8_t* decrypted_cluster; 295 | uint8_t cluster_iv[16]; 296 | uint8_t h0[0x14]; 297 | uint8_t block_sha1[0x14]; 298 | int64_t max_copy_size; 299 | int64_t copy_size; 300 | int64_t read_offset = 0; 301 | int64_t block_size = 0xFC00; 302 | int64_t iv_block = 0; 303 | struct block blockstruct; 304 | FILE* outfile; 305 | 306 | memset(block_sha1, 0, 0x14); 307 | 308 | outfile = fopen(outputpath, "w"); 309 | if (outfile == NULL) { 310 | fprintf(stderr, "Error: Cannot write output file, wasn't able to open it\n"); 311 | fprintf(stderr, "Error for \"%s\"", outputpath); 312 | return; 313 | } 314 | 315 | decrypted_header = (uint8_t*)malloc(0x400 * sizeof(uint8_t)); 316 | decrypted_cluster = (uint8_t*)malloc(block_size * sizeof(uint8_t)); 317 | while (size > 0) { 318 | blockstruct.number = file_offset / block_size; 319 | blockstruct.offset = file_offset % block_size; 320 | 321 | if(blockstruct.offset != (file_offset - (file_offset / block_size * block_size))) { 322 | blockstruct.offset = file_offset - (file_offset / block_size * block_size); 323 | } 324 | 325 | iv_block = blockstruct.number & 0xF; 326 | 327 | read_offset = WIIU_DECRYPTED_AREA_OFFSET + volume_offset + cluster_offset + (blockstruct.number * 0x10000); 328 | 329 | encrypted_cluster = readFileOffset(read_offset, sizeof(uint8_t), 0x400, infile); 330 | AES128_CBC_decrypt_buffer(decrypted_header, encrypted_cluster, 0x400, key, iv); 331 | free(encrypted_cluster); 332 | 333 | memcpy(cluster_iv, decrypted_header + (iv_block * 0x14), 16); 334 | memcpy(h0, decrypted_header + (iv_block * 0x14), 0x14); 335 | 336 | if (iv_block == 0) { 337 | cluster_iv[1] ^= (uint8_t)cluster_id; 338 | } 339 | 340 | encrypted_cluster = readFileOffset(read_offset + 0x400, sizeof(uint8_t), block_size, infile); 341 | AES128_CBC_decrypt_buffer(decrypted_cluster, encrypted_cluster, block_size, key, cluster_iv); 342 | free(encrypted_cluster); 343 | 344 | mbedtls_sha1(decrypted_cluster, block_size, block_sha1); 345 | if (iv_block == 0) { 346 | block_sha1[1] ^= (uint8_t)cluster_id; 347 | } 348 | 349 | if (memcmp(block_sha1, h0, 0x14) != 0) { 350 | fprintf(stderr, "Warning: Failed SHA1 checksum verification for %s\n", outputpath); 351 | } 352 | 353 | max_copy_size = block_size - blockstruct.offset; 354 | copy_size = (size > max_copy_size) ? max_copy_size : size; 355 | if (fwrite(decrypted_cluster + blockstruct.offset, sizeof(uint8_t), copy_size, outfile) != copy_size) { 356 | fprintf(stderr, "Warning: Couldn't write expected output for %s\n", outputpath); 357 | } 358 | 359 | size -= copy_size; 360 | file_offset += copy_size; 361 | } 362 | free(decrypted_header); 363 | free(decrypted_cluster); 364 | 365 | fclose(outfile); 366 | } 367 | 368 | void extract_file_unhashed(FILE* infile, char* outputpath, char* volumename, int64_t volume_offset, int64_t cluster_offset, int64_t file_offset, int64_t size, uint8_t* key, uint8_t* iv) { 369 | uint8_t* encrypted_cluster; 370 | uint8_t* decrypted_cluster; 371 | int64_t max_copy_size; 372 | int64_t copy_size; 373 | int64_t read_offset = 0; 374 | struct block blockstruct; 375 | FILE* outfile; 376 | 377 | outfile = fopen(outputpath, "w"); 378 | if (outfile == NULL) { 379 | fprintf(stderr, "Error: Cannot write output file, wasn't able to open it\n"); 380 | fprintf(stderr, "Error for \"%s\"", outputpath); 381 | return; 382 | } 383 | 384 | decrypted_cluster = (uint8_t*)malloc(0x8000 * sizeof(uint8_t)); 385 | while (size > 0) { 386 | blockstruct.number = file_offset / 0x8000; 387 | blockstruct.offset = file_offset % 0x8000; 388 | 389 | read_offset = WIIU_DECRYPTED_AREA_OFFSET + volume_offset + cluster_offset + (blockstruct.number * 0x8000); 390 | 391 | encrypted_cluster = readFileOffset(read_offset, sizeof(uint8_t), 0x8000, infile); 392 | AES128_CBC_decrypt_buffer(decrypted_cluster, encrypted_cluster, 0x8000, key, iv); 393 | free(encrypted_cluster); 394 | 395 | max_copy_size = 0x8000 - blockstruct.offset; 396 | copy_size = (size > max_copy_size) ? max_copy_size : size; 397 | if (fwrite(decrypted_cluster + blockstruct.offset, sizeof(uint8_t), copy_size, outfile) != copy_size) { 398 | fprintf(stderr, "Warning: Couldn't write expected output for\n%s\n", outputpath); 399 | } 400 | 401 | 402 | size -= copy_size; 403 | file_offset += copy_size; 404 | } 405 | free(decrypted_cluster); 406 | 407 | fclose(outfile); 408 | } 409 | 410 | int strincmp(const char *s1, const char *s2, int n) 411 | { 412 | /* case insensitive comparison */ 413 | int d; 414 | while (--n >= 0) { 415 | d = (tolower(*s1) - tolower(*s2)); 416 | if ( d != 0 || *s1 == '\0' || *s2 == '\0' ) 417 | return d; 418 | ++s1; 419 | ++s2; 420 | } 421 | return 0; 422 | } 423 | 424 | int titlekeycmp(const void* e1, const void* e2) { 425 | const struct titlekey* ele1 = (struct titlekey*)e1; 426 | const struct titlekey* ele2 = (struct titlekey*)e2; 427 | return strncmp(ele1->name, ele2->name, 18); 428 | } 429 | 430 | uint16_t bytesToUShortBE(uint8_t* bytes) { 431 | return (bytes[0] << 8) | bytes[1]; 432 | } 433 | 434 | uint32_t bytesToUIntBE(uint8_t* bytes) { 435 | return (bytes[0] << 24) | (bytes[1] << 16) | (bytes[2] << 8) | bytes[3]; 436 | } 437 | -------------------------------------------------------------------------------- /aes.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | This is an implementation of the AES128 algorithm, specifically ECB and CBC mode. 4 | 5 | The implementation is verified against the test vectors in: 6 | National Institute of Standards and Technology Special Publication 800-38A 2001 ED 7 | 8 | ECB-AES128 9 | ---------- 10 | 11 | plain-text: 12 | 6bc1bee22e409f96e93d7e117393172a 13 | ae2d8a571e03ac9c9eb76fac45af8e51 14 | 30c81c46a35ce411e5fbc1191a0a52ef 15 | f69f2445df4f9b17ad2b417be66c3710 16 | 17 | key: 18 | 2b7e151628aed2a6abf7158809cf4f3c 19 | 20 | resulting cipher 21 | 3ad77bb40d7a3660a89ecaf32466ef97 22 | f5d3d58503b9699de785895a96fdbaaf 23 | 43b1cd7f598ece23881b00e3ed030688 24 | 7b0c785e27e8ad3f8223207104725dd4 25 | 26 | 27 | NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0) 28 | You should pad the end of the string with zeros if this is not the case. 29 | 30 | */ 31 | 32 | 33 | /*****************************************************************************/ 34 | /* Includes: */ 35 | /*****************************************************************************/ 36 | #include 37 | #include // CBC mode, for memset 38 | #include "aes.h" 39 | 40 | 41 | /*****************************************************************************/ 42 | /* Defines: */ 43 | /*****************************************************************************/ 44 | // The number of columns comprising a state in AES. This is a constant in AES. Value=4 45 | #define Nb 4 46 | // The number of 32 bit words in a key. 47 | #define Nk 4 48 | // Key length in bytes [128 bit] 49 | #define KEYLEN 16 50 | // The number of rounds in AES Cipher. 51 | #define Nr 10 52 | 53 | // jcallan@github points out that declaring Multiply as a function 54 | // reduces code size considerably with the Keil ARM compiler. 55 | // See this link for more information: https://github.com/kokke/tiny-AES128-C/pull/3 56 | #ifndef MULTIPLY_AS_A_FUNCTION 57 | #define MULTIPLY_AS_A_FUNCTION 0 58 | #endif 59 | 60 | 61 | /*****************************************************************************/ 62 | /* Private variables: */ 63 | /*****************************************************************************/ 64 | // state - array holding the intermediate results during decryption. 65 | typedef uint8_t state_t[4][4]; 66 | static state_t* state; 67 | 68 | // The array that stores the round keys. 69 | static uint8_t RoundKey[176]; 70 | 71 | // The Key input to the AES Program 72 | static const uint8_t* Key; 73 | 74 | #if defined(CBC) && CBC 75 | // Initial Vector used only for CBC mode 76 | static uint8_t* Iv; 77 | #endif 78 | 79 | // The lookup-tables are marked const so they can be placed in read-only storage instead of RAM 80 | // The numbers below can be computed dynamically trading ROM for RAM - 81 | // This can be useful in (embedded) bootloader applications, where ROM is often limited. 82 | static const uint8_t sbox[256] = { 83 | //0 1 2 3 4 5 6 7 8 9 A B C D E F 84 | 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, 85 | 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 86 | 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, 87 | 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, 88 | 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 89 | 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, 90 | 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, 91 | 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 92 | 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, 93 | 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, 94 | 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 95 | 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, 96 | 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, 97 | 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 98 | 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, 99 | 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; 100 | 101 | static const uint8_t rsbox[256] = 102 | { 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, 103 | 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 104 | 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, 105 | 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, 106 | 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 107 | 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, 108 | 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, 109 | 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 110 | 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, 111 | 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, 112 | 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 113 | 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, 114 | 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, 115 | 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 116 | 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, 117 | 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; 118 | 119 | 120 | // The round constant word array, Rcon[i], contains the values given by 121 | // x to th e power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8) 122 | // Note that i starts at 1, not 0). 123 | static const uint8_t Rcon[255] = { 124 | 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 125 | 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 126 | 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 127 | 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 128 | 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 129 | 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 130 | 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 131 | 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 132 | 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 133 | 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 134 | 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 135 | 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 136 | 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 137 | 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 138 | 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 139 | 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb }; 140 | 141 | 142 | /*****************************************************************************/ 143 | /* Private functions: */ 144 | /*****************************************************************************/ 145 | static uint8_t getSBoxValue(uint8_t num) 146 | { 147 | return sbox[num]; 148 | } 149 | 150 | static uint8_t getSBoxInvert(uint8_t num) 151 | { 152 | return rsbox[num]; 153 | } 154 | 155 | // This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states. 156 | static void KeyExpansion(void) 157 | { 158 | uint32_t i, j, k; 159 | uint8_t tempa[4]; // Used for the column/row operations 160 | 161 | // The first round key is the key itself. 162 | for(i = 0; i < Nk; ++i) 163 | { 164 | RoundKey[(i * 4) + 0] = Key[(i * 4) + 0]; 165 | RoundKey[(i * 4) + 1] = Key[(i * 4) + 1]; 166 | RoundKey[(i * 4) + 2] = Key[(i * 4) + 2]; 167 | RoundKey[(i * 4) + 3] = Key[(i * 4) + 3]; 168 | } 169 | 170 | // All other round keys are found from the previous round keys. 171 | for(; (i < (Nb * (Nr + 1))); ++i) 172 | { 173 | for(j = 0; j < 4; ++j) 174 | { 175 | tempa[j]=RoundKey[(i-1) * 4 + j]; 176 | } 177 | if (i % Nk == 0) 178 | { 179 | // This function rotates the 4 bytes in a word to the left once. 180 | // [a0,a1,a2,a3] becomes [a1,a2,a3,a0] 181 | 182 | // Function RotWord() 183 | { 184 | k = tempa[0]; 185 | tempa[0] = tempa[1]; 186 | tempa[1] = tempa[2]; 187 | tempa[2] = tempa[3]; 188 | tempa[3] = k; 189 | } 190 | 191 | // SubWord() is a function that takes a four-byte input word and 192 | // applies the S-box to each of the four bytes to produce an output word. 193 | 194 | // Function Subword() 195 | { 196 | tempa[0] = getSBoxValue(tempa[0]); 197 | tempa[1] = getSBoxValue(tempa[1]); 198 | tempa[2] = getSBoxValue(tempa[2]); 199 | tempa[3] = getSBoxValue(tempa[3]); 200 | } 201 | 202 | tempa[0] = tempa[0] ^ Rcon[i/Nk]; 203 | } 204 | else if (Nk > 6 && i % Nk == 4) 205 | { 206 | // Function Subword() 207 | { 208 | tempa[0] = getSBoxValue(tempa[0]); 209 | tempa[1] = getSBoxValue(tempa[1]); 210 | tempa[2] = getSBoxValue(tempa[2]); 211 | tempa[3] = getSBoxValue(tempa[3]); 212 | } 213 | } 214 | RoundKey[i * 4 + 0] = RoundKey[(i - Nk) * 4 + 0] ^ tempa[0]; 215 | RoundKey[i * 4 + 1] = RoundKey[(i - Nk) * 4 + 1] ^ tempa[1]; 216 | RoundKey[i * 4 + 2] = RoundKey[(i - Nk) * 4 + 2] ^ tempa[2]; 217 | RoundKey[i * 4 + 3] = RoundKey[(i - Nk) * 4 + 3] ^ tempa[3]; 218 | } 219 | } 220 | 221 | // This function adds the round key to state. 222 | // The round key is added to the state by an XOR function. 223 | static void AddRoundKey(uint8_t round) 224 | { 225 | uint8_t i,j; 226 | for(i=0;i<4;++i) 227 | { 228 | for(j = 0; j < 4; ++j) 229 | { 230 | (*state)[i][j] ^= RoundKey[round * Nb * 4 + i * Nb + j]; 231 | } 232 | } 233 | } 234 | 235 | // The SubBytes Function Substitutes the values in the 236 | // state matrix with values in an S-box. 237 | static void SubBytes(void) 238 | { 239 | uint8_t i, j; 240 | for(i = 0; i < 4; ++i) 241 | { 242 | for(j = 0; j < 4; ++j) 243 | { 244 | (*state)[j][i] = getSBoxValue((*state)[j][i]); 245 | } 246 | } 247 | } 248 | 249 | // The ShiftRows() function shifts the rows in the state to the left. 250 | // Each row is shifted with different offset. 251 | // Offset = Row number. So the first row is not shifted. 252 | static void ShiftRows(void) 253 | { 254 | uint8_t temp; 255 | 256 | // Rotate first row 1 columns to left 257 | temp = (*state)[0][1]; 258 | (*state)[0][1] = (*state)[1][1]; 259 | (*state)[1][1] = (*state)[2][1]; 260 | (*state)[2][1] = (*state)[3][1]; 261 | (*state)[3][1] = temp; 262 | 263 | // Rotate second row 2 columns to left 264 | temp = (*state)[0][2]; 265 | (*state)[0][2] = (*state)[2][2]; 266 | (*state)[2][2] = temp; 267 | 268 | temp = (*state)[1][2]; 269 | (*state)[1][2] = (*state)[3][2]; 270 | (*state)[3][2] = temp; 271 | 272 | // Rotate third row 3 columns to left 273 | temp = (*state)[0][3]; 274 | (*state)[0][3] = (*state)[3][3]; 275 | (*state)[3][3] = (*state)[2][3]; 276 | (*state)[2][3] = (*state)[1][3]; 277 | (*state)[1][3] = temp; 278 | } 279 | 280 | static uint8_t xtime(uint8_t x) 281 | { 282 | return ((x<<1) ^ (((x>>7) & 1) * 0x1b)); 283 | } 284 | 285 | // MixColumns function mixes the columns of the state matrix 286 | static void MixColumns(void) 287 | { 288 | uint8_t i; 289 | uint8_t Tmp,Tm,t; 290 | for(i = 0; i < 4; ++i) 291 | { 292 | t = (*state)[i][0]; 293 | Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ; 294 | Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ; 295 | Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ; 296 | Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ; 297 | Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ; 298 | } 299 | } 300 | 301 | // Multiply is used to multiply numbers in the field GF(2^8) 302 | #if MULTIPLY_AS_A_FUNCTION 303 | static uint8_t Multiply(uint8_t x, uint8_t y) 304 | { 305 | return (((y & 1) * x) ^ 306 | ((y>>1 & 1) * xtime(x)) ^ 307 | ((y>>2 & 1) * xtime(xtime(x))) ^ 308 | ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ 309 | ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))); 310 | } 311 | #else 312 | #define Multiply(x, y) \ 313 | ( ((y & 1) * x) ^ \ 314 | ((y>>1 & 1) * xtime(x)) ^ \ 315 | ((y>>2 & 1) * xtime(xtime(x))) ^ \ 316 | ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \ 317 | ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \ 318 | 319 | #endif 320 | 321 | // MixColumns function mixes the columns of the state matrix. 322 | // The method used to multiply may be difficult to understand for the inexperienced. 323 | // Please use the references to gain more information. 324 | static void InvMixColumns(void) 325 | { 326 | int i; 327 | uint8_t a,b,c,d; 328 | for(i=0;i<4;++i) 329 | { 330 | a = (*state)[i][0]; 331 | b = (*state)[i][1]; 332 | c = (*state)[i][2]; 333 | d = (*state)[i][3]; 334 | 335 | (*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09); 336 | (*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d); 337 | (*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b); 338 | (*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e); 339 | } 340 | } 341 | 342 | 343 | // The SubBytes Function Substitutes the values in the 344 | // state matrix with values in an S-box. 345 | static void InvSubBytes(void) 346 | { 347 | uint8_t i,j; 348 | for(i=0;i<4;++i) 349 | { 350 | for(j=0;j<4;++j) 351 | { 352 | (*state)[j][i] = getSBoxInvert((*state)[j][i]); 353 | } 354 | } 355 | } 356 | 357 | static void InvShiftRows(void) 358 | { 359 | uint8_t temp; 360 | 361 | // Rotate first row 1 columns to right 362 | temp=(*state)[3][1]; 363 | (*state)[3][1]=(*state)[2][1]; 364 | (*state)[2][1]=(*state)[1][1]; 365 | (*state)[1][1]=(*state)[0][1]; 366 | (*state)[0][1]=temp; 367 | 368 | // Rotate second row 2 columns to right 369 | temp=(*state)[0][2]; 370 | (*state)[0][2]=(*state)[2][2]; 371 | (*state)[2][2]=temp; 372 | 373 | temp=(*state)[1][2]; 374 | (*state)[1][2]=(*state)[3][2]; 375 | (*state)[3][2]=temp; 376 | 377 | // Rotate third row 3 columns to right 378 | temp=(*state)[0][3]; 379 | (*state)[0][3]=(*state)[1][3]; 380 | (*state)[1][3]=(*state)[2][3]; 381 | (*state)[2][3]=(*state)[3][3]; 382 | (*state)[3][3]=temp; 383 | } 384 | 385 | 386 | // Cipher is the main function that encrypts the PlainText. 387 | static void Cipher(void) 388 | { 389 | uint8_t round = 0; 390 | 391 | // Add the First round key to the state before starting the rounds. 392 | AddRoundKey(0); 393 | 394 | // There will be Nr rounds. 395 | // The first Nr-1 rounds are identical. 396 | // These Nr-1 rounds are executed in the loop below. 397 | for(round = 1; round < Nr; ++round) 398 | { 399 | SubBytes(); 400 | ShiftRows(); 401 | MixColumns(); 402 | AddRoundKey(round); 403 | } 404 | 405 | // The last round is given below. 406 | // The MixColumns function is not here in the last round. 407 | SubBytes(); 408 | ShiftRows(); 409 | AddRoundKey(Nr); 410 | } 411 | 412 | static void InvCipher(void) 413 | { 414 | uint8_t round=0; 415 | 416 | // Add the First round key to the state before starting the rounds. 417 | AddRoundKey(Nr); 418 | 419 | // There will be Nr rounds. 420 | // The first Nr-1 rounds are identical. 421 | // These Nr-1 rounds are executed in the loop below. 422 | for(round=Nr-1;round>0;round--) 423 | { 424 | InvShiftRows(); 425 | InvSubBytes(); 426 | AddRoundKey(round); 427 | InvMixColumns(); 428 | } 429 | 430 | // The last round is given below. 431 | // The MixColumns function is not here in the last round. 432 | InvShiftRows(); 433 | InvSubBytes(); 434 | AddRoundKey(0); 435 | } 436 | 437 | static void BlockCopy(uint8_t* output, uint8_t* input) 438 | { 439 | uint8_t i; 440 | for (i=0;i 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | --------------------------------------------------------------------------------