初始提交
This commit is contained in:
7
lib/playfair/CMakeLists.txt
Normal file
7
lib/playfair/CMakeLists.txt
Normal file
@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
aux_source_directory(. playfair_src)
|
||||
set(DIR_SRCS ${playfair_src})
|
||||
include_directories(.)
|
||||
add_library( playfair
|
||||
STATIC
|
||||
${DIR_SRCS})
|
637
lib/playfair/LICENSE.md
Normal file
637
lib/playfair/LICENSE.md
Normal file
@ -0,0 +1,637 @@
|
||||
|
||||
# GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 [Free Software Foundation, Inc.](http://fsf.org/)
|
||||
|
||||
Everyone is permitted to copy and distribute verbatim copies of this license
|
||||
document, but changing it is not allowed.
|
||||
|
||||
## Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for software and
|
||||
other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed to take
|
||||
away your freedom to share and change the works. By contrast, the GNU General
|
||||
Public License is intended to guarantee your freedom to share and change all
|
||||
versions of a program--to make sure it remains free software for all its users.
|
||||
We, the Free Software Foundation, use the GNU General Public License for most
|
||||
of our software; it applies also to any other work released this way by its
|
||||
authors. You can apply it to your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not price. Our
|
||||
General Public Licenses are designed to make sure that you have the freedom to
|
||||
distribute copies of free software (and charge for them if you wish), that you
|
||||
receive source code or can get it if you want it, that you can change the
|
||||
software or use pieces of it in new free programs, and that you know you can do
|
||||
these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you these rights
|
||||
or asking you to surrender the rights. Therefore, you have certain
|
||||
responsibilities if you distribute copies of the software, or if you modify it:
|
||||
responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether gratis or for
|
||||
a fee, you must pass on to the recipients the same freedoms that you received.
|
||||
You must make sure that they, too, receive or can get the source code. And you
|
||||
must show them these terms so they know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
|
||||
1. assert copyright on the software, and
|
||||
2. offer you this License giving you legal permission to copy, distribute
|
||||
and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains that
|
||||
there is no warranty for this free software. For both users' and authors' sake,
|
||||
the GPL requires that modified versions be marked as changed, so that their
|
||||
problems will not be attributed erroneously to authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run modified
|
||||
versions of the software inside them, although the manufacturer can do so. This
|
||||
is fundamentally incompatible with the aim of protecting users' freedom to
|
||||
change the software. The systematic pattern of such abuse occurs in the area of
|
||||
products for individuals to use, which is precisely where it is most
|
||||
unacceptable. Therefore, we have designed this version of the GPL to prohibit
|
||||
the practice for those products. If such problems arise substantially in other
|
||||
domains, we stand ready to extend this provision to those domains in future
|
||||
versions of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents. States
|
||||
should not allow patents to restrict development and use of software on
|
||||
general-purpose computers, but in those that do, we wish to avoid the special
|
||||
danger that patents applied to a free program could make it effectively
|
||||
proprietary. To prevent this, the GPL assures that patents cannot be used to
|
||||
render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and modification
|
||||
follow.
|
||||
|
||||
## TERMS AND CONDITIONS
|
||||
|
||||
### 0. Definitions.
|
||||
|
||||
*This License* refers to version 3 of the GNU General Public License.
|
||||
|
||||
*Copyright* also means copyright-like laws that apply to other kinds of works,
|
||||
such as semiconductor masks.
|
||||
|
||||
*The Program* refers to any copyrightable work licensed under this License.
|
||||
Each licensee is addressed as *you*. *Licensees* and *recipients* may be
|
||||
individuals or organizations.
|
||||
|
||||
To *modify* a work means to copy from or adapt all or part of the work in a
|
||||
fashion requiring copyright permission, other than the making of an exact copy.
|
||||
The resulting work is called a *modified version* of the earlier work or a work
|
||||
*based on* the earlier work.
|
||||
|
||||
A *covered work* means either the unmodified Program or a work based on the
|
||||
Program.
|
||||
|
||||
To *propagate* a work means to do anything with it that, without permission,
|
||||
would make you directly or secondarily liable for infringement under applicable
|
||||
copyright law, except executing it on a computer or modifying a private copy.
|
||||
Propagation includes copying, distribution (with or without modification),
|
||||
making available to the public, and in some countries other activities as well.
|
||||
|
||||
To *convey* a work means any kind of propagation that enables other parties to
|
||||
make or receive copies. Mere interaction with a user through a computer
|
||||
network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays *Appropriate Legal Notices* to the
|
||||
extent that it includes a convenient and prominently visible feature that
|
||||
|
||||
1. displays an appropriate copyright notice, and
|
||||
2. tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the work
|
||||
under this License, and how to view a copy of this License.
|
||||
|
||||
If the interface presents a list of user commands or options, such as a menu, a
|
||||
prominent item in the list meets this criterion.
|
||||
|
||||
### 1. Source Code.
|
||||
|
||||
The *source code* for a work means the preferred form of the work for making
|
||||
modifications to it. *Object code* means any non-source form of a work.
|
||||
|
||||
A *Standard Interface* means an interface that either is an official standard
|
||||
defined by a recognized standards body, or, in the case of interfaces specified
|
||||
for a particular programming language, one that is widely used among developers
|
||||
working in that language.
|
||||
|
||||
The *System Libraries* of an executable work include anything, other than the
|
||||
work as a whole, that (a) is included in the normal form of packaging a Major
|
||||
Component, but which is not part of that Major Component, and (b) serves only
|
||||
to enable use of the work with that Major Component, or to implement a Standard
|
||||
Interface for which an implementation is available to the public in source code
|
||||
form. A *Major Component*, in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system (if any) on
|
||||
which the executable work runs, or a compiler used to produce the work, or an
|
||||
object code interpreter used to run it.
|
||||
|
||||
The *Corresponding Source* for a work in object code form means all the source
|
||||
code needed to generate, install, and (for an executable work) run the object
|
||||
code and to modify the work, including scripts to control those activities.
|
||||
However, it does not include the work's System Libraries, or general-purpose
|
||||
tools or generally available free programs which are used unmodified in
|
||||
performing those activities but which are not part of the work. For example,
|
||||
Corresponding Source includes interface definition files associated with source
|
||||
files for the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require, such as
|
||||
by intimate data communication or control flow between those subprograms and
|
||||
other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users can regenerate
|
||||
automatically from other parts of the Corresponding Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that same work.
|
||||
|
||||
### 2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of copyright on
|
||||
the Program, and are irrevocable provided the stated conditions are met. This
|
||||
License explicitly affirms your unlimited permission to run the unmodified
|
||||
Program. The output from running a covered work is covered by this License only
|
||||
if the output, given its content, constitutes a covered work. This License
|
||||
acknowledges your rights of fair use or other equivalent, as provided by
|
||||
copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not convey, without
|
||||
conditions so long as your license otherwise remains in force. You may convey
|
||||
covered works to others for the sole purpose of having them make modifications
|
||||
exclusively for you, or provide you with facilities for running those works,
|
||||
provided that you comply with the terms of this License in conveying all
|
||||
material for which you do not control copyright. Those thus making or running
|
||||
the covered works for you must do so exclusively on your behalf, under your
|
||||
direction and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under the
|
||||
conditions stated below. Sublicensing is not allowed; section 10 makes it
|
||||
unnecessary.
|
||||
|
||||
### 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological measure
|
||||
under any applicable law fulfilling obligations under article 11 of the WIPO
|
||||
copyright treaty adopted on 20 December 1996, or similar laws prohibiting or
|
||||
restricting circumvention of such measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention is
|
||||
effected by exercising rights under this License with respect to the covered
|
||||
work, and you disclaim any intention to limit operation or modification of the
|
||||
work as a means of enforcing, against the work's users, your or third parties'
|
||||
legal rights to forbid circumvention of technological measures.
|
||||
|
||||
### 4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you receive it,
|
||||
in any medium, provided that you conspicuously and appropriately publish on
|
||||
each copy an appropriate copyright notice; keep intact all notices stating that
|
||||
this License and any non-permissive terms added in accord with section 7 apply
|
||||
to the code; keep intact all notices of the absence of any warranty; and give
|
||||
all recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey, and you may
|
||||
offer support or warranty protection for a fee.
|
||||
|
||||
### 5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to produce it
|
||||
from the Program, in the form of source code under the terms of section 4,
|
||||
provided that you also meet all of these conditions:
|
||||
|
||||
- a) The work must carry prominent notices stating that you modified it, and
|
||||
giving a relevant date.
|
||||
- b) The work must carry prominent notices stating that it is released under
|
||||
this License and any conditions added under section 7. This requirement
|
||||
modifies the requirement in section 4 to *keep intact all notices*.
|
||||
- c) You must license the entire work, as a whole, under this License to
|
||||
anyone who comes into possession of a copy. This License will therefore
|
||||
apply, along with any applicable section 7 additional terms, to the whole
|
||||
of the work, and all its parts, regardless of how they are packaged. This
|
||||
License gives no permission to license the work in any other way, but it
|
||||
does not invalidate such permission if you have separately received it.
|
||||
- d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your work need
|
||||
not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent works,
|
||||
which are not by their nature extensions of the covered work, and which are not
|
||||
combined with it such as to form a larger program, in or on a volume of a
|
||||
storage or distribution medium, is called an *aggregate* if the compilation and
|
||||
its resulting copyright are not used to limit the access or legal rights of the
|
||||
compilation's users beyond what the individual works permit. Inclusion of a
|
||||
covered work in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
### 6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms of sections 4
|
||||
and 5, provided that you also convey the machine-readable Corresponding Source
|
||||
under the terms of this License, in one of these ways:
|
||||
|
||||
- a) Convey the object code in, or embodied in, a physical product (including
|
||||
a physical distribution medium), accompanied by the Corresponding Source
|
||||
fixed on a durable physical medium customarily used for software
|
||||
interchange.
|
||||
- b) Convey the object code in, or embodied in, a physical product (including
|
||||
a physical distribution medium), accompanied by a written offer, valid for
|
||||
at least three years and valid for as long as you offer spare parts or
|
||||
customer support for that product model, to give anyone who possesses the
|
||||
object code either
|
||||
1. a copy of the Corresponding Source for all the software in the product
|
||||
that is covered by this License, on a durable physical medium
|
||||
customarily used for software interchange, for a price no more than your
|
||||
reasonable cost of physically performing this conveying of source, or
|
||||
2. access to copy the Corresponding Source from a network server at no
|
||||
charge.
|
||||
- c) Convey individual copies of the object code with a copy of the written
|
||||
offer to provide the Corresponding Source. This alternative is allowed only
|
||||
occasionally and noncommercially, and only if you received the object code
|
||||
with such an offer, in accord with subsection 6b.
|
||||
- d) Convey the object code by offering access from a designated place
|
||||
(gratis or for a charge), and offer equivalent access to the Corresponding
|
||||
Source in the same way through the same place at no further charge. You
|
||||
need not require recipients to copy the Corresponding Source along with the
|
||||
object code. If the place to copy the object code is a network server, the
|
||||
Corresponding Source may be on a different server operated by you or a
|
||||
third party) that supports equivalent copying facilities, provided you
|
||||
maintain clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the Corresponding
|
||||
Source, you remain obligated to ensure that it is available for as long as
|
||||
needed to satisfy these requirements.
|
||||
- e) Convey the object code using peer-to-peer transmission, provided you
|
||||
inform other peers where the object code and Corresponding Source of the
|
||||
work are being offered to the general public at no charge under subsection
|
||||
6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded from the
|
||||
Corresponding Source as a System Library, need not be included in conveying the
|
||||
object code work.
|
||||
|
||||
A *User Product* is either
|
||||
|
||||
1. a *consumer product*, which means any tangible personal property which is
|
||||
normally used for personal, family, or household purposes, or
|
||||
2. anything designed or sold for incorporation into a dwelling.
|
||||
|
||||
In determining whether a product is a consumer product, doubtful cases shall be
|
||||
resolved in favor of coverage. For a particular product received by a
|
||||
particular user, *normally used* refers to a typical or common use of that
|
||||
class of product, regardless of the status of the particular user or of the way
|
||||
in which the particular user actually uses, or expects or is expected to use,
|
||||
the product. A product is a consumer product regardless of whether the product
|
||||
has substantial commercial, industrial or non-consumer uses, unless such uses
|
||||
represent the only significant mode of use of the product.
|
||||
|
||||
*Installation Information* for a User Product means any methods, procedures,
|
||||
authorization keys, or other information required to install and execute
|
||||
modified versions of a covered work in that User Product from a modified
|
||||
version of its Corresponding Source. The information must suffice to ensure
|
||||
that the continued functioning of the modified object code is in no case
|
||||
prevented or interfered with solely because modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as part of a
|
||||
transaction in which the right of possession and use of the User Product is
|
||||
transferred to the recipient in perpetuity or for a fixed term (regardless of
|
||||
how the transaction is characterized), the Corresponding Source conveyed under
|
||||
this section must be accompanied by the Installation Information. But this
|
||||
requirement does not apply if neither you nor any third party retains the
|
||||
ability to install modified object code on the User Product (for example, the
|
||||
work has been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates for a
|
||||
work that has been modified or installed by the recipient, or for the User
|
||||
Product in which it has been modified or installed. Access to a network may be
|
||||
denied when the modification itself materially and adversely affects the
|
||||
operation of the network or violates the rules and protocols for communication
|
||||
across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided, in accord
|
||||
with this section must be in a format that is publicly documented (and with an
|
||||
implementation available to the public in source code form), and must require
|
||||
no special password or key for unpacking, reading or copying.
|
||||
|
||||
### 7. Additional Terms.
|
||||
|
||||
*Additional permissions* are terms that supplement the terms of this License by
|
||||
making exceptions from one or more of its conditions. Additional permissions
|
||||
that are applicable to the entire Program shall be treated as though they were
|
||||
included in this License, to the extent that they are valid under applicable
|
||||
law. If additional permissions apply only to part of the Program, that part may
|
||||
be used separately under those permissions, but the entire Program remains
|
||||
governed by this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option remove any
|
||||
additional permissions from that copy, or from any part of it. (Additional
|
||||
permissions may be written to require their own removal in certain cases when
|
||||
you modify the work.) You may place additional permissions on material, added
|
||||
by you to a covered work, for which you have or can give appropriate copyright
|
||||
permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you add to a
|
||||
covered work, you may (if authorized by the copyright holders of that material)
|
||||
supplement the terms of this License with terms:
|
||||
|
||||
- a) Disclaiming warranty or limiting liability differently from the terms of
|
||||
sections 15 and 16 of this License; or
|
||||
- b) Requiring preservation of specified reasonable legal notices or author
|
||||
attributions in that material or in the Appropriate Legal Notices displayed
|
||||
by works containing it; or
|
||||
- c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in reasonable
|
||||
ways as different from the original version; or
|
||||
- d) Limiting the use for publicity purposes of names of licensors or authors
|
||||
of the material; or
|
||||
- e) Declining to grant rights under trademark law for use of some trade
|
||||
names, trademarks, or service marks; or
|
||||
- f) Requiring indemnification of licensors and authors of that material by
|
||||
anyone who conveys the material (or modified versions of it) with
|
||||
contractual assumptions of liability to the recipient, for any liability
|
||||
that these contractual assumptions directly impose on those licensors and
|
||||
authors.
|
||||
|
||||
All other non-permissive additional terms are considered *further restrictions*
|
||||
within the meaning of section 10. If the Program as you received it, or any
|
||||
part of it, contains a notice stating that it is governed by this License along
|
||||
with a term that is a further restriction, you may remove that term. If a
|
||||
license document contains a further restriction but permits relicensing or
|
||||
conveying under this License, you may add to a covered work material governed
|
||||
by the terms of that license document, provided that the further restriction
|
||||
does not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you must place,
|
||||
in the relevant source files, a statement of the additional terms that apply to
|
||||
those files, or a notice indicating where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the form of a
|
||||
separately written license, or stated as exceptions; the above requirements
|
||||
apply either way.
|
||||
|
||||
### 8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly provided
|
||||
under this License. Any attempt otherwise to propagate or modify it is void,
|
||||
and will automatically terminate your rights under this License (including any
|
||||
patent licenses granted under the third paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your license from a
|
||||
particular copyright holder is reinstated
|
||||
|
||||
- a) provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and
|
||||
- b) permanently, if the copyright holder fails to notify you of the
|
||||
violation by some reasonable means prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is reinstated
|
||||
permanently if the copyright holder notifies you of the violation by some
|
||||
reasonable means, this is the first time you have received notice of violation
|
||||
of this License (for any work) from that copyright holder, and you cure the
|
||||
violation prior to 30 days after your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the licenses
|
||||
of parties who have received copies or rights from you under this License. If
|
||||
your rights have been terminated and not permanently reinstated, you do not
|
||||
qualify to receive new licenses for the same material under section 10.
|
||||
|
||||
### 9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or run a copy
|
||||
of the Program. Ancillary propagation of a covered work occurring solely as a
|
||||
consequence of using peer-to-peer transmission to receive a copy likewise does
|
||||
not require acceptance. However, nothing other than this License grants you
|
||||
permission to propagate or modify any covered work. These actions infringe
|
||||
copyright if you do not accept this License. Therefore, by modifying or
|
||||
propagating a covered work, you indicate your acceptance of this License to do
|
||||
so.
|
||||
|
||||
### 10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically receives a
|
||||
license from the original licensors, to run, modify and propagate that work,
|
||||
subject to this License. You are not responsible for enforcing compliance by
|
||||
third parties with this License.
|
||||
|
||||
An *entity transaction* is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered work
|
||||
results from an entity transaction, each party to that transaction who receives
|
||||
a copy of the work also receives whatever licenses to the work the party's
|
||||
predecessor in interest had or could give under the previous paragraph, plus a
|
||||
right to possession of the Corresponding Source of the work from the
|
||||
predecessor in interest, if the predecessor has it or can get it with
|
||||
reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the rights
|
||||
granted or affirmed under this License. For example, you may not impose a
|
||||
license fee, royalty, or other charge for exercise of rights granted under this
|
||||
License, and you may not initiate litigation (including a cross-claim or
|
||||
counterclaim in a lawsuit) alleging that any patent claim is infringed by
|
||||
making, using, selling, offering for sale, or importing the Program or any
|
||||
portion of it.
|
||||
|
||||
### 11. Patents.
|
||||
|
||||
A *contributor* is a copyright holder who authorizes use under this License of
|
||||
the Program or a work on which the Program is based. The work thus licensed is
|
||||
called the contributor's *contributor version*.
|
||||
|
||||
A contributor's *essential patent claims* are all patent claims owned or
|
||||
controlled by the contributor, whether already acquired or hereafter acquired,
|
||||
that would be infringed by some manner, permitted by this License, of making,
|
||||
using, or selling its contributor version, but do not include claims that would
|
||||
be infringed only as a consequence of further modification of the contributor
|
||||
version. For purposes of this definition, *control* includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of this
|
||||
License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free patent
|
||||
license under the contributor's essential patent claims, to make, use, sell,
|
||||
offer for sale, import and otherwise run, modify and propagate the contents of
|
||||
its contributor version.
|
||||
|
||||
In the following three paragraphs, a *patent license* is any express agreement
|
||||
or commitment, however denominated, not to enforce a patent (such as an express
|
||||
permission to practice a patent or covenant not to sue for patent
|
||||
infringement). To *grant* such a patent license to a party means to make such
|
||||
an agreement or commitment not to enforce a patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license, and the
|
||||
Corresponding Source of the work is not available for anyone to copy, free of
|
||||
charge and under the terms of this License, through a publicly available
|
||||
network server or other readily accessible means, then you must either
|
||||
|
||||
1. cause the Corresponding Source to be so available, or
|
||||
2. arrange to deprive yourself of the benefit of the patent license for this
|
||||
particular work, or
|
||||
3. arrange, in a manner consistent with the requirements of this License, to
|
||||
extend the patent license to downstream recipients.
|
||||
|
||||
*Knowingly relying* means you have actual knowledge that, but for the patent
|
||||
license, your conveying the covered work in a country, or your recipient's use
|
||||
of the covered work in a country, would infringe one or more identifiable
|
||||
patents in that country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or arrangement, you
|
||||
convey, or propagate by procuring conveyance of, a covered work, and grant a
|
||||
patent license to some of the parties receiving the covered work authorizing
|
||||
them to use, propagate, modify or convey a specific copy of the covered work,
|
||||
then the patent license you grant is automatically extended to all recipients
|
||||
of the covered work and works based on it.
|
||||
|
||||
A patent license is *discriminatory* if it does not include within the scope of
|
||||
its coverage, prohibits the exercise of, or is conditioned on the non-exercise
|
||||
of one or more of the rights that are specifically granted under this License.
|
||||
You may not convey a covered work if you are a party to an arrangement with a
|
||||
third party that is in the business of distributing software, under which you
|
||||
make payment to the third party based on the extent of your activity of
|
||||
conveying the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory patent
|
||||
license
|
||||
|
||||
- a) in connection with copies of the covered work conveyed by you (or copies
|
||||
made from those copies), or
|
||||
- b) primarily for and in connection with specific products or compilations
|
||||
that contain the covered work, unless you entered into that arrangement, or
|
||||
that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting any implied
|
||||
license or other defenses to infringement that may otherwise be available to
|
||||
you under applicable patent law.
|
||||
|
||||
### 12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not excuse
|
||||
you from the conditions of this License. If you cannot convey a covered work so
|
||||
as to satisfy simultaneously your obligations under this License and any other
|
||||
pertinent obligations, then as a consequence you may not convey it at all. For
|
||||
example, if you agree to terms that obligate you to collect a royalty for
|
||||
further conveying from those to whom you convey the Program, the only way you
|
||||
could satisfy both those terms and this License would be to refrain entirely
|
||||
from conveying the Program.
|
||||
|
||||
### 13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have permission to
|
||||
link or combine any covered work with a work licensed under version 3 of the
|
||||
GNU Affero General Public License into a single combined work, and to convey
|
||||
the resulting work. The terms of this License will continue to apply to the
|
||||
part which is the covered work, but the special requirements of the GNU Affero
|
||||
General Public License, section 13, concerning interaction through a network
|
||||
will apply to the combination as such.
|
||||
|
||||
### 14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of the GNU
|
||||
General Public License from time to time. Such new versions will be similar in
|
||||
spirit to the present version, but may differ in detail to address new problems
|
||||
or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program specifies
|
||||
that a certain numbered version of the GNU General Public License *or any later
|
||||
version* applies to it, you have the option of following the terms and
|
||||
conditions either of that numbered version or of any later version published by
|
||||
the Free Software Foundation. If the Program does not specify a version number
|
||||
of the GNU General Public License, you may choose any version ever published by
|
||||
the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future versions of the
|
||||
GNU General Public License can be used, that proxy's public statement of
|
||||
acceptance of a version permanently authorizes you to choose that version for
|
||||
the Program.
|
||||
|
||||
Later license versions may give you additional or different permissions.
|
||||
However, no additional obligations are imposed on any author or copyright
|
||||
holder as a result of your choosing to follow a later version.
|
||||
|
||||
### 15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE
|
||||
LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
|
||||
PARTIES PROVIDE THE PROGRAM *AS IS* WITHOUT WARRANTY OF ANY KIND, EITHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE
|
||||
QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
|
||||
DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
|
||||
CORRECTION.
|
||||
|
||||
### 16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
|
||||
COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS
|
||||
PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
|
||||
INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
|
||||
THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED
|
||||
INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE
|
||||
PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY
|
||||
HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
### 17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided above cannot
|
||||
be given local legal effect according to their terms, reviewing courts shall
|
||||
apply local law that most closely approximates an absolute waiver of all civil
|
||||
liability in connection with the Program, unless a warranty or assumption of
|
||||
liability accompanies a copy of the Program in return for a fee.
|
||||
|
||||
## END OF TERMS AND CONDITIONS ###
|
||||
|
||||
### How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest possible
|
||||
use to the public, the best way to achieve this is to make it free software
|
||||
which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest to attach
|
||||
them to the start of each source file to most effectively state the exclusion
|
||||
of warranty; and each file should have at least the *copyright* line and a
|
||||
pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short notice like
|
||||
this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w` and `show c` should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands might
|
||||
be different; for a GUI interface, you would use an *about box*.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school, if
|
||||
any, to sign a *copyright disclaimer* for the program, if necessary. For more
|
||||
information on this, and how to apply and follow the GNU GPL, see
|
||||
[http://www.gnu.org/licenses/](http://www.gnu.org/licenses/).
|
||||
|
||||
The GNU General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may consider
|
||||
it more useful to permit linking proprietary applications with the library. If
|
||||
this is what you want to do, use the GNU Lesser General Public License instead
|
||||
of this License. But first, please read
|
||||
[http://www.gnu.org/philosophy/why-not-lgpl.html](http://www.gnu.org/philosophy/why-not-lgpl.html).
|
443
lib/playfair/hand_garble.c
Normal file
443
lib/playfair/hand_garble.c
Normal file
@ -0,0 +1,443 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define printf(...) (void)0;
|
||||
|
||||
uint8_t rol8(uint8_t x, int y);
|
||||
uint32_t rol8x(uint8_t x, int y);
|
||||
|
||||
uint32_t weird_ror8(uint8_t input, int count)
|
||||
{
|
||||
if (count == 0)
|
||||
return 0;
|
||||
return ((input >> count) & 0xff) | (input & 0xff) << (8-count);
|
||||
|
||||
}
|
||||
|
||||
uint32_t weird_rol8(uint8_t input, int count)
|
||||
{
|
||||
if (count == 0)
|
||||
return 0;
|
||||
return ((input << count) & 0xff) | (input & 0xff) >> (8-count);
|
||||
}
|
||||
|
||||
uint32_t weird_rol32(uint8_t input, int count)
|
||||
{
|
||||
if (count == 0)
|
||||
return 0;
|
||||
return (input << count) ^ (input >> (8 - count));
|
||||
}
|
||||
|
||||
// I do not know why it is doing all of this, and there is still a possibility for a gremlin or two to be lurking in the background
|
||||
// I DO know it is not trivial. It could be purely random garbage, of course.
|
||||
void garble(unsigned char* buffer0, unsigned char* buffer1, unsigned char* buffer2, unsigned char* buffer3, unsigned char* buffer4)
|
||||
{
|
||||
unsigned int tmp, tmp2, tmp3;
|
||||
unsigned int A, B, C, D, E, M, J, G, F, H, K, R, S, T, U, V, W, X, Y, Z;
|
||||
// buffer1[64] = A
|
||||
// (buffer1[99] / 3) = B
|
||||
// 0ABAAABB
|
||||
// Then we AND with a complex expression, and add 20 just for good measure
|
||||
buffer2[12] = 0x14 + (((buffer1[64] & 92) | ((buffer1[99] / 3) & 35)) & buffer4[rol8x(buffer4[(buffer1[206] % 21)],4) % 21]);
|
||||
printf("buffer2[12] = %02x\n", buffer2[12]);
|
||||
|
||||
// This is a bit simpler: 2*B*B/25
|
||||
buffer1[4] = (buffer1[99] / 5) * (buffer1[99] / 5) * 2;
|
||||
printf("buffer1[4] = %02x\n", buffer1[4]);
|
||||
|
||||
// Simpler still!
|
||||
buffer2[34] = 0xb8;
|
||||
printf("buffer2[34] = %02x\n", buffer2[34]);
|
||||
|
||||
// ...
|
||||
buffer1[153] ^= (buffer2[buffer1[203] % 35] * buffer2[buffer1[203] % 35] * buffer1[190]);
|
||||
printf("buffer1[153] = %02x\n", buffer1[153]);
|
||||
|
||||
// This one looks simple, but wow was it not :(
|
||||
buffer0[3] -= (((buffer4[buffer1[205] % 21]>>1) & 80) | 0xe6440);
|
||||
printf("buffer0[3] = %02x\n", buffer0[3]);
|
||||
|
||||
// This is always 0x93
|
||||
buffer0[16] = 0x93;
|
||||
printf("buffer0[16] = %02x\n", buffer0[16]);
|
||||
|
||||
// This is always 0x62
|
||||
buffer0[13] = 0x62;
|
||||
printf("buffer0[13] = %02x\n", buffer0[13]);
|
||||
|
||||
buffer1[33] -= (buffer4[buffer1[36] % 21] & 0xf6);
|
||||
printf("buffer1[33] = %02x\n", buffer1[33]);
|
||||
|
||||
// This is always 7
|
||||
tmp2 = buffer2[buffer1[67] % 35];
|
||||
buffer2[12] = 0x07;
|
||||
printf("buffer2[12] = %02x\n", buffer2[12]);
|
||||
|
||||
// This is pretty easy!
|
||||
tmp = buffer0[buffer1[181] % 20];
|
||||
buffer1[2] -= 3136;
|
||||
printf("buffer1[2] = %02x\n", buffer1[2]);
|
||||
|
||||
buffer0[19] = buffer4[buffer1[58] % 21];
|
||||
printf("buffer0[19] = %02x\n", buffer0[19]);
|
||||
|
||||
buffer3[0] = 92 - buffer2[buffer1[32] % 35];
|
||||
printf("buffer3[0] = %02x\n", buffer3[0]);
|
||||
|
||||
buffer3[4] = buffer2[buffer1[15] % 35] + 0x9e;
|
||||
printf("buffer3[4] = %02x\n", buffer3[4]);
|
||||
|
||||
buffer1[34] += (buffer4[((buffer2[buffer1[15] % 35] + 0x9e) & 0xff) % 21] / 5);
|
||||
printf("buffer1[34] = %02x\n", buffer1[34]);
|
||||
|
||||
buffer0[19] += 0xfffffee6 - ((buffer0[buffer3[4] % 20]>>1) & 102);
|
||||
printf("buffer0[19] = %02x\n", buffer0[19]);
|
||||
|
||||
// This LOOKS like it should be a rol8x, but it just doesnt work out because if the shift amount is 0, then the output is 0 too :(
|
||||
// FIXME: Switch to weird_ror8
|
||||
buffer1[15] = (3*(((buffer1[72] >> (buffer4[buffer1[190] % 21] & 7)) ^ (buffer1[72] << ((7 - (buffer4[buffer1[190] % 21]-1)&7)))) - (3*buffer4[buffer1[126] % 21]))) ^ buffer1[15];
|
||||
printf("buffer1[15] = %02x\n", buffer1[15]);
|
||||
|
||||
buffer0[15] ^= buffer2[buffer1[181] % 35] * buffer2[buffer1[181] % 35] * buffer2[buffer1[181] % 35];
|
||||
printf("buffer0[15] = %02x\n", buffer0[15]);
|
||||
|
||||
buffer2[4] ^= buffer1[202]/3;
|
||||
printf("buffer2[4] = %02x\n", buffer2[4]);
|
||||
|
||||
// This could probably be quite a bit simpler.
|
||||
A = 92 - buffer0[buffer3[0] % 20];
|
||||
E = (A & 0xc6) | (~buffer1[105] & 0xc6) | (A & (~buffer1[105]));
|
||||
buffer2[1] += (E*E*E);
|
||||
printf("buffer2[1] = %02x\n", buffer2[1]);
|
||||
|
||||
buffer0[19] ^= ((224 | (buffer4[buffer1[92] % 21] & 27)) * buffer2[buffer1[41] % 35]) / 3;
|
||||
printf("buffer0[19] = %02x\n", buffer0[19]);
|
||||
|
||||
buffer1[140] += weird_ror8(92, buffer1[5] & 7);
|
||||
printf("buffer1[140] = %02x\n", buffer1[140]);
|
||||
|
||||
// Is this as simple as it could be?
|
||||
buffer2[12] += ((((~buffer1[4]) ^ buffer2[buffer1[12] % 35]) | buffer1[182]) & 192) | (((~buffer1[4]) ^ buffer2[buffer1[12] % 35]) & buffer1[182]);
|
||||
printf("buffer2[12] = %02x\n", buffer2[12]);
|
||||
|
||||
buffer1[36] += 125;
|
||||
printf("buffer1[36] = %02x\n", buffer1[36]);
|
||||
|
||||
buffer1[124] = rol8x((((74 & buffer1[138]) | ((74 | buffer1[138]) & buffer0[15])) & buffer0[buffer1[43] % 20]) | (((74 & buffer1[138]) | ((74 | buffer1[138]) & buffer0[15]) | buffer0[buffer1[43] % 20]) & 95), 4);
|
||||
printf("buffer1[124] = %02x\n", buffer1[124]);
|
||||
|
||||
buffer3[8] = ((((buffer0[buffer3[4] % 20] & 95)) & ((buffer4[buffer1[68] % 21] & 46) << 1)) | 16) ^ 92;
|
||||
printf("buffer3[8] = %02x\n", buffer3[8]);
|
||||
|
||||
A = buffer1[177] + buffer4[buffer1[79] % 21];
|
||||
D = (((A >> 1) | ((3 * buffer1[148]) / 5)) & buffer2[1]) | ((A >> 1) & ((3 * buffer1[148])/5));
|
||||
buffer3[12] = ((-34 - D));
|
||||
printf("buffer3[12] = %02x\n", buffer3[12]);
|
||||
|
||||
A = 8 - ((buffer2[22] & 7)); // FIXME: buffer2[22] = 74, so A is always 6 and B^C is just ror8(buffer1[33], 6)
|
||||
B = (buffer1[33] >> (A & 7));
|
||||
C = buffer1[33] << (buffer2[22] & 7);
|
||||
buffer2[16] += ((buffer2[buffer3[0] % 35] & 159) | buffer0[buffer3[4] % 20] | 8) - ((B^C) | 128);
|
||||
printf("buffer2[16] = %02x\n", buffer2[16]);
|
||||
|
||||
// This one was very easy so I just skipped ahead and did it
|
||||
buffer0[14] ^= buffer2[buffer3[12] % 35];
|
||||
printf("buffer0[14] = %02x\n", buffer0[14]);
|
||||
|
||||
// Monster goes here
|
||||
A = weird_rol8(buffer4[buffer0[buffer1[201] % 20] % 21], ((buffer2[buffer1[112] % 35] << 1) & 7));
|
||||
D = (buffer0[buffer1[208] % 20] & 131) | (buffer0[buffer1[164] % 20] & 124);
|
||||
buffer1[19] += (A & (D/5)) | ((A | (D/5)) & 37);
|
||||
printf("buffer1[19] = %02x\n", buffer1[19]);
|
||||
|
||||
buffer2[8] = weird_ror8(140, ((buffer4[buffer1[45] % 21] + 92) * (buffer4[buffer1[45] % 21] + 92)) & 7);
|
||||
printf("buffer2[8] = %02x\n", buffer2[8]);
|
||||
|
||||
buffer1[190] = 56;
|
||||
printf("buffer1[190] = %02x\n", buffer1[190]);
|
||||
|
||||
buffer2[8] ^= buffer3[0];
|
||||
printf("buffer2[8] = %02x\n", buffer2[8]);
|
||||
|
||||
buffer1[53] = ~((buffer0[buffer1[83] % 20] | 204)/5);
|
||||
printf("buffer1[53] = %02x\n", buffer1[53]);
|
||||
|
||||
buffer0[13] += buffer0[buffer1[41] % 20];
|
||||
printf("buffer0[13] = %02x\n", buffer0[13]);
|
||||
|
||||
buffer0[10] = ((buffer2[buffer3[0] % 35] & buffer1[2]) | ((buffer2[buffer3[0] % 35] | buffer1[2]) & buffer3[12])) / 15;
|
||||
printf("buffer0[10] = %02x\n", buffer0[10]);
|
||||
|
||||
A = (((56 | (buffer4[buffer1[2] % 21] & 68)) | buffer2[buffer3[8] % 35]) & 42) | (((buffer4[buffer1[2] % 21] & 68) | 56) & buffer2[buffer3[8] % 35]);
|
||||
buffer3[16] = (A*A) + 110;
|
||||
printf("buffer3[16] = %02x\n", buffer3[16]);
|
||||
|
||||
buffer3[20] = 202 - buffer3[16];
|
||||
printf("buffer3[20] = %02x\n", buffer3[20]);
|
||||
|
||||
buffer3[24] = buffer1[151];
|
||||
printf("buffer3[24] = %02x\n", buffer3[24]);
|
||||
|
||||
buffer2[13] ^= buffer4[buffer3[0] % 21];
|
||||
printf("buffer2[13] = %02x\n", buffer2[13]);
|
||||
|
||||
B = ((buffer2[buffer1[179] % 35] - 38) & 177) | (buffer3[12] & 177);
|
||||
C = ((buffer2[buffer1[179] % 35] - 38)) & buffer3[12];
|
||||
buffer3[28] = 30 + ((B | C) * (B | C));
|
||||
printf("buffer3[28] = %02x\n", buffer3[28]);
|
||||
|
||||
buffer3[32] = buffer3[28] + 62;
|
||||
printf("buffer3[32] = %02x\n", buffer3[32]);
|
||||
|
||||
// eek
|
||||
A = ((buffer3[20] + (buffer3[0] & 74)) | ~buffer4[buffer3[0] % 21]) & 121;
|
||||
B = ((buffer3[20] + (buffer3[0] & 74)) & ~buffer4[buffer3[0] % 21]);
|
||||
tmp3 = (A|B);
|
||||
C = ((((A|B) ^ 0xffffffa6) | buffer3[0]) & 4) | (((A|B) ^ 0xffffffa6) & buffer3[0]);
|
||||
buffer1[47] = (buffer2[buffer1[89] % 35] + C) ^ buffer1[47];
|
||||
printf("buffer1[47] = %02x\n", buffer1[47]);
|
||||
|
||||
buffer3[36] = ((rol8((tmp & 179) + 68, 2) & buffer0[3]) | (tmp2 & ~buffer0[3])) - 15;
|
||||
printf("buffer3[36] = %02x\n", buffer3[36]);
|
||||
|
||||
buffer1[123] ^= 221;
|
||||
printf("buffer1[123] = %02x\n", buffer1[123]);
|
||||
|
||||
A = ((buffer4[buffer3[0] % 21]) / 3) - buffer2[buffer3[4] % 35];
|
||||
C = (((buffer3[0] & 163) + 92) & 246) | (buffer3[0] & 92);
|
||||
E = ((C | buffer3[24]) & 54) | (C & buffer3[24]);
|
||||
buffer3[40] = A - E;
|
||||
printf("buffer3[40] = %02x\n", buffer3[40]);
|
||||
|
||||
buffer3[44] = tmp3 ^ 81 ^ (((buffer3[0] >> 1) & 101) + 26);
|
||||
printf("buffer3[44] = %02x\n", buffer3[44]);
|
||||
|
||||
buffer3[48] = buffer2[buffer3[4] % 35] & 27;
|
||||
printf("buffer3[48] = %02x\n", buffer3[48]);
|
||||
buffer3[52] = 27;
|
||||
printf("buffer3[52] = %02x\n", buffer3[52]);
|
||||
buffer3[56] = 199;
|
||||
printf("buffer3[56] = %02x\n", buffer3[56]);
|
||||
|
||||
// caffeine
|
||||
buffer3[64] = buffer3[4] + (((((((buffer3[40] | buffer3[24]) & 177) | (buffer3[40] & buffer3[24])) & ((((buffer4[buffer3[0] % 20] & 177) | 176)) | ((buffer4[buffer3[0] % 21]) & ~3))) | ((((buffer3[40] & buffer3[24]) | ((buffer3[40] | buffer3[24]) & 177)) & 199) | ((((buffer4[buffer3[0] % 21] & 1) + 176) | (buffer4[buffer3[0] % 21] & ~3)) & buffer3[56]))) & (~buffer3[52])) | buffer3[48]);
|
||||
printf("buffer3[64] = %02x (want E7)\n", buffer3[64]);
|
||||
|
||||
buffer2[33] ^= buffer1[26];
|
||||
printf("buffer2[33] = %02x\n", buffer2[33]);
|
||||
|
||||
buffer1[106] ^= buffer3[20] ^ 133;
|
||||
printf("buffer1[106] = %02x\n", buffer1[106]);
|
||||
|
||||
buffer2[30] = ((buffer3[64] / 3) - (275 | (buffer3[0] & 247))) ^ buffer0[buffer1[122] % 20];
|
||||
printf("buffer2[130] = %02x\n", buffer2[30]);
|
||||
|
||||
buffer1[22] = (buffer2[buffer1[90] % 35] & 95) | 68;
|
||||
printf("buffer1[22] = %02x\n", buffer1[22]);
|
||||
|
||||
A = (buffer4[buffer3[36] % 21] & 184) | (buffer2[buffer3[44] % 35] & ~184);
|
||||
buffer2[18] += ((A*A*A) >> 1);
|
||||
printf("buffer2[18] = %02x\n", buffer2[18]);
|
||||
|
||||
buffer2[5] -= buffer4[buffer1[92] % 21];
|
||||
printf("buffer2[5] = %02x\n", buffer2[5]);
|
||||
|
||||
A = (((buffer1[41] & ~24)|(buffer2[buffer1[183] % 35] & 24)) & (buffer3[16] + 53)) | (buffer3[20] & buffer2[buffer3[20] % 35]);
|
||||
B = (buffer1[17] & (~buffer3[44])) | (buffer0[buffer1[59] % 20] & buffer3[44]);
|
||||
buffer2[18] ^= (A*B);
|
||||
printf("buffer2[18] = %02x\n", buffer2[18]);
|
||||
|
||||
|
||||
A = weird_ror8(buffer1[11], buffer2[buffer1[28] % 35] & 7) & 7;
|
||||
B = (((buffer0[buffer1[93] % 20] & ~buffer0[14]) | (buffer0[14] & 150)) & ~28) | (buffer1[7] & 28);
|
||||
buffer2[22] = (((((B | weird_rol8(buffer2[buffer3[0] % 35], A)) & buffer2[33]) | (B & weird_rol8(buffer2[buffer3[0] % 35], A))) + 74) & 0xff);
|
||||
printf("buffer2[22] = %02x\n", buffer2[22]);
|
||||
|
||||
A = buffer4[(buffer0[buffer1[39] % 20] ^ 217) % 21]; // X5
|
||||
buffer0[15] -= ((((buffer3[20] | buffer3[0]) & 214) | (buffer3[20] & buffer3[0])) & A) | ((((buffer3[20] | buffer3[0]) & 214) | (buffer3[20] & buffer3[0]) | A) & buffer3[32]);
|
||||
printf("buffer0[15] = %02x\n", buffer0[15]);
|
||||
|
||||
// We need to save T here, and boy is it complicated to calculate!
|
||||
B = (((buffer2[buffer1[57] % 35] & buffer0[buffer3[64] % 20]) | ((buffer0[buffer3[64] % 20] | buffer2[buffer1[57] % 35]) & 95) | (buffer3[64] & 45) | 82) & 32);
|
||||
C = ((buffer2[buffer1[57] % 35] & buffer0[buffer3[64] % 20]) | ((buffer2[buffer1[57] % 35] | buffer0[buffer3[64] % 20]) & 95)) & ((buffer3[64] & 45) | 82);
|
||||
D = ((((buffer3[0]/3) - (buffer3[64]|buffer1[22]))) ^ (buffer3[28] + 62) ^ ((B|C)));
|
||||
T = buffer0[(D & 0xff) % 20];
|
||||
|
||||
buffer3[68] = (buffer0[buffer1[99] % 20] * buffer0[buffer1[99] % 20] * buffer0[buffer1[99] % 20] * buffer0[buffer1[99] % 20]) | buffer2[buffer3[64] % 35];
|
||||
printf("buffer3[68] = %02x\n", buffer3[68]);
|
||||
|
||||
U = buffer0[buffer1[50] % 20]; // this is also v100
|
||||
W = buffer2[buffer1[138] % 35];
|
||||
X = buffer4[buffer1[39] % 21];
|
||||
Y = buffer0[buffer1[4] % 20]; // this is also v120
|
||||
Z = buffer4[buffer1[202] % 21]; // also v124
|
||||
V = buffer0[buffer1[151] % 20];
|
||||
S = buffer2[buffer1[14] % 35];
|
||||
R = buffer0[buffer1[145] % 20];
|
||||
|
||||
A = (buffer2[buffer3[68] % 35] & buffer0[buffer1[209] % 20]) | ((buffer2[buffer3[68] % 35] | buffer0[buffer1[209] % 20]) & 24);
|
||||
B = weird_rol8(buffer4[buffer1[127] % 21], buffer2[buffer3[68] % 35] & 7);
|
||||
C = (A & buffer0[10]) | (B & ~buffer0[10]);
|
||||
D = 7 ^ (buffer4[buffer2[buffer3[36] % 35] % 21] << 1);
|
||||
buffer3[72] = (C & 71) | (D & ~71);
|
||||
printf("buffer3[72] = %02x\n", buffer3[72]);
|
||||
|
||||
buffer2[2] += (((buffer0[buffer3[20] % 20] << 1) & 159) | (buffer4[buffer1[190] % 21] & ~159)) & ((((buffer4[buffer3[64] % 21] & 110) | (buffer0[buffer1[25] % 20] & ~110)) & ~150) | (buffer1[25] & 150));
|
||||
printf("buffer2[2] = %02x\n", buffer2[2]);
|
||||
|
||||
buffer2[14] -= ((buffer2[buffer3[20] % 35] & (buffer3[72] ^ buffer2[buffer1[100] % 35])) & ~34) | (buffer1[97] & 34);
|
||||
printf("buffer2[14] = %02x\n", buffer2[14]);
|
||||
|
||||
buffer0[17] = 115;
|
||||
printf("buffer0[17] = %02x\n", buffer0[17]);
|
||||
|
||||
buffer1[23] ^= ((((((buffer4[buffer1[17] % 21] | buffer0[buffer3[20] % 20]) & buffer3[72]) | (buffer4[buffer1[17] % 21] & buffer0[buffer3[20] % 20])) & (buffer1[50]/3)) |
|
||||
((((buffer4[buffer1[17] % 21] | buffer0[buffer3[20] % 20]) & buffer3[72]) | (buffer4[buffer1[17] % 21] & buffer0[buffer3[20] % 20]) | (buffer1[50] / 3)) & 246)) << 1);
|
||||
printf("buffer1[23] = %02x\n", buffer1[23]);
|
||||
|
||||
buffer0[13] = ((((((buffer0[buffer3[40] % 20] | buffer1[10]) & 82) | (buffer0[buffer3[40] % 20] & buffer1[10])) & 209) |
|
||||
((buffer0[buffer1[39] % 20] << 1) & 46)) >> 1);
|
||||
printf("buffer0[13] = %02x\n", buffer0[13]);
|
||||
|
||||
buffer2[33] -= buffer1[113] & 9;
|
||||
printf("buffer2[33] = %02x\n", buffer2[33]);
|
||||
|
||||
buffer2[28] -= ((((2 | (buffer1[110] & 222)) >> 1) & ~223) | (buffer3[20] & 223));
|
||||
printf("buffer2[28] = %02x\n", buffer2[28]);
|
||||
|
||||
J = weird_rol8((V | Z), (U & 7)); // OK
|
||||
A = (buffer2[16] & T) | (W & (~buffer2[16]));
|
||||
B = (buffer1[33] & 17) | (X & ~17);
|
||||
E = ((Y | ((A+B) / 5)) & 147) |
|
||||
(Y & ((A+B) / 5)); // OK
|
||||
M = (buffer3[40] & buffer4[((buffer3[8] + J + E) & 0xff) % 21]) |
|
||||
((buffer3[40] | buffer4[((buffer3[8] + J + E) & 0xff) % 21]) & buffer2[23]);
|
||||
|
||||
buffer0[15] = (((buffer4[buffer3[20] % 21] - 48) & (~buffer1[184])) | ((buffer4[buffer3[20] % 21] - 48) & 189) | (189 & ~buffer1[184])) & (M*M*M);
|
||||
printf("buffer0[15] = %02x\n", buffer0[15]);
|
||||
|
||||
buffer2[22] += buffer1[183];
|
||||
printf("buffer2[22] = %02x\n", buffer2[22]);
|
||||
|
||||
buffer3[76] = (3 * buffer4[buffer1[1] % 21]) ^ buffer3[0];
|
||||
printf("buffer3[76] = %02x\n", buffer3[76]);
|
||||
|
||||
A = buffer2[((buffer3[8] + (J + E)) & 0xff) % 35];
|
||||
F = (((buffer4[buffer1[178] % 21] & A) | ((buffer4[buffer1[178] % 21] | A) & 209)) * buffer0[buffer1[13] % 20]) * (buffer4[buffer1[26] % 21] >> 1);
|
||||
G = (F + 0x733ffff9) * 198 - (((F + 0x733ffff9) * 396 + 212) & 212) + 85;
|
||||
buffer3[80] = buffer3[36] + (G ^ 148) + ((G ^ 107) << 1) - 127;
|
||||
printf("buffer3[80] = %02x\n", buffer3[80]);
|
||||
|
||||
buffer3[84] = ((buffer2[buffer3[64] % 35]) & 245) | (buffer2[buffer3[20] % 35] & 10);
|
||||
printf("buffer3[84] = %02x\n", buffer3[84]);
|
||||
|
||||
A = buffer0[buffer3[68] % 20] | 81;
|
||||
buffer2[18] -= ((A*A*A) & ~buffer0[15]) | ((buffer3[80] / 15) & buffer0[15]);
|
||||
printf("buffer2[18] = %02x\n", buffer2[18]);
|
||||
|
||||
buffer3[88] = buffer3[8] + J + E - buffer0[buffer1[160] % 20] + (buffer4[buffer0[((buffer3[8] + J + E) & 255) % 20] % 21] / 3);
|
||||
printf("buffer3[88] = %02x\n", buffer3[88]);
|
||||
|
||||
B = ((R ^ buffer3[72]) & ~198) | ((S * S) & 198);
|
||||
F = (buffer4[buffer1[69] % 21] & buffer1[172]) | ((buffer4[buffer1[69] % 21] | buffer1[172] ) & ((buffer3[12] - B) + 77));
|
||||
buffer0[16] = 147 - ((buffer3[72] & ((F & 251) | 1)) | (((F & 250) | buffer3[72]) & 198));
|
||||
printf("buffer0[16] = %02x\n", buffer0[16]);
|
||||
|
||||
C = (buffer4[buffer1[168] % 21] & buffer0[buffer1[29] % 20] & 7) | ((buffer4[buffer1[168] % 21] | buffer0[buffer1[29] % 20]) & 6);
|
||||
F = (buffer4[buffer1[155] % 21] & buffer1[105]) | ((buffer4[buffer1[155] % 21] | buffer1[105]) & 141);
|
||||
buffer0[3] -= buffer4[weird_rol32(F, C) % 21];
|
||||
printf("buffer0[3] = %02x\n", buffer0[3]);
|
||||
|
||||
buffer1[5] = weird_ror8(buffer0[12], ((buffer0[buffer1[61] % 20] / 5) & 7)) ^ (((~buffer2[buffer3[84] % 35]) & 0xffffffff) / 5);
|
||||
printf("buffer1[5] = %02x\n", buffer1[5]);
|
||||
|
||||
buffer1[198] += buffer1[3];
|
||||
printf("buffer1[198] = %02x\n", buffer1[198]);
|
||||
|
||||
A = (162 | buffer2[buffer3[64] % 35]);
|
||||
buffer1[164] += ((A*A)/5);
|
||||
printf("buffer1[164] = %02x\n", buffer1[164]);
|
||||
|
||||
G = weird_ror8(139, (buffer3[80] & 7));
|
||||
C = ((buffer4[buffer3[64] % 21] * buffer4[buffer3[64] % 21] * buffer4[buffer3[64] % 21]) & 95) | (buffer0[buffer3[40] % 20] & ~95);
|
||||
buffer3[92] = (G & 12) | (buffer0[buffer3[20] % 20] & 12) | (G & buffer0[buffer3[20] % 20]) | C;
|
||||
printf("buffer3[92] = %02x\n", buffer3[92]);
|
||||
|
||||
buffer2[12] += ((buffer1[103] & 32) | (buffer3[92] & ((buffer1[103] | 60))) | 16)/3;
|
||||
printf("buffer2[12] = %02x\n", buffer2[12]);
|
||||
|
||||
buffer3[96] = buffer1[143];
|
||||
printf("buffer3[96] = %02x\n", buffer3[96]);
|
||||
|
||||
buffer3[100] = 27;
|
||||
printf("buffer3[100] = %02x\n", buffer3[100]);
|
||||
|
||||
buffer3[104] = (((buffer3[40] & ~buffer2[8]) | (buffer1[35] & buffer2[8])) & buffer3[64]) ^ 119;
|
||||
printf("buffer3[104] = %02x\n", buffer3[104]);
|
||||
|
||||
buffer3[108] = 238 & ((((buffer3[40] & ~buffer2[8]) | (buffer1[35] & buffer2[8])) & buffer3[64]) << 1);
|
||||
printf("buffer3[108] = %02x\n", buffer3[108]);
|
||||
|
||||
buffer3[112] = (~buffer3[64] & (buffer3[84] / 3)) ^ 49;
|
||||
printf("buffer3[112] = %02x\n", buffer3[112]);
|
||||
|
||||
buffer3[116] = 98 & ((~buffer3[64] & (buffer3[84] / 3)) << 1);
|
||||
printf("buffer3[116] = %02x\n", buffer3[116]);
|
||||
|
||||
// finale
|
||||
A = (buffer1[35] & buffer2[8]) | (buffer3[40] & ~buffer2[8]);
|
||||
B = (A & buffer3[64]) | (((buffer3[84] / 3) & ~buffer3[64]));
|
||||
buffer1[143] = buffer3[96] - ((B & (86 + ((buffer1[172] & 64) >> 1))) | (((((buffer1[172] & 65) >> 1) ^ 86) | ((~buffer3[64] & (buffer3[84] / 3)) | (((buffer3[40] & ~buffer2[8]) | (buffer1[35] & buffer2[8])) & buffer3[64]))) & buffer3[100]));
|
||||
printf("buffer1[143] = %02x\n", buffer1[143]);
|
||||
|
||||
buffer2[29] = 162;
|
||||
printf("buffer2[29] = %02x\n", buffer2[29]);
|
||||
|
||||
A = ((((buffer4[buffer3[88] % 21]) & 160) | (buffer0[buffer1[125] % 20] & 95)) >> 1);
|
||||
B = buffer2[buffer1[149] % 35] ^ (buffer1[43] * buffer1[43]);
|
||||
buffer0[15] += (B&A) | ((A|B) & 115);
|
||||
printf("buffer0[15] = %02x\n", buffer0[15]);
|
||||
|
||||
buffer3[120] = buffer3[64] - buffer0[buffer3[40] % 20];
|
||||
printf("buffer3[120] = %02x\n", buffer3[120]);
|
||||
|
||||
buffer1[95] = buffer4[buffer3[20] % 21];
|
||||
printf("buffer1[95] = %02x\n", buffer1[95]);
|
||||
|
||||
A = weird_ror8(buffer2[buffer3[80] % 35], (buffer2[buffer1[17] % 35] * buffer2[buffer1[17] % 35] * buffer2[buffer1[17] % 35]) & 7);
|
||||
buffer0[7] -= (A*A);
|
||||
printf("buffer0[7] = %02x\n", buffer0[7]);
|
||||
|
||||
buffer2[8] = buffer2[8] - buffer1[184] + (buffer4[buffer1[202] % 21] * buffer4[buffer1[202] % 21] * buffer4[buffer1[202] % 21]);
|
||||
printf("buffer2[8] = %02x\n", buffer2[8]);
|
||||
|
||||
buffer0[16] = (buffer2[buffer1[102] % 35] << 1) & 132;
|
||||
printf("buffer0[16] = %02x\n", buffer0[16]);
|
||||
|
||||
buffer3[124] = (buffer4[buffer3[40] % 21] >> 1) ^ buffer3[68];
|
||||
printf("buffer3[124] = %02x\n", buffer3[124]);
|
||||
|
||||
buffer0[7] -= (buffer0[buffer1[191] % 20] - (((buffer4[buffer1[80] % 21] << 1) & ~177) | (buffer4[buffer4[buffer3[88] % 21] % 21] & 177)));
|
||||
printf("buffer0[7] = %02x\n", buffer0[7]);
|
||||
|
||||
buffer0[6] = buffer0[buffer1[119] % 20];
|
||||
printf("buffer0[6] = %02x\n", buffer0[6]);
|
||||
|
||||
A = (buffer4[buffer1[190] % 21] & ~209) | (buffer1[118] & 209);
|
||||
B = buffer0[buffer3[120] % 20] * buffer0[buffer3[120] % 20];
|
||||
buffer0[12] = (buffer0[buffer3[84] % 20] ^ (buffer2[buffer1[71] % 35] + buffer2[buffer1[15] % 35])) & ((A & B) | ((A | B) & 27));
|
||||
printf("buffer0[12] = %02x\n", buffer0[12]);
|
||||
|
||||
B = (buffer1[32] & buffer2[buffer3[88] % 35]) | ((buffer1[32] | buffer2[buffer3[88] % 35]) & 23);
|
||||
D = (((buffer4[buffer1[57] % 21] * 231) & 169) | (B & 86));
|
||||
F = (((buffer0[buffer1[82] % 20] & ~29) | (buffer4[buffer3[124] % 21] & 29)) & 190) | (buffer4[(D/5) % 21] & ~190);
|
||||
H = buffer0[buffer3[40] % 20] * buffer0[buffer3[40] % 20] * buffer0[buffer3[40] % 20];
|
||||
K = (H & buffer1[82]) | (H & 92) | (buffer1[82] & 92);
|
||||
buffer3[128] = ((F & K) | ((F | K) & 192)) ^ (D/5);
|
||||
printf("buffer3[128] = %02x\n", buffer3[128]);
|
||||
|
||||
buffer2[25] ^= ((buffer0[buffer3[120] % 20] << 1) * buffer1[5]) - (weird_rol8(buffer3[76], (buffer4[buffer3[124] % 21] & 7)) & (buffer3[20] + 110));
|
||||
printf("buffer2[25] = %02x\n", buffer2[25]);
|
||||
//exit(0);
|
||||
|
||||
}
|
119
lib/playfair/modified_md5.c
Normal file
119
lib/playfair/modified_md5.c
Normal file
@ -0,0 +1,119 @@
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#define printf(...) (void)0;
|
||||
|
||||
int shift[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
|
||||
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
|
||||
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
|
||||
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
|
||||
|
||||
uint32_t F(uint32_t B, uint32_t C, uint32_t D)
|
||||
{
|
||||
return (B & C) | (~B & D);
|
||||
}
|
||||
|
||||
uint32_t G(uint32_t B, uint32_t C, uint32_t D)
|
||||
{
|
||||
return (B & D) | (C & ~D);
|
||||
}
|
||||
|
||||
uint32_t H(uint32_t B, uint32_t C, uint32_t D)
|
||||
{
|
||||
return B ^ C ^ D;
|
||||
}
|
||||
|
||||
uint32_t I(uint32_t B, uint32_t C, uint32_t D)
|
||||
{
|
||||
return C ^ (B | ~D);
|
||||
}
|
||||
|
||||
|
||||
uint32_t rol(uint32_t input, int count)
|
||||
{
|
||||
return ((input << count) & 0xffffffff) | (input & 0xffffffff) >> (32-count);
|
||||
}
|
||||
|
||||
void swap(uint32_t* a, uint32_t* b)
|
||||
{
|
||||
printf("%08x <-> %08x\n", *a, *b);
|
||||
uint32_t c = *a;
|
||||
*a = *b;
|
||||
*b = c;
|
||||
}
|
||||
|
||||
void modified_md5(unsigned char* originalblockIn, unsigned char* keyIn, unsigned char* keyOut)
|
||||
{
|
||||
unsigned char blockIn[64];
|
||||
uint32_t* block_words = (uint32_t*)blockIn;
|
||||
uint32_t* key_words = (uint32_t*)keyIn;
|
||||
uint32_t* out_words = (uint32_t*)keyOut;
|
||||
uint32_t A, B, C, D, Z, tmp;
|
||||
int i;
|
||||
|
||||
memcpy(blockIn, originalblockIn, 64);
|
||||
|
||||
// Each cycle does something like this:
|
||||
A = key_words[0];
|
||||
B = key_words[1];
|
||||
C = key_words[2];
|
||||
D = key_words[3];
|
||||
for (i = 0; i < 64; i++)
|
||||
{
|
||||
uint32_t input;
|
||||
int j;
|
||||
if (i < 16)
|
||||
j = i;
|
||||
else if (i < 32)
|
||||
j = (5*i + 1) % 16;
|
||||
else if (i < 48)
|
||||
j = (3*i + 5) % 16;
|
||||
else if (i < 64)
|
||||
j = 7*i % 16;
|
||||
|
||||
input = blockIn[4*j] << 24 | blockIn[4*j+1] << 16 | blockIn[4*j+2] << 8 | blockIn[4*j+3];
|
||||
printf("Key = %08x\n", A);
|
||||
Z = A + input + (int)(long long)((1LL << 32) * fabs(sin(i + 1)));
|
||||
if (i < 16)
|
||||
Z = rol(Z + F(B,C,D), shift[i]);
|
||||
else if (i < 32)
|
||||
Z = rol(Z + G(B,C,D), shift[i]);
|
||||
else if (i < 48)
|
||||
Z = rol(Z + H(B,C,D), shift[i]);
|
||||
else if (i < 64)
|
||||
Z = rol(Z + I(B,C,D), shift[i]);
|
||||
if (i == 63)
|
||||
printf("Ror is %08x\n", Z);
|
||||
printf("Output of round %d: %08X + %08X = %08X (shift %d, constant %08X)\n", i, Z, B, Z+B, shift[i], (int)(long long)((1LL << 32) * fabs(sin(i + 1))));
|
||||
Z = Z + B;
|
||||
tmp = D;
|
||||
D = C;
|
||||
C = B;
|
||||
B = Z;
|
||||
A = tmp;
|
||||
if (i == 31)
|
||||
{
|
||||
// swapsies
|
||||
swap(&block_words[A & 15], &block_words[B & 15]);
|
||||
swap(&block_words[C & 15], &block_words[D & 15]);
|
||||
swap(&block_words[(A & (15<<4))>>4], &block_words[(B & (15<<4))>>4]);
|
||||
swap(&block_words[(A & (15<<8))>>8], &block_words[(B & (15<<8))>>8]);
|
||||
swap(&block_words[(A & (15<<12))>>12], &block_words[(B & (15<<12))>>12]);
|
||||
}
|
||||
}
|
||||
printf("%08X %08X %08X %08X\n", A, B, C, D);
|
||||
// Now we can actually compute the output
|
||||
printf("Out:\n");
|
||||
printf("%08x + %08x = %08x\n", key_words[0], A, key_words[0] + A);
|
||||
printf("%08x + %08x = %08x\n", key_words[1], B, key_words[1] + B);
|
||||
printf("%08x + %08x = %08x\n", key_words[2], C, key_words[2] + C);
|
||||
printf("%08x + %08x = %08x\n", key_words[3], D, key_words[3] + D);
|
||||
out_words[0] = key_words[0] + A;
|
||||
out_words[1] = key_words[1] + B;
|
||||
out_words[2] = key_words[2] + C;
|
||||
out_words[3] = key_words[3] + D;
|
||||
|
||||
}
|
540
lib/playfair/omg_hax.c
Normal file
540
lib/playfair/omg_hax.c
Normal file
@ -0,0 +1,540 @@
|
||||
void modified_md5(unsigned char* originalblockIn, unsigned char* keyIn, unsigned char* keyOut);
|
||||
void sap_hash(unsigned char* blockIn, unsigned char* keyOut);
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "omg_hax.h"
|
||||
|
||||
#define printf(...) (void)0;
|
||||
|
||||
void xor_blocks(unsigned char* a, unsigned char* b, unsigned char* out)
|
||||
{
|
||||
for (int i = 0; i < 16; i++)
|
||||
out[i] = a[i] ^ b[i];
|
||||
}
|
||||
|
||||
|
||||
void z_xor(unsigned char* in, unsigned char* out, int blocks)
|
||||
{
|
||||
for (int j = 0; j < blocks; j++)
|
||||
for (int i = 0; i < 16; i++)
|
||||
out[j*16+i] = in[j*16+i] ^ z_key[i];
|
||||
}
|
||||
|
||||
void x_xor(unsigned char* in, unsigned char* out, int blocks)
|
||||
{
|
||||
for (int j = 0; j < blocks; j++)
|
||||
for (int i = 0; i < 16; i++)
|
||||
out[j*16+i] = in[j*16+i] ^ x_key[i];
|
||||
}
|
||||
|
||||
|
||||
void t_xor(unsigned char* in, unsigned char* out)
|
||||
{
|
||||
for (int i = 0; i < 16; i++)
|
||||
out[i] = in[i] ^ t_key[i];
|
||||
}
|
||||
|
||||
unsigned char sap_iv[] = {0x2B,0x84,0xFB,0x79,0xDA,0x75,0xB9,0x04,0x6C,0x24,0x73,0xF7,0xD1,0xC4,0xAB,0x0E,0x2B,0x84,0xFB,0x79,0x75,0xB9,0x04,0x6C,0x24,0x73};
|
||||
|
||||
unsigned char sap_key_material[] = {0xA1, 0x1A, 0x4A, 0x83,
|
||||
0xF2, 0x7A, 0x75, 0xEE,
|
||||
0xA2, 0x1A, 0x7D, 0xB8,
|
||||
0x8D, 0x77, 0x92, 0xAB};
|
||||
|
||||
unsigned char index_mangle[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36, 0x6C};
|
||||
|
||||
unsigned char* table_index(int i)
|
||||
{
|
||||
return &table_s1[((31*i) % 0x28) << 8];
|
||||
}
|
||||
|
||||
unsigned char* message_table_index(int i)
|
||||
{
|
||||
return &table_s2[(97*i % 144) << 8];
|
||||
}
|
||||
|
||||
void print_block(char* msg, unsigned char* dword)
|
||||
{
|
||||
printf("%s", msg);
|
||||
for (int i = 0; i < 16; i++)
|
||||
printf("%02X ", dword[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void permute_block_1(unsigned char* block)
|
||||
{
|
||||
block[0] = table_s3[block[0]];
|
||||
block[4] = table_s3[0x400+block[4]];
|
||||
block[8] = table_s3[0x800+block[8]];
|
||||
block[12] = table_s3[0xc00+block[12]];
|
||||
|
||||
unsigned char tmp = block[13];
|
||||
block[13] = table_s3[0x100+block[9]];
|
||||
block[9] = table_s3[0xd00+block[5]];
|
||||
block[5] = table_s3[0x900+block[1]];
|
||||
block[1] = table_s3[0x500+tmp];
|
||||
|
||||
tmp = block[2];
|
||||
block[2] = table_s3[0xa00+block[10]];
|
||||
block[10] = table_s3[0x200+tmp];
|
||||
tmp = block[6];
|
||||
block[6] = table_s3[0xe00+block[14]];
|
||||
block[14] = table_s3[0x600+tmp];
|
||||
|
||||
tmp = block[3];
|
||||
block[3] = table_s3[0xf00+block[7]];
|
||||
block[7] = table_s3[0x300+block[11]];
|
||||
block[11] = table_s3[0x700+block[15]];
|
||||
block[15] = table_s3[0xb00+tmp];
|
||||
print_block("Permutation complete. Final value of block: ", block); // This looks right to me, at least for decrypt_kernel
|
||||
}
|
||||
|
||||
unsigned char* permute_table_2(unsigned int i)
|
||||
{
|
||||
return &table_s4[((71 * i) % 144) << 8];
|
||||
}
|
||||
|
||||
void permute_block_2(unsigned char* block, int round)
|
||||
{
|
||||
// round is 0..8?
|
||||
printf("Permuting via table2, round %d... (block[0] = %02X)\n", round, block[0]);
|
||||
block[0] = permute_table_2(round*16+0)[block[0]];
|
||||
block[4] = permute_table_2(round*16+4)[block[4]];
|
||||
block[8] = permute_table_2(round*16+8)[block[8]];
|
||||
block[12] = permute_table_2(round*16+12)[block[12]];
|
||||
|
||||
unsigned char tmp = block[13];
|
||||
block[13] = permute_table_2(round*16+13)[block[9]];
|
||||
block[9] = permute_table_2(round*16+9)[block[5]];
|
||||
block[5] = permute_table_2(round*16+5)[block[1]];
|
||||
block[1] = permute_table_2(round*16+1)[tmp];
|
||||
|
||||
tmp = block[2];
|
||||
block[2] = permute_table_2(round*16+2)[block[10]];
|
||||
block[10] = permute_table_2(round*16+10)[tmp];
|
||||
tmp = block[6];
|
||||
block[6] = permute_table_2(round*16+6)[block[14]];
|
||||
block[14] = permute_table_2(round*16+14)[tmp];
|
||||
|
||||
tmp = block[3];
|
||||
block[3] = permute_table_2(round*16+3)[block[7]];
|
||||
block[7] = permute_table_2(round*16+7)[block[11]];
|
||||
block[11] = permute_table_2(round*16+11)[block[15]];
|
||||
block[15] = permute_table_2(round*16+15)[tmp];
|
||||
print_block("Permutation (2) complete. Final value of block: ", block); // This looks right to me, at least for decrypt_kernel
|
||||
}
|
||||
|
||||
// This COULD just be Rijndael key expansion, but with a different set of S-boxes
|
||||
void generate_key_schedule(unsigned char* key_material, uint32_t key_schedule[11][4])
|
||||
{
|
||||
uint32_t key_data[4];
|
||||
int i;
|
||||
for (i = 0; i < 11; i++)
|
||||
{
|
||||
key_schedule[i][0] = 0xdeadbeef;
|
||||
key_schedule[i][1] = 0xdeadbeef;
|
||||
key_schedule[i][2] = 0xdeadbeef;
|
||||
key_schedule[i][3] = 0xdeadbeef;
|
||||
}
|
||||
unsigned char* buffer = (unsigned char*)key_data;
|
||||
int ti = 0;
|
||||
printf("Generating key schedule\n");
|
||||
// G
|
||||
print_block("Raw key material: ", key_material);
|
||||
t_xor(key_material, buffer);
|
||||
print_block("G has produced: ", buffer);
|
||||
for (int round = 0; round < 11; round++)
|
||||
{
|
||||
printf("Starting round %d\n", round);
|
||||
// H
|
||||
key_schedule[round][0] = key_data[0];
|
||||
printf("H has set chunk 1 of round %d %08X\n", round, key_schedule[round][0]);
|
||||
printf("H complete\n");
|
||||
// I
|
||||
unsigned char* table1 = table_index(ti);
|
||||
unsigned char* table2 = table_index(ti+1);
|
||||
unsigned char* table3 = table_index(ti+2);
|
||||
unsigned char* table4 = table_index(ti+3);
|
||||
ti += 4;
|
||||
//buffer[0] = (buffer[0] - (4 & (buffer[0] << 1)) + 2) ^ 2 ^ index_mangle[round] ^ table1[buffer[0x0d]];
|
||||
printf("S-box: 0x%02x -> 0x%02x\n", buffer[0x0d], table1[buffer[0x0d]]);
|
||||
printf("S-box: 0x%02x -> 0x%02x\n", buffer[0x0e], table2[buffer[0x0e]]);
|
||||
printf("S-box: 0x%02x -> 0x%02x\n", buffer[0x0f], table3[buffer[0x0f]]);
|
||||
printf("S-box: 0x%02x -> 0x%02x\n", buffer[0x0c], table4[buffer[0x0c]]);
|
||||
buffer[0] ^= table1[buffer[0x0d]] ^ index_mangle[round];
|
||||
buffer[1] ^= table2[buffer[0x0e]];
|
||||
buffer[2] ^= table3[buffer[0x0f]];
|
||||
buffer[3] ^= table4[buffer[0x0c]];
|
||||
print_block("After I, buffer is now: ", buffer);
|
||||
printf("I complete\n");
|
||||
// H
|
||||
key_schedule[round][1] = key_data[1];
|
||||
printf("H has set chunk 2 to %08X\n", key_schedule[round][1]);
|
||||
|
||||
printf("H complete\n");
|
||||
// J
|
||||
key_data[1] ^= key_data[0];
|
||||
printf("J complete\n");
|
||||
print_block("Buffer is now ", buffer);
|
||||
// H
|
||||
key_schedule[round][2] = key_data[2];
|
||||
printf("H has set chunk3 to %08X\n", key_schedule[round][2]);
|
||||
printf("H complete\n");
|
||||
|
||||
// J
|
||||
key_data[2] ^= key_data[1];
|
||||
printf("J complete\n");
|
||||
// K and L
|
||||
// Implement K and L to fill in other bits of the key schedule
|
||||
key_schedule[round][3] = key_data[3];
|
||||
// J again
|
||||
key_data[3] ^= key_data[2];
|
||||
printf("J complete\n");
|
||||
}
|
||||
for (i = 0; i < 11; i++)
|
||||
print_block("Schedule: ", (unsigned char*)key_schedule[i]);
|
||||
}
|
||||
|
||||
// This MIGHT just be AES, or some variant thereof.
|
||||
void cycle(unsigned char* block, uint32_t key_schedule[11][4])
|
||||
{
|
||||
uint32_t ptr1 = 0;
|
||||
uint32_t ptr2 = 0;
|
||||
uint32_t ptr3 = 0;
|
||||
uint32_t ptr4 = 0;
|
||||
uint32_t ab;
|
||||
unsigned char* buffer = (unsigned char*)&ab;
|
||||
uint32_t* bWords = (uint32_t*)block;
|
||||
bWords[0] ^= key_schedule[10][0];
|
||||
bWords[1] ^= key_schedule[10][1];
|
||||
bWords[2] ^= key_schedule[10][2];
|
||||
bWords[3] ^= key_schedule[10][3];
|
||||
// First, these are permuted
|
||||
permute_block_1(block);
|
||||
|
||||
for (int round = 0; round < 9; round++)
|
||||
{
|
||||
// E
|
||||
// Note that table_s5 is a table of 4-byte words. Therefore we do not need to <<2 these indices
|
||||
// TODO: Are these just T-tables?
|
||||
unsigned char* key0 = (unsigned char*)&key_schedule[9-round][0];
|
||||
ptr1 = table_s5[block[3] ^ key0[3]];
|
||||
ptr2 = table_s6[block[2] ^ key0[2]];
|
||||
ptr3 = table_s8[block[0] ^ key0[0]];
|
||||
ptr4 = table_s7[block[1] ^ key0[1]];
|
||||
|
||||
// A B
|
||||
ab = ptr1 ^ ptr2 ^ ptr3 ^ ptr4;
|
||||
printf("ab: %08X %08X %08X %08X -> %08X\n", ptr1, ptr2, ptr3, ptr4, ab);
|
||||
// C
|
||||
((uint32_t*)block)[0] = ab;
|
||||
printf("f7 = %02X\n", block[7]);
|
||||
unsigned char* key1 = (unsigned char*)&key_schedule[9-round][1];
|
||||
ptr2 = table_s5[block[7] ^ key1[3]];
|
||||
ptr1 = table_s6[block[6] ^ key1[2]];
|
||||
ptr4 = table_s7[block[5] ^ key1[1]];
|
||||
ptr3 = table_s8[block[4] ^ key1[0]];
|
||||
// A B again
|
||||
ab = ptr1 ^ ptr2 ^ ptr3 ^ ptr4;
|
||||
printf("ab: %08X %08X %08X %08X -> %08X\n", ptr1, ptr2, ptr3, ptr4, ab);
|
||||
// D is a bit of a nightmare, but it is really not as complicated as you might think
|
||||
unsigned char* key2 = (unsigned char*)&key_schedule[9-round][2];
|
||||
unsigned char* key3 = (unsigned char*)&key_schedule[9-round][3];
|
||||
((uint32_t*)block)[1] = ab;
|
||||
((uint32_t*)block)[2] = table_s5[block[11] ^ key2[3]] ^
|
||||
table_s6[block[10] ^ key2[2]] ^
|
||||
table_s7[block[9] ^ key2[1]] ^
|
||||
table_s8[block[8] ^ key2[0]];
|
||||
|
||||
((uint32_t*)block)[3] = table_s5[block[15] ^ key3[3]] ^
|
||||
table_s6[block[14] ^ key3[2]] ^
|
||||
table_s7[block[13] ^ key3[1]] ^
|
||||
table_s8[block[12] ^ key3[0]];
|
||||
printf("Set block2 = %08X, block3 = %08X\n", ((uint32_t*)block)[2], ((uint32_t*)block)[3]);
|
||||
// In the last round, instead of the permute, we do F
|
||||
permute_block_2(block, 8-round);
|
||||
}
|
||||
printf("Using last bit of key up: %08X xor %08X -> %08X\n", ((uint32_t*)block)[0], key_schedule[0][0], ((uint32_t*)block)[0] ^ key_schedule[0][0]);
|
||||
((uint32_t*)block)[0] ^= key_schedule[0][0];
|
||||
((uint32_t*)block)[1] ^= key_schedule[0][1];
|
||||
((uint32_t*)block)[2] ^= key_schedule[0][2];
|
||||
((uint32_t*)block)[3] ^= key_schedule[0][3];
|
||||
}
|
||||
|
||||
|
||||
void decrypt_sap(unsigned char* sapIn, unsigned char* sapOut)
|
||||
{
|
||||
uint32_t key_schedule[11][4];
|
||||
unsigned char* iv;
|
||||
print_block("Base sap: ", &sapIn[0xf0]);
|
||||
z_xor(sapIn, sapOut, 16);
|
||||
generate_key_schedule(sap_key_material, key_schedule);
|
||||
print_block("lastSap before cycle: ", &sapOut[0xf0]);
|
||||
for (int i = 0xf0; i >= 0x00; i-=0x10)
|
||||
{
|
||||
printf("Ready to cycle %02X\n", i);
|
||||
cycle(&sapOut[i], key_schedule);
|
||||
print_block("After cycling, block is: ", &sapOut[i]);
|
||||
if (i > 0)
|
||||
{ // xor with previous block
|
||||
iv = &sapOut[i-0x10];
|
||||
}
|
||||
else
|
||||
{ // xor with sap IV
|
||||
iv = sap_iv;
|
||||
}
|
||||
for (int j = 0; j < 16; j++)
|
||||
{
|
||||
printf("%02X ^ %02X -> %02X\n", sapOut[i+j], iv[j], sapOut[i+j] ^ iv[j]);
|
||||
sapOut[i+j] = sapOut[i+j] ^ iv[j];
|
||||
}
|
||||
printf("Decrypted SAP %02X-%02X:\n", i, i+0xf);
|
||||
print_block("", &sapOut[i]);
|
||||
}
|
||||
// Lastly grind the whole thing through x_key. This is the last time we modify sap
|
||||
x_xor(sapOut, sapOut, 16);
|
||||
printf("Sap is decrypted to\n");
|
||||
for (int i = 0xf0; i >= 0x00; i-=0x10)
|
||||
{
|
||||
printf("Final SAP %02X-%02X: ", i, i+0xf);
|
||||
print_block("", &sapOut[i]);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char initial_session_key[] = {0xDC, 0xDC, 0xF3, 0xB9, 0x0B, 0x74, 0xDC, 0xFB, 0x86, 0x7F, 0xF7, 0x60, 0x16, 0x72, 0x90, 0x51};
|
||||
|
||||
|
||||
void decrypt_key(unsigned char* decryptedSap, unsigned char* keyIn, unsigned char* iv, unsigned char* keyOut)
|
||||
{
|
||||
unsigned char blockIn[16];
|
||||
uint32_t key_schedule[11][4];
|
||||
uint32_t mode_key_schedule[11][4];
|
||||
generate_key_schedule(&decryptedSap[8], key_schedule);
|
||||
printf("Generating mode key:\n");
|
||||
generate_key_schedule(initial_session_key, mode_key_schedule);
|
||||
z_xor(keyIn, blockIn, 1);
|
||||
print_block("Input to cycle is: ", blockIn);
|
||||
cycle(blockIn, key_schedule);
|
||||
for (int j = 0; j < 16; j++)
|
||||
keyOut[j] = blockIn[j] ^ iv[j];
|
||||
print_block("Output from cycle is: ", keyOut);
|
||||
x_xor(keyOut, keyOut, 1);
|
||||
}
|
||||
|
||||
|
||||
void decryptMessage(unsigned char* messageIn, unsigned char* decryptedMessage)
|
||||
{
|
||||
unsigned char buffer[16];
|
||||
int i, j;
|
||||
unsigned char tmp;
|
||||
uint32_t key_schedule[11][4];
|
||||
int mode = messageIn[12]; // 0,1,2,3
|
||||
printf("mode = %02x\n", mode);
|
||||
generate_key_schedule(initial_session_key, key_schedule);
|
||||
|
||||
// For M0-M6 we follow the same pattern
|
||||
for (i = 0; i < 8; i++)
|
||||
{
|
||||
// First, copy in the nth block (we must start with the last one)
|
||||
for (j = 0; j < 16; j++)
|
||||
{
|
||||
if (mode == 3)
|
||||
buffer[j] = messageIn[(0x80-0x10*i)+j];
|
||||
else if (mode == 2 || mode == 1 || mode == 0)
|
||||
buffer[j] = messageIn[(0x10*(i+1))+j];
|
||||
}
|
||||
// do this permutation and update 9 times. Could this be cycle(), or the reverse of cycle()?
|
||||
for (j = 0; j < 9; j++)
|
||||
{
|
||||
int base = 0x80 - 0x10*j;
|
||||
//print_block("About to cycle. Buffer is currently: ", buffer);
|
||||
buffer[0x0] = message_table_index(base+0x0)[buffer[0x0]] ^ message_key[mode][base+0x0];
|
||||
buffer[0x4] = message_table_index(base+0x4)[buffer[0x4]] ^ message_key[mode][base+0x4];
|
||||
buffer[0x8] = message_table_index(base+0x8)[buffer[0x8]] ^ message_key[mode][base+0x8];
|
||||
buffer[0xc] = message_table_index(base+0xc)[buffer[0xc]] ^ message_key[mode][base+0xc];
|
||||
|
||||
tmp = buffer[0x0d];
|
||||
buffer[0xd] = message_table_index(base+0xd)[buffer[0x9]] ^ message_key[mode][base+0xd];
|
||||
buffer[0x9] = message_table_index(base+0x9)[buffer[0x5]] ^ message_key[mode][base+0x9];
|
||||
buffer[0x5] = message_table_index(base+0x5)[buffer[0x1]] ^ message_key[mode][base+0x5];
|
||||
buffer[0x1] = message_table_index(base+0x1)[tmp] ^ message_key[mode][base+0x1];
|
||||
|
||||
tmp = buffer[0x02];
|
||||
buffer[0x2] = message_table_index(base+0x2)[buffer[0xa]] ^ message_key[mode][base+0x2];
|
||||
buffer[0xa] = message_table_index(base+0xa)[tmp] ^ message_key[mode][base+0xa];
|
||||
tmp = buffer[0x06];
|
||||
buffer[0x6] = message_table_index(base+0x6)[buffer[0xe]] ^ message_key[mode][base+0x6];
|
||||
buffer[0xe] = message_table_index(base+0xe)[tmp] ^ message_key[mode][base+0xe];
|
||||
|
||||
tmp = buffer[0x3];
|
||||
buffer[0x3] = message_table_index(base+0x3)[buffer[0x7]] ^ message_key[mode][base+0x3];
|
||||
buffer[0x7] = message_table_index(base+0x7)[buffer[0xb]] ^ message_key[mode][base+0x7];
|
||||
buffer[0xb] = message_table_index(base+0xb)[buffer[0xf]] ^ message_key[mode][base+0xb];
|
||||
buffer[0xf] = message_table_index(base+0xf)[tmp] ^ message_key[mode][base+0xf];
|
||||
|
||||
// Now we must replace the entire buffer with 4 words that we read and xor together
|
||||
uint32_t word;
|
||||
uint32_t* block = (uint32_t*)buffer;
|
||||
|
||||
block[0] = table_s9[0x000 + buffer[0x0]] ^
|
||||
table_s9[0x100 + buffer[0x1]] ^
|
||||
table_s9[0x200 + buffer[0x2]] ^
|
||||
table_s9[0x300 + buffer[0x3]];
|
||||
block[1] = table_s9[0x000 + buffer[0x4]] ^
|
||||
table_s9[0x100 + buffer[0x5]] ^
|
||||
table_s9[0x200 + buffer[0x6]] ^
|
||||
table_s9[0x300 + buffer[0x7]];
|
||||
block[2] = table_s9[0x000 + buffer[0x8]] ^
|
||||
table_s9[0x100 + buffer[0x9]] ^
|
||||
table_s9[0x200 + buffer[0xa]] ^
|
||||
table_s9[0x300 + buffer[0xb]];
|
||||
block[3] = table_s9[0x000 + buffer[0xc]] ^
|
||||
table_s9[0x100 + buffer[0xd]] ^
|
||||
table_s9[0x200 + buffer[0xe]] ^
|
||||
table_s9[0x300 + buffer[0xf]];
|
||||
}
|
||||
// Next, another permute with a different table
|
||||
buffer[0x0] = table_s10[(0x0 << 8) + buffer[0x0]];
|
||||
buffer[0x4] = table_s10[(0x4 << 8) + buffer[0x4]];
|
||||
buffer[0x8] = table_s10[(0x8 << 8) + buffer[0x8]];
|
||||
buffer[0xc] = table_s10[(0xc << 8) + buffer[0xc]];
|
||||
|
||||
tmp = buffer[0x0d];
|
||||
buffer[0xd] = table_s10[(0xd << 8) + buffer[0x9]];
|
||||
buffer[0x9] = table_s10[(0x9 << 8) + buffer[0x5]];
|
||||
buffer[0x5] = table_s10[(0x5 << 8) + buffer[0x1]];
|
||||
buffer[0x1] = table_s10[(0x1 << 8) + tmp];
|
||||
|
||||
tmp = buffer[0x02];
|
||||
buffer[0x2] = table_s10[(0x2 << 8) + buffer[0xa]];
|
||||
buffer[0xa] = table_s10[(0xa << 8) + tmp];
|
||||
tmp = buffer[0x06];
|
||||
buffer[0x6] = table_s10[(0x6 << 8) + buffer[0xe]];
|
||||
buffer[0xe] = table_s10[(0xe << 8) + tmp];
|
||||
|
||||
tmp = buffer[0x3];
|
||||
buffer[0x3] = table_s10[(0x3 << 8) + buffer[0x7]];
|
||||
buffer[0x7] = table_s10[(0x7 << 8) + buffer[0xb]];
|
||||
buffer[0xb] = table_s10[(0xb << 8) + buffer[0xf]];
|
||||
buffer[0xf] = table_s10[(0xf << 8) + tmp];
|
||||
|
||||
// And finally xor with the previous block of the message, except in mode-2 where we do this in reverse
|
||||
if (mode == 2 || mode == 1 || mode == 0)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
xor_blocks(buffer, &messageIn[0x10*i], &decryptedMessage[0x10*i]); // remember that the first 0x10 bytes are the header
|
||||
}
|
||||
else
|
||||
xor_blocks(buffer, message_iv[mode], &decryptedMessage[0x10*i]);
|
||||
print_block(" ", &decryptedMessage[0x10*i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (i < 7)
|
||||
xor_blocks(buffer, &messageIn[0x70 - 0x10*i], &decryptedMessage[0x70 - 0x10*i]);
|
||||
else
|
||||
xor_blocks(buffer, message_iv[mode], &decryptedMessage[0x70 - 0x10*i]);
|
||||
printf("Decrypted message block %02X-%02X:", 0x70 - 0x10*i, 0x70 - 0x10*i+0xf);
|
||||
print_block(" ", &decryptedMessage[0x70 - 0x10*i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char static_source_1[] = {0xFA, 0x9C, 0xAD, 0x4D, 0x4B, 0x68, 0x26, 0x8C, 0x7F, 0xF3, 0x88, 0x99, 0xDE, 0x92, 0x2E, 0x95,
|
||||
0x1E};
|
||||
unsigned char static_source_2[] = {0xEC, 0x4E, 0x27, 0x5E, 0xFD, 0xF2, 0xE8, 0x30, 0x97, 0xAE, 0x70, 0xFB, 0xE0, 0x00, 0x3F, 0x1C,
|
||||
0x39, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x09, 0x00, 0x0, 0x00, 0x00, 0x00, 0x00};
|
||||
|
||||
void swap_bytes(unsigned char* a, unsigned char *b)
|
||||
{
|
||||
unsigned char c = *a;
|
||||
*a = *b;
|
||||
*b = c;
|
||||
}
|
||||
|
||||
void generate_session_key(unsigned char* oldSap, unsigned char* messageIn, unsigned char* sessionKey)
|
||||
{
|
||||
unsigned char decryptedMessage[128];
|
||||
unsigned char newSap[320];
|
||||
unsigned char Q[210];
|
||||
int i;
|
||||
int round;
|
||||
unsigned char md5[16];
|
||||
unsigned char otherHash[16];
|
||||
|
||||
decryptMessage(messageIn, decryptedMessage);
|
||||
// Now that we have the decrypted message, we can combine it with our initial sap to form the 5 blocks needed to generate the 5 words which, when added together, give
|
||||
// the session key.
|
||||
memcpy(&newSap[0x000], static_source_1, 0x11);
|
||||
memcpy(&newSap[0x011], decryptedMessage, 0x80);
|
||||
memcpy(&newSap[0x091], &oldSap[0x80], 0x80);
|
||||
memcpy(&newSap[0x111], static_source_2, 0x2f);
|
||||
memcpy(sessionKey, initial_session_key, 16);
|
||||
|
||||
for (round = 0; round < 5; round++)
|
||||
{
|
||||
unsigned char* base = &newSap[round * 64];
|
||||
print_block("Input block: ", &base[0]);
|
||||
print_block("Input block: ", &base[0x10]);
|
||||
print_block("Input block: ", &base[0x20]);
|
||||
print_block("Input block: ", &base[0x30]);
|
||||
modified_md5(base, sessionKey, md5);
|
||||
printf("MD5 OK\n");
|
||||
sap_hash(base, sessionKey);
|
||||
printf("OtherHash OK\n");
|
||||
|
||||
printf("MD5 = ");
|
||||
for (i = 0; i < 4; i++)
|
||||
printf("%08x ", ((uint32_t*)md5)[i]);
|
||||
printf("\nOtherHash = ");
|
||||
for (i = 0; i < 4; i++)
|
||||
printf("%08x ", ((uint32_t*)sessionKey)[i]);
|
||||
printf("\n");
|
||||
|
||||
uint32_t* sessionKeyWords = (uint32_t*)sessionKey;
|
||||
uint32_t* md5Words = (uint32_t*)md5;
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
sessionKeyWords[i] = (sessionKeyWords[i] + md5Words[i]) & 0xffffffff;
|
||||
}
|
||||
printf("Current key: ");
|
||||
for (i = 0; i < 16; i++)
|
||||
printf("%02x", sessionKey[i]);
|
||||
printf("\n");
|
||||
}
|
||||
for (i = 0; i < 16; i+=4)
|
||||
{
|
||||
swap_bytes(&sessionKey[i], &sessionKey[i+3]);
|
||||
swap_bytes(&sessionKey[i+1], &sessionKey[i+2]);
|
||||
}
|
||||
|
||||
// Finally the whole thing is XORd with 121:
|
||||
for (i = 0; i < 16; i++)
|
||||
sessionKey[i] ^= 121;
|
||||
print_block("Session key computed as: ", sessionKey);
|
||||
}
|
||||
|
||||
unsigned char default_sap[] =
|
||||
{ 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
|
||||
0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
|
||||
0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
|
||||
0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
|
||||
0x79, 0x79, 0x79, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x02, 0x53,
|
||||
0x00, 0x01, 0xcc, 0x34, 0x2a, 0x5e, 0x5b, 0x1a, 0x67, 0x73, 0xc2, 0x0e, 0x21, 0xb8, 0x22, 0x4d,
|
||||
0xf8, 0x62, 0x48, 0x18, 0x64, 0xef, 0x81, 0x0a, 0xae, 0x2e, 0x37, 0x03, 0xc8, 0x81, 0x9c, 0x23,
|
||||
0x53, 0x9d, 0xe5, 0xf5, 0xd7, 0x49, 0xbc, 0x5b, 0x7a, 0x26, 0x6c, 0x49, 0x62, 0x83, 0xce, 0x7f,
|
||||
0x03, 0x93, 0x7a, 0xe1, 0xf6, 0x16, 0xde, 0x0c, 0x15, 0xff, 0x33, 0x8c, 0xca, 0xff, 0xb0, 0x9e,
|
||||
0xaa, 0xbb, 0xe4, 0x0f, 0x5d, 0x5f, 0x55, 0x8f, 0xb9, 0x7f, 0x17, 0x31, 0xf8, 0xf7, 0xda, 0x60,
|
||||
0xa0, 0xec, 0x65, 0x79, 0xc3, 0x3e, 0xa9, 0x83, 0x12, 0xc3, 0xb6, 0x71, 0x35, 0xa6, 0x69, 0x4f,
|
||||
0xf8, 0x23, 0x05, 0xd9, 0xba, 0x5c, 0x61, 0x5f, 0xa2, 0x54, 0xd2, 0xb1, 0x83, 0x45, 0x83, 0xce,
|
||||
0xe4, 0x2d, 0x44, 0x26, 0xc8, 0x35, 0xa7, 0xa5, 0xf6, 0xc8, 0x42, 0x1c, 0x0d, 0xa3, 0xf1, 0xc7,
|
||||
0x00, 0x50, 0xf2, 0xe5, 0x17, 0xf8, 0xd0, 0xfa, 0x77, 0x8d, 0xfb, 0x82, 0x8d, 0x40, 0xc7, 0x8e,
|
||||
0x94, 0x1e, 0x1e, 0x1e};
|
32
lib/playfair/omg_hax.h
Normal file
32
lib/playfair/omg_hax.h
Normal file
File diff suppressed because one or more lines are too long
31
lib/playfair/playfair.c
Normal file
31
lib/playfair/playfair.c
Normal file
@ -0,0 +1,31 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "playfair.h"
|
||||
|
||||
void generate_key_schedule(unsigned char* key_material, uint32_t key_schedule[11][4]);
|
||||
void generate_session_key(unsigned char* oldSap, unsigned char* messageIn, unsigned char* sessionKey);
|
||||
void cycle(unsigned char* block, uint32_t key_schedule[11][4]);
|
||||
void z_xor(unsigned char* in, unsigned char* out, int blocks);
|
||||
void x_xor(unsigned char* in, unsigned char* out, int blocks);
|
||||
|
||||
extern unsigned char default_sap[];
|
||||
|
||||
void playfair_decrypt(unsigned char* message3, unsigned char* cipherText, unsigned char* keyOut)
|
||||
{
|
||||
unsigned char* chunk1 = &cipherText[16];
|
||||
unsigned char* chunk2 = &cipherText[56];
|
||||
int i;
|
||||
unsigned char blockIn[16];
|
||||
unsigned char sapKey[16];
|
||||
uint32_t key_schedule[11][4];
|
||||
generate_session_key(default_sap, message3, sapKey);
|
||||
generate_key_schedule(sapKey, key_schedule);
|
||||
z_xor(chunk2, blockIn, 1);
|
||||
cycle(blockIn, key_schedule);
|
||||
for (i = 0; i < 16; i++) {
|
||||
keyOut[i] = blockIn[i] ^ chunk1[i];
|
||||
}
|
||||
x_xor(keyOut, keyOut, 1);
|
||||
z_xor(keyOut, keyOut, 1);
|
||||
}
|
||||
|
6
lib/playfair/playfair.h
Normal file
6
lib/playfair/playfair.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef PLAYFAIR_H
|
||||
#define PLAYFAIR_H
|
||||
|
||||
void playfair_decrypt(unsigned char* message3, unsigned char* cipherText, unsigned char* keyOut);
|
||||
|
||||
#endif
|
96
lib/playfair/sap_hash.c
Normal file
96
lib/playfair/sap_hash.c
Normal file
@ -0,0 +1,96 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define printf(...) (void)0;
|
||||
|
||||
void garble(unsigned char*, unsigned char*, unsigned char*, unsigned char*, unsigned char*);
|
||||
|
||||
unsigned char rol8(unsigned char input, int count)
|
||||
{
|
||||
return ((input << count) & 0xff) | (input & 0xff) >> (8-count);
|
||||
}
|
||||
|
||||
uint32_t rol8x(unsigned char input, int count)
|
||||
{
|
||||
return ((input << count)) | (input) >> (8-count);
|
||||
}
|
||||
|
||||
|
||||
void sap_hash(unsigned char* blockIn, unsigned char* keyOut)
|
||||
{
|
||||
uint32_t* block_words = (uint32_t*)blockIn;
|
||||
uint32_t* out_words = (uint32_t*)keyOut;
|
||||
unsigned char buffer0[20] = {0x96, 0x5F, 0xC6, 0x53, 0xF8, 0x46, 0xCC, 0x18, 0xDF, 0xBE, 0xB2, 0xF8, 0x38, 0xD7, 0xEC, 0x22, 0x03, 0xD1, 0x20, 0x8F};
|
||||
unsigned char buffer1[210];
|
||||
unsigned char buffer2[35] = {0x43, 0x54, 0x62, 0x7A, 0x18, 0xC3, 0xD6, 0xB3, 0x9A, 0x56, 0xF6, 0x1C, 0x14, 0x3F, 0x0C, 0x1D, 0x3B, 0x36, 0x83, 0xB1, 0x39, 0x51, 0x4A, 0xAA, 0x09, 0x3E, 0xFE, 0x44, 0xAF, 0xDE, 0xC3, 0x20, 0x9D, 0x42, 0x3A};
|
||||
unsigned char buffer3[132];
|
||||
unsigned char buffer4[21] = {0xED, 0x25, 0xD1, 0xBB, 0xBC, 0x27, 0x9F, 0x02, 0xA2, 0xA9, 0x11, 0x00, 0x0C, 0xB3, 0x52, 0xC0, 0xBD, 0xE3, 0x1B, 0x49, 0xC7};
|
||||
int i0_index[11] = {18, 22, 23, 0, 5, 19, 32, 31, 10, 21, 30};
|
||||
uint8_t w,x,y,z;
|
||||
int i, j;
|
||||
|
||||
// Load the input into the buffer
|
||||
for (i = 0; i < 210; i++)
|
||||
{
|
||||
// We need to swap the byte order around so it is the right endianness
|
||||
uint32_t in_word = block_words[((i % 64)>>2)];
|
||||
uint32_t in_byte = (in_word >> ((3-(i % 4)) << 3)) & 0xff;
|
||||
buffer1[i] = in_byte;
|
||||
}
|
||||
// Next a scrambling
|
||||
for (i = 0; i < 840; i++)
|
||||
{
|
||||
// We have to do unsigned, 32-bit modulo, or we get the wrong indices
|
||||
x = buffer1[((i-155) & 0xffffffff) % 210];
|
||||
y = buffer1[((i-57) & 0xffffffff) % 210];
|
||||
z = buffer1[((i-13) & 0xffffffff) % 210];
|
||||
w = buffer1[(i & 0xffffffff) % 210];
|
||||
buffer1[i % 210] = (rol8(y, 5) + (rol8(z, 3) ^ w) - rol8(x,7)) & 0xff;
|
||||
}
|
||||
printf("Garbling...\n");
|
||||
// I have no idea what this is doing (yet), but it gives the right output
|
||||
garble(buffer0, buffer1, buffer2, buffer3, buffer4);
|
||||
|
||||
// Fill the output with 0xE1
|
||||
for (i = 0; i < 16; i++)
|
||||
keyOut[i] = 0xE1;
|
||||
|
||||
// Now we use all the buffers we have calculated to grind out the output. First buffer3
|
||||
for (i = 0; i < 11; i++)
|
||||
{
|
||||
// Note that this is addition (mod 255) and not XOR
|
||||
// Also note that we only use certain indices
|
||||
// And that index 3 is hard-coded to be 0x3d (Maybe we can hack this up by changing buffer3[0] to be 0xdc?
|
||||
if (i == 3)
|
||||
keyOut[i] = 0x3d;
|
||||
else
|
||||
keyOut[i] = ((keyOut[i] + buffer3[i0_index[i] * 4]) & 0xff);
|
||||
}
|
||||
|
||||
// Then buffer0
|
||||
for (i = 0; i < 20; i++)
|
||||
keyOut[i % 16] ^= buffer0[i];
|
||||
|
||||
// Then buffer2
|
||||
for (i = 0; i < 35; i++)
|
||||
keyOut[i % 16] ^= buffer2[i];
|
||||
|
||||
// Do buffer1
|
||||
for (i = 0; i < 210; i++)
|
||||
keyOut[(i % 16)] ^= buffer1[i];
|
||||
|
||||
|
||||
// Now we do a kind of reverse-scramble
|
||||
for (j = 0; j < 16; j++)
|
||||
{
|
||||
for (i = 0; i < 16; i++)
|
||||
{
|
||||
x = keyOut[((i-7) & 0xffffffff) % 16];
|
||||
y = keyOut[i % 16];
|
||||
z = keyOut[((i-37) & 0xffffffff) % 16];
|
||||
w = keyOut[((i-177) & 0xffffffff) % 16];
|
||||
keyOut[i] = rol8(x, 1) ^ y ^ rol8(z, 6) ^ rol8(w, 5);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user