5 Commits

Author SHA1 Message Date
waterjuice
e39760a850 Version 2.3.0
* Added AES-CBC module.
* Added functions Md5Calculate, Sha1Calculate, Sha256Calculate, and
  Sha512Calculate to calculate a hash in one call.
* Added function Rc4XorWithKey to encrypt/decrypt a buffer with RC4 in
  one call.
* Bugfix: AesInitialise now returns -1 if invalid key size is provided.
  Previously it would return 0 despite what was documented.
2018-03-16 13:30:49 +11:00
waterjuice
1683e5d9f6 Version 2.2.0
* Added AES-OFB module.
* File names have been changed to have the prefix `WjCryptLib_` rather
than `CryptLib_`.
* Removed compiled binaries from source tree.
2018-01-07 18:00:57 +11:00
waterjuice
3201fb4d83 Version 2.1.0
* Changed implementation of AES to one which is almost 5 times as fast. The new implementation comes from LibTomCrypt. The newer implementation produces a larger binary size as a trade-off.
* AES-CTR module now supports OpenMP and when compiled with OpenMP will run in parallel giving a much greater speed.
* Changed interface for Initialisation functions for both AES and AES-CTR to be match RC4 (The context is first parameter not last)
2017-12-11 11:22:47 +11:00
WaterJuice
33bf2dc97f Delete README.md 2017-12-02 00:23:11 +11:00
zebra
f75d43bb19 Version 2.0.0
* Added AES and AES-CTR modules. AES-CTR conforms to the same counter
mode used with AES in *OpenSSL*.
* All algorithms now work on Big-Endian architectures.
* Now uses CMake for building rather than make files and Visual Studio
projects. CMake will generate whatever system is required.
* Input function parameters are now marked `const`
* File names have been changed to have the prefix `CryptLib_` rather
than `Lib`.
* Various formatting changes to the files.
2017-12-02 00:16:32 +11:00
92 changed files with 5223 additions and 1669 deletions

38
CMakeLists.txt Normal file
View File

@@ -0,0 +1,38 @@
cmake_minimum_required(VERSION 3.6.0)
project( WjCryptLib )
# WjCryptLib Static Library
add_library( WjCryptLib STATIC
lib/WjCryptLib_Aes.h
lib/WjCryptLib_Aes.c
lib/WjCryptLib_AesCbc.h
lib/WjCryptLib_AesCbc.c
lib/WjCryptLib_AesCtr.h
lib/WjCryptLib_AesCtr.c
lib/WjCryptLib_AesOfb.h
lib/WjCryptLib_AesOfb.c
lib/WjCryptLib_Md5.h
lib/WjCryptLib_Md5.c
lib/WjCryptLib_Rc4.h
lib/WjCryptLib_Rc4.c
lib/WjCryptLib_Sha1.h
lib/WjCryptLib_Sha1.c
lib/WjCryptLib_Sha256.h
lib/WjCryptLib_Sha256.c
lib/WjCryptLib_Sha512.h
lib/WjCryptLib_Sha512.c )
target_include_directories( WjCryptLib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/lib )
set_target_properties ( WjCryptLib PROPERTIES FOLDER lib )
# Add the demo project directories
add_subdirectory( projects/WjCryptLibTest )
add_subdirectory( projects/Md5String )
add_subdirectory( projects/Rc4Output )
add_subdirectory( projects/Sha1String )
add_subdirectory( projects/Sha256String )
add_subdirectory( projects/Sha512String )
add_subdirectory( projects/AesBlock )
add_subdirectory( projects/AesCtrOutput )
add_subdirectory( projects/AesOfbOutput )

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,85 +0,0 @@
Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ProofOfWork", "VSProjectFiles\ProofOfWork.vcxproj", "{78DE2AA5-4DEB-4601-AAB8-A3452BAC810C}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Md5String", "VSProjectFiles\Md5String.vcxproj", "{2F5CA1B6-92CC-4142-86AC-62FADFF0B834}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Rc4Output", "VSProjectFiles\Rc4Output.vcxproj", "{E5A739D1-4246-4FB1-8AA4-673068EDE33A}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Sha1String", "VSProjectFiles\Sha1String.vcxproj", "{B01EA8DA-3ABB-410A-B28E-8336467F94F8}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Sha256String", "VSProjectFiles\Sha256String.vcxproj", "{43164E66-9EA5-48E5-9708-6AF8C3642F6E}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Sha512String", "VSProjectFiles\Sha512String.vcxproj", "{FC8B119A-DF2A-48A6-B834-F1CDF80AECB6}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CryptLibTest", "VSProjectFiles\CryptLibTest.vcxproj", "{543C3780-F2F4-47BA-8263-278B911A73AC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Debug|x64 = Debug|x64
Release|Win32 = Release|Win32
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{78DE2AA5-4DEB-4601-AAB8-A3452BAC810C}.Debug|Win32.ActiveCfg = Debug|Win32
{78DE2AA5-4DEB-4601-AAB8-A3452BAC810C}.Debug|Win32.Build.0 = Debug|Win32
{78DE2AA5-4DEB-4601-AAB8-A3452BAC810C}.Debug|x64.ActiveCfg = Debug|x64
{78DE2AA5-4DEB-4601-AAB8-A3452BAC810C}.Debug|x64.Build.0 = Debug|x64
{78DE2AA5-4DEB-4601-AAB8-A3452BAC810C}.Release|Win32.ActiveCfg = Release|Win32
{78DE2AA5-4DEB-4601-AAB8-A3452BAC810C}.Release|Win32.Build.0 = Release|Win32
{78DE2AA5-4DEB-4601-AAB8-A3452BAC810C}.Release|x64.ActiveCfg = Release|x64
{78DE2AA5-4DEB-4601-AAB8-A3452BAC810C}.Release|x64.Build.0 = Release|x64
{2F5CA1B6-92CC-4142-86AC-62FADFF0B834}.Debug|Win32.ActiveCfg = Debug|Win32
{2F5CA1B6-92CC-4142-86AC-62FADFF0B834}.Debug|Win32.Build.0 = Debug|Win32
{2F5CA1B6-92CC-4142-86AC-62FADFF0B834}.Debug|x64.ActiveCfg = Debug|x64
{2F5CA1B6-92CC-4142-86AC-62FADFF0B834}.Debug|x64.Build.0 = Debug|x64
{2F5CA1B6-92CC-4142-86AC-62FADFF0B834}.Release|Win32.ActiveCfg = Release|Win32
{2F5CA1B6-92CC-4142-86AC-62FADFF0B834}.Release|Win32.Build.0 = Release|Win32
{2F5CA1B6-92CC-4142-86AC-62FADFF0B834}.Release|x64.ActiveCfg = Release|x64
{2F5CA1B6-92CC-4142-86AC-62FADFF0B834}.Release|x64.Build.0 = Release|x64
{E5A739D1-4246-4FB1-8AA4-673068EDE33A}.Debug|Win32.ActiveCfg = Debug|Win32
{E5A739D1-4246-4FB1-8AA4-673068EDE33A}.Debug|Win32.Build.0 = Debug|Win32
{E5A739D1-4246-4FB1-8AA4-673068EDE33A}.Debug|x64.ActiveCfg = Debug|x64
{E5A739D1-4246-4FB1-8AA4-673068EDE33A}.Debug|x64.Build.0 = Debug|x64
{E5A739D1-4246-4FB1-8AA4-673068EDE33A}.Release|Win32.ActiveCfg = Release|Win32
{E5A739D1-4246-4FB1-8AA4-673068EDE33A}.Release|Win32.Build.0 = Release|Win32
{E5A739D1-4246-4FB1-8AA4-673068EDE33A}.Release|x64.ActiveCfg = Release|x64
{E5A739D1-4246-4FB1-8AA4-673068EDE33A}.Release|x64.Build.0 = Release|x64
{B01EA8DA-3ABB-410A-B28E-8336467F94F8}.Debug|Win32.ActiveCfg = Debug|Win32
{B01EA8DA-3ABB-410A-B28E-8336467F94F8}.Debug|Win32.Build.0 = Debug|Win32
{B01EA8DA-3ABB-410A-B28E-8336467F94F8}.Debug|x64.ActiveCfg = Debug|x64
{B01EA8DA-3ABB-410A-B28E-8336467F94F8}.Debug|x64.Build.0 = Debug|x64
{B01EA8DA-3ABB-410A-B28E-8336467F94F8}.Release|Win32.ActiveCfg = Release|Win32
{B01EA8DA-3ABB-410A-B28E-8336467F94F8}.Release|Win32.Build.0 = Release|Win32
{B01EA8DA-3ABB-410A-B28E-8336467F94F8}.Release|x64.ActiveCfg = Release|x64
{B01EA8DA-3ABB-410A-B28E-8336467F94F8}.Release|x64.Build.0 = Release|x64
{43164E66-9EA5-48E5-9708-6AF8C3642F6E}.Debug|Win32.ActiveCfg = Debug|Win32
{43164E66-9EA5-48E5-9708-6AF8C3642F6E}.Debug|Win32.Build.0 = Debug|Win32
{43164E66-9EA5-48E5-9708-6AF8C3642F6E}.Debug|x64.ActiveCfg = Debug|x64
{43164E66-9EA5-48E5-9708-6AF8C3642F6E}.Debug|x64.Build.0 = Debug|x64
{43164E66-9EA5-48E5-9708-6AF8C3642F6E}.Release|Win32.ActiveCfg = Release|Win32
{43164E66-9EA5-48E5-9708-6AF8C3642F6E}.Release|Win32.Build.0 = Release|Win32
{43164E66-9EA5-48E5-9708-6AF8C3642F6E}.Release|x64.ActiveCfg = Release|x64
{43164E66-9EA5-48E5-9708-6AF8C3642F6E}.Release|x64.Build.0 = Release|x64
{FC8B119A-DF2A-48A6-B834-F1CDF80AECB6}.Debug|Win32.ActiveCfg = Debug|Win32
{FC8B119A-DF2A-48A6-B834-F1CDF80AECB6}.Debug|Win32.Build.0 = Debug|Win32
{FC8B119A-DF2A-48A6-B834-F1CDF80AECB6}.Debug|x64.ActiveCfg = Debug|x64
{FC8B119A-DF2A-48A6-B834-F1CDF80AECB6}.Debug|x64.Build.0 = Debug|x64
{FC8B119A-DF2A-48A6-B834-F1CDF80AECB6}.Release|Win32.ActiveCfg = Release|Win32
{FC8B119A-DF2A-48A6-B834-F1CDF80AECB6}.Release|Win32.Build.0 = Release|Win32
{FC8B119A-DF2A-48A6-B834-F1CDF80AECB6}.Release|x64.ActiveCfg = Release|x64
{FC8B119A-DF2A-48A6-B834-F1CDF80AECB6}.Release|x64.Build.0 = Release|x64
{543C3780-F2F4-47BA-8263-278B911A73AC}.Debug|Win32.ActiveCfg = Debug|Win32
{543C3780-F2F4-47BA-8263-278B911A73AC}.Debug|Win32.Build.0 = Debug|Win32
{543C3780-F2F4-47BA-8263-278B911A73AC}.Debug|x64.ActiveCfg = Debug|x64
{543C3780-F2F4-47BA-8263-278B911A73AC}.Debug|x64.Build.0 = Debug|x64
{543C3780-F2F4-47BA-8263-278B911A73AC}.Release|Win32.ActiveCfg = Release|Win32
{543C3780-F2F4-47BA-8263-278B911A73AC}.Release|Win32.Build.0 = Release|Win32
{543C3780-F2F4-47BA-8263-278B911A73AC}.Release|x64.ActiveCfg = Release|x64
{543C3780-F2F4-47BA-8263-278B911A73AC}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@@ -1,90 +0,0 @@
CryptLib
========
CryptLib is a collection of cryptographic functions written in C. Each
module is fully independent and requires only a single .c file and a
a single .h file. The functions are designed to be portable, however
they do require a Little-Endian processor.
The makefile is for gnu make and works on Linux, OSX, Cygwin.
It also works on Windows with VSS if cygwin (or some other gnu make) is
setup and the environment has been setup correctly.
A Visual Studio 2010 solution file also exists, which has project files.
This can be used instead of the makefile for VS2010.
*Created June 2013*
Version 1.0.0 - June 2013
-------------------------
To use the library functions, only the following files are required,
depending on what cryptographic functions are wanted.
* MD5 - (LibMd5.h, and LibMd5.c)
* SHA1 - (LibSha1.h, and LibSha1.c)
* SHA256 - (LibSha256.h, and LibSha256.c)
* SHA512 - (LibSha512.h, and LibSha512.c)
* RC4 - (LibRc4.h, and LibRc4.c)
Test Programs
-------------
In the projects directory there are several programs that compile to
command line executables. One is CryptLibTest. This tests the algorithms
against known test vectors. If compiling on a different system this
is useful to verify that the results are still valid. For example if you
compile on a Big-Endian system then some of the functions will undoubtable
fail. The test program can be used to verify that the correct modifications
have been made if you wish to adapt the fiules to Big-Endian.
Additionally there are sample programs that demonstrate the functions. For
each of the hash functions there is a program that creates a hash from a
string given on command line. For RC4 there is a program that outputs
the stream in hex.
* Md5String
* Sha1String
* Sha256String
* Sha512String
* Rc4Output
Executables
-----------
Included in the Exe directory are executables of the above programs for Windows,
OSX, and Linux. All of them are compiled for x64 versions of the operating
systems. The Windows one is compiled for Vista and greater. The Linux binaries
are comiled on Ubuntu 12.04. The binaries are built with a dependncy on GLICC2.14
which means it will only load on fairly new versions of Linux. However linux
is the easiest system to build form source as almost every linux platform will
have make and gcc already installed.
License
=======
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>

146
ReadMe.md Normal file
View File

@@ -0,0 +1,146 @@
WjCryptLib
==========
WjCryptLib is a collection of cryptographic functions written in C. Each
module is fully independent and generally requires only a single .c file
and a a single .h file. AES-CTR does depend on the AES module, so in this
case all four files are needed.
The functions are designed to be portable and have been tested on both
a Little-Endian and a Big-Endian architecture. OpenMP is supported where
parallelisation can occur.
The library and the demo programs can be built using CMake to generate
a build setup for any system, including Visual Studio on Windows and
Make or Ninja for Linux. Refer to cmake.org to get CMake.
*Placed into Public Domain by WaterJuice 2013 - 2018*
Library
-------
To use the library functions, only the following files are required,
depending on what cryptographic functions are wanted.
* MD5 - (WjCryptLib_Md5.h, and WjCryptLib_Md5.c)
* SHA1 - (WjCryptLib_Sha1.h, and WjCryptLib_Sha1.c)
* SHA256 - (WjCryptLib_Sha256.h, and WjCryptLib_Sha256.c)
* SHA512 - (WjCryptLib_Sha512.h, and WjCryptLib_Sha512.c)
* RC4 - (WjCryptLib_Rc4.h, and WjCryptLib_Rc4.c)
* AES - (WjCryptLib_Aes.h, and WjCryptLib_Aes.c)
* AES-CTR - (WjCryptLib_AesCtr.h, and WjCryptLib_AesCtr.c, WjCryptLib_Aes.h,
and WjCryptLib_Aes.c)
* AES-OFB - (WjCryptLib_AesOfb.h, and WjCryptLib_AesOfb.c, WjCryptLib_Aes.h,
and WjCryptLib_Aes.c)
* AES-CBC - (WjCryptLib_AesCbc.h, and WjCryptLib_AesCbc.c, WjCryptLib_Aes.h,
and WjCryptLib_Aes.c)
Version 2.3.0 - March 2018
--------------------------
* Added AES-CBC module.
* Added functions Md5Calculate, Sha1Calculate, Sha256Calculate, and
Sha512Calculate to calculate a hash in one call.
* Added function Rc4XorWithKey to encrypt/decrypt a buffer with RC4 in
one call.
* Bugfix: AesInitialise now returns -1 if invalid key size is provided.
Previously it would return 0 despite what was documented.
Version 2.2.0 - January 2018
----------------------------
* Added AES-OFB module.
* File names have been changed to have the prefix `WjCryptLib_` rather
than `CryptLib_`.
* Removed compiled binaries from source tree.
Version 2.1.0 - December 2017
-----------------------------
Changes:
* Changed implementation of AES to one which is almost 5 times as fast.
The new implementation comes from LibTomCrypt. The newer implementation
produces a larger binary size as a trade-off.
* AES-CTR module now supports OpenMP and when compiled with OpenMP will
run in parallel giving a much greater speed.
* Changed interface for Initialisation functions for both AES and AES-CTR
to be match RC4 (The context is first parameter not last)
Version 2.0.0 - December 2017
-----------------------------
Changes:
* Added AES and AES-CTR modules. AES-CTR conforms to the same counter
mode used with AES in *OpenSSL*.
* All algorithms now work on Big-Endian architectures.
* Now uses CMake for building rather than make files and Visual Studio
projects. CMake will generate whatever system is required.
* Input function parameters are now marked `const`
* File names have been changed to have the prefix `CryptLib_` rather
than `Lib`.
* Various formatting changes to the files.
Version 1.0.0 - June 2013
-------------------------
Contains following algorithms:
* MD5
* SHA1
* SHA256
* SHA512
* RC4
Test Programs
-------------
In the projects directory there are several programs that compile to
command line executables. One is WjCryptLibTest. This tests the algorithms
against known test vectors. If compiling on a different system this
is useful to verify that the results are still valid.
Additionally there are sample programs that demonstrate the functions. For
each of the hash functions there is a program that creates a hash from a
string given on command line. For RC4 and AES-CTR there are programs that
output the stream in hex.
* Md5String
* Sha1String
* Sha256String
* Sha512String
* Rc4Output
* AesBlock
* AesCtrOutput
* AesOfbOutput
Unlicense
=========
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>

View File

@@ -1,76 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\lib\LibMd5.c" />
<ClCompile Include="..\lib\LibRc4.c" />
<ClCompile Include="..\lib\LibSha1.c" />
<ClCompile Include="..\lib\LibSha256.c" />
<ClCompile Include="..\lib\LibSha512.c" />
<ClCompile Include="..\projects\CryptLibTest\CryptLibTest.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibMd5.h" />
<ClInclude Include="..\lib\LibRc4.h" />
<ClInclude Include="..\lib\LibSha1.h" />
<ClInclude Include="..\lib\LibSha256.h" />
<ClInclude Include="..\lib\LibSha512.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{543C3780-F2F4-47BA-8263-278B911A73AC}</ProjectGuid>
<RootNamespace>CryptLibTest</RootNamespace>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<_ProjectFileVersion>10.0.40219.1</_ProjectFileVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -1,49 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="lib">
<UniqueIdentifier>{aabf3f27-af59-42e0-94ef-f81adde8e682}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\lib\LibRc4.c">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="..\lib\LibMd5.c">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="..\lib\LibSha1.c">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="..\lib\LibSha256.c">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="..\lib\LibSha512.c">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="..\projects\CryptLibTest\CryptLibTest.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibRc4.h">
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\lib\LibMd5.h">
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\lib\LibSha1.h">
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\lib\LibSha256.h">
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\lib\LibSha512.h">
<Filter>lib</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -1,68 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\lib\LibMd5.c" />
<ClCompile Include="..\projects\Md5String\Md5String.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibMd5.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{2F5CA1B6-92CC-4142-86AC-62FADFF0B834}</ProjectGuid>
<RootNamespace>Md5String</RootNamespace>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<_ProjectFileVersion>10.0.40219.1</_ProjectFileVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -1,25 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="lib">
<UniqueIdentifier>{aabf3f27-af59-42e0-94ef-f81adde8e682}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\lib\LibMd5.c">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="..\projects\Md5String\Md5String.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibMd5.h">
<Filter>lib</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -1,78 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\lib\LibMd5.c" />
<ClCompile Include="..\lib\LibRc4.c" />
<ClCompile Include="..\lib\LibSha1.c" />
<ClCompile Include="..\lib\LibSha256.c" />
<ClCompile Include="..\lib\LibSha512.c" />
<ClCompile Include="..\lib\LibThread.c" />
<ClCompile Include="..\projects\ProofOfWork\ProofOfWork.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibMd5.h" />
<ClInclude Include="..\lib\LibRc4.h" />
<ClInclude Include="..\lib\LibSha1.h" />
<ClInclude Include="..\lib\LibSha256.h" />
<ClInclude Include="..\lib\LibThread.h" />
<ClInclude Include="..\projects\ProofOfWork\CreateFindProofOfWorkThreadFunction.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{78DE2AA5-4DEB-4601-AAB8-A3452BAC810C}</ProjectGuid>
<RootNamespace>ProofOfWork</RootNamespace>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<_ProjectFileVersion>10.0.40219.1</_ProjectFileVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -1,55 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="lib">
<UniqueIdentifier>{6ec1a200-d167-4831-b36c-9ddd0b2ba00a}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\lib\LibMd5.c">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="..\lib\LibRc4.c">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="..\lib\LibThread.c">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="..\lib\LibSha1.c">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="..\lib\LibSha256.c">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="..\lib\LibSha512.c">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="..\projects\ProofOfWork\ProofOfWork.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibMd5.h">
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\lib\LibRc4.h">
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\lib\LibThread.h">
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\lib\LibSha1.h">
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\lib\LibSha256.h">
<Filter>lib</Filter>
</ClInclude>
<ClInclude Include="..\projects\ProofOfWork\CreateFindProofOfWorkThreadFunction.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -1,68 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\lib\LibRc4.c" />
<ClCompile Include="..\projects\Rc4Output\Rc4Output.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibRc4.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{E5A739D1-4246-4FB1-8AA4-673068EDE33A}</ProjectGuid>
<RootNamespace>Rc4Output</RootNamespace>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<_ProjectFileVersion>10.0.40219.1</_ProjectFileVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -1,25 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="lib">
<UniqueIdentifier>{aabf3f27-af59-42e0-94ef-f81adde8e682}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\lib\LibRc4.c">
<Filter>lib</Filter>
</ClCompile>
<ClCompile Include="..\projects\Rc4Output\Rc4Output.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibRc4.h">
<Filter>lib</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -1,68 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\lib\LibSha1.c" />
<ClCompile Include="..\projects\Sha1String\Sha1String.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibSha1.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{B01EA8DA-3ABB-410A-B28E-8336467F94F8}</ProjectGuid>
<RootNamespace>Sha1String</RootNamespace>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<_ProjectFileVersion>10.0.40219.1</_ProjectFileVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -1,29 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="lib">
<UniqueIdentifier>{aabf3f27-af59-42e0-94ef-f81adde8e682}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Sha1String.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\LibSha1.c">
<Filter>lib</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibSha1.h">
<Filter>lib</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -1,68 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\lib\LibSha256.c" />
<ClCompile Include="..\projects\Sha256String\Sha256String.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibSha256.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{43164E66-9EA5-48E5-9708-6AF8C3642F6E}</ProjectGuid>
<RootNamespace>Sha256String</RootNamespace>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<_ProjectFileVersion>10.0.40219.1</_ProjectFileVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -1,29 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="lib">
<UniqueIdentifier>{aabf3f27-af59-42e0-94ef-f81adde8e682}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Sha256String.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\LibSha256.c">
<Filter>lib</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibSha256.h">
<Filter>lib</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -1,68 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\lib\LibSha512.c" />
<ClCompile Include="..\projects\Sha512String\Sha512String.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibSha512.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{FC8B119A-DF2A-48A6-B834-F1CDF80AECB6}</ProjectGuid>
<RootNamespace>Sha512String</RootNamespace>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="props\$(Platform)$(Configuration).props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<_ProjectFileVersion>10.0.40219.1</_ProjectFileVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -1,29 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="lib">
<UniqueIdentifier>{aabf3f27-af59-42e0-94ef-f81adde8e682}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Sha512String.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\lib\LibSha512.c">
<Filter>lib</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\lib\LibSha512.h">
<Filter>lib</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -1,42 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets" />
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<OutDir>$(SolutionDir)Binaries\$(Platform)$(Configuration)\</OutDir>
</PropertyGroup>
<PropertyGroup>
<IntDir>$(SolutionDir)Build\$(Platform)$(Configuration)\$(ProjectName)\</IntDir>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<WarningLevel>Level4</WarningLevel>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup>
<ClCompile>
<TreatWarningAsError>true</TreatWarningAsError>
<EnablePREfast>true</EnablePREfast>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<Optimization>Full</Optimization>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
<WholeProgramOptimization>true</WholeProgramOptimization>
<PreprocessorDefinitions>_WIN32_WINNT=0x0600;_CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
<IntrinsicFunctions>true</IntrinsicFunctions>
<AdditionalIncludeDirectories>$(SolutionDir)\lib;$(SolutionDir)\stdbool</AdditionalIncludeDirectories>
</ClCompile>
<Lib>
<OutputFile>$(IntDir)$(TargetName)$(TargetExt)</OutputFile>
</Lib>
<Link>
<Version>6.0</Version>
</Link>
<Link>
<LinkStatus>false</LinkStatus>
<TreatLinkerWarningAsErrors>true</TreatLinkerWarningAsErrors>
<SetChecksum>true</SetChecksum>
</Link>
</ItemDefinitionGroup>
<ItemGroup />
</Project>

View File

@@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
<PropertyGroup>
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
<ClCompile>
<StringPooling>false</StringPooling>
</ClCompile>
<ClCompile>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<Optimization>Disabled</Optimization>
<InlineFunctionExpansion>Disabled</InlineFunctionExpansion>
</ClCompile>
<ClCompile />
</ItemDefinitionGroup>
</Project>

View File

@@ -1,3 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>

View File

@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets">
<Import Project="All.props" />
<Import Project="Win32.props" />
<Import Project="Debug.props" />
</ImportGroup>
</Project>

View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets">
<Import Project="All.props" />
<Import Project="Win32.props" />
</ImportGroup>
</Project>

View File

@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemDefinitionGroup>
<ClCompile>
<EnablePREfast>false</EnablePREfast>
</ClCompile>
</ItemDefinitionGroup>
</Project>

View File

@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets">
<Import Project="All.props" />
<Import Project="x64.props" />
<Import Project="Debug.props" />
</ImportGroup>
</Project>

View File

@@ -1,7 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ImportGroup Label="PropertySheets">
<Import Project="All.props" />
<Import Project="x64.props" />
</ImportGroup>
</Project>

View File

@@ -1,78 +0,0 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LibRC4
//
// An implementation of RC4 stream cipher
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _LibRc4_h_
#define _LibRc4_h_
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Context - This must be initialised using Rc4Initialised. Do not modify the contents of this structure directly.
typedef struct
{
uint32_t i;
uint32_t j;
uint8_t S[256];
} Rc4Context;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Initialise
//
// Initialises an RC4 cipher and discards the specified number of first bytes.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4Initialise
(
Rc4Context* Context,
void* Key,
uint32_t KeySize,
uint32_t DropN
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Output
//
// Outputs the requested number of bytes from the RC4 stream
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4Output
(
Rc4Context* Context,
void* Buffer,
uint32_t Size
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Xor
//
// XORs the RC4 stream with an input buffer and puts the results in an output buffer. This is used for encrypting
// and decrypting data. InBuffer and OutBuffer can point to the same location for inplace encrypting/decrypting
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4Xor
(
Rc4Context* Context,
void* InBuffer,
void* OutBuffer,
uint32_t Size
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif //_LibRc4_h_

1010
lib/WjCryptLib_Aes.c Normal file

File diff suppressed because it is too large Load Diff

120
lib/WjCryptLib_Aes.h Normal file
View File

@@ -0,0 +1,120 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_Aes
//
// Implementation of AES block cipher. This implementation was modified from LibTomCrypt written by Tom St Denis
// (https://github.com/libtom). Modified by WaterJuice retaining Public Domain license.
// Derived from Public Domain source by original authors:
// Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
// Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
// Paulo Barreto <paulo.barreto@terra.com.br>
//
// AES is a block cipher that operates on 128 bit blocks. Encryption an Decryption routines use an AesContext which
// must be initialised with the key. An AesContext can be initialised with a 128, 192, or 256 bit key. Use the
// AesInitialise[n] functions to initialise the context with the key. Once an AES context is initialised its contents
// are not changed by the encrypting and decrypting functions. A context only needs to be initialised once for any
// given key and the context may be used by the encrypt/decrypt functions in simultaneous threads.
// All operations are performed BYTE wise and this implementation works in both little and endian processors.
// There are no alignment requirements with the keys and data blocks.
//
// This is free and unencumbered software released into the public domain - December 2017 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define AES_KEY_SIZE_128 16
#define AES_KEY_SIZE_192 24
#define AES_KEY_SIZE_256 32
#define AES_BLOCK_SIZE 16
// AesContext - This must be initialised using AesInitialise128, AesInitialise192 or AesInitialise256
// Do not modify the contents of this structure directly.
typedef struct
{
uint32_t eK[60];
uint32_t dK[60];
uint_fast32_t Nr;
} AesContext;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesInitialise
//
// Initialises an AesContext with an AES Key. KeySize must be 16, 24, or 32 (for 128, 192, or 256 bit key size)
// Returns 0 if successful, or -1 if invalid KeySize provided
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesInitialise
(
AesContext* Context, // [out]
void const* Key, // [in]
uint32_t KeySize // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesEncrypt
//
// Performs an AES encryption of one block (128 bits) with the AesContext initialised with one of the functions
// AesInitialise[n]. Input and Output can point to same memory location, however it is more efficient to use
// AesEncryptInPlace in this situation.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesEncrypt
(
AesContext const* Context, // [in]
uint8_t const Input [AES_BLOCK_SIZE], // [in]
uint8_t Output [AES_BLOCK_SIZE] // [out]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesDecrypt
//
// Performs an AES decryption of one block (128 bits) with the AesContext initialised with one of the functions
// AesInitialise[n]. Input and Output can point to same memory location, however it is more efficient to use
// AesDecryptInPlace in this situation.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesDecrypt
(
AesContext const* Context, // [in]
uint8_t const Input [AES_BLOCK_SIZE], // [in]
uint8_t Output [AES_BLOCK_SIZE] // [out]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesEncryptInPlace
//
// Performs an AES encryption of one block (128 bits) with the AesContext initialised with one of the functions
// AesInitialise[n]. The encryption is performed in place.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesEncryptInPlace
(
AesContext const* Context, // [in]
uint8_t Block [AES_BLOCK_SIZE] // [in out]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesDecryptInPlace
//
// Performs an AES decryption of one block (128 bits) with the AesContext initialised with one of the functions
// AesInitialise[n]. The decryption is performed in place.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesDecryptInPlace
(
AesContext const* Context, // [in]
uint8_t Block [AES_BLOCK_SIZE] // [in out]
);

265
lib/WjCryptLib_AesCbc.c Normal file
View File

@@ -0,0 +1,265 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_AesCbc
//
// Implementation of AES CBC cipher.
//
// Depends on: CryptoLib_Aes
//
// AES CBC is a cipher using AES in Cipher Block Chaining mode. Encryption and decryption must be performed in
// multiples of the AES block size (128 bits).
// This implementation works on both little and big endian architectures.
//
// This is free and unencumbered software released into the public domain - March 2018 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "WjCryptLib_AesCbc.h"
#include "WjCryptLib_Aes.h"
#include <stdint.h>
#include <memory.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MACROS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MIN( x, y ) ( ((x)<(y))?(x):(y) )
#define STORE64H( x, y ) \
{ (y)[0] = (uint8_t)(((x)>>56)&255); (y)[1] = (uint8_t)(((x)>>48)&255); \
(y)[2] = (uint8_t)(((x)>>40)&255); (y)[3] = (uint8_t)(((x)>>32)&255); \
(y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255); \
(y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// XorAesBlock
//
// Takes two source blocks (size AES_BLOCK_SIZE) and XORs them together and puts the result in first block
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void
XorAesBlock
(
uint8_t* Block1, // [in out]
uint8_t const* Block2 // [in]
)
{
uint32_t i;
for( i=0; i<AES_BLOCK_SIZE; i++ )
{
Block1[i] ^= Block2[i];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCbcInitialise
//
// Initialises an AesCbcContext with an already initialised AesContext and a IV. This function can quickly be used
// to change the IV without requiring the more lengthy processes of reinitialising an AES key.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCbcInitialise
(
AesCbcContext* Context, // [out]
AesContext const* InitialisedAesContext, // [in]
uint8_t const IV [AES_CBC_IV_SIZE] // [in]
)
{
// Setup context values
Context->Aes = *InitialisedAesContext;
memcpy( Context->PreviousCipherBlock, IV, sizeof(Context->PreviousCipherBlock) );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCbcInitialiseWithKey
//
// Initialises an AesCbcContext with an AES Key and an IV. This combines the initialising an AES Context and then
// running AesCbcInitialise. KeySize must be 16, 24, or 32 (for 128, 192, or 256 bit key size)
// Returns 0 if successful, or -1 if invalid KeySize provided
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCbcInitialiseWithKey
(
AesCbcContext* Context, // [out]
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_CBC_IV_SIZE] // [in]
)
{
AesContext aes;
// Initialise AES Context
if( 0 != AesInitialise( &aes, Key, KeySize ) )
{
return -1;
}
// Now set-up AesCbcContext
AesCbcInitialise( Context, &aes, IV );
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCbcEncrypt
//
// Encrypts a buffer of data using an AES CBC context. The data buffer must be a multiple of 16 bytes (128 bits)
// in size. The "position" of the context will be advanced by the buffer amount. A buffer can be encrypted in one
// go or in smaller chunks at a time. The result will be the same as long as data is fed into the function in the
// same order.
// InBuffer and OutBuffer can point to the same location for in-place encrypting.
// Returns 0 if successful, or -1 if Size is not a multiple of 16 bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCbcEncrypt
(
AesCbcContext* Context, // [in out]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t Size // [in]
)
{
uint32_t numBlocks = Size / AES_BLOCK_SIZE;
uint32_t offset = 0;
uint32_t i;
if( 0 != Size % AES_BLOCK_SIZE )
{
// Size not a multiple of AES block size (16 bytes).
return -1;
}
for( i=0; i<numBlocks; i++ )
{
// XOR on the next block of data onto the previous cipher block
XorAesBlock( Context->PreviousCipherBlock, (uint8_t*)InBuffer + offset );
// Encrypt to make new cipher block
AesEncryptInPlace( &Context->Aes, Context->PreviousCipherBlock );
// Output cipher block
memcpy( (uint8_t*)OutBuffer + offset, Context->PreviousCipherBlock, AES_BLOCK_SIZE );
offset += AES_BLOCK_SIZE;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCbcDecrypt
//
// Decrypts a buffer of data using an AES CBC context. The data buffer must be a multiple of 16 bytes (128 bits)
// in size. The "position" of the context will be advanced by the buffer amount.
// InBuffer and OutBuffer can point to the same location for in-place decrypting.
// Returns 0 if successful, or -1 if Size is not a multiple of 16 bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCbcDecrypt
(
AesCbcContext* Context, // [in out]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t Size // [in]
)
{
uint32_t numBlocks = Size / AES_BLOCK_SIZE;
uint32_t offset = 0;
uint32_t i;
uint8_t previousCipherBlock [AES_BLOCK_SIZE];
if( 0 != Size % AES_BLOCK_SIZE )
{
// Size not a multiple of AES block size (16 bytes).
return -1;
}
for( i=0; i<numBlocks; i++ )
{
// Copy previous cipher block and place current one in context
memcpy( previousCipherBlock, Context->PreviousCipherBlock, AES_BLOCK_SIZE );
memcpy( Context->PreviousCipherBlock, (uint8_t*)InBuffer + offset, AES_BLOCK_SIZE );
// Decrypt cipher block
AesDecrypt( &Context->Aes, Context->PreviousCipherBlock, (uint8_t*)OutBuffer + offset );
// XOR on previous cipher block
XorAesBlock( (uint8_t*)OutBuffer + offset, previousCipherBlock );
offset += AES_BLOCK_SIZE;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCbcEncryptWithKey
//
// This function combines AesCbcInitialiseWithKey and AesCbcEncrypt. This is suitable when encrypting data in one go
// with a key that is not going to be reused.
// InBuffer and OutBuffer can point to the same location for inplace encrypting.
// Returns 0 if successful, or -1 if invalid KeySize provided or BufferSize not a multiple of 16 bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCbcEncryptWithKey
(
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_CBC_IV_SIZE], // [in]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t BufferSize // [in]
)
{
int error;
AesCbcContext context;
error = AesCbcInitialiseWithKey( &context, Key, KeySize, IV );
if( 0 == error )
{
error = AesCbcEncrypt( &context, InBuffer, OutBuffer, BufferSize );
}
return error;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCbcDecryptWithKey
//
// This function combines AesCbcInitialiseWithKey and AesCbcDecrypt. This is suitable when decrypting data in one go
// with a key that is not going to be reused.
// InBuffer and OutBuffer can point to the same location for inplace decrypting.
// Returns 0 if successful, or -1 if invalid KeySize provided or BufferSize not a multiple of 16 bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCbcDecryptWithKey
(
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_CBC_IV_SIZE], // [in]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t BufferSize // [in]
)
{
int error;
AesCbcContext context;
error = AesCbcInitialiseWithKey( &context, Key, KeySize, IV );
if( 0 == error )
{
error = AesCbcDecrypt( &context, InBuffer, OutBuffer, BufferSize );
}
return error;
}

148
lib/WjCryptLib_AesCbc.h Normal file
View File

@@ -0,0 +1,148 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_AesCbc
//
// Implementation of AES CBC cipher.
//
// Depends on: CryptoLib_Aes
//
// AES CBC is a cipher using AES in Cipher Block Chaining mode. Encryption and decryption must be performed in
// multiples of the AES block size (128 bits).
// This implementation works on both little and big endian architectures.
//
// This is free and unencumbered software released into the public domain - March 2018 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include "WjCryptLib_Aes.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define AES_CBC_IV_SIZE AES_BLOCK_SIZE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCbcContext
// Do not modify the contents of this structure directly.
typedef struct
{
AesContext Aes;
uint8_t PreviousCipherBlock [AES_BLOCK_SIZE];
} AesCbcContext;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCbcInitialise
//
// Initialises an AesCbcContext with an already initialised AesContext and a IV. This function can quickly be used
// to change the IV without requiring the more lengthy processes of reinitialising an AES key.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCbcInitialise
(
AesCbcContext* Context, // [out]
AesContext const* InitialisedAesContext, // [in]
uint8_t const IV [AES_CBC_IV_SIZE] // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCbcInitialiseWithKey
//
// Initialises an AesCbcContext with an AES Key and an IV. This combines the initialising an AES Context and then
// running AesCbcInitialise. KeySize must be 16, 24, or 32 (for 128, 192, or 256 bit key size)
// Returns 0 if successful, or -1 if invalid KeySize provided
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCbcInitialiseWithKey
(
AesCbcContext* Context, // [out]
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_CBC_IV_SIZE] // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCbcEncrypt
//
// Encrypts a buffer of data using an AES CBC context. The data buffer must be a multiple of 16 bytes (128 bits)
// in size. The "position" of the context will be advanced by the buffer amount. A buffer can be encrypted in one
// go or in smaller chunks at a time. The result will be the same as long as data is fed into the function in the
// same order.
// InBuffer and OutBuffer can point to the same location for in-place encrypting.
// Returns 0 if successful, or -1 if Size is not a multiple of 16 bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCbcEncrypt
(
AesCbcContext* Context, // [in out]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t Size // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCbcDecrypt
//
// Decrypts a buffer of data using an AES CBC context. The data buffer must be a multiple of 16 bytes (128 bits)
// in size. The "position" of the context will be advanced by the buffer amount.
// InBuffer and OutBuffer can point to the same location for in-place decrypting.
// Returns 0 if successful, or -1 if Size is not a multiple of 16 bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCbcDecrypt
(
AesCbcContext* Context, // [in out]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t Size // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCbcEncryptWithKey
//
// This function combines AesCbcInitialiseWithKey and AesCbcEncrypt. This is suitable when encrypting data in one go
// with a key that is not going to be reused.
// InBuffer and OutBuffer can point to the same location for inplace encrypting.
// Returns 0 if successful, or -1 if invalid KeySize provided or BufferSize not a multiple of 16 bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCbcEncryptWithKey
(
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_CBC_IV_SIZE], // [in]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t BufferSize // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCbcDecryptWithKey
//
// This function combines AesCbcInitialiseWithKey and AesCbcDecrypt. This is suitable when decrypting data in one go
// with a key that is not going to be reused.
// InBuffer and OutBuffer can point to the same location for inplace decrypting.
// Returns 0 if successful, or -1 if invalid KeySize provided or BufferSize not a multiple of 16 bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCbcDecryptWithKey
(
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_CBC_IV_SIZE], // [in]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t BufferSize // [in]
);

299
lib/WjCryptLib_AesCtr.c Normal file
View File

@@ -0,0 +1,299 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_AesCtr
//
// Implementation of AES CTR stream cipher.
//
// Depends on: CryptoLib_Aes
//
// AES CTR is a stream cipher using the AES block cipher in counter mode.
// This implementation works on both little and big endian architectures.
//
// This is free and unencumbered software released into the public domain - November 2017 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "WjCryptLib_AesCtr.h"
#include "WjCryptLib_Aes.h"
#include <stdint.h>
#include <memory.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MACROS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MIN( x, y ) ( ((x)<(y))?(x):(y) )
#define STORE64H( x, y ) \
{ (y)[0] = (uint8_t)(((x)>>56)&255); (y)[1] = (uint8_t)(((x)>>48)&255); \
(y)[2] = (uint8_t)(((x)>>40)&255); (y)[3] = (uint8_t)(((x)>>32)&255); \
(y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255); \
(y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CreateCurrentCipherBlock
//
// Takes the IV and the counter in the AesCtrContext and produces the cipher block (CurrentCipherBlock). The cipher
// block is produced by first creating a 128 bit block with the IV as first 64 bits and the CurrentCipherBlockIndex
// stored as the remaining 64bits in Network byte order (Big Endian)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void
CreateCurrentCipherBlock
(
AesCtrContext* Context // [in out]
)
{
// Build block by first copying in the IV
memcpy( Context->CurrentCipherBlock, Context->IV, AES_CTR_IV_SIZE );
// Now place in the counter in Big Endian form
STORE64H( Context->CurrentCipherBlockIndex, Context->CurrentCipherBlock + AES_CTR_IV_SIZE );
// Perform AES encryption on the block
AesEncryptInPlace( &Context->Aes, Context->CurrentCipherBlock );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// XorBuffer
//
// Takes two Source buffers and XORs them together and puts the result in DestinationBuffer
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void
XorBuffers
(
uint8_t const* SourceBuffer1, // [in]
uint8_t const* SourceBuffer2, // [in]
uint8_t* DestinationBuffer, // [out]
uint32_t Amount // [in]
)
{
uint32_t i;
for( i=0; i<Amount; i++ )
{
DestinationBuffer[i] = SourceBuffer1[i] ^ SourceBuffer2[i];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrInitialise
//
// Initialises an AesCtrContext with an already initialised AesContext and a IV. This function can quickly be used
// to change the IV without requiring the more length processes of reinitialising an AES key.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCtrInitialise
(
AesCtrContext* Context, // [out]
AesContext const* InitialisedAesContext, // [in]
uint8_t const IV [AES_CTR_IV_SIZE] // [in]
)
{
// Setup context values
Context->Aes = *InitialisedAesContext;
memcpy( Context->IV, IV, AES_CTR_IV_SIZE );
Context->StreamIndex = 0;
Context->CurrentCipherBlockIndex = 0;
// Generate the first cipher block of the stream.
CreateCurrentCipherBlock( Context );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrInitialiseWithKey
//
// Initialises an AesCtrContext with an AES Key and an IV. This combines the initialising an AES Context and then
// running AesCtrInitialise. KeySize must be 16, 24, or 32 (for 128, 192, or 256 bit key size)
// Returns 0 if successful, or -1 if invalid KeySize provided
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCtrInitialiseWithKey
(
AesCtrContext* Context, // [out]
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_CTR_IV_SIZE] // [in]
)
{
AesContext aes;
// Initialise AES Context
if( 0 != AesInitialise( &aes, Key, KeySize ) )
{
return -1;
}
// Now set-up AesCtrContext
AesCtrInitialise( Context, &aes, IV );
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrSetStreamIndex
//
// Sets the current stream index to any arbitrary position. Setting to 0 sets it to the beginning of the stream. Any
// subsequent output will start from this position
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCtrSetStreamIndex
(
AesCtrContext* Context, // [in out]
uint64_t StreamIndex // [in]
)
{
uint64_t blockIndex = StreamIndex / AES_BLOCK_SIZE;
Context->StreamIndex = StreamIndex;
if( blockIndex != Context->CurrentCipherBlockIndex )
{
// Update block index and generate new cipher block as the new StreamIndex is inside a different block to the
// one we currently had.
Context->CurrentCipherBlockIndex = blockIndex;
CreateCurrentCipherBlock( Context );
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrXor
//
// XORs the stream of byte of the AesCtrContext from its current stream position onto the specified buffer. This will
// advance the stream index by that number of bytes.
// Use once over data to encrypt it. Use it a second time over the same data from the same stream position and the
// data will be decrypted.
// InBuffer and OutBuffer can point to the same location for in-place encrypting/decrypting
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCtrXor
(
AesCtrContext* Context, // [in out]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t Size // [in]
)
{
uint32_t firstChunkSize;
uint32_t amountAvailableInBlock;
int numIterations;
int i;
uint64_t loopStartingCipherBlockIndex;
uint32_t loopStartingOutputOffset;
uint8_t preCipherBlock [AES_KEY_SIZE_128];
uint8_t encCipherBlock [AES_KEY_SIZE_128];
uint64_t cipherBlockIndex = 0;
// First determine how much is available in the current block.
amountAvailableInBlock = AES_BLOCK_SIZE - (Context->StreamIndex % AES_BLOCK_SIZE);
// Determine how much of the current block we will take, either all that is available, or less
// if the amount requested is smaller.
firstChunkSize = MIN( amountAvailableInBlock, Size );
// XOR the bytes from the cipher block
XorBuffers( InBuffer, Context->CurrentCipherBlock + (AES_BLOCK_SIZE - amountAvailableInBlock), OutBuffer, firstChunkSize );
// Determine how many iterations will be needed for generating cipher blocks.
// We always have to finish with a non-depleted cipher block.
// Also calculate the cipher block index and the output offset for when we start the loop.
// This function may be built with OpenMP and the loop will run in parallel. So we set-up variables that will
// be common at the start of the loop.
numIterations = ( (Size - firstChunkSize) + AES_BLOCK_SIZE ) / AES_BLOCK_SIZE;
loopStartingCipherBlockIndex = Context->CurrentCipherBlockIndex + 1;
loopStartingOutputOffset = firstChunkSize;
// Copy the IV into the first half of the preCipherBlock. When built for OpenMP preCipherBlock will be copied into
// a local version within the loop.
memcpy( preCipherBlock, Context->IV, AES_CTR_IV_SIZE );
// Now start generating new cipher blocks as required.
#ifdef _OPENMP
#pragma omp parallel for firstprivate( preCipherBlock, cipherBlockIndex ) lastprivate( encCipherBlock, cipherBlockIndex )
#endif
for( i=0; i<numIterations; i++ )
{
uint32_t outputOffset = loopStartingOutputOffset + (AES_BLOCK_SIZE * i);
uint32_t amountLeft = Size - outputOffset;
uint32_t chunkSize = MIN( amountLeft, AES_BLOCK_SIZE );
// Increment block index and regenerate cipher block
cipherBlockIndex = loopStartingCipherBlockIndex + i;
// Now place in the counter in Big Endian form in second half of preCipherBlock
STORE64H( cipherBlockIndex, preCipherBlock + AES_CTR_IV_SIZE );
// Perform AES encryption on the preCipherBlock and put result in encCipherBlock
AesEncrypt( &Context->Aes, preCipherBlock, encCipherBlock );
// XOR block out onto the buffer.
XorBuffers( (uint8_t*)InBuffer + outputOffset, encCipherBlock, (uint8_t*)OutBuffer + outputOffset, chunkSize );
}
// Update context
Context->StreamIndex += Size;
if( numIterations > 0 )
{
Context->CurrentCipherBlockIndex = cipherBlockIndex;
memcpy( Context->CurrentCipherBlock, encCipherBlock, AES_BLOCK_SIZE );
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrOutput
//
// Outputs the stream of byte of the AesCtrContext from its current stream position. This will advance the stream
// index by that number of bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCtrOutput
(
AesCtrContext* Context, // [in out]
void* Buffer, // [out]
uint32_t Size // [in]
)
{
memset( Buffer, 0, Size );
AesCtrXor( Context, Buffer, Buffer, Size );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrXorWithKey
//
// This function combines AesCtrInitialiseWithKey and AesCtrXor. This is suitable when encrypting/decypting data in
// one go with a key that is not going to be reused.
// This will used the provided Key and IV and generate a stream that is XORed over Buffer.
// InBuffer and OutBuffer can point to the same location for inplace encrypting/decrypting
// Returns 0 if successful, or -1 if invalid KeySize provided
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCtrXorWithKey
(
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_CTR_IV_SIZE], // [in]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t BufferSize // [in]
)
{
int error;
AesCtrContext context;
error = AesCtrInitialiseWithKey( &context, Key, KeySize, IV );
if( 0 == error )
{
AesCtrXor( &context, InBuffer, OutBuffer, BufferSize );
}
return error;
}

140
lib/WjCryptLib_AesCtr.h Normal file
View File

@@ -0,0 +1,140 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_AesCtr
//
// Implementation of AES CTR stream cipher.
//
// Depends on: CryptoLib_Aes
//
// AES CTR is a stream cipher using the AES block cipher in counter mode.
// This implementation works on both little and big endian architectures.
//
// This is free and unencumbered software released into the public domain - November 2017 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include "WjCryptLib_Aes.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define AES_CTR_IV_SIZE 8
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrContext
// Do not modify the contents of this structure directly.
typedef struct
{
AesContext Aes;
uint8_t IV [AES_CTR_IV_SIZE];
uint64_t StreamIndex;
uint64_t CurrentCipherBlockIndex;
uint8_t CurrentCipherBlock [AES_BLOCK_SIZE];
} AesCtrContext;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrInitialise
//
// Initialises an AesCtrContext with an already initialised AesContext and a IV. This function can quickly be used
// to change the IV without requiring the more length processes of reinitialising an AES key.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCtrInitialise
(
AesCtrContext* Context, // [out]
AesContext const* InitialisedAesContext, // [in]
uint8_t const IV [AES_CTR_IV_SIZE] // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrInitialiseWithKey
//
// Initialises an AesCtrContext with an AES Key and an IV. This combines the initialising an AES Context and then
// running AesCtrInitialise. KeySize must be 16, 24, or 32 (for 128, 192, or 256 bit key size)
// Returns 0 if successful, or -1 if invalid KeySize provided
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCtrInitialiseWithKey
(
AesCtrContext* Context, // [out]
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_CTR_IV_SIZE] // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrSetStreamIndex
//
// Sets the current stream index to any arbitrary position. Setting to 0 sets it to the beginning of the stream. Any
// subsequent output will start from this position
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCtrSetStreamIndex
(
AesCtrContext* Context, // [in out]
uint64_t StreamIndex // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrXor
//
// XORs the stream of byte of the AesCtrContext from its current stream position onto the specified buffer. This will
// advance the stream index by that number of bytes.
// Use once over data to encrypt it. Use it a second time over the same data from the same stream position and the
// data will be decrypted.
// InBuffer and OutBuffer can point to the same location for in-place encrypting/decrypting
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCtrXor
(
AesCtrContext* Context, // [in out]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t Size // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrOutput
//
// Outputs the stream of byte of the AesCtrContext from its current stream position. This will advance the stream
// index by that number of bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesCtrOutput
(
AesCtrContext* Context, // [in out]
void* Buffer, // [out]
uint32_t Size // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrXorWithKey
//
// This function combines AesCtrInitialiseWithKey and AesCtrXor. This is suitable when encrypting/decypting data in
// one go with a key that is not going to be reused.
// This will used the provided Key and IV and generate a stream that is XORed over Buffer.
// Returns 0 if successful, or -1 if invalid KeySize provided
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesCtrXorWithKey
(
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_CTR_IV_SIZE], // [in]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t BufferSize // [in]
);

226
lib/WjCryptLib_AesOfb.c Normal file
View File

@@ -0,0 +1,226 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_AesOfb
//
// Implementation of AES OFB stream cipher.
//
// Depends on: CryptoLib_Aes
//
// AES OFB is a stream cipher using the AES block cipher in output feedback mode.
// This implementation works on both little and big endian architectures.
//
// This is free and unencumbered software released into the public domain - January 2018 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "WjCryptLib_AesOfb.h"
#include "WjCryptLib_Aes.h"
#include <stdint.h>
#include <memory.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MACROS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MIN( x, y ) ( ((x)<(y))?(x):(y) )
#define STORE64H( x, y ) \
{ (y)[0] = (uint8_t)(((x)>>56)&255); (y)[1] = (uint8_t)(((x)>>48)&255); \
(y)[2] = (uint8_t)(((x)>>40)&255); (y)[3] = (uint8_t)(((x)>>32)&255); \
(y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255); \
(y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// XorBuffer
//
// Takes two Source buffers and XORs them together and puts the result in DestinationBuffer
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void
XorBuffers
(
uint8_t const* SourceBuffer1, // [in]
uint8_t const* SourceBuffer2, // [in]
uint8_t* DestinationBuffer, // [out]
uint32_t Amount // [in]
)
{
uint32_t i;
for( i=0; i<Amount; i++ )
{
DestinationBuffer[i] = SourceBuffer1[i] ^ SourceBuffer2[i];
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesOfbInitialise
//
// Initialises an AesOfbContext with an already initialised AesContext and a IV. This function can quickly be used
// to change the IV without requiring the more lengthy processes of reinitialising an AES key.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesOfbInitialise
(
AesOfbContext* Context, // [out]
AesContext const* InitialisedAesContext, // [in]
uint8_t const IV [AES_OFB_IV_SIZE] // [in]
)
{
// Setup context values
Context->Aes = *InitialisedAesContext;
memcpy( Context->CurrentCipherBlock, IV, sizeof(Context->CurrentCipherBlock) );
Context->IndexWithinCipherBlock = 0;
// Generate the first cipher block of the stream.
AesEncryptInPlace( &Context->Aes, Context->CurrentCipherBlock );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesOfbInitialiseWithKey
//
// Initialises an AesOfbContext with an AES Key and an IV. This combines the initialising an AES Context and then
// running AesOfbInitialise. KeySize must be 16, 24, or 32 (for 128, 192, or 256 bit key size)
// Returns 0 if successful, or -1 if invalid KeySize provided
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesOfbInitialiseWithKey
(
AesOfbContext* Context, // [out]
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_OFB_IV_SIZE] // [in]
)
{
AesContext aes;
// Initialise AES Context
if( 0 != AesInitialise( &aes, Key, KeySize ) )
{
return -1;
}
// Now set-up AesOfbContext
AesOfbInitialise( Context, &aes, IV );
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesOfbXor
//
// XORs the stream of byte of the AesOfbContext from its current stream position onto the specified buffer. This will
// advance the stream index by that number of bytes.
// Use once over data to encrypt it. Use it a second time over the same data from the same stream position and the
// data will be decrypted.
// InBuffer and OutBuffer can point to the same location for in-place encrypting/decrypting
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesOfbXor
(
AesOfbContext* Context, // [in out]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t Size // [in]
)
{
uint32_t amountLeft = Size;
uint32_t outputOffset = 0;
uint32_t chunkSize;
uint32_t amountAvailableInBlock;
// First determine how much is available in the current block.
amountAvailableInBlock = AES_BLOCK_SIZE - Context->IndexWithinCipherBlock;
// Determine how much of the current block we will take, either all that is available, or less
// if the amount requested is smaller.
chunkSize = MIN( amountAvailableInBlock, amountLeft );
// XOR the bytes from the cipher block
XorBuffers( InBuffer, Context->CurrentCipherBlock + (AES_BLOCK_SIZE - amountAvailableInBlock), OutBuffer, chunkSize );
amountLeft -= chunkSize;
outputOffset += chunkSize;
Context->IndexWithinCipherBlock += chunkSize;
// Now start generating new cipher blocks as required.
while( amountLeft > 0 )
{
// Generate new cipher block
AesEncryptInPlace( &Context->Aes, Context->CurrentCipherBlock );
// Determine how much of the current block we need and XOR it out onto the buffer
chunkSize = MIN( amountLeft, AES_BLOCK_SIZE );
XorBuffers( (uint8_t*)InBuffer + outputOffset, Context->CurrentCipherBlock, (uint8_t*)OutBuffer + outputOffset, chunkSize );
amountLeft -= chunkSize;
outputOffset += chunkSize;
Context->IndexWithinCipherBlock = chunkSize; // Note: Not incremented
}
// If we ended up completely reading the last cipher block we need to generate a new one for next time.
if( AES_BLOCK_SIZE == chunkSize )
{
AesEncryptInPlace( &Context->Aes, Context->CurrentCipherBlock );
Context->IndexWithinCipherBlock = 0;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesOfbOutput
//
// Outputs the stream of byte of the AesOfbContext from its current stream position. This will advance the stream
// index by that number of bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesOfbOutput
(
AesOfbContext* Context, // [in out]
void* Buffer, // [out]
uint32_t Size // [in]
)
{
memset( Buffer, 0, Size );
AesOfbXor( Context, Buffer, Buffer, Size );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesOfbXorWithKey
//
// This function combines AesOfbInitialiseWithKey and AesOfbXor. This is suitable when encrypting/decypting data in
// one go with a key that is not going to be reused.
// This will used the provided Key and IV and generate a stream that is XORed over Buffer.
// InBuffer and OutBuffer can point to the same location for inplace encrypting/decrypting
// Returns 0 if successful, or -1 if invalid KeySize provided
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesOfbXorWithKey
(
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_OFB_IV_SIZE], // [in]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t BufferSize // [in]
)
{
int error;
AesOfbContext context;
error = AesOfbInitialiseWithKey( &context, Key, KeySize, IV );
if( 0 == error )
{
AesOfbXor( &context, InBuffer, OutBuffer, BufferSize );
}
return error;
}

126
lib/WjCryptLib_AesOfb.h Normal file
View File

@@ -0,0 +1,126 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_AesOfb
//
// Implementation of AES OFB stream cipher.
//
// Depends on: CryptoLib_Aes
//
// AES OFB is a stream cipher using the AES block cipher in output feedback mode.
// This implementation works on both little and big endian architectures.
//
// This is free and unencumbered software released into the public domain - January 2018 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include "WjCryptLib_Aes.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define AES_OFB_IV_SIZE AES_BLOCK_SIZE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesOfbContext
// Do not modify the contents of this structure directly.
typedef struct
{
AesContext Aes;
uint8_t CurrentCipherBlock [AES_BLOCK_SIZE];
uint32_t IndexWithinCipherBlock;
} AesOfbContext;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesOfbInitialise
//
// Initialises an AesOfbContext with an already initialised AesContext and a IV. This function can quickly be used
// to change the IV without requiring the more lengthy processes of reinitialising an AES key.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesOfbInitialise
(
AesOfbContext* Context, // [out]
AesContext const* InitialisedAesContext, // [in]
uint8_t const IV [AES_OFB_IV_SIZE] // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesOfbInitialiseWithKey
//
// Initialises an AesOfbContext with an AES Key and an IV. This combines the initialising an AES Context and then
// running AesOfbInitialise. KeySize must be 16, 24, or 32 (for 128, 192, or 256 bit key size)
// Returns 0 if successful, or -1 if invalid KeySize provided
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesOfbInitialiseWithKey
(
AesOfbContext* Context, // [out]
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_OFB_IV_SIZE] // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesOfbXor
//
// XORs the stream of byte of the AesOfbContext from its current stream position onto the specified buffer. This will
// advance the stream index by that number of bytes.
// Use once over data to encrypt it. Use it a second time over the same data from the same stream position and the
// data will be decrypted.
// InBuffer and OutBuffer can point to the same location for in-place encrypting/decrypting
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesOfbXor
(
AesOfbContext* Context, // [in out]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t Size // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesOfbOutput
//
// Outputs the stream of byte of the AesOfbContext from its current stream position. This will advance the stream
// index by that number of bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
AesOfbOutput
(
AesOfbContext* Context, // [in out]
void* Buffer, // [out]
uint32_t Size // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesOfbXorWithKey
//
// This function combines AesOfbInitialiseWithKey and AesOfbXor. This is suitable when encrypting/decypting data in
// one go with a key that is not going to be reused.
// This will used the provided Key and IV and generate a stream that is XORed over Buffer.
// InBuffer and OutBuffer can point to the same location for inplace encrypting/decrypting
// Returns 0 if successful, or -1 if invalid KeySize provided
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
AesOfbXorWithKey
(
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint8_t const IV [AES_OFB_IV_SIZE], // [in]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t BufferSize // [in]
);

View File

@@ -1,65 +1,56 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LibMd5
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_Md5
//
// Implementation of MD5 hash function. Originally written by Alexander Peslyak. Modified by WaterJuice retaining
// Public Domain license.
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "LibMd5.h"
#include "WjCryptLib_Md5.h"
#include <memory.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// F, G, H, I
//
// The basic MD5 functions. F and G are optimized compared to their RFC 1321 definitions for architectures that lack
// The basic MD5 functions. F and G are optimised compared to their RFC 1321 definitions for architectures that lack
// an AND-NOT instruction, just like in Colin Plumb's implementation.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define F( x, y, z ) ( (z) ^ ((x) & ((y) ^ (z))) )
#define G( x, y, z ) ( (y) ^ ((z) & ((x) ^ (y))) )
#define H( x, y, z ) ( (x) ^ (y) ^ (z) )
#define I( x, y, z ) ( (y) ^ ((x) | ~(z)) )
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// STEP
//
// The MD5 transformation for all four rounds.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define STEP( f, a, b, c, d, x, t, s ) \
(a) += f((b), (c), (d)) + (x) + (t); \
(a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
(a) += (b);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SET, GET
//
// SET reads 4 input bytes in little-endian byte order and stores them in a properly aligned word in host byte order.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define SET(n) (*(uint32_t *)&ptr[(n) * 4])
#define GET(n) SET(n)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TransformFunction
//
// This processes one or more 64-byte data blocks, but does NOT update the bit counters. There are no alignment
// requirements.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void*
TransformFunction
(
Md5Context* ctx,
void* data,
void const* data,
uintmax_t size
)
{
@@ -73,6 +64,13 @@ void*
uint32_t saved_c;
uint32_t saved_d;
#define GET(n) (ctx->block[(n)])
#define SET(n) (ctx->block[(n)] = \
((uint32_t)ptr[(n)*4 + 0] << 0 ) \
| ((uint32_t)ptr[(n)*4 + 1] << 8 ) \
| ((uint32_t)ptr[(n)*4 + 2] << 16) \
| ((uint32_t)ptr[(n)*4 + 3] << 24) )
ptr = (uint8_t*)data;
a = ctx->a;
@@ -172,22 +170,25 @@ void*
ctx->c = c;
ctx->d = d;
#undef GET
#undef SET
return ptr;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// EXPORTED FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Md5Initialise
//
// Initialises an MD5 Context. Use this to initialise/reset a context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Md5Initialise
(
Md5Context* Context
Md5Context* Context // [out]
)
{
Context->a = 0x67452301;
@@ -199,18 +200,18 @@ void
Context->hi = 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Md5Update
//
// Adds data to the MD5 context. This will process the data and update the internal state of the context. Keep on
// calling this function until all the data has been added. Then call Md5Finalise to calculate the hash.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Md5Update
(
Md5Context* Context,
void* Buffer,
uint32_t BufferSize
Md5Context* Context, // [in out]
void const* Buffer, // [in]
uint32_t BufferSize // [in]
)
{
uint32_t saved_lo;
@@ -251,17 +252,17 @@ void
memcpy( Context->buffer, Buffer, BufferSize );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Md5Finalise
//
// Performs the final calculation of the hash and returns the digest (16 byte buffer containing 128bit hash). After
// calling this, Md5Initialised must be used to reuse the context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Md5Finalise
(
Md5Context* Context,
MD5_HASH* Digest
Md5Context* Context, // [in out]
MD5_HASH* Digest // [in]
)
{
uint32_t used;
@@ -313,3 +314,22 @@ void
Digest->bytes[15] = (uint8_t)( Context->d >> 24 );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Md5Calculate
//
// Combines Md5Initialise, Md5Update, and Md5Finalise into one function. Calculates the MD5 hash of the buffer.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Md5Calculate
(
void const* Buffer, // [in]
uint32_t BufferSize, // [in]
MD5_HASH* Digest // [in]
)
{
Md5Context context;
Md5Initialise( &context );
Md5Update( &context, Buffer, BufferSize );
Md5Finalise( &context, Digest );
}

View File

@@ -1,25 +1,24 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LibMd5
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_Md5
//
// Implementation of MD5 hash function. Originally written by Alexander Peslyak. Modified by WaterJuice retaining
// Public Domain license.
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _LibMd5_h_
#define _LibMd5_h_
#pragma once
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <stdio.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Md5Context - This must be initialised using Md5Initialised. Do not modify the contents of this structure directly.
typedef struct
@@ -41,49 +40,57 @@ typedef struct
uint8_t bytes [MD5_HASH_SIZE];
} MD5_HASH;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Md5Initialise
//
// Initialises an MD5 Context. Use this to initialise/reset a context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Md5Initialise
(
Md5Context* Context
Md5Context* Context // [out]
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Md5Update
//
// Adds data to the MD5 context. This will process the data and update the internal state of the context. Keep on
// calling this function until all the data has been added. Then call Md5Finalise to calculate the hash.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Md5Update
(
Md5Context* Context,
void* Buffer,
uint32_t BufferSize
Md5Context* Context, // [in out]
void const* Buffer, // [in]
uint32_t BufferSize // [in]
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Md5Finalise
//
// Performs the final calculation of the hash and returns the digest (16 byte buffer containing 128bit hash). After
// calling this, Md5Initialised must be used to reuse the context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Md5Finalise
(
Md5Context* Context,
MD5_HASH* Digest
Md5Context* Context, // [in out]
MD5_HASH* Digest // [in]
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif //_LibMd5_h_
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Md5Calculate
//
// Combines Md5Initialise, Md5Update, and Md5Finalise into one function. Calculates the MD5 hash of the buffer.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Md5Calculate
(
void const* Buffer, // [in]
uint32_t BufferSize, // [in]
MD5_HASH* Digest // [in]
);

View File

@@ -1,21 +1,21 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LibRC4
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_RC4
//
// An implementation of RC4 stream cipher
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "LibRc4.h"
#include "WjCryptLib_Rc4.h"
#include <stdlib.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define SwapBytes( Value1, Value2 ) \
{ \
@@ -24,22 +24,22 @@
Value2 = temp; \
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Initialise
//
// Initialises an RC4 cipher and discards the specified number of first bytes.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4Initialise
(
Rc4Context* Context,
void* Key,
uint32_t KeySize,
uint32_t DropN
Rc4Context* Context, // [out]
void const* Key, // [in]
uint32_t KeySize, // [in]
uint32_t DropN // [in]
)
{
uint32_t i;
@@ -74,17 +74,17 @@ void
Context->j = j;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Output
//
// Outputs the requested number of bytes from the RC4 stream
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4Output
(
Rc4Context* Context,
void* Buffer,
uint32_t Size
Rc4Context* Context, // [in out]
void* Buffer, // [out]
uint32_t Size // [in]
)
{
uint32_t n;
@@ -99,19 +99,19 @@ void
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Xor
//
// XORs the RC4 stream with an input buffer and puts the results in an output buffer. This is used for encrypting
// and decrypting data. InBuffer and OutBuffer can point to the same location for inplace encrypting/decrypting
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4Xor
(
Rc4Context* Context,
void* InBuffer,
void* OutBuffer,
uint32_t Size
Rc4Context* Context, // [in out]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t Size // [in]
)
{
uint32_t n;
@@ -127,3 +127,26 @@ void
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4XorWithKey
//
// This function combines Rc4Initialise and Rc4Xor. This is suitable when encrypting/decrypting data in one go with a
// key that is not going to be reused.
// InBuffer and OutBuffer can point to the same location for inplace encrypting/decrypting
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4XorWithKey
(
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint32_t DropN, // [in]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t BufferSize // [in]
)
{
Rc4Context context;
Rc4Initialise( &context, Key, KeySize, DropN );
Rc4Xor( &context, InBuffer, OutBuffer, BufferSize );
}

91
lib/WjCryptLib_Rc4.h Normal file
View File

@@ -0,0 +1,91 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_RC4
//
// An implementation of RC4 stream cipher
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Context - This must be initialised using Rc4Initialised. Do not modify the contents of this structure directly.
typedef struct
{
uint32_t i;
uint32_t j;
uint8_t S[256];
} Rc4Context;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Initialise
//
// Initialises an RC4 cipher and discards the specified number of first bytes.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4Initialise
(
Rc4Context* Context, // [out]
void const* Key, // [in]
uint32_t KeySize, // [in]
uint32_t DropN // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Output
//
// Outputs the requested number of bytes from the RC4 stream
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4Output
(
Rc4Context* Context, // [in out]
void* Buffer, // [out]
uint32_t Size // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4Xor
//
// XORs the RC4 stream with an input buffer and puts the results in an output buffer. This is used for encrypting
// and decrypting data. InBuffer and OutBuffer can point to the same location for inplace encrypting/decrypting
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4Xor
(
Rc4Context* Context, // [in out]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t Size // [in]
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4XorWithKey
//
// This function combines Rc4Initialise and Rc4Xor. This is suitable when encrypting/decrypting data in one go with a
// key that is not going to be reused.
// InBuffer and OutBuffer can point to the same location for inplace encrypting/decrypting
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Rc4XorWithKey
(
uint8_t const* Key, // [in]
uint32_t KeySize, // [in]
uint32_t DropN, // [in]
void const* InBuffer, // [in]
void* OutBuffer, // [out]
uint32_t BufferSize // [in]
);

View File

@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LibSha1
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_Sha1
//
// Implementation of SHA1 hash function.
// Original author: Steve Reid <sreid@sea-to-sky.net>
@@ -8,18 +8,33 @@
// Modified by WaterJuice retaining Public Domain license.
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "LibSha1.h"
#include "WjCryptLib_Sha1.h"
#include <memory.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DEFINES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Decide whether to use the Little-Endian shortcut. If the shortcut is not used then the code will work correctly
// on either big or little endian, however if we do know it is a little endian architecture we can speed it up a bit.
// Note, there are TWO places where USE_LITTLE_ENDIAN_SHORTCUT is used. They MUST be paired together.
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && ( __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ )
// gcc defines __BYTE_ORDER__ so if it says its little endian we can use that.
#define USE_LITTLE_ENDIAN_SHORTCUT
#elif defined( _WIN32 )
// Windows is always little endian so we can use that.
#define USE_LITTLE_ENDIAN_SHORTCUT
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef union
{
@@ -27,18 +42,27 @@ typedef union
uint32_t l [16];
} CHAR64LONG16;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Endian neutral macro for loading 32 bit value from 4 byte array (in big endian form).
#define LOAD32H(x, y) \
{ x = ((uint32_t)((y)[0] & 255)<<24) | \
((uint32_t)((y)[1] & 255)<<16) | \
((uint32_t)((y)[2] & 255)<<8) | \
((uint32_t)((y)[3] & 255)); }
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
// blk0() and blk() perform the initial expand.
#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
|(rol(block->l[i],8)&0x00FF00FF))
#ifdef USE_LITTLE_ENDIAN_SHORTCUT
#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) | (rol(block->l[i],8)&0x00FF00FF))
#else
#define blk0(i) block->l[i]
#endif
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
^block->l[(i+2)&15]^block->l[i&15],1))
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15] ^ block->l[(i+8)&15] ^ block->l[(i+2)&15] ^ block->l[i&15],1))
// (R0+R1), R2, R3, R4 are the different operations used in SHA1
#define R0(v,w,x,y,z,i) z += ((w&(x^y))^y) + blk0(i)+ 0x5A827999 + rol(v,5); w=rol(w,30);
@@ -47,18 +71,32 @@ typedef union
#define R3(v,w,x,y,z,i) z += (((w|x)&y)|(w&x)) + blk(i) + 0x8F1BBCDC + rol(v,5); w=rol(w,30);
#define R4(v,w,x,y,z,i) z += (w^x^y) + blk(i) + 0xCA62C1D6 + rol(v,5); w=rol(w,30);
// Loads the 128 bits from ByteArray into WordArray, treating ByteArray as big endian data
#ifdef USE_LITTLE_ENDIAN_SHORTCUT
#define Load128BitsAsWords( WordArray, ByteArray ) \
memcpy( WordArray, ByteArray, 64 )
#else
#define Load128BitsAsWords( WordArray, ByteArray ) \
{ \
uint32_t i; \
for( i=0; i<16; i++ ) \
{ \
LOAD32H( (WordArray)[i], (ByteArray)+(i*4) ); \
} \
}
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TransformFunction
//
// Hash a single 512-bit block. This is the core of the algorithm
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void
TransformFunction
(
uint32_t state[5],
const uint8_t buffer[64]
uint8_t const buffer[64]
)
{
uint32_t a;
@@ -69,7 +107,7 @@ void
uint8_t workspace[64];
CHAR64LONG16* block = (CHAR64LONG16*) workspace;
memcpy( block, buffer, 64 );
Load128BitsAsWords( block->l, buffer );
// Copy context->state[] to working vars
a = state[0];
@@ -108,22 +146,22 @@ void
state[4] += e;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha1Initialise
//
// Initialises an SHA1 Context. Use this to initialise/reset a context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha1Initialise
(
Sha1Context* Context
Sha1Context* Context // [out]
)
{
// SHA1 initialization constants
// SHA1 initialisation constants
Context->State[0] = 0x67452301;
Context->State[1] = 0xEFCDAB89;
Context->State[2] = 0x98BADCFE;
@@ -133,18 +171,18 @@ void
Context->Count[1] = 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha1Update
//
// Adds data to the SHA1 context. This will process the data and update the internal state of the context. Keep on
// calling this function until all the data has been added. Then call Sha1Finalise to calculate the hash.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha1Update
(
Sha1Context* Context,
void* Buffer,
uint32_t BufferSize
Sha1Context* Context, // [in out]
void const* Buffer, // [in]
uint32_t BufferSize // [in]
)
{
uint32_t i;
@@ -176,17 +214,17 @@ void
memcpy( &Context->Buffer[j], &((uint8_t*)Buffer)[i], BufferSize - i );
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha1Finalise
//
// Performs the final calculation of the hash and returns the digest (20 byte buffer containing 160bit hash). After
// calling this, Sha1Initialised must be used to reuse the context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha1Finalise
(
Sha1Context* Context,
SHA1_HASH* Digest
Sha1Context* Context, // [in out]
SHA1_HASH* Digest // [in]
)
{
uint32_t i;
@@ -210,3 +248,22 @@ void
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha1Calculate
//
// Combines Sha1Initialise, Sha1Update, and Sha1Finalise into one function. Calculates the SHA1 hash of the buffer.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha1Calculate
(
void const* Buffer, // [in]
uint32_t BufferSize, // [in]
SHA1_HASH* Digest // [in]
)
{
Sha1Context context;
Sha1Initialise( &context );
Sha1Update( &context, Buffer, BufferSize );
Sha1Finalise( &context, Digest );
}

View File

@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LibSha1
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_Sha1
//
// Implementation of SHA1 hash function.
// Original author: Steve Reid <sreid@sea-to-sky.net>
@@ -8,21 +8,20 @@
// Modified by WaterJuice retaining Public Domain license.
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _LibSha1_h_
#define _LibSha1_h_
#pragma once
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <stdio.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha1Context - This must be initialised using Sha1Initialised. Do not modify the contents of this structure directly.
typedef struct
@@ -39,48 +38,57 @@ typedef struct
uint8_t bytes [SHA1_HASH_SIZE];
} SHA1_HASH;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha1Initialise
//
// Initialises an SHA1 Context. Use this to initialise/reset a context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha1Initialise
(
Sha1Context* Context
Sha1Context* Context // [out]
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha1Update
//
// Adds data to the SHA1 context. This will process the data and update the internal state of the context. Keep on
// calling this function until all the data has been added. Then call Sha1Finalise to calculate the hash.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha1Update
(
Sha1Context* Context,
void* Buffer,
uint32_t BufferSize
Sha1Context* Context, // [in out]
void const* Buffer, // [in]
uint32_t BufferSize // [in]
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha1Finalise
//
// Performs the final calculation of the hash and returns the digest (20 byte buffer containing 160bit hash). After
// calling this, Sha1Initialised must be used to reuse the context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha1Finalise
(
Sha1Context* Context,
SHA1_HASH* Digest
Sha1Context* Context, // [in out]
SHA1_HASH* Digest // [in]
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif //_LibSha1_h_
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha1Calculate
//
// Combines Sha1Initialise, Sha1Update, and Sha1Finalise into one function. Calculates the SHA1 hash of the buffer.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha1Calculate
(
void const* Buffer, // [in]
uint32_t BufferSize, // [in]
SHA1_HASH* Digest // [in]
);

View File

@@ -1,23 +1,23 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LibSha256
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_Sha256
//
// Implementation of SHA256 hash function.
// Original author: Tom St Denis, tomstdenis@gmail.com, http://libtom.org
// Modified by WaterJuice retaining Public Domain license.
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "LibSha256.h"
#include "WjCryptLib_Sha256.h"
#include <memory.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MACROS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ror(value, bits) (((value) >> (bits)) | ((value) << (32 - (bits))))
@@ -39,10 +39,9 @@
(y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255); \
(y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CONSTANTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// The K array
static const uint32_t K[64] = {
@@ -63,9 +62,9 @@ static const uint32_t K[64] = {
#define BLOCK_SIZE 64
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Various logical functions
#define Ch( x, y, z ) (z ^ (x & (y ^ z)))
@@ -83,17 +82,17 @@ static const uint32_t K[64] = {
d += t0; \
h = t0 + t1;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TransformFunction
//
// Compress 512-bits
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void
TransformFunction
(
Sha256Context* Context,
uint8_t* Buffer
uint8_t const* Buffer
)
{
uint32_t S[8];
@@ -143,18 +142,19 @@ void
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha256Initialise
//
// Initialises a SHA256 Context. Use this to initialise/reset a context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Sha256Initialise
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha256Initialise
(
Sha256Context* Context
Sha256Context* Context // [out]
)
{
Context->curlen = 0;
@@ -169,21 +169,22 @@ void Sha256Initialise
Context->state[7] = 0x5BE0CD19UL;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha256Update
//
// Adds data to the SHA256 context. This will process the data and update the internal state of the context. Keep on
// calling this function until all the data has been added. Then call Sha256Finalise to calculate the hash.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Sha256Update
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha256Update
(
Sha256Context* Context,
void* Buffer,
uint32_t BufferSize
Sha256Context* Context, // [in out]
void const* Buffer, // [in]
uint32_t BufferSize // [in]
)
{
uint32_t n
;
uint32_t n;
if( Context->curlen > sizeof(Context->buf) )
{
return;
@@ -215,17 +216,17 @@ void Sha256Update
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha256Finalise
//
// Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit hash). After
// calling this, Sha256Initialised must be used to reuse the context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha256Finalise
(
Sha256Context* Context,
SHA256_HASH* Digest
Sha256Context* Context, // [in out]
SHA256_HASH* Digest // [out]
)
{
int i;
@@ -271,3 +272,23 @@ void
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha256Calculate
//
// Combines Sha256Initialise, Sha256Update, and Sha256Finalise into one function. Calculates the SHA256 hash of the
// buffer.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha256Calculate
(
void const* Buffer, // [in]
uint32_t BufferSize, // [in]
SHA256_HASH* Digest // [in]
)
{
Sha256Context context;
Sha256Initialise( &context );
Sha256Update( &context, Buffer, BufferSize );
Sha256Finalise( &context, Digest );
}

View File

@@ -1,19 +1,18 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LibSha256
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_Sha256
//
// Implementation of SHA256 hash function.
// Original author: Tom St Denis, tomstdenis@gmail.com, http://libtom.org
// Modified by WaterJuice retaining Public Domain license.
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _LibSha256_h_
#define _LibSha256_h_
#pragma once
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <stdio.h>
@@ -33,46 +32,58 @@ typedef struct
uint8_t bytes [SHA256_HASH_SIZE];
} SHA256_HASH;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha256Initialise
//
// Initialises a SHA256 Context. Use this to initialise/reset a context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Sha256Initialise
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha256Initialise
(
Sha256Context* Context
Sha256Context* Context // [out]
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha256Update
//
// Adds data to the SHA256 context. This will process the data and update the internal state of the context. Keep on
// calling this function until all the data has been added. Then call Sha256Finalise to calculate the hash.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Sha256Update
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha256Update
(
Sha256Context* Context,
void* Buffer,
uint32_t BufferSize
Sha256Context* Context, // [in out]
void const* Buffer, // [in]
uint32_t BufferSize // [in]
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha256Finalise
//
// Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit hash). After
// calling this, Sha256Initialised must be used to reuse the context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha256Finalise
(
Sha256Context* Context,
SHA256_HASH* Digest
Sha256Context* Context, // [in out]
SHA256_HASH* Digest // [out]
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif //_LibSha256_h_
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha256Calculate
//
// Combines Sha256Initialise, Sha256Update, and Sha256Finalise into one function. Calculates the SHA256 hash of the
// buffer.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha256Calculate
(
void const* Buffer, // [in]
uint32_t BufferSize, // [in]
SHA256_HASH* Digest // [in]
);

View File

@@ -1,23 +1,23 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LibSha512
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_Sha512
//
// Implementation of SHA512 hash function.
// Original author: Tom St Denis, tomstdenis@gmail.com, http://libtom.org
// Modified by WaterJuice retaining Public Domain license.
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "LibSha512.h"
#include "WjCryptLib_Sha512.h"
#include <memory.h>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MACROS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ROR64( value, bits ) (((value) >> (bits)) | ((value) << (64 - (bits))))
@@ -35,10 +35,9 @@
(y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255); \
(y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CONSTANTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// The K array
static const uint64_t K[80] = {
@@ -66,9 +65,9 @@ static const uint64_t K[80] = {
#define BLOCK_SIZE 128
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Various logical functions
#define Ch( x, y, z ) (z ^ (x & (y ^ z)))
@@ -86,17 +85,17 @@ static const uint64_t K[80] = {
d += t0; \
h = t0 + t1;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TransformFunction
//
// Compress 1024-bits
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void
TransformFunction
(
Sha512Context* Context,
uint8_t* Buffer
uint8_t const* Buffer
)
{
uint64_t S[8];
@@ -143,19 +142,19 @@ void
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha512Initialise
//
// Initialises a SHA512 Context. Use this to initialise/reset a context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha512Initialise
(
Sha512Context* Context
Sha512Context* Context // [out]
)
{
Context->curlen = 0;
@@ -170,18 +169,18 @@ void
Context->state[7] = 0x5be0cd19137e2179ULL;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha512Update
//
// Adds data to the SHA512 context. This will process the data and update the internal state of the context. Keep on
// calling this function until all the data has been added. Then call Sha512Finalise to calculate the hash.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha512Update
(
Sha512Context* Context,
void* Buffer,
uint32_t BufferSize
Sha512Context* Context, // [in out]
void const* Buffer, // [in]
uint32_t BufferSize // [in]
)
{
uint32_t n;
@@ -217,17 +216,17 @@ void
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha512Finalise
//
// Performs the final calculation of the hash and returns the digest (64 byte buffer containing 512bit hash). After
// calling this, Sha512Initialised must be used to reuse the context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha512Finalise
(
Sha512Context* Context,
SHA512_HASH* Digest
Sha512Context* Context, // [in out]
SHA512_HASH* Digest // [out]
)
{
int i;
@@ -269,9 +268,29 @@ void
TransformFunction( Context, Context->buf );
// Copy output
for( i=0; i<8; i++ )
for( i=0; i<8; i++ )
{
STORE64H( Context->state[i], Digest->bytes+(8*i) );
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha512Calculate
//
// Combines Sha512Initialise, Sha512Update, and Sha512Finalise into one function. Calculates the SHA512 hash of the
// buffer.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha512Calculate
(
void const* Buffer, // [in]
uint32_t BufferSize, // [in]
SHA512_HASH* Digest // [in]
)
{
Sha512Context context;
Sha512Initialise( &context );
Sha512Update( &context, Buffer, BufferSize );
Sha512Finalise( &context, Digest );
}

View File

@@ -1,19 +1,18 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LibSha512
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLib_Sha512
//
// Implementation of SHA512 hash function.
// Original author: Tom St Denis, tomstdenis@gmail.com, http://libtom.org
// Modified by WaterJuice retaining Public Domain license.
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _LibSha512_h_
#define _LibSha512_h_
#pragma once
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdint.h>
#include <stdio.h>
@@ -33,46 +32,44 @@ typedef struct
uint8_t bytes [SHA512_HASH_SIZE];
} SHA512_HASH;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha512Initialise
//
// Initialises a SHA512 Context. Use this to initialise/reset a context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Sha512Initialise
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha512Initialise
(
Sha512Context* Context
Sha512Context* Context // [out]
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha512Update
//
// Adds data to the SHA512 context. This will process the data and update the internal state of the context. Keep on
// calling this function until all the data has been added. Then call Sha512Finalise to calculate the hash.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Sha512Update
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha512Update
(
Sha512Context* Context,
void* Buffer,
uint32_t BufferSize
Sha512Context* Context, // [in out]
void const* Buffer, // [in]
uint32_t BufferSize // [in]
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha512Finalise
//
// Performs the final calculation of the hash and returns the digest (64 byte buffer containing 512bit hash). After
// calling this, Sha512Initialised must be used to reuse the context.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void
Sha512Finalise
(
Sha512Context* Context,
SHA512_HASH* Digest
Sha512Context* Context, // [in out]
SHA512_HASH* Digest // [out]
);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif //_LibSha512_h_

View File

@@ -1,70 +0,0 @@
OUTDIR = Bin/$(PLATFORM)
INCLUDES = -I ./lib
all: CryptLibTest Md5String Sha1String Sha256String Sha512String Rc4Output
CryptLibTest: dir
$(COMPILE) projects/CryptLibTest/CryptLibTest.c lib/LibRc4.c lib/LibMd5.c lib/LibSha1.c lib/LibSha256.c lib/LibSha512.c $(TAIL)
$(STRIP)
Md5String: dir
$(COMPILE) projects/Md5String/Md5String.c lib/LibMd5.c lib/LibSha1.c lib/LibSha256.c lib/LibSha512.c $(TAIL)
$(STRIP)
Sha1String: dir
$(COMPILE) projects/Sha1String/Sha1String.c lib/LibSha1.c $(TAIL)
$(STRIP)
Sha256String: dir
$(COMPILE) projects/Sha256String/Sha256String.c lib/LibSha256.c $(TAIL)
$(STRIP)
Sha512String: dir
$(COMPILE) projects/Sha512String/Sha512String.c lib/LibSha512.c $(TAIL)
$(STRIP)
Rc4Output: dir
$(COMPILE) projects/Rc4Output/Rc4Output.c lib/LibRc4.c $(TAIL)
$(STRIP)
dir:
@mkdir -p $(OUTDIR)
###### Setup build parameters #####
ifeq ($(OS),Windows_NT)
ifdef VSINSTALLDIR
ifeq ($(Platform),X64)
PLATFORM = Windows
else
PLATFORM = WindowsX86
endif
INTDIR = Build/$(PLATFORM)/$@
INCLUDES := $(INCLUDES) /I ./stdbool
COMPILE = @echo & echo ::::: Building $(PLATFORM) $@ & mkdir -p $(INTDIR) & cl $(INCLUDES) /nologo /Ox /Oi /Ot /GL /MD /W4 /WX /Fe$(OUTDIR)/$@ /D_CRT_SECURE_NO_WARNINGS /Fo$(INTDIR)/
TAIL=/link /RELEASE
else ifneq (,$findstring /cygwin/,$(PATH))
PLATFORM = Cygwin
COMPILE = @echo ::::: Building $(PLATFORM) $@ & gcc $(INCLUDES) -O3 -Wall -Werror -o $(OUTDIR)/$@
STRIP = @strip $(OUTDIR)/$@
else
PLATFORM = None
COMPILE = echo
STRIP =
$(error Windows requires VS environment, or Cygwin)
endif
else
PLATFORM = $(shell uname)
ifeq ($(PLATFORM),Darwin)
PLATFORM=OSX
else ifeq ($(PLATFORM),Linux)
PLATFORM=Linux
else
$(error Unsupported platform. Non Windows platform support: OSX and Linux)
endif
COMPILE = @echo ::::: Building $(PLATFORM) $@ & gcc $(INCLUDES) -O3 -Wall -Werror -pthread -o $(OUTDIR)/$@
STRIP = @strip $(OUTDIR)/$@
TAIL = -lm
endif

View File

@@ -0,0 +1,174 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesBlock
//
// Encrypts or Decrypts a single 128 bit block specified on the command line as a hex string. Key is also on
// command line and may be 128, 192, or 256 bits in size.
//
// This is free and unencumbered software released into the public domain - November 2017 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "WjCryptLib_Aes.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DEFINES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
#define StringCaseInsensitiveCmp stricmp
#else
#define StringCaseInsensitiveCmp strcasecmp
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ReadHexData
//
// Reads a string as hex and places it in Data. *pDataSize on entry specifies maximum number of bytes that can be
// read, and on return is set to how many were read. This will be zero if it failed to read any.
// This function ignores any character that isn't a hex character.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void
ReadHexData
(
char const* HexString,
uint8_t* Data,
uint32_t* pDataSize
)
{
uint32_t i;
char holdingBuffer [3] = {0};
uint32_t holdingBufferIndex = 0;
unsigned hexToNumber;
unsigned outputIndex = 0;
for( i=0; i<strlen(HexString); i++ )
{
if( ( HexString[i] >= '0' && HexString[i] <= '9' )
|| ( HexString[i] >= 'A' && HexString[i] <= 'F' )
|| ( HexString[i] >= 'a' && HexString[i] <= 'f' ) )
{
holdingBuffer[holdingBufferIndex] = HexString[i];
holdingBufferIndex += 1;
if( 2 == holdingBufferIndex )
{
// Have two digits now so read it as a byte.
sscanf( holdingBuffer, "%x", &hexToNumber );
Data[outputIndex] = (uint8_t) hexToNumber;
outputIndex += 1;
if( outputIndex == *pDataSize )
{
// No more space so stop reading
break;
}
holdingBufferIndex = 0;
}
}
}
*pDataSize = outputIndex;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//
// Program entry point
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main
(
int ArgC,
char** ArgV
)
{
uint8_t block [128 / 8] = {0};
uint32_t blockSize = sizeof( block );
uint8_t key [256 / 8] = {0};
uint32_t keySize = sizeof( key );
uint8_t* bufferPtr;
uint32_t* bufferSizePtr;
uint32_t i;
uint32_t paramIndex = 0;
bool decryptMode = false;
AesContext aesContext;
if( 4 != ArgC && 3 != ArgC )
{
printf(
"Syntax\n"
" AesBlock [-D] <KeyHex> <BlockHex>\n" );
return 1;
}
for( i=1; i<(uint32_t)ArgC; i++ )
{
if( 0 == StringCaseInsensitiveCmp( ArgV[i], "-d" ) )
{
decryptMode = true;
}
else
{
if ( 0 == paramIndex ) { bufferPtr = key; bufferSizePtr = &keySize; }
else if( 1 == paramIndex ) { bufferPtr = block; bufferSizePtr = &blockSize; }
else
{
printf( "Invalid syntax\n" );
exit( 1 );
}
ReadHexData( ArgV[i], bufferPtr, bufferSizePtr );
paramIndex += 1;
}
}
if( 128/8 != blockSize )
{
printf( "Invalid block size, must be 128 bits (was %u bits)\n", blockSize*8 );
exit( 1 );
}
switch( keySize )
{
case 128/8: AesInitialise( &aesContext, key, AES_KEY_SIZE_128 ); break;
case 192/8: AesInitialise( &aesContext, key, AES_KEY_SIZE_192 ); break;
case 256/8: AesInitialise( &aesContext, key, AES_KEY_SIZE_256 ); break;
default:
printf( "Invalid key size, must be 128, 192, or 256 bits (was %u bits)\n", keySize*8 );
exit( 1 );
}
if( decryptMode )
{
AesDecryptInPlace( &aesContext, block );
}
else
{
AesEncryptInPlace( &aesContext, block );
}
// Display
for( i=0; i<sizeof(block); i++ )
{
printf( "%2.2x", block[i] );
}
printf( "\n" );
return 0;
}

View File

@@ -0,0 +1,6 @@
add_executable( AesBlock
AesBlock.c )
target_link_libraries( AesBlock
WjCryptLib )
install(TARGETS AesBlock DESTINATION .)

View File

@@ -0,0 +1,155 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesCtrOutput
//
// Outputs bytes from an AES CTR stream. Key and IV are taken from command line. Bytes are output as hex
//
// This is free and unencumbered software released into the public domain - November 2017 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "WjCryptLib_AesCtr.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DEFINITIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef __min
#define __min( x, y ) (((x) < (y))?(x):(y))
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CONSTANTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define BUFFER_SIZE 1024
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ReadHexData
//
// Reads a string as hex and places it in Data. *pDataSize on entry specifies maximum number of bytes that can be
// read, and on return is set to how many were read. This will be zero if it failed to read any.
// This function ignores any character that isn't a hex character.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void
ReadHexData
(
char const* HexString, // [in]
uint8_t* Data, // [out]
uint32_t* pDataSize // [in out]
)
{
uint32_t i;
char holdingBuffer [3] = {0};
uint32_t holdingBufferIndex = 0;
unsigned hexToNumber;
unsigned outputIndex = 0;
for( i=0; i<strlen(HexString); i++ )
{
if( ( HexString[i] >= '0' && HexString[i] <= '9' )
|| ( HexString[i] >= 'A' && HexString[i] <= 'F' )
|| ( HexString[i] >= 'a' && HexString[i] <= 'f' ) )
{
holdingBuffer[holdingBufferIndex] = HexString[i];
holdingBufferIndex += 1;
if( 2 == holdingBufferIndex )
{
// Have two digits now so read it as a byte.
sscanf( holdingBuffer, "%x", &hexToNumber );
Data[outputIndex] = (uint8_t) hexToNumber;
outputIndex += 1;
if( outputIndex == *pDataSize )
{
// No more space so stop reading
break;
}
holdingBufferIndex = 0;
}
}
}
*pDataSize = outputIndex;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//
// Program entry point
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main
(
int ArgC,
char** ArgV
)
{
uint32_t numBytes;
uint32_t i;
uint8_t buffer [BUFFER_SIZE];
uint32_t amountLeft;
uint32_t chunk;
AesCtrContext aesCtr;
uint8_t key [AES_KEY_SIZE_256];
uint32_t keySize = sizeof(key);
uint8_t IV [AES_CTR_IV_SIZE];
uint32_t IVSize = sizeof(IV);
if( 4 != ArgC )
{
printf(
"Syntax\n"
" AesCtrOutput <Key> <IV> <NumBytes>\n"
" <Key> - 128, 192, or 256 bit written as hex\n"
" <IV> - 64 bit written as hex\n"
" <NumBytes> - Number of bytes of stream to output\n" );
return 1;
}
ReadHexData( ArgV[1], key, &keySize );
if( AES_KEY_SIZE_128 != keySize && AES_KEY_SIZE_192 != keySize && AES_KEY_SIZE_256 != keySize )
{
printf( "Invalid key size. Must be 128, 192, or 256 bits\n" );
return 1;
}
ReadHexData( ArgV[2], IV, &IVSize );
if( AES_CTR_IV_SIZE != IVSize )
{
printf( "Invalid IV size. Must be 64 bits\n" );
return 1;
}
numBytes = atoi( ArgV[3] );
AesCtrInitialiseWithKey( &aesCtr, key, keySize, IV );
amountLeft = numBytes;
while( amountLeft > 0 )
{
chunk = __min( amountLeft, BUFFER_SIZE );
AesCtrOutput( &aesCtr, buffer, chunk );
amountLeft -= chunk;
for( i=0; i<chunk; i++ )
{
printf( "%2.2x", buffer[i] );
}
}
printf( "\n" );
return 0;
}

View File

@@ -0,0 +1,7 @@
add_executable( AesCtrOutput
AesCtrOutput.c )
target_link_libraries( AesCtrOutput
WjCryptLib )
install(TARGETS AesCtrOutput DESTINATION .)

View File

@@ -0,0 +1,155 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// AesOfbOutput
//
// Outputs bytes from an AES OFB stream. Key and IV are taken from command line. Bytes are output as hex
//
// This is free and unencumbered software released into the public domain - January 2018 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "WjCryptLib_AesOfb.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DEFINITIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef __min
#define __min( x, y ) (((x) < (y))?(x):(y))
#endif
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CONSTANTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define BUFFER_SIZE 1024
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ReadHexData
//
// Reads a string as hex and places it in Data. *pDataSize on entry specifies maximum number of bytes that can be
// read, and on return is set to how many were read. This will be zero if it failed to read any.
// This function ignores any character that isn't a hex character.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void
ReadHexData
(
char const* HexString, // [in]
uint8_t* Data, // [out]
uint32_t* pDataSize // [in out]
)
{
uint32_t i;
char holdingBuffer [3] = {0};
uint32_t holdingBufferIndex = 0;
unsigned hexToNumber;
unsigned outputIndex = 0;
for( i=0; i<strlen(HexString); i++ )
{
if( ( HexString[i] >= '0' && HexString[i] <= '9' )
|| ( HexString[i] >= 'A' && HexString[i] <= 'F' )
|| ( HexString[i] >= 'a' && HexString[i] <= 'f' ) )
{
holdingBuffer[holdingBufferIndex] = HexString[i];
holdingBufferIndex += 1;
if( 2 == holdingBufferIndex )
{
// Have two digits now so read it as a byte.
sscanf( holdingBuffer, "%x", &hexToNumber );
Data[outputIndex] = (uint8_t) hexToNumber;
outputIndex += 1;
if( outputIndex == *pDataSize )
{
// No more space so stop reading
break;
}
holdingBufferIndex = 0;
}
}
}
*pDataSize = outputIndex;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//
// Program entry point
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main
(
int ArgC,
char** ArgV
)
{
uint32_t numBytes;
uint32_t i;
uint8_t buffer [BUFFER_SIZE];
uint32_t amountLeft;
uint32_t chunk;
AesOfbContext aesOfb;
uint8_t key [AES_KEY_SIZE_256];
uint32_t keySize = sizeof(key);
uint8_t IV [AES_OFB_IV_SIZE];
uint32_t IVSize = sizeof(IV);
if( 4 != ArgC )
{
printf(
"Syntax\n"
" AesOfbOutput <Key> <IV> <NumBytes>\n"
" <Key> - 128, 192, or 256 bit written as hex\n"
" <IV> - 128 bit written as hex\n"
" <NumBytes> - Number of bytes of stream to output\n" );
return 1;
}
ReadHexData( ArgV[1], key, &keySize );
if( AES_KEY_SIZE_128 != keySize && AES_KEY_SIZE_192 != keySize && AES_KEY_SIZE_256 != keySize )
{
printf( "Invalid key size. Must be 128, 192, or 256 bits\n" );
return 1;
}
ReadHexData( ArgV[2], IV, &IVSize );
if( AES_OFB_IV_SIZE != IVSize )
{
printf( "Invalid IV size. Must be 128 bits\n" );
return 1;
}
numBytes = atoi( ArgV[3] );
AesOfbInitialiseWithKey( &aesOfb, key, keySize, IV );
amountLeft = numBytes;
while( amountLeft > 0 )
{
chunk = __min( amountLeft, BUFFER_SIZE );
AesOfbOutput( &aesOfb, buffer, chunk );
amountLeft -= chunk;
for( i=0; i<chunk; i++ )
{
printf( "%2.2x", buffer[i] );
}
}
printf( "\n" );
return 0;
}

View File

@@ -0,0 +1,7 @@
add_executable( AesOfbOutput
AesOfbOutput.c )
target_link_libraries( AesOfbOutput
WjCryptLib )
install(TARGETS AesOfbOutput DESTINATION .)

View File

@@ -0,0 +1,8 @@
SET( MODULE_NAME Md5String )
add_executable( ${MODULE_NAME}
Md5String.c )
target_link_libraries( ${MODULE_NAME}
WjCryptLib )
install(TARGETS ${MODULE_NAME} DESTINATION .)

View File

@@ -1,30 +1,30 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Md5String
//
// Outputs MD5 hash of a string specified on command line. Hash is output in hex
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "LibMd5.h"
#include "WjCryptLib_Md5.h"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//
// Program entry point
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main
(
@@ -59,4 +59,3 @@ int
return 0;
}

View File

@@ -0,0 +1,7 @@
add_executable( Rc4Output
Rc4Output.c )
target_link_libraries( Rc4Output
WjCryptLib )
install(TARGETS Rc4Output DESTINATION .)

View File

@@ -1,44 +1,44 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Rc4String
//
// Outputs bytes from an RC4 stream. Key is taken from command line. Bytes are output as hex
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "LibRc4.h"
#include "WjCryptLib_Rc4.h"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DEFINITIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef __min
#define __min( x, y ) (((x) < (y))?(x):(y))
#endif
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CONSTANTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define BUFFER_SIZE 1024
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//
// Program entry point
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main
(
@@ -72,7 +72,6 @@ int
Rc4Initialise( &rc4, string, (uint32_t)strlen(string), dropN );
amountLeft = numBytes;
while( amountLeft > 0 )
{
@@ -90,4 +89,3 @@ int
return 0;
}

View File

@@ -0,0 +1,7 @@
add_executable( Sha1String
Sha1String.c )
target_link_libraries( Sha1String
WjCryptLib )
install(TARGETS Sha1String DESTINATION .)

View File

@@ -1,30 +1,30 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha1String
//
// Outputs SHA1 hash of a string specified on command line. Hash is output in hex
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "LibSha1.h"
#include "WjCryptLib_Sha1.h"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//
// Program entry point
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main
(
@@ -59,4 +59,3 @@ int
return 0;
}

View File

@@ -0,0 +1,6 @@
add_executable( Sha256String
Sha256String.c )
target_link_libraries( Sha256String
WjCryptLib )
install(TARGETS Sha256String DESTINATION .)

View File

@@ -1,30 +1,30 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha256String
//
// Outputs SHA256 hash of a string specified on command line. Hash is output in hex
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "LibSha256.h"
#include "WjCryptLib_Sha256.h"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//
// Program entry point
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main
(
@@ -59,4 +59,3 @@ int
return 0;
}

View File

@@ -0,0 +1,7 @@
add_executable( Sha512String
Sha512String.c )
target_link_libraries( Sha512String
WjCryptLib )
install(TARGETS Sha512String DESTINATION .)

View File

@@ -1,30 +1,30 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sha512String
//
// Outputs SHA512 hash of a string specified on command line. Hash is output in hex
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "LibSha512.h"
#include "WjCryptLib_Sha512.h"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//
// Program entry point
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main
(
@@ -59,4 +59,3 @@ int
return 0;
}

View File

@@ -0,0 +1,20 @@
SET( MODULE_NAME WjCryptLibTest )
add_executable( ${MODULE_NAME}
WjCryptLibTest.c
WjCryptLibTest_Hashes.c
WjCryptLibTest_Hashes.h
WjCryptLibTest_Rc4.c
WjCryptLibTest_Rc4.h
WjCryptLibTest_Aes.c
WjCryptLibTest_Aes.h
WjCryptLibTest_AesCbc.c
WjCryptLibTest_AesCbc.h
WjCryptLibTest_AesCtr.c
WjCryptLibTest_AesCtr.h
WjCryptLibTest_AesOfb.c
WjCryptLibTest_AesOfb.h )
target_link_libraries( ${MODULE_NAME}
WjCryptLib )
install(TARGETS ${MODULE_NAME} DESTINATION .)

View File

@@ -0,0 +1,83 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLibTest
//
// Tests the cryptography functions against known test vectors to verify algorithms are correct.
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "WjCryptLibTest_Aes.h"
#include "WjCryptLibTest_AesCbc.h"
#include "WjCryptLibTest_AesCtr.h"
#include "WjCryptLibTest_AesOfb.h"
#include "WjCryptLibTest_Hashes.h"
#include "WjCryptLibTest_Rc4.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//
// Program entry point
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main
(
void
)
{
bool success;
bool allSuccess = true;
printf(
"WjCryptLibTest\n"
"------------\n"
"\n" );
success = TestHashes( );
if( !success ) { allSuccess = false; }
success = TestRc4( );
if( !success ) { allSuccess = false; }
printf( "Test RC4 - %s\n", success?"Pass":"Fail" );
success = TestAes( );
if( !success ) { allSuccess = false; }
printf( "Test AES - %s\n", success?"Pass":"Fail" );
success = TestAesCbc( );
if( !success ) { allSuccess = false; }
printf( "Test AES CBC - %s\n", success?"Pass":"Fail" );
success = TestAesCtr( );
if( !success ) { allSuccess = false; }
printf( "Test AES CTR - %s\n", success?"Pass":"Fail" );
success = TestAesOfb( );
if( !success ) { allSuccess = false; }
printf( "Test AES OFB - %s\n", success?"Pass":"Fail" );
printf( "\n" );
if( allSuccess )
{
printf( "All tests passed.\n" );
}
else
{
printf( "Fail.\n" );
return 1;
}
return 0;
}

View File

@@ -0,0 +1,149 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLibTest_Aes
//
// Tests the cryptography functions against known test vectors to verify algorithms are correct.
// Tests the following:
// AES
//
// This is free and unencumbered software released into the public domain - November 2017 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "WjCryptLib_Aes.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MAX_PLAINTEXT_SIZE 100
typedef struct
{
uint8_t PlainText [16];
uint8_t Key128 [16];
uint8_t CipherText128 [16];
uint8_t Key192 [24];
uint8_t CipherText192 [16];
uint8_t Key256 [32];
uint8_t CipherText256 [16];
} TestVector;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GLOBALS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static TestVector gTestVectors [] =
{
{
{ 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a },
{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
{ 0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60, 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97 },
{ 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b },
{ 0xbd, 0x33, 0x4f, 0x1d, 0x6e, 0x45, 0xf2, 0x5f, 0xf7, 0x12, 0xa2, 0x14, 0x57, 0x1f, 0xa5, 0xcc },
{ 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 },
{ 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8 },
},
{
{ 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51 },
{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
{ 0xf5, 0xd3, 0xd5, 0x85, 0x03, 0xb9, 0x69, 0x9d, 0xe7, 0x85, 0x89, 0x5a, 0x96, 0xfd, 0xba, 0xaf },
{ 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b },
{ 0x97, 0x41, 0x04, 0x84, 0x6d, 0x0a, 0xd3, 0xad, 0x77, 0x34, 0xec, 0xb3, 0xec, 0xee, 0x4e, 0xef },
{ 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 },
{ 0x59, 0x1c, 0xcb, 0x10, 0xd4, 0x10, 0xed, 0x26, 0xdc, 0x5b, 0xa7, 0x4a, 0x31, 0x36, 0x28, 0x70 },
},
{
{ 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef },
{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
{ 0x43, 0xb1, 0xcd, 0x7f, 0x59, 0x8e, 0xce, 0x23, 0x88, 0x1b, 0x00, 0xe3, 0xed, 0x03, 0x06, 0x88 },
{ 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b },
{ 0xef, 0x7a, 0xfd, 0x22, 0x70, 0xe2, 0xe6, 0x0a, 0xdc, 0xe0, 0xba, 0x2f, 0xac, 0xe6, 0x44, 0x4e },
{ 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 },
{ 0xb6, 0xed, 0x21, 0xb9, 0x9c, 0xa6, 0xf4, 0xf9, 0xf1, 0x53, 0xe7, 0xb1, 0xbe, 0xaf, 0xed, 0x1d },
},
{
{ 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 },
{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c },
{ 0x7b, 0x0c, 0x78, 0x5e, 0x27, 0xe8, 0xad, 0x3f, 0x82, 0x23, 0x20, 0x71, 0x04, 0x72, 0x5d, 0xd4 },
{ 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b },
{ 0x9a, 0x4b, 0x41, 0xba, 0x73, 0x8d, 0x6c, 0x72, 0xfb, 0x16, 0x69, 0x16, 0x03, 0xc1, 0x8e, 0x0e },
{ 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 },
{ 0x23, 0x30, 0x4b, 0x7a, 0x39, 0xf9, 0xf3, 0xff, 0x06, 0x7d, 0x8d, 0x8f, 0x9e, 0x24, 0xec, 0xc7 },
},
};
#define NUM_TEST_VECTORS ( sizeof(gTestVectors) / sizeof(gTestVectors[0]) )
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// EXPORTED FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestAes
//
// Test AES algorithm against test vectors
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
TestAes
(
void
)
{
uint32_t i;
AesContext context;
bool success = true;
uint8_t decBlock128 [16];
uint8_t encBlock128 [16];
uint8_t decBlock192 [16];
uint8_t encBlock192 [16];
uint8_t decBlock256 [16];
uint8_t encBlock256 [16];
for( i=0; i<NUM_TEST_VECTORS; i++ )
{
AesInitialise( &context, gTestVectors[i].Key128, AES_KEY_SIZE_128 );
AesEncrypt( &context, gTestVectors[i].PlainText, encBlock128 );
AesDecrypt( &context, gTestVectors[i].CipherText128, decBlock128 );
AesInitialise( &context, gTestVectors[i].Key192, AES_KEY_SIZE_192 );
AesEncrypt( &context, gTestVectors[i].PlainText, encBlock192 );
AesDecrypt( &context, gTestVectors[i].CipherText192, decBlock192 );
AesInitialise( &context, gTestVectors[i].Key256, AES_KEY_SIZE_256 );
AesEncrypt( &context, gTestVectors[i].PlainText, encBlock256 );
AesDecrypt( &context, gTestVectors[i].CipherText256, decBlock256 );
if( memcmp( encBlock128, &gTestVectors[i].CipherText128, 16 ) == 0
&& memcmp( decBlock128, &gTestVectors[i].PlainText, 16 ) == 0
&& memcmp( encBlock192, &gTestVectors[i].CipherText192, 16 ) == 0
&& memcmp( decBlock192, &gTestVectors[i].PlainText, 16 ) == 0
&& memcmp( encBlock256, &gTestVectors[i].CipherText256, 16 ) == 0
&& memcmp( decBlock256, &gTestVectors[i].PlainText, 16 ) == 0 )
{
// Test vector passed
}
else
{
printf( "TestAes - Test vector %u failed\n", i );
success = false;
}
}
return success;
}

View File

@@ -0,0 +1,30 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLibTest_Aes
//
// Tests the cryptography functions against known test vectors to verify algorithms are correct.
// Tests the following:
// AES
//
// This is free and unencumbered software released into the public domain - November 2017 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdbool.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// EXPORTED FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestAes
//
// Test AES algorithm against test vectors
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
TestAes
(
void
);

View File

@@ -0,0 +1,299 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLibTest_AesCbc
//
// Tests the cryptography functions against known test vectors to verify algorithms are correct.
// Tests the following:
// AES CBC
//
// This is free and unencumbered software released into the public domain - March 2018 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "WjCryptLib_AesCbc.h"
#include "WjCryptLib_Sha1.h"
#include "WjCryptLib_Rc4.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MACROS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MIN( x, y ) ( ((x)<(y))?(x):(y) )
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MAX_PLAINTEXT_SIZE 100
typedef struct
{
char* KeyHex;
char* IvHex;
char* CipherTextHex;
} TestVector;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GLOBALS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// These test vectors were created using openssl. Using the following commands:
// (Note: As CBC is not a stream cipher, the input is created using an RC4 stream generated from a key of 0)
// (Also note: openssl outputs an additional block of data due to some padding. We ignore this)
// > dd if=/dev/zero iflag=count_bytes count=64 status=none | openssl enc -rc4 -K 0 | openssl enc -aes-128-cbc -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000 | head -c 64 | xxd -p -c 64
// > dd if=/dev/zero iflag=count_bytes count=64 status=none | openssl enc -rc4 -K 0 | openssl enc -aes-128-cbc -K 0102030405060708a1a2a3a4a5a6a7a8 -iv 00000000000000000000000000000000 | head -c 64 | xxd -p -c 64
// > dd if=/dev/zero iflag=count_bytes count=64 status=none | openssl enc -rc4 -K 0 | openssl enc -aes-128-cbc -K 00000000000000000000000000000000 -iv b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8 | head -c 64 | xxd -p -c 64
// > dd if=/dev/zero iflag=count_bytes count=64 status=none | openssl enc -rc4 -K 0 | openssl enc -aes-128-cbc -K 0102030405060708a1a2a3a4a5a6a7a8 -iv b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8 | head -c 64 | xxd -p -c 64
// > dd if=/dev/zero iflag=count_bytes count=64 status=none | openssl enc -rc4 -K 0 | openssl enc -aes-192-cbc -K 0102030405060708a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8 -iv c1c2c3c4c5c6c7c8d1d2d3d4d5d6d7d8 | head -c 64 | xxd -p -c 64
// > dd if=/dev/zero iflag=count_bytes count=64 status=none | openssl enc -rc4 -K 0 | openssl enc -aes-256-cbc -K 0102030405060708a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8 -iv d1d2d3d4d5d6d7d8e1e2e3e4e5e6e7e8 | head -c 64 | xxd -p -c 64
static TestVector gTestVectors [] =
{
{
"00000000000000000000000000000000",
"00000000000000000000000000000000",
"c2af41ffe8b9f1b295d68038e3e8ed3f70b72b168cd3d402ccbf0bb4fa12561fc703951c91d8ce81c5643155b5db1d34eb7b36c2cc4715c03ea24944bb5c5625"
},
{
"0102030405060708a1a2a3a4a5a6a7a8",
"00000000000000000000000000000000",
"638198794af111670d5d7a7e13851484f71831108a5a134a9329787ad73379eb449e5068150233c4f0ae8c08d86708bc09724efaad3e6936e03c58f83f2abf3f"
},
{
"00000000000000000000000000000000",
"b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8",
"c696d1b757d5b4ee2069d1c50b1e5569aa931d0ecc058a5adce099e2f844153db0cf0884102720e42ab58efe449faba054edd92c4006fffbd9b0aec297b852ae"
},
{
"0102030405060708a1a2a3a4a5a6a7a8",
"b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8",
"a3c80c1c5ee817ad5faf31c6610e7895f480bdc9055362f0a7148b47b1dc5f11d041d94026266625cd6b512451a539ee9f3820667a84ace6cfbbe7edf746a14d"
},
{
"0102030405060708a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8",
"c1c2c3c4c5c6c7c8d1d2d3d4d5d6d7d8",
"93928e29c82e5536bc5942c35bbbd4d7a69f0a7daa35c77ecb13b3ac2c46c473cb608f403982d8401385fd7fe66a1e329aa0f90a50180fb73b36e98cb7214736"
},
{
"0102030405060708a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8",
"d1d2d3d4d5d6d7d8e1e2e3e4e5e6e7e8",
"2b559a644b62f1540c4ff9c50140fadedeefd49de9827dfbc8be8e4f7e2ac4ea746c8432d184059f62facaf765d90eadb7bdecac5e23bdc23f4026cd32d18ae2"
},
};
#define NUM_TEST_VECTORS ( sizeof(gTestVectors) / sizeof(gTestVectors[0]) )
#define TEST_VECTOR_OUTPUT_SIZE 48
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// HexToBytes
//
// Reads a string as hex and places it in Data. This function will output as many bytes as represented in the input
// string, it will not check the output buffer length. On return *pDataSize will be number of bytes read.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void
HexToBytes
(
char const* HexString, // [in]
uint8_t* Data, // [out]
uint32_t* pDataSize // [out optional]
)
{
uint32_t i;
char holdingBuffer [3] = {0};
unsigned hexToNumber;
uint32_t outputIndex = 0;
for( i=0; i<strlen(HexString)/2; i++ )
{
holdingBuffer[0] = HexString[i*2 + 0];
holdingBuffer[1] = HexString[i*2 + 1];
sscanf( holdingBuffer, "%x", &hexToNumber );
Data[i] = (uint8_t) hexToNumber;
outputIndex += 1;
}
if( NULL != pDataSize )
{
*pDataSize = outputIndex;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestVectors
//
// Tests AES CBC against fixed test vectors
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestVectors
(
void
)
{
uint32_t vectorIndex;
uint8_t key [AES_KEY_SIZE_256];
uint32_t keySize = 0;
uint8_t iv [AES_CBC_IV_SIZE];
uint8_t vector [TEST_VECTOR_OUTPUT_SIZE];
uint8_t aesCbcOutput [TEST_VECTOR_OUTPUT_SIZE];
uint8_t decryptBuffer [TEST_VECTOR_OUTPUT_SIZE];
uint8_t inputBuffer [TEST_VECTOR_OUTPUT_SIZE] = {0};
uint8_t rc4Key = 0;
// We can't encrypt just a zero buffer or we will end up with same result as OFB. As this is not a stream
// cipher we need to change the input. These test vectors were generated by using an RC4 stream as input.
// The RC4 stream is created by using a key of 0.
Rc4XorWithKey( &rc4Key, sizeof(rc4Key), 0, inputBuffer, inputBuffer, sizeof(inputBuffer) );
for( vectorIndex=0; vectorIndex<NUM_TEST_VECTORS; vectorIndex++ )
{
HexToBytes( gTestVectors[vectorIndex].KeyHex, key, &keySize );
HexToBytes( gTestVectors[vectorIndex].IvHex, iv, NULL );
HexToBytes( gTestVectors[vectorIndex].CipherTextHex, vector, NULL );
AesCbcEncryptWithKey( key, keySize, iv, inputBuffer, aesCbcOutput, TEST_VECTOR_OUTPUT_SIZE );
if( 0 != memcmp( aesCbcOutput, vector, TEST_VECTOR_OUTPUT_SIZE ) )
{
printf( "Test vector (index:%u) failed\n", vectorIndex );
return false;
}
AesCbcDecryptWithKey( key, keySize, iv, aesCbcOutput, decryptBuffer, TEST_VECTOR_OUTPUT_SIZE );
if( 0 != memcmp( decryptBuffer, inputBuffer, TEST_VECTOR_OUTPUT_SIZE ) )
{
printf( "Test vector (index:%u) failed decrypt\n", vectorIndex );
return false;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestLargeVector
//
// Tests AES OFB against a known large vector (of 1 million bytes). We check it against a known SHA-1 hash of
// the output.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestLargeVector
(
void
)
{
//dd if=/dev/zero iflag=count_bytes count=1000000 status=none | openssl enc -rc4 -K 0 | openssl enc -aes-128-cbc -K 00001111222233334444555566667777 -iv 88889999aaaabbbbccccddddeeeeffff | head -c 1000000 | openssl sha1
//(stdin)= 859463d3f0f27e67d37f05603f19b9d5c71c2059
uint8_t const* key = (uint8_t const*)"\x00\x00\x11\x11\x22\x22\x33\x33\x44\x44\x55\x55\x66\x66\x77\x77";
uint8_t const* iv = (uint8_t const*)"\x88\x88\x99\x99\xaa\xaa\xbb\xbb\xcc\xcc\xdd\xdd\xee\xee\xff\xff";
uint8_t const* sha1Hash = (uint8_t const*)"\x85\x94\x63\xd3\xf0\xf2\x7e\x67\xd3\x7f\x05\x60\x3f\x19\xb9\xd5\xc7\x1c\x20\x59";
uint32_t const numBytesToGenerate = 1000000;
uint8_t const rc4Key = 0;
uint8_t* buffer = malloc( numBytesToGenerate );
uint8_t* buffer2 = malloc( numBytesToGenerate );
uint32_t amountLeft = numBytesToGenerate;
uint32_t chunkSize;
Sha1Context sha1Context;
AesCbcContext aesCbcContext;
SHA1_HASH calcSha1;
uint32_t offset;
SHA1_HASH initialInputSha1;
// Encrypt in one go first.
// Generate the Rc4 stream to encrypt
memset( buffer, 0, numBytesToGenerate );
Rc4XorWithKey( &rc4Key, 1, 0, buffer, buffer, numBytesToGenerate );
Sha1Calculate( buffer, numBytesToGenerate, &initialInputSha1 );
AesCbcEncryptWithKey( key, AES_KEY_SIZE_128, iv, buffer, buffer2, numBytesToGenerate );
Sha1Initialise( &sha1Context );
Sha1Update( &sha1Context, buffer2, numBytesToGenerate );
Sha1Finalise( &sha1Context, &calcSha1 );
if( 0 != memcmp( &calcSha1, sha1Hash, SHA1_HASH_SIZE ) )
{
printf( "Large test vector failed (1)\n" );
return false;
}
// Now decrypt the buffer to verify it goes back to the original.
AesCbcDecryptWithKey( key, AES_KEY_SIZE_128, iv, buffer, buffer2, numBytesToGenerate );
Sha1Calculate( buffer, numBytesToGenerate, &calcSha1 );
if( 0 != memcmp( &calcSha1, &initialInputSha1, SHA1_HASH_SIZE ) )
{
printf( "Large test vector failed decrypting\n" );
return false;
}
memset( buffer, 0, numBytesToGenerate );
// Now encrypt in smaller pieces (10000 bytes at a time)
Sha1Initialise( &sha1Context );
AesCbcInitialiseWithKey( &aesCbcContext, key, AES_KEY_SIZE_128, iv );
memset( buffer, 0, numBytesToGenerate );
Rc4XorWithKey( &rc4Key, 1, 0, buffer, buffer, numBytesToGenerate );
offset = 0;
while( amountLeft > 0 )
{
chunkSize = MIN( amountLeft, 10000 );
AesCbcEncrypt( &aesCbcContext, buffer+offset, buffer+offset, chunkSize );
Sha1Update( &sha1Context, buffer+offset, chunkSize );
amountLeft -= chunkSize;
offset += chunkSize;
}
Sha1Finalise( &sha1Context, &calcSha1 );
if( 0 != memcmp( &calcSha1, sha1Hash, SHA1_HASH_SIZE ) )
{
printf( "Large test vector failed (2)\n" );
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestAesOfb
//
// Test AES CBC algorithm
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
TestAesCbc
(
void
)
{
bool totalSuccess = true;
bool success;
success = TestVectors( );
if( !success ) { totalSuccess = false; }
success = TestLargeVector( );
if( !success ) { totalSuccess = false; }
return totalSuccess;
}

View File

@@ -0,0 +1,30 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLibTest_AesCbc
//
// Tests the cryptography functions against known test vectors to verify algorithms are correct.
// Tests the following:
// AES CBC
//
// This is free and unencumbered software released into the public domain - March 2018 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdbool.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// EXPORTED FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestAesCbc
//
// Test AES CBC algorithm
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
TestAesCbc
(
void
);

View File

@@ -0,0 +1,379 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLibTest_AesCtr
//
// Tests the cryptography functions against known test vectors to verify algorithms are correct.
// Tests the following:
// AES CTR
//
// This is free and unencumbered software released into the public domain - November 2017 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "WjCryptLib_AesCtr.h"
#include "WjCryptLib_Sha1.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MACROS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MIN( x, y ) ( ((x)<(y))?(x):(y) )
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MAX_PLAINTEXT_SIZE 100
typedef struct
{
char* KeyHex;
char* IvHex;
char* CipherTextHex;
} TestVector;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GLOBALS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// These test vectors were created using openssl. Using the following commands:
// > openssl enc -aes-128-ctr -K 00000000000000000000000000000000 -iv 0000000000000000 -in zero.bin -out output0.bin
// > openssl enc -aes-128-ctr -K 0102030405060708a1a2a3a4a5a6a7a8 -iv 0000000000000000 -in zero.bin -out output1.bin
// > openssl enc -aes-128-ctr -K 00000000000000000000000000000000 -iv b1b2b3b4b5b6b7b8 -in zero.bin -out output2.bin
// > openssl enc -aes-128-ctr -K 0102030405060708a1a2a3a4a5a6a7a8 -iv b1b2b3b4b5b6b7b8 -in zero.bin -out output3.bin
// > openssl enc -aes-192-ctr -K 0102030405060708a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8 -iv c1c2c3c4c5c6c7c8 -in zero.bin -out output4.bin
// > openssl enc -aes-256-ctr -K 0102030405060708a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8 -iv d1d2d3d4d5d6d7d8 -in zero.bin -out output5.bin
// Where zero.bin is a file containing 48 zero bytes.
static TestVector gTestVectors [] =
{
{
"00000000000000000000000000000000",
"0000000000000000",
"66e94bd4ef8a2c3b884cfa59ca342b2e58e2fccefa7e3061367f1d57a4e7455a0388dace60b6a392f328c2b971b2fe78"
},
{
"0102030405060708a1a2a3a4a5a6a7a8",
"0000000000000000",
"cdb33c236caa155b28d14e6db350537141fa2f4eafecf40a986f83229c7e74d30a981d4547b3c802ea215ed55a858a08"
},
{
"00000000000000000000000000000000",
"b1b2b3b4b5b6b7b8",
"5ddcedba6a63f96e2b0429ee1a4459fc85e7e624ab33b89fdc4e88c034d483273568e033c96ad8a0bf5b420f4b43600d"
},
{
"0102030405060708a1a2a3a4a5a6a7a8",
"b1b2b3b4b5b6b7b8",
"7f1e34c4f33ee8dc162af7fbed6f317aa5806d244dd86557268be2296708ef7327aa4e5ed5780a3c070209ea2db04d79"
},
{
"0102030405060708a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8",
"c1c2c3c4c5c6c7c8",
"8bd0847cad4f66dec6abeadcc85d1e0a62ab64931e16f1e8ccb6212c5cea3672c27d4cfd74b3e87ee2d787cc93f24496"
},
{
"0102030405060708a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8",
"d1d2d3d4d5d6d7d8",
"1419da0fdac1f19ec0eb64af657201c672ab0df425d3faec3b67d70c86d5f780a222b63dbbc71ae7749417449dc39bfb"
},
};
#define NUM_TEST_VECTORS ( sizeof(gTestVectors) / sizeof(gTestVectors[0]) )
#define TEST_VECTOR_OUTPUT_SIZE 48
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// HexToBytes
//
// Reads a string as hex and places it in Data. This function will output as many bytes as represented in the input
// string, it will not check the output buffer length. On return *pDataSize will be number of bytes read.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void
HexToBytes
(
char const* HexString, // [in]
uint8_t* Data, // [out]
uint32_t* pDataSize // [out optional]
)
{
uint32_t i;
char holdingBuffer [3] = {0};
unsigned hexToNumber;
uint32_t outputIndex = 0;
for( i=0; i<strlen(HexString)/2; i++ )
{
holdingBuffer[0] = HexString[i*2 + 0];
holdingBuffer[1] = HexString[i*2 + 1];
sscanf( holdingBuffer, "%x", &hexToNumber );
Data[i] = (uint8_t) hexToNumber;
outputIndex += 1;
}
if( NULL != pDataSize )
{
*pDataSize = outputIndex;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestVectors
//
// Tests AES CTR against fixed test vectors
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestVectors
(
void
)
{
uint32_t vectorIndex;
uint8_t key [AES_KEY_SIZE_256];
uint32_t keySize = 0;
uint8_t iv [AES_CTR_IV_SIZE];
uint8_t vector [TEST_VECTOR_OUTPUT_SIZE];
uint8_t aesCtrOutput [TEST_VECTOR_OUTPUT_SIZE];
uint8_t const zeroBuffer [TEST_VECTOR_OUTPUT_SIZE] = {0};
for( vectorIndex=0; vectorIndex<NUM_TEST_VECTORS; vectorIndex++ )
{
HexToBytes( gTestVectors[vectorIndex].KeyHex, key, &keySize );
HexToBytes( gTestVectors[vectorIndex].IvHex, iv, NULL );
HexToBytes( gTestVectors[vectorIndex].CipherTextHex, vector, NULL );
AesCtrXorWithKey( key, keySize, iv, zeroBuffer, aesCtrOutput, TEST_VECTOR_OUTPUT_SIZE );
if( 0 != memcmp( aesCtrOutput, vector, TEST_VECTOR_OUTPUT_SIZE ) )
{
printf( "Test vector (index:%u) failed\n", vectorIndex );
return false;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestLargeVector
//
// Tests AES CTR against a known large vector (of 1 million bytes). We check it against a known SHA-1 hash of
// the output.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestLargeVector
(
void
)
{
//dd if=/dev/zero iflag=count_bytes count=1000000 status=none | openssl enc -aes-128-ctr -K 00001111222233334444555566667777 -iv 88889999aaaabbbb | openssl sha1
//(stdin)= 6227c0192b110133fadd6d229790bbdf13c068ab
uint8_t const* key = (uint8_t const*)"\x00\x00\x11\x11\x22\x22\x33\x33\x44\x44\x55\x55\x66\x66\x77\x77";
uint8_t const* iv = (uint8_t const*)"\x88\x88\x99\x99\xaa\xaa\xbb\xbb";
uint8_t const* sha1Hash = (uint8_t const*)"\xe1\x63\x5f\xa4\xf5\x7c\x98\x54\xf6\x18\xec\x0c\x8f\x18\x7f\x04\x34\xa2\xe1\x72";
uint32_t const numBytesToGenerate = 1000000;
uint8_t* buffer = malloc( numBytesToGenerate );
uint32_t amountLeft = numBytesToGenerate;
uint32_t chunkSize;
Sha1Context sha1Context;
AesCtrContext aesCtrContext;
SHA1_HASH calcSha1;
// Encrypt in one go first.
memset( buffer, 0, numBytesToGenerate );
AesCtrXorWithKey( key, AES_KEY_SIZE_128, iv, buffer, buffer, numBytesToGenerate );
Sha1Initialise( &sha1Context );
Sha1Update( &sha1Context, buffer, numBytesToGenerate );
Sha1Finalise( &sha1Context, &calcSha1 );
if( 0 != memcmp( &calcSha1, sha1Hash, SHA1_HASH_SIZE ) )
{
printf( "Large test vector failed\n" );
return false;
}
memset( buffer, 0, numBytesToGenerate );
// Now encrypt in smaller pieces (10000 bytes at a time)
Sha1Initialise( &sha1Context );
AesCtrInitialiseWithKey( &aesCtrContext, key, AES_KEY_SIZE_128, iv );
while( amountLeft > 0 )
{
memset( buffer, 0, numBytesToGenerate );
chunkSize = MIN( amountLeft, 10000 );
AesCtrOutput( &aesCtrContext, buffer, chunkSize );
Sha1Update( &sha1Context, buffer, chunkSize );
amountLeft -= chunkSize;
}
Sha1Finalise( &sha1Context, &calcSha1 );
if( 0 != memcmp( &calcSha1, sha1Hash, SHA1_HASH_SIZE ) )
{
printf( "Large test vector failed\n" );
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestStreamConsistency
//
// Tests that an AES CTR stream is consistent regardless of the chunk sizes of the requests and/or stream
// repositioning.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestStreamConsistency
(
void
)
{
bool success = true;
uint8_t const key[AES_KEY_SIZE_128] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
uint8_t const iv[AES_CTR_IV_SIZE] = { 1,2,3,4,5,6,7,8 };
#define STREAMSIZE 1000
uint8_t stream [STREAMSIZE];
uint8_t newStream [STREAMSIZE];
uint8_t const zeroStream [STREAMSIZE] = {0};
AesCtrContext context;
uint32_t chunkSize;
// First fill in stream with 1000 bytes generated in one go.
memset( stream, 0, STREAMSIZE );
AesCtrXorWithKey( key, sizeof(key), iv, stream, stream, STREAMSIZE );
// Perform sanity check that the key is not all zero!
if( 0 == memcmp( stream, zeroStream, STREAMSIZE ) )
{
printf( "AES CTR Stream all zero\n" );
success = false;
return success;
}
// Now recreate the stream in small bits. Starting at 1 byte at a time and increasing chunk size
AesCtrInitialiseWithKey( &context, key, sizeof(key), iv );
for( chunkSize=1; chunkSize<64; chunkSize++ )
{
uint32_t amountLeft = STREAMSIZE;
uint32_t offset = 0;
memset( newStream, 0, STREAMSIZE );
while( amountLeft > 0 )
{
uint32_t thisChunkSize = MIN( chunkSize, amountLeft );
// Set stream position to +8 where it currently is, this will mean half the time it will have to
// reset the internal block. We are going to ignore this position and bring it back straight away,
// we just want to verify that it can handle being moved around.
AesCtrSetStreamIndex( &context, offset+8 );
// Set stream pointer to correct place and output the chunk
AesCtrSetStreamIndex( &context, offset );
AesCtrOutput( &context, newStream+offset, thisChunkSize );
offset += thisChunkSize;
amountLeft -= thisChunkSize;
}
// Now verify that the stream is consistent with the one generated all at once.
if( 0 != memcmp( stream, newStream, STREAMSIZE ) )
{
printf( "AES CTR Stream not consistent\n" );
success = false;
break;
}
}
#undef STREAMSIZE
return success;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestEndianCorrectness
//
// Verifies that endianess is handled correctly. This will force the internal block counter to be a large number
// that uses multiple bytes, and then checks the final output.
// This should return correctly regardless of big or little endian processor.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestEndianCorrectness
(
void
)
{
AesCtrContext context;
uint8_t const key [AES_KEY_SIZE_128] = { 1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4 };
uint8_t const iv [AES_CTR_IV_SIZE] = { 5,5,5,5,6,6,6,6 };
uint64_t const positionIndex = 0x1020304050607080ULL;
uint8_t output [256 / 8] = {0};
uint8_t const vector [256 / 8] =
{ 0x17, 0x07, 0x27, 0x7b, 0x9e, 0x51, 0xdf, 0x5b,
0x23, 0xbe, 0xa1, 0xce, 0xc9, 0x40, 0x49, 0xfc,
0xf8, 0x8f, 0x45, 0xd1, 0xf6, 0x68, 0x28, 0x54,
0x6f, 0xef, 0xce, 0xf9, 0x23, 0x1b, 0xb0, 0x08 };
AesCtrInitialiseWithKey( &context, key, sizeof(key), iv );
AesCtrSetStreamIndex( &context, positionIndex );
AesCtrOutput( &context, output, sizeof(output) );
if( 0 != memcmp( vector, output, sizeof(vector) ) )
{
printf( "Fail on endianness test\n" );
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestAesCtr
//
// Test AES CTR algorithm
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
TestAesCtr
(
void
)
{
bool totalSuccess = true;
bool success;
success = TestVectors( );
if( !success ) { totalSuccess = false; }
success = TestLargeVector( );
if( !success ) { totalSuccess = false; }
success = TestStreamConsistency( );
if( !success ) { totalSuccess = false; }
success = TestEndianCorrectness( );
if( !success ) { totalSuccess = false; }
return totalSuccess;
}

View File

@@ -0,0 +1,30 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLibTest_AesCtr
//
// Tests the cryptography functions against known test vectors to verify algorithms are correct.
// Tests the following:
// AES CTR
//
// This is free and unencumbered software released into the public domain - November 2017 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdbool.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// EXPORTED FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestAesCtr
//
// Test AES CTR algorithm
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
TestAesCtr
(
void
);

View File

@@ -0,0 +1,331 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLibTest_AesOfb
//
// Tests the cryptography functions against known test vectors to verify algorithms are correct.
// Tests the following:
// AES OFB
//
// This is free and unencumbered software released into the public domain - January 2018 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "WjCryptLib_AesOfb.h"
#include "WjCryptLib_Sha1.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MACROS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MIN( x, y ) ( ((x)<(y))?(x):(y) )
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MAX_PLAINTEXT_SIZE 100
typedef struct
{
char* KeyHex;
char* IvHex;
char* CipherTextHex;
} TestVector;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GLOBALS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// These test vectors were created using openssl. Using the following commands:
// > dd if=/dev/zero iflag=count_bytes count=48 status=none | openssl enc -aes-128-ofb -K 00000000000000000000000000000000 -iv 00000000000000000000000000000000 | xxd -p -c 48
// > dd if=/dev/zero iflag=count_bytes count=48 status=none | openssl enc -aes-128-ofb -K 0102030405060708a1a2a3a4a5a6a7a8 -iv 00000000000000000000000000000000 | xxd -p -c 48
// > dd if=/dev/zero iflag=count_bytes count=48 status=none | openssl enc -aes-128-ofb -K 00000000000000000000000000000000 -iv b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8 | xxd -p -c 48
// > dd if=/dev/zero iflag=count_bytes count=48 status=none | openssl enc -aes-128-ofb -K 0102030405060708a1a2a3a4a5a6a7a8 -iv b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8 | xxd -p -c 48
// > dd if=/dev/zero iflag=count_bytes count=48 status=none | openssl enc -aes-192-ofb -K 0102030405060708a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8 -iv c1c2c3c4c5c6c7c8d1d2d3d4d5d6d7d8 | xxd -p -c 48
// > dd if=/dev/zero iflag=count_bytes count=48 status=none | openssl enc -aes-256-ofb -K 0102030405060708a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8 -iv d1d2d3d4d5d6d7d8e1e2e3e4e5e6e7e8 | xxd -p -c 48
static TestVector gTestVectors [] =
{
{
"00000000000000000000000000000000",
"00000000000000000000000000000000",
"66e94bd4ef8a2c3b884cfa59ca342b2ef795bd4a52e29ed713d313fa20e98dbca10cf66d0fddf3405370b4bf8df5bfb3"
},
{
"0102030405060708a1a2a3a4a5a6a7a8",
"00000000000000000000000000000000",
"cdb33c236caa155b28d14e6db35053718a906fc0050ae8ad054621e487e5b0a264873309a9471152104a0a51361a91af"
},
{
"00000000000000000000000000000000",
"b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8",
"93fc4d6374dc544d40181d39066e9b0077aa627a84dbd57c9e72a1bbbc8bd1e082faf44d5ce57f6320e9f33d38a3a268"
},
{
"0102030405060708a1a2a3a4a5a6a7a8",
"b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8",
"551eb0c4d89d7e1b537b30f627cc5a0afdebd5a07483107df8555dbae9453189ae13766c9678554971151486cee958af"
},
{
"0102030405060708a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8",
"c1c2c3c4c5c6c7c8d1d2d3d4d5d6d7d8",
"e9128df92fd1da443f826d84fd46be40fffb4ad23477a02efb14cbfd9a28ebcc2e6a5948cd1980e7cd6f5d386f7f6539"
},
{
"0102030405060708a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8c1c2c3c4c5c6c7c8",
"d1d2d3d4d5d6d7d8e1e2e3e4e5e6e7e8",
"06a9a20023d47df78a5ead97715a85921cab7d5114fb74a1b99e66d915a0e125a0fcf198d93364235f9a33c02dc170f6"
},
};
#define NUM_TEST_VECTORS ( sizeof(gTestVectors) / sizeof(gTestVectors[0]) )
#define TEST_VECTOR_OUTPUT_SIZE 48
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// HexToBytes
//
// Reads a string as hex and places it in Data. This function will output as many bytes as represented in the input
// string, it will not check the output buffer length. On return *pDataSize will be number of bytes read.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
void
HexToBytes
(
char const* HexString, // [in]
uint8_t* Data, // [out]
uint32_t* pDataSize // [out optional]
)
{
uint32_t i;
char holdingBuffer [3] = {0};
unsigned hexToNumber;
uint32_t outputIndex = 0;
for( i=0; i<strlen(HexString)/2; i++ )
{
holdingBuffer[0] = HexString[i*2 + 0];
holdingBuffer[1] = HexString[i*2 + 1];
sscanf( holdingBuffer, "%x", &hexToNumber );
Data[i] = (uint8_t) hexToNumber;
outputIndex += 1;
}
if( NULL != pDataSize )
{
*pDataSize = outputIndex;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestVectors
//
// Tests AES OFB against fixed test vectors
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestVectors
(
void
)
{
uint32_t vectorIndex;
uint8_t key [AES_KEY_SIZE_256];
uint32_t keySize = 0;
uint8_t iv [AES_OFB_IV_SIZE];
uint8_t vector [TEST_VECTOR_OUTPUT_SIZE];
uint8_t aesOfbOutput [TEST_VECTOR_OUTPUT_SIZE];
uint8_t const zeroBuffer [TEST_VECTOR_OUTPUT_SIZE] = {0};
for( vectorIndex=0; vectorIndex<NUM_TEST_VECTORS; vectorIndex++ )
{
HexToBytes( gTestVectors[vectorIndex].KeyHex, key, &keySize );
HexToBytes( gTestVectors[vectorIndex].IvHex, iv, NULL );
HexToBytes( gTestVectors[vectorIndex].CipherTextHex, vector, NULL );
AesOfbXorWithKey( key, keySize, iv, zeroBuffer, aesOfbOutput, TEST_VECTOR_OUTPUT_SIZE );
if( 0 != memcmp( aesOfbOutput, vector, TEST_VECTOR_OUTPUT_SIZE ) )
{
printf( "Test vector (index:%u) failed\n", vectorIndex );
return false;
}
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestLargeVector
//
// Tests AES OFB against a known large vector (of 1 million bytes). We check it against a known SHA-1 hash of
// the output.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestLargeVector
(
void
)
{
//dd if=/dev/zero iflag=count_bytes count=1000000 status=none | openssl enc -aes-128-ofb -K 00001111222233334444555566667777 -iv 88889999aaaabbbbccccddddeeeeffff | openssl sha1
//(stdin)= a0824dca21938b33a5a8db26c8ab2428624db6d3
uint8_t const* key = (uint8_t const*)"\x00\x00\x11\x11\x22\x22\x33\x33\x44\x44\x55\x55\x66\x66\x77\x77";
uint8_t const* iv = (uint8_t const*)"\x88\x88\x99\x99\xaa\xaa\xbb\xbb\xcc\xcc\xdd\xdd\xee\xee\xff\xff";
uint8_t const* sha1Hash = (uint8_t const*)"\xa0\x82\x4d\xca\x21\x93\x8b\x33\xa5\xa8\xdb\x26\xc8\xab\x24\x28\x62\x4d\xb6\xd3";
uint32_t const numBytesToGenerate = 1000000;
uint8_t* buffer = malloc( numBytesToGenerate );
uint32_t amountLeft = numBytesToGenerate;
uint32_t chunkSize;
Sha1Context sha1Context;
AesOfbContext aesOfbContext;
SHA1_HASH calcSha1;
// Encrypt in one go first.
memset( buffer, 0, numBytesToGenerate );
AesOfbXorWithKey( key, AES_KEY_SIZE_128, iv, buffer, buffer, numBytesToGenerate );
Sha1Initialise( &sha1Context );
Sha1Update( &sha1Context, buffer, numBytesToGenerate );
Sha1Finalise( &sha1Context, &calcSha1 );
if( 0 != memcmp( &calcSha1, sha1Hash, SHA1_HASH_SIZE ) )
{
printf( "Large test vector failed\n" );
return false;
}
memset( buffer, 0, numBytesToGenerate );
// Now encrypt in smaller pieces (10000 bytes at a time)
Sha1Initialise( &sha1Context );
AesOfbInitialiseWithKey( &aesOfbContext, key, AES_KEY_SIZE_128, iv );
while( amountLeft > 0 )
{
memset( buffer, 0, numBytesToGenerate );
chunkSize = MIN( amountLeft, 10000 );
AesOfbOutput( &aesOfbContext, buffer, chunkSize );
Sha1Update( &sha1Context, buffer, chunkSize );
amountLeft -= chunkSize;
}
Sha1Finalise( &sha1Context, &calcSha1 );
if( 0 != memcmp( &calcSha1, sha1Hash, SHA1_HASH_SIZE ) )
{
printf( "Large test vector failed\n" );
return false;
}
return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestStreamConsistency
//
// Tests that an AES OFB stream is consistent regardless of the chunk sizes of the requests and/or stream
// repositioning.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestStreamConsistency
(
void
)
{
bool success = true;
uint8_t const key[AES_KEY_SIZE_128] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
uint8_t const iv[AES_OFB_IV_SIZE] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
#define STREAMSIZE 1000
uint8_t stream [STREAMSIZE];
uint8_t newStream [STREAMSIZE];
uint8_t const zeroStream [STREAMSIZE] = {0};
AesOfbContext context;
uint32_t chunkSize;
// First fill in stream with 1000 bytes generated in one go.
memset( stream, 0, STREAMSIZE );
AesOfbXorWithKey( key, sizeof(key), iv, stream, stream, STREAMSIZE );
// Perform sanity check that the key is not all zero!
if( 0 == memcmp( stream, zeroStream, STREAMSIZE ) )
{
printf( "AES OFB Stream all zero\n" );
success = false;
return success;
}
// Now recreate the stream in small bits. Starting at 1 byte at a time and increasing chunk size
for( chunkSize=1; chunkSize<64; chunkSize++ )
{
uint32_t amountLeft = STREAMSIZE;
uint32_t offset = 0;
memset( newStream, 0, STREAMSIZE );
AesOfbInitialiseWithKey( &context, key, sizeof(key), iv );
while( amountLeft > 0 )
{
uint32_t thisChunkSize = MIN( chunkSize, amountLeft );
AesOfbOutput( &context, newStream+offset, thisChunkSize );
offset += thisChunkSize;
amountLeft -= thisChunkSize;
}
// Now verify that the stream is consistent with the one generated all at once.
if( 0 != memcmp( stream, newStream, STREAMSIZE ) )
{
printf( "AES OFB Stream not consistent\n" );
success = false;
break;
}
}
#undef STREAMSIZE
return success;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestAesOfb
//
// Test AES OFB algorithm
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
TestAesOfb
(
void
)
{
bool totalSuccess = true;
bool success;
success = TestVectors( );
if( !success ) { totalSuccess = false; }
success = TestLargeVector( );
if( !success ) { totalSuccess = false; }
success = TestStreamConsistency( );
if( !success ) { totalSuccess = false; }
return totalSuccess;
}

View File

@@ -0,0 +1,30 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLibTest_AesOfb
//
// Tests the cryptography functions against known test vectors to verify algorithms are correct.
// Tests the following:
// AES OFB
//
// This is free and unencumbered software released into the public domain - January 2018 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdbool.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// EXPORTED FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestAesOfb
//
// Test AES OFB algorithm
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
TestAesOfb
(
void
);

View File

@@ -1,53 +1,52 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CryptLibTest
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLibTest_Hashes
//
// Tests the cryptography functions against known test vectors to verify algorithyms are correct.
// Tests the hash functions against known test vectors to verify algorithms are correct.
// Tests the following:
// RC4
// MD5
// SHA1
// SHA256
// SHA512
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "LibRc4.h"
#include "LibMd5.h"
#include "LibSha1.h"
#include "LibSha256.h"
#include "LibSha512.h"
#include "WjCryptLib_Md5.h"
#include "WjCryptLib_Sha1.h"
#include "WjCryptLib_Sha256.h"
#include "WjCryptLib_Sha512.h"
#include "WjCryptLibTest_Aes.h"
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TYPES
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define MAX_PLAINTEXT_SIZE 100
typedef struct
{
char PlainText [MAX_PLAINTEXT_SIZE];
int PlainTextSize; // 0 to use (int)strlen
uint32_t PlainTextSize; // 0 to use (uint32_t)strlen
MD5_HASH Md5Hash;
SHA1_HASH Sha1Hash;
SHA256_HASH Sha256Hash;
SHA512_HASH Sha512Hash;
} TestVector;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GLOBALS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static TestVector gTestVectors [] =
static TestVector gTestVectors [] =
{
{
"", 0,
@@ -60,7 +59,7 @@ static TestVector gTestVectors [] =
0x47,0xd0,0xd1,0x3c,0x5d,0x85,0xf2,0xb0,0xff,0x83,0x18,0xd2,0x87,0x7e,0xec,0x2f,
0x63,0xb9,0x31,0xbd,0x47,0x41,0x7a,0x81,0xa5,0x38,0x32,0x7a,0xf9,0x27,0xda,0x3e}}, // sha512
},
{
{
"a", 0,
{{0x0c,0xc1,0x75,0xb9,0xc0,0xf1,0xb6,0xa8,0x31,0xc3,0x99,0xe2,0x69,0x77,0x26,0x61}}, // md5
{{0x86,0xf7,0xe4,0x37,0xfa,0xa5,0xa7,0xfc,0xe1,0x5d,0x1d,0xdc,0xb9,0xea,0xea,0xea,0x37,0x76,0x67,0xb8}}, // sha1
@@ -173,15 +172,15 @@ static TestVector gTestVectors [] =
};
#define NUM_TEST_VECTORS ( sizeof(gTestVectors) / sizeof(gTestVectors[0]) )
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PRIVATE FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestMd5
//
//
// Test MD5 algorithm against test vectors
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestMd5
@@ -189,9 +188,9 @@ bool
void
)
{
int i;
int k;
int len;
uint32_t i;
uint32_t k;
uint32_t len;
Md5Context context;
MD5_HASH hash;
bool success = true;
@@ -199,7 +198,7 @@ bool
for( i=0; i<NUM_TEST_VECTORS; i++ )
{
Md5Initialise( &context );
len = (int) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (int)strlen( gTestVectors[i].PlainText );
len = (uint32_t) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (uint32_t)strlen( gTestVectors[i].PlainText );
Md5Update( &context, gTestVectors[i].PlainText, (uint32_t)len );
Md5Finalise( &context, &hash );
@@ -218,7 +217,7 @@ bool
for( i=0; i<NUM_TEST_VECTORS; i++ )
{
Md5Initialise( &context );
len = (int) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (int)strlen( gTestVectors[i].PlainText );
len = (uint32_t) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (uint32_t)strlen( gTestVectors[i].PlainText );
for( k=0; k<len; k++ )
{
Md5Update( &context, &gTestVectors[i].PlainText[k], 1 );
@@ -236,15 +235,14 @@ bool
}
}
return success;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestSha1
//
//
// Test SHA1 algorithm against test vectors
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestSha1
@@ -252,9 +250,9 @@ bool
void
)
{
int i;
int k;
int len;
uint32_t i;
uint32_t k;
uint32_t len;
Sha1Context context;
SHA1_HASH hash;
bool success = true;
@@ -262,7 +260,7 @@ bool
for( i=0; i<NUM_TEST_VECTORS; i++ )
{
Sha1Initialise( &context );
len = (int) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (int)strlen( gTestVectors[i].PlainText );
len = (uint32_t) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (uint32_t)strlen( gTestVectors[i].PlainText );
Sha1Update( &context, gTestVectors[i].PlainText, (uint32_t)len );
Sha1Finalise( &context, &hash );
@@ -281,7 +279,7 @@ bool
for( i=0; i<NUM_TEST_VECTORS; i++ )
{
Sha1Initialise( &context );
len = (int) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (int)strlen( gTestVectors[i].PlainText );
len = (uint32_t) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (uint32_t)strlen( gTestVectors[i].PlainText );
for( k=0; k<len; k++ )
{
Sha1Update( &context, &gTestVectors[i].PlainText[k], 1 );
@@ -299,16 +297,14 @@ bool
}
}
return success;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestSha256
//
//
// Test SHA1 algorithm against test vectors
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestSha256
@@ -316,9 +312,9 @@ bool
void
)
{
int i;
int k;
int len;
uint32_t i;
uint32_t k;
uint32_t len;
Sha256Context context;
SHA256_HASH hash;
bool success = true;
@@ -326,7 +322,7 @@ bool
for( i=0; i<NUM_TEST_VECTORS; i++ )
{
Sha256Initialise( &context );
len = (int) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (int)strlen( gTestVectors[i].PlainText );
len = (uint32_t) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (uint32_t)strlen( gTestVectors[i].PlainText );
Sha256Update( &context, gTestVectors[i].PlainText, (uint32_t)len );
Sha256Finalise( &context, &hash );
@@ -345,7 +341,7 @@ bool
for( i=0; i<NUM_TEST_VECTORS; i++ )
{
Sha256Initialise( &context );
len = (int) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (int)strlen( gTestVectors[i].PlainText );
len = (uint32_t) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (uint32_t)strlen( gTestVectors[i].PlainText );
for( k=0; k<len; k++ )
{
Sha256Update( &context, &gTestVectors[i].PlainText[k], 1 );
@@ -366,11 +362,11 @@ bool
return success;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestSha512
//
//
// Test SHA1 algorithm against test vectors
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestSha512
@@ -378,9 +374,9 @@ bool
void
)
{
int i;
int k;
int len;
uint32_t i;
uint32_t k;
uint32_t len;
Sha512Context context;
SHA512_HASH hash;
bool success = true;
@@ -388,7 +384,7 @@ bool
for( i=0; i<NUM_TEST_VECTORS; i++ )
{
Sha512Initialise( &context );
len = (int) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (int)strlen( gTestVectors[i].PlainText );
len = (uint32_t) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (uint32_t)strlen( gTestVectors[i].PlainText );
Sha512Update( &context, gTestVectors[i].PlainText, (uint32_t)len );
Sha512Finalise( &context, &hash );
@@ -407,7 +403,7 @@ bool
for( i=0; i<NUM_TEST_VECTORS; i++ )
{
Sha512Initialise( &context );
len = (int) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (int)strlen( gTestVectors[i].PlainText );
len = (uint32_t) gTestVectors[i].PlainTextSize ? gTestVectors[i].PlainTextSize : (uint32_t)strlen( gTestVectors[i].PlainText );
for( k=0; k<len; k++ )
{
Sha512Update( &context, &gTestVectors[i].PlainText[k], 1 );
@@ -428,81 +424,17 @@ bool
return success;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestRc4
//
// Test RC4 algorithm against test vectors
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static
bool
TestRc4
(
void
)
{
struct
{
char Key [100];
int Drop;
uint8_t Output [16];
} TestVectors [] =
{
{ "Key", 0, {0xeb,0x9f,0x77,0x81,0xb7,0x34,0xca,0x72,0xa7,0x19,0x4a,0x28,0x67,0xb6,0x42,0x95} },
{ "Wiki", 0, {0x60,0x44,0xdb,0x6d,0x41,0xb7,0xe8,0xe7,0xa4,0xd6,0xf9,0xfb,0xd4,0x42,0x83,0x54} },
{ "Secret", 0, {0x04,0xd4,0x6b,0x05,0x3c,0xa8,0x7b,0x59,0x41,0x72,0x30,0x2a,0xec,0x9b,0xb9,0x92} },
{ "Key", 1, {0x9f,0x77,0x81,0xb7,0x34,0xca,0x72,0xa7,0x19,0x4a,0x28,0x67,0xb6,0x42,0x95,0x0d} },
{ "Key", 256, {0x92,0xfd,0xd9,0xb6,0xe4,0x04,0xef,0x4f,0xa0,0x75,0xf1,0xa3,0x44,0xed,0x81,0x6b} },
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Rc4Context context;
uint8_t output [16];
int i;
bool success = true;
for( i=0; i<(sizeof(TestVectors)/sizeof(TestVectors[0])); i++ )
{
Rc4Initialise( &context, TestVectors[i].Key, (uint8_t)strlen(TestVectors[i].Key), TestVectors[i].Drop );
Rc4Output( &context, output, sizeof(output) );
if( memcmp( output, TestVectors[i].Output, sizeof(output) ) != 0 )
{
printf( "TestRc4 - Failed test vector: %u\n", i );
success = false;
}
}
// Test by doing drop manually
for( i=0; i<(sizeof(TestVectors)/sizeof(TestVectors[0])); i++ )
{
int x;
Rc4Initialise( &context, TestVectors[i].Key, (uint8_t)strlen(TestVectors[i].Key), 0 );
for( x=0; x<TestVectors[i].Drop; x++ )
{
Rc4Output( &context, output, 1 );
}
Rc4Output( &context, output, sizeof(output) );
if( memcmp( output, TestVectors[i].Output, sizeof(output) ) != 0 )
{
printf( "TestRc4 - Failed test vector: %u [manual drop]\n", i );
success = false;
}
}
return success;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestHashes
//
// Program entry point
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int
main
// Test Hash functions algorithm against test vectors
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
TestHashes
(
void
)
@@ -510,41 +442,21 @@ int
bool success;
bool allSuccess = true;
printf(
"CryptLibTest\n"
"------------\n"
"\n" );
success = TestMd5( );
if( !success ) { allSuccess = false; }
printf( "Test MD5 - %s\n", success?"Pass":"Fail" );
printf( "Test MD5 - %s\n", success?"Pass":"Fail" );
success = TestSha1( );
if( !success ) { allSuccess = false; }
printf( "Test SHA1 - %s\n", success?"Pass":"Fail" );
printf( "Test SHA1 - %s\n", success?"Pass":"Fail" );
success = TestSha256( );
if( !success ) { allSuccess = false; }
printf( "Test SHA256 - %s\n", success?"Pass":"Fail" );
printf( "Test SHA256 - %s\n", success?"Pass":"Fail" );
success = TestSha512( );
if( !success ) { allSuccess = false; }
printf( "Test SHA512 - %s\n", success?"Pass":"Fail" );
printf( "Test SHA512 - %s\n", success?"Pass":"Fail" );
success = TestRc4( );
if( !success ) { allSuccess = false; }
printf( "Test RC4 - %s\n", success?"Pass":"Fail" );
printf( "\n" );
if( allSuccess )
{
printf( "All tests passed.\n" );
}
else
{
printf( "Fail.\n" );
return 1;
}
return 0;
return allSuccess;
}

View File

@@ -0,0 +1,33 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLibTest_Hashes
//
// Tests the hash functions against known test vectors to verify algorithms are correct.
// Tests the following:
// MD5
// SHA1
// SHA256
// SHA512
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdbool.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestHashes
//
// Test Hash functions algorithm against test vectors
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
TestHashes
(
void
);

View File

@@ -0,0 +1,84 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLibTest_Rc4
//
// Tests the RC4 function against known test vectors to verify algorithms are correct.
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "WjCryptLib_Rc4.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestRc4
//
// Test RC4 algorithm against test vectors
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
TestRc4
(
void
)
{
struct
{
char Key [100];
uint32_t Drop;
uint8_t Output [16];
} TestVectors [] =
{
{ "Key", 0, {0xeb,0x9f,0x77,0x81,0xb7,0x34,0xca,0x72,0xa7,0x19,0x4a,0x28,0x67,0xb6,0x42,0x95} },
{ "Wiki", 0, {0x60,0x44,0xdb,0x6d,0x41,0xb7,0xe8,0xe7,0xa4,0xd6,0xf9,0xfb,0xd4,0x42,0x83,0x54} },
{ "Secret", 0, {0x04,0xd4,0x6b,0x05,0x3c,0xa8,0x7b,0x59,0x41,0x72,0x30,0x2a,0xec,0x9b,0xb9,0x92} },
{ "Key", 1, {0x9f,0x77,0x81,0xb7,0x34,0xca,0x72,0xa7,0x19,0x4a,0x28,0x67,0xb6,0x42,0x95,0x0d} },
{ "Key", 256, {0x92,0xfd,0xd9,0xb6,0xe4,0x04,0xef,0x4f,0xa0,0x75,0xf1,0xa3,0x44,0xed,0x81,0x6b} },
};
Rc4Context context;
uint8_t output [16];
uint32_t i;
bool success = true;
for( i=0; i<(sizeof(TestVectors)/sizeof(TestVectors[0])); i++ )
{
Rc4Initialise( &context, TestVectors[i].Key, (uint8_t)strlen(TestVectors[i].Key), TestVectors[i].Drop );
Rc4Output( &context, output, sizeof(output) );
if( memcmp( output, TestVectors[i].Output, sizeof(output) ) != 0 )
{
printf( "TestRc4 - Failed test vector: %u\n", i );
success = false;
}
}
// Test by doing drop manually
for( i=0; i<(sizeof(TestVectors)/sizeof(TestVectors[0])); i++ )
{
uint32_t x;
Rc4Initialise( &context, TestVectors[i].Key, (uint8_t)strlen(TestVectors[i].Key), 0 );
for( x=0; x<TestVectors[i].Drop; x++ )
{
Rc4Output( &context, output, 1 );
}
Rc4Output( &context, output, sizeof(output) );
if( memcmp( output, TestVectors[i].Output, sizeof(output) ) != 0 )
{
printf( "TestRc4 - Failed test vector: %u [manual drop]\n", i );
success = false;
}
}
return success;
}

View File

@@ -0,0 +1,28 @@
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WjCryptLibTest_Rc4
//
// Tests the RC4 function against known test vectors to verify algorithms are correct.
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdbool.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PUBLIC FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TestRc4
//
// Test RC4 algorithm against test vectors
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool
TestRc4
(
void
);

View File

@@ -1,27 +0,0 @@
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <stdbool.h>
//
// stdbool.h is part of the C99 standard. Unforunately MSVC compiler does not support C99. Although it has several
// types such as stdint.h, it does not include stdbool.h. This file defines the type bool and values true and false
// according to the C99 standard. This only needs to be included on systems such as MSVC that do not have stdbool.h
// gcc has its own version of stdbool.h that should be used in preference.
//
// This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef _STDBOOL_H_
#define _STDBOOL_H_
#ifndef __cplusplus
#ifndef __bool_true_false_are_defined
typedef int bool;
#define true 1
#define false 0
#define __bool_true_false_are_defined
#endif //__bool_true_false_are_defined
#endif //__cplusplus
#endif //_STDBOOL_H_