├── .gitignore ├── .travis.yml ├── 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 ├── 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 ├── Makefile ├── 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 └── objects.mk ├── Hashes.h ├── Hashes ├── HMAC.h ├── HashAlg.cpp ├── HashAlg.h ├── KECCAK.cpp ├── KECCAK.h ├── KECCAK_Const.h ├── LM.cpp ├── LM.h ├── MD2.cpp ├── MD2.h ├── MD2_Const.h ├── MD4.cpp ├── MD4.h ├── MD4_Const.h ├── MD5.cpp ├── MD5.h ├── MD5_Const.h ├── Makefile ├── MerkleDamgard.cpp ├── MerkleDamgard.h ├── POLY1305AES.cpp ├── POLY1305AES.h ├── RIPEMD128.cpp ├── RIPEMD128.h ├── RIPEMD128_Const.h ├── RIPEMD160.cpp ├── RIPEMD160.h ├── RIPEMD160_Const.h ├── RIPEMD_Const.h ├── SHA1.cpp ├── SHA1.h ├── SHA224.cpp ├── SHA224.h ├── SHA256.cpp ├── SHA256.h ├── SHA256_Const.h ├── SHA2_Functions.cpp ├── SHA2_Functions.h ├── SHA3.h ├── SHA3.tpp ├── SHA384.cpp ├── SHA384.h ├── SHA512.cpp ├── SHA512.h ├── SHA512_Const.h └── objects.mk ├── LICENSE ├── Makefile ├── README.md ├── common ├── Makefile ├── cryptomath.h ├── includes.cpp ├── includes.h └── objects.mk └── tests ├── Makefile ├── hmac.cpp ├── keccak.cpp ├── lm.cpp ├── md2.cpp ├── md4.cpp ├── md5.cpp ├── objects.mk ├── poly1305aes.cpp ├── ripemd128.cpp ├── ripemd160.cpp ├── sha1.cpp ├── sha224.cpp ├── sha256.cpp ├── sha3.cpp ├── sha384.cpp ├── sha512.cpp └── testvectors ├── datadigest.h ├── hmac ├── md5.h ├── sha1.h ├── sha256.h └── sha512.h ├── keccak └── keccak.h ├── keydatadigest.h ├── lm └── lm.h ├── md ├── md2.h ├── md4.h └── md5.h ├── poly1305aes └── poly1305aes.h ├── ripemd ├── ripemd128.h └── ripemd160.h └── sha ├── sha1.h ├── sha224.h ├── sha256.h ├── sha3.h ├── sha384.h └── sha512.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Build results 2 | *.o 3 | *.a 4 | *.exe 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # from 2 | 3 | language: cpp 4 | 5 | os: linux 6 | sudo: false 7 | 8 | env: 9 | - CXX_COMPILER=g++-4.9 10 | - CXX_COMPILER=g++-5 11 | - CXX_COMPILER=g++-6 12 | - CXX_COMPILER=g++-7 13 | - CXX_COMPILER=g++-8 14 | - CXX_COMPILER=clang++-3.8 15 | - CXX_COMPILER=clang++-3.9 16 | - CXX_COMPILER=clang++-4.0 17 | - CXX_COMPILER=clang++-5.0 18 | - CXX_COMPILER=clang++-6.0 19 | - CXX_COMPILER=clang++-7 20 | - CXX_COMPILER=clang++-8 21 | 22 | addons: 23 | apt: 24 | sources: 25 | - ubuntu-toolchain-r-test 26 | update: false 27 | 28 | before_install: 29 | - 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/xenial/ llvm-toolchain-$(lsb_release -c | awk '{printf $2}')-${CXX_COMPILER##*-} main" -y; fi 30 | - sudo apt-get update 31 | - sudo apt-get install "${CXX_COMPILER}" cmake pkg-config $(if [[ "${CXX_COMPILER}" =~ "clang++-" ]]; then echo llvm; fi) -y 32 | 33 | before_script: 34 | - export CXX="${CXX_COMPILER}" 35 | - export GTEST_COLOR=1 36 | 37 | # not much better than git submodules, but there was never a need/want for the repo in this repo 38 | - cd .. 39 | - git clone https://github.com/google/googletest.git 40 | - cd googletest 41 | - mkdir build 42 | - cd build 43 | - cmake .. 44 | - make 45 | - cd ../../Hashes/tests 46 | 47 | script: 48 | - make 49 | - make test 50 | -------------------------------------------------------------------------------- /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.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/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/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.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/Makefile: -------------------------------------------------------------------------------- 1 | # Encryptions Makefile 2 | CXX?=g++ 3 | CXXFLAGS=-std=c++11 -Wall -c 4 | 5 | include objects.mk 6 | 7 | debug: CXXFLAGS += -g 8 | debug: all 9 | 10 | .PHONY: clean 11 | 12 | all: $(ENCRYPTIONS_OBJECTS) 13 | 14 | %.o : %.cpp %.h ../common/cryptomath.h ../common/includes.h SymAlg.h 15 | $(CXX) $(CXXFLAGS) $< -o $@ 16 | 17 | clean: 18 | rm -f $(ENCRYPTIONS_OBJECTS) 19 | -------------------------------------------------------------------------------- /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 | 31 | void rc_pq(const int w , uint64_t & p, uint64_t & q); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /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.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 | -------------------------------------------------------------------------------- /Encryptions/objects.mk: -------------------------------------------------------------------------------- 1 | ENCRYPTIONS_OBJECTS=SymAlg.o \ 2 | AES.o \ 3 | Blowfish.o \ 4 | Camellia.o \ 5 | CAST128.o \ 6 | CAST256.o \ 7 | DES.o \ 8 | DESX.o \ 9 | GOST.o \ 10 | IDEA.o \ 11 | MISTY1.o \ 12 | RC2.o \ 13 | RC4.o \ 14 | RC5.o \ 15 | RC6.o \ 16 | RC_PQ.o \ 17 | SAFERK64.o \ 18 | SEED.o \ 19 | Skipjack.o \ 20 | TDES.o \ 21 | TEA.o \ 22 | Twofish.o \ 23 | XTEA.o -------------------------------------------------------------------------------- /Hashes.h: -------------------------------------------------------------------------------- 1 | /* 2 | Hashes.h 3 | File to include to make all hash algorithms 4 | available. Also provides a testing function 5 | 6 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | THE SOFTWARE. 25 | */ 26 | 27 | #ifndef HASHES_H 28 | #define HASHES_H 29 | 30 | #include "common/cryptomath.h" 31 | #include "common/includes.h" 32 | #include "Hashes/HashAlg.h" 33 | 34 | // #include "Keccak.h" 35 | #include "Hashes/LM.h" 36 | #include "Hashes/MD2.h" 37 | #include "Hashes/MD4.h" 38 | #include "Hashes/MD5.h" 39 | #include "Hashes/RIPEMD128.h" 40 | #include "Hashes/RIPEMD160.h" 41 | #include "Hashes/SHA1.h" 42 | #include "Hashes/SHA256.h" 43 | #include "Hashes/SHA224.h" 44 | #include "Hashes/SHA512.h" 45 | #include "Hashes/SHA384.h" 46 | #include "Hashes/SHA3.h" 47 | 48 | #include "Hashes/HMAC.h" 49 | #include "Hashes/POLY1305AES.h" 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /Hashes/HMAC.h: -------------------------------------------------------------------------------- 1 | /* 2 | HMAC.h 3 | Hash-based message authentication code 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __HMAC__ 27 | #define __HMAC__ 28 | 29 | #include 30 | 31 | #include "../Hashes.h" 32 | 33 | template 34 | class HMAC{ 35 | private: 36 | std::string h; 37 | H algo; 38 | 39 | public: 40 | HMAC(std::string KEY, const std::string & MESSAGE){ 41 | std::size_t blocksize = algo.blocksize() >> 3; 42 | if (KEY.size() > blocksize) 43 | KEY = H(KEY).digest(); 44 | while (KEY.size() < blocksize) 45 | KEY += zero; 46 | std::string opad = "", ipad = ""; 47 | for(unsigned int x = 0; x < KEY.size(); x++){ 48 | opad += std::string(1, 0x5c ^ KEY[x]); 49 | ipad += std::string(1, 0x36 ^ KEY[x]); 50 | } 51 | h = H(opad + H(ipad + MESSAGE).digest()).digest(); 52 | } 53 | 54 | std::string digest(){return h;} 55 | 56 | std::string hexdigest(){return hexlify(h);} 57 | 58 | std::size_t digestsize() const {return H().digestsize();} 59 | }; 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /Hashes/HashAlg.cpp: -------------------------------------------------------------------------------- 1 | #include "HashAlg.h" 2 | 3 | HashAlg::HashAlg(){} 4 | 5 | HashAlg::~HashAlg(){} 6 | 7 | std::string HashAlg::digest(){ 8 | return unhexlify(hexdigest()); 9 | } 10 | -------------------------------------------------------------------------------- /Hashes/HashAlg.h: -------------------------------------------------------------------------------- 1 | /* 2 | Hash.h 3 | Base class for inheritance 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __HASH__ 27 | #define __HASH__ 28 | 29 | #include 30 | 31 | #include "../common/includes.h" 32 | 33 | class HashAlg{ 34 | public: 35 | HashAlg(); 36 | virtual ~HashAlg(); 37 | virtual std::string hexdigest() = 0; 38 | std::string digest(); 39 | virtual std::size_t digestsize() const = 0; // digest size in bits 40 | }; 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /Hashes/KECCAK_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | Keccak_Const.h 3 | Constant values for MD2 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 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. 22 | */ 23 | 24 | #ifndef __KECCAK_CONST__ 25 | #define __KECCAK_CONST__ 26 | 27 | /* 28 | Rotation offsets 29 | x = 3 x = 4 x = 0 x = 1 x = 2 30 | y = 2 25 39 3 10 43 31 | y = 1 55 20 36 44 6 32 | y = 0 28 27 0 1 62 33 | y = 4 56 14 18 2 61 34 | y = 3 21 8 41 45 15 35 | */ 36 | 37 | const uint8_t Keccak_rot[5][5] = {{0, 36, 3, 41, 18}, 38 | {1, 44, 10, 45, 2}, 39 | {62, 6, 43, 15, 61}, 40 | {28, 55, 25, 21, 56}, 41 | {27, 20, 39, 8, 14}}; 42 | 43 | // round constants for iota 44 | const uint64_t Keccak_RC[24] = {0x0000000000000001ULL, 45 | 0x0000000000008082ULL, 46 | 0x800000000000808AULL, 47 | 0x8000000080008000ULL, 48 | 0x000000000000808BULL, 49 | 0x0000000080000001ULL, 50 | 0x8000000080008081ULL, 51 | 0x8000000000008009ULL, 52 | 0x000000000000008AULL, 53 | 0x0000000000000088ULL, 54 | 0x0000000080008009ULL, 55 | 0x000000008000000AULL, 56 | 0x000000008000808BULL, 57 | 0x800000000000008BULL, 58 | 0x8000000000008089ULL, 59 | 0x8000000000008003ULL, 60 | 0x8000000000008002ULL, 61 | 0x8000000000000080ULL, 62 | 0x000000000000800AULL, 63 | 0x800000008000000AULL, 64 | 0x8000000080008081ULL, 65 | 0x8000000000008080ULL, 66 | 0x0000000080000001ULL, 67 | 0x8000000080008008ULL}; 68 | 69 | #endif 70 | -------------------------------------------------------------------------------- /Hashes/LM.cpp: -------------------------------------------------------------------------------- 1 | #include "LM.h" 2 | 3 | LM::LM(const std::string & key) 4 | : HashAlg() 5 | { 6 | run(key); 7 | } 8 | 9 | void LM::run(std::string key){ 10 | for(unsigned int x = 0; x < std::min(key.size(), (std::string::size_type) 14); x++){ 11 | key[x] = toupper(key[x]); 12 | } 13 | while (key.size() < 14){ 14 | key += zero; 15 | } 16 | std::string left = "", right = ""; 17 | for(uint8_t x = 0; x < 7; x++){ 18 | left += makebin((uint8_t) key[x], 8); 19 | right += makebin((uint8_t) key[x + 7], 8); 20 | } 21 | for(uint8_t x = 0; x < 8; x++){ 22 | left += left.substr(7 * x, 7) + "0"; 23 | right += right.substr(7 * x, 7) + "0"; 24 | } 25 | left = unhexlify(bintohex(left.substr(56, 64))); 26 | right = unhexlify(bintohex(right.substr(56, 64))); 27 | data = DES(left).encrypt("\x4b\x47\x53\x21\x40\x23\x24\x25") + DES(right).encrypt("\x4b\x47\x53\x21\x40\x23\x24\x25"); 28 | } 29 | 30 | std::string LM::hexdigest(){ 31 | return hexlify(data); 32 | } 33 | 34 | std::size_t LM::digestsize() const { 35 | return 128; 36 | } -------------------------------------------------------------------------------- /Hashes/LM.h: -------------------------------------------------------------------------------- 1 | /* 2 | LM.h 3 | Microsoft's LM hash 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __LM__ 27 | #define __LM__ 28 | 29 | #include "../common/includes.h" 30 | #include "../Encryptions/DES.h" 31 | #include "HashAlg.h" 32 | 33 | class LM : public HashAlg{ 34 | private: 35 | std::string data; 36 | 37 | public: 38 | LM(const std::string & key = ""); 39 | void run(std::string key); 40 | std::string hexdigest(); 41 | std::size_t digestsize() const; 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /Hashes/MD2.cpp: -------------------------------------------------------------------------------- 1 | #include "MD2.h" 2 | 3 | void MD2::checksum(std::string & c, const std::string & data){ 4 | for(uint8_t j = 0; j < 16; j++){ 5 | c[j] ^= MD2_C[data[j] ^ L]; 6 | L = c[j]; 7 | } 8 | } 9 | 10 | void MD2::process(std::string & x, const std::string & data){ 11 | for(uint8_t j = 0; j < 16; j++){ 12 | x[16 + j] = data[j]; 13 | x[32 + j] = x[16 + j] ^ x[j]; 14 | } 15 | uint8_t t = 0; 16 | for(uint8_t j = 0; j < 18; j++){ 17 | for(uint8_t k = 0; k < 48; k++){ 18 | t = x[k] = x[k] ^ MD2_C[t]; 19 | } 20 | t += j;// (t + j) & 255; 21 | } 22 | } 23 | 24 | MD2::MD2(): 25 | MerkleDamgard(), 26 | L(0), 27 | C(std::string(16, 0)), 28 | X(std::string(48, 0)) 29 | { 30 | } 31 | 32 | MD2::MD2(const std::string & data): 33 | MD2() 34 | { 35 | update(data); 36 | } 37 | 38 | void MD2::update(const std::string & data){ 39 | stack += data; 40 | for(unsigned int i = 0; i < (stack.size() >> 4); i++){ 41 | std::string temp = data.substr(i << 4, 16); 42 | checksum(C, temp); 43 | process(X, temp); 44 | } 45 | stack = stack.substr(stack.size() - (stack.size() & 15), 16); 46 | } 47 | 48 | std::string MD2::hexdigest(){ 49 | uint8_t pad = 16 - (stack.size() & 15); 50 | std::string data = stack + std::string(pad, pad); 51 | std::string c = C; 52 | checksum(c, data); 53 | std::string x = X; 54 | process(x, data); 55 | process(x, c); 56 | return hexlify(x.substr(0, 16)); 57 | } 58 | 59 | std::size_t MD2::blocksize() const { 60 | return 128; 61 | }; 62 | 63 | std::size_t MD2::digestsize() const { 64 | return 128; 65 | }; 66 | -------------------------------------------------------------------------------- /Hashes/MD2.h: -------------------------------------------------------------------------------- 1 | /* 2 | MD2.h 3 | RSA Data Security, Inc. MD2 Message Digest Algorithm 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __MD2__ 27 | #define __MD2__ 28 | 29 | #include 30 | 31 | #include "../common/includes.h" 32 | #include "MerkleDamgard.h" 33 | 34 | #include "MD2_Const.h" 35 | 36 | class MD2 : public MerkleDamgard{ 37 | private: 38 | uint8_t L = 0; 39 | std::string C; 40 | std::string X; 41 | 42 | void checksum(std::string & c, const std::string & data); 43 | void process(std::string & x, const std::string & data); 44 | void run(); 45 | 46 | public: 47 | MD2(); 48 | MD2(const std::string & data); 49 | void update(const std::string & data = ""); 50 | std::string hexdigest(); 51 | std::size_t blocksize() const; 52 | std::size_t digestsize() const; 53 | }; 54 | 55 | #endif 56 | -------------------------------------------------------------------------------- /Hashes/MD2_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | MD2_const.h 3 | Constant values for MD2 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __MD2_CONST__ 27 | #define __MD2_CONST__ 28 | 29 | const uint8_t MD2_C[256] = {0x29, 0x2E, 0x43, 0xC9, 0xA2, 0xD8, 0x7C, 0x01, 0x3D, 0x36, 0x54, 0xA1, 0xEC, 0xF0, 0x06, 0x13, 30 | 0x62, 0xA7, 0x05, 0xF3, 0xC0, 0xC7, 0x73, 0x8C, 0x98, 0x93, 0x2B, 0xD9, 0xBC, 0x4C, 0x82, 0xCA, 31 | 0x1E, 0x9B, 0x57, 0x3C, 0xFD, 0xD4, 0xE0, 0x16, 0x67, 0x42, 0x6F, 0x18, 0x8A, 0x17, 0xE5, 0x12, 32 | 0xBE, 0x4E, 0xC4, 0xD6, 0xDA, 0x9E, 0xDE, 0x49, 0xA0, 0xFB, 0xF5, 0x8E, 0xBB, 0x2F, 0xEE, 0x7A, 33 | 0xA9, 0x68, 0x79, 0x91, 0x15, 0xB2, 0x07, 0x3F, 0x94, 0xC2, 0x10, 0x89, 0x0B, 0x22, 0x5F, 0x21, 34 | 0x80, 0x7F, 0x5D, 0x9A, 0x5A, 0x90, 0x32, 0x27, 0x35, 0x3E, 0xCC, 0xE7, 0xBF, 0xF7, 0x97, 0x03, 35 | 0xFF, 0x19, 0x30, 0xB3, 0x48, 0xA5, 0xB5, 0xD1, 0xD7, 0x5E, 0x92, 0x2A, 0xAC, 0x56, 0xAA, 0xC6, 36 | 0x4F, 0xB8, 0x38, 0xD2, 0x96, 0xA4, 0x7D, 0xB6, 0x76, 0xFC, 0x6B, 0xE2, 0x9C, 0x74, 0x04, 0xF1, 37 | 0x45, 0x9D, 0x70, 0x59, 0x64, 0x71, 0x87, 0x20, 0x86, 0x5B, 0xCF, 0x65, 0xE6, 0x2D, 0xA8, 0x02, 38 | 0x1B, 0x60, 0x25, 0xAD, 0xAE, 0xB0, 0xB9, 0xF6, 0x1C, 0x46, 0x61, 0x69, 0x34, 0x40, 0x7E, 0x0F, 39 | 0x55, 0x47, 0xA3, 0x23, 0xDD, 0x51, 0xAF, 0x3A, 0xC3, 0x5C, 0xF9, 0xCE, 0xBA, 0xC5, 0xEA, 0x26, 40 | 0x2C, 0x53, 0x0D, 0x6E, 0x85, 0x28, 0x84, 0x09, 0xD3, 0xDF, 0xCD, 0xF4, 0x41, 0x81, 0x4D, 0x52, 41 | 0x6A, 0xDC, 0x37, 0xC8, 0x6C, 0xC1, 0xAB, 0xFA, 0x24, 0xE1, 0x7B, 0x08, 0x0C, 0xBD, 0xB1, 0x4A, 42 | 0x78, 0x88, 0x95, 0x8B, 0xE3, 0x63, 0xE8, 0x6D, 0xE9, 0xCB, 0xD5, 0xFE, 0x3B, 0x00, 0x1D, 0x39, 43 | 0xF2, 0xEF, 0xB7, 0x0E, 0x66, 0x58, 0xD0, 0xE4, 0xA6, 0x77, 0x72, 0xF8, 0xEB, 0x75, 0x4B, 0x0A, 44 | 0x31, 0x44, 0x50, 0xB4, 0x8F, 0xED, 0x1F, 0x1A, 0xDB, 0x99, 0x8D, 0x33, 0x9F, 0x11, 0x83, 0x14}; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /Hashes/MD4.cpp: -------------------------------------------------------------------------------- 1 | #include "MD4.h" 2 | 3 | void MD4::run(const std::string & data, context & state) const { 4 | for(unsigned int i = 0; i < (data.size() >> 6); i++){ 5 | uint32_t a = state.h0, b = state.h1, c = state.h2, d = state.h3; 6 | std::string W = data.substr(i << 6, 64); 7 | uint32_t w[16]; 8 | for(uint8_t x = 0; x < 16; x++){ 9 | w[x] = toint(W.substr(x << 2, 4), 256); 10 | } 11 | for(uint8_t x = 0; x < 48; x++){ 12 | uint32_t F[4] = {(b & c) | ((~b) & d), ((b & c) | (b & d) | (c & d)) + 0x5a827999, (b ^ c ^ d) + 0x6ed9eba1}; 13 | uint32_t f = F[x >> 4]; 14 | uint32_t K[3][16]; 15 | for(uint8_t y = 0; y < 16; y++){ 16 | K[0][y] = y; 17 | } 18 | for(uint8_t y = 0; y < 16; y++){ 19 | K[1][y] = 4 * (y & 3) + (y >> 2); 20 | } 21 | K[2][0] = 0; K[2][1] = 8; K[2][2] = 4; K[2][3] = 12; K[2][4] = 2; K[2][5] = 10; K[2][6] = 6; K[2][7] = 14; K[2][8] = 1; K[2][9] = 9; K[2][10] = 5; K[2][11] = 13; K[2][12] = 3; K[2][13] = 11; K[2][14] = 7; K[2][15] = 15; 22 | uint32_t k[16]; 23 | for(uint8_t y = 0; y < 16; y++){ 24 | k[y] = K[x >> 4][y]; 25 | } 26 | uint32_t t = d; 27 | d = c; 28 | c = b; 29 | b = ROL(a + f + w[k[x & 15]], MD4_R[x >> 4][x & 3], 32); 30 | a = t; 31 | } 32 | state.h0 += a; 33 | state.h1 += b; 34 | state.h2 += c; 35 | state.h3 += d; 36 | } 37 | } 38 | 39 | MD4::MD4(): 40 | MerkleDamgard(), 41 | ctx(0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476) 42 | { 43 | } 44 | 45 | MD4::MD4(const std::string & data): 46 | MD4() 47 | { 48 | update(data); 49 | } 50 | 51 | void MD4::update(const std::string & data){ 52 | clen += data.size(); 53 | stack += data; 54 | 55 | std::string temp = ""; 56 | for(unsigned int i = 0; i < ((clen >> 6) << 6); i += 4){ 57 | temp += little_end(stack.substr(i, 4), 256); 58 | } 59 | run(temp, ctx); 60 | stack = stack.substr(stack.size() - (stack.size() & 63), 64); 61 | } 62 | 63 | std::string MD4::hexdigest(){ 64 | context out = ctx; 65 | std::string data = stack + "\x80" + std::string((((clen & 63) > 55)?119:55) - (clen & 63), 0); 66 | 67 | std::string temp = ""; 68 | for(unsigned int i = 0; i < data.size(); i += 4){ 69 | temp += little_end(data.substr(i, 4), 256); 70 | } 71 | 72 | std::string len = unhexlify(makehex(clen << 3, 16)); 73 | temp += len.substr(4, 4) + len.substr(0, 4); 74 | 75 | run(temp, out); 76 | 77 | return little_end(makehex(out.h0, 8), 16) + little_end(makehex(out.h1, 8), 16) + little_end(makehex(out.h2, 8), 16) + little_end(makehex(out.h3, 8), 16); 78 | } 79 | 80 | std::size_t MD4::blocksize() const { 81 | return 512; 82 | } 83 | 84 | std::size_t MD4::digestsize() const { 85 | return 128; 86 | } 87 | -------------------------------------------------------------------------------- /Hashes/MD4.h: -------------------------------------------------------------------------------- 1 | /* 2 | MD4.h 3 | MD4 hashing algorithm 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __MD4__ 27 | #define __MD4__ 28 | 29 | #include "../common/cryptomath.h" 30 | #include "../common/includes.h" 31 | #include "MerkleDamgard.h" 32 | 33 | #include "MD4_Const.h" 34 | 35 | class MD4 : public MerkleDamgard{ 36 | private: 37 | struct context{ 38 | uint32_t h0, h1, h2, h3; 39 | context(uint32_t h0, uint32_t h1, uint32_t h2, uint32_t h3) : 40 | h0(h0), 41 | h1(h1), 42 | h2(h2), 43 | h3(h3) 44 | {} 45 | ~context(){ 46 | h0 = h1 = h2 = h3 = 0; 47 | } 48 | }; 49 | context ctx; 50 | 51 | void run(const std::string & data, context & ctx) const; 52 | 53 | public: 54 | MD4(); 55 | MD4(const std::string & data); 56 | void update(const std::string & data = ""); 57 | std::string hexdigest(); 58 | std::size_t blocksize() const; 59 | std::size_t digestsize() const; 60 | }; 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /Hashes/MD4_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | MD4_Const.h 3 | Constant values for MD4 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __MD4_CONST__ 27 | #define __MD4_CONST__ 28 | 29 | const uint8_t MD4_R[3][4] = {{3, 7, 11, 19}, 30 | {3, 5, 9, 13}, 31 | {3, 9, 11, 15}}; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /Hashes/MD5.cpp: -------------------------------------------------------------------------------- 1 | #include "MD5.h" 2 | 3 | std::string MD5::to_little_end(const std::string & data) const { 4 | std::string result; 5 | for(unsigned int x = 0; x < (data.size() >> 2); x++){ 6 | result += little_end(data.substr(x << 2, 4), 256); 7 | } 8 | return result; 9 | } 10 | 11 | void MD5::calc(const std::string & data, context & state) const { 12 | for(unsigned int i = 0; i < (data.size() >> 6); i++){ 13 | uint32_t a = state.h0, b = state.h1, c = state.h2, d = state.h3; 14 | uint32_t w[16]; 15 | for(uint8_t x = 0; x < 16; x++){ 16 | w[x] = toint(data.substr(i << 6, 64).substr(x << 2, 4), 256); 17 | } 18 | for(uint8_t x = 0; x < 64; x++){ 19 | uint32_t f = 0, g = 0; 20 | if (x < 16){ 21 | f = (b & c) | ((~ b) & d); 22 | g = x; 23 | } 24 | else if ((16 <= x) && (x < 32)){ 25 | f = (d & b) | ((~ d) & c); 26 | g = (5 * x + 1) & 15; 27 | } 28 | else if ((32 <= x) && (x < 48)){ 29 | f = b ^ c ^ d; 30 | g = (3 * x + 5) & 15; 31 | } 32 | else if (48 <= x){ 33 | f = c ^ (b | (~ d)); 34 | g = (7 * x) & 15; 35 | } 36 | uint32_t t = d; 37 | d = c; 38 | c = b; 39 | b += ROL(a + f + MD5_K[x] + w[g], MD5_R[x], 32); 40 | a = t; 41 | } 42 | state.h0 += a; 43 | state.h1 += b; 44 | state.h2 += c; 45 | state.h3 += d; 46 | } 47 | } 48 | 49 | MD5::MD5() : 50 | MerkleDamgard(), 51 | ctx(0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476) 52 | {} 53 | 54 | MD5::MD5(const std::string & str) : 55 | MD5() 56 | { 57 | update(str); 58 | } 59 | 60 | void MD5::update(const std::string & str){ 61 | std::string data = stack + str; 62 | stack.clear(); 63 | std::string::size_type size = ((data.size() >> 6) << 6); 64 | if ( std::string::size_type rem = ( data.size() - size ) ){ 65 | stack = data.substr(size, rem); 66 | } 67 | calc(to_little_end(data.substr(0, size)), ctx); 68 | clen += size; 69 | } 70 | 71 | std::string MD5::hexdigest(){ 72 | context tmp = ctx; 73 | uint16_t size = stack.size(); 74 | std::string last = stack + "\x80" + std::string((((size & 63) > 55)?119:55) - (size & 63), 0); 75 | last = to_little_end(last); 76 | std::string temp = unhexlify(makehex((clen+size) << 3, 16)); 77 | last += temp.substr(4, 4) + temp.substr(0, 4); 78 | calc(last, tmp); 79 | return little_end(makehex(tmp.h0, 8)) + little_end(makehex(tmp.h1, 8)) + little_end(makehex(tmp.h2, 8)) + little_end(makehex(tmp.h3, 8)); 80 | } 81 | 82 | std::size_t MD5::blocksize() const { 83 | return 512; 84 | } 85 | 86 | std::size_t MD5::digestsize() const { 87 | return 128; 88 | } -------------------------------------------------------------------------------- /Hashes/MD5.h: -------------------------------------------------------------------------------- 1 | /* 2 | MD5.h 3 | MD5 hashing algorithm 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __MD5__ 27 | #define __MD5__ 28 | 29 | #include "../common/cryptomath.h" 30 | #include "../common/includes.h" 31 | #include "MerkleDamgard.h" 32 | 33 | #include "MD5_Const.h" 34 | 35 | class MD5 : public MerkleDamgard{ 36 | private: 37 | struct context{ 38 | uint32_t h0, h1, h2, h3; 39 | context(uint32_t h0, uint32_t h1, uint32_t h2, uint32_t h3) : 40 | h0(h0), 41 | h1(h1), 42 | h2(h2), 43 | h3(h3) 44 | {} 45 | ~context(){ 46 | h0 = h1 = h2 = h3 = 0; 47 | } 48 | }; 49 | context ctx; 50 | 51 | std::string to_little_end(const std::string & data) const; 52 | void calc(const std::string & data, context & state) const; 53 | 54 | public: 55 | MD5(); 56 | MD5(const std::string & data); 57 | void update(const std::string & data); 58 | std::string hexdigest(); 59 | std::size_t blocksize() const; 60 | std::size_t digestsize() const; 61 | }; 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /Hashes/MD5_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | MD5_Const.h 3 | Constants for the MD5 algorithm 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __MD5_CONST__ 27 | #define __MD5_CONST__ 28 | 29 | const uint8_t MD5_R[64] = { 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 30 | 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 31 | 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 32 | 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}; 33 | 34 | const uint32_t MD5_K[64] = {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 35 | 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 36 | 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 37 | 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 38 | 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 39 | 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 40 | 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 41 | 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391}; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /Hashes/Makefile: -------------------------------------------------------------------------------- 1 | # Hashes Makefile 2 | CXX?=g++ 3 | CXXFLAGS=-std=c++11 -Wall -c 4 | 5 | include objects.mk 6 | 7 | debug: CXXFLAGS += -g 8 | debug: all 9 | 10 | .PHONY: clean 11 | 12 | all: $(HASHES_OBJECTS) 13 | 14 | %.o : %.cpp %.h HashAlg.h ../common/cryptomath.h ../common/includes.h 15 | $(CXX) $(CXXFLAGS) $< -o $@ 16 | 17 | clean: 18 | rm -f $(HASHES_OBJECTS) 19 | -------------------------------------------------------------------------------- /Hashes/MerkleDamgard.cpp: -------------------------------------------------------------------------------- 1 | #include "MerkleDamgard.h" 2 | 3 | MerkleDamgard::MerkleDamgard() 4 | : HashAlg(), 5 | stack(), 6 | clen(0) 7 | {} 8 | 9 | MerkleDamgard::~MerkleDamgard(){} -------------------------------------------------------------------------------- /Hashes/MerkleDamgard.h: -------------------------------------------------------------------------------- 1 | /* 2 | Merkle–Damgård Base type 3 | Base class for inheritance 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __MERKLE_DAMGARD__ 27 | #define __MERKLE_DAMGARD__ 28 | 29 | #include "HashAlg.h" 30 | 31 | class MerkleDamgard : public HashAlg{ 32 | protected: 33 | std::string stack; 34 | uint64_t clen; 35 | 36 | public: 37 | MerkleDamgard(); 38 | virtual ~MerkleDamgard(); 39 | virtual void update(const std::string & str) = 0; 40 | virtual std::size_t blocksize() const = 0; // blocksize in bits 41 | }; 42 | 43 | #endif -------------------------------------------------------------------------------- /Hashes/POLY1305AES.cpp: -------------------------------------------------------------------------------- 1 | #include "POLY1305AES.h" 2 | 3 | const mpz_class POLY1305AES::mod1305("3fffffffffffffffffffffffffffffffb", 16); 4 | 5 | POLY1305AES::POLY1305AES(const std::string & R, const std::string & NONCE) 6 | : r(mpz_class(little_end(zfill(hexlify(R), 32, '0').substr(0, 32), 16), 16)), 7 | nonce(zfill(NONCE, 16, 0).substr(0, 16)) 8 | {} 9 | 10 | void POLY1305AES::HASH(const std::string & key, const std::string & message){ 11 | std::string aes = little_end(hexlify(AES(zfill(key, 16, 0).substr(0, 16)).encrypt(nonce)), 16); 12 | unsigned int q = (message.size() >> 4) + (bool) (message.size() & 15); 13 | std::vector m; 14 | unsigned int x = 0; 15 | while ((message.size() - x) >= 16){ 16 | m.push_back("1" + little_end(hexlify(message.substr(x, 16)), 16)); 17 | x += 16; 18 | } 19 | if (message.size()){ 20 | m.push_back(zfill("1" + little_end(hexlify(message.substr(x, 16)), 16), 33, '0')); 21 | } 22 | 23 | mpz_class h(aes, 16); 24 | for(unsigned int x = 0; x < q; x++){ 25 | mpz_class temp; mpz_pow_ui(temp.get_mpz_t(), r.get_mpz_t(), q - x); 26 | h += mpz_class(m[x], 16) * temp; 27 | } 28 | h %= mod1305; 29 | 30 | mpz_class mask("ffffffffffffffffffffffffffffffff", 16); 31 | mpz_and(mask.get_mpz_t(), mask.get_mpz_t(), h.get_mpz_t()); 32 | H = mask.get_str(16); 33 | H = little_end(H, 16); 34 | } 35 | 36 | std::string POLY1305AES::hexdigest(){ 37 | return H; 38 | } 39 | 40 | std::string POLY1305AES::digest(){ 41 | return unhexlify(H); 42 | } 43 | -------------------------------------------------------------------------------- /Hashes/POLY1305AES.h: -------------------------------------------------------------------------------- 1 | /* 2 | POLY1305AES.h 3 | The Poly1305-AES algorithm 4 | http://cr.yp.to/mac.html 5 | 6 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | THE SOFTWARE. 25 | */ 26 | 27 | #ifndef __POLYAES1305__ 28 | #define __POLYAES1305__ 29 | 30 | #include "../common/cryptomath.h" 31 | #include "../common/includes.h" 32 | #include "../Encryptions/AES.h" 33 | 34 | #include 35 | 36 | class POLY1305AES { 37 | /*bytes r[3], r[7], r[11], r[15] have to be less than 16 38 | r[4], r[8], r[12] have to = 0 mod 4 39 | */ 40 | private: 41 | static const mpz_class mod1305; 42 | 43 | const mpz_class r; 44 | const std::string nonce; 45 | std::string H; 46 | 47 | public: 48 | POLY1305AES(const std::string & R, const std::string & NONCE); 49 | void HASH(const std::string & key, const std::string & message); 50 | std::string hexdigest(); 51 | std::string digest(); 52 | }; 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /Hashes/RIPEMD128.cpp: -------------------------------------------------------------------------------- 1 | #include "RIPEMD128.h" 2 | 3 | uint32_t RIPEMD128::F(const uint32_t & x, const uint32_t & y, const uint32_t & z, const uint8_t round) const { 4 | if (round < 16){ 5 | return x ^ y ^ z; 6 | } 7 | else if (16 <= round && round < 32){ 8 | return (x & y) | (~x & z); 9 | } 10 | else if (32 <= round && round < 48){ 11 | return (x | ~y) ^ z; 12 | } 13 | else{ //if (48 <= round && round < 64) 14 | return (x & z) | (y & ~z); 15 | } 16 | } 17 | 18 | void RIPEMD128::run(const std::string & data, context & state) const { 19 | for(unsigned int i = 0; i < (data.size() >> 6); i++){ 20 | uint32_t a = state.h0, b = state.h1, c = state.h2, d = state.h3, A = state.h0, B = state.h1, C = state.h2, D = state.h3; 21 | uint32_t X[16]; 22 | for(uint8_t j = 0; j < 16; j++){ 23 | X[j] = toint(data.substr(i << 6, 64).substr(j << 2, 4), 256); 24 | } 25 | uint32_t T; 26 | for(uint8_t j = 0; j < 64; j++){ 27 | T = ROL(a + F(b, c, d, j) + X[RIPEMD_r[j]] + RIPEMD128_k[j >> 4], RIPEMD_s[j], 32); 28 | a = d; d = c; c = b; b = T; 29 | T = ROL(A + F(B, C, D, 63 - j) + X[RIPEMD_R[j]] + RIPEMD128_K[(j >> 4)], RIPEMD_S[j], 32);// + 1 to RIPEMD_K because constants are for RIPEMD160 30 | A = D; D = C; C = B; B = T; 31 | } 32 | T = state.h1 + c + D; 33 | state.h1 = state.h2 + d + A; 34 | state.h2 = state.h3 + a + B; 35 | state.h3 = state.h0 + b + C; 36 | state.h0 = T; 37 | } 38 | } 39 | 40 | RIPEMD128::RIPEMD128(): 41 | MerkleDamgard(), 42 | ctx(RIPEMD_H0, RIPEMD_H1, RIPEMD_H2, RIPEMD_H3) 43 | { 44 | } 45 | 46 | RIPEMD128::RIPEMD128(const std::string & data): 47 | RIPEMD128() 48 | { 49 | update(data); 50 | } 51 | 52 | void RIPEMD128::update(const std::string & data){ 53 | clen += data.size(); 54 | stack += data; 55 | 56 | std::string temp = ""; 57 | for(unsigned int i = 0; i < ((clen >> 6) << 6); i += 4){ 58 | temp += little_end(stack.substr(i, 4), 256); 59 | } 60 | run(temp, ctx); 61 | stack = stack.substr(stack.size() - (stack.size() & 63), 64); 62 | } 63 | 64 | std::string RIPEMD128::hexdigest(){ 65 | context out = ctx; 66 | std::string data = stack + "\x80" + std::string((((clen & 63) > 55)?119:55) - (clen & 63), 0); 67 | 68 | std::string temp = ""; 69 | for(unsigned int i = 0; i < data.size(); i += 4){ 70 | temp += little_end(data.substr(i, 4), 256); 71 | } 72 | 73 | std::string len = unhexlify(makehex(clen << 3, 16)); 74 | temp += len.substr(4, 4) + len.substr(0, 4); 75 | 76 | run(temp, out); 77 | 78 | return little_end(makehex(out.h0, 8), 16) + little_end(makehex(out.h1, 8), 16) + little_end(makehex(out.h2, 8), 16) + little_end(makehex(out.h3, 8), 16); 79 | } 80 | 81 | std::size_t RIPEMD128::blocksize() const { 82 | return 512; 83 | } 84 | 85 | std::size_t RIPEMD128::digestsize() const { 86 | return 128; 87 | } 88 | -------------------------------------------------------------------------------- /Hashes/RIPEMD128.h: -------------------------------------------------------------------------------- 1 | /* 2 | RIPEMD128.h 3 | RIPEMD128 hash function 4 | http://homes.esat.kuleuven.be/~bosselae/ripemd160.html 5 | 6 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | THE SOFTWARE. 25 | */ 26 | 27 | #ifndef __RIPEMD128__ 28 | #define __RIPEMD128__ 29 | 30 | #include "../common/cryptomath.h" 31 | #include "../common/includes.h" 32 | #include "MerkleDamgard.h" 33 | 34 | #include "RIPEMD_Const.h" 35 | #include "RIPEMD128_Const.h" 36 | 37 | class RIPEMD128 : public MerkleDamgard{ 38 | private: 39 | struct context{ 40 | uint32_t h0, h1, h2, h3; 41 | context(uint32_t h0, uint32_t h1, uint32_t h2, uint32_t h3) : 42 | h0(h0), 43 | h1(h1), 44 | h2(h2), 45 | h3(h3) 46 | {} 47 | ~context(){ 48 | h0 = h1 = h2 = h3 = 0; 49 | } 50 | }; 51 | context ctx; 52 | 53 | uint32_t F(const uint32_t & x, const uint32_t & y, const uint32_t & z, const uint8_t round) const; 54 | 55 | void run(const std::string & data, context & state) const; 56 | 57 | public: 58 | RIPEMD128(); 59 | RIPEMD128(const std::string & data); 60 | void update(const std::string & data = ""); 61 | std::string hexdigest(); 62 | std::size_t blocksize() const; 63 | std::size_t digestsize() const; 64 | }; 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /Hashes/RIPEMD128_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | RIPEMD128_Const.h 3 | Constant values for RIPEMD128 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __RIPEMD128_CONST__ 27 | #define __RIPEMD128_CONST__ 28 | 29 | const uint32_t RIPEMD128_k[4] = {0, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC}; 30 | const uint32_t RIPEMD128_K[4] = {0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0}; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /Hashes/RIPEMD160.cpp: -------------------------------------------------------------------------------- 1 | #include "RIPEMD160.h" 2 | 3 | uint32_t RIPEMD160::F(const uint32_t & x, const uint32_t & y, const uint32_t & z, const uint8_t round) const { 4 | if (round < 16){ 5 | return x ^ y ^ z; 6 | } 7 | else if (16 <= round && round < 32){ 8 | return (x & y) | (~x & z); 9 | } 10 | else if (32 <= round && round < 48){ 11 | return (x | ~y) ^ z; 12 | } 13 | else if (48 <= round && round < 64){ 14 | return (x & z) | (y & ~z); 15 | } 16 | else{ //if (64 <= round) 17 | return x ^ (y | ~z); 18 | } 19 | } 20 | 21 | std::string RIPEMD160::to_little_end(const std::string &data) const { 22 | std::string result; 23 | for(unsigned int x = 0; x < (data.size() >> 2); x++){ 24 | result += little_end(data.substr(x << 2, 4), 256); 25 | } 26 | return result; 27 | } 28 | 29 | void RIPEMD160::calc(const std::string &data, context &state) const { 30 | for(unsigned int i = 0; i < (data.size() >> 6); i++){ 31 | uint32_t a = state.h0, b = state.h1, c = state.h2, d = state.h3, e = state.h4, A = state.h0, B = state.h1, C = state.h2, D = state.h3, E = state.h4; 32 | uint32_t X[16]; 33 | for(uint8_t j = 0; j < 16; j++){ 34 | X[j] = toint(data.substr(i << 6, 64).substr(j << 2, 4), 256); 35 | } 36 | uint32_t T; 37 | for(uint8_t j = 0; j < 80; j++){ 38 | T = ROL((a + F(b, c, d, j) + X[RIPEMD_r[j]] + RIPEMD160_k[j >> 4]) & mod32, RIPEMD_s[j], 32) + e; 39 | a = e; e = d; d = ROL(c, 10, 32); c = b; b = T; 40 | T = ROL((A + F(B, C, D, 79 - j) + X[RIPEMD_R[j]] + RIPEMD160_K[j >> 4]) & mod32, RIPEMD_S[j], 32) + E; 41 | A = E; E = D; D = ROL(C, 10, 32); C = B; B = T; 42 | 43 | } 44 | T = state.h1 + c + D; 45 | state.h1 = state.h2 + d + E; 46 | state.h2 = state.h3 + e + A; 47 | state.h3 = state.h4 + a + B; 48 | state.h4 = state.h0 + b + C; 49 | state.h0 = T; 50 | } 51 | } 52 | 53 | RIPEMD160::RIPEMD160() : 54 | MerkleDamgard(), 55 | ctx(RIPEMD_H0, RIPEMD_H1, RIPEMD_H2, RIPEMD_H3, RIPEMD_H4) 56 | {} 57 | 58 | RIPEMD160::RIPEMD160(const std::string & str) : 59 | RIPEMD160() 60 | { 61 | update(str); 62 | } 63 | 64 | void RIPEMD160::update(const std::string & str){ 65 | std::string data = stack + str; 66 | stack.clear(); 67 | std::string::size_type size = ((data.size() >> 6) << 6); 68 | if ( std::string::size_type rem = ( data.size() - size ) ){ 69 | stack = data.substr(size, rem); 70 | } 71 | calc(to_little_end(data.substr(0, size)), ctx); 72 | clen += size; 73 | } 74 | 75 | std::string RIPEMD160::hexdigest(){ 76 | context tmp = ctx; 77 | uint16_t size = stack.size(); 78 | std::string last = stack + "\x80" + std::string((((size & 63) > 55)?119:55) - (size & 63), 0); 79 | last = to_little_end(last); 80 | std::string temp = unhexlify(makehex(((clen+size) << 3), 16)); 81 | last += temp.substr(4, 4) + temp.substr(0, 4); 82 | calc(last, tmp); 83 | return little_end(makehex(tmp.h0, 8)) + little_end(makehex(tmp.h1, 8)) + little_end(makehex(tmp.h2, 8)) + little_end(makehex(tmp.h3, 8)) + little_end(makehex(tmp.h4, 8)); 84 | } 85 | 86 | std::size_t RIPEMD160::blocksize() const { 87 | return 512; 88 | } 89 | 90 | std::size_t RIPEMD160::digestsize() const { 91 | return 160; 92 | } -------------------------------------------------------------------------------- /Hashes/RIPEMD160.h: -------------------------------------------------------------------------------- 1 | /* 2 | RIPEMD160.h 3 | RIPEMD160 hash function 4 | http://homes.esat.kuleuven.be/~bosselae/ripemd160.html 5 | 6 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | THE SOFTWARE. 25 | */ 26 | 27 | #ifndef __RIPEMD160__ 28 | #define __RIPEMD160__ 29 | 30 | #include "../common/cryptomath.h" 31 | #include "../common/includes.h" 32 | #include "MerkleDamgard.h" 33 | 34 | #include "RIPEMD_Const.h" 35 | #include "RIPEMD160_Const.h" 36 | 37 | class RIPEMD160 : public MerkleDamgard{ 38 | private: 39 | struct context{ 40 | uint32_t h0, h1, h2, h3, h4; 41 | context(uint32_t h0, uint32_t h1, uint32_t h2, uint32_t h3, uint32_t h4) : 42 | h0(h0), 43 | h1(h1), 44 | h2(h2), 45 | h3(h3), 46 | h4(h4) 47 | {} 48 | ~context(){ 49 | h0 = h1 = h2 = h3 = h4 = 0; 50 | } 51 | }; 52 | context ctx; 53 | 54 | uint32_t F(const uint32_t & x, const uint32_t & y, const uint32_t & z, const uint8_t round) const; 55 | 56 | std::string to_little_end(const std::string & data) const; 57 | void calc(const std::string & data, context & state) const; 58 | 59 | public: 60 | RIPEMD160(); 61 | RIPEMD160(const std::string & data); 62 | void update(const std::string & data); 63 | std::string hexdigest(); 64 | std::size_t blocksize() const; 65 | std::size_t digestsize() const; 66 | }; 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /Hashes/RIPEMD160_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | RIPEMD160_Const.h 3 | Constant values for RIPEMD160 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __RIPEMD160_CONST__ 27 | #define __RIPEMD160_CONST__ 28 | 29 | const uint32_t RIPEMD160_k[5] = {0, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E}; 30 | const uint32_t RIPEMD160_K[5] = {0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0}; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /Hashes/RIPEMD_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | RIPEMD_Const.h 3 | Constant values shared by RIPEMD128 and RIPEMD160 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __RIPEMD_CONST__ 27 | #define __RIPEMD_CONST__ 28 | 29 | const uint32_t RIPEMD_H0 = 0x67452301; 30 | const uint32_t RIPEMD_H1 = 0xEFCDAB89; 31 | const uint32_t RIPEMD_H2 = 0x98BADCFE; 32 | const uint32_t RIPEMD_H3 = 0x10325476; 33 | const uint32_t RIPEMD_H4 = 0xC3D2E1F0; 34 | 35 | const uint8_t RIPEMD_s[80] = {11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, 36 | 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, 37 | 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, 38 | 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12, 39 | 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6}; 40 | 41 | const uint8_t RIPEMD_S[80] = {8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, 42 | 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, 43 | 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, 44 | 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8, 45 | 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11}; 46 | 47 | const uint8_t RIPEMD_r[80] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 48 | 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, 49 | 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, 50 | 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, 51 | 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13}; 52 | 53 | const uint8_t RIPEMD_R[80] = {5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, 54 | 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, 55 | 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, 56 | 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, 57 | 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11}; 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /Hashes/SHA1.cpp: -------------------------------------------------------------------------------- 1 | #include "SHA1.h" 2 | 3 | void SHA1::calc(const std::string & data, context & state) const { 4 | for(unsigned int n = 0; n < (data.size() >> 6); n++){ 5 | std::string temp = data.substr(n << 6, 64); 6 | uint32_t skey[80]; 7 | for(uint8_t x = 0; x < 16; x++){ 8 | skey[x] = toint(temp.substr(x << 2, 4), 256); 9 | } 10 | for(uint8_t x = 16; x < 80; x++){ 11 | skey[x] = ROL((skey[x - 3] ^ skey[x - 8] ^ skey[x - 14] ^ skey[x - 16]), 1, 32); 12 | } 13 | uint32_t a = state.h0, b = state.h1, c = state.h2, d = state.h3, e = state.h4; 14 | for(uint8_t j = 0; j < 80; j++){ 15 | uint32_t f = 0, k = 0; 16 | if (j <= 19){ 17 | f = (b & c) | ((~b) & d); 18 | k = 0x5A827999; 19 | } 20 | if (20 <= j && j <= 39){ 21 | f = b ^ c ^ d; 22 | k = 0x6ED9EBA1; 23 | } 24 | if (40 <= j && j <= 59){ 25 | f = (b & c) | (b & d) | (c & d); 26 | k = 0x8F1BBCDC; 27 | } 28 | if (60 <= j && j <= 79){ 29 | f = b ^ c ^ d; 30 | k = 0xCA62C1D6; 31 | } 32 | uint32_t temp = ROL(a, 5, 32) + f + e + k + skey[j]; 33 | e = d; 34 | d = c; 35 | c = ROL(b, 30, 32); 36 | b = a; 37 | a = temp; 38 | } 39 | state.h0 += a; 40 | state.h1 += b; 41 | state.h2 += c; 42 | state.h3 += d; 43 | state.h4 += e; 44 | } 45 | } 46 | 47 | SHA1::SHA1() : 48 | MerkleDamgard(), 49 | ctx(0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0) 50 | {} 51 | 52 | SHA1::SHA1(const std::string & str) : 53 | SHA1() 54 | { 55 | update(str); 56 | } 57 | 58 | void SHA1::update(const std::string & str){ 59 | std::string data = stack + str; 60 | stack.clear(); 61 | std::string::size_type size = ((data.size() >> 6) << 6); 62 | if ( std::string::size_type rem = ( data.size() - size ) ){ 63 | stack = data.substr(size, rem); 64 | } 65 | calc(data.substr(0, size), ctx); 66 | clen += size; 67 | } 68 | 69 | std::string SHA1::hexdigest(){ 70 | context tmp = ctx; 71 | uint16_t size = stack.size(); 72 | std::string last = stack + "\x80" + std::string((((size & 63) > 55)?119:55) - (size & 63), 0) + unhexlify(makehex((clen+size) << 3, 16)); 73 | calc(last, tmp); 74 | return makehex(tmp.h0, 8) + makehex(tmp.h1, 8) + makehex(tmp.h2, 8) + makehex(tmp.h3, 8) + makehex(tmp.h4, 8); 75 | } 76 | 77 | std::size_t SHA1::blocksize() const { 78 | return 512; 79 | } 80 | 81 | std::size_t SHA1::digestsize() const { 82 | return 160; 83 | } -------------------------------------------------------------------------------- /Hashes/SHA1.h: -------------------------------------------------------------------------------- 1 | /* 2 | SHA1.h 3 | Secure Hash Algorithm 1 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __SHA1__ 27 | #define __SHA1__ 28 | 29 | #include "../common/cryptomath.h" 30 | #include "../common/includes.h" 31 | #include "MerkleDamgard.h" 32 | 33 | class SHA1 : public MerkleDamgard{ 34 | private: 35 | struct context{ 36 | uint32_t h0, h1, h2, h3, h4; 37 | 38 | context(uint32_t h0, uint32_t h1, uint32_t h2, uint32_t h3, uint32_t h4) : 39 | h0(h0), 40 | h1(h1), 41 | h2(h2), 42 | h3(h3), 43 | h4(h4) 44 | {} 45 | ~context(){ 46 | h0 = h1 = h2 = h3 = h4 = 0; 47 | } 48 | }; 49 | 50 | context ctx; 51 | 52 | void calc(const std::string & data, context & state) const; 53 | 54 | public: 55 | SHA1(); 56 | SHA1(const std::string & str); 57 | void update(const std::string & str); 58 | std::string hexdigest(); 59 | std::size_t blocksize() const; 60 | std::size_t digestsize() const; 61 | }; 62 | 63 | #endif 64 | -------------------------------------------------------------------------------- /Hashes/SHA224.cpp: -------------------------------------------------------------------------------- 1 | #include "SHA224.h" 2 | 3 | void SHA224::original_h(){ 4 | ctx.h0 = 0xc1059ed8; 5 | ctx.h1 = 0x367cd507; 6 | ctx.h2 = 0x3070dd17; 7 | ctx.h3 = 0xf70e5939; 8 | ctx.h4 = 0xffc00b31; 9 | ctx.h5 = 0x68581511; 10 | ctx.h6 = 0x64f98fa7; 11 | ctx.h7 = 0xbefa4fa4; 12 | } 13 | 14 | SHA224::SHA224() : 15 | SHA256() 16 | { 17 | original_h(); 18 | } 19 | 20 | SHA224::SHA224(const std::string & str) : 21 | SHA224() 22 | { 23 | update(str); 24 | } 25 | 26 | std::string SHA224::hexdigest(){ 27 | return SHA256::hexdigest().substr(0, 56); 28 | } 29 | 30 | std::size_t SHA224::blocksize() const { 31 | return 512; 32 | } 33 | 34 | std::size_t SHA224::digestsize() const { 35 | return 224; 36 | } -------------------------------------------------------------------------------- /Hashes/SHA224.h: -------------------------------------------------------------------------------- 1 | /* 2 | SHA224.h 3 | The SHA2 algorithm SHA-224 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __SHA224__ 27 | #define __SHA224__ 28 | 29 | #include "SHA256.h" 30 | 31 | class SHA224 : public SHA256{ 32 | private: 33 | void original_h(); 34 | 35 | public: 36 | SHA224(); 37 | SHA224(const std::string & data); 38 | std::string hexdigest(); 39 | std::size_t blocksize() const; 40 | std::size_t digestsize() const; 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /Hashes/SHA256.cpp: -------------------------------------------------------------------------------- 1 | #include "SHA256.h" 2 | 3 | uint32_t SHA256::S0(const uint32_t & value) const { 4 | return ROR(value, 2, 32) ^ ROR(value, 13, 32) ^ ROR(value, 22, 32); 5 | } 6 | 7 | uint32_t SHA256::S1(const uint32_t & value) const { 8 | return ROR(value, 6, 32) ^ ROR(value, 11, 32) ^ ROR(value, 25, 32); 9 | } 10 | 11 | uint32_t SHA256::s0(const uint32_t & value) const { 12 | return ROR(value, 7, 32) ^ ROR(value, 18, 32) ^ (value >> 3); 13 | } 14 | 15 | uint32_t SHA256::s1(const uint32_t & value) const { 16 | return ROR(value, 17, 32) ^ ROR(value, 19, 32) ^ (value >> 10); 17 | } 18 | 19 | void SHA256::original_h(){ 20 | ctx.h0 = 0x6a09e667; 21 | ctx.h1 = 0xbb67ae85; 22 | ctx.h2 = 0x3c6ef372; 23 | ctx.h3 = 0xa54ff53a; 24 | ctx.h4 = 0x510e527f; 25 | ctx.h5 = 0x9b05688c; 26 | ctx.h6 = 0x1f83d9ab; 27 | ctx.h7 = 0x5be0cd19; 28 | } 29 | 30 | void SHA256::calc(const std::string &data, context &state) const { 31 | for(unsigned int n = 0; n < (data.size() >> 6); n++){ 32 | std::string temp = data.substr(n << 6, 64); 33 | uint32_t skey[64]; 34 | for(uint8_t x = 0; x < 16; x++){ 35 | skey[x] = toint(temp.substr(x << 2, 4), 256); 36 | } 37 | for(uint8_t x = 16; x < 64; x++){ 38 | skey[x] = s1(skey[x - 2]) + skey[x - 7] + s0(skey[x - 15]) + skey[x - 16]; 39 | } 40 | uint32_t a = state.h0, b = state.h1, c = state.h2, d = state.h3, e = state.h4, f = state.h5, g = state.h6, h = state.h7; 41 | for(uint8_t x = 0; x < 64; x++){ 42 | uint32_t t1 = h + S1(e) + Ch(e, f, g) + SHA256_K[x] + skey[x]; 43 | uint32_t t2 = S0(a) + Maj(a, b, c); 44 | h = g; 45 | g = f; 46 | f = e; 47 | e = d + t1; 48 | d = c; 49 | c = b; 50 | b = a; 51 | a = t1 + t2; 52 | } 53 | state.h0 += a; state.h1 += b; state.h2 += c; state.h3 += d; state.h4 += e; state.h5 += f; state.h6 += g; state.h7 += h; 54 | } 55 | } 56 | 57 | SHA256::SHA256() : 58 | MerkleDamgard(), 59 | ctx() 60 | { 61 | original_h(); 62 | } 63 | 64 | SHA256::SHA256(const std::string & str) : 65 | SHA256() 66 | { 67 | update(str); 68 | } 69 | 70 | void SHA256::update(const std::string &str){ 71 | std::string data = stack + str; 72 | stack.clear(); 73 | std::string::size_type size = ((data.size() >> 6) << 6); 74 | if ( std::string::size_type rem = ( data.size() - size ) ){ 75 | stack = data.substr(size, rem); 76 | } 77 | calc(data.substr(0, size), ctx); 78 | clen += size; 79 | } 80 | 81 | std::string SHA256::hexdigest(){ 82 | context tmp = ctx; 83 | uint32_t size = stack.size(); 84 | std::string last = stack + "\x80" + std::string((((size & 63) > 55)?119:55) - (size & 63), 0) + unhexlify(makehex((clen+size) << 3, 16)); 85 | calc(last, tmp); 86 | return makehex(tmp.h0, 8) + makehex(tmp.h1, 8) + makehex(tmp.h2, 8) + makehex(tmp.h3, 8) + makehex(tmp.h4, 8) + makehex(tmp.h5, 8) + makehex(tmp.h6, 8) + makehex(tmp.h7, 8); 87 | } 88 | 89 | std::size_t SHA256::blocksize() const { 90 | return 512; 91 | } 92 | 93 | std::size_t SHA256::digestsize() const { 94 | return 256; 95 | } -------------------------------------------------------------------------------- /Hashes/SHA256.h: -------------------------------------------------------------------------------- 1 | /* 2 | SHA256.h 3 | The SHA2 algorithm SHA-256 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __SHA256__ 27 | #define __SHA256__ 28 | 29 | #include "../common/cryptomath.h" 30 | #include "../common/includes.h" 31 | #include "MerkleDamgard.h" 32 | 33 | #include "SHA2_Functions.h" 34 | #include "SHA256_Const.h" 35 | 36 | class SHA256 : public MerkleDamgard{ 37 | protected: 38 | struct context{ 39 | uint32_t h0, h1, h2, h3, h4, h5, h6, h7; 40 | 41 | ~context(){ 42 | h0 = h1 = h2 = h3 = h4 = h5 = h6 = h7 = 0; 43 | } 44 | }; 45 | context ctx; 46 | 47 | uint32_t S0(const uint32_t & value) const; 48 | uint32_t S1(const uint32_t & value) const; 49 | uint32_t s0(const uint32_t & value) const; 50 | uint32_t s1(const uint32_t & value) const; 51 | 52 | virtual void original_h(); 53 | 54 | void calc(const std::string & data, context & state) const; 55 | 56 | public: 57 | SHA256(); 58 | SHA256(const std::string & data); 59 | 60 | void update(const std::string &str); 61 | virtual std::string hexdigest(); 62 | virtual std::size_t blocksize() const; 63 | virtual std::size_t digestsize() const; 64 | }; 65 | 66 | #endif 67 | -------------------------------------------------------------------------------- /Hashes/SHA256_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | SHA256_Const.h 3 | Constant values for SHA256 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __SHA256_CONST__ 27 | #define __SHA256_CONST__ 28 | 29 | const uint32_t SHA256_K[64] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 30 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 31 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 32 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 33 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 34 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 35 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 36 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /Hashes/SHA2_Functions.cpp: -------------------------------------------------------------------------------- 1 | #include "SHA2_Functions.h" 2 | 3 | uint64_t Ch(const uint64_t & m, const uint64_t & n, const uint64_t & o){ 4 | return (m & n) ^ (~m & o); 5 | } 6 | 7 | uint64_t Maj(const uint64_t & m, const uint64_t & n, const uint64_t & o){ 8 | return (m & n) ^ (m & o) ^ (n & o); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /Hashes/SHA2_Functions.h: -------------------------------------------------------------------------------- 1 | /* 2 | SHA2_Functions.h 3 | Functions used by the SHA2 family of algorithms. 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __SHA2_FUNCTIONS__ 27 | #define __SHA2_FUNCTIONS__ 28 | 29 | #include 30 | 31 | uint64_t Ch(const uint64_t & m, const uint64_t & n, const uint64_t & o); 32 | uint64_t Maj(const uint64_t & m, const uint64_t & n, const uint64_t & o); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /Hashes/SHA3.h: -------------------------------------------------------------------------------- 1 | /* 2 | SHA3.h 3 | This implementation only works for SHA3 since it uses 4 | 64 bit unsigned ints as lanes instead of individual bits. 5 | 6 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 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 __SHA3__ 26 | #define __SHA3__ 27 | 28 | #include 29 | #include 30 | 31 | #include "../common/cryptomath.h" 32 | #include "../common/includes.h" 33 | #include "HashAlg.h" 34 | 35 | #include "KECCAK_Const.h" 36 | 37 | template ::type> 38 | class SHA3 : public HashAlg{ 39 | private: 40 | const unsigned int b, // width in bits (1600) 41 | w, // lane size (64) 42 | l, // log of lane size (6) 43 | nr; // number of rounds (24) 44 | 45 | typedef std::array , 5> StateArray; 46 | 47 | std::string state; 48 | std::string stack; 49 | int r; // rate (1600 - 2 * d) 50 | 51 | // Keccak Functions ///////////////// 52 | StateArray s2sa(const std::string & S) const; 53 | std::string sa2s(const StateArray & A) const; 54 | 55 | StateArray theta(const StateArray & A) const; 56 | StateArray pirho(const StateArray & A) const; 57 | StateArray chi(const StateArray & A) const; 58 | StateArray iota(const StateArray & A, const uint64_t rc) const; 59 | 60 | std::string f(const std::string & S) const; 61 | // ////////////////////////////////// 62 | 63 | // Sponge Functions ///////////////// 64 | // absorb data into given state 65 | void absorb(std::string & S, const std::string & P) const; 66 | 67 | // squeeze output data (concatenate results of f) 68 | std::string squeeze(std::string & S) const; 69 | // ////////////////////////////////// 70 | 71 | public: 72 | SHA3(); 73 | SHA3(const std::string & M); 74 | 75 | void update(const std::string & M); 76 | std::string digest(); 77 | std::string hexdigest(); 78 | std::size_t blocksize() const; 79 | std::size_t digestsize() const; 80 | }; 81 | 82 | #include "SHA3.tpp" 83 | 84 | #endif 85 | -------------------------------------------------------------------------------- /Hashes/SHA384.cpp: -------------------------------------------------------------------------------- 1 | #include "SHA384.h" 2 | 3 | void SHA384::original_h(){ 4 | ctx.h0 = 0xcbbb9d5dc1059ed8ULL; 5 | ctx.h1 = 0x629a292a367cd507ULL; 6 | ctx.h2 = 0x9159015a3070dd17ULL; 7 | ctx.h3 = 0x152fecd8f70e5939ULL; 8 | ctx.h4 = 0x67332667ffc00b31ULL; 9 | ctx.h5 = 0x8eb44a8768581511ULL; 10 | ctx.h6 = 0xdb0c2e0d64f98fa7ULL; 11 | ctx.h7 = 0x47b5481dbefa4fa4ULL; 12 | } 13 | 14 | SHA384::SHA384() : 15 | SHA512() 16 | { 17 | original_h(); 18 | } 19 | 20 | SHA384::SHA384(const std::string & str) : 21 | SHA384() 22 | { 23 | update(str); 24 | } 25 | 26 | std::string SHA384::hexdigest(){ 27 | return SHA512::hexdigest().substr(0, 96); 28 | } 29 | 30 | std::size_t SHA384::blocksize() const { 31 | return 1024; 32 | } 33 | 34 | std::size_t SHA384::digestsize() const { 35 | return 384; 36 | } -------------------------------------------------------------------------------- /Hashes/SHA384.h: -------------------------------------------------------------------------------- 1 | /* 2 | SHA348.h 3 | The SHA2 algorithm SHA-384 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __SHA384__ 27 | #define __SHA384__ 28 | 29 | #include "SHA512.h" 30 | 31 | class SHA384 : public SHA512{ 32 | private: 33 | void original_h(); 34 | 35 | public: 36 | SHA384(); 37 | SHA384(const std::string & data); 38 | std::string hexdigest(); 39 | std::size_t blocksize() const; 40 | std::size_t digestsize() const; 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /Hashes/SHA512.cpp: -------------------------------------------------------------------------------- 1 | #include "SHA512.h" 2 | 3 | uint64_t SHA512::S0(uint64_t & value) const { 4 | return ROR(value, 28, 64) ^ ROR(value, 34, 64) ^ ROR(value, 39, 64); 5 | } 6 | 7 | uint64_t SHA512::S1(uint64_t & value) const { 8 | return ROR(value, 14, 64) ^ ROR(value, 18, 64) ^ ROR(value, 41, 64); 9 | } 10 | 11 | uint64_t SHA512::s0(uint64_t & value) const { 12 | return ROR(value, 1, 64) ^ ROR(value, 8, 64) ^ (value >> 7); 13 | } 14 | 15 | uint64_t SHA512::s1(uint64_t & value) const { 16 | return ROR(value, 19, 64) ^ ROR(value, 61, 64) ^ (value >> 6); 17 | } 18 | 19 | void SHA512::original_h(){ 20 | ctx.h0 = 0x6a09e667f3bcc908ULL; 21 | ctx.h1 = 0xbb67ae8584caa73bULL; 22 | ctx.h2 = 0x3c6ef372fe94f82bULL; 23 | ctx.h3 = 0xa54ff53a5f1d36f1ULL; 24 | ctx.h4 = 0x510e527fade682d1ULL; 25 | ctx.h5 = 0x9b05688c2b3e6c1fULL; 26 | ctx.h6 = 0x1f83d9abfb41bd6bULL; 27 | ctx.h7 = 0x5be0cd19137e2179ULL; 28 | } 29 | 30 | void SHA512::calc(const std::string & data, context & state) const { 31 | for(unsigned int n = 0; n < (data.size() >> 7); n++){ 32 | std::string temp = data.substr(n << 7, 128); 33 | uint64_t skey[80]; 34 | for(uint8_t x = 0; x < 16; x++){ 35 | skey[x] = toint(temp.substr(x << 3, 8), 256); 36 | } 37 | for(uint8_t x = 16; x < 80; x++){ 38 | skey[x] = s1(skey[x - 2]) + skey[x - 7] + s0(skey[x - 15]) + skey[x - 16]; 39 | } 40 | uint64_t a = state.h0, b = state.h1, c = state.h2, d = state.h3, e = state.h4, f = state.h5, g = state.h6, h = state.h7; 41 | for(uint8_t x = 0; x < 80; x++){ 42 | uint64_t t1 = h + S1(e) + Ch(e, f, g) + SHA512_K[x] + skey[x]; 43 | uint64_t t2 = S0(a) + Maj(a, b, c); 44 | h = g; 45 | g = f; 46 | f = e; 47 | e = d + t1; 48 | d = c; 49 | c = b; 50 | b = a; 51 | a = t1 + t2; 52 | } 53 | state.h0 += a; state.h1 += b; state.h2 += c; state.h3 += d; state.h4 += e; state.h5 += f; state.h6 += g; state.h7 += h; 54 | } 55 | } 56 | 57 | SHA512::SHA512() : 58 | MerkleDamgard(), 59 | ctx() 60 | { 61 | original_h(); 62 | } 63 | 64 | SHA512::SHA512(const std::string & str) : 65 | SHA512() 66 | { 67 | update(str); 68 | } 69 | 70 | void SHA512::update(const std::string &str){ 71 | std::string data = stack + str; 72 | stack.clear(); 73 | std::string::size_type size = ((data.size() >> 7) << 7); 74 | if ( std::string::size_type rem = ( data.size() - size ) ){ 75 | stack = data.substr(size, rem); 76 | } 77 | calc(data.substr(0, size), ctx); 78 | clen += size; 79 | } 80 | 81 | std::string SHA512::hexdigest(){ 82 | context tmp = ctx; 83 | uint64_t size = stack.size(); 84 | std::string last = stack + "\x80" + std::string((((size & 127) > 111)?239:111) - (size & 127), 0) + unhexlify(makehex((clen+size) << 3, 32)); 85 | calc(last, tmp); 86 | return makehex(tmp.h0, 16) + makehex(tmp.h1, 16) + makehex(tmp.h2, 16) + makehex(tmp.h3, 16) + makehex(tmp.h4, 16) + makehex(tmp.h5, 16) + makehex(tmp.h6, 16) + makehex(tmp.h7, 16); 87 | } 88 | 89 | std::size_t SHA512::blocksize() const { 90 | return 1024; 91 | } 92 | 93 | std::size_t SHA512::digestsize() const { 94 | return 512; 95 | } -------------------------------------------------------------------------------- /Hashes/SHA512.h: -------------------------------------------------------------------------------- 1 | /* 2 | SHA512.h 3 | The SHA2 algorithm SHA-512 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __SHA512__ 27 | #define __SHA512__ 28 | 29 | #include "../common/cryptomath.h" 30 | #include "../common/includes.h" 31 | #include "MerkleDamgard.h" 32 | 33 | #include "SHA2_Functions.h" 34 | #include "SHA512_Const.h" 35 | 36 | class SHA512 : public MerkleDamgard{ 37 | protected: 38 | struct context{ 39 | uint64_t h0, h1, h2, h3, h4, h5, h6, h7; 40 | ~context(){ 41 | h0 = h1 = h2 = h3 = h4 = h5 = h6 = h7 = 0; 42 | } 43 | }; 44 | context ctx; 45 | 46 | uint64_t S0(uint64_t & value) const; 47 | uint64_t S1(uint64_t & value) const; 48 | uint64_t s0(uint64_t & value) const; 49 | uint64_t s1(uint64_t & value) const; 50 | 51 | virtual void original_h(); 52 | 53 | void calc(const std::string & data, context & state) const; 54 | 55 | public: 56 | SHA512(); 57 | SHA512(const std::string & data); 58 | void update(const std::string & str); 59 | virtual std::string hexdigest(); 60 | virtual std::size_t blocksize() const; 61 | virtual std::size_t digestsize() const; 62 | }; 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /Hashes/SHA512_Const.h: -------------------------------------------------------------------------------- 1 | /* 2 | SHA512_Const.h 3 | Constants for SHA-512 4 | 5 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at gmail.com 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | */ 25 | 26 | #ifndef __SHA512_CONST__ 27 | #define __SHA512_CONST__ 28 | 29 | const uint64_t SHA512_K[80] = { 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, 30 | 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, 31 | 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 32 | 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, 33 | 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 34 | 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, 35 | 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, 36 | 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, 37 | 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, 38 | 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, 39 | 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 40 | 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, 41 | 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, 42 | 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, 43 | 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, 44 | 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, 45 | 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 46 | 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, 47 | 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, 48 | 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL}; 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /Hashes/objects.mk: -------------------------------------------------------------------------------- 1 | HASHES_OBJECTS=HashAlg.o \ 2 | MerkleDamgard.o \ 3 | KECCAK.o \ 4 | LM.o \ 5 | MD2.o \ 6 | MD4.o \ 7 | MD5.o \ 8 | POLY1305AES.o \ 9 | RIPEMD128.o \ 10 | RIPEMD160.o \ 11 | SHA1.o \ 12 | SHA224.o \ 13 | SHA256.o \ 14 | SHA2_Functions.o \ 15 | SHA384.o \ 16 | SHA512.o 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 - 2017 Jason Lee @ calccrypto at 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. -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Hashes Library Makefile 2 | AR=ar 3 | TARGET=libHashes.a 4 | 5 | include common/objects.mk 6 | include Encryptions/objects.mk 7 | include Hashes/objects.mk 8 | 9 | .PHONY: common Encryptions Hashes clean clean-all 10 | 11 | all: $(TARGET) 12 | 13 | common: 14 | $(MAKE) -C common 15 | 16 | Encryptions: 17 | $(MAKE) -C Encryptions 18 | 19 | Hashes: 20 | $(MAKE) -C Hashes 21 | 22 | $(TARGET): common Encryptions Hashes 23 | $(AR) -r $(TARGET) $(addprefix common/, $(COMMON_OBJECTS)) $(addprefix Encryptions/, $(ENCRYPTIONS_OBJECTS)) $(addprefix Hashes/, $(HASHES_OBJECTS)) 24 | 25 | clean: 26 | rm -f $(TARGET) 27 | 28 | clean-all: clean 29 | $(MAKE) clean -C common 30 | $(MAKE) clean -C Encryptions 31 | $(MAKE) clean -C Hashes 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A library of cryptographic hashing 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/Hashes.svg?branch=master)](https://travis-ci.org/calccrypto/Hashes) 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 | Hashes:
20 |     Microsoft's LM Hash
21 |     MD2
22 |     MD4
23 |     MD5
24 |     RIPEMD-128
25 |     RIPEMD-160
26 |     SHA-1
27 |     SHA-224
28 |     SHA-256
29 |     SHA-384
30 |     SHA-512
31 |     SHA-3 (independent Keccak implementation active, but not sure on correctness)
32 | 
33 | MACs
34 |     HMAC
35 |     POLY1305AES
36 | 
37 | Build:
38 | 	make (creates the object files and libHashes.a)
39 | 
40 |     or
41 | 
42 |     g++ -std=c++11 main.cpp common/*.cpp Encryptions/*.cpp Hashes/*.cpp -lgmpxx -lgmp
43 | 
44 |     or some equivalent
45 | 
46 |     Test code and its makefile are included in the test folder.
47 | 
48 | Usage:
49 |     Ex:
50 |         SHA1 instance(data to hash in ASCII)
51 | 
52 |         To get digest:
53 |             instance.digest()
54 |         To get hex string of digest:
55 |             instance.hexdigest()
56 |         To update:
57 |             instance.update(more data)
58 | 
59 | Notes:
60 |     The GNU Multiple Precision Arithmetic Library (GMP) is needed
61 |     for POly1305-Aes (gmp.org, sudo apt-get install libdev-gmp, or
62 |     equivalent).
63 | 
64 |     My Encryptions library is needed for this library. It is included
65 |     in this project.
66 | 
67 |     The format was inspired by the Python 2.7 hashlib module
68 | 
69 |     Hashes.h provides a quick test to check that the algorithms are correct.
70 | 
-------------------------------------------------------------------------------- /common/Makefile: -------------------------------------------------------------------------------- 1 | # common Makefile 2 | CXX?=g++ 3 | CXXFLAGS=-std=c++11 -Wall -c 4 | 5 | include objects.mk 6 | 7 | debug: CXXFLAGS += -g 8 | debug: all 9 | 10 | .PHONY: clean 11 | 12 | all: $(COMMON_OBJECTS) 13 | 14 | %.o : %.cpp %.h 15 | $(CXX) $(CXXFLAGS) $< -o $@ 16 | 17 | clean: 18 | rm -f $(COMMON_OBJECTS) 19 | -------------------------------------------------------------------------------- /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 | 16 | // Greatest Common Divisor 17 | template T gcd(T a, T b){ 18 | static_assert(std::is_integral ::value, "Error: Input value should be integral."); 19 | 20 | T c = 1; 21 | while (c != 0){ 22 | c = a % b; 23 | a = b; 24 | b = c; 25 | } 26 | return a; 27 | } 28 | 29 | // Inverse Mod b * x = 1 mod a 30 | template T invmod(T a, T b){ 31 | static_assert(std::is_integral ::value, "Error: Input values should be integral."); 32 | 33 | T A = a; 34 | T x = 0, lastx = 1, y = 1, lasty = 0; 35 | while (b != 0){ 36 | T quotient = a / b; 37 | T temp = b; 38 | b = a % b; 39 | a = temp; 40 | temp = x; 41 | x = lastx - quotient * x; 42 | lastx = temp; 43 | temp = y; 44 | y = lasty - quotient * y; 45 | lasty = temp; 46 | } 47 | if (lasty < 0){ 48 | lasty += A; 49 | } 50 | return lasty; 51 | } 52 | 53 | // Faster Exponentiation by Squaring 54 | // adapted from http://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-mpz_class-based-power-function-powint-int 55 | template 56 | S POW(S base, T exp){ 57 | static_assert(std::is_integral ::value && 58 | std::is_integral ::value 59 | , "Error: Arguments should be integral."); 60 | 61 | S result = 1; 62 | while (exp != 0) 63 | { 64 | if ((exp & 1) == 1){ 65 | result *= base; 66 | } 67 | exp >>= 1; 68 | base *= base; 69 | } 70 | return result; 71 | } 72 | 73 | // Exponentiation by squaring 74 | template 75 | T POW(R base, S exponent, const T mod){ 76 | static_assert(std::is_integral ::value && 77 | std::is_integral ::value && 78 | std::is_integral ::value 79 | , "Error: Arguments should be integral."); 80 | T result = 1; 81 | while (exponent != 0){ 82 | if ((exponent & 1) == 1){ 83 | result = (result * base) % mod; 84 | } 85 | exponent >>= 1; 86 | base = (base * base) % mod; 87 | } 88 | return result; 89 | } 90 | 91 | // Two's compliment of input 92 | template 93 | T two_comp(const T & a, const uint8_t bits = 16){ 94 | static_assert(std::is_integral ::value, "Error: Input value should be integral."); 95 | return (a ^ ((1ULL << bits) - 1)) + 1; 96 | } 97 | 98 | // From a Twofish code (modified) 99 | // Bitwise rotation to the right 100 | template 101 | T ROR(T x, const uint64_t & n, const uint64_t & bits){ 102 | static_assert(std::is_integral ::value, "Error: Value being rotated should be integral."); 103 | return (x >> n) | ((x & ((1ULL << n) - 1)) << (bits - n)); 104 | } 105 | 106 | // Rotate Left 107 | // Bitwise rotation to the left 108 | template 109 | T ROL(const T & x, const uint64_t & n, const uint64_t & bits){ 110 | static_assert(std::is_integral ::value, "Error: Value being rotated should be integral."); 111 | return ROR(x, bits - n, bits); 112 | } 113 | 114 | #endif 115 | -------------------------------------------------------------------------------- /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 | 14 | // Some useful constants 15 | static const std::string zero(1, 0); 16 | static const uint8_t mod8 = 0xffU; 17 | static const uint16_t mod16 = 0xffffU; 18 | static const uint32_t mod32 = 0xffffffffUL; 19 | static const uint64_t mod64 = 0xffffffffffffffffULL; 20 | 21 | // string to integer 22 | uint64_t toint(const std::string & s, const int & base = 10); 23 | 24 | // flip the order of the octets 25 | // base = 2 for binary source 26 | // 16 for hex source 27 | // 256 for ASCII source 28 | std::string little_end(const std::string & str, const unsigned int & base = 16); 29 | 30 | // Changes a numeric value into its binary string 31 | template std::string makebin(T value, unsigned int size = 8 * sizeof(T)){ 32 | std::string out(size, '0'); 33 | if (!size){ 34 | out = ""; 35 | while (value){ 36 | out = "01"[value & 1] + out; 37 | value >>= 1; 38 | } 39 | return out; 40 | } 41 | while (value && size){ 42 | out[--size] = "01"[value & 1]; 43 | value >>= 1; 44 | } 45 | return out; 46 | } 47 | 48 | // Thanks to Ben Voigt @ stackoverflow for the makehex function 49 | // which I then adapted to makebin 50 | // Changes a numeric value to its hexadecimal string 51 | template std::string makehex(T value, unsigned int size = 2 * sizeof(T), bool caps = false){ 52 | if (!size){ 53 | std::stringstream out; 54 | out << std::hex << value; 55 | return out.str(); 56 | } 57 | 58 | std::string out(size, '0'); 59 | while (value && size){ 60 | if (caps){ 61 | out[--size] = "0123456789ABCDEF"[value & 15]; 62 | } 63 | else{ 64 | out[--size] = "0123456789abcdef"[value & 15]; 65 | } 66 | value >>= 4; 67 | } 68 | return out; 69 | } 70 | 71 | // extract 8 bits from a numeric value 72 | template uint8_t byte(const T & value, const uint16_t & n){ 73 | return (value >> (n << 3)) & 0xff; 74 | } 75 | 76 | // direct binary to hex string 77 | std::string bintohex(const std::string & in, bool caps = false); 78 | 79 | // convert an ASCII string into a string of 0s and 1s 80 | std::string binify(const std::string & in, unsigned int size = 0); 81 | 82 | // character to string of 0s and 1s 83 | std::string binify(unsigned char c); 84 | 85 | // string of 0s and 1s to ASCII 86 | std::string unbinify(const std::string & in); 87 | 88 | // ASCII string to hex string 89 | std::string hexlify(const std::string & in, bool caps = false); 90 | 91 | // character to hex string 92 | std::string hexlify(const char in, bool caps = false); 93 | 94 | // hex string to ASCII string 95 | std::string unhexlify(const std::string & in); 96 | 97 | std::string pkcs5(const std::string & data, const unsigned int & blocksize); 98 | 99 | std::string remove_pkcs5(std::string data); 100 | 101 | // adds characters to the front of the string 102 | std::string zfill(const std::string & str, const unsigned int & n, const char fill); 103 | 104 | // Left rotate a string 105 | std::string ROL(const std::string & str, const std::size_t bits); 106 | 107 | // and two strings, up to the last character of the shorter string 108 | std::string and_strings(const std::string & str1, const std::string & str2); 109 | 110 | // or two strings, up to the last character of the shorter string 111 | std::string or_strings(const std::string & str1, const std::string & str2); 112 | 113 | // xor the contents of 2 strings, up to the last character of the shorter string 114 | std::string xor_strings(const std::string & str1, const std::string & str2); 115 | 116 | #endif 117 | -------------------------------------------------------------------------------- /common/objects.mk: -------------------------------------------------------------------------------- 1 | COMMON_OBJECTS=includes.o -------------------------------------------------------------------------------- /tests/Makefile: -------------------------------------------------------------------------------- 1 | # Hashes Library Test Makefile 2 | CXX?=g++ 3 | CXXFLAGS=-std=c++11 -Wall -g -D_POSIX_C_SOURCE=200809L -I../../googletest/googletest/include -I.. 4 | LDFLAGS=-lHashes -lgmp -lgmpxx -L.. -L../../googletest/build/lib -lgtest -lgtest_main -lpthread 5 | TARGET=tests 6 | 7 | include objects.mk 8 | 9 | debug: CXXFLAGS += -g 10 | debug: all 11 | 12 | .PHONY: clean clean-all 13 | 14 | all: $(TARGET) 15 | 16 | ../libHashes.a: 17 | $(MAKE) -C .. 18 | 19 | $(HASHES_TESTCASES_OBJECTS): %.o : %.cpp testvectors/datadigest.h testvectors/keydatadigest.h 20 | $(CXX) $(CXXFLAGS) -c $< -o $@ 21 | 22 | $(TARGET): ../libHashes.a $(HASHES_TESTCASES_OBJECTS) 23 | $(CXX) $(CXXFLAGS) $(HASHES_TESTCASES_OBJECTS) $(LDFLAGS) -o $(TARGET) 24 | 25 | test: $(TARGET) 26 | ./$(TARGET) 27 | 28 | clean: 29 | rm -f $(HASHES_TESTCASES_OBJECTS) $(TARGET) 30 | 31 | clean-all: clean 32 | $(MAKE) clean-all -C .. 33 | -------------------------------------------------------------------------------- /tests/hmac.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/HMAC.h" 4 | #include "Hashes/MD5.h" 5 | #include "Hashes/SHA1.h" 6 | #include "Hashes/SHA256.h" 7 | #include "Hashes/SHA512.h" 8 | 9 | #include "testvectors/hmac/md5.h" 10 | #include "testvectors/hmac/sha1.h" 11 | #include "testvectors/hmac/sha256.h" 12 | #include "testvectors/hmac/sha512.h" 13 | 14 | TEST(HMAC, MD5) { 15 | EXPECT_EQ(HMAC ("", "").digestsize(), MD5().digestsize()); 16 | hmac_test (HMAC_MD5_TEST_VECTORS); 17 | } 18 | 19 | TEST(HMAC, SHA1) { 20 | EXPECT_EQ(HMAC ("", "").digestsize(), SHA1().digestsize()); 21 | hmac_test (HMAC_SHA1_TEST_VECTORS); 22 | } 23 | 24 | TEST(HMAC, SHA256) { 25 | EXPECT_EQ(HMAC ("", "").digestsize(), SHA256().digestsize()); 26 | hmac_test (HMAC_SHA256_TEST_VECTORS); 27 | } 28 | 29 | TEST(HMAC, SHA512) { 30 | EXPECT_EQ(HMAC ("", "").digestsize(), SHA512().digestsize()); 31 | hmac_test (HMAC_SHA512_TEST_VECTORS); 32 | } 33 | -------------------------------------------------------------------------------- /tests/keccak.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/KECCAK.h" 4 | 5 | #include "testvectors/keccak/keccak.h" 6 | 7 | TEST(KECCAK, 224) { 8 | for(DataDigest const & dd : KECCAK_224_TEST_VECTORS){ 9 | std::string data, digest; 10 | std::tie(data, digest) = dd; 11 | 12 | // unhexlify digest just in case the input digest uses uppercase hex digits 13 | EXPECT_EQ(unbinify(KECCAK(448)(data, 224)), unhexlify(digest)); 14 | } 15 | } 16 | 17 | TEST(KECCAK, 256) { 18 | for(DataDigest const & dd : KECCAK_256_TEST_VECTORS){ 19 | std::string data, digest; 20 | std::tie(data, digest) = dd; 21 | 22 | // unhexlify digest just in case the input digest uses uppercase hex digits 23 | EXPECT_EQ(unbinify(KECCAK(512)(data, 256)), unhexlify(digest)); 24 | }} 25 | 26 | TEST(KECCAK, 384) { 27 | for(DataDigest const & dd : KECCAK_384_TEST_VECTORS){ 28 | std::string data, digest; 29 | std::tie(data, digest) = dd; 30 | 31 | // unhexlify digest just in case the input digest uses uppercase hex digits 32 | EXPECT_EQ(unbinify(KECCAK(768)(data, 384)), unhexlify(digest)); 33 | }} 34 | 35 | TEST(KECCAK, 512) { 36 | for(DataDigest const & dd : KECCAK_512_TEST_VECTORS){ 37 | std::string data, digest; 38 | std::tie(data, digest) = dd; 39 | 40 | // unhexlify digest just in case the input digest uses uppercase hex digits 41 | EXPECT_EQ(unbinify(KECCAK(1024)(data, 512)), unhexlify(digest)); 42 | }} 43 | -------------------------------------------------------------------------------- /tests/lm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/LM.h" 4 | 5 | #include "testvectors/lm/lm.h" 6 | 7 | TEST(LM, testvectors) { 8 | EXPECT_EQ(LM().digestsize(), (std::size_t) 128); 9 | hash_test (LM_TEST_VECTORS); 10 | } 11 | -------------------------------------------------------------------------------- /tests/md2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/MD2.h" 4 | 5 | #include "testvectors/md/md2.h" 6 | 7 | TEST(MD2, testvectors) { 8 | EXPECT_EQ(MD2().blocksize(), (std::size_t) 128); 9 | EXPECT_EQ(MD2().digestsize(), (std::size_t) 128); 10 | hash_test (MD2_TEST_VECTORS); 11 | } 12 | -------------------------------------------------------------------------------- /tests/md4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/MD4.h" 4 | 5 | #include "testvectors/md/md4.h" 6 | 7 | TEST(MD4, testvectors) { 8 | EXPECT_EQ(MD4().blocksize(), (std::size_t) 512); 9 | EXPECT_EQ(MD4().digestsize(), (std::size_t) 128); 10 | hash_test (MD4_TEST_VECTORS); 11 | } 12 | -------------------------------------------------------------------------------- /tests/md5.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/MD5.h" 4 | 5 | #include "testvectors/md/md5.h" 6 | 7 | TEST(MD5, testvectors) { 8 | EXPECT_EQ(MD5().blocksize(), (std::size_t) 512); 9 | EXPECT_EQ(MD5().digestsize(), (std::size_t) 128); 10 | hash_test (MD5_TEST_VECTORS); 11 | } 12 | -------------------------------------------------------------------------------- /tests/objects.mk: -------------------------------------------------------------------------------- 1 | HASHES_TESTCASES_OBJECTS=keccak.o \ 2 | lm.o \ 3 | md2.o \ 4 | md4.o \ 5 | md5.o \ 6 | ripemd128.o \ 7 | ripemd160.o \ 8 | sha1.o \ 9 | sha224.o \ 10 | sha256.o \ 11 | sha384.o \ 12 | sha512.o \ 13 | sha3.o \ 14 | hmac.o \ 15 | poly1305aes.o 16 | -------------------------------------------------------------------------------- /tests/poly1305aes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/POLY1305AES.h" 4 | 5 | #include "testvectors/poly1305aes/poly1305aes.h" 6 | 7 | TEST(POLY1305AES, testvectors) { 8 | for(std::array const & tv : POLY1305AES_TEST_VECTORS){ 9 | POLY1305AES p(unhexlify(tv[2]), unhexlify(tv[3])); 10 | p.HASH(unhexlify(tv[1]), unhexlify(tv[0])); 11 | EXPECT_EQ(p.digest(), unhexlify(tv[4])); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/ripemd128.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/RIPEMD128.h" 4 | 5 | #include "testvectors/ripemd/ripemd128.h" 6 | 7 | TEST(RIPEMD128, testvectors) { 8 | EXPECT_EQ(RIPEMD128().blocksize(), (std::size_t) 512); 9 | EXPECT_EQ(RIPEMD128().digestsize(), (std::size_t) 128); 10 | hash_test (RIPEMD128_TEST_VECTORS); 11 | } 12 | -------------------------------------------------------------------------------- /tests/ripemd160.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/RIPEMD160.h" 4 | 5 | #include "testvectors/ripemd/ripemd160.h" 6 | 7 | TEST(RIPEMD160, testvectors) { 8 | EXPECT_EQ(RIPEMD160().blocksize(), (std::size_t) 512); 9 | EXPECT_EQ(RIPEMD160().digestsize(), (std::size_t) 160); 10 | hash_test (RIPEMD160_TEST_VECTORS); 11 | } 12 | -------------------------------------------------------------------------------- /tests/sha1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/SHA1.h" 4 | 5 | #include "testvectors/sha/sha1.h" 6 | 7 | TEST(SHA1, short_msg) { 8 | EXPECT_EQ(SHA1().blocksize(), (std::size_t) 512); 9 | EXPECT_EQ(SHA1().digestsize(), (std::size_t) 160); 10 | hash_test (SHA1_SHORT_MSG); 11 | } 12 | -------------------------------------------------------------------------------- /tests/sha224.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/SHA224.h" 4 | 5 | #include "testvectors/sha/sha224.h" 6 | 7 | TEST(SHA224, short_msg) { 8 | EXPECT_EQ(SHA224().blocksize(), (std::size_t) 512); 9 | EXPECT_EQ(SHA224().digestsize(), (std::size_t) 224); 10 | hash_test (SHA224_SHORT_MSG); 11 | } 12 | -------------------------------------------------------------------------------- /tests/sha256.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/SHA256.h" 4 | 5 | #include "testvectors/sha/sha256.h" 6 | 7 | TEST(SHA256, short_msg) { 8 | EXPECT_EQ(SHA256().blocksize(), (std::size_t) 512); 9 | EXPECT_EQ(SHA256().digestsize(), (std::size_t) 256); 10 | hash_test (SHA256_SHORT_MSG); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /tests/sha3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/SHA3.h" 4 | 5 | #include "testvectors/sha/sha3.h" 6 | 7 | TEST(SHA3, 224) { 8 | EXPECT_EQ(SHA3 <224> ().blocksize(), (std::size_t) (1600 - 2 * 224)); 9 | EXPECT_EQ(SHA3 <224> ().digestsize(), (std::size_t) 224); 10 | hash_test > (SHA3_224_TEST_VECTORS); 11 | } 12 | 13 | TEST(SHA3, 256) { 14 | EXPECT_EQ(SHA3 <256> ().blocksize(), (std::size_t) (1600 - 2 * 256)); 15 | EXPECT_EQ(SHA3 <256> ().digestsize(), (std::size_t) 256); 16 | hash_test > (SHA3_256_TEST_VECTORS); 17 | } 18 | 19 | TEST(SHA3, 384) { 20 | EXPECT_EQ(SHA3 <384> ().blocksize(), (std::size_t) (1600 - 2 * 384)); 21 | EXPECT_EQ(SHA3 <384> ().digestsize(), (std::size_t) 384); 22 | hash_test > (SHA3_384_TEST_VECTORS); 23 | } 24 | 25 | TEST(SHA3, 512) { 26 | EXPECT_EQ(SHA3 <512> ().blocksize(), (std::size_t) (1600 - 2 * 512)); 27 | EXPECT_EQ(SHA3 <512> ().digestsize(), (std::size_t) 512); 28 | hash_test > (SHA3_512_TEST_VECTORS); 29 | } 30 | -------------------------------------------------------------------------------- /tests/sha384.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/SHA384.h" 4 | 5 | #include "testvectors/sha/sha384.h" 6 | 7 | TEST(SHA384, short_msg) { 8 | EXPECT_EQ(SHA384().blocksize(), (std::size_t) 1024); 9 | EXPECT_EQ(SHA384().digestsize(), (std::size_t) 384); 10 | hash_test (SHA384_SHORT_MSG); 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /tests/sha512.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Hashes/SHA512.h" 4 | 5 | #include "testvectors/sha/sha512.h" 6 | 7 | TEST(SHA512, short_msg) { 8 | EXPECT_EQ(SHA512().blocksize(), (std::size_t) 1024); 9 | EXPECT_EQ(SHA512().digestsize(), (std::size_t) 512); 10 | hash_test (SHA512_SHORT_MSG); 11 | } 12 | 13 | 14 | -------------------------------------------------------------------------------- /tests/testvectors/datadigest.h: -------------------------------------------------------------------------------- 1 | /* 2 | datadigest.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 __DATA_DIGEST__ 26 | #define __DATA_DIGEST__ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | 35 | #include "common/includes.h" 36 | #include "Hashes/HashAlg.h" 37 | 38 | // all input is in hex 39 | 40 | typedef std::tuple DataDigest; 41 | 42 | static const std::string QUICKFOXLAZYDOG = hexlify("The quick brown fox jumps over the lazy dog"); 43 | 44 | template 45 | void hash_test(const std::vector & test_vectors){ 46 | static_assert(std::is_base_of ::value, "Error: Algorithm type should be a hash algorithm."); 47 | 48 | for(DataDigest const & dd : test_vectors){ 49 | std::string data, digest; 50 | std::tie(data, digest) = dd; 51 | 52 | // unhexlify digest just in case the input digest uses uppercase hex digits 53 | EXPECT_EQ(Hash(unhexlify(data)).digest(), unhexlify(digest)); 54 | } 55 | } 56 | 57 | #endif -------------------------------------------------------------------------------- /tests/testvectors/hmac/md5.h: -------------------------------------------------------------------------------- 1 | #ifndef __HMACMD5TESTVECTORS__ 2 | #define __HMACMD5TESTVECTORS__ 3 | 4 | #include "../keydatadigest.h" 5 | 6 | // Test vectors from somewhere 7 | 8 | static const std::vector HMAC_MD5_TEST_VECTORS = { 9 | std::make_tuple("", "", "74e6f7298a9c2d168935f58c001bad88"), 10 | std::make_tuple(hexlify("key"), QUICKFOXLAZYDOG, "80070713463e7749b90c2dc24911e275"), 11 | }; 12 | 13 | 14 | #endif -------------------------------------------------------------------------------- /tests/testvectors/hmac/sha1.h: -------------------------------------------------------------------------------- 1 | #ifndef __HMACSHA1TESTVECTORS__ 2 | #define __HMACSHA1TESTVECTORS__ 3 | 4 | #include "../keydatadigest.h" 5 | 6 | // Test vectors from somewhere 7 | 8 | static const std::vector HMAC_SHA1_TEST_VECTORS = { 9 | std::make_tuple("", "", "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d"), 10 | std::make_tuple(hexlify("key"), QUICKFOXLAZYDOG, "de7c9b85b8b78aa6bc8a7a36f70a90701c9db4d9"), 11 | }; 12 | 13 | 14 | #endif -------------------------------------------------------------------------------- /tests/testvectors/hmac/sha256.h: -------------------------------------------------------------------------------- 1 | #ifndef __HMACSHA256TESTVECTORS__ 2 | #define __HMACSHA256TESTVECTORS__ 3 | 4 | #include "../keydatadigest.h" 5 | 6 | // Test vectors from somewhere 7 | 8 | static const std::vector HMAC_SHA256_TEST_VECTORS = { 9 | std::make_tuple("", "", "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad"), 10 | std::make_tuple(hexlify("key"), QUICKFOXLAZYDOG, "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"), 11 | }; 12 | 13 | 14 | #endif -------------------------------------------------------------------------------- /tests/testvectors/hmac/sha512.h: -------------------------------------------------------------------------------- 1 | #ifndef __HMACSHA512TESTVECTORS__ 2 | #define __HMACSHA512TESTVECTORS__ 3 | 4 | #include "../keydatadigest.h" 5 | 6 | // Test vectors from somewhere 7 | 8 | static const std::vector HMAC_SHA512_TEST_VECTORS = { 9 | std::make_tuple("", "", "b936cee86c9f87aa5d3c6f2e84cb5a4239a5fe50480a6ec66b70ab5b1f4ac6730c6c515421b327ec1d69402e53dfb49ad7381eb067b338fd7b0cb22247225d47"), 10 | std::make_tuple(hexlify("key"), QUICKFOXLAZYDOG, "b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe86653c73dd248fb82f948a549f7b791a5b41915ee4d1ec3935357e4e2317250d0372afa2ebeeb3a"), 11 | }; 12 | 13 | 14 | #endif -------------------------------------------------------------------------------- /tests/testvectors/keccak/keccak.h: -------------------------------------------------------------------------------- 1 | #ifndef __KECCAKTESTVECTORS__ 2 | #define __KECCAKTESTVECTORS__ 3 | 4 | #include "../datadigest.h" 5 | 6 | // Test vectors available at 7 | // not sure how they got those results 8 | 9 | static const std::vector KECCAK_224_TEST_VECTORS = { 10 | std::make_tuple("", "f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd"), 11 | std::make_tuple("0", "860e3ec314c5cbf19c1a4314e9ea8cb85cecd18bd850b42f5c6f2a07"), 12 | }; 13 | 14 | static const std::vector KECCAK_256_TEST_VECTORS = { 15 | std::make_tuple("", "C5D2460186F7233C927E7DB2DCC703C0E500B653CA82273B7BFAD8045D85A470"), 16 | std::make_tuple("0", "C3E5CB55999EEFF4E07B7EFFEC77582D0A5A11A94FC268A872493099273992E1"), 17 | }; 18 | 19 | static const std::vector KECCAK_384_TEST_VECTORS = { 20 | std::make_tuple("", "2C23146A63A29ACF99E73B88F8C24EAA7DC60AA771780CCC006AFBFA8FE2479B2DD2B21362337441AC12B515911957FF"), 21 | std::make_tuple("0", "4C6D164043571A32E169A527CA3503EA391BF91F22287215DF75EA243D53A0D042BC66EFE2956D8606A24F39E255A081"), 22 | }; 23 | 24 | static const std::vector KECCAK_512_TEST_VECTORS = { 25 | std::make_tuple("", "0EAB42DE4C3CEB9235FC91ACFFE746B29C29A8C366B7C60E4E67C466F36A4304C00FA9CAF9D87976BA469BCBE06713B435F091EF2769FB160CDAB33D3670680E"), 26 | std::make_tuple("0", "7D9025BB145A0814083E934BAA80EDE67322651DE52062BF9EB93623C37EFC74C62240CF8539107F9210C1E1126F79CBAEDA6B82B4A8CE6821589C403FA76B9A"), 27 | }; 28 | 29 | #endif -------------------------------------------------------------------------------- /tests/testvectors/keydatadigest.h: -------------------------------------------------------------------------------- 1 | /* 2 | keydatadigest.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 __KEY_DATA_DIGEST__ 26 | #define __KEY_DATA_DIGEST__ 27 | 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | #include 34 | 35 | #include "common/includes.h" 36 | #include "Hashes/HashAlg.h" 37 | #include "Hashes/HMAC.h" 38 | 39 | #include "datadigest.h" 40 | 41 | // all input is in hex 42 | 43 | typedef std::tuple KeyDataDigest; 44 | 45 | template 46 | void hmac_test(const std::vector & test_vectors){ 47 | static_assert(std::is_base_of ::value, "Error: Algorithm type should be a hash algorithm."); 48 | 49 | for(KeyDataDigest const & kdd : test_vectors){ 50 | std::string key, data, digest; 51 | std::tie(key, data, digest) = kdd; 52 | 53 | // unhexlify digest just in case the input digest uses uppercase hex digits 54 | EXPECT_EQ(HMAC (unhexlify(key), unhexlify(data)).digest(), unhexlify(digest)); 55 | } 56 | } 57 | 58 | #endif -------------------------------------------------------------------------------- /tests/testvectors/lm/lm.h: -------------------------------------------------------------------------------- 1 | #ifndef __LMTESTVECTORS__ 2 | #define __LMTESTVECTORS__ 3 | 4 | #include "../datadigest.h" 5 | 6 | // Test vectors from somewhere 7 | 8 | static const std::vector LM_TEST_VECTORS = { 9 | std::make_tuple("", "aad3b435b51404eeaad3b435b51404ee"), 10 | std::make_tuple(QUICKFOXLAZYDOG, "a7b07f9948d8cc7f97c4b0b30cae500f"), 11 | }; 12 | 13 | #endif // __LMTESTVECTORS__ 14 | -------------------------------------------------------------------------------- /tests/testvectors/md/md2.h: -------------------------------------------------------------------------------- 1 | #ifndef __MD2TESTVECTORS__ 2 | #define __MD2TESTVECTORS__ 3 | 4 | #include "../datadigest.h" 5 | 6 | // Test vectors from 7 | 8 | static const std::vector MD2_TEST_VECTORS = { 9 | std::make_tuple("", "8350e5a3e24c153df2275c9f80692773"), 10 | std::make_tuple(hexlify("a"), "32ec01ec4a6dac72c0ab96fb34c0b5d1"), 11 | std::make_tuple(hexlify("abc"), "da853b0d3f88d99b30283a69e6ded6bb"), 12 | std::make_tuple(hexlify("message digest"), "ab4f496bfb2a530b219ff33031fe06b0"), 13 | std::make_tuple(hexlify("abcdefghijklmnopqrstuvwxyz"), "4e8ddff3650292ab5a4108c3aa47940b"), 14 | std::make_tuple(hexlify("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), "da33def2a42df13975352846c30338cd"), 15 | std::make_tuple(hexlify("12345678901234567890123456789012345678901234567890123456789012345678901234567890"), "d5976f79d83d3a0dc9806c3c66f3efd8"), 16 | std::make_tuple(QUICKFOXLAZYDOG, "03d85a0d629d2c442e987525319fc471"), 17 | }; 18 | 19 | #endif // __MD2TESTVECTORS__ 20 | -------------------------------------------------------------------------------- /tests/testvectors/md/md4.h: -------------------------------------------------------------------------------- 1 | #ifndef __MD4TESTVECTORS__ 2 | #define __MD4TESTVECTORS__ 3 | 4 | #include "../datadigest.h" 5 | 6 | // Test vectors from 7 | 8 | static const std::vector MD4_TEST_VECTORS = { 9 | std::make_tuple("", "31d6cfe0d16ae931b73c59d7e0c089c0"), 10 | std::make_tuple(hexlify("a"), "bde52cb31de33e46245e05fbdbd6fb24"), 11 | std::make_tuple(hexlify("abc"), "a448017aaf21d8525fc10ae87aa6729d"), 12 | std::make_tuple(hexlify("message digest"), "d9130a8164549fe818874806e1c7014b"), 13 | std::make_tuple(hexlify("abcdefghijklmnopqrstuvwxyz"), "d79e1c308aa5bbcdeea8ed63df412da9"), 14 | std::make_tuple(hexlify("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), "043f8582f241db351ce627e153e7f0e4"), 15 | std::make_tuple(hexlify("12345678901234567890123456789012345678901234567890123456789012345678901234567890"), "e33b4ddc9c38f2199c3e7b164fcc0536"), 16 | std::make_tuple(QUICKFOXLAZYDOG, "1bee69a46ba811185c194762abaeae90"), 17 | }; 18 | 19 | #endif // __MD4TESTVECTORS__ 20 | -------------------------------------------------------------------------------- /tests/testvectors/poly1305aes/poly1305aes.h: -------------------------------------------------------------------------------- 1 | #ifndef __POLY1305AESTESTVECTORS__ 2 | #define __POLY1305AESTESTVECTORS__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | // {data, key, r, nonce, MAC} 9 | std::vector > POLY1305AES_TEST_VECTORS = { 10 | {{"f3f6", "ec074c835580741701425b623235add6", "851fc40c3467ac0be05cc20404f3f700", "fb447350c4e868c52ac3275cf9d4327e", "f4c633c3044fc145f84f335cb81953de"}}, 11 | {{"", "75deaa25c09f208e1dc4ce6b5cad3fbf", "a0f3080000f46400d0c7e9076c834403", "61ee09218d29b0aaed7e154a2c5509cc", "dd3fab2251f11ac759f0887129cc2ee7"}}, 12 | {{"ab0812724a7f1e342742cbed374d94d136c6b8795d45b3819830f2c04491faf0990c62e48b8018b2c3e4a0fa3134cb67fa83e158c994d961c4cb21095c1bf9", "e1a5668a4d5b66a5f68cc5424ed5982d", "12976a08c4426d0ce8a82407c4f48207", "9ae831e743978d3a23527c7128149e3a", "5154ad0d2cb26e01274fc51148491f1b"}}, 13 | }; 14 | 15 | #endif -------------------------------------------------------------------------------- /tests/testvectors/ripemd/ripemd128.h: -------------------------------------------------------------------------------- 1 | #ifndef __RIPEMD128TESTVECTORS__ 2 | #define __RIPEMD128TESTVECTORS__ 3 | 4 | #include "../datadigest.h" 5 | 6 | // Test vectors from 7 | 8 | static const std::vector RIPEMD128_TEST_VECTORS = { 9 | std::make_tuple("", "cdf26213a150dc3ecb610f18f6b38b46"), 10 | std::make_tuple("61", "86be7afa339d0fc7cfc785e72f578d33"), 11 | std::make_tuple("616263", "c14a12199c66e4ba84636b0f69144c77"), 12 | std::make_tuple("6d65737361676520646967657374", "9e327b3d6e523062afc1132d7df9d1b8"), 13 | std::make_tuple("6162636465666768696a6b6c6d6e6f707172737475767778797a", "fd2aa607f71dc8f510714922b371834e"), 14 | std::make_tuple("6162636462636465636465666465666765666768666768696768696a68696a6b696a6b6c6a6b6c6d6b6c6d6e6c6d6e6f6d6e6f706e6f7071", "a1aa0689d0fafa2ddc22e88b49133a06"), 15 | std::make_tuple("4142434445464748494a4b4c4d4e4f505152535455565758595a6162636465666768696a6b6c6d6e6f707172737475767778797a30313233343536373839", "d1e959eb179c911faea4624c60c5c702"), 16 | std::make_tuple("3132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930", "3f45ef194732c2dbb2c4a2c769795fa3"), 17 | }; 18 | 19 | #endif // __RIPEMD128TESTVECTORS__ 20 | -------------------------------------------------------------------------------- /tests/testvectors/ripemd/ripemd160.h: -------------------------------------------------------------------------------- 1 | #ifndef __RIPEMD160TESTVECTORS__ 2 | #define __RIPEMD160TESTVECTORS__ 3 | 4 | #include "../datadigest.h" 5 | 6 | // Test vectors from 7 | 8 | static const std::vector RIPEMD160_TEST_VECTORS = { 9 | std::make_tuple("", "9c1185a5c5e9fc54612808977ee8f548b2258d31"), 10 | std::make_tuple("61", "0bdc9d2d256b3ee9daae347be6f4dc835a467ffe"), 11 | std::make_tuple("616263", "8eb208f7e05d987a9b044a8e98c6b087f15a0bfc"), 12 | std::make_tuple("6d65737361676520646967657374", "5d0689ef49d2fae572b881b123a85ffa21595f36"), 13 | std::make_tuple("6162636465666768696a6b6c6d6e6f707172737475767778797a", "f71c27109c692c1b56bbdceb5b9d2865b3708dbc"), 14 | std::make_tuple("6162636462636465636465666465666765666768666768696768696a68696a6b696a6b6c6a6b6c6d6b6c6d6e6c6d6e6f6d6e6f706e6f7071", "12a053384a9c0c88e405a06c27dcf49ada62eb2b"), 15 | std::make_tuple("4142434445464748494a4b4c4d4e4f505152535455565758595a6162636465666768696a6b6c6d6e6f707172737475767778797a30313233343536373839", "b0e20b6e3116640286ed3a87a5713079b21f5189"), 16 | std::make_tuple("3132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930313233343536373839303132333435363738393031323334353637383930", "9b752e45573d4b39f4dbd3323cab82bf63326bfb"), 17 | }; 18 | 19 | #endif // __RIPEMD160TESTVECTORS__ 20 | --------------------------------------------------------------------------------