├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── include └── opensslpp │ ├── aes_cbc.h │ ├── aes_gcm.h │ ├── base64.h │ ├── details │ └── common.h │ ├── random.h │ ├── rsa.h │ └── sha.h └── tests ├── CMakeLists.txt ├── main.cpp ├── test_aes_cbc.cpp ├── test_aes_gcm.cpp ├── test_base64.cpp ├── test_rsa.cpp └── test_sha.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /CMakeLists.txt.user 3 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4) 2 | 3 | project(opensslpp) 4 | 5 | set(CMAKE_CXX_STANDARD 14) 6 | 7 | if(MSVC) 8 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /W4") 9 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /W4") 10 | set(OPENSSLPP_ADDITIONAL_LIBS Ws2_32 crypt32) 11 | else() 12 | set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall -Wpedantic") 13 | set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wpedantic") 14 | endif() 15 | 16 | get_filename_component(OPENSSLPP_ROOT "${CMAKE_CURRENT_LIST_DIR}" ABSOLUTE) 17 | 18 | set(OPENSSLPP_INCLUDE "${OPENSSLPP_ROOT}/include") 19 | include_directories(${OPENSSLPP_INCLUDE}) 20 | 21 | set(OPENSSL_MSVC_STATIC_RT ON) 22 | set(OPENSSL_USE_STATIC_LIBS ON) 23 | find_package(OpenSSL REQUIRED) 24 | include_directories(${OPENSSL_INCLUDE_DIR}) 25 | 26 | find_package(Threads) 27 | 28 | set(OPENSSLPP_HEADERS "${OPENSSLPP_INCLUDE}/opensslpp") 29 | 30 | set(HEADERS 31 | ${OPENSSLPP_HEADERS}/aes_cbc.h 32 | ${OPENSSLPP_HEADERS}/aes_gcm.h 33 | ${OPENSSLPP_HEADERS}/rsa.h 34 | ${OPENSSLPP_HEADERS}/sha.h 35 | 36 | ${OPENSSLPP_HEADERS}/base64.h 37 | ${OPENSSLPP_HEADERS}/random.h 38 | 39 | ${OPENSSLPP_HEADERS}/details/common.h 40 | ) 41 | 42 | add_library(${PROJECT_NAME} STATIC ${HEADERS}) 43 | set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE CXX) 44 | 45 | set(OPENSSLPP_TEST_PROJECT test_${PROJECT_NAME}) 46 | 47 | add_test(NAME ${OPENSSLPP_TEST_PROJECT} COMMAND ${OPENSSLPP_TEST_PROJECT}) 48 | 49 | add_subdirectory(tests) 50 | 51 | enable_testing() 52 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | Copyright (c) 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /include/opensslpp/aes_cbc.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "base64.h" 4 | #include "random.h" 5 | 6 | namespace opensslpp 7 | { 8 | class Random; 9 | 10 | template 11 | class AesCbc final 12 | { 13 | public: 14 | using Key = std::array; 15 | 16 | static constexpr size_t IvSize = 128 / 8; 17 | using Iv = std::array; 18 | 19 | static constexpr decltype(EVP_aes_256_cbc)* Mode = &EVP_aes_256_cbc; 20 | 21 | static std::unique_ptr createNewKey() 22 | { 23 | auto random = Random::create(); 24 | if (!random) 25 | return nullptr; 26 | 27 | Key key; 28 | if (!random->getRandomBytes(key.data(), key.size())) 29 | return nullptr; 30 | 31 | return std::unique_ptr(new AesCbc(std::move(random), std::move(key))); 32 | } 33 | 34 | static std::unique_ptr createWithKey(const std::string& base64Key) 35 | { 36 | auto random = Random::create(); 37 | if (!random) 38 | return nullptr; 39 | 40 | Key key; 41 | 42 | if (Base64::decode(base64Key, key.data(), key.size()) != key.size()) 43 | return nullptr; 44 | 45 | return std::unique_ptr(new AesCbc(std::move(random), std::move(key))); 46 | } 47 | 48 | std::string base64Key() const 49 | { 50 | return Base64::encode(key_.data(), key_.size()); 51 | } 52 | 53 | const Key& key() const 54 | { 55 | return key_; 56 | } 57 | 58 | bool encrypt(const std::string& plainText, std::vector& cipher, Iv& iv) const 59 | { 60 | return encrypt(reinterpret_cast(plainText.c_str()), plainText.size(), cipher, iv); 61 | } 62 | 63 | bool encrypt(const uint8_t* plainData, size_t plainDataSize, std::vector& cipher, Iv& iv) const 64 | { 65 | if (!random_->getRandomBytes(iv.data(), iv.size())) 66 | return false; 67 | 68 | CipherContextPtr context(EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free); 69 | if (!context) 70 | return false; 71 | 72 | if (EVP_EncryptInit_ex(context.get(), EVP_aes_256_cbc(), nullptr, key_.data(), iv.data()) != Success) 73 | return false; 74 | 75 | cipher.resize(getCipherSize(plainDataSize)); 76 | 77 | int size = 0; 78 | 79 | if (EVP_EncryptUpdate(context.get(), cipher.data(), &size, plainData, static_cast(plainDataSize)) != Success) 80 | return false; 81 | 82 | if (EVP_EncryptFinal_ex(context.get(), cipher.data() + size, &size) != Success) 83 | return false; 84 | 85 | return true; 86 | } 87 | 88 | bool decrypt(const std::vector& cipher, const Iv& iv, std::vector& plainData) const 89 | { 90 | CipherContextPtr context(EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free); 91 | if (!context) 92 | return false; 93 | 94 | if (EVP_DecryptInit_ex(context.get(), EVP_aes_256_cbc(), nullptr, key_.data(), iv.data()) != Success) 95 | return false; 96 | 97 | plainData.resize(cipher.size()); 98 | 99 | int size = 0; 100 | if (EVP_DecryptUpdate(context.get(), plainData.data(), &size, cipher.data(), static_cast(cipher.size())) != Success) 101 | return false; 102 | 103 | auto plainDataSize = size; 104 | 105 | if (EVP_DecryptFinal_ex(context.get(), plainData.data() + size, &size) != Success) 106 | return false; 107 | 108 | plainDataSize += size; 109 | 110 | plainData.resize(plainDataSize); 111 | 112 | return true; 113 | } 114 | 115 | static constexpr size_t getCipherSize(size_t plainSize) 116 | { 117 | return (plainSize / IvSize + 1) * IvSize; 118 | } 119 | 120 | private: 121 | AesCbc(std::unique_ptr&& random, Key&& key) 122 | : random_(std::move(random)) 123 | , key_(std::move(key)) 124 | { 125 | } 126 | 127 | private: 128 | std::unique_ptr random_; 129 | Key key_; 130 | }; 131 | 132 | using Aes256Cbc = AesCbc<256>; 133 | } 134 | -------------------------------------------------------------------------------- /include/opensslpp/aes_gcm.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "base64.h" 4 | #include "random.h" 5 | 6 | namespace opensslpp 7 | { 8 | class Random; 9 | 10 | template 11 | class AesGcm final 12 | { 13 | public: 14 | using Key = std::array; 15 | 16 | static constexpr size_t IvSize = 96 / 8; 17 | using Iv = std::array; 18 | 19 | static constexpr size_t TagSize = 128 / 8; 20 | using Tag = std::array; 21 | 22 | static std::unique_ptr createNewKey() 23 | { 24 | auto random = Random::create(); 25 | if (!random) 26 | return nullptr; 27 | 28 | Key key; 29 | if (!random->getRandomBytes(key.data(), key.size())) 30 | return nullptr; 31 | 32 | return std::unique_ptr(new AesGcm(std::move(random), std::move(key))); 33 | } 34 | 35 | static std::unique_ptr createWithKey(const std::string& base64Key) 36 | { 37 | auto random = Random::create(); 38 | if (!random) 39 | return nullptr; 40 | 41 | Key key; 42 | 43 | if (Base64::decode(base64Key, key.data(), key.size()) != key.size()) 44 | return nullptr; 45 | 46 | return std::unique_ptr(new AesGcm(std::move(random), std::move(key))); 47 | } 48 | 49 | std::string base64Key() const 50 | { 51 | return Base64::encode(key_.data(), key_.size()); 52 | } 53 | 54 | const Key& key() const 55 | { 56 | return key_; 57 | } 58 | 59 | bool encrypt(const std::string& plainText, std::vector& cipher, Iv& iv, Tag& tag) const 60 | { 61 | return encrypt(reinterpret_cast(plainText.c_str()), plainText.size(), cipher, iv, tag); 62 | } 63 | 64 | bool encrypt(const uint8_t* plainData, size_t plainDataSize, std::vector& cipher, Iv& iv, Tag& tag) const 65 | { 66 | if (!random_->getRandomBytes(iv.data(), iv.size())) 67 | return false; 68 | 69 | CipherContextPtr context(EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free); 70 | if (!context) 71 | return false; 72 | 73 | if (EVP_EncryptInit_ex(context.get(), EVP_aes_256_gcm(), nullptr, nullptr, nullptr) != Success) 74 | return false; 75 | 76 | if (EVP_CIPHER_CTX_ctrl(context.get(), EVP_CTRL_GCM_SET_IVLEN, IvSize, nullptr) != Success) 77 | return false; 78 | 79 | if (EVP_EncryptInit_ex(context.get(), nullptr, nullptr, key_.data(), iv.data()) != Success) 80 | return false; 81 | 82 | cipher.resize(getCipherSize(plainDataSize)); 83 | 84 | int size = 0; 85 | 86 | if (EVP_EncryptUpdate(context.get(), cipher.data(), &size, plainData, static_cast(plainDataSize)) != Success) 87 | return false; 88 | 89 | if (EVP_EncryptFinal_ex(context.get(), cipher.data() + size, &size) != Success) 90 | return false; 91 | 92 | if (EVP_CIPHER_CTX_ctrl(context.get(), EVP_CTRL_GCM_GET_TAG, static_cast(tag.size()), tag.data()) != Success) 93 | return false; 94 | 95 | return true; 96 | } 97 | 98 | bool decrypt(const std::vector& cipher, const Iv& iv, const Tag& tag, std::vector& plainData) const 99 | { 100 | CipherContextPtr context(EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free); 101 | if (!context) 102 | return false; 103 | 104 | if (EVP_DecryptInit_ex(context.get(), EVP_aes_256_gcm(), nullptr, nullptr, nullptr) != Success) 105 | return false; 106 | 107 | if (EVP_CIPHER_CTX_ctrl(context.get(), EVP_CTRL_GCM_SET_IVLEN, IvSize, nullptr) != Success) 108 | return false; 109 | 110 | if (EVP_DecryptInit_ex(context.get(), nullptr, nullptr, key_.data(), iv.data()) != Success) 111 | return false; 112 | 113 | plainData.resize(cipher.size()); 114 | 115 | int size = 0; 116 | 117 | if (EVP_DecryptUpdate(context.get(), plainData.data(), &size, cipher.data(), static_cast(cipher.size())) != Success) 118 | return false; 119 | 120 | if (EVP_CIPHER_CTX_ctrl(context.get(), EVP_CTRL_GCM_SET_TAG, static_cast(tag.size()), const_cast(tag.data())) != Success) 121 | return false; 122 | 123 | if (EVP_DecryptFinal_ex(context.get(), plainData.data() + size, &size) != Success) 124 | return false; 125 | 126 | return true; 127 | } 128 | 129 | static constexpr size_t getCipherSize(size_t plainSize) 130 | { 131 | return plainSize; 132 | } 133 | 134 | private: 135 | AesGcm(std::unique_ptr&& random, Key&& key) 136 | : random_(std::move(random)) 137 | , key_(std::move(key)) 138 | { 139 | } 140 | 141 | private: 142 | std::unique_ptr random_; 143 | Key key_; 144 | }; 145 | 146 | using Aes256Gcm = AesGcm<256>; 147 | } 148 | -------------------------------------------------------------------------------- /include/opensslpp/base64.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "details/common.h" 4 | 5 | namespace opensslpp 6 | { 7 | class Base64 final 8 | { 9 | public: 10 | static std::string encode(const std::vector& data) 11 | { 12 | return encode(data.data(), data.size()); 13 | } 14 | 15 | static std::string encode(const uint8_t* data, size_t size) 16 | { 17 | auto encoder = makeBio(BIO_f_base64()); 18 | auto buffer = makeBio(BIO_s_mem()); 19 | auto stream = BIO_push(encoder.get(), buffer.get()); 20 | 21 | if (!stream) 22 | return std::string(); 23 | 24 | BIO_set_flags(stream, BIO_FLAGS_BASE64_NO_NL); 25 | BIO_write(stream, data, static_cast(size)); 26 | BIO_flush(stream); 27 | 28 | BUF_MEM* ptr = nullptr; 29 | BIO_get_mem_ptr(stream, &ptr); 30 | 31 | return std::string(ptr->data, ptr->length); 32 | } 33 | 34 | static std::vector decode(const std::string& text) 35 | { 36 | const auto size = calculateLengthOfDecoded(text); 37 | 38 | std::vector result(size); 39 | 40 | if (decode(text, result.data(), result.size()) != size) 41 | return std::vector(); 42 | 43 | return result; 44 | } 45 | 46 | static size_t decode(const std::string& text, uint8_t* data, size_t size) 47 | { 48 | auto buffer = makeBio(BIO_new_mem_buf(text.c_str(), static_cast(text.size()))); 49 | auto decoder = makeBio(BIO_f_base64()); 50 | auto stream = BIO_push(decoder.get(), buffer.get()); 51 | 52 | if (!stream) 53 | return 0; 54 | 55 | BIO_set_flags(stream, BIO_FLAGS_BASE64_NO_NL); 56 | return BIO_read(stream, data, static_cast(size)); 57 | } 58 | 59 | private: 60 | static size_t calculateLengthOfDecoded(const std::string& text) 61 | { 62 | const size_t length = text.length(); 63 | 64 | size_t padding = 0; 65 | if (text[length - 1] == '=') 66 | { 67 | padding = text[length - 2] == '=' ? 2 : 1; 68 | } 69 | 70 | return (length * 3) / 4 - padding; 71 | } 72 | }; 73 | } 74 | -------------------------------------------------------------------------------- /include/opensslpp/details/common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | namespace opensslpp 17 | { 18 | static constexpr int Success = 1; 19 | 20 | struct Sha256Type 21 | { 22 | static constexpr decltype(EVP_sha256)* Function = &EVP_sha256; 23 | }; 24 | 25 | struct Sha512Type 26 | { 27 | static constexpr decltype(EVP_sha256)* Function = &EVP_sha512; 28 | }; 29 | 30 | using BignumPtr = std::unique_ptr; 31 | using RsaPtr = std::unique_ptr; 32 | using KeyPtr = std::unique_ptr; 33 | using KeyContextPtr = std::unique_ptr; 34 | using CipherContextPtr = std::unique_ptr; 35 | using DigestContextPtr = std::unique_ptr; 36 | using BioMemPtr = std::unique_ptr; 37 | 38 | template 39 | BioMemPtr makeBio(BioMethod method) 40 | { 41 | return BioMemPtr(BIO_new(method), BIO_free); 42 | } 43 | 44 | inline BioMemPtr makeBio(BIO* bio) 45 | { 46 | return BioMemPtr(bio, BIO_free); 47 | } 48 | 49 | template 50 | std::string keyToString(WritePem write) 51 | { 52 | auto bio = makeBio(BIO_s_mem()); 53 | 54 | if (write(bio.get()) != Success) 55 | return std::string(); 56 | 57 | BUF_MEM* buffer = nullptr; 58 | BIO_get_mem_ptr(bio.get(), &buffer); 59 | 60 | if (!buffer || !buffer->data || !buffer->length) 61 | return std::string(); 62 | 63 | return std::string(buffer->data, buffer->length); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /include/opensslpp/random.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "details/common.h" 4 | 5 | namespace opensslpp 6 | { 7 | class Random final 8 | { 9 | public: 10 | static std::unique_ptr create() 11 | { 12 | if (RAND_status() != Success) 13 | { 14 | if (!seed()) 15 | { 16 | return nullptr; 17 | } 18 | } 19 | 20 | return std::unique_ptr(new Random()); 21 | } 22 | 23 | ~Random() 24 | { 25 | } 26 | 27 | Random(const Random&) = delete; 28 | Random& operator=(const Random&) = delete; 29 | 30 | Random(Random&&) = delete; 31 | Random& operator=(Random&&) = delete; 32 | 33 | std::vector getRandomBytes(size_t count) const 34 | { 35 | { 36 | std::vector result(count); 37 | 38 | if (RAND_bytes(result.data(), static_cast(count)) != Success) 39 | result.clear(); 40 | 41 | return result; 42 | } 43 | } 44 | 45 | bool getRandomBytes(uint8_t* result, size_t count) const 46 | { 47 | return RAND_bytes(result, static_cast(count)) == Success; 48 | } 49 | 50 | private: 51 | Random() 52 | { 53 | } 54 | 55 | static bool seed() 56 | { 57 | //TODO implement this 58 | return false; 59 | } 60 | }; 61 | } 62 | -------------------------------------------------------------------------------- /include/opensslpp/rsa.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "aes_cbc.h" 4 | #include "random.h" 5 | 6 | namespace opensslpp 7 | { 8 | template 9 | class Rsa final 10 | { 11 | using RsaT = Rsa; 12 | public: 13 | static constexpr size_t KeySize = Bits / 8; 14 | using EncryptedKey = std::array; 15 | 16 | using Iv = typename Aes::Iv; 17 | 18 | static std::unique_ptr createNewKeys() 19 | { 20 | KeyContextPtr context(EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr), EVP_PKEY_CTX_free); 21 | if (!context) 22 | return nullptr; 23 | 24 | if (EVP_PKEY_keygen_init(context.get()) != Success) 25 | return nullptr; 26 | 27 | if (EVP_PKEY_CTX_set_rsa_keygen_bits(context.get(), Bits) != Success) 28 | return nullptr; 29 | 30 | EVP_PKEY* key = nullptr; 31 | 32 | if (EVP_PKEY_keygen(context.get(), &key) != Success) 33 | return nullptr; 34 | 35 | return std::unique_ptr(new RsaT(KeyPtr(key, EVP_PKEY_free))); 36 | } 37 | 38 | static std::unique_ptr createWithPublicKey(const std::string& publicKey) 39 | { 40 | auto bio = makeBio(BIO_new_mem_buf(publicKey.c_str(), static_cast(publicKey.size()))); 41 | KeyPtr key(PEM_read_bio_PUBKEY(bio.get(), nullptr, nullptr, nullptr), EVP_PKEY_free); 42 | if (!key) 43 | return nullptr; 44 | return std::unique_ptr(new RsaT(std::move(key))); 45 | } 46 | 47 | static std::unique_ptr createWithPrivateKey(const std::string& privateKey, std::string passPhrase = std::string()) 48 | { 49 | auto bio = makeBio(BIO_new_mem_buf(privateKey.c_str(), static_cast(privateKey.size()))); 50 | auto ptr = const_cast(passPhrase.c_str()); 51 | KeyPtr key(PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, ptr), EVP_PKEY_free); 52 | if (!key) 53 | return nullptr; 54 | return std::unique_ptr(new RsaT(std::move(key))); 55 | } 56 | 57 | std::string publicKey() const 58 | { 59 | return keyToString([this](BIO* bio) 60 | { 61 | return PEM_write_bio_PUBKEY(bio, rsaKey_.get()); 62 | }); 63 | } 64 | 65 | std::string privateKey() const 66 | { 67 | return keyToString([this](BIO* bio) 68 | { 69 | return PEM_write_bio_PrivateKey(bio, rsaKey_.get(), nullptr, nullptr, 0, nullptr, nullptr); 70 | }); 71 | } 72 | 73 | std::string privateKeyPKCS8(std::string passPhrase) const 74 | { 75 | return keyToString([this, &passPhrase](BIO* bio) 76 | { 77 | auto ptr = const_cast(passPhrase.c_str()); 78 | return PEM_write_bio_PKCS8PrivateKey(bio, rsaKey_.get(), Aes::Mode(), nullptr, 0, nullptr, ptr); 79 | }); 80 | } 81 | 82 | bool encrypt(const std::string& plainText, EncryptedKey& key, Iv& iv, std::vector& cipher) const 83 | { 84 | return encrypt(reinterpret_cast(plainText.c_str()), plainText.size(), key, iv, cipher); 85 | } 86 | 87 | bool encrypt(const uint8_t* plainData, size_t plainDataSize, EncryptedKey& key, Iv& iv, std::vector& cipher) const 88 | { 89 | CipherContextPtr context(EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free); 90 | if (!context) 91 | return false; 92 | 93 | const int publicKeysCount = 1; 94 | uint8_t* keys[] = { key.data() }; 95 | EVP_PKEY* publicKeys[] = { rsaKey_.get() }; 96 | 97 | int keySize = 0; 98 | if (EVP_SealInit(context.get(), Aes::Mode(), keys, &keySize, iv.data(), publicKeys, publicKeysCount) != publicKeysCount) 99 | return false; 100 | 101 | const size_t cipherSize = Aes::getCipherSize(plainDataSize); 102 | cipher.resize(cipherSize); 103 | 104 | int size = 0; 105 | if (EVP_SealUpdate(context.get(), cipher.data(), &size, plainData, static_cast(plainDataSize)) != Success) 106 | return false; 107 | 108 | if (EVP_SealFinal(context.get(), cipher.data() + size, &size) != Success) 109 | return false; 110 | 111 | return true; 112 | } 113 | 114 | bool decrypt(const EncryptedKey& key, const Iv& iv, const std::vector& cipher, std::vector& plainData) const 115 | { 116 | CipherContextPtr context(EVP_CIPHER_CTX_new(), EVP_CIPHER_CTX_free); 117 | if (!context) 118 | return false; 119 | 120 | if (EVP_OpenInit(context.get(), Aes::Mode(), key.data(), static_cast(key.size()), iv.data(), rsaKey_.get()) != Success) 121 | return false; 122 | 123 | plainData.resize(cipher.size()); 124 | 125 | int size = 0; 126 | if (EVP_OpenUpdate(context.get(), plainData.data(), &size, cipher.data(), static_cast(cipher.size())) != Success) 127 | return false; 128 | 129 | auto plainDataSize = size; 130 | 131 | if (EVP_OpenFinal(context.get(), plainData.data() + size, &size) != Success) 132 | return false; 133 | 134 | plainDataSize += size; 135 | 136 | plainData.resize(plainDataSize); 137 | 138 | return true; 139 | } 140 | 141 | bool sign(const std::string& plainText, std::vector& signature) 142 | { 143 | DigestContextPtr context(EVP_MD_CTX_create(), EVP_MD_CTX_free); 144 | if (!context) 145 | return false; 146 | 147 | if (EVP_DigestSignInit(context.get(), nullptr, Hash::Function(), nullptr, rsaKey_.get()) != Success) 148 | return false; 149 | 150 | if (EVP_DigestSignUpdate(context.get(), plainText.c_str(), plainText.size()) != Success) 151 | return false; 152 | 153 | size_t size = 0; 154 | if (EVP_DigestSignFinal(context.get(), nullptr, &size) != Success) 155 | return false; 156 | 157 | signature.resize(size); 158 | 159 | if (EVP_DigestSignFinal(context.get(), signature.data(), &size) != Success) 160 | return false; 161 | 162 | return true; 163 | } 164 | 165 | bool isCorrectSignature(const std::string& plainText, const std::vector& signature) 166 | { 167 | DigestContextPtr context(EVP_MD_CTX_create(), EVP_MD_CTX_free); 168 | if (!context) 169 | return false; 170 | 171 | if (EVP_DigestVerifyInit(context.get(), nullptr, Hash::Function(), nullptr, rsaKey_.get()) != Success) 172 | return false; 173 | 174 | if (EVP_DigestVerifyUpdate(context.get(), plainText.c_str(), plainText.size()) != Success) 175 | return false; 176 | 177 | if (EVP_DigestVerifyFinal(context.get(), signature.data(), signature.size()) != Success) 178 | return false; 179 | 180 | return true; 181 | } 182 | 183 | Rsa(const Rsa&) = delete; 184 | Rsa& operator=(const Rsa&) = delete; 185 | 186 | Rsa(Rsa&&) = delete; 187 | Rsa& operator=(Rsa&&) = delete; 188 | 189 | ~Rsa() 190 | { 191 | } 192 | 193 | private: 194 | Rsa(KeyPtr&& key) 195 | : rsaKey_(std::move(key)) 196 | { 197 | } 198 | 199 | private: 200 | KeyPtr rsaKey_; 201 | }; 202 | 203 | using Rsa2048 = Rsa<2048, Aes256Cbc, Sha256Type>; 204 | } 205 | -------------------------------------------------------------------------------- /include/opensslpp/sha.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "base64.h" 4 | 5 | namespace opensslpp 6 | { 7 | template 8 | class Sha final 9 | { 10 | public: 11 | static constexpr size_t DigestSize = Bits / 8; 12 | using Digest = std::array; 13 | 14 | static bool calculate(const std::string& message, Digest& result) 15 | { 16 | DigestContextPtr context(EVP_MD_CTX_create(), EVP_MD_CTX_free); 17 | if (!context) 18 | return false; 19 | 20 | if (EVP_DigestInit_ex(context.get(), Type::Function(), nullptr) != Success) 21 | return false; 22 | 23 | if (EVP_DigestUpdate(context.get(), message.c_str(), message.size()) != Success) 24 | return false; 25 | 26 | unsigned size = 0; 27 | if (EVP_DigestFinal_ex(context.get(), result.data(), &size) != Success) 28 | return false; 29 | 30 | return true; 31 | } 32 | 33 | static std::string toBase64(const Digest& digest) 34 | { 35 | return Base64::encode(digest.data(), digest.size()); 36 | } 37 | 38 | static Digest fromBase64(const std::string& digest) 39 | { 40 | Digest result; 41 | Base64::decode(digest, result.data(), result.size()); 42 | return result; 43 | } 44 | }; 45 | 46 | using Sha256 = Sha<256, Sha256Type>; 47 | using Sha512 = Sha<512, Sha512Type>; 48 | } 49 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(${OPENSSLPP_TEST_PROJECT}) 2 | 3 | find_package(GTest REQUIRED) 4 | include_directories(${GTEST_INCLUDE_DIRS}) 5 | 6 | include_directories(${OPENSSLPP_INCLUDE}) 7 | 8 | set(SOURCES 9 | test_aes_cbc.cpp 10 | test_aes_gcm.cpp 11 | test_base64.cpp 12 | test_rsa.cpp 13 | test_sha.cpp 14 | main.cpp) 15 | 16 | add_executable(${OPENSSLPP_TEST_PROJECT} ${SOURCES}) 17 | 18 | target_link_libraries(${OPENSSLPP_TEST_PROJECT} 19 | ${GTEST_BOTH_LIBRARIES} 20 | ${OPENSSLPP_ADDITIONAL_LIBS} 21 | ${OPENSSL_CRYPTO_LIBRARY} 22 | ${CMAKE_THREAD_LIBS_INIT} 23 | ${CMAKE_DL_LIBS}) 24 | -------------------------------------------------------------------------------- /tests/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(int argc, char* argv[]) 4 | { 5 | ::testing::InitGoogleTest(&argc, argv); 6 | return RUN_ALL_TESTS(); 7 | } 8 | -------------------------------------------------------------------------------- /tests/test_aes_cbc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | TEST(aes256, cbc) 6 | { 7 | auto newAes = opensslpp::Aes256Cbc::createNewKey(); 8 | ASSERT_NE(newAes, nullptr); 9 | 10 | const auto base64Key = newAes->base64Key(); 11 | auto aes = opensslpp::Aes256Cbc::createWithKey(base64Key); 12 | ASSERT_NE(aes, nullptr); 13 | ASSERT_EQ(aes->base64Key(), base64Key); 14 | 15 | const std::string plainText = "1234567890abcdef-+=!qwerty0987654321ABCDEF"; 16 | 17 | std::vector cipher; 18 | opensslpp::Aes256Cbc::Iv iv; 19 | 20 | ASSERT_TRUE(aes->encrypt(plainText, cipher, iv)); 21 | 22 | std::vector plainData; 23 | 24 | ASSERT_TRUE(aes->decrypt(cipher, iv, plainData)); 25 | 26 | ASSERT_EQ(plainText, std::string(reinterpret_cast(plainData.data()), plainData.size())); 27 | } 28 | -------------------------------------------------------------------------------- /tests/test_aes_gcm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | TEST(aes256gcm, common) 6 | { 7 | auto newAes = opensslpp::Aes256Gcm::createNewKey(); 8 | ASSERT_NE(newAes, nullptr); 9 | 10 | const auto base64Key = newAes->base64Key(); 11 | auto aes = opensslpp::Aes256Gcm::createWithKey(base64Key); 12 | ASSERT_NE(aes, nullptr); 13 | ASSERT_EQ(aes->base64Key(), base64Key); 14 | 15 | const std::string plainText = "1234567890abcdef-+=!qwerty0987654321ABCDEF"; 16 | 17 | std::vector cipher; 18 | opensslpp::Aes256Gcm::Iv iv; 19 | opensslpp::Aes256Gcm::Tag tag; 20 | 21 | ASSERT_TRUE(aes->encrypt(plainText, cipher, iv, tag)); 22 | 23 | std::vector plainData; 24 | 25 | ASSERT_TRUE(aes->decrypt(cipher, iv, tag, plainData)); 26 | 27 | ASSERT_EQ(plainText, std::string(reinterpret_cast(plainData.data()), plainData.size())); 28 | } 29 | -------------------------------------------------------------------------------- /tests/test_base64.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | TEST(base64, common) 6 | { 7 | const std::vector data = { 1, 2, 3, 4, 5 }; 8 | 9 | { 10 | const auto encoded = opensslpp::Base64::encode(data); 11 | const auto decoded = opensslpp::Base64::decode(encoded); 12 | 13 | ASSERT_EQ(data, decoded); 14 | } 15 | 16 | { 17 | std::vector decoded(data.size()); 18 | 19 | const auto encoded = opensslpp::Base64::encode(data.data(), data.size()); 20 | const auto size = opensslpp::Base64::decode(encoded, decoded.data(), decoded.size()); 21 | 22 | ASSERT_EQ(size, decoded.size()); 23 | ASSERT_EQ(data, decoded); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tests/test_rsa.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | TEST(rsa, common) 6 | { 7 | auto newRsa = opensslpp::Rsa2048::createNewKeys(); 8 | ASSERT_NE(newRsa, nullptr); 9 | 10 | const auto publicKey = newRsa->publicKey(); 11 | const auto privateKey = newRsa->privateKey(); 12 | 13 | auto publicRsa = opensslpp::Rsa2048::createWithPublicKey(publicKey); 14 | ASSERT_NE(publicRsa, nullptr); 15 | ASSERT_EQ(publicRsa->publicKey(), publicKey); 16 | 17 | auto privateRsa = opensslpp::Rsa2048::createWithPrivateKey(privateKey); 18 | ASSERT_NE(privateRsa, nullptr); 19 | ASSERT_EQ(privateRsa->privateKey(), privateKey); 20 | 21 | const std::string plainText = "1234567890abcdef-+=!qwerty0987654321ABCDEF"; 22 | 23 | opensslpp::Rsa2048::EncryptedKey encryptedKey; 24 | opensslpp::Rsa2048::Iv iv; 25 | 26 | std::vector cipher; 27 | 28 | ASSERT_TRUE(publicRsa->encrypt(plainText, encryptedKey, iv, cipher)); 29 | 30 | std::vector plainData; 31 | 32 | ASSERT_TRUE(privateRsa->decrypt(encryptedKey, iv, cipher, plainData)); 33 | 34 | ASSERT_EQ(plainText, std::string(reinterpret_cast(plainData.data()), plainData.size())); 35 | 36 | std::vector signature; 37 | ASSERT_TRUE(privateRsa->sign(plainText, signature)); 38 | 39 | ASSERT_TRUE(publicRsa->isCorrectSignature(plainText, signature)); 40 | } 41 | 42 | TEST(rsa, passPhrase) 43 | { 44 | auto newRsa = opensslpp::Rsa2048::createNewKeys(); 45 | ASSERT_NE(newRsa, nullptr); 46 | 47 | const std::string passPhrase = "testPhrase"; 48 | 49 | const auto publicKey = newRsa->publicKey(); 50 | const auto privateKey = newRsa->privateKey(); 51 | const auto cryptedPrivateKey = newRsa->privateKeyPKCS8(passPhrase); 52 | 53 | ASSERT_NE(privateKey, cryptedPrivateKey); 54 | 55 | auto publicRsa = opensslpp::Rsa2048::createWithPublicKey(publicKey); 56 | ASSERT_NE(publicRsa, nullptr); 57 | ASSERT_EQ(publicRsa->publicKey(), publicKey); 58 | 59 | auto privateRsa = opensslpp::Rsa2048::createWithPrivateKey(cryptedPrivateKey, passPhrase); 60 | ASSERT_NE(privateRsa, nullptr); 61 | ASSERT_EQ(privateRsa->privateKey(), privateKey); 62 | 63 | auto invalid = opensslpp::Rsa2048::createWithPrivateKey(cryptedPrivateKey); 64 | ASSERT_EQ(invalid, nullptr); 65 | const std::string plainText = "1234567890abcdef-+=!qwerty0987654321ABCDEF"; 66 | 67 | opensslpp::Rsa2048::EncryptedKey encryptedKey; 68 | opensslpp::Rsa2048::Iv iv; 69 | 70 | std::vector cipher; 71 | 72 | ASSERT_TRUE(publicRsa->encrypt(plainText, encryptedKey, iv, cipher)); 73 | 74 | std::vector plainData; 75 | 76 | ASSERT_TRUE(privateRsa->decrypt(encryptedKey, iv, cipher, plainData)); 77 | 78 | ASSERT_EQ(plainText, std::string(reinterpret_cast(plainData.data()), plainData.size())); 79 | 80 | std::vector signature; 81 | ASSERT_TRUE(privateRsa->sign(plainText, signature)); 82 | 83 | ASSERT_TRUE(publicRsa->isCorrectSignature(plainText, signature)); 84 | } 85 | -------------------------------------------------------------------------------- /tests/test_sha.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | TEST(sha256, common) 6 | { 7 | opensslpp::Sha256::Digest digest1; 8 | ASSERT_TRUE(opensslpp::Sha256::calculate("test1", digest1)); 9 | 10 | opensslpp::Sha256::Digest digest2; 11 | ASSERT_TRUE(opensslpp::Sha256::calculate("test2", digest2)); 12 | 13 | opensslpp::Sha256::Digest digest3; 14 | ASSERT_TRUE(opensslpp::Sha256::calculate("test1", digest3)); 15 | 16 | ASSERT_EQ(digest1, digest3); 17 | ASSERT_NE(digest1, digest2); 18 | 19 | auto base1 = opensslpp::Sha256::toBase64(digest1); 20 | auto base2 = opensslpp::Sha256::toBase64(digest2); 21 | auto base3 = opensslpp::Sha256::toBase64(digest3); 22 | 23 | ASSERT_EQ(base1, base3); 24 | ASSERT_NE(base1, base2); 25 | 26 | auto d1 = opensslpp::Sha256::fromBase64(base1); 27 | auto d2 = opensslpp::Sha256::fromBase64(base2); 28 | auto d3 = opensslpp::Sha256::fromBase64(base3); 29 | 30 | ASSERT_EQ(d1, d3); 31 | ASSERT_NE(d1, d2); 32 | } 33 | 34 | TEST(sha512, common) 35 | { 36 | opensslpp::Sha512::Digest digest1; 37 | ASSERT_TRUE(opensslpp::Sha512::calculate("test1", digest1)); 38 | 39 | opensslpp::Sha512::Digest digest2; 40 | ASSERT_TRUE(opensslpp::Sha512::calculate("test2", digest2)); 41 | 42 | opensslpp::Sha512::Digest digest3; 43 | ASSERT_TRUE(opensslpp::Sha512::calculate("test1", digest3)); 44 | 45 | ASSERT_EQ(digest1, digest3); 46 | ASSERT_NE(digest1, digest2); 47 | 48 | auto base1 = opensslpp::Sha512::toBase64(digest1); 49 | auto base2 = opensslpp::Sha512::toBase64(digest2); 50 | auto base3 = opensslpp::Sha512::toBase64(digest3); 51 | 52 | ASSERT_EQ(base1, base3); 53 | ASSERT_NE(base1, base2); 54 | 55 | auto d1 = opensslpp::Sha512::fromBase64(base1); 56 | auto d2 = opensslpp::Sha512::fromBase64(base2); 57 | auto d3 = opensslpp::Sha512::fromBase64(base3); 58 | 59 | ASSERT_EQ(d1, d3); 60 | ASSERT_NE(d1, d2); 61 | } 62 | --------------------------------------------------------------------------------