├── LICENSE ├── Makefile ├── README.md ├── aes.c ├── aes.h ├── common.h ├── fpkg_rename.c ├── genidx.c ├── mingw_mmap.c ├── mingw_mmap.h ├── pupunpack.c ├── sha1.c ├── sha1.h ├── sha2.c ├── sha2.h ├── tools.c ├── tools.h ├── trophy.c ├── trp_resigner.c ├── types.h ├── undat.c ├── unpfs.c └── unpkg.c /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 António Sarmento 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC ?= gcc 2 | CFLAGS += -g -O3 -W -D"_LARGEFILE64_SOURCE" -D"_FILE_OFFSET_BITS=64" -D"__MSVCRT__" -D"__USE_MINGW_FSEEK" 3 | LDLIBS = -lz 4 | FILES = pupunpack unpkg unpfs trophy trp_resigner genidx undat fpkg_rename 5 | COMMON = sha2.o mingw_mmap.o tools.o aes.o sha1.o 6 | DEPS = Makefile sha2.h 7 | 8 | OBJS = $(COMMON) $(addsuffix .o, $(FILES)) 9 | 10 | all: $(FILES) 11 | 12 | $(FILES): %: %.o $(COMMON) $(DEPS) 13 | $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< $(COMMON) $(LDLIBS) 14 | 15 | $(OBJS): %.o: %.c $(DEPS) 16 | $(CC) $(CFLAGS) -c -o $@ $< 17 | 18 | clean: 19 | rm -f $(OBJS) $(FILES) *.exe *~ 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ps4tools 2 | ======== 3 | 4 | My collection of tools for PS4 file handling. 5 | 6 | Credits 7 | ------- 8 | 9 | flat_z (original Python scripts for PUP and PKG unpacking) 10 | 11 | zecoxao (updates and bug fixes) 12 | 13 | CrazyVoid (for genidx sources) 14 | 15 | zecoxao (for undat sources) 16 | 17 | CHANGELOG 18 | -------- 19 | 20 | * First Release 21 | 22 | - pupunpack: splits PS4UPDATE.PUP and exposes inner PUP files (encrypted). 23 | - unpkg: unpacks retail/debug PKG files while collecting data and dumping internal files (mostly a C port of flat_z's Python script, at the moment). 24 | - unpfs: unpacks pfs images 25 | - trophy: unpack trophy files (incl. bruteforce for npcommid) 26 | - genidx: generate PS4 IDX File 27 | - undat: index.dat decrypter for ps4 28 | - fpkg_rename: Renames fpkg into following format $TITLE - ($TITLE_ID) ($VERSION).pkg 29 | -------------------------------------------------------------------------------- /aes.c: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | * aes.c - integrated in QEMU by Fabrice Bellard from the OpenSSL project. 4 | */ 5 | /* 6 | * rijndael-alg-fst.c 7 | * 8 | * @version 3.0 (December 2000) 9 | * 10 | * Optimised ANSI C code for the Rijndael cipher (now AES) 11 | * 12 | * @author Vincent Rijmen 13 | * @author Antoon Bosselaers 14 | * @author Paulo Barreto 15 | * 16 | * This code is hereby placed in the public domain. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS 19 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 25 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 27 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 28 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | 32 | #include "tools.h" 33 | #include "aes.h" 34 | 35 | #define MAXKC (256/32) 36 | #define MAXKB (256/8) 37 | #define MAXNR 14 38 | 39 | /* This controls loop-unrolling in aes_core.c */ 40 | #undef FULL_UNROLL 41 | # define GETU32(pt) (((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3])) 42 | # define PUTU32(ct, st) { (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); (ct)[2] = (u8)((st) >> 8); (ct)[3] = (u8)(st); } 43 | 44 | /* 45 | Te0[x] = S [x].[02, 01, 01, 03]; 46 | Te1[x] = S [x].[03, 02, 01, 01]; 47 | Te2[x] = S [x].[01, 03, 02, 01]; 48 | Te3[x] = S [x].[01, 01, 03, 02]; 49 | Te4[x] = S [x].[01, 01, 01, 01]; 50 | 51 | Td0[x] = Si[x].[0e, 09, 0d, 0b]; 52 | Td1[x] = Si[x].[0b, 0e, 09, 0d]; 53 | Td2[x] = Si[x].[0d, 0b, 0e, 09]; 54 | Td3[x] = Si[x].[09, 0d, 0b, 0e]; 55 | Td4[x] = Si[x].[01, 01, 01, 01]; 56 | */ 57 | 58 | static const u32 Te0[256] = { 59 | 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU, 60 | 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U, 61 | 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU, 62 | 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU, 63 | 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U, 64 | 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU, 65 | 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU, 66 | 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU, 67 | 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU, 68 | 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU, 69 | 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U, 70 | 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU, 71 | 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU, 72 | 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U, 73 | 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU, 74 | 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU, 75 | 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU, 76 | 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU, 77 | 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU, 78 | 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U, 79 | 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU, 80 | 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU, 81 | 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU, 82 | 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU, 83 | 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U, 84 | 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U, 85 | 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U, 86 | 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U, 87 | 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU, 88 | 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U, 89 | 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U, 90 | 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU, 91 | 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU, 92 | 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U, 93 | 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U, 94 | 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U, 95 | 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU, 96 | 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U, 97 | 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU, 98 | 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U, 99 | 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU, 100 | 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U, 101 | 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U, 102 | 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU, 103 | 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U, 104 | 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U, 105 | 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U, 106 | 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U, 107 | 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U, 108 | 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U, 109 | 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U, 110 | 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U, 111 | 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU, 112 | 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U, 113 | 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U, 114 | 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U, 115 | 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U, 116 | 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U, 117 | 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U, 118 | 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU, 119 | 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U, 120 | 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U, 121 | 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U, 122 | 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU, 123 | }; 124 | static const u32 Te1[256] = { 125 | 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU, 126 | 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U, 127 | 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU, 128 | 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U, 129 | 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU, 130 | 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U, 131 | 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU, 132 | 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U, 133 | 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U, 134 | 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU, 135 | 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U, 136 | 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U, 137 | 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U, 138 | 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU, 139 | 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U, 140 | 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U, 141 | 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU, 142 | 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U, 143 | 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U, 144 | 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U, 145 | 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU, 146 | 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU, 147 | 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U, 148 | 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU, 149 | 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU, 150 | 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U, 151 | 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU, 152 | 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U, 153 | 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU, 154 | 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U, 155 | 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U, 156 | 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U, 157 | 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU, 158 | 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U, 159 | 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU, 160 | 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U, 161 | 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU, 162 | 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U, 163 | 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U, 164 | 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU, 165 | 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU, 166 | 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU, 167 | 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U, 168 | 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U, 169 | 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU, 170 | 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U, 171 | 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU, 172 | 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U, 173 | 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU, 174 | 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U, 175 | 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU, 176 | 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU, 177 | 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U, 178 | 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU, 179 | 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U, 180 | 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU, 181 | 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U, 182 | 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U, 183 | 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U, 184 | 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU, 185 | 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU, 186 | 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U, 187 | 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU, 188 | 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U, 189 | }; 190 | static const u32 Te2[256] = { 191 | 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU, 192 | 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U, 193 | 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU, 194 | 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U, 195 | 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU, 196 | 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U, 197 | 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU, 198 | 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U, 199 | 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U, 200 | 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU, 201 | 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U, 202 | 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U, 203 | 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U, 204 | 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU, 205 | 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U, 206 | 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U, 207 | 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU, 208 | 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U, 209 | 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U, 210 | 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U, 211 | 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU, 212 | 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU, 213 | 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U, 214 | 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU, 215 | 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU, 216 | 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U, 217 | 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU, 218 | 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U, 219 | 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU, 220 | 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U, 221 | 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U, 222 | 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U, 223 | 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU, 224 | 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U, 225 | 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU, 226 | 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U, 227 | 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU, 228 | 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U, 229 | 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U, 230 | 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU, 231 | 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU, 232 | 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU, 233 | 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U, 234 | 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U, 235 | 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU, 236 | 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U, 237 | 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU, 238 | 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U, 239 | 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU, 240 | 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U, 241 | 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU, 242 | 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU, 243 | 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U, 244 | 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU, 245 | 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U, 246 | 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU, 247 | 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U, 248 | 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U, 249 | 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U, 250 | 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU, 251 | 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU, 252 | 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U, 253 | 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU, 254 | 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U, 255 | }; 256 | static const u32 Te3[256] = { 257 | 258 | 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U, 259 | 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U, 260 | 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U, 261 | 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU, 262 | 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU, 263 | 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU, 264 | 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U, 265 | 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU, 266 | 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU, 267 | 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U, 268 | 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U, 269 | 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU, 270 | 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU, 271 | 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU, 272 | 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU, 273 | 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU, 274 | 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U, 275 | 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU, 276 | 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU, 277 | 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U, 278 | 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U, 279 | 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U, 280 | 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U, 281 | 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U, 282 | 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU, 283 | 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U, 284 | 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU, 285 | 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU, 286 | 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U, 287 | 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U, 288 | 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U, 289 | 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU, 290 | 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U, 291 | 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU, 292 | 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU, 293 | 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U, 294 | 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U, 295 | 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU, 296 | 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U, 297 | 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU, 298 | 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U, 299 | 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U, 300 | 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U, 301 | 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U, 302 | 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU, 303 | 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U, 304 | 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU, 305 | 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U, 306 | 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU, 307 | 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U, 308 | 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU, 309 | 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU, 310 | 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU, 311 | 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU, 312 | 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U, 313 | 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U, 314 | 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U, 315 | 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U, 316 | 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U, 317 | 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U, 318 | 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU, 319 | 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U, 320 | 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU, 321 | 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU, 322 | }; 323 | static const u32 Te4[256] = { 324 | 0x63636363U, 0x7c7c7c7cU, 0x77777777U, 0x7b7b7b7bU, 325 | 0xf2f2f2f2U, 0x6b6b6b6bU, 0x6f6f6f6fU, 0xc5c5c5c5U, 326 | 0x30303030U, 0x01010101U, 0x67676767U, 0x2b2b2b2bU, 327 | 0xfefefefeU, 0xd7d7d7d7U, 0xababababU, 0x76767676U, 328 | 0xcacacacaU, 0x82828282U, 0xc9c9c9c9U, 0x7d7d7d7dU, 329 | 0xfafafafaU, 0x59595959U, 0x47474747U, 0xf0f0f0f0U, 330 | 0xadadadadU, 0xd4d4d4d4U, 0xa2a2a2a2U, 0xafafafafU, 331 | 0x9c9c9c9cU, 0xa4a4a4a4U, 0x72727272U, 0xc0c0c0c0U, 332 | 0xb7b7b7b7U, 0xfdfdfdfdU, 0x93939393U, 0x26262626U, 333 | 0x36363636U, 0x3f3f3f3fU, 0xf7f7f7f7U, 0xccccccccU, 334 | 0x34343434U, 0xa5a5a5a5U, 0xe5e5e5e5U, 0xf1f1f1f1U, 335 | 0x71717171U, 0xd8d8d8d8U, 0x31313131U, 0x15151515U, 336 | 0x04040404U, 0xc7c7c7c7U, 0x23232323U, 0xc3c3c3c3U, 337 | 0x18181818U, 0x96969696U, 0x05050505U, 0x9a9a9a9aU, 338 | 0x07070707U, 0x12121212U, 0x80808080U, 0xe2e2e2e2U, 339 | 0xebebebebU, 0x27272727U, 0xb2b2b2b2U, 0x75757575U, 340 | 0x09090909U, 0x83838383U, 0x2c2c2c2cU, 0x1a1a1a1aU, 341 | 0x1b1b1b1bU, 0x6e6e6e6eU, 0x5a5a5a5aU, 0xa0a0a0a0U, 342 | 0x52525252U, 0x3b3b3b3bU, 0xd6d6d6d6U, 0xb3b3b3b3U, 343 | 0x29292929U, 0xe3e3e3e3U, 0x2f2f2f2fU, 0x84848484U, 344 | 0x53535353U, 0xd1d1d1d1U, 0x00000000U, 0xededededU, 345 | 0x20202020U, 0xfcfcfcfcU, 0xb1b1b1b1U, 0x5b5b5b5bU, 346 | 0x6a6a6a6aU, 0xcbcbcbcbU, 0xbebebebeU, 0x39393939U, 347 | 0x4a4a4a4aU, 0x4c4c4c4cU, 0x58585858U, 0xcfcfcfcfU, 348 | 0xd0d0d0d0U, 0xefefefefU, 0xaaaaaaaaU, 0xfbfbfbfbU, 349 | 0x43434343U, 0x4d4d4d4dU, 0x33333333U, 0x85858585U, 350 | 0x45454545U, 0xf9f9f9f9U, 0x02020202U, 0x7f7f7f7fU, 351 | 0x50505050U, 0x3c3c3c3cU, 0x9f9f9f9fU, 0xa8a8a8a8U, 352 | 0x51515151U, 0xa3a3a3a3U, 0x40404040U, 0x8f8f8f8fU, 353 | 0x92929292U, 0x9d9d9d9dU, 0x38383838U, 0xf5f5f5f5U, 354 | 0xbcbcbcbcU, 0xb6b6b6b6U, 0xdadadadaU, 0x21212121U, 355 | 0x10101010U, 0xffffffffU, 0xf3f3f3f3U, 0xd2d2d2d2U, 356 | 0xcdcdcdcdU, 0x0c0c0c0cU, 0x13131313U, 0xececececU, 357 | 0x5f5f5f5fU, 0x97979797U, 0x44444444U, 0x17171717U, 358 | 0xc4c4c4c4U, 0xa7a7a7a7U, 0x7e7e7e7eU, 0x3d3d3d3dU, 359 | 0x64646464U, 0x5d5d5d5dU, 0x19191919U, 0x73737373U, 360 | 0x60606060U, 0x81818181U, 0x4f4f4f4fU, 0xdcdcdcdcU, 361 | 0x22222222U, 0x2a2a2a2aU, 0x90909090U, 0x88888888U, 362 | 0x46464646U, 0xeeeeeeeeU, 0xb8b8b8b8U, 0x14141414U, 363 | 0xdedededeU, 0x5e5e5e5eU, 0x0b0b0b0bU, 0xdbdbdbdbU, 364 | 0xe0e0e0e0U, 0x32323232U, 0x3a3a3a3aU, 0x0a0a0a0aU, 365 | 0x49494949U, 0x06060606U, 0x24242424U, 0x5c5c5c5cU, 366 | 0xc2c2c2c2U, 0xd3d3d3d3U, 0xacacacacU, 0x62626262U, 367 | 0x91919191U, 0x95959595U, 0xe4e4e4e4U, 0x79797979U, 368 | 0xe7e7e7e7U, 0xc8c8c8c8U, 0x37373737U, 0x6d6d6d6dU, 369 | 0x8d8d8d8dU, 0xd5d5d5d5U, 0x4e4e4e4eU, 0xa9a9a9a9U, 370 | 0x6c6c6c6cU, 0x56565656U, 0xf4f4f4f4U, 0xeaeaeaeaU, 371 | 0x65656565U, 0x7a7a7a7aU, 0xaeaeaeaeU, 0x08080808U, 372 | 0xbabababaU, 0x78787878U, 0x25252525U, 0x2e2e2e2eU, 373 | 0x1c1c1c1cU, 0xa6a6a6a6U, 0xb4b4b4b4U, 0xc6c6c6c6U, 374 | 0xe8e8e8e8U, 0xddddddddU, 0x74747474U, 0x1f1f1f1fU, 375 | 0x4b4b4b4bU, 0xbdbdbdbdU, 0x8b8b8b8bU, 0x8a8a8a8aU, 376 | 0x70707070U, 0x3e3e3e3eU, 0xb5b5b5b5U, 0x66666666U, 377 | 0x48484848U, 0x03030303U, 0xf6f6f6f6U, 0x0e0e0e0eU, 378 | 0x61616161U, 0x35353535U, 0x57575757U, 0xb9b9b9b9U, 379 | 0x86868686U, 0xc1c1c1c1U, 0x1d1d1d1dU, 0x9e9e9e9eU, 380 | 0xe1e1e1e1U, 0xf8f8f8f8U, 0x98989898U, 0x11111111U, 381 | 0x69696969U, 0xd9d9d9d9U, 0x8e8e8e8eU, 0x94949494U, 382 | 0x9b9b9b9bU, 0x1e1e1e1eU, 0x87878787U, 0xe9e9e9e9U, 383 | 0xcecececeU, 0x55555555U, 0x28282828U, 0xdfdfdfdfU, 384 | 0x8c8c8c8cU, 0xa1a1a1a1U, 0x89898989U, 0x0d0d0d0dU, 385 | 0xbfbfbfbfU, 0xe6e6e6e6U, 0x42424242U, 0x68686868U, 386 | 0x41414141U, 0x99999999U, 0x2d2d2d2dU, 0x0f0f0f0fU, 387 | 0xb0b0b0b0U, 0x54545454U, 0xbbbbbbbbU, 0x16161616U, 388 | }; 389 | static const u32 Td0[256] = { 390 | 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U, 391 | 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U, 392 | 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U, 393 | 0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU, 394 | 0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U, 395 | 0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U, 396 | 0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU, 397 | 0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U, 398 | 0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU, 399 | 0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U, 400 | 0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U, 401 | 0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U, 402 | 0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U, 403 | 0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU, 404 | 0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U, 405 | 0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU, 406 | 0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U, 407 | 0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU, 408 | 0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U, 409 | 0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U, 410 | 0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U, 411 | 0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU, 412 | 0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U, 413 | 0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU, 414 | 0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U, 415 | 0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU, 416 | 0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U, 417 | 0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU, 418 | 0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU, 419 | 0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U, 420 | 0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU, 421 | 0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U, 422 | 0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU, 423 | 0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U, 424 | 0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U, 425 | 0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U, 426 | 0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU, 427 | 0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U, 428 | 0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U, 429 | 0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU, 430 | 0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U, 431 | 0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U, 432 | 0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U, 433 | 0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U, 434 | 0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U, 435 | 0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU, 436 | 0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U, 437 | 0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U, 438 | 0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U, 439 | 0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U, 440 | 0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U, 441 | 0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU, 442 | 0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU, 443 | 0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU, 444 | 0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU, 445 | 0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U, 446 | 0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U, 447 | 0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU, 448 | 0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU, 449 | 0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U, 450 | 0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU, 451 | 0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U, 452 | 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U, 453 | 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U, 454 | }; 455 | static const u32 Td1[256] = { 456 | 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU, 457 | 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U, 458 | 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU, 459 | 0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U, 460 | 0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U, 461 | 0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U, 462 | 0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U, 463 | 0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U, 464 | 0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U, 465 | 0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU, 466 | 0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU, 467 | 0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU, 468 | 0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U, 469 | 0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU, 470 | 0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U, 471 | 0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U, 472 | 0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U, 473 | 0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU, 474 | 0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU, 475 | 0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U, 476 | 0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU, 477 | 0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U, 478 | 0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU, 479 | 0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU, 480 | 0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U, 481 | 0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U, 482 | 0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U, 483 | 0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU, 484 | 0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U, 485 | 0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU, 486 | 0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U, 487 | 0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U, 488 | 0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U, 489 | 0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU, 490 | 0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U, 491 | 0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U, 492 | 0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U, 493 | 0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U, 494 | 0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U, 495 | 0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U, 496 | 0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU, 497 | 0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU, 498 | 0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U, 499 | 0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU, 500 | 0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U, 501 | 0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU, 502 | 0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU, 503 | 0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U, 504 | 0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU, 505 | 0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U, 506 | 0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U, 507 | 0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U, 508 | 0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U, 509 | 0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U, 510 | 0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U, 511 | 0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U, 512 | 0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU, 513 | 0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U, 514 | 0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U, 515 | 0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU, 516 | 0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U, 517 | 0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U, 518 | 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U, 519 | 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U, 520 | }; 521 | static const u32 Td2[256] = { 522 | 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U, 523 | 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U, 524 | 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U, 525 | 0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U, 526 | 0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU, 527 | 0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U, 528 | 0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U, 529 | 0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U, 530 | 0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U, 531 | 0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU, 532 | 0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U, 533 | 0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U, 534 | 0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU, 535 | 0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U, 536 | 0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U, 537 | 0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U, 538 | 0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U, 539 | 0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U, 540 | 0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U, 541 | 0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU, 542 | 543 | 0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U, 544 | 0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U, 545 | 0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U, 546 | 0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U, 547 | 0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U, 548 | 0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU, 549 | 0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU, 550 | 0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U, 551 | 0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU, 552 | 0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U, 553 | 0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU, 554 | 0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU, 555 | 0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU, 556 | 0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU, 557 | 0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U, 558 | 0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U, 559 | 0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U, 560 | 0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U, 561 | 0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U, 562 | 0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U, 563 | 0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U, 564 | 0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU, 565 | 0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU, 566 | 0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U, 567 | 0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U, 568 | 0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU, 569 | 0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU, 570 | 0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U, 571 | 0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U, 572 | 0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U, 573 | 0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U, 574 | 0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U, 575 | 0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U, 576 | 0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U, 577 | 0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU, 578 | 0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U, 579 | 0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U, 580 | 0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U, 581 | 0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U, 582 | 0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U, 583 | 0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U, 584 | 0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU, 585 | 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U, 586 | 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U, 587 | }; 588 | static const u32 Td3[256] = { 589 | 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU, 590 | 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU, 591 | 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U, 592 | 0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U, 593 | 0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU, 594 | 0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU, 595 | 0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U, 596 | 0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU, 597 | 0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U, 598 | 0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU, 599 | 0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U, 600 | 0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U, 601 | 0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U, 602 | 0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U, 603 | 0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U, 604 | 0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU, 605 | 0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU, 606 | 0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U, 607 | 0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U, 608 | 0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU, 609 | 0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU, 610 | 0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U, 611 | 0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U, 612 | 0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U, 613 | 0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U, 614 | 0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU, 615 | 0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U, 616 | 0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U, 617 | 0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU, 618 | 0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU, 619 | 0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U, 620 | 0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U, 621 | 0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U, 622 | 0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU, 623 | 0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U, 624 | 0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U, 625 | 0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U, 626 | 0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U, 627 | 0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U, 628 | 0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U, 629 | 0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U, 630 | 0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU, 631 | 0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U, 632 | 0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U, 633 | 0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU, 634 | 0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU, 635 | 0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U, 636 | 0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU, 637 | 0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U, 638 | 0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U, 639 | 0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U, 640 | 0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U, 641 | 0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U, 642 | 0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U, 643 | 0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU, 644 | 0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU, 645 | 0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU, 646 | 0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU, 647 | 0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U, 648 | 0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U, 649 | 0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U, 650 | 0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU, 651 | 0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U, 652 | 0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U, 653 | }; 654 | static const u32 Td4[256] = { 655 | 0x52525252U, 0x09090909U, 0x6a6a6a6aU, 0xd5d5d5d5U, 656 | 0x30303030U, 0x36363636U, 0xa5a5a5a5U, 0x38383838U, 657 | 0xbfbfbfbfU, 0x40404040U, 0xa3a3a3a3U, 0x9e9e9e9eU, 658 | 0x81818181U, 0xf3f3f3f3U, 0xd7d7d7d7U, 0xfbfbfbfbU, 659 | 0x7c7c7c7cU, 0xe3e3e3e3U, 0x39393939U, 0x82828282U, 660 | 0x9b9b9b9bU, 0x2f2f2f2fU, 0xffffffffU, 0x87878787U, 661 | 0x34343434U, 0x8e8e8e8eU, 0x43434343U, 0x44444444U, 662 | 0xc4c4c4c4U, 0xdedededeU, 0xe9e9e9e9U, 0xcbcbcbcbU, 663 | 0x54545454U, 0x7b7b7b7bU, 0x94949494U, 0x32323232U, 664 | 0xa6a6a6a6U, 0xc2c2c2c2U, 0x23232323U, 0x3d3d3d3dU, 665 | 0xeeeeeeeeU, 0x4c4c4c4cU, 0x95959595U, 0x0b0b0b0bU, 666 | 0x42424242U, 0xfafafafaU, 0xc3c3c3c3U, 0x4e4e4e4eU, 667 | 0x08080808U, 0x2e2e2e2eU, 0xa1a1a1a1U, 0x66666666U, 668 | 0x28282828U, 0xd9d9d9d9U, 0x24242424U, 0xb2b2b2b2U, 669 | 0x76767676U, 0x5b5b5b5bU, 0xa2a2a2a2U, 0x49494949U, 670 | 0x6d6d6d6dU, 0x8b8b8b8bU, 0xd1d1d1d1U, 0x25252525U, 671 | 0x72727272U, 0xf8f8f8f8U, 0xf6f6f6f6U, 0x64646464U, 672 | 0x86868686U, 0x68686868U, 0x98989898U, 0x16161616U, 673 | 0xd4d4d4d4U, 0xa4a4a4a4U, 0x5c5c5c5cU, 0xccccccccU, 674 | 0x5d5d5d5dU, 0x65656565U, 0xb6b6b6b6U, 0x92929292U, 675 | 0x6c6c6c6cU, 0x70707070U, 0x48484848U, 0x50505050U, 676 | 0xfdfdfdfdU, 0xededededU, 0xb9b9b9b9U, 0xdadadadaU, 677 | 0x5e5e5e5eU, 0x15151515U, 0x46464646U, 0x57575757U, 678 | 0xa7a7a7a7U, 0x8d8d8d8dU, 0x9d9d9d9dU, 0x84848484U, 679 | 0x90909090U, 0xd8d8d8d8U, 0xababababU, 0x00000000U, 680 | 0x8c8c8c8cU, 0xbcbcbcbcU, 0xd3d3d3d3U, 0x0a0a0a0aU, 681 | 0xf7f7f7f7U, 0xe4e4e4e4U, 0x58585858U, 0x05050505U, 682 | 0xb8b8b8b8U, 0xb3b3b3b3U, 0x45454545U, 0x06060606U, 683 | 0xd0d0d0d0U, 0x2c2c2c2cU, 0x1e1e1e1eU, 0x8f8f8f8fU, 684 | 0xcacacacaU, 0x3f3f3f3fU, 0x0f0f0f0fU, 0x02020202U, 685 | 0xc1c1c1c1U, 0xafafafafU, 0xbdbdbdbdU, 0x03030303U, 686 | 0x01010101U, 0x13131313U, 0x8a8a8a8aU, 0x6b6b6b6bU, 687 | 0x3a3a3a3aU, 0x91919191U, 0x11111111U, 0x41414141U, 688 | 0x4f4f4f4fU, 0x67676767U, 0xdcdcdcdcU, 0xeaeaeaeaU, 689 | 0x97979797U, 0xf2f2f2f2U, 0xcfcfcfcfU, 0xcecececeU, 690 | 0xf0f0f0f0U, 0xb4b4b4b4U, 0xe6e6e6e6U, 0x73737373U, 691 | 0x96969696U, 0xacacacacU, 0x74747474U, 0x22222222U, 692 | 0xe7e7e7e7U, 0xadadadadU, 0x35353535U, 0x85858585U, 693 | 0xe2e2e2e2U, 0xf9f9f9f9U, 0x37373737U, 0xe8e8e8e8U, 694 | 0x1c1c1c1cU, 0x75757575U, 0xdfdfdfdfU, 0x6e6e6e6eU, 695 | 0x47474747U, 0xf1f1f1f1U, 0x1a1a1a1aU, 0x71717171U, 696 | 0x1d1d1d1dU, 0x29292929U, 0xc5c5c5c5U, 0x89898989U, 697 | 0x6f6f6f6fU, 0xb7b7b7b7U, 0x62626262U, 0x0e0e0e0eU, 698 | 0xaaaaaaaaU, 0x18181818U, 0xbebebebeU, 0x1b1b1b1bU, 699 | 0xfcfcfcfcU, 0x56565656U, 0x3e3e3e3eU, 0x4b4b4b4bU, 700 | 0xc6c6c6c6U, 0xd2d2d2d2U, 0x79797979U, 0x20202020U, 701 | 0x9a9a9a9aU, 0xdbdbdbdbU, 0xc0c0c0c0U, 0xfefefefeU, 702 | 0x78787878U, 0xcdcdcdcdU, 0x5a5a5a5aU, 0xf4f4f4f4U, 703 | 0x1f1f1f1fU, 0xddddddddU, 0xa8a8a8a8U, 0x33333333U, 704 | 0x88888888U, 0x07070707U, 0xc7c7c7c7U, 0x31313131U, 705 | 0xb1b1b1b1U, 0x12121212U, 0x10101010U, 0x59595959U, 706 | 0x27272727U, 0x80808080U, 0xececececU, 0x5f5f5f5fU, 707 | 0x60606060U, 0x51515151U, 0x7f7f7f7fU, 0xa9a9a9a9U, 708 | 0x19191919U, 0xb5b5b5b5U, 0x4a4a4a4aU, 0x0d0d0d0dU, 709 | 0x2d2d2d2dU, 0xe5e5e5e5U, 0x7a7a7a7aU, 0x9f9f9f9fU, 710 | 0x93939393U, 0xc9c9c9c9U, 0x9c9c9c9cU, 0xefefefefU, 711 | 0xa0a0a0a0U, 0xe0e0e0e0U, 0x3b3b3b3bU, 0x4d4d4d4dU, 712 | 0xaeaeaeaeU, 0x2a2a2a2aU, 0xf5f5f5f5U, 0xb0b0b0b0U, 713 | 0xc8c8c8c8U, 0xebebebebU, 0xbbbbbbbbU, 0x3c3c3c3cU, 714 | 0x83838383U, 0x53535353U, 0x99999999U, 0x61616161U, 715 | 0x17171717U, 0x2b2b2b2bU, 0x04040404U, 0x7e7e7e7eU, 716 | 0xbabababaU, 0x77777777U, 0xd6d6d6d6U, 0x26262626U, 717 | 0xe1e1e1e1U, 0x69696969U, 0x14141414U, 0x63636363U, 718 | 0x55555555U, 0x21212121U, 0x0c0c0c0cU, 0x7d7d7d7dU, 719 | }; 720 | static const u32 rcon[] = { 721 | 0x01000000, 0x02000000, 0x04000000, 0x08000000, 722 | 0x10000000, 0x20000000, 0x40000000, 0x80000000, 723 | 0x1B000000, 0x36000000, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */ 724 | }; 725 | 726 | /** 727 | * Expand the cipher key into the encryption key schedule. 728 | */ 729 | int AES_set_encrypt_key(const unsigned char *userKey, const int bits, 730 | AES_KEY *key) { 731 | 732 | u32 *rk; 733 | int i = 0; 734 | u32 temp; 735 | 736 | if (!userKey || !key) 737 | return -1; 738 | if (bits != 128 && bits != 192 && bits != 256) 739 | return -2; 740 | 741 | rk = key->rd_key; 742 | 743 | if (bits==128) 744 | key->rounds = 10; 745 | else if (bits==192) 746 | key->rounds = 12; 747 | else 748 | key->rounds = 14; 749 | 750 | rk[0] = GETU32(userKey ); 751 | rk[1] = GETU32(userKey + 4); 752 | rk[2] = GETU32(userKey + 8); 753 | rk[3] = GETU32(userKey + 12); 754 | if (bits == 128) { 755 | while (1) { 756 | temp = rk[3]; 757 | rk[4] = rk[0] ^ 758 | (Te4[(temp >> 16) & 0xff] & 0xff000000) ^ 759 | (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ 760 | (Te4[(temp ) & 0xff] & 0x0000ff00) ^ 761 | (Te4[(temp >> 24) ] & 0x000000ff) ^ 762 | rcon[i]; 763 | rk[5] = rk[1] ^ rk[4]; 764 | rk[6] = rk[2] ^ rk[5]; 765 | rk[7] = rk[3] ^ rk[6]; 766 | if (++i == 10) { 767 | return 0; 768 | } 769 | rk += 4; 770 | } 771 | } 772 | rk[4] = GETU32(userKey + 16); 773 | rk[5] = GETU32(userKey + 20); 774 | if (bits == 192) { 775 | while (1) { 776 | temp = rk[ 5]; 777 | rk[ 6] = rk[ 0] ^ 778 | (Te4[(temp >> 16) & 0xff] & 0xff000000) ^ 779 | (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ 780 | (Te4[(temp ) & 0xff] & 0x0000ff00) ^ 781 | (Te4[(temp >> 24) ] & 0x000000ff) ^ 782 | rcon[i]; 783 | rk[ 7] = rk[ 1] ^ rk[ 6]; 784 | rk[ 8] = rk[ 2] ^ rk[ 7]; 785 | rk[ 9] = rk[ 3] ^ rk[ 8]; 786 | if (++i == 8) { 787 | return 0; 788 | } 789 | rk[10] = rk[ 4] ^ rk[ 9]; 790 | rk[11] = rk[ 5] ^ rk[10]; 791 | rk += 6; 792 | } 793 | } 794 | rk[6] = GETU32(userKey + 24); 795 | rk[7] = GETU32(userKey + 28); 796 | if (bits == 256) { 797 | while (1) { 798 | temp = rk[ 7]; 799 | rk[ 8] = rk[ 0] ^ 800 | (Te4[(temp >> 16) & 0xff] & 0xff000000) ^ 801 | (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^ 802 | (Te4[(temp ) & 0xff] & 0x0000ff00) ^ 803 | (Te4[(temp >> 24) ] & 0x000000ff) ^ 804 | rcon[i]; 805 | rk[ 9] = rk[ 1] ^ rk[ 8]; 806 | rk[10] = rk[ 2] ^ rk[ 9]; 807 | rk[11] = rk[ 3] ^ rk[10]; 808 | if (++i == 7) { 809 | return 0; 810 | } 811 | temp = rk[11]; 812 | rk[12] = rk[ 4] ^ 813 | (Te4[(temp >> 24) ] & 0xff000000) ^ 814 | (Te4[(temp >> 16) & 0xff] & 0x00ff0000) ^ 815 | (Te4[(temp >> 8) & 0xff] & 0x0000ff00) ^ 816 | (Te4[(temp ) & 0xff] & 0x000000ff); 817 | rk[13] = rk[ 5] ^ rk[12]; 818 | rk[14] = rk[ 6] ^ rk[13]; 819 | rk[15] = rk[ 7] ^ rk[14]; 820 | 821 | rk += 8; 822 | } 823 | } 824 | return 0; 825 | } 826 | 827 | /** 828 | * Expand the cipher key into the decryption key schedule. 829 | */ 830 | int AES_set_decrypt_key(const unsigned char *userKey, const int bits, 831 | AES_KEY *key) { 832 | 833 | u32 *rk; 834 | int i, j, status; 835 | u32 temp; 836 | 837 | /* first, start with an encryption schedule */ 838 | status = AES_set_encrypt_key(userKey, bits, key); 839 | if (status < 0) 840 | return status; 841 | 842 | rk = key->rd_key; 843 | 844 | /* invert the order of the round keys: */ 845 | for (i = 0, j = 4*(key->rounds); i < j; i += 4, j -= 4) { 846 | temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp; 847 | temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp; 848 | temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp; 849 | temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp; 850 | } 851 | /* apply the inverse MixColumn transform to all round keys but the first and the last: */ 852 | for (i = 1; i < (key->rounds); i++) { 853 | rk += 4; 854 | rk[0] = 855 | Td0[Te4[(rk[0] >> 24) ] & 0xff] ^ 856 | Td1[Te4[(rk[0] >> 16) & 0xff] & 0xff] ^ 857 | Td2[Te4[(rk[0] >> 8) & 0xff] & 0xff] ^ 858 | Td3[Te4[(rk[0] ) & 0xff] & 0xff]; 859 | rk[1] = 860 | Td0[Te4[(rk[1] >> 24) ] & 0xff] ^ 861 | Td1[Te4[(rk[1] >> 16) & 0xff] & 0xff] ^ 862 | Td2[Te4[(rk[1] >> 8) & 0xff] & 0xff] ^ 863 | Td3[Te4[(rk[1] ) & 0xff] & 0xff]; 864 | rk[2] = 865 | Td0[Te4[(rk[2] >> 24) ] & 0xff] ^ 866 | Td1[Te4[(rk[2] >> 16) & 0xff] & 0xff] ^ 867 | Td2[Te4[(rk[2] >> 8) & 0xff] & 0xff] ^ 868 | Td3[Te4[(rk[2] ) & 0xff] & 0xff]; 869 | rk[3] = 870 | Td0[Te4[(rk[3] >> 24) ] & 0xff] ^ 871 | Td1[Te4[(rk[3] >> 16) & 0xff] & 0xff] ^ 872 | Td2[Te4[(rk[3] >> 8) & 0xff] & 0xff] ^ 873 | Td3[Te4[(rk[3] ) & 0xff] & 0xff]; 874 | } 875 | return 0; 876 | } 877 | 878 | #ifndef AES_ASM 879 | /* 880 | * Encrypt a single block 881 | * in and out can overlap 882 | */ 883 | void AES_encrypt(const unsigned char *in, unsigned char *out, 884 | const AES_KEY *key) { 885 | 886 | const u32 *rk; 887 | u32 s0, s1, s2, s3, t0, t1, t2, t3; 888 | #ifndef FULL_UNROLL 889 | int r; 890 | #endif /* ?FULL_UNROLL */ 891 | 892 | // assert(in && out && key); 893 | rk = key->rd_key; 894 | 895 | /* 896 | * map byte array block to cipher state 897 | * and add initial round key: 898 | */ 899 | s0 = GETU32(in ) ^ rk[0]; 900 | s1 = GETU32(in + 4) ^ rk[1]; 901 | s2 = GETU32(in + 8) ^ rk[2]; 902 | s3 = GETU32(in + 12) ^ rk[3]; 903 | #ifdef FULL_UNROLL 904 | /* round 1: */ 905 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[ 4]; 906 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[ 5]; 907 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[ 6]; 908 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[ 7]; 909 | /* round 2: */ 910 | s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[ 8]; 911 | s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[ 9]; 912 | s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[10]; 913 | s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[11]; 914 | /* round 3: */ 915 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[12]; 916 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[13]; 917 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[14]; 918 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[15]; 919 | /* round 4: */ 920 | s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[16]; 921 | s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[17]; 922 | s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[18]; 923 | s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[19]; 924 | /* round 5: */ 925 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[20]; 926 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[21]; 927 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[22]; 928 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[23]; 929 | /* round 6: */ 930 | s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[24]; 931 | s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[25]; 932 | s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[26]; 933 | s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[27]; 934 | /* round 7: */ 935 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[28]; 936 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[29]; 937 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[30]; 938 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[31]; 939 | /* round 8: */ 940 | s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[32]; 941 | s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[33]; 942 | s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[34]; 943 | s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[35]; 944 | /* round 9: */ 945 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[36]; 946 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[37]; 947 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[38]; 948 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[39]; 949 | if (key->rounds > 10) { 950 | /* round 10: */ 951 | s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[40]; 952 | s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[41]; 953 | s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[42]; 954 | s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[43]; 955 | /* round 11: */ 956 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[44]; 957 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[45]; 958 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[46]; 959 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[47]; 960 | if (key->rounds > 12) { 961 | /* round 12: */ 962 | s0 = Te0[t0 >> 24] ^ Te1[(t1 >> 16) & 0xff] ^ Te2[(t2 >> 8) & 0xff] ^ Te3[t3 & 0xff] ^ rk[48]; 963 | s1 = Te0[t1 >> 24] ^ Te1[(t2 >> 16) & 0xff] ^ Te2[(t3 >> 8) & 0xff] ^ Te3[t0 & 0xff] ^ rk[49]; 964 | s2 = Te0[t2 >> 24] ^ Te1[(t3 >> 16) & 0xff] ^ Te2[(t0 >> 8) & 0xff] ^ Te3[t1 & 0xff] ^ rk[50]; 965 | s3 = Te0[t3 >> 24] ^ Te1[(t0 >> 16) & 0xff] ^ Te2[(t1 >> 8) & 0xff] ^ Te3[t2 & 0xff] ^ rk[51]; 966 | /* round 13: */ 967 | t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[52]; 968 | t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[53]; 969 | t2 = Te0[s2 >> 24] ^ Te1[(s3 >> 16) & 0xff] ^ Te2[(s0 >> 8) & 0xff] ^ Te3[s1 & 0xff] ^ rk[54]; 970 | t3 = Te0[s3 >> 24] ^ Te1[(s0 >> 16) & 0xff] ^ Te2[(s1 >> 8) & 0xff] ^ Te3[s2 & 0xff] ^ rk[55]; 971 | } 972 | } 973 | rk += key->rounds << 2; 974 | #else /* !FULL_UNROLL */ 975 | /* 976 | * Nr - 1 full rounds: 977 | */ 978 | r = key->rounds >> 1; 979 | for (;;) { 980 | t0 = 981 | Te0[(s0 >> 24) ] ^ 982 | Te1[(s1 >> 16) & 0xff] ^ 983 | Te2[(s2 >> 8) & 0xff] ^ 984 | Te3[(s3 ) & 0xff] ^ 985 | rk[4]; 986 | t1 = 987 | Te0[(s1 >> 24) ] ^ 988 | Te1[(s2 >> 16) & 0xff] ^ 989 | Te2[(s3 >> 8) & 0xff] ^ 990 | Te3[(s0 ) & 0xff] ^ 991 | rk[5]; 992 | t2 = 993 | Te0[(s2 >> 24) ] ^ 994 | Te1[(s3 >> 16) & 0xff] ^ 995 | Te2[(s0 >> 8) & 0xff] ^ 996 | Te3[(s1 ) & 0xff] ^ 997 | rk[6]; 998 | t3 = 999 | Te0[(s3 >> 24) ] ^ 1000 | Te1[(s0 >> 16) & 0xff] ^ 1001 | Te2[(s1 >> 8) & 0xff] ^ 1002 | Te3[(s2 ) & 0xff] ^ 1003 | rk[7]; 1004 | 1005 | rk += 8; 1006 | if (--r == 0) { 1007 | break; 1008 | } 1009 | 1010 | s0 = 1011 | Te0[(t0 >> 24) ] ^ 1012 | Te1[(t1 >> 16) & 0xff] ^ 1013 | Te2[(t2 >> 8) & 0xff] ^ 1014 | Te3[(t3 ) & 0xff] ^ 1015 | rk[0]; 1016 | s1 = 1017 | Te0[(t1 >> 24) ] ^ 1018 | Te1[(t2 >> 16) & 0xff] ^ 1019 | Te2[(t3 >> 8) & 0xff] ^ 1020 | Te3[(t0 ) & 0xff] ^ 1021 | rk[1]; 1022 | s2 = 1023 | Te0[(t2 >> 24) ] ^ 1024 | Te1[(t3 >> 16) & 0xff] ^ 1025 | Te2[(t0 >> 8) & 0xff] ^ 1026 | Te3[(t1 ) & 0xff] ^ 1027 | rk[2]; 1028 | s3 = 1029 | Te0[(t3 >> 24) ] ^ 1030 | Te1[(t0 >> 16) & 0xff] ^ 1031 | Te2[(t1 >> 8) & 0xff] ^ 1032 | Te3[(t2 ) & 0xff] ^ 1033 | rk[3]; 1034 | } 1035 | #endif /* ?FULL_UNROLL */ 1036 | /* 1037 | * apply last round and 1038 | * map cipher state to byte array block: 1039 | */ 1040 | s0 = 1041 | (Te4[(t0 >> 24) ] & 0xff000000) ^ 1042 | (Te4[(t1 >> 16) & 0xff] & 0x00ff0000) ^ 1043 | (Te4[(t2 >> 8) & 0xff] & 0x0000ff00) ^ 1044 | (Te4[(t3 ) & 0xff] & 0x000000ff) ^ 1045 | rk[0]; 1046 | PUTU32(out , s0); 1047 | s1 = 1048 | (Te4[(t1 >> 24) ] & 0xff000000) ^ 1049 | (Te4[(t2 >> 16) & 0xff] & 0x00ff0000) ^ 1050 | (Te4[(t3 >> 8) & 0xff] & 0x0000ff00) ^ 1051 | (Te4[(t0 ) & 0xff] & 0x000000ff) ^ 1052 | rk[1]; 1053 | PUTU32(out + 4, s1); 1054 | s2 = 1055 | (Te4[(t2 >> 24) ] & 0xff000000) ^ 1056 | (Te4[(t3 >> 16) & 0xff] & 0x00ff0000) ^ 1057 | (Te4[(t0 >> 8) & 0xff] & 0x0000ff00) ^ 1058 | (Te4[(t1 ) & 0xff] & 0x000000ff) ^ 1059 | rk[2]; 1060 | PUTU32(out + 8, s2); 1061 | s3 = 1062 | (Te4[(t3 >> 24) ] & 0xff000000) ^ 1063 | (Te4[(t0 >> 16) & 0xff] & 0x00ff0000) ^ 1064 | (Te4[(t1 >> 8) & 0xff] & 0x0000ff00) ^ 1065 | (Te4[(t2 ) & 0xff] & 0x000000ff) ^ 1066 | rk[3]; 1067 | PUTU32(out + 12, s3); 1068 | } 1069 | 1070 | /* 1071 | * Decrypt a single block 1072 | * in and out can overlap 1073 | */ 1074 | void AES_decrypt(const unsigned char *in, unsigned char *out, 1075 | const AES_KEY *key) { 1076 | 1077 | const u32 *rk; 1078 | u32 s0, s1, s2, s3, t0, t1, t2, t3; 1079 | #ifndef FULL_UNROLL 1080 | int r; 1081 | #endif /* ?FULL_UNROLL */ 1082 | 1083 | // assert(in && out && key); 1084 | rk = key->rd_key; 1085 | 1086 | /* 1087 | * map byte array block to cipher state 1088 | * and add initial round key: 1089 | */ 1090 | s0 = GETU32(in ) ^ rk[0]; 1091 | s1 = GETU32(in + 4) ^ rk[1]; 1092 | s2 = GETU32(in + 8) ^ rk[2]; 1093 | s3 = GETU32(in + 12) ^ rk[3]; 1094 | #ifdef FULL_UNROLL 1095 | /* round 1: */ 1096 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[ 4]; 1097 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[ 5]; 1098 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[ 6]; 1099 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[ 7]; 1100 | /* round 2: */ 1101 | s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[ 8]; 1102 | s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[ 9]; 1103 | s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[10]; 1104 | s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[11]; 1105 | /* round 3: */ 1106 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[12]; 1107 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[13]; 1108 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[14]; 1109 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[15]; 1110 | /* round 4: */ 1111 | s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[16]; 1112 | s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[17]; 1113 | s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[18]; 1114 | s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[19]; 1115 | /* round 5: */ 1116 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[20]; 1117 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[21]; 1118 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[22]; 1119 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[23]; 1120 | /* round 6: */ 1121 | s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[24]; 1122 | s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[25]; 1123 | s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[26]; 1124 | s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[27]; 1125 | /* round 7: */ 1126 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[28]; 1127 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[29]; 1128 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[30]; 1129 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[31]; 1130 | /* round 8: */ 1131 | s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[32]; 1132 | s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[33]; 1133 | s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[34]; 1134 | s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[35]; 1135 | /* round 9: */ 1136 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[36]; 1137 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[37]; 1138 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[38]; 1139 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[39]; 1140 | if (key->rounds > 10) { 1141 | /* round 10: */ 1142 | s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[40]; 1143 | s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[41]; 1144 | s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[42]; 1145 | s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[43]; 1146 | /* round 11: */ 1147 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[44]; 1148 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[45]; 1149 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[46]; 1150 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[47]; 1151 | if (key->rounds > 12) { 1152 | /* round 12: */ 1153 | s0 = Td0[t0 >> 24] ^ Td1[(t3 >> 16) & 0xff] ^ Td2[(t2 >> 8) & 0xff] ^ Td3[t1 & 0xff] ^ rk[48]; 1154 | s1 = Td0[t1 >> 24] ^ Td1[(t0 >> 16) & 0xff] ^ Td2[(t3 >> 8) & 0xff] ^ Td3[t2 & 0xff] ^ rk[49]; 1155 | s2 = Td0[t2 >> 24] ^ Td1[(t1 >> 16) & 0xff] ^ Td2[(t0 >> 8) & 0xff] ^ Td3[t3 & 0xff] ^ rk[50]; 1156 | s3 = Td0[t3 >> 24] ^ Td1[(t2 >> 16) & 0xff] ^ Td2[(t1 >> 8) & 0xff] ^ Td3[t0 & 0xff] ^ rk[51]; 1157 | /* round 13: */ 1158 | t0 = Td0[s0 >> 24] ^ Td1[(s3 >> 16) & 0xff] ^ Td2[(s2 >> 8) & 0xff] ^ Td3[s1 & 0xff] ^ rk[52]; 1159 | t1 = Td0[s1 >> 24] ^ Td1[(s0 >> 16) & 0xff] ^ Td2[(s3 >> 8) & 0xff] ^ Td3[s2 & 0xff] ^ rk[53]; 1160 | t2 = Td0[s2 >> 24] ^ Td1[(s1 >> 16) & 0xff] ^ Td2[(s0 >> 8) & 0xff] ^ Td3[s3 & 0xff] ^ rk[54]; 1161 | t3 = Td0[s3 >> 24] ^ Td1[(s2 >> 16) & 0xff] ^ Td2[(s1 >> 8) & 0xff] ^ Td3[s0 & 0xff] ^ rk[55]; 1162 | } 1163 | } 1164 | rk += key->rounds << 2; 1165 | #else /* !FULL_UNROLL */ 1166 | /* 1167 | * Nr - 1 full rounds: 1168 | */ 1169 | r = key->rounds >> 1; 1170 | for (;;) { 1171 | t0 = 1172 | Td0[(s0 >> 24) ] ^ 1173 | Td1[(s3 >> 16) & 0xff] ^ 1174 | Td2[(s2 >> 8) & 0xff] ^ 1175 | Td3[(s1 ) & 0xff] ^ 1176 | rk[4]; 1177 | t1 = 1178 | Td0[(s1 >> 24) ] ^ 1179 | Td1[(s0 >> 16) & 0xff] ^ 1180 | Td2[(s3 >> 8) & 0xff] ^ 1181 | Td3[(s2 ) & 0xff] ^ 1182 | rk[5]; 1183 | t2 = 1184 | Td0[(s2 >> 24) ] ^ 1185 | Td1[(s1 >> 16) & 0xff] ^ 1186 | Td2[(s0 >> 8) & 0xff] ^ 1187 | Td3[(s3 ) & 0xff] ^ 1188 | rk[6]; 1189 | t3 = 1190 | Td0[(s3 >> 24) ] ^ 1191 | Td1[(s2 >> 16) & 0xff] ^ 1192 | Td2[(s1 >> 8) & 0xff] ^ 1193 | Td3[(s0 ) & 0xff] ^ 1194 | rk[7]; 1195 | 1196 | rk += 8; 1197 | if (--r == 0) { 1198 | break; 1199 | } 1200 | 1201 | s0 = 1202 | Td0[(t0 >> 24) ] ^ 1203 | Td1[(t3 >> 16) & 0xff] ^ 1204 | Td2[(t2 >> 8) & 0xff] ^ 1205 | Td3[(t1 ) & 0xff] ^ 1206 | rk[0]; 1207 | s1 = 1208 | Td0[(t1 >> 24) ] ^ 1209 | Td1[(t0 >> 16) & 0xff] ^ 1210 | Td2[(t3 >> 8) & 0xff] ^ 1211 | Td3[(t2 ) & 0xff] ^ 1212 | rk[1]; 1213 | s2 = 1214 | Td0[(t2 >> 24) ] ^ 1215 | Td1[(t1 >> 16) & 0xff] ^ 1216 | Td2[(t0 >> 8) & 0xff] ^ 1217 | Td3[(t3 ) & 0xff] ^ 1218 | rk[2]; 1219 | s3 = 1220 | Td0[(t3 >> 24) ] ^ 1221 | Td1[(t2 >> 16) & 0xff] ^ 1222 | Td2[(t1 >> 8) & 0xff] ^ 1223 | Td3[(t0 ) & 0xff] ^ 1224 | rk[3]; 1225 | } 1226 | #endif /* ?FULL_UNROLL */ 1227 | /* 1228 | * apply last round and 1229 | * map cipher state to byte array block: 1230 | */ 1231 | s0 = 1232 | (Td4[(t0 >> 24) ] & 0xff000000) ^ 1233 | (Td4[(t3 >> 16) & 0xff] & 0x00ff0000) ^ 1234 | (Td4[(t2 >> 8) & 0xff] & 0x0000ff00) ^ 1235 | (Td4[(t1 ) & 0xff] & 0x000000ff) ^ 1236 | rk[0]; 1237 | PUTU32(out , s0); 1238 | s1 = 1239 | (Td4[(t1 >> 24) ] & 0xff000000) ^ 1240 | (Td4[(t0 >> 16) & 0xff] & 0x00ff0000) ^ 1241 | (Td4[(t3 >> 8) & 0xff] & 0x0000ff00) ^ 1242 | (Td4[(t2 ) & 0xff] & 0x000000ff) ^ 1243 | rk[1]; 1244 | PUTU32(out + 4, s1); 1245 | s2 = 1246 | (Td4[(t2 >> 24) ] & 0xff000000) ^ 1247 | (Td4[(t1 >> 16) & 0xff] & 0x00ff0000) ^ 1248 | (Td4[(t0 >> 8) & 0xff] & 0x0000ff00) ^ 1249 | (Td4[(t3 ) & 0xff] & 0x000000ff) ^ 1250 | rk[2]; 1251 | PUTU32(out + 8, s2); 1252 | s3 = 1253 | (Td4[(t3 >> 24) ] & 0xff000000) ^ 1254 | (Td4[(t2 >> 16) & 0xff] & 0x00ff0000) ^ 1255 | (Td4[(t1 >> 8) & 0xff] & 0x0000ff00) ^ 1256 | (Td4[(t0 ) & 0xff] & 0x000000ff) ^ 1257 | rk[3]; 1258 | PUTU32(out + 12, s3); 1259 | } 1260 | 1261 | #endif /* AES_ASM */ 1262 | 1263 | #if 0 1264 | void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, 1265 | const unsigned long length, const AES_KEY *key, 1266 | unsigned char *ivec, const int enc) 1267 | { 1268 | 1269 | unsigned long n; 1270 | unsigned long len = length; 1271 | unsigned char tmp[AES_BLOCK_SIZE]; 1272 | 1273 | assert(in && out && key && ivec); 1274 | 1275 | if (enc) { 1276 | while (len >= AES_BLOCK_SIZE) { 1277 | for(n=0; n < AES_BLOCK_SIZE; ++n) 1278 | tmp[n] = in[n] ^ ivec[n]; 1279 | AES_encrypt(tmp, out, key); 1280 | memcpy(ivec, out, AES_BLOCK_SIZE); 1281 | len -= AES_BLOCK_SIZE; 1282 | in += AES_BLOCK_SIZE; 1283 | out += AES_BLOCK_SIZE; 1284 | } 1285 | if (len) { 1286 | for(n=0; n < len; ++n) 1287 | tmp[n] = in[n] ^ ivec[n]; 1288 | for(n=len; n < AES_BLOCK_SIZE; ++n) 1289 | tmp[n] = ivec[n]; 1290 | AES_encrypt(tmp, tmp, key); 1291 | memcpy(out, tmp, AES_BLOCK_SIZE); 1292 | memcpy(ivec, tmp, AES_BLOCK_SIZE); 1293 | } 1294 | } else { 1295 | while (len >= AES_BLOCK_SIZE) { 1296 | memcpy(tmp, in, AES_BLOCK_SIZE); 1297 | AES_decrypt(in, out, key); 1298 | for(n=0; n < AES_BLOCK_SIZE; ++n) 1299 | out[n] ^= ivec[n]; 1300 | memcpy(ivec, tmp, AES_BLOCK_SIZE); 1301 | len -= AES_BLOCK_SIZE; 1302 | in += AES_BLOCK_SIZE; 1303 | out += AES_BLOCK_SIZE; 1304 | } 1305 | if (len) { 1306 | memcpy(tmp, in, AES_BLOCK_SIZE); 1307 | AES_decrypt(tmp, tmp, key); 1308 | for(n=0; n < len; ++n) 1309 | out[n] = tmp[n] ^ ivec[n]; 1310 | memcpy(ivec, tmp, AES_BLOCK_SIZE); 1311 | } 1312 | } 1313 | } 1314 | #endif 1315 | -------------------------------------------------------------------------------- /aes.h: -------------------------------------------------------------------------------- 1 | #ifndef QEMU_AES_H 2 | #define QEMU_AES_H 3 | 4 | #include "tools.h" 5 | 6 | #define AES_MAXNR 14 7 | #define AES_BLOCK_SIZE 16 8 | 9 | struct aes_key_st { 10 | uint32_t rd_key[4 *(AES_MAXNR + 1)]; 11 | int rounds; 12 | }; 13 | typedef struct aes_key_st AES_KEY; 14 | 15 | int AES_set_encrypt_key(const unsigned char *userKey, const int bits, 16 | AES_KEY *key); 17 | int AES_set_decrypt_key(const unsigned char *userKey, const int bits, 18 | AES_KEY *key); 19 | 20 | void AES_encrypt(const unsigned char *in, unsigned char *out, 21 | const AES_KEY *key); 22 | void AES_decrypt(const unsigned char *in, unsigned char *out, 23 | const AES_KEY *key); 24 | 25 | #if 0 26 | void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, 27 | const unsigned long length, const AES_KEY *key, 28 | unsigned char *ivec, const int enc); 29 | #endif 30 | #endif 31 | -------------------------------------------------------------------------------- /common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) Youness Alaoui (KaKaRoTo) 3 | * 4 | * This software is distributed under the terms of the GNU General Public 5 | * License ("GPL") version 3, as published by the Free Software Foundation. 6 | * 7 | */ 8 | 9 | #ifndef __COMMON_H__ 10 | #define __COMMON_H__ 11 | 12 | 13 | #include 14 | 15 | #ifdef WIN32 16 | #define MKDIR(x,y) mkdir(x) 17 | #else 18 | #define MKDIR(x,y) mkdir(x,y) 19 | #endif 20 | 21 | #ifdef __BIG_ENDIAN__ 22 | #define swap16(x) (x) 23 | #define swap32(x) (x) 24 | #define swap64(x) (x) 25 | #else 26 | #define swap16(x) ((((uint16_t)(x) & 0xff00) >> 8) | \ 27 | (((uint16_t)(x) & 0x00ff) << 8)) 28 | #define swap32(x) ((((uint32_t)(x) & 0xff000000) >> 24) | \ 29 | (((uint32_t)(x) & 0x00ff0000) >> 8) | \ 30 | (((uint32_t)(x) & 0x0000ff00) << 8) | \ 31 | (((uint32_t)(x) & 0x000000ff) << 24)) 32 | #define swap64(x) \ 33 | ((((uint64_t)(x) & 0xff00000000000000ull) >> 56) | \ 34 | ((uint64_t)((x) & 0x00ff000000000000ull) >> 40) | \ 35 | ((uint64_t)((x) & 0x0000ff0000000000ull) >> 24) | \ 36 | ((uint64_t)((x) & 0x000000ff00000000ull) >> 8) | \ 37 | ((uint64_t)((x) & 0x00000000ff000000ull) << 8) | \ 38 | ((uint64_t)((x) & 0x0000000000ff0000ull) << 24) | \ 39 | ((uint64_t)((x) & 0x000000000000ff00ull) << 40) | \ 40 | ((uint64_t)((x) & 0x00000000000000ffull) << 56)) 41 | #endif 42 | 43 | 44 | #define ERROR(err, msg) do { \ 45 | perror (msg); \ 46 | exit (err); \ 47 | } while(0); 48 | 49 | 50 | #endif /* __COMMON_H__ */ 51 | -------------------------------------------------------------------------------- /fpkg_rename.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2018 harlequin 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "sha2.h" 11 | #include "tools.h" 12 | 13 | #ifdef WIN32 14 | #define MKDIR(x,y) mkdir(x) 15 | #else 16 | #define MKDIR(x,y) mkdir(x,y) 17 | #endif 18 | 19 | #define PS4_PKG_MAGIC 0x544E437F // .CNT 20 | 21 | typedef struct sfo_header_t { 22 | int magic; //PSF 23 | int version; //1.1 24 | int keyTableOffset; 25 | int dataTableOffset; 26 | int indexTableEntries; 27 | } sfo_header_t; 28 | 29 | typedef struct indexTableEntry_t { 30 | unsigned short keyOffset; //offset of keytable + keyOffset 31 | unsigned short param_fmt; //enum (see below) 32 | unsigned int paramLen; 33 | unsigned int paramMaxLen; 34 | unsigned int dataOffset; //offset of datatable + dataOffset 35 | } indexTableEntry_t; 36 | 37 | enum PS4_PKG_ENTRY_TYPES { 38 | PS4_PKG_ENTRY_TYPE_DIGEST_TABLE = 0x0001, 39 | PS4_PKG_ENTRY_TYPE_0x800 = 0x0010, 40 | PS4_PKG_ENTRY_TYPE_0x200 = 0x0020, 41 | PS4_PKG_ENTRY_TYPE_0x180 = 0x0080, 42 | PS4_PKG_ENTRY_TYPE_META_TABLE = 0x0100, 43 | PS4_PKG_ENTRY_TYPE_NAME_TABLE = 0x0200, 44 | PS4_PKG_ENTRY_TYPE_LICENSE = 0x0400, 45 | PS4_PKG_ENTRY_TYPE_FILE1 = 0x1000, 46 | PS4_PKG_ENTRY_TYPE_FILE2 = 0x1200 47 | }; 48 | 49 | 50 | #ifdef _WIN32 51 | #define OS_SEPARATOR "\\" 52 | #else 53 | #define OS_SEPARATOR "/" 54 | #endif 55 | 56 | // CNT/PKG structures. 57 | struct cnt_pkg_main_header { 58 | uint32_t magic; 59 | uint32_t type; 60 | uint32_t unk_0x08; 61 | uint32_t unk_0x0C; 62 | uint16_t unk1_entries_num; 63 | uint16_t table_entries_num; 64 | uint16_t system_entries_num; 65 | uint16_t unk2_entries_num; 66 | uint32_t file_table_offset; 67 | uint32_t main_entries_data_size; 68 | uint32_t unk_0x20; 69 | uint32_t body_offset; 70 | uint32_t unk_0x28; 71 | uint32_t body_size; 72 | uint8_t unk_0x30[0x10]; 73 | uint8_t content_id[0x30]; 74 | uint32_t unk_0x70; 75 | uint32_t unk_0x74; 76 | uint32_t unk_0x78; 77 | uint32_t unk_0x7C; 78 | uint32_t date; 79 | uint32_t time; 80 | uint32_t unk_0x88; 81 | uint32_t unk_0x8C; 82 | uint8_t unk_0x90[0x70]; 83 | uint8_t main_entries1_digest[0x20]; 84 | uint8_t main_entries2_digest[0x20]; 85 | uint8_t digest_table_digest[0x20]; 86 | uint8_t body_digest[0x20]; 87 | } __attribute__((packed)); 88 | 89 | struct cnt_pkg_content_header { 90 | uint32_t unk_0x400; 91 | uint32_t unk_0x404; 92 | uint32_t unk_0x408; 93 | uint32_t unk_0x40C; 94 | uint32_t unk_0x410; 95 | uint32_t content_offset; 96 | uint32_t unk_0x418; 97 | uint32_t content_size; 98 | uint32_t unk_0x420; 99 | uint32_t unk_0x424; 100 | uint32_t unk_0x428; 101 | uint32_t unk_0x42C; 102 | uint32_t unk_0x430; 103 | uint32_t unk_0x434; 104 | uint32_t unk_0x438; 105 | uint32_t unk_0x43C; 106 | uint8_t content_digest[0x20]; 107 | uint8_t content_one_block_digest[0x20]; 108 | } __attribute__((packed)); 109 | 110 | struct cnt_pkg_table_entry { 111 | uint32_t type; 112 | uint32_t unk1; 113 | uint32_t flags1; 114 | uint32_t flags2; 115 | uint32_t offset; 116 | uint32_t size; 117 | uint32_t unk2; 118 | uint32_t unk3; 119 | } __attribute__((packed)); 120 | 121 | // Internal structure. 122 | struct file_entry { 123 | int offset; 124 | int size; 125 | char *name; 126 | }; 127 | 128 | // Helper functions. 129 | static inline uint16_t bswap_16(uint16_t val) 130 | { 131 | return ((val & (uint16_t)0x00ffU) << 8) 132 | | ((val & (uint16_t)0xff00U) >> 8); 133 | } 134 | 135 | static inline uint32_t bswap_32(uint32_t val) 136 | { 137 | return ((val & (uint32_t)0x000000ffUL) << 24) 138 | | ((val & (uint32_t)0x0000ff00UL) << 8) 139 | | ((val & (uint32_t)0x00ff0000UL) >> 8) 140 | | ((val & (uint32_t)0xff000000UL) >> 24); 141 | } 142 | 143 | static inline uint64_t bswap_64(uint64_t val) 144 | { 145 | return ((val & (uint64_t)0x00000000000000ffULL) << 56) 146 | | ((val & (uint64_t)0x000000000000ff00ULL) << 40) 147 | | ((val & (uint64_t)0x0000000000ff0000ULL) << 24) 148 | | ((val & (uint64_t)0x00000000ff000000ULL) << 8) 149 | | ((val & (uint64_t)0x000000ff00000000ULL) >> 8) 150 | | ((val & (uint64_t)0x0000ff0000000000ULL) >> 24) 151 | | ((val & (uint64_t)0x00ff000000000000ULL) >> 40) 152 | | ((val & (uint64_t)0xff00000000000000ULL) >> 56); 153 | } 154 | 155 | char *read_string(FILE* f) 156 | { 157 | char *string = malloc(sizeof(char) * 256); 158 | int c; 159 | int length = 0; 160 | if(!string) return string; 161 | while((c = fgetc(f)) != '\00') 162 | { 163 | string[length++] = c; 164 | } 165 | string[length++] = '\0'; 166 | 167 | return realloc(string, sizeof(char) * length); 168 | } 169 | 170 | char *build_path(const char *str, char c, const char *r) 171 | { 172 | int count = 0; 173 | const char *tmp; 174 | for (tmp = str; *tmp; tmp++) { 175 | count += (*tmp == c); 176 | } 177 | 178 | int rlen = strlen(r); 179 | char *res = malloc(strlen(str) + (rlen - 1) * count + 1); 180 | char *ptr = res; 181 | for (tmp = str; *tmp; tmp++) { 182 | if (*tmp == c) { 183 | MKDIR(res, S_IRWXU); 184 | memcpy(ptr, r, rlen); 185 | ptr += rlen; 186 | } else { 187 | *ptr++ = *tmp; 188 | } 189 | } 190 | *ptr = 0; 191 | return res; 192 | } 193 | 194 | typedef struct { 195 | uint32_t type; 196 | char *name; 197 | } pkg_entry_value; 198 | 199 | char *get_entry_name_by_type(uint32_t type) 200 | { 201 | pkg_entry_value entries [] = { 202 | { PS4_PKG_ENTRY_TYPE_DIGEST_TABLE, "digest_table.bin" }, 203 | { PS4_PKG_ENTRY_TYPE_0x800, "unknown_entry_0x800.bin" }, 204 | { PS4_PKG_ENTRY_TYPE_0x200, "unknown_entry_0x200.bin" }, 205 | { PS4_PKG_ENTRY_TYPE_0x180, "unknown_entry_0x180.bin" }, 206 | { PS4_PKG_ENTRY_TYPE_META_TABLE, "meta_table.bin" }, 207 | { PS4_PKG_ENTRY_TYPE_NAME_TABLE, "name_table.bin" }, 208 | { 0x0400, "license.dat" }, 209 | { 0x0401, "license.info" }, 210 | { 0x1000, "param.sfo" }, 211 | { 0x1001, "playgo-chunk.dat" }, 212 | { 0x1002, "playgo-chunk.sha" }, 213 | { 0x1003, "playgo-manifest.xml" }, 214 | { 0x1004, "pronunciation.xml" }, 215 | { 0x1005, "pronunciation.sig" }, 216 | { 0x1006, "pic1.png" }, 217 | { 0x1008, "app/playgo-chunk.dat" }, 218 | { 0x1200, "icon0.png" }, 219 | { 0x1220, "pic0.png" }, 220 | { 0x1240, "snd0.at9" }, 221 | { 0x1260, "changeinfo/changeinfo.xml" } 222 | }; 223 | char *entry_name = NULL; 224 | size_t i; 225 | for (i = 0; i < sizeof entries / sizeof entries[0]; i++) { 226 | if (type == entries[i].type) { 227 | entry_name = entries[i].name; 228 | break; 229 | } 230 | } 231 | 232 | return entry_name; 233 | } 234 | 235 | char *title(unsigned char *p) { 236 | sfo_header_t *h = malloc(0x20); 237 | indexTableEntry_t *e = malloc(0x10); 238 | int i; 239 | 240 | char *title = NULL; 241 | char *title_id = NULL; 242 | char *version = NULL; 243 | 244 | memcpy(h, p, 0x20); 245 | for(i = 0; i < h->indexTableEntries; i++) { 246 | memcpy(e, p + 0x14 + (i * 0x10), 0x10); 247 | 248 | unsigned char *key; 249 | key = malloc(0x16); 250 | memcpy(key, p + h->keyTableOffset + e->keyOffset, 0x16); 251 | if (!strncmp(key, "TITLE", strlen(key))) { 252 | title = malloc(e->paramLen); 253 | sprintf(title, "%.*s", e->paramLen, p + h->dataTableOffset + e->dataOffset); 254 | } else if (!strncmp(key, "TITLE_ID", strlen(key))) { 255 | title_id = malloc(e->paramLen); 256 | sprintf(title_id, "%.*s", e->paramLen, p + h->dataTableOffset + e->dataOffset); 257 | } else if (!strncmp(key, "VERSION", strlen(key))) { 258 | version = malloc(e->paramLen); 259 | sprintf(version, "%.*s", e->paramLen, p + h->dataTableOffset + e->dataOffset); 260 | } 261 | } 262 | 263 | if ( title && title_id && version) { 264 | char *res = malloc(256); 265 | sprintf(res, "%s - (%s) (%s).pkg", title, title_id, version); 266 | return res; 267 | } else { 268 | return NULL; 269 | } 270 | 271 | 272 | } 273 | 274 | 275 | int main(int argc, char *argv[]) 276 | { 277 | if (argc < 2) { 278 | printf("Usage: unpkg [PKG FILE]\n"); 279 | return 0; 280 | } 281 | 282 | FILE *in = NULL; 283 | FILE *out = NULL; 284 | struct cnt_pkg_main_header m_header; 285 | struct cnt_pkg_content_header c_header; 286 | memset(&m_header, 0, sizeof(struct cnt_pkg_main_header)); 287 | memset(&c_header, 0, sizeof(struct cnt_pkg_content_header)); 288 | 289 | if ((in = fopen(argv[1], "rb")) == NULL) { 290 | printf("File not found!\n"); 291 | perror(argv[1]); 292 | return 0; 293 | } 294 | 295 | // Read in the main CNT header (size seems to be 0x180 with 4 hashes included). 296 | fseek(in, 0, SEEK_SET); 297 | fread(&m_header, 1, 0x180, in); 298 | 299 | if (m_header.magic != PS4_PKG_MAGIC) { 300 | printf("Invalid PS4 PKG file!\n"); 301 | return 0; 302 | } 303 | 304 | // Seek to offset 0x400 and read content associated header (size seems to be 0x80 with 2 hashes included). 305 | fseek(in, 0x400, SEEK_SET); 306 | fread(&c_header, 1, 0x80, in); 307 | 308 | // Locate the entry table and list each type of section inside the PKG/CNT file. 309 | fseek(in, bswap_32(m_header.file_table_offset), SEEK_SET); 310 | 311 | struct cnt_pkg_table_entry entries[m_header.table_entries_num]; 312 | int i; 313 | for (i = 0; i < bswap_16(m_header.table_entries_num); i++) { 314 | fread(&entries[i], 1, 0x20, in); 315 | } 316 | 317 | // Vars for file name listing. 318 | struct file_entry entry_files[bswap_16(m_header.table_entries_num)]; 319 | char *file_name_list[256]; 320 | char *unknown_file_name; 321 | int file_name_index = 0; 322 | int unknown_file_count = 0; 323 | int file_count = 0; 324 | 325 | // Vars for entry mapping. 326 | int entry_size; 327 | int entry_offset; 328 | unsigned char *entry_digests; 329 | 330 | // Vars for calculating SHA-256 hashes. 331 | unsigned char *main_entries_data = NULL; 332 | unsigned char *main_entries_sub_data = NULL; 333 | unsigned char *digest_table_data = NULL; 334 | unsigned char *body_data = NULL; 335 | unsigned char *content_one_block_data = NULL; 336 | unsigned char *content_data = NULL; 337 | 338 | int main_entries_data_size = 0; 339 | int main_entries_sub_data_size = 0; 340 | int digest_table_data_size = 0; 341 | int body_data_size = 0; 342 | 343 | unsigned char computed_main_entries_digest[0x20]; 344 | unsigned char computed_main_entries_sub_digest[0x20]; 345 | unsigned char computed_digest_table_digest[0x20]; 346 | unsigned char computed_body_digest[0x20]; 347 | unsigned char computed_content_one_block_digest[0x20]; 348 | unsigned char computed_content_digest[0x20]; 349 | 350 | int block_size = 0x10000; 351 | int num_blocks = (c_header.content_size > 0) ? 1 + ((c_header.content_size - 1) / block_size) : 0; 352 | 353 | // Var for file writing. 354 | unsigned char *entry_file_data; 355 | 356 | // Search through the data entries and locate the name table entry. 357 | // This section should keep relevant strings for internal files inside the PKG/CNT file. 358 | for (i = 0; i < bswap_16(m_header.table_entries_num); i++) { 359 | if (bswap_32(entries[i].type) == PS4_PKG_ENTRY_TYPE_NAME_TABLE) { 360 | fseek(in, bswap_32(entries[i].offset) + 1, SEEK_SET); 361 | while ((file_name_list[file_name_index] = read_string(in))[0] != '\0') { 362 | file_name_index++; 363 | } 364 | } 365 | } 366 | 367 | // Search through the data entries and locate file entries. 368 | // These entries need to be mapped with the names collected from the name table. 369 | for (i = 0; i < bswap_16(m_header.table_entries_num); i++) { 370 | // Use a predefined list for most file names. 371 | entry_files[i].name = get_entry_name_by_type(bswap_32(entries[i].type)); 372 | entry_files[i].offset = bswap_32(entries[i].offset); 373 | entry_files[i].size = bswap_32(entries[i].size); 374 | 375 | if (((bswap_32(entries[i].type) & PS4_PKG_ENTRY_TYPE_FILE1) == PS4_PKG_ENTRY_TYPE_FILE1) 376 | || (((bswap_32(entries[i].type) & PS4_PKG_ENTRY_TYPE_FILE2) == PS4_PKG_ENTRY_TYPE_FILE2))) { 377 | // If a file was found and it's name is not on the predefined list, try to map it with 378 | // a name from the name table. 379 | if (entry_files[i].name == NULL) { 380 | entry_files[i].name = file_name_list[file_count]; 381 | } 382 | file_count++; 383 | } else { 384 | // If everything failed, give a custom unknown tag to the file. 385 | if (entry_files[i].name == NULL) { 386 | unknown_file_name = (char *)malloc(256); 387 | sprintf(unknown_file_name, "unknown_file_%d.bin", ++unknown_file_count); 388 | entry_files[i].name = unknown_file_name; 389 | } 390 | } 391 | } 392 | 393 | // Search through the data entries and generate SHA-256 hashes for checking with the hash table. 394 | for (i = 0; i < bswap_16(m_header.table_entries_num); i++) { 395 | // Calculate hash for the digest table. 396 | if ((bswap_32(entries[i].type) == PS4_PKG_ENTRY_TYPE_DIGEST_TABLE)) { 397 | entry_size = bswap_32(entries[i].size); 398 | entry_offset = bswap_32(entries[i].offset); 399 | entry_digests = (unsigned char *)realloc(NULL, entry_size); 400 | 401 | fseek(in, entry_offset, SEEK_SET); 402 | fread(entry_digests, 1, entry_size, in); 403 | 404 | digest_table_data_size += entry_size; 405 | digest_table_data = (unsigned char *)realloc(NULL, digest_table_data_size); 406 | *digest_table_data += *entry_digests; 407 | } 408 | } 409 | sha2(digest_table_data, digest_table_data_size, computed_digest_table_digest, 0); 410 | 411 | for (i = 0; i < bswap_16(m_header.table_entries_num); i++) { 412 | // Calculate first hash for the main entries. 413 | if ((bswap_32(entries[i].type) == PS4_PKG_ENTRY_TYPE_DIGEST_TABLE) 414 | || (bswap_32(entries[i].type) == PS4_PKG_ENTRY_TYPE_0x800) 415 | || (bswap_32(entries[i].type) == PS4_PKG_ENTRY_TYPE_0x200) 416 | || (bswap_32(entries[i].type) == PS4_PKG_ENTRY_TYPE_0x180) 417 | || (bswap_32(entries[i].type) == PS4_PKG_ENTRY_TYPE_META_TABLE)) 418 | { 419 | entry_size = bswap_32(entries[i].size); 420 | entry_offset = bswap_32(entries[i].offset); 421 | entry_digests = (unsigned char *)realloc(NULL, entry_size); 422 | 423 | fseek(in, entry_offset, SEEK_SET); 424 | fread(entry_digests, 1, entry_size, in); 425 | 426 | main_entries_data_size += entry_size; 427 | main_entries_data = (unsigned char *)realloc(NULL, main_entries_data_size); 428 | *main_entries_data += *entry_digests; 429 | } 430 | } 431 | sha2(main_entries_data, main_entries_data_size, computed_main_entries_digest, 0); 432 | 433 | for (i = 0; i < bswap_16(m_header.table_entries_num); i++) { 434 | // Calculate second hash for the main entries. 435 | if ((bswap_32(entries[i].type) == PS4_PKG_ENTRY_TYPE_0x800) 436 | || (bswap_32(entries[i].type) == PS4_PKG_ENTRY_TYPE_0x200) 437 | || (bswap_32(entries[i].type) == PS4_PKG_ENTRY_TYPE_0x180) 438 | || (bswap_32(entries[i].type) == PS4_PKG_ENTRY_TYPE_META_TABLE)) 439 | { 440 | if ((bswap_32(entries[i].type) == PS4_PKG_ENTRY_TYPE_META_TABLE)) { 441 | entry_size = bswap_16(m_header.system_entries_num) * 0x20; 442 | } else { 443 | entry_size = bswap_32(entries[i].size); 444 | } 445 | entry_offset = bswap_32(entries[i].offset); 446 | entry_digests = (unsigned char *)realloc(NULL, entry_size); 447 | 448 | fseek(in, entry_offset, SEEK_SET); 449 | fread(entry_digests, 1, entry_size, in); 450 | 451 | main_entries_sub_data_size += entry_size; 452 | main_entries_sub_data = (unsigned char *)realloc(NULL, main_entries_sub_data_size); 453 | *main_entries_sub_data += *entry_digests; 454 | } 455 | } 456 | sha2(main_entries_sub_data, main_entries_sub_data_size, computed_main_entries_sub_digest, 0); 457 | 458 | // Calculate hash for file body. 459 | body_data_size = m_header.body_size; 460 | body_data = (unsigned char *)malloc(body_data_size); 461 | fseek(in, m_header.body_offset, SEEK_SET); 462 | fread(body_data, 1, body_data_size, in); 463 | sha2(body_data, body_data_size, computed_body_digest, 0); 464 | 465 | // Calculate hash for one block of the content section. 466 | content_one_block_data = (unsigned char *)malloc(block_size); 467 | fseek(in, c_header.content_offset, SEEK_SET); 468 | fread(content_one_block_data, 1, block_size, in); 469 | sha2(content_one_block_data, block_size, computed_content_one_block_digest, 0); 470 | 471 | // Calculate hash for the entire content section. 472 | content_data = (unsigned char *)malloc(block_size); 473 | fseek(in, c_header.content_offset, SEEK_SET); 474 | 475 | int bytes_left = c_header.content_size; 476 | int current_size; 477 | int b; 478 | sha2_context ctx; 479 | sha2_starts(&ctx, 0); 480 | for (b = 0; b < num_blocks; b++) { 481 | current_size = (bytes_left > block_size) ? block_size : bytes_left; 482 | fread(content_data, 1, current_size, in); 483 | sha2_update(&ctx, content_data, current_size); 484 | bytes_left -= block_size; 485 | } 486 | sha2_finish(&ctx, computed_content_digest); 487 | 488 | // Set up the output directory for file writing. 489 | char dest_path[256]; 490 | 491 | // Search through the entries for mapped file data and output it. 492 | for (i = 0; i < bswap_16(m_header.table_entries_num); i++) { 493 | 494 | if ( entry_files[i].name == "param.sfo" ) { 495 | entry_file_data = (unsigned char *)realloc(NULL, entry_files[i].size); 496 | 497 | fseek(in, entry_files[i].offset, SEEK_SET); 498 | fread(entry_file_data, 1, entry_files[i].size, in); 499 | char *t = title(entry_file_data); 500 | if ( t ) { 501 | fclose(in); 502 | printf("RENAME: %d\n", rename(argv[1], t)); 503 | return 0; 504 | } 505 | } 506 | } 507 | 508 | // Clean up. 509 | fclose(in); 510 | 511 | printf("Finished!\n"); 512 | 513 | return 0; 514 | } 515 | -------------------------------------------------------------------------------- /genidx.c: -------------------------------------------------------------------------------- 1 | /* CrazyVoid Generate PS4 IDX File 2 | * - 07/24/2017 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #ifdef WIN32 19 | #include "mingw_mmap.h" 20 | #include 21 | #else 22 | #include 23 | #endif 24 | 25 | #include "tools.h" 26 | 27 | u8 magicPreset[0x9] = { 0x72, 0x69, 0x64, 0x78, 0x01, 0x00, 0x00, 0x00, 0x01 }; 28 | u8 padding1Preset[0x4] = { 0x00, 0x00, 0x00, 0x00 }; 29 | u8 unknownPreset[0x16] = { 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 30 | u8 padding2Preset[0x16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; 31 | 32 | u8 outputFile[0x50]; 33 | 34 | int generateIDX(char *CONTENT_ID, char *ENTITLEMENT_LABEL) 35 | { 36 | int ret; 37 | if(strlen(CONTENT_ID) == 19) 38 | { 39 | if(strlen(ENTITLEMENT_LABEL) == 16) 40 | { 41 | char outputFilename[30] = "fake"; 42 | strcat(outputFilename, CONTENT_ID); 43 | strcat(outputFilename, ".idx"); 44 | printf("Output : %s\n\n", outputFilename); 45 | 46 | memcpy(outputFile, magicPreset, 9); 47 | memcpy(outputFile+9, CONTENT_ID, 19); 48 | memcpy(outputFile+28, padding1Preset, 4); 49 | memcpy(outputFile+32, ENTITLEMENT_LABEL, 16); 50 | memcpy(outputFile+48, unknownPreset, 16); 51 | memcpy(outputFile+64, padding2Preset, 16); 52 | 53 | FILE *fp; 54 | fp = fopen(outputFilename, "w+"); 55 | fwrite(outputFile, sizeof(outputFile), 1, fp); 56 | fclose(fp); 57 | ret = 0; 58 | } 59 | else 60 | { 61 | printf("[ERROR] Invalid length of ENTITLEMENT LABLE\nFailed to create IDX\n\n"); 62 | ret = 1; 63 | } 64 | } 65 | else 66 | { 67 | printf("[ERROR] Invalid length of CONTENT ID\nFailed to create IDX\n\n"); 68 | ret = 2; 69 | } 70 | 71 | return ret; 72 | } 73 | 74 | void help_Output() 75 | { 76 | printf("====PS4 IDX Generator - CrazyVoid======\n"); 77 | #ifdef WIN32 78 | printf("genidx.exe CONTENT_ID ENTITLEMENT_LABEL\n\n"); 79 | #else 80 | printf("./genidx CONTENT_ID ENTITLEMENT_LABEL\n\n"); 81 | #endif 82 | printf("[ERROR] INCORRECT AMOUNT OF ARGUMENTS\n\n"); 83 | } 84 | 85 | int main(int argc, char *argv[]) 86 | { 87 | int ret; 88 | 89 | 90 | if (argc != 3) 91 | { 92 | help_Output(); 93 | ret = 3; 94 | } 95 | else 96 | { 97 | // Dont change this line, stealing others work is not nice :) 98 | // If you add code, feel free to add your name. 99 | printf("[CrazyVoid] IDX Generator v0.1\n"); 100 | ret = generateIDX(argv[1], argv[2]); 101 | } 102 | 103 | return ret; 104 | } 105 | -------------------------------------------------------------------------------- /mingw_mmap.c: -------------------------------------------------------------------------------- 1 | /** This code is adapted from: 2 | * https://kerneltrap.org/mailarchive/git/2008/11/21/4186494 3 | * Original code by Vasyl Vavrychuk. 4 | * 5 | * This file is part of Rockstars. 6 | * Coded in Hungarian Notation so it looks stupid. :D 7 | * 8 | * If you haven't seen the header file, call mmap() and munmap() not the 9 | * function names below! 10 | */ 11 | 12 | #ifdef __MINGW32__ 13 | 14 | #include 15 | #include "mingw_mmap.h" 16 | 17 | extern int getpagesize(void); 18 | 19 | /** 20 | * Use CreateFileMapping and MapViewOfFile to simulate POSIX mmap(). 21 | * Why Microsoft won't just implement these is beyond everyone's comprehension. 22 | * @return pointer or NULL 23 | */ 24 | void *mingw_mmap(void *pStart, size_t sLength, int nProt, int nFlags, int nFd, off_t oOffset) { 25 | (void)nProt; 26 | HANDLE hHandle; 27 | 28 | if (pStart != NULL || !(nFlags & MAP_PRIVATE)) { 29 | printf("Invalid usage of mingw_mmap"); 30 | return NULL; 31 | } 32 | 33 | if (oOffset % getpagesize() != 0) { 34 | printf("Offset does not match the memory allocation granularity"); 35 | return NULL; 36 | } 37 | 38 | hHandle = CreateFileMapping((HANDLE)_get_osfhandle(nFd), NULL, PAGE_WRITECOPY, 0, 0, NULL); 39 | if (hHandle != NULL) { 40 | pStart = MapViewOfFile(hHandle, FILE_MAP_COPY, 0, oOffset, sLength); 41 | } 42 | 43 | return pStart; 44 | } 45 | 46 | /** 47 | * Use UnmapViewOfFile to undo mmap() above. 48 | * @param pStart 49 | * @param length - Not used, kept for compatibility. 50 | * @return boolean; no checks are performed. 51 | */ 52 | int mingw_munmap(void *pStart, size_t sLength) { 53 | (void)sLength; 54 | 55 | if (UnmapViewOfFile(pStart) != 0) 56 | return FALSE; 57 | 58 | return TRUE; 59 | } 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /mingw_mmap.h: -------------------------------------------------------------------------------- 1 | /** This code is adapted from: 2 | * https://kerneltrap.org/mailarchive/git/2008/11/21/4186494 3 | * Original code by Vasyl Vavrychuk. 4 | * 5 | * This file is part of Rockstars. 6 | * Coded in Hungarian Notation so it looks stupid. :D 7 | * 8 | * If you read nothing from this file, know NOT to call the function names 9 | * below but the original mmap() and munmap() functions. 10 | */ 11 | 12 | #ifndef _MINGW_MMAP_H 13 | #define _MINGW_MMAP_H 14 | 15 | #include 16 | #include 17 | 18 | #define PROT_READ 1 19 | #define PROT_WRITE 2 20 | #define MAP_SHARED 2 21 | #define MAP_PRIVATE 3 22 | 23 | void *mingw_mmap(void *pStart, size_t sLength, int nProt, int nFlags, int nFd, off_t oOffset); 24 | #define mmap mingw_mmap 25 | 26 | int mingw_munmap(void *pStart, size_t sLength); 27 | #define munmap mingw_munmap 28 | 29 | #endif /* _MINGW_MMAP_H */ 30 | -------------------------------------------------------------------------------- /pupunpack.c: -------------------------------------------------------------------------------- 1 | // Copyright (C) 2013 Hykem 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define PS4_PUP_PACK_MAGIC 0x32424C53 // SLB2 11 | #define PS4_PUP_PACK_HEADER_SIZE 0x200 12 | 13 | // Main PUP packed header (size == 0x20): 14 | // 0x00: 53 4C 42 32 -> SLB2 15 | // 0x04: 01 00 00 00 -> Version? 16 | // 0x08: 00 00 00 00 -> Unknown 17 | // 0x0C: 02 00 00 00 -> Number of PUP files in this pack 18 | // 0x10: 03 9F 09 00 -> Total number of blocks (512 bytes) 19 | // 0x14: 00 00 00 00 -> Unknown 20 | // 0x18: 00 00 00 00 -> Unknown 21 | // 0x1C: 00 00 00 00 -> Unknown 22 | struct pup_pack_header { 23 | uint32_t magic; 24 | uint32_t version; 25 | uint32_t unk1; 26 | uint32_t pup_file_num; 27 | uint32_t pup_total_block_num; 28 | uint32_t unk2; 29 | uint32_t unk3; 30 | uint32_t unk4; 31 | struct pup_entry *pup_entry_list; 32 | } __attribute__((packed)); 33 | 34 | // PUP file entry (size == 0x30): 35 | // 0x00: 01 00 00 00 -> Offset (in blocks, so 1 is the first block of 512 bytes after the header) 36 | // 0x04: 00 76 AE 0D -> File size 37 | // 0x08: 00 00 00 00 -> Unknown 38 | // 0x0C: 00 00 00 00 -> Unknown 39 | // 0x10: 50 53 34 55 -> File name (e.g.: PS4UPDATE1.PUP) (0x20 bytes) 40 | // 0x14: 50 44 41 54 41 | // 0x18: 45 31 2E 50 42 | // 0x1C: 55 50 00 00 43 | // 0x20: 00 00 00 00 44 | // 0x24: 00 00 00 00 45 | // 0x28: 00 00 00 00 46 | // 0x2C: 00 00 00 00 47 | struct pup_entry { 48 | uint32_t block_offset; 49 | uint32_t file_size; 50 | uint32_t unk1; 51 | uint32_t unk2; 52 | uint8_t file_name[32]; 53 | } __attribute__((packed)); 54 | 55 | int main (int argc, char *argv[]) 56 | { 57 | if (argc < 2) { 58 | printf("Usage: pupunpack [PS4UPDATE.PUP]\n"); 59 | return 0; 60 | } 61 | 62 | // Open file and set up the header struct. 63 | FILE *in; 64 | FILE *out; 65 | struct pup_pack_header header; 66 | memset(&header, 0, sizeof(struct pup_pack_header)); 67 | 68 | if ((in = fopen(argv[1], "rb")) == NULL ) { 69 | printf("File not found!\n"); 70 | return 0; 71 | } 72 | 73 | // Read in the main pack header. 74 | fseek(in, 0, SEEK_SET); 75 | fread(&header, 1, 0x20, in); 76 | 77 | if (header.magic != PS4_PUP_PACK_MAGIC) { 78 | printf("Invalid PS4 PUP file!\n"); 79 | return 0; 80 | } 81 | 82 | printf("PS4 PUP pack header:\n"); 83 | printf("- PUP pack magic: 0x%X\n", header.magic); 84 | printf("- PUP pack version: %i\n", header.version); 85 | printf("- PUP files in this pack: %i\n", header.pup_file_num); 86 | printf("- Total number of blocks: %i\n", header.pup_total_block_num); 87 | printf("\n"); 88 | 89 | // Read in all the PUP entries. 90 | int i; 91 | header.pup_entry_list = malloc(header.pup_file_num * sizeof(struct pup_entry)); 92 | 93 | for (i = 0; i < header.pup_file_num; ++i) { 94 | fread(&header.pup_entry_list[i], 1, 0x30, in); 95 | printf("PUP file entry %i:\n", i); 96 | printf("- Block offset: 0x%X\n", header.pup_entry_list[i].block_offset); 97 | printf("- PUP file size: %i\n", header.pup_entry_list[i].file_size); 98 | printf("- PUP file name: %s\n", header.pup_entry_list[i].file_name); 99 | printf("\n"); 100 | } 101 | 102 | // Create a large enough buffer and start copying the data. 103 | int buffer_size = PS4_PUP_PACK_HEADER_SIZE * 4; 104 | int pup_offset = PS4_PUP_PACK_HEADER_SIZE; 105 | uint8_t buffer[buffer_size]; 106 | 107 | int ii; 108 | for (ii = 0; ii < header.pup_file_num; ++ii) { 109 | fseek(in, pup_offset, SEEK_SET); 110 | out = fopen(header.pup_entry_list[ii].file_name, "wb"); 111 | 112 | printf("Dumping PUP file %s from offset 0x%X with size %i\n", header.pup_entry_list[ii].file_name, pup_offset, header.pup_entry_list[ii].file_size); 113 | 114 | int fsize = header.pup_entry_list[ii].file_size; 115 | pup_offset += (fsize + 511) & ~511; // 512 bytes alignment. 116 | 117 | while (fsize > 0) { 118 | if (fsize > buffer_size) { 119 | fread(buffer, 1, buffer_size, in); 120 | fwrite(buffer, 1, buffer_size, out); 121 | fsize -= buffer_size; 122 | } else { 123 | fread(buffer, 1, fsize, in); 124 | fwrite(buffer, 1, fsize, out); 125 | fsize = 0; 126 | } 127 | } 128 | 129 | fclose(out); 130 | } 131 | 132 | fclose(in); 133 | 134 | printf("Finished!\n"); 135 | 136 | return 0; 137 | } -------------------------------------------------------------------------------- /sha1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * sha1.c 3 | * 4 | * Copyright (C) 1998 5 | * Paul E. Jones 6 | * All Rights Reserved 7 | * 8 | * This software is licensed as "freeware." Permission to distribute 9 | * this software in source and binary forms is hereby granted without 10 | * a fee. THIS SOFTWARE IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED 11 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 12 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 13 | * THE AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DAMAGES RESULTING 14 | * FROM THE USE OF THIS SOFTWARE, EITHER DIRECTLY OR INDIRECTLY, INCLUDING, 15 | * BUT NOT LIMITED TO, LOSS OF DATA OR DATA BEING RENDERED INACCURATE. 16 | * 17 | ***************************************************************************** 18 | * $Id: sha1.c,v 1.2 2004/03/27 18:00:33 paulej Exp $ 19 | ***************************************************************************** 20 | * 21 | * Description: 22 | * This file implements the Secure Hashing Standard as defined 23 | * in FIPS PUB 180-1 published April 17, 1995. 24 | * 25 | * The Secure Hashing Standard, which uses the Secure Hashing 26 | * Algorithm (SHA), produces a 160-bit message digest for a 27 | * given data stream. In theory, it is highly improbable that 28 | * two messages will produce the same message digest. Therefore, 29 | * this algorithm can serve as a means of providing a "fingerprint" 30 | * for a message. 31 | * 32 | * Portability Issues: 33 | * SHA-1 is defined in terms of 32-bit "words". This code was 34 | * written with the expectation that the processor has at least 35 | * a 32-bit machine word size. If the machine word size is larger, 36 | * the code should still function properly. One caveat to that 37 | * is that the input functions taking characters and character 38 | * arrays assume that only 8 bits of information are stored in each 39 | * character. 40 | * 41 | * Caveats: 42 | * SHA-1 is designed to work with messages less than 2^64 bits 43 | * long. Although SHA-1 allows a message digest to be generated for 44 | * messages of any number of bits less than 2^64, this 45 | * implementation only works with messages with a length that is a 46 | * multiple of the size of an 8-bit character. 47 | * 48 | */ 49 | 50 | #include "sha1.h" 51 | 52 | /* 53 | * Define the circular shift macro 54 | */ 55 | #define SHA1CircularShift(bits,word) \ 56 | ((((word) << (bits)) & 0xFFFFFFFF) | \ 57 | ((word) >> (32-(bits)))) 58 | 59 | /* Function prototypes */ 60 | void SHA1ProcessMessageBlock(SHA1Context *); 61 | void SHA1PadMessage(SHA1Context *); 62 | 63 | /* 64 | * SHA1Reset 65 | * 66 | * Description: 67 | * This function will initialize the SHA1Context in preparation 68 | * for computing a new message digest. 69 | * 70 | * Parameters: 71 | * context: [in/out] 72 | * The context to reset. 73 | * 74 | * Returns: 75 | * Nothing. 76 | * 77 | * Comments: 78 | * 79 | */ 80 | void SHA1Reset(SHA1Context *context) 81 | { 82 | context->Length_Low = 0; 83 | context->Length_High = 0; 84 | context->Message_Block_Index = 0; 85 | 86 | context->Message_Digest[0] = 0x67452301; 87 | context->Message_Digest[1] = 0xEFCDAB89; 88 | context->Message_Digest[2] = 0x98BADCFE; 89 | context->Message_Digest[3] = 0x10325476; 90 | context->Message_Digest[4] = 0xC3D2E1F0; 91 | 92 | context->Computed = 0; 93 | context->Corrupted = 0; 94 | } 95 | 96 | /* 97 | * SHA1Result 98 | * 99 | * Description: 100 | * This function will return the 160-bit message digest into the 101 | * Message_Digest array within the SHA1Context provided 102 | * 103 | * Parameters: 104 | * context: [in/out] 105 | * The context to use to calculate the SHA-1 hash. 106 | * 107 | * Returns: 108 | * 1 if successful, 0 if it failed. 109 | * 110 | * Comments: 111 | * 112 | */ 113 | int SHA1Result(SHA1Context *context) 114 | { 115 | 116 | if (context->Corrupted) 117 | { 118 | return 0; 119 | } 120 | 121 | if (!context->Computed) 122 | { 123 | SHA1PadMessage(context); 124 | context->Computed = 1; 125 | } 126 | 127 | return 1; 128 | } 129 | 130 | /* 131 | * SHA1Input 132 | * 133 | * Description: 134 | * This function accepts an array of octets as the next portion of 135 | * the message. 136 | * 137 | * Parameters: 138 | * context: [in/out] 139 | * The SHA-1 context to update 140 | * message_array: [in] 141 | * An array of characters representing the next portion of the 142 | * message. 143 | * length: [in] 144 | * The length of the message in message_array 145 | * 146 | * Returns: 147 | * Nothing. 148 | * 149 | * Comments: 150 | * 151 | */ 152 | void SHA1Input( SHA1Context *context, 153 | const unsigned char *message_array, 154 | unsigned length) 155 | { 156 | if (!length) 157 | { 158 | return; 159 | } 160 | 161 | if (context->Computed || context->Corrupted) 162 | { 163 | context->Corrupted = 1; 164 | return; 165 | } 166 | 167 | while(length-- && !context->Corrupted) 168 | { 169 | context->Message_Block[context->Message_Block_Index++] = 170 | (*message_array & 0xFF); 171 | 172 | context->Length_Low += 8; 173 | /* Force it to 32 bits */ 174 | context->Length_Low &= 0xFFFFFFFF; 175 | if (context->Length_Low == 0) 176 | { 177 | context->Length_High++; 178 | /* Force it to 32 bits */ 179 | context->Length_High &= 0xFFFFFFFF; 180 | if (context->Length_High == 0) 181 | { 182 | /* Message is too long */ 183 | context->Corrupted = 1; 184 | } 185 | } 186 | 187 | if (context->Message_Block_Index == 64) 188 | { 189 | SHA1ProcessMessageBlock(context); 190 | } 191 | 192 | message_array++; 193 | } 194 | } 195 | 196 | /* 197 | * SHA1ProcessMessageBlock 198 | * 199 | * Description: 200 | * This function will process the next 512 bits of the message 201 | * stored in the Message_Block array. 202 | * 203 | * Parameters: 204 | * None. 205 | * 206 | * Returns: 207 | * Nothing. 208 | * 209 | * Comments: 210 | * Many of the variable names in the SHAContext, especially the 211 | * single character names, were used because those were the names 212 | * used in the publication. 213 | * 214 | * 215 | */ 216 | void SHA1ProcessMessageBlock(SHA1Context *context) 217 | { 218 | const unsigned K[] = /* Constants defined in SHA-1 */ 219 | { 220 | 0x5A827999, 221 | 0x6ED9EBA1, 222 | 0x8F1BBCDC, 223 | 0xCA62C1D6 224 | }; 225 | int t; /* Loop counter */ 226 | unsigned temp; /* Temporary word value */ 227 | unsigned W[80]; /* Word sequence */ 228 | unsigned A, B, C, D, E; /* Word buffers */ 229 | 230 | /* 231 | * Initialize the first 16 words in the array W 232 | */ 233 | for(t = 0; t < 16; t++) 234 | { 235 | W[t] = ((unsigned) context->Message_Block[t * 4]) << 24; 236 | W[t] |= ((unsigned) context->Message_Block[t * 4 + 1]) << 16; 237 | W[t] |= ((unsigned) context->Message_Block[t * 4 + 2]) << 8; 238 | W[t] |= ((unsigned) context->Message_Block[t * 4 + 3]); 239 | } 240 | 241 | for(t = 16; t < 80; t++) 242 | { 243 | W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]); 244 | } 245 | 246 | A = context->Message_Digest[0]; 247 | B = context->Message_Digest[1]; 248 | C = context->Message_Digest[2]; 249 | D = context->Message_Digest[3]; 250 | E = context->Message_Digest[4]; 251 | 252 | for(t = 0; t < 20; t++) 253 | { 254 | temp = SHA1CircularShift(5,A) + 255 | ((B & C) | ((~B) & D)) + E + W[t] + K[0]; 256 | temp &= 0xFFFFFFFF; 257 | E = D; 258 | D = C; 259 | C = SHA1CircularShift(30,B); 260 | B = A; 261 | A = temp; 262 | } 263 | 264 | for(t = 20; t < 40; t++) 265 | { 266 | temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1]; 267 | temp &= 0xFFFFFFFF; 268 | E = D; 269 | D = C; 270 | C = SHA1CircularShift(30,B); 271 | B = A; 272 | A = temp; 273 | } 274 | 275 | for(t = 40; t < 60; t++) 276 | { 277 | temp = SHA1CircularShift(5,A) + 278 | ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2]; 279 | temp &= 0xFFFFFFFF; 280 | E = D; 281 | D = C; 282 | C = SHA1CircularShift(30,B); 283 | B = A; 284 | A = temp; 285 | } 286 | 287 | for(t = 60; t < 80; t++) 288 | { 289 | temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3]; 290 | temp &= 0xFFFFFFFF; 291 | E = D; 292 | D = C; 293 | C = SHA1CircularShift(30,B); 294 | B = A; 295 | A = temp; 296 | } 297 | 298 | context->Message_Digest[0] = 299 | (context->Message_Digest[0] + A) & 0xFFFFFFFF; 300 | context->Message_Digest[1] = 301 | (context->Message_Digest[1] + B) & 0xFFFFFFFF; 302 | context->Message_Digest[2] = 303 | (context->Message_Digest[2] + C) & 0xFFFFFFFF; 304 | context->Message_Digest[3] = 305 | (context->Message_Digest[3] + D) & 0xFFFFFFFF; 306 | context->Message_Digest[4] = 307 | (context->Message_Digest[4] + E) & 0xFFFFFFFF; 308 | 309 | context->Message_Block_Index = 0; 310 | } 311 | 312 | /* 313 | * SHA1PadMessage 314 | * 315 | * Description: 316 | * According to the standard, the message must be padded to an even 317 | * 512 bits. The first padding bit must be a '1'. The last 64 318 | * bits represent the length of the original message. All bits in 319 | * between should be 0. This function will pad the message 320 | * according to those rules by filling the Message_Block array 321 | * accordingly. It will also call SHA1ProcessMessageBlock() 322 | * appropriately. When it returns, it can be assumed that the 323 | * message digest has been computed. 324 | * 325 | * Parameters: 326 | * context: [in/out] 327 | * The context to pad 328 | * 329 | * Returns: 330 | * Nothing. 331 | * 332 | * Comments: 333 | * 334 | */ 335 | void SHA1PadMessage(SHA1Context *context) 336 | { 337 | /* 338 | * Check to see if the current message block is too small to hold 339 | * the initial padding bits and length. If so, we will pad the 340 | * block, process it, and then continue padding into a second 341 | * block. 342 | */ 343 | if (context->Message_Block_Index > 55) 344 | { 345 | context->Message_Block[context->Message_Block_Index++] = 0x80; 346 | while(context->Message_Block_Index < 64) 347 | { 348 | context->Message_Block[context->Message_Block_Index++] = 0; 349 | } 350 | 351 | SHA1ProcessMessageBlock(context); 352 | 353 | while(context->Message_Block_Index < 56) 354 | { 355 | context->Message_Block[context->Message_Block_Index++] = 0; 356 | } 357 | } 358 | else 359 | { 360 | context->Message_Block[context->Message_Block_Index++] = 0x80; 361 | while(context->Message_Block_Index < 56) 362 | { 363 | context->Message_Block[context->Message_Block_Index++] = 0; 364 | } 365 | } 366 | 367 | /* 368 | * Store the message length as the last 8 octets 369 | */ 370 | context->Message_Block[56] = (context->Length_High >> 24) & 0xFF; 371 | context->Message_Block[57] = (context->Length_High >> 16) & 0xFF; 372 | context->Message_Block[58] = (context->Length_High >> 8) & 0xFF; 373 | context->Message_Block[59] = (context->Length_High) & 0xFF; 374 | context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF; 375 | context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF; 376 | context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF; 377 | context->Message_Block[63] = (context->Length_Low) & 0xFF; 378 | 379 | SHA1ProcessMessageBlock(context); 380 | } 381 | -------------------------------------------------------------------------------- /sha1.h: -------------------------------------------------------------------------------- 1 | /* 2 | * sha1.h 3 | * 4 | * Copyright (C) 1998 5 | * Paul E. Jones 6 | * All Rights Reserved 7 | * 8 | * This software is licensed as "freeware." Permission to distribute 9 | * this software in source and binary forms is hereby granted without 10 | * a fee. THIS SOFTWARE IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED 11 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 12 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 13 | * THE AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DAMAGES RESULTING 14 | * FROM THE USE OF THIS SOFTWARE, EITHER DIRECTLY OR INDIRECTLY, INCLUDING, 15 | * BUT NOT LIMITED TO, LOSS OF DATA OR DATA BEING RENDERED INACCURATE. 16 | * 17 | * This software is licensed as "freeware." Permission to distribute 18 | * this software in source and binary forms is hereby granted without 19 | * a fee. THIS SOFTWARE IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED 20 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 | * THE AUTHOR SHALL NOT BE HELD LIABLE FOR ANY DAMAGES RESULTING 23 | * FROM THE USE OF THIS SOFTWARE, EITHER DIRECTLY OR INDIRECTLY, INCLUDING, 24 | * BUT NOT LIMITED TO, LOSS OF DATA OR DATA BEING RENDERED INACCURATE. 25 | * 26 | ***************************************************************************** 27 | * $Id: sha1.h,v 1.2 2004/03/27 18:00:33 paulej Exp $ 28 | ***************************************************************************** 29 | * 30 | * Description: 31 | * This class implements the Secure Hashing Standard as defined 32 | * in FIPS PUB 180-1 published April 17, 1995. 33 | * 34 | * Many of the variable names in the SHA1Context, especially the 35 | * single character names, were used because those were the names 36 | * used in the publication. 37 | * 38 | * Please read the file sha1.c for more information. 39 | * 40 | */ 41 | 42 | #ifndef _SHA1_H_ 43 | #define _SHA1_H_ 44 | 45 | /* 46 | * This structure will hold context information for the hashing 47 | * operation 48 | */ 49 | typedef struct SHA1Context 50 | { 51 | unsigned Message_Digest[5]; /* Message Digest (output) */ 52 | 53 | unsigned Length_Low; /* Message length in bits */ 54 | unsigned Length_High; /* Message length in bits */ 55 | 56 | unsigned char Message_Block[64]; /* 512-bit message blocks */ 57 | int Message_Block_Index; /* Index into message block array */ 58 | 59 | int Computed; /* Is the digest computed? */ 60 | int Corrupted; /* Is the message digest corruped? */ 61 | } SHA1Context; 62 | 63 | /* 64 | * Function Prototypes 65 | */ 66 | void SHA1Reset(SHA1Context *); 67 | int SHA1Result(SHA1Context *); 68 | void SHA1Input( SHA1Context *, 69 | const unsigned char *, 70 | unsigned); 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /sha2.c: -------------------------------------------------------------------------------- 1 | /* 2 | * FIPS-180-2 compliant SHA-256 implementation 3 | * 4 | * Copyright (C) 2006-2013, Brainspark B.V. 5 | * 6 | * This file is part of PolarSSL (http://www.polarssl.org) 7 | * Lead Maintainer: Paul Bakker 8 | * 9 | * All rights reserved. 10 | * 11 | * This program is free software; you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation; either version 2 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License along 22 | * with this program; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 24 | */ 25 | /* 26 | * The SHA-256 Secure Hash Standard was published by NIST in 2002. 27 | * 28 | * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf 29 | */ 30 | 31 | #include "sha2.h" 32 | 33 | #if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST) 34 | #include 35 | #endif 36 | 37 | #if !defined(POLARSSL_SHA2_ALT) 38 | 39 | /* 40 | * 32-bit integer manipulation macros (big endian) 41 | */ 42 | #ifndef GET_UINT32_BE 43 | #define GET_UINT32_BE(n,b,i) \ 44 | { \ 45 | (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ 46 | | ( (uint32_t) (b)[(i) + 1] << 16 ) \ 47 | | ( (uint32_t) (b)[(i) + 2] << 8 ) \ 48 | | ( (uint32_t) (b)[(i) + 3] ); \ 49 | } 50 | #endif 51 | 52 | #ifndef PUT_UINT32_BE 53 | #define PUT_UINT32_BE(n,b,i) \ 54 | { \ 55 | (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ 56 | (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ 57 | (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ 58 | (b)[(i) + 3] = (unsigned char) ( (n) ); \ 59 | } 60 | #endif 61 | 62 | /* 63 | * SHA-256 context setup 64 | */ 65 | void sha2_starts( sha2_context *ctx, int is224 ) 66 | { 67 | ctx->total[0] = 0; 68 | ctx->total[1] = 0; 69 | 70 | if( is224 == 0 ) 71 | { 72 | /* SHA-256 */ 73 | ctx->state[0] = 0x6A09E667; 74 | ctx->state[1] = 0xBB67AE85; 75 | ctx->state[2] = 0x3C6EF372; 76 | ctx->state[3] = 0xA54FF53A; 77 | ctx->state[4] = 0x510E527F; 78 | ctx->state[5] = 0x9B05688C; 79 | ctx->state[6] = 0x1F83D9AB; 80 | ctx->state[7] = 0x5BE0CD19; 81 | } 82 | else 83 | { 84 | /* SHA-224 */ 85 | ctx->state[0] = 0xC1059ED8; 86 | ctx->state[1] = 0x367CD507; 87 | ctx->state[2] = 0x3070DD17; 88 | ctx->state[3] = 0xF70E5939; 89 | ctx->state[4] = 0xFFC00B31; 90 | ctx->state[5] = 0x68581511; 91 | ctx->state[6] = 0x64F98FA7; 92 | ctx->state[7] = 0xBEFA4FA4; 93 | } 94 | 95 | ctx->is224 = is224; 96 | } 97 | 98 | void sha2_process( sha2_context *ctx, const unsigned char data[64] ) 99 | { 100 | uint32_t temp1, temp2, W[64]; 101 | uint32_t A, B, C, D, E, F, G, H; 102 | 103 | GET_UINT32_BE( W[ 0], data, 0 ); 104 | GET_UINT32_BE( W[ 1], data, 4 ); 105 | GET_UINT32_BE( W[ 2], data, 8 ); 106 | GET_UINT32_BE( W[ 3], data, 12 ); 107 | GET_UINT32_BE( W[ 4], data, 16 ); 108 | GET_UINT32_BE( W[ 5], data, 20 ); 109 | GET_UINT32_BE( W[ 6], data, 24 ); 110 | GET_UINT32_BE( W[ 7], data, 28 ); 111 | GET_UINT32_BE( W[ 8], data, 32 ); 112 | GET_UINT32_BE( W[ 9], data, 36 ); 113 | GET_UINT32_BE( W[10], data, 40 ); 114 | GET_UINT32_BE( W[11], data, 44 ); 115 | GET_UINT32_BE( W[12], data, 48 ); 116 | GET_UINT32_BE( W[13], data, 52 ); 117 | GET_UINT32_BE( W[14], data, 56 ); 118 | GET_UINT32_BE( W[15], data, 60 ); 119 | 120 | #define SHR(x,n) ((x & 0xFFFFFFFF) >> n) 121 | #define ROTR(x,n) (SHR(x,n) | (x << (32 - n))) 122 | 123 | #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3)) 124 | #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10)) 125 | 126 | #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22)) 127 | #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25)) 128 | 129 | #define F0(x,y,z) ((x & y) | (z & (x | y))) 130 | #define F1(x,y,z) (z ^ (x & (y ^ z))) 131 | 132 | #define R(t) \ 133 | ( \ 134 | W[t] = S1(W[t - 2]) + W[t - 7] + \ 135 | S0(W[t - 15]) + W[t - 16] \ 136 | ) 137 | 138 | #define P(a,b,c,d,e,f,g,h,x,K) \ 139 | { \ 140 | temp1 = h + S3(e) + F1(e,f,g) + K + x; \ 141 | temp2 = S2(a) + F0(a,b,c); \ 142 | d += temp1; h = temp1 + temp2; \ 143 | } 144 | 145 | A = ctx->state[0]; 146 | B = ctx->state[1]; 147 | C = ctx->state[2]; 148 | D = ctx->state[3]; 149 | E = ctx->state[4]; 150 | F = ctx->state[5]; 151 | G = ctx->state[6]; 152 | H = ctx->state[7]; 153 | 154 | P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 ); 155 | P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 ); 156 | P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF ); 157 | P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 ); 158 | P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B ); 159 | P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 ); 160 | P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 ); 161 | P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 ); 162 | P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 ); 163 | P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01 ); 164 | P( G, H, A, B, C, D, E, F, W[10], 0x243185BE ); 165 | P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3 ); 166 | P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74 ); 167 | P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE ); 168 | P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7 ); 169 | P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174 ); 170 | P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1 ); 171 | P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786 ); 172 | P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6 ); 173 | P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC ); 174 | P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F ); 175 | P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA ); 176 | P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC ); 177 | P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA ); 178 | P( A, B, C, D, E, F, G, H, R(24), 0x983E5152 ); 179 | P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D ); 180 | P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8 ); 181 | P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7 ); 182 | P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3 ); 183 | P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147 ); 184 | P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351 ); 185 | P( B, C, D, E, F, G, H, A, R(31), 0x14292967 ); 186 | P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85 ); 187 | P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138 ); 188 | P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC ); 189 | P( F, G, H, A, B, C, D, E, R(35), 0x53380D13 ); 190 | P( E, F, G, H, A, B, C, D, R(36), 0x650A7354 ); 191 | P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB ); 192 | P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E ); 193 | P( B, C, D, E, F, G, H, A, R(39), 0x92722C85 ); 194 | P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1 ); 195 | P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B ); 196 | P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70 ); 197 | P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3 ); 198 | P( E, F, G, H, A, B, C, D, R(44), 0xD192E819 ); 199 | P( D, E, F, G, H, A, B, C, R(45), 0xD6990624 ); 200 | P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585 ); 201 | P( B, C, D, E, F, G, H, A, R(47), 0x106AA070 ); 202 | P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116 ); 203 | P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08 ); 204 | P( G, H, A, B, C, D, E, F, R(50), 0x2748774C ); 205 | P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5 ); 206 | P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3 ); 207 | P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A ); 208 | P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F ); 209 | P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3 ); 210 | P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE ); 211 | P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F ); 212 | P( G, H, A, B, C, D, E, F, R(58), 0x84C87814 ); 213 | P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208 ); 214 | P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA ); 215 | P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB ); 216 | P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7 ); 217 | P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2 ); 218 | 219 | ctx->state[0] += A; 220 | ctx->state[1] += B; 221 | ctx->state[2] += C; 222 | ctx->state[3] += D; 223 | ctx->state[4] += E; 224 | ctx->state[5] += F; 225 | ctx->state[6] += G; 226 | ctx->state[7] += H; 227 | } 228 | 229 | /* 230 | * SHA-256 process buffer 231 | */ 232 | void sha2_update( sha2_context *ctx, const unsigned char *input, size_t ilen ) 233 | { 234 | size_t fill; 235 | uint32_t left; 236 | 237 | if( ilen <= 0 ) 238 | return; 239 | 240 | left = ctx->total[0] & 0x3F; 241 | fill = 64 - left; 242 | 243 | ctx->total[0] += (uint32_t) ilen; 244 | ctx->total[0] &= 0xFFFFFFFF; 245 | 246 | if( ctx->total[0] < (uint32_t) ilen ) 247 | ctx->total[1]++; 248 | 249 | if( left && ilen >= fill ) 250 | { 251 | memcpy( (void *) (ctx->buffer + left), input, fill ); 252 | sha2_process( ctx, ctx->buffer ); 253 | input += fill; 254 | ilen -= fill; 255 | left = 0; 256 | } 257 | 258 | while( ilen >= 64 ) 259 | { 260 | sha2_process( ctx, input ); 261 | input += 64; 262 | ilen -= 64; 263 | } 264 | 265 | if( ilen > 0 ) 266 | memcpy( (void *) (ctx->buffer + left), input, ilen ); 267 | } 268 | 269 | static const unsigned char sha2_padding[64] = 270 | { 271 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 272 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 273 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 274 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 275 | }; 276 | 277 | /* 278 | * SHA-256 final digest 279 | */ 280 | void sha2_finish( sha2_context *ctx, unsigned char output[32] ) 281 | { 282 | uint32_t last, padn; 283 | uint32_t high, low; 284 | unsigned char msglen[8]; 285 | 286 | high = ( ctx->total[0] >> 29 ) 287 | | ( ctx->total[1] << 3 ); 288 | low = ( ctx->total[0] << 3 ); 289 | 290 | PUT_UINT32_BE( high, msglen, 0 ); 291 | PUT_UINT32_BE( low, msglen, 4 ); 292 | 293 | last = ctx->total[0] & 0x3F; 294 | padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); 295 | 296 | sha2_update( ctx, sha2_padding, padn ); 297 | sha2_update( ctx, msglen, 8 ); 298 | 299 | PUT_UINT32_BE( ctx->state[0], output, 0 ); 300 | PUT_UINT32_BE( ctx->state[1], output, 4 ); 301 | PUT_UINT32_BE( ctx->state[2], output, 8 ); 302 | PUT_UINT32_BE( ctx->state[3], output, 12 ); 303 | PUT_UINT32_BE( ctx->state[4], output, 16 ); 304 | PUT_UINT32_BE( ctx->state[5], output, 20 ); 305 | PUT_UINT32_BE( ctx->state[6], output, 24 ); 306 | 307 | if( ctx->is224 == 0 ) 308 | PUT_UINT32_BE( ctx->state[7], output, 28 ); 309 | } 310 | 311 | #endif /* !POLARSSL_SHA2_ALT */ 312 | 313 | /* 314 | * output = SHA-256( input buffer ) 315 | */ 316 | void sha2( const unsigned char *input, size_t ilen, 317 | unsigned char output[32], int is224 ) 318 | { 319 | sha2_context ctx; 320 | 321 | sha2_starts( &ctx, is224 ); 322 | sha2_update( &ctx, input, ilen ); 323 | sha2_finish( &ctx, output ); 324 | 325 | memset( &ctx, 0, sizeof( sha2_context ) ); 326 | } 327 | 328 | #if defined(POLARSSL_FS_IO) 329 | /* 330 | * output = SHA-256( file contents ) 331 | */ 332 | int sha2_file( const char *path, unsigned char output[32], int is224 ) 333 | { 334 | FILE *f; 335 | size_t n; 336 | sha2_context ctx; 337 | unsigned char buf[1024]; 338 | 339 | if( ( f = fopen( path, "rb" ) ) == NULL ) 340 | return( POLARSSL_ERR_SHA2_FILE_IO_ERROR ); 341 | 342 | sha2_starts( &ctx, is224 ); 343 | 344 | while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 ) 345 | sha2_update( &ctx, buf, n ); 346 | 347 | sha2_finish( &ctx, output ); 348 | 349 | memset( &ctx, 0, sizeof( sha2_context ) ); 350 | 351 | if( ferror( f ) != 0 ) 352 | { 353 | fclose( f ); 354 | return( POLARSSL_ERR_SHA2_FILE_IO_ERROR ); 355 | } 356 | 357 | fclose( f ); 358 | return( 0 ); 359 | } 360 | #endif /* POLARSSL_FS_IO */ 361 | 362 | /* 363 | * SHA-256 HMAC context setup 364 | */ 365 | void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, size_t keylen, 366 | int is224 ) 367 | { 368 | size_t i; 369 | unsigned char sum[32]; 370 | 371 | if( keylen > 64 ) 372 | { 373 | sha2( key, keylen, sum, is224 ); 374 | keylen = ( is224 ) ? 28 : 32; 375 | key = sum; 376 | } 377 | 378 | memset( ctx->ipad, 0x36, 64 ); 379 | memset( ctx->opad, 0x5C, 64 ); 380 | 381 | for( i = 0; i < keylen; i++ ) 382 | { 383 | ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] ); 384 | ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] ); 385 | } 386 | 387 | sha2_starts( ctx, is224 ); 388 | sha2_update( ctx, ctx->ipad, 64 ); 389 | 390 | memset( sum, 0, sizeof( sum ) ); 391 | } 392 | 393 | /* 394 | * SHA-256 HMAC process buffer 395 | */ 396 | void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, size_t ilen ) 397 | { 398 | sha2_update( ctx, input, ilen ); 399 | } 400 | 401 | /* 402 | * SHA-256 HMAC final digest 403 | */ 404 | void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] ) 405 | { 406 | int is224, hlen; 407 | unsigned char tmpbuf[32]; 408 | 409 | is224 = ctx->is224; 410 | hlen = ( is224 == 0 ) ? 32 : 28; 411 | 412 | sha2_finish( ctx, tmpbuf ); 413 | sha2_starts( ctx, is224 ); 414 | sha2_update( ctx, ctx->opad, 64 ); 415 | sha2_update( ctx, tmpbuf, hlen ); 416 | sha2_finish( ctx, output ); 417 | 418 | memset( tmpbuf, 0, sizeof( tmpbuf ) ); 419 | } 420 | 421 | /* 422 | * SHA-256 HMAC context reset 423 | */ 424 | void sha2_hmac_reset( sha2_context *ctx ) 425 | { 426 | sha2_starts( ctx, ctx->is224 ); 427 | sha2_update( ctx, ctx->ipad, 64 ); 428 | } 429 | 430 | /* 431 | * output = HMAC-SHA-256( hmac key, input buffer ) 432 | */ 433 | void sha2_hmac( const unsigned char *key, size_t keylen, 434 | const unsigned char *input, size_t ilen, 435 | unsigned char output[32], int is224 ) 436 | { 437 | sha2_context ctx; 438 | 439 | sha2_hmac_starts( &ctx, key, keylen, is224 ); 440 | sha2_hmac_update( &ctx, input, ilen ); 441 | sha2_hmac_finish( &ctx, output ); 442 | 443 | memset( &ctx, 0, sizeof( sha2_context ) ); 444 | } 445 | 446 | #if defined(POLARSSL_SELF_TEST) 447 | /* 448 | * FIPS-180-2 test vectors 449 | */ 450 | static unsigned char sha2_test_buf[3][57] = 451 | { 452 | { "abc" }, 453 | { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" }, 454 | { "" } 455 | }; 456 | 457 | static const int sha2_test_buflen[3] = 458 | { 459 | 3, 56, 1000 460 | }; 461 | 462 | static const unsigned char sha2_test_sum[6][32] = 463 | { 464 | /* 465 | * SHA-224 test vectors 466 | */ 467 | { 0x23, 0x09, 0x7D, 0x22, 0x34, 0x05, 0xD8, 0x22, 468 | 0x86, 0x42, 0xA4, 0x77, 0xBD, 0xA2, 0x55, 0xB3, 469 | 0x2A, 0xAD, 0xBC, 0xE4, 0xBD, 0xA0, 0xB3, 0xF7, 470 | 0xE3, 0x6C, 0x9D, 0xA7 }, 471 | { 0x75, 0x38, 0x8B, 0x16, 0x51, 0x27, 0x76, 0xCC, 472 | 0x5D, 0xBA, 0x5D, 0xA1, 0xFD, 0x89, 0x01, 0x50, 473 | 0xB0, 0xC6, 0x45, 0x5C, 0xB4, 0xF5, 0x8B, 0x19, 474 | 0x52, 0x52, 0x25, 0x25 }, 475 | { 0x20, 0x79, 0x46, 0x55, 0x98, 0x0C, 0x91, 0xD8, 476 | 0xBB, 0xB4, 0xC1, 0xEA, 0x97, 0x61, 0x8A, 0x4B, 477 | 0xF0, 0x3F, 0x42, 0x58, 0x19, 0x48, 0xB2, 0xEE, 478 | 0x4E, 0xE7, 0xAD, 0x67 }, 479 | 480 | /* 481 | * SHA-256 test vectors 482 | */ 483 | { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA, 484 | 0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23, 485 | 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C, 486 | 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD }, 487 | { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8, 488 | 0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39, 489 | 0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67, 490 | 0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 }, 491 | { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92, 492 | 0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67, 493 | 0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E, 494 | 0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 } 495 | }; 496 | 497 | /* 498 | * RFC 4231 test vectors 499 | */ 500 | static unsigned char sha2_hmac_test_key[7][26] = 501 | { 502 | { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B" 503 | "\x0B\x0B\x0B\x0B" }, 504 | { "Jefe" }, 505 | { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA" 506 | "\xAA\xAA\xAA\xAA" }, 507 | { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10" 508 | "\x11\x12\x13\x14\x15\x16\x17\x18\x19" }, 509 | { "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C" 510 | "\x0C\x0C\x0C\x0C" }, 511 | { "" }, /* 0xAA 131 times */ 512 | { "" } 513 | }; 514 | 515 | static const int sha2_hmac_test_keylen[7] = 516 | { 517 | 20, 4, 20, 25, 20, 131, 131 518 | }; 519 | 520 | static unsigned char sha2_hmac_test_buf[7][153] = 521 | { 522 | { "Hi There" }, 523 | { "what do ya want for nothing?" }, 524 | { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" 525 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" 526 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" 527 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" 528 | "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" }, 529 | { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" 530 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" 531 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" 532 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" 533 | "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" }, 534 | { "Test With Truncation" }, 535 | { "Test Using Larger Than Block-Size Key - Hash Key First" }, 536 | { "This is a test using a larger than block-size key " 537 | "and a larger than block-size data. The key needs to " 538 | "be hashed before being used by the HMAC algorithm." } 539 | }; 540 | 541 | static const int sha2_hmac_test_buflen[7] = 542 | { 543 | 8, 28, 50, 50, 20, 54, 152 544 | }; 545 | 546 | static const unsigned char sha2_hmac_test_sum[14][32] = 547 | { 548 | /* 549 | * HMAC-SHA-224 test vectors 550 | */ 551 | { 0x89, 0x6F, 0xB1, 0x12, 0x8A, 0xBB, 0xDF, 0x19, 552 | 0x68, 0x32, 0x10, 0x7C, 0xD4, 0x9D, 0xF3, 0x3F, 553 | 0x47, 0xB4, 0xB1, 0x16, 0x99, 0x12, 0xBA, 0x4F, 554 | 0x53, 0x68, 0x4B, 0x22 }, 555 | { 0xA3, 0x0E, 0x01, 0x09, 0x8B, 0xC6, 0xDB, 0xBF, 556 | 0x45, 0x69, 0x0F, 0x3A, 0x7E, 0x9E, 0x6D, 0x0F, 557 | 0x8B, 0xBE, 0xA2, 0xA3, 0x9E, 0x61, 0x48, 0x00, 558 | 0x8F, 0xD0, 0x5E, 0x44 }, 559 | { 0x7F, 0xB3, 0xCB, 0x35, 0x88, 0xC6, 0xC1, 0xF6, 560 | 0xFF, 0xA9, 0x69, 0x4D, 0x7D, 0x6A, 0xD2, 0x64, 561 | 0x93, 0x65, 0xB0, 0xC1, 0xF6, 0x5D, 0x69, 0xD1, 562 | 0xEC, 0x83, 0x33, 0xEA }, 563 | { 0x6C, 0x11, 0x50, 0x68, 0x74, 0x01, 0x3C, 0xAC, 564 | 0x6A, 0x2A, 0xBC, 0x1B, 0xB3, 0x82, 0x62, 0x7C, 565 | 0xEC, 0x6A, 0x90, 0xD8, 0x6E, 0xFC, 0x01, 0x2D, 566 | 0xE7, 0xAF, 0xEC, 0x5A }, 567 | { 0x0E, 0x2A, 0xEA, 0x68, 0xA9, 0x0C, 0x8D, 0x37, 568 | 0xC9, 0x88, 0xBC, 0xDB, 0x9F, 0xCA, 0x6F, 0xA8 }, 569 | { 0x95, 0xE9, 0xA0, 0xDB, 0x96, 0x20, 0x95, 0xAD, 570 | 0xAE, 0xBE, 0x9B, 0x2D, 0x6F, 0x0D, 0xBC, 0xE2, 571 | 0xD4, 0x99, 0xF1, 0x12, 0xF2, 0xD2, 0xB7, 0x27, 572 | 0x3F, 0xA6, 0x87, 0x0E }, 573 | { 0x3A, 0x85, 0x41, 0x66, 0xAC, 0x5D, 0x9F, 0x02, 574 | 0x3F, 0x54, 0xD5, 0x17, 0xD0, 0xB3, 0x9D, 0xBD, 575 | 0x94, 0x67, 0x70, 0xDB, 0x9C, 0x2B, 0x95, 0xC9, 576 | 0xF6, 0xF5, 0x65, 0xD1 }, 577 | 578 | /* 579 | * HMAC-SHA-256 test vectors 580 | */ 581 | { 0xB0, 0x34, 0x4C, 0x61, 0xD8, 0xDB, 0x38, 0x53, 582 | 0x5C, 0xA8, 0xAF, 0xCE, 0xAF, 0x0B, 0xF1, 0x2B, 583 | 0x88, 0x1D, 0xC2, 0x00, 0xC9, 0x83, 0x3D, 0xA7, 584 | 0x26, 0xE9, 0x37, 0x6C, 0x2E, 0x32, 0xCF, 0xF7 }, 585 | { 0x5B, 0xDC, 0xC1, 0x46, 0xBF, 0x60, 0x75, 0x4E, 586 | 0x6A, 0x04, 0x24, 0x26, 0x08, 0x95, 0x75, 0xC7, 587 | 0x5A, 0x00, 0x3F, 0x08, 0x9D, 0x27, 0x39, 0x83, 588 | 0x9D, 0xEC, 0x58, 0xB9, 0x64, 0xEC, 0x38, 0x43 }, 589 | { 0x77, 0x3E, 0xA9, 0x1E, 0x36, 0x80, 0x0E, 0x46, 590 | 0x85, 0x4D, 0xB8, 0xEB, 0xD0, 0x91, 0x81, 0xA7, 591 | 0x29, 0x59, 0x09, 0x8B, 0x3E, 0xF8, 0xC1, 0x22, 592 | 0xD9, 0x63, 0x55, 0x14, 0xCE, 0xD5, 0x65, 0xFE }, 593 | { 0x82, 0x55, 0x8A, 0x38, 0x9A, 0x44, 0x3C, 0x0E, 594 | 0xA4, 0xCC, 0x81, 0x98, 0x99, 0xF2, 0x08, 0x3A, 595 | 0x85, 0xF0, 0xFA, 0xA3, 0xE5, 0x78, 0xF8, 0x07, 596 | 0x7A, 0x2E, 0x3F, 0xF4, 0x67, 0x29, 0x66, 0x5B }, 597 | { 0xA3, 0xB6, 0x16, 0x74, 0x73, 0x10, 0x0E, 0xE0, 598 | 0x6E, 0x0C, 0x79, 0x6C, 0x29, 0x55, 0x55, 0x2B }, 599 | { 0x60, 0xE4, 0x31, 0x59, 0x1E, 0xE0, 0xB6, 0x7F, 600 | 0x0D, 0x8A, 0x26, 0xAA, 0xCB, 0xF5, 0xB7, 0x7F, 601 | 0x8E, 0x0B, 0xC6, 0x21, 0x37, 0x28, 0xC5, 0x14, 602 | 0x05, 0x46, 0x04, 0x0F, 0x0E, 0xE3, 0x7F, 0x54 }, 603 | { 0x9B, 0x09, 0xFF, 0xA7, 0x1B, 0x94, 0x2F, 0xCB, 604 | 0x27, 0x63, 0x5F, 0xBC, 0xD5, 0xB0, 0xE9, 0x44, 605 | 0xBF, 0xDC, 0x63, 0x64, 0x4F, 0x07, 0x13, 0x93, 606 | 0x8A, 0x7F, 0x51, 0x53, 0x5C, 0x3A, 0x35, 0xE2 } 607 | }; 608 | 609 | /* 610 | * Checkup routine 611 | */ 612 | int sha2_self_test( int verbose ) 613 | { 614 | int i, j, k, buflen; 615 | unsigned char buf[1024]; 616 | unsigned char sha2sum[32]; 617 | sha2_context ctx; 618 | 619 | for( i = 0; i < 6; i++ ) 620 | { 621 | j = i % 3; 622 | k = i < 3; 623 | 624 | if( verbose != 0 ) 625 | printf( " SHA-%d test #%d: ", 256 - k * 32, j + 1 ); 626 | 627 | sha2_starts( &ctx, k ); 628 | 629 | if( j == 2 ) 630 | { 631 | memset( buf, 'a', buflen = 1000 ); 632 | 633 | for( j = 0; j < 1000; j++ ) 634 | sha2_update( &ctx, buf, buflen ); 635 | } 636 | else 637 | sha2_update( &ctx, sha2_test_buf[j], 638 | sha2_test_buflen[j] ); 639 | 640 | sha2_finish( &ctx, sha2sum ); 641 | 642 | if( memcmp( sha2sum, sha2_test_sum[i], 32 - k * 4 ) != 0 ) 643 | { 644 | if( verbose != 0 ) 645 | printf( "failed\n" ); 646 | 647 | return( 1 ); 648 | } 649 | 650 | if( verbose != 0 ) 651 | printf( "passed\n" ); 652 | } 653 | 654 | if( verbose != 0 ) 655 | printf( "\n" ); 656 | 657 | for( i = 0; i < 14; i++ ) 658 | { 659 | j = i % 7; 660 | k = i < 7; 661 | 662 | if( verbose != 0 ) 663 | printf( " HMAC-SHA-%d test #%d: ", 256 - k * 32, j + 1 ); 664 | 665 | if( j == 5 || j == 6 ) 666 | { 667 | memset( buf, '\xAA', buflen = 131 ); 668 | sha2_hmac_starts( &ctx, buf, buflen, k ); 669 | } 670 | else 671 | sha2_hmac_starts( &ctx, sha2_hmac_test_key[j], 672 | sha2_hmac_test_keylen[j], k ); 673 | 674 | sha2_hmac_update( &ctx, sha2_hmac_test_buf[j], 675 | sha2_hmac_test_buflen[j] ); 676 | 677 | sha2_hmac_finish( &ctx, sha2sum ); 678 | 679 | buflen = ( j == 4 ) ? 16 : 32 - k * 4; 680 | 681 | if( memcmp( sha2sum, sha2_hmac_test_sum[i], buflen ) != 0 ) 682 | { 683 | if( verbose != 0 ) 684 | printf( "failed\n" ); 685 | 686 | return( 1 ); 687 | } 688 | 689 | if( verbose != 0 ) 690 | printf( "passed\n" ); 691 | } 692 | 693 | if( verbose != 0 ) 694 | printf( "\n" ); 695 | 696 | return( 0 ); 697 | } 698 | 699 | #endif 700 | -------------------------------------------------------------------------------- /sha2.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file sha2.h 3 | * 4 | * \brief SHA-224 and SHA-256 cryptographic hash function 5 | * 6 | * Copyright (C) 2006-2013, Brainspark B.V. 7 | * 8 | * This file is part of PolarSSL (http://www.polarssl.org) 9 | * Lead Maintainer: Paul Bakker 10 | * 11 | * All rights reserved. 12 | * 13 | * This program is free software; you can redistribute it and/or modify 14 | * it under the terms of the GNU General Public License as published by 15 | * the Free Software Foundation; either version 2 of the License, or 16 | * (at your option) any later version. 17 | * 18 | * This program is distributed in the hope that it will be useful, 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 | * GNU General Public License for more details. 22 | * 23 | * You should have received a copy of the GNU General Public License along 24 | * with this program; if not, write to the Free Software Foundation, Inc., 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 26 | */ 27 | #ifndef POLARSSL_SHA2_H 28 | #define POLARSSL_SHA2_H 29 | 30 | #include 31 | 32 | #ifdef _MSC_VER 33 | #include 34 | typedef UINT32 uint32_t; 35 | #else 36 | #include 37 | #endif 38 | 39 | #define POLARSSL_ERR_SHA2_FILE_IO_ERROR -0x0078 /**< Read/write error in file. */ 40 | 41 | #if !defined(POLARSSL_SHA2_ALT) 42 | // Regular implementation 43 | // 44 | 45 | /** 46 | * \brief SHA-256 context structure 47 | */ 48 | typedef struct 49 | { 50 | uint32_t total[2]; /*!< number of bytes processed */ 51 | uint32_t state[8]; /*!< intermediate digest state */ 52 | unsigned char buffer[64]; /*!< data block being processed */ 53 | 54 | unsigned char ipad[64]; /*!< HMAC: inner padding */ 55 | unsigned char opad[64]; /*!< HMAC: outer padding */ 56 | int is224; /*!< 0 => SHA-256, else SHA-224 */ 57 | } 58 | sha2_context; 59 | 60 | #ifdef __cplusplus 61 | extern "C" { 62 | #endif 63 | 64 | /** 65 | * \brief SHA-256 context setup 66 | * 67 | * \param ctx context to be initialized 68 | * \param is224 0 = use SHA256, 1 = use SHA224 69 | */ 70 | void sha2_starts( sha2_context *ctx, int is224 ); 71 | 72 | /** 73 | * \brief SHA-256 process buffer 74 | * 75 | * \param ctx SHA-256 context 76 | * \param input buffer holding the data 77 | * \param ilen length of the input data 78 | */ 79 | void sha2_update( sha2_context *ctx, const unsigned char *input, size_t ilen ); 80 | 81 | /** 82 | * \brief SHA-256 final digest 83 | * 84 | * \param ctx SHA-256 context 85 | * \param output SHA-224/256 checksum result 86 | */ 87 | void sha2_finish( sha2_context *ctx, unsigned char output[32] ); 88 | 89 | /* Internal use */ 90 | void sha2_process( sha2_context *ctx, const unsigned char data[64] ); 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | 96 | #else /* POLARSSL_SHA2_ALT */ 97 | #include "sha2_alt.h" 98 | #endif /* POLARSSL_SHA2_ALT */ 99 | 100 | #ifdef __cplusplus 101 | extern "C" { 102 | #endif 103 | 104 | /** 105 | * \brief Output = SHA-256( input buffer ) 106 | * 107 | * \param input buffer holding the data 108 | * \param ilen length of the input data 109 | * \param output SHA-224/256 checksum result 110 | * \param is224 0 = use SHA256, 1 = use SHA224 111 | */ 112 | void sha2( const unsigned char *input, size_t ilen, 113 | unsigned char output[32], int is224 ); 114 | 115 | /** 116 | * \brief Output = SHA-256( file contents ) 117 | * 118 | * \param path input file name 119 | * \param output SHA-224/256 checksum result 120 | * \param is224 0 = use SHA256, 1 = use SHA224 121 | * 122 | * \return 0 if successful, or POLARSSL_ERR_SHA2_FILE_IO_ERROR 123 | */ 124 | int sha2_file( const char *path, unsigned char output[32], int is224 ); 125 | 126 | /** 127 | * \brief SHA-256 HMAC context setup 128 | * 129 | * \param ctx HMAC context to be initialized 130 | * \param key HMAC secret key 131 | * \param keylen length of the HMAC key 132 | * \param is224 0 = use SHA256, 1 = use SHA224 133 | */ 134 | void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, size_t keylen, 135 | int is224 ); 136 | 137 | /** 138 | * \brief SHA-256 HMAC process buffer 139 | * 140 | * \param ctx HMAC context 141 | * \param input buffer holding the data 142 | * \param ilen length of the input data 143 | */ 144 | void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, size_t ilen ); 145 | 146 | /** 147 | * \brief SHA-256 HMAC final digest 148 | * 149 | * \param ctx HMAC context 150 | * \param output SHA-224/256 HMAC checksum result 151 | */ 152 | void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] ); 153 | 154 | /** 155 | * \brief SHA-256 HMAC context reset 156 | * 157 | * \param ctx HMAC context to be reset 158 | */ 159 | void sha2_hmac_reset( sha2_context *ctx ); 160 | 161 | /** 162 | * \brief Output = HMAC-SHA-256( hmac key, input buffer ) 163 | * 164 | * \param key HMAC secret key 165 | * \param keylen length of the HMAC key 166 | * \param input buffer holding the data 167 | * \param ilen length of the input data 168 | * \param output HMAC-SHA-224/256 result 169 | * \param is224 0 = use SHA256, 1 = use SHA224 170 | */ 171 | void sha2_hmac( const unsigned char *key, size_t keylen, 172 | const unsigned char *input, size_t ilen, 173 | unsigned char output[32], int is224 ); 174 | 175 | /** 176 | * \brief Checkup routine 177 | * 178 | * \return 0 if successful, or 1 if the test failed 179 | */ 180 | int sha2_self_test( int verbose ); 181 | 182 | #ifdef __cplusplus 183 | } 184 | #endif 185 | 186 | #endif /* sha2.h */ 187 | -------------------------------------------------------------------------------- /tools.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Copyright 2007,2008,2010 Segher Boessenkool 3 | // Licensed under the terms of the GNU GPL, version 2 4 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #ifdef WIN32 19 | #include "mingw_mmap.h" 20 | #include 21 | #include 22 | #else 23 | #include 24 | #endif 25 | 26 | #include "tools.h" 27 | #include "aes.h" 28 | #include "sha1.h" 29 | #include "common.h" 30 | 31 | static struct id2name_tbl t_key2file[] = { 32 | {KEY_LV0, "lv0"}, 33 | {KEY_LV1, "lv1"}, 34 | {KEY_LV2, "lv2"}, 35 | {KEY_APP, "app"}, 36 | {KEY_ISO, "iso"}, 37 | {KEY_LDR, "ldr"}, 38 | {KEY_PKG, "pkg"}, 39 | {KEY_SPP, "spp"}, 40 | {KEY_NPDRM, "app"}, 41 | {0, NULL} 42 | }; 43 | 44 | // 45 | // misc 46 | // 47 | 48 | void print_hash(u8 *ptr, u32 len) 49 | { 50 | while(len--) 51 | printf(" %02x", *ptr++); 52 | } 53 | 54 | void *mmap_file(const char *path) 55 | { 56 | int fd; 57 | struct stat st; 58 | void *ptr; 59 | 60 | fd = open(path, O_RDONLY); 61 | if(fd == -1) 62 | fail("open %s", path); 63 | if(fstat(fd, &st) != 0) 64 | fail("fstat %s", path); 65 | 66 | ptr = mmap(0, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); 67 | if(ptr==NULL) 68 | fail("mmap"); 69 | close(fd); 70 | 71 | return ptr; 72 | } 73 | 74 | void memcpy_to_file(const char *fname, u8 *ptr, u64 size) 75 | { 76 | FILE *fp; 77 | 78 | fp = fopen(fname, "wb"); 79 | fwrite(ptr, size, 1, fp); 80 | fclose(fp); 81 | } 82 | 83 | void fail(const char *a, ...) 84 | { 85 | char msg[1024]; 86 | va_list va; 87 | 88 | va_start(va, a); 89 | vsnprintf(msg, sizeof msg, a, va); 90 | fprintf(stderr, "%s\n", msg); 91 | perror("perror"); 92 | 93 | exit(1); 94 | } 95 | 96 | void decompress(u8 *in, u64 in_len, u8 *out, u64 out_len) 97 | { 98 | z_stream s; 99 | int ret; 100 | 101 | memset(&s, 0, sizeof(s)); 102 | 103 | s.zalloc = Z_NULL; 104 | s.zfree = Z_NULL; 105 | s.opaque = Z_NULL; 106 | 107 | ret = inflateInit(&s); 108 | if (ret != Z_OK) 109 | fail("inflateInit returned %d", ret); 110 | 111 | s.avail_in = in_len; 112 | s.next_in = in; 113 | 114 | s.avail_out = out_len; 115 | s.next_out = out; 116 | 117 | ret = inflate(&s, Z_FINISH); 118 | if (ret != Z_OK && ret != Z_STREAM_END) 119 | fail("inflate returned %d", ret); 120 | 121 | inflateEnd(&s); 122 | } 123 | 124 | const char *id2name(u32 id, struct id2name_tbl *t, const char *unk) 125 | { 126 | while (t->name != NULL) { 127 | if (id == t->id) 128 | return t->name; 129 | t++; 130 | } 131 | return unk; 132 | } 133 | 134 | #ifdef WIN32 135 | void get_rand(u8 *bfr, u32 size) 136 | { 137 | HCRYPTPROV hProv; 138 | 139 | if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) 140 | fail("unable to open random"); 141 | 142 | if (!CryptGenRandom(hProv, size, bfr)) 143 | fail("unable to read random numbers"); 144 | 145 | CryptReleaseContext(hProv, 0); 146 | } 147 | #else 148 | void get_rand(u8 *bfr, u32 size) 149 | { 150 | FILE *fp; 151 | 152 | fp = fopen("/dev/urandom", "r"); 153 | if (fp == NULL) 154 | fail("unable to open random"); 155 | 156 | if (fread(bfr, size, 1, fp) != 1) 157 | fail("unable to read random numbers"); 158 | 159 | fclose(fp); 160 | } 161 | #endif 162 | 163 | // 164 | // ELF helpers 165 | // 166 | int elf_read_hdr(u8 *hdr, struct elf_hdr *h) 167 | { 168 | int arch64; 169 | memcpy(h->e_ident, hdr, 16); 170 | hdr += 16; 171 | 172 | arch64 = h->e_ident[4] == 2; 173 | 174 | h->e_type = be16(hdr); 175 | hdr += 2; 176 | h->e_machine = be16(hdr); 177 | hdr += 2; 178 | h->e_version = be32(hdr); 179 | hdr += 4; 180 | 181 | if (arch64) { 182 | h->e_entry = be64(hdr); 183 | h->e_phoff = be64(hdr + 8); 184 | h->e_shoff = be64(hdr + 16); 185 | hdr += 24; 186 | } else { 187 | h->e_entry = be32(hdr); 188 | h->e_phoff = be32(hdr + 4); 189 | h->e_shoff = be32(hdr + 8); 190 | hdr += 12; 191 | } 192 | 193 | h->e_flags = be32(hdr); 194 | hdr += 4; 195 | 196 | h->e_ehsize = be16(hdr); 197 | hdr += 2; 198 | h->e_phentsize = be16(hdr); 199 | hdr += 2; 200 | h->e_phnum = be16(hdr); 201 | hdr += 2; 202 | h->e_shentsize = be16(hdr); 203 | hdr += 2; 204 | h->e_shnum = be16(hdr); 205 | hdr += 2; 206 | h->e_shtrndx = be16(hdr); 207 | 208 | return arch64; 209 | } 210 | 211 | void elf_read_phdr(int arch64, u8 *phdr, struct elf_phdr *p) 212 | { 213 | if (arch64) { 214 | p->p_type = be32(phdr + 0); 215 | p->p_flags = be32(phdr + 4); 216 | p->p_off = be64(phdr + 1*8); 217 | p->p_vaddr = be64(phdr + 2*8); 218 | p->p_paddr = be64(phdr + 3*8); 219 | p->p_filesz = be64(phdr + 4*8); 220 | p->p_memsz = be64(phdr + 5*8); 221 | p->p_align = be64(phdr + 6*8); 222 | } else { 223 | p->p_type = be32(phdr + 0*4); 224 | p->p_off = be32(phdr + 1*4); 225 | p->p_vaddr = be32(phdr + 2*4); 226 | p->p_paddr = be32(phdr + 3*4); 227 | p->p_filesz = be32(phdr + 4*4); 228 | p->p_memsz = be32(phdr + 5*4); 229 | p->p_flags = be32(phdr + 6*4); 230 | p->p_align = be32(phdr + 7*4); 231 | } 232 | } 233 | 234 | void elf_read_shdr(int arch64, u8 *shdr, struct elf_shdr *s) 235 | { 236 | if (arch64) { 237 | s->sh_name = be32(shdr + 0*4); 238 | s->sh_type = be32(shdr + 1*4); 239 | s->sh_flags = be64(shdr + 2*4); 240 | s->sh_addr = be64(shdr + 2*4 + 1*8); 241 | s->sh_offset = be64(shdr + 2*4 + 2*8); 242 | s->sh_size = be64(shdr + 2*4 + 3*8); 243 | s->sh_link = be32(shdr + 2*4 + 4*8); 244 | s->sh_info = be32(shdr + 3*4 + 4*8); 245 | s->sh_addralign = be64(shdr + 4*4 + 4*8); 246 | s->sh_entsize = be64(shdr + 4*4 + 5*8); 247 | } else { 248 | s->sh_name = be32(shdr + 0*4); 249 | s->sh_type = be32(shdr + 1*4); 250 | s->sh_flags = be32(shdr + 2*4); 251 | s->sh_addr = be32(shdr + 3*4); 252 | s->sh_offset = be32(shdr + 4*4); 253 | s->sh_size = be32(shdr + 5*4); 254 | s->sh_link = be32(shdr + 6*4); 255 | s->sh_info = be32(shdr + 7*4); 256 | s->sh_addralign = be32(shdr + 8*4); 257 | s->sh_entsize = be32(shdr + 9*4); 258 | } 259 | } 260 | 261 | void elf_write_shdr(int arch64, u8 *shdr, struct elf_shdr *s) 262 | { 263 | if (arch64) { 264 | wbe32(shdr + 0*4, s->sh_name); 265 | wbe32(shdr + 1*4, s->sh_type); 266 | wbe64(shdr + 2*4, s->sh_flags); 267 | wbe64(shdr + 2*4 + 1*8, s->sh_addr); 268 | wbe64(shdr + 2*4 + 2*8, s->sh_offset); 269 | wbe64(shdr + 2*4 + 3*8, s->sh_size); 270 | wbe32(shdr + 2*4 + 4*8, s->sh_link); 271 | wbe32(shdr + 3*4 + 4*8, s->sh_info); 272 | wbe64(shdr + 4*4 + 4*8, s->sh_addralign); 273 | wbe64(shdr + 4*4 + 5*8, s->sh_entsize); 274 | } else { 275 | wbe32(shdr + 0*4, s->sh_name); 276 | wbe32(shdr + 1*4, s->sh_type); 277 | wbe32(shdr + 2*4, s->sh_flags); 278 | wbe32(shdr + 3*4, s->sh_addr); 279 | wbe32(shdr + 4*4, s->sh_offset); 280 | wbe32(shdr + 5*4, s->sh_size); 281 | wbe32(shdr + 6*4, s->sh_link); 282 | wbe32(shdr + 7*4, s->sh_info); 283 | wbe32(shdr + 8*4, s->sh_addralign); 284 | wbe32(shdr + 9*4, s->sh_entsize); 285 | } 286 | } 287 | 288 | // 289 | // crypto 290 | // 291 | void aes256cbc(u8 *key, u8 *iv_in, u8 *in, u64 len, u8 *out) 292 | { 293 | AES_KEY k; 294 | u32 i; 295 | u8 tmp[16]; 296 | u8 iv[16]; 297 | 298 | memcpy(iv, iv_in, 16); 299 | memset(&k, 0, sizeof k); 300 | AES_set_decrypt_key(key, 256, &k); 301 | 302 | while (len > 0) { 303 | memcpy(tmp, in, 16); 304 | AES_decrypt(in, out, &k); 305 | 306 | for (i = 0; i < 16; i++) 307 | out[i] ^= iv[i]; 308 | 309 | memcpy(iv, tmp, 16); 310 | 311 | out += 16; 312 | in += 16; 313 | len -= 16; 314 | 315 | } 316 | } 317 | 318 | 319 | void aes256cbc_enc(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out) 320 | { 321 | AES_KEY k; 322 | u32 i; 323 | u8 tmp[16]; 324 | 325 | memcpy(tmp, iv, 16); 326 | memset(&k, 0, sizeof k); 327 | AES_set_encrypt_key(key, 256, &k); 328 | 329 | while (len > 0) { 330 | for (i = 0; i < 16; i++) 331 | tmp[i] ^= *in++; 332 | 333 | AES_encrypt(tmp, out, &k); 334 | memcpy(tmp, out, 16); 335 | 336 | out += 16; 337 | len -= 16; 338 | } 339 | } 340 | 341 | 342 | void aes128cbc(u8 *key, u8 *iv_in, u8 *in, u64 len, u8 *out) 343 | { 344 | AES_KEY k; 345 | u32 i; 346 | u8 tmp[16]; 347 | u8 iv[16]; 348 | 349 | memcpy(iv, iv_in, 16); 350 | memset(&k, 0, sizeof k); 351 | AES_set_decrypt_key(key, 128, &k); 352 | 353 | while (len > 0) { 354 | memcpy(tmp, in, 16); 355 | AES_decrypt(in, out, &k); 356 | 357 | for (i = 0; i < 16; i++) 358 | out[i] ^= iv[i]; 359 | 360 | memcpy(iv, tmp, 16); 361 | 362 | out += 16; 363 | in += 16; 364 | len -= 16; 365 | 366 | } 367 | } 368 | 369 | void aes128cbc_enc(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out) 370 | { 371 | AES_KEY k; 372 | u32 i; 373 | u8 tmp[16]; 374 | 375 | memcpy(tmp, iv, 16); 376 | memset(&k, 0, sizeof k); 377 | AES_set_encrypt_key(key, 128, &k); 378 | 379 | while (len > 0) { 380 | for (i = 0; i < 16; i++) 381 | tmp[i] ^= *in++; 382 | 383 | AES_encrypt(tmp, out, &k); 384 | memcpy(tmp, out, 16); 385 | 386 | out += 16; 387 | len -= 16; 388 | } 389 | } 390 | 391 | void aes128ctr(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out) 392 | { 393 | AES_KEY k; 394 | u32 i; 395 | u8 ctr[16]; 396 | u64 tmp; 397 | 398 | memset(ctr, 0, 16); 399 | memset(&k, 0, sizeof k); 400 | 401 | AES_set_encrypt_key(key, 128, &k); 402 | 403 | for (i = 0; i < len; i++) { 404 | if ((i & 0xf) == 0) { 405 | AES_encrypt(iv, ctr, &k); 406 | 407 | // increase nonce 408 | tmp = be64(iv + 8) + 1; 409 | wbe64(iv + 8, tmp); 410 | if (tmp == 0) 411 | wbe64(iv, be64(iv) + 1); 412 | } 413 | *out++ = *in++ ^ ctr[i & 0x0f]; 414 | } 415 | } 416 | 417 | void aes128(u8 *key, const u8 *in, u8 *out) { 418 | AES_KEY k; 419 | 420 | assert(!AES_set_decrypt_key(key, 128, &k)); 421 | AES_decrypt(in, out, &k); 422 | } 423 | 424 | void aes128_enc(u8 *key, const u8 *in, u8 *out) { 425 | AES_KEY k; 426 | 427 | assert(!AES_set_encrypt_key(key, 128, &k)); 428 | AES_encrypt(in, out, &k); 429 | } 430 | 431 | 432 | 433 | // FIXME: use a non-broken sha1.c *sigh* 434 | static void sha1_fixup(struct SHA1Context *ctx, u8 *digest) 435 | { 436 | u32 i; 437 | 438 | for(i = 0; i < 5; i++) { 439 | *digest++ = ctx->Message_Digest[i] >> 24 & 0xff; 440 | *digest++ = ctx->Message_Digest[i] >> 16 & 0xff; 441 | *digest++ = ctx->Message_Digest[i] >> 8 & 0xff; 442 | *digest++ = ctx->Message_Digest[i] & 0xff; 443 | } 444 | } 445 | 446 | void sha1(u8 *data, u32 len, u8 *digest) 447 | { 448 | struct SHA1Context ctx; 449 | 450 | SHA1Reset(&ctx); 451 | SHA1Input(&ctx, data, len); 452 | SHA1Result(&ctx); 453 | 454 | sha1_fixup(&ctx, digest); 455 | } 456 | 457 | void sha1_hmac(u8 *key, u8 *data, u32 len, u8 *digest) 458 | { 459 | struct SHA1Context ctx; 460 | u32 i; 461 | u8 ipad[0x40]; 462 | u8 tmp[0x40 + 0x14]; // opad + hash(ipad + message) 463 | 464 | SHA1Reset(&ctx); 465 | 466 | for (i = 0; i < sizeof ipad; i++) { 467 | tmp[i] = key[i] ^ 0x5c; // opad 468 | ipad[i] = key[i] ^ 0x36; 469 | } 470 | 471 | SHA1Input(&ctx, ipad, sizeof ipad); 472 | SHA1Input(&ctx, data, len); 473 | SHA1Result(&ctx); 474 | 475 | sha1_fixup(&ctx, tmp + 0x40); 476 | 477 | sha1(tmp, sizeof tmp, digest); 478 | 479 | } 480 | 481 | static int key_build_path(char *ptr) 482 | { 483 | char *home = NULL; 484 | char *dir = NULL; 485 | 486 | memset(ptr, 0, 256); 487 | 488 | dir = getenv("PS4_KEYS"); 489 | if (dir != NULL) { 490 | strncpy(ptr, dir, 256); 491 | return 0; 492 | } 493 | 494 | #ifdef WIN32 495 | home = getenv("USERPROFILE"); 496 | #else 497 | home = getenv("HOME"); 498 | #endif 499 | if (home == NULL) { 500 | snprintf (ptr, 256, "ps4keys"); 501 | } else { 502 | #ifdef WIN32 503 | snprintf(ptr, 256, "%s\\ps4keys\\", home); 504 | #else 505 | snprintf(ptr, 256, "%s/.ps4/", home); 506 | #endif 507 | } 508 | 509 | return 0; 510 | } 511 | 512 | static int key_read(const char *path, u32 len, u8 *dst) 513 | { 514 | FILE *fp = NULL; 515 | u32 read; 516 | int ret = -1; 517 | 518 | fp = fopen(path, "rb"); 519 | if (fp == NULL) 520 | goto fail; 521 | 522 | read = fread(dst, len, 1, fp); 523 | 524 | if (read != 1) 525 | goto fail; 526 | 527 | ret = 0; 528 | 529 | fail: 530 | if (fp != NULL) 531 | fclose(fp); 532 | 533 | return ret; 534 | } 535 | 536 | struct keylist *keys_get(enum sce_key type) 537 | { 538 | const char *name = NULL; 539 | char base[256]; 540 | char path[256]; 541 | void *tmp = NULL; 542 | char *id; 543 | DIR *dp; 544 | struct dirent *dent; 545 | struct keylist *klist; 546 | u8 bfr[4]; 547 | 548 | klist = malloc(sizeof *klist); 549 | if (klist == NULL) 550 | goto fail; 551 | 552 | memset(klist, 0, sizeof *klist); 553 | 554 | name = id2name(type, t_key2file, NULL); 555 | if (name == NULL) 556 | goto fail; 557 | 558 | if (key_build_path(base) < 0) 559 | goto fail; 560 | 561 | dp = opendir(base); 562 | if (dp == NULL) 563 | goto fail; 564 | 565 | while ((dent = readdir(dp)) != NULL) { 566 | if (strncmp(dent->d_name, name, strlen(name)) == 0 && 567 | strstr(dent->d_name, "key") != NULL) { 568 | tmp = realloc(klist->keys, (klist->n + 1) * sizeof(struct key)); 569 | if (tmp == NULL) 570 | goto fail; 571 | 572 | id = strrchr(dent->d_name, '-'); 573 | if (id != NULL) 574 | id++; 575 | 576 | klist->keys = tmp; 577 | memset(&klist->keys[klist->n], 0, sizeof(struct key)); 578 | 579 | snprintf(path, sizeof path, "%s/%s-key-%s", base, name, id); 580 | if (key_read(path, 32, klist->keys[klist->n].key) != 0) { 581 | printf(" key file: %s (ERROR)\n", path); 582 | } 583 | 584 | snprintf(path, sizeof path, "%s/%s-iv-%s", base, name, id); 585 | if (key_read(path, 16, klist->keys[klist->n].iv) != 0) { 586 | printf(" iv file: %s (ERROR)\n", path); 587 | } 588 | 589 | klist->keys[klist->n].pub_avail = -1; 590 | klist->keys[klist->n].priv_avail = -1; 591 | 592 | snprintf(path, sizeof path, "%s/%s-pub-%s", base, name, id); 593 | if (key_read(path, 40, klist->keys[klist->n].pub) == 0) { 594 | snprintf(path, sizeof path, "%s/%s-ctype-%s", base, name, id); 595 | key_read(path, 4, bfr); 596 | 597 | klist->keys[klist->n].pub_avail = 1; 598 | klist->keys[klist->n].ctype = be32(bfr); 599 | } else { 600 | printf(" pub file: %s (ERROR)\n", path); 601 | } 602 | 603 | snprintf(path, sizeof path, "%s/%s-priv-%s", base, name, id); 604 | if (key_read(path, 21, klist->keys[klist->n].priv) == 0) { 605 | klist->keys[klist->n].priv_avail = 1; 606 | } else { 607 | printf(" priv file: %s (ERROR)\n", path); 608 | } 609 | 610 | 611 | klist->n++; 612 | } 613 | } 614 | 615 | if (type == KEY_NPDRM) { 616 | klist->idps = calloc(sizeof(struct key), 1); 617 | if (klist->idps == NULL) 618 | goto fail; 619 | snprintf(path, sizeof path, "%s/idps", base); 620 | if (key_read(path, 16, klist->idps->key) != 0) { 621 | printf(" key file: %s (ERROR)\n", path); 622 | } 623 | 624 | klist->klic = calloc(sizeof(struct key), 1); 625 | if (klist->klic == NULL) 626 | goto fail; 627 | snprintf(path, sizeof path, "%s/klic-key", base); 628 | if (key_read(path, 16, klist->klic->key) != 0) { 629 | printf(" key file: %s (ERROR)\n", path); 630 | } 631 | 632 | klist->rif = calloc(sizeof(struct key), 1); 633 | if (klist->rif == NULL) 634 | goto fail; 635 | snprintf(path, sizeof path, "%s/rif-key", base); 636 | if (key_read(path, 16, klist->rif->key) != 0) { 637 | printf(" key file: %s (ERROR)\n", path); 638 | } 639 | 640 | klist->npdrm_const = calloc(sizeof(struct key), 1); 641 | if (klist->npdrm_const == NULL) 642 | goto fail; 643 | snprintf(path, sizeof path, "%s/npdrm-const", base); 644 | if (key_read(path, 16, klist->npdrm_const->key) != 0) { 645 | printf(" key file: %s (ERROR)\n", path); 646 | } 647 | 648 | klist->free_klicensee = calloc(sizeof(struct key), 1); 649 | if (klist->free_klicensee == NULL) 650 | goto fail; 651 | snprintf(path, sizeof path, "%s/free_klicensee-key", base); 652 | if (key_read(path, 16, klist->free_klicensee->key) != 0) { 653 | printf(" key file: %s (ERROR)\n", path); 654 | } 655 | } 656 | 657 | return klist; 658 | 659 | fail: 660 | if (klist != NULL) { 661 | if (klist->keys != NULL) 662 | free(klist->keys); 663 | free(klist); 664 | } 665 | klist = NULL; 666 | 667 | return NULL; 668 | } 669 | 670 | int key_get_simple(const char *name, u8 *bfr, u32 len) 671 | { 672 | char base[256]; 673 | char path[256]; 674 | 675 | if (key_build_path(base) < 0) 676 | return -1; 677 | 678 | snprintf(path, sizeof path, "%s/%s", base, name); 679 | if (key_read(path, len, bfr) < 0) 680 | return -1; 681 | 682 | return 0; 683 | } 684 | 685 | int key_get(enum sce_key type, const char *suffix, struct key *k) 686 | { 687 | const char *name = ""; 688 | const char *rev = ""; 689 | char base[256]; 690 | char path[256]; 691 | u8 tmp[4]; 692 | 693 | if ( strncmp( suffix, "retail", strlen( suffix ) ) == 0 ) { 694 | rev = "retail"; 695 | } else if ( atoi( suffix ) <= 92 ) { 696 | suffix = "092"; 697 | rev = "0x00"; 698 | } else if ( atoi( suffix ) <= 331 ) { 699 | suffix = "315"; 700 | rev = "0x01"; 701 | } else if ( atoi( suffix ) <= 342 ) { 702 | suffix = "341"; 703 | rev = "0x04"; 704 | } else if ( atoi( suffix ) <= 350 ) { 705 | suffix = "350"; 706 | rev = "0x07"; 707 | } else if ( atoi( suffix ) <= 355 ) { 708 | suffix = "355"; 709 | rev = "0x0a"; 710 | } else if ( atoi( suffix ) <= 356 ) { 711 | suffix = "356"; 712 | rev = "0x0d"; 713 | } 714 | printf(" file suffix: %s (rev %s)\n", suffix, rev ); 715 | 716 | if (key_build_path(base) < 0) 717 | return -1; 718 | 719 | name = id2name(type, t_key2file, NULL); 720 | if (name == NULL) 721 | return -1; 722 | 723 | snprintf(path, sizeof path, "%s/%s-key-%s", base, name, suffix); 724 | if (key_read(path, 32, k->key) < 0) { 725 | printf(" key file: %s (ERROR)\n", path); 726 | return -1; 727 | } 728 | 729 | snprintf(path, sizeof path, "%s/%s-iv-%s", base, name, suffix); 730 | if (key_read(path, 16, k->iv) < 0) { 731 | printf(" iv file: %s (ERROR)\n", path); 732 | return -1; 733 | } 734 | 735 | k->pub_avail = k->priv_avail = 1; 736 | 737 | snprintf(path, sizeof path, "%s/%s-ctype-%s", base, name, suffix); 738 | if (key_read(path, 4, tmp) < 0) { 739 | k->pub_avail = k->priv_avail = -1; 740 | printf(" ctype file: %s (ERROR)\n", path); 741 | return 0; 742 | } 743 | 744 | k->ctype = be32(tmp); 745 | 746 | snprintf(path, sizeof path, "%s/%s-pub-%s", base, name, suffix); 747 | if (key_read(path, 40, k->pub) < 0) { 748 | printf(" pub file: %s (ERROR)\n", path); 749 | k->pub_avail = -1; 750 | } 751 | 752 | snprintf(path, sizeof path, "%s/%s-priv-%s", base, name, suffix); 753 | if (key_read(path, 21, k->priv) < 0) { 754 | printf(" priv file: %s (ERROR)\n", path); 755 | k->priv_avail = -1; 756 | } 757 | 758 | return 0; 759 | } 760 | 761 | struct rif *rif_get(const char *content_id) { 762 | char base[256]; 763 | char path[256]; 764 | struct rif *rif; 765 | FILE *fp; 766 | u32 read; 767 | 768 | rif = malloc(sizeof(struct rif)); 769 | if (rif == NULL) 770 | goto fail; 771 | 772 | if (key_build_path(base) < 0) 773 | goto fail; 774 | 775 | snprintf(path, sizeof path, "%s/exdata/%s.rif", base, content_id); 776 | 777 | fp = fopen(path, "rb"); 778 | if (fp == NULL) 779 | goto fail; 780 | 781 | read = fread(rif, sizeof(struct rif), 1, fp); 782 | 783 | if (read != 1) 784 | goto fail; 785 | 786 | return rif; 787 | 788 | fail: 789 | if (rif != NULL) { 790 | free(rif); 791 | } 792 | 793 | return NULL; 794 | } 795 | 796 | struct actdat *actdat_get(void) { 797 | char base[256]; 798 | char path[256]; 799 | struct actdat *actdat; 800 | FILE *fp; 801 | u32 read; 802 | 803 | actdat = malloc(sizeof(struct actdat)); 804 | if (actdat == NULL) 805 | goto fail; 806 | 807 | if (key_build_path(base) < 0) 808 | goto fail; 809 | 810 | snprintf(path, sizeof path, "%s/exdata/act.dat", base); 811 | 812 | fp = fopen(path, "rb"); 813 | if (fp == NULL) 814 | goto fail; 815 | 816 | read = fread(actdat, sizeof(struct actdat), 1, fp); 817 | 818 | if (read != 1) 819 | goto fail; 820 | 821 | return actdat; 822 | 823 | fail: 824 | if (actdat != NULL) { 825 | free(actdat); 826 | } 827 | 828 | return NULL; 829 | } 830 | 831 | static void memcpy_inv(u8 *dst, u8 *src, u32 len) 832 | { 833 | u32 j; 834 | 835 | for (j = 0; j < len; j++) 836 | dst[j] = ~src[j]; 837 | } 838 | 839 | int ecdsa_get_params(u32 type, u8 *p, u8 *a, u8 *b, u8 *N, u8 *Gx, u8 *Gy) 840 | { 841 | static u8 tbl[64 * 121]; 842 | char path[256]; 843 | u32 offset; 844 | 845 | if (type >= 64) 846 | return -1; 847 | 848 | if (key_build_path(path) < 0) 849 | return -1; 850 | 851 | strncat(path, "/curves", sizeof path); 852 | 853 | if (key_read(path, sizeof tbl, tbl) < 0) 854 | return -1; 855 | 856 | offset = type * 121; 857 | 858 | memcpy_inv(p, tbl + offset + 0, 20); 859 | memcpy_inv(a, tbl + offset + 20, 20); 860 | memcpy_inv(b, tbl + offset + 40, 20); 861 | memcpy_inv(N, tbl + offset + 60, 21); 862 | memcpy_inv(Gx, tbl + offset + 81, 20); 863 | memcpy_inv(Gy, tbl + offset + 101, 20); 864 | 865 | return 0; 866 | } 867 | 868 | int sce_remove_npdrm(u8 *ptr, struct keylist *klist) 869 | { 870 | u64 ctrl_offset; 871 | u64 ctrl_size; 872 | u32 block_type; 873 | u32 block_size; 874 | u32 license_type; 875 | char content_id[0x31] = {'\0'}; 876 | struct rif *rif; 877 | struct actdat *actdat; 878 | u8 enc_const[0x10]; 879 | u8 dec_actdat[0x10]; 880 | struct key klicensee; 881 | u64 i; 882 | 883 | ctrl_offset = be64(ptr + 0x58); 884 | ctrl_size = be64(ptr + 0x60); 885 | 886 | for (i = 0; i < ctrl_size; ) { 887 | block_type = be32(ptr + ctrl_offset + i); 888 | block_size = be32(ptr + ctrl_offset + i + 0x4); 889 | 890 | if (block_type == 3) { 891 | license_type = be32(ptr + ctrl_offset + i + 0x18); 892 | switch (license_type) { 893 | case 1: 894 | // cant decrypt network stuff 895 | return -1; 896 | case 2: 897 | memcpy(content_id, ptr + ctrl_offset + i + 0x20, 0x30); 898 | rif = rif_get(content_id); 899 | if (rif == NULL) { 900 | return -1; 901 | } 902 | aes128(klist->rif->key, rif->padding, rif->padding); 903 | aes128_enc(klist->idps->key, klist->npdrm_const->key, enc_const); 904 | actdat = actdat_get(); 905 | if (actdat == NULL) { 906 | return -1; 907 | } 908 | aes128(enc_const, &actdat->keyTable[swap32(rif->actDatIndex)*0x10], dec_actdat); 909 | aes128(dec_actdat, rif->key, klicensee.key); 910 | sce_decrypt_npdrm(ptr, klist, &klicensee); 911 | return 1; 912 | case 3: 913 | sce_decrypt_npdrm(ptr, klist, klist->free_klicensee); 914 | return 1; 915 | } 916 | } 917 | 918 | i += block_size; 919 | } 920 | 921 | return 0; 922 | } 923 | 924 | void sce_decrypt_npdrm(u8 *ptr, struct keylist *klist, struct key *klicensee) 925 | { 926 | u32 meta_offset; 927 | struct key d_klic; 928 | 929 | meta_offset = be32(ptr + 0x0c); 930 | 931 | // iv is 0 932 | memset(&d_klic, 0, sizeof(struct key)); 933 | aes128(klist->klic->key, klicensee->key, d_klic.key); 934 | 935 | aes128cbc(d_klic.key, d_klic.iv, ptr + meta_offset + 0x20, 0x40, ptr + meta_offset + 0x20); 936 | } 937 | 938 | 939 | int sce_decrypt_header(u8 *ptr, struct keylist *klist) 940 | { 941 | u32 meta_offset; 942 | u32 meta_len; 943 | u64 header_len; 944 | u32 i, j; 945 | u8 tmp[0x40]; 946 | int success = 0; 947 | 948 | 949 | meta_offset = be32(ptr + 0x0c); 950 | header_len = be64(ptr + 0x10); 951 | 952 | for (i = 0; i < klist->n; i++) { 953 | aes256cbc(klist->keys[i].key, 954 | klist->keys[i].iv, 955 | ptr + meta_offset + 0x20, 956 | 0x40, 957 | tmp); 958 | 959 | success = 1; 960 | for (j = 0x10; j < (0x10 + 0x10); j++) 961 | if (tmp[j] != 0) 962 | success = 0; 963 | 964 | for (j = 0x30; j < (0x30 + 0x10); j++) 965 | if (tmp[j] != 0) 966 | success = 0; 967 | 968 | if (success == 1) { 969 | memcpy(ptr + meta_offset + 0x20, tmp, 0x40); 970 | break; 971 | } 972 | } 973 | 974 | if (success != 1) 975 | return -1; 976 | 977 | memcpy(tmp, ptr + meta_offset + 0x40, 0x10); 978 | aes128ctr(ptr + meta_offset + 0x20, 979 | tmp, 980 | ptr + meta_offset + 0x60, 981 | 0x20, 982 | ptr + meta_offset + 0x60); 983 | 984 | meta_len = header_len - meta_offset; 985 | 986 | aes128ctr(ptr + meta_offset + 0x20, 987 | tmp, 988 | ptr + meta_offset + 0x80, 989 | meta_len - 0x80, 990 | ptr + meta_offset + 0x80); 991 | 992 | return i; 993 | } 994 | 995 | int sce_encrypt_header(u8 *ptr, struct key *k) 996 | { 997 | u32 meta_offset; 998 | u32 meta_len; 999 | u64 header_len; 1000 | u8 iv[16]; 1001 | 1002 | meta_offset = be32(ptr + 0x0c); 1003 | header_len = be64(ptr + 0x10); 1004 | meta_len = header_len - meta_offset; 1005 | 1006 | memcpy(iv, ptr + meta_offset + 0x40, 0x10); 1007 | aes128ctr(ptr + meta_offset + 0x20, 1008 | iv, 1009 | ptr + meta_offset + 0x60, 1010 | meta_len - 0x60, 1011 | ptr + meta_offset + 0x60); 1012 | 1013 | aes256cbc_enc(k->key, k->iv, 1014 | ptr + meta_offset + 0x20, 1015 | 0x40, 1016 | ptr + meta_offset + 0x20); 1017 | 1018 | 1019 | return 0; 1020 | } 1021 | 1022 | int sce_decrypt_data(u8 *ptr) 1023 | { 1024 | u64 meta_offset; 1025 | u32 meta_len; 1026 | u32 meta_n_hdr; 1027 | u64 header_len; 1028 | u32 i; 1029 | 1030 | u64 offset; 1031 | u64 size; 1032 | u32 keyid; 1033 | u32 ivid; 1034 | u8 *tmp; 1035 | 1036 | u8 iv[16]; 1037 | 1038 | meta_offset = be32(ptr + 0x0c); 1039 | header_len = be64(ptr + 0x10); 1040 | meta_len = header_len - meta_offset; 1041 | meta_n_hdr = be32(ptr + meta_offset + 0x60 + 0xc); 1042 | 1043 | for (i = 0; i < meta_n_hdr; i++) { 1044 | tmp = ptr + meta_offset + 0x80 + 0x30*i; 1045 | offset = be64(tmp); 1046 | size = be64(tmp + 8); 1047 | keyid = be32(tmp + 0x24); 1048 | ivid = be32(tmp + 0x28); 1049 | 1050 | if (keyid == 0xffffffff || ivid == 0xffffffff) 1051 | continue; 1052 | 1053 | memcpy(iv, ptr + meta_offset + 0x80 + 0x30 * meta_n_hdr + ivid * 0x10, 0x10); 1054 | aes128ctr(ptr + meta_offset + 0x80 + 0x30 * meta_n_hdr + keyid * 0x10, 1055 | iv, 1056 | ptr + offset, 1057 | size, 1058 | ptr + offset); 1059 | } 1060 | 1061 | return 0; 1062 | } 1063 | 1064 | int sce_encrypt_data(u8 *ptr) 1065 | { 1066 | return sce_decrypt_data(ptr); 1067 | } 1068 | -------------------------------------------------------------------------------- /tools.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Copyright 2007,2008,2010 Segher Boessenkool 3 | // Licensed under the terms of the GNU GPL, version 2 4 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 5 | 6 | #ifndef TOOLS_H__ 7 | #define TOOLS_H__ 1 8 | #include 9 | 10 | #include "types.h" 11 | 12 | enum sce_key { 13 | KEY_LV0 = 0, 14 | KEY_LV1, 15 | KEY_LV2, 16 | KEY_APP, 17 | KEY_ISO, 18 | KEY_LDR, 19 | KEY_PKG, 20 | KEY_SPP, 21 | KEY_NPDRM 22 | }; 23 | 24 | void print_hash(u8 *ptr, u32 len); 25 | void *mmap_file(const char *path); 26 | void memcpy_to_file(const char *fname, u8 *ptr, u64 size); 27 | const char *id2name(u32 id, struct id2name_tbl *t, const char *unk); 28 | void fail(const char *fmt, ...) __attribute__((noreturn)); 29 | void decompress(u8 *in, u64 in_len, u8 *out, u64 out_len); 30 | void get_rand(u8 *bfr, u32 size); 31 | 32 | int elf_read_hdr(u8 *hdr, struct elf_hdr *h); 33 | void elf_read_phdr(int arch64, u8 *phdr, struct elf_phdr *p); 34 | void elf_read_shdr(int arch64, u8 *shdr, struct elf_shdr *s); 35 | void elf_write_shdr(int arch64, u8 *shdr, struct elf_shdr *s); 36 | 37 | void aes256cbc(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out); 38 | void aes256cbc_enc(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out); 39 | void aes128ctr(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out); 40 | void aes128cbc(u8 *key, u8 *iv_in, u8 *in, u64 len, u8 *out); 41 | void aes128cbc_enc(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out); 42 | void aes128(u8 *key, const u8 *in, u8 *out); 43 | void aes128_enc(u8 *key, const u8 *in, u8 *out); 44 | 45 | void sha1(u8 *data, u32 len, u8 *digest); 46 | void sha1_hmac(u8 *key, u8 *data, u32 len, u8 *digest); 47 | 48 | int key_get(enum sce_key type, const char *suffix, struct key *k); 49 | int key_get_simple(const char *name, u8 *bfr, u32 len); 50 | struct keylist *keys_get(enum sce_key type); 51 | 52 | struct rif *rif_get(const char *content_id); 53 | struct actdat *actdat_get(void); 54 | 55 | int sce_remove_npdrm(u8 *ptr, struct keylist *klist); 56 | void sce_decrypt_npdrm(u8 *ptr, struct keylist *klist, struct key *klicensee); 57 | 58 | int sce_decrypt_header(u8 *ptr, struct keylist *klist); 59 | int sce_encrypt_header(u8 *ptr, struct key *k); 60 | int sce_decrypt_data(u8 *ptr); 61 | int sce_encrypt_data(u8 *ptr); 62 | 63 | int ecdsa_get_params(u32 type, u8 *p, u8 *a, u8 *b, u8 *N, u8 *Gx, u8 *Gy); 64 | int ecdsa_set_curve(u32 type); 65 | void ecdsa_set_pub(u8 *Q); 66 | void ecdsa_set_priv(u8 *k); 67 | int ecdsa_verify(u8 *hash, u8 *R, u8 *S); 68 | void ecdsa_sign(u8 *hash, u8 *R, u8 *S); 69 | 70 | void bn_copy(u8 *d, u8 *a, u32 n); 71 | int bn_compare(u8 *a, u8 *b, u32 n); 72 | void bn_reduce(u8 *d, u8 *N, u32 n); 73 | void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n); 74 | void bn_sub(u8 *d, u8 *a, u8 *b, u8 *N, u32 n); 75 | void bn_to_mon(u8 *d, u8 *N, u32 n); 76 | void bn_from_mon(u8 *d, u8 *N, u32 n); 77 | void bn_mon_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n); 78 | void bn_mon_inv(u8 *d, u8 *a, u8 *N, u32 n); 79 | 80 | #define round_up(x,n) (-(-(x) & -(n))) 81 | 82 | #define array_size(x) (sizeof(x) / sizeof(*(x))) 83 | #endif 84 | -------------------------------------------------------------------------------- /trophy.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #ifdef WIN32 15 | #include "mingw_mmap.h" 16 | #include 17 | #include 18 | #else 19 | #include 20 | #endif 21 | 22 | #ifdef WIN32 23 | #define MKDIR(x,y) mkdir(x) 24 | #else 25 | #define MKDIR(x,y) mkdir(x,y) 26 | #endif 27 | 28 | #include "types.h" 29 | #include "tools.h" 30 | 31 | typedef struct { 32 | unsigned int magic; // 33 | unsigned int version; 34 | unsigned long file_size; // size of full trp file 35 | unsigned int entry_num; // num entries 36 | unsigned int entry_size; // size of entry 37 | unsigned int dev_flag; // 1: dev 38 | unsigned char digest[20]; //sha1 hash 39 | unsigned int key_index; 40 | unsigned char padding[44]; 41 | } trp_header; 42 | 43 | typedef struct { 44 | signed char entry_name[32]; 45 | unsigned long entry_pos; 46 | unsigned long entry_len; 47 | unsigned int flag; //3 on some, 0 on others, could be flags or an enum to determine if encrypted or not? 48 | unsigned char padding[12]; 49 | } trp_entry; 50 | 51 | 52 | static u8 *trophy = NULL; 53 | u8 key[0x10]= {0}; 54 | u8 iv[0x10] = {0}; 55 | u8 ckey[0x10]; 56 | u8 civ[0x10]; 57 | trp_header *header; 58 | u8 np_comm_id[0x10]; 59 | 60 | 61 | u8 brutforce_npcommid (u8 *ptr, u64 len) { 62 | u32 i; 63 | u8 *out; 64 | u8 *str; 65 | 66 | // printf("[!] Bruteforce attack started ... "); 67 | 68 | for ( i = 0; i < 99999; i++ ) { 69 | memset(np_comm_id, 0, 0x10); 70 | sprintf(np_comm_id, "%s%05d_00", "NPWR", i); 71 | aes128cbc_enc( key, iv, np_comm_id, 0x10, ckey); 72 | 73 | memcpy(civ, ptr, 0x10); 74 | 75 | out = malloc(len - 0x10); 76 | aes128cbc(ckey, civ, ptr + 0x10, len - 0x10, out); 77 | 78 | str = malloc(0x04); 79 | memcpy(str, out, 0x04); 80 | 81 | if ( !strcmp(str, "