├── .gitattributes ├── LICENSE ├── README.md ├── bin ├── Debug │ └── ps3encdec.exe └── Release │ └── ps3encdec.exe ├── ps3encdec.sln ├── ps3encdec.vcxproj ├── ps3encdec.vcxproj.filters ├── ps3encdec.vcxproj.user └── src ├── Makefile ├── aes.c ├── aes.h ├── aes_xts.c ├── aes_xts.h ├── getopt.c ├── getopt.h ├── keys.h ├── kgen.c ├── kgen.h ├── main.c ├── types.h ├── util.c └── util.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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) 2018 Sorvigolova 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 | {signature of Ty Coon}, 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EncDec Device emulator 2 | ======================== 3 | 4 | Tool to decrypt / encrypt PS3 Storage Data 5 | 6 | ==> Usage <== 7 | 8 | ps3encdec.exe [-h] [-e] [-v] [-p] [-a] [-s START_SECTOR] [-n NUM_SECTORS] 9 | 10 | [eid_root_key_file] [sector_file] 11 | 12 | positional arguments: 13 | 14 | eid_root_key_file 15 | 16 | sector_file 17 | 18 | out_file 19 | 20 | optional arguments: 21 | 22 | -h, --help show this help message and exit 23 | 24 | -e, --encrypt encrypt data instead of decrypt 25 | 26 | -v, --vflash vflash/eflash region 27 | 28 | -p, --phat phat console 29 | 30 | -a, --arcade arcade console 31 | 32 | -s START_SECTOR, --start-sector START_SECTOR 33 | 34 | sector start index, used for crypto only 35 | 36 | -n NUM_SECTORS, --num-sectors NUM_SECTORS 37 | 38 | sector count 39 | 40 | 41 | ==> Command examples <== 42 | 43 | ps3encdec -p -n 0x200 eid_root_key hdd.bin hdd.dec // to decrypt 0x200 sectors of PS3 FAT hdd backup 44 | 45 | ps3encdec -v -s 8 eid_root_key vflash.bin vflash.dec // to decrypt the whole vflash backup of PS3-Slim 46 | 47 | ps3encdec -p -v -s 0x7800 eid_root_key eflash.bin // to decrypt the whole eflash to out.bin (FAT PS3) 48 | 49 | ps3encdec -e -p -v -s 0x7800 eid_root_key out.bin eflash // to encrypt decrypted eflash to eflash (FAT PS3) 50 | 51 | ==> Credits <== 52 | 53 | Flat_z - The original Author of the Python solution. 54 | 55 | ZecoXao - Tests, benchmarks, suggestions. Many Thanks to You. -------------------------------------------------------------------------------- /bin/Debug/ps3encdec.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sorvigolova/ps3encdec/16e9b392fd8619aec21272378988d5a3b46549d6/bin/Debug/ps3encdec.exe -------------------------------------------------------------------------------- /bin/Release/ps3encdec.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sorvigolova/ps3encdec/16e9b392fd8619aec21272378988d5a3b46549d6/bin/Release/ps3encdec.exe -------------------------------------------------------------------------------- /ps3encdec.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.40629.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ps3encdec", "ps3encdec.vcxproj", "{85F9C8A2-99A9-4BA6-8726-0AA261ABA9B5}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {85F9C8A2-99A9-4BA6-8726-0AA261ABA9B5}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {85F9C8A2-99A9-4BA6-8726-0AA261ABA9B5}.Debug|Win32.Build.0 = Debug|Win32 16 | {85F9C8A2-99A9-4BA6-8726-0AA261ABA9B5}.Release|Win32.ActiveCfg = Release|Win32 17 | {85F9C8A2-99A9-4BA6-8726-0AA261ABA9B5}.Release|Win32.Build.0 = Release|Win32 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /ps3encdec.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {85F9C8A2-99A9-4BA6-8726-0AA261ABA9B5} 15 | Win32Proj 16 | 17 | 18 | 19 | Application 20 | true 21 | v120_xp 22 | 23 | 24 | Application 25 | false 26 | v120_xp 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | false 40 | 41 | 42 | false 43 | 44 | 45 | 46 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) 47 | MultiThreadedDebug 48 | Level3 49 | ProgramDatabase 50 | Disabled 51 | 52 | 53 | MachineX86 54 | true 55 | Console 56 | /SUBSYSTEM:CONSOLE,5.01 %(AdditionalOptions) 57 | 58 | 59 | 60 | 61 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 62 | MultiThreaded 63 | Level3 64 | ProgramDatabase 65 | 66 | 67 | MachineX86 68 | true 69 | Console 70 | true 71 | true 72 | /SUBSYSTEM:CONSOLE,5.01 %(AdditionalOptions) 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /ps3encdec.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | Source Files 32 | 33 | 34 | Source Files 35 | 36 | 37 | 38 | 39 | Header Files 40 | 41 | 42 | Header Files 43 | 44 | 45 | Header Files 46 | 47 | 48 | Header Files 49 | 50 | 51 | Header Files 52 | 53 | 54 | Header Files 55 | 56 | 57 | Header Files 58 | 59 | 60 | -------------------------------------------------------------------------------- /ps3encdec.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS = 3 | 4 | ifeq ($(DEBUG), 1) 5 | CFLAGS+=-g -O0 6 | else 7 | CFLAGS+=-O2 8 | endif 9 | 10 | OUT=ps3encdec 11 | 12 | OBJ=main.o aes.o aes_xts.o kgen.o util.o 13 | 14 | all: $(OBJ) 15 | $(CC) $(CFLAGS) -o $(OUT) $(OBJ) 16 | 17 | clean: 18 | rm -f *.o $(OUT) *~ -------------------------------------------------------------------------------- /src/aes.c: -------------------------------------------------------------------------------- 1 | /* 2 | * FIPS-197 compliant AES implementation 3 | * 4 | * Copyright (C) 2006-2010, Brainspark B.V. 5 | * 6 | * This file is part of PolarSSL (http://www.polarssl.org) 7 | * Lead Maintainer: Paul Bakker 8 | * 9 | * All rights reserved. 10 | * 11 | * This program is free software; you can redistribute it and/or modify 12 | * it under the terms of the GNU General Public License as published by 13 | * the Free Software Foundation; either version 2 of the License, or 14 | * (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License along 22 | * with this program; if not, write to the Free Software Foundation, Inc., 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 24 | */ 25 | /* 26 | * The AES block cipher was designed by Vincent Rijmen and Joan Daemen. 27 | * 28 | * http://csrc.nist.gov/encryption/aes/rijndael/Rijndael.pdf 29 | * http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf 30 | */ 31 | 32 | #include "aes.h" 33 | 34 | /* 35 | * 32-bit integer manipulation macros (little endian) 36 | */ 37 | #ifndef GET_ULONG_LE 38 | #define GET_ULONG_LE(n,b,i) \ 39 | { \ 40 | (n) = ( (unsigned long) (b)[(i) ] ) \ 41 | | ( (unsigned long) (b)[(i) + 1] << 8 ) \ 42 | | ( (unsigned long) (b)[(i) + 2] << 16 ) \ 43 | | ( (unsigned long) (b)[(i) + 3] << 24 ); \ 44 | } 45 | #endif 46 | 47 | #ifndef PUT_ULONG_LE 48 | #define PUT_ULONG_LE(n,b,i) \ 49 | { \ 50 | (b)[(i) ] = (unsigned char) ( (n) ); \ 51 | (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \ 52 | (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \ 53 | (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \ 54 | } 55 | #endif 56 | 57 | /* 58 | * Forward S-box 59 | */ 60 | static const unsigned char FSb[256] = 61 | { 62 | 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 63 | 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, 64 | 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 65 | 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 66 | 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 67 | 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15, 68 | 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 69 | 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75, 70 | 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 71 | 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 72 | 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 73 | 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF, 74 | 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 75 | 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8, 76 | 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 77 | 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2, 78 | 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 79 | 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73, 80 | 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 81 | 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB, 82 | 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 83 | 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79, 84 | 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 85 | 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08, 86 | 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 87 | 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A, 88 | 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 89 | 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 90 | 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 91 | 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF, 92 | 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 93 | 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16 94 | }; 95 | 96 | /* 97 | * Forward tables 98 | */ 99 | #define FT \ 100 | \ 101 | V(A5,63,63,C6), V(84,7C,7C,F8), V(99,77,77,EE), V(8D,7B,7B,F6), \ 102 | V(0D,F2,F2,FF), V(BD,6B,6B,D6), V(B1,6F,6F,DE), V(54,C5,C5,91), \ 103 | V(50,30,30,60), V(03,01,01,02), V(A9,67,67,CE), V(7D,2B,2B,56), \ 104 | V(19,FE,FE,E7), V(62,D7,D7,B5), V(E6,AB,AB,4D), V(9A,76,76,EC), \ 105 | V(45,CA,CA,8F), V(9D,82,82,1F), V(40,C9,C9,89), V(87,7D,7D,FA), \ 106 | V(15,FA,FA,EF), V(EB,59,59,B2), V(C9,47,47,8E), V(0B,F0,F0,FB), \ 107 | V(EC,AD,AD,41), V(67,D4,D4,B3), V(FD,A2,A2,5F), V(EA,AF,AF,45), \ 108 | V(BF,9C,9C,23), V(F7,A4,A4,53), V(96,72,72,E4), V(5B,C0,C0,9B), \ 109 | V(C2,B7,B7,75), V(1C,FD,FD,E1), V(AE,93,93,3D), V(6A,26,26,4C), \ 110 | V(5A,36,36,6C), V(41,3F,3F,7E), V(02,F7,F7,F5), V(4F,CC,CC,83), \ 111 | V(5C,34,34,68), V(F4,A5,A5,51), V(34,E5,E5,D1), V(08,F1,F1,F9), \ 112 | V(93,71,71,E2), V(73,D8,D8,AB), V(53,31,31,62), V(3F,15,15,2A), \ 113 | V(0C,04,04,08), V(52,C7,C7,95), V(65,23,23,46), V(5E,C3,C3,9D), \ 114 | V(28,18,18,30), V(A1,96,96,37), V(0F,05,05,0A), V(B5,9A,9A,2F), \ 115 | V(09,07,07,0E), V(36,12,12,24), V(9B,80,80,1B), V(3D,E2,E2,DF), \ 116 | V(26,EB,EB,CD), V(69,27,27,4E), V(CD,B2,B2,7F), V(9F,75,75,EA), \ 117 | V(1B,09,09,12), V(9E,83,83,1D), V(74,2C,2C,58), V(2E,1A,1A,34), \ 118 | V(2D,1B,1B,36), V(B2,6E,6E,DC), V(EE,5A,5A,B4), V(FB,A0,A0,5B), \ 119 | V(F6,52,52,A4), V(4D,3B,3B,76), V(61,D6,D6,B7), V(CE,B3,B3,7D), \ 120 | V(7B,29,29,52), V(3E,E3,E3,DD), V(71,2F,2F,5E), V(97,84,84,13), \ 121 | V(F5,53,53,A6), V(68,D1,D1,B9), V(00,00,00,00), V(2C,ED,ED,C1), \ 122 | V(60,20,20,40), V(1F,FC,FC,E3), V(C8,B1,B1,79), V(ED,5B,5B,B6), \ 123 | V(BE,6A,6A,D4), V(46,CB,CB,8D), V(D9,BE,BE,67), V(4B,39,39,72), \ 124 | V(DE,4A,4A,94), V(D4,4C,4C,98), V(E8,58,58,B0), V(4A,CF,CF,85), \ 125 | V(6B,D0,D0,BB), V(2A,EF,EF,C5), V(E5,AA,AA,4F), V(16,FB,FB,ED), \ 126 | V(C5,43,43,86), V(D7,4D,4D,9A), V(55,33,33,66), V(94,85,85,11), \ 127 | V(CF,45,45,8A), V(10,F9,F9,E9), V(06,02,02,04), V(81,7F,7F,FE), \ 128 | V(F0,50,50,A0), V(44,3C,3C,78), V(BA,9F,9F,25), V(E3,A8,A8,4B), \ 129 | V(F3,51,51,A2), V(FE,A3,A3,5D), V(C0,40,40,80), V(8A,8F,8F,05), \ 130 | V(AD,92,92,3F), V(BC,9D,9D,21), V(48,38,38,70), V(04,F5,F5,F1), \ 131 | V(DF,BC,BC,63), V(C1,B6,B6,77), V(75,DA,DA,AF), V(63,21,21,42), \ 132 | V(30,10,10,20), V(1A,FF,FF,E5), V(0E,F3,F3,FD), V(6D,D2,D2,BF), \ 133 | V(4C,CD,CD,81), V(14,0C,0C,18), V(35,13,13,26), V(2F,EC,EC,C3), \ 134 | V(E1,5F,5F,BE), V(A2,97,97,35), V(CC,44,44,88), V(39,17,17,2E), \ 135 | V(57,C4,C4,93), V(F2,A7,A7,55), V(82,7E,7E,FC), V(47,3D,3D,7A), \ 136 | V(AC,64,64,C8), V(E7,5D,5D,BA), V(2B,19,19,32), V(95,73,73,E6), \ 137 | V(A0,60,60,C0), V(98,81,81,19), V(D1,4F,4F,9E), V(7F,DC,DC,A3), \ 138 | V(66,22,22,44), V(7E,2A,2A,54), V(AB,90,90,3B), V(83,88,88,0B), \ 139 | V(CA,46,46,8C), V(29,EE,EE,C7), V(D3,B8,B8,6B), V(3C,14,14,28), \ 140 | V(79,DE,DE,A7), V(E2,5E,5E,BC), V(1D,0B,0B,16), V(76,DB,DB,AD), \ 141 | V(3B,E0,E0,DB), V(56,32,32,64), V(4E,3A,3A,74), V(1E,0A,0A,14), \ 142 | V(DB,49,49,92), V(0A,06,06,0C), V(6C,24,24,48), V(E4,5C,5C,B8), \ 143 | V(5D,C2,C2,9F), V(6E,D3,D3,BD), V(EF,AC,AC,43), V(A6,62,62,C4), \ 144 | V(A8,91,91,39), V(A4,95,95,31), V(37,E4,E4,D3), V(8B,79,79,F2), \ 145 | V(32,E7,E7,D5), V(43,C8,C8,8B), V(59,37,37,6E), V(B7,6D,6D,DA), \ 146 | V(8C,8D,8D,01), V(64,D5,D5,B1), V(D2,4E,4E,9C), V(E0,A9,A9,49), \ 147 | V(B4,6C,6C,D8), V(FA,56,56,AC), V(07,F4,F4,F3), V(25,EA,EA,CF), \ 148 | V(AF,65,65,CA), V(8E,7A,7A,F4), V(E9,AE,AE,47), V(18,08,08,10), \ 149 | V(D5,BA,BA,6F), V(88,78,78,F0), V(6F,25,25,4A), V(72,2E,2E,5C), \ 150 | V(24,1C,1C,38), V(F1,A6,A6,57), V(C7,B4,B4,73), V(51,C6,C6,97), \ 151 | V(23,E8,E8,CB), V(7C,DD,DD,A1), V(9C,74,74,E8), V(21,1F,1F,3E), \ 152 | V(DD,4B,4B,96), V(DC,BD,BD,61), V(86,8B,8B,0D), V(85,8A,8A,0F), \ 153 | V(90,70,70,E0), V(42,3E,3E,7C), V(C4,B5,B5,71), V(AA,66,66,CC), \ 154 | V(D8,48,48,90), V(05,03,03,06), V(01,F6,F6,F7), V(12,0E,0E,1C), \ 155 | V(A3,61,61,C2), V(5F,35,35,6A), V(F9,57,57,AE), V(D0,B9,B9,69), \ 156 | V(91,86,86,17), V(58,C1,C1,99), V(27,1D,1D,3A), V(B9,9E,9E,27), \ 157 | V(38,E1,E1,D9), V(13,F8,F8,EB), V(B3,98,98,2B), V(33,11,11,22), \ 158 | V(BB,69,69,D2), V(70,D9,D9,A9), V(89,8E,8E,07), V(A7,94,94,33), \ 159 | V(B6,9B,9B,2D), V(22,1E,1E,3C), V(92,87,87,15), V(20,E9,E9,C9), \ 160 | V(49,CE,CE,87), V(FF,55,55,AA), V(78,28,28,50), V(7A,DF,DF,A5), \ 161 | V(8F,8C,8C,03), V(F8,A1,A1,59), V(80,89,89,09), V(17,0D,0D,1A), \ 162 | V(DA,BF,BF,65), V(31,E6,E6,D7), V(C6,42,42,84), V(B8,68,68,D0), \ 163 | V(C3,41,41,82), V(B0,99,99,29), V(77,2D,2D,5A), V(11,0F,0F,1E), \ 164 | V(CB,B0,B0,7B), V(FC,54,54,A8), V(D6,BB,BB,6D), V(3A,16,16,2C) 165 | 166 | #define V(a,b,c,d) 0x##a##b##c##d 167 | static const unsigned long FT0[256] = { FT }; 168 | #undef V 169 | 170 | #define V(a,b,c,d) 0x##b##c##d##a 171 | static const unsigned long FT1[256] = { FT }; 172 | #undef V 173 | 174 | #define V(a,b,c,d) 0x##c##d##a##b 175 | static const unsigned long FT2[256] = { FT }; 176 | #undef V 177 | 178 | #define V(a,b,c,d) 0x##d##a##b##c 179 | static const unsigned long FT3[256] = { FT }; 180 | #undef V 181 | 182 | #undef FT 183 | 184 | /* 185 | * Reverse S-box 186 | */ 187 | static const unsigned char RSb[256] = 188 | { 189 | 0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 190 | 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB, 191 | 0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 192 | 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB, 193 | 0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 194 | 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E, 195 | 0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 196 | 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25, 197 | 0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 198 | 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92, 199 | 0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 200 | 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84, 201 | 0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 202 | 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06, 203 | 0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 204 | 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B, 205 | 0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 206 | 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73, 207 | 0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 208 | 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E, 209 | 0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 210 | 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B, 211 | 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 212 | 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4, 213 | 0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 214 | 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F, 215 | 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 216 | 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, 217 | 0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 218 | 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61, 219 | 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 220 | 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D 221 | }; 222 | 223 | /* 224 | * Reverse tables 225 | */ 226 | #define RT \ 227 | \ 228 | V(50,A7,F4,51), V(53,65,41,7E), V(C3,A4,17,1A), V(96,5E,27,3A), \ 229 | V(CB,6B,AB,3B), V(F1,45,9D,1F), V(AB,58,FA,AC), V(93,03,E3,4B), \ 230 | V(55,FA,30,20), V(F6,6D,76,AD), V(91,76,CC,88), V(25,4C,02,F5), \ 231 | V(FC,D7,E5,4F), V(D7,CB,2A,C5), V(80,44,35,26), V(8F,A3,62,B5), \ 232 | V(49,5A,B1,DE), V(67,1B,BA,25), V(98,0E,EA,45), V(E1,C0,FE,5D), \ 233 | V(02,75,2F,C3), V(12,F0,4C,81), V(A3,97,46,8D), V(C6,F9,D3,6B), \ 234 | V(E7,5F,8F,03), V(95,9C,92,15), V(EB,7A,6D,BF), V(DA,59,52,95), \ 235 | V(2D,83,BE,D4), V(D3,21,74,58), V(29,69,E0,49), V(44,C8,C9,8E), \ 236 | V(6A,89,C2,75), V(78,79,8E,F4), V(6B,3E,58,99), V(DD,71,B9,27), \ 237 | V(B6,4F,E1,BE), V(17,AD,88,F0), V(66,AC,20,C9), V(B4,3A,CE,7D), \ 238 | V(18,4A,DF,63), V(82,31,1A,E5), V(60,33,51,97), V(45,7F,53,62), \ 239 | V(E0,77,64,B1), V(84,AE,6B,BB), V(1C,A0,81,FE), V(94,2B,08,F9), \ 240 | V(58,68,48,70), V(19,FD,45,8F), V(87,6C,DE,94), V(B7,F8,7B,52), \ 241 | V(23,D3,73,AB), V(E2,02,4B,72), V(57,8F,1F,E3), V(2A,AB,55,66), \ 242 | V(07,28,EB,B2), V(03,C2,B5,2F), V(9A,7B,C5,86), V(A5,08,37,D3), \ 243 | V(F2,87,28,30), V(B2,A5,BF,23), V(BA,6A,03,02), V(5C,82,16,ED), \ 244 | V(2B,1C,CF,8A), V(92,B4,79,A7), V(F0,F2,07,F3), V(A1,E2,69,4E), \ 245 | V(CD,F4,DA,65), V(D5,BE,05,06), V(1F,62,34,D1), V(8A,FE,A6,C4), \ 246 | V(9D,53,2E,34), V(A0,55,F3,A2), V(32,E1,8A,05), V(75,EB,F6,A4), \ 247 | V(39,EC,83,0B), V(AA,EF,60,40), V(06,9F,71,5E), V(51,10,6E,BD), \ 248 | V(F9,8A,21,3E), V(3D,06,DD,96), V(AE,05,3E,DD), V(46,BD,E6,4D), \ 249 | V(B5,8D,54,91), V(05,5D,C4,71), V(6F,D4,06,04), V(FF,15,50,60), \ 250 | V(24,FB,98,19), V(97,E9,BD,D6), V(CC,43,40,89), V(77,9E,D9,67), \ 251 | V(BD,42,E8,B0), V(88,8B,89,07), V(38,5B,19,E7), V(DB,EE,C8,79), \ 252 | V(47,0A,7C,A1), V(E9,0F,42,7C), V(C9,1E,84,F8), V(00,00,00,00), \ 253 | V(83,86,80,09), V(48,ED,2B,32), V(AC,70,11,1E), V(4E,72,5A,6C), \ 254 | V(FB,FF,0E,FD), V(56,38,85,0F), V(1E,D5,AE,3D), V(27,39,2D,36), \ 255 | V(64,D9,0F,0A), V(21,A6,5C,68), V(D1,54,5B,9B), V(3A,2E,36,24), \ 256 | V(B1,67,0A,0C), V(0F,E7,57,93), V(D2,96,EE,B4), V(9E,91,9B,1B), \ 257 | V(4F,C5,C0,80), V(A2,20,DC,61), V(69,4B,77,5A), V(16,1A,12,1C), \ 258 | V(0A,BA,93,E2), V(E5,2A,A0,C0), V(43,E0,22,3C), V(1D,17,1B,12), \ 259 | V(0B,0D,09,0E), V(AD,C7,8B,F2), V(B9,A8,B6,2D), V(C8,A9,1E,14), \ 260 | V(85,19,F1,57), V(4C,07,75,AF), V(BB,DD,99,EE), V(FD,60,7F,A3), \ 261 | V(9F,26,01,F7), V(BC,F5,72,5C), V(C5,3B,66,44), V(34,7E,FB,5B), \ 262 | V(76,29,43,8B), V(DC,C6,23,CB), V(68,FC,ED,B6), V(63,F1,E4,B8), \ 263 | V(CA,DC,31,D7), V(10,85,63,42), V(40,22,97,13), V(20,11,C6,84), \ 264 | V(7D,24,4A,85), V(F8,3D,BB,D2), V(11,32,F9,AE), V(6D,A1,29,C7), \ 265 | V(4B,2F,9E,1D), V(F3,30,B2,DC), V(EC,52,86,0D), V(D0,E3,C1,77), \ 266 | V(6C,16,B3,2B), V(99,B9,70,A9), V(FA,48,94,11), V(22,64,E9,47), \ 267 | V(C4,8C,FC,A8), V(1A,3F,F0,A0), V(D8,2C,7D,56), V(EF,90,33,22), \ 268 | V(C7,4E,49,87), V(C1,D1,38,D9), V(FE,A2,CA,8C), V(36,0B,D4,98), \ 269 | V(CF,81,F5,A6), V(28,DE,7A,A5), V(26,8E,B7,DA), V(A4,BF,AD,3F), \ 270 | V(E4,9D,3A,2C), V(0D,92,78,50), V(9B,CC,5F,6A), V(62,46,7E,54), \ 271 | V(C2,13,8D,F6), V(E8,B8,D8,90), V(5E,F7,39,2E), V(F5,AF,C3,82), \ 272 | V(BE,80,5D,9F), V(7C,93,D0,69), V(A9,2D,D5,6F), V(B3,12,25,CF), \ 273 | V(3B,99,AC,C8), V(A7,7D,18,10), V(6E,63,9C,E8), V(7B,BB,3B,DB), \ 274 | V(09,78,26,CD), V(F4,18,59,6E), V(01,B7,9A,EC), V(A8,9A,4F,83), \ 275 | V(65,6E,95,E6), V(7E,E6,FF,AA), V(08,CF,BC,21), V(E6,E8,15,EF), \ 276 | V(D9,9B,E7,BA), V(CE,36,6F,4A), V(D4,09,9F,EA), V(D6,7C,B0,29), \ 277 | V(AF,B2,A4,31), V(31,23,3F,2A), V(30,94,A5,C6), V(C0,66,A2,35), \ 278 | V(37,BC,4E,74), V(A6,CA,82,FC), V(B0,D0,90,E0), V(15,D8,A7,33), \ 279 | V(4A,98,04,F1), V(F7,DA,EC,41), V(0E,50,CD,7F), V(2F,F6,91,17), \ 280 | V(8D,D6,4D,76), V(4D,B0,EF,43), V(54,4D,AA,CC), V(DF,04,96,E4), \ 281 | V(E3,B5,D1,9E), V(1B,88,6A,4C), V(B8,1F,2C,C1), V(7F,51,65,46), \ 282 | V(04,EA,5E,9D), V(5D,35,8C,01), V(73,74,87,FA), V(2E,41,0B,FB), \ 283 | V(5A,1D,67,B3), V(52,D2,DB,92), V(33,56,10,E9), V(13,47,D6,6D), \ 284 | V(8C,61,D7,9A), V(7A,0C,A1,37), V(8E,14,F8,59), V(89,3C,13,EB), \ 285 | V(EE,27,A9,CE), V(35,C9,61,B7), V(ED,E5,1C,E1), V(3C,B1,47,7A), \ 286 | V(59,DF,D2,9C), V(3F,73,F2,55), V(79,CE,14,18), V(BF,37,C7,73), \ 287 | V(EA,CD,F7,53), V(5B,AA,FD,5F), V(14,6F,3D,DF), V(86,DB,44,78), \ 288 | V(81,F3,AF,CA), V(3E,C4,68,B9), V(2C,34,24,38), V(5F,40,A3,C2), \ 289 | V(72,C3,1D,16), V(0C,25,E2,BC), V(8B,49,3C,28), V(41,95,0D,FF), \ 290 | V(71,01,A8,39), V(DE,B3,0C,08), V(9C,E4,B4,D8), V(90,C1,56,64), \ 291 | V(61,84,CB,7B), V(70,B6,32,D5), V(74,5C,6C,48), V(42,57,B8,D0) 292 | 293 | #define V(a,b,c,d) 0x##a##b##c##d 294 | static const unsigned long RT0[256] = { RT }; 295 | #undef V 296 | 297 | #define V(a,b,c,d) 0x##b##c##d##a 298 | static const unsigned long RT1[256] = { RT }; 299 | #undef V 300 | 301 | #define V(a,b,c,d) 0x##c##d##a##b 302 | static const unsigned long RT2[256] = { RT }; 303 | #undef V 304 | 305 | #define V(a,b,c,d) 0x##d##a##b##c 306 | static const unsigned long RT3[256] = { RT }; 307 | #undef V 308 | 309 | #undef RT 310 | 311 | /* 312 | * Round constants 313 | */ 314 | static const unsigned long RCON[10] = 315 | { 316 | 0x00000001, 0x00000002, 0x00000004, 0x00000008, 317 | 0x00000010, 0x00000020, 0x00000040, 0x00000080, 318 | 0x0000001B, 0x00000036 319 | }; 320 | 321 | /* 322 | * AES key schedule (encryption) 323 | */ 324 | int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int keysize ) 325 | { 326 | unsigned int i; 327 | unsigned long *RK; 328 | 329 | switch( keysize ) 330 | { 331 | case 128: ctx->nr = 10; break; 332 | case 192: ctx->nr = 12; break; 333 | case 256: ctx->nr = 14; break; 334 | default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH ); 335 | } 336 | 337 | ctx->rk = RK = ctx->buf; 338 | 339 | for( i = 0; i < (keysize >> 5); i++ ) 340 | { 341 | GET_ULONG_LE( RK[i], key, i << 2 ); 342 | } 343 | 344 | switch( ctx->nr ) 345 | { 346 | case 10: 347 | 348 | for( i = 0; i < 10; i++, RK += 4 ) 349 | { 350 | RK[4] = RK[0] ^ RCON[i] ^ 351 | ( (unsigned long) FSb[ ( RK[3] >> 8 ) & 0xFF ] ) ^ 352 | ( (unsigned long) FSb[ ( RK[3] >> 16 ) & 0xFF ] << 8 ) ^ 353 | ( (unsigned long) FSb[ ( RK[3] >> 24 ) & 0xFF ] << 16 ) ^ 354 | ( (unsigned long) FSb[ ( RK[3] ) & 0xFF ] << 24 ); 355 | 356 | RK[5] = RK[1] ^ RK[4]; 357 | RK[6] = RK[2] ^ RK[5]; 358 | RK[7] = RK[3] ^ RK[6]; 359 | } 360 | break; 361 | 362 | case 12: 363 | 364 | for( i = 0; i < 8; i++, RK += 6 ) 365 | { 366 | RK[6] = RK[0] ^ RCON[i] ^ 367 | ( (unsigned long) FSb[ ( RK[5] >> 8 ) & 0xFF ] ) ^ 368 | ( (unsigned long) FSb[ ( RK[5] >> 16 ) & 0xFF ] << 8 ) ^ 369 | ( (unsigned long) FSb[ ( RK[5] >> 24 ) & 0xFF ] << 16 ) ^ 370 | ( (unsigned long) FSb[ ( RK[5] ) & 0xFF ] << 24 ); 371 | 372 | RK[7] = RK[1] ^ RK[6]; 373 | RK[8] = RK[2] ^ RK[7]; 374 | RK[9] = RK[3] ^ RK[8]; 375 | RK[10] = RK[4] ^ RK[9]; 376 | RK[11] = RK[5] ^ RK[10]; 377 | } 378 | break; 379 | 380 | case 14: 381 | 382 | for( i = 0; i < 7; i++, RK += 8 ) 383 | { 384 | RK[8] = RK[0] ^ RCON[i] ^ 385 | ( (unsigned long) FSb[ ( RK[7] >> 8 ) & 0xFF ] ) ^ 386 | ( (unsigned long) FSb[ ( RK[7] >> 16 ) & 0xFF ] << 8 ) ^ 387 | ( (unsigned long) FSb[ ( RK[7] >> 24 ) & 0xFF ] << 16 ) ^ 388 | ( (unsigned long) FSb[ ( RK[7] ) & 0xFF ] << 24 ); 389 | 390 | RK[9] = RK[1] ^ RK[8]; 391 | RK[10] = RK[2] ^ RK[9]; 392 | RK[11] = RK[3] ^ RK[10]; 393 | 394 | RK[12] = RK[4] ^ 395 | ( (unsigned long) FSb[ ( RK[11] ) & 0xFF ] ) ^ 396 | ( (unsigned long) FSb[ ( RK[11] >> 8 ) & 0xFF ] << 8 ) ^ 397 | ( (unsigned long) FSb[ ( RK[11] >> 16 ) & 0xFF ] << 16 ) ^ 398 | ( (unsigned long) FSb[ ( RK[11] >> 24 ) & 0xFF ] << 24 ); 399 | 400 | RK[13] = RK[5] ^ RK[12]; 401 | RK[14] = RK[6] ^ RK[13]; 402 | RK[15] = RK[7] ^ RK[14]; 403 | } 404 | break; 405 | 406 | default: 407 | 408 | break; 409 | } 410 | 411 | return( 0 ); 412 | } 413 | 414 | /* 415 | * AES key schedule (decryption) 416 | */ 417 | int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int keysize ) 418 | { 419 | int i, j; 420 | aes_context cty; 421 | unsigned long *RK; 422 | unsigned long *SK; 423 | int ret; 424 | 425 | switch( keysize ) 426 | { 427 | case 128: ctx->nr = 10; break; 428 | case 192: ctx->nr = 12; break; 429 | case 256: ctx->nr = 14; break; 430 | default : return( POLARSSL_ERR_AES_INVALID_KEY_LENGTH ); 431 | } 432 | 433 | ctx->rk = RK = ctx->buf; 434 | 435 | ret = aes_setkey_enc( &cty, key, keysize ); 436 | if( ret != 0 ) 437 | return( ret ); 438 | 439 | SK = cty.rk + cty.nr * 4; 440 | 441 | *RK++ = *SK++; 442 | *RK++ = *SK++; 443 | *RK++ = *SK++; 444 | *RK++ = *SK++; 445 | 446 | for( i = ctx->nr - 1, SK -= 8; i > 0; i--, SK -= 8 ) 447 | { 448 | for( j = 0; j < 4; j++, SK++ ) 449 | { 450 | *RK++ = RT0[ FSb[ ( *SK ) & 0xFF ] ] ^ 451 | RT1[ FSb[ ( *SK >> 8 ) & 0xFF ] ] ^ 452 | RT2[ FSb[ ( *SK >> 16 ) & 0xFF ] ] ^ 453 | RT3[ FSb[ ( *SK >> 24 ) & 0xFF ] ]; 454 | } 455 | } 456 | 457 | *RK++ = *SK++; 458 | *RK++ = *SK++; 459 | *RK++ = *SK++; 460 | *RK++ = *SK++; 461 | 462 | memset( &cty, 0, sizeof( aes_context ) ); 463 | 464 | return( 0 ); 465 | } 466 | 467 | #define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \ 468 | { \ 469 | X0 = *RK++ ^ FT0[ ( Y0 ) & 0xFF ] ^ \ 470 | FT1[ ( Y1 >> 8 ) & 0xFF ] ^ \ 471 | FT2[ ( Y2 >> 16 ) & 0xFF ] ^ \ 472 | FT3[ ( Y3 >> 24 ) & 0xFF ]; \ 473 | \ 474 | X1 = *RK++ ^ FT0[ ( Y1 ) & 0xFF ] ^ \ 475 | FT1[ ( Y2 >> 8 ) & 0xFF ] ^ \ 476 | FT2[ ( Y3 >> 16 ) & 0xFF ] ^ \ 477 | FT3[ ( Y0 >> 24 ) & 0xFF ]; \ 478 | \ 479 | X2 = *RK++ ^ FT0[ ( Y2 ) & 0xFF ] ^ \ 480 | FT1[ ( Y3 >> 8 ) & 0xFF ] ^ \ 481 | FT2[ ( Y0 >> 16 ) & 0xFF ] ^ \ 482 | FT3[ ( Y1 >> 24 ) & 0xFF ]; \ 483 | \ 484 | X3 = *RK++ ^ FT0[ ( Y3 ) & 0xFF ] ^ \ 485 | FT1[ ( Y0 >> 8 ) & 0xFF ] ^ \ 486 | FT2[ ( Y1 >> 16 ) & 0xFF ] ^ \ 487 | FT3[ ( Y2 >> 24 ) & 0xFF ]; \ 488 | } 489 | 490 | #define AES_RROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \ 491 | { \ 492 | X0 = *RK++ ^ RT0[ ( Y0 ) & 0xFF ] ^ \ 493 | RT1[ ( Y3 >> 8 ) & 0xFF ] ^ \ 494 | RT2[ ( Y2 >> 16 ) & 0xFF ] ^ \ 495 | RT3[ ( Y1 >> 24 ) & 0xFF ]; \ 496 | \ 497 | X1 = *RK++ ^ RT0[ ( Y1 ) & 0xFF ] ^ \ 498 | RT1[ ( Y0 >> 8 ) & 0xFF ] ^ \ 499 | RT2[ ( Y3 >> 16 ) & 0xFF ] ^ \ 500 | RT3[ ( Y2 >> 24 ) & 0xFF ]; \ 501 | \ 502 | X2 = *RK++ ^ RT0[ ( Y2 ) & 0xFF ] ^ \ 503 | RT1[ ( Y1 >> 8 ) & 0xFF ] ^ \ 504 | RT2[ ( Y0 >> 16 ) & 0xFF ] ^ \ 505 | RT3[ ( Y3 >> 24 ) & 0xFF ]; \ 506 | \ 507 | X3 = *RK++ ^ RT0[ ( Y3 ) & 0xFF ] ^ \ 508 | RT1[ ( Y2 >> 8 ) & 0xFF ] ^ \ 509 | RT2[ ( Y1 >> 16 ) & 0xFF ] ^ \ 510 | RT3[ ( Y0 >> 24 ) & 0xFF ]; \ 511 | } 512 | 513 | /* 514 | * AES-ECB block encryption/decryption 515 | */ 516 | int aes_crypt_ecb( aes_context *ctx, 517 | int mode, 518 | const unsigned char input[16], 519 | unsigned char output[16] ) 520 | { 521 | int i; 522 | unsigned long *RK, X0, X1, X2, X3, Y0, Y1, Y2, Y3; 523 | 524 | RK = ctx->rk; 525 | 526 | GET_ULONG_LE( X0, input, 0 ); X0 ^= *RK++; 527 | GET_ULONG_LE( X1, input, 4 ); X1 ^= *RK++; 528 | GET_ULONG_LE( X2, input, 8 ); X2 ^= *RK++; 529 | GET_ULONG_LE( X3, input, 12 ); X3 ^= *RK++; 530 | 531 | if( mode == AES_DECRYPT ) 532 | { 533 | for( i = (ctx->nr >> 1) - 1; i > 0; i-- ) 534 | { 535 | AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); 536 | AES_RROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); 537 | } 538 | 539 | AES_RROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); 540 | 541 | X0 = *RK++ ^ \ 542 | ( (unsigned long) RSb[ ( Y0 ) & 0xFF ] ) ^ 543 | ( (unsigned long) RSb[ ( Y3 >> 8 ) & 0xFF ] << 8 ) ^ 544 | ( (unsigned long) RSb[ ( Y2 >> 16 ) & 0xFF ] << 16 ) ^ 545 | ( (unsigned long) RSb[ ( Y1 >> 24 ) & 0xFF ] << 24 ); 546 | 547 | X1 = *RK++ ^ \ 548 | ( (unsigned long) RSb[ ( Y1 ) & 0xFF ] ) ^ 549 | ( (unsigned long) RSb[ ( Y0 >> 8 ) & 0xFF ] << 8 ) ^ 550 | ( (unsigned long) RSb[ ( Y3 >> 16 ) & 0xFF ] << 16 ) ^ 551 | ( (unsigned long) RSb[ ( Y2 >> 24 ) & 0xFF ] << 24 ); 552 | 553 | X2 = *RK++ ^ \ 554 | ( (unsigned long) RSb[ ( Y2 ) & 0xFF ] ) ^ 555 | ( (unsigned long) RSb[ ( Y1 >> 8 ) & 0xFF ] << 8 ) ^ 556 | ( (unsigned long) RSb[ ( Y0 >> 16 ) & 0xFF ] << 16 ) ^ 557 | ( (unsigned long) RSb[ ( Y3 >> 24 ) & 0xFF ] << 24 ); 558 | 559 | X3 = *RK++ ^ \ 560 | ( (unsigned long) RSb[ ( Y3 ) & 0xFF ] ) ^ 561 | ( (unsigned long) RSb[ ( Y2 >> 8 ) & 0xFF ] << 8 ) ^ 562 | ( (unsigned long) RSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^ 563 | ( (unsigned long) RSb[ ( Y0 >> 24 ) & 0xFF ] << 24 ); 564 | } 565 | else /* AES_ENCRYPT */ 566 | { 567 | for( i = (ctx->nr >> 1) - 1; i > 0; i-- ) 568 | { 569 | AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); 570 | AES_FROUND( X0, X1, X2, X3, Y0, Y1, Y2, Y3 ); 571 | } 572 | 573 | AES_FROUND( Y0, Y1, Y2, Y3, X0, X1, X2, X3 ); 574 | 575 | X0 = *RK++ ^ \ 576 | ( (unsigned long) FSb[ ( Y0 ) & 0xFF ] ) ^ 577 | ( (unsigned long) FSb[ ( Y1 >> 8 ) & 0xFF ] << 8 ) ^ 578 | ( (unsigned long) FSb[ ( Y2 >> 16 ) & 0xFF ] << 16 ) ^ 579 | ( (unsigned long) FSb[ ( Y3 >> 24 ) & 0xFF ] << 24 ); 580 | 581 | X1 = *RK++ ^ \ 582 | ( (unsigned long) FSb[ ( Y1 ) & 0xFF ] ) ^ 583 | ( (unsigned long) FSb[ ( Y2 >> 8 ) & 0xFF ] << 8 ) ^ 584 | ( (unsigned long) FSb[ ( Y3 >> 16 ) & 0xFF ] << 16 ) ^ 585 | ( (unsigned long) FSb[ ( Y0 >> 24 ) & 0xFF ] << 24 ); 586 | 587 | X2 = *RK++ ^ \ 588 | ( (unsigned long) FSb[ ( Y2 ) & 0xFF ] ) ^ 589 | ( (unsigned long) FSb[ ( Y3 >> 8 ) & 0xFF ] << 8 ) ^ 590 | ( (unsigned long) FSb[ ( Y0 >> 16 ) & 0xFF ] << 16 ) ^ 591 | ( (unsigned long) FSb[ ( Y1 >> 24 ) & 0xFF ] << 24 ); 592 | 593 | X3 = *RK++ ^ \ 594 | ( (unsigned long) FSb[ ( Y3 ) & 0xFF ] ) ^ 595 | ( (unsigned long) FSb[ ( Y0 >> 8 ) & 0xFF ] << 8 ) ^ 596 | ( (unsigned long) FSb[ ( Y1 >> 16 ) & 0xFF ] << 16 ) ^ 597 | ( (unsigned long) FSb[ ( Y2 >> 24 ) & 0xFF ] << 24 ); 598 | } 599 | 600 | PUT_ULONG_LE( X0, output, 0 ); 601 | PUT_ULONG_LE( X1, output, 4 ); 602 | PUT_ULONG_LE( X2, output, 8 ); 603 | PUT_ULONG_LE( X3, output, 12 ); 604 | 605 | return( 0 ); 606 | } 607 | 608 | /* 609 | * AES-CBC buffer encryption/decryption 610 | */ 611 | int aes_crypt_cbc( aes_context *ctx, 612 | int mode, 613 | size_t length, 614 | unsigned char iv[16], 615 | const unsigned char *input, 616 | unsigned char *output ) 617 | { 618 | int i; 619 | unsigned char temp[16]; 620 | 621 | if( length % 16 ) 622 | return( POLARSSL_ERR_AES_INVALID_INPUT_LENGTH ); 623 | 624 | if( mode == AES_DECRYPT ) 625 | { 626 | while( length > 0 ) 627 | { 628 | memcpy( temp, input, 16 ); 629 | aes_crypt_ecb( ctx, mode, input, output ); 630 | 631 | for( i = 0; i < 16; i++ ) 632 | output[i] = (unsigned char)( output[i] ^ iv[i] ); 633 | 634 | memcpy( iv, temp, 16 ); 635 | 636 | input += 16; 637 | output += 16; 638 | length -= 16; 639 | } 640 | } 641 | else 642 | { 643 | while( length > 0 ) 644 | { 645 | for( i = 0; i < 16; i++ ) 646 | output[i] = (unsigned char)( input[i] ^ iv[i] ); 647 | 648 | aes_crypt_ecb( ctx, mode, output, output ); 649 | memcpy( iv, output, 16 ); 650 | 651 | input += 16; 652 | output += 16; 653 | length -= 16; 654 | } 655 | } 656 | 657 | return( 0 ); 658 | } 659 | -------------------------------------------------------------------------------- /src/aes.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file aes.h 3 | * 4 | * \brief AES block cipher 5 | * 6 | * Copyright (C) 2006-2010, Brainspark B.V. 7 | * 8 | * This file is part of PolarSSL (http://www.polarssl.org) 9 | * Lead Maintainer: Paul Bakker 10 | * 11 | * All rights reserved. 12 | * 13 | * This program is free software; you can redistribute it and/or modify 14 | * it under the terms of the GNU General Public License as published by 15 | * the Free Software Foundation; either version 2 of the License, or 16 | * (at your option) any later version. 17 | * 18 | * This program is distributed in the hope that it will be useful, 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 | * GNU General Public License for more details. 22 | * 23 | * You should have received a copy of the GNU General Public License along 24 | * with this program; if not, write to the Free Software Foundation, Inc., 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 26 | */ 27 | #ifndef POLARSSL_AES_H 28 | #define POLARSSL_AES_H 29 | 30 | #include 31 | 32 | #define AES_ENCRYPT 1 33 | #define AES_DECRYPT 0 34 | 35 | #define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */ 36 | #define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */ 37 | 38 | /** 39 | * \brief AES context structure 40 | */ 41 | typedef struct 42 | { 43 | int nr; /*!< number of rounds */ 44 | unsigned long *rk; /*!< AES round keys */ 45 | unsigned long buf[68]; /*!< unaligned data */ 46 | } 47 | aes_context; 48 | 49 | #ifdef __cplusplus 50 | extern "C" { 51 | #endif 52 | 53 | /** 54 | * \brief AES key schedule (encryption) 55 | * 56 | * \param ctx AES context to be initialized 57 | * \param key encryption key 58 | * \param keysize must be 128, 192 or 256 59 | * 60 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH 61 | */ 62 | int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int keysize ); 63 | 64 | /** 65 | * \brief AES key schedule (decryption) 66 | * 67 | * \param ctx AES context to be initialized 68 | * \param key decryption key 69 | * \param keysize must be 128, 192 or 256 70 | * 71 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH 72 | */ 73 | int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int keysize ); 74 | 75 | /** 76 | * \brief AES-ECB block encryption/decryption 77 | * 78 | * \param ctx AES context 79 | * \param mode AES_ENCRYPT or AES_DECRYPT 80 | * \param input 16-byte input block 81 | * \param output 16-byte output block 82 | * 83 | * \return 0 if successful 84 | */ 85 | int aes_crypt_ecb( aes_context *ctx, 86 | int mode, 87 | const unsigned char input[16], 88 | unsigned char output[16] ); 89 | 90 | /** 91 | * \brief AES-CBC buffer encryption/decryption 92 | * Length should be a multiple of the block 93 | * size (16 bytes) 94 | * 95 | * \param ctx AES context 96 | * \param mode AES_ENCRYPT or AES_DECRYPT 97 | * \param length length of the input data 98 | * \param iv initialization vector (updated after use) 99 | * \param input buffer holding the input data 100 | * \param output buffer holding the output data 101 | * 102 | * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH 103 | */ 104 | int aes_crypt_cbc( aes_context *ctx, 105 | int mode, 106 | size_t length, 107 | unsigned char iv[16], 108 | const unsigned char *input, 109 | unsigned char *output ); 110 | 111 | #ifdef __cplusplus 112 | } 113 | #endif 114 | 115 | #endif /* aes.h */ 116 | -------------------------------------------------------------------------------- /src/aes_xts.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012 by naehrwert 3 | * This file is released under the GPLv2. 4 | */ 5 | 6 | #include "types.h" 7 | #include "aes.h" 8 | #include "aes_xts.h" 9 | 10 | int aes_xts_init(aes_xts_ctxt_t *ctxt, int mode, const u8 *data_key, const u8 *tweak_key, int keybits) 11 | { 12 | ctxt->mode = mode; 13 | ctxt->keybits = keybits; 14 | aes_setkey_enc(&ctxt->twk_ctxt, tweak_key, keybits); 15 | 16 | if(mode == AES_ENCRYPT) 17 | aes_setkey_enc(&ctxt->aes_ctxt, data_key, keybits); 18 | else if(mode == AES_DECRYPT) 19 | aes_setkey_dec(&ctxt->aes_ctxt, data_key, keybits); 20 | else 21 | return -1; 22 | 23 | return 0; 24 | } 25 | 26 | int aes_xts_crypt(aes_xts_ctxt_t *ctxt, u64 seqno, u32 sector_size, const u8 *in, u8 *out) 27 | { 28 | u32 i = 0, j = 0; 29 | u8 tweak[0x10], buf[0x10]; 30 | u8 carry_in, carry_out; 31 | 32 | //Check alignment. 33 | if(!(sector_size % 0x10 == 0)) 34 | return -1; 35 | 36 | //Init tweak. 37 | ((u64 *)tweak)[0] = seqno;//_ES64(seqno); 38 | ((u64 *)tweak)[1] = 0; 39 | aes_crypt_ecb(&ctxt->twk_ctxt, AES_ENCRYPT, tweak, tweak); 40 | 41 | //En-/decrypt sector. 42 | for(i = 0; i < sector_size; i += 0x10) 43 | { 44 | for(j = 0; j < 0x10; j++) 45 | buf[j] = in[i+j] ^ tweak[j]; 46 | 47 | //En-/decrypt 0x10 bytes. 48 | aes_crypt_ecb(&ctxt->aes_ctxt, ctxt->mode, buf, buf); 49 | 50 | for(j = 0; j < 0x10; j++) 51 | out[i+j] = buf[j] ^ tweak[j]; 52 | 53 | //Update tweak (GF MUL). 54 | carry_in = 0; 55 | for(j = 0; j < 0x10; j++) 56 | { 57 | carry_out = (tweak[j] >> 7) & 1; 58 | tweak[j] = ((tweak[j] << 1) + carry_in) & 0xFF; 59 | carry_in = carry_out; 60 | } 61 | if(carry_out) 62 | tweak[0] ^= 0x87; 63 | } 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /src/aes_xts.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012 by naehrwert 3 | * This file is released under the GPLv2. 4 | */ 5 | 6 | #ifndef _AES_XTS_H_ 7 | #define _AES_XTS_H_ 8 | 9 | #include "types.h" 10 | //Using polarssl. 11 | #include "aes.h" 12 | 13 | /*! AES-XTS context. */ 14 | typedef struct _aes_xts_ctxt 15 | { 16 | aes_context twk_ctxt; 17 | aes_context aes_ctxt; 18 | int mode; 19 | int keybits; 20 | } aes_xts_ctxt_t; 21 | 22 | /*! Initialize AES-XTS context. */ 23 | int aes_xts_init(aes_xts_ctxt_t *ctxt, int mode, const u8 *data_key, const u8 *tweak_key, int keybits); 24 | /*! Crypt buffer (must be aligned to 0x10). */ 25 | int aes_xts_crypt(aes_xts_ctxt_t *ctxt, u64 seqno, u32 sector_size, const u8 *in, u8 *out); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /src/getopt.c: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | 3 | /* Getopt for Microsoft C 4 | This code is a modification of the Free Software Foundation, Inc. 5 | Getopt library for parsing command line argument the purpose was 6 | to provide a Microsoft Visual C friendly derivative. This code 7 | provides functionality for both Unicode and Multibyte builds. 8 | 9 | Date: 02/03/2011 - Ludvik Jerabek - Initial Release 10 | Version: 1.0 11 | Comment: Supports getopt, getopt_long, and getopt_long_only 12 | and POSIXLY_CORRECT environment flag 13 | License: LGPL 14 | 15 | Revisions: 16 | 17 | 02/03/2011 - Ludvik Jerabek - Initial Release 18 | 02/20/2011 - Ludvik Jerabek - Fixed compiler warnings at Level 4 19 | 07/05/2011 - Ludvik Jerabek - Added no_argument, required_argument, optional_argument defs 20 | 08/03/2011 - Ludvik Jerabek - Fixed non-argument runtime bug which caused runtime exception 21 | 08/09/2011 - Ludvik Jerabek - Added code to export functions for DLL and LIB 22 | 23 | **DISCLAIMER** 24 | THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 25 | EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE 26 | IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 27 | PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE 28 | EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT 29 | APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY 30 | DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY 31 | USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST 32 | PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON 33 | YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE 34 | EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 35 | */ 36 | 37 | #define _CRT_SECURE_NO_WARNINGS 38 | #include 39 | #include 40 | #include "getopt.h" 41 | 42 | enum ENUM_ORDERING { REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER }; 43 | 44 | struct _getopt_data 45 | { 46 | int optind; 47 | int opterr; 48 | int optopt; 49 | TCHAR *optarg; 50 | int __initialized; 51 | TCHAR *__nextchar; 52 | int __ordering; 53 | int __posixly_correct; 54 | int __first_nonopt; 55 | int __last_nonopt; 56 | }; 57 | 58 | static struct _getopt_data getopt_data; 59 | 60 | TCHAR *optarg; 61 | int optind = 1; 62 | int opterr = 1; 63 | int optopt = _T('?'); 64 | 65 | static void exchange(TCHAR **argv, struct _getopt_data *d) 66 | { 67 | int bottom = d->__first_nonopt; 68 | int middle = d->__last_nonopt; 69 | int top = d->optind; 70 | TCHAR *tem; 71 | while (top > middle && middle > bottom) 72 | { 73 | if (top - middle > middle - bottom) 74 | { 75 | int len = middle - bottom; 76 | register int i; 77 | for (i = 0; i < len; i++) 78 | { 79 | tem = argv[bottom + i]; 80 | argv[bottom + i] = argv[top - (middle - bottom) + i]; 81 | argv[top - (middle - bottom) + i] = tem; 82 | } 83 | top -= len; 84 | } 85 | else 86 | { 87 | int len = top - middle; 88 | register int i; 89 | for (i = 0; i < len; i++) 90 | { 91 | tem = argv[bottom + i]; 92 | argv[bottom + i] = argv[middle + i]; 93 | argv[middle + i] = tem; 94 | } 95 | bottom += len; 96 | } 97 | } 98 | d->__first_nonopt += (d->optind - d->__last_nonopt); 99 | d->__last_nonopt = d->optind; 100 | } 101 | 102 | 103 | static const TCHAR *_getopt_initialize (const TCHAR *optstring, struct _getopt_data *d, int posixly_correct) 104 | { 105 | d->__first_nonopt = d->__last_nonopt = d->optind; 106 | d->__nextchar = NULL; 107 | d->__posixly_correct = posixly_correct | !!_tgetenv(_T("POSIXLY_CORRECT")); 108 | 109 | 110 | if (optstring[0] == _T('-')) 111 | { 112 | d->__ordering = RETURN_IN_ORDER; 113 | ++optstring; 114 | } 115 | else if (optstring[0] == _T('+')) 116 | { 117 | d->__ordering = REQUIRE_ORDER; 118 | ++optstring; 119 | } 120 | else if (d->__posixly_correct) 121 | d->__ordering = REQUIRE_ORDER; 122 | else 123 | d->__ordering = PERMUTE; 124 | return optstring; 125 | } 126 | 127 | int _getopt_internal_r (int argc, TCHAR *const *argv, const TCHAR *optstring, const struct option *longopts, int *longind, int long_only, struct _getopt_data *d, int posixly_correct) 128 | { 129 | int print_errors = d->opterr; 130 | 131 | if (argc < 1) 132 | return -1; 133 | 134 | d->optarg = NULL; 135 | 136 | if (d->optind == 0 || !d->__initialized) 137 | { 138 | if (d->optind == 0) 139 | d->optind = 1; 140 | optstring = _getopt_initialize (optstring, d, posixly_correct); 141 | d->__initialized = 1; 142 | } 143 | else if (optstring[0] == _T('-') || optstring[0] == _T('+')) 144 | optstring++; 145 | if (optstring[0] == _T(':')) 146 | print_errors = 0; 147 | 148 | if (d->__nextchar == NULL || *d->__nextchar == _T('\0')) 149 | { 150 | if (d->__last_nonopt > d->optind) 151 | d->__last_nonopt = d->optind; 152 | if (d->__first_nonopt > d->optind) 153 | d->__first_nonopt = d->optind; 154 | 155 | if (d->__ordering == PERMUTE) 156 | { 157 | if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind) 158 | exchange ((TCHAR **) argv, d); 159 | else if (d->__last_nonopt != d->optind) 160 | d->__first_nonopt = d->optind; 161 | 162 | while (d->optind < argc && (argv[d->optind][0] != _T('-') || argv[d->optind][1] == _T('\0'))) 163 | d->optind++; 164 | d->__last_nonopt = d->optind; 165 | } 166 | 167 | if (d->optind != argc && !_tcscmp(argv[d->optind], _T("--"))) 168 | { 169 | d->optind++; 170 | 171 | if (d->__first_nonopt != d->__last_nonopt && d->__last_nonopt != d->optind) 172 | exchange ((TCHAR **) argv, d); 173 | else if (d->__first_nonopt == d->__last_nonopt) 174 | d->__first_nonopt = d->optind; 175 | d->__last_nonopt = argc; 176 | 177 | d->optind = argc; 178 | } 179 | 180 | if (d->optind == argc) 181 | { 182 | if (d->__first_nonopt != d->__last_nonopt) 183 | d->optind = d->__first_nonopt; 184 | return -1; 185 | } 186 | 187 | if ((argv[d->optind][0] != _T('-') || argv[d->optind][1] == _T('\0'))) 188 | { 189 | if (d->__ordering == REQUIRE_ORDER) 190 | return -1; 191 | d->optarg = argv[d->optind++]; 192 | return 1; 193 | } 194 | 195 | d->__nextchar = (argv[d->optind] + 1 + (longopts != NULL && argv[d->optind][1] == _T('-'))); 196 | } 197 | 198 | if (longopts != NULL && (argv[d->optind][1] == _T('-') || (long_only && (argv[d->optind][2] || !_tcschr(optstring, argv[d->optind][1]))))) 199 | { 200 | TCHAR *nameend; 201 | const struct option *p; 202 | const struct option *pfound = NULL; 203 | int exact = 0; 204 | int ambig = 0; 205 | int indfound = -1; 206 | int option_index; 207 | 208 | for (nameend = d->__nextchar; *nameend && *nameend != _T('='); nameend++); 209 | 210 | for (p = longopts, option_index = 0; p->name; p++, option_index++) 211 | if (!_tcsncmp(p->name, d->__nextchar, nameend - d->__nextchar)) 212 | { 213 | if ((unsigned int)(nameend - d->__nextchar) == (unsigned int)_tcslen(p->name)) 214 | { 215 | pfound = p; 216 | indfound = option_index; 217 | exact = 1; 218 | break; 219 | } 220 | else if (pfound == NULL) 221 | { 222 | pfound = p; 223 | indfound = option_index; 224 | } 225 | else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val) 226 | ambig = 1; 227 | } 228 | 229 | if (ambig && !exact) 230 | { 231 | if (print_errors) 232 | { 233 | _ftprintf(stderr, _T("%s: option '%s' is ambiguous\n"), 234 | argv[0], argv[d->optind]); 235 | } 236 | d->__nextchar += _tcslen(d->__nextchar); 237 | d->optind++; 238 | d->optopt = 0; 239 | return _T('?'); 240 | } 241 | 242 | if (pfound != NULL) 243 | { 244 | option_index = indfound; 245 | d->optind++; 246 | if (*nameend) 247 | { 248 | if (pfound->has_arg) 249 | d->optarg = nameend + 1; 250 | else 251 | { 252 | if (print_errors) 253 | { 254 | if (argv[d->optind - 1][1] == _T('-')) 255 | { 256 | _ftprintf(stderr, _T("%s: option '--%s' doesn't allow an argument\n"),argv[0], pfound->name); 257 | } 258 | else 259 | { 260 | _ftprintf(stderr, _T("%s: option '%c%s' doesn't allow an argument\n"),argv[0], argv[d->optind - 1][0],pfound->name); 261 | } 262 | 263 | } 264 | 265 | d->__nextchar += _tcslen(d->__nextchar); 266 | 267 | d->optopt = pfound->val; 268 | return _T('?'); 269 | } 270 | } 271 | else if (pfound->has_arg == 1) 272 | { 273 | if (d->optind < argc) 274 | d->optarg = argv[d->optind++]; 275 | else 276 | { 277 | if (print_errors) 278 | { 279 | _ftprintf(stderr,_T("%s: option '--%s' requires an argument\n"),argv[0], pfound->name); 280 | } 281 | d->__nextchar += _tcslen(d->__nextchar); 282 | d->optopt = pfound->val; 283 | return optstring[0] == _T(':') ? _T(':') : _T('?'); 284 | } 285 | } 286 | d->__nextchar += _tcslen(d->__nextchar); 287 | if (longind != NULL) 288 | *longind = option_index; 289 | if (pfound->flag) 290 | { 291 | *(pfound->flag) = pfound->val; 292 | return 0; 293 | } 294 | return pfound->val; 295 | } 296 | 297 | if (!long_only || argv[d->optind][1] == _T('-') || _tcschr(optstring, *d->__nextchar) == NULL) 298 | { 299 | if (print_errors) 300 | { 301 | if (argv[d->optind][1] == _T('-')) 302 | { 303 | /* --option */ 304 | _ftprintf(stderr, _T("%s: unrecognized option '--%s'\n"),argv[0], d->__nextchar); 305 | } 306 | else 307 | { 308 | /* +option or -option */ 309 | _ftprintf(stderr, _T("%s: unrecognized option '%c%s'\n"),argv[0], argv[d->optind][0], d->__nextchar); 310 | } 311 | } 312 | d->__nextchar = (TCHAR *)_T(""); 313 | d->optind++; 314 | d->optopt = 0; 315 | return _T('?'); 316 | } 317 | } 318 | 319 | { 320 | TCHAR c = *d->__nextchar++; 321 | TCHAR *temp = (TCHAR*)_tcschr(optstring, c); 322 | 323 | if (*d->__nextchar == _T('\0')) 324 | ++d->optind; 325 | 326 | if (temp == NULL || c == _T(':') || c == _T(';')) 327 | { 328 | if (print_errors) 329 | { 330 | _ftprintf(stderr, _T("%s: invalid option -- '%c'\n"), argv[0], c); 331 | } 332 | d->optopt = c; 333 | return _T('?'); 334 | } 335 | if (temp[0] == _T('W') && temp[1] == _T(';')) 336 | { 337 | TCHAR *nameend; 338 | const struct option *p; 339 | const struct option *pfound = NULL; 340 | int exact = 0; 341 | int ambig = 0; 342 | int indfound = 0; 343 | int option_index; 344 | 345 | if (*d->__nextchar != _T('\0')) 346 | { 347 | d->optarg = d->__nextchar; 348 | d->optind++; 349 | } 350 | else if (d->optind == argc) 351 | { 352 | if (print_errors) 353 | { 354 | _ftprintf(stderr, 355 | _T("%s: option requires an argument -- '%c'\n"), 356 | argv[0], c); 357 | } 358 | d->optopt = c; 359 | if (optstring[0] == _T(':')) 360 | c = _T(':'); 361 | else 362 | c = _T('?'); 363 | return c; 364 | } 365 | else 366 | d->optarg = argv[d->optind++]; 367 | 368 | for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != _T('='); nameend++); 369 | 370 | for (p = longopts, option_index = 0; p->name; p++, option_index++) 371 | if (!_tcsncmp(p->name, d->__nextchar, nameend - d->__nextchar)) 372 | { 373 | if ((unsigned int) (nameend - d->__nextchar) == _tcslen(p->name)) 374 | { 375 | pfound = p; 376 | indfound = option_index; 377 | exact = 1; 378 | break; 379 | } 380 | else if (pfound == NULL) 381 | { 382 | pfound = p; 383 | indfound = option_index; 384 | } 385 | else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag || pfound->val != p->val) 386 | ambig = 1; 387 | } 388 | if (ambig && !exact) 389 | { 390 | if (print_errors) 391 | { 392 | _ftprintf(stderr, _T("%s: option '-W %s' is ambiguous\n"), 393 | argv[0], d->optarg); 394 | } 395 | d->__nextchar += _tcslen(d->__nextchar); 396 | d->optind++; 397 | return _T('?'); 398 | } 399 | if (pfound != NULL) 400 | { 401 | option_index = indfound; 402 | if (*nameend) 403 | { 404 | if (pfound->has_arg) 405 | d->optarg = nameend + 1; 406 | else 407 | { 408 | if (print_errors) 409 | { 410 | _ftprintf(stderr, _T("\ 411 | %s: option '-W %s' doesn't allow an argument\n"), 412 | argv[0], pfound->name); 413 | } 414 | 415 | d->__nextchar += _tcslen(d->__nextchar); 416 | return _T('?'); 417 | } 418 | } 419 | else if (pfound->has_arg == 1) 420 | { 421 | if (d->optind < argc) 422 | d->optarg = argv[d->optind++]; 423 | else 424 | { 425 | if (print_errors) 426 | { 427 | _ftprintf(stderr, _T("\ 428 | %s: option '-W %s' requires an argument\n"), 429 | argv[0], pfound->name); 430 | } 431 | d->__nextchar += _tcslen(d->__nextchar); 432 | return optstring[0] == _T(':') ? _T(':') : _T('?'); 433 | } 434 | } 435 | else 436 | d->optarg = NULL; 437 | d->__nextchar += _tcslen(d->__nextchar); 438 | if (longind != NULL) 439 | *longind = option_index; 440 | if (pfound->flag) 441 | { 442 | *(pfound->flag) = pfound->val; 443 | return 0; 444 | } 445 | return pfound->val; 446 | } 447 | d->__nextchar = NULL; 448 | return _T('W'); 449 | } 450 | if (temp[1] == _T(':')) 451 | { 452 | if (temp[2] == _T(':')) 453 | { 454 | if (*d->__nextchar != _T('\0')) 455 | { 456 | d->optarg = d->__nextchar; 457 | d->optind++; 458 | } 459 | else 460 | d->optarg = NULL; 461 | d->__nextchar = NULL; 462 | } 463 | else 464 | { 465 | if (*d->__nextchar != _T('\0')) 466 | { 467 | d->optarg = d->__nextchar; 468 | d->optind++; 469 | } 470 | else if (d->optind == argc) 471 | { 472 | if (print_errors) 473 | { 474 | _ftprintf(stderr, 475 | _T("%s: option requires an argument -- '%c'\n"), 476 | argv[0], c); 477 | } 478 | d->optopt = c; 479 | if (optstring[0] == _T(':')) 480 | c = _T(':'); 481 | else 482 | c = _T('?'); 483 | } 484 | else 485 | d->optarg = argv[d->optind++]; 486 | d->__nextchar = NULL; 487 | } 488 | } 489 | return c; 490 | } 491 | } 492 | 493 | int _getopt_internal (int argc, TCHAR *const *argv, const TCHAR *optstring, const struct option *longopts, int *longind, int long_only, int posixly_correct) 494 | { 495 | int result; 496 | getopt_data.optind = optind; 497 | getopt_data.opterr = opterr; 498 | result = _getopt_internal_r (argc, argv, optstring, longopts,longind, long_only, &getopt_data,posixly_correct); 499 | optind = getopt_data.optind; 500 | optarg = getopt_data.optarg; 501 | optopt = getopt_data.optopt; 502 | return result; 503 | } 504 | 505 | int getopt (int argc, TCHAR *const *argv, const TCHAR *optstring) 506 | { 507 | return _getopt_internal (argc, argv, optstring, (const struct option *) 0, (int *) 0, 0, 0); 508 | } 509 | 510 | int getopt_long (int argc, TCHAR *const *argv, const TCHAR *options, const struct option *long_options, int *opt_index) 511 | { 512 | return _getopt_internal (argc, argv, options, long_options, opt_index, 0, 0); 513 | } 514 | 515 | int _getopt_long_r (int argc, TCHAR *const *argv, const TCHAR *options, const struct option *long_options, int *opt_index, struct _getopt_data *d) 516 | { 517 | return _getopt_internal_r (argc, argv, options, long_options, opt_index,0, d, 0); 518 | } 519 | 520 | int getopt_long_only (int argc, TCHAR *const *argv, const TCHAR *options, const struct option *long_options, int *opt_index) 521 | { 522 | return _getopt_internal (argc, argv, options, long_options, opt_index, 1, 0); 523 | } 524 | 525 | int _getopt_long_only_r (int argc, TCHAR *const *argv, const TCHAR *options, const struct option *long_options, int *opt_index, struct _getopt_data *d) 526 | { 527 | return _getopt_internal_r (argc, argv, options, long_options, opt_index, 1, d, 0); 528 | } 529 | 530 | #endif 531 | -------------------------------------------------------------------------------- /src/getopt.h: -------------------------------------------------------------------------------- 1 | #ifdef _WIN32 2 | 3 | /* Getopt for Microsoft C 4 | This code is a modification of the Free Software Foundation, Inc. 5 | Getopt library for parsing command line argument the purpose was 6 | to provide a Microsoft Visual C friendly derivative. This code 7 | provides functionality for both Unicode and Multibyte builds. 8 | 9 | Date: 02/03/2011 - Ludvik Jerabek - Initial Release 10 | Version: 1.0 11 | Comment: Supports getopt, getopt_long, and getopt_long_only 12 | and POSIXLY_CORRECT environment flag 13 | License: LGPL 14 | 15 | Revisions: 16 | 17 | 02/03/2011 - Ludvik Jerabek - Initial Release 18 | 02/20/2011 - Ludvik Jerabek - Fixed compiler warnings at Level 4 19 | 07/05/2011 - Ludvik Jerabek - Added no_argument, required_argument, optional_argument defs 20 | 08/03/2011 - Ludvik Jerabek - Fixed non-argument runtime bug which caused runtime exception 21 | 08/09/2011 - Ludvik Jerabek - Added code to export functions for DLL and LIB 22 | 23 | **DISCLAIMER** 24 | THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 25 | EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE 26 | IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 27 | PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE 28 | EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT 29 | APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY 30 | DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY 31 | USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST 32 | PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON 33 | YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE 34 | EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 35 | */ 36 | #ifndef __GETOPT_H_ 37 | #define __GETOPT_H_ 38 | 39 | #define STATIC_GETOPT 40 | 41 | #ifdef _GETOPT_API 42 | #undef _GETOPT_API 43 | #endif 44 | 45 | #if defined(EXPORTS_GETOPT) && defined(STATIC_GETOPT) 46 | #error "The preprocessor definitions of EXPORTS_GETOPT and STATIC_GETOPT can only be used individually" 47 | #elif defined(STATIC_GETOPT) 48 | #define _GETOPT_API 49 | #elif defined(EXPORTS_GETOPT) 50 | #pragma message("Exporting getopt library") 51 | #define _GETOPT_API __declspec(dllexport) 52 | #else 53 | #pragma message("Importing getopt library") 54 | #define _GETOPT_API __declspec(dllimport) 55 | #endif 56 | 57 | 58 | #include 59 | 60 | // Standard GNU options 61 | #define null_argument 0 /*Argument Null*/ 62 | #define no_argument 0 /*Argument Switch Only*/ 63 | #define required_argument 1 /*Argument Required*/ 64 | #define optional_argument 2 /*Argument Optional*/ 65 | 66 | // Change behavior for C\C++ 67 | #ifdef __cplusplus 68 | #define _BEGIN_EXTERN_C extern "C" { 69 | #define _END_EXTERN_C } 70 | #define _GETOPT_THROW throw() 71 | #else 72 | #define _BEGIN_EXTERN_C 73 | #define _END_EXTERN_C 74 | #define _GETOPT_THROW 75 | #endif 76 | 77 | _BEGIN_EXTERN_C 78 | 79 | extern _GETOPT_API TCHAR *optarg; 80 | extern _GETOPT_API int optind; 81 | extern _GETOPT_API int opterr; 82 | extern _GETOPT_API int optopt; 83 | 84 | struct option 85 | { 86 | const TCHAR* name; 87 | int has_arg; 88 | int *flag; 89 | TCHAR val; 90 | }; 91 | 92 | extern _GETOPT_API int getopt(int argc, TCHAR *const *argv, const TCHAR *optstring) _GETOPT_THROW; 93 | extern _GETOPT_API int getopt_long(int ___argc, TCHAR *const *___argv, const TCHAR *__shortopts, const struct option *__longopts, int *__longind) _GETOPT_THROW; 94 | extern _GETOPT_API int getopt_long_only(int ___argc, TCHAR *const *___argv, const TCHAR *__shortopts, const struct option *__longopts, int *__longind) _GETOPT_THROW; 95 | _END_EXTERN_C 96 | 97 | // Undefine so the macros are not included 98 | #undef _BEGIN_EXTERN_C 99 | #undef _END_EXTERN_C 100 | #undef _GETOPT_THROW 101 | #undef _GETOPT_API 102 | 103 | #endif // __GETOPT_H_ 104 | 105 | #endif 106 | -------------------------------------------------------------------------------- /src/keys.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012 by naehrwert 3 | * This file is released under the GPLv2. 4 | */ 5 | 6 | //dummy eid_root_key 7 | u8 arcade_root_key[] = 8 | { 9 | 0x35, 0x9F, 0x59, 0xBB, 0x8C, 0x25, 0x6B, 0x91, 0x09, 0x3A, 0x92, 0x00, 0x72, 0x03, 0xAB, 0xB3, 10 | 0x3B, 0xAD, 0xF5, 0xAC, 0x09, 0xA0, 0xDC, 0x00, 0x58, 0x59, 0xD6, 0xF1, 0x59, 0xC4, 0xF5, 0x4F 11 | }; 12 | 13 | //dummy eid_root_iv 14 | u8 arcade_root_iv[] = 15 | { 16 | 0x92, 0x92, 0x14, 0xD8, 0xFC, 0xCB, 0x4C, 0xE7, 0x09, 0x9A, 0xCE, 0xBD, 0xFC, 0x66, 0x12, 0xB9 17 | }; 18 | 19 | //sb individual seed 0 20 | u8 ata_data_seed[] = 21 | { 22 | 0xD9, 0x2D, 0x65, 0xDB, 0x05, 0x7D, 0x49, 0xE1, 0xA6, 0x6F, 0x22, 0x74, 0xB8, 0xBA, 0xC5, 0x08, 23 | 0x83, 0x84, 0x4E, 0xD7, 0x56, 0xCA, 0x79, 0x51, 0x63, 0x62, 0xEA, 0x8A, 0xDA, 0xC6, 0x03, 0x26 24 | }; 25 | 26 | //sb individual seed 1 27 | u8 ata_tweak_seed[] = 28 | { 29 | 0xC3, 0xB3, 0xB5, 0xAA, 0xCC, 0x74, 0xCD, 0x6A, 0x48, 0xEF, 0xAB, 0xF4, 0x4D, 0xCD, 0xF1, 0x6E, 30 | 0x37, 0x9F, 0x55, 0xF5, 0x77, 0x7D, 0x09, 0xFB, 0xEE, 0xDE, 0x07, 0x05, 0x8E, 0x94, 0xBE, 0x08 31 | }; 32 | 33 | //encdec data key seed 34 | u8 encdec_data_seed[] = 35 | { 36 | 0xE2, 0xD0, 0x5D, 0x40, 0x71, 0x94, 0x5B, 0x01, 0xC3, 0x6D, 0x51, 0x51, 0xE8, 0x8C, 0xB8, 0x33, 37 | 0x4A, 0xAA, 0x29, 0x80, 0x81, 0xD8, 0xC4, 0x4F, 0x18, 0x5D, 0xC6, 0x60, 0xED, 0x57, 0x56, 0x86 38 | }; 39 | 40 | //encdec tweak key seed 41 | u8 encdec_tweak_seed[] = 42 | { 43 | 0x02, 0x08, 0x32, 0x92, 0xC3, 0x05, 0xD5, 0x38, 0xBC, 0x50, 0xE6, 0x99, 0x71, 0x0C, 0x0A, 0x3E, 44 | 0x55, 0xF5, 0x1C, 0xBA, 0xA5, 0x35, 0xA3, 0x80, 0x30, 0xB6, 0x7F, 0x79, 0xC9, 0x05, 0xBD, 0xA3 45 | }; 46 | 47 | //ata data/tweak dummy key seed 48 | u8 ata_arcade_seed[] = 49 | { 50 | 0xDA, 0x73, 0xED, 0x90, 0x20, 0x91, 0x8F, 0x4C, 0x0A, 0x70, 0x3D, 0xCC, 0xF8, 0x90, 0x61, 0x7B, 51 | 0xFF, 0xD2, 0x5E, 0x33, 0x40, 0x00, 0x91, 0x09, 0x58, 0x3C, 0x64, 0x3D, 0xF4, 0xA2, 0x13, 0x24 52 | }; 53 | 54 | //encdec data dummy key seed 55 | u8 encdec_arcade_data_seed[] = 56 | { 57 | 0xD2, 0xBC, 0xFF, 0x74, 0x2D, 0x57, 0x1A, 0x80, 0xDF, 0xEE, 0x5E, 0x24, 0x96, 0xD1, 0x9C, 0x3A, 58 | 0x6F, 0x25, 0xFA, 0x0F, 0xC6, 0x97, 0x64, 0xCA, 0xC2, 0x0F, 0x42, 0x69, 0xEB, 0x54, 0x0F, 0xD8 59 | }; 60 | 61 | //encdec tweak dummy key seed 62 | u8 encdec_arcade_tweak_seed[] = 63 | { 64 | 0xC1, 0x9C, 0x7F, 0x98, 0x7E, 0xDB, 0x6E, 0x24, 0x4B, 0x07, 0xBE, 0xDE, 0xFA, 0x1E, 0x6C, 0xC9, 65 | 0xF0, 0x85, 0x24, 0xD9, 0x8C, 0x05, 0x65, 0x4C, 0xC7, 0x42, 0x14, 0x1E, 0x01, 0xF8, 0x23, 0xE1 66 | }; 67 | -------------------------------------------------------------------------------- /src/kgen.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012 by naehrwert 3 | * This file is released under the GPLv2. 4 | */ 5 | 6 | #include "types.h" 7 | #include "aes.h" 8 | 9 | void generate_ata_keys(u8 *eid_root_key, u8 *eid_root_iv, u8 *ata_data_seed, u8 *ata_tweak_seed, u8 *data_key_dst, u8 *tweak_key_dst) 10 | { 11 | aes_context aes_ctxt; 12 | u8 iv[0x10]; 13 | 14 | //Generate ATA data key. 15 | aes_setkey_enc(&aes_ctxt, eid_root_key, 0x100); 16 | memcpy(iv, eid_root_iv, 0x10); 17 | aes_crypt_cbc(&aes_ctxt, AES_ENCRYPT, 0x20, iv, ata_data_seed, data_key_dst); 18 | 19 | //Generate ATA tweak key. 20 | aes_setkey_enc(&aes_ctxt, eid_root_key, 0x100); 21 | memcpy(iv, eid_root_iv, 0x10); 22 | aes_crypt_cbc(&aes_ctxt, AES_ENCRYPT, 0x20, iv, ata_tweak_seed, tweak_key_dst); 23 | } 24 | 25 | void generate_encdec_keys(u8 *eid_root_key, u8 *eid_root_iv, u8 *encdec_data_seed, u8 *encdec_tweak_seed, u8 *data_key_dst, u8 *tweak_key_dst) 26 | { 27 | aes_context aes_ctxt; 28 | u8 iv[0x10]; 29 | 30 | //Generate encdec_k1. 31 | aes_setkey_enc(&aes_ctxt, eid_root_key, 0x100); 32 | memcpy(iv, eid_root_iv, 0x10); 33 | aes_crypt_cbc(&aes_ctxt, AES_ENCRYPT, 0x20, iv, encdec_data_seed, data_key_dst); 34 | 35 | //Generate encdec_k3. 36 | aes_setkey_enc(&aes_ctxt, eid_root_key, 0x100); 37 | memcpy(iv, eid_root_iv, 0x10); 38 | aes_crypt_cbc(&aes_ctxt, AES_ENCRYPT, 0x20, iv, encdec_tweak_seed, tweak_key_dst); 39 | } 40 | -------------------------------------------------------------------------------- /src/kgen.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012 by naehrwert 3 | * This file is released under the GPLv2. 4 | */ 5 | 6 | #ifndef _KGEN_H_ 7 | #define _KGEN_H_ 8 | 9 | void generate_ata_keys(u8 *eid_root_key, u8 *eid_root_iv, u8 *ata_data_seed, u8 *ata_tweak_seed, u8 *data_key_dst, u8 *tweak_key_dst); 10 | void generate_encdec_keys(u8 *eid_root_key, u8 *eid_root_iv, u8 *encdec_data_seed, u8 *encdec_tweak_seed, u8 *data_key_dst, u8 *tweak_key_dst); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012 by naehrwert 3 | * This file is released under the GPLv2. 4 | */ 5 | #define _CRT_SECURE_NO_WARNINGS 6 | 7 | #include 8 | #include 9 | 10 | #include "util.h" 11 | #include "kgen.h" 12 | #include "aes_xts.h" 13 | #include "keys.h" 14 | #include 15 | 16 | #ifdef _WIN32 17 | #include 18 | #include "getopt.h" 19 | #else 20 | #include 21 | #include 22 | #endif 23 | 24 | 25 | /*! Size of one sector. */ 26 | #define SECTOR_SIZE 0x200 27 | #define BUFFER_SIZE 0x100000 28 | 29 | /*! Encrypt data. */ 30 | static BOOL do_encrypt = FALSE; 31 | /*! eflash or vflash sector. */ 32 | static BOOL is_vflash = FALSE; 33 | /*! Phat console type. */ 34 | static BOOL is_phat = FALSE; 35 | /*! Arcade console type. */ 36 | static BOOL is_arcade = FALSE; 37 | 38 | /*! Parameters. */ 39 | s8 *_start_sector = "0"; 40 | s8 *_num_sectors = NULL; 41 | 42 | /*! Input eid root key file. */ 43 | static s8 *_eid_root_key_file = NULL; 44 | /*! Input data file. */ 45 | static s8 *_file_in = NULL; 46 | /*! Output data file. */ 47 | static s8 *_file_out = NULL; 48 | 49 | /*! Shorter Versions of arg options. */ 50 | #define ARG_NULL no_argument 51 | #define ARG_NONE no_argument 52 | #define ARG_REQ required_argument 53 | #define ARG_OPT optional_argument 54 | 55 | static struct option options[] = 56 | { 57 | {"help", ARG_NONE, NULL, 'h'}, 58 | {"encrypt", ARG_NONE, NULL, 'e'}, 59 | {"vflash", ARG_NONE, NULL, 'v'}, 60 | {"phat", ARG_NONE, NULL, 'p'}, 61 | {"arcade", ARG_NONE, NULL, 'a'}, 62 | {"start-sector", ARG_REQ, NULL, 's'}, 63 | {"num-sectors", ARG_REQ, NULL, 'n'}, 64 | {NULL, ARG_NULL, NULL, 0} 65 | }; 66 | 67 | 68 | void print_help(char **argv) 69 | { 70 | printf("PlayStation 3 ENCDEC emulator 0.1.0 \n"); 71 | printf("usage: %s [-h] [-e] [-v] [-p] [-a] [-s START_SECTOR] [-n NUM_SECTORS]\n", argv[0] ); 72 | printf(" [eid_root_key_file] [sector_file] \n\n"); 73 | printf("positional arguments:\n"); 74 | printf(" eid_root_key_file\n"); 75 | printf(" sector_file\n"); 76 | printf(" out_file\n\n"); 77 | printf("optional arguments:\n"); 78 | printf(" -h, --help show this help message and exit\n"); 79 | printf(" -e, --encrypt encrypt data instead of decrypt\n"); 80 | printf(" -v, --vflash vflash/eflash region\n"); 81 | printf(" -p, --phat phat console\n"); 82 | printf(" -a, --arcade arcade console\n"); 83 | printf(" -s START_SECTOR, --start-sector START_SECTOR\n"); 84 | printf(" sector start index, used for crypto only\n"); 85 | printf(" -n NUM_SECTORS, --num-sectors NUM_SECTORS\n"); 86 | printf(" sector count\n"); 87 | 88 | exit(1); 89 | } 90 | 91 | static void parse_args(int argc, char **argv) 92 | { 93 | char c; 94 | 95 | while((c = getopt_long(argc, argv, "h?evpas:n:", options, NULL)) != -1) 96 | { 97 | switch(c) 98 | { 99 | case 'h': 100 | print_help(argv); 101 | break; 102 | case 'e': 103 | do_encrypt = TRUE; 104 | break; 105 | case 'v': 106 | is_vflash = TRUE; 107 | break; 108 | case 'p': 109 | is_phat = TRUE; 110 | break; 111 | case 'a': 112 | is_arcade = TRUE; 113 | break; 114 | case 's': 115 | _start_sector = optarg; 116 | break; 117 | case 'n': 118 | _num_sectors = optarg; 119 | break; 120 | case '?': 121 | print_help(argv); 122 | break; 123 | } 124 | } 125 | 126 | // Get positional arguments. 127 | if(argc - optind < 2) 128 | { 129 | printf("[*] Error: incorrect arguments!\n"); 130 | print_help(argv); 131 | } 132 | _eid_root_key_file = argv[optind]; 133 | _file_in = argv[optind + 1]; 134 | 135 | if(argc - optind == 3) 136 | _file_out = argv[optind + 2]; 137 | else 138 | _file_out = "out.bin"; 139 | } 140 | 141 | /*! Swap u16 endianness. */ 142 | static void _es16_buffer(u8 *buf, u32 length) 143 | { 144 | u16 *ptr = (u16 *)buf; 145 | u32 i; 146 | 147 | for(i = 0; i < length/2; i++) 148 | ptr[i] = _ES16(ptr[i]); 149 | } 150 | 151 | /*! Decrypt sectors. */ 152 | void decrypt_all_sectors(const s8 *out_file, const s8 *in_file, u64 start_sector, u64 num_sectors, u8 *ata_k1, u8 *ata_k2, u8 *edec_k1, u8 *edec_k2, BOOL is_phat, BOOL is_vflash) 153 | { 154 | FILE *in; 155 | FILE *out; 156 | aes_xts_ctxt_t xts_ctxt; 157 | aes_context aes_ctxt; 158 | u64 i; 159 | u64 chunk_size; 160 | u64 position = 0; 161 | u64 sectors_to_read = num_sectors; 162 | u8 *zero_iv = (u8 *)malloc(sizeof(u8) * 0x10); 163 | u8 *buffer = (u8 *)malloc(sizeof(u8) * BUFFER_SIZE); 164 | 165 | 166 | while (sectors_to_read > 0) 167 | { 168 | //Read file to buffer. 169 | in = fopen(in_file, "rb"); 170 | 171 | _fseeki64(in, position, SEEK_SET); 172 | if (sectors_to_read >= (BUFFER_SIZE / SECTOR_SIZE)) 173 | chunk_size = BUFFER_SIZE; 174 | else 175 | chunk_size = (sectors_to_read * SECTOR_SIZE); 176 | 177 | fread(buffer, (size_t)chunk_size, 1, in); 178 | fclose(in); 179 | 180 | //Decrypt buffer. 181 | for(i = 0; i < (chunk_size / SECTOR_SIZE); i++) 182 | { 183 | //Decrypt sector. 184 | if (is_vflash == TRUE) 185 | { 186 | if (is_phat == TRUE) 187 | { 188 | //Set key for AES-CBC 189 | aes_setkey_dec(&aes_ctxt, edec_k1, 128); 190 | //Decrypt CBC sector. 191 | memset(zero_iv, 0, 0x10); 192 | aes_crypt_cbc(&aes_ctxt, AES_DECRYPT, SECTOR_SIZE, zero_iv, (buffer + (SECTOR_SIZE * i)), buffer + (SECTOR_SIZE * i)); 193 | //XOR initial block in sector with sector index value. 194 | buffer[(SECTOR_SIZE * i)+0x8] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 56 & 0xFF); 195 | buffer[(SECTOR_SIZE * i)+0x9] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 48 & 0xFF); 196 | buffer[(SECTOR_SIZE * i)+0xA] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 40 & 0xFF); 197 | buffer[(SECTOR_SIZE * i)+0xB] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 32 & 0xFF); 198 | buffer[(SECTOR_SIZE * i)+0xC] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 24 & 0xFF); 199 | buffer[(SECTOR_SIZE * i)+0xD] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 16 & 0xFF); 200 | buffer[(SECTOR_SIZE * i)+0xE] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 8 & 0xFF); 201 | buffer[(SECTOR_SIZE * i)+0xF] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) & 0xFF); 202 | } 203 | else 204 | { 205 | //Init AES-XTS context. 206 | aes_xts_init(&xts_ctxt, AES_DECRYPT, edec_k1, edec_k2, 128); 207 | //Decrypt XTS sector. 208 | aes_xts_crypt(&xts_ctxt, (u64)((position / SECTOR_SIZE) + i + start_sector), SECTOR_SIZE, (buffer + (SECTOR_SIZE * i)), buffer + (SECTOR_SIZE * i)); 209 | } 210 | } 211 | else 212 | { 213 | if (is_phat == TRUE) 214 | { 215 | //Swap endian for ata only. 216 | _es16_buffer(buffer + (SECTOR_SIZE * i), SECTOR_SIZE); 217 | //Set key for AES-CBC 218 | aes_setkey_dec(&aes_ctxt, ata_k1, 192); 219 | //Decrypt CBC sector. 220 | memset(zero_iv, 0, 0x10); 221 | aes_crypt_cbc(&aes_ctxt, AES_DECRYPT, SECTOR_SIZE, zero_iv, (buffer + (SECTOR_SIZE * i)), buffer + (SECTOR_SIZE * i)); 222 | } 223 | else 224 | { 225 | //Swap endian for ata only. 226 | _es16_buffer(buffer + (SECTOR_SIZE * i), SECTOR_SIZE); 227 | //Init AES-XTS context. 228 | aes_xts_init(&xts_ctxt, AES_DECRYPT, ata_k1, ata_k2, 128); 229 | //Decrypt XTS sector. 230 | aes_xts_crypt(&xts_ctxt, (u64)((position / SECTOR_SIZE) + i + start_sector), SECTOR_SIZE, (buffer + (SECTOR_SIZE * i)), buffer + (SECTOR_SIZE * i)); 231 | } 232 | } 233 | } 234 | 235 | //Write buffer to file 236 | out = fopen(out_file, "r+b"); 237 | _fseeki64(out, position, SEEK_SET); 238 | fwrite(buffer, (size_t)chunk_size, 1, out); 239 | fclose(out); 240 | 241 | //Updating vars. 242 | position += chunk_size; 243 | sectors_to_read -= (u64)(chunk_size / SECTOR_SIZE); 244 | } 245 | } 246 | 247 | /*! Encrypt sectors. */ 248 | void encrypt_all_sectors(const s8 *out_file, const s8 *in_file, u64 start_sector, u64 num_sectors, u8 *ata_k1, u8 *ata_k2, u8 *edec_k1, u8 *edec_k2, BOOL is_phat, BOOL is_vflash) 249 | { 250 | FILE *in; 251 | FILE *out; 252 | aes_xts_ctxt_t xts_ctxt; 253 | aes_context aes_ctxt; 254 | u64 i; 255 | u64 chunk_size; 256 | u64 position = 0; 257 | u64 sectors_to_read = num_sectors; 258 | u8 *zero_iv = (u8 *)malloc(sizeof(u8) * 0x10); 259 | u8 *buffer = (u8 *)malloc(sizeof(u8) * BUFFER_SIZE); 260 | 261 | 262 | while (sectors_to_read > 0) 263 | { 264 | //Read file to buffer. 265 | in = fopen(in_file, "rb"); 266 | _fseeki64(in, position, SEEK_SET); 267 | 268 | if (sectors_to_read >= (BUFFER_SIZE / SECTOR_SIZE)) 269 | chunk_size = BUFFER_SIZE; 270 | else 271 | chunk_size = (sectors_to_read * SECTOR_SIZE); 272 | 273 | fread(buffer, (size_t)chunk_size, 1, in); 274 | fclose(in); 275 | 276 | //Encrypt buffer. 277 | for(i = 0; i < (chunk_size / SECTOR_SIZE); i++) 278 | { 279 | //Encrypt sector. 280 | if (is_vflash == TRUE) 281 | { 282 | if (is_phat == TRUE) 283 | { 284 | //XOR initial block in sector with sector index value. 285 | buffer[(SECTOR_SIZE * i)+0x8] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 56 & 0xFF); 286 | buffer[(SECTOR_SIZE * i)+0x9] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 48 & 0xFF); 287 | buffer[(SECTOR_SIZE * i)+0xA] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 40 & 0xFF); 288 | buffer[(SECTOR_SIZE * i)+0xB] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 32 & 0xFF); 289 | buffer[(SECTOR_SIZE * i)+0xC] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 24 & 0xFF); 290 | buffer[(SECTOR_SIZE * i)+0xD] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 16 & 0xFF); 291 | buffer[(SECTOR_SIZE * i)+0xE] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) >> 8 & 0xFF); 292 | buffer[(SECTOR_SIZE * i)+0xF] ^= ((u64)((position / SECTOR_SIZE)+start_sector + i) & 0xFF); 293 | //Set key for AES-CBC 294 | aes_setkey_enc(&aes_ctxt, edec_k1, 128); 295 | //Encrypt CBC sector. 296 | memset(zero_iv, 0, 0x10); 297 | aes_crypt_cbc(&aes_ctxt, AES_ENCRYPT, SECTOR_SIZE, zero_iv, (buffer + (SECTOR_SIZE * i)), buffer + (SECTOR_SIZE * i)); 298 | } 299 | else 300 | { 301 | //Init AES-XTS context. 302 | aes_xts_init(&xts_ctxt, AES_ENCRYPT, edec_k1, edec_k2, 128); 303 | //Encrypt XTS sector. 304 | aes_xts_crypt(&xts_ctxt, (u64)((position / SECTOR_SIZE) + i + start_sector), SECTOR_SIZE, (buffer + (SECTOR_SIZE * i)), buffer + (SECTOR_SIZE * i)); 305 | } 306 | } 307 | else 308 | { 309 | if (is_phat == TRUE) 310 | { 311 | //Set key for AES-CBC 312 | aes_setkey_enc(&aes_ctxt, ata_k1, 192); 313 | //Encrypt CBC sector. 314 | memset(zero_iv, 0, 0x10); 315 | aes_crypt_cbc(&aes_ctxt, AES_ENCRYPT, SECTOR_SIZE, zero_iv, (buffer + (SECTOR_SIZE * i)), buffer + (SECTOR_SIZE * i)); 316 | //Swap endian for ata only. 317 | _es16_buffer(buffer + (SECTOR_SIZE * i), SECTOR_SIZE); 318 | } 319 | else 320 | { 321 | //Init AES-XTS context. 322 | aes_xts_init(&xts_ctxt, AES_ENCRYPT, ata_k1, ata_k2, 128); 323 | //Encrypt XTS sector. 324 | aes_xts_crypt(&xts_ctxt, (u64)((position / SECTOR_SIZE) + i + start_sector), SECTOR_SIZE, (buffer + (SECTOR_SIZE * i)), buffer + (SECTOR_SIZE * i)); 325 | //Swap endian for ata only. 326 | _es16_buffer(buffer + (SECTOR_SIZE * i), SECTOR_SIZE); 327 | } 328 | } 329 | } 330 | 331 | //Write buffer to file 332 | out = fopen(out_file, "r+b"); 333 | 334 | _fseeki64(out, position, SEEK_SET); 335 | fwrite(buffer, (size_t)chunk_size, 1, out); 336 | fclose(out); 337 | 338 | //Updating vars. 339 | position += chunk_size; 340 | sectors_to_read -= (u64)(chunk_size / SECTOR_SIZE); 341 | } 342 | } 343 | 344 | int main(int argc, char **argv) 345 | { 346 | time_t t1 = time(NULL); 347 | 348 | //Check for args. 349 | if(argc <= 1) 350 | print_help(argv); 351 | 352 | //Parse them. 353 | parse_args(argc, argv); 354 | 355 | //Check eid_root_key_file. 356 | FILE* eid_root_key_file = fopen(_eid_root_key_file, "rb"); 357 | if (eid_root_key_file == NULL) 358 | { 359 | printf("[*] Error: could not read eid_root_key_file!\n"); 360 | return -1; 361 | } 362 | else 363 | { 364 | _fseeki64(eid_root_key_file, 0, SEEK_END); 365 | u64 eid_root_key_file_size = _ftelli64(eid_root_key_file); 366 | 367 | fclose(eid_root_key_file); 368 | 369 | if (eid_root_key_file_size != 0x30) 370 | { 371 | printf("[*] Error: incorrect eid_root_key_file size!\n"); 372 | return -1; 373 | } 374 | } 375 | 376 | 377 | //Setup vars. 378 | u8 *eid_root_key = _read_buffer(_eid_root_key_file, NULL); 379 | u8 ata_k1[0x20], ata_k2[0x20], edec_k1[0x20], edec_k2[0x20]; 380 | memset(ata_k1, 0, 0x20); 381 | memset(ata_k2, 0, 0x20); 382 | memset(edec_k1, 0, 0x20); 383 | memset(edec_k2, 0, 0x20); 384 | 385 | //Generate keys. 386 | if (is_arcade) 387 | { 388 | generate_ata_keys(arcade_root_key, arcade_root_iv, ata_arcade_seed, ata_arcade_seed, ata_k1, ata_k2); 389 | generate_encdec_keys(arcade_root_key, arcade_root_iv, encdec_arcade_data_seed, encdec_arcade_tweak_seed, edec_k1, edec_k2); 390 | } 391 | else 392 | { 393 | generate_ata_keys(eid_root_key, eid_root_key + 0x20, ata_data_seed, ata_tweak_seed, ata_k1, ata_k2); 394 | generate_encdec_keys(eid_root_key, eid_root_key + 0x20, encdec_data_seed, encdec_tweak_seed, edec_k1, edec_k2); 395 | } 396 | 397 | //Print keys. 398 | _hexdump(stdout, "ATA-DATA-KEY ", 0, ata_k1, 0x20, 0); 399 | _hexdump(stdout, "ATA-TWEAK-KEY ", 0, ata_k2, 0x20, 0); 400 | _hexdump(stdout, "ENCDEC-DATA-KEY ", 0, edec_k1, 0x20, 0); 401 | _hexdump(stdout, "ENCDEC-TWEAK-KEY", 0, edec_k2, 0x20, 0); 402 | 403 | FILE* sector_file = fopen(_file_in, "rb"); 404 | if (sector_file == NULL) 405 | { 406 | printf("[*] Error: incorrect sector_file!\n"); 407 | return -1; 408 | } 409 | 410 | // Get sector file size. 411 | _fseeki64(sector_file, 0, SEEK_END); 412 | u64 sector_file_size = _ftelli64(sector_file); 413 | _fseeki64(sector_file, 0, SEEK_SET); 414 | fclose(sector_file); 415 | 416 | //Check sector file size. 417 | if (sector_file_size % 0x200 != 0) 418 | { 419 | printf("[*] Error: incorrect sector file size!\n"); 420 | return -1; 421 | } 422 | 423 | u64 start_sector = strtoll(_start_sector, NULL, 0); 424 | u64 num_sectors; 425 | 426 | //Setup sector count. 427 | if (_num_sectors == NULL) 428 | num_sectors = (sector_file_size / 0x200); 429 | else 430 | num_sectors = strtoll(_num_sectors, NULL, 0); 431 | 432 | //Check sector count. 433 | if (num_sectors > (sector_file_size / 0x200) ) 434 | { 435 | printf("[*] Error: num sectors too big!\n"); 436 | return -1; 437 | } 438 | 439 | //Check output file and create new one if not existed. 440 | FILE* out = fopen(_file_out, "rb"); 441 | if (out == NULL) 442 | { 443 | FILE* newfile = fopen(_file_out, "wb"); 444 | fclose(newfile); 445 | } 446 | else 447 | fclose(out); 448 | 449 | //Do the task. 450 | if (do_encrypt == FALSE) 451 | { 452 | decrypt_all_sectors(_file_out, _file_in, start_sector, num_sectors, ata_k1, ata_k2, edec_k1, edec_k2, is_phat, is_vflash); 453 | printf("[*] Sector file successfully decrypted.\n"); 454 | } 455 | else 456 | { 457 | encrypt_all_sectors(_file_out, _file_in, start_sector, num_sectors, ata_k1, ata_k2, edec_k1, edec_k2, is_phat, is_vflash); 458 | printf("[*] Sector file successfully encrypted.\n"); 459 | } 460 | 461 | time_t t2 = time(NULL); 462 | double spent = difftime(t2,t1); 463 | printf("[*] Time spent: %.f seconds\n", spent ); 464 | 465 | return 0; 466 | } 467 | -------------------------------------------------------------------------------- /src/types.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011-2012 by naehrwert 3 | * This file is released under the GPLv2. 4 | */ 5 | 6 | #pragma once 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | typedef char s8; 13 | typedef unsigned char u8; 14 | typedef short s16; 15 | typedef unsigned short u16; 16 | typedef int s32; 17 | typedef unsigned int u32; 18 | #if defined(_WIN32) && defined(_MSC_VER) 19 | typedef __int64 s64; 20 | typedef unsigned __int64 u64; 21 | #else 22 | typedef long long int s64; 23 | typedef unsigned long long int u64; 24 | #endif 25 | 26 | #define BOOL int 27 | #define TRUE 1 28 | #define FALSE 0 29 | 30 | 31 | #ifdef _WIN32 32 | #elif __CYGWIN__ 33 | #define _fseeki64 fseeko 34 | #define _ftelli64 ftello 35 | #else 36 | #define _fseeki64 fseeko64 37 | #define _ftelli64 ftello64 38 | #endif 39 | 40 | 41 | 42 | //Align. 43 | #define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) 44 | 45 | //Bits <-> bytes conversion. 46 | #define BITS2BYTES(x) ((x) / 8) 47 | #define BYTES2BITS(x) ((x) * 8) 48 | 49 | //Endian swap for u16. 50 | #define _ES16(val) \ 51 | ((u16)(((((u16)val) & 0xff00) >> 8) | \ 52 | ((((u16)val) & 0x00ff) << 8))) 53 | 54 | //Endian swap for u32. 55 | #define _ES32(val) \ 56 | ((u32)(((((u32)val) & 0xff000000) >> 24) | \ 57 | ((((u32)val) & 0x00ff0000) >> 8 ) | \ 58 | ((((u32)val) & 0x0000ff00) << 8 ) | \ 59 | ((((u32)val) & 0x000000ff) << 24))) 60 | 61 | //Endian swap for u64. 62 | #define _ES64(val) \ 63 | ((u64)(((((u64)val) & 0xff00000000000000ull) >> 56) | \ 64 | ((((u64)val) & 0x00ff000000000000ull) >> 40) | \ 65 | ((((u64)val) & 0x0000ff0000000000ull) >> 24) | \ 66 | ((((u64)val) & 0x000000ff00000000ull) >> 8 ) | \ 67 | ((((u64)val) & 0x00000000ff000000ull) << 8 ) | \ 68 | ((((u64)val) & 0x0000000000ff0000ull) << 24) | \ 69 | ((((u64)val) & 0x000000000000ff00ull) << 40) | \ 70 | ((((u64)val) & 0x00000000000000ffull) << 56))) 71 | 72 | #ifdef __cplusplus 73 | } 74 | #endif 75 | 76 | -------------------------------------------------------------------------------- /src/util.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011-2012 by naehrwert 3 | * This file is released under the GPLv2. 4 | */ 5 | 6 | #define _CRT_SECURE_NO_WARNINGS 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "types.h" 13 | #include "util.h" 14 | 15 | void _hexdump(FILE *fp, const char *name, u32 offset, u8 *buf, int len, BOOL print_addr) 16 | { 17 | int i, j, align = strlen(name) + 1; 18 | 19 | fprintf(fp, "%s ", name); 20 | if(print_addr == TRUE) 21 | fprintf(fp, "%08x: ", offset); 22 | for(i = 0; i < len; i++) 23 | { 24 | if(i % 16 == 0 && i != 0) 25 | { 26 | fprintf(fp, "\n"); 27 | for(j = 0; j < align; j++) 28 | putchar(' '); 29 | if(print_addr == TRUE) 30 | fprintf(fp, "%08X: ", offset + i); 31 | } 32 | fprintf(fp, "%02X ", buf[i]); 33 | } 34 | fprintf(fp, "\n"); 35 | } 36 | 37 | void _print_align(FILE *fp, const s8 *str, s32 align, s32 len) 38 | { 39 | s32 i, tmp; 40 | tmp = align - len; 41 | if(tmp < 0) 42 | tmp = 0; 43 | for(i = 0; i < tmp; i++) 44 | fputs(str, fp); 45 | } 46 | 47 | u8 *_read_buffer(const s8 *file, u32 *length) 48 | { 49 | FILE *fp; 50 | u32 size; 51 | 52 | if((fp = fopen(file, "rb")) == NULL) 53 | return NULL; 54 | 55 | fseek(fp, 0, SEEK_END); 56 | size = ftell(fp); 57 | fseek(fp, 0, SEEK_SET); 58 | 59 | u8 *buffer = (u8 *)malloc(sizeof(u8) * size); 60 | fread(buffer, sizeof(u8), size, fp); 61 | 62 | if(length != NULL) 63 | *length = size; 64 | 65 | fclose(fp); 66 | 67 | return buffer; 68 | } 69 | 70 | int _write_buffer(const s8 *file, u8 *buffer, u32 length) 71 | { 72 | FILE *fp; 73 | 74 | if((fp = fopen(file, "wb")) == NULL) 75 | return 0; 76 | 77 | /**/ 78 | while(length > 0) 79 | { 80 | u32 wrlen = 1024; 81 | if(length < 1024) 82 | wrlen = length; 83 | fwrite(buffer, sizeof(u8), wrlen, fp); 84 | length -= wrlen; 85 | buffer += 1024; 86 | } 87 | /**/ 88 | 89 | //fwrite(buffer, sizeof(u8), length, fp); 90 | 91 | fclose(fp); 92 | 93 | return 1; 94 | } 95 | -------------------------------------------------------------------------------- /src/util.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2011-2012 by naehrwert 3 | * This file is released under the GPLv2. 4 | */ 5 | 6 | #ifndef _UTIL_H_ 7 | #define _UTIL_H_ 8 | 9 | #include 10 | 11 | #include "types.h" 12 | 13 | /*! Utility functions. */ 14 | void _hexdump(FILE *fp, const char *name, u32 offset, u8 *buf, int len, BOOL print_addr); 15 | void _print_align(FILE *fp, const s8 *str, s32 align, s32 len); 16 | u8 *_read_buffer(const s8 *file, u32 *length); 17 | int _write_buffer(const s8 *file, u8 *buffer, u32 length); 18 | 19 | #endif 20 | --------------------------------------------------------------------------------