Commit 7ba6afb53a2f6ac4bfb68c9bcc4b3a50e69d6254

Authored by Steven de Ridder
0 parents

Initial commit. dependencies not resolved yet.

.gitignore 0 → 100644
  1 +++ a/.gitignore
  1 +build/
  2 +CMakeLists.txt.user
CMakeLists.txt 0 → 100644
  1 +++ a/CMakeLists.txt
  1 +cmake_minimum_required(VERSION 3.0)
  2 +
  3 +# Check to see where cmake is located.
  4 +if( IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/cmake )
  5 + LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
  6 +elseif( IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../cmake )
  7 + LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
  8 +else()
  9 + return()
  10 +endif()
  11 +
  12 +# Check to see if there is versioning information available
  13 +if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/osdev_versioning/cmake)
  14 + LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/osdev_versioning/cmake)
  15 + include(osdevversion)
  16 +endif()
  17 +
  18 +include(projectheader)
  19 +project_header(osdev_crypter)
  20 +
  21 +add_subdirectory(src)
  22 +add_subdirectory(tests)
  23 +
  24 +# include(packaging)
  25 +# package_component()
README.md 0 → 100644
  1 +++ a/README.md
src/CMakeLists.txt 0 → 100644
  1 +++ a/src/CMakeLists.txt
  1 +cmake_minimum_required(VERSION 3.0)
  2 +LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../../external/hsoa_create_version_include/cmake)
  3 +include(projectheader)
  4 +project_header(crypter)
  5 +
  6 +find_package( Qt5Core REQUIRED )
  7 +
  8 +include_directories( SYSTEM
  9 + ${Qt5Core_INCLUDE_DIRS}
  10 +)
  11 +
  12 +include(compiler)
  13 +
  14 +set(SRC_LIST
  15 + ${CMAKE_CURRENT_SOURCE_DIR}/crypter.h
  16 + ${CMAKE_CURRENT_SOURCE_DIR}/crypter.cpp
  17 + ${CMAKE_CURRENT_SOURCE_DIR}/scopeguard.h
  18 + ${CMAKE_CURRENT_SOURCE_DIR}/scopeguard.cpp
  19 +)
  20 +
  21 +link_directories(
  22 + ${CMAKE_BINARY_DIR}/lib
  23 +)
  24 +
  25 +include(library)
  26 +add_libraries(
  27 + ${Qt5Core_LIBRARIES}
  28 + crypto
  29 + logutils
  30 +)
  31 +
  32 +include(installation)
  33 +install_component()
src/crypter.cpp 0 → 100644
  1 +++ a/src/crypter.cpp
  1 +/* ****************************************************************************
  2 + * Copyright 2019 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +
  23 +#include "crypter.h"
  24 +
  25 +// std
  26 +#include <iomanip>
  27 +#include <memory>
  28 +#include <sstream>
  29 +#include <stdexcept>
  30 +#include <system_error>
  31 +
  32 +// openssl
  33 +#include <openssl/evp.h>
  34 +
  35 +// osdev::components
  36 +#include "log.h"
  37 +#include "scopeguard.h"
  38 +
  39 +using namespace osdev::components;
  40 +
  41 +namespace {
  42 +
  43 +const EVP_MD* getAlgo( Crypter::AlgorithmEnum algo )
  44 +{
  45 + switch ( algo )
  46 + {
  47 + case Crypter::AlgorithmEnum::MD0:
  48 + return EVP_md_null();
  49 + case Crypter::AlgorithmEnum::MD2:
  50 + // return EVP_md2();
  51 + case Crypter::AlgorithmEnum::MD5:
  52 + return EVP_md5();
  53 + case Crypter::AlgorithmEnum::MDC2:
  54 + // deprecated.
  55 + // md = EVP_mdc2();
  56 + return EVP_md_null();
  57 + case Crypter::AlgorithmEnum::SHA1:
  58 + return EVP_sha1();
  59 + case Crypter::AlgorithmEnum::SHA224:
  60 + return EVP_sha224();
  61 + case Crypter::AlgorithmEnum::SHA256:
  62 + return EVP_sha256();
  63 + case Crypter::AlgorithmEnum::SHA384:
  64 + return EVP_sha384();
  65 + case Crypter::AlgorithmEnum::SHA512:
  66 + return EVP_sha512();
  67 + case Crypter::AlgorithmEnum::RIPEMD160:
  68 + return EVP_ripemd160();
  69 + }
  70 +
  71 + throw std::invalid_argument("Crypto algorithm not found.");
  72 +}
  73 +
  74 +} // anonymous
  75 +
  76 +Crypter::Crypter()
  77 +{
  78 +}
  79 +
  80 +std::string Crypter::encrypt( const std::string& message, Crypter::AlgorithmEnum algo )
  81 +{
  82 + // Create the environment
  83 + auto md = getAlgo(algo);
  84 +
  85 +#if OPENSSL_VERSION_NUMBER >= 0x1010008fL
  86 + auto mdContext = std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>(
  87 + EVP_MD_CTX_create(),
  88 + [](EVP_MD_CTX* ptr){EVP_MD_CTX_free(ptr);});
  89 +
  90 + // This will call EVP_cleanup if the guard goes out of scope.
  91 + ScopeGuard oGuard( &EVP_PBE_cleanup );
  92 +#else
  93 + auto mdContext = std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_destroy)>(
  94 + EVP_MD_CTX_create(),
  95 + [](EVP_MD_CTX* ptr){EVP_MD_CTX_destroy(ptr);});
  96 +
  97 + // This will call EVP_cleanup if the guard goes out of scope.
  98 + ScopeGuard oGuard( &EVP_cleanup );
  99 +#endif
  100 +
  101 + (void)oGuard; // Satisfy the compiler for unused variables.
  102 +
  103 + auto errorCode = EVP_DigestInit_ex( mdContext.get(), md, NULL );
  104 + if( 1 != errorCode )
  105 + {
  106 + LogError( "[Crypter::encrypt]", QString( "No encryption digest environment created." ) );
  107 + throw std::system_error( errorCode, std::system_category(), "No encryption digest environment created." );
  108 + }
  109 +
  110 + // Update the environment with the message
  111 + errorCode = EVP_DigestUpdate( mdContext.get(), message.c_str(), message.length() );
  112 + if( 1 != errorCode )
  113 + {
  114 + LogError( "[Crypter::encrypt]", QString("Digest failed.") );
  115 + throw std::system_error( errorCode, std::system_category(), "Digest failed.." );
  116 + }
  117 +
  118 + // End the Digest so we can read the crypted message.
  119 + unsigned int mdLen;
  120 + unsigned char mdValue[EVP_MAX_MD_SIZE];
  121 + errorCode = EVP_DigestFinal_ex( mdContext.get(), mdValue, &mdLen );
  122 + if( 1 != errorCode )
  123 + {
  124 + LogError( "[Crypter::encrypt]", QString("There was an error closing the digest environment.") );
  125 + throw std::system_error( errorCode, std::system_category(), "There was an error closing the digest environment." );
  126 + }
  127 +
  128 + // If we got here, all went well. We retrieve the crypted message
  129 + // through a stringstream : convert to hex, padding and width and return the string.
  130 + std::stringstream ss;
  131 + for( unsigned int nIndex = 0; nIndex < mdLen; nIndex++ )
  132 + {
  133 + ss << std::hex << std::setw( 2 ) << std::setfill( '0' ) << static_cast<int>( mdValue[nIndex] );
  134 + }
  135 +
  136 + return ss.str();
  137 +}
src/crypter.h 0 → 100644
  1 +++ a/src/crypter.h
  1 +/* ****************************************************************************
  2 + * Copyright 2019 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +
  23 +#ifndef OSDEV_COMPONENTS_CRYPTER_H
  24 +#define OSDEV_COMPONENTS_CRYPTER_H
  25 +
  26 +// std
  27 +#include <string>
  28 +
  29 +namespace osdev {
  30 +namespace components {
  31 +
  32 +/**
  33 + * @brief Provides encryption/hashing functionality.
  34 + */
  35 +class Crypter
  36 +{
  37 +public:
  38 + /**
  39 + * @brief Defines the encryption/hashing algorithms.
  40 + */
  41 + enum class AlgorithmEnum
  42 + {
  43 + MD0, ///< Empty initializer.
  44 + MD2,
  45 + MD5,
  46 + MDC2,
  47 + SHA1,
  48 + SHA224,
  49 + SHA256,
  50 + SHA384,
  51 + SHA512,
  52 + RIPEMD160
  53 + };
  54 +
  55 + /**
  56 + * @brief Constructs a default instance of Crypter.
  57 + */
  58 + Crypter();
  59 +
  60 + /**
  61 + * @brief Encrypts the specified message using the specified algorithm.
  62 + * @param message The message to encrypt.
  63 + * @param algo The encryption algorithm to use. The default value is SHA256.
  64 + * @return The encrypted string.
  65 + */
  66 + static std::string encrypt( const std::string& message, Crypter::AlgorithmEnum algo = Crypter::AlgorithmEnum::SHA256 );
  67 +};
  68 +
  69 +} /* End namespace components */
  70 +} /* End namespace osdev */
  71 +
  72 +#endif /* OSDEV_COMPONENTS_CRYPTER_H */
src/scopeguard.cpp 0 → 100644
  1 +++ a/src/scopeguard.cpp
  1 +/* ****************************************************************************
  2 + * Copyright 2019 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +
  23 +#include "scopeguard.h"
  24 +
  25 +using namespace osdev::components;
  26 +
  27 +ScopeGuard::ScopeGuard( const CleanUpFunction& cleanupFunc )
  28 + : m_cleanupFunc( cleanupFunc )
  29 +{
  30 +}
  31 +
  32 +ScopeGuard::~ScopeGuard() noexcept
  33 +{
  34 + try
  35 + {
  36 + if( m_cleanupFunc )
  37 + {
  38 + m_cleanupFunc();
  39 + }
  40 + }
  41 + catch (...) {}
  42 +}
src/scopeguard.h 0 → 100644
  1 +++ a/src/scopeguard.h
  1 +/* ****************************************************************************
  2 + * Copyright 2019 Open Systems Development BV *
  3 + * *
  4 + * Permission is hereby granted, free of charge, to any person obtaining a *
  5 + * copy of this software and associated documentation files (the "Software"), *
  6 + * to deal in the Software without restriction, including without limitation *
  7 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  8 + * and/or sell copies of the Software, and to permit persons to whom the *
  9 + * Software is furnished to do so, subject to the following conditions: *
  10 + * *
  11 + * The above copyright notice and this permission notice shall be included in *
  12 + * all copies or substantial portions of the Software. *
  13 + * *
  14 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  15 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  16 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  17 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  18 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  19 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  20 + * DEALINGS IN THE SOFTWARE. *
  21 + * ***************************************************************************/
  22 +
  23 +#ifndef OSDEV_COMPONENTS_SCOPEGUARD_H
  24 +#define OSDEV_COMPONENTS_SCOPEGUARD_H
  25 +
  26 +#include <functional>
  27 +
  28 +namespace osdev {
  29 +namespace components {
  30 +
  31 +using CleanUpFunction = std::function<void() noexcept>;
  32 +
  33 +/**
  34 + * @brief Ensures that a cleanup function is called at the end of the current scope.
  35 + */
  36 +class ScopeGuard
  37 +{
  38 +public:
  39 +
  40 + /**
  41 + * @brief Constructs a RAII instance that will call cleanupFunc in it's destructor.
  42 + * @param cleanupFunc The cleanup function to call at the end of the current scope.
  43 + * This cleanup function must not throw exceptions. If it does, the behaviour
  44 + * is undefined.
  45 + */
  46 + ScopeGuard( const CleanUpFunction& cleanupFunc );
  47 +
  48 + // not copyable
  49 + ScopeGuard( const ScopeGuard& ) = delete;
  50 + ScopeGuard& operator=( const ScopeGuard& ) = delete;
  51 +
  52 + ~ScopeGuard() noexcept;
  53 +
  54 +private:
  55 + CleanUpFunction m_cleanupFunc;
  56 +};
  57 +
  58 +} /* End namespace components */
  59 +} /* End namespace osdev */
  60 +
  61 +#endif /* OSDEV_COMPONENTS_SCOPEGUARD_H */
tests/CMakeLists.txt 0 → 100644
  1 +++ a/tests/CMakeLists.txt
  1 +cmake_minimum_required(VERSION 3.0)
  2 +LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../cmake)
  3 +
  4 +include(projectheader)
  5 +project_header(test_logutils)
  6 +
  7 +include_directories( SYSTEM
  8 + ${CMAKE_CURRENT_SOURCE_DIR}/../../src
  9 +)
  10 +
  11 +include(compiler)
  12 +set(SRC_LIST
  13 +)
  14 +
  15 +# add_executable( ${PROJECT_NAME}
  16 +# ${SRC_LIST}
  17 +# )
  18 +
  19 +# target_link_libraries(
  20 +# ${PROJECT_NAME}
  21 +# )
  22 +
  23 +# set_target_properties( ${PROJECT_NAME} PROPERTIES
  24 +# RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
  25 +# LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
  26 +# ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/archive
  27 +# )
  28 +
  29 +# include(installation)
  30 +# install_application()