├── COPYING ├── Makefile ├── README.md ├── aes.c ├── aes.h ├── bn.c ├── ec.c ├── little_endian.h ├── mingw_mmap.c ├── mingw_mmap.h ├── pupunpack.c ├── readself.c ├── readself.h ├── sfo_stuff.c ├── sfo_stuff.h ├── sha1.c ├── sha1.h ├── spkg_reader.c ├── tools.c ├── tools.h ├── types.h ├── ungpkg.c └── unpkg.c /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | TOOLS = ungpkg spkg_reader pupunpack readself 2 | COMMON = tools.o aes.o sha1.o ec.o bn.o sfo_stuff.o 3 | DEPS = Makefile tools.h types.h little_endian.h 4 | OSNAME = $(shell uname -s) 5 | 6 | ifeq ($(findstring MINGW, $(OSNAME)), MINGW) 7 | COMMON += mingw_mmap.o 8 | endif 9 | 10 | CC = gcc 11 | CFLAGS = -g -O2 -Wall -W 12 | LDLIBS = -lz 13 | 14 | # Darwin's MacPorts Default Path 15 | ifeq ($(shell test -e /opt/local/include/gmp.h; echo $$?),0) 16 | CFLAGS += -I/opt/local/include 17 | LDLIBS += -L/opt/local/lib 18 | endif 19 | 20 | OBJS = $(COMMON) $(addsuffix .o, $(TOOLS)) 21 | 22 | all: $(TOOLS) 23 | 24 | $(TOOLS): %: %.o $(COMMON) $(DEPS) 25 | $(CC) $(CFLAGS) -o $@ $< $(COMMON) $(LDLIBS) 26 | 27 | $(OBJS): %.o: %.c $(DEPS) 28 | $(CC) $(CFLAGS) -c -o $@ $< 29 | 30 | clean: 31 | -rm -f $(OBJS) $(TOOLS) *.exe *~ 32 | 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | VITA TOOLS 2 | ========== 3 | 4 | ### Based on fail0verflow tools and modified to be used with the vita. 5 | 6 | Authors 7 | ------- 8 | 9 | Fail0verflow team 10 | deroad 11 | 12 | CHANGELOG 13 | -------- 14 | 15 | 16 | * 1.07 17 | 18 | Fixed little_endian.h. 19 | Added pupunpack entries and fixed some wrong things 20 | Fixed readself. 21 | 22 | * 1.06 23 | 24 | Added ungpkg to allow to unpack game packages. 25 | Fixed tools.c to allow to search ps vita keys in the right folder. 26 | Removed pkg_reader. useless 27 | 28 | * 1.05b 29 | 30 | Updated readself. again more infos about selfs. 31 | 32 | * 1.05 33 | 34 | Updated readself. now shows more infos about selfs. 35 | Updated pupunpack. corrected some ID stuff (a self 0x200 is sprx, same for 0x204. it is a self). 36 | 37 | * 1.04 38 | 39 | added readself. it show information about the Playstation Vita Signed Executable and Linkable Format. 40 | 41 | * 1.03 42 | 43 | added security_1 to pupunpack 44 | 45 | * 1.02 46 | 47 | initial version of unpkg. for now it is NOT able to decrypt pkg. it just extract the main parts. 48 | deleted some id on the pupunpack 49 | pkg_reader is still unfinished 50 | 51 | * 1.01 52 | 53 | pupunpack has been updated to unpack the PUP inside a folder called: NAME_UPDATE.PUP_0 54 | 55 | * 1.00 56 | 57 | pkg_reader fixed and fully working pupunpack!! 58 | 59 | * Initial release 60 | 61 | pkg_reader (unfinished and bugged) 62 | 63 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /bn.c: -------------------------------------------------------------------------------- 1 | // Copyright 2007,2008,2010 Segher Boessenkool 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | 8 | #include "tools.h" 9 | 10 | void bn_print(char *name, u8 *a, u32 n) 11 | { 12 | u32 i; 13 | 14 | printf("%s = ", name); 15 | 16 | for (i = 0; i < n; i++) 17 | printf("%02x", a[i]); 18 | 19 | printf("\n"); 20 | } 21 | 22 | static void bn_zero(u8 *d, u32 n) 23 | { 24 | memset(d, 0, n); 25 | } 26 | 27 | void bn_copy(u8 *d, u8 *a, u32 n) 28 | { 29 | memcpy(d, a, n); 30 | } 31 | 32 | int bn_compare(u8 *a, u8 *b, u32 n) 33 | { 34 | u32 i; 35 | 36 | for (i = 0; i < n; i++) { 37 | if (a[i] < b[i]) 38 | return -1; 39 | if (a[i] > b[i]) 40 | return 1; 41 | } 42 | 43 | return 0; 44 | } 45 | 46 | static u8 bn_add_1(u8 *d, u8 *a, u8 *b, u32 n) 47 | { 48 | u32 i; 49 | u32 dig; 50 | u8 c; 51 | 52 | c = 0; 53 | for (i = n - 1; i < n; i--) { 54 | dig = a[i] + b[i] + c; 55 | c = dig >> 8; 56 | d[i] = dig; 57 | } 58 | 59 | return c; 60 | } 61 | 62 | static u8 bn_sub_1(u8 *d, u8 *a, u8 *b, u32 n) 63 | { 64 | u32 i; 65 | u32 dig; 66 | u8 c; 67 | 68 | c = 1; 69 | for (i = n - 1; i < n; i--) { 70 | dig = a[i] + 255 - b[i] + c; 71 | c = dig >> 8; 72 | d[i] = dig; 73 | } 74 | 75 | return 1 - c; 76 | } 77 | 78 | void bn_reduce(u8 *d, u8 *N, u32 n) 79 | { 80 | if (bn_compare(d, N, n) >= 0) 81 | bn_sub_1(d, d, N, n); 82 | } 83 | 84 | void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n) 85 | { 86 | if (bn_add_1(d, a, b, n)) 87 | bn_sub_1(d, d, N, n); 88 | 89 | bn_reduce(d, N, n); 90 | } 91 | 92 | void bn_sub(u8 *d, u8 *a, u8 *b, u8 *N, u32 n) 93 | { 94 | if (bn_sub_1(d, a, b, n)) 95 | bn_add_1(d, d, N, n); 96 | } 97 | 98 | static const u8 inv256[0x80] = { 99 | 0x01, 0xab, 0xcd, 0xb7, 0x39, 0xa3, 0xc5, 0xef, 100 | 0xf1, 0x1b, 0x3d, 0xa7, 0x29, 0x13, 0x35, 0xdf, 101 | 0xe1, 0x8b, 0xad, 0x97, 0x19, 0x83, 0xa5, 0xcf, 102 | 0xd1, 0xfb, 0x1d, 0x87, 0x09, 0xf3, 0x15, 0xbf, 103 | 0xc1, 0x6b, 0x8d, 0x77, 0xf9, 0x63, 0x85, 0xaf, 104 | 0xb1, 0xdb, 0xfd, 0x67, 0xe9, 0xd3, 0xf5, 0x9f, 105 | 0xa1, 0x4b, 0x6d, 0x57, 0xd9, 0x43, 0x65, 0x8f, 106 | 0x91, 0xbb, 0xdd, 0x47, 0xc9, 0xb3, 0xd5, 0x7f, 107 | 0x81, 0x2b, 0x4d, 0x37, 0xb9, 0x23, 0x45, 0x6f, 108 | 0x71, 0x9b, 0xbd, 0x27, 0xa9, 0x93, 0xb5, 0x5f, 109 | 0x61, 0x0b, 0x2d, 0x17, 0x99, 0x03, 0x25, 0x4f, 110 | 0x51, 0x7b, 0x9d, 0x07, 0x89, 0x73, 0x95, 0x3f, 111 | 0x41, 0xeb, 0x0d, 0xf7, 0x79, 0xe3, 0x05, 0x2f, 112 | 0x31, 0x5b, 0x7d, 0xe7, 0x69, 0x53, 0x75, 0x1f, 113 | 0x21, 0xcb, 0xed, 0xd7, 0x59, 0xc3, 0xe5, 0x0f, 114 | 0x11, 0x3b, 0x5d, 0xc7, 0x49, 0x33, 0x55, 0xff, 115 | }; 116 | 117 | static void bn_mon_muladd_dig(u8 *d, u8 *a, u8 b, u8 *N, u32 n) 118 | { 119 | u32 dig; 120 | u32 i; 121 | 122 | u8 z = -(d[n-1] + a[n-1]*b) * inv256[N[n-1]/2]; 123 | 124 | dig = d[n-1] + a[n-1]*b + N[n-1]*z; 125 | dig >>= 8; 126 | 127 | for (i = n - 2; i < n; i--) { 128 | dig += d[i] + a[i]*b + N[i]*z; 129 | d[i+1] = dig; 130 | dig >>= 8; 131 | } 132 | 133 | d[0] = dig; 134 | dig >>= 8; 135 | 136 | if (dig) 137 | bn_sub_1(d, d, N, n); 138 | 139 | bn_reduce(d, N, n); 140 | } 141 | 142 | void bn_mon_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n) 143 | { 144 | u8 t[512]; 145 | u32 i; 146 | 147 | bn_zero(t, n); 148 | 149 | for (i = n - 1; i < n; i--) 150 | bn_mon_muladd_dig(t, a, b[i], N, n); 151 | 152 | bn_copy(d, t, n); 153 | } 154 | 155 | void bn_to_mon(u8 *d, u8 *N, u32 n) 156 | { 157 | u32 i; 158 | 159 | for (i = 0; i < 8*n; i++) 160 | bn_add(d, d, d, N, n); 161 | } 162 | 163 | void bn_from_mon(u8 *d, u8 *N, u32 n) 164 | { 165 | u8 t[512]; 166 | 167 | bn_zero(t, n); 168 | t[n-1] = 1; 169 | bn_mon_mul(d, d, t, N, n); 170 | } 171 | 172 | static void bn_mon_exp(u8 *d, u8 *a, u8 *N, u32 n, u8 *e, u32 en) 173 | { 174 | u8 t[512]; 175 | u32 i; 176 | u8 mask; 177 | 178 | bn_zero(d, n); 179 | d[n-1] = 1; 180 | bn_to_mon(d, N, n); 181 | 182 | for (i = 0; i < en; i++) 183 | for (mask = 0x80; mask != 0; mask >>= 1) { 184 | bn_mon_mul(t, d, d, N, n); 185 | if ((e[i] & mask) != 0) 186 | bn_mon_mul(d, t, a, N, n); 187 | else 188 | bn_copy(d, t, n); 189 | } 190 | } 191 | 192 | void bn_mon_inv(u8 *d, u8 *a, u8 *N, u32 n) 193 | { 194 | u8 t[512], s[512]; 195 | 196 | bn_zero(s, n); 197 | s[n-1] = 2; 198 | bn_sub_1(t, N, s, n); 199 | bn_mon_exp(d, a, N, n, t, n); 200 | } 201 | -------------------------------------------------------------------------------- /ec.c: -------------------------------------------------------------------------------- 1 | // Copyright 2007,2008,2010 Segher Boessenkool 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | 8 | #include "tools.h" 9 | 10 | struct point { 11 | u8 x[20]; 12 | u8 y[20]; 13 | }; 14 | 15 | static u8 ec_p[20]; 16 | static u8 ec_a[20]; // mon 17 | static u8 ec_b[20]; // mon 18 | static u8 ec_N[21]; 19 | static struct point ec_G; // mon 20 | static struct point ec_Q; // mon 21 | static u8 ec_k[21]; 22 | 23 | static void elt_copy(u8 *d, u8 *a) 24 | { 25 | memcpy(d, a, 20); 26 | } 27 | 28 | static void elt_zero(u8 *d) 29 | { 30 | memset(d, 0, 20); 31 | } 32 | 33 | static int elt_is_zero(u8 *d) 34 | { 35 | u32 i; 36 | 37 | for (i = 0; i < 20; i++) 38 | if (d[i] != 0) 39 | return 0; 40 | 41 | return 1; 42 | } 43 | 44 | static void elt_add(u8 *d, u8 *a, u8 *b) 45 | { 46 | bn_add(d, a, b, ec_p, 20); 47 | } 48 | 49 | static void elt_sub(u8 *d, u8 *a, u8 *b) 50 | { 51 | bn_sub(d, a, b, ec_p, 20); 52 | } 53 | 54 | static void elt_mul(u8 *d, u8 *a, u8 *b) 55 | { 56 | bn_mon_mul(d, a, b, ec_p, 20); 57 | } 58 | 59 | static void elt_square(u8 *d, u8 *a) 60 | { 61 | elt_mul(d, a, a); 62 | } 63 | 64 | static void elt_inv(u8 *d, u8 *a) 65 | { 66 | u8 s[20]; 67 | elt_copy(s, a); 68 | bn_mon_inv(d, s, ec_p, 20); 69 | } 70 | 71 | static void point_to_mon(struct point *p) 72 | { 73 | bn_to_mon(p->x, ec_p, 20); 74 | bn_to_mon(p->y, ec_p, 20); 75 | } 76 | 77 | static void point_from_mon(struct point *p) 78 | { 79 | bn_from_mon(p->x, ec_p, 20); 80 | bn_from_mon(p->y, ec_p, 20); 81 | } 82 | 83 | #if 0 84 | static int point_is_on_curve(u8 *p) 85 | { 86 | u8 s[20], t[20]; 87 | u8 *x, *y; 88 | 89 | x = p; 90 | y = p + 20; 91 | 92 | elt_square(t, x); 93 | elt_mul(s, t, x); 94 | 95 | elt_mul(t, x, ec_a); 96 | elt_add(s, s, t); 97 | 98 | elt_add(s, s, ec_b); 99 | 100 | elt_square(t, y); 101 | elt_sub(s, s, t); 102 | 103 | return elt_is_zero(s); 104 | } 105 | #endif 106 | 107 | static void point_zero(struct point *p) 108 | { 109 | elt_zero(p->x); 110 | elt_zero(p->y); 111 | } 112 | 113 | static int point_is_zero(struct point *p) 114 | { 115 | return elt_is_zero(p->x) && elt_is_zero(p->y); 116 | } 117 | 118 | static void point_double(struct point *r, struct point *p) 119 | { 120 | u8 s[20], t[20]; 121 | struct point pp; 122 | u8 *px, *py, *rx, *ry; 123 | 124 | pp = *p; 125 | 126 | px = pp.x; 127 | py = pp.y; 128 | rx = r->x; 129 | ry = r->y; 130 | 131 | if (elt_is_zero(py)) { 132 | point_zero(r); 133 | return; 134 | } 135 | 136 | elt_square(t, px); // t = px*px 137 | elt_add(s, t, t); // s = 2*px*px 138 | elt_add(s, s, t); // s = 3*px*px 139 | elt_add(s, s, ec_a); // s = 3*px*px + a 140 | elt_add(t, py, py); // t = 2*py 141 | elt_inv(t, t); // t = 1/(2*py) 142 | elt_mul(s, s, t); // s = (3*px*px+a)/(2*py) 143 | 144 | elt_square(rx, s); // rx = s*s 145 | elt_add(t, px, px); // t = 2*px 146 | elt_sub(rx, rx, t); // rx = s*s - 2*px 147 | 148 | elt_sub(t, px, rx); // t = -(rx-px) 149 | elt_mul(ry, s, t); // ry = -s*(rx-px) 150 | elt_sub(ry, ry, py); // ry = -s*(rx-px) - py 151 | } 152 | 153 | static void point_add(struct point *r, struct point *p, struct point *q) 154 | { 155 | u8 s[20], t[20], u[20]; 156 | u8 *px, *py, *qx, *qy, *rx, *ry; 157 | struct point pp, qq; 158 | 159 | pp = *p; 160 | qq = *q; 161 | 162 | px = pp.x; 163 | py = pp.y; 164 | qx = qq.x; 165 | qy = qq.y; 166 | rx = r->x; 167 | ry = r->y; 168 | 169 | if (point_is_zero(&pp)) { 170 | elt_copy(rx, qx); 171 | elt_copy(ry, qy); 172 | return; 173 | } 174 | 175 | if (point_is_zero(&qq)) { 176 | elt_copy(rx, px); 177 | elt_copy(ry, py); 178 | return; 179 | } 180 | 181 | elt_sub(u, qx, px); 182 | 183 | if (elt_is_zero(u)) { 184 | elt_sub(u, qy, py); 185 | if (elt_is_zero(u)) 186 | point_double(r, &pp); 187 | else 188 | point_zero(r); 189 | 190 | return; 191 | } 192 | 193 | elt_inv(t, u); // t = 1/(qx-px) 194 | elt_sub(u, qy, py); // u = qy-py 195 | elt_mul(s, t, u); // s = (qy-py)/(qx-px) 196 | 197 | elt_square(rx, s); // rx = s*s 198 | elt_add(t, px, qx); // t = px+qx 199 | elt_sub(rx, rx, t); // rx = s*s - (px+qx) 200 | 201 | elt_sub(t, px, rx); // t = -(rx-px) 202 | elt_mul(ry, s, t); // ry = -s*(rx-px) 203 | elt_sub(ry, ry, py); // ry = -s*(rx-px) - py 204 | } 205 | 206 | static void point_mul(struct point *d, u8 *a, struct point *b) // a is bignum 207 | { 208 | u32 i; 209 | u8 mask; 210 | 211 | point_zero(d); 212 | 213 | for (i = 0; i < 21; i++) 214 | for (mask = 0x80; mask != 0; mask >>= 1) { 215 | point_double(d, d); 216 | if ((a[i] & mask) != 0) 217 | point_add(d, d, b); 218 | } 219 | } 220 | 221 | static void generate_ecdsa(u8 *R, u8 *S, u8 *k, u8 *hash) 222 | { 223 | u8 e[21]; 224 | u8 kk[21]; 225 | u8 m[21]; 226 | u8 minv[21]; 227 | struct point mG; 228 | 229 | e[0] = 0; 230 | memcpy(e + 1, hash, 20); 231 | bn_reduce(e, ec_N, 21); 232 | 233 | try_again: 234 | 235 | get_rand(m, sizeof m); 236 | m[0] = 0; 237 | if (bn_compare(m, ec_N, 21) >= 0) 238 | goto try_again; 239 | 240 | // R = (mG).x 241 | 242 | point_mul(&mG, m, &ec_G); 243 | point_from_mon(&mG); 244 | R[0] = 0; 245 | elt_copy(R+1, mG.x); 246 | 247 | // S = m**-1*(e + Rk) (mod N) 248 | 249 | bn_copy(kk, k, 21); 250 | bn_reduce(kk, ec_N, 21); 251 | bn_to_mon(m, ec_N, 21); 252 | bn_to_mon(e, ec_N, 21); 253 | bn_to_mon(R, ec_N, 21); 254 | bn_to_mon(kk, ec_N, 21); 255 | 256 | bn_mon_mul(S, R, kk, ec_N, 21); 257 | bn_add(kk, S, e, ec_N, 21); 258 | bn_mon_inv(minv, m, ec_N, 21); 259 | bn_mon_mul(S, minv, kk, ec_N, 21); 260 | 261 | bn_from_mon(R, ec_N, 21); 262 | bn_from_mon(S, ec_N, 21); 263 | } 264 | 265 | static int check_ecdsa(struct point *Q, u8 *R, u8 *S, u8 *hash) 266 | { 267 | u8 Sinv[21]; 268 | u8 e[21]; 269 | u8 w1[21], w2[21]; 270 | struct point r1, r2; 271 | u8 rr[21]; 272 | 273 | e[0] = 0; 274 | memcpy(e + 1, hash, 20); 275 | bn_reduce(e, ec_N, 21); 276 | 277 | bn_to_mon(R, ec_N, 21); 278 | bn_to_mon(S, ec_N, 21); 279 | bn_to_mon(e, ec_N, 21); 280 | 281 | bn_mon_inv(Sinv, S, ec_N, 21); 282 | 283 | bn_mon_mul(w1, e, Sinv, ec_N, 21); 284 | bn_mon_mul(w2, R, Sinv, ec_N, 21); 285 | 286 | bn_from_mon(w1, ec_N, 21); 287 | bn_from_mon(w2, ec_N, 21); 288 | 289 | point_mul(&r1, w1, &ec_G); 290 | point_mul(&r2, w2, Q); 291 | 292 | point_add(&r1, &r1, &r2); 293 | 294 | point_from_mon(&r1); 295 | 296 | rr[0] = 0; 297 | memcpy(rr + 1, r1.x, 20); 298 | bn_reduce(rr, ec_N, 21); 299 | 300 | bn_from_mon(R, ec_N, 21); 301 | bn_from_mon(S, ec_N, 21); 302 | 303 | return (bn_compare(rr, R, 21) == 0); 304 | } 305 | 306 | #if 0 307 | static void ec_priv_to_pub(u8 *k, u8 *Q) 308 | { 309 | point_mul(Q, k, ec_G); 310 | } 311 | #endif 312 | 313 | int ecdsa_set_curve(u32 type) 314 | { 315 | if (ecdsa_get_params(type, ec_p, ec_a, ec_b, ec_N, ec_G.x, ec_G.y) < 0) 316 | return -1; 317 | 318 | bn_to_mon(ec_a, ec_p, 20); 319 | bn_to_mon(ec_b, ec_p, 20); 320 | 321 | point_to_mon(&ec_G); 322 | 323 | return 0; 324 | } 325 | 326 | void ecdsa_set_pub(u8 *Q) 327 | { 328 | memcpy(ec_Q.x, Q, 20); 329 | memcpy(ec_Q.y, Q+20, 20); 330 | point_to_mon(&ec_Q); 331 | } 332 | 333 | void ecdsa_set_priv(u8 *k) 334 | { 335 | memcpy(ec_k, k, sizeof ec_k); 336 | } 337 | 338 | int ecdsa_verify(u8 *hash, u8 *R, u8 *S) 339 | { 340 | return check_ecdsa(&ec_Q, R, S, hash); 341 | } 342 | 343 | void ecdsa_sign(u8 *hash, u8 *R, u8 *S) 344 | { 345 | generate_ecdsa(R, S, ec_k, hash); 346 | } 347 | -------------------------------------------------------------------------------- /little_endian.h: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Grazioli Giovanni 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #ifndef _ENDIANESS_TOOLS_H__ 6 | #define _ENDIANESS_TOOLS_H__ 7 | #include "types.h" 8 | 9 | static inline u8 le8(u8 *p) 10 | { 11 | return *p; 12 | } 13 | 14 | static inline u16 le16(u8 *p) 15 | { 16 | u16 a; 17 | 18 | a = p[1] << 8; 19 | a |= p[0]; 20 | 21 | return a; 22 | } 23 | 24 | static inline u32 le32(u8 *p) 25 | { 26 | u32 a; 27 | 28 | a = p[3] << 24; 29 | a |= p[2] << 16; 30 | a |= p[1] << 8; 31 | a |= p[0] << 0; 32 | 33 | return a; 34 | } 35 | 36 | static inline u64 le64(u8 *p) 37 | { 38 | u32 a, b; 39 | 40 | a = le32(p + 4); 41 | b = le32(p); 42 | 43 | return ((u64)a<<32) | b; 44 | } 45 | 46 | static inline void wle16(u8 *p, u16 v) 47 | { 48 | p[1] = v >> 8; 49 | p[2] = v; 50 | } 51 | 52 | static inline void wle32(u8 *p, u32 v) 53 | { 54 | p[3] = v >> 24; 55 | p[2] = v >> 16; 56 | p[1] = v >> 8; 57 | p[0] = v; 58 | } 59 | 60 | static inline void wle64(u8 *p, u64 v) 61 | { 62 | wle32(p + 4, v); 63 | v >>= 32; 64 | wle32(p, v); 65 | } 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /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 | #include 13 | #include "mingw_mmap.h" 14 | 15 | extern int getpagesize(); 16 | 17 | /** 18 | * Use CreateFileMapping and MapViewOfFile to simulate POSIX mmap(). 19 | * Why Microsoft won't just implement these is beyond everyone's comprehension. 20 | * @return pointer or NULL 21 | */ 22 | void *mingw_mmap(void *pStart, size_t sLength, int nProt, int nFlags, int nFd, off_t oOffset) { 23 | (void)nProt; 24 | HANDLE hHandle; 25 | 26 | if (pStart != NULL || !(nFlags & MAP_PRIVATE)) { 27 | printf("Invalid usage of mingw_mmap"); 28 | return NULL; 29 | } 30 | 31 | if (oOffset % getpagesize() != 0) { 32 | printf("Offset does not match the memory allocation granularity"); 33 | return NULL; 34 | } 35 | 36 | hHandle = CreateFileMapping((HANDLE)_get_osfhandle(nFd), NULL, PAGE_WRITECOPY, 0, 0, NULL); 37 | if (hHandle != NULL) { 38 | pStart = MapViewOfFile(hHandle, FILE_MAP_COPY, 0, oOffset, sLength); 39 | } 40 | 41 | return pStart; 42 | } 43 | 44 | /** 45 | * Use UnmapViewOfFile to undo mmap() above. 46 | * @param pStart 47 | * @param length - Not used, kept for compatibility. 48 | * @return boolean; no checks are performed. 49 | */ 50 | int mingw_munmap(void *pStart, size_t sLength) { 51 | (void)sLength; 52 | 53 | if (UnmapViewOfFile(pStart) != 0) 54 | return FALSE; 55 | 56 | return TRUE; 57 | } 58 | -------------------------------------------------------------------------------- /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 2012 Grazioli Giovanni 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include "tools.h" 13 | #include "types.h" 14 | #include "little_endian.h" 15 | 16 | #ifdef WIN32 17 | #define MKDIR(x,y) mkdir(x) 18 | #else 19 | #define MKDIR(x,y) mkdir(x,y) 20 | #endif 21 | 22 | static u8 *pup = NULL; 23 | static u32 file_count; 24 | 25 | const u64 PSV_HDR = 0x5343455546000001ull; 26 | 27 | static int read_only = 0; 28 | static int unknown_type = 0; 29 | static char unknown_name[256]; 30 | 31 | static struct id2name_tbl t_names[] = { 32 | {0x100, "version.txt"}, 33 | {0x101, "license.xml"}, 34 | {0x200, "psp2swu.self"}, 35 | {0x204, "psp2swu.sprx"}, 36 | {0x221, "psv_package_data01.pkg"}, 37 | {0x231, "psv_package_data02.pkg"}, 38 | {0x301, "package_data01.pkg"}, 39 | {0x302, "package_data02.pkg"}, 40 | {0x303, "package_data03.pkg"}, 41 | {0x304, "package_data04.pkg"}, 42 | {0x305, "package_data05.pkg"}, 43 | {0x306, "package_data06.pkg"}, 44 | {0x307, "package_data07.pkg"}, 45 | {0x308, "package_data08.pkg"}, 46 | {0x309, "package_data09.pkg"}, 47 | {0x30a, "package_data10.pkg"}, 48 | {0x30b, "package_data11.pkg"}, 49 | {0x30c, "package_data12.pkg"}, 50 | {0x30d, "package_data13.pkg"}, 51 | {0x30e, "package_data14.pkg"}, 52 | {0x30f, "package_data15.pkg"}, 53 | {0x310, "package_data16.pkg"}, 54 | {0x311, "package_data17.pkg"}, 55 | {0x312, "package_data18.pkg"}, 56 | {0x313, "package_data19.pkg"}, 57 | {0x314, "package_data20.pkg"}, 58 | {0x315, "package_data21.pkg"}, 59 | {0x316, "package_data22.pkg"}, 60 | {0x317, "package_data23.pkg"}, 61 | {0x318, "package_data24.pkg"}, 62 | {0x319, "package_data25.pkg"}, 63 | {0x31a, "package_data26.pkg"}, 64 | {0x326, "debug_data00.pkg"}, 65 | {0x327, "debug_data01.pkg"}, 66 | {0x328, "debug_data02.pkg"}, 67 | {0x329, "debug_data03.pkg"}, 68 | {0x32a, "debug_data04.pkg"}, 69 | {0x32b, "debug_data05.pkg"}, 70 | {0x32c, "debug_data06.pkg"}, 71 | {0x400, "package_scewm.wm"}, 72 | {0x401, "package_sceas.as"}, 73 | {0, NULL} 74 | }; 75 | 76 | void Write(const char *file, u32 offset, u32 size){ 77 | FILE *fp; 78 | fp = fopen(file, "wb"); 79 | u32 i; 80 | for(i=0;i>32,HDR); 109 | 110 | u32 pkg_version = le32(pup+0x08); 111 | u32 img_version = le32(pup+0x10); 112 | file_count = le32(pup+0x18); 113 | u32 hdr_lenght = le32(pup+0x20); 114 | u32 pkg_lenght = le32(pup+0x28); 115 | 116 | dmsg("HDR 0x%08x%08x\n",HDR>>32,HDR); 117 | dmsg("PKG VERSION 0x%08x\n",pkg_version); 118 | dmsg("IMG VERSION 0x%08x\n",img_version); 119 | printf("N of Files %u\n",file_count); 120 | dmsg("HDR Lenght 0x%08x\n",hdr_lenght); 121 | dmsg("PKG Lenght 0x%08x\n",pkg_lenght); 122 | dmsg("Table Lenght 0x%08x\n",0x80+(0x20*file_count)); 123 | u32 entry,offset,size; 124 | for(i=0;i 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "tools.h" 12 | #include "types.h" 13 | #include "little_endian.h" 14 | #include "readself.h" 15 | 16 | static u8 *self = NULL; 17 | 18 | const u32 HDR = 0x53434500; 19 | 20 | //64bit 21 | 22 | 23 | void program_hdr(u64 offset){ 24 | uint32_t p_type = le32(self+offset); /* type of segment */ 25 | offset+=sizeof(uint32_t); 26 | uint32_t p_flags = le32(self+offset); /* segment attributes */ 27 | offset+=sizeof(uint32_t); 28 | uint32_t p_offset = le32(self+offset); /* offset in file */ 29 | offset+=sizeof(uint32_t); 30 | uint32_t p_vaddr = le32(self+offset); /* virtual address in memory */ 31 | offset+=sizeof(uint32_t); 32 | uint32_t p_paddr = le32(self+offset); /* reserved */ 33 | offset+=sizeof(uint32_t); 34 | uint32_t p_filesz = le32(self+offset); /* size of segment in file */ 35 | offset+=sizeof(uint32_t); 36 | uint32_t p_memsz = le32(self+offset); /* size of segment in memory */ 37 | offset+=sizeof(uint32_t); 38 | uint32_t p_align = le32(self+offset); /* alignment of segment */ 39 | offset+=sizeof(uint32_t); 40 | 41 | const char* p_type_char = prg_type(p_type); 42 | const char* p_flags_char = prg_flags(p_flags); 43 | 44 | printf("--- SELF PROGRAM INFO ---\n"); 45 | printf("Type 0x%08x [%s]\n",p_type,p_type_char); 46 | printf("Flags 0x%08x [%s]\n",p_flags,p_flags_char); 47 | printf("Offset 0x%08x\n",p_offset); 48 | printf("Virtual Address 0x%08x\n",p_vaddr); 49 | printf("Reserved Adderess 0x%08x\n",p_paddr); 50 | printf("Real Size 0x%08x (%u Bytes)\n",p_filesz,p_filesz); 51 | printf("Size in Memory 0x%08x (%u Bytes)\n",p_memsz,p_memsz); 52 | printf("Alignment 0x%08x (%u)\n",p_align,p_align); 53 | } 54 | 55 | void section_hdr(u64 offset){ 56 | uint32_t sh_name = le32(self+offset); /* section name */ 57 | offset+=sizeof(uint32_t); 58 | uint32_t sh_type = le32(self+offset); /* section type */ 59 | offset+=sizeof(uint32_t); 60 | uint32_t sh_flags = le32(self+offset); /* section attributes */ 61 | offset+=sizeof(uint32_t); 62 | uint32_t sh_addr = le32(self+offset); /* virtual address in memory */ 63 | offset+=sizeof(uint32_t); 64 | uint32_t sh_offset = le32(self+offset); /* offset in file */ 65 | offset+=sizeof(uint32_t); 66 | uint32_t sh_size = le32(self+offset); /* size of section */ 67 | offset+=sizeof(uint32_t); 68 | uint32_t sh_link = le32(self+offset); /* link to other section */ 69 | offset+=sizeof(uint32_t); 70 | uint32_t sh_info = le32(self+offset); /* miscellaneous information */ 71 | offset+=sizeof(uint32_t); 72 | uint32_t sh_addralign = le32(self+offset); /* address alignment boundary */ 73 | offset+=sizeof(uint32_t); 74 | uint32_t sh_entsize = le32(self+offset); /* size of entries, if section has table */ 75 | offset+=sizeof(uint32_t); 76 | 77 | printf("--- SELF PROGRAM INFO ---\n"); 78 | printf("Name 0x%08x\n",sh_name); 79 | printf("Type 0x%08x\n",sh_type); 80 | printf("Flags 0x%08x\n",sh_flags); 81 | printf("Virtual Address 0x%08x\n",sh_addr); 82 | printf("Offset 0x%08x (%u Bytes)\n",sh_offset,sh_offset); 83 | printf("Size 0x%08x\n",sh_size); 84 | printf("Link to other section 0x%08x\n",sh_link); 85 | printf("Info 0x%08x\n",sh_info); 86 | printf("Address Alignment 0x%08x\n",sh_addralign); 87 | printf("Size Entries 0x%08x (%u Bytes)\n",sh_entsize,sh_entsize); 88 | } 89 | 90 | void elf(u64 offset){ 91 | uint8_t e_ident[16]; 92 | int i=0; 93 | for(i=0;i<0x10;i++) 94 | e_ident[i] = be8(self+offset+i); 95 | 96 | uint16_t e_type = le16(self+offset+0x10); /* object file type */ 97 | offset+=0x10+sizeof(uint16_t); 98 | 99 | uint16_t e_machine = le16(self+offset); /* machine type */ 100 | offset+=sizeof(uint16_t); 101 | 102 | uint32_t e_version = le32(self+offset); /* object file version */ 103 | offset+=sizeof(uint32_t); 104 | 105 | uint32_t e_entry = le32(self+offset); /* entry point address */ 106 | offset+=sizeof(uint32_t); 107 | 108 | uint32_t e_phoff = le32(self+offset); /* program header offset */ 109 | offset+=sizeof(uint32_t); 110 | 111 | uint32_t e_shoff = le32(self+offset); /* section header offset */ 112 | offset+=sizeof(uint32_t); 113 | 114 | uint16_t e_flags = le16(self+offset); /* processor-specific flags */ 115 | offset+=sizeof(uint16_t); 116 | 117 | uint32_t e_ehsize = le16(self+offset); /* ELF header size */ 118 | offset+=sizeof(uint16_t); 119 | 120 | uint16_t e_phentsize = le16(self+offset); /* size of program header entry */ 121 | offset+=sizeof(uint16_t); 122 | 123 | uint16_t e_phnum = le16(self+offset); /* number of program header entries */ 124 | offset+=sizeof(uint16_t); 125 | 126 | uint16_t e_shentsize = le16(self+offset); /* size of section header entry */ 127 | offset+=sizeof(uint16_t); 128 | 129 | uint16_t e_shnum = le16(self+offset); /* number of section header entries */ 130 | offset+=sizeof(uint16_t); 131 | 132 | uint16_t e_shstrndx = le16(self+offset); /* section name string table index */ 133 | offset+=sizeof(uint16_t); 134 | 135 | printf("--- ELF INFO ---\n"); 136 | 137 | printf("File Class 0x%02x [%s]\n",e_ident[4],(e_ident[4]==1) ? "ELFCLASS64" : "ELFCLASS32"); 138 | printf("File Data 0x%02x [%s]\n",e_ident[5],(e_ident[5]==1) ? "ELFDATA2MSB][BIG ENDIAN" : "ELFDATA2LSB][LITTLE ENDIAN"); 139 | printf("File Version 0x%02x\n",e_ident[6]); 140 | 141 | printf("Type 0x%04x [%s]\n",e_type,(e_type==2) ? "EXEC" : "PRX"); 142 | printf("Machine Type 0x%04x\n",e_machine); 143 | printf("Version 0x%08x\n",e_version); 144 | printf("Entry Point Address 0x%08x\n",e_entry); 145 | printf("Program Header Offset 0x%08x\n",e_phoff); 146 | printf("Section Header Offset 0x%08x\n",e_shoff); 147 | printf("Flag 0x%04x\n",e_flags); 148 | printf("ELF Header Size 0x%08x (%u Bytes)\n",e_ehsize,e_ehsize); 149 | printf("Size Progr Hdr Entry 0x%04x (%u Bytes)\n",e_phentsize,e_phentsize); 150 | printf("Number Hdr Entries 0x%04x (%u)\n",e_phnum,e_phnum); 151 | printf("Size Sect Hdr Entry 0x%04x (%u Bytes)\n",e_shentsize,e_shentsize); 152 | printf("Number Hdr Entries 0x%04x (%u)\n",e_shnum,e_shnum); 153 | printf("Section String Table 0x%04x\n",e_shstrndx); 154 | 155 | } 156 | 157 | void app_info(u64 offset){ 158 | u64 authid = le64(self+offset); /* auth id */ 159 | offset += sizeof(u64); 160 | u32 vendor_id = le32(self+offset); /* vendor id */ 161 | offset += sizeof(u32); 162 | u32 self_type = le32(self+offset); /* app type */ 163 | offset += sizeof(u32); 164 | u64 version = le64(self+offset); /* app version */ 165 | offset += sizeof(u64); 166 | u64 padding = le64(self+offset); /* UNKNOWN */ 167 | offset += sizeof(u64); 168 | 169 | printf("--- APP INFO ---\n"); 170 | printf("Authotiry ID 0x%016lx\n",authid); 171 | printf("Vendor ID 0x%08x\n",vendor_id); 172 | printf("SELF Type 0x%08x\n",self_type); 173 | printf("Version 0x%016lx\n",version); 174 | printf("Padding 0x%016lx\n",padding); 175 | 176 | 177 | } 178 | 179 | void sce_version(u64 offset){ 180 | uint32_t unknown1 = le32(self+offset); 181 | offset+=sizeof(uint32_t); 182 | uint32_t unknown2 = le32(self+offset); 183 | offset+=sizeof(uint32_t); 184 | uint32_t unknown3 = le32(self+offset); 185 | offset+=sizeof(uint32_t); 186 | uint32_t unknown4 = le32(self+offset); 187 | offset+=sizeof(uint32_t); 188 | 189 | printf("--- SELF SCE VERSION ---\n"); 190 | printf("Unknown_1 0x%08x\n",unknown1); 191 | printf("Unknown_2 0x%08x\n",unknown2); 192 | printf("Unknown_3 0x%08x\n",unknown3); 193 | printf("Unknown_4 0x%08x\n",unknown4); 194 | 195 | } 196 | 197 | void control_information(u64 offset){ 198 | uint32_t type = le32(self+offset); // 4== ; 6==;7== 199 | offset+=sizeof(uint32_t); 200 | uint32_t size = le32(self+offset); 201 | offset+=sizeof(uint32_t); 202 | uint64_t unknown = le64(self+offset); // 0;1 203 | offset+=sizeof(uint64_t); 204 | 205 | printf("--- CONTROL INFORMATION ---\n"); 206 | printf("Type 0x%08x\n",type); 207 | printf("Size 0x%08x\n",size); 208 | printf("Unknown 0x%016lx\n",unknown); 209 | 210 | } 211 | 212 | 213 | void readself(){ 214 | printf("\nReading...\n"); 215 | u32 offset=0; 216 | u32 magic = be32(self); /* 53434500 = SCE\0 */ 217 | offset += sizeof(u32); 218 | u32 version = le32(self+offset); /* header version 3*/ 219 | offset += sizeof(u32); 220 | u16 sdk_type = le16(self+offset); /* SDK type */ 221 | offset += sizeof(u16); 222 | u16 header_type = le16(self+offset); /* 1 self, 2 unknown, 3 pkg */ 223 | offset += sizeof(u16); 224 | u32 metadata_offset = le32(self+offset); /* metadata offset */ 225 | offset += sizeof(u32); 226 | u64 header_len = le64(self+offset); /* self header length */ 227 | offset += sizeof(u64); 228 | u64 elf_filesize = le64(self+offset); /* ELF file length */ 229 | offset += sizeof(u64); 230 | u64 self_filesize = le64(self+offset); /* SELF file length */ 231 | offset += sizeof(u64); 232 | u64 unknown1 = le64(self+offset); /* UNKNOWN */ 233 | offset += sizeof(u64); 234 | u64 self_offset = le64(self+offset); /* Self Offset */ 235 | offset += sizeof(u64); 236 | u64 appinfo_offset = le64(self+offset); /* app info offset */ 237 | offset += sizeof(u64); 238 | u64 elf_offset = le64(self+offset); /* ELF #1 offset */ 239 | offset += sizeof(u64); 240 | u64 phdr_offset = le64(self+offset); /* program header offset */ 241 | offset += sizeof(u64); 242 | u64 shdr_offset = le64(self+offset); /* section header offset */ 243 | offset += sizeof(u64); 244 | u64 section_info_offset = le64(self+offset); /* section info offset */ 245 | offset += sizeof(u64); 246 | u64 sceversion_offset = le64(self+offset); /* version offset */ 247 | offset += sizeof(u64); 248 | u64 controlinfo_offset = le64(self+offset); /* control info offset */ 249 | offset += sizeof(u64); 250 | u64 controlinfo_size = le64(self+offset); /* control info size */ 251 | offset += sizeof(u64); 252 | 253 | 254 | if(magic!=HDR) 255 | fail("\nERROR! Not a PlayStation Vita Self File (Magic: %08x)",magic); 256 | 257 | if(header_type!=1){ 258 | char* HType; 259 | if(header_type==2) 260 | HType = "rvk"; 261 | else if(header_type==3) 262 | HType = "pkg"; 263 | else if(header_type==4) 264 | HType = "spp"; 265 | else 266 | HType = "Unknown"; 267 | 268 | fail("\nERROR! Not a PlayStation Vita Self File (Type : %08x)\n" 269 | " [%s]",header_type,HType); 270 | } 271 | 272 | printf("Magic 0x%08x\n",magic); 273 | printf("Version 0x%08x\n",version); 274 | printf("SDK Type 0x%08x\n",sdk_type); 275 | printf("Header Type 0x%08x\n",header_type); 276 | printf("Metadata offset 0x%08x\n",metadata_offset); 277 | printf("Header Length 0x%016lx (%lu Bytes)\n",header_len,header_len); 278 | printf("Elf Size 0x%016lx (%lu Bytes)\n",elf_filesize,elf_filesize); 279 | printf("Self Size 0x%016lx (%lu Bytes)\n",self_filesize,self_filesize); 280 | printf("Unknown_1 0x%016lx\n",unknown1); 281 | printf("Self Offset 0x%016lx\n",self_offset); 282 | printf("Application Info Offset 0x%016lx\n",appinfo_offset); 283 | printf("Elf Offset 0x%016lx\n",elf_offset); 284 | printf("Program hdr Offset 0x%016lx\n",phdr_offset); 285 | printf("Section hdr Offset 0x%016lx\n",shdr_offset); 286 | printf("Section Info Offset 0x%016lx\n",section_info_offset); 287 | printf("Version Offset 0x%016lx\n",sceversion_offset); 288 | printf("Control Info Offset 0x%016lx\n",controlinfo_offset); 289 | printf("Control Info Size 0x%016lx (%lu Bytes)\n",controlinfo_size,controlinfo_size); 290 | 291 | app_info(appinfo_offset); 292 | elf(elf_offset); 293 | program_hdr(phdr_offset); 294 | section_hdr(shdr_offset); 295 | sce_version(sceversion_offset); 296 | control_information(controlinfo_offset); 297 | 298 | printf("\nDone\n"); 299 | } 300 | 301 | int main(int argc, char *argv[]){ 302 | 303 | if (argc == 2) { 304 | self = mmap_file(argv[1]); 305 | printf( "readself - v0.1 By deroad\n\n"); 306 | readself(); 307 | }else { 308 | fail("usage: %s file.self" 309 | ,argv[0]); 310 | } 311 | 312 | 313 | return 0; 314 | } 315 | -------------------------------------------------------------------------------- /readself.h: -------------------------------------------------------------------------------- 1 | #ifndef __READSELF_H__ 2 | #define __READSELF_H__ 3 | 4 | #include "little_endian.h" 5 | 6 | //PROGRAM TYPES 7 | 8 | #define PT_NULL 0 9 | #define PT_LOAD 1 10 | #define PT_DYNAMIC 2 11 | #define PT_INTERP 3 12 | #define PT_NOTE 4 13 | #define PT_SHLIB 5 14 | #define PT_PHDR 6 15 | #define PT_TLS 7 16 | #define PT_LOOS 0x60000000 17 | #define PT_HIOS 0x6fffffff 18 | #define PT_LOPROC 0x70000000 19 | #define PT_HIPROC 0x7fffffff 20 | 21 | //PROGRAM FLAGS 22 | 23 | #define PF_X 0x1 //Execute 24 | #define PF_W 0x2 //Write 25 | #define PF_R 0x4 //Read 26 | #define PF_MASKOS 0x0ff00000 //Unspecified 27 | #define PF_MASKPROC 0xf0000000 //Unspecified 28 | 29 | 30 | 31 | const char *prg_type(u32 type){ 32 | if(type==PT_NULL) 33 | return "PT_NULL"; 34 | else if(type==PT_LOAD) 35 | return "PT_LOAD"; 36 | else if(type==PT_DYNAMIC) 37 | return "PT_DYNAMIC"; 38 | else if(type==PT_INTERP) 39 | return "PT_INTERP"; 40 | else if(type==PT_NOTE) 41 | return "PT_NOTE"; 42 | else if(type==PT_SHLIB) 43 | return "PT_SHLIB"; 44 | else if(type==PT_PHDR) 45 | return "PT_PHDR"; 46 | else if(type==PT_TLS) 47 | return "PT_TLS"; 48 | else if(type==PT_LOOS) 49 | return "PT_LOOS"; 50 | else if(type==PT_HIOS) 51 | return "PT_HIOS"; 52 | else if(type==PT_LOPROC) 53 | return "PT_LOPROC"; 54 | else if(type==PT_HIPROC) 55 | return "PT_HIPROC"; 56 | else 57 | return "PT_UNKNOWN"; 58 | } 59 | 60 | 61 | const char *prg_flags(u32 flags){ 62 | if(flags==PF_X) 63 | return "PF_X"; 64 | else if(flags==PF_W) 65 | return "PF_W"; 66 | else if(flags==PF_R) 67 | return "PF_DYNAMIC"; 68 | else if(flags==PF_MASKOS) 69 | return "PF_MASKOS"; 70 | else if(flags==PF_MASKPROC) 71 | return "PF_MASKPROC"; 72 | else 73 | return "PF_UNKNOWN"; 74 | } 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /sfo_stuff.c: -------------------------------------------------------------------------------- 1 | #include "sfo_stuff.h" 2 | u32 sfo_header = 0x46535000; //Big endian 3 | 4 | 5 | void analizeSFO(u8 *sfo, u32 sfo_offset, u32 sfo_size){ 6 | u32 sfo_vals = le32(sfo+sfo_offset+0x08)+sfo_offset; 7 | u32 sfo_param = le32(sfo+sfo_offset+0x0c)+sfo_offset; 8 | u32 sfo_type; 9 | u32 sfo_n_params = le32(sfo+sfo_offset+0x10); 10 | u32 sfo_val_ptr,sfo_val_size,sfo_param_ptr,sfo_param_size; 11 | u64 param32; 12 | u32 i,k; 13 | 14 | 15 | dmsg("[SFO HDR] 0x%08x\n", (unsigned int)le32(sfo+sfo_offset)); 16 | dmsg("[SFO Version] 0x%08x\n", (unsigned int)le32(sfo+sfo_offset+0x4)); 17 | dmsg("[SFO N] %u Value(s)\n", sfo_n_params); 18 | dmsg("[SFO Offset] 0x%08x\n", sfo_offset); 19 | dmsg("[SFO Size] 0x%08x\n", sfo_size); 20 | dmsg("[SFO Values] 0x%08x\n", sfo_vals); 21 | dmsg("[SFO Params] 0x%08x\n", sfo_param); 22 | printf("[ SFO ]\n"); 23 | for(i=0x0;i0x4&&sfo_param_size<=0x8){ 49 | if(sfo_type!=UTF8){ 50 | param32 = be32(sfo+sfo_param_ptr); 51 | printf(" | Param: 0x%x\n",(unsigned int)param32); 52 | }else{ 53 | for(k=0x0;k0x8&&sfo_param_size<=0x16){ 59 | if(sfo_type!=UTF8){ 60 | param32 = be64(sfo+sfo_param_ptr); 61 | printf(" | Param: 0x%x\n",(unsigned int)param32); 62 | }else{ 63 | for(k=0x0;k0x16&&sfo_param_size<=0x30){ 69 | if(sfo_type!=UTF8){ 70 | param32 = be64(sfo+sfo_param_ptr); 71 | printf(" | Param: 0x%x\n",(unsigned int)param32); 72 | }else{ 73 | for(k=0x0;k<0x30;k+=0x1) 74 | param[k] = le8(sfo+sfo_param_ptr+k); 75 | printf(" | Param: %s\n", param); 76 | } 77 | } 78 | else if(sfo_param_size>=0x32){ 79 | if(sfo_type!=UTF8){ 80 | param32 = be64(sfo+sfo_param_ptr); 81 | printf(" | Param: 0x%x\n",(unsigned int)param32); 82 | }else{ 83 | for(k=0x0;k 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "tools.h" 10 | #include "types.h" 11 | #include "little_endian.h" 12 | 13 | #define SFO_0x4 0x4 14 | #define SFO_0x8 0x8 15 | #define SFO_0x12 0x12 16 | #define SFO_0x16 0x16 17 | #define SFO_0x30 0x30 18 | #define SFO_0x32 0x32 19 | 20 | #define UNKN 0x0004 21 | #define UTF8 0x0204 22 | #define INTG 0x0404 23 | 24 | void analizeSFO(u8*,u32,u32); 25 | 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /spkg_reader.c: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Grazioli Giovanni 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "tools.h" 12 | #include "little_endian.h" 13 | #include "types.h" 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 | static u8 *pkg = NULL; 22 | 23 | const u32 hdr_spkg = 0x53434500; 24 | 25 | void readSPKG(){ 26 | u32 offset=0; 27 | u32 hdr = be32(pkg); 28 | offset += sizeof(u32); 29 | u32 pkg_type = le32(pkg+offset); 30 | offset += sizeof(u32); 31 | if(hdr_spkg!=hdr) 32 | fail("[Unknown HDR] 0x%08X \nNot a PS VITA System PKG", hdr); 33 | if(pkg_type!=0x3) 34 | fail("[Unknown Type] 0x%X \nNot a PS VITA System PKG", pkg_type-0x80000000); 35 | 36 | u64 time = le64(pkg+offset); offset += sizeof(u64); //??is it the time? it's static in all pkgs 37 | u64 hdr_size = le64(pkg+offset); offset += sizeof(u64); 38 | u64 content_size = le64(pkg+offset); offset += sizeof(u64); 39 | u64 size = le64(pkg+offset); offset += sizeof(u64); 40 | 41 | printf("Unknown 0x%016lx (%lu)\n",time,time); 42 | printf("Header Size 0x%016lx (%lu Bytes)\n",hdr_size,hdr_size); 43 | printf("Content Size 0x%016lx (%lu Bytes)\n",content_size,content_size); 44 | printf("Package Size 0x%016lx (%lu Bytes)\n",size,size); 45 | 46 | offset = hdr_size; 47 | u32 unknown01 = le32(pkg+offset); offset += sizeof(u32); 48 | u32 unknown02 = le32(pkg+offset); offset += sizeof(u32); 49 | u64 unknown03 = le64(pkg+offset); offset += sizeof(u64); 50 | 51 | u64 version = le64(pkg+offset); offset += sizeof(u64); 52 | u64 unknown05 = le64(pkg+offset); offset += sizeof(u64); 53 | u64 unknown06 = le64(pkg+offset); offset += sizeof(u64); 54 | u64 unknown07 = le64(pkg+offset); offset += sizeof(u64); 55 | offset += sizeof(u64)+sizeof(u64); 56 | 57 | u64 unknown08 = le64(pkg+offset); offset += sizeof(u64); 58 | u64 unknown09 = le64(pkg+offset); offset += sizeof(u64); 59 | u64 unknown10 = le64(pkg+offset); offset += sizeof(u64); 60 | u64 unknown11 = le64(pkg+offset); offset += sizeof(u64); 61 | u64 unknown12 = le64(pkg+offset); offset += sizeof(u64); 62 | u64 unknown13 = le64(pkg+offset); offset += sizeof(u64); 63 | 64 | printf("Unknown 0x%08x (%u)\n",unknown01,unknown01); 65 | printf("Unknown 0x%08x (%u)\n",unknown02,unknown02); 66 | 67 | printf("Unknown 0x%016lx (%lu)\n",unknown03,unknown03); 68 | printf("Version 0x%016lx (%lu)\n",version,version); 69 | printf("Unknown 0x%016lx (%lu)\n",unknown05,unknown05); 70 | printf("Unknown 0x%016lx (%lu)\n",unknown06,unknown06); 71 | printf("Unknown 0x%016lx (%lu)\n",unknown07,unknown07); 72 | printf("Unknown 0x%016lx (%lu)\n",unknown08,unknown08); 73 | printf("Unknown 0x%016lx (%lu)\n",unknown09,unknown09); 74 | printf("Unknown 0x%016lx (%lu)\n",unknown10,unknown10); 75 | printf("Unknown 0x%016lx (%lu)\n",unknown11,unknown11); 76 | printf("Unknown 0x%016lx (%lu)\n",unknown12,unknown12); 77 | printf("Unknown 0x%016lx (%lu)\n",unknown13,unknown13); 78 | } 79 | 80 | int main(int argc, char *argv[]){ 81 | 82 | if (argc == 2) { 83 | pkg = mmap_file(argv[1]); 84 | readSPKG(); 85 | 86 | } else if(argc == 3) { 87 | if (strcmp(argv[1], "-d") != 0) 88 | fail("invalid option: %s", argv[1]); 89 | else 90 | set_dbg_flag(); 91 | 92 | pkg = mmap_file(argv[2]); 93 | printf("[PKG File] %s\n",argv[2]); 94 | readSPKG(); 95 | 96 | }else { 97 | fail("usage: %s vita_system_package.pkg\n" 98 | /*" -d | show more informations"*/,argv[0]); 99 | } 100 | 101 | 102 | return 0; 103 | } 104 | 105 | -------------------------------------------------------------------------------- /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 | 17 | #ifdef WIN32 18 | #include "mingw_mmap.h" 19 | #include 20 | #include 21 | #else 22 | #include 23 | #endif 24 | 25 | #include "tools.h" 26 | #include "aes.h" 27 | #include "sha1.h" 28 | 29 | int DBG=0; 30 | 31 | void set_dbg_flag(void){ 32 | DBG=1; 33 | } 34 | 35 | int dbg(void){ 36 | return DBG; 37 | } 38 | 39 | // 40 | // misc 41 | // 42 | void *mmap_file(const char *path) 43 | { 44 | int fd; 45 | struct stat st; 46 | void *ptr; 47 | 48 | fd = open(path, O_RDONLY); 49 | if(fd == -1) 50 | fail("open %s", path); 51 | if(fstat(fd, &st) != 0) 52 | fail("fstat %s", path); 53 | 54 | ptr = mmap(0, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); 55 | if(ptr==NULL) 56 | fail("mmap"); 57 | close(fd); 58 | 59 | return ptr; 60 | } 61 | 62 | void memcpy_to_file(const char *fname, u8 *ptr, u64 size) 63 | { 64 | FILE *fp; 65 | 66 | fp = fopen(fname, "wb"); 67 | fwrite(ptr, size, 1, fp); 68 | fclose(fp); 69 | } 70 | 71 | void fail(const char *a, ...) 72 | { 73 | char msg[1024]; 74 | va_list va; 75 | 76 | va_start(va, a); 77 | vsnprintf(msg, sizeof msg, a, va); 78 | fprintf(stderr, "%s\n", msg); 79 | perror("perror"); 80 | 81 | exit(1); 82 | } 83 | 84 | void dmsg(const char *a, ...){ 85 | char msg[1024]; 86 | va_list va; 87 | 88 | va_start(va, a); 89 | vsnprintf(msg, sizeof msg, a, va); 90 | if(DBG!=0) fprintf(stderr, "%s", msg); 91 | } 92 | 93 | void decompress(u8 *in, u64 in_len, u8 *out, u64 out_len) 94 | { 95 | z_stream s; 96 | int ret; 97 | 98 | memset(&s, 0, sizeof(s)); 99 | 100 | s.zalloc = Z_NULL; 101 | s.zfree = Z_NULL; 102 | s.opaque = Z_NULL; 103 | 104 | ret = inflateInit(&s); 105 | if (ret != Z_OK) 106 | fail("inflateInit returned %d", ret); 107 | 108 | s.avail_in = in_len; 109 | s.next_in = in; 110 | 111 | s.avail_out = out_len; 112 | s.next_out = out; 113 | 114 | ret = inflate(&s, Z_FINISH); 115 | if (ret != Z_OK && ret != Z_STREAM_END) 116 | fail("inflate returned %d", ret); 117 | 118 | inflateEnd(&s); 119 | } 120 | 121 | const char *id2name(u32 id, struct id2name_tbl *t, const char *unk) 122 | { 123 | while (t->name != NULL) { 124 | if (id == t->id) 125 | return t->name; 126 | t++; 127 | } 128 | return unk; 129 | } 130 | 131 | #ifdef WIN32 132 | void get_rand(u8 *bfr, u32 size) 133 | { 134 | HCRYPTPROV hProv; 135 | 136 | if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) 137 | fail("unable to open random"); 138 | 139 | if (!CryptGenRandom(hProv, size, bfr)) 140 | fail("unable to read random numbers"); 141 | 142 | CryptReleaseContext(hProv, 0); 143 | } 144 | #else 145 | void get_rand(u8 *bfr, u32 size) 146 | { 147 | FILE *fp; 148 | 149 | fp = fopen("/dev/urandom", "r"); 150 | if (fp == NULL) 151 | fail("unable to open random"); 152 | 153 | if (fread(bfr, size, 1, fp) != 1) 154 | fail("unable to read random numbers"); 155 | 156 | fclose(fp); 157 | } 158 | #endif 159 | 160 | // 161 | // ELF helpers 162 | // 163 | int elf_read_hdr(u8 *hdr, struct elf_hdr *h) 164 | { 165 | int arch64; 166 | memcpy(h->e_ident, hdr, 16); 167 | hdr += 16; 168 | 169 | arch64 = h->e_ident[4] == 2; 170 | 171 | h->e_type = be16(hdr); 172 | hdr += 2; 173 | h->e_machine = be16(hdr); 174 | hdr += 2; 175 | h->e_version = be32(hdr); 176 | hdr += 4; 177 | 178 | if (arch64) { 179 | h->e_entry = be64(hdr); 180 | h->e_phoff = be64(hdr + 8); 181 | h->e_shoff = be64(hdr + 16); 182 | hdr += 24; 183 | } else { 184 | h->e_entry = be32(hdr); 185 | h->e_phoff = be32(hdr + 4); 186 | h->e_shoff = be32(hdr + 8); 187 | hdr += 12; 188 | } 189 | 190 | h->e_flags = be32(hdr); 191 | hdr += 4; 192 | 193 | h->e_ehsize = be16(hdr); 194 | hdr += 2; 195 | h->e_phentsize = be16(hdr); 196 | hdr += 2; 197 | h->e_phnum = be16(hdr); 198 | hdr += 2; 199 | h->e_shentsize = be16(hdr); 200 | hdr += 2; 201 | h->e_shnum = be16(hdr); 202 | hdr += 2; 203 | h->e_shtrndx = be16(hdr); 204 | 205 | return arch64; 206 | } 207 | 208 | void elf_read_phdr(int arch64, u8 *phdr, struct elf_phdr *p) 209 | { 210 | if (arch64) { 211 | p->p_type = be32(phdr + 0); 212 | p->p_flags = be32(phdr + 4); 213 | p->p_off = be64(phdr + 1*8); 214 | p->p_vaddr = be64(phdr + 2*8); 215 | p->p_paddr = be64(phdr + 3*8); 216 | p->p_filesz = be64(phdr + 4*8); 217 | p->p_memsz = be64(phdr + 5*8); 218 | p->p_align = be64(phdr + 6*8); 219 | } else { 220 | p->p_type = be32(phdr + 0*4); 221 | p->p_off = be32(phdr + 1*4); 222 | p->p_vaddr = be32(phdr + 2*4); 223 | p->p_paddr = be32(phdr + 3*4); 224 | p->p_filesz = be32(phdr + 4*4); 225 | p->p_memsz = be32(phdr + 5*4); 226 | p->p_flags = be32(phdr + 6*4); 227 | p->p_align = be32(phdr + 7*4); 228 | } 229 | } 230 | 231 | void elf_read_shdr(int arch64, u8 *shdr, struct elf_shdr *s) 232 | { 233 | if (arch64) { 234 | s->sh_name = be32(shdr + 0*4); 235 | s->sh_type = be32(shdr + 1*4); 236 | s->sh_flags = be64(shdr + 2*4); 237 | s->sh_addr = be64(shdr + 2*4 + 1*8); 238 | s->sh_offset = be64(shdr + 2*4 + 2*8); 239 | s->sh_size = be64(shdr + 2*4 + 3*8); 240 | s->sh_link = be32(shdr + 2*4 + 4*8); 241 | s->sh_info = be32(shdr + 3*4 + 4*8); 242 | s->sh_addralign = be64(shdr + 4*4 + 4*8); 243 | s->sh_entsize = be64(shdr + 4*4 + 5*8); 244 | } else { 245 | s->sh_name = be32(shdr + 0*4); 246 | s->sh_type = be32(shdr + 1*4); 247 | s->sh_flags = be32(shdr + 2*4); 248 | s->sh_addr = be32(shdr + 3*4); 249 | s->sh_offset = be32(shdr + 4*4); 250 | s->sh_size = be32(shdr + 5*4); 251 | s->sh_link = be32(shdr + 6*4); 252 | s->sh_info = be32(shdr + 7*4); 253 | s->sh_addralign = be32(shdr + 8*4); 254 | s->sh_entsize = be32(shdr + 9*4); 255 | } 256 | } 257 | 258 | void elf_write_shdr(int arch64, u8 *shdr, struct elf_shdr *s) 259 | { 260 | if (arch64) { 261 | wbe32(shdr + 0*4, s->sh_name); 262 | wbe32(shdr + 1*4, s->sh_type); 263 | wbe64(shdr + 2*4, s->sh_flags); 264 | wbe64(shdr + 2*4 + 1*8, s->sh_addr); 265 | wbe64(shdr + 2*4 + 2*8, s->sh_offset); 266 | wbe64(shdr + 2*4 + 3*8, s->sh_size); 267 | wbe32(shdr + 2*4 + 4*8, s->sh_link); 268 | wbe32(shdr + 3*4 + 4*8, s->sh_info); 269 | wbe64(shdr + 4*4 + 4*8, s->sh_addralign); 270 | wbe64(shdr + 4*4 + 5*8, s->sh_entsize); 271 | } else { 272 | wbe32(shdr + 0*4, s->sh_name); 273 | wbe32(shdr + 1*4, s->sh_type); 274 | wbe32(shdr + 2*4, s->sh_flags); 275 | wbe32(shdr + 3*4, s->sh_addr); 276 | wbe32(shdr + 4*4, s->sh_offset); 277 | wbe32(shdr + 5*4, s->sh_size); 278 | wbe32(shdr + 6*4, s->sh_link); 279 | wbe32(shdr + 7*4, s->sh_info); 280 | wbe32(shdr + 8*4, s->sh_addralign); 281 | wbe32(shdr + 9*4, s->sh_entsize); 282 | } 283 | } 284 | 285 | // 286 | // crypto 287 | // 288 | void aes256cbc(u8 *key, u8 *iv_in, u8 *in, u64 len, u8 *out) 289 | { 290 | AES_KEY k; 291 | u32 i; 292 | u8 tmp[16]; 293 | u8 iv[16]; 294 | 295 | memcpy(iv, iv_in, 16); 296 | memset(&k, 0, sizeof k); 297 | AES_set_decrypt_key(key, 256, &k); 298 | 299 | while (len > 0) { 300 | memcpy(tmp, in, 16); 301 | AES_decrypt(in, out, &k); 302 | 303 | for (i = 0; i < 16; i++) 304 | out[i] ^= iv[i]; 305 | 306 | memcpy(iv, tmp, 16); 307 | 308 | out += 16; 309 | in += 16; 310 | len -= 16; 311 | 312 | } 313 | } 314 | 315 | void aes256cbc_enc(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out) 316 | { 317 | AES_KEY k; 318 | u32 i; 319 | u8 tmp[16]; 320 | 321 | memcpy(tmp, iv, 16); 322 | memset(&k, 0, sizeof k); 323 | AES_set_encrypt_key(key, 256, &k); 324 | 325 | while (len > 0) { 326 | for (i = 0; i < 16; i++) 327 | tmp[i] ^= *in++; 328 | 329 | AES_encrypt(tmp, out, &k); 330 | memcpy(tmp, out, 16); 331 | 332 | out += 16; 333 | len -= 16; 334 | } 335 | } 336 | 337 | void aes128ctr(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out) 338 | { 339 | AES_KEY k; 340 | u32 i; 341 | u8 ctr[16]; 342 | u64 tmp; 343 | 344 | memset(ctr, 0, 16); 345 | memset(&k, 0, sizeof k); 346 | 347 | AES_set_encrypt_key(key, 128, &k); 348 | 349 | for (i = 0; i < len; i++) { 350 | if ((i & 0xf) == 0) { 351 | AES_encrypt(iv, ctr, &k); 352 | 353 | // increase nonce 354 | tmp = be64(iv + 8) + 1; 355 | wbe64(iv + 8, tmp); 356 | if (tmp == 0) 357 | wbe64(iv, be64(iv) + 1); 358 | } 359 | *out++ = *in++ ^ ctr[i & 0x0f]; 360 | } 361 | } 362 | 363 | 364 | // FIXME: use a non-broken sha1.c *sigh* 365 | static void sha1_fixup(struct SHA1Context *ctx, u8 *digest) 366 | { 367 | u32 i; 368 | 369 | for(i = 0; i < 5; i++) { 370 | *digest++ = ctx->Message_Digest[i] >> 24 & 0xff; 371 | *digest++ = ctx->Message_Digest[i] >> 16 & 0xff; 372 | *digest++ = ctx->Message_Digest[i] >> 8 & 0xff; 373 | *digest++ = ctx->Message_Digest[i] & 0xff; 374 | } 375 | } 376 | 377 | void sha1(u8 *data, u32 len, u8 *digest) 378 | { 379 | struct SHA1Context ctx; 380 | 381 | SHA1Reset(&ctx); 382 | SHA1Input(&ctx, data, len); 383 | SHA1Result(&ctx); 384 | 385 | sha1_fixup(&ctx, digest); 386 | } 387 | 388 | void sha1_hmac(u8 *key, u8 *data, u32 len, u8 *digest) 389 | { 390 | struct SHA1Context ctx; 391 | u32 i; 392 | u8 ipad[0x40]; 393 | u8 tmp[0x40 + 0x14]; // opad + hash(ipad + message) 394 | 395 | SHA1Reset(&ctx); 396 | 397 | for (i = 0; i < sizeof ipad; i++) { 398 | tmp[i] = key[i] ^ 0x5c; // opad 399 | ipad[i] = key[i] ^ 0x36; 400 | } 401 | 402 | SHA1Input(&ctx, ipad, sizeof ipad); 403 | SHA1Input(&ctx, data, len); 404 | SHA1Result(&ctx); 405 | 406 | sha1_fixup(&ctx, tmp + 0x40); 407 | 408 | sha1(tmp, sizeof tmp, digest); 409 | 410 | } 411 | 412 | static struct id2name_tbl t_key2file[] = { 413 | {KEY_LV0, "lv0"}, 414 | {KEY_LV1, "lv1"}, 415 | {KEY_LV2, "lv2"}, 416 | {KEY_APP, "app"}, 417 | {KEY_ISO, "iso"}, 418 | {KEY_LDR, "ldr"}, 419 | {KEY_PKG, "pkg"}, 420 | {KEY_SPP, "spp"}, 421 | {0, NULL} 422 | }; 423 | 424 | static int key_build_path(char *ptr) 425 | { 426 | char *home = NULL; 427 | char *dir = NULL; 428 | 429 | memset(ptr, 0, 256); 430 | 431 | dir = getenv("VITA_KEYS"); 432 | if (dir != NULL) { 433 | strncpy(ptr, dir, 256); 434 | return 0; 435 | } 436 | 437 | #ifdef WIN32 438 | home = getenv("USERPROFILE"); 439 | #else 440 | home = getenv("HOME"); 441 | #endif 442 | if (home == NULL) { 443 | snprintf (ptr, 256, "vitakeys"); 444 | } else { 445 | #ifdef WIN32 446 | snprintf(ptr, 256, "%s\\vitakeys\\", home); 447 | #else 448 | snprintf(ptr, 256, "%s/.vita/", home); 449 | #endif 450 | } 451 | 452 | return 0; 453 | } 454 | 455 | static int key_read(const char *path, u32 len, u8 *dst) 456 | { 457 | FILE *fp = NULL; 458 | u32 read; 459 | int ret = -1; 460 | 461 | fp = fopen(path, "rb"); 462 | if (fp == NULL) 463 | goto fail; 464 | 465 | read = fread(dst, len, 1, fp); 466 | 467 | if (read != 1) 468 | goto fail; 469 | 470 | ret = 0; 471 | 472 | fail: 473 | if (fp != NULL) 474 | fclose(fp); 475 | 476 | return ret; 477 | } 478 | 479 | struct keylist *keys_get(enum sce_key type) 480 | { 481 | const char *name = NULL; 482 | char base[256]; 483 | char path[256]; 484 | void *tmp = NULL; 485 | char *id; 486 | DIR *dp; 487 | struct dirent *dent; 488 | struct keylist *klist; 489 | u8 bfr[4]; 490 | 491 | klist = malloc(sizeof *klist); 492 | if (klist == NULL) 493 | goto fail; 494 | 495 | memset(klist, 0, sizeof *klist); 496 | 497 | name = id2name(type, t_key2file, NULL); 498 | if (name == NULL) 499 | goto fail; 500 | 501 | if (key_build_path(base) < 0) 502 | goto fail; 503 | 504 | dp = opendir(base); 505 | if (dp == NULL) 506 | goto fail; 507 | 508 | while ((dent = readdir(dp)) != NULL) { 509 | if (strncmp(dent->d_name, name, strlen(name)) == 0 && 510 | strstr(dent->d_name, "key") != NULL) { 511 | tmp = realloc(klist->keys, (klist->n + 1) * sizeof(struct key)); 512 | if (tmp == NULL) 513 | goto fail; 514 | 515 | id = strrchr(dent->d_name, '-'); 516 | if (id != NULL) 517 | id++; 518 | 519 | klist->keys = tmp; 520 | memset(&klist->keys[klist->n], 0, sizeof(struct key)); 521 | 522 | snprintf(path, sizeof path, "%s/%s-key-%s", base, name, id); 523 | key_read(path, 32, klist->keys[klist->n].key); 524 | 525 | snprintf(path, sizeof path, "%s/%s-iv-%s", base, name, id); 526 | key_read(path, 16, klist->keys[klist->n].iv); 527 | 528 | klist->keys[klist->n].pub_avail = -1; 529 | klist->keys[klist->n].priv_avail = -1; 530 | 531 | snprintf(path, sizeof path, "%s/%s-pub-%s", base, name, id); 532 | if (key_read(path, 40, klist->keys[klist->n].pub) == 0) { 533 | snprintf(path, sizeof path, "%s/%s-ctype-%s", base, name, id); 534 | key_read(path, 4, bfr); 535 | 536 | klist->keys[klist->n].pub_avail = 1; 537 | klist->keys[klist->n].ctype = be32(bfr); 538 | } 539 | 540 | snprintf(path, sizeof path, "%s/%s-priv-%s", base, name, id); 541 | if (key_read(path, 21, klist->keys[klist->n].priv) == 0) 542 | klist->keys[klist->n].priv_avail = 1; 543 | 544 | 545 | klist->n++; 546 | } 547 | } 548 | 549 | return klist; 550 | 551 | fail: 552 | if (klist != NULL) { 553 | if (klist->keys != NULL) 554 | free(klist->keys); 555 | free(klist); 556 | } 557 | klist = NULL; 558 | 559 | return NULL; 560 | } 561 | 562 | int key_get_simple(const char *name, u8 *bfr, u32 len) 563 | { 564 | char base[256]; 565 | char path[256]; 566 | 567 | if (key_build_path(base) < 0) 568 | return -1; 569 | 570 | snprintf(path, sizeof path, "%s/%s", base, name); 571 | if (key_read(path, len, bfr) < 0) 572 | return -1; 573 | 574 | return 0; 575 | } 576 | 577 | int key_get(enum sce_key type, const char *suffix, struct key *k) 578 | { 579 | const char *name; 580 | char base[256]; 581 | char path[256]; 582 | u8 tmp[4]; 583 | 584 | if (key_build_path(base) < 0) 585 | return -1; 586 | 587 | name = id2name(type, t_key2file, NULL); 588 | if (name == NULL) 589 | return -1; 590 | 591 | snprintf(path, sizeof path, "%s/%s-key-%s", base, name, suffix); 592 | if (key_read(path, 32, k->key) < 0) 593 | return -1; 594 | 595 | snprintf(path, sizeof path, "%s/%s-iv-%s", base, name, suffix); 596 | if (key_read(path, 16, k->iv) < 0) 597 | return -1; 598 | 599 | k->pub_avail = k->priv_avail = 1; 600 | 601 | snprintf(path, sizeof path, "%s/%s-ctype-%s", base, name, suffix); 602 | if (key_read(path, 4, tmp) < 0) { 603 | k->pub_avail = k->priv_avail = -1; 604 | return 0; 605 | } 606 | 607 | k->ctype = be32(tmp); 608 | 609 | snprintf(path, sizeof path, "%s/%s-pub-%s", base, name, suffix); 610 | if (key_read(path, 40, k->pub) < 0) 611 | k->pub_avail = -1; 612 | 613 | snprintf(path, sizeof path, "%s/%s-priv-%s", base, name, suffix); 614 | if (key_read(path, 21, k->priv) < 0) 615 | k->priv_avail = -1; 616 | 617 | return 0; 618 | } 619 | 620 | static void memcpy_inv(u8 *dst, u8 *src, u32 len) 621 | { 622 | u32 j; 623 | 624 | for (j = 0; j < len; j++) 625 | dst[j] = ~src[j]; 626 | } 627 | 628 | int ecdsa_get_params(u32 type, u8 *p, u8 *a, u8 *b, u8 *N, u8 *Gx, u8 *Gy) 629 | { 630 | static u8 tbl[64 * 121]; 631 | char path[256]; 632 | u32 offset; 633 | 634 | if (type >= 64) 635 | return -1; 636 | 637 | if (key_build_path(path) < 0) 638 | return -1; 639 | 640 | strncat(path, "/curves", sizeof path); 641 | 642 | if (key_read(path, sizeof tbl, tbl) < 0) 643 | return -1; 644 | 645 | offset = type * 121; 646 | 647 | memcpy_inv(p, tbl + offset + 0, 20); 648 | memcpy_inv(a, tbl + offset + 20, 20); 649 | memcpy_inv(b, tbl + offset + 40, 20); 650 | memcpy_inv(N, tbl + offset + 60, 21); 651 | memcpy_inv(Gx, tbl + offset + 81, 20); 652 | memcpy_inv(Gy, tbl + offset + 101, 20); 653 | 654 | return 0; 655 | } 656 | 657 | int sce_decrypt_header(u8 *ptr, struct keylist *klist) 658 | { 659 | u32 meta_offset; 660 | u32 meta_len; 661 | u64 header_len; 662 | u32 i, j; 663 | u8 tmp[0x40]; 664 | int success = 0; 665 | 666 | 667 | meta_offset = be32(ptr + 0x0c); 668 | header_len = be64(ptr + 0x10); 669 | 670 | for (i = 0; i < klist->n; i++) { 671 | aes256cbc(klist->keys[i].key, 672 | klist->keys[i].iv, 673 | ptr + meta_offset + 0x20, 674 | 0x40, 675 | tmp); 676 | 677 | success = 1; 678 | for (j = 0x10; j < (0x10 + 0x10); j++) 679 | if (tmp[j] != 0) 680 | success = 0; 681 | 682 | for (j = 0x30; j < (0x30 + 0x10); j++) 683 | if (tmp[j] != 0) 684 | success = 0; 685 | 686 | if (success == 1) { 687 | memcpy(ptr + meta_offset + 0x20, tmp, 0x40); 688 | break; 689 | } 690 | } 691 | 692 | if (success != 1) 693 | return -1; 694 | 695 | memcpy(tmp, ptr + meta_offset + 0x40, 0x10); 696 | aes128ctr(ptr + meta_offset + 0x20, 697 | tmp, 698 | ptr + meta_offset + 0x60, 699 | 0x20, 700 | ptr + meta_offset + 0x60); 701 | 702 | meta_len = header_len - meta_offset; 703 | 704 | aes128ctr(ptr + meta_offset + 0x20, 705 | tmp, 706 | ptr + meta_offset + 0x80, 707 | meta_len - 0x80, 708 | ptr + meta_offset + 0x80); 709 | 710 | return i; 711 | } 712 | 713 | int sce_encrypt_header(u8 *ptr, struct key *k) 714 | { 715 | u32 meta_offset; 716 | u32 meta_len; 717 | u64 header_len; 718 | u8 iv[16]; 719 | 720 | meta_offset = be32(ptr + 0x0c); 721 | header_len = be64(ptr + 0x10); 722 | meta_len = header_len - meta_offset; 723 | 724 | memcpy(iv, ptr + meta_offset + 0x40, 0x10); 725 | aes128ctr(ptr + meta_offset + 0x20, 726 | iv, 727 | ptr + meta_offset + 0x60, 728 | meta_len - 0x60, 729 | ptr + meta_offset + 0x60); 730 | 731 | aes256cbc_enc(k->key, k->iv, 732 | ptr + meta_offset + 0x20, 733 | 0x40, 734 | ptr + meta_offset + 0x20); 735 | 736 | 737 | return 0; 738 | } 739 | 740 | int sce_decrypt_data(u8 *ptr) 741 | { 742 | u64 meta_offset; 743 | u32 meta_len; 744 | u32 meta_n_hdr; 745 | u64 header_len; 746 | u32 i; 747 | 748 | u64 offset; 749 | u64 size; 750 | u32 keyid; 751 | u32 ivid; 752 | u8 *tmp; 753 | 754 | u8 iv[16]; 755 | 756 | meta_offset = be32(ptr + 0x0c); 757 | header_len = be64(ptr + 0x10); 758 | meta_len = header_len - meta_offset; 759 | meta_n_hdr = be32(ptr + meta_offset + 0x60 + 0xc); 760 | 761 | for (i = 0; i < meta_n_hdr; i++) { 762 | tmp = ptr + meta_offset + 0x80 + 0x30*i; 763 | offset = be64(tmp); 764 | size = be64(tmp + 8); 765 | keyid = be32(tmp + 0x24); 766 | ivid = be32(tmp + 0x28); 767 | 768 | if (keyid == 0xffffffff || ivid == 0xffffffff) 769 | continue; 770 | 771 | memcpy(iv, ptr + meta_offset + 0x80 + 0x30 * meta_n_hdr + ivid * 0x10, 0x10); 772 | aes128ctr(ptr + meta_offset + 0x80 + 0x30 * meta_n_hdr + keyid * 0x10, 773 | iv, 774 | ptr + offset, 775 | size, 776 | ptr + offset); 777 | } 778 | 779 | return 0; 780 | } 781 | 782 | int sce_encrypt_data(u8 *ptr) 783 | { 784 | return sce_decrypt_data(ptr); 785 | } 786 | -------------------------------------------------------------------------------- /tools.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Copyright 2007,2008,2010 Segher Boessenkool 3 | // Licensed under the terms of the GNU GPL, version 2 4 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 5 | 6 | #ifndef TOOLS_H__ 7 | #define TOOLS_H__ 1 8 | #include 9 | 10 | #include "types.h" 11 | 12 | enum sce_key { 13 | KEY_LV0 = 0, 14 | KEY_LV1, 15 | KEY_LV2, 16 | KEY_APP, 17 | KEY_ISO, 18 | KEY_LDR, 19 | KEY_PKG, 20 | KEY_SPP 21 | }; 22 | 23 | void set_dbg_flag(void); 24 | int dbg(void); 25 | 26 | void *mmap_file(const char *path); 27 | void memcpy_to_file(const char *fname, u8 *ptr, u64 size); 28 | const char *id2name(u32 id, struct id2name_tbl *t, const char *unk); 29 | void fail(const char *fmt, ...) __attribute__((noreturn)); 30 | void dmsg(const char *a, ...); 31 | void decompress(u8 *in, u64 in_len, u8 *out, u64 out_len); 32 | void get_rand(u8 *bfr, u32 size); 33 | 34 | int elf_read_hdr(u8 *hdr, struct elf_hdr *h); 35 | void elf_read_phdr(int arch64, u8 *phdr, struct elf_phdr *p); 36 | void elf_read_shdr(int arch64, u8 *shdr, struct elf_shdr *s); 37 | void elf_write_shdr(int arch64, u8 *shdr, struct elf_shdr *s); 38 | 39 | void aes256cbc(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out); 40 | void aes256cbc_enc(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out); 41 | void aes128ctr(u8 *key, u8 *iv, u8 *in, u64 len, u8 *out); 42 | 43 | void sha1(u8 *data, u32 len, u8 *digest); 44 | void sha1_hmac(u8 *key, u8 *data, u32 len, u8 *digest); 45 | 46 | int key_get(enum sce_key type, const char *suffix, struct key *k); 47 | int key_get_simple(const char *name, u8 *bfr, u32 len); 48 | struct keylist *keys_get(enum sce_key type); 49 | 50 | int sce_decrypt_header(u8 *ptr, struct keylist *klist); 51 | int sce_encrypt_header(u8 *ptr, struct key *k); 52 | int sce_decrypt_data(u8 *ptr); 53 | int sce_encrypt_data(u8 *ptr); 54 | 55 | int ecdsa_get_params(u32 type, u8 *p, u8 *a, u8 *b, u8 *N, u8 *Gx, u8 *Gy); 56 | int ecdsa_set_curve(u32 type); 57 | void ecdsa_set_pub(u8 *Q); 58 | void ecdsa_set_priv(u8 *k); 59 | int ecdsa_verify(u8 *hash, u8 *R, u8 *S); 60 | void ecdsa_sign(u8 *hash, u8 *R, u8 *S); 61 | 62 | void bn_copy(u8 *d, u8 *a, u32 n); 63 | int bn_compare(u8 *a, u8 *b, u32 n); 64 | void bn_reduce(u8 *d, u8 *N, u32 n); 65 | void bn_add(u8 *d, u8 *a, u8 *b, u8 *N, u32 n); 66 | void bn_sub(u8 *d, u8 *a, u8 *b, u8 *N, u32 n); 67 | void bn_to_mon(u8 *d, u8 *N, u32 n); 68 | void bn_from_mon(u8 *d, u8 *N, u32 n); 69 | void bn_mon_mul(u8 *d, u8 *a, u8 *b, u8 *N, u32 n); 70 | void bn_mon_inv(u8 *d, u8 *a, u8 *N, u32 n); 71 | 72 | #define round_up(x,n) (-(-(x) & -(n))) 73 | 74 | #define array_size(x) (sizeof(x) / sizeof(*(x))) 75 | #endif 76 | -------------------------------------------------------------------------------- /types.h: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | #ifndef TYPES_H__ 5 | #define TYPES_H__ 6 | #include 7 | 8 | typedef uint64_t u64; 9 | typedef uint32_t u32; 10 | typedef uint16_t u16; 11 | typedef uint8_t u8; 12 | 13 | struct elf_phdr { 14 | u32 p_type; 15 | u64 p_off; 16 | u64 p_vaddr; 17 | u64 p_paddr; 18 | u64 p_filesz; 19 | u64 p_memsz; 20 | u32 p_flags; 21 | u64 p_align; 22 | 23 | void *ptr; 24 | }; 25 | 26 | struct elf_shdr { 27 | u32 sh_name; 28 | u32 sh_type; 29 | u32 sh_flags; 30 | u64 sh_addr; 31 | u64 sh_offset; 32 | u32 sh_size; 33 | u32 sh_link; 34 | u32 sh_info; 35 | u32 sh_addralign; 36 | u32 sh_entsize; 37 | }; 38 | 39 | #define ET_NONE 0 40 | #define ET_REL 1 41 | #define ET_EXEC 2 42 | #define ET_DYN 3 43 | #define ET_CORE 4 44 | #define ET_LOOS 0xfe00 45 | #define ET_HIOS 0xfeff 46 | #define ET_LOPROC 0xff00 47 | #define ET_HIPROC 0xffff 48 | struct elf_hdr { 49 | char e_ident[16]; 50 | u16 e_type; 51 | u16 e_machine; 52 | u32 e_version; 53 | u64 e_entry; 54 | u64 e_phoff; 55 | u64 e_shoff; 56 | u32 e_flags; 57 | u16 e_ehsize; 58 | u16 e_phentsize; 59 | u16 e_phnum; 60 | u16 e_shentsize; 61 | u16 e_shnum; 62 | u16 e_shtrndx; 63 | }; 64 | 65 | 66 | struct id2name_tbl { 67 | u32 id; 68 | const char *name; 69 | }; 70 | 71 | struct key { 72 | u8 key[32]; 73 | u8 iv[16]; 74 | 75 | int pub_avail; 76 | int priv_avail; 77 | u8 pub[40]; 78 | u8 priv[21]; 79 | u32 ctype; 80 | }; 81 | 82 | struct keylist { 83 | u32 n; 84 | struct key *keys; 85 | }; 86 | 87 | static inline u8 be8(u8 *p) 88 | { 89 | return *p; 90 | } 91 | 92 | static inline u16 be16(u8 *p) 93 | { 94 | u16 a; 95 | 96 | a = p[0] << 8; 97 | a |= p[1]; 98 | 99 | return a; 100 | } 101 | 102 | static inline u32 be32(u8 *p) 103 | { 104 | u32 a; 105 | 106 | a = p[0] << 24; 107 | a |= p[1] << 16; 108 | a |= p[2] << 8; 109 | a |= p[3] << 0; 110 | 111 | return a; 112 | } 113 | 114 | static inline u64 be64(u8 *p) 115 | { 116 | u32 a, b; 117 | 118 | a = be32(p); 119 | b = be32(p + 4); 120 | 121 | return ((u64)a<<32) | b; 122 | } 123 | 124 | static inline void wbe16(u8 *p, u16 v) 125 | { 126 | p[0] = v >> 8; 127 | p[1] = v; 128 | } 129 | 130 | static inline void wbe32(u8 *p, u32 v) 131 | { 132 | p[0] = v >> 24; 133 | p[1] = v >> 16; 134 | p[2] = v >> 8; 135 | p[3] = v; 136 | } 137 | 138 | static inline void wbe64(u8 *p, u64 v) 139 | { 140 | wbe32(p + 4, v); 141 | v >>= 32; 142 | wbe32(p, v); 143 | } 144 | 145 | 146 | #endif 147 | -------------------------------------------------------------------------------- /ungpkg.c: -------------------------------------------------------------------------------- 1 | // Copyright 2010-2011 Sven Peter 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | // Thanks to Mathieulh for his C# retail unpacker 6 | // (http://twitter.com/#!/Mathieulh/status/23070344881381376) 7 | // Thanks to Matt_P for his python debug unpacker 8 | // (https://github.com/HACKERCHANNEL/PS3Py/blob/master/pkg.py) 9 | // Thanks to deroad for his modification to port this on the vita 10 | 11 | 12 | #include "tools.h" 13 | #include "types.h" 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 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 | 29 | static u8 *pkg = NULL; 30 | static u64 size; 31 | static u64 offset; 32 | 33 | 34 | 35 | static void decrypt_retail_pkg(void) 36 | { 37 | u8 key[0x10]; 38 | u8 iv[0x10]; 39 | 40 | if (be16(pkg + 0x06) != 2) 41 | fail("invalid pkg type: %x", be16(pkg + 0x06)); 42 | 43 | u32 type = be32(pkg + 0xE4) & 0xFF; 44 | if(type == 1) { 45 | if (key_get_simple("gpkg-key-psp", key, 0x10) < 0) 46 | fail("failed to load the psp package key."); 47 | } else if(type == 2) { 48 | if (key_get_simple("gpkg-key-2", key, 0x10) < 0) 49 | fail("failed to load the package key."); 50 | } else if(type == 3) { 51 | if (key_get_simple("gpkg-key-3", key, 0x10) < 0) 52 | fail("failed to load the package key."); 53 | } else if(type == 4) { 54 | if (key_get_simple("gpkg-key-4", key, 0x10) < 0) 55 | fail("failed to load the package key."); 56 | } else { 57 | printf("Key Type: %d\n", type); 58 | fail("failed to find the package key."); 59 | } 60 | 61 | memcpy(iv, pkg + 0x70, 0x10); 62 | aes128ctr(key, iv, pkg + offset, size, pkg + offset); 63 | } 64 | 65 | static void decrypt_debug_pkg(void) 66 | { 67 | u8 key[0x40]; 68 | u8 bfr[0x1c]; 69 | u64 i; 70 | 71 | memset(key, 0, sizeof key); 72 | memcpy(key, pkg + 0x60, 8); 73 | memcpy(key + 0x08, pkg + 0x60, 8); 74 | memcpy(key + 0x10, pkg + 0x60 + 0x08, 8); 75 | memcpy(key + 0x18, pkg + 0x60 + 0x08, 8); 76 | 77 | sha1(key, sizeof key, bfr); 78 | 79 | for (i = 0; i < size; i++) { 80 | if (i != 0 && (i % 16) == 0) { 81 | wbe64(key + 0x38, be64(key + 0x38) + 1); 82 | sha1(key, sizeof key, bfr); 83 | } 84 | pkg[offset + i] ^= bfr[i & 0xf]; 85 | } 86 | } 87 | 88 | static void unpack_pkg(void) 89 | { 90 | 91 | char contentid[0x30]; 92 | u8 k_licensee[0x10]; 93 | 94 | u32 j; 95 | for(j=0x0;j<0x30;j++) 96 | contentid[j] = be8(pkg+0x30+j); 97 | for(j=0x0;j<0x10;j++) 98 | k_licensee[j] = be8(pkg+0x70+j); 99 | 100 | printf("Content ID %s\n",contentid); 101 | printf("K Licensee "); 102 | for(j=0x0;j<0x10;j++) 103 | printf("%x",k_licensee[j]); 104 | printf("\n"); 105 | 106 | u64 i; 107 | u64 n_files; 108 | u32 fname_len; 109 | u32 fname_off; 110 | u64 file_offset; 111 | u32 flags; 112 | char fname[256]; 113 | u8 *tmp; 114 | 115 | n_files = be32(pkg + 0x14); 116 | for (i = 0; i < n_files; i++) { 117 | tmp = pkg + offset + i*0x20; 118 | 119 | fname_off = be32(tmp) + offset; 120 | fname_len = be32(tmp + 0x04); 121 | file_offset = be64(tmp + 0x08) + offset; 122 | size = be64(tmp + 0x10); 123 | flags = be32(tmp + 0x18); 124 | 125 | if (fname_len >= sizeof fname) 126 | fail("filename too long: %s", pkg + fname_off); 127 | 128 | memset(fname, 0, sizeof fname); 129 | strncpy(fname, (char *)(pkg + fname_off), fname_len); 130 | 131 | flags &= 0xff; 132 | if (flags == 4 || flags == 18) { 133 | printf(" %02x : %s\n", flags, fname); 134 | MKDIR(fname, 0777); 135 | } else if (flags == 0 || flags == 1 || flags == 3 || flags == 14 || 136 | flags == 15 || flags == 16 || flags == 17 || flags == 19 || 137 | flags == 21 || flags == 22) { 138 | printf(" %02x : %s (%u bytes)\n", flags, fname, size); 139 | memcpy_to_file(fname, pkg + file_offset, size); 140 | } else 141 | fail("unknown flag %d for %s", flags, pkg + fname_off); 142 | } 143 | } 144 | 145 | int main(int argc, char *argv[]) 146 | { 147 | char *dir; 148 | 149 | int beta=0; 150 | 151 | if (argc != 2 && argc != 3) 152 | fail("usage: ungpkg filename.pkg\n" 153 | " This is just for PS Vita Game Packages. not PS Vita System Packages.\n"); 154 | 155 | if (argc == 2) { 156 | pkg = mmap_file(argv[1]); 157 | dir = malloc(0x31); 158 | memset(dir, 0, 0x31); 159 | memset(dir, 0, 0x30); 160 | memcpy(dir, pkg + 0x30, 0x30); 161 | } else { 162 | pkg = mmap_file(argv[2]); 163 | dir = malloc(0x31); 164 | memset(dir, 0, 0x31); 165 | memset(dir, 0, 0x30); 166 | memcpy(dir, pkg + 0x30, 0x30); 167 | } 168 | 169 | MKDIR(dir, 0777); 170 | 171 | if (chdir(dir) != 0) 172 | fail("chdir(%s)", dir); 173 | 174 | offset = be64(pkg + 0x20); 175 | size = be64(pkg + 0x28); 176 | 177 | if(be16(pkg + 0x04) & 0x8000) 178 | decrypt_retail_pkg(); 179 | else 180 | decrypt_debug_pkg(); 181 | 182 | unpack_pkg(); 183 | 184 | return 0; 185 | } 186 | -------------------------------------------------------------------------------- /unpkg.c: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Grazioli Giovanni 2 | // Licensed under the terms of the GNU GPL, version 2 3 | // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "tools.h" 12 | #include "types.h" 13 | #include "little_endian.h" 14 | 15 | static int read_only = 0; 16 | 17 | static u8 *pkg = NULL; 18 | const u32 PKG_HDR = 0x53434500; 19 | 20 | void Extract(const char* name, u32 offset, u32 size){ 21 | printf("Extracting %s..\n",name); 22 | memcpy_to_file(name,pkg+offset,size); 23 | } 24 | 25 | void unpkg(){ 26 | if(read_only!=0) 27 | printf("Read Only Mode!\n"); 28 | printf("Reading...\n"); 29 | 30 | u32 HDR = be32(pkg); 31 | u16 type = le16(pkg+0x0a); 32 | u32 data1_size = le32(pkg+0x10)-0x30; 33 | u32 data2_size = le32(pkg+0x18)-0x70; 34 | u32 pkg_size = le32(pkg+0x20); 35 | u32 data2_ver = le32(pkg+(data1_size+0x10)); 36 | if(HDR!=PKG_HDR) 37 | fail("\nERROR! Not a PlayStation Vita Update File (%08x)",HDR); 38 | 39 | if(type!=3) 40 | fail("Not a PSVITA System Package (%X)",type); 41 | dmsg("[PKG][SIZE] %10u Bytes\n",pkg_size); 42 | printf("Found: data1 %10u Bytes\n",data1_size); 43 | 44 | if(read_only==0) 45 | Extract("data1", 0x30 , data1_size); 46 | printf("Found: data2 %10u Bytes\n",data2_size); 47 | dmsg("[DATA2][VER] %08x\n",data2_ver); 48 | if(read_only==0) 49 | Extract("data2", 0x60 + data1_size + 0x50, data2_size); 50 | 51 | 52 | 53 | 54 | printf("Done!\n"); 55 | } 56 | 57 | 58 | int main(int argc, char *argv[]){ 59 | 60 | if (argc == 2) { 61 | pkg = mmap_file(argv[1]); 62 | unpkg(); 63 | 64 | } else if(argc == 3) { 65 | if (strcmp(argv[1], "-d") != 0) 66 | if(strcmp(argv[1], "-r") !=0) 67 | if(strcmp(argv[1], "-rd") !=0) 68 | fail("invalid option: %s", argv[1]); 69 | 70 | if(strcmp(argv[1], "-d")==0) 71 | set_dbg_flag(); 72 | 73 | if(strcmp(argv[1], "-r")==0) 74 | read_only=1; 75 | 76 | if(strcmp(argv[1], "-rd")==0){ 77 | read_only=1; 78 | set_dbg_flag(); 79 | } 80 | 81 | pkg = mmap_file(argv[2]); 82 | dmsg("[PKG File] %s\n",argv[2]); 83 | unpkg(); 84 | 85 | }else { 86 | fail("usage: %s ps_vita_system_package.pkg\n" 87 | " -d | debug messages\n" 88 | " -r | read only mode\n" 89 | " -rd | read only + debug messages" 90 | ,argv[0]); 91 | } 92 | 93 | 94 | return 0; 95 | } 96 | --------------------------------------------------------------------------------