├── .gitignore ├── .travis.yml ├── CMakeLists.txt ├── Encryptions.h ├── Encryptions ├── AES.cpp ├── AES.h ├── AES_Const.h ├── Blowfish.cpp ├── Blowfish.h ├── Blowfish_Const.h ├── CAST128.cpp ├── CAST128.h ├── CAST128_Const.h ├── CAST256.cpp ├── CAST256.h ├── CAST_Const.h ├── CMakeLists.txt ├── Camellia.cpp ├── Camellia.h ├── Camellia_Const.h ├── DES.cpp ├── DES.h ├── DESX.cpp ├── DESX.h ├── DES_Const.h ├── GOST.cpp ├── GOST.h ├── GOST_Const.h ├── IDEA.cpp ├── IDEA.h ├── MISTY1.cpp ├── MISTY1.h ├── MISTY1_Const.h ├── RC2.cpp ├── RC2.h ├── RC2_Const.h ├── RC4.cpp ├── RC4.h ├── RC5.cpp ├── RC5.h ├── RC6.cpp ├── RC6.h ├── RC_PQ.cpp ├── RC_PQ.h ├── SAFERK64.cpp ├── SAFERK64.h ├── SAFERK64_Const.h ├── SEED.cpp ├── SEED.h ├── SEED_Const.h ├── Skipjack.cpp ├── Skipjack.h ├── Skipjack_Const.h ├── SymAlg.cpp ├── SymAlg.h ├── TDES.cpp ├── TDES.h ├── TEA.cpp ├── TEA.h ├── Twofish.cpp ├── Twofish.h ├── Twofish_Const.h ├── XTEA.cpp └── XTEA.h ├── LICENSE ├── README.md ├── common ├── CMakeLists.txt ├── cryptomath.h ├── includes.cpp └── includes.h ├── modes ├── CBC.cpp ├── CBC.h ├── CFB.cpp ├── CFB.h ├── CMakeLists.txt ├── CTR.cpp ├── CTR.h ├── ECB.cpp ├── ECB.h ├── OFB.cpp ├── OFB.h ├── PCPB.cpp └── PCPB.h └── tests ├── CMakeLists.txt ├── aes.cpp ├── blowfish.cpp ├── camellia.cpp ├── cast128.cpp ├── cast256.cpp ├── des.cpp ├── gost.cpp ├── idea.cpp ├── misty1.cpp ├── rc2.cpp ├── rc4.cpp ├── rc5.cpp ├── rc6.cpp ├── saferk64.cpp ├── seed.cpp ├── skipjack.cpp ├── tea.cpp ├── testvectors ├── aes │ ├── gfsbox128.h │ ├── gfsbox192.h │ ├── gfsbox256.h │ ├── sbox128.h │ ├── sbox192.h │ ├── sbox256.h │ ├── varkey128.h │ ├── varkey192.h │ ├── varkey256.h │ ├── vartxt128.h │ ├── vartxt192.h │ └── vartxt256.h ├── blowfish │ └── schneier.h ├── camellia │ └── ntt.h ├── cast │ ├── cast128rfc2144.h │ └── cast256rfc2612.h ├── des │ ├── nessieset1.h │ ├── nessieset2.h │ ├── nessieset3.h │ └── nessieset4.h ├── gost │ └── testvectors.h ├── idea │ ├── nessieset1.h │ ├── nessieset2.h │ ├── nessieset3.h │ ├── nessieset4.h │ ├── nessieset5.h │ ├── nessieset6.h │ ├── nessieset7.h │ └── nessieset8.h ├── misty1 │ ├── nessieset1.h │ ├── nessieset2.h │ ├── nessieset3.h │ └── nessieset4.h ├── plainkeycipher.h ├── rc │ ├── rc2rfc2268.h │ ├── rc4scicrypt.h │ ├── rc5nessieset1.h │ ├── rc5nessieset2.h │ ├── rc5nessieset3.h │ ├── rc5nessieset4.h │ ├── rc5paper.h │ ├── rc6nessieset1.h │ ├── rc6nessieset2.h │ ├── rc6nessieset3.h │ └── rc6nessieset4.h ├── safer │ └── saferk64paper.h ├── seed │ ├── nessieset1.h │ ├── nessieset2.h │ ├── nessieset3.h │ └── nessieset4.h ├── skipjack │ ├── nist.h │ └── scicrypt.h ├── tdes │ ├── invperm.h │ ├── permop.h │ ├── subtab.h │ ├── varkey.h │ └── vartxt.h ├── tea │ └── edipermadi.h ├── twofish │ ├── vk.h │ └── vt.h └── xtea │ └── tayloredge.h ├── tripledes.cpp ├── twofish.cpp └── xtea.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Build results 2 | *.o 3 | *.a 4 | *.exe 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # from 2 | 3 | language: cpp 4 | 5 | os: linux 6 | dist: bionic 7 | 8 | env: 9 | - CXX_COMPILER=g++-5 10 | - CXX_COMPILER=g++-6 11 | - CXX_COMPILER=g++-7 12 | - CXX_COMPILER=g++-8 13 | - CXX_COMPILER=clang++-5.0 14 | - CXX_COMPILER=clang++-6.0 15 | - CXX_COMPILER=clang++-7 16 | - CXX_COMPILER=clang++-8 17 | 18 | addons: 19 | apt: 20 | packages: 21 | - python3-pip 22 | sources: 23 | - ubuntu-toolchain-r-test 24 | update: false 25 | 26 | before_install: 27 | - if [[ "${CXX_COMPILER}" =~ "clang++" ]]; then wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add -; sudo apt-add-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-$(lsb_release -c | awk '{printf $2}')-${CXX_COMPILER##*-} main" -y; fi 28 | - sudo apt-get update 29 | - sudo apt-get install "${CXX_COMPILER}" cmake pkg-config $(if [[ "${CXX_COMPILER}" =~ "clang++-" ]]; then echo llvm; fi) -y 30 | 31 | before_script: 32 | - export CXX="${CXX_COMPILER}" 33 | - export GTEST_COLOR=1 34 | 35 | - cd .. 36 | 37 | # get googletest 38 | - git clone https://github.com/google/googletest.git 39 | - cd googletest 40 | - mkdir build 41 | - cd build 42 | - export GTEST_ROOT=$HOME/.local/googletest 43 | - cmake .. -DCMAKE_INSTALL_PREFIX=$GTEST_ROOT -DCMAKE_CXX_FLAGS="-std=c++11" 44 | - make -j 2 45 | - make install 46 | - cd ../.. 47 | 48 | # get newer CMake 49 | - pip3 install --user cmake 50 | - export CMAKE_ROOT=$(dirname $(dirname $(dirname $(pip3 show cmake | grep "Location:" | awk '{print $2}'))))/bin/cmake 51 | - export PATH=$(dirname ${CMAKE_ROOT}):$PATH 52 | 53 | - cd Encryptions 54 | 55 | script: 56 | - mkdir build 57 | - cd build 58 | - cmake .. 59 | - make 60 | - ctest -V 61 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11.4) 2 | project(Encryptions LANGUAGES CXX) 3 | 4 | # require C++11 5 | set(CMAKE_CXX_STANDARD 11) 6 | set(CMAKE_CXX_STANDARD_REQUIRED ON) 7 | set(CMAKE_CXX_EXTENSIONS OFF) 8 | 9 | # cygwin doesn't define _POSIX_C_SOURCE by default 10 | if (CMAKE_SYSTEM_NAME STREQUAL "CYGWIN") 11 | add_definitions(-D_POSIX_C_SOURCE=200809L) 12 | endif() 13 | 14 | add_library(Encryptions_static STATIC Encryptions.h) 15 | 16 | # build a separate set of object files with PIC enabled 17 | set(BUILD_SHARED_LIB ON CACHE BOOL "Build a shared library") 18 | if (BUILD_SHARED_LIB) 19 | add_library(Encryptions_shared SHARED Encryptions.h) 20 | set_target_properties(Encryptions_shared PROPERTIES OUTPUT_NAME "Encryptions") 21 | install(TARGETS Encryptions_shared DESTINATION lib) 22 | endif() 23 | 24 | add_subdirectory(common) 25 | add_subdirectory(Encryptions) 26 | add_subdirectory(modes) 27 | enable_testing() 28 | add_subdirectory(tests) 29 | 30 | 31 | target_include_directories(Encryptions_static PUBLIC 32 | $ 33 | $ 34 | ) 35 | 36 | set_target_properties(Encryptions_static 37 | PROPERTIES 38 | ARCHIVE_OUTPUT_DIRECTORY ${BINARY_DIR}/lib 39 | LIBRARY_OUTPUT_DIRECTORY ${BINARY_DIR}/lib 40 | RUNTIME_OUTPUT_DIRECTORY ${BINARY_DIR} 41 | VERSION ${PROJECT_VERSION} 42 | SOVERSION 1 43 | DEBUG_POSTFIX "d" 44 | ) 45 | 46 | install(TARGETS Encryptions_static EXPORT Encryptions_staticConfig 47 | ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 48 | LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 49 | RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) 50 | 51 | install(FILES Encryptions.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include) 52 | install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/modes DESTINATION ${CMAKE_INSTALL_PREFIX}/include) 53 | install(EXPORT Encryptions_staticConfig DESTINATION share/Encryptions_staticConfig/cmake) 54 | export(TARGETS Encryptions_static FILE Encryptions_staticConfig.cmake) -------------------------------------------------------------------------------- /Encryptions.h: -------------------------------------------------------------------------------- 1 | /* 2 | Encryptions.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef ENCRYPTIONS_H 26 | #define ENCRYPTIONS_H 27 | 28 | // common includes 29 | #include "common/includes.h" 30 | #include "common/cryptomath.h" 31 | #include "Encryptions/SymAlg.h" 32 | 33 | // Algorithms 34 | #include "Encryptions/AES.h" 35 | #include "Encryptions/Blowfish.h" 36 | #include "Encryptions/Camellia.h" 37 | #include "Encryptions/CAST128.h" 38 | #include "Encryptions/CAST256.h" 39 | #include "Encryptions/DES.h" 40 | #include "Encryptions/DESX.h" 41 | #include "Encryptions/GOST.h" 42 | #include "Encryptions/IDEA.h" 43 | #include "Encryptions/MISTY1.h" 44 | #include "Encryptions/RC2.h" 45 | #include "Encryptions/RC4.h" 46 | #include "Encryptions/RC5.h" 47 | #include "Encryptions/RC6.h" 48 | #include "Encryptions/SAFERK64.h" 49 | #include "Encryptions/SEED.h" 50 | #include "Encryptions/Skipjack.h" 51 | #include "Encryptions/TDES.h" 52 | #include "Encryptions/TEA.h" 53 | #include "Encryptions/Twofish.h" 54 | #include "Encryptions/XTEA.h" 55 | 56 | // Modes of Operation 57 | #include "modes/ECB.h" 58 | #include "modes/CBC.h" 59 | #include "modes/CFB.h" 60 | #include "modes/CTR.h" 61 | #include "modes/OFB.h" 62 | #include "modes/PCPB.h" 63 | 64 | #endif -------------------------------------------------------------------------------- /Encryptions/AES.h: -------------------------------------------------------------------------------- 1 | /* 2 | AES.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | #ifndef __AES__ 29 | #define __AES__ 30 | 31 | #include "../common/cryptomath.h" 32 | #include "../common/includes.h" 33 | #include "SymAlg.h" 34 | 35 | #include "AES_Const.h" 36 | 37 | class AES : public SymAlg{ 38 | private: 39 | uint8_t b, rounds, columns; 40 | std::vector > keys; 41 | 42 | void shiftrow(std::vector & data); 43 | void invshiftrow(std::vector & data); 44 | uint8_t GF(uint8_t a, uint8_t b); 45 | void mixcolumns(std::vector & data); 46 | void invmixcolumns(std::vector & data); 47 | std::string OUT(std::vector & data); 48 | 49 | public: 50 | AES(); 51 | AES(const std::string & KEY); 52 | void setkey(const std::string & KEY); 53 | std::string encrypt(const std::string & DATA); 54 | std::string decrypt(const std::string & DATA); 55 | unsigned int blocksize() const; 56 | }; 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /Encryptions/Blowfish.cpp: -------------------------------------------------------------------------------- 1 | #include "Blowfish.h" 2 | 3 | uint32_t Blowfish::f(const uint32_t & left){ 4 | return ((((sbox[0][ left >> 24] + sbox[1][(left >> 16) & 255]) & mod32) ^ sbox[2][(left >> 8) & 255]) + sbox[3][left & 255]) & mod32; 5 | } 6 | 7 | std::string Blowfish::run(const std::string & data){ 8 | if (!keyset ^ settingkey){ 9 | throw std::runtime_error("Error: Key has not been set"); 10 | } 11 | 12 | if (data.size() != 8){ 13 | throw std::runtime_error("Error: Data must be 64 bits in length."); 14 | } 15 | 16 | uint32_t left = toint(data.substr(0, 4), 256), right = toint(data.substr(4, 4), 256); 17 | for(uint8_t i = 0; i < 16; i++){ 18 | left ^= p[i]; 19 | right ^= f(left); 20 | std::swap(left,right); 21 | } 22 | //std::swap(right, left); // Save 513 swaps 23 | right ^= p[17]; 24 | left ^= p[16]; 25 | return unhexlify(makehex(right, 8) + makehex(left, 8)); 26 | } 27 | 28 | Blowfish::Blowfish() 29 | : SymAlg(), 30 | settingkey(true), 31 | p(), sbox() 32 | {} 33 | 34 | Blowfish::Blowfish(const std::string & KEY) 35 | : Blowfish() 36 | { 37 | setkey(KEY); 38 | } 39 | 40 | void Blowfish::setkey(const std::string & KEY){ 41 | if (keyset){ 42 | throw std::runtime_error("Error: Key has already been set."); 43 | } 44 | 45 | if ((KEY.size() < 4) || (KEY.size() > 112)){ 46 | throw std::runtime_error("Error: Key size does not fit defined sizes."); 47 | } 48 | 49 | for(uint8_t x = 0; x < 18; x++){ 50 | p[x] = Blowfish_P[x]; 51 | } 52 | 53 | for(uint8_t x = 0; x < 4; x++){ 54 | for(uint16_t y = 0; y < 512; y++){ 55 | sbox[x][y] = Blowfish_SBOX[x][y]; 56 | } 57 | } 58 | 59 | std::string key = KEY; 60 | uint8_t s = (72 / key.size()) + 1; 61 | for(uint8_t i = 0; i < s; i++){ 62 | key += key; 63 | } 64 | 65 | key = key.substr(0, 72); 66 | for(uint8_t x = 0; x < 18; x++){ 67 | p[x] ^= static_cast (toint(key.substr(x << 2, 4), 256)); 68 | } 69 | 70 | std::string ini(8, 0); 71 | for(uint8_t x = 0; x < 9; x++){ 72 | std::string NEW = run(ini); 73 | ini = NEW; 74 | p[x << 1] = toint(NEW.substr(0, 4), 256); 75 | p[(x << 1) + 1] = toint(NEW.substr(4, 4), 256); 76 | } 77 | 78 | for(uint8_t x = 0; x < 4; x++){ 79 | for(uint8_t y = 0; y < 128; y++){ 80 | std::string NEW = run(ini); 81 | ini = NEW; 82 | sbox[x][y << 1] = toint(NEW.substr(0, 4), 256); 83 | sbox[x][(y << 1) + 1] = toint(NEW.substr(4, 4), 256); 84 | } 85 | } 86 | keyset = true; 87 | settingkey = false; 88 | } 89 | 90 | std::string Blowfish::encrypt(const std::string & DATA){ 91 | return run(DATA); 92 | } 93 | 94 | std::string Blowfish::decrypt(const std::string & DATA){ 95 | std::reverse(p, p + 18); 96 | std::string out = run(DATA); 97 | std::reverse(p, p + 18); 98 | return out; 99 | } 100 | 101 | unsigned int Blowfish::blocksize() const { 102 | return 64; 103 | } 104 | -------------------------------------------------------------------------------- /Encryptions/Blowfish.h: -------------------------------------------------------------------------------- 1 | /* 2 | Blowfish.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __BLOWFISH__ 26 | #define __BLOWFISH__ 27 | 28 | #include 29 | 30 | #include "../common/includes.h" 31 | #include "SymAlg.h" 32 | 33 | #include "Blowfish_Const.h" 34 | 35 | class Blowfish : public SymAlg{ 36 | private: 37 | bool settingkey; 38 | uint32_t p[18], sbox[4][512]; //Taken from a C file from the Blowfish site 39 | uint32_t f(const uint32_t & left); 40 | std::string run(const std::string & data); 41 | 42 | public: 43 | Blowfish(); 44 | Blowfish(const std::string & KEY); 45 | void setkey(const std::string & KEY); 46 | std::string encrypt(const std::string & DATA); 47 | std::string decrypt(const std::string & DATA); 48 | unsigned int blocksize() const; 49 | }; 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /Encryptions/CAST128.h: -------------------------------------------------------------------------------- 1 | /* 2 | CAST128.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __CAST128__ 26 | #define __CAST128__ 27 | 28 | #include "../common/cryptomath.h" 29 | #include "../common/includes.h" 30 | #include "SymAlg.h" 31 | 32 | #include "CAST_Const.h" 33 | #include "CAST128_Const.h" 34 | 35 | class CAST128 : public SymAlg{ 36 | private: 37 | uint8_t rounds, kr[16]; 38 | uint32_t km[16]; 39 | uint32_t F(const uint8_t & round, const uint32_t & D, const uint32_t & Kmi, const uint8_t & Kri); 40 | std::string run(const std::string & data, const uint8_t start, const uint8_t stop, const uint8_t step); 41 | 42 | public: 43 | CAST128(); 44 | CAST128(const std::string & KEY); 45 | void setkey(std::string KEY); 46 | std::string encrypt(const std::string & DATA); 47 | std::string decrypt(const std::string & DATA); 48 | unsigned int blocksize() const; 49 | }; 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /Encryptions/CAST256.cpp: -------------------------------------------------------------------------------- 1 | #include "CAST256.h" 2 | 3 | uint32_t CAST256::F1(const uint32_t Data, const uint32_t Kmi, const uint8_t Kri){ 4 | uint32_t temp = ROL((Kmi + Data) & mod32, Kri, 32); 5 | uint8_t Ia = temp >> 24, Ib = (temp >> 16) & 255, Ic = (temp >> 8) & 255, Id = temp & 255; 6 | return ((CAST_S1[Ia] ^ CAST_S2[Ib]) - CAST_S3[Ic] + CAST_S4[Id]) & mod32; 7 | } 8 | 9 | uint32_t CAST256::F2(const uint32_t Data, const uint32_t Kmi, const uint8_t Kri){ 10 | uint32_t temp = ROL(Kmi ^ Data, Kri, 32); 11 | uint8_t Ia = temp >> 24, Ib = (temp >> 16) & 255, Ic = (temp >> 8) & 255, Id = temp & 255; 12 | return ((CAST_S1[Ia] - CAST_S2[Ib] + CAST_S3[Ic]) & mod32) ^ CAST_S4[Id]; 13 | } 14 | 15 | uint32_t CAST256::F3(const uint32_t Data, const uint32_t Kmi, const uint8_t Kri){ 16 | uint32_t temp = ROL((Kmi - Data) & mod32, Kri, 32); 17 | uint8_t Ia = temp >> 24, Ib = (temp >> 16) & 255, Ic = (temp >> 8) & 255, Id = temp & 255; 18 | return ((((CAST_S1[Ia] + CAST_S2[Ib]) & mod32) ^ CAST_S3[Ic]) - CAST_S4[Id]) & mod32; 19 | } 20 | 21 | void CAST256::W(const uint8_t i){ 22 | g ^= F1(h, Tm[0][i], Tr[0][i]); 23 | f ^= F2(g, Tm[1][i], Tr[1][i]); 24 | e ^= F3(f, Tm[2][i], Tr[2][i]); 25 | d ^= F1(e, Tm[3][i], Tr[3][i]); 26 | c ^= F2(d, Tm[4][i], Tr[4][i]); 27 | b ^= F3(c, Tm[5][i], Tr[5][i]); 28 | a ^= F1(b, Tm[6][i], Tr[6][i]); 29 | h ^= F2(a, Tm[7][i], Tr[7][i]); 30 | } 31 | 32 | std::vector CAST256::kr(){ 33 | std::vector out; 34 | out.push_back(a & 31); 35 | out.push_back(c & 31); 36 | out.push_back(e & 31); 37 | out.push_back(g & 31); 38 | return out; 39 | } 40 | 41 | std::vector CAST256::km(){ 42 | std::vector out; 43 | out.push_back(h); 44 | out.push_back(f); 45 | out.push_back(d); 46 | out.push_back(b); 47 | return out; 48 | } 49 | 50 | void CAST256::Q(const uint8_t & i){ 51 | C ^= F1(D, Km[i][0], Kr[i][0]); 52 | B ^= F2(C, Km[i][1], Kr[i][1]); 53 | A ^= F3(B, Km[i][2], Kr[i][2]); 54 | D ^= F1(A, Km[i][3], Kr[i][3]); 55 | } 56 | 57 | void CAST256::QBAR(const uint8_t & i){ 58 | D ^= F1(A, Km[i][3], Kr[i][3]); 59 | A ^= F3(B, Km[i][2], Kr[i][2]); 60 | B ^= F2(C, Km[i][1], Kr[i][1]); 61 | C ^= F1(D, Km[i][0], Kr[i][0]); 62 | } 63 | 64 | std::string CAST256::run(const std::string & DATA){ 65 | if (!keyset){ 66 | throw std::runtime_error("Error: Key has not been set."); 67 | } 68 | 69 | if (DATA.size() != 16){ 70 | throw std::runtime_error("Error: Data must be 128 bits in length."); 71 | } 72 | 73 | A = toint(DATA.substr(0, 4), 256); 74 | B = toint(DATA.substr(4, 4), 256); 75 | C = toint(DATA.substr(8, 4), 256); 76 | D = toint(DATA.substr(12, 4), 256); 77 | for(uint8_t i = 0; i < 6; i++){ 78 | Q(i); 79 | } 80 | for(uint8_t i = 6; i < 12; i++){ 81 | QBAR(i); 82 | } 83 | return unhexlify(makehex(A, 8) + makehex(B, 8) + makehex(C, 8) + makehex(D, 8)); 84 | } 85 | 86 | CAST256::CAST256() 87 | : SymAlg(), 88 | A(0), B(0), C(0), D(0), a(0), b(0), c(0), d(0), e(0), f(0), g (0), h(0), 89 | Kr(0), Tr(0), 90 | Km(0), Tm(0) 91 | {} 92 | 93 | CAST256::CAST256(const std::string & KEY) 94 | : CAST256() 95 | { 96 | setkey(KEY); 97 | } 98 | 99 | void CAST256::setkey(std::string KEY){ 100 | if (keyset){ 101 | throw std::runtime_error("Error: Key has already been set."); 102 | } 103 | 104 | if ((KEY.size() != 16) && (KEY.size() != 20) && (KEY.size() != 24) && (KEY.size() != 28) && (KEY.size() != 32)){ 105 | throw std::runtime_error("Error: Key must be 128, 160, 192, 224, or 256 bits in length."); 106 | } 107 | 108 | KEY += std::string(32 - KEY.size(), 0); 109 | a = toint(KEY.substr(0, 4), 256); 110 | b = toint(KEY.substr(4, 4), 256); 111 | c = toint(KEY.substr(8, 4), 256); 112 | d = toint(KEY.substr(12, 4), 256); 113 | e = toint(KEY.substr(16, 4), 256); 114 | f = toint(KEY.substr(20, 4), 256); 115 | g = toint(KEY.substr(24, 4), 256); 116 | h = toint(KEY.substr(28, 4), 256); 117 | 118 | uint32_t Cm = 0x5A827999, Mm = 0x6ED9EBA1, Cr = 19, Mr = 17; 119 | 120 | std::vector range24_8(24, 0); 121 | std::vector range24_32(24, 0); 122 | 123 | Tm.push_back(range24_32); Tr.push_back(range24_8); 124 | for(uint8_t x = 0; x < 8; x++){ 125 | Tm.push_back(range24_32); 126 | Tr.push_back(range24_8); 127 | } 128 | for(uint8_t i = 0; i < 24; i++){ 129 | for(uint8_t j = 0; j < 8; j++){ 130 | Tm[j][i] = Cm; 131 | Cm = (Cm + Mm) & mod32; 132 | Tr[j][i] = Cr; 133 | Cr = (Cr + Mr) & 31; 134 | } 135 | } 136 | for(uint8_t i = 0; i < 12; i++){ 137 | W(i << 1); 138 | W((i << 1) + 1); 139 | Kr.push_back(kr()); 140 | Km.push_back(km()); 141 | } 142 | 143 | keyset = true; 144 | } 145 | 146 | std::string CAST256::encrypt(const std::string & DATA){ 147 | return run(DATA); 148 | } 149 | 150 | std::string CAST256::decrypt(const std::string & DATA){ 151 | std::reverse(Kr.begin(), Kr.end()); 152 | std::reverse(Km.begin(), Km.end()); 153 | std::string out = run(DATA); 154 | std::reverse(Kr.begin(), Kr.end()); 155 | std::reverse(Km.begin(), Km.end()); 156 | return out; 157 | } 158 | 159 | unsigned int CAST256::blocksize() const { 160 | return 128; 161 | } 162 | -------------------------------------------------------------------------------- /Encryptions/CAST256.h: -------------------------------------------------------------------------------- 1 | /* 2 | CAST256.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __CAST256__ 26 | #define __CAST256__ 27 | 28 | #include 29 | #include 30 | 31 | #include "../common/cryptomath.h" 32 | #include "../common/includes.h" 33 | #include "SymAlg.h" 34 | 35 | #include "CAST_Const.h" 36 | 37 | class CAST256 : public SymAlg{ 38 | private: 39 | uint32_t A, B, C, D, a, b, c, d, e, f, g, h; 40 | std::vector > Kr, Tr; 41 | std::vector > Km, Tm; 42 | uint32_t F1(const uint32_t Data, const uint32_t Kmi, const uint8_t Kri); 43 | uint32_t F2(const uint32_t Data, const uint32_t Kmi, const uint8_t Kri); 44 | uint32_t F3(const uint32_t Data, const uint32_t Kmi, const uint8_t Kri); 45 | void W(const uint8_t i); 46 | std::vector kr(); 47 | std::vector km(); 48 | void Q(const uint8_t & i); 49 | void QBAR(const uint8_t & i); 50 | std::string run(const std::string & data); 51 | 52 | public: 53 | CAST256(); 54 | CAST256(const std::string & KEY); 55 | void setkey(std::string KEY); 56 | std::string encrypt(const std::string & DATA); 57 | std::string decrypt(const std::string & DATA); 58 | unsigned int blocksize() const; 59 | }; 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /Encryptions/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11.4) 2 | 3 | set(FILES 4 | SymAlg.cpp SymAlg.h 5 | 6 | AES.cpp AES.h AES_Const.h 7 | Blowfish.cpp Blowfish.h Blowfish_Const.h 8 | Camellia.cpp Camellia.h Camellia_Const.h 9 | CAST_Const.h 10 | CAST128.cpp CAST128.h CAST128_Const.h 11 | CAST256.cpp CAST256.h 12 | DES.cpp DES.h DES_Const.h 13 | DESX.cpp DESX.h 14 | GOST.cpp GOST.h GOST_Const.h 15 | IDEA.cpp IDEA.h 16 | MISTY1.cpp MISTY1.h MISTY1_Const.h 17 | RC_PQ.cpp RC_PQ.h 18 | RC2.cpp RC2.h RC2_Const.h 19 | RC4.cpp RC4.h 20 | RC5.cpp RC5.h 21 | RC6.cpp RC6.h 22 | SAFERK64.cpp SAFERK64.h SAFERK64_Const.h 23 | SEED.cpp SEED.h SEED_Const.h 24 | Skipjack.cpp Skipjack.h Skipjack_Const.h 25 | TDES.cpp TDES.h 26 | TEA.cpp TEA.h 27 | Twofish.cpp Twofish.h Twofish_Const.h 28 | XTEA.cpp XTEA.h 29 | ) 30 | 31 | foreach(FILE ${FILES}) 32 | target_sources(Encryptions_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}) 33 | 34 | get_filename_component(EXT "${FILE}" EXT) 35 | if (EXT STREQUAL ".h") 36 | INSTALL(FILES ${FILE} 37 | DESTINATION include/Encryptions) 38 | endif() 39 | endforeach() 40 | 41 | if (BUILD_SHARED_LIB) 42 | foreach(FILE ${FILES}) 43 | target_sources(Encryptions_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}) 44 | endforeach() 45 | endif() 46 | -------------------------------------------------------------------------------- /Encryptions/Camellia.h: -------------------------------------------------------------------------------- 1 | /* 2 | Camellia.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __CAMELLIA__ 26 | #define __CAMELLIA__ 27 | 28 | #include 29 | #include 30 | 31 | #include "../common/cryptomath.h" 32 | #include "../common/includes.h" 33 | #include "SymAlg.h" 34 | 35 | #include "Camellia_Const.h" 36 | 37 | class Camellia : public SymAlg{ 38 | // Camellia is a CRYPTmetric key block cipher developed jointly in 2000 by 39 | // world top class encryption researchers at NTT and Mitsubishi Electric 40 | // Corporation. See:http://info.isl.ntt.co.jp/crypt/eng/camellia/index.html 41 | 42 | private: 43 | uint16_t keysize; 44 | std::vector keys; 45 | uint8_t SBOX(const uint8_t s, const uint8_t value); 46 | std::string FL(const std::string & FL_IN, const std::string & KE); 47 | std::string FLINV(const std::string & FLINV_IN, const std::string & KE); 48 | std::string F(const std::string & F_IN, const std::string & KE); 49 | std::string run(const std::string & data); 50 | 51 | public: 52 | Camellia(); 53 | Camellia(const std::string & KEY); 54 | void setkey(const std::string & KEY); 55 | std::string encrypt(const std::string & DATA); 56 | std::string decrypt(const std::string & DATA); 57 | unsigned int blocksize() const; 58 | }; 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /Encryptions/Camellia_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | Camellia_Const.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __CAMELLIA_CONST__ 26 | #define __CAMELLIA_CONST__ 27 | 28 | const std::string Camellia_Sigma[6] = {unhexlify("A09E667F3BCC908B"), 29 | unhexlify("B67AE8584CAA73B2"), 30 | unhexlify("C6EF372FE94F82BE"), 31 | unhexlify("54FF53A5F1D36F1C"), 32 | unhexlify("10E527FADE682D1D"), 33 | unhexlify("B05688C2B3E6C1FD")}; 34 | 35 | const uint8_t Camellia_SBox[256] = {0x70, 0x82, 0x2c, 0xec, 0xb3, 0x27, 0xc0, 0xe5, 0xe4, 0x85, 0x57, 0x35, 0xea, 0x0c, 0xae, 0x41, 36 | 0x23, 0xef, 0x6b, 0x93, 0x45, 0x19, 0xa5, 0x21, 0xed, 0x0e, 0x4f, 0x4e, 0x1d, 0x65, 0x92, 0xbd, 37 | 0x86, 0xb8, 0xaf, 0x8f, 0x7c, 0xeb, 0x1f, 0xce, 0x3e, 0x30, 0xdc, 0x5f, 0x5e, 0xc5, 0x0b, 0x1a, 38 | 0xa6, 0xe1, 0x39, 0xca, 0xd5, 0x47, 0x5d, 0x3d, 0xd9, 0x01, 0x5a, 0xd6, 0x51, 0x56, 0x6c, 0x4d, 39 | 0x8b, 0x0d, 0x9a, 0x66, 0xfb, 0xcc, 0xb0, 0x2d, 0x74, 0x12, 0x2b, 0x20, 0xf0, 0xb1, 0x84, 0x99, 40 | 0xdf, 0x4c, 0xcb, 0xc2, 0x34, 0x7e, 0x76, 0x05, 0x6d, 0xb7, 0xa9, 0x31, 0xd1, 0x17, 0x04, 0xd7, 41 | 0x14, 0x58, 0x3a, 0x61, 0xde, 0x1b, 0x11, 0x1c, 0x32, 0x0f, 0x9c, 0x16, 0x53, 0x18, 0xf2, 0x22, 42 | 0xfe, 0x44, 0xcf, 0xb2, 0xc3, 0xb5, 0x7a, 0x91, 0x24, 0x08, 0xe8, 0xa8, 0x60, 0xfc, 0x69, 0x50, 43 | 0xaa, 0xd0, 0xa0, 0x7d, 0xa1, 0x89, 0x62, 0x97, 0x54, 0x5b, 0x1e, 0x95, 0xe0, 0xff, 0x64, 0xd2, 44 | 0x10, 0xc4, 0x00, 0x48, 0xa3, 0xf7, 0x75, 0xdb, 0x8a, 0x03, 0xe6, 0xda, 0x09, 0x3f, 0xdd, 0x94, 45 | 0x87, 0x5c, 0x83, 0x02, 0xcd, 0x4a, 0x90, 0x33, 0x73, 0x67, 0xf6, 0xf3, 0x9d, 0x7f, 0xbf, 0xe2, 46 | 0x52, 0x9b, 0xd8, 0x26, 0xc8, 0x37, 0xc6, 0x3b, 0x81, 0x96, 0x6f, 0x4b, 0x13, 0xbe, 0x63, 0x2e, 47 | 0xe9, 0x79, 0xa7, 0x8c, 0x9f, 0x6e, 0xbc, 0x8e, 0x29, 0xf5, 0xf9, 0xb6, 0x2f, 0xfd, 0xb4, 0x59, 48 | 0x78, 0x98, 0x06, 0x6a, 0xe7, 0x46, 0x71, 0xba, 0xd4, 0x25, 0xab, 0x42, 0x88, 0xa2, 0x8d, 0xfa, 49 | 0x72, 0x07, 0xb9, 0x55, 0xf8, 0xee, 0xac, 0x0a, 0x36, 0x49, 0x2a, 0x68, 0x3c, 0x38, 0xf1, 0xa4, 50 | 0x40, 0x28, 0xd3, 0x7b, 0xbb, 0xc9, 0x43, 0xc1, 0x15, 0xe3, 0xad, 0xf4, 0x77, 0xc7, 0x80, 0x9e}; 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /Encryptions/DES.cpp: -------------------------------------------------------------------------------- 1 | #include "DES.h" 2 | 3 | std::string DES::run(const std::string & DATA){ 4 | if (!keyset){ 5 | throw std::runtime_error("Error: Key has not been set."); 6 | } 7 | 8 | if (DATA.size() != 8){ 9 | throw std::runtime_error("Error: Data must be 64 bits in length."); 10 | } 11 | 12 | std::string data = "", temp = ""; 13 | for(uint8_t x = 0; x < 8; x++){ 14 | data += makebin(static_cast (DATA[x]), 8); 15 | } 16 | // IP 17 | for(uint8_t x = 0; x < 64; x++){ 18 | temp += data[DES_IP[x] - 1]; 19 | } 20 | data = temp; 21 | for(uint8_t x = 0; x < 16; x++){ 22 | // split left and right and duplicate right 23 | std::string left = data.substr(0, 32); 24 | std::string right = data.substr(32, 32); 25 | std::string old_right = right; 26 | 27 | // expand right side 28 | uint64_t t = 0; 29 | temp = ""; 30 | for(uint8_t y = 0; y < 48; y++){ 31 | temp += right[DES_EX[y] - 1]; 32 | } 33 | t = toint(temp, 2); 34 | 35 | // expanded_right xor key 36 | right = makebin(t ^ keys[x], 48); 37 | 38 | // split right into 8 parts 39 | std::string RIGHT[8]; 40 | for(uint8_t y = 0; y < 8; y++){ 41 | RIGHT[y] = right.substr(6 * y, 6); 42 | } 43 | // use sboxes 44 | temp = ""; 45 | for(uint8_t y = 0; y < 8; y++){ 46 | std::string s = ""; 47 | s += RIGHT[y][0]; 48 | s += RIGHT[y][5]; 49 | temp += makebin(DES_S_BOX[y][toint(s, 2)][toint(RIGHT[y].substr(1, 4), 2)], 4); 50 | } 51 | 52 | // permutate 53 | right = ""; 54 | for(uint8_t y = 0; y < 32; y++){ 55 | right += temp[DES_P[y]-1]; 56 | } 57 | 58 | // right xor left and combine with old right 59 | data = old_right + makebin(toint(left, 2) ^ toint(right, 2), 32); 60 | } 61 | // reverse last switch 62 | data = data.substr(32, 32) + data.substr(0, 32); 63 | 64 | // IP^-1 65 | uint64_t out = 0; 66 | for(uint8_t x = 0; x < 64; x++){ 67 | out += static_cast (data[DES_INVIP[x] - 1] == '1') << (63 - x); 68 | } 69 | return unhexlify(makehex(out, 16)); 70 | } 71 | 72 | DES::DES() 73 | : SymAlg(), 74 | keys() 75 | {} 76 | 77 | DES::DES(const std::string & KEY) 78 | : DES() 79 | { 80 | setkey(KEY); 81 | } 82 | 83 | void DES::setkey(const std::string & KEY){ 84 | if (keyset){ 85 | throw std::runtime_error("Error: Key has already been set."); 86 | } 87 | if (KEY.size() != 8){ 88 | throw std::runtime_error("Error: Key must be 64 bits long."); 89 | } 90 | 91 | std::string key = ""; 92 | for(int x = 0; x < 8; x++){ 93 | key += makebin(static_cast (KEY[x]), 8); 94 | } 95 | 96 | std::string left = "", right = ""; 97 | for (uint8_t x = 0; x < 28; x++){ 98 | left += key[DES_PC1_l[x] - 1]; 99 | right += key[DES_PC1_r[x] - 1]; 100 | } 101 | 102 | for(uint8_t x = 0; x < 16; x++){ 103 | left = (left + left).substr(DES_rot[x], 28); 104 | right = (right + right).substr(DES_rot[x], 28); 105 | std::string k = ""; 106 | for(uint8_t y = 0; y < 48; y++) 107 | k += (left + right)[DES_PC2[y] - 1]; 108 | keys[x] = toint(k, 2); 109 | } 110 | 111 | keyset = true; 112 | } 113 | 114 | std::string DES::encrypt(const std::string & DATA){ 115 | return run(DATA); 116 | } 117 | 118 | std::string DES::decrypt(const std::string & DATA){ 119 | std::reverse(keys, keys + 16); 120 | std::string out = run(DATA); 121 | std::reverse(keys, keys + 16); 122 | return out; 123 | } 124 | 125 | unsigned int DES::blocksize() const { 126 | return 64; 127 | } 128 | -------------------------------------------------------------------------------- /Encryptions/DES.h: -------------------------------------------------------------------------------- 1 | /* 2 | DES.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __DES__ 26 | #define __DES__ 27 | 28 | #include 29 | 30 | #include "../common/includes.h" 31 | #include "SymAlg.h" 32 | 33 | #include "DES_Const.h" 34 | 35 | class DES : public SymAlg{ 36 | private: 37 | uint64_t keys[16]; 38 | std::string run(const std::string & data); 39 | 40 | public: 41 | DES(); 42 | DES(const std::string & KEY); 43 | void setkey(const std::string & KEY); 44 | std::string encrypt(const std::string & DATA); 45 | std::string decrypt(const std::string & DATA); 46 | unsigned int blocksize() const; 47 | }; 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /Encryptions/DESX.cpp: -------------------------------------------------------------------------------- 1 | #include "DESX.h" 2 | 3 | DESX::DESX() 4 | : SymAlg(), 5 | K1(), K2() 6 | {} 7 | 8 | DESX::DESX(const std::string & KEY, const std::string & KEY1, const std::string & KEY2) 9 | : DESX() 10 | { 11 | if (keyset){ 12 | throw std::runtime_error("Error: Key has already been set."); 13 | } 14 | 15 | des.setkey(KEY); 16 | K1 = KEY1; 17 | K2 = KEY2; 18 | 19 | keyset = true; 20 | } 21 | 22 | std::string DESX::encrypt(const std::string & DATA){ 23 | if (!keyset){ 24 | throw std::runtime_error("Error: Key has not been set."); 25 | } 26 | return xor_strings(des.encrypt(xor_strings(DATA, K1)), K2); 27 | } 28 | 29 | std::string DESX::decrypt(const std::string & DATA){ 30 | if (!keyset){ 31 | throw std::runtime_error("Error: Key has not been set."); 32 | } 33 | return xor_strings(des.decrypt(xor_strings(DATA, K2)), K1); 34 | } 35 | 36 | unsigned int DESX::blocksize() const { 37 | return 64; 38 | } 39 | -------------------------------------------------------------------------------- /Encryptions/DESX.h: -------------------------------------------------------------------------------- 1 | /* 2 | DESX.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __DESX__ 26 | #define __DESX__ 27 | 28 | #include "../common/includes.h" 29 | #include "SymAlg.h" 30 | 31 | #include "DES.h" 32 | 33 | class DESX : public SymAlg{ 34 | private: 35 | DES des; 36 | std::string K1, K2; 37 | 38 | public: 39 | DESX(); 40 | 41 | DESX(const std::string & KEY, const std::string & KEY1, const std::string & KEY2); 42 | std::string encrypt(const std::string & DATA); 43 | std::string decrypt(const std::string & DATA); 44 | unsigned int blocksize() const; 45 | }; 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /Encryptions/GOST.cpp: -------------------------------------------------------------------------------- 1 | #include "GOST.h" 2 | 3 | // GOST 28147-89 4 | uint32_t GOST::CM1(const uint32_t a, const uint32_t b){ 5 | return static_cast (a + b); 6 | } 7 | 8 | uint32_t GOST::CM2(const uint32_t a, const uint32_t b){ 9 | return static_cast (a ^ b); 10 | } 11 | 12 | //uint32_t CM3(const uint32_t a, const uint32_t b){ 13 | // return static_cast (a + b); 14 | //} 15 | 16 | //uint32_t CM4(const uint32_t a, const uint32_t b){ 17 | // return static_cast (a + b) % mod32; 18 | //} 19 | 20 | //uint32_t CM5(const uint32_t a, const uint32_t b){ 21 | // return static_cast (a ^ b); 22 | //} 23 | 24 | uint32_t GOST::sub(const uint32_t in){ 25 | uint32_t out = 0; 26 | for(uint8_t x = 0; x < 8; x++){ 27 | out = (out << 4) + k[x][(in >> (28 - (x << 2))) & 15]; 28 | } 29 | return out; 30 | } 31 | 32 | GOST::GOST() 33 | : SymAlg(), 34 | k(), 35 | X(), N(), C1(0), C2(0) 36 | {} 37 | 38 | GOST::GOST(const std::string & KEY, const uint8_t sbox[8][16]) 39 | : GOST() 40 | { 41 | setkey(KEY, sbox); 42 | } 43 | 44 | void GOST::setkey(const std::string & KEY, const uint8_t sbox[8][16]){ 45 | if (keyset){ 46 | throw std::runtime_error("Error: Key has already been set."); 47 | } 48 | 49 | if (KEY.size() != 32){ 50 | throw std::runtime_error("Error: Key must be 256 bits long."); 51 | } 52 | 53 | C1 = 0b00000001000000010000000100000100UL; 54 | C2 = 0b00000001000000010000000100000001UL; 55 | for(uint8_t x = 0; x < 8; x++){ 56 | X[x] = static_cast (toint(hexlify(KEY.substr(x << 2, 4)), 16)); 57 | for(int y = 0; y < 16; y++){ 58 | k[x][y] = sbox[x][y]; 59 | } 60 | } 61 | keyset = true; 62 | } 63 | 64 | std::string GOST::encrypt(const std::string & DATA){ 65 | if (!keyset){ 66 | throw std::runtime_error("Error: Key has not been set."); 67 | } 68 | 69 | if (DATA.size() != 8){ 70 | throw std::runtime_error("Error: Data must be 64 bits in length."); 71 | } 72 | 73 | for(uint8_t x = 0; x < 2; x++){ 74 | N[x] = static_cast (toint(DATA.substr(x << 2, 4), 256)); 75 | } 76 | 77 | for(uint8_t x = 0; x < 3; x++){ 78 | for(uint8_t y = 0; y < 4; y++){ 79 | N[1] = CM2(ROL(sub(CM1(N[0], X[(y << 1) & 7])), 11, 32), N[1]); 80 | N[0] = CM2(ROL(sub(CM1(N[1], X[((y << 1) + 1) & 7])), 11, 32), N[0]); 81 | } 82 | } 83 | 84 | for(int8_t x = 3; x > -1; x--){ 85 | N[1] = CM2(ROL(sub(CM1(N[0], X[((x << 1) + 1) & 7])), 11, 32), N[1]); 86 | N[0] = CM2(ROL(sub(CM1(N[1], X[(x << 1) & 7])), 11, 32), N[0]); 87 | } 88 | return unhexlify(makehex(N[1], 8) + makehex(N[0], 8)); 89 | } 90 | 91 | std::string GOST::decrypt(const std::string & DATA){ 92 | if (!keyset){ 93 | throw std::runtime_error("Error: Key has not been set."); 94 | } 95 | 96 | if (DATA.size() != 8){ 97 | throw std::runtime_error("Error: Data must be 64 bits in length."); 98 | } 99 | 100 | for(uint8_t x = 0; x < 2; x++){ 101 | N[x] = static_cast (toint(DATA.substr(x << 2, 4), 256)); 102 | } 103 | 104 | for(uint8_t x = 0; x < 4; x++){ 105 | N[1] = CM2(ROL(sub(CM1(N[0], X[(x << 1) & 7])), 11, 32), N[1]); 106 | N[0] = CM2(ROL(sub(CM1(N[1], X[((x << 1) + 1) & 7])), 11, 32), N[0]); 107 | } 108 | 109 | for(int8_t x = 2; x > -1; x--){ 110 | for(int8_t y = 3; y > -1; y--){ 111 | N[1] = CM2(ROL(sub(CM1(N[0], X[((y << 1) + 1) & 7])), 11, 32), N[1]); 112 | N[0] = CM2(ROL(sub(CM1(N[1], X[(y << 1) & 7])), 11, 32), N[0]); 113 | } 114 | } 115 | return unhexlify(makehex(N[1], 8) + makehex(N[0], 8)); 116 | } 117 | 118 | unsigned int GOST::blocksize() const { 119 | return 64; 120 | } 121 | -------------------------------------------------------------------------------- /Encryptions/GOST.h: -------------------------------------------------------------------------------- 1 | /* 2 | GOST.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __GOST__ 26 | #define __GOST__ 27 | 28 | #include "../common/cryptomath.h" 29 | #include "../common/includes.h" 30 | #include "SymAlg.h" 31 | 32 | #include "GOST_Const.h" 33 | 34 | // GOST 28147-89 35 | class GOST : public SymAlg{ 36 | private: 37 | uint8_t k[8][16]; 38 | uint32_t X[8], N[6], C1, C2; 39 | uint32_t CM1(const uint32_t a, const uint32_t b); 40 | uint32_t CM2(const uint32_t a, const uint32_t b); 41 | // uint32_t CM3(const uint32_t a, const uint32_t b){return static_cast (a + b);} 42 | // uint32_t CM4(const uint32_t a, const uint32_t b){return static_cast (a + b) % mod32;} 43 | // uint32_t CM5(const uint32_t a, const uint32_t b){return static_cast (a ^ b);} 44 | uint32_t sub(uint32_t in); 45 | 46 | public: 47 | GOST(); 48 | GOST(const std::string & KEY, const uint8_t sbox[8][16]); 49 | void setkey(const std::string & KEY, const uint8_t sbox[8][16]); 50 | std::string encrypt(const std::string & DATA); 51 | std::string decrypt(const std::string & DATA); 52 | unsigned int blocksize() const; 53 | }; 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /Encryptions/GOST_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | GOST_Const.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __GOST_SBOX__ 26 | #define __GOST_SBOX__ 27 | 28 | // GOST Cipher S Boxes 29 | const uint8_t CryptoPro_sbox[8][16] = { {10, 4, 5, 6, 8, 1, 3, 7, 13, 12, 14, 0, 9, 2, 11, 15}, 30 | {5, 15, 4, 0, 2, 13, 11, 9, 1, 7, 6, 3, 12, 14, 10, 8}, 31 | {7, 15, 12, 14, 9, 4, 1, 0, 3, 11, 5, 2, 6, 10, 8, 13}, 32 | {4, 10, 7, 12, 0, 15, 2, 8, 14, 1, 6, 5, 13, 11, 9, 3}, 33 | {7, 6, 4, 11, 9, 12, 2, 10, 1, 8, 0, 14, 15, 13, 3, 5}, 34 | {7, 6, 2, 4, 13, 9, 15, 0, 10, 1, 5, 11, 8, 14, 12, 3}, 35 | {13, 14, 4, 1, 7, 0, 5, 10, 3, 12, 8, 15, 6, 2, 9, 11}, 36 | {1, 3, 10, 9, 5, 11, 4, 15, 8, 6, 7, 14, 13, 0, 2, 12}}; 37 | 38 | const uint8_t DES_sbox[8][16] = { {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7}, 39 | {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10}, 40 | {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8}, 41 | {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15}, 42 | {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9}, 43 | {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11}, 44 | {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1}, 45 | {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7}}; 46 | 47 | const uint8_t RFC4357_sbox[8][16] = { {9, 6, 3, 2, 8, 11, 1, 7, 10, 4, 14, 15, 12, 0, 13, 5}, 48 | {3, 7, 14, 9, 8, 10, 15, 0, 5, 2, 6, 12, 11, 4, 13, 1}, 49 | {14, 4, 6, 2, 11, 3, 13, 8, 12, 15, 5, 10, 0, 7, 1, 9}, 50 | {14, 7, 10, 12, 13, 1, 3, 9, 0, 2, 11, 4, 15, 8, 5, 6}, 51 | {11, 5, 1, 9, 8, 13, 15, 0, 14, 4, 2, 3, 12, 7, 10, 6}, 52 | {3, 10, 13, 12, 1, 2, 0, 11, 7, 5, 9, 4, 8, 15, 14, 6}, 53 | {1, 13, 2, 9, 7, 10, 6, 0, 8, 14, 4, 5, 15, 3, 11, 14}, 54 | {11, 10, 15, 5, 0, 12, 14, 8, 6, 2, 3, 9, 1 ,7, 13, 4}}; 55 | 56 | const uint8_t RFC5831_sbox[8][16] = { {4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3}, 57 | {14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9}, 58 | {5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11}, 59 | {7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3}, 60 | {6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2}, 61 | {4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14}, 62 | {13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12}, 63 | {1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12}}; 64 | 65 | #endif 66 | -------------------------------------------------------------------------------- /Encryptions/IDEA.h: -------------------------------------------------------------------------------- 1 | /* 2 | IDEA.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __IDEA__ 26 | #define __IDEA__ 27 | 28 | #include 29 | 30 | #include "../common/cryptomath.h" 31 | #include "../common/includes.h" 32 | #include "SymAlg.h" 33 | 34 | class IDEA : public SymAlg{ 35 | private: 36 | std::vector > keys; 37 | std::vector k; 38 | uint16_t mult(uint32_t value1, uint32_t value2); 39 | std::string run(const std::string & data); 40 | 41 | public: 42 | IDEA(); 43 | IDEA(const std::string & KEY); 44 | void setkey(const std::string & KEY); 45 | std::string encrypt(const std::string & DATA); 46 | std::string decrypt(const std::string & DATA); 47 | unsigned int blocksize() const; 48 | }; 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /Encryptions/MISTY1.cpp: -------------------------------------------------------------------------------- 1 | #include "MISTY1.h" 2 | 3 | uint16_t MISTY1::FI(const uint16_t FI_IN, const uint16_t FI_KEY){ 4 | uint16_t d9 = FI_IN >> 7; 5 | uint16_t d7 = FI_IN & 0x7f; 6 | d9 = MISTY1_S9[d9] ^ d7; 7 | d7 = MISTY1_S7[d7] ^ d9; 8 | d7 &= 0x7f; 9 | d7 ^= (FI_KEY >> 9); 10 | d9 ^= (FI_KEY & 0x1ff); 11 | d9 = MISTY1_S9[d9] ^ d7; 12 | return (static_cast (d7 & 0xffffU) << 9) | d9; 13 | } 14 | 15 | uint32_t MISTY1::FO(const uint32_t FO_IN, const uint16_t k){ 16 | uint16_t t0 = FO_IN >> 16; 17 | uint16_t t1 = FO_IN & 0xffff; 18 | t0 ^= EK[k]; 19 | t0 = FI(t0, EK[(k+5)%8+8]); 20 | t0 ^= t1; 21 | t1 ^= EK[(k+2)%8]; 22 | t1 = FI(t1, EK[(k+1)%8+8]); 23 | t1 ^= t0; 24 | t0 ^= EK[(k+7)%8]; 25 | t0 = FI(t0, EK[(k+3)%8+8]); 26 | t0 ^= t1; 27 | t1 ^= EK[(k+4)%8]; 28 | return (t1 << 16) | t0; 29 | } 30 | 31 | uint32_t MISTY1::FL(const uint32_t FL_IN, const uint32_t k){ 32 | uint16_t d0 = FL_IN >> 16; 33 | uint16_t d1 = FL_IN & 0xffff; 34 | if (!(k & 1)){ 35 | d1 ^= (d0 & EK[k/2]); 36 | d0 ^= (d1 | EK[(k/2+6)%8+8]); 37 | } 38 | else{ 39 | d1 ^= (d0 & EK[((k-1)/2+2)%8+8]); 40 | d0 ^= (d1 | EK[((k-1)/2+4)%8]); 41 | } 42 | return (d0 << 16) | d1; 43 | } 44 | 45 | uint32_t MISTY1::FLINV(const uint32_t FL_IN, const uint32_t k){ 46 | uint16_t d0 = FL_IN >> 16; 47 | uint16_t d1 = FL_IN & 0xffff; 48 | if (!(k & 1)){ 49 | d0 ^= (d1 | EK[(k/2+6)%8+8]); 50 | d1 ^= (d0 & EK[k/2]); 51 | } 52 | else{ 53 | d0 ^= (d1 | EK[((k-1)/2+4)%8]); 54 | d1 ^= (d0 & EK[((k-1)/2+2)%8+8]); 55 | } 56 | return (d0 << 16) | d1; 57 | } 58 | 59 | MISTY1::MISTY1() 60 | : SymAlg(), 61 | EK() 62 | {} 63 | 64 | MISTY1::MISTY1(const std::string & KEY) 65 | : MISTY1() 66 | { 67 | setkey(KEY); 68 | } 69 | 70 | void MISTY1::setkey(const std::string & KEY){ 71 | if (keyset){ 72 | throw std::runtime_error("Error: Key has already been set."); 73 | } 74 | 75 | if (KEY.size() != 16){ 76 | throw std::runtime_error("Error: Key must be 128 bits in length."); 77 | } 78 | 79 | for(uint8_t i = 0; i < 8; i++){ 80 | EK[i] = ((KEY[i*2]*256) & 0xffff) + (KEY[i*2+1] & 0xff); 81 | } 82 | for(uint8_t i = 0; i < 8; i++){ 83 | EK[i+ 8] = FI(EK[i], EK[(i+1)%8]); 84 | EK[i+16] = EK[i+8] & 0x1ff; 85 | EK[i+24] = EK[i+8] >> 9; 86 | } 87 | keyset = true; 88 | } 89 | 90 | std::string MISTY1::encrypt(const std::string & DATA){ 91 | if (!keyset){ 92 | throw std::runtime_error("Error: Key has not been set."); 93 | } 94 | 95 | if (DATA.size() != 8){ 96 | throw std::runtime_error("Error: Data must be 64 bits in length."); 97 | } 98 | 99 | uint32_t D0 = toint(DATA.substr(0, 4), 256); 100 | uint32_t D1 = toint(DATA.substr(4, 4), 256); 101 | // 0 round 102 | D0 = FL(D0, 0); 103 | D1 = FL(D1, 1); 104 | D1 ^= FO(D0, 0); 105 | // 1 round 106 | D0 ^= FO(D1, 1); 107 | // 2 round 108 | D0 = FL(D0, 2); 109 | D1 = FL(D1, 3); 110 | D1 ^= FO(D0, 2); 111 | // 3 round 112 | D0 ^= FO(D1, 3); 113 | // 4 round 114 | D0 = FL(D0, 4); 115 | D1 = FL(D1, 5); 116 | D1 ^= FO(D0, 4); 117 | // 5 round 118 | D0 ^= FO(D1, 5); 119 | // 6 round 120 | D0 = FL(D0, 6); 121 | D1 = FL(D1, 7); 122 | D1 ^= FO(D0, 6); 123 | // 7 round 124 | D0 ^= FO(D1, 7); 125 | // final 126 | D0 = FL(D0, 8); 127 | D1 = FL(D1, 9); 128 | return unhexlify(makehex(D1, 8) + makehex(D0, 8)); 129 | } 130 | 131 | std::string MISTY1::decrypt(const std::string & DATA){ 132 | if (!keyset){ 133 | throw std::runtime_error("Error: Key has not been set."); 134 | } 135 | 136 | if (DATA.size() != 8){ 137 | throw std::runtime_error("Error: Data must be 64 bits in length."); 138 | } 139 | 140 | uint32_t D0 = toint(DATA.substr(4, 4), 256); 141 | uint32_t D1 = toint(DATA.substr(0, 4), 256); 142 | D0 = FLINV(D0, 8); 143 | D1 = FLINV(D1, 9); 144 | D0 ^= FO(D1, 7); 145 | D1 ^= FO(D0, 6); 146 | D0 = FLINV(D0, 6); 147 | D1 = FLINV(D1, 7); 148 | D0 ^= FO(D1, 5); 149 | D1 ^= FO(D0, 4); 150 | D0 = FLINV(D0, 4); 151 | D1 = FLINV(D1, 5); 152 | D0 ^= FO(D1, 3); 153 | D1 ^= FO(D0, 2); 154 | D0 = FLINV(D0, 2); 155 | D1 = FLINV(D1, 3); 156 | D0 ^= FO(D1, 1); 157 | D1 ^= FO(D0, 0); 158 | D0 = FLINV(D0, 0); 159 | D1 = FLINV(D1, 1); 160 | return unhexlify(makehex(D0, 8) + makehex(D1)); 161 | } 162 | 163 | unsigned int MISTY1::blocksize() const { 164 | return 64; 165 | }; 166 | -------------------------------------------------------------------------------- /Encryptions/MISTY1.h: -------------------------------------------------------------------------------- 1 | /* 2 | MISTY1.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __MISTY1__ 26 | #define __MISTY1__ 27 | 28 | #include "../common/includes.h" 29 | #include "SymAlg.h" 30 | 31 | #include "MISTY1_Const.h" 32 | 33 | class MISTY1 : public SymAlg{ 34 | private: 35 | uint16_t EK[32]; 36 | uint16_t FI(const uint16_t FI_IN, const uint16_t FI_KEY); 37 | uint32_t FO(const uint32_t FO_IN, const uint16_t k); 38 | uint32_t FL(const uint32_t FL_IN, const uint32_t k); 39 | uint32_t FLINV(const uint32_t FL_IN, const uint32_t k); 40 | 41 | public: 42 | MISTY1(); 43 | MISTY1(const std::string & KEY); 44 | void setkey(const std::string & KEY); 45 | std::string encrypt(const std::string & DATA); 46 | std::string decrypt(const std::string & DATA); 47 | unsigned int blocksize() const; 48 | }; 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /Encryptions/RC2.cpp: -------------------------------------------------------------------------------- 1 | #include "RC2.h" 2 | 3 | void RC2::mix(const uint8_t j, const uint8_t i, const uint8_t s){ 4 | R[i] += K[(j << 2) + i] + (R[(i + 3) & 3] & R[(i + 2) & 3]) + ((~R[(i + 3) & 3]) & R[(i + 1) & 3]); 5 | R[i] = ROL(R[i], s, 16); 6 | } 7 | 8 | void RC2::mash(const uint8_t i){ 9 | R[i] += K[R[(i + 3) & 3] & 63]; 10 | } 11 | 12 | void RC2::r_mix(const uint8_t j, const uint8_t i, const uint8_t s){ 13 | R[i] = ROR(R[i], s, 16); 14 | R[i] = (R[i] - K[(j < 2) + i] - (R[(i+3) & 3] & R[(i + 2) & 3]) - ((~R[(i + 3) & 3]) & R[(i + 1) & 3]) + 65536) & 65535; 15 | } 16 | 17 | void RC2::r_mash(const uint8_t i){ 18 | R[i] = (R[i] - K[R[(i + 3) & 3] & 63] + mod16 + 1) & mod16; 19 | } 20 | 21 | RC2::RC2() 22 | : SymAlg(), 23 | K(), R(), 24 | T1(64) 25 | {} 26 | 27 | RC2::RC2(const std::string & KEY, const uint32_t & t1) 28 | : RC2() 29 | { 30 | T1 = t1; 31 | setkey(KEY); 32 | } 33 | 34 | void RC2::setkey(const std::string & KEY, const uint32_t & t1){ 35 | if (keyset){ 36 | throw std::runtime_error("Error: Key has already been set."); 37 | } 38 | 39 | T1 = t1; 40 | 41 | unsigned int T = KEY.size(); 42 | std::vector L; 43 | for(unsigned int x = 0; x < T; x++){ 44 | L.push_back(static_cast (KEY[x])); 45 | } 46 | 47 | while (L.size() < 128){ 48 | L.push_back(0); 49 | } 50 | 51 | unsigned int T8 = (T1 + 7) >> 3; 52 | unsigned int TM = 255 % (1UL << (8 + T1 - (T8 << 3))); 53 | for(int i = T; i < 128; i++){ 54 | int j = i - 1, k = i - T; 55 | if (j < 0){ 56 | j += 128; 57 | } 58 | if (k < 0){ 59 | k += 128; 60 | } 61 | L[i] = RC2_PITABLE[(L[j] + L[k]) & 255]; 62 | } 63 | L[128 - T8] = RC2_PITABLE[L[128 - T8] & TM]; 64 | 65 | for(int i = 127 - T8; i > -1; i--){ 66 | int j = i - 1, k = i - T; 67 | if (j < 0){ 68 | j += 128; 69 | } 70 | if (k < 0){ 71 | k += 128; 72 | } 73 | int l = L[i + 1] ^ L[i + T8]; 74 | if (l < 0){ 75 | l += 128; 76 | } 77 | L[i] = RC2_PITABLE[l]; 78 | } 79 | 80 | for(uint8_t i = 0; i < 64; i++){ 81 | K[i] = L[i << 1] + (L[(i << 1) + 1] << 8); 82 | } 83 | keyset = true; 84 | } 85 | 86 | std::string RC2::encrypt(const std::string & DATA){ 87 | if (!keyset){ 88 | throw std::runtime_error("Error: Key has not been set."); 89 | } 90 | 91 | if (DATA.size() != 8){ 92 | throw std::runtime_error("Error: Data must be 64 bits in length."); 93 | } 94 | 95 | for(uint8_t x = 0; x < 4; x++){ 96 | R[x] = static_cast (toint(DATA.substr(x << 1, 2), 256)); 97 | } 98 | 99 | for(uint8_t j = 0; j < 16; j++){ 100 | mix(j, 0, 1); 101 | mix(j, 1, 2); 102 | mix(j, 2, 3); 103 | mix(j, 3, 5); 104 | if ((j == 4) || (j == 10)){ 105 | mash(0); 106 | mash(1); 107 | mash(2); 108 | mash(3); 109 | } 110 | } 111 | 112 | std::string out = ""; 113 | for(uint8_t x = 0; x < 4; x++){ 114 | out += (unsigned char) R[x]; 115 | } 116 | return out; 117 | } 118 | 119 | std::string RC2::decrypt(const std::string & DATA){ 120 | if (!keyset){ 121 | throw std::runtime_error("Error: Key has not been set."); 122 | } 123 | 124 | if (DATA.size() != 8){ 125 | throw std::runtime_error("Error: Data must be 64 bits in length."); 126 | } 127 | 128 | for(uint8_t x = 0; x < 4; x++){ 129 | R[x] = static_cast (toint(DATA.substr(x << 1, 2), 256)); 130 | } 131 | 132 | for(int8_t j = 15; j > -1; j--){ 133 | if ((j == 4) || (j == 10)){ 134 | r_mash(3); 135 | r_mash(2); 136 | r_mash(1); 137 | r_mash(0); 138 | } 139 | r_mix(j, 3, 5); 140 | r_mix(j, 2, 3); 141 | r_mix(j, 1, 2); 142 | r_mix(j, 0, 1); 143 | } 144 | 145 | std::string out= ""; 146 | for(uint8_t x = 0; x < 4; x++){ 147 | out += (unsigned char) R[x]; 148 | } 149 | return out; 150 | } 151 | 152 | unsigned int RC2::blocksize() const { 153 | return 64; 154 | } 155 | 156 | -------------------------------------------------------------------------------- /Encryptions/RC2.h: -------------------------------------------------------------------------------- 1 | /* 2 | RC2.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __RC2__ 26 | #define __RC2__ 27 | 28 | #include 29 | 30 | #include "../common/cryptomath.h" 31 | #include "../common/includes.h" 32 | #include "SymAlg.h" 33 | 34 | #include "RC2_Const.h" 35 | 36 | class RC2 : public SymAlg{ 37 | private: 38 | uint16_t K[64], R[4]; 39 | uint32_t T1; 40 | void mix(const uint8_t j, const uint8_t i, const uint8_t s); 41 | void mash(const uint8_t i); 42 | void r_mix(const uint8_t j, const uint8_t i, const uint8_t s); 43 | void r_mash(const uint8_t i); 44 | 45 | public: 46 | RC2(); 47 | RC2(const std::string & KEY, const uint32_t & t1 = 64); 48 | void setkey(const std::string & KEY, const uint32_t & t1 = 64); 49 | std::string encrypt(const std::string & DATA); 50 | std::string decrypt(const std::string & DATA); 51 | unsigned int blocksize() const; 52 | }; 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /Encryptions/RC2_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | RC2_Const.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __RC2_CONST__ 26 | #define __RC2_CONST__ 27 | 28 | const uint8_t RC2_PITABLE[256] = { 0xd9, 0x78, 0xf9, 0xc4, 0x19, 0xdd, 0xb5, 0xed, 0x28, 0xe9, 0xfd, 0x79, 0x4a, 0xa0, 0xd8, 0x9d, 29 | 0xc6, 0x7e, 0x37, 0x83, 0x2b, 0x76, 0x53, 0x8e, 0x62, 0x4c, 0x64, 0x88, 0x44, 0x8b, 0xfb, 0xa2, 30 | 0x17, 0x9a, 0x59, 0xf5, 0x87, 0xb3, 0x4f, 0x13, 0x61, 0x45, 0x6d, 0x8d, 0x09, 0x81, 0x7d, 0x32, 31 | 0xbd, 0x8f, 0x40, 0xeb, 0x86, 0xb7, 0x7b, 0x0b, 0xf0, 0x95, 0x21, 0x22, 0x5c, 0x6b, 0x4e, 0x82, 32 | 0x54, 0xd6, 0x65, 0x93, 0xce, 0x60, 0xb2, 0x1c, 0x73, 0x56, 0xc0, 0x14, 0xa7, 0x8c, 0xf1, 0xdc, 33 | 0x12, 0x75, 0xca, 0x1f, 0x3b, 0xbe, 0xe4, 0xd1, 0x42, 0x3d, 0xd4, 0x30, 0xa3, 0x3c, 0xb6, 0x26, 34 | 0x6f, 0xbf, 0x0e, 0xda, 0x46, 0x69, 0x07, 0x57, 0x27, 0xf2, 0x1d, 0x9b, 0xbc, 0x94, 0x43, 0x03, 35 | 0xf8, 0x11, 0xc7, 0xf6, 0x90, 0xef, 0x3e, 0xe7, 0x06, 0xc3, 0xd5, 0x2f, 0xc8, 0x66, 0x1e, 0xd7, 36 | 0x08, 0xe8, 0xea, 0xde, 0x80, 0x52, 0xee, 0xf7, 0x84, 0xaa, 0x72, 0xac, 0x35, 0x4d, 0x6a, 0x2a, 37 | 0x96, 0x1a, 0xd2, 0x71, 0x5a, 0x15, 0x49, 0x74, 0x4b, 0x9f, 0xd0, 0x5e, 0x04, 0x18, 0xa4, 0xec, 38 | 0xc2, 0xe0, 0x41, 0x6e, 0x0f, 0x51, 0xcb, 0xcc, 0x24, 0x91, 0xaf, 0x50, 0xa1, 0xf4, 0x70, 0x39, 39 | 0x99, 0x7c, 0x3a, 0x85, 0x23, 0xb8, 0xb4, 0x7a, 0xfc, 0x02, 0x36, 0x5b, 0x25, 0x55, 0x97, 0x31, 40 | 0x2d, 0x5d, 0xfa, 0x98, 0xe3, 0x8a, 0x92, 0xae, 0x05, 0xdf, 0x29, 0x10, 0x67, 0x6c, 0xba, 0xc9, 41 | 0xd3, 0x00, 0xe6, 0xcf, 0xe1, 0x9e, 0xa8, 0x2c, 0x63, 0x16, 0x01, 0x3f, 0x58, 0xe2, 0x89, 0xa9, 42 | 0x0d, 0x38, 0x34, 0x1b, 0xab, 0x33, 0xff, 0xb0, 0xbb, 0x48, 0x0c, 0x5f, 0xb9, 0xb1, 0xcd, 0x2e, 43 | 0xc5, 0xf3, 0xdb, 0x47, 0xe5, 0xa5, 0x9c, 0x77, 0x0a, 0xa6, 0x20, 0x68, 0xfe, 0x7f, 0xc1, 0xad}; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /Encryptions/RC4.cpp: -------------------------------------------------------------------------------- 1 | #include "RC4.h" 2 | 3 | void RC4::ksa(const std::string & k){ 4 | // Original SBox 5 | for(uint16_t i = 0; i < 256; i++){ 6 | s_e[i] = i; 7 | } 8 | 9 | uint8_t j = 0; 10 | for(uint16_t i = 0; i < 256; i++){ 11 | j = (j + s_e[i] + k[i % k.size()]); 12 | std::swap(s_e[i], s_e[j]); 13 | } 14 | 15 | for(uint16_t i = 0; i < 256; i++){ 16 | s_d[i] = s_e[i]; 17 | } 18 | } 19 | 20 | uint8_t RC4::prga(const char mode){ 21 | if (mode == 'e'){ 22 | i_e++; 23 | j_e += s_e[i_e]; 24 | std::swap(s_e[j_e], s_e[i_e]); 25 | return s_e[(s_e[i_e] + s_e[j_e]) & 255]; 26 | } 27 | else if (mode == 'd'){ 28 | i_d++; 29 | j_d += s_d[i_d]; 30 | std::swap(s_d[j_d], s_d[i_d]); 31 | return s_d[(s_d[i_d] + s_d[j_d]) & 255]; 32 | } 33 | else{ 34 | throw std::runtime_error("Error: Unknown mode used in prga."); 35 | } 36 | return 0; // get rid of compiler warnings 37 | } 38 | 39 | RC4::RC4() 40 | : SymAlg(), 41 | s_e(), i_e(0), j_e(0), s_d(), i_d(0), j_d(0) 42 | {} 43 | 44 | RC4::RC4(const std::string & KEY) 45 | : RC4() 46 | { 47 | setkey(KEY); 48 | } 49 | 50 | void RC4::setkey(const std::string & KEY){ 51 | if (keyset){ 52 | throw std::runtime_error("Error: Key has already been set."); 53 | } 54 | 55 | ksa(KEY); 56 | keyset = true; 57 | } 58 | 59 | std::string RC4::encrypt(const std::string & DATA){ 60 | if (!keyset){ 61 | throw std::runtime_error("Error: Key has not been set."); 62 | } 63 | 64 | std::string out = ""; 65 | for(uint8_t x = 0; x < DATA.size(); x++){ 66 | out += static_cast (static_cast (DATA[x]) ^ prga('e')); 67 | } 68 | return out; 69 | } 70 | 71 | std::string RC4::decrypt(const std::string & DATA){ 72 | if (!keyset){ 73 | throw std::runtime_error("Error: Key has not been set."); 74 | } 75 | 76 | std::string out = ""; 77 | for(uint8_t x = 0; x < DATA.size(); x++){ 78 | out += static_cast (static_cast (DATA[x]) ^ prga('d')); 79 | } 80 | return out; 81 | } 82 | 83 | unsigned int RC4::blocksize() const { 84 | return 64; 85 | } 86 | -------------------------------------------------------------------------------- /Encryptions/RC4.h: -------------------------------------------------------------------------------- 1 | /* 2 | RC4.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __RC4__ 26 | #define __RC4__ 27 | 28 | #include 29 | 30 | #include "SymAlg.h" 31 | 32 | class RC4 : public SymAlg{ 33 | private: 34 | uint8_t s_e[256], i_e, j_e, // encryption SBox and "pointers" 35 | s_d[256], i_d, j_d; // decryption SBox and "pointers" 36 | 37 | void ksa(const std::string & k); // key scheduling algorithm 38 | uint8_t prga(const char mode); // pseudo random generation algorithm 39 | 40 | public: 41 | RC4(); 42 | RC4(const std::string & KEY); 43 | void setkey(const std::string & KEY); 44 | std::string encrypt(const std::string & DATA); 45 | std::string decrypt(const std::string & DATA); 46 | unsigned int blocksize() const; 47 | }; 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /Encryptions/RC5.cpp: -------------------------------------------------------------------------------- 1 | #include "RC5.h" 2 | 3 | RC5::RC5() 4 | : SymAlg(), 5 | w(0), r(0), b(0), 6 | mod(0) 7 | {} 8 | 9 | RC5::RC5(const std::string & KEY, const uint64_t & W, const uint64_t & R, const uint64_t & B) 10 | : RC5() 11 | { 12 | setkey(KEY, W, R, B); 13 | } 14 | 15 | void RC5::setkey(std::string KEY, const uint64_t & W, const uint64_t & R, const uint64_t & B){ 16 | if (keyset){ 17 | throw std::runtime_error("Error: Key has already been set."); 18 | } 19 | 20 | w = W; 21 | r = R; 22 | b = B; 23 | mod = (1ULL << w) - 1; 24 | 25 | uint64_t p, q; 26 | rc_pq(w, p, q); 27 | 28 | uint64_t t = (r + 1) << 1; 29 | uint64_t u = (uint64_t) ceil(w / 8.); 30 | uint64_t c = (uint64_t) ceil((float) b / u); 31 | while (KEY.size() % u != 0){ 32 | KEY += zero; 33 | } 34 | 35 | std::vector L; 36 | for(unsigned int x = 0; x < c; x++){ 37 | L.push_back(toint(little_end(KEY.substr(u * x, u), 256), 256)); 38 | } 39 | 40 | S.push_back(p); 41 | for(unsigned int i = 1; i < t; i++){ 42 | S.push_back((S[i - 1] + q) & mod); 43 | } 44 | 45 | uint64_t a = 0, b = 0, i = 0, j = 0, v = 3 * std::max(c, t); 46 | for(unsigned int x = 0; x < v; x++){ 47 | a = S[i] = ROL(static_cast (S[i] + a + b) & mod, 3, w); 48 | b = L[j] = ROL(static_cast (L[j] + a + b) & mod, (a + b) % w, w); 49 | i = (i + 1) % t; 50 | j = (j + 1) % c; 51 | } 52 | 53 | keyset = true; 54 | } 55 | 56 | std::string RC5::encrypt(const std::string & DATA){ 57 | if (!keyset){ 58 | throw std::runtime_error("Error: Key has not been set."); 59 | } 60 | uint64_t A = toint(little_end(DATA.substr(0, w >> 3), 256), 256), B = toint(little_end(DATA.substr(w >> 3, (w >> 3)), 256), 256); 61 | A = (A + S[0]) & mod; 62 | B = (B + S[1]) & mod; 63 | for(uint8_t i = 1; i < r + 1; i++){ 64 | A = (ROL(A ^ B, B % w, 32) + S[i << 1]) & mod; 65 | B = (ROL(A ^ B, A % w, 32) + S[(i << 1) + 1]) & mod; 66 | } 67 | return unhexlify(little_end(makehex(A & mod, w >> 2), 16) + little_end(makehex(B & mod, w >> 2), 16)); 68 | } 69 | 70 | std::string RC5::decrypt(const std::string & DATA){ 71 | if (!keyset){ 72 | throw std::runtime_error("Error: Key has not been set."); 73 | } 74 | uint64_t A = toint(little_end(DATA.substr(0, w >> 3), 256), 256), B = toint(little_end(DATA.substr(w >> 3, (w >> 3)), 256), 256); 75 | for(uint8_t i = r; i > 0; i--){ 76 | B = ROR((B - S[(i << 1) + 1]) & mod, A % w, 32) ^ A; 77 | A = ROR((A - S[i << 1]) & mod, B % w, 32) ^ B; 78 | } 79 | B = (B - S[1]) & mod; 80 | A = (A - S[0]) & mod; 81 | return unhexlify(little_end(makehex(A & mod, w >> 2), 16) + little_end(makehex(B & mod, w >> 2), 16)); 82 | } 83 | 84 | unsigned int RC5::blocksize() const { 85 | return w << 1; 86 | } 87 | -------------------------------------------------------------------------------- /Encryptions/RC5.h: -------------------------------------------------------------------------------- 1 | /* 2 | RC5.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __RC5__ 26 | #define __RC5__ 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #include "../common/cryptomath.h" 33 | #include "../common/includes.h" 34 | #include "SymAlg.h" 35 | 36 | #include "RC_PQ.h" 37 | 38 | class RC5 : public SymAlg{ 39 | private: 40 | uint64_t w, r, b; 41 | uint64_t mod; 42 | std::vector S; 43 | 44 | public: 45 | RC5(); 46 | RC5(const std::string & KEY, const uint64_t & W = 32, const uint64_t & R = 12, const uint64_t & B = 16); 47 | void setkey(std::string KEY, const uint64_t & W = 32, const uint64_t & R = 12, const uint64_t & B = 16); 48 | std::string encrypt(const std::string & DATA); 49 | std::string decrypt(const std::string & DATA); 50 | unsigned int blocksize() const; 51 | }; 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /Encryptions/RC6.cpp: -------------------------------------------------------------------------------- 1 | #include "RC6.h" 2 | 3 | RC6::RC6() 4 | : SymAlg(), 5 | w(0), r(0), b(0), lgw(0), 6 | S(0) 7 | {} 8 | 9 | RC6::RC6(const std::string & KEY, const unsigned int & W, const unsigned int & R) 10 | : RC6() 11 | { 12 | setkey(KEY, W, R); 13 | } 14 | 15 | void RC6::setkey(std::string KEY, const unsigned int & W, const unsigned int & R){ 16 | if (keyset){ 17 | throw std::runtime_error("Error: Key has already been set."); 18 | } 19 | 20 | w = W; 21 | r = R; 22 | b = KEY.size(); 23 | lgw = log2(w); 24 | 25 | uint64_t p, q; 26 | rc_pq(w, p, q); 27 | 28 | unsigned int u = (w >> 3) + (bool) (w & 7); 29 | unsigned int c = ceil(float(b) / u); 30 | while (KEY.size() % u){ 31 | KEY += zero; 32 | } 33 | std::vector L; 34 | for(unsigned int x = 0; x < c; x++){ 35 | L.push_back(toint(little_end(KEY.substr(u * x, u), 256), 256)); 36 | } 37 | S.push_back(p); 38 | for(unsigned int i = 0; i < 2 * r + 3; i++){ 39 | S.push_back((S[i] + q)); 40 | } 41 | uint32_t A = 0, B = 0, i = 0, j = 0; 42 | uint32_t v = 3 * std::max(c, (r << 1) + 4); 43 | for(unsigned int s = 1; s < v + 1; s++){ 44 | A = S[i] = ROL((S[i] + A + B), 3, w); 45 | B = L[j] = ROL((L[j] + A + B), (A + B) % w, w); 46 | i = (i + 1) % ((r << 1) + 4); 47 | j = (j + 1) % c; 48 | } 49 | keyset = true; 50 | } 51 | 52 | std::string RC6::encrypt(const std::string & DATA){ 53 | if (!keyset){ 54 | throw std::runtime_error("Error: Key has not been set."); 55 | } 56 | 57 | uint32_t A = toint(little_end(DATA.substr(0, w >> 3), 256), 256), B = toint(little_end(DATA.substr(w >> 3, w >> 3), 256), 256), C = toint(little_end(DATA.substr(w >> 2, w >> 3), 256), 256), D = toint(little_end(DATA.substr(3 * (w >> 3), w >> 3), 256), 256); 58 | B += S[0]; 59 | D += S[1]; 60 | for(uint8_t i = 1; i < r + 1; i++){ 61 | uint64_t t = ROL((B * (B + B + 1)), lgw, w); 62 | uint64_t u = ROL((D * (D + D + 1)), lgw, w); 63 | A = ROL(A ^ t, u % w, w) + S[i << 1]; 64 | C = ROL(C ^ u, t % w, w) + S[(i << 1) + 1]; 65 | uint64_t temp = A; A = B; B = C; C = D; D = temp; 66 | } 67 | A += S[(r << 1) + 2]; 68 | C += S[(r << 1) + 3]; 69 | return unhexlify(little_end(makehex(A, w >> 2)) + little_end(makehex(B, w >> 2)) + little_end(makehex(C, w >> 2)) + little_end(makehex(D, w >> 2))); 70 | } 71 | 72 | std::string RC6::decrypt(const std::string & DATA){ 73 | if (!keyset){ 74 | throw std::runtime_error("Error: Key has not been set."); 75 | } 76 | 77 | uint32_t A = toint(little_end(DATA.substr(0, w >> 3), 256), 256), B = toint(little_end(DATA.substr(w >> 3, w >> 3), 256), 256), C = toint(little_end(DATA.substr(w >> 2, w >> 3), 256), 256), D = toint(little_end(DATA.substr(3 * (w >> 3), w >> 3), 256), 256); 78 | C -= S[(r << 1) + 3]; 79 | A -= S[(r << 1) + 2]; 80 | for(uint8_t i = r; i > 0; i--){ 81 | uint64_t temp = D; D = C; C = B; B = A; A = temp; 82 | uint64_t u = ROL((uint64_t) ((D * (D + D + 1))), lgw, w); 83 | uint64_t t = ROL((uint64_t) ((B * (B + B + 1))), lgw, w); 84 | C = ROR((C - S[(i << 1) + 1]), t % w, w) ^ u; 85 | A = ROR((A - S[i << 1]), u % w, w) ^ t; 86 | } 87 | D -= S[1]; 88 | B -= S[0]; 89 | return unhexlify(little_end(makehex(A, w >> 2)) + little_end(makehex(B, w >> 2)) + little_end(makehex(C, w >> 2)) + little_end(makehex(D, w >> 2))); 90 | } 91 | 92 | unsigned int RC6::blocksize() const { 93 | return w << 2; 94 | } 95 | -------------------------------------------------------------------------------- /Encryptions/RC6.h: -------------------------------------------------------------------------------- 1 | /* 2 | RC6.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __RC6__ 26 | #define __RC6__ 27 | 28 | #include 29 | #include 30 | 31 | #include "../common/cryptomath.h" 32 | #include "../common/includes.h" 33 | 34 | #include "SymAlg.h" 35 | #include "RC_PQ.h" 36 | 37 | class RC6 : public SymAlg{ 38 | private: 39 | uint32_t w, r, b, lgw; 40 | std::vector S; 41 | 42 | public: 43 | RC6(); 44 | RC6(const std::string & KEY, const unsigned int & W = 32, const unsigned int & R = 20); 45 | void setkey(std::string KEY, const unsigned int & W = 32, const unsigned int & R = 20); 46 | std::string encrypt(const std::string & DATA); 47 | std::string decrypt(const std::string & DATA); 48 | unsigned int blocksize() const; 49 | }; 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /Encryptions/RC_PQ.cpp: -------------------------------------------------------------------------------- 1 | #include "RC_PQ.h" 2 | void rc_pq(const int w, uint64_t & p, uint64_t & q){ 3 | // e - 2 = 0.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746639193200305992181741359662904357290033429526059563073813232862794349076323382988075319525101901157383418793070215408914993488416750924476146066808226480016847741185374234544243710753907774499206955170276183860626133138458300075204493382656029760673711320070932870912744374704723069697720931014169283681902551510865746377211125238978442505695369677078544996996794686445490598793163688923009879312773617821542499922957635148220826989519366803318252886939849646510582093923982948879332036250944311730123819706841614039701983767932068328237646480429531180232878250981945581530175671736133206981125099618188159304169035159888851934580727386673858942287922849989208680582574927961048419844436346324496848756023362482704197862320900216099023530436994184914631409343173814364054625315209618369088870701676839642437814059271456354906130310720851038375051011574770417189861068739696552126715468895703503540212340784981933432106... 4 | // phi - 1 = 0.61803398874989484820458683436563811772030917980576286213544862270526046281890244970720720418939113748475408807538689175212663386222353693179318006076672635443338908659593958290563832266131992829026788067520876689250171169620703222104321626954862629631361443814975870122034080588795445474924618569536486444924104432077134494704956584678850987433944221254487706647809158846074998871240076521705751797883416625624940758906970400028121042762177111777805315317141011704666599146697987317613560067087480710131795236894275219484353056783002287856997829778347845878228911097625003026961561700250464338243776486102838312683303724292675263116533924731671112115881863851331620384005222165791286675294654906811317159934323597349498509040947621322298101726107059611645629909816290555208524790352406020172799747175342777592778625619432082750513121815628551222480939471234145170223735805772786160086883829523045926478780178899219902707769038953219681986151437803149974110692608867429622675756052317277752035361393621076738937645560606059... 5 | switch (w){ 6 | case 16: 7 | p = 0xb7e1U; 8 | q = 0x9e37U; 9 | break; 10 | case 32: 11 | p = 0xb7e15163UL; 12 | q = 0x9e3779b9UL; 13 | break; 14 | case 64: 15 | p = 0xb7e151628aed2a6bULL; 16 | q = 0x9e3779b97f4a7c15ULL; 17 | break; 18 | default: 19 | throw std::runtime_error("Error: No values defined for w = " + std::to_string(w)); 20 | break; 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Encryptions/RC_PQ.h: -------------------------------------------------------------------------------- 1 | /* 2 | RC_PQ.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __RC_PQ__ 26 | #define __RC_PQ__ 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | void rc_pq(const int w , uint64_t & p, uint64_t & q); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /Encryptions/SAFERK64.h: -------------------------------------------------------------------------------- 1 | /* 2 | SAFERK64.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __SAFERK64__ 26 | #define __SAFERK64__ 27 | 28 | #include 29 | 30 | #include "../common/cryptomath.h" 31 | #include "SymAlg.h" 32 | 33 | #include "SAFERK64_Const.h" 34 | 35 | class SAFERK64 : public SymAlg{ 36 | private: 37 | uint8_t r; // 6 <= r <= 10 38 | std::vector > keys; 39 | void add_bias(std::vector & key_i, const uint8_t b[8]); 40 | std::vector byte_rotate_3(std::vector & key_i); 41 | void xor_add(std::vector & data, std::vector & key); 42 | void add_xor(std::vector & data, std::vector & key); 43 | void xor_sub(std::vector & data, std::vector & key); 44 | void sub_xor(std::vector & data, std::vector & key); 45 | void nonlinear_lajer(std::vector & data); 46 | void inv_nonlinear_lajer(std::vector & data); 47 | void pht(uint8_t & a1, uint8_t & a2); 48 | void inv_pht(uint8_t & b1, uint8_t & b2); 49 | void pht_layer(std::vector & data); 50 | void inv_pht_layer(std::vector & data); 51 | 52 | public: 53 | SAFERK64(); 54 | SAFERK64(const std::string & KEY, const uint8_t & rounds = 10); 55 | void setkey(const std::string & KEY, const uint8_t & rounds = 10); 56 | std::string encrypt(const std::string & DATA); 57 | std::string decrypt(const std::string & DATA); 58 | unsigned int blocksize() const; 59 | }; 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /Encryptions/SEED.cpp: -------------------------------------------------------------------------------- 1 | #include "SEED.h" 2 | 3 | uint32_t SEED::G(uint32_t X){ 4 | X %= mod32; 5 | uint8_t X3 = (X >> 24) & 255; 6 | uint8_t X2 = (X >> 16) & 255; 7 | uint8_t X1 = (X >> 8) & 255; 8 | uint8_t X0 = X & 255; 9 | uint8_t m0 = 0xFC, m1 = 0xF3, m2 = 0xCF, m3 = 0x3F; 10 | uint32_t Z0 = (SEED_S0[X0] & m0) ^ (SEED_S1[X1] & m1) ^ (SEED_S0[X2] & m2) ^ (SEED_S1[X3] & m3); 11 | uint32_t Z1 = (SEED_S0[X0] & m1) ^ (SEED_S1[X1] & m2) ^ (SEED_S0[X2] & m3) ^ (SEED_S1[X3] & m0); 12 | uint32_t Z2 = (SEED_S0[X0] & m2) ^ (SEED_S1[X1] & m3) ^ (SEED_S0[X2] & m0) ^ (SEED_S1[X3] & m1); 13 | uint32_t Z3 = (SEED_S0[X0] & m3) ^ (SEED_S1[X1] & m0) ^ (SEED_S0[X2] & m1) ^ (SEED_S1[X3] & m2); 14 | return (Z3 << 24) + (Z2 << 16) + (Z1 << 8) + Z0; 15 | } 16 | 17 | uint64_t SEED::F(const uint64_t & right, const std::pair & K){ 18 | uint64_t R0 = right >> 32; 19 | uint64_t R1 = right & mod32; 20 | uint64_t r = G(G(G((R0 ^ K.first) ^ (R1 ^ K.second)) + (R0 ^ K.first)) + G((R0 ^ K.first) ^ (R1 ^ K.second))) & mod32; 21 | return ((r + (G(G((R0 ^ K.first) ^ (R1 ^ K.second)) + (R0 ^ K.first)))) << 32) + r; 22 | } 23 | 24 | std::string SEED::run(const std::string & DATA){ 25 | if (!keyset){ 26 | throw std::runtime_error("Error: Key has not been set."); 27 | } 28 | 29 | if (DATA.size() != 16){ 30 | throw std::runtime_error("Error: Data must be 128 bits in length."); 31 | } 32 | 33 | uint64_t L = toint(DATA.substr(0, 8), 256), R = toint(DATA.substr(8, 8), 256); 34 | for(uint8_t i = 0; i < 16; i++){ 35 | uint64_t temp = L; 36 | L = R; 37 | R = temp ^ F(R, k[i]); 38 | } 39 | return unhexlify(makehex(R, 16) + makehex(L, 16)); 40 | } 41 | 42 | SEED::SEED() 43 | : SymAlg(), 44 | mode(""), 45 | k() 46 | {} 47 | 48 | SEED::SEED(const std::string & KEY) 49 | : SEED() 50 | { 51 | setkey(KEY); 52 | } 53 | 54 | void SEED::setkey(const std::string & KEY){ 55 | if (keyset){ 56 | throw std::runtime_error("Error: Key has already been set."); 57 | } 58 | 59 | if (KEY.size() != 16){ 60 | throw std::runtime_error("Error: Key must be 128 bits in length."); 61 | } 62 | 63 | uint64_t K0 = toint(KEY.substr(0, 4), 256); 64 | uint64_t K1 = toint(KEY.substr(4, 4), 256); 65 | uint64_t K2 = toint(KEY.substr(8, 4), 256); 66 | uint64_t K3 = toint(KEY.substr(12, 4), 256); 67 | uint64_t T; 68 | for(uint8_t i = 1; i < 17; i++){ 69 | k[i - 1].first = G((K0 + K2 - SEED_KC[i - 1])); 70 | k[i - 1].second = G((K1 - K3 + SEED_KC[i - 1])); 71 | if (i % 2){ 72 | T = ROR(((K0 << 32) + K1) & mod64, 8, 64); 73 | K0 = T >> 32; K1 = T & mod32; 74 | } 75 | else{ 76 | T = ROL(((K2 << 32) + K3) & mod64, 8, 64); 77 | K2 = T >> 32; K3 = T & mod32; 78 | } 79 | } 80 | keyset = true; 81 | } 82 | 83 | std::string SEED::encrypt(const std::string & DATA){ 84 | return run(DATA); 85 | } 86 | 87 | std::string SEED::decrypt(const std::string & DATA){ 88 | std::reverse(k, k + 16); 89 | std::string out = run(DATA); 90 | std::reverse(k, k + 16); 91 | return out; 92 | } 93 | 94 | unsigned int SEED::blocksize() const { 95 | return 128; 96 | } 97 | -------------------------------------------------------------------------------- /Encryptions/SEED.h: -------------------------------------------------------------------------------- 1 | /* 2 | SEED.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __SEED__ 26 | #define __SEED__ 27 | 28 | #include 29 | 30 | #include "../common/cryptomath.h" 31 | #include "../common/includes.h" 32 | #include "SymAlg.h" 33 | 34 | #include "SEED_Const.h" 35 | 36 | class SEED : public SymAlg{ 37 | private: 38 | std::string mode; 39 | std::pair k[16]; 40 | uint32_t G(uint32_t X); 41 | uint64_t F(const uint64_t & right, const std::pair & K); 42 | std::string run(const std::string & data); 43 | 44 | public: 45 | SEED(); 46 | SEED(const std::string & KEY); 47 | void setkey(const std::string & KEY); 48 | std::string encrypt(const std::string & DATA); 49 | std::string decrypt(const std::string & DATA); 50 | unsigned int blocksize() const; 51 | }; 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /Encryptions/Skipjack.cpp: -------------------------------------------------------------------------------- 1 | #include "Skipjack.h" 2 | 3 | uint16_t Skipjack::G_e(const uint16_t quarter, const uint32_t fourkeys){ 4 | uint16_t left = quarter >> 8; 5 | uint16_t right = quarter & 255; 6 | uint8_t k0 = fourkeys >> 24; 7 | uint8_t k1 = (fourkeys >> 16) & 255; 8 | uint8_t k2 = (fourkeys >> 8) & 255; 9 | uint8_t k3 = fourkeys & 255; 10 | left ^= Skipjack_F[right ^ k0]; 11 | right ^= Skipjack_F[left ^ k1]; 12 | left ^= Skipjack_F[right ^ k2]; 13 | right ^= Skipjack_F[left ^ k3]; 14 | return (left << 8) + right; 15 | } 16 | 17 | void Skipjack::A(uint16_t & w1, uint16_t & w2, uint16_t & w3, uint16_t & w4, const uint16_t i){ 18 | uint16_t w5 = w4; 19 | w4 = w3; 20 | w3 = w2; 21 | w2 = G_e(w1, toint(key.substr((i << 3) - 8, 8), 16)); 22 | w1 = w2 ^ w5 ^ i; 23 | } 24 | 25 | void Skipjack::B(uint16_t & w1, uint16_t & w2, uint16_t & w3, uint16_t & w4, const uint16_t i){ 26 | uint16_t w5 = w4; 27 | w4 = w3; 28 | w3 = w1 ^ w2 ^ i; 29 | w2 = G_e(w1, toint(key.substr((i << 3) - 8, 8), 16)); 30 | w1 = w5; 31 | } 32 | 33 | uint16_t Skipjack::G_d(const uint16_t quarter, const uint32_t fourkeys){ 34 | uint16_t left = quarter >> 8; 35 | uint16_t right = quarter & 255; 36 | uint8_t k0 = fourkeys >> 24; 37 | uint8_t k1 = (fourkeys >> 16) & 255; 38 | uint8_t k2 = (fourkeys >> 8) & 255; 39 | uint8_t k3 = fourkeys & 255; 40 | right ^= Skipjack_F[left ^ k3]; 41 | left ^= Skipjack_F[right ^ k2]; 42 | right ^= Skipjack_F[left ^ k1]; 43 | left ^= Skipjack_F[right ^ k0]; 44 | return (left << 8) + right; 45 | } 46 | 47 | void Skipjack::invA(uint16_t & w1, uint16_t & w2, uint16_t & w3, uint16_t & w4, const uint16_t i){ 48 | uint16_t w5 = w1 ^ w2 ^ i; 49 | w1 = G_d(w2, toint(key.substr((i << 3) - 8, 8), 16)); 50 | w2 = w3; 51 | w3 = w4; 52 | w4 = w5; 53 | } 54 | 55 | void Skipjack::invB(uint16_t & w1, uint16_t & w2, uint16_t & w3, uint16_t & w4, const uint16_t i){ 56 | uint16_t w5 = w1; 57 | w1 = G_d(w2, toint(key.substr((i << 3) - 8, 8), 16)); 58 | w2 = w1 ^ w3 ^ i; 59 | w3 = w4; 60 | w4 = w5; 61 | } 62 | 63 | Skipjack::Skipjack() 64 | : SymAlg(), 65 | key("") 66 | {} 67 | 68 | Skipjack::Skipjack(const std::string & KEY) 69 | : Skipjack() 70 | { 71 | setkey(KEY); 72 | } 73 | 74 | void Skipjack::setkey(const std::string & KEY){ 75 | if (keyset){ 76 | throw std::runtime_error("Error: Key has already been set."); 77 | } 78 | 79 | if (KEY.size() != 10){ 80 | throw std::runtime_error("Error: Key must be 80 bits in length."); 81 | } 82 | 83 | key = hexlify(KEY); 84 | while (key.size() < 256){ 85 | key = key + key; 86 | } 87 | key = key.substr(0, 256); 88 | keyset = true; 89 | } 90 | 91 | std::string Skipjack::encrypt(const std::string & DATA){ 92 | if (!keyset){ 93 | throw std::runtime_error("Error: Key has not been set"); 94 | } 95 | 96 | if (DATA.size() != 8){ 97 | throw std::runtime_error("Error: Data must be 64 bits in length."); 98 | } 99 | 100 | uint16_t w1 = toint(DATA.substr(0, 2), 256); 101 | uint16_t w2 = toint(DATA.substr(2, 2), 256); 102 | uint16_t w3 = toint(DATA.substr(4, 2), 256); 103 | uint16_t w4 = toint(DATA.substr(6, 2), 256); 104 | 105 | for(uint8_t i = 1; i < 9; i++){ 106 | A(w1, w2, w3, w4, i); 107 | } 108 | 109 | for(uint8_t i = 9; i < 17; i++){ 110 | B(w1, w2, w3, w4, i); 111 | } 112 | 113 | for(uint8_t i = 17; i < 25; i++){ 114 | A(w1, w2, w3, w4, i); 115 | } 116 | 117 | for(uint8_t i = 25; i < 33; i++){ 118 | B(w1, w2, w3, w4, i); 119 | } 120 | 121 | return unhexlify(makehex(w1, 4) + makehex(w2, 4) + makehex(w3, 4) + makehex(w4, 4)); 122 | } 123 | 124 | std::string Skipjack::decrypt(const std::string & DATA){ 125 | if (!keyset){ 126 | throw std::runtime_error("Error: Key has not been set"); 127 | } 128 | 129 | if (DATA.size() != 8){ 130 | throw std::runtime_error("Error: Data must be 64 bits in length."); 131 | } 132 | 133 | uint16_t w1 = toint(DATA.substr(0, 2), 256); 134 | uint16_t w2 = toint(DATA.substr(2, 2), 256); 135 | uint16_t w3 = toint(DATA.substr(4, 2), 256); 136 | uint16_t w4 = toint(DATA.substr(6, 2), 256); 137 | 138 | for(uint8_t i = 32; i > 24; i--){ 139 | invB(w1, w2, w3, w4, i); 140 | } 141 | 142 | for(uint8_t i = 24; i > 16; i--){ 143 | invA(w1, w2, w3, w4, i); 144 | } 145 | 146 | for(uint8_t i = 16; i > 8; i--){ 147 | invB(w1, w2, w3, w4, i); 148 | } 149 | 150 | for(uint8_t i = 8; i > 0; i--){ 151 | invA(w1, w2, w3, w4, i); 152 | } 153 | 154 | return unhexlify(makehex(w1, 4) + makehex(w2, 4) + makehex(w3, 4) + makehex(w4, 4)); 155 | } 156 | 157 | unsigned int Skipjack::blocksize() const { 158 | return 64; 159 | } 160 | -------------------------------------------------------------------------------- /Encryptions/Skipjack.h: -------------------------------------------------------------------------------- 1 | /* 2 | Skipjack.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __SKIPJACK__ 26 | #define __SKIPJACK__ 27 | 28 | #include "../common/includes.h" 29 | #include "SymAlg.h" 30 | 31 | #include "Skipjack_Const.h" 32 | 33 | class Skipjack : public SymAlg{ 34 | private: 35 | std::string key; 36 | uint16_t G_e(const uint16_t quarter, const uint32_t fourkeys); 37 | void A(uint16_t & w1, uint16_t & w2, uint16_t & w3, uint16_t & w4, const uint16_t i); 38 | void B(uint16_t & w1, uint16_t & w2, uint16_t & w3, uint16_t & w4, const uint16_t i); 39 | uint16_t G_d(const uint16_t quarter, const uint32_t fourkeys); 40 | void invA(uint16_t & w1, uint16_t & w2, uint16_t & w3, uint16_t & w4, const uint16_t i); 41 | void invB(uint16_t & w1, uint16_t & w2, uint16_t & w3, uint16_t & w4, const uint16_t i); 42 | 43 | public: 44 | Skipjack(); 45 | Skipjack(const std::string & KEY); 46 | void setkey(const std::string & KEY); 47 | std::string encrypt(const std::string & DATA); 48 | std::string decrypt(const std::string & DATA); 49 | unsigned int blocksize() const; 50 | }; 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /Encryptions/Skipjack_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | Skipjack_Const.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __SKIPJACK_CONST__ 26 | #define __SKIPJACK_CONST__ 27 | 28 | const uint8_t Skipjack_F[256] = {0xa3, 0xd7, 0x09, 0x83, 0xf8, 0x48, 0xf6, 0xf4, 0xb3, 0x21, 0x15, 0x78, 0x99, 0xb1, 0xaf, 0xf9, 29 | 0xe7, 0x2d, 0x4d, 0x8a, 0xce, 0x4c, 0xca, 0x2e, 0x52, 0x95, 0xd9, 0x1e, 0x4e, 0x38, 0x44, 0x28, 30 | 0x0a, 0xdf, 0x02, 0xa0, 0x17, 0xf1, 0x60, 0x68, 0x12, 0xb7, 0x7a, 0xc3, 0xe9, 0xfa, 0x3d, 0x53, 31 | 0x96, 0x84, 0x6b, 0xba, 0xf2, 0x63, 0x9a, 0x19, 0x7c, 0xae, 0xe5, 0xf5, 0xf7, 0x16, 0x6a, 0xa2, 32 | 0x39, 0xb6, 0x7b, 0x0f, 0xc1, 0x93, 0x81, 0x1b, 0xee, 0xb4, 0x1a, 0xea, 0xd0, 0x91, 0x2f, 0xb8, 33 | 0x55, 0xb9, 0xda, 0x85, 0x3f, 0x41, 0xbf, 0xe0, 0x5a, 0x58, 0x80, 0x5f, 0x66, 0x0b, 0xd8, 0x90, 34 | 0x35, 0xd5, 0xc0, 0xa7, 0x33, 0x06, 0x65, 0x69, 0x45, 0x00, 0x94, 0x56, 0x6d, 0x98, 0x9b, 0x76, 35 | 0x97, 0xfc, 0xb2, 0xc2, 0xb0, 0xfe, 0xdb, 0x20, 0xe1, 0xeb, 0xd6, 0xe4, 0xdd, 0x47, 0x4a, 0x1d, 36 | 0x42, 0xed, 0x9e, 0x6e, 0x49, 0x3c, 0xcd, 0x43, 0x27, 0xd2, 0x07, 0xd4, 0xde, 0xc7, 0x67, 0x18, 37 | 0x89, 0xcb, 0x30, 0x1f, 0x8d, 0xc6, 0x8f, 0xaa, 0xc8, 0x74, 0xdc, 0xc9, 0x5d, 0x5c, 0x31, 0xa4, 38 | 0x70, 0x88, 0x61, 0x2c, 0x9f, 0x0d, 0x2b, 0x87, 0x50, 0x82, 0x54, 0x64, 0x26, 0x7d, 0x03, 0x40, 39 | 0x34, 0x4b, 0x1c, 0x73, 0xd1, 0xc4, 0xfd, 0x3b, 0xcc, 0xfb, 0x7f, 0xab, 0xe6, 0x3e, 0x5b, 0xa5, 40 | 0xad, 0x04, 0x23, 0x9c, 0x14, 0x51, 0x22, 0xf0, 0x29, 0x79, 0x71, 0x7e, 0xff, 0x8c, 0x0e, 0xe2, 41 | 0x0c, 0xef, 0xbc, 0x72, 0x75, 0x6f, 0x37, 0xa1, 0xec, 0xd3, 0x8e, 0x62, 0x8b, 0x86, 0x10, 0xe8, 42 | 0x08, 0x77, 0x11, 0xbe, 0x92, 0x4f, 0x24, 0xc5, 0x32, 0x36, 0x9d, 0xcf, 0xf3, 0xa6, 0xbb, 0xac, 43 | 0x5e, 0x6c, 0xa9, 0x13, 0x57, 0x25, 0xb5, 0xe3, 0xbd, 0xa8, 0x3a, 0x01, 0x05, 0x59, 0x2a, 0x46}; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /Encryptions/SymAlg.cpp: -------------------------------------------------------------------------------- 1 | #include "SymAlg.h" 2 | 3 | SymAlg::SymAlg() 4 | : keyset(false) 5 | {} 6 | 7 | SymAlg::~SymAlg(){} 8 | -------------------------------------------------------------------------------- /Encryptions/SymAlg.h: -------------------------------------------------------------------------------- 1 | /* 2 | SymAlg.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __SYMALG__ 26 | #define __SYMALG__ 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | class SymAlg{ 33 | protected: 34 | bool keyset; 35 | 36 | public: 37 | typedef std::shared_ptr Ptr; 38 | 39 | SymAlg(); 40 | virtual ~SymAlg(); 41 | virtual std::string encrypt(const std::string & DATA) = 0; 42 | virtual std::string decrypt(const std::string & DATA) = 0; 43 | virtual unsigned int blocksize() const = 0; // blocksize in bits 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /Encryptions/TDES.cpp: -------------------------------------------------------------------------------- 1 | #include "TDES.h" 2 | 3 | std::string TDES::run(const std::string & data, const std::string & key, const bool & mode){ 4 | if (!keyset){ 5 | throw std::runtime_error("Error: Key has not been set."); 6 | } 7 | 8 | if (data.size() != 8){ 9 | throw std::runtime_error("Error: Data must be 64 bits in length."); 10 | } 11 | 12 | if (!mode){ 13 | return DES(key).encrypt(data); 14 | } 15 | return DES(key).decrypt(data); 16 | } 17 | 18 | TDES::TDES() 19 | : SymAlg(), 20 | k1(), k2(), k3(), 21 | m1(), m2(), m3() 22 | {} 23 | 24 | TDES::TDES(const std::string & key1, const std::string & mode1, const std::string & key2, const std::string & mode2, const std::string & key3, const std::string & mode3) 25 | : TDES() 26 | { 27 | setkey(key1, mode1, key2, mode2, key3, mode3); 28 | } 29 | 30 | void TDES::setkey(const std::string & key1, const std::string & mode1, const std::string & key2, const std::string & mode2, const std::string & key3, const std::string & mode3){ 31 | if (keyset){ 32 | throw std::runtime_error("Error: Key has already been set."); 33 | } 34 | 35 | if (key1.size() != 8){ 36 | throw std::runtime_error("Error: Key must be 64 bits in length."); 37 | } 38 | 39 | if (key2.size() != 8){ 40 | throw std::runtime_error("Error: Key must be 64 bits in length."); 41 | } 42 | 43 | if (key3.size() != 8){ 44 | throw std::runtime_error("Error: Key must be 64 bits in length."); 45 | } 46 | 47 | k1 = key1; 48 | k2 = key2; 49 | k3 = key3; 50 | m1 = (mode1 == "d"); 51 | m2 = (mode2 == "d"); 52 | m3 = (mode3 == "d"); 53 | 54 | keyset = true; 55 | } 56 | 57 | std::string TDES::encrypt(const std::string & DATA){ 58 | return run(run(run(DATA, k1, m1), k2, m2), k3, m3); 59 | } 60 | 61 | std::string TDES::decrypt(const std::string & DATA){ 62 | return run(run(run(DATA, k1, !m1), k2, !m2), k3, !m3); 63 | } 64 | 65 | unsigned int TDES::blocksize() const { 66 | return 64; 67 | } 68 | -------------------------------------------------------------------------------- /Encryptions/TDES.h: -------------------------------------------------------------------------------- 1 | /* 2 | TDES.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #include "DES.h" 26 | #include "SymAlg.h" 27 | 28 | #ifndef __TDES__ 29 | #define __TDES__ 30 | class TDES : public SymAlg{ 31 | private: 32 | std::string k1, k2, k3; 33 | bool m1, m2, m3; 34 | std::string run(const std::string & data, const std::string & key, const bool & mode); 35 | 36 | public: 37 | TDES(); 38 | TDES(const std::string & key1, const std::string & mode1, const std::string & key2, const std::string & mode2, const std::string & key3, const std::string & mode3); 39 | void setkey(const std::string & key1, const std::string & mode1, const std::string & key2, const std::string & mode2, const std::string & key3, const std::string & mode3); 40 | std::string encrypt(const std::string & DATA); 41 | std::string decrypt(const std::string & DATA); 42 | unsigned int blocksize() const; 43 | }; 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /Encryptions/TEA.cpp: -------------------------------------------------------------------------------- 1 | #include "TEA.h" 2 | 3 | TEA::TEA() 4 | : SymAlg(), 5 | delta(0), cycles(0), total(0), key() 6 | {} 7 | 8 | TEA::TEA(const std::string & KEY, const uint32_t & ROUNDS, const uint32_t & DELTA, const uint32_t & TOTAL) 9 | : TEA() 10 | { 11 | setkey(KEY, ROUNDS, DELTA, TOTAL); 12 | } 13 | 14 | void TEA::setkey(const std::string & KEY, const uint32_t & ROUNDS, const uint32_t & DELTA, const uint32_t & TOTAL){ 15 | if (keyset){ 16 | throw std::runtime_error("Error: Key has already been set."); 17 | } 18 | 19 | if (KEY.size() != 16){ 20 | throw std::runtime_error("Error: Key must be 128 bits in length."); 21 | } 22 | 23 | delta = DELTA; 24 | total = TOTAL; 25 | cycles = ROUNDS >> 1; 26 | for(uint8_t x = 0; x < 4; x++){ 27 | key[x] = static_cast (toint(KEY.substr(x << 2, 4), 256)); 28 | } 29 | keyset = true; 30 | } 31 | 32 | std::string TEA::encrypt(const std::string & DATA){ 33 | if (!keyset){ 34 | throw std::runtime_error("Error: Key has not been set."); 35 | } 36 | 37 | if (DATA.size() != 8){ 38 | throw std::runtime_error("Error: Data must be 64 bits in length."); 39 | } 40 | 41 | uint32_t data[2] = {static_cast (toint(DATA.substr(0, 4), 256)), static_cast (toint(DATA.substr(4, 4), 256))}; 42 | total = 0; 43 | for(uint8_t i = 0; i < cycles; i++){ 44 | total += delta; 45 | data[0] += ((data[1] << 4) + key[0]) ^ (data[1] + total) ^ ((data[1] >> 5) + key[1]); 46 | data[1] += ((data[0] << 4) + key[2]) ^ (data[0] + total) ^ ((data[0] >> 5) + key[3]); 47 | } 48 | return unhexlify(makehex(data[0], 8) + makehex(data[1], 8)); 49 | } 50 | 51 | std::string TEA::decrypt(const std::string & DATA){ 52 | if (!keyset){ 53 | throw std::runtime_error("Error: Key has not been set."); 54 | } 55 | 56 | if (DATA.size() != 8){ 57 | throw std::runtime_error("Error: Data must be 64 bits in length."); 58 | } 59 | 60 | uint32_t data[2] = {static_cast (toint(DATA.substr(0, 4), 256)), static_cast (toint(DATA.substr(4, 4), 256))}; 61 | for(uint8_t i = 0; i < cycles; i++){ 62 | data[1] -= ((data[0] << 4) + key[2]) ^ (data[0] + total) ^ ((data[0] >> 5) + key[3]); 63 | data[0] -= ((data[1] << 4) + key[0]) ^ (data[1] + total) ^ ((data[1] >> 5) + key[1]); 64 | total -= delta; 65 | } 66 | return unhexlify(makehex(data[0], 8) + makehex(data[1], 8)); 67 | } 68 | 69 | unsigned int TEA::blocksize() const { 70 | return 64; 71 | } 72 | -------------------------------------------------------------------------------- /Encryptions/TEA.h: -------------------------------------------------------------------------------- 1 | /* 2 | TEA.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __TEA__ 26 | #define __TEA__ 27 | 28 | #include "../common/includes.h" 29 | #include "SymAlg.h" 30 | 31 | class TEA : public SymAlg{ 32 | private: 33 | uint32_t delta, cycles, total, key[4]; 34 | 35 | public: 36 | TEA(); 37 | TEA(const std::string & KEY, const uint32_t & ROUNDS = 64, const uint32_t & DELTA = 0x9e3779b9, const uint32_t & TOTAL = 0xc6ef3720); 38 | void setkey(const std::string & KEY, const uint32_t & ROUNDS = 64, const uint32_t & DELTA = 0x9e3779b9, const uint32_t & TOTAL = 0xc6ef3720); 39 | std::string encrypt(const std::string & DATA); 40 | std::string decrypt(const std::string & DATA); 41 | unsigned int blocksize() const; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /Encryptions/Twofish.h: -------------------------------------------------------------------------------- 1 | #ifndef __TWOFISH__ 2 | #define __TWOFISH__ 3 | 4 | #include 5 | 6 | #include "../common/includes.h" 7 | #include "SymAlg.h" 8 | 9 | #include "Twofish_Const.h" 10 | 11 | class Twofish : public SymAlg{ 12 | private: 13 | std::vector l_key; 14 | std::vector> mk_tab; 15 | 16 | uint32_t h_fun(uint32_t x, const std::vector & key); 17 | std::string run(const std::string & data, bool enc); 18 | 19 | public: 20 | Twofish(); 21 | Twofish(const std::string & KEY); 22 | void setkey(const std::string & KEY); 23 | std::string encrypt(const std::string & DATA); 24 | std::string decrypt(const std::string & DATA); 25 | unsigned int blocksize() const; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /Encryptions/XTEA.cpp: -------------------------------------------------------------------------------- 1 | #include "XTEA.h" 2 | 3 | XTEA::XTEA() 4 | : SymAlg(), 5 | delta(0), cycles(0), total(0), key() 6 | {} 7 | 8 | XTEA::XTEA(const std::string & KEY, const uint8_t & ROUNDS, const uint32_t & DELTA) //, const uint32_t & TOTAL = 0xc6ef3720){ 9 | : XTEA() 10 | { 11 | setkey(KEY, ROUNDS, DELTA); 12 | } 13 | 14 | void XTEA::setkey(const std::string & KEY, const uint8_t & ROUNDS, const uint32_t & DELTA){ 15 | if (keyset){ 16 | throw std::runtime_error("Error: Key has already been set."); 17 | } 18 | 19 | if (KEY.size() != 16){ 20 | throw std::runtime_error("Error: Key must be 128 bits in length."); 21 | } 22 | 23 | delta = DELTA; 24 | cycles = ROUNDS >> 1; 25 | for(uint8_t x = 0; x < 4; x++){ 26 | key[x] = static_cast (toint(KEY.substr(x << 2, 4), 256)); 27 | } 28 | keyset = true; 29 | } 30 | 31 | std::string XTEA::encrypt(const std::string & DATA){ 32 | if (!keyset){ 33 | throw std::runtime_error("Error: Key has not been set."); 34 | } 35 | 36 | if (DATA.size() != 8){ 37 | throw std::runtime_error("Error: Data must be 64 bits in length."); 38 | } 39 | 40 | uint32_t data[2] = {static_cast (toint(DATA.substr(0, 4), 256)), static_cast (toint(DATA.substr(4, 4), 256))}; 41 | total = 0; 42 | for(uint8_t i = 0; i < cycles; i++){ 43 | data[0] += (((data[1] << 4) ^ (data[1] >> 5)) + data[1]) ^ (total + key[total & 3]); 44 | total += delta; 45 | data[1] += (((data[0] << 4) ^ (data[0] >> 5)) + data[0]) ^ (total + key[(total >> 11) & 3]); 46 | } 47 | return unhexlify(makehex(data[0], 8) + makehex(data[1], 8)); 48 | } 49 | 50 | std::string XTEA::decrypt(const std::string & DATA){ 51 | if (!keyset){ 52 | throw std::runtime_error("Error: Key has not been set."); 53 | } 54 | 55 | if (DATA.size() != 8){ 56 | throw std::runtime_error("Error: Data must be 64 bits in length."); 57 | } 58 | 59 | uint32_t data[2] = {static_cast (toint(DATA.substr(0, 4), 256)), static_cast (toint(DATA.substr(4, 4), 256))}; 60 | total = delta * cycles; 61 | for(uint8_t i = 0; i < cycles; i++){ 62 | data[1] -= (((data[0] << 4) ^ (data[0] >> 5)) + data[0]) ^ (total + key[(total >> 11) & 3]); 63 | total -= delta; 64 | data[0] -= (((data[1] << 4) ^ (data[1] >> 5)) + data[1]) ^ (total + key[total & 3]); 65 | } 66 | return unhexlify(makehex(data[0], 8) + makehex(data[1], 8)); 67 | } 68 | 69 | unsigned int XTEA::blocksize() const { 70 | return 64; 71 | } 72 | -------------------------------------------------------------------------------- /Encryptions/XTEA.h: -------------------------------------------------------------------------------- 1 | /* 2 | XTEA.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef __XTEA__ 26 | #define __XTEA__ 27 | 28 | #include "../common/includes.h" 29 | #include "SymAlg.h" 30 | 31 | class XTEA : public SymAlg{ 32 | private: 33 | uint32_t delta, cycles, total, key[4]; 34 | 35 | public: 36 | XTEA(); 37 | XTEA(const std::string & KEY, const uint8_t & ROUNDS = 64, const uint32_t & DELTA = 0x9e3779b9); 38 | void setkey(const std::string & KEY, const uint8_t & ROUNDS = 64, const uint32_t & DELTA = 0x9e3779b9); 39 | std::string encrypt(const std::string & DATA); 40 | std::string decrypt(const std::string & DATA); 41 | unsigned int blocksize() const; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto@gmail.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A C++ library of encryption algorithms 2 | 3 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 4 | 5 | Please see LICENSE file for license. 6 | 7 | [![Build Status](https://travis-ci.org/calccrypto/Encryptions.svg?branch=master)](https://travis-ci.org/calccrypto/Encryptions) 8 |
  9 | 
 10 | IMPORTANT:
 11 |     This library was not written for actual use.
 12 |     Rather, it was meant for educational purposes,
 13 |     so if you choose to use it in a real setting
 14 |     where secrecy is required, do so at your own risk.
 15 |     People who use this library to learn about the
 16 |     algorithms can easily add a few std::couts to
 17 |     see the internal data.
 18 | 
 19 | Algorithms:
 20 |     AES 128/192/256
 21 |     Blowfish
 22 |     Camellia 128/192/256
 23 |     CAST-128 aka CAST5
 24 |     CAST-256 aka CAST6
 25 |     DES
 26 |     DES-X
 27 |     GOST 28147-89
 28 |     IDEA
 29 |     MISTY1
 30 |     RC2
 31 |     RC4 (Allegedly)
 32 |     RC5-32/12/16 (can be changed)
 33 |     RC6-32/20 (can be changed)
 34 |     SAFER K-64
 35 |     SEED
 36 |     Skipjack
 37 |     Triple DES
 38 |     TEA
 39 |     Twofish
 40 |     XTEA
 41 | 
 42 | Modes of Operation (mod_op):
 43 |     Electronic Codebook (ECB)
 44 |     Cipher - Block Chaining (CBC)
 45 |     Cipher Feedback (CFB)
 46 |     Counter (CTR)
 47 |     Output Feedback (OFB)
 48 |     Propagating Cipher - Block Chaining (PCBC)
 49 | 
 50 | Notes:
 51 |     Algorithms:
 52 |         All above algorithms are derived from the "SymAlg" class,
 53 |         which is used in the Mode of Operations template.
 54 | 
 55 |         Algorithms will expect eactly 1 block or b bits of data in
 56 |         8 bit ASCII, where b is the block size of the algorithm in
 57 |         bits. If there are the wrong number of bits, the algorithm
 58 |         should crash.
 59 | 
 60 |         The keying algorithms will do the same, unless there are
 61 |         defined padding methods.
 62 | 
 63 |         blocksize() returns the blocksize in bits
 64 | 
 65 |         Data padding is a hopefully correct implementation of PKCS5
 66 | 
 67 |     Modes of Operation:
 68 |         Only the general ideas of how each Mode of Operation works is used.
 69 |         If any of the included algorithm has a specific method of running
 70 |         under a certain Mode of Operation, it was probably not programmed.
 71 | 
 72 |         The default Initialization Vector is just a series of 0s.
 73 | 
 74 | Build:
 75 |     mkdir build
 76 |     cd build
 77 |     cmake ..
 78 |     make
 79 |     (optional) make test
 80 |     (optional) make install
 81 | 
 82 | Usage:
 83 |     'instance' is an instance of an algorithm
 84 |     'key' is a string that is one key long for any algorithm
 85 |     'plaintext' is a one block long string of non encrypted data
 86 |     'ciphertext' is a one block long string of encrypted data
 87 | 
 88 |     To run the algorithms separately (run on only 1 block), simply use '.encrypt(data)'
 89 |     to encrypt or '.decrypt(data)' to decrypt, since the key has already been expanded.
 90 | 
 91 |     Ex:
 92 |         AES instance(key)
 93 |         ciphertext = instance.encrypt(plaintext)
 94 | 
 95 |     To encrypt or decrypt data using a Mode of Operation function, Simply create an instance of the mod_op.
 96 |     The input is the entire data in one string.
 97 |         Notes:
 98 |             * ECB mode does not require an IV.
 99 |             * Modes CFB, CTR, and OFB are always in encrypt mode (already programmed in)
100 |             * The other modes can use encrypt or decrypt instances of the algorithms
101 |             * IV are hex strings of any length, preferably one block long
102 |             * Any input with default value NULL can have any input. It does not matter
103 |     Ex:
104 |         Encrypting using CBC mode on AES:
105 |             SymAlg * instance = new AES(key)
106 |             data = CBC(instance, IV).encrypt(plaintext1 + plaintext2 + ... + plaintextN)
107 |             delete(instance)
108 | 
109 | -------------------------------------------------------------------------------- /common/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11.4) 2 | 3 | set(COMMON_FILES 4 | cryptomath.h 5 | includes.cpp includes.h 6 | ) 7 | 8 | foreach(FILE ${COMMON_FILES}) 9 | target_sources(Encryptions_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}) 10 | 11 | get_filename_component(EXT "${FILE}" EXT) 12 | if (EXT STREQUAL ".h") 13 | INSTALL(FILES ${FILE} 14 | DESTINATION include/common) 15 | endif() 16 | endforeach() 17 | 18 | if (BUILD_SHARED_LIB) 19 | foreach(FILE ${COMMON_FILES}) 20 | target_sources(Encryptions_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}) 21 | endforeach() 22 | endif() 23 | -------------------------------------------------------------------------------- /common/cryptomath.h: -------------------------------------------------------------------------------- 1 | /* 2 | A bunch of math algorithms that 3 | I have found useful. 4 | 5 | gcd, invmod, and pow are deprecated since 6 | they are now done through GMP. However, 7 | they are still useful for integer types 8 | that do not have these functions defined. 9 | */ 10 | 11 | #ifndef __CRYPTO_MATH__ 12 | #define __CRYPTO_MATH__ 13 | 14 | #include 15 | #include 16 | 17 | // Greatest Common Divisor 18 | template T gcd(T a, T b){ 19 | static_assert(std::is_integral ::value, "Error: Input value should be integral."); 20 | 21 | T c = 1; 22 | while (c != 0){ 23 | c = a % b; 24 | a = b; 25 | b = c; 26 | } 27 | return a; 28 | } 29 | 30 | // Inverse Mod b * x = 1 mod a 31 | template T invmod(T a, T b){ 32 | static_assert(std::is_integral ::value, "Error: Input values should be integral."); 33 | 34 | T A = a; 35 | T x = 0, lastx = 1, y = 1, lasty = 0; 36 | while (b != 0){ 37 | T quotient = a / b; 38 | T temp = b; 39 | b = a % b; 40 | a = temp; 41 | temp = x; 42 | x = lastx - quotient * x; 43 | lastx = temp; 44 | temp = y; 45 | y = lasty - quotient * y; 46 | lasty = temp; 47 | } 48 | if (lasty < 0){ 49 | lasty += A; 50 | } 51 | return lasty; 52 | } 53 | 54 | // Faster Exponentiation by Squaring 55 | // adapted from http://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-mpz_class-based-power-function-powint-int 56 | template 57 | S POW(S base, T exp){ 58 | static_assert(std::is_integral ::value && 59 | std::is_integral ::value 60 | , "Error: Arguments should be integral."); 61 | 62 | S result = 1; 63 | while (exp != 0) 64 | { 65 | if ((exp & 1) == 1){ 66 | result *= base; 67 | } 68 | exp >>= 1; 69 | base *= base; 70 | } 71 | return result; 72 | } 73 | 74 | // Exponentiation by squaring 75 | template 76 | T POW(R base, S exponent, const T mod){ 77 | static_assert(std::is_integral ::value && 78 | std::is_integral ::value && 79 | std::is_integral ::value 80 | , "Error: Arguments should be integral."); 81 | T result = 1; 82 | while (exponent != 0){ 83 | if ((exponent & 1) == 1){ 84 | result = (result * base) % mod; 85 | } 86 | exponent >>= 1; 87 | base = (base * base) % mod; 88 | } 89 | return result; 90 | } 91 | 92 | // Two's compliment of input 93 | template 94 | T two_comp(const T & a, const uint8_t bits = 16){ 95 | static_assert(std::is_integral ::value, "Error: Input value should be integral."); 96 | return (a ^ ((1ULL << bits) - 1)) + 1; 97 | } 98 | 99 | // From a Twofish code (modified) 100 | // Bitwise rotation to the right 101 | template 102 | T ROR(T x, const uint64_t & n, const uint64_t & bits){ 103 | static_assert(std::is_integral ::value, "Error: Value being rotated should be integral."); 104 | return (x >> n) | ((x & ((1ULL << n) - 1)) << (bits - n)); 105 | } 106 | 107 | // Rotate Left 108 | // Bitwise rotation to the left 109 | template 110 | T ROL(const T & x, const uint64_t & n, const uint64_t & bits){ 111 | static_assert(std::is_integral ::value, "Error: Value being rotated should be integral."); 112 | return ROR(x, bits - n, bits); 113 | } 114 | 115 | #endif 116 | -------------------------------------------------------------------------------- /common/includes.h: -------------------------------------------------------------------------------- 1 | /* 2 | A bunch of useful constants and functions. 3 | Some functions were heavily influenced by python 2.7.2 4 | */ 5 | 6 | #ifndef __INCLUDES__ 7 | #define __INCLUDES__ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | // Some useful constants 16 | static const std::string zero(1, 0); 17 | static const uint8_t mod8 = 0xffU; 18 | static const uint16_t mod16 = 0xffffU; 19 | static const uint32_t mod32 = 0xffffffffUL; 20 | static const uint64_t mod64 = 0xffffffffffffffffULL; 21 | 22 | // string to integer 23 | uint64_t toint(const std::string & s, const int & base = 10); 24 | 25 | // flip the order of the octets 26 | // base = 2 for binary source 27 | // 16 for hex source 28 | // 256 for ASCII source 29 | std::string little_end(const std::string & str, const unsigned int & base = 16); 30 | 31 | // Changes a numeric value into its binary string 32 | template std::string makebin(T value, unsigned int size = 8 * sizeof(T)){ 33 | std::string out(size, '0'); 34 | if (!size){ 35 | out = ""; 36 | while (value){ 37 | out = "01"[value & 1] + out; 38 | value >>= 1; 39 | } 40 | return out; 41 | } 42 | while (value && size){ 43 | out[--size] = "01"[value & 1]; 44 | value >>= 1; 45 | } 46 | return out; 47 | } 48 | 49 | // Thanks to Ben Voigt @ stackoverflow for the makehex function 50 | // which I then adapted to makebin 51 | // Changes a numeric value to its hexadecimal string 52 | template std::string makehex(T value, unsigned int size = 2 * sizeof(T), bool caps = false){ 53 | if (!size){ 54 | std::stringstream out; 55 | out << std::hex << value; 56 | return out.str(); 57 | } 58 | 59 | std::string out(size, '0'); 60 | while (value && size){ 61 | if (caps){ 62 | out[--size] = "0123456789ABCDEF"[value & 15]; 63 | } 64 | else{ 65 | out[--size] = "0123456789abcdef"[value & 15]; 66 | } 67 | value >>= 4; 68 | } 69 | return out; 70 | } 71 | 72 | // extract 8 bits from a numeric value 73 | template uint8_t byte(const T & value, const uint16_t & n){ 74 | return (value >> (n << 3)) & 0xff; 75 | } 76 | 77 | // direct binary to hex string 78 | std::string bintohex(const std::string & in, bool caps = false); 79 | 80 | // convert an ASCII string into a string of 0s and 1s 81 | std::string binify(const std::string & in, unsigned int size = 0); 82 | 83 | // character to string of 0s and 1s 84 | std::string binify(unsigned char c); 85 | 86 | // string of 0s and 1s to ASCII 87 | std::string unbinify(const std::string & in); 88 | 89 | // ASCII string to hex string 90 | std::string hexlify(const std::string & in, bool caps = false); 91 | 92 | // character to hex string 93 | std::string hexlify(const char in, bool caps = false); 94 | 95 | // hex string to ASCII string 96 | std::string unhexlify(const std::string & in); 97 | 98 | std::string pkcs5(const std::string & data, const unsigned int & blocksize); 99 | 100 | std::string remove_pkcs5(std::string data); 101 | 102 | // adds characters to the front of the string 103 | std::string zfill(const std::string & str, const unsigned int & n, const char fill); 104 | 105 | // Left rotate a string 106 | std::string ROL(const std::string & str, const std::size_t bits); 107 | 108 | // and two strings, up to the last character of the shorter string 109 | std::string and_strings(const std::string & str1, const std::string & str2); 110 | 111 | // or two strings, up to the last character of the shorter string 112 | std::string or_strings(const std::string & str1, const std::string & str2); 113 | 114 | // xor the contents of 2 strings, up to the last character of the shorter string 115 | std::string xor_strings(const std::string & str1, const std::string & str2); 116 | 117 | #endif 118 | -------------------------------------------------------------------------------- /modes/CBC.cpp: -------------------------------------------------------------------------------- 1 | #include "CBC.h" 2 | 3 | CBC::CBC(SymAlg * instance, const std::string & iv) 4 | : algo(instance) 5 | { 6 | blocksize = algo -> blocksize() >> 3; 7 | const_IV = iv; 8 | if (const_IV == ""){ 9 | const_IV = std::string(blocksize, 0); 10 | } 11 | } 12 | 13 | std::string CBC::encrypt(const std::string & data){ 14 | const std::string temp = pkcs5(data, blocksize); 15 | std::string out = ""; 16 | std::string IV = const_IV; 17 | for(std::string::size_type x = 0; x < temp.size(); x += blocksize){ 18 | IV = algo -> encrypt(xor_strings(temp.substr(x, blocksize), IV)); 19 | out += IV; 20 | } 21 | return out; 22 | } 23 | 24 | std::string CBC::decrypt(const std::string & data){ 25 | std::string out = ""; 26 | std::string IV = const_IV; 27 | for(std::string::size_type x = 0; x < data.size(); x += blocksize){ 28 | out += xor_strings(algo -> decrypt(data.substr(x, blocksize)), IV); 29 | IV = data.substr(x, blocksize); 30 | } 31 | return remove_pkcs5(out); 32 | } 33 | -------------------------------------------------------------------------------- /modes/CBC.h: -------------------------------------------------------------------------------- 1 | /* 2 | CBC.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | // Cipher Block Chaining 26 | #ifndef __CBC__ 27 | #define __CBC__ 28 | 29 | #include "../common/includes.h" 30 | #include "../Encryptions/SymAlg.h" 31 | 32 | class CBC{ 33 | private: 34 | SymAlg * algo; 35 | std::string const_IV; 36 | uint8_t blocksize; 37 | 38 | public: 39 | CBC(SymAlg * instance, const std::string & iv = ""); 40 | std::string encrypt(const std::string & data); 41 | std::string decrypt(const std::string & data); 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /modes/CFB.cpp: -------------------------------------------------------------------------------- 1 | #include "CFB.h" 2 | 3 | CFB::CFB(SymAlg * instance, const std::string & iv) 4 | : algo(instance) 5 | { 6 | blocksize = algo -> blocksize() >> 3; 7 | const_IV = iv; 8 | if (const_IV == ""){ 9 | const_IV = std::string(blocksize, 0); 10 | } 11 | } 12 | 13 | std::string CFB::encrypt(const std::string & data){ 14 | const std::string temp = pkcs5(data, blocksize); 15 | std::string out = ""; 16 | std::string IV = const_IV; 17 | for(std::string::size_type x = 0; x < temp.size(); x += blocksize){ 18 | IV = xor_strings(algo -> encrypt(IV), temp.substr(x, blocksize)); 19 | out += IV; 20 | } 21 | return out; 22 | } 23 | 24 | std::string CFB::decrypt(const std::string & data){ 25 | std::string out = ""; 26 | std::string IV = const_IV; 27 | for(std::string::size_type x = 0; x < data.size(); x += blocksize){ 28 | out += xor_strings(algo -> encrypt(IV), data.substr(x, blocksize)); 29 | IV = data.substr(x, blocksize); 30 | } 31 | return remove_pkcs5(out); 32 | } 33 | -------------------------------------------------------------------------------- /modes/CFB.h: -------------------------------------------------------------------------------- 1 | /* 2 | CFB.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | // Cipher Feedback 26 | #ifndef __CFB__ 27 | #define __CFB__ 28 | 29 | #include "../common/includes.h" 30 | #include "../Encryptions/SymAlg.h" 31 | 32 | class CFB{ 33 | private: 34 | SymAlg * algo; 35 | std::string const_IV; 36 | uint8_t blocksize; 37 | 38 | public: 39 | CFB(SymAlg * instance, const std::string & iv = ""); 40 | std::string encrypt(const std::string & data); 41 | std::string decrypt(const std::string & data); 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /modes/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11) 2 | 3 | set(MODE_FILES 4 | CBC.cpp CBC.h 5 | CFB.cpp CFB.h 6 | CTR.cpp CTR.h 7 | ECB.cpp ECB.h 8 | OFB.cpp OFB.h 9 | PCPB.cpp PCPB.h 10 | ) 11 | 12 | foreach(FILE ${COMMON_FILES}) 13 | target_sources(Encryptions_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}) 14 | 15 | get_filename_component(EXT "${FILE}" EXT) 16 | if (EXT STREQUAL ".h") 17 | INSTALL(FILES ${FILE} 18 | DESTINATION include/modes) 19 | endif() 20 | endforeach() 21 | 22 | if (BUILD_SHARED_LIB) 23 | foreach(FILE ${COMMON_FILES}) 24 | target_sources(Encryptions_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${FILE}) 25 | endforeach() 26 | endif() 27 | -------------------------------------------------------------------------------- /modes/CTR.cpp: -------------------------------------------------------------------------------- 1 | #include "CTR.h" 2 | 3 | std::string & CTR::increment_IV(std::string & IV){ 4 | std::string::size_type i = IV.size(); 5 | while ((i > 0) && ((uint8_t) IV[i - 1] == (uint8_t) 0xff)){ 6 | IV[i - 1] = 0; 7 | i--; 8 | } 9 | 10 | if (i){ 11 | IV[i - 1]++; 12 | } 13 | 14 | return IV; 15 | } 16 | 17 | CTR::CTR(SymAlg * instance, const std::string & iv) 18 | : algo(instance) 19 | { 20 | blocksize = algo -> blocksize() >> 3; 21 | const_IV = iv; 22 | if (const_IV == ""){ 23 | const_IV = std::string(blocksize, 0); 24 | } 25 | } 26 | 27 | std::string CTR::encrypt(const std::string & data){ 28 | const std::string temp = pkcs5(data, blocksize); 29 | std::string out = ""; 30 | std::string IV = const_IV; 31 | for(std::string::size_type x = 0; x < temp.size(); x += blocksize){ 32 | out += xor_strings(algo -> encrypt(IV), temp.substr(x, blocksize)); 33 | increment_IV(IV); 34 | } 35 | return out; 36 | } 37 | 38 | std::string CTR::decrypt(const std::string & data){ 39 | std::string out = ""; 40 | std::string IV = const_IV; 41 | for(std::string::size_type x = 0; x < data.size(); x += blocksize){ 42 | out += xor_strings(algo -> encrypt(IV), data.substr(x, blocksize)); 43 | increment_IV(IV); 44 | } 45 | return remove_pkcs5(out); 46 | } 47 | -------------------------------------------------------------------------------- /modes/CTR.h: -------------------------------------------------------------------------------- 1 | /* 2 | CTR.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | // Counter 26 | #ifndef __CTR__ 27 | #define __CTR__ 28 | 29 | #include "../common/includes.h" 30 | #include "../Encryptions/SymAlg.h" 31 | 32 | class CTR{ 33 | private: 34 | SymAlg * algo; 35 | std::string const_IV; 36 | uint8_t blocksize; 37 | 38 | std::string & increment_IV(std::string & IV); 39 | 40 | public: 41 | CTR(SymAlg * instance, const std::string & iv = ""); 42 | std::string encrypt(const std::string & data); 43 | std::string decrypt(const std::string & data); 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /modes/ECB.cpp: -------------------------------------------------------------------------------- 1 | #include "ECB.h" 2 | 3 | ECB::ECB(SymAlg * instance, const std::string & iv) 4 | : algo(instance) 5 | { 6 | blocksize = algo -> blocksize() >> 3; 7 | } 8 | 9 | std::string ECB::encrypt(const std::string & data){ 10 | std::string temp = pkcs5(data, blocksize); 11 | std::string out = ""; 12 | for(std::string::size_type x = 0; x < temp.size(); x += blocksize){ 13 | out += algo -> encrypt(data.substr(x, blocksize)); 14 | } 15 | return out; 16 | } 17 | 18 | std::string ECB::decrypt(const std::string & data){ 19 | std::string out = ""; 20 | for(std::string::size_type x = 0; x < data.size(); x += blocksize){ 21 | out += algo -> decrypt(data.substr(x, blocksize)); 22 | } 23 | return remove_pkcs5(out); 24 | } 25 | -------------------------------------------------------------------------------- /modes/ECB.h: -------------------------------------------------------------------------------- 1 | /* 2 | ECB.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | // Electronic Codebook 26 | #ifndef __ECB__ 27 | #define __ECB__ 28 | 29 | #include "../common/includes.h" 30 | #include "../Encryptions/SymAlg.h" 31 | 32 | class ECB{ 33 | private: 34 | SymAlg * algo; 35 | uint8_t blocksize; 36 | 37 | public: 38 | ECB(SymAlg * instance, const std::string & iv = ""); 39 | std::string encrypt(const std::string & data); 40 | std::string decrypt(const std::string & data); 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /modes/OFB.cpp: -------------------------------------------------------------------------------- 1 | #include "OFB.h" 2 | 3 | OFB::OFB(SymAlg * instance, const std::string & iv) 4 | : algo(instance) 5 | { 6 | blocksize = algo -> blocksize() >> 3; 7 | const_IV = iv; 8 | if (const_IV == ""){ 9 | const_IV = std::string(blocksize, 0); 10 | } 11 | } 12 | 13 | std::string OFB::encrypt(const std::string & data){ 14 | const std::string temp = pkcs5(data, blocksize); 15 | std::string out = ""; 16 | std::string IV = const_IV; 17 | for(std::string::size_type x = 0; x < temp.size(); x += blocksize){ 18 | IV = algo -> encrypt(IV); 19 | out += xor_strings(IV, temp.substr(x, blocksize)); 20 | } 21 | return out; 22 | } 23 | 24 | std::string OFB::decrypt(const std::string & data){ 25 | std::string out = ""; 26 | std::string IV = const_IV; 27 | for(std::string::size_type x = 0; x < data.size(); x += blocksize){ 28 | IV = algo -> encrypt(IV); 29 | out += xor_strings(IV, data.substr(x, blocksize)); 30 | } 31 | return out; 32 | } 33 | -------------------------------------------------------------------------------- /modes/OFB.h: -------------------------------------------------------------------------------- 1 | /* 2 | OFB.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | // Output Feedback 26 | #ifndef __OFB__ 27 | #define __OFB__ 28 | 29 | #include "../common/includes.h" 30 | #include "../Encryptions/SymAlg.h" 31 | 32 | class OFB{ 33 | private: 34 | SymAlg * algo; 35 | std::string const_IV; 36 | uint8_t blocksize; 37 | 38 | public: 39 | OFB(SymAlg * instance, const std::string & iv = ""); 40 | std::string encrypt(const std::string & data); 41 | std::string decrypt(const std::string & data); 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /modes/PCPB.cpp: -------------------------------------------------------------------------------- 1 | #include "PCPB.h" 2 | 3 | PCPB::PCPB(SymAlg * instance, const std::string & iv) 4 | : algo(instance) 5 | { 6 | blocksize = algo -> blocksize() >> 3; 7 | const_IV = iv; 8 | if (const_IV == ""){ 9 | const_IV = std::string(blocksize, 0); 10 | } 11 | } 12 | 13 | std::string PCPB::encrypt(const std::string & data){ 14 | std::string temp = pkcs5(data, blocksize); 15 | std::string out = ""; 16 | std::string IV = const_IV; 17 | for(std::string::size_type x = 0; x < temp.size(); x += blocksize){ 18 | const std::string block = temp.substr(x, blocksize); 19 | out += algo -> encrypt(xor_strings(block, IV)); 20 | IV = xor_strings(out.substr(out.size() - blocksize, blocksize), block); 21 | } 22 | return out; 23 | } 24 | 25 | std::string PCPB::decrypt(const std::string & data){ 26 | std::string out = ""; 27 | std::string IV = const_IV; 28 | for(std::string::size_type x = 0; x < data.size(); x += blocksize){ 29 | const std::string block = data.substr(x, blocksize); 30 | out += xor_strings(algo -> decrypt(block), IV); 31 | IV = xor_strings(out.substr(out.size() - blocksize, blocksize), block); 32 | } 33 | return remove_pkcs5(out); 34 | } 35 | -------------------------------------------------------------------------------- /modes/PCPB.h: -------------------------------------------------------------------------------- 1 | /* 2 | PCPB.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | // Propagating Cipher - Block Chaining 26 | #ifndef __PCPB__ 27 | #define __PCPB__ 28 | 29 | #include "../common/includes.h" 30 | #include "../Encryptions/SymAlg.h" 31 | 32 | class PCPB{ 33 | private: 34 | SymAlg * algo; 35 | std::string const_IV; 36 | uint8_t blocksize; 37 | 38 | public: 39 | PCPB(SymAlg * instance, const std::string & iv = ""); 40 | std::string encrypt(const std::string & data); 41 | std::string decrypt(const std::string & data); 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.11) 2 | 3 | set(TESTS 4 | aes.cpp 5 | blowfish.cpp 6 | camellia.cpp 7 | cast128.cpp 8 | cast256.cpp 9 | CMakeLists.txt 10 | des.cpp 11 | gost.cpp 12 | idea.cpp 13 | misty1.cpp 14 | rc2.cpp 15 | rc4.cpp 16 | rc5.cpp 17 | rc6.cpp 18 | saferk64.cpp 19 | seed.cpp 20 | skipjack.cpp 21 | tea.cpp 22 | testvectors 23 | tripledes.cpp 24 | twofish.cpp 25 | xtea.cpp 26 | ) 27 | 28 | find_package(Threads REQUIRED) 29 | find_package(GTest REQUIRED) 30 | 31 | add_executable(tests) 32 | target_include_directories(tests SYSTEM PRIVATE ${GTEST_INCLUDE_DIRS}) 33 | target_include_directories(tests PRIVATE ${CMAKE_SOURCE_DIR}) 34 | foreach(TEST ${TESTS}) 35 | target_sources(tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/${TEST}) 36 | endforeach() 37 | target_link_libraries(tests ${GTEST_BOTH_LIBRARIES} Encryptions_static Threads::Threads) 38 | 39 | add_test(Name tests COMMAND tests) 40 | -------------------------------------------------------------------------------- /tests/aes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/AES.h" 4 | 5 | #include "testvectors/aes/gfsbox128.h" 6 | #include "testvectors/aes/sbox128.h" 7 | #include "testvectors/aes/varkey128.h" 8 | #include "testvectors/aes/vartxt128.h" 9 | 10 | TEST(AES, 128_gfsbox) { 11 | sym_test (AES128_GFSBOX); 12 | } 13 | 14 | TEST(AES, 128_sbox) { 15 | sym_test (AES128_SBOX); 16 | } 17 | 18 | TEST(AES, 128_varkey) { 19 | sym_test (AES128_VARKEY); 20 | } 21 | 22 | TEST(AES, 128_vartxt) { 23 | sym_test (AES128_VARTXT); 24 | } 25 | 26 | #include "testvectors/aes/gfsbox192.h" 27 | #include "testvectors/aes/sbox192.h" 28 | #include "testvectors/aes/varkey192.h" 29 | #include "testvectors/aes/vartxt192.h" 30 | 31 | TEST(AES, 192_gfsbox) { 32 | sym_test (AES192_GFSBOX); 33 | } 34 | 35 | TEST(AES, 192_sbox) { 36 | sym_test (AES192_SBOX); 37 | } 38 | 39 | TEST(AES, 192_varkey) { 40 | sym_test (AES192_VARKEY); 41 | } 42 | 43 | TEST(AES, 192_vartxt) { 44 | sym_test (AES192_VARTXT); 45 | } 46 | 47 | 48 | #include "testvectors/aes/gfsbox256.h" 49 | #include "testvectors/aes/sbox256.h" 50 | #include "testvectors/aes/varkey256.h" 51 | #include "testvectors/aes/vartxt256.h" 52 | 53 | TEST(AES, 256_gfsbox) { 54 | sym_test (AES256_GFSBOX); 55 | } 56 | 57 | TEST(AES, 256_sbox) { 58 | sym_test (AES256_SBOX); 59 | } 60 | 61 | TEST(AES, 256_varkey) { 62 | sym_test (AES256_VARKEY); 63 | } 64 | 65 | TEST(AES, 256_vartxt) { 66 | sym_test (AES256_VARTXT); 67 | } 68 | -------------------------------------------------------------------------------- /tests/blowfish.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/Blowfish.h" 4 | 5 | #include "testvectors/blowfish/schneier.h" 6 | 7 | TEST(Blowfish, 64) { 8 | sym_test (BLOWFISH_SCHNEIER); 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /tests/camellia.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/Camellia.h" 4 | 5 | #include "testvectors/camellia/ntt.h" 6 | 7 | // don't use sym_test; too much swapping 8 | 9 | TEST(Camellia, 128) { 10 | ASSERT_EQ(CAMELLIA128_KEY.size(), CAMELLIA128_CIPHER.size()); 11 | 12 | for ( unsigned int i = 0; i < CAMELLIA128_KEY.size(); ++i ) { 13 | ASSERT_EQ(CAMELLIA_PLAIN.size(), CAMELLIA128_CIPHER[i].size()); 14 | 15 | auto camellia128 = Camellia(unhexlify(CAMELLIA128_KEY[i])); 16 | for ( unsigned int x = 0; x < CAMELLIA_PLAIN.size(); ++x ) { 17 | EXPECT_EQ(hexlify(camellia128.encrypt(unhexlify(CAMELLIA_PLAIN[x]))), CAMELLIA128_CIPHER[i][x]); 18 | EXPECT_EQ(hexlify(camellia128.decrypt(unhexlify(CAMELLIA128_CIPHER[i][x]))), CAMELLIA_PLAIN[x]); 19 | } 20 | } 21 | } 22 | 23 | TEST(Camellia, 192) { 24 | ASSERT_EQ(CAMELLIA192_KEY.size(), CAMELLIA192_CIPHER.size()); 25 | 26 | for ( unsigned int i = 0; i < CAMELLIA192_KEY.size(); ++i ) { 27 | 28 | ASSERT_EQ(CAMELLIA_PLAIN.size(), CAMELLIA192_CIPHER[i].size()); 29 | 30 | auto camellia192 = Camellia(unhexlify(CAMELLIA192_KEY[i])); 31 | for ( unsigned int x = 0; x < CAMELLIA_PLAIN.size(); ++x ) { 32 | EXPECT_EQ(hexlify(camellia192.encrypt(unhexlify(CAMELLIA_PLAIN[x]))), CAMELLIA192_CIPHER[i][x]); 33 | EXPECT_EQ(hexlify(camellia192.decrypt(unhexlify(CAMELLIA192_CIPHER[i][x]))), CAMELLIA_PLAIN[x]); 34 | } 35 | } 36 | } 37 | 38 | TEST(Camellia, 256) { 39 | ASSERT_EQ(CAMELLIA256_KEY.size(), CAMELLIA256_CIPHER.size()); 40 | 41 | for ( unsigned int i = 0; i < CAMELLIA256_KEY.size(); ++i ) { 42 | 43 | ASSERT_EQ(CAMELLIA_PLAIN.size(), CAMELLIA256_CIPHER[i].size()); 44 | 45 | auto camellia256 = Camellia(unhexlify(CAMELLIA256_KEY[i])); 46 | for ( unsigned int x = 0; x < CAMELLIA_PLAIN.size(); ++x ) { 47 | EXPECT_EQ(hexlify(camellia256.encrypt(unhexlify(CAMELLIA_PLAIN[x]))), CAMELLIA256_CIPHER[i][x]); 48 | EXPECT_EQ(hexlify(camellia256.decrypt(unhexlify(CAMELLIA256_CIPHER[i][x]))), CAMELLIA_PLAIN[x]); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/cast128.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/CAST128.h" 4 | 5 | #include "testvectors/cast/cast128rfc2144.h" 6 | 7 | TEST(CAST128, rfc2144) { 8 | sym_test (CAST128_RFC2144); 9 | } 10 | 11 | /* 12 | TEST(CAST128, cast128_maintenance_test) { 13 | 14 | // Test vector from 15 | std::string aL, aR, bL, bR; 16 | aL = bL = unhexlify("0123456712345678"); 17 | aR = bR = unhexlify("234567893456789a"); 18 | 19 | // 1,000,000 times 20 | for ( unsigned int i = 0; i < 1000000; ++i ) { 21 | auto enc1 = CAST128(bL+bR); 22 | aL = enc1.encrypt(aL); 23 | aR = enc1.encrypt(aR); 24 | auto enc2 = CAST128(aL+aR); 25 | bL = enc2.encrypt(bL); 26 | bR = enc2.encrypt(bR); 27 | } 28 | 29 | EXPECT_EQ(aL+aR, unhexlify("eea9d0a249fd3ba6b3436fb89d6dca92")); 30 | EXPECT_EQ(bL+bR, unhexlify("b2c95eb00c31ad7180ac05b8e83d696e")); 31 | 32 | } 33 | */ 34 | -------------------------------------------------------------------------------- /tests/cast256.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/CAST256.h" 4 | 5 | #include "testvectors/cast/cast256rfc2612.h" 6 | 7 | TEST(CAST256, rfc2612) { 8 | sym_test (CAST256_RFC2612); 9 | } 10 | -------------------------------------------------------------------------------- /tests/des.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/DES.h" 4 | 5 | #include "testvectors/des/nessieset1.h" 6 | #include "testvectors/des/nessieset2.h" 7 | #include "testvectors/des/nessieset3.h" 8 | #include "testvectors/des/nessieset4.h" 9 | 10 | TEST(DES, set1) { 11 | sym_test (DES_NESSIE_SET_1); 12 | } 13 | 14 | TEST(DES, set2) { 15 | sym_test (DES_NESSIE_SET_2); 16 | } 17 | 18 | TEST(DES, set3) { 19 | sym_test (DES_NESSIE_SET_3); 20 | } 21 | 22 | TEST(DES, set4) { 23 | sym_test (DES_NESSIE_SET_4); 24 | } 25 | -------------------------------------------------------------------------------- /tests/gost.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/GOST.h" 4 | 5 | #include "testvectors/gost/testvectors.h" 6 | 7 | TEST(GOST, 28147_89) { 8 | for(PlainKeyCipher const & pkc : GOST_TEST_VECTORS){ 9 | std::string plain, key, cipher; 10 | std::tie(plain, key, cipher) = pkc; 11 | auto gost = GOST(unhexlify(key), DES_sbox); 12 | EXPECT_EQ(gost.encrypt(unhexlify(plain)), unhexlify(cipher)); 13 | EXPECT_EQ(gost.decrypt(unhexlify(cipher)), unhexlify(plain)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/idea.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/IDEA.h" 4 | 5 | #include "testvectors/idea/nessieset1.h" 6 | #include "testvectors/idea/nessieset2.h" 7 | #include "testvectors/idea/nessieset3.h" 8 | #include "testvectors/idea/nessieset4.h" 9 | #include "testvectors/idea/nessieset5.h" 10 | #include "testvectors/idea/nessieset6.h" 11 | #include "testvectors/idea/nessieset7.h" 12 | #include "testvectors/idea/nessieset8.h" 13 | 14 | TEST(IDEA, set1) { 15 | sym_test (IDEA_NESSIE_SET_1); 16 | } 17 | 18 | TEST(IDEA, set2) { 19 | sym_test (IDEA_NESSIE_SET_2); 20 | } 21 | 22 | TEST(IDEA, set3) { 23 | sym_test (IDEA_NESSIE_SET_3); 24 | } 25 | 26 | TEST(IDEA, set4) { 27 | sym_test (IDEA_NESSIE_SET_4); 28 | } 29 | 30 | TEST(IDEA, set5) { 31 | sym_test (IDEA_NESSIE_SET_5); 32 | } 33 | 34 | TEST(IDEA, set6) { 35 | sym_test (IDEA_NESSIE_SET_6); 36 | } 37 | 38 | TEST(IDEA, set7) { 39 | sym_test (IDEA_NESSIE_SET_7); 40 | } 41 | 42 | TEST(IDEA, set8) { 43 | sym_test (IDEA_NESSIE_SET_8); 44 | } 45 | -------------------------------------------------------------------------------- /tests/misty1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/MISTY1.h" 4 | 5 | #include "testvectors/misty1/nessieset1.h" 6 | #include "testvectors/misty1/nessieset2.h" 7 | #include "testvectors/misty1/nessieset3.h" 8 | #include "testvectors/misty1/nessieset4.h" 9 | 10 | TEST(MISTY1, set1) { 11 | sym_test (MISTY1_NESSIE_SET_1); 12 | } 13 | 14 | TEST(MISTY1, set2) { 15 | sym_test (MISTY1_NESSIE_SET_2); 16 | } 17 | 18 | TEST(MISTY1, set3) { 19 | sym_test (MISTY1_NESSIE_SET_3); 20 | } 21 | 22 | TEST(MISTY1, set4) { 23 | sym_test (MISTY1_NESSIE_SET_4); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /tests/rc2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "common/includes.h" 4 | #include "Encryptions/RC2.h" 5 | 6 | #include "testvectors/rc/rc2rfc2268.h" 7 | 8 | TEST(RC2, rfc2268) { 9 | sym_test (RC2_RFC2268); 10 | } 11 | -------------------------------------------------------------------------------- /tests/rc4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "common/includes.h" 4 | #include "Encryptions/RC4.h" 5 | 6 | #include "testvectors/rc/rc4scicrypt.h" 7 | 8 | TEST(RC4, sci_crypt) { 9 | sym_test (RC4_SCI_CRYPT); 10 | } 11 | -------------------------------------------------------------------------------- /tests/rc5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/RC5.h" 4 | 5 | #include "testvectors/rc/rc5paper.h" 6 | #include "testvectors/rc/rc5nessieset1.h" 7 | #include "testvectors/rc/rc5nessieset2.h" 8 | #include "testvectors/rc/rc5nessieset3.h" 9 | #include "testvectors/rc/rc5nessieset4.h" 10 | 11 | TEST(RC5, paper) { 12 | sym_test (RC5_PAPER); 13 | } 14 | 15 | TEST(RC5, set1) { 16 | sym_test (RC5_NESSIE_SET_1); 17 | } 18 | 19 | TEST(RC5, set2) { 20 | sym_test (RC5_NESSIE_SET_2); 21 | } 22 | 23 | TEST(RC5, set3) { 24 | sym_test (RC5_NESSIE_SET_3); 25 | } 26 | 27 | TEST(RC5, set4) { 28 | sym_test (RC5_NESSIE_SET_4); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /tests/rc6.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/RC6.h" 4 | 5 | #include "testvectors/rc/rc6nessieset1.h" 6 | #include "testvectors/rc/rc6nessieset2.h" 7 | #include "testvectors/rc/rc6nessieset3.h" 8 | #include "testvectors/rc/rc6nessieset4.h" 9 | 10 | TEST(RC6, set1) { 11 | sym_test (RC6_NESSIE_SET_1); 12 | } 13 | 14 | TEST(RC6, set2) { 15 | sym_test (RC6_NESSIE_SET_2); 16 | } 17 | 18 | TEST(RC6, set3) { 19 | sym_test (RC6_NESSIE_SET_3); 20 | } 21 | 22 | TEST(RC6, set4) { 23 | sym_test (RC6_NESSIE_SET_4); 24 | } 25 | -------------------------------------------------------------------------------- /tests/saferk64.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "common/includes.h" 4 | #include "Encryptions/SAFERK64.h" 5 | 6 | #include "testvectors/safer/saferk64paper.h" 7 | 8 | TEST(SAFERK64, 6_rounds) { 9 | for(PlainKeyCipher const & pkc : SAFERK64_PAPER){ 10 | std::string plain, key, cipher; 11 | std::tie(plain, key, cipher) = pkc; 12 | auto saferk64 = SAFERK64(unhexlify(key), 6); 13 | EXPECT_EQ(saferk64.encrypt(unhexlify(plain)), unhexlify(cipher)); 14 | EXPECT_EQ(saferk64.decrypt(unhexlify(cipher)), unhexlify(plain)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/seed.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/SEED.h" 4 | 5 | #include "testvectors/seed/nessieset1.h" 6 | #include "testvectors/seed/nessieset2.h" 7 | #include "testvectors/seed/nessieset3.h" 8 | #include "testvectors/seed/nessieset4.h" 9 | 10 | TEST(SEED, set1) { 11 | sym_test (SEED_NESSIE_SET_1); 12 | } 13 | 14 | TEST(SEED, set2) { 15 | sym_test (SEED_NESSIE_SET_2); 16 | } 17 | 18 | TEST(SEED, set3) { 19 | sym_test (SEED_NESSIE_SET_3); 20 | } 21 | 22 | TEST(SEED, set4) { 23 | sym_test (SEED_NESSIE_SET_4); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /tests/skipjack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/Skipjack.h" 4 | 5 | #include "testvectors/skipjack/nist.h" 6 | #include "testvectors/skipjack/scicrypt.h" 7 | 8 | TEST(Skipjack, NIST) { 9 | sym_test (SKIPJACK_NIST); 10 | } 11 | 12 | TEST(Skipjack, sci_crypt) { 13 | sym_test (SKIPJACK_SCI_CRYPT); 14 | } 15 | -------------------------------------------------------------------------------- /tests/tea.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/TEA.h" 4 | 5 | #include "testvectors/tea/edipermadi.h" 6 | 7 | TEST(TEA, edipermadi) { 8 | sym_test (TEA_EDIPERMADI); 9 | } 10 | -------------------------------------------------------------------------------- /tests/testvectors/aes/gfsbox128.h: -------------------------------------------------------------------------------- 1 | #ifndef _GFSBOX128__ 2 | #define _GFSBOX128__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | const std::vector AES128_GFSBOX = { 9 | std::make_tuple("f34481ec3cc627bacd5dc3fb08f273e6", "00000000000000000000000000000000", "0336763e966d92595a567cc9ce537f5e"), 10 | std::make_tuple("9798c4640bad75c7c3227db910174e72", "00000000000000000000000000000000", "a9a1631bf4996954ebc093957b234589"), 11 | std::make_tuple("96ab5c2ff612d9dfaae8c31f30c42168", "00000000000000000000000000000000", "ff4f8391a6a40ca5b25d23bedd44a597"), 12 | std::make_tuple("6a118a874519e64e9963798a503f1d35", "00000000000000000000000000000000", "dc43be40be0e53712f7e2bf5ca707209"), 13 | std::make_tuple("cb9fceec81286ca3e989bd979b0cb284", "00000000000000000000000000000000", "92beedab1895a94faa69b632e5cc47ce"), 14 | std::make_tuple("b26aeb1874e47ca8358ff22378f09144", "00000000000000000000000000000000", "459264f4798f6a78bacb89c15ed3d601"), 15 | std::make_tuple("58c8e00b2631686d54eab84b91f0aca1", "00000000000000000000000000000000", "08a4e2efec8a8e3312ca7460b9040bbf"), 16 | }; 17 | 18 | #endif // _GFSBOX128__ 19 | -------------------------------------------------------------------------------- /tests/testvectors/aes/gfsbox192.h: -------------------------------------------------------------------------------- 1 | #ifndef __AESECBGFSBOX192__ 2 | #define __AESECBGFSBOX192__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | const std::vector AES192_GFSBOX = { 8 | std::make_tuple("1b077a6af4b7f98229de786d7516b639", "000000000000000000000000000000000000000000000000", "275cfc0413d8ccb70513c3859b1d0f72"), 9 | std::make_tuple("9c2d8842e5f48f57648205d39a239af1", "000000000000000000000000000000000000000000000000", "c9b8135ff1b5adc413dfd053b21bd96d"), 10 | std::make_tuple("bff52510095f518ecca60af4205444bb", "000000000000000000000000000000000000000000000000", "4a3650c3371ce2eb35e389a171427440"), 11 | std::make_tuple("51719783d3185a535bd75adc65071ce1", "000000000000000000000000000000000000000000000000", "4f354592ff7c8847d2d0870ca9481b7c"), 12 | std::make_tuple("26aa49dcfe7629a8901a69a9914e6dfd", "000000000000000000000000000000000000000000000000", "d5e08bf9a182e857cf40b3a36ee248cc"), 13 | std::make_tuple("941a4773058224e1ef66d10e0a6ee782", "000000000000000000000000000000000000000000000000", "067cd9d3749207791841562507fa9626"), 14 | }; 15 | 16 | #endif // __AESECBGFSBOX192__ 17 | -------------------------------------------------------------------------------- /tests/testvectors/aes/gfsbox256.h: -------------------------------------------------------------------------------- 1 | #ifndef __AESECBGFSBOX256__ 2 | #define __AESECBGFSBOX256__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | const std::vector AES256_GFSBOX = { 9 | std::make_tuple("014730f80ac625fe84f026c60bfd547d", "0000000000000000000000000000000000000000000000000000000000000000", "5c9d844ed46f9885085e5d6a4f94c7d7"), 10 | std::make_tuple("0b24af36193ce4665f2825d7b4749c98", "0000000000000000000000000000000000000000000000000000000000000000", "a9ff75bd7cf6613d3731c77c3b6d0c04"), 11 | std::make_tuple("761c1fe41a18acf20d241650611d90f1", "0000000000000000000000000000000000000000000000000000000000000000", "623a52fcea5d443e48d9181ab32c7421"), 12 | std::make_tuple("8a560769d605868ad80d819bdba03771", "0000000000000000000000000000000000000000000000000000000000000000", "38f2c7ae10612415d27ca190d27da8b4"), 13 | std::make_tuple("91fbef2d15a97816060bee1feaa49afe", "0000000000000000000000000000000000000000000000000000000000000000", "1bc704f1bce135ceb810341b216d7abe"), 14 | }; 15 | 16 | #endif // __AESECBGFSBOX256__ 17 | -------------------------------------------------------------------------------- /tests/testvectors/aes/sbox128.h: -------------------------------------------------------------------------------- 1 | #ifndef __AESECBSBOX128__ 2 | #define __AESECBSBOX128__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | const std::vector AES128_SBOX = { 9 | std::make_tuple("00000000000000000000000000000000", "10a58869d74be5a374cf867cfb473859", "6d251e6944b051e04eaa6fb4dbf78465"), 10 | std::make_tuple("00000000000000000000000000000000", "caea65cdbb75e9169ecd22ebe6e54675", "6e29201190152df4ee058139def610bb"), 11 | std::make_tuple("00000000000000000000000000000000", "a2e2fa9baf7d20822ca9f0542f764a41", "c3b44b95d9d2f25670eee9a0de099fa3"), 12 | std::make_tuple("00000000000000000000000000000000", "b6364ac4e1de1e285eaf144a2415f7a0", "5d9b05578fc944b3cf1ccf0e746cd581"), 13 | std::make_tuple("00000000000000000000000000000000", "64cf9c7abc50b888af65f49d521944b2", "f7efc89d5dba578104016ce5ad659c05"), 14 | std::make_tuple("00000000000000000000000000000000", "47d6742eefcc0465dc96355e851b64d9", "0306194f666d183624aa230a8b264ae7"), 15 | std::make_tuple("00000000000000000000000000000000", "3eb39790678c56bee34bbcdeccf6cdb5", "858075d536d79ccee571f7d7204b1f67"), 16 | std::make_tuple("00000000000000000000000000000000", "64110a924f0743d500ccadae72c13427", "35870c6a57e9e92314bcb8087cde72ce"), 17 | std::make_tuple("00000000000000000000000000000000", "18d8126516f8a12ab1a36d9f04d68e51", "6c68e9be5ec41e22c825b7c7affb4363"), 18 | std::make_tuple("00000000000000000000000000000000", "f530357968578480b398a3c251cd1093", "f5df39990fc688f1b07224cc03e86cea"), 19 | std::make_tuple("00000000000000000000000000000000", "da84367f325d42d601b4326964802e8e", "bba071bcb470f8f6586e5d3add18bc66"), 20 | std::make_tuple("00000000000000000000000000000000", "e37b1c6aa2846f6fdb413f238b089f23", "43c9f7e62f5d288bb27aa40ef8fe1ea8"), 21 | std::make_tuple("00000000000000000000000000000000", "6c002b682483e0cabcc731c253be5674", "3580d19cff44f1014a7c966a69059de5"), 22 | std::make_tuple("00000000000000000000000000000000", "143ae8ed6555aba96110ab58893a8ae1", "806da864dd29d48deafbe764f8202aef"), 23 | std::make_tuple("00000000000000000000000000000000", "b69418a85332240dc82492353956ae0c", "a303d940ded8f0baff6f75414cac5243"), 24 | std::make_tuple("00000000000000000000000000000000", "71b5c08a1993e1362e4d0ce9b22b78d5", "c2dabd117f8a3ecabfbb11d12194d9d0"), 25 | std::make_tuple("00000000000000000000000000000000", "e234cdca2606b81f29408d5f6da21206", "fff60a4740086b3b9c56195b98d91a7b"), 26 | std::make_tuple("00000000000000000000000000000000", "13237c49074a3da078dc1d828bb78c6f", "8146a08e2357f0caa30ca8c94d1a0544"), 27 | std::make_tuple("00000000000000000000000000000000", "3071a2a48fe6cbd04f1a129098e308f8", "4b98e06d356deb07ebb824e5713f7be3"), 28 | std::make_tuple("00000000000000000000000000000000", "90f42ec0f68385f2ffc5dfc03a654dce", "7a20a53d460fc9ce0423a7a0764c6cf2"), 29 | std::make_tuple("00000000000000000000000000000000", "febd9a24d8b65c1c787d50a4ed3619a9", "f4a70d8af877f9b02b4c40df57d45b17"), 30 | }; 31 | 32 | #endif // __AESECBSBOX128__ 33 | -------------------------------------------------------------------------------- /tests/testvectors/aes/sbox192.h: -------------------------------------------------------------------------------- 1 | #ifndef __AESECBSBOX192__ 2 | #define __AESECBSBOX192__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | const std::vector AES192_SBOX = { 8 | std::make_tuple("00000000000000000000000000000000", "e9f065d7c13573587f7875357dfbb16c53489f6a4bd0f7cd", "0956259c9cd5cfd0181cca53380cde06"), 9 | std::make_tuple("00000000000000000000000000000000", "15d20f6ebc7e649fd95b76b107e6daba967c8a9484797f29", "8e4e18424e591a3d5b6f0876f16f8594"), 10 | std::make_tuple("00000000000000000000000000000000", "a8a282ee31c03fae4f8e9b8930d5473c2ed695a347e88b7c", "93f3270cfc877ef17e106ce938979cb0"), 11 | std::make_tuple("00000000000000000000000000000000", "cd62376d5ebb414917f0c78f05266433dc9192a1ec943300", "7f6c25ff41858561bb62f36492e93c29"), 12 | std::make_tuple("00000000000000000000000000000000", "502a6ab36984af268bf423c7f509205207fc1552af4a91e5", "8e06556dcbb00b809a025047cff2a940"), 13 | std::make_tuple("00000000000000000000000000000000", "25a39dbfd8034f71a81f9ceb55026e4037f8f6aa30ab44ce", "3608c344868e94555d23a120f8a5502d"), 14 | std::make_tuple("00000000000000000000000000000000", "e08c15411774ec4a908b64eadc6ac4199c7cd453f3aaef53", "77da2021935b840b7f5dcc39132da9e5"), 15 | std::make_tuple("00000000000000000000000000000000", "3b375a1ff7e8d44409696e6326ec9dec86138e2ae010b980", "3b7c24f825e3bf9873c9f14d39a0e6f4"), 16 | std::make_tuple("00000000000000000000000000000000", "950bb9f22cc35be6fe79f52c320af93dec5bc9c0c2f9cd53", "64ebf95686b353508c90ecd8b6134316"), 17 | std::make_tuple("00000000000000000000000000000000", "7001c487cc3e572cfc92f4d0e697d982e8856fdcc957da40", "ff558c5d27210b7929b73fc708eb4cf1"), 18 | std::make_tuple("00000000000000000000000000000000", "f029ce61d4e5a405b41ead0a883cc6a737da2cf50a6c92ae", "a2c3b2a818075490a7b4c14380f02702"), 19 | std::make_tuple("00000000000000000000000000000000", "61257134a518a0d57d9d244d45f6498cbc32f2bafc522d79", "cfe4d74002696ccf7d87b14a2f9cafc9"), 20 | std::make_tuple("00000000000000000000000000000000", "b0ab0a6a818baef2d11fa33eac947284fb7d748cfb75e570", "d2eafd86f63b109b91f5dbb3a3fb7e13"), 21 | std::make_tuple("00000000000000000000000000000000", "ee053aa011c8b428cdcc3636313c54d6a03cac01c71579d6", "9b9fdd1c5975655f539998b306a324af"), 22 | std::make_tuple("00000000000000000000000000000000", "d2926527e0aa9f37b45e2ec2ade5853ef807576104c7ace3", "dd619e1cf204446112e0af2b9afa8f8c"), 23 | std::make_tuple("00000000000000000000000000000000", "982215f4e173dfa0fcffe5d3da41c4812c7bcc8ed3540f93", "d4f0aae13c8fe9339fbf9e69ed0ad74d"), 24 | std::make_tuple("00000000000000000000000000000000", "98c6b8e01e379fbd14e61af6af891596583565f2a27d59e9", "19c80ec4a6deb7e5ed1033dda933498f"), 25 | std::make_tuple("00000000000000000000000000000000", "b3ad5cea1dddc214ca969ac35f37dae1a9a9d1528f89bb35", "3cf5e1d21a17956d1dffad6a7c41c659"), 26 | std::make_tuple("00000000000000000000000000000000", "45899367c3132849763073c435a9288a766c8b9ec2308516", "69fd12e8505f8ded2fdcb197a121b362"), 27 | std::make_tuple("00000000000000000000000000000000", "ec250e04c3903f602647b85a401a1ae7ca2f02f67fa4253e", "8aa584e2cc4d17417a97cb9a28ba29c8"), 28 | std::make_tuple("00000000000000000000000000000000", "d077a03bd8a38973928ccafe4a9d2f455130bd0af5ae46a9", "abc786fb1edb504580c4d882ef29a0c7"), 29 | std::make_tuple("00000000000000000000000000000000", "d184c36cf0dddfec39e654195006022237871a47c33d3198", "2e19fb60a3e1de0166f483c97824a978"), 30 | std::make_tuple("00000000000000000000000000000000", "4c6994ffa9dcdc805b60c2c0095334c42d95a8fc0ca5b080", "7656709538dd5fec41e0ce6a0f8e207d"), 31 | std::make_tuple("00000000000000000000000000000000", "c88f5b00a4ef9a6840e2acaf33f00a3bdc4e25895303fa72", "a67cf333b314d411d3c0ae6e1cfcd8f5"), 32 | }; 33 | 34 | #endif // __AESECBSBOX192__ 35 | -------------------------------------------------------------------------------- /tests/testvectors/aes/sbox256.h: -------------------------------------------------------------------------------- 1 | #ifndef __AESECBSBOX256__ 2 | #define __AESECBSBOX256__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | const std::vector AES256_SBOX = { 9 | std::make_tuple("00000000000000000000000000000000", "c47b0294dbbbee0fec4757f22ffeee3587ca4730c3d33b691df38bab076bc558", "46f2fb342d6f0ab477476fc501242c5f"), 10 | std::make_tuple("00000000000000000000000000000000", "28d46cffa158533194214a91e712fc2b45b518076675affd910edeca5f41ac64", "4bf3b0a69aeb6657794f2901b1440ad4"), 11 | std::make_tuple("00000000000000000000000000000000", "c1cc358b449909a19436cfbb3f852ef8bcb5ed12ac7058325f56e6099aab1a1c", "352065272169abf9856843927d0674fd"), 12 | std::make_tuple("00000000000000000000000000000000", "984ca75f4ee8d706f46c2d98c0bf4a45f5b00d791c2dfeb191b5ed8e420fd627", "4307456a9e67813b452e15fa8fffe398"), 13 | std::make_tuple("00000000000000000000000000000000", "b43d08a447ac8609baadae4ff12918b9f68fc1653f1269222f123981ded7a92f", "4663446607354989477a5c6f0f007ef4"), 14 | std::make_tuple("00000000000000000000000000000000", "1d85a181b54cde51f0e098095b2962fdc93b51fe9b88602b3f54130bf76a5bd9", "531c2c38344578b84d50b3c917bbb6e1"), 15 | std::make_tuple("00000000000000000000000000000000", "dc0eba1f2232a7879ded34ed8428eeb8769b056bbaf8ad77cb65c3541430b4cf", "fc6aec906323480005c58e7e1ab004ad"), 16 | std::make_tuple("00000000000000000000000000000000", "f8be9ba615c5a952cabbca24f68f8593039624d524c816acda2c9183bd917cb9", "a3944b95ca0b52043584ef02151926a8"), 17 | std::make_tuple("00000000000000000000000000000000", "797f8b3d176dac5b7e34a2d539c4ef367a16f8635f6264737591c5c07bf57a3e", "a74289fe73a4c123ca189ea1e1b49ad5"), 18 | std::make_tuple("00000000000000000000000000000000", "6838d40caf927749c13f0329d331f448e202c73ef52c5f73a37ca635d4c47707", "b91d4ea4488644b56cf0812fa7fcf5fc"), 19 | std::make_tuple("00000000000000000000000000000000", "ccd1bc3c659cd3c59bc437484e3c5c724441da8d6e90ce556cd57d0752663bbc", "304f81ab61a80c2e743b94d5002a126b"), 20 | std::make_tuple("00000000000000000000000000000000", "13428b5e4c005e0636dd338405d173ab135dec2a25c22c5df0722d69dcc43887", "649a71545378c783e368c9ade7114f6c"), 21 | std::make_tuple("00000000000000000000000000000000", "07eb03a08d291d1b07408bf3512ab40c91097ac77461aad4bb859647f74f00ee", "47cb030da2ab051dfc6c4bf6910d12bb"), 22 | std::make_tuple("00000000000000000000000000000000", "90143ae20cd78c5d8ebdd6cb9dc1762427a96c78c639bccc41a61424564eafe1", "798c7c005dee432b2c8ea5dfa381ecc3"), 23 | std::make_tuple("00000000000000000000000000000000", "b7a5794d52737475d53d5a377200849be0260a67a2b22ced8bbef12882270d07", "637c31dc2591a07636f646b72daabbe7"), 24 | std::make_tuple("00000000000000000000000000000000", "fca02f3d5011cfc5c1e23165d413a049d4526a991827424d896fe3435e0bf68e", "179a49c712154bbffbe6e7a84a18e220"), 25 | }; 26 | 27 | #endif // __AESECBSBOX256__ 28 | -------------------------------------------------------------------------------- /tests/testvectors/blowfish/schneier.h: -------------------------------------------------------------------------------- 1 | #ifndef __BLOWFISHTESTVECTORS__ 2 | #define __BLOWFISHTESTVECTORS__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | const std::vector BLOWFISH_SCHNEIER = { 9 | std::make_tuple("0000000000000000", "0000000000000000", "4ef997456198dd78"), 10 | std::make_tuple("ffffffffffffffff", "ffffffffffffffff", "51866fd5b85ecb8a"), 11 | std::make_tuple("1000000000000001", "3000000000000000", "7d856f9a613063f2"), 12 | std::make_tuple("1111111111111111", "1111111111111111", "2466dd878b963c9d"), 13 | std::make_tuple("1111111111111111", "0123456789abcdef", "61f9c3802281b096"), 14 | std::make_tuple("0123456789abcdef", "1111111111111111", "7d0cc630afda1ec7"), 15 | std::make_tuple("0000000000000000", "0000000000000000", "4ef997456198dd78"), 16 | std::make_tuple("0123456789abcdef", "fedcba9876543210", "0aceab0fc6a0a28d"), 17 | std::make_tuple("01a1d6d039776742", "7ca110454a1a6e57", "59c68245eb05282b"), 18 | std::make_tuple("5cd54ca83def57da", "0131d9619dc1376e", "b1b8cc0b250f09a0"), 19 | std::make_tuple("0248d43806f67172", "07a1133e4a0b2686", "1730e5778bea1da4"), 20 | std::make_tuple("51454b582ddf440a", "3849674c2602319e", "a25e7856cf2651eb"), 21 | std::make_tuple("42fd443059577fa2", "04b915ba43feb5b6", "353882b109ce8f1a"), 22 | std::make_tuple("059b5e0851cf143a", "0113b970fd34f2ce", "48f4d0884c379918"), 23 | std::make_tuple("0756d8e0774761d2", "0170f175468fb5e6", "432193b78951fc98"), 24 | std::make_tuple("762514b829bf486a", "43297fad38e373fe", "13f04154d69d1ae5"), 25 | std::make_tuple("3bdd119049372802", "07a7137045da2a16", "2eedda93ffd39c79"), 26 | std::make_tuple("26955f6835af609a", "04689104c2fd3b2f", "d887e0393c2da6e3"), 27 | std::make_tuple("164d5e404f275232", "37d06bb516cb7546", "5f99d04f5b163969"), 28 | std::make_tuple("6b056e18759f5cca", "1f08260d1ac2465e", "4a057a3b24d3977b"), 29 | std::make_tuple("004bd6ef09176062", "584023641aba6176", "452031c1e4fada8e"), 30 | std::make_tuple("480d39006ee762f2", "025816164629b007", "7555ae39f59b87bd"), 31 | std::make_tuple("437540c8698f3cfa", "49793ebc79b3258f", "53c55f9cb49fc019"), 32 | std::make_tuple("072d43a077075292", "4fb05e1515ab73a7", "7a8e7bfa937e89a3"), 33 | std::make_tuple("02fe55778117f12a", "49e95d6d4ca229bf", "cf9c5d7a4986adb5"), 34 | std::make_tuple("1d9d5c5018f728c2", "018310dc409b26d6", "d1abb290658bc778"), 35 | std::make_tuple("305532286d6f295a", "1c587f1c13924fef", "55cb3774d13ef201"), 36 | std::make_tuple("0123456789abcdef", "0101010101010101", "fa34ec4847b268b2"), 37 | std::make_tuple("0123456789abcdef", "1f1f1f1f0e0e0e0e", "a790795108ea3cae"), 38 | std::make_tuple("0123456789abcdef", "e0fee0fef1fef1fe", "c39e072d9fac631d"), 39 | std::make_tuple("ffffffffffffffff", "0000000000000000", "014933e0cdaff6e4"), 40 | std::make_tuple("0000000000000000", "ffffffffffffffff", "f21e9a77b71c49bc"), 41 | std::make_tuple("0000000000000000", "0123456789abcdef", "245946885754369a"), 42 | std::make_tuple("ffffffffffffffff", "fedcba9876543210", "6b5c5a9c5d9e0a5a"), 43 | }; 44 | 45 | #endif // __BLOWFISHTESTVECTORS__ 46 | -------------------------------------------------------------------------------- /tests/testvectors/cast/cast128rfc2144.h: -------------------------------------------------------------------------------- 1 | #ifndef __CAST128TESTVECTORS__ 2 | #define __CAST128TESTVECTORS__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vector from 7 | 8 | static const std::vector CAST128_RFC2144 = { 9 | std::make_tuple("0123456789abcdef", "0123456712", "7ac816d16e9b302e"), 10 | std::make_tuple("0123456789abcdef", "01234567123456782345", "eb6a711a2c02271b"), 11 | std::make_tuple("0123456789abcdef", "0123456712345678234567893456789a", "238B4fe5847e44b2"), 12 | }; 13 | 14 | #endif // __CAST128TESTVECTORS__ 15 | -------------------------------------------------------------------------------- /tests/testvectors/cast/cast256rfc2612.h: -------------------------------------------------------------------------------- 1 | #ifndef __CAST256TESTVECTORS__ 2 | #define __CAST256TESTVECTORS__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vector from 7 | 8 | static const std::vector CAST256_RFC2612 = { 9 | std::make_tuple("00000000000000000000000000000000", "2342bb9efa38542c0af75647f29f615d", "c842a08972b43d20836c91d1b7530f6b"), 10 | std::make_tuple("00000000000000000000000000000000", "2342bb9efa38542cbed0ac83940ac298bac77a7717942863", "1b386c0210dcadcbdd0e41aa08a7a7e8"), 11 | std::make_tuple("00000000000000000000000000000000", "2342bb9efa38542cbed0ac83940ac2988d7c47ce264908461cc1b5137ae6b604", "4f6a2038286897b9c9870136553317fa"), 12 | }; 13 | 14 | #endif // __CAST256TESTVECTORS__ 15 | -------------------------------------------------------------------------------- /tests/testvectors/des/nessieset4.h: -------------------------------------------------------------------------------- 1 | #ifndef __DESnessieset4__ 2 | #define __DESnessieset4__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | static const std::vector DES_NESSIE_SET_4 = { 9 | std::make_tuple("0011223344556677", "0001020304050607", "3EF0A891CF8ED990"), 10 | std::make_tuple("EA024714AD5C4D84", "2BD6459F82C5B300", "126EFE8ED312190A"), 11 | }; 12 | 13 | #endif // __DESnessieset4__ -------------------------------------------------------------------------------- /tests/testvectors/gost/testvectors.h: -------------------------------------------------------------------------------- 1 | #ifndef __GOSTTESTVECTORS__ 2 | #define __GOSTTESTVECTORS__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vector from somewhere 7 | 8 | const std::vector GOST_TEST_VECTORS = { 9 | std::make_tuple ("3031323330313233", "3031323330313233303132333031323330313233303132333031323330313233", "f56b8a589f6f7417"), 10 | }; 11 | 12 | #endif // __GOSTTESTVECTORS__ 13 | -------------------------------------------------------------------------------- /tests/testvectors/idea/nessieset4.h: -------------------------------------------------------------------------------- 1 | #ifndef __IDEAnessieset4__ 2 | #define __IDEAnessieset4__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | const std::vector IDEA_NESSIE_SET_4 = { 9 | std::make_tuple("0011223344556677", "000102030405060708090a0b0c0d0e0f", "f526ab9a62c0d258"), 10 | std::make_tuple("ea024714ad5c4d84", "2bd6459f82c5b300952c49104881ff48", "c8fb51d3516627a8"), 11 | }; 12 | 13 | #endif // __IDEAnessieset4__ 14 | -------------------------------------------------------------------------------- /tests/testvectors/idea/nessieset8.h: -------------------------------------------------------------------------------- 1 | #ifndef __TEST_IDEAnessieset8__ 2 | #define __TEST_IDEAnessieset8__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | const std::vector IDEA_NESSIE_SET_8 = { 9 | std::make_tuple("db2d4a92aa68273f", "000102030405060708090a0b0c0d0e0f", "0011223344556677"), 10 | std::make_tuple("f129a6601ef62a47", "2bd6459f82c5b300952c49104881ff48", "ea024714ad5c4d84"), 11 | }; 12 | 13 | #endif // __TEST_IDEAnessieset8__ 14 | -------------------------------------------------------------------------------- /tests/testvectors/misty1/nessieset4.h: -------------------------------------------------------------------------------- 1 | #ifndef __MISTY1NESSIESET4__ 2 | #define __MISTY1NESSIESET4__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vector from 7 | 8 | const std::vector MISTY1_NESSIE_SET_4 = { 9 | std::make_tuple("0011223344556677", "000102030405060708090A0B0C0D0E0F", "3A3D8F2F2CDB11A7"), 10 | std::make_tuple("EA024714AD5C4D84", "2BD6459F82C5B300952C49104881FF48", "5901047178CCA69C"), 11 | }; 12 | 13 | #endif // __MISTY1NESSIESET4__ 14 | -------------------------------------------------------------------------------- /tests/testvectors/plainkeycipher.h: -------------------------------------------------------------------------------- 1 | /* 2 | plainkeycipher.h 3 | 4 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | 26 | #ifndef __PLAINKEYCIPHER__ 27 | #define __PLAINKEYCIPHER__ 28 | 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | 35 | #include "common/includes.h" 36 | 37 | // hold matching plaintext/key/ciphertext tuples 38 | typedef std::tuple PlainKeyCipher; 39 | 40 | // generic function to test symmetric key algorithms 41 | template 42 | void sym_test(const std::vector & test_vectors){ 43 | static_assert(std::is_base_of ::value, "Error: Algorithm type should be a symmetric key algorithm."); 44 | 45 | for(PlainKeyCipher const & pkc : test_vectors){ 46 | std::string plain, key, cipher; 47 | std::tie(plain, key, cipher) = pkc; 48 | auto alg = Alg(unhexlify(key)); 49 | EXPECT_EQ(alg.encrypt(unhexlify(plain)), unhexlify(cipher)); 50 | EXPECT_EQ(alg.decrypt(unhexlify(cipher)), unhexlify(plain)); 51 | } 52 | } 53 | 54 | #endif -------------------------------------------------------------------------------- /tests/testvectors/rc/rc2rfc2268.h: -------------------------------------------------------------------------------- 1 | #ifndef __RC2RFC2268__ 2 | #define __RC2RFC2268__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | static const std::vector RC2_RFC2268 = { 9 | // std::make_tuple("0000000000000000", "0000000000000000", "ebb773f993278eff"), 10 | // std::make_tuple("ffffffffffffffff", "ffffffffffffffff", "278b27e42e2f0d49"), 11 | // std::make_tuple("1000000000000001", "3000000000000000", "30649edf9be7d2c2"), 12 | // std::make_tuple("0000000000000000", "88", "61a8a244adacccf0"), 13 | // std::make_tuple("0000000000000000", "88bca90e90875a", "6ccf4308974c267f"), 14 | // std::make_tuple("0000000000000000", "88bca90e90875a7f0f79c384627bafb2", "1a807d272bbe5db1"), 15 | // std::make_tuple("0000000000000000", "88bca90e90875a7f0f79c384627bafb2", "2269552ab0f85ca6"), 16 | // std::make_tuple("0000000000000000", "88bca90e90875a7f0f79c384627bafb216f80a6f85920584c42fceb0be255daf1e", "5b78d3a43dfff1f1"), 17 | }; 18 | 19 | #endif // __RC2RFC2268__ -------------------------------------------------------------------------------- /tests/testvectors/rc/rc4scicrypt.h: -------------------------------------------------------------------------------- 1 | #ifndef __RC4SCICRYPT__ 2 | #define __RC4SCICRYPT__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vector from 7 | 8 | static const std::vector RC4_SCI_CRYPT = { 9 | std::make_tuple("0123456789abcdef", "0123456789abcdef", "75b7878099e0c596"), 10 | std::make_tuple("0000000000000000", "0123456789abcdef", "7494c2e7104b0879"), 11 | std::make_tuple("0000000000000000", "0000000000000000", "de188941a3375d3a"), 12 | std::make_tuple("00000000000000000000", "ef012345", "d6a141a7ec3c38dfbd61"), 13 | }; 14 | 15 | #endif // __RC4SCICRYPT__ 16 | -------------------------------------------------------------------------------- /tests/testvectors/rc/rc5nessieset4.h: -------------------------------------------------------------------------------- 1 | #ifndef __RC5nessieset4__ 2 | #define __RC5nessieset4__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | static const std::vector RC5_NESSIE_SET_4 = { 9 | std::make_tuple("0011223344556677", "000102030405060708090A0B0C0D0E0F", "2DDC149BCF088B9E"), 10 | std::make_tuple("EA024714AD5C4D84", "2BD6459F82C5B300952C49104881FF48", "11E43B86D231EA64"), 11 | }; 12 | 13 | #endif // __RC5nessieset4__ -------------------------------------------------------------------------------- /tests/testvectors/rc/rc5paper.h: -------------------------------------------------------------------------------- 1 | #ifndef __RC5PAPER__ 2 | #define __RC5PAPER__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | static const std::vector RC5_PAPER = { 9 | std::make_tuple("0000000000000000", "00000000000000000000000000000000", "21a5dbee154b8f6d"), 10 | std::make_tuple("21a5dbee154b8f6d", "915f4619be41b2516355a50110a9ce91", "f7c013ac5b2b8952"), 11 | std::make_tuple("f7c013ac5b2b8952", "783348e75aeb0f2fd7b169bb8dc16787", "2f42b3b70369fc92"), 12 | std::make_tuple("2f42b3b70369fc92", "dc49db1375a5584f6485b413b5f12baf", "65c178b284d197cc"), 13 | std::make_tuple("65c178b284d197cc", "5269f149d41ba0152497574d7f153125", "eb44e415da319824"), 14 | }; 15 | 16 | #endif // __RC5TESTVECTORSPAPER__ -------------------------------------------------------------------------------- /tests/testvectors/rc/rc6nessieset4.h: -------------------------------------------------------------------------------- 1 | #ifndef __RC6nessieset4__ 2 | #define __RC6nessieset4__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vector from 7 | 8 | static const std::vector RC6_NESSIE_SET_4 = { 9 | std::make_tuple("00112233445566778899AABBCCDDEEFF", "000102030405060708090A0B0C0D0E0F1011121314151617", "37E13FB5351BD78D3E7912FDC5F80FCD"), 10 | std::make_tuple("EA024714AD5C4D84EA024714AD5C4D84", "2BD6459F82C5B300952C49104881FF482BD6459F82C5B300", "50D29745329694B65D6F54EE65455180"), 11 | }; 12 | 13 | #endif // __RC6nessieset4__ 14 | -------------------------------------------------------------------------------- /tests/testvectors/safer/saferk64paper.h: -------------------------------------------------------------------------------- 1 | #ifndef __SAFERK64PAPER__ 2 | #define __SAFERK64PAPER__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vector from 7 | 8 | static const std::vector SAFERK64_PAPER = { 9 | std::make_tuple("0102030405060708", "0000000000000000", "7d28038633b92eb4"), 10 | std::make_tuple("0000000000000000", "0102030405060708", "5ab27f7214a33ae1"), 11 | std::make_tuple("0102030405060708", "0807060504030201", "c8f29cdd87783ed9"), 12 | std::make_tuple("0000000000000000", "0000000000000000", "032808c90ee7ab7f"), 13 | }; 14 | 15 | #endif // __SAFERK64PAPER__ 16 | -------------------------------------------------------------------------------- /tests/testvectors/seed/nessieset4.h: -------------------------------------------------------------------------------- 1 | #ifndef __SEEDNESSIESET4__ 2 | #define __SEEDNESSIESET4__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | static const std::vector SEED_NESSIE_SET_4 = { 9 | std::make_tuple("00112233445566778899AABBCCDDEEFF", "000102030405060708090A0B0C0D0E0F", "AF527210EB79C7A023ABF348E70C9045"), 10 | std::make_tuple("EA024714AD5C4D84EA024714AD5C4D84", "2BD6459F82C5B300952C49104881FF48", "14AE1E10DDB7764BD7D6CD66717CAF76"), 11 | }; 12 | 13 | #endif // __SEEDNESSIESET4__ -------------------------------------------------------------------------------- /tests/testvectors/skipjack/nist.h: -------------------------------------------------------------------------------- 1 | #ifndef __SKIPJACKSNIST__ 2 | #define __SKIPJACKSNIST__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vector from 7 | 8 | static const std::vector SKIPJACK_NIST = { 9 | std::make_tuple("33221100ddccbbaa", "00998877665544332211", "2587cae27a12d300"), 10 | }; 11 | 12 | #endif // __SKIPJACKSNIST__ 13 | -------------------------------------------------------------------------------- /tests/testvectors/skipjack/scicrypt.h: -------------------------------------------------------------------------------- 1 | #ifndef __SKIPJACKSCICRYPT__ 2 | #define __SKIPJACKSCICRYPT__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vector from 7 | 8 | static const std::vector SKIPJACK_SCI_CRYPT = { 9 | std::make_tuple("99ccfe2b90fd550b", "e7496e99e4628b7f9ffb", "60a73d387b517fca"), 10 | std::make_tuple("1ddf39abf5cd711e", "f8da02647722f7103adf", "c92d22324c6b31ae"), 11 | std::make_tuple("dd6c6cce8f83e69e", "82760ac137948821eee4", "e32877c1d9527fff"), 12 | std::make_tuple("beaacf177fa41a11", "843c1687d3cdca5fc5c3", "4745783f75b8861a"), 13 | std::make_tuple("c4c09f216c1bc60a", "ae870cd7ff33a995f7e5", "5c101636b8a57a72"), 14 | std::make_tuple("d3f814b000245856", "5ccbd913ea8b73bd6391", "b4fc0f8e54728f91"), 15 | std::make_tuple("356ec7d93832329c", "f65e74cd599c68a40cc7", "93b750608f5701f8"), 16 | std::make_tuple("209ecf1c537ad56c", "aa106e46d7087c4e93dc", "d823d45510099e61"), 17 | std::make_tuple("892eea9d64e17d66", "a93f9789a20c3cc34fea", "0959e231b275d95f"), 18 | std::make_tuple("991390fd760fc91b", "88b163cbd61616888899", "e7700209886767ae"), 19 | std::make_tuple("daebc947ddca9c9e", "fb6cd1ff70487561df10", "e7cc49a56bd6a322"), 20 | std::make_tuple("6419ddefe2cd8f2e", "5edc1ac0c4e7ef5f002c", "e48a05cf26e242fd"), 21 | std::make_tuple("322998ecbd068112", "8e3090c19aa32f94496a", "62c0e537b14df2c1"), 22 | std::make_tuple("3aae2aee20da93cc", "b96e3fd46fa4263f9092", "54d1e58a6b624d71"), 23 | std::make_tuple("14311112ca11f727", "9e6635baee28c5bce2bc", "5d0f235a9d221ce0"), 24 | std::make_tuple("300e4313e7ad6796", "04127ce16dc1b1726a66", "8e5b03522e68cbeb"), 25 | std::make_tuple("09cd1c1accbe7797", "f0b89d75e979ccc9b172", "572c9b4025a9134b"), 26 | std::make_tuple("31b30ca354af3cd8", "f9bfc78798cbf1bcd4b5", "8c959c904789fbda"), 27 | std::make_tuple("08c59b0db99ec267", "f43a51b4273bde27d2b0", "b7d7f5fa342988fb"), 28 | std::make_tuple("9784b1e3e7e60e60", "cd51f0a75aa73c48edd2", "763aa8ee109397b3"), 29 | std::make_tuple("f65216373d4b43c7", "b3319a3f6622aa726bb3", "0325600337b8ad3c"), 30 | std::make_tuple("cba4c1215d5d36ce", "493254c9596e993f5f9c", "68e1c551c59108c0"), 31 | std::make_tuple("82294851288e75cb", "76150c2c3ced1c7ca021", "7eb6325d82a2096c"), 32 | std::make_tuple("c3a7b7e4a52e407b", "7140d6c5486305872df6", "2483f385a42ee3c6"), 33 | std::make_tuple("1bfde32ab559e13a", "3c2c3901f0ee9a3b2b0e", "d6fa9db8685fd88a"), 34 | std::make_tuple("d205f7486c782838", "606a8b4bdfaae8a0ba51", "0330489170b85293"), 35 | std::make_tuple("d96ff1f7c7fc60e0", "7847a47a0fe79ab770ce", "1f9b3301c9b2708c"), 36 | std::make_tuple("241d4bde19a75f8f", "73b9ab0c36c99e91a891", "2b86c57ffe168895"), 37 | std::make_tuple("7be1b8d58321c619", "a37f2ad5a85e170741f5", "5af7ceb3eed9dca1"), 38 | std::make_tuple("c9214ea01ec14948", "f7b0c2a8170e3c4e48b3", "1b587736e116c04b"), 39 | std::make_tuple("e2a3091feb581588", "a1fc67e44eacacf4a902", "f3ecf0f1789a3923"), 40 | std::make_tuple("3cb466d301b60854", "f14430affc5fe82a9ae9", "e8d114c20ffa1c79"), 41 | std::make_tuple("b0684f8a5e63d935", "fd26df50486a4cd96d9b", "222903994d64fe3a"), 42 | std::make_tuple("ba1f37e88edec55f", "a6d46d46ca287e1a332a", "91f2baad6fa0de55"), 43 | std::make_tuple("e9fed8501b7a6579", "83c3f1cb8d06efc6196b", "83f9f08f89a854ee"), 44 | std::make_tuple("eb5ca8b3fa1fcdbb", "0edfa44c7d4a4ef0725e", "2b1b6670a6be0324"), 45 | std::make_tuple("b8b525c6382af277", "b8edcf167d99a711ccee", "211a695da473766f"), 46 | std::make_tuple("9162e781ff683853", "4f639e0d5a5d2ad7e9a4", "eb2976370d22ef22"), 47 | std::make_tuple("c9f23a20a39ded11", "37e006256a4ae6d320c4", "dba4c0ef0ea098c0"), 48 | std::make_tuple("5a6f12f32f7eefdf", "e41d0bd25f931ba1d85c", "923daee8000709f9"), 49 | std::make_tuple("cad5414c1c64f194", "fdf65bbc5fe600f3cd68", "d5771e78b6f1fe1e"), 50 | std::make_tuple("063a58a20b45378f", "1c269af2ff166acb27ef", "634f7a3861af97a1"), 51 | std::make_tuple("08fbf42b4313347b", "1179f64acb6122ccf649", "3a803a4bd0e8c3e6"), 52 | std::make_tuple("6d4ed0e9930532d1", "078c87265eb8da323e90", "f4fa372e7e1441a1"), 53 | std::make_tuple("40b699812345661d", "2fff35f8eb774c843bb0", "63a9197f7b75f53f"), 54 | std::make_tuple("22ed54626a51e505", "09f77346a4393ce99856", "e91a050a7481b3dd"), 55 | std::make_tuple("0c489b66e2da531b", "5b878e0b22a705acf8fb", "6e9370a91b994878"), 56 | std::make_tuple("c64b10f8b191bc2c", "9d72c1ab2092c1b10877", "5bdecded96d656c9"), 57 | std::make_tuple("91fdf7236f85bdd6", "72865f289725e1b55502", "1a5680e51736026f"), 58 | std::make_tuple("40009f8a465a9feb", "06e3c0e541f4aae6fe93", "0e7aace421bc79d8"), 59 | std::make_tuple("543208b05bfa3858", "2ea09f1cc89e064f09bc", "a95d87fad12c3593"), 60 | }; 61 | 62 | #endif // __SKIPJACKSCICRYPT__ 63 | -------------------------------------------------------------------------------- /tests/testvectors/tdes/permop.h: -------------------------------------------------------------------------------- 1 | #ifndef __TRIPLEDESECBPERMOP__ 2 | #define __TRIPLEDESECBPERMOP__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | static const std::vector TDES_PERMOP = { 9 | std::make_tuple("0000000000000000", "1046913489980131", "88d55e54f54c97b4"), 10 | std::make_tuple("0000000000000000", "1007103489988020", "0c0cc00c83ea48fd"), 11 | std::make_tuple("0000000000000000", "10071034c8980120", "83bc8ef3a6570183"), 12 | std::make_tuple("0000000000000000", "1046103489988020", "df725dcad94ea2e9"), 13 | std::make_tuple("0000000000000000", "1086911519190101", "e652b53b550be8b0"), 14 | std::make_tuple("0000000000000000", "1086911519580101", "af527120c485cbb0"), 15 | std::make_tuple("0000000000000000", "5107b01519580101", "0f04ce393db926d5"), 16 | std::make_tuple("0000000000000000", "1007b01519190101", "c9f00ffc74079067"), 17 | std::make_tuple("0000000000000000", "3107915498080101", "7cfd82a593252b4e"), 18 | std::make_tuple("0000000000000000", "3107919498080101", "cb49a2f9e91363e3"), 19 | std::make_tuple("0000000000000000", "10079115b9080140", "00b588be70d23f56"), 20 | std::make_tuple("0000000000000000", "3107911598080140", "406a9a6ab43399ae"), 21 | std::make_tuple("0000000000000000", "1007d01589980101", "6cb773611dca9ada"), 22 | std::make_tuple("0000000000000000", "9107911589980101", "67fd21c17dbb5d70"), 23 | std::make_tuple("0000000000000000", "9107d01589190101", "9592cb4110430787"), 24 | std::make_tuple("0000000000000000", "1007d01598980120", "a6b7ff68a318ddd3"), 25 | std::make_tuple("0000000000000000", "1007940498190101", "4d102196c914ca16"), 26 | std::make_tuple("0000000000000000", "0107910491190401", "2dfa9f4573594965"), 27 | std::make_tuple("0000000000000000", "0107910491190101", "b46604816c0e0774"), 28 | std::make_tuple("0000000000000000", "0107940491190401", "6e7e6221a4f34e87"), 29 | std::make_tuple("0000000000000000", "19079210981a0101", "aa85e74643233199"), 30 | std::make_tuple("0000000000000000", "1007911998190801", "2e5a19db4d1962d6"), 31 | std::make_tuple("0000000000000000", "10079119981a0801", "23a866a809d30894"), 32 | std::make_tuple("0000000000000000", "1007921098190101", "d812d961f017d320"), 33 | std::make_tuple("0000000000000000", "100791159819010b", "055605816e58608f"), 34 | std::make_tuple("0000000000000000", "1004801598190101", "abd88e8b1b7716f1"), 35 | std::make_tuple("0000000000000000", "1004801598190102", "537ac95be69da1e1"), 36 | std::make_tuple("0000000000000000", "1004801598190108", "aed0f6ae3c25cdd8"), 37 | std::make_tuple("0000000000000000", "1002911598100104", "b3e35a5ee53e7b8d"), 38 | std::make_tuple("0000000000000000", "1002911598190104", "61c79c71921a2ef8"), 39 | std::make_tuple("0000000000000000", "1002911598100201", "e2f5728f0995013c"), 40 | std::make_tuple("0000000000000000", "1002911698100101", "1aeac39a61f0a464"), 41 | }; 42 | 43 | #endif // __TRIPLEDESECBPERMOP__ 44 | -------------------------------------------------------------------------------- /tests/testvectors/tdes/subtab.h: -------------------------------------------------------------------------------- 1 | #ifndef __TRIPLEDESECBSUBTAB__ 2 | #define __TRIPLEDESECBSUBTAB__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | static const std::vector TDES_SUBTAB = { 9 | std::make_tuple("01a1d6d039776742", "7ca110454a1a6e57", "690f5b0d9a26939b"), 10 | std::make_tuple("5cd54ca83def57da", "0131d9619dc1376e", "7a389d10354bd271"), 11 | std::make_tuple("0248d43806f67172", "07a1133e4a0b2686", "868ebb51cab4599a"), 12 | std::make_tuple("51454b582ddf440a", "3849674c2602319e", "7178876e01f19b2a"), 13 | std::make_tuple("42fd443059577fa2", "04b915ba43feb5b6", "af37fb421f8c4095"), 14 | std::make_tuple("059b5e0851cf143a", "0113b970fd34f2ce", "86a560f10ec6d85b"), 15 | std::make_tuple("0756d8e0774761d2", "0170f175468fb5e6", "0cd3da020021dc09"), 16 | std::make_tuple("762514b829bf486a", "43297fad38e373fe", "ea676b2cb7db2b7a"), 17 | std::make_tuple("3bdd119049372802", "07a7137045da2a16", "dfd64a815caf1a0f"), 18 | std::make_tuple("26955f6835af609a", "04689104c2fd3b2f", "5c513c9c4886c088"), 19 | std::make_tuple("164d5e404f275232", "37d06bb516cb7546", "0a2aeeae3ff4ab77"), 20 | std::make_tuple("6b056e18759f5cca", "1f08260d1ac2465e", "ef1bf03e5dfa575a"), 21 | std::make_tuple("004bd6ef09176062", "584023641aba6176", "88bf0db6d70dee56"), 22 | std::make_tuple("480d39006ee762f2", "025816164629b007", "a1f9915541020b56"), 23 | std::make_tuple("437540c8698f3cfa", "49793ebc79b3258f", "6fbf1cafcffd0556"), 24 | std::make_tuple("072d43a077075292", "4fb05e1515ab73a7", "2f22e49bab7ca1ac"), 25 | std::make_tuple("02fe55778117f12a", "49e95d6d4ca229bf", "5a6b612cc26cce4a"), 26 | std::make_tuple("1d9d5c5018f728c2", "018310dc409b26d6", "5f4c038ed12b2e41"), 27 | std::make_tuple("305532286d6f295a", "1c587f1c13924fef", "63fac0d034d9f793"), 28 | }; 29 | 30 | #endif // __TRIPLEDESECBSUBTAB__ 31 | -------------------------------------------------------------------------------- /tests/testvectors/tdes/varkey.h: -------------------------------------------------------------------------------- 1 | #ifndef __TRIPLEDESECBVARKEY__ 2 | #define __TRIPLEDESECBVARKEY__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vectors from 7 | 8 | static const std::vector TDES_VARKEY = { 9 | std::make_tuple("0000000000000000", "8001010101010101", "95a8d72813daa94d"), 10 | std::make_tuple("0000000000000000", "4001010101010101", "0eec1487dd8c26d5"), 11 | std::make_tuple("0000000000000000", "2001010101010101", "7ad16ffb79c45926"), 12 | std::make_tuple("0000000000000000", "1001010101010101", "d3746294ca6a6cf3"), 13 | std::make_tuple("0000000000000000", "0801010101010101", "809f5f873c1fd761"), 14 | std::make_tuple("0000000000000000", "0401010101010101", "c02faffec989d1fc"), 15 | std::make_tuple("0000000000000000", "0201010101010101", "4615aa1d33e72f10"), 16 | std::make_tuple("0000000000000000", "0180010101010101", "2055123350c00858"), 17 | std::make_tuple("0000000000000000", "0140010101010101", "df3b99d6577397c8"), 18 | std::make_tuple("0000000000000000", "0120010101010101", "31fe17369b5288c9"), 19 | std::make_tuple("0000000000000000", "0110010101010101", "dfdd3cc64dae1642"), 20 | std::make_tuple("0000000000000000", "0108010101010101", "178c83ce2b399d94"), 21 | std::make_tuple("0000000000000000", "0104010101010101", "50f636324a9b7f80"), 22 | std::make_tuple("0000000000000000", "0102010101010101", "a8468ee3bc18f06d"), 23 | std::make_tuple("0000000000000000", "0101800101010101", "a2dc9e92fd3cde92"), 24 | std::make_tuple("0000000000000000", "0101400101010101", "cac09f797d031287"), 25 | std::make_tuple("0000000000000000", "0101200101010101", "90ba680b22aeb525"), 26 | std::make_tuple("0000000000000000", "0101100101010101", "ce7a24f350e280b6"), 27 | std::make_tuple("0000000000000000", "0101080101010101", "882bff0aa01a0b87"), 28 | std::make_tuple("0000000000000000", "0101040101010101", "25610288924511c2"), 29 | std::make_tuple("0000000000000000", "0101020101010101", "c71516c29c75d170"), 30 | std::make_tuple("0000000000000000", "0101018001010101", "5199c29a52c9f059"), 31 | std::make_tuple("0000000000000000", "0101014001010101", "c22f0a294a71f29f"), 32 | std::make_tuple("0000000000000000", "0101012001010101", "ee371483714c02ea"), 33 | std::make_tuple("0000000000000000", "0101011001010101", "a81fbd448f9e522f"), 34 | std::make_tuple("0000000000000000", "0101010801010101", "4f644c92e192dfed"), 35 | std::make_tuple("0000000000000000", "0101010401010101", "1afa9a66a6df92ae"), 36 | std::make_tuple("0000000000000000", "0101010201010101", "b3c1cc715cb879d8"), 37 | std::make_tuple("0000000000000000", "0101010180010101", "19d032e64ab0bd8b"), 38 | std::make_tuple("0000000000000000", "0101010140010101", "3cfaa7a7dc8720dc"), 39 | std::make_tuple("0000000000000000", "0101010120010101", "b7265f7f447ac6f3"), 40 | std::make_tuple("0000000000000000", "0101010110010101", "9db73b3c0d163f54"), 41 | std::make_tuple("0000000000000000", "0101010108010101", "8181b65babf4a975"), 42 | std::make_tuple("0000000000000000", "0101010104010101", "93c9b64042eaa240"), 43 | std::make_tuple("0000000000000000", "0101010102010101", "5570530829705592"), 44 | std::make_tuple("0000000000000000", "0101010101800101", "8638809e878787a0"), 45 | std::make_tuple("0000000000000000", "0101010101400101", "41b9a79af79ac208"), 46 | std::make_tuple("0000000000000000", "0101010101200101", "7a9be42f2009a892"), 47 | std::make_tuple("0000000000000000", "0101010101100101", "29038d56ba6d2745"), 48 | std::make_tuple("0000000000000000", "0101010101080101", "5495c6abf1e5df51"), 49 | std::make_tuple("0000000000000000", "0101010101040101", "ae13dbd561488933"), 50 | std::make_tuple("0000000000000000", "0101010101020101", "024d1ffa8904e389"), 51 | std::make_tuple("0000000000000000", "0101010101018001", "d1399712f99bf02e"), 52 | std::make_tuple("0000000000000000", "0101010101014001", "14c1d7c1cffec79e"), 53 | std::make_tuple("0000000000000000", "0101010101012001", "1de5279dae3bed6f"), 54 | std::make_tuple("0000000000000000", "0101010101011001", "e941a33f85501303"), 55 | std::make_tuple("0000000000000000", "0101010101010801", "da99dbbc9a03f379"), 56 | std::make_tuple("0000000000000000", "0101010101010401", "b7fc92f91d8e92e9"), 57 | std::make_tuple("0000000000000000", "0101010101010201", "ae8e5caa3ca04e85"), 58 | std::make_tuple("0000000000000000", "0101010101010180", "9cc62df43b6eed74"), 59 | std::make_tuple("0000000000000000", "0101010101010140", "d863dbb5c59a91a0"), 60 | std::make_tuple("0000000000000000", "0101010101010120", "a1ab2190545b91d7"), 61 | std::make_tuple("0000000000000000", "0101010101010110", "0875041e64c570f7"), 62 | std::make_tuple("0000000000000000", "0101010101010108", "5a594528bebef1cc"), 63 | std::make_tuple("0000000000000000", "0101010101010104", "fcdb3291de21f0c0"), 64 | std::make_tuple("0000000000000000", "0101010101010102", "869efd7f9f265a09"), 65 | }; 66 | 67 | #endif // __TRIPLEDESECBVARKEY__ 68 | -------------------------------------------------------------------------------- /tests/testvectors/tea/edipermadi.h: -------------------------------------------------------------------------------- 1 | #ifndef __TEATESTVECTORS__ 2 | #define __TEATESTVECTORS__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vector from 7 | 8 | static const std::vector TEA_EDIPERMADI = { 9 | std::make_tuple("0123456789abcdef", "00112233445566778899aabbccddeeff", "126c6b92c0653a3e"), 10 | }; 11 | 12 | #endif // __TEATESTVECTORS__ 13 | -------------------------------------------------------------------------------- /tests/testvectors/xtea/tayloredge.h: -------------------------------------------------------------------------------- 1 | #ifndef __XTEATESTVECTORS__ 2 | #define __XTEATESTVECTORS__ 3 | 4 | #include "../plainkeycipher.h" 5 | 6 | // Test vector from 7 | 8 | static const std::vector XTEA_TAYLOR_EDGE = { 9 | std::make_tuple("AF20A390547571AA", "27f917b1c1da899360e2acaaa6eb923d", "D26428AF0A202283"), 10 | std::make_tuple("0288419716939937", "31415926535897932384626433832795", "46e2007d58bbc2ea"), 11 | std::make_tuple("abc1234abc1234ab", "1234abc1234abc1234abc1234abc1234", "5c0754c1f6f0bd9b"), 12 | std::make_tuple("234abc1234abc123", "abc1234abc1234abc1234abc1234abc1", "cdfcc72c24bc116b"), 13 | std::make_tuple("deadbeefdeadbeef", "deadbeefdeadbeefdeadbeefdeadbeef", "faf28cb50940c0e0"), 14 | std::make_tuple("9647a9189ec565d5", "deadbeefdeadbeefdeadbeefdeadbeef", "deadbeefdeadbeef"), 15 | std::make_tuple("499602d2499602d2", "499602d2499602d2499602d2499602d2", "69cc2fbbe234a670"), 16 | std::make_tuple("74c4424ca09494e2", "499602d2499602d2499602d2499602d2", "499602d2499602d2"), 17 | }; 18 | 19 | #endif // __XTEATESTVECTORS__ 20 | -------------------------------------------------------------------------------- /tests/tripledes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/TDES.h" 4 | 5 | #include "testvectors/tdes/invperm.h" 6 | #include "testvectors/tdes/permop.h" 7 | #include "testvectors/tdes/subtab.h" 8 | #include "testvectors/tdes/varkey.h" 9 | #include "testvectors/tdes/vartxt.h" 10 | 11 | TEST(TripleDES, invperm) { 12 | for(PlainKeyCipher const & pkc : TDES_INVPERM){ 13 | std::string plain, key, cipher; 14 | std::tie(plain, key, cipher) = pkc; 15 | key = unhexlify(key); 16 | auto tdes = TDES(key, "e", key, "d", key, "e"); //!< DES(key) == TDES(key, key, key) 17 | EXPECT_EQ(tdes.encrypt(unhexlify(plain)), unhexlify(cipher)); 18 | EXPECT_EQ(tdes.decrypt(unhexlify(cipher)), unhexlify(plain)); 19 | } 20 | } 21 | 22 | TEST(TripleDES, permop) { 23 | for(PlainKeyCipher const & pkc : TDES_PERMOP){ 24 | std::string plain, key, cipher; 25 | std::tie(plain, key, cipher) = pkc; 26 | key = unhexlify(key); 27 | auto tdes = TDES(key, "e", key, "d", key, "e"); //!< DES(key) == TDES(key, key, key) 28 | EXPECT_EQ(tdes.encrypt(unhexlify(plain)), unhexlify(cipher)); 29 | EXPECT_EQ(tdes.decrypt(unhexlify(cipher)), unhexlify(plain)); 30 | } 31 | } 32 | 33 | TEST(TripleDES, subtab) { 34 | for(PlainKeyCipher const & pkc : TDES_SUBTAB){ 35 | std::string plain, key, cipher; 36 | std::tie(plain, key, cipher) = pkc; 37 | key = unhexlify(key); 38 | auto tdes = TDES(key, "e", key, "d", key, "e"); //!< DES(key) == TDES(key, key, key) 39 | EXPECT_EQ(tdes.encrypt(unhexlify(plain)), unhexlify(cipher)); 40 | EXPECT_EQ(tdes.decrypt(unhexlify(cipher)), unhexlify(plain)); 41 | } 42 | } 43 | 44 | TEST(TripleDES, varkey) { 45 | for(PlainKeyCipher const & pkc : TDES_VARKEY){ 46 | std::string plain, key, cipher; 47 | std::tie(plain, key, cipher) = pkc; 48 | key = unhexlify(key); 49 | auto tdes = TDES(key, "e", key, "d", key, "e"); //!< DES(key) == TDES(key, key, key) 50 | EXPECT_EQ(tdes.encrypt(unhexlify(plain)), unhexlify(cipher)); 51 | EXPECT_EQ(tdes.decrypt(unhexlify(cipher)), unhexlify(plain)); 52 | } 53 | } 54 | 55 | TEST(TripleDES, vartext) { 56 | for(PlainKeyCipher const & pkc : TDES_VARTXT){ 57 | std::string plain, key, cipher; 58 | std::tie(plain, key, cipher) = pkc; 59 | key = unhexlify(key); 60 | auto tdes = TDES(key, "e", key, "d", key, "e"); //!< DES(key) == TDES(key, key, key) 61 | EXPECT_EQ(tdes.encrypt(unhexlify(plain)), unhexlify(cipher)); 62 | EXPECT_EQ(tdes.decrypt(unhexlify(cipher)), unhexlify(plain)); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /tests/twofish.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/Twofish.h" 4 | 5 | #include "testvectors/twofish/vk.h" 6 | #include "testvectors/twofish/vt.h" 7 | 8 | // Test vectors from 9 | 10 | TEST(Twofish, 128_ival) { 11 | std::string key(16, 0); 12 | std::string plain(16, 0); 13 | std::string cipher = unhexlify("9F589F5CF6122C32B6BFEC2F2AE8C35A"); 14 | 15 | auto twofish = Twofish(key); 16 | EXPECT_EQ(twofish.encrypt(plain), cipher); 17 | EXPECT_EQ(twofish.decrypt(cipher), plain); 18 | } 19 | 20 | TEST(Twofish, 192_ival) { 21 | std::string key = unhexlify("0123456789ABCDEFFEDCBA98765432100011223344556677"); 22 | std::string plain(16, 0); 23 | std::string cipher = unhexlify("CFD1D2E5A9BE9CDF501F13B892BD2248"); 24 | 25 | auto twofish = Twofish(key); 26 | EXPECT_EQ(twofish.encrypt(plain), cipher); 27 | EXPECT_EQ(twofish.decrypt(cipher), plain); 28 | } 29 | 30 | TEST(Twofish, 256_ival) { 31 | std::string key = unhexlify("0123456789ABCDEFFEDCBA987654321000112233445566778899AABBCCDDEEFF"); 32 | std::string plain(16, 0); 33 | std::string cipher = unhexlify("37527BE0052334B89F0CFCCAE87CFA20"); 34 | 35 | auto twofish = Twofish(key); 36 | EXPECT_EQ(twofish.encrypt(plain), cipher); 37 | EXPECT_EQ(twofish.decrypt(cipher), plain); 38 | } 39 | 40 | TEST(Twofish, 128_tbl) { 41 | std::string key(16, 0); 42 | std::string plain(16, 0); 43 | 44 | for ( unsigned int i = 0; i < 49; ++i ) { 45 | auto twofish = Twofish(key); 46 | key = plain; 47 | plain = twofish.encrypt(plain); 48 | } 49 | 50 | EXPECT_EQ(plain, unhexlify("5D9D4EEFFA9151575524F115815A12E0")); 51 | } 52 | 53 | TEST(Twofish, 192_tbl) { 54 | std::string key(24, 0); 55 | std::string plain(16, 0); 56 | 57 | for ( unsigned int i = 0; i < 49; ++i ) { 58 | auto twofish = Twofish(key); 59 | key = (plain + key).substr(0, 24); 60 | plain = twofish.encrypt(plain); 61 | } 62 | 63 | EXPECT_EQ(plain, unhexlify("E75449212BEEF9F4A390BD860A640941")); 64 | } 65 | 66 | TEST(Twofish, 256_tbl) { 67 | std::string key(32, 0); 68 | std::string plain(16, 0); 69 | 70 | for ( unsigned int i = 0; i < 49; ++i ) { 71 | auto twofish = Twofish(key); 72 | key = (plain + key).substr(0, 32); 73 | plain = twofish.encrypt(plain); 74 | } 75 | 76 | EXPECT_EQ(plain, unhexlify("37FE26FF1CF66175F5DDF4C33B97A205")); 77 | } 78 | 79 | TEST(Twofish, 128_vk) { 80 | sym_test (TWOFISH128_VK_TEST_VECTORS); 81 | } 82 | 83 | TEST(Twofish, 192_vk) { 84 | sym_test (TWOFISH192_VK_TEST_VECTORS); 85 | } 86 | 87 | TEST(Twofish, 256_vk) { 88 | sym_test (TWOFISH256_VK_TEST_VECTORS); 89 | } 90 | 91 | TEST(Twofish, 128_vt) { 92 | sym_test (TWOFISH128_VT_TEST_VECTORS); 93 | } 94 | 95 | TEST(Twofish, 192_vt) { 96 | sym_test (TWOFISH192_VT_TEST_VECTORS); 97 | } 98 | 99 | TEST(Twofish, 256_vt) { 100 | sym_test (TWOFISH256_VT_TEST_VECTORS); 101 | } 102 | 103 | -------------------------------------------------------------------------------- /tests/xtea.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Encryptions/XTEA.h" 4 | 5 | #include "testvectors/xtea/tayloredge.h" 6 | 7 | TEST(XTEA, Taylor_Edge) { 8 | sym_test (XTEA_TAYLOR_EDGE); 9 | } 10 | --------------------------------------------------------------------------------