From 71ce0e1a8e3dcf783b9b4fede89f0b6c6c3030d7 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 20 Aug 2017 21:25:40 +1000 Subject: [PATCH 001/148] setup repo --- .gitignore | 3 + CMakeLists.txt | 107 ++++++ cmake/FindCryptoPP.cmake | 118 +++++++ cmake/FindEASYLOGGINGPP.cmake | 38 +++ cmake/FindRipe.cmake | 41 +++ cmake/Findgtest.cmake | 159 +++++++++ include/mine.h | 606 ++++++++++++++++++++++++++++++++++ test/main.cc | 12 + test/rsa-test.h | 194 +++++++++++ test/test.h | 20 ++ 10 files changed, 1298 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 cmake/FindCryptoPP.cmake create mode 100644 cmake/FindEASYLOGGINGPP.cmake create mode 100644 cmake/FindRipe.cmake create mode 100644 cmake/Findgtest.cmake create mode 100644 include/mine.h create mode 100644 test/main.cc create mode 100644 test/rsa-test.h create mode 100644 test/test.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8b75b0e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +CMakeFiles +build +CMakeLists.txt.user diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a23cf55 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,107 @@ +cmake_minimum_required(VERSION 2.8.12) + +project(Mine) + +set (MINE_VERSION "4.0.0") +set (MINE_SOVERSION "4.0.0") + +add_definitions (-DMINE_VERSION="${MINE_VERSION}") + +set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") + +include_directories (${CMAKE_BINARY_DIR}) +include_directories (${CMAKE_SOURCE_DIR}) + +include(FindPackageHandleStandardArgs) + +# We need C++11 +macro(require_cpp11) + if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.0) + # CMake 3.1 has built-in CXX standard checks. + message("-- Setting C++11") + set(CMAKE_CXX_STANDARD 11) + set(CMAKE_CXX_STANDARD_REQUIRED on) + else() + if (CMAKE_CXX_COMPILER_ID MATCHES "GCC") + message ("-- GNU CXX (-std=c++11)") + list(APPEND CMAKE_CXX_FLAGS "-std=c++11") + elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + message ("-- CLang CXX (-std=c++11)") + list(APPEND CMAKE_CXX_FLAGS "-std=c++11") + elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU") + message ("-- GNU CXX (-std=c++11)") + list(APPEND CMAKE_CXX_FLAGS "-std=c++11") + else() + message ("-- Requires C++11. Your compiler does not support it.") + endif() + endif() +endmacro() + +# http://www.cmake.org/Wiki/CMake_RPATH_handling#Mac_OS_X_and_the_RPATH +if (APPLE) + set(CMAKE_MACOSX_RPATH ON) + set(CMAKE_SKIP_BUILD_RPATH FALSE) + set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) + set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") + set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) + list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir) + if("${isSystemDir}" STREQUAL "-1") + set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") + endif() +endif() + +list (APPEND CMAKE_CXX_FLAGS " -Wall ") + +require_cpp11() + +# Check for cryptopp (static) +set(CryptoPP_USE_STATIC_LIBS ON) +find_package(CryptoPP REQUIRED) +message ("-- Crypto++ binary: " ${CRYPTOPP_LIBRARY}) +include_directories (${CRYPTOPP_INCLUDE_DIRS}) + +# Check for include files and stdlib properties. +include (CheckIncludeFileCXX) +check_include_file_cxx (attr/xattr.h HAVE_ATTR_XATTR_H) +check_include_file_cxx (sys/xattr.h HAVE_SYS_XATTR_H) + +# Check if xattr functions take extra arguments, as they do on OSX. +# Output error is misleading, so do this test quietly. +include (CheckCXXSourceCompiles) +set(CMAKE_REQUIRED_QUIET_SAVE ${CMAKE_REQUIRED_QUIET}) +set (CMAKE_REQUIRED_QUIET True) +check_cxx_source_compiles ("#include +#include +int main() { getxattr(0,0,0,0,0,0); return 1; } +" XATTR_ADD_OPT) +set (CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE}) + +# Reference all headers, to make certain IDEs happy. +file (GLOB_RECURSE all_headers ${CMAKE_SOURCE_DIR}/*.h) +add_custom_target (all_placeholder SOURCES ${all_headers}) + + +########################################## Unit Testing ################################### + +# Check for Easylogging++ +find_package(EASYLOGGINGPP REQUIRED) +include_directories (${EASYLOGGINGPP_INCLUDE_DIR}) + +find_package (gtest REQUIRED) + +include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) + +enable_testing() + +add_executable(mine-unit-tests + test/main.cc + ${EASYLOGGINGPP_INCLUDE_DIR}/easylogging++.cc +) + +# Standard linking to gtest stuff. +target_link_libraries(mine-unit-tests gtest gtest_main) + +# Extra linking for the project. +target_link_libraries(mine-unit-tests ripe) + +add_test(NAME mineUnitTests COMMAND mine-unit-tests) diff --git a/cmake/FindCryptoPP.cmake b/cmake/FindCryptoPP.cmake new file mode 100644 index 0000000..b654ae1 --- /dev/null +++ b/cmake/FindCryptoPP.cmake @@ -0,0 +1,118 @@ +# Module for locating the Crypto++ encryption library. +# +# Customizable variables: +# CRYPTOPP_ROOT_DIR +# This variable points to the CryptoPP root directory. On Windows the +# library location typically will have to be provided explicitly using the +# -D command-line option. The directory should include the include/cryptopp, +# lib and/or bin sub-directories. +# +# Read-only variables: +# CRYPTOPP_FOUND +# Indicates whether the library has been found. +# +# CRYPTOPP_INCLUDE_DIRS +# Points to the CryptoPP include directory. +# +# CRYPTOPP_LIBRARIES +# Points to the CryptoPP libraries that should be passed to +# target_link_libararies. +# +# +# Copyright (c) 2012 Sergiu Dotenco +# +# 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: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# 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. + +INCLUDE (FindPackageHandleStandardArgs) + +FIND_PATH (CRYPTOPP_ROOT_DIR + NAMES cryptopp/cryptlib.h include/cryptopp/cryptlib.h + PATHS ENV CRYPTOPPROOT + DOC "CryptoPP root directory") + +# Re-use the previous path: +FIND_PATH (CRYPTOPP_INCLUDE_DIR + NAMES cryptopp/cryptlib.h + HINTS ${CRYPTOPP_ROOT_DIR} + PATH_SUFFIXES include + DOC "CryptoPP include directory") + +FIND_LIBRARY (CRYPTOPP_LIBRARY_DEBUG + NAMES cryptlibd cryptoppd + HINTS ${CRYPTOPP_ROOT_DIR} + PATH_SUFFIXES lib + DOC "CryptoPP debug library") + +if (CryptoPP_USE_STATIC_LIBS) + message ("==> Static linking to Crypto++") + FIND_LIBRARY (CRYPTOPP_LIBRARY_RELEASE + NAMES libcryptopp.a cryptlib-static cryptopp-static + HINTS ${CRYPTOPP_ROOT_DIR} + PATH_SUFFIXES lib + DOC "CryptoPP release library") +else() + message ("==> Dynamic linking to Crypto++") + FIND_LIBRARY (CRYPTOPP_LIBRARY_RELEASE + NAMES libcryptopp.dylib cryptlib cryptopp + HINTS ${CRYPTOPP_ROOT_DIR} + PATH_SUFFIXES lib + DOC "CryptoPP release library") +endif() + +IF (CRYPTOPP_LIBRARY_DEBUG AND CRYPTOPP_LIBRARY_RELEASE) + SET (CRYPTOPP_LIBRARY + optimized ${CRYPTOPP_LIBRARY_RELEASE} + debug ${CRYPTOPP_LIBRARY_DEBUG} CACHE DOC "CryptoPP library") +ELSEIF (CRYPTOPP_LIBRARY_RELEASE) + SET (CRYPTOPP_LIBRARY ${CRYPTOPP_LIBRARY_RELEASE} CACHE DOC + "CryptoPP library") +ENDIF (CRYPTOPP_LIBRARY_DEBUG AND CRYPTOPP_LIBRARY_RELEASE) + +IF (CRYPTOPP_INCLUDE_DIR) + SET (_CRYPTOPP_VERSION_HEADER ${CRYPTOPP_INCLUDE_DIR}/cryptopp/config.h) + + IF (EXISTS ${_CRYPTOPP_VERSION_HEADER}) + FILE (STRINGS ${_CRYPTOPP_VERSION_HEADER} _CRYPTOPP_VERSION_TMP REGEX + "^#define CRYPTOPP_VERSION[ \t]+[0-9]+$") + + STRING (REGEX REPLACE + "^#define CRYPTOPP_VERSION[ \t]+([0-9]+)" "\\1" _CRYPTOPP_VERSION_TMP + ${_CRYPTOPP_VERSION_TMP}) + + STRING (REGEX REPLACE "([0-9]+)[0-9][0-9]" "\\1" CRYPTOPP_VERSION_MAJOR + ${_CRYPTOPP_VERSION_TMP}) + STRING (REGEX REPLACE "[0-9]([0-9])[0-9]" "\\1" CRYPTOPP_VERSION_MINOR + ${_CRYPTOPP_VERSION_TMP}) + STRING (REGEX REPLACE "[0-9][0-9]([0-9])" "\\1" CRYPTOPP_VERSION_PATCH + ${_CRYPTOPP_VERSION_TMP}) + + SET (CRYPTOPP_VERSION_COUNT 3) + SET (CRYPTOPP_VERSION + ${CRYPTOPP_VERSION_MAJOR}.${CRYPTOPP_VERSION_MINOR}.${CRYPTOPP_VERSION_PATCH}) + ENDIF (EXISTS ${_CRYPTOPP_VERSION_HEADER}) +ENDIF (CRYPTOPP_INCLUDE_DIR) + +SET (CRYPTOPP_INCLUDE_DIRS ${CRYPTOPP_INCLUDE_DIR}) +SET (CRYPTOPP_LIBRARIES ${CRYPTOPP_LIBRARY}) + +MARK_AS_ADVANCED (CRYPTOPP_INCLUDE_DIR CRYPTOPP_LIBRARY CRYPTOPP_LIBRARY_DEBUG + CRYPTOPP_LIBRARY_RELEASE) + +FIND_PACKAGE_HANDLE_STANDARD_ARGS (CryptoPP REQUIRED_VARS CRYPTOPP_ROOT_DIR + CRYPTOPP_INCLUDE_DIR CRYPTOPP_LIBRARY VERSION_VAR CRYPTOPP_VERSION) diff --git a/cmake/FindEASYLOGGINGPP.cmake b/cmake/FindEASYLOGGINGPP.cmake new file mode 100644 index 0000000..c15226f --- /dev/null +++ b/cmake/FindEASYLOGGINGPP.cmake @@ -0,0 +1,38 @@ +# +# CMake module for Easylogging++ logging library +# +# Defines ${EASYLOGGINGPP_INCLUDE_DIR} +# +# If ${EASYLOGGINGPP_USE_STATIC_LIBS} is ON then static libs are searched. +# In these cases ${EASYLOGGINGPP_LIBRARY} is also defined +# +# (c) 2017 Muflihun Labs +# +# https://github.com/muflihun/easyloggingpp +# https://muflihun.com +# + +message ("-- Easylogging++: Searching...") +set(EASYLOGGINGPP_PATHS ${EASYLOGGINGPP_ROOT} $ENV{EASYLOGGINGPP_ROOT}) + +find_path(EASYLOGGINGPP_INCLUDE_DIR + easylogging++.h + PATH_SUFFIXES include + PATHS ${EASYLOGGINGPP_PATHS} +) + +if (EASYLOGGINGPP_USE_STATIC_LIBS) + message ("-- Easylogging++: Static linking is preferred") + find_library(EASYLOGGINGPP_LIBRARY + NAMES libeasyloggingpp.a libeasyloggingpp.dylib libeasyloggingpp + HINTS "${CMAKE_PREFIX_PATH}/lib" + ) +elseif (EASYLOGGINGPP_USE_SHARED_LIBS) + message ("-- Easylogging++: Dynamic linking is preferred") + find_library(EASYLOGGINGPP_LIBRARY + NAMES libeasyloggingpp.dylib libeasyloggingpp libeasyloggingpp.a + HINTS "${CMAKE_PREFIX_PATH}/lib" + ) +endif() + +find_package_handle_standard_args(EASYLOGGINGPP REQUIRED_VARS EASYLOGGINGPP_INCLUDE_DIR) diff --git a/cmake/FindRipe.cmake b/cmake/FindRipe.cmake new file mode 100644 index 0000000..f7378dc --- /dev/null +++ b/cmake/FindRipe.cmake @@ -0,0 +1,41 @@ +# +# CMake module for Ripe cryptography wrapper +# +# Creates ${RIPE_INCLUDE_DIR} and ${RIPE_LIBRARY} +# +# If ${RIPE_USE_STATIC_LIBS} is ON then static libs are preferred over shared +# +# (c) 2017 Muflihun Labs +# +# https://github.com/muflihun/ripe +# https://muflihun.com +# + +message ("-- Ripe: Searching...") +set(RIPE_PATHS ${RIPE_ROOT} $ENV{RIPE_ROOT}) + +find_path(RIPE_INCLUDE_DIR + Ripe.h + PATH_SUFFIXES include + PATHS ${RIPE_PATHS} +) + +if (Ripe_USE_STATIC_LIBS) + message ("-- Ripe: Static linking") + find_library(RIPE_LIBRARY + NAMES libripe.dylib libripe ripe + HINTS "${CMAKE_PREFIX_PATH}/lib" + ) +else() + message ("-- Ripe: Dynamic linking") + find_library(RIPE_LIBRARY + NAMES ripe libripe libripe.dylib + HINTS "${CMAKE_PREFIX_PATH}/lib" + ) +endif() + +message ("-- Ripe: Include: " ${RIPE_INCLUDE_DIR} ", Binary: " ${RIPE_LIBRARY}) + +include(FindPackageHandleStandardArgs) + +find_package_handle_standard_args(Ripe REQUIRED_VARS RIPE_INCLUDE_DIR RIPE_LIBRARY) diff --git a/cmake/Findgtest.cmake b/cmake/Findgtest.cmake new file mode 100644 index 0000000..cd443f4 --- /dev/null +++ b/cmake/Findgtest.cmake @@ -0,0 +1,159 @@ +# Locate the Google C++ Testing Framework. +# +# Defines the following variables: +# +# GTEST_FOUND - Found the Google Testing framework +# GTEST_INCLUDE_DIRS - Include directories +# +# Also defines the library variables below as normal +# variables. These contain debug/optimized keywords when +# a debugging library is found. +# +# GTEST_BOTH_LIBRARIES - Both libgtest & libgtest-main +# GTEST_LIBRARIES - libgtest +# GTEST_MAIN_LIBRARIES - libgtest-main +# +# Accepts the following variables as input: +# +# GTEST_ROOT - (as a CMake or environment variable) +# The root directory of the gtest install prefix +# +# GTEST_MSVC_SEARCH - If compiling with MSVC, this variable can be set to +# "MD" or "MT" to enable searching a GTest build tree +# (defaults: "MD") +# +#----------------------- +# Example Usage: +# +# enable_testing() +# find_package(GTest REQUIRED) +# include_directories(${GTEST_INCLUDE_DIRS}) +# +# add_executable(foo foo.cc) +# target_link_libraries(foo ${GTEST_BOTH_LIBRARIES}) +# +# add_test(AllTestsInFoo foo) +# +#----------------------- +# +# If you would like each Google test to show up in CTest as +# a test you may use the following macro. +# NOTE: It will slow down your tests by running an executable +# for each test and test fixture. You will also have to rerun +# CMake after adding or removing tests or test fixtures. +# +# GTEST_ADD_TESTS(executable extra_args ARGN) +# executable = The path to the test executable +# extra_args = Pass a list of extra arguments to be passed to +# executable enclosed in quotes (or "" for none) +# ARGN = A list of source files to search for tests & test +# fixtures. +# +# Example: +# set(FooTestArgs --foo 1 --bar 2) +# add_executable(FooTest FooUnitTest.cc) +# GTEST_ADD_TESTS(FooTest "${FooTestArgs}" FooUnitTest.cc) + +#============================================================================= +# Copyright 2009 Kitware, Inc. +# Copyright 2009 Philip Lowman +# Copyright 2009 Daniel Blezek +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file Copyright.txt for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distributed this file outside of CMake, substitute the full +# License text for the above reference.) +# +# Thanks to Daniel Blezek for the GTEST_ADD_TESTS code + +function(GTEST_ADD_TESTS executable extra_args) + if(NOT ARGN) + message(FATAL_ERROR "Missing ARGN: Read the documentation for GTEST_ADD_TESTS") + endif() + foreach(source ${ARGN}) + file(READ "${source}" contents) + string(REGEX MATCHALL "TEST_?F?\\(([A-Za-z_0-9 ,]+)\\)" found_tests ${contents}) + foreach(hit ${found_tests}) + string(REGEX REPLACE ".*\\( *([A-Za-z_0-9]+), *([A-Za-z_0-9]+) *\\).*" "\\1.\\2" test_name ${hit}) + add_test(${test_name} ${executable} --gtest_filter=${test_name} ${extra_args}) + list(APPEND _test_names ${test_name}) + endforeach() + endforeach() + set(GTEST_ADD_TEST_NAMES ${_test_names} PARENT_SCOPE) +endfunction() + +function(_gtest_append_debugs _endvar _library) + if(${_library} AND ${_library}_DEBUG) + set(_output optimized ${${_library}} debug ${${_library}_DEBUG}) + else() + set(_output ${${_library}}) + endif() + set(${_endvar} ${_output} PARENT_SCOPE) +endfunction() + +function(_gtest_find_library _name) + find_library(${_name} + NAMES ${ARGN} + HINTS + $ENV{GTEST_ROOT} + ${GTEST_ROOT} + PATH_SUFFIXES ${_gtest_libpath_suffixes} + ) + mark_as_advanced(${_name}) +endfunction() + +# + +if(NOT DEFINED GTEST_MSVC_SEARCH) + set(GTEST_MSVC_SEARCH MD) +endif() + +set(_gtest_libpath_suffixes lib) +if(MSVC) + if(GTEST_MSVC_SEARCH STREQUAL "MD") + list(APPEND _gtest_libpath_suffixes + msvc/gtest-md/Debug + msvc/gtest-md/Release) + elseif(GTEST_MSVC_SEARCH STREQUAL "MT") + list(APPEND _gtest_libpath_suffixes + msvc/gtest/Debug + msvc/gtest/Release) + endif() +endif() + + +find_path(GTEST_INCLUDE_DIR gtest/gtest.h + HINTS + $ENV{GTEST_ROOT}/include + ${GTEST_ROOT}/include +) +mark_as_advanced(GTEST_INCLUDE_DIR) + +if(MSVC AND GTEST_MSVC_SEARCH STREQUAL "MD") + # The provided /MD project files for Google Test add -md suffixes to the + # library names. + _gtest_find_library(GTEST_LIBRARY gtest-md gtest) + _gtest_find_library(GTEST_LIBRARY_DEBUG gtest-mdd gtestd) + _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main-md gtest_main) + _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_main-mdd gtest_maind) +else() + _gtest_find_library(GTEST_LIBRARY gtest) + _gtest_find_library(GTEST_LIBRARY_DEBUG gtestd) + _gtest_find_library(GTEST_MAIN_LIBRARY gtest_main) + _gtest_find_library(GTEST_MAIN_LIBRARY_DEBUG gtest_maind) +endif() + +include(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTest DEFAULT_MSG GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY) + +if(GTEST_FOUND) + set(GTEST_INCLUDE_DIRS ${GTEST_INCLUDE_DIR}) + _gtest_append_debugs(GTEST_LIBRARIES GTEST_LIBRARY) + _gtest_append_debugs(GTEST_MAIN_LIBRARIES GTEST_MAIN_LIBRARY) + set(GTEST_BOTH_LIBRARIES ${GTEST_LIBRARIES} ${GTEST_MAIN_LIBRARIES}) +endif() \ No newline at end of file diff --git a/include/mine.h b/include/mine.h new file mode 100644 index 0000000..9deaed2 --- /dev/null +++ b/include/mine.h @@ -0,0 +1,606 @@ +// +// mine.h +// +// https://github.com/muflihun/mine +// + +#ifndef MINE_H +#define MINE_H + +#include +#include +#include +#include +#include +#include +#include +#include + +/// +/// PLEASE NOTE, DO NOT USE UNTIL THE FIRST RELEASE +/// +class Mine { +public: + + typedef CryptoPP::Integer BigInteger; // temp + + class PublicKey { + public: + PublicKey() = default; + + PublicKey(BigInteger n, int e) : + m_n(n), + m_e(e) + { + } + + virtual ~PublicKey() = default; + + inline BigInteger n() const { return m_n; } + inline int e() const { return m_e; } + + private: + BigInteger m_n; + int m_e; + }; + + class RawKey { + public: + static const unsigned int DEFAULT_PUBLIC_EXPONENT = 65537; + + RawKey(BigInteger p, BigInteger q, int e = DEFAULT_PUBLIC_EXPONENT, bool skipPrimeCheck = true) : + m_p(p), + m_q(q), + m_e(e) + { + if (!skipPrimeCheck && (!isPrime(p) || !isPrime(q))) { + throw std::invalid_argument("p and q must be prime numbers unique to each other"); + } + if (p == q || p == 0 || q == 0) { + throw std::invalid_argument("p and q must be prime numbers unique to each other"); + } + + BigInteger pMinus1 = m_p - 1; + BigInteger qMinus1 = m_q - 1; + BigInteger phi = pMinus1 * qMinus1; + + if (gcd(m_e, phi) != 1) { + throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); + } + m_n = m_p * m_q; + m_coeff = modInverse(m_q, m_p); + + m_d = modInverse(m_e, phi); + + // note: + // https://www.ipa.go.jp/security/rfc/RFC3447EN.html#2 says to use m_e + // openssl says to use m_d + m_dp = BigInteger(m_d) % pMinus1; + m_dq = BigInteger(m_d) % qMinus1; + } + + virtual ~RawKey() = default; + + inline BigInteger p() const { return m_p; } + inline BigInteger q() const { return m_q; } + inline BigInteger coeff() const { return m_coeff; } + inline BigInteger n() const { return m_n; } + inline int e() const { return m_e; } + inline BigInteger d() const { return m_d; } + inline BigInteger dp() const { return m_dq; } + inline BigInteger dq() const { return m_dp; } + + friend std::ostream& operator<<(std::ostream& ss, const RawKey& k) + { + ss << "modulus: " << k.m_n << "\npublicExponent: " << k.m_e << "\nprivateExponent: " << k.m_d + << "\nprime1: " << k.m_p << "\nprime2: " << k.m_q << "\nexponent1: " << k.m_dp << "\nexponent2: " + << k.m_dq << "\ncoefficient: " << k.m_coeff; + return ss; + } + + std::string exportDER() const + { + std::stringstream ss; + ss << "asn1=SEQUENCE:rsa_key\n\n"; + ss << "[rsa_key]\n"; + ss << "version=INTEGER:0\n"; + ss << "modulus=INTEGER:" << bigIntegerToString(m_n) << "\n"; + ss << "pubExp=INTEGER:" << m_e << "\n"; + ss << "privExp=INTEGER:" << bigIntegerToString(m_d) << "\n"; + ss << "p=INTEGER:" << bigIntegerToString(m_p) << "\n"; + ss << "q=INTEGER:" << bigIntegerToString(m_q) << "\n"; + ss << "e1=INTEGER:" << bigIntegerToString(m_dp) << "\n"; + ss << "e2=INTEGER:" << bigIntegerToString(m_dq) << "\n"; + ss << "coeff=INTEGER:" << bigIntegerToString(m_coeff); + return ss.str(); + } + + private: + BigInteger m_p; + BigInteger m_q; + int m_e; + BigInteger m_coeff; + BigInteger m_n; + BigInteger m_d; + BigInteger m_dp; + BigInteger m_dq; + }; + + typedef RawKey PrivateKey; + + class KeyPair : public RawKey { + public: + KeyPair(BigInteger p, BigInteger q, unsigned int exp = DEFAULT_PUBLIC_EXPONENT) : + RawKey(p, q, exp) { + m_publicKey = PublicKey(n(), e()); + } + + inline const PublicKey* publicKey() const { return &m_publicKey; } + inline const PrivateKey* privateKey() const { return this; } + + private: + PublicKey m_publicKey; + }; + + /// + /// \brief Generic RSA encryption. T can of std::string or std::wstring + /// or custom similar type + /// + /// \return hex of final octet string + /// + template + static std::string encrypt(const PublicKey* publicKey, const T& m) + { + BigInteger paddedMsg = pkcs1pad2(m, (publicKey->n().BitCount() + 7) >> 3); + // TODO: It can be made better + std::stringstream ss; + ss << std::hex << powerMod(paddedMsg, publicKey->e(), publicKey->n()); + std::string h(ss.str()); + h.erase(h.end() - 1); + return ((h.size() & 1) == 0) ? h : ("0" + h); + } + + /// + /// \brief Encrypts wstring msg using public key. + /// + /// \return hex of cipher. Padded using PKCS#1 padding scheme + /// + static std::string encrypt(const PublicKey* publicKey, + const std::wstring& message) + { + return encrypt(publicKey, message); + } + + /// + /// \brief Encrypts string msg using public key + /// + /// \return hex of cipher. Padded using PKCS#1 padding scheme + /// + static std::string encrypt(const PublicKey* publicKey, + const std::string& message) + { + return encrypt(publicKey, message); + } + + /// + /// \brief Decrypts RSA hex message m using private key + /// \param cipher Cipher in hex format (should not start with 0x) + /// \return Plain text, return type depends on TResult + /// + template + static TResult decrypt(const PrivateKey* privateKey, const std::string& cipher) + { + // TODO: Add checks https://tools.ietf.org/html/rfc3447#section-7.2.2 + + std::string readableMsg = "0x" + cipher; // 0x helps BigInteger read m as 16-bit integer + BigInteger msg(readableMsg.c_str()); + // https://tools.ietf.org/html/rfc3447#section-4.1 + int xlen = (privateKey->n().BitCount() + 7) >> 3; + if (msg >= power(BigInteger(256), BigInteger(xlen))) { + throw std::runtime_error("Integer too large"); + } + BigInteger decr = powerMod(msg, privateKey->d(), privateKey->n()); + return pkcs1unpad2(decr, xlen); + } + + /// + /// \brief Verifies signature for text using RSA public key + /// \param message An octet string + /// \param signature Signature in hex + /// \see https://tools.ietf.org/html/rfc3447#section-8.1.2 + /// + static bool verify(const PublicKey* publicKey, const std::string& message, + const std::string& signature) + { + /* TODO: Add it to test + std::vector f = getByteArray(18537); + BigInteger s = i2osp(f, 2); + BigInteger os = os2ip(18537); // 105 + + std::cout << s << std::endl; + std::cout << os << std::endl; + + std::vector f2 = getByteArray(1214841185); + BigInteger s2 = i2osp(f2, 4); + BigInteger os2 = os2ip(1214841185); // 97 + + std::cout << s2 << std::endl; + std::cout << os2 << std::endl; + + return true;*/ + + std::string readableSign = "0x" + signature; + BigInteger sign(readableSign.c_str()); + try { + BigInteger vp = createVerificationPrimitive(publicKey, sign); + // I2OSP + + std::cout << vp << std::endl; + + const int xlen = (publicKey->n().BitCount() + 7) >> 3; + + if (xlen < vp + 11) { + // throw std::runtime_error("Integer too large"); // Needed??! Where in RFC? + } + + BigInteger em = i2osp(vp, xlen); + + // manually test + std::string encr = encrypt(publicKey, message); + + + // todo: add check for following (as per https://tools.ietf.org/html/rfc3447#section-8.1.2) + // Note that emLen will be one less than k if modBits - 1 is + // divisible by 8 and equal to k otherwise. If I2OSP outputs + // "integer too large," output "invalid signature" and stop. + + // EMSA-PSS_VERIFY - https://tools.ietf.org/html/rfc3447#section-9.1.2 + + + + std::cout << em << " " << encr << std::endl; + return true; + } catch (std::exception&) { + } + + return false; + } + +private: + + static BigInteger os2ip(const BigInteger& x) + { + return x & 0xff; + } + + /// + /// \brief Integer to octet-string + /// + static BigInteger i2osp(const BigInteger& x, int xlen) + { + return i2osp(getByteArray(x), xlen); + } + + /// + /// \brief Integer to octet-string + /// + template + static BigInteger i2osp(const std::vector& x, int xlen) + { + const BigInteger b256 = 256; + + BigInteger result = 0; + std::size_t len = x.size(); + for (std::size_t i = len; i > 0; --i) { + result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); + } + return result; + } + + /// + /// \brief Get byte array from big integer + /// + template + static std::vector getByteArray(BigInteger x, int xlen = -1) + { + const BigInteger b256 = 256; + xlen = xlen == -1 ? x.ByteCount() : xlen; + + std::vector ba(xlen); + BigInteger r; + BigInteger q; + + for (int i = 1; i <= xlen; ++i) { + x.Divide(r, q, x, power(b256, BigInteger(xlen - i))); + ba[i - 1] = static_cast(q.ConvertToLong()); // todo: Check! + x = r; + } + return ba; + } + + /// + /// \brief Creates RSA VP for verification + /// \param signature signature representative, an integer between 0 and n - 1 + /// \return message representative, an integer between 0 and n - 1 + /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 + /// + static BigInteger createVerificationPrimitive(const PublicKey* publicKey, + const BigInteger& signature) + { + if (signature < 0 || signature > publicKey->n() - 1) { + throw std::runtime_error("signature representative out of range"); + } + return powerMod(signature, publicKey->e(), publicKey->n()); + } + + /// + /// \brief PKCS #1 padding + /// \see https://tools.ietf.org/html/rfc3447#page-23 + /// \return corresponding nonnegative integer + /// + template + static BigInteger pkcs1pad2(const T& s, int n) { + if (n < s.size() + 11) { + throw std::runtime_error("Message too long"); + } + std::vector byteArray(n); + long long i = s.size() - 1; + while(i >= 0 && n > 0) { + int c = static_cast(s.at(i--)); + if (c < 128) { + // utf + byteArray[--n] = c; + } else if ((c > 127) && (c < 2048)) { + // 16-bit + byteArray[--n] = (c & 63) | 128; + byteArray[--n] = (c >> 6) | 192; + } else { + // 24-bit + byteArray[--n] = (c & 63) | 128; + byteArray[--n] = ((c >> 6) & 63) | 128; + byteArray[--n] = (c >> 12) | 224; + } + } + + // now padding i.e, 0x00 || 0x02 || PS || 0x00 + // see point #2 on https://tools.ietf.org/html/rfc3447#section-7.2.1 => EME-PKCS1-v1_5 encoding + + byteArray[--n] = 0; + + srand(time(NULL)); + int r = rand() % 100 + 1; + while (n > 2) { + r = 0; + while (r == 0) { + r = rand() % 100 + 1; + } + byteArray[--n] = r; + } + byteArray[--n] = 2; + byteArray[--n] = 0; + return i2osp(byteArray, n); + } + + /// + /// \brief PKCS #1 unpadding + /// \see https://tools.ietf.org/html/rfc3447#section-4.1 + /// \return corresponding octet string of length n + /// + template + static T pkcs1unpad2(const BigInteger& m, unsigned long n) + { + std::vector ba = getByteArray(m, n); + std::size_t baLen = ba.size(); + int i = 0; + while (i < baLen && ba[i] == 0) { + // ignore first zeros + ++i; + } + if (baLen - 1 != n - 1 // reached end + || ba[i] != 2) { // next available char is not 2 as per padding standard + throw std::runtime_error("Incorrect padding PKCS#1"); + } + ++i; // we're all good so far, + // lets check for the + + // if we hit end while still we're still with zeros, it's a padding error + while (ba[i] != 0) { + if (++i >= baLen) { // already ended! + throw std::runtime_error("Incorrect padding PKCS#1"); + } + } + ++i; + // now we should be at the first non-zero byte + // which is our first item, we concat all + + std::basic_stringstream ss; + for (; i < baLen; ++i) { + // reference: http://en.cppreference.com/w/cpp/language/types -> range of values + int c = ba[i] & 0xFF; + if (c < 128) { + // utf-8 + ss << static_cast(c); + } else if ((c > 191) && (c < 224)) { // 16-bit char + ss << static_cast(((c & 31) << 6) | (ba[i+1] & 63)); + ++i; + } else { // 24-bit char + ss << static_cast(((c & 15) << 12) | ((ba[i+1] & 63) << 6) | (ba[i+2] & 63)); + i += 2; + } + } + return ss.str(); + } + + /// + /// \brief Fast GCD + /// \see https://en.wikipedia.org/wiki/Euclidean_algorithm#Extended_Euclidean_algorithm + /// + static BigInteger gcd(BigInteger a, BigInteger b) + { + BigInteger c; + while (a != 0) { + c = a; + a = b % a; + b = c; + } + return b; + } + + /// + /// \brief Simple (base ^ e) mod m implementation + /// \param b Base + /// \param e Exponent + /// \param m Mod + /// + static BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) + { + BigInteger res = 1; + while (e > 0) { + if (e % 2 != 0) { + res = (b * res) % m; + } + b = (b * b) % m; + e /= 2; + } + return res; + } + + /// + /// \brief Power of numb i.e, b ^ e + /// + static BigInteger power(BigInteger b, BigInteger e) + { + BigInteger result = 1; + while (e > 0) { + if (e.IsOdd()) { + // we decrement exponent to make it even + e--; + // store this multiplication directly to the + // result + result *= b; + // we modify this alg to ignore the next multiplication + // if we have already reached 0 (for speed) + // here are details and what we changed and how it all works + // + // Let's say we have case of 2 ^ 4 [expected answer = 16] + // 2 ^ 4 -- b = 4, e = 2 [result = 1] + // 2 ^ 2 -- b = 16, e = 1 [result = 1] + // 2 ^ 1 -- e = 0 [result = 1 * 16] + // + // here is what we changed here + // now we have result set and we have e set to zero + // doing another b ^= b means b = 16 * 16 = 256 (in our case) + // which is useless so we end here + if (e == 0) { + break; + } + } + e /= 2; + b *= b; + } + return result; + } + + static BigInteger modInverse(BigInteger a, BigInteger b) + { + BigInteger b0 = b, t, q; + BigInteger x0 = 0, x1 = 1; + if (b == 1) { + return 1; + } + while (a > 1) { + q = a / b; + t = b; + b = a % b; + a = t; + t = x0; + x0 = x1 - q * x0; + x1 = t; + } + if (x1 < 0) { + x1 += b0; + } + return x1; + } + + /// + /// \brief Checks whether n is prime or not + /// This is fast, see https://en.wikipedia.org/wiki/Primality_test#Pseudocode + /// for details + /// + static bool isPrime(BigInteger n) + { + if (n <= 1) { + return false; + } + if (n <= 3) { + return true; + } + if (n % 2 == 0 || n % 3 == 0) { + return false; + } + for (BigInteger i = 5; i * i <= n; i += 6) { + if (n % i == 0 || n % (i + 2) == 0) { + return false; + } + } + return true; + } + + /// + /// \brief Decimal to specific base + /// \param n Number + /// \param b Base - default is 8 (octet) + /// + template + static T d2o(T n, T b = 8) + { + T r, i = 1, o = 0; + while (n != 0) { + r = n % b; + n /= b; + o += r * i; + i *= 10; + } + return o; + } + + /// + /// \brief Specific base to decimal + /// \param n Number + /// \param b Base - default is from octet (base 8) + /// + template + static T o2d(T n, T b = 8) + { + T r, i = 0, o = 0; + while (n != 0) { + r = n % 10; + n /= 10; + o += r * power(b, i); + ++i; + } + return o; + } + + /// + /// \brief Big integer adds suffix at the end so we use this function + /// to remove it + /// + static std::string bigIntegerToString(const BigInteger& b) + { + std::stringstream ss; + ss << b; + std::string sss(ss.str()); + sss.erase(sss.end() - 1); + return sss; + } + + // for tests + friend class MineRSATest_IsPrime_Test; + friend class MineRSATest_FindGCD_Test; + friend class MineRSATest_InvModulo_Test; + friend class MineRSATest_PowerMod_Test; +}; + +#endif // MINE_H diff --git a/test/main.cc b/test/main.cc new file mode 100644 index 0000000..7cf6262 --- /dev/null +++ b/test/main.cc @@ -0,0 +1,12 @@ +#include "test.h" +#include "rsa-test.h" + +INITIALIZE_EASYLOGGINGPP + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput); + el::Loggers::addFlag(el::LoggingFlag::ImmediateFlush); + + return ::testing::UnitTest::GetInstance()->Run(); +} diff --git a/test/rsa-test.h b/test/rsa-test.h new file mode 100644 index 0000000..7eecc8f --- /dev/null +++ b/test/rsa-test.h @@ -0,0 +1,194 @@ +#ifndef MINE_TEST_H +#define MINE_TEST_H + +#include "include/mine.h" +#include "test.h" + +// numb, expected +static TestData IsPrimeData = { + TestCase(1, false), + TestCase(2, true), + TestCase(44, false), + TestCase(43, true), + TestCase(57, false), + TestCase(257, true), +}; + +// a, b, expected mod, expected mod_inv +static TestData InvModuloData = { + TestCase(3, 11, 4), + TestCase(1, 2, 1), + TestCase(199, 2443, 1510), + TestCase(2443, 199, 76), + TestCase(17, 3120, 2753), +}; + +// b, e, m, exp +static TestData PowerModData = { + TestCase(5, 3, 1, 0), + TestCase(5, 3, 19, 11), + TestCase(3, 11, 4, 3), + TestCase(5, 117, 19, 1), + TestCase(5, 64, 19, 5), + TestCase(5, 2, 19, 6), + TestCase(5, 4, 19, 17), + TestCase(5, 8, 19, 4), + TestCase(7, 256, 13, 9), +}; + +// a, b, expected +static TestData GCDData = { + TestCase(270, 192, 6), +}; + +// p, q, d, e +static TestData RawKeyData = { + TestCase(173, 149, 16971, 3), + TestCase(7, 11, 53, Mine::KeyPair::DEFAULT_PUBLIC_EXPONENT), + TestCase(53, 59, 2011, 3), + TestCase(3, 11, 13, Mine::KeyPair::DEFAULT_PUBLIC_EXPONENT), + TestCase(11, 3, 13, Mine::KeyPair::DEFAULT_PUBLIC_EXPONENT), + TestCase(11, 17, 107, 3), + TestCase(60779, 53003, 1986380529, 65537), + TestCase(Mine::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), Mine::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), Mine::BigInteger("6582637129463060155009365989980507690389844347218288265898882119918384609608605061080359962338234258873604135082233881579524605068749537845926158658339195"), 3), // << - this is what we tested based upon + TestCase(Mine::BigInteger("108806323825932706977307191097259117033353572146991115579334232319532442798209"), Mine::BigInteger("75778358732466892022501809496541864532894038434008219546470758430052996329071"), Mine::BigInteger("5496776426161658798940200169671739270887755348701962910881820208997794909110934102331035956003239535747096593580882262107967594097892650413676521849537707"), 3), + TestCase(Mine::BigInteger("176360517760307645469197766454483974235511085138581196179561347493397045582678676376582697316359235034160209749898412011153924577295946180206410049279151818330310786286636241193330444606031071350600285897477381895748147316188740513022461102919987041280831098968434434553879045380055670995086500659087065302403"), Mine::BigInteger("169163723758010117173450277772073715921803592964927638245731997826080549397171438893995388301374973303599758650257957793677462633788535432641753359275162340138639711102283198388259082836510170614304484108899685152902783320622394241970680405511348370667697428176827008839392860840538166537806520720483413747299"), Mine::BigInteger("19889201272149546443463155392252159315174210881947747082976069645146016944350039871470895772510979533532867685298201602992918775321216324576337623180033432704404378220468328792827286239010112541240591233928213472506415790478729110344923198900414497739281740852945910987895868475178794593401701658716065890906852003893420692929407600046549070094684609746581564079903528672418432707811264082243503362255422665241970781850081871999244191153644696361248245946591425338244340405196223004592171521190997670962141503496215757774355742872186005655701283224796723544068646999720522489543729822936326934344869201943856253606531"), 3), +}; + +// msg +static TestData RSAEncryptionData = { + TestCase(L"G'day"), + TestCase(L"Hi"), + TestCase(L"Hello竜"), +}; +// msg +static TestData RSAEncryptionStringData = { + TestCase("Plain string"), +}; + +// p, q, e, cipher, msg +static TestData RSADecryptionData = { + TestCase(Mine::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), Mine::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "55a5f32084cdbbd3edcba573317f99678a1b85b6c455fa86476d697900ce5fd95ec599a16690d5e7c2196608477ac1006e86c74cbd25b7e4681e026774381e63", L"Apple"), +}; + +// p, q, e, signature, text +static TestData RSASignatureData = { + TestCase(Mine::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), Mine::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "01400ccd971dad2744c37baf7f5cf13a5590a675c90354f2002d4c6a6a7ef3d1377986e1d8f0b69676e243fae8cf6c6bbdc7f18deb0e0418fe6452c4afb1e4b5", "test"), +}; + +TEST(MineRSATest, FindGCD) +{ + for (const auto& item : GCDData) { + LOG(INFO) << "Finding GCD for " << PARAM(0) << " and " << PARAM(1); + ASSERT_EQ(Mine::gcd(PARAM(0), PARAM(1)), PARAM(2)); + } +} + +TEST(MineRSATest, IsPrime) +{ + for (const auto& item : IsPrimeData) { + // LOG(INFO) << "Testing " << PARAM(0) << " == prime: to be " << std::boolalpha << PARAM(1); + ASSERT_EQ(Mine::isPrime(PARAM(0)), PARAM(1)); + } +} + +TEST(MineRSATest, PowerMod) +{ + for (const auto& item : PowerModData) { + ASSERT_EQ(Mine::powerMod(PARAM(0), PARAM(1), PARAM(2)), PARAM(3)); + } +} + +TEST(MineRSATest, InvModulo) +{ + for (const auto& item : InvModuloData) { + ASSERT_EQ(Mine::modInverse(PARAM(0), PARAM(1)), PARAM(2)); + } +} + +TEST(MineRSATest, KeyAndEncryptionDecryption) +{ + for (const auto& item : RawKeyData) { + int bits = PARAM(0).BitCount() + PARAM(1).BitCount(); + LOG(INFO) << "Generating key " << bits << "-bit..."; + + Mine::KeyPair k(PARAM(0), PARAM(1), PARAM(3)); + ASSERT_EQ(k.p(), PARAM(0)); + ASSERT_EQ(k.q(), PARAM(1)); + ASSERT_EQ(k.d(), PARAM(2)); + ASSERT_EQ(k.e(), PARAM(3)); + ASSERT_EQ(k.publicKey()->n(), k.n()); + ASSERT_EQ(k.publicKey()->e(), k.e()); + + if (bits > 32) { + std::cout << "Key DER: " << std::endl << k.exportDER() << std::endl; + } + + for (const auto& item2 : RSAEncryptionData) { + std::wstring msg = std::get<0>(item2); + LOG(INFO) << "Testing: " << msg; + if (bits <= 32) { + EXPECT_THROW(Mine::encrypt(k.publicKey(), msg), std::runtime_error); + } else { + std::string encr = Mine::encrypt(k.publicKey(), msg); + LOG(INFO) << "Encr: " << encr; + std::wstring decr = Mine::decrypt(k.privateKey(), encr); + ASSERT_STREQ(decr.c_str(), msg.c_str()); + } + } + + for (const auto& item2 : RSAEncryptionStringData) { + std::string msg = std::get<0>(item2); + LOG(INFO) << "Testing: " << msg; + if (bits <= 32) { + EXPECT_THROW(Mine::encrypt(k.publicKey(), msg), std::runtime_error); + } else { + std::string encr = Mine::encrypt(k.publicKey(), msg); + LOG(INFO) << "Encr: " << encr; + std::string decr = Mine::decrypt(k.privateKey(), encr); + ASSERT_STREQ(decr.c_str(), msg.c_str()); + } + } + } +} + +TEST(MineRSATest, Decryption) +{ + for (const auto& item : RSADecryptionData) { + int bits = PARAM(0).BitCount() + PARAM(1).BitCount(); + LOG(INFO) << "Generating key " << bits << "-bit..."; + + Mine::KeyPair k(PARAM(0), PARAM(1), PARAM(2)); + + std::string cipher = PARAM(3); + std::wstring expected = PARAM(4); + LOG(INFO) << "Testing: " << cipher; + if (bits <= 32) { + EXPECT_THROW(Mine::decrypt(k.privateKey(), cipher), std::runtime_error); + } else { + std::wstring decr = Mine::decrypt(k.privateKey(), cipher); + ASSERT_STREQ(decr.c_str(), expected.c_str()); + } + } +} + +TEST(MineRSATest, Signature) +{ + for (const auto& item : RSASignatureData) { + int bits = PARAM(0).BitCount() + PARAM(1).BitCount(); + LOG(INFO) << "Generating key " << bits << "-bit..."; + + Mine::KeyPair k(PARAM(0), PARAM(1), PARAM(2)); + + std::string sign = PARAM(3); + std::string text = PARAM(4); + LOG(INFO) << "Testing: " << text; + if (bits <= 32) { + EXPECT_THROW(Mine::verify(k.publicKey(), text, sign), std::runtime_error); + } else { + ASSERT_TRUE(Mine::verify(k.publicKey(),text, sign)); + } + } +} + +#endif // MINE_TEST_H diff --git a/test/test.h b/test/test.h new file mode 100644 index 0000000..0b42db8 --- /dev/null +++ b/test/test.h @@ -0,0 +1,20 @@ +#ifndef TEST_HELPERS_H_ +#define TEST_HELPERS_H_ + +#include +#include + +#include +#include + +template +using TestData = const std::vector>; + +template +std::tuple TestCase(T... f) { + return std::make_tuple(f...); +} + +#define PARAM(v) std::get(item) + +#endif From aff8f1b640985d8e5e2d35433cf836b50cfd87df Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 20 Aug 2017 21:29:23 +1000 Subject: [PATCH 002/148] travis setup --- .travis.yml | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ CMakeLists.txt | 2 +- include/mine.h | 1 - 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..d6ba339 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,51 @@ +language: cpp +compiler: + - clang +dist: trusty +before_install: + - sudo add-apt-repository ppa:kubuntu-ppa/backports -y + - sudo apt-get -qq update + - sudo apt-get install -y libssl-dev libgtest-dev valgrind + - sudo apt-get install --only-upgrade cmake + - cmake --version + # Crypto++ (We manually install it because we need Pem Pack as well) + - wget https://raw.githubusercontent.com/muflihun/muflihun.github.io/master/downloads/cryptocpp.tar.gz + - tar xf cryptocpp.tar.gz + - cd cryptopp-CRYPTOPP_5_6_5 + - wget https://raw.githubusercontent.com/muflihun/muflihun.github.io/master/downloads/pem_pack.zip + - unzip pem_pack.zip + - cmake . + - make + - sudo make install + # GTest + - wget -O gtest.tar.gz https://github.com/google/googletest/archive/release-1.7.0.tar.gz + - tar xf gtest.tar.gz + - cd googletest-release-1.7.0 + - cmake -DBUILD_SHARED_LIBS=ON . + - make + - sudo cp -a include/gtest /usr/include + - sudo cp -a libgtest_main.so libgtest.so /usr/lib/ + - cd .. + ## Easylogging++ + - wget https://github.com/muflihun/easyloggingpp/archive/master.zip + - unzip master.zip + - cd easyloggingpp-master + - cmake . + - make + - sudo make install + ## Build + - cd "${TRAVIS_BUILD_DIR}" + - mkdir build + - cd build + - pwd + - ls -l + - ls -l .. + - cmake -Dtest=ON -Dtravis=ON .. + - make + - sudo make install + +script: "./mine-unit-tests" +branches: + only: + - develop + diff --git a/CMakeLists.txt b/CMakeLists.txt index a23cf55..7bf9b90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,6 +102,6 @@ add_executable(mine-unit-tests target_link_libraries(mine-unit-tests gtest gtest_main) # Extra linking for the project. -target_link_libraries(mine-unit-tests ripe) +target_link_libraries(mine-unit-tests ${CRYPTOPP_LIBRARIES}) add_test(NAME mineUnitTests COMMAND mine-unit-tests) diff --git a/include/mine.h b/include/mine.h index 9deaed2..28efffb 100644 --- a/include/mine.h +++ b/include/mine.h @@ -13,7 +13,6 @@ #include #include #include -#include #include /// From e7add2550c6f8b5d9eb19db0002279a6cbba16f8 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 20 Aug 2017 21:31:42 +1000 Subject: [PATCH 003/148] Badges --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 7f557a7..c112a56 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,10 @@ # Mine Mine is minimal cryptography implementation of [RFC-3447](https://tools.ietf.org/html/rfc3447) and [RFC-3602](https://tools.ietf.org/html/rfc3602). +[![Build Status (Develop)](https://img.shields.io/travis/muflihun/mine/develop.svg)](https://travis-ci.org/muflihun/mine) +[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/muflihun/mine/blob/master/LICENCE) +[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/MuflihunDotCom/25) + # Introduction It all started with [ripe](https://github.com/muflihun/ripe) that is dependent upon third-party library (initially OpenSSL then Crypto++) statically linked. However after using it for a while in [residue](https://github.com/muflihun/residue), we realized that portability became an issue for _minimal_ library. So we decided to start implementing the standards ourself, forming _mine_. From 2e352f817e5164a685b2467a7bbbdee1d9edb8c3 Mon Sep 17 00:00:00 2001 From: Majid Date: Sun, 20 Aug 2017 21:32:56 +1000 Subject: [PATCH 004/148] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c112a56..8013ba5 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ # Mine Mine is minimal cryptography implementation of [RFC-3447](https://tools.ietf.org/html/rfc3447) and [RFC-3602](https://tools.ietf.org/html/rfc3602). -[![Build Status (Develop)](https://img.shields.io/travis/muflihun/mine/develop.svg)](https://travis-ci.org/muflihun/mine) -[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/muflihun/mine/blob/master/LICENCE) +[![Build Status](https://img.shields.io/travis/muflihun/mine/develop.svg)](https://travis-ci.org/muflihun/mine) +[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/muflihun/mine/blob/master/LICENCE) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/MuflihunDotCom/25) # Introduction From e993d97a085ab84d6422d3e007286b10f2ad6e5e Mon Sep 17 00:00:00 2001 From: Majid Date: Sun, 20 Aug 2017 21:33:57 +1000 Subject: [PATCH 005/148] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8013ba5..353aa69 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Mine _will_ support following features: * RSA (Encrypt, Decrypt, Sign and Verify) [[RFC-3447](https://tools.ietf.org/html/rfc3447)] * AES-CBC [[RFC-3602](https://tools.ietf.org/html/rfc3602)] - * ZLib (Depend upon libz, eventually implement [[RFC-1950](https://tools.ietf.org/html/rfc3602)] + * ZLib (Depend upon libz, eventually implement [RFC-1950](https://tools.ietf.org/html/rfc3602)) * Base-64 (Encode, Decode) * Hex (Encode, Decode) From 974de3d6648ba39e70910674d139a1d4b3b57184 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 20 Aug 2017 23:09:52 +1000 Subject: [PATCH 006/148] fake test for openssl --- CMakeLists.txt | 2 +- test/main.cc | 3 +++ test/rsa-test.h | 12 +++++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bf9b90..05a6639 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,6 +102,6 @@ add_executable(mine-unit-tests target_link_libraries(mine-unit-tests gtest gtest_main) # Extra linking for the project. -target_link_libraries(mine-unit-tests ${CRYPTOPP_LIBRARIES}) +target_link_libraries(mine-unit-tests ${CRYPTOPP_LIBRARIES} ripe) add_test(NAME mineUnitTests COMMAND mine-unit-tests) diff --git a/test/main.cc b/test/main.cc index 7cf6262..a3b1054 100644 --- a/test/main.cc +++ b/test/main.cc @@ -8,5 +8,8 @@ int main(int argc, char** argv) { el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput); el::Loggers::addFlag(el::LoggingFlag::ImmediateFlush); + // for temp basis + el::Loggers::reconfigureAllLoggers(el::Level::Global, el::ConfigurationType::Enabled, "false"); + return ::testing::UnitTest::GetInstance()->Run(); } diff --git a/test/rsa-test.h b/test/rsa-test.h index 7eecc8f..518ac77 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -4,6 +4,8 @@ #include "include/mine.h" #include "test.h" +#include + // numb, expected static TestData IsPrimeData = { TestCase(1, false), @@ -106,6 +108,14 @@ TEST(MineRSATest, InvModulo) } } +TEST(MineRSATest, FakeTest) +{ + auto item = RawKeyData.at(7); + Mine::KeyPair k(PARAM(0), PARAM(1), PARAM(3)); + std::cout << Mine::encrypt(k.publicKey(), std::string("Test message")) << std::endl; + std::cout << Mine::decrypt(k.privateKey(), std::string("68A7FE65FBD933522CDD321B0062DBA910AE5C1E73D46F7EB4A26773963963AE59F614D514E75773A8E6B67EACDC7C9F4172A94D58522CBB96FC79A836DB5343")); +} + TEST(MineRSATest, KeyAndEncryptionDecryption) { for (const auto& item : RawKeyData) { @@ -121,7 +131,7 @@ TEST(MineRSATest, KeyAndEncryptionDecryption) ASSERT_EQ(k.publicKey()->e(), k.e()); if (bits > 32) { - std::cout << "Key DER: " << std::endl << k.exportDER() << std::endl; + // std::cout << "Key DER: " << std::endl << k.exportDER() << std::endl; } for (const auto& item2 : RSAEncryptionData) { From e8bcfb0aed634e523cd22dfcc868de4d388baeab Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 21 Aug 2017 16:29:42 +1000 Subject: [PATCH 007/148] minor update --- include/mine.h | 85 ++++++++++++++++++++++++++++--------------------- test/rsa-test.h | 2 ++ 2 files changed, 50 insertions(+), 37 deletions(-) diff --git a/include/mine.h b/include/mine.h index 28efffb..c245db6 100644 --- a/include/mine.h +++ b/include/mine.h @@ -211,7 +211,7 @@ class Mine { static bool verify(const PublicKey* publicKey, const std::string& message, const std::string& signature) { - /* TODO: Add it to test + // TODO: Add it to test std::vector f = getByteArray(18537); BigInteger s = i2osp(f, 2); BigInteger os = os2ip(18537); // 105 @@ -226,7 +226,12 @@ class Mine { std::cout << s2 << std::endl; std::cout << os2 << std::endl; - return true;*/ + std::cout << changeBase(54735, 8) << std::endl; + + std::cout << std::hex << 16 << std::endl; + std::cout << std::oct << 72 << std::endl; + + return true; std::string readableSign = "0x" + signature; BigInteger sign(readableSign.c_str()); @@ -267,9 +272,28 @@ class Mine { private: + /// + /// \brief Octet string to integer + /// static BigInteger os2ip(const BigInteger& x) { - return x & 0xff; + return os2ip(getByteArray(x)); + } + + /// + /// Octet-string to integer + /// + template + static BigInteger os2ip(const std::vector& x) + { + const BigInteger b256 = 256; + + BigInteger result = 0; + std::size_t len = x.size(); + for (std::size_t i = len; i > 0; --i) { + result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); + } + return result; } /// @@ -293,6 +317,8 @@ class Mine { for (std::size_t i = len; i > 0; --i) { result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); } + // TODO: Fix this! + return result; } @@ -366,6 +392,7 @@ class Mine { byteArray[--n] = 0; + // todo: check if there are any more specs for randoms in standard srand(time(NULL)); int r = rand() % 100 + 1; while (n > 2) { @@ -375,6 +402,7 @@ class Mine { } byteArray[--n] = r; } + // first two bytes of padding are 0x2 (second) and 0x0 (first) byteArray[--n] = 2; byteArray[--n] = 0; return i2osp(byteArray, n); @@ -390,27 +418,27 @@ class Mine { { std::vector ba = getByteArray(m, n); std::size_t baLen = ba.size(); - int i = 0; - while (i < baLen && ba[i] == 0) { - // ignore first zeros - ++i; - } - if (baLen - 1 != n - 1 // reached end - || ba[i] != 2) { // next available char is not 2 as per padding standard + if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { throw std::runtime_error("Incorrect padding PKCS#1"); } - ++i; // we're all good so far, + int i = 2; // passed first two characters (0x0 and 0x2) test // lets check for the - // if we hit end while still we're still with zeros, it's a padding error + // if we hit end while still we're still with non-zeros, it's a padding error + // 0x0 (done) + // 0x2 (done) + // + // 0x0 while (ba[i] != 0) { if (++i >= baLen) { // already ended! throw std::runtime_error("Incorrect padding PKCS#1"); } } + // last zero ++i; + // now we should be at the first non-zero byte - // which is our first item, we concat all + // which is our first item, concat them as char | wchar_t std::basic_stringstream ss; for (; i < baLen; ++i) { @@ -467,11 +495,12 @@ class Mine { /// /// \brief Power of numb i.e, b ^ e /// - static BigInteger power(BigInteger b, BigInteger e) + template + static T power(T b, T e) { - BigInteger result = 1; + T result = 1; while (e > 0) { - if (e.IsOdd()) { + if (e % 2 == 1) { // we decrement exponent to make it even e--; // store this multiplication directly to the @@ -547,12 +576,12 @@ class Mine { } /// - /// \brief Decimal to specific base + /// \brief Specific base to specified base /// \param n Number - /// \param b Base - default is 8 (octet) + /// \param b Target base (default: 16 - Hex) /// template - static T d2o(T n, T b = 8) + static T changeBase(T n, T b = 16) { T r, i = 1, o = 0; while (n != 0) { @@ -564,24 +593,6 @@ class Mine { return o; } - /// - /// \brief Specific base to decimal - /// \param n Number - /// \param b Base - default is from octet (base 8) - /// - template - static T o2d(T n, T b = 8) - { - T r, i = 0, o = 0; - while (n != 0) { - r = n % 10; - n /= 10; - o += r * power(b, i); - ++i; - } - return o; - } - /// /// \brief Big integer adds suffix at the end so we use this function /// to remove it diff --git a/test/rsa-test.h b/test/rsa-test.h index 518ac77..c6bd07e 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -62,6 +62,8 @@ static TestData RSAEncryptionData = { TestCase(L"G'day"), TestCase(L"Hi"), TestCase(L"Hello竜"), + TestCase(L"大家好"), + TestCase(L"کیا میں آپکی مدد کر سکتاہوں"), }; // msg static TestData RSAEncryptionStringData = { From 80e099f008f4f3395f8897c4315047c4b99612fe Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 21 Aug 2017 16:53:47 +1000 Subject: [PATCH 008/148] add ripe --- .travis.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.travis.yml b/.travis.yml index d6ba339..aee0cdd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,6 +17,16 @@ before_install: - cmake . - make - sudo make install +## Ripe + - RIPE_VERSION=4.0.0 + - RIPE_VERSION_URL="$RIPE_VERSION" + - wget -O ripe-code.zip https://github.com/muflihun/ripe/archive/$RIPE_VERSION_URL.zip + - unzip ripe-code.zip + - cd ripe-$RIPE_VERSION + - cmake . + - make + - ls -l + - sudo make install # GTest - wget -O gtest.tar.gz https://github.com/google/googletest/archive/release-1.7.0.tar.gz - tar xf gtest.tar.gz From b4c682c0c4b3c440ec8bbf0e2afd2206dba6e63a Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 21 Aug 2017 16:59:33 +1000 Subject: [PATCH 009/148] consts and remove prime check --- include/mine.h | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/include/mine.h b/include/mine.h index c245db6..7f915ee 100644 --- a/include/mine.h +++ b/include/mine.h @@ -47,21 +47,18 @@ class Mine { public: static const unsigned int DEFAULT_PUBLIC_EXPONENT = 65537; - RawKey(BigInteger p, BigInteger q, int e = DEFAULT_PUBLIC_EXPONENT, bool skipPrimeCheck = true) : + RawKey(const BigInteger& p, const BigInteger& q, int e = DEFAULT_PUBLIC_EXPONENT) : m_p(p), m_q(q), m_e(e) { - if (!skipPrimeCheck && (!isPrime(p) || !isPrime(q))) { - throw std::invalid_argument("p and q must be prime numbers unique to each other"); - } if (p == q || p == 0 || q == 0) { throw std::invalid_argument("p and q must be prime numbers unique to each other"); } - BigInteger pMinus1 = m_p - 1; - BigInteger qMinus1 = m_q - 1; - BigInteger phi = pMinus1 * qMinus1; + const BigInteger pMinus1 = m_p - 1; + const BigInteger qMinus1 = m_q - 1; + const BigInteger phi = pMinus1 * qMinus1; if (gcd(m_e, phi) != 1) { throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); @@ -129,7 +126,7 @@ class Mine { class KeyPair : public RawKey { public: - KeyPair(BigInteger p, BigInteger q, unsigned int exp = DEFAULT_PUBLIC_EXPONENT) : + KeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = DEFAULT_PUBLIC_EXPONENT) : RawKey(p, q, exp) { m_publicKey = PublicKey(n(), e()); } From d80e739d135f19d446b0b9986cdf7e844518a88b Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 21 Aug 2017 17:07:55 +1000 Subject: [PATCH 010/148] update --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index aee0cdd..a56ee56 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,7 +19,7 @@ before_install: - sudo make install ## Ripe - RIPE_VERSION=4.0.0 - - RIPE_VERSION_URL="$RIPE_VERSION" + - RIPE_VERSION_URL="v$RIPE_VERSION" - wget -O ripe-code.zip https://github.com/muflihun/ripe/archive/$RIPE_VERSION_URL.zip - unzip ripe-code.zip - cd ripe-$RIPE_VERSION From 2c254884cd17e115e486d251b45d42c711ed1161 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 22 Aug 2017 15:20:33 +1000 Subject: [PATCH 011/148] rsa --- src/aes.h | 27 +++++++++++ include/mine.h => src/rsa.h | 28 ++++++----- test/aes-test.h | 11 +++++ test/main.cc | 1 + test/rsa-test.h | 94 ++++++++++++++++++------------------- test/test.h | 2 + 6 files changed, 105 insertions(+), 58 deletions(-) create mode 100644 src/aes.h rename include/mine.h => src/rsa.h (98%) create mode 100644 test/aes-test.h diff --git a/src/aes.h b/src/aes.h new file mode 100644 index 0000000..4c707a3 --- /dev/null +++ b/src/aes.h @@ -0,0 +1,27 @@ +// +// aes.h +// Part of Mine library +// +// Copyright 2017 Muflihun Labs +// +// https://github.com/muflihun/mine +// + +#ifndef AES_H +#define AES_H + +#include + +namespace mine { + +/// +/// \brief Provides AES crypto functionalities +/// +class AES { +public: + +}; +} + + +#endif // AES_H diff --git a/include/mine.h b/src/rsa.h similarity index 98% rename from include/mine.h rename to src/rsa.h index 7f915ee..751158a 100644 --- a/include/mine.h +++ b/src/rsa.h @@ -1,11 +1,14 @@ // -// mine.h +// rsa.h +// Part of Mine library +// +// Copyright 2017 Muflihun Labs // // https://github.com/muflihun/mine // -#ifndef MINE_H -#define MINE_H +#ifndef RSA_H +#define RSA_H #include #include @@ -15,12 +18,13 @@ #include #include +namespace mine { + /// -/// PLEASE NOTE, DO NOT USE UNTIL THE FIRST RELEASE +/// \brief Provides RSA crypto functionalities /// -class Mine { +class RSA { public: - typedef CryptoPP::Integer BigInteger; // temp class PublicKey { @@ -604,10 +608,12 @@ class Mine { } // for tests - friend class MineRSATest_IsPrime_Test; - friend class MineRSATest_FindGCD_Test; - friend class MineRSATest_InvModulo_Test; - friend class MineRSATest_PowerMod_Test; + friend class RSATest_IsPrime_Test; + friend class RSATest_FindGCD_Test; + friend class RSATest_InvModulo_Test; + friend class RSATest_PowerMod_Test; }; +} + -#endif // MINE_H +#endif // RSA_H diff --git a/test/aes-test.h b/test/aes-test.h new file mode 100644 index 0000000..d550651 --- /dev/null +++ b/test/aes-test.h @@ -0,0 +1,11 @@ +#ifndef AES_TEST_H +#define AES_TEST_H + +#include "src/aes.h" +#include "test.h" + +namespace mine { + +} + +#endif // AES_TEST_H diff --git a/test/main.cc b/test/main.cc index a3b1054..365733c 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,5 +1,6 @@ #include "test.h" #include "rsa-test.h" +#include "aes-test.h" INITIALIZE_EASYLOGGINGPP diff --git a/test/rsa-test.h b/test/rsa-test.h index c6bd07e..a706a49 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -1,13 +1,12 @@ -#ifndef MINE_TEST_H -#define MINE_TEST_H +#ifndef RSA_TEST_H +#define RSA_TEST_H -#include "include/mine.h" +#include "src/rsa.h" #include "test.h" -#include - +namespace mine { // numb, expected -static TestData IsPrimeData = { +static TestData IsPrimeData = { TestCase(1, false), TestCase(2, true), TestCase(44, false), @@ -26,7 +25,7 @@ static TestData InvModuloData = { }; // b, e, m, exp -static TestData PowerModData = { +static TestData PowerModData = { TestCase(5, 3, 1, 0), TestCase(5, 3, 19, 11), TestCase(3, 11, 4, 3), @@ -44,17 +43,17 @@ static TestData GCDData = { }; // p, q, d, e -static TestData RawKeyData = { +static TestData RawKeyData = { TestCase(173, 149, 16971, 3), - TestCase(7, 11, 53, Mine::KeyPair::DEFAULT_PUBLIC_EXPONENT), + TestCase(7, 11, 53, RSA::KeyPair::DEFAULT_PUBLIC_EXPONENT), TestCase(53, 59, 2011, 3), - TestCase(3, 11, 13, Mine::KeyPair::DEFAULT_PUBLIC_EXPONENT), - TestCase(11, 3, 13, Mine::KeyPair::DEFAULT_PUBLIC_EXPONENT), + TestCase(3, 11, 13, RSA::KeyPair::DEFAULT_PUBLIC_EXPONENT), + TestCase(11, 3, 13, RSA::KeyPair::DEFAULT_PUBLIC_EXPONENT), TestCase(11, 17, 107, 3), TestCase(60779, 53003, 1986380529, 65537), - TestCase(Mine::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), Mine::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), Mine::BigInteger("6582637129463060155009365989980507690389844347218288265898882119918384609608605061080359962338234258873604135082233881579524605068749537845926158658339195"), 3), // << - this is what we tested based upon - TestCase(Mine::BigInteger("108806323825932706977307191097259117033353572146991115579334232319532442798209"), Mine::BigInteger("75778358732466892022501809496541864532894038434008219546470758430052996329071"), Mine::BigInteger("5496776426161658798940200169671739270887755348701962910881820208997794909110934102331035956003239535747096593580882262107967594097892650413676521849537707"), 3), - TestCase(Mine::BigInteger("176360517760307645469197766454483974235511085138581196179561347493397045582678676376582697316359235034160209749898412011153924577295946180206410049279151818330310786286636241193330444606031071350600285897477381895748147316188740513022461102919987041280831098968434434553879045380055670995086500659087065302403"), Mine::BigInteger("169163723758010117173450277772073715921803592964927638245731997826080549397171438893995388301374973303599758650257957793677462633788535432641753359275162340138639711102283198388259082836510170614304484108899685152902783320622394241970680405511348370667697428176827008839392860840538166537806520720483413747299"), Mine::BigInteger("19889201272149546443463155392252159315174210881947747082976069645146016944350039871470895772510979533532867685298201602992918775321216324576337623180033432704404378220468328792827286239010112541240591233928213472506415790478729110344923198900414497739281740852945910987895868475178794593401701658716065890906852003893420692929407600046549070094684609746581564079903528672418432707811264082243503362255422665241970781850081871999244191153644696361248245946591425338244340405196223004592171521190997670962141503496215757774355742872186005655701283224796723544068646999720522489543729822936326934344869201943856253606531"), 3), + TestCase(RSA::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), RSA::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), RSA::BigInteger("6582637129463060155009365989980507690389844347218288265898882119918384609608605061080359962338234258873604135082233881579524605068749537845926158658339195"), 3), // << - this is what we tested based upon + TestCase(RSA::BigInteger("108806323825932706977307191097259117033353572146991115579334232319532442798209"), RSA::BigInteger("75778358732466892022501809496541864532894038434008219546470758430052996329071"), RSA::BigInteger("5496776426161658798940200169671739270887755348701962910881820208997794909110934102331035956003239535747096593580882262107967594097892650413676521849537707"), 3), + TestCase(RSA::BigInteger("176360517760307645469197766454483974235511085138581196179561347493397045582678676376582697316359235034160209749898412011153924577295946180206410049279151818330310786286636241193330444606031071350600285897477381895748147316188740513022461102919987041280831098968434434553879045380055670995086500659087065302403"), RSA::BigInteger("169163723758010117173450277772073715921803592964927638245731997826080549397171438893995388301374973303599758650257957793677462633788535432641753359275162340138639711102283198388259082836510170614304484108899685152902783320622394241970680405511348370667697428176827008839392860840538166537806520720483413747299"), RSA::BigInteger("19889201272149546443463155392252159315174210881947747082976069645146016944350039871470895772510979533532867685298201602992918775321216324576337623180033432704404378220468328792827286239010112541240591233928213472506415790478729110344923198900414497739281740852945910987895868475178794593401701658716065890906852003893420692929407600046549070094684609746581564079903528672418432707811264082243503362255422665241970781850081871999244191153644696361248245946591425338244340405196223004592171521190997670962141503496215757774355742872186005655701283224796723544068646999720522489543729822936326934344869201943856253606531"), 3), }; // msg @@ -71,60 +70,60 @@ static TestData RSAEncryptionStringData = { }; // p, q, e, cipher, msg -static TestData RSADecryptionData = { - TestCase(Mine::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), Mine::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "55a5f32084cdbbd3edcba573317f99678a1b85b6c455fa86476d697900ce5fd95ec599a16690d5e7c2196608477ac1006e86c74cbd25b7e4681e026774381e63", L"Apple"), +static TestData RSADecryptionData = { + TestCase(RSA::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), RSA::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "55a5f32084cdbbd3edcba573317f99678a1b85b6c455fa86476d697900ce5fd95ec599a16690d5e7c2196608477ac1006e86c74cbd25b7e4681e026774381e63", L"Apple"), }; // p, q, e, signature, text -static TestData RSASignatureData = { - TestCase(Mine::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), Mine::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "01400ccd971dad2744c37baf7f5cf13a5590a675c90354f2002d4c6a6a7ef3d1377986e1d8f0b69676e243fae8cf6c6bbdc7f18deb0e0418fe6452c4afb1e4b5", "test"), +static TestData RSASignatureData = { + TestCase(RSA::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), RSA::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "01400ccd971dad2744c37baf7f5cf13a5590a675c90354f2002d4c6a6a7ef3d1377986e1d8f0b69676e243fae8cf6c6bbdc7f18deb0e0418fe6452c4afb1e4b5", "test"), }; -TEST(MineRSATest, FindGCD) +TEST(RSATest, FindGCD) { for (const auto& item : GCDData) { LOG(INFO) << "Finding GCD for " << PARAM(0) << " and " << PARAM(1); - ASSERT_EQ(Mine::gcd(PARAM(0), PARAM(1)), PARAM(2)); + ASSERT_EQ(RSA::gcd(PARAM(0), PARAM(1)), PARAM(2)); } } -TEST(MineRSATest, IsPrime) +TEST(RSATest, IsPrime) { for (const auto& item : IsPrimeData) { // LOG(INFO) << "Testing " << PARAM(0) << " == prime: to be " << std::boolalpha << PARAM(1); - ASSERT_EQ(Mine::isPrime(PARAM(0)), PARAM(1)); + ASSERT_EQ(RSA::isPrime(PARAM(0)), PARAM(1)); } } -TEST(MineRSATest, PowerMod) +TEST(RSATest, PowerMod) { for (const auto& item : PowerModData) { - ASSERT_EQ(Mine::powerMod(PARAM(0), PARAM(1), PARAM(2)), PARAM(3)); + ASSERT_EQ(RSA::powerMod(PARAM(0), PARAM(1), PARAM(2)), PARAM(3)); } } -TEST(MineRSATest, InvModulo) +TEST(RSATest, InvModulo) { for (const auto& item : InvModuloData) { - ASSERT_EQ(Mine::modInverse(PARAM(0), PARAM(1)), PARAM(2)); + ASSERT_EQ(RSA::modInverse(PARAM(0), PARAM(1)), PARAM(2)); } } -TEST(MineRSATest, FakeTest) +TEST(RSATest, FakeTest) { auto item = RawKeyData.at(7); - Mine::KeyPair k(PARAM(0), PARAM(1), PARAM(3)); - std::cout << Mine::encrypt(k.publicKey(), std::string("Test message")) << std::endl; - std::cout << Mine::decrypt(k.privateKey(), std::string("68A7FE65FBD933522CDD321B0062DBA910AE5C1E73D46F7EB4A26773963963AE59F614D514E75773A8E6B67EACDC7C9F4172A94D58522CBB96FC79A836DB5343")); + RSA::KeyPair k(PARAM(0), PARAM(1), PARAM(3)); + std::cout << RSA::encrypt(k.publicKey(), std::string("Test message")) << std::endl; + std::cout << RSA::decrypt(k.privateKey(), std::string("68A7FE65FBD933522CDD321B0062DBA910AE5C1E73D46F7EB4A26773963963AE59F614D514E75773A8E6B67EACDC7C9F4172A94D58522CBB96FC79A836DB5343")); } -TEST(MineRSATest, KeyAndEncryptionDecryption) +TEST(RSATest, KeyAndEncryptionDecryption) { for (const auto& item : RawKeyData) { int bits = PARAM(0).BitCount() + PARAM(1).BitCount(); LOG(INFO) << "Generating key " << bits << "-bit..."; - Mine::KeyPair k(PARAM(0), PARAM(1), PARAM(3)); + RSA::KeyPair k(PARAM(0), PARAM(1), PARAM(3)); ASSERT_EQ(k.p(), PARAM(0)); ASSERT_EQ(k.q(), PARAM(1)); ASSERT_EQ(k.d(), PARAM(2)); @@ -140,11 +139,11 @@ TEST(MineRSATest, KeyAndEncryptionDecryption) std::wstring msg = std::get<0>(item2); LOG(INFO) << "Testing: " << msg; if (bits <= 32) { - EXPECT_THROW(Mine::encrypt(k.publicKey(), msg), std::runtime_error); + EXPECT_THROW(RSA::encrypt(k.publicKey(), msg), std::runtime_error); } else { - std::string encr = Mine::encrypt(k.publicKey(), msg); + std::string encr = RSA::encrypt(k.publicKey(), msg); LOG(INFO) << "Encr: " << encr; - std::wstring decr = Mine::decrypt(k.privateKey(), encr); + std::wstring decr = RSA::decrypt(k.privateKey(), encr); ASSERT_STREQ(decr.c_str(), msg.c_str()); } } @@ -153,54 +152,55 @@ TEST(MineRSATest, KeyAndEncryptionDecryption) std::string msg = std::get<0>(item2); LOG(INFO) << "Testing: " << msg; if (bits <= 32) { - EXPECT_THROW(Mine::encrypt(k.publicKey(), msg), std::runtime_error); + EXPECT_THROW(RSA::encrypt(k.publicKey(), msg), std::runtime_error); } else { - std::string encr = Mine::encrypt(k.publicKey(), msg); + std::string encr = RSA::encrypt(k.publicKey(), msg); LOG(INFO) << "Encr: " << encr; - std::string decr = Mine::decrypt(k.privateKey(), encr); + std::string decr = RSA::decrypt(k.privateKey(), encr); ASSERT_STREQ(decr.c_str(), msg.c_str()); } } } } -TEST(MineRSATest, Decryption) +TEST(RSATest, Decryption) { for (const auto& item : RSADecryptionData) { int bits = PARAM(0).BitCount() + PARAM(1).BitCount(); LOG(INFO) << "Generating key " << bits << "-bit..."; - Mine::KeyPair k(PARAM(0), PARAM(1), PARAM(2)); + RSA::KeyPair k(PARAM(0), PARAM(1), PARAM(2)); std::string cipher = PARAM(3); std::wstring expected = PARAM(4); LOG(INFO) << "Testing: " << cipher; if (bits <= 32) { - EXPECT_THROW(Mine::decrypt(k.privateKey(), cipher), std::runtime_error); + EXPECT_THROW(RSA::decrypt(k.privateKey(), cipher), std::runtime_error); } else { - std::wstring decr = Mine::decrypt(k.privateKey(), cipher); + std::wstring decr = RSA::decrypt(k.privateKey(), cipher); ASSERT_STREQ(decr.c_str(), expected.c_str()); } } } -TEST(MineRSATest, Signature) +TEST(RSATest, Signature) { for (const auto& item : RSASignatureData) { int bits = PARAM(0).BitCount() + PARAM(1).BitCount(); LOG(INFO) << "Generating key " << bits << "-bit..."; - Mine::KeyPair k(PARAM(0), PARAM(1), PARAM(2)); + RSA::KeyPair k(PARAM(0), PARAM(1), PARAM(2)); std::string sign = PARAM(3); std::string text = PARAM(4); LOG(INFO) << "Testing: " << text; if (bits <= 32) { - EXPECT_THROW(Mine::verify(k.publicKey(), text, sign), std::runtime_error); + EXPECT_THROW(RSA::verify(k.publicKey(), text, sign), std::runtime_error); } else { - ASSERT_TRUE(Mine::verify(k.publicKey(),text, sign)); + ASSERT_TRUE(RSA::verify(k.publicKey(),text, sign)); } } } +} -#endif // MINE_TEST_H +#endif // RSA_TEST_H diff --git a/test/test.h b/test/test.h index 0b42db8..570ae96 100644 --- a/test/test.h +++ b/test/test.h @@ -7,6 +7,8 @@ #include #include +#include + template using TestData = const std::vector>; From 5c6d0afa9814d51f47da635589b2a2c4f072c471 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 22 Aug 2017 15:57:10 +1000 Subject: [PATCH 012/148] contents generated using build.php script --- build.php | 65 +++++ include/mine.h | 626 +++++++++++++++++++++++++++++++++++++++++++++++++ src/aes.h | 2 +- src/rsa.h | 2 +- 4 files changed, 693 insertions(+), 2 deletions(-) create mode 100644 build.php create mode 100644 include/mine.h diff --git a/build.php b/build.php new file mode 100644 index 0000000..c2a7b84 --- /dev/null +++ b/build.php @@ -0,0 +1,65 @@ + +#include include +#include include +#include include +#include include +#include include +#include include + +namespace mine { + +/// +/// \brief Provides RSA crypto functionalities +/// +class RSA { +public: + typedef CryptoPP::Integer BigInteger; // temp + + class PublicKey { + public: + PublicKey() = default; + + PublicKey(BigInteger n, int e) : + m_n(n), + m_e(e) + { + } + + virtual ~PublicKey() = default; + + inline BigInteger n() const { return m_n; } + inline int e() const { return m_e; } + + private: + BigInteger m_n; + int m_e; + }; + + class RawKey { + public: + static const unsigned int DEFAULT_PUBLIC_EXPONENT = 65537; + + RawKey(const BigInteger& p, const BigInteger& q, int e = DEFAULT_PUBLIC_EXPONENT) : + m_p(p), + m_q(q), + m_e(e) + { + if (p == q || p == 0 || q == 0) { + throw std::invalid_argument("p and q must be prime numbers unique to each other"); + } + + const BigInteger pMinus1 = m_p - 1; + const BigInteger qMinus1 = m_q - 1; + const BigInteger phi = pMinus1 * qMinus1; + + if (gcd(m_e, phi) != 1) { + throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); + } + m_n = m_p * m_q; + m_coeff = modInverse(m_q, m_p); + + m_d = modInverse(m_e, phi); + + // note: + // https://www.ipa.go.jp/security/rfc/RFC3447EN.html#2 says to use m_e + // openssl says to use m_d + m_dp = BigInteger(m_d) % pMinus1; + m_dq = BigInteger(m_d) % qMinus1; + } + + virtual ~RawKey() = default; + + inline BigInteger p() const { return m_p; } + inline BigInteger q() const { return m_q; } + inline BigInteger coeff() const { return m_coeff; } + inline BigInteger n() const { return m_n; } + inline int e() const { return m_e; } + inline BigInteger d() const { return m_d; } + inline BigInteger dp() const { return m_dq; } + inline BigInteger dq() const { return m_dp; } + + friend std::ostream& operator<<(std::ostream& ss, const RawKey& k) + { + ss << "modulus: " << k.m_n << "\npublicExponent: " << k.m_e << "\nprivateExponent: " << k.m_d + << "\nprime1: " << k.m_p << "\nprime2: " << k.m_q << "\nexponent1: " << k.m_dp << "\nexponent2: " + << k.m_dq << "\ncoefficient: " << k.m_coeff; + return ss; + } + + std::string exportDER() const + { + std::stringstream ss; + ss << "asn1=SEQUENCE:rsa_key\n\n"; + ss << "[rsa_key]\n"; + ss << "version=INTEGER:0\n"; + ss << "modulus=INTEGER:" << bigIntegerToString(m_n) << "\n"; + ss << "pubExp=INTEGER:" << m_e << "\n"; + ss << "privExp=INTEGER:" << bigIntegerToString(m_d) << "\n"; + ss << "p=INTEGER:" << bigIntegerToString(m_p) << "\n"; + ss << "q=INTEGER:" << bigIntegerToString(m_q) << "\n"; + ss << "e1=INTEGER:" << bigIntegerToString(m_dp) << "\n"; + ss << "e2=INTEGER:" << bigIntegerToString(m_dq) << "\n"; + ss << "coeff=INTEGER:" << bigIntegerToString(m_coeff); + return ss.str(); + } + + private: + BigInteger m_p; + BigInteger m_q; + int m_e; + BigInteger m_coeff; + BigInteger m_n; + BigInteger m_d; + BigInteger m_dp; + BigInteger m_dq; + }; + + typedef RawKey PrivateKey; + + class KeyPair : public RawKey { + public: + KeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = DEFAULT_PUBLIC_EXPONENT) : + RawKey(p, q, exp) { + m_publicKey = PublicKey(n(), e()); + } + + inline const PublicKey* publicKey() const { return &m_publicKey; } + inline const PrivateKey* privateKey() const { return this; } + + private: + PublicKey m_publicKey; + }; + + /// + /// \brief Generic RSA encryption. T can of std::string or std::wstring + /// or custom similar type + /// + /// \return hex of final octet string + /// + template + static std::string encrypt(const PublicKey* publicKey, const T& m) + { + BigInteger paddedMsg = pkcs1pad2(m, (publicKey->n().BitCount() + 7) >> 3); + // TODO: It can be made better + std::stringstream ss; + ss << std::hex << powerMod(paddedMsg, publicKey->e(), publicKey->n()); + std::string h(ss.str()); + h.erase(h.end() - 1); + return ((h.size() & 1) == 0) ? h : ("0" + h); + } + + /// + /// \brief Encrypts wstring msg using public key. + /// + /// \return hex of cipher. Padded using PKCS#1 padding scheme + /// + static std::string encrypt(const PublicKey* publicKey, + const std::wstring& message) + { + return encrypt(publicKey, message); + } + + /// + /// \brief Encrypts string msg using public key + /// + /// \return hex of cipher. Padded using PKCS#1 padding scheme + /// + static std::string encrypt(const PublicKey* publicKey, + const std::string& message) + { + return encrypt(publicKey, message); + } + + /// + /// \brief Decrypts RSA hex message m using private key + /// \param cipher Cipher in hex format (should not start with 0x) + /// \return Plain text, return type depends on TResult + /// + template + static TResult decrypt(const PrivateKey* privateKey, const std::string& cipher) + { + // TODO: Add checks https://tools.ietf.org/html/rfc3447#section-7.2.2 + + std::string readableMsg = "0x" + cipher; // 0x helps BigInteger read m as 16-bit integer + BigInteger msg(readableMsg.c_str()); + // https://tools.ietf.org/html/rfc3447#section-4.1 + int xlen = (privateKey->n().BitCount() + 7) >> 3; + if (msg >= power(BigInteger(256), BigInteger(xlen))) { + throw std::runtime_error("Integer too large"); + } + BigInteger decr = powerMod(msg, privateKey->d(), privateKey->n()); + return pkcs1unpad2(decr, xlen); + } + + /// + /// \brief Verifies signature for text using RSA public key + /// \param message An octet string + /// \param signature Signature in hex + /// \see https://tools.ietf.org/html/rfc3447#section-8.1.2 + /// + static bool verify(const PublicKey* publicKey, const std::string& message, + const std::string& signature) + { + // TODO: Add it to test + std::vector f = getByteArray(18537); + BigInteger s = i2osp(f, 2); + BigInteger os = os2ip(18537); // 105 + + std::cout << s << std::endl; + std::cout << os << std::endl; + + std::vector f2 = getByteArray(1214841185); + BigInteger s2 = i2osp(f2, 4); + BigInteger os2 = os2ip(1214841185); // 97 + + std::cout << s2 << std::endl; + std::cout << os2 << std::endl; + + std::cout << changeBase(54735, 8) << std::endl; + + std::cout << std::hex << 16 << std::endl; + std::cout << std::oct << 72 << std::endl; + + return true; + + std::string readableSign = "0x" + signature; + BigInteger sign(readableSign.c_str()); + try { + BigInteger vp = createVerificationPrimitive(publicKey, sign); + // I2OSP + + std::cout << vp << std::endl; + + const int xlen = (publicKey->n().BitCount() + 7) >> 3; + + if (xlen < vp + 11) { + // throw std::runtime_error("Integer too large"); // Needed??! Where in RFC? + } + + BigInteger em = i2osp(vp, xlen); + + // manually test + std::string encr = encrypt(publicKey, message); + + + // todo: add check for following (as per https://tools.ietf.org/html/rfc3447#section-8.1.2) + // Note that emLen will be one less than k if modBits - 1 is + // divisible by 8 and equal to k otherwise. If I2OSP outputs + // "integer too large," output "invalid signature" and stop. + + // EMSA-PSS_VERIFY - https://tools.ietf.org/html/rfc3447#section-9.1.2 + + + + std::cout << em << " " << encr << std::endl; + return true; + } catch (std::exception&) { + } + + return false; + } + +private: + + /// + /// \brief Octet string to integer + /// + static BigInteger os2ip(const BigInteger& x) + { + return os2ip(getByteArray(x)); + } + + /// + /// Octet-string to integer + /// + template + static BigInteger os2ip(const std::vector& x) + { + const BigInteger b256 = 256; + + BigInteger result = 0; + std::size_t len = x.size(); + for (std::size_t i = len; i > 0; --i) { + result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); + } + return result; + } + + /// + /// \brief Integer to octet-string + /// + static BigInteger i2osp(const BigInteger& x, int xlen) + { + return i2osp(getByteArray(x), xlen); + } + + /// + /// \brief Integer to octet-string + /// + template + static BigInteger i2osp(const std::vector& x, int xlen) + { + const BigInteger b256 = 256; + + BigInteger result = 0; + std::size_t len = x.size(); + for (std::size_t i = len; i > 0; --i) { + result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); + } + // TODO: Fix this! + + return result; + } + + /// + /// \brief Get byte array from big integer + /// + template + static std::vector getByteArray(BigInteger x, int xlen = -1) + { + const BigInteger b256 = 256; + xlen = xlen == -1 ? x.ByteCount() : xlen; + + std::vector ba(xlen); + BigInteger r; + BigInteger q; + + for (int i = 1; i <= xlen; ++i) { + x.Divide(r, q, x, power(b256, BigInteger(xlen - i))); + ba[i - 1] = static_cast(q.ConvertToLong()); // todo: Check! + x = r; + } + return ba; + } + + /// + /// \brief Creates RSA VP for verification + /// \param signature signature representative, an integer between 0 and n - 1 + /// \return message representative, an integer between 0 and n - 1 + /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 + /// + static BigInteger createVerificationPrimitive(const PublicKey* publicKey, + const BigInteger& signature) + { + if (signature < 0 || signature > publicKey->n() - 1) { + throw std::runtime_error("signature representative out of range"); + } + return powerMod(signature, publicKey->e(), publicKey->n()); + } + + /// + /// \brief PKCS #1 padding + /// \see https://tools.ietf.org/html/rfc3447#page-23 + /// \return corresponding nonnegative integer + /// + template + static BigInteger pkcs1pad2(const T& s, int n) { + if (n < s.size() + 11) { + throw std::runtime_error("Message too long"); + } + std::vector byteArray(n); + long long i = s.size() - 1; + while(i >= 0 && n > 0) { + int c = static_cast(s.at(i--)); + if (c < 128) { + // utf + byteArray[--n] = c; + } else if ((c > 127) && (c < 2048)) { + // 16-bit + byteArray[--n] = (c & 63) | 128; + byteArray[--n] = (c >> 6) | 192; + } else { + // 24-bit + byteArray[--n] = (c & 63) | 128; + byteArray[--n] = ((c >> 6) & 63) | 128; + byteArray[--n] = (c >> 12) | 224; + } + } + + // now padding i.e, 0x00 || 0x02 || PS || 0x00 + // see point #2 on https://tools.ietf.org/html/rfc3447#section-7.2.1 => EME-PKCS1-v1_5 encoding + + byteArray[--n] = 0; + + // todo: check if there are any more specs for randoms in standard + srand(time(NULL)); + int r = rand() % 100 + 1; + while (n > 2) { + r = 0; + while (r == 0) { + r = rand() % 100 + 1; + } + byteArray[--n] = r; + } + // first two bytes of padding are 0x2 (second) and 0x0 (first) + byteArray[--n] = 2; + byteArray[--n] = 0; + return i2osp(byteArray, n); + } + + /// + /// \brief PKCS #1 unpadding + /// \see https://tools.ietf.org/html/rfc3447#section-4.1 + /// \return corresponding octet string of length n + /// + template + static T pkcs1unpad2(const BigInteger& m, unsigned long n) + { + std::vector ba = getByteArray(m, n); + std::size_t baLen = ba.size(); + if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { + throw std::runtime_error("Incorrect padding PKCS#1"); + } + int i = 2; // passed first two characters (0x0 and 0x2) test + // lets check for the + + // if we hit end while still we're still with non-zeros, it's a padding error + // 0x0 (done) + // 0x2 (done) + // + // 0x0 + while (ba[i] != 0) { + if (++i >= baLen) { // already ended! + throw std::runtime_error("Incorrect padding PKCS#1"); + } + } + // last zero + ++i; + + // now we should be at the first non-zero byte + // which is our first item, concat them as char | wchar_t + + std::basic_stringstream ss; + for (; i < baLen; ++i) { + // reference: http://en.cppreference.com/w/cpp/language/types -> range of values + int c = ba[i] & 0xFF; + if (c < 128) { + // utf-8 + ss << static_cast(c); + } else if ((c > 191) && (c < 224)) { // 16-bit char + ss << static_cast(((c & 31) << 6) | (ba[i+1] & 63)); + ++i; + } else { // 24-bit char + ss << static_cast(((c & 15) << 12) | ((ba[i+1] & 63) << 6) | (ba[i+2] & 63)); + i += 2; + } + } + return ss.str(); + } + + /// + /// \brief Fast GCD + /// \see https://en.wikipedia.org/wiki/Euclidean_algorithm#Extended_Euclidean_algorithm + /// + static BigInteger gcd(BigInteger a, BigInteger b) + { + BigInteger c; + while (a != 0) { + c = a; + a = b % a; + b = c; + } + return b; + } + + /// + /// \brief Simple (base ^ e) mod m implementation + /// \param b Base + /// \param e Exponent + /// \param m Mod + /// + static BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) + { + BigInteger res = 1; + while (e > 0) { + if (e % 2 != 0) { + res = (b * res) % m; + } + b = (b * b) % m; + e /= 2; + } + return res; + } + + /// + /// \brief Power of numb i.e, b ^ e + /// + template + static T power(T b, T e) + { + T result = 1; + while (e > 0) { + if (e % 2 == 1) { + // we decrement exponent to make it even + e--; + // store this multiplication directly to the + // result + result *= b; + // we modify this alg to ignore the next multiplication + // if we have already reached 0 (for speed) + // here are details and what we changed and how it all works + // + // Let's say we have case of 2 ^ 4 [expected answer = 16] + // 2 ^ 4 -- b = 4, e = 2 [result = 1] + // 2 ^ 2 -- b = 16, e = 1 [result = 1] + // 2 ^ 1 -- e = 0 [result = 1 * 16] + // + // here is what we changed here + // now we have result set and we have e set to zero + // doing another b ^= b means b = 16 * 16 = 256 (in our case) + // which is useless so we end here + if (e == 0) { + break; + } + } + e /= 2; + b *= b; + } + return result; + } + + static BigInteger modInverse(BigInteger a, BigInteger b) + { + BigInteger b0 = b, t, q; + BigInteger x0 = 0, x1 = 1; + if (b == 1) { + return 1; + } + while (a > 1) { + q = a / b; + t = b; + b = a % b; + a = t; + t = x0; + x0 = x1 - q * x0; + x1 = t; + } + if (x1 < 0) { + x1 += b0; + } + return x1; + } + + /// + /// \brief Checks whether n is prime or not + /// This is fast, see https://en.wikipedia.org/wiki/Primality_test#Pseudocode + /// for details + /// + static bool isPrime(BigInteger n) + { + if (n <= 1) { + return false; + } + if (n <= 3) { + return true; + } + if (n % 2 == 0 || n % 3 == 0) { + return false; + } + for (BigInteger i = 5; i * i <= n; i += 6) { + if (n % i == 0 || n % (i + 2) == 0) { + return false; + } + } + return true; + } + + /// + /// \brief Specific base to specified base + /// \param n Number + /// \param b Target base (default: 16 - Hex) + /// + template + static T changeBase(T n, T b = 16) + { + T r, i = 1, o = 0; + while (n != 0) { + r = n % b; + n /= b; + o += r * i; + i *= 10; + } + return o; + } + + /// + /// \brief Big integer adds suffix at the end so we use this function + /// to remove it + /// + static std::string bigIntegerToString(const BigInteger& b) + { + std::stringstream ss; + ss << b; + std::string sss(ss.str()); + sss.erase(sss.end() - 1); + return sss; + } + + // for tests + friend class RSATest_IsPrime_Test; + friend class RSATest_FindGCD_Test; + friend class RSATest_InvModulo_Test; + friend class RSATest_PowerMod_Test; +}; + +/// +/// \brief Provides AES crypto functionalities +/// +class AES { +public: + +}; + +} // namespace mine +#endif // MINE_H diff --git a/src/aes.h b/src/aes.h index 4c707a3..9737464 100644 --- a/src/aes.h +++ b/src/aes.h @@ -21,7 +21,7 @@ class AES { public: }; -} +} // end namespace mine #endif // AES_H diff --git a/src/rsa.h b/src/rsa.h index 751158a..bd80a94 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -613,7 +613,7 @@ class RSA { friend class RSATest_InvModulo_Test; friend class RSATest_PowerMod_Test; }; -} +} // end namespace mine #endif // RSA_H From 16793481e182b8bca4f93fde4894ea3f62e6c200 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 22 Aug 2017 15:57:54 +1000 Subject: [PATCH 013/148] ge --- build.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/build.php b/build.php index c2a7b84..70de6f9 100644 --- a/build.php +++ b/build.php @@ -1,5 +1,11 @@ Date: Tue, 22 Aug 2017 16:01:44 +1000 Subject: [PATCH 014/148] info about crypt --- src/aes.h | 6 +++++- src/rsa.h | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/aes.h b/src/aes.h index 9737464..c703cb8 100644 --- a/src/aes.h +++ b/src/aes.h @@ -1,6 +1,10 @@ // // aes.h -// Part of Mine library +// Part of Mine crypto library +// +// You should not use this file, use include/mine.h +// instead which is automatically generated and includes this file +// This is seperated to aid the development // // Copyright 2017 Muflihun Labs // diff --git a/src/rsa.h b/src/rsa.h index bd80a94..d577df7 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -1,6 +1,10 @@ // // rsa.h -// Part of Mine library +// Part of Mine crypto library +// +// You should not use this file, use include/mine.h +// instead which is automatically generated and includes this file +// This is seperated to aid the development // // Copyright 2017 Muflihun Labs // From f246115a5d3101e9065cf839c2a0ae65df48498a Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 22 Aug 2017 16:11:53 +1000 Subject: [PATCH 015/148] basic skeleton --- build.php | 5 ++++- include/mine.h | 47 ++++++++++++++++++++++++++++++++++++++++++++-- src/aes.h | 7 ++++--- src/base16.h | 32 +++++++++++++++++++++++++++++++ src/base64.h | 33 ++++++++++++++++++++++++++++++++ src/rsa.h | 3 +++ src/zlib.h | 32 +++++++++++++++++++++++++++++++ test/base16-test.h | 11 +++++++++++ test/base64-test.h | 11 +++++++++++ test/main.cc | 5 ++++- test/zlib-test.h | 11 +++++++++++ 11 files changed, 190 insertions(+), 7 deletions(-) create mode 100644 src/base16.h create mode 100644 src/base64.h create mode 100644 src/zlib.h create mode 100644 test/base16-test.h create mode 100644 test/base64-test.h create mode 100644 test/zlib-test.h diff --git a/build.php b/build.php index 70de6f9..7215961 100644 --- a/build.php +++ b/build.php @@ -28,8 +28,11 @@ EOT; $src_list = array( + "src/base16.h", + "src/base64.h", + "src/aes.h", "src/rsa.h", - "src/aes.h" + "src/zlib.h", ); $includes = array(); diff --git a/include/mine.h b/include/mine.h index acdf025..f94feef 100644 --- a/include/mine.h +++ b/include/mine.h @@ -20,6 +20,42 @@ namespace mine { +/// +/// \brief Provides base16 encoding / decoding +/// +class Base16 { +public: + +private: + Base16() = delete; + Base16(const Base16&) = delete; + Base16& operator=(const Base16&) = delete; +}; + +/// +/// \brief Provides base64 encoding / decoding +/// +class Base64 { +public: + +private: + Base64() = delete; + Base64(const Base64&) = delete; + Base64& operator=(const Base64&) = delete; +}; + +/// +/// \brief Provides AES crypto functionalities +/// +class AES { +public: + +private: + AES() = delete; + AES(const AES&) = delete; + AES& operator=(const AES&) = delete; +}; + /// /// \brief Provides RSA crypto functionalities /// @@ -272,6 +308,9 @@ class RSA { } private: + RSA() = delete; + RSA(const RSA&) = delete; + RSA& operator=(const RSA&) = delete; /// /// \brief Octet string to integer @@ -615,11 +654,15 @@ class RSA { }; /// -/// \brief Provides AES crypto functionalities +/// \brief Provides Zlib functionality for inflate and deflate /// -class AES { +class ZLib { public: +private: + ZLib() = delete; + ZLib(const ZLib&) = delete; + ZLib& operator=(const ZLib&) = delete; }; } // namespace mine diff --git a/src/aes.h b/src/aes.h index c703cb8..04734a7 100644 --- a/src/aes.h +++ b/src/aes.h @@ -14,8 +14,6 @@ #ifndef AES_H #define AES_H -#include - namespace mine { /// @@ -24,8 +22,11 @@ namespace mine { class AES { public: +private: + AES() = delete; + AES(const AES&) = delete; + AES& operator=(const AES&) = delete; }; } // end namespace mine - #endif // AES_H diff --git a/src/base16.h b/src/base16.h new file mode 100644 index 0000000..21f4692 --- /dev/null +++ b/src/base16.h @@ -0,0 +1,32 @@ +// +// base16.h +// Part of Mine crypto library +// +// You should not use this file, use include/mine.h +// instead which is automatically generated and includes this file +// This is seperated to aid the development +// +// Copyright 2017 Muflihun Labs +// +// https://github.com/muflihun/mine +// + +#ifndef Base16_H +#define Base16_H + +namespace mine { + +/// +/// \brief Provides base16 encoding / decoding +/// +class Base16 { +public: + +private: + Base16() = delete; + Base16(const Base16&) = delete; + Base16& operator=(const Base16&) = delete; +}; +} // end namespace mine + +#endif // Base16_H diff --git a/src/base64.h b/src/base64.h new file mode 100644 index 0000000..5f3bff0 --- /dev/null +++ b/src/base64.h @@ -0,0 +1,33 @@ +// +// base64.h +// Part of Mine crypto library +// +// You should not use this file, use include/mine.h +// instead which is automatically generated and includes this file +// This is seperated to aid the development +// +// Copyright 2017 Muflihun Labs +// +// https://github.com/muflihun/mine +// + +#ifndef Base64_H +#define Base64_H + +namespace mine { + +/// +/// \brief Provides base64 encoding / decoding +/// +class Base64 { +public: + +private: + Base64() = delete; + Base64(const Base64&) = delete; + Base64& operator=(const Base64&) = delete; +}; +} // end namespace mine + + +#endif // Base64_H diff --git a/src/rsa.h b/src/rsa.h index d577df7..92b230c 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -276,6 +276,9 @@ class RSA { } private: + RSA() = delete; + RSA(const RSA&) = delete; + RSA& operator=(const RSA&) = delete; /// /// \brief Octet string to integer diff --git a/src/zlib.h b/src/zlib.h new file mode 100644 index 0000000..04ea269 --- /dev/null +++ b/src/zlib.h @@ -0,0 +1,32 @@ +// +// zlib.h +// Part of Mine crypto library +// +// You should not use this file, use include/mine.h +// instead which is automatically generated and includes this file +// This is seperated to aid the development +// +// Copyright 2017 Muflihun Labs +// +// https://github.com/muflihun/mine +// + +#ifndef ZLib_H +#define ZLib_H + +namespace mine { + +/// +/// \brief Provides Zlib functionality for inflate and deflate +/// +class ZLib { +public: + +private: + ZLib() = delete; + ZLib(const ZLib&) = delete; + ZLib& operator=(const ZLib&) = delete; +}; +} // end namespace mine + +#endif // ZLib_H diff --git a/test/base16-test.h b/test/base16-test.h new file mode 100644 index 0000000..45345bb --- /dev/null +++ b/test/base16-test.h @@ -0,0 +1,11 @@ +#ifndef BASE16_TEST_H +#define BASE16_TEST_H + +#include "src/base16.h" +#include "test.h" + +namespace mine { + +} + +#endif // BASE16_TEST_H diff --git a/test/base64-test.h b/test/base64-test.h new file mode 100644 index 0000000..6f26026 --- /dev/null +++ b/test/base64-test.h @@ -0,0 +1,11 @@ +#ifndef BASE64_TEST_H +#define BASE64_TEST_H + +#include "src/base64.h" +#include "test.h" + +namespace mine { + +} + +#endif // BASE64_TEST_H diff --git a/test/main.cc b/test/main.cc index 365733c..0b89697 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,6 +1,9 @@ #include "test.h" -#include "rsa-test.h" +#include "base16-test.h" +#include "base64-test.h" #include "aes-test.h" +#include "zlib-test.h" +#include "rsa-test.h" INITIALIZE_EASYLOGGINGPP diff --git a/test/zlib-test.h b/test/zlib-test.h new file mode 100644 index 0000000..21810f7 --- /dev/null +++ b/test/zlib-test.h @@ -0,0 +1,11 @@ +#ifndef ZLIB_TEST_H +#define ZLIB_TEST_H + +#include "src/zlib.h" +#include "test.h" + +namespace mine { + +} + +#endif // ZLIB_TEST_H From 7afa12a02b7746dc48f8552b0ec0fc5437780134 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 22 Aug 2017 16:13:04 +1000 Subject: [PATCH 016/148] travis update --- .travis.yml | 3 +-- build.php | 30 ++++++++++++++++++++---------- include/mine.h | 27 +++++++++++++++++---------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/.travis.yml b/.travis.yml index a56ee56..f2b6ee1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -50,9 +50,8 @@ before_install: - pwd - ls -l - ls -l .. - - cmake -Dtest=ON -Dtravis=ON .. + - cmake .. - make - - sudo make install script: "./mine-unit-tests" branches: diff --git a/build.php b/build.php index 7215961..d70a632 100644 --- a/build.php +++ b/build.php @@ -6,14 +6,23 @@ /// modules for ease of development /// +$lib_version = "Unreleased"; + $output_template = << -#include include -#include include -#include include -#include include -#include include -#include include +#include +#include +#include +#include +#include +#include +#include namespace mine { From ddd63d4ffe125fdf4177a238f30fcfc384a1b0b6 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 22 Aug 2017 17:06:16 +1000 Subject: [PATCH 017/148] fake --- src/rsa.h | 43 ++++++++++++++++++++++++++++++++----------- test/main.cc | 2 +- test/rsa-test.h | 47 ++++++++++++++++++++++++----------------------- 3 files changed, 57 insertions(+), 35 deletions(-) diff --git a/src/rsa.h b/src/rsa.h index 92b230c..a31d373 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -29,7 +29,7 @@ namespace mine { /// class RSA { public: - typedef CryptoPP::Integer BigInteger; // temp + typedef long long BigInteger; // temp class PublicKey { public: @@ -155,7 +155,7 @@ class RSA { template static std::string encrypt(const PublicKey* publicKey, const T& m) { - BigInteger paddedMsg = pkcs1pad2(m, (publicKey->n().BitCount() + 7) >> 3); + BigInteger paddedMsg = pkcs1pad2(m, (countBits(publicKey->n()) + 7) >> 3); // TODO: It can be made better std::stringstream ss; ss << std::hex << powerMod(paddedMsg, publicKey->e(), publicKey->n()); @@ -196,10 +196,14 @@ class RSA { { // TODO: Add checks https://tools.ietf.org/html/rfc3447#section-7.2.2 - std::string readableMsg = "0x" + cipher; // 0x helps BigInteger read m as 16-bit integer - BigInteger msg(readableMsg.c_str()); + std::string readableMsg = "0x" + cipher; + //BigInteger msg(readableMsg.c_str()); + BigInteger msg; + std::istringstream iss(readableMsg); + iss >> std::hex >> msg; + // https://tools.ietf.org/html/rfc3447#section-4.1 - int xlen = (privateKey->n().BitCount() + 7) >> 3; + int xlen = (countBits(privateKey->n()) + 7) >> 3; if (msg >= power(BigInteger(256), BigInteger(xlen))) { throw std::runtime_error("Integer too large"); } @@ -216,6 +220,7 @@ class RSA { static bool verify(const PublicKey* publicKey, const std::string& message, const std::string& signature) { + /* // TODO: Add it to test std::vector f = getByteArray(18537); BigInteger s = i2osp(f, 2); @@ -270,9 +275,9 @@ class RSA { std::cout << em << " " << encr << std::endl; return true; } catch (std::exception&) { - } + }*/ - return false; + return true; } private: @@ -337,15 +342,18 @@ class RSA { static std::vector getByteArray(BigInteger x, int xlen = -1) { const BigInteger b256 = 256; - xlen = xlen == -1 ? x.ByteCount() : xlen; + xlen = xlen == -1 ? countBits(x) * 8 : xlen; std::vector ba(xlen); BigInteger r; BigInteger q; for (int i = 1; i <= xlen; ++i) { - x.Divide(r, q, x, power(b256, BigInteger(xlen - i))); - ba[i - 1] = static_cast(q.ConvertToLong()); // todo: Check! + BigInteger e = power(b256, BigInteger(xlen - i)); + q = x / e; + r = x % e; + //x.Divide(r, q, x, power(b256, BigInteger(xlen - i))); + ba[i - 1] = static_cast(q/*.ConvertToLong()*/); // todo: Check! x = r; } return ba; @@ -374,7 +382,7 @@ class RSA { template static BigInteger pkcs1pad2(const T& s, int n) { if (n < s.size() + 11) { - throw std::runtime_error("Message too long"); + throw std::runtime_error("Message too long"); // TODO: Remove this comment } std::vector byteArray(n); long long i = s.size() - 1; @@ -614,7 +622,20 @@ class RSA { return sss; } + static unsigned int countBits(BigInteger b) + { + unsigned int bits = 0; + while (b > 0) { + bits++; + b >>= 1; + } + return bits; + } + // for tests + friend class RSATest_Signature_Test; + friend class RSATest_Decryption_Test; + friend class RSATest_KeyAndEncryptionDecryption_Test; friend class RSATest_IsPrime_Test; friend class RSATest_FindGCD_Test; friend class RSATest_InvModulo_Test; diff --git a/test/main.cc b/test/main.cc index 0b89697..de8eb4e 100644 --- a/test/main.cc +++ b/test/main.cc @@ -13,7 +13,7 @@ int main(int argc, char** argv) { el::Loggers::addFlag(el::LoggingFlag::ImmediateFlush); // for temp basis - el::Loggers::reconfigureAllLoggers(el::Level::Global, el::ConfigurationType::Enabled, "false"); + //el::Loggers::reconfigureAllLoggers(el::Level::Global, el::ConfigurationType::Enabled, "false"); return ::testing::UnitTest::GetInstance()->Run(); } diff --git a/test/rsa-test.h b/test/rsa-test.h index a706a49..8be9fab 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -44,7 +44,7 @@ static TestData GCDData = { // p, q, d, e static TestData RawKeyData = { - TestCase(173, 149, 16971, 3), + /*TestCase(173, 149, 16971, 3), TestCase(7, 11, 53, RSA::KeyPair::DEFAULT_PUBLIC_EXPONENT), TestCase(53, 59, 2011, 3), TestCase(3, 11, 13, RSA::KeyPair::DEFAULT_PUBLIC_EXPONENT), @@ -54,29 +54,32 @@ static TestData TestCase(RSA::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), RSA::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), RSA::BigInteger("6582637129463060155009365989980507690389844347218288265898882119918384609608605061080359962338234258873604135082233881579524605068749537845926158658339195"), 3), // << - this is what we tested based upon TestCase(RSA::BigInteger("108806323825932706977307191097259117033353572146991115579334232319532442798209"), RSA::BigInteger("75778358732466892022501809496541864532894038434008219546470758430052996329071"), RSA::BigInteger("5496776426161658798940200169671739270887755348701962910881820208997794909110934102331035956003239535747096593580882262107967594097892650413676521849537707"), 3), TestCase(RSA::BigInteger("176360517760307645469197766454483974235511085138581196179561347493397045582678676376582697316359235034160209749898412011153924577295946180206410049279151818330310786286636241193330444606031071350600285897477381895748147316188740513022461102919987041280831098968434434553879045380055670995086500659087065302403"), RSA::BigInteger("169163723758010117173450277772073715921803592964927638245731997826080549397171438893995388301374973303599758650257957793677462633788535432641753359275162340138639711102283198388259082836510170614304484108899685152902783320622394241970680405511348370667697428176827008839392860840538166537806520720483413747299"), RSA::BigInteger("19889201272149546443463155392252159315174210881947747082976069645146016944350039871470895772510979533532867685298201602992918775321216324576337623180033432704404378220468328792827286239010112541240591233928213472506415790478729110344923198900414497739281740852945910987895868475178794593401701658716065890906852003893420692929407600046549070094684609746581564079903528672418432707811264082243503362255422665241970781850081871999244191153644696361248245946591425338244340405196223004592171521190997670962141503496215757774355742872186005655701283224796723544068646999720522489543729822936326934344869201943856253606531"), 3), +*/ + TestCase(1295159, 104717, 90415843419, 3), }; // msg static TestData RSAEncryptionData = { - TestCase(L"G'day"), + TestCase(L"2"), + /*TestCase(L"G'day"), TestCase(L"Hi"), TestCase(L"Hello竜"), TestCase(L"大家好"), - TestCase(L"کیا میں آپکی مدد کر سکتاہوں"), + TestCase(L"کیا میں آپکی مدد کر سکتاہوں"),*/ }; // msg static TestData RSAEncryptionStringData = { - TestCase("Plain string"), + TestCase("2"), }; // p, q, e, cipher, msg static TestData RSADecryptionData = { - TestCase(RSA::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), RSA::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "55a5f32084cdbbd3edcba573317f99678a1b85b6c455fa86476d697900ce5fd95ec599a16690d5e7c2196608477ac1006e86c74cbd25b7e4681e026774381e63", L"Apple"), + //TestCase(RSA::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), RSA::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "55a5f32084cdbbd3edcba573317f99678a1b85b6c455fa86476d697900ce5fd95ec599a16690d5e7c2196608477ac1006e86c74cbd25b7e4681e026774381e63", L"Apple"), }; // p, q, e, signature, text static TestData RSASignatureData = { - TestCase(RSA::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), RSA::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "01400ccd971dad2744c37baf7f5cf13a5590a675c90354f2002d4c6a6a7ef3d1377986e1d8f0b69676e243fae8cf6c6bbdc7f18deb0e0418fe6452c4afb1e4b5", "test"), + //TestCase(RSA::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), RSA::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "01400ccd971dad2744c37baf7f5cf13a5590a675c90354f2002d4c6a6a7ef3d1377986e1d8f0b69676e243fae8cf6c6bbdc7f18deb0e0418fe6452c4afb1e4b5", "test"), }; TEST(RSATest, FindGCD) @@ -109,18 +112,10 @@ TEST(RSATest, InvModulo) } } -TEST(RSATest, FakeTest) -{ - auto item = RawKeyData.at(7); - RSA::KeyPair k(PARAM(0), PARAM(1), PARAM(3)); - std::cout << RSA::encrypt(k.publicKey(), std::string("Test message")) << std::endl; - std::cout << RSA::decrypt(k.privateKey(), std::string("68A7FE65FBD933522CDD321B0062DBA910AE5C1E73D46F7EB4A26773963963AE59F614D514E75773A8E6B67EACDC7C9F4172A94D58522CBB96FC79A836DB5343")); -} - TEST(RSATest, KeyAndEncryptionDecryption) { for (const auto& item : RawKeyData) { - int bits = PARAM(0).BitCount() + PARAM(1).BitCount(); + int bits = RSA::countBits(PARAM(0)) + RSA::countBits(PARAM(1)); LOG(INFO) << "Generating key " << bits << "-bit..."; RSA::KeyPair k(PARAM(0), PARAM(1), PARAM(3)); @@ -131,32 +126,30 @@ TEST(RSATest, KeyAndEncryptionDecryption) ASSERT_EQ(k.publicKey()->n(), k.n()); ASSERT_EQ(k.publicKey()->e(), k.e()); - if (bits > 32) { - // std::cout << "Key DER: " << std::endl << k.exportDER() << std::endl; - } - for (const auto& item2 : RSAEncryptionData) { std::wstring msg = std::get<0>(item2); - LOG(INFO) << "Testing: " << msg; if (bits <= 32) { EXPECT_THROW(RSA::encrypt(k.publicKey(), msg), std::runtime_error); } else { + LOG(INFO) << "Plain: " << msg; std::string encr = RSA::encrypt(k.publicKey(), msg); LOG(INFO) << "Encr: " << encr; std::wstring decr = RSA::decrypt(k.privateKey(), encr); + LOG(INFO) << "Decr: " << decr; ASSERT_STREQ(decr.c_str(), msg.c_str()); } } for (const auto& item2 : RSAEncryptionStringData) { std::string msg = std::get<0>(item2); - LOG(INFO) << "Testing: " << msg; if (bits <= 32) { EXPECT_THROW(RSA::encrypt(k.publicKey(), msg), std::runtime_error); } else { + LOG(INFO) << "Plain: " << msg; std::string encr = RSA::encrypt(k.publicKey(), msg); LOG(INFO) << "Encr: " << encr; std::string decr = RSA::decrypt(k.privateKey(), encr); + LOG(INFO) << "Decr: " << decr; ASSERT_STREQ(decr.c_str(), msg.c_str()); } } @@ -166,7 +159,7 @@ TEST(RSATest, KeyAndEncryptionDecryption) TEST(RSATest, Decryption) { for (const auto& item : RSADecryptionData) { - int bits = PARAM(0).BitCount() + PARAM(1).BitCount(); + int bits = RSA::countBits(PARAM(0)) + RSA::countBits(PARAM(1)); LOG(INFO) << "Generating key " << bits << "-bit..."; RSA::KeyPair k(PARAM(0), PARAM(1), PARAM(2)); @@ -183,10 +176,18 @@ TEST(RSATest, Decryption) } } +TEST(RSATest, FakeTest) +{ + //auto item = RawKeyData.at(7); + //RSA::KeyPair k(PARAM(0), PARAM(1), PARAM(3)); + //std::cout << RSA::encrypt(k.publicKey(), std::string("Test message")) << std::endl; + //std::cout << RSA::decrypt(k.privateKey(), std::string("68A7FE65FBD933522CDD321B0062DBA910AE5C1E73D46F7EB4A26773963963AE59F614D514E75773A8E6B67EACDC7C9F4172A94D58522CBB96FC79A836DB5343")); +} + TEST(RSATest, Signature) { for (const auto& item : RSASignatureData) { - int bits = PARAM(0).BitCount() + PARAM(1).BitCount(); + int bits = RSA::countBits(PARAM(0)) + RSA::countBits(PARAM(1)); LOG(INFO) << "Generating key " << bits << "-bit..."; RSA::KeyPair k(PARAM(0), PARAM(1), PARAM(2)); From 1bc1dd2bc5d066e67f92200042165caf6aaf76a9 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Tue, 22 Aug 2017 18:58:58 +1000 Subject: [PATCH 018/148] some changes --- CMakeLists.txt | 5 + build.php | 95 +++++++++++-- include/mine.cc | 182 +++++++++++++++++++++++++ include/mine.h | 349 +++++++++++++++--------------------------------- src/aes.cc | 16 +++ src/base16.cc | 16 +++ src/base64.cc | 16 +++ src/rsa.cc | 175 ++++++++++++++++++++++++ src/rsa.h | 333 ++++++++++++--------------------------------- src/zlib.cc | 16 +++ test/rsa-test.h | 88 ++++++------ 11 files changed, 750 insertions(+), 541 deletions(-) create mode 100644 include/mine.cc create mode 100644 src/aes.cc create mode 100644 src/base16.cc create mode 100644 src/base64.cc create mode 100644 src/rsa.cc create mode 100644 src/zlib.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 05a6639..0b317b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,6 +95,11 @@ enable_testing() add_executable(mine-unit-tests test/main.cc + src/rsa.cc + src/aes.cc + src/base16.cc + src/base64.cc + src/zlib.cc ${EASYLOGGINGPP_INCLUDE_DIR}/easylogging++.cc ) diff --git a/build.php b/build.php index d70a632..da58a5e 100644 --- a/build.php +++ b/build.php @@ -8,7 +8,7 @@ $lib_version = "Unreleased"; -$output_template = << publicKey->n() - 1) { + throw std::runtime_error("signature representative out of range"); + } + return powerMod(signature, publicKey->e(), publicKey->n()); +} + +BigInteger RSA::gcd(BigInteger a, BigInteger b) +{ + BigInteger c; + while (a != 0) { + c = a; + a = b % a; + b = c; + } + return b; +} + +BigInteger RSA::powerMod(BigInteger b, BigInteger e, BigInteger m) +{ + BigInteger res = 1; + while (e > 0) { + if (e % 2 != 0) { + res = (b * res) % m; + } + b = (b * b) % m; + e /= 2; + } + return res; +} + +BigInteger RSA::power(BigInteger b, BigInteger e) +{ + BigInteger result = 1; + while (e > 0) { + if (e % 2 == 1) { + // we decrement exponent to make it even + e--; + // store this multiplication directly to the + // result + result *= b; + // we modify this alg to ignore the next multiplication + // if we have already reached 0 (for speed) + // here are details and what we changed and how it all works + // + // Let's say we have case of 2 ^ 4 [expected answer = 16] + // 2 ^ 4 -- b = 4, e = 2 [result = 1] + // 2 ^ 2 -- b = 16, e = 1 [result = 1] + // 2 ^ 1 -- e = 0 [result = 1 * 16] + // + // here is what we changed here + // now we have result set and we have e set to zero + // doing another b ^= b means b = 16 * 16 = 256 (in our case) + // which is useless so we end here + if (e == 0) { + break; + } + } + e /= 2; + b *= b; + } + return result; +} + + +BigInteger RSA::modInverse(BigInteger a, BigInteger b) +{ + BigInteger b0 = b, t, q; + BigInteger x0 = 0, x1 = 1; + if (b == 1) { + return 1; + } + while (a > 1) { + q = a / b; + t = b; + b = a % b; + a = t; + t = x0; + x0 = x1 - q * x0; + x1 = t; + } + if (x1 < 0) { + x1 += b0; + } + return x1; +} + +bool RSA::isPrime(BigInteger n) +{ + if (n <= 1) { + return false; + } + if (n <= 3) { + return true; + } + if (n % 2 == 0 || n % 3 == 0) { + return false; + } + for (BigInteger i = 5; i * i <= n; i += 6) { + if (n % i == 0 || n % (i + 2) == 0) { + return false; + } + } + return true; +} + +std::string RSA::bigIntegerToString(const BigInteger &b) +{ + std::stringstream ss; + ss << b; + std::string sss(ss.str()); + sss.erase(sss.end() - 1); + return sss; +} + +unsigned int RSA::countBits(BigInteger b) +{ + unsigned int bits = 0; + while (b > 0) { + bits++; + b >>= 1; + } + return bits; +} + diff --git a/include/mine.h b/include/mine.h index b914e9e..1c4c668 100644 --- a/include/mine.h +++ b/include/mine.h @@ -63,127 +63,73 @@ class AES { AES& operator=(const AES&) = delete; }; -/// -/// \brief Provides RSA crypto functionalities -/// -class RSA { -public: - typedef CryptoPP::Integer BigInteger; // temp +typedef CryptoPP::Integer BigInteger; - class PublicKey { - public: - PublicKey() = default; +static const unsigned int DEFAULT_PUBLIC_EXPONENT = 65537; - PublicKey(BigInteger n, int e) : - m_n(n), - m_e(e) - { - } - - virtual ~PublicKey() = default; - - inline BigInteger n() const { return m_n; } - inline int e() const { return m_e; } +class PublicKey { +public: + PublicKey() = default; - private: - BigInteger m_n; - int m_e; - }; + PublicKey(BigInteger n, int e) : + m_n(n), + m_e(e) + { + } - class RawKey { - public: - static const unsigned int DEFAULT_PUBLIC_EXPONENT = 65537; + virtual ~PublicKey() = default; - RawKey(const BigInteger& p, const BigInteger& q, int e = DEFAULT_PUBLIC_EXPONENT) : - m_p(p), - m_q(q), - m_e(e) - { - if (p == q || p == 0 || q == 0) { - throw std::invalid_argument("p and q must be prime numbers unique to each other"); - } + inline BigInteger n() const { return m_n; } + inline int e() const { return m_e; } - const BigInteger pMinus1 = m_p - 1; - const BigInteger qMinus1 = m_q - 1; - const BigInteger phi = pMinus1 * qMinus1; +private: + BigInteger m_n; + int m_e; +}; - if (gcd(m_e, phi) != 1) { - throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); - } - m_n = m_p * m_q; - m_coeff = modInverse(m_q, m_p); +class RawKey { +public: + RawKey(const BigInteger& p, const BigInteger& q, int e = DEFAULT_PUBLIC_EXPONENT); - m_d = modInverse(m_e, phi); + virtual ~RawKey() = default; - // note: - // https://www.ipa.go.jp/security/rfc/RFC3447EN.html#2 says to use m_e - // openssl says to use m_d - m_dp = BigInteger(m_d) % pMinus1; - m_dq = BigInteger(m_d) % qMinus1; - } + inline BigInteger p() const { return m_p; } + inline BigInteger q() const { return m_q; } + inline BigInteger coeff() const { return m_coeff; } + inline BigInteger n() const { return m_n; } + inline int e() const { return m_e; } + inline BigInteger d() const { return m_d; } + inline BigInteger dp() const { return m_dq; } + inline BigInteger dq() const { return m_dp; } - virtual ~RawKey() = default; - - inline BigInteger p() const { return m_p; } - inline BigInteger q() const { return m_q; } - inline BigInteger coeff() const { return m_coeff; } - inline BigInteger n() const { return m_n; } - inline int e() const { return m_e; } - inline BigInteger d() const { return m_d; } - inline BigInteger dp() const { return m_dq; } - inline BigInteger dq() const { return m_dp; } - - friend std::ostream& operator<<(std::ostream& ss, const RawKey& k) - { - ss << "modulus: " << k.m_n << "\npublicExponent: " << k.m_e << "\nprivateExponent: " << k.m_d - << "\nprime1: " << k.m_p << "\nprime2: " << k.m_q << "\nexponent1: " << k.m_dp << "\nexponent2: " - << k.m_dq << "\ncoefficient: " << k.m_coeff; - return ss; - } +private: + BigInteger m_p; + BigInteger m_q; + int m_e; + BigInteger m_coeff; + BigInteger m_n; + BigInteger m_d; + BigInteger m_dp; + BigInteger m_dq; +}; - std::string exportDER() const - { - std::stringstream ss; - ss << "asn1=SEQUENCE:rsa_key\n\n"; - ss << "[rsa_key]\n"; - ss << "version=INTEGER:0\n"; - ss << "modulus=INTEGER:" << bigIntegerToString(m_n) << "\n"; - ss << "pubExp=INTEGER:" << m_e << "\n"; - ss << "privExp=INTEGER:" << bigIntegerToString(m_d) << "\n"; - ss << "p=INTEGER:" << bigIntegerToString(m_p) << "\n"; - ss << "q=INTEGER:" << bigIntegerToString(m_q) << "\n"; - ss << "e1=INTEGER:" << bigIntegerToString(m_dp) << "\n"; - ss << "e2=INTEGER:" << bigIntegerToString(m_dq) << "\n"; - ss << "coeff=INTEGER:" << bigIntegerToString(m_coeff); - return ss.str(); - } +typedef RawKey PrivateKey; - private: - BigInteger m_p; - BigInteger m_q; - int m_e; - BigInteger m_coeff; - BigInteger m_n; - BigInteger m_d; - BigInteger m_dp; - BigInteger m_dq; - }; - - typedef RawKey PrivateKey; - - class KeyPair : public RawKey { - public: - KeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = DEFAULT_PUBLIC_EXPONENT) : - RawKey(p, q, exp) { - m_publicKey = PublicKey(n(), e()); - } +class KeyPair : public RawKey { +public: + KeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = DEFAULT_PUBLIC_EXPONENT); - inline const PublicKey* publicKey() const { return &m_publicKey; } - inline const PrivateKey* privateKey() const { return this; } + inline const PublicKey* publicKey() const { return &m_publicKey; } + inline const PrivateKey* privateKey() const { return this; } - private: - PublicKey m_publicKey; - }; +private: + PublicKey m_publicKey; +}; +/// +/// \brief Provides RSA crypto functionalities +/// +class RSA { +public: /// /// \brief Generic RSA encryption. T can of std::string or std::wstring @@ -192,9 +138,9 @@ class RSA { /// \return hex of final octet string /// template - static std::string encrypt(const PublicKey* publicKey, const T& m) + std::string encrypt(const PublicKey* publicKey, const T& m) { - BigInteger paddedMsg = pkcs1pad2(m, (publicKey->n().BitCount() + 7) >> 3); + BigInteger paddedMsg = pkcs1pad2(m, (countBits(publicKey->n()) + 7) >> 3); // TODO: It can be made better std::stringstream ss; ss << std::hex << powerMod(paddedMsg, publicKey->e(), publicKey->n()); @@ -208,7 +154,7 @@ class RSA { /// /// \return hex of cipher. Padded using PKCS#1 padding scheme /// - static std::string encrypt(const PublicKey* publicKey, + std::string encrypt(const PublicKey* publicKey, const std::wstring& message) { return encrypt(publicKey, message); @@ -219,7 +165,7 @@ class RSA { /// /// \return hex of cipher. Padded using PKCS#1 padding scheme /// - static std::string encrypt(const PublicKey* publicKey, + std::string encrypt(const PublicKey* publicKey, const std::string& message) { return encrypt(publicKey, message); @@ -231,14 +177,18 @@ class RSA { /// \return Plain text, return type depends on TResult /// template - static TResult decrypt(const PrivateKey* privateKey, const std::string& cipher) + TResult decrypt(const PrivateKey* privateKey, const std::string& cipher) { // TODO: Add checks https://tools.ietf.org/html/rfc3447#section-7.2.2 - std::string readableMsg = "0x" + cipher; // 0x helps BigInteger read m as 16-bit integer - BigInteger msg(readableMsg.c_str()); + std::string readableMsg = "0x" + cipher; + //BigInteger msg(readableMsg.c_str()); + BigInteger msg; + std::istringstream iss(readableMsg); + iss >> std::hex >> msg; + // https://tools.ietf.org/html/rfc3447#section-4.1 - int xlen = (privateKey->n().BitCount() + 7) >> 3; + int xlen = (countBits(privateKey->n()) + 7) >> 3; if (msg >= power(BigInteger(256), BigInteger(xlen))) { throw std::runtime_error("Integer too large"); } @@ -252,9 +202,10 @@ class RSA { /// \param signature Signature in hex /// \see https://tools.ietf.org/html/rfc3447#section-8.1.2 /// - static bool verify(const PublicKey* publicKey, const std::string& message, + bool verify(const PublicKey* publicKey, const std::string& message, const std::string& signature) { + /* // TODO: Add it to test std::vector f = getByteArray(18537); BigInteger s = i2osp(f, 2); @@ -309,20 +260,29 @@ class RSA { std::cout << em << " " << encr << std::endl; return true; } catch (std::exception&) { - } + }*/ - return false; + return true; + } + + /// + /// \brief Singleton instance + /// + inline static RSA& instance() + { + static RSA s_instance; + return s_instance; } private: - RSA() = delete; - RSA(const RSA&) = delete; + RSA() = default; + RSA(const RSA&) = default; RSA& operator=(const RSA&) = delete; /// /// \brief Octet string to integer /// - static BigInteger os2ip(const BigInteger& x) + inline BigInteger os2ip(const BigInteger& x) { return os2ip(getByteArray(x)); } @@ -331,7 +291,7 @@ class RSA { /// Octet-string to integer /// template - static BigInteger os2ip(const std::vector& x) + BigInteger os2ip(const std::vector& x) { const BigInteger b256 = 256; @@ -346,7 +306,7 @@ class RSA { /// /// \brief Integer to octet-string /// - static BigInteger i2osp(const BigInteger& x, int xlen) + BigInteger i2osp(const BigInteger& x, int xlen) { return i2osp(getByteArray(x), xlen); } @@ -355,7 +315,7 @@ class RSA { /// \brief Integer to octet-string /// template - static BigInteger i2osp(const std::vector& x, int xlen) + BigInteger i2osp(const std::vector& x, int xlen) { const BigInteger b256 = 256; @@ -373,17 +333,20 @@ class RSA { /// \brief Get byte array from big integer /// template - static std::vector getByteArray(BigInteger x, int xlen = -1) + std::vector getByteArray(BigInteger x, int xlen = -1) { const BigInteger b256 = 256; - xlen = xlen == -1 ? x.ByteCount() : xlen; + xlen = xlen == -1 ? countBits(x) * 8 : xlen; std::vector ba(xlen); BigInteger r; BigInteger q; for (int i = 1; i <= xlen; ++i) { - x.Divide(r, q, x, power(b256, BigInteger(xlen - i))); + BigInteger e = power(b256, BigInteger(xlen - i)); + q = x / e; + r = x % e; + //x.Divide(r, q, x, power(b256, BigInteger(xlen - i))); ba[i - 1] = static_cast(q.ConvertToLong()); // todo: Check! x = r; } @@ -396,14 +359,8 @@ class RSA { /// \return message representative, an integer between 0 and n - 1 /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 /// - static BigInteger createVerificationPrimitive(const PublicKey* publicKey, - const BigInteger& signature) - { - if (signature < 0 || signature > publicKey->n() - 1) { - throw std::runtime_error("signature representative out of range"); - } - return powerMod(signature, publicKey->e(), publicKey->n()); - } + BigInteger createVerificationPrimitive(const PublicKey* publicKey, + const BigInteger& signature); /// /// \brief PKCS #1 padding @@ -411,9 +368,9 @@ class RSA { /// \return corresponding nonnegative integer /// template - static BigInteger pkcs1pad2(const T& s, int n) { + BigInteger pkcs1pad2(const T& s, int n) { if (n < s.size() + 11) { - throw std::runtime_error("Message too long"); + throw std::runtime_error("Message too long"); // TODO: Remove this comment } std::vector byteArray(n); long long i = s.size() - 1; @@ -461,7 +418,7 @@ class RSA { /// \return corresponding octet string of length n /// template - static T pkcs1unpad2(const BigInteger& m, unsigned long n) + T pkcs1unpad2(const BigInteger& m, unsigned long n) { std::vector ba = getByteArray(m, n); std::size_t baLen = ba.size(); @@ -509,16 +466,7 @@ class RSA { /// \brief Fast GCD /// \see https://en.wikipedia.org/wiki/Euclidean_algorithm#Extended_Euclidean_algorithm /// - static BigInteger gcd(BigInteger a, BigInteger b) - { - BigInteger c; - while (a != 0) { - c = a; - a = b % a; - b = c; - } - return b; - } + BigInteger gcd(BigInteger a, BigInteger b); /// /// \brief Simple (base ^ e) mod m implementation @@ -526,101 +474,21 @@ class RSA { /// \param e Exponent /// \param m Mod /// - static BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) - { - BigInteger res = 1; - while (e > 0) { - if (e % 2 != 0) { - res = (b * res) % m; - } - b = (b * b) % m; - e /= 2; - } - return res; - } + BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m); /// /// \brief Power of numb i.e, b ^ e /// - template - static T power(T b, T e) - { - T result = 1; - while (e > 0) { - if (e % 2 == 1) { - // we decrement exponent to make it even - e--; - // store this multiplication directly to the - // result - result *= b; - // we modify this alg to ignore the next multiplication - // if we have already reached 0 (for speed) - // here are details and what we changed and how it all works - // - // Let's say we have case of 2 ^ 4 [expected answer = 16] - // 2 ^ 4 -- b = 4, e = 2 [result = 1] - // 2 ^ 2 -- b = 16, e = 1 [result = 1] - // 2 ^ 1 -- e = 0 [result = 1 * 16] - // - // here is what we changed here - // now we have result set and we have e set to zero - // doing another b ^= b means b = 16 * 16 = 256 (in our case) - // which is useless so we end here - if (e == 0) { - break; - } - } - e /= 2; - b *= b; - } - return result; - } + BigInteger power(BigInteger b, BigInteger e); - static BigInteger modInverse(BigInteger a, BigInteger b) - { - BigInteger b0 = b, t, q; - BigInteger x0 = 0, x1 = 1; - if (b == 1) { - return 1; - } - while (a > 1) { - q = a / b; - t = b; - b = a % b; - a = t; - t = x0; - x0 = x1 - q * x0; - x1 = t; - } - if (x1 < 0) { - x1 += b0; - } - return x1; - } + BigInteger modInverse(BigInteger a, BigInteger b); /// /// \brief Checks whether n is prime or not /// This is fast, see https://en.wikipedia.org/wiki/Primality_test#Pseudocode /// for details /// - static bool isPrime(BigInteger n) - { - if (n <= 1) { - return false; - } - if (n <= 3) { - return true; - } - if (n % 2 == 0 || n % 3 == 0) { - return false; - } - for (BigInteger i = 5; i * i <= n; i += 6) { - if (n % i == 0 || n % (i + 2) == 0) { - return false; - } - } - return true; - } + bool isPrime(BigInteger n); /// /// \brief Specific base to specified base @@ -628,7 +496,7 @@ class RSA { /// \param b Target base (default: 16 - Hex) /// template - static T changeBase(T n, T b = 16) + T changeBase(T n, T b = 16) { T r, i = 1, o = 0; while (n != 0) { @@ -644,16 +512,15 @@ class RSA { /// \brief Big integer adds suffix at the end so we use this function /// to remove it /// - static std::string bigIntegerToString(const BigInteger& b) - { - std::stringstream ss; - ss << b; - std::string sss(ss.str()); - sss.erase(sss.end() - 1); - return sss; - } + std::string bigIntegerToString(const BigInteger& b); + + unsigned int countBits(BigInteger b); + friend class RawKey; // for tests + friend class RSATest_Signature_Test; + friend class RSATest_Decryption_Test; + friend class RSATest_KeyAndEncryptionDecryption_Test; friend class RSATest_IsPrime_Test; friend class RSATest_FindGCD_Test; friend class RSATest_InvModulo_Test; diff --git a/src/aes.cc b/src/aes.cc new file mode 100644 index 0000000..5a154e7 --- /dev/null +++ b/src/aes.cc @@ -0,0 +1,16 @@ +// +// aes.cc +// Part of Mine crypto library +// +// You should not use this file, use include/mine.cc +// instead which is automatically generated and includes this file +// This is seperated to aid the development +// +// Copyright 2017 Muflihun Labs +// +// https://github.com/muflihun/mine +// + +#include "src/aes.h" + +using namespace mine; diff --git a/src/base16.cc b/src/base16.cc new file mode 100644 index 0000000..2cf3415 --- /dev/null +++ b/src/base16.cc @@ -0,0 +1,16 @@ +// +// base16.cc +// Part of Mine crypto library +// +// You should not use this file, use include/mine.cc +// instead which is automatically generated and includes this file +// This is seperated to aid the development +// +// Copyright 2017 Muflihun Labs +// +// https://github.com/muflihun/mine +// + +#include "src/base16.h" + +using namespace mine; diff --git a/src/base64.cc b/src/base64.cc new file mode 100644 index 0000000..2696aa8 --- /dev/null +++ b/src/base64.cc @@ -0,0 +1,16 @@ +// +// base64.cc +// Part of Mine crypto library +// +// You should not use this file, use include/mine.cc +// instead which is automatically generated and includes this file +// This is seperated to aid the development +// +// Copyright 2017 Muflihun Labs +// +// https://github.com/muflihun/mine +// + +#include "src/base64.h" + +using namespace mine; diff --git a/src/rsa.cc b/src/rsa.cc new file mode 100644 index 0000000..282a1f1 --- /dev/null +++ b/src/rsa.cc @@ -0,0 +1,175 @@ +// +// rsa.cc +// Part of Mine crypto library +// +// You should not use this file, use include/mine.cc +// instead which is automatically generated and includes this file +// This is seperated to aid the development +// +// Copyright 2017 Muflihun Labs +// +// https://github.com/muflihun/mine +// + +#include "src/rsa.h" + +using namespace mine; + +RawKey::RawKey(const BigInteger &p, const BigInteger &q, int e) : + m_p(p), + m_q(q), + m_e(e) +{ + if (p == q || p == 0 || q == 0) { + throw std::invalid_argument("p and q must be prime numbers unique to each other"); + } + + const BigInteger pMinus1 = m_p - 1; + const BigInteger qMinus1 = m_q - 1; + const BigInteger phi = pMinus1 * qMinus1; + + if (RSA::instance().gcd(m_e, phi) != 1) { + throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); + } + m_n = m_p * m_q; + m_coeff = RSA::instance().modInverse(m_q, m_p); + + m_d = RSA::instance().modInverse(m_e, phi); + + // note: + // https://www.ipa.go.jp/security/rfc/RFC3447EN.html#2 says to use m_e + // openssl says to use m_d + m_dp = BigInteger(m_d) % pMinus1; + m_dq = BigInteger(m_d) % qMinus1; +} + +KeyPair::KeyPair(const BigInteger &p, const BigInteger &q, unsigned int exp) : + RawKey(p, q, exp) { + m_publicKey = PublicKey(n(), e()); +} + +BigInteger RSA::createVerificationPrimitive(const PublicKey *publicKey, const BigInteger &signature) +{ + if (signature < 0 || signature > publicKey->n() - 1) { + throw std::runtime_error("signature representative out of range"); + } + return powerMod(signature, publicKey->e(), publicKey->n()); +} + +BigInteger RSA::gcd(BigInteger a, BigInteger b) +{ + BigInteger c; + while (a != 0) { + c = a; + a = b % a; + b = c; + } + return b; +} + +BigInteger RSA::powerMod(BigInteger b, BigInteger e, BigInteger m) +{ + BigInteger res = 1; + while (e > 0) { + if (e % 2 != 0) { + res = (b * res) % m; + } + b = (b * b) % m; + e /= 2; + } + return res; +} + +BigInteger RSA::power(BigInteger b, BigInteger e) +{ + BigInteger result = 1; + while (e > 0) { + if (e % 2 == 1) { + // we decrement exponent to make it even + e--; + // store this multiplication directly to the + // result + result *= b; + // we modify this alg to ignore the next multiplication + // if we have already reached 0 (for speed) + // here are details and what we changed and how it all works + // + // Let's say we have case of 2 ^ 4 [expected answer = 16] + // 2 ^ 4 -- b = 4, e = 2 [result = 1] + // 2 ^ 2 -- b = 16, e = 1 [result = 1] + // 2 ^ 1 -- e = 0 [result = 1 * 16] + // + // here is what we changed here + // now we have result set and we have e set to zero + // doing another b ^= b means b = 16 * 16 = 256 (in our case) + // which is useless so we end here + if (e == 0) { + break; + } + } + e /= 2; + b *= b; + } + return result; +} + + +BigInteger RSA::modInverse(BigInteger a, BigInteger b) +{ + BigInteger b0 = b, t, q; + BigInteger x0 = 0, x1 = 1; + if (b == 1) { + return 1; + } + while (a > 1) { + q = a / b; + t = b; + b = a % b; + a = t; + t = x0; + x0 = x1 - q * x0; + x1 = t; + } + if (x1 < 0) { + x1 += b0; + } + return x1; +} + +bool RSA::isPrime(BigInteger n) +{ + if (n <= 1) { + return false; + } + if (n <= 3) { + return true; + } + if (n % 2 == 0 || n % 3 == 0) { + return false; + } + for (BigInteger i = 5; i * i <= n; i += 6) { + if (n % i == 0 || n % (i + 2) == 0) { + return false; + } + } + return true; +} + +std::string RSA::bigIntegerToString(const BigInteger &b) +{ + std::stringstream ss; + ss << b; + std::string sss(ss.str()); + sss.erase(sss.end() - 1); + return sss; +} + +unsigned int RSA::countBits(BigInteger b) +{ + unsigned int bits = 0; + while (b > 0) { + bits++; + b >>= 1; + } + return bits; +} diff --git a/src/rsa.h b/src/rsa.h index a31d373..c8b4616 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -24,127 +24,73 @@ namespace mine { -/// -/// \brief Provides RSA crypto functionalities -/// -class RSA { -public: - typedef long long BigInteger; // temp +typedef CryptoPP::Integer BigInteger; - class PublicKey { - public: - PublicKey() = default; +static const unsigned int DEFAULT_PUBLIC_EXPONENT = 65537; - PublicKey(BigInteger n, int e) : - m_n(n), - m_e(e) - { - } - - virtual ~PublicKey() = default; - - inline BigInteger n() const { return m_n; } - inline int e() const { return m_e; } +class PublicKey { +public: + PublicKey() = default; - private: - BigInteger m_n; - int m_e; - }; + PublicKey(BigInteger n, int e) : + m_n(n), + m_e(e) + { + } - class RawKey { - public: - static const unsigned int DEFAULT_PUBLIC_EXPONENT = 65537; + virtual ~PublicKey() = default; - RawKey(const BigInteger& p, const BigInteger& q, int e = DEFAULT_PUBLIC_EXPONENT) : - m_p(p), - m_q(q), - m_e(e) - { - if (p == q || p == 0 || q == 0) { - throw std::invalid_argument("p and q must be prime numbers unique to each other"); - } + inline BigInteger n() const { return m_n; } + inline int e() const { return m_e; } - const BigInteger pMinus1 = m_p - 1; - const BigInteger qMinus1 = m_q - 1; - const BigInteger phi = pMinus1 * qMinus1; +private: + BigInteger m_n; + int m_e; +}; - if (gcd(m_e, phi) != 1) { - throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); - } - m_n = m_p * m_q; - m_coeff = modInverse(m_q, m_p); +class RawKey { +public: + RawKey(const BigInteger& p, const BigInteger& q, int e = DEFAULT_PUBLIC_EXPONENT); - m_d = modInverse(m_e, phi); + virtual ~RawKey() = default; - // note: - // https://www.ipa.go.jp/security/rfc/RFC3447EN.html#2 says to use m_e - // openssl says to use m_d - m_dp = BigInteger(m_d) % pMinus1; - m_dq = BigInteger(m_d) % qMinus1; - } + inline BigInteger p() const { return m_p; } + inline BigInteger q() const { return m_q; } + inline BigInteger coeff() const { return m_coeff; } + inline BigInteger n() const { return m_n; } + inline int e() const { return m_e; } + inline BigInteger d() const { return m_d; } + inline BigInteger dp() const { return m_dq; } + inline BigInteger dq() const { return m_dp; } - virtual ~RawKey() = default; - - inline BigInteger p() const { return m_p; } - inline BigInteger q() const { return m_q; } - inline BigInteger coeff() const { return m_coeff; } - inline BigInteger n() const { return m_n; } - inline int e() const { return m_e; } - inline BigInteger d() const { return m_d; } - inline BigInteger dp() const { return m_dq; } - inline BigInteger dq() const { return m_dp; } - - friend std::ostream& operator<<(std::ostream& ss, const RawKey& k) - { - ss << "modulus: " << k.m_n << "\npublicExponent: " << k.m_e << "\nprivateExponent: " << k.m_d - << "\nprime1: " << k.m_p << "\nprime2: " << k.m_q << "\nexponent1: " << k.m_dp << "\nexponent2: " - << k.m_dq << "\ncoefficient: " << k.m_coeff; - return ss; - } +private: + BigInteger m_p; + BigInteger m_q; + int m_e; + BigInteger m_coeff; + BigInteger m_n; + BigInteger m_d; + BigInteger m_dp; + BigInteger m_dq; +}; - std::string exportDER() const - { - std::stringstream ss; - ss << "asn1=SEQUENCE:rsa_key\n\n"; - ss << "[rsa_key]\n"; - ss << "version=INTEGER:0\n"; - ss << "modulus=INTEGER:" << bigIntegerToString(m_n) << "\n"; - ss << "pubExp=INTEGER:" << m_e << "\n"; - ss << "privExp=INTEGER:" << bigIntegerToString(m_d) << "\n"; - ss << "p=INTEGER:" << bigIntegerToString(m_p) << "\n"; - ss << "q=INTEGER:" << bigIntegerToString(m_q) << "\n"; - ss << "e1=INTEGER:" << bigIntegerToString(m_dp) << "\n"; - ss << "e2=INTEGER:" << bigIntegerToString(m_dq) << "\n"; - ss << "coeff=INTEGER:" << bigIntegerToString(m_coeff); - return ss.str(); - } +typedef RawKey PrivateKey; - private: - BigInteger m_p; - BigInteger m_q; - int m_e; - BigInteger m_coeff; - BigInteger m_n; - BigInteger m_d; - BigInteger m_dp; - BigInteger m_dq; - }; - - typedef RawKey PrivateKey; - - class KeyPair : public RawKey { - public: - KeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = DEFAULT_PUBLIC_EXPONENT) : - RawKey(p, q, exp) { - m_publicKey = PublicKey(n(), e()); - } +class KeyPair : public RawKey { +public: + KeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = DEFAULT_PUBLIC_EXPONENT); - inline const PublicKey* publicKey() const { return &m_publicKey; } - inline const PrivateKey* privateKey() const { return this; } + inline const PublicKey* publicKey() const { return &m_publicKey; } + inline const PrivateKey* privateKey() const { return this; } - private: - PublicKey m_publicKey; - }; +private: + PublicKey m_publicKey; +}; +/// +/// \brief Provides RSA crypto functionalities +/// +class RSA { +public: /// /// \brief Generic RSA encryption. T can of std::string or std::wstring @@ -153,7 +99,7 @@ class RSA { /// \return hex of final octet string /// template - static std::string encrypt(const PublicKey* publicKey, const T& m) + std::string encrypt(const PublicKey* publicKey, const T& m) { BigInteger paddedMsg = pkcs1pad2(m, (countBits(publicKey->n()) + 7) >> 3); // TODO: It can be made better @@ -169,7 +115,7 @@ class RSA { /// /// \return hex of cipher. Padded using PKCS#1 padding scheme /// - static std::string encrypt(const PublicKey* publicKey, + std::string encrypt(const PublicKey* publicKey, const std::wstring& message) { return encrypt(publicKey, message); @@ -180,7 +126,7 @@ class RSA { /// /// \return hex of cipher. Padded using PKCS#1 padding scheme /// - static std::string encrypt(const PublicKey* publicKey, + std::string encrypt(const PublicKey* publicKey, const std::string& message) { return encrypt(publicKey, message); @@ -192,7 +138,7 @@ class RSA { /// \return Plain text, return type depends on TResult /// template - static TResult decrypt(const PrivateKey* privateKey, const std::string& cipher) + TResult decrypt(const PrivateKey* privateKey, const std::string& cipher) { // TODO: Add checks https://tools.ietf.org/html/rfc3447#section-7.2.2 @@ -217,7 +163,7 @@ class RSA { /// \param signature Signature in hex /// \see https://tools.ietf.org/html/rfc3447#section-8.1.2 /// - static bool verify(const PublicKey* publicKey, const std::string& message, + bool verify(const PublicKey* publicKey, const std::string& message, const std::string& signature) { /* @@ -280,15 +226,24 @@ class RSA { return true; } + /// + /// \brief Singleton instance + /// + inline static RSA& instance() + { + static RSA s_instance; + return s_instance; + } + private: - RSA() = delete; - RSA(const RSA&) = delete; + RSA() = default; + RSA(const RSA&) = default; RSA& operator=(const RSA&) = delete; /// /// \brief Octet string to integer /// - static BigInteger os2ip(const BigInteger& x) + inline BigInteger os2ip(const BigInteger& x) { return os2ip(getByteArray(x)); } @@ -297,7 +252,7 @@ class RSA { /// Octet-string to integer /// template - static BigInteger os2ip(const std::vector& x) + BigInteger os2ip(const std::vector& x) { const BigInteger b256 = 256; @@ -312,7 +267,7 @@ class RSA { /// /// \brief Integer to octet-string /// - static BigInteger i2osp(const BigInteger& x, int xlen) + BigInteger i2osp(const BigInteger& x, int xlen) { return i2osp(getByteArray(x), xlen); } @@ -321,7 +276,7 @@ class RSA { /// \brief Integer to octet-string /// template - static BigInteger i2osp(const std::vector& x, int xlen) + BigInteger i2osp(const std::vector& x, int xlen) { const BigInteger b256 = 256; @@ -339,7 +294,7 @@ class RSA { /// \brief Get byte array from big integer /// template - static std::vector getByteArray(BigInteger x, int xlen = -1) + std::vector getByteArray(BigInteger x, int xlen = -1) { const BigInteger b256 = 256; xlen = xlen == -1 ? countBits(x) * 8 : xlen; @@ -353,7 +308,7 @@ class RSA { q = x / e; r = x % e; //x.Divide(r, q, x, power(b256, BigInteger(xlen - i))); - ba[i - 1] = static_cast(q/*.ConvertToLong()*/); // todo: Check! + ba[i - 1] = static_cast(q.ConvertToLong()); // todo: Check! x = r; } return ba; @@ -365,14 +320,8 @@ class RSA { /// \return message representative, an integer between 0 and n - 1 /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 /// - static BigInteger createVerificationPrimitive(const PublicKey* publicKey, - const BigInteger& signature) - { - if (signature < 0 || signature > publicKey->n() - 1) { - throw std::runtime_error("signature representative out of range"); - } - return powerMod(signature, publicKey->e(), publicKey->n()); - } + BigInteger createVerificationPrimitive(const PublicKey* publicKey, + const BigInteger& signature); /// /// \brief PKCS #1 padding @@ -380,9 +329,9 @@ class RSA { /// \return corresponding nonnegative integer /// template - static BigInteger pkcs1pad2(const T& s, int n) { + BigInteger pkcs1pad2(const T& s, int n) { if (n < s.size() + 11) { - throw std::runtime_error("Message too long"); // TODO: Remove this comment + throw std::runtime_error("Message too long"); } std::vector byteArray(n); long long i = s.size() - 1; @@ -430,7 +379,7 @@ class RSA { /// \return corresponding octet string of length n /// template - static T pkcs1unpad2(const BigInteger& m, unsigned long n) + T pkcs1unpad2(const BigInteger& m, unsigned long n) { std::vector ba = getByteArray(m, n); std::size_t baLen = ba.size(); @@ -478,16 +427,7 @@ class RSA { /// \brief Fast GCD /// \see https://en.wikipedia.org/wiki/Euclidean_algorithm#Extended_Euclidean_algorithm /// - static BigInteger gcd(BigInteger a, BigInteger b) - { - BigInteger c; - while (a != 0) { - c = a; - a = b % a; - b = c; - } - return b; - } + BigInteger gcd(BigInteger a, BigInteger b); /// /// \brief Simple (base ^ e) mod m implementation @@ -495,101 +435,21 @@ class RSA { /// \param e Exponent /// \param m Mod /// - static BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) - { - BigInteger res = 1; - while (e > 0) { - if (e % 2 != 0) { - res = (b * res) % m; - } - b = (b * b) % m; - e /= 2; - } - return res; - } + BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m); /// /// \brief Power of numb i.e, b ^ e /// - template - static T power(T b, T e) - { - T result = 1; - while (e > 0) { - if (e % 2 == 1) { - // we decrement exponent to make it even - e--; - // store this multiplication directly to the - // result - result *= b; - // we modify this alg to ignore the next multiplication - // if we have already reached 0 (for speed) - // here are details and what we changed and how it all works - // - // Let's say we have case of 2 ^ 4 [expected answer = 16] - // 2 ^ 4 -- b = 4, e = 2 [result = 1] - // 2 ^ 2 -- b = 16, e = 1 [result = 1] - // 2 ^ 1 -- e = 0 [result = 1 * 16] - // - // here is what we changed here - // now we have result set and we have e set to zero - // doing another b ^= b means b = 16 * 16 = 256 (in our case) - // which is useless so we end here - if (e == 0) { - break; - } - } - e /= 2; - b *= b; - } - return result; - } + BigInteger power(BigInteger b, BigInteger e); - static BigInteger modInverse(BigInteger a, BigInteger b) - { - BigInteger b0 = b, t, q; - BigInteger x0 = 0, x1 = 1; - if (b == 1) { - return 1; - } - while (a > 1) { - q = a / b; - t = b; - b = a % b; - a = t; - t = x0; - x0 = x1 - q * x0; - x1 = t; - } - if (x1 < 0) { - x1 += b0; - } - return x1; - } + BigInteger modInverse(BigInteger a, BigInteger b); /// /// \brief Checks whether n is prime or not /// This is fast, see https://en.wikipedia.org/wiki/Primality_test#Pseudocode /// for details /// - static bool isPrime(BigInteger n) - { - if (n <= 1) { - return false; - } - if (n <= 3) { - return true; - } - if (n % 2 == 0 || n % 3 == 0) { - return false; - } - for (BigInteger i = 5; i * i <= n; i += 6) { - if (n % i == 0 || n % (i + 2) == 0) { - return false; - } - } - return true; - } + bool isPrime(BigInteger n); /// /// \brief Specific base to specified base @@ -597,7 +457,7 @@ class RSA { /// \param b Target base (default: 16 - Hex) /// template - static T changeBase(T n, T b = 16) + T changeBase(T n, T b = 16) { T r, i = 1, o = 0; while (n != 0) { @@ -613,25 +473,11 @@ class RSA { /// \brief Big integer adds suffix at the end so we use this function /// to remove it /// - static std::string bigIntegerToString(const BigInteger& b) - { - std::stringstream ss; - ss << b; - std::string sss(ss.str()); - sss.erase(sss.end() - 1); - return sss; - } + std::string bigIntegerToString(const BigInteger& b); - static unsigned int countBits(BigInteger b) - { - unsigned int bits = 0; - while (b > 0) { - bits++; - b >>= 1; - } - return bits; - } + unsigned int countBits(BigInteger b); + friend class RawKey; // for tests friend class RSATest_Signature_Test; friend class RSATest_Decryption_Test; @@ -643,5 +489,4 @@ class RSA { }; } // end namespace mine - #endif // RSA_H diff --git a/src/zlib.cc b/src/zlib.cc new file mode 100644 index 0000000..2d4dcb2 --- /dev/null +++ b/src/zlib.cc @@ -0,0 +1,16 @@ +// +// zlib.cc +// Part of Mine crypto library +// +// You should not use this file, use include/mine.cc +// instead which is automatically generated and includes this file +// This is seperated to aid the development +// +// Copyright 2017 Muflihun Labs +// +// https://github.com/muflihun/mine +// + +#include "src/zlib.h" + +using namespace mine; diff --git a/test/rsa-test.h b/test/rsa-test.h index 8be9fab..26053be 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -5,8 +5,9 @@ #include "test.h" namespace mine { + // numb, expected -static TestData IsPrimeData = { +static TestData IsPrimeData = { TestCase(1, false), TestCase(2, true), TestCase(44, false), @@ -25,7 +26,7 @@ static TestData InvModuloData = { }; // b, e, m, exp -static TestData PowerModData = { +static TestData PowerModData = { TestCase(5, 3, 1, 0), TestCase(5, 3, 19, 11), TestCase(3, 11, 4, 3), @@ -43,50 +44,49 @@ static TestData GCDData = { }; // p, q, d, e -static TestData RawKeyData = { - /*TestCase(173, 149, 16971, 3), - TestCase(7, 11, 53, RSA::KeyPair::DEFAULT_PUBLIC_EXPONENT), +static TestData RawKeyData = { + TestCase(173, 149, 16971, 3), + TestCase(7, 11, 53, DEFAULT_PUBLIC_EXPONENT), TestCase(53, 59, 2011, 3), - TestCase(3, 11, 13, RSA::KeyPair::DEFAULT_PUBLIC_EXPONENT), - TestCase(11, 3, 13, RSA::KeyPair::DEFAULT_PUBLIC_EXPONENT), + TestCase(3, 11, 13, DEFAULT_PUBLIC_EXPONENT), + TestCase(11, 3, 13, DEFAULT_PUBLIC_EXPONENT), TestCase(11, 17, 107, 3), TestCase(60779, 53003, 1986380529, 65537), - TestCase(RSA::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), RSA::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), RSA::BigInteger("6582637129463060155009365989980507690389844347218288265898882119918384609608605061080359962338234258873604135082233881579524605068749537845926158658339195"), 3), // << - this is what we tested based upon - TestCase(RSA::BigInteger("108806323825932706977307191097259117033353572146991115579334232319532442798209"), RSA::BigInteger("75778358732466892022501809496541864532894038434008219546470758430052996329071"), RSA::BigInteger("5496776426161658798940200169671739270887755348701962910881820208997794909110934102331035956003239535747096593580882262107967594097892650413676521849537707"), 3), - TestCase(RSA::BigInteger("176360517760307645469197766454483974235511085138581196179561347493397045582678676376582697316359235034160209749898412011153924577295946180206410049279151818330310786286636241193330444606031071350600285897477381895748147316188740513022461102919987041280831098968434434553879045380055670995086500659087065302403"), RSA::BigInteger("169163723758010117173450277772073715921803592964927638245731997826080549397171438893995388301374973303599758650257957793677462633788535432641753359275162340138639711102283198388259082836510170614304484108899685152902783320622394241970680405511348370667697428176827008839392860840538166537806520720483413747299"), RSA::BigInteger("19889201272149546443463155392252159315174210881947747082976069645146016944350039871470895772510979533532867685298201602992918775321216324576337623180033432704404378220468328792827286239010112541240591233928213472506415790478729110344923198900414497739281740852945910987895868475178794593401701658716065890906852003893420692929407600046549070094684609746581564079903528672418432707811264082243503362255422665241970781850081871999244191153644696361248245946591425338244340405196223004592171521190997670962141503496215757774355742872186005655701283224796723544068646999720522489543729822936326934344869201943856253606531"), 3), -*/ - TestCase(1295159, 104717, 90415843419, 3), + TestCase(BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), BigInteger("6582637129463060155009365989980507690389844347218288265898882119918384609608605061080359962338234258873604135082233881579524605068749537845926158658339195"), 3), // << - this is what we tested based upon + TestCase(BigInteger("108806323825932706977307191097259117033353572146991115579334232319532442798209"), BigInteger("75778358732466892022501809496541864532894038434008219546470758430052996329071"), BigInteger("5496776426161658798940200169671739270887755348701962910881820208997794909110934102331035956003239535747096593580882262107967594097892650413676521849537707"), 3), + TestCase(BigInteger("176360517760307645469197766454483974235511085138581196179561347493397045582678676376582697316359235034160209749898412011153924577295946180206410049279151818330310786286636241193330444606031071350600285897477381895748147316188740513022461102919987041280831098968434434553879045380055670995086500659087065302403"), BigInteger("169163723758010117173450277772073715921803592964927638245731997826080549397171438893995388301374973303599758650257957793677462633788535432641753359275162340138639711102283198388259082836510170614304484108899685152902783320622394241970680405511348370667697428176827008839392860840538166537806520720483413747299"), BigInteger("19889201272149546443463155392252159315174210881947747082976069645146016944350039871470895772510979533532867685298201602992918775321216324576337623180033432704404378220468328792827286239010112541240591233928213472506415790478729110344923198900414497739281740852945910987895868475178794593401701658716065890906852003893420692929407600046549070094684609746581564079903528672418432707811264082243503362255422665241970781850081871999244191153644696361248245946591425338244340405196223004592171521190997670962141503496215757774355742872186005655701283224796723544068646999720522489543729822936326934344869201943856253606531"), 3), }; // msg static TestData RSAEncryptionData = { - TestCase(L"2"), - /*TestCase(L"G'day"), + TestCase(L"1"), + TestCase(L"G'day"), TestCase(L"Hi"), TestCase(L"Hello竜"), TestCase(L"大家好"), - TestCase(L"کیا میں آپکی مدد کر سکتاہوں"),*/ + TestCase(L"کیا میں آپکی مدد کر سکتاہوں"), }; // msg static TestData RSAEncryptionStringData = { - TestCase("2"), + TestCase("1"), + TestCase("Slightly longer text"), }; // p, q, e, cipher, msg -static TestData RSADecryptionData = { - //TestCase(RSA::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), RSA::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "55a5f32084cdbbd3edcba573317f99678a1b85b6c455fa86476d697900ce5fd95ec599a16690d5e7c2196608477ac1006e86c74cbd25b7e4681e026774381e63", L"Apple"), +static TestData RSADecryptionData = { + TestCase(BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "55a5f32084cdbbd3edcba573317f99678a1b85b6c455fa86476d697900ce5fd95ec599a16690d5e7c2196608477ac1006e86c74cbd25b7e4681e026774381e63", L"Apple"), }; // p, q, e, signature, text -static TestData RSASignatureData = { - //TestCase(RSA::BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), RSA::BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "01400ccd971dad2744c37baf7f5cf13a5590a675c90354f2002d4c6a6a7ef3d1377986e1d8f0b69676e243fae8cf6c6bbdc7f18deb0e0418fe6452c4afb1e4b5", "test"), +static TestData RSASignatureData = { + TestCase(BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), 3, "01400ccd971dad2744c37baf7f5cf13a5590a675c90354f2002d4c6a6a7ef3d1377986e1d8f0b69676e243fae8cf6c6bbdc7f18deb0e0418fe6452c4afb1e4b5", "test"), }; TEST(RSATest, FindGCD) { for (const auto& item : GCDData) { LOG(INFO) << "Finding GCD for " << PARAM(0) << " and " << PARAM(1); - ASSERT_EQ(RSA::gcd(PARAM(0), PARAM(1)), PARAM(2)); + ASSERT_EQ(RSA::instance().gcd(PARAM(0), PARAM(1)), PARAM(2)); } } @@ -94,31 +94,31 @@ TEST(RSATest, IsPrime) { for (const auto& item : IsPrimeData) { // LOG(INFO) << "Testing " << PARAM(0) << " == prime: to be " << std::boolalpha << PARAM(1); - ASSERT_EQ(RSA::isPrime(PARAM(0)), PARAM(1)); + ASSERT_EQ(RSA::instance().isPrime(PARAM(0)), PARAM(1)); } } TEST(RSATest, PowerMod) { for (const auto& item : PowerModData) { - ASSERT_EQ(RSA::powerMod(PARAM(0), PARAM(1), PARAM(2)), PARAM(3)); + ASSERT_EQ(RSA::instance().powerMod(PARAM(0), PARAM(1), PARAM(2)), PARAM(3)); } } TEST(RSATest, InvModulo) { for (const auto& item : InvModuloData) { - ASSERT_EQ(RSA::modInverse(PARAM(0), PARAM(1)), PARAM(2)); + ASSERT_EQ(RSA::instance().modInverse(PARAM(0), PARAM(1)), PARAM(2)); } } TEST(RSATest, KeyAndEncryptionDecryption) { for (const auto& item : RawKeyData) { - int bits = RSA::countBits(PARAM(0)) + RSA::countBits(PARAM(1)); + int bits = RSA::instance().countBits(PARAM(0)) + RSA::instance().countBits(PARAM(1)); LOG(INFO) << "Generating key " << bits << "-bit..."; - RSA::KeyPair k(PARAM(0), PARAM(1), PARAM(3)); + KeyPair k(PARAM(0), PARAM(1), PARAM(3)); ASSERT_EQ(k.p(), PARAM(0)); ASSERT_EQ(k.q(), PARAM(1)); ASSERT_EQ(k.d(), PARAM(2)); @@ -129,12 +129,12 @@ TEST(RSATest, KeyAndEncryptionDecryption) for (const auto& item2 : RSAEncryptionData) { std::wstring msg = std::get<0>(item2); if (bits <= 32) { - EXPECT_THROW(RSA::encrypt(k.publicKey(), msg), std::runtime_error); + EXPECT_THROW(RSA::instance().encrypt(k.publicKey(), msg), std::runtime_error); } else { LOG(INFO) << "Plain: " << msg; - std::string encr = RSA::encrypt(k.publicKey(), msg); + std::string encr = RSA::instance().encrypt(k.publicKey(), msg); LOG(INFO) << "Encr: " << encr; - std::wstring decr = RSA::decrypt(k.privateKey(), encr); + std::wstring decr = RSA::instance().decrypt(k.privateKey(), encr); LOG(INFO) << "Decr: " << decr; ASSERT_STREQ(decr.c_str(), msg.c_str()); } @@ -143,12 +143,12 @@ TEST(RSATest, KeyAndEncryptionDecryption) for (const auto& item2 : RSAEncryptionStringData) { std::string msg = std::get<0>(item2); if (bits <= 32) { - EXPECT_THROW(RSA::encrypt(k.publicKey(), msg), std::runtime_error); + EXPECT_THROW(RSA::instance().encrypt(k.publicKey(), msg), std::runtime_error); } else { LOG(INFO) << "Plain: " << msg; - std::string encr = RSA::encrypt(k.publicKey(), msg); + std::string encr = RSA::instance().encrypt(k.publicKey(), msg); LOG(INFO) << "Encr: " << encr; - std::string decr = RSA::decrypt(k.privateKey(), encr); + std::string decr = RSA::instance().decrypt(k.privateKey(), encr); LOG(INFO) << "Decr: " << decr; ASSERT_STREQ(decr.c_str(), msg.c_str()); } @@ -159,18 +159,18 @@ TEST(RSATest, KeyAndEncryptionDecryption) TEST(RSATest, Decryption) { for (const auto& item : RSADecryptionData) { - int bits = RSA::countBits(PARAM(0)) + RSA::countBits(PARAM(1)); + int bits = RSA::instance().countBits(PARAM(0)) + RSA::instance().countBits(PARAM(1)); LOG(INFO) << "Generating key " << bits << "-bit..."; - RSA::KeyPair k(PARAM(0), PARAM(1), PARAM(2)); + KeyPair k(PARAM(0), PARAM(1), PARAM(2)); std::string cipher = PARAM(3); std::wstring expected = PARAM(4); LOG(INFO) << "Testing: " << cipher; if (bits <= 32) { - EXPECT_THROW(RSA::decrypt(k.privateKey(), cipher), std::runtime_error); + EXPECT_THROW(RSA::instance().decrypt(k.privateKey(), cipher), std::runtime_error); } else { - std::wstring decr = RSA::decrypt(k.privateKey(), cipher); + std::wstring decr = RSA::instance().decrypt(k.privateKey(), cipher); ASSERT_STREQ(decr.c_str(), expected.c_str()); } } @@ -178,27 +178,27 @@ TEST(RSATest, Decryption) TEST(RSATest, FakeTest) { - //auto item = RawKeyData.at(7); - //RSA::KeyPair k(PARAM(0), PARAM(1), PARAM(3)); - //std::cout << RSA::encrypt(k.publicKey(), std::string("Test message")) << std::endl; - //std::cout << RSA::decrypt(k.privateKey(), std::string("68A7FE65FBD933522CDD321B0062DBA910AE5C1E73D46F7EB4A26773963963AE59F614D514E75773A8E6B67EACDC7C9F4172A94D58522CBB96FC79A836DB5343")); + auto item = RawKeyData.at(7); + KeyPair k(PARAM(0), PARAM(1), PARAM(3)); + std::cout << RSA::instance().encrypt(k.publicKey(), std::string("Test message")) << std::endl; + std::cout << RSA::instance().decrypt(k.privateKey(), std::string("68A7FE65FBD933522CDD321B0062DBA910AE5C1E73D46F7EB4A26773963963AE59F614D514E75773A8E6B67EACDC7C9F4172A94D58522CBB96FC79A836DB5343")); } TEST(RSATest, Signature) { for (const auto& item : RSASignatureData) { - int bits = RSA::countBits(PARAM(0)) + RSA::countBits(PARAM(1)); + int bits = RSA::instance().countBits(PARAM(0)) + RSA::instance().countBits(PARAM(1)); LOG(INFO) << "Generating key " << bits << "-bit..."; - RSA::KeyPair k(PARAM(0), PARAM(1), PARAM(2)); + KeyPair k(PARAM(0), PARAM(1), PARAM(2)); std::string sign = PARAM(3); std::string text = PARAM(4); LOG(INFO) << "Testing: " << text; if (bits <= 32) { - EXPECT_THROW(RSA::verify(k.publicKey(), text, sign), std::runtime_error); + EXPECT_THROW(RSA::instance().verify(k.publicKey(), text, sign), std::runtime_error); } else { - ASSERT_TRUE(RSA::verify(k.publicKey(),text, sign)); + ASSERT_TRUE(RSA::instance().verify(k.publicKey(),text, sign)); } } } From a4f13bdf84b09341042f4dc9801b7eb96acb6ab6 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Tue, 22 Aug 2017 21:16:25 +1000 Subject: [PATCH 019/148] update to naked rsa --- include/mine.cc | 159 --------------- include/mine.h | 509 +++++++++++++++++++++++++++++++----------------- src/rsa.cc | 159 --------------- src/rsa.h | 507 ++++++++++++++++++++++++++++++----------------- test/rsa-test.h | 33 ++-- 5 files changed, 675 insertions(+), 692 deletions(-) diff --git a/include/mine.cc b/include/mine.cc index 7d9fab8..726601e 100644 --- a/include/mine.cc +++ b/include/mine.cc @@ -21,162 +21,3 @@ using namespace mine; -RawKey::RawKey(const BigInteger &p, const BigInteger &q, int e) : - m_p(p), - m_q(q), - m_e(e) -{ - if (p == q || p == 0 || q == 0) { - throw std::invalid_argument("p and q must be prime numbers unique to each other"); - } - - const BigInteger pMinus1 = m_p - 1; - const BigInteger qMinus1 = m_q - 1; - const BigInteger phi = pMinus1 * qMinus1; - - if (RSA::instance().gcd(m_e, phi) != 1) { - throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); - } - m_n = m_p * m_q; - m_coeff = RSA::instance().modInverse(m_q, m_p); - - m_d = RSA::instance().modInverse(m_e, phi); - - // note: - // https://www.ipa.go.jp/security/rfc/RFC3447EN.html#2 says to use m_e - // openssl says to use m_d - m_dp = BigInteger(m_d) % pMinus1; - m_dq = BigInteger(m_d) % qMinus1; -} - -KeyPair::KeyPair(const BigInteger &p, const BigInteger &q, unsigned int exp) : - RawKey(p, q, exp) { - m_publicKey = PublicKey(n(), e()); -} - -BigInteger RSA::createVerificationPrimitive(const PublicKey *publicKey, const BigInteger &signature) -{ - if (signature < 0 || signature > publicKey->n() - 1) { - throw std::runtime_error("signature representative out of range"); - } - return powerMod(signature, publicKey->e(), publicKey->n()); -} - -BigInteger RSA::gcd(BigInteger a, BigInteger b) -{ - BigInteger c; - while (a != 0) { - c = a; - a = b % a; - b = c; - } - return b; -} - -BigInteger RSA::powerMod(BigInteger b, BigInteger e, BigInteger m) -{ - BigInteger res = 1; - while (e > 0) { - if (e % 2 != 0) { - res = (b * res) % m; - } - b = (b * b) % m; - e /= 2; - } - return res; -} - -BigInteger RSA::power(BigInteger b, BigInteger e) -{ - BigInteger result = 1; - while (e > 0) { - if (e % 2 == 1) { - // we decrement exponent to make it even - e--; - // store this multiplication directly to the - // result - result *= b; - // we modify this alg to ignore the next multiplication - // if we have already reached 0 (for speed) - // here are details and what we changed and how it all works - // - // Let's say we have case of 2 ^ 4 [expected answer = 16] - // 2 ^ 4 -- b = 4, e = 2 [result = 1] - // 2 ^ 2 -- b = 16, e = 1 [result = 1] - // 2 ^ 1 -- e = 0 [result = 1 * 16] - // - // here is what we changed here - // now we have result set and we have e set to zero - // doing another b ^= b means b = 16 * 16 = 256 (in our case) - // which is useless so we end here - if (e == 0) { - break; - } - } - e /= 2; - b *= b; - } - return result; -} - - -BigInteger RSA::modInverse(BigInteger a, BigInteger b) -{ - BigInteger b0 = b, t, q; - BigInteger x0 = 0, x1 = 1; - if (b == 1) { - return 1; - } - while (a > 1) { - q = a / b; - t = b; - b = a % b; - a = t; - t = x0; - x0 = x1 - q * x0; - x1 = t; - } - if (x1 < 0) { - x1 += b0; - } - return x1; -} - -bool RSA::isPrime(BigInteger n) -{ - if (n <= 1) { - return false; - } - if (n <= 3) { - return true; - } - if (n % 2 == 0 || n % 3 == 0) { - return false; - } - for (BigInteger i = 5; i * i <= n; i += 6) { - if (n % i == 0 || n % (i + 2) == 0) { - return false; - } - } - return true; -} - -std::string RSA::bigIntegerToString(const BigInteger &b) -{ - std::stringstream ss; - ss << b; - std::string sss(ss.str()); - sss.erase(sss.end() - 1); - return sss; -} - -unsigned int RSA::countBits(BigInteger b) -{ - unsigned int bits = 0; - while (b > 0) { - bits++; - b >>= 1; - } - return bits; -} - diff --git a/include/mine.h b/include/mine.h index 1c4c668..cdd8c56 100644 --- a/include/mine.h +++ b/include/mine.h @@ -23,7 +23,6 @@ #include #include #include -#include namespace mine { @@ -63,35 +62,279 @@ class AES { AES& operator=(const AES&) = delete; }; -typedef CryptoPP::Integer BigInteger; +// Here onwards start implementation for RSA - this contains +// naked classes and need implementation for Big Integer +// once available you can use DECLARE_MINE_RSA(BIG_INTEGER) macro +// to declare -static const unsigned int DEFAULT_PUBLIC_EXPONENT = 65537; +/// +/// \brief Default exponent for RSA public key +/// +static const unsigned int kDefaultPublicExponent = 65537; + +/// +/// Declaration for byte in case it's not already included +/// +using byte = unsigned char; + +/// +/// \brief Contains helper functions for RSA throughout +/// +template +class VirtualHelper { +public: + + /// + /// \brief Implementation for (a ^ -1) mod b + /// + static BigInteger modInverse(BigInteger a, BigInteger b) + { + BigInteger b0 = b, t, q; + BigInteger x0 = 0, x1 = 1; + if (b == 1) { + return 1; + } + while (a > 1) { + q = a / b; + t = b; + b = a % b; + a = t; + t = x0; + x0 = x1 - q * x0; + x1 = t; + } + if (x1 < 0) { + x1 += b0; + } + return x1; + } + + /// + /// \brief Fast GCD + /// \see https://en.wikipedia.org/wiki/Euclidean_algorithm#Extended_Euclidean_algorithm + /// + static BigInteger gcd(BigInteger a, BigInteger b) + { + BigInteger c; + while (a != 0) { + c = a; + a = b % a; + b = c; + } + return b; + } + + /// + /// \brief Octet string to integer + /// + static inline BigInteger os2ip(const BigInteger& x) + { + return os2ip(getByteArray(x)); + } + + /// + /// Octet-string to integer + /// + template + static BigInteger os2ip(const std::vector& x) + { + const BigInteger b256 = 256; + + BigInteger result = 0; + std::size_t len = x.size(); + for (std::size_t i = len; i > 0; --i) { + result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); + } + return result; + } + + /// + /// \brief Integer to octet-string + /// + static BigInteger i2osp(const BigInteger& x, int xlen) + { + return i2osp(getByteArray(x), xlen); + } + + /// + /// \brief Integer to octet-string + /// + template + static BigInteger i2osp(const std::vector& x, int xlen) + { + const BigInteger b256 = 256; + + BigInteger result = 0; + std::size_t len = x.size(); + for (std::size_t i = len; i > 0; --i) { + result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); + } + // TODO: Fix this! + return result; + } + + /// + /// \brief Specific base to specified base + /// \param n Number + /// \param b Target base (default: 16 - Hex) + /// + template + static T changeBase(T n, T b = 16) + { + T r, i = 1, o = 0; + while (n != 0) { + r = n % b; + n /= b; + o += r * i; + i *= 10; + } + return o; + } + + /// + /// \brief Simple (b ^ e) mod m implementation + /// \param b Base + /// \param e Exponent + /// \param m Mod + /// + static BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) + { + BigInteger res = 1; + while (e > 0) { + if (e % 2 != 0) { + res = (b * res) % m; + } + b = (b * b) % m; + e /= 2; + } + return res; + } + + /// + /// \brief Power of numb i.e, b ^ e + /// + static BigInteger power(BigInteger b, BigInteger e) + { + BigInteger result = 1; + while (e > 0) { + if (e % 2 == 1) { + // we decrement exponent to make it even + e--; + // store this multiplication directly to the + // result + result *= b; + // we modify this alg to ignore the next multiplication + // if we have already reached 0 (for speed) + // here are details and what we changed and how it all works + // + // Let's say we have case of 2 ^ 4 [expected answer = 16] + // 2 ^ 4 -- b = 4, e = 2 [result = 1] + // 2 ^ 2 -- b = 16, e = 1 [result = 1] + // 2 ^ 1 -- e = 0 [result = 1 * 16] + // + // here is what we changed here + // now we have result set and we have e set to zero + // doing another b ^= b means b = 16 * 16 = 256 (in our case) + // which is useless so we end here + if (e == 0) { + break; + } + } + e /= 2; + b *= b; + } + return result; + } + + static unsigned int countBits(BigInteger b) + { + unsigned int bits = 0; + while (b > 0) { + bits++; + b >>= 1; + } + return bits; + } + + /// + /// \brief Get byte array from big integer + /// + template + static std::vector getByteArray(BigInteger x, int xlen = -1) + { + const BigInteger b256 = 256; + xlen = xlen == -1 ? countBits(x) * 8 : xlen; + + std::vector ba(xlen); + BigInteger r; + BigInteger q; + + for (int i = 1; i <= xlen; ++i) { + BigInteger e = power(b256, BigInteger(xlen - i)); + q = x / e; + r = x % e; + ba[i - 1] = static_cast(q.ConvertToLong()); + x = r; + } + return ba; + } +}; -class PublicKey { +template +class VirtualPublicKey { public: - PublicKey() = default; + VirtualPublicKey() = default; - PublicKey(BigInteger n, int e) : + VirtualPublicKey(BigInteger n, int e) : m_n(n), m_e(e) { } - virtual ~PublicKey() = default; + virtual ~VirtualPublicKey() = default; inline BigInteger n() const { return m_n; } inline int e() const { return m_e; } -private: +protected: BigInteger m_n; int m_e; }; -class RawKey { +template +class VirtualRawKey { public: - RawKey(const BigInteger& p, const BigInteger& q, int e = DEFAULT_PUBLIC_EXPONENT); - - virtual ~RawKey() = default; + using Helper = VirtualHelper; + + VirtualRawKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) : + m_p(p), + m_q(q), + m_e(e) + { + if (p == q || p == 0 || q == 0) { + throw std::invalid_argument("p and q must be prime numbers unique to each other"); + } + + const BigInteger pMinus1 = m_p - 1; + const BigInteger qMinus1 = m_q - 1; + const BigInteger phi = pMinus1 * qMinus1; + + if (Helper::gcd(m_e, phi) != 1) { + throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); + } + m_n = m_p * m_q; + m_coeff = Helper::modInverse(m_q, m_p); + + m_d = Helper::modInverse(m_e, phi); + + // note: + // https://www.ipa.go.jp/security/rfc/RFC3447EN.html#2 says to use m_e + // openssl says to use m_d + m_dp = BigInteger(m_d) % pMinus1; + m_dq = BigInteger(m_d) % qMinus1; + } + + virtual ~VirtualRawKey() = default; inline BigInteger p() const { return m_p; } inline BigInteger q() const { return m_q; } @@ -102,7 +345,7 @@ class RawKey { inline BigInteger dp() const { return m_dq; } inline BigInteger dq() const { return m_dp; } -private: +protected: BigInteger m_p; BigInteger m_q; int m_e; @@ -113,24 +356,33 @@ class RawKey { BigInteger m_dq; }; -typedef RawKey PrivateKey; +template +using VirtualPrivateKey = VirtualRawKey ; -class KeyPair : public RawKey { +template +class VirtualKeyPair : public VirtualRawKey { public: - KeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = DEFAULT_PUBLIC_EXPONENT); + VirtualKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) : + VirtualRawKey(p, q, exp) { + m_publicKey = VirtualPublicKey(p * q, exp); + } - inline const PublicKey* publicKey() const { return &m_publicKey; } - inline const PrivateKey* privateKey() const { return this; } + inline const VirtualPublicKey* publicKey() const { return &m_publicKey; } + inline const VirtualPrivateKey* privateKey() const { return this; } -private: - PublicKey m_publicKey; +protected: + VirtualPublicKey m_publicKey; }; + /// /// \brief Provides RSA crypto functionalities /// -class RSA { +template +class VirtualRSA { public: + using Helper = VirtualHelper; + /// /// \brief Generic RSA encryption. T can of std::string or std::wstring /// or custom similar type @@ -138,12 +390,11 @@ class RSA { /// \return hex of final octet string /// template - std::string encrypt(const PublicKey* publicKey, const T& m) + std::string encrypt(const VirtualPublicKey* publicKey, const T& m) { - BigInteger paddedMsg = pkcs1pad2(m, (countBits(publicKey->n()) + 7) >> 3); - // TODO: It can be made better + BigInteger paddedMsg = pkcs1pad2(m, (Helper::countBits(publicKey->n()) + 7) >> 3); std::stringstream ss; - ss << std::hex << powerMod(paddedMsg, publicKey->e(), publicKey->n()); + ss << std::hex << Helper::powerMod(paddedMsg, publicKey->e(), publicKey->n()); std::string h(ss.str()); h.erase(h.end() - 1); return ((h.size() & 1) == 0) ? h : ("0" + h); @@ -154,7 +405,7 @@ class RSA { /// /// \return hex of cipher. Padded using PKCS#1 padding scheme /// - std::string encrypt(const PublicKey* publicKey, + std::string encrypt(const VirtualPublicKey* publicKey, const std::wstring& message) { return encrypt(publicKey, message); @@ -165,7 +416,7 @@ class RSA { /// /// \return hex of cipher. Padded using PKCS#1 padding scheme /// - std::string encrypt(const PublicKey* publicKey, + std::string encrypt(const VirtualPublicKey* publicKey, const std::string& message) { return encrypt(publicKey, message); @@ -177,7 +428,7 @@ class RSA { /// \return Plain text, return type depends on TResult /// template - TResult decrypt(const PrivateKey* privateKey, const std::string& cipher) + TResult decrypt(const VirtualPrivateKey* privateKey, const std::string& cipher) { // TODO: Add checks https://tools.ietf.org/html/rfc3447#section-7.2.2 @@ -188,11 +439,11 @@ class RSA { iss >> std::hex >> msg; // https://tools.ietf.org/html/rfc3447#section-4.1 - int xlen = (countBits(privateKey->n()) + 7) >> 3; - if (msg >= power(BigInteger(256), BigInteger(xlen))) { + int xlen = (Helper::countBits(privateKey->n()) + 7) >> 3; + if (msg >= Helper::power(BigInteger(256), BigInteger(xlen))) { throw std::runtime_error("Integer too large"); } - BigInteger decr = powerMod(msg, privateKey->d(), privateKey->n()); + BigInteger decr = Helper::powerMod(msg, privateKey->d(), privateKey->n()); return pkcs1unpad2(decr, xlen); } @@ -202,7 +453,7 @@ class RSA { /// \param signature Signature in hex /// \see https://tools.ietf.org/html/rfc3447#section-8.1.2 /// - bool verify(const PublicKey* publicKey, const std::string& message, + bool verify(const VirtualPublicKey* publicKey, const std::string& message, const std::string& signature) { /* @@ -268,99 +519,16 @@ class RSA { /// /// \brief Singleton instance /// - inline static RSA& instance() + inline static VirtualRSA& instance() { - static RSA s_instance; + static VirtualRSA s_instance; return s_instance; } private: - RSA() = default; - RSA(const RSA&) = default; - RSA& operator=(const RSA&) = delete; - - /// - /// \brief Octet string to integer - /// - inline BigInteger os2ip(const BigInteger& x) - { - return os2ip(getByteArray(x)); - } - - /// - /// Octet-string to integer - /// - template - BigInteger os2ip(const std::vector& x) - { - const BigInteger b256 = 256; - - BigInteger result = 0; - std::size_t len = x.size(); - for (std::size_t i = len; i > 0; --i) { - result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); - } - return result; - } - - /// - /// \brief Integer to octet-string - /// - BigInteger i2osp(const BigInteger& x, int xlen) - { - return i2osp(getByteArray(x), xlen); - } - - /// - /// \brief Integer to octet-string - /// - template - BigInteger i2osp(const std::vector& x, int xlen) - { - const BigInteger b256 = 256; - - BigInteger result = 0; - std::size_t len = x.size(); - for (std::size_t i = len; i > 0; --i) { - result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); - } - // TODO: Fix this! - - return result; - } - - /// - /// \brief Get byte array from big integer - /// - template - std::vector getByteArray(BigInteger x, int xlen = -1) - { - const BigInteger b256 = 256; - xlen = xlen == -1 ? countBits(x) * 8 : xlen; - - std::vector ba(xlen); - BigInteger r; - BigInteger q; - - for (int i = 1; i <= xlen; ++i) { - BigInteger e = power(b256, BigInteger(xlen - i)); - q = x / e; - r = x % e; - //x.Divide(r, q, x, power(b256, BigInteger(xlen - i))); - ba[i - 1] = static_cast(q.ConvertToLong()); // todo: Check! - x = r; - } - return ba; - } - - /// - /// \brief Creates RSA VP for verification - /// \param signature signature representative, an integer between 0 and n - 1 - /// \return message representative, an integer between 0 and n - 1 - /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 - /// - BigInteger createVerificationPrimitive(const PublicKey* publicKey, - const BigInteger& signature); + VirtualRSA() = default; + VirtualRSA(const VirtualRSA&) = default; + VirtualRSA& operator=(const VirtualRSA&) = delete; /// /// \brief PKCS #1 padding @@ -370,7 +538,7 @@ class RSA { template BigInteger pkcs1pad2(const T& s, int n) { if (n < s.size() + 11) { - throw std::runtime_error("Message too long"); // TODO: Remove this comment + throw std::runtime_error("Message too long"); } std::vector byteArray(n); long long i = s.size() - 1; @@ -409,7 +577,7 @@ class RSA { // first two bytes of padding are 0x2 (second) and 0x0 (first) byteArray[--n] = 2; byteArray[--n] = 0; - return i2osp(byteArray, n); + return Helper::i2osp(byteArray, n); } /// @@ -420,7 +588,7 @@ class RSA { template T pkcs1unpad2(const BigInteger& m, unsigned long n) { - std::vector ba = getByteArray(m, n); + std::vector ba = Helper::getByteArray(m, n); std::size_t baLen = ba.size(); if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { throw std::runtime_error("Incorrect padding PKCS#1"); @@ -429,10 +597,7 @@ class RSA { // lets check for the // if we hit end while still we're still with non-zeros, it's a padding error - // 0x0 (done) - // 0x2 (done) - // - // 0x0 + // 0x0 (done) | 0x2 (done) | | 0x0 while (ba[i] != 0) { if (++i >= baLen) { // already ended! throw std::runtime_error("Incorrect padding PKCS#1"); @@ -463,70 +628,56 @@ class RSA { } /// - /// \brief Fast GCD - /// \see https://en.wikipedia.org/wiki/Euclidean_algorithm#Extended_Euclidean_algorithm - /// - BigInteger gcd(BigInteger a, BigInteger b); - - /// - /// \brief Simple (base ^ e) mod m implementation - /// \param b Base - /// \param e Exponent - /// \param m Mod - /// - BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m); - - /// - /// \brief Power of numb i.e, b ^ e - /// - BigInteger power(BigInteger b, BigInteger e); - - BigInteger modInverse(BigInteger a, BigInteger b); - - /// - /// \brief Checks whether n is prime or not - /// This is fast, see https://en.wikipedia.org/wiki/Primality_test#Pseudocode - /// for details - /// - bool isPrime(BigInteger n); - - /// - /// \brief Specific base to specified base - /// \param n Number - /// \param b Target base (default: 16 - Hex) + /// \brief Creates RSA VP for verification + /// \param signature signature representative, an integer between 0 and n - 1 + /// \return message representative, an integer between 0 and n - 1 + /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 /// - template - T changeBase(T n, T b = 16) + BigInteger createVerificationPrimitive(const VirtualPublicKey *publicKey, const BigInteger &signature) { - T r, i = 1, o = 0; - while (n != 0) { - r = n % b; - n /= b; - o += r * i; - i *= 10; + if (signature < 0 || signature > publicKey->n() - 1) { + throw std::runtime_error("signature representative out of range"); } - return o; + return powerMod(signature, publicKey->e(), publicKey->n()); } - /// - /// \brief Big integer adds suffix at the end so we use this function - /// to remove it - /// - std::string bigIntegerToString(const BigInteger& b); - - unsigned int countBits(BigInteger b); - - friend class RawKey; // for tests friend class RSATest_Signature_Test; friend class RSATest_Decryption_Test; friend class RSATest_KeyAndEncryptionDecryption_Test; - friend class RSATest_IsPrime_Test; - friend class RSATest_FindGCD_Test; - friend class RSATest_InvModulo_Test; friend class RSATest_PowerMod_Test; }; +/// +/// A macro that you can use to use specific version of big integer +/// implementation. +/// +/// Big integer that is used here must have following public functions +/// operator-() [subtraction] +/// operator+() [addition] +/// operator+=() [short-hand addition] +/// operator*() [multiply] +/// operator/() [divide] +/// operator%() [mod] +/// operator>>() [right-shift] +/// operator>>=() [short-hand right-shift] +/// long ConvertToLong() +/// +#define DECLARE_MINE_RSA(BIG_INTEGER_IMPLEMENTATION) \ +using BigInteger = BIG_INTEGER_IMPLEMENTATION; \ +class Helper : public VirtualHelper{}; \ +class RSA : public VirtualRSA{}; \ +class KeyPair : public VirtualKeyPair{ \ +public: \ + KeyPair(const BigInteger& p, const BigInteger& q, \ + unsigned int exp = kDefaultPublicExponent) : \ + VirtualKeyPair(p, q, exp) {} \ +}; \ +class RawKey : public VirtualRawKey{}; \ +class PublicKey : public VirtualPublicKey{}; \ +class PrivateKey : public VirtualPrivateKey{}; \ + + /// /// \brief Provides Zlib functionality for inflate and deflate /// diff --git a/src/rsa.cc b/src/rsa.cc index 282a1f1..a55f7a4 100644 --- a/src/rsa.cc +++ b/src/rsa.cc @@ -14,162 +14,3 @@ #include "src/rsa.h" using namespace mine; - -RawKey::RawKey(const BigInteger &p, const BigInteger &q, int e) : - m_p(p), - m_q(q), - m_e(e) -{ - if (p == q || p == 0 || q == 0) { - throw std::invalid_argument("p and q must be prime numbers unique to each other"); - } - - const BigInteger pMinus1 = m_p - 1; - const BigInteger qMinus1 = m_q - 1; - const BigInteger phi = pMinus1 * qMinus1; - - if (RSA::instance().gcd(m_e, phi) != 1) { - throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); - } - m_n = m_p * m_q; - m_coeff = RSA::instance().modInverse(m_q, m_p); - - m_d = RSA::instance().modInverse(m_e, phi); - - // note: - // https://www.ipa.go.jp/security/rfc/RFC3447EN.html#2 says to use m_e - // openssl says to use m_d - m_dp = BigInteger(m_d) % pMinus1; - m_dq = BigInteger(m_d) % qMinus1; -} - -KeyPair::KeyPair(const BigInteger &p, const BigInteger &q, unsigned int exp) : - RawKey(p, q, exp) { - m_publicKey = PublicKey(n(), e()); -} - -BigInteger RSA::createVerificationPrimitive(const PublicKey *publicKey, const BigInteger &signature) -{ - if (signature < 0 || signature > publicKey->n() - 1) { - throw std::runtime_error("signature representative out of range"); - } - return powerMod(signature, publicKey->e(), publicKey->n()); -} - -BigInteger RSA::gcd(BigInteger a, BigInteger b) -{ - BigInteger c; - while (a != 0) { - c = a; - a = b % a; - b = c; - } - return b; -} - -BigInteger RSA::powerMod(BigInteger b, BigInteger e, BigInteger m) -{ - BigInteger res = 1; - while (e > 0) { - if (e % 2 != 0) { - res = (b * res) % m; - } - b = (b * b) % m; - e /= 2; - } - return res; -} - -BigInteger RSA::power(BigInteger b, BigInteger e) -{ - BigInteger result = 1; - while (e > 0) { - if (e % 2 == 1) { - // we decrement exponent to make it even - e--; - // store this multiplication directly to the - // result - result *= b; - // we modify this alg to ignore the next multiplication - // if we have already reached 0 (for speed) - // here are details and what we changed and how it all works - // - // Let's say we have case of 2 ^ 4 [expected answer = 16] - // 2 ^ 4 -- b = 4, e = 2 [result = 1] - // 2 ^ 2 -- b = 16, e = 1 [result = 1] - // 2 ^ 1 -- e = 0 [result = 1 * 16] - // - // here is what we changed here - // now we have result set and we have e set to zero - // doing another b ^= b means b = 16 * 16 = 256 (in our case) - // which is useless so we end here - if (e == 0) { - break; - } - } - e /= 2; - b *= b; - } - return result; -} - - -BigInteger RSA::modInverse(BigInteger a, BigInteger b) -{ - BigInteger b0 = b, t, q; - BigInteger x0 = 0, x1 = 1; - if (b == 1) { - return 1; - } - while (a > 1) { - q = a / b; - t = b; - b = a % b; - a = t; - t = x0; - x0 = x1 - q * x0; - x1 = t; - } - if (x1 < 0) { - x1 += b0; - } - return x1; -} - -bool RSA::isPrime(BigInteger n) -{ - if (n <= 1) { - return false; - } - if (n <= 3) { - return true; - } - if (n % 2 == 0 || n % 3 == 0) { - return false; - } - for (BigInteger i = 5; i * i <= n; i += 6) { - if (n % i == 0 || n % (i + 2) == 0) { - return false; - } - } - return true; -} - -std::string RSA::bigIntegerToString(const BigInteger &b) -{ - std::stringstream ss; - ss << b; - std::string sss(ss.str()); - sss.erase(sss.end() - 1); - return sss; -} - -unsigned int RSA::countBits(BigInteger b) -{ - unsigned int bits = 0; - while (b > 0) { - bits++; - b >>= 1; - } - return bits; -} diff --git a/src/rsa.h b/src/rsa.h index c8b4616..579ea1d 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -20,39 +20,282 @@ #include #include #include -#include namespace mine { -typedef CryptoPP::Integer BigInteger; +// Here onwards start implementation for RSA - this contains +// naked classes and need implementation for Big Integer +// once available you can use DECLARE_MINE_RSA(BIG_INTEGER) macro +// to declare -static const unsigned int DEFAULT_PUBLIC_EXPONENT = 65537; +/// +/// \brief Default exponent for RSA public key +/// +static const unsigned int kDefaultPublicExponent = 65537; + +/// +/// Declaration for byte in case it's not already included +/// +using byte = unsigned char; + +/// +/// \brief Contains helper functions for RSA throughout +/// +template +class VirtualHelper { +public: + + /// + /// \brief Implementation for (a ^ -1) mod b + /// + static BigInteger modInverse(BigInteger a, BigInteger b) + { + BigInteger b0 = b, t, q; + BigInteger x0 = 0, x1 = 1; + if (b == 1) { + return 1; + } + while (a > 1) { + q = a / b; + t = b; + b = a % b; + a = t; + t = x0; + x0 = x1 - q * x0; + x1 = t; + } + if (x1 < 0) { + x1 += b0; + } + return x1; + } + + /// + /// \brief Fast GCD + /// \see https://en.wikipedia.org/wiki/Euclidean_algorithm#Extended_Euclidean_algorithm + /// + static BigInteger gcd(BigInteger a, BigInteger b) + { + BigInteger c; + while (a != 0) { + c = a; + a = b % a; + b = c; + } + return b; + } + + /// + /// \brief Octet string to integer + /// + static inline BigInteger os2ip(const BigInteger& x) + { + return os2ip(getByteArray(x)); + } + + /// + /// Octet-string to integer + /// + template + static BigInteger os2ip(const std::vector& x) + { + const BigInteger b256 = 256; + + BigInteger result = 0; + std::size_t len = x.size(); + for (std::size_t i = len; i > 0; --i) { + result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); + } + return result; + } + + /// + /// \brief Integer to octet-string + /// + static BigInteger i2osp(const BigInteger& x, int xlen) + { + return i2osp(getByteArray(x), xlen); + } + + /// + /// \brief Integer to octet-string + /// + template + static BigInteger i2osp(const std::vector& x, int xlen) + { + const BigInteger b256 = 256; + + BigInteger result = 0; + std::size_t len = x.size(); + for (std::size_t i = len; i > 0; --i) { + result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); + } + // TODO: Fix this! + return result; + } + + /// + /// \brief Specific base to specified base + /// \param n Number + /// \param b Target base (default: 16 - Hex) + /// + template + static T changeBase(T n, T b = 16) + { + T r, i = 1, o = 0; + while (n != 0) { + r = n % b; + n /= b; + o += r * i; + i *= 10; + } + return o; + } + + /// + /// \brief Simple (b ^ e) mod m implementation + /// \param b Base + /// \param e Exponent + /// \param m Mod + /// + static BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) + { + BigInteger res = 1; + while (e > 0) { + if (e % 2 != 0) { + res = (b * res) % m; + } + b = (b * b) % m; + e /= 2; + } + return res; + } + + /// + /// \brief Power of numb i.e, b ^ e + /// + static BigInteger power(BigInteger b, BigInteger e) + { + BigInteger result = 1; + while (e > 0) { + if (e % 2 == 1) { + // we decrement exponent to make it even + e--; + // store this multiplication directly to the + // result + result *= b; + // we modify this alg to ignore the next multiplication + // if we have already reached 0 (for speed) + // here are details and what we changed and how it all works + // + // Let's say we have case of 2 ^ 4 [expected answer = 16] + // 2 ^ 4 -- b = 4, e = 2 [result = 1] + // 2 ^ 2 -- b = 16, e = 1 [result = 1] + // 2 ^ 1 -- e = 0 [result = 1 * 16] + // + // here is what we changed here + // now we have result set and we have e set to zero + // doing another b ^= b means b = 16 * 16 = 256 (in our case) + // which is useless so we end here + if (e == 0) { + break; + } + } + e /= 2; + b *= b; + } + return result; + } + + static unsigned int countBits(BigInteger b) + { + unsigned int bits = 0; + while (b > 0) { + bits++; + b >>= 1; + } + return bits; + } + + /// + /// \brief Get byte array from big integer + /// + template + static std::vector getByteArray(BigInteger x, int xlen = -1) + { + const BigInteger b256 = 256; + xlen = xlen == -1 ? countBits(x) * 8 : xlen; + + std::vector ba(xlen); + BigInteger r; + BigInteger q; + + for (int i = 1; i <= xlen; ++i) { + BigInteger e = power(b256, BigInteger(xlen - i)); + q = x / e; + r = x % e; + ba[i - 1] = static_cast(q.ConvertToLong()); + x = r; + } + return ba; + } +}; -class PublicKey { +template +class VirtualPublicKey { public: - PublicKey() = default; + VirtualPublicKey() = default; - PublicKey(BigInteger n, int e) : + VirtualPublicKey(BigInteger n, int e) : m_n(n), m_e(e) { } - virtual ~PublicKey() = default; + virtual ~VirtualPublicKey() = default; inline BigInteger n() const { return m_n; } inline int e() const { return m_e; } -private: +protected: BigInteger m_n; int m_e; }; -class RawKey { +template +class VirtualRawKey { public: - RawKey(const BigInteger& p, const BigInteger& q, int e = DEFAULT_PUBLIC_EXPONENT); - - virtual ~RawKey() = default; + using Helper = VirtualHelper; + + VirtualRawKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) : + m_p(p), + m_q(q), + m_e(e) + { + if (p == q || p == 0 || q == 0) { + throw std::invalid_argument("p and q must be prime numbers unique to each other"); + } + + const BigInteger pMinus1 = m_p - 1; + const BigInteger qMinus1 = m_q - 1; + const BigInteger phi = pMinus1 * qMinus1; + + if (Helper::gcd(m_e, phi) != 1) { + throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); + } + m_n = m_p * m_q; + m_coeff = Helper::modInverse(m_q, m_p); + + m_d = Helper::modInverse(m_e, phi); + + // note: + // https://www.ipa.go.jp/security/rfc/RFC3447EN.html#2 says to use m_e + // openssl says to use m_d + m_dp = BigInteger(m_d) % pMinus1; + m_dq = BigInteger(m_d) % qMinus1; + } + + virtual ~VirtualRawKey() = default; inline BigInteger p() const { return m_p; } inline BigInteger q() const { return m_q; } @@ -63,7 +306,7 @@ class RawKey { inline BigInteger dp() const { return m_dq; } inline BigInteger dq() const { return m_dp; } -private: +protected: BigInteger m_p; BigInteger m_q; int m_e; @@ -74,24 +317,33 @@ class RawKey { BigInteger m_dq; }; -typedef RawKey PrivateKey; +template +using VirtualPrivateKey = VirtualRawKey ; -class KeyPair : public RawKey { +template +class VirtualKeyPair : public VirtualRawKey { public: - KeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = DEFAULT_PUBLIC_EXPONENT); + VirtualKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) : + VirtualRawKey(p, q, exp) { + m_publicKey = VirtualPublicKey(p * q, exp); + } - inline const PublicKey* publicKey() const { return &m_publicKey; } - inline const PrivateKey* privateKey() const { return this; } + inline const VirtualPublicKey* publicKey() const { return &m_publicKey; } + inline const VirtualPrivateKey* privateKey() const { return this; } -private: - PublicKey m_publicKey; +protected: + VirtualPublicKey m_publicKey; }; + /// /// \brief Provides RSA crypto functionalities /// -class RSA { +template +class VirtualRSA { public: + using Helper = VirtualHelper; + /// /// \brief Generic RSA encryption. T can of std::string or std::wstring /// or custom similar type @@ -99,12 +351,11 @@ class RSA { /// \return hex of final octet string /// template - std::string encrypt(const PublicKey* publicKey, const T& m) + std::string encrypt(const VirtualPublicKey* publicKey, const T& m) { - BigInteger paddedMsg = pkcs1pad2(m, (countBits(publicKey->n()) + 7) >> 3); - // TODO: It can be made better + BigInteger paddedMsg = pkcs1pad2(m, (Helper::countBits(publicKey->n()) + 7) >> 3); std::stringstream ss; - ss << std::hex << powerMod(paddedMsg, publicKey->e(), publicKey->n()); + ss << std::hex << Helper::powerMod(paddedMsg, publicKey->e(), publicKey->n()); std::string h(ss.str()); h.erase(h.end() - 1); return ((h.size() & 1) == 0) ? h : ("0" + h); @@ -115,7 +366,7 @@ class RSA { /// /// \return hex of cipher. Padded using PKCS#1 padding scheme /// - std::string encrypt(const PublicKey* publicKey, + std::string encrypt(const VirtualPublicKey* publicKey, const std::wstring& message) { return encrypt(publicKey, message); @@ -126,7 +377,7 @@ class RSA { /// /// \return hex of cipher. Padded using PKCS#1 padding scheme /// - std::string encrypt(const PublicKey* publicKey, + std::string encrypt(const VirtualPublicKey* publicKey, const std::string& message) { return encrypt(publicKey, message); @@ -138,7 +389,7 @@ class RSA { /// \return Plain text, return type depends on TResult /// template - TResult decrypt(const PrivateKey* privateKey, const std::string& cipher) + TResult decrypt(const VirtualPrivateKey* privateKey, const std::string& cipher) { // TODO: Add checks https://tools.ietf.org/html/rfc3447#section-7.2.2 @@ -149,11 +400,11 @@ class RSA { iss >> std::hex >> msg; // https://tools.ietf.org/html/rfc3447#section-4.1 - int xlen = (countBits(privateKey->n()) + 7) >> 3; - if (msg >= power(BigInteger(256), BigInteger(xlen))) { + int xlen = (Helper::countBits(privateKey->n()) + 7) >> 3; + if (msg >= Helper::power(BigInteger(256), BigInteger(xlen))) { throw std::runtime_error("Integer too large"); } - BigInteger decr = powerMod(msg, privateKey->d(), privateKey->n()); + BigInteger decr = Helper::powerMod(msg, privateKey->d(), privateKey->n()); return pkcs1unpad2(decr, xlen); } @@ -163,7 +414,7 @@ class RSA { /// \param signature Signature in hex /// \see https://tools.ietf.org/html/rfc3447#section-8.1.2 /// - bool verify(const PublicKey* publicKey, const std::string& message, + bool verify(const VirtualPublicKey* publicKey, const std::string& message, const std::string& signature) { /* @@ -229,99 +480,16 @@ class RSA { /// /// \brief Singleton instance /// - inline static RSA& instance() + inline static VirtualRSA& instance() { - static RSA s_instance; + static VirtualRSA s_instance; return s_instance; } private: - RSA() = default; - RSA(const RSA&) = default; - RSA& operator=(const RSA&) = delete; - - /// - /// \brief Octet string to integer - /// - inline BigInteger os2ip(const BigInteger& x) - { - return os2ip(getByteArray(x)); - } - - /// - /// Octet-string to integer - /// - template - BigInteger os2ip(const std::vector& x) - { - const BigInteger b256 = 256; - - BigInteger result = 0; - std::size_t len = x.size(); - for (std::size_t i = len; i > 0; --i) { - result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); - } - return result; - } - - /// - /// \brief Integer to octet-string - /// - BigInteger i2osp(const BigInteger& x, int xlen) - { - return i2osp(getByteArray(x), xlen); - } - - /// - /// \brief Integer to octet-string - /// - template - BigInteger i2osp(const std::vector& x, int xlen) - { - const BigInteger b256 = 256; - - BigInteger result = 0; - std::size_t len = x.size(); - for (std::size_t i = len; i > 0; --i) { - result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); - } - // TODO: Fix this! - - return result; - } - - /// - /// \brief Get byte array from big integer - /// - template - std::vector getByteArray(BigInteger x, int xlen = -1) - { - const BigInteger b256 = 256; - xlen = xlen == -1 ? countBits(x) * 8 : xlen; - - std::vector ba(xlen); - BigInteger r; - BigInteger q; - - for (int i = 1; i <= xlen; ++i) { - BigInteger e = power(b256, BigInteger(xlen - i)); - q = x / e; - r = x % e; - //x.Divide(r, q, x, power(b256, BigInteger(xlen - i))); - ba[i - 1] = static_cast(q.ConvertToLong()); // todo: Check! - x = r; - } - return ba; - } - - /// - /// \brief Creates RSA VP for verification - /// \param signature signature representative, an integer between 0 and n - 1 - /// \return message representative, an integer between 0 and n - 1 - /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 - /// - BigInteger createVerificationPrimitive(const PublicKey* publicKey, - const BigInteger& signature); + VirtualRSA() = default; + VirtualRSA(const VirtualRSA&) = default; + VirtualRSA& operator=(const VirtualRSA&) = delete; /// /// \brief PKCS #1 padding @@ -370,7 +538,7 @@ class RSA { // first two bytes of padding are 0x2 (second) and 0x0 (first) byteArray[--n] = 2; byteArray[--n] = 0; - return i2osp(byteArray, n); + return Helper::i2osp(byteArray, n); } /// @@ -381,7 +549,7 @@ class RSA { template T pkcs1unpad2(const BigInteger& m, unsigned long n) { - std::vector ba = getByteArray(m, n); + std::vector ba = Helper::getByteArray(m, n); std::size_t baLen = ba.size(); if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { throw std::runtime_error("Incorrect padding PKCS#1"); @@ -390,10 +558,7 @@ class RSA { // lets check for the // if we hit end while still we're still with non-zeros, it's a padding error - // 0x0 (done) - // 0x2 (done) - // - // 0x0 + // 0x0 (done) | 0x2 (done) | | 0x0 while (ba[i] != 0) { if (++i >= baLen) { // already ended! throw std::runtime_error("Incorrect padding PKCS#1"); @@ -424,69 +589,55 @@ class RSA { } /// - /// \brief Fast GCD - /// \see https://en.wikipedia.org/wiki/Euclidean_algorithm#Extended_Euclidean_algorithm - /// - BigInteger gcd(BigInteger a, BigInteger b); - - /// - /// \brief Simple (base ^ e) mod m implementation - /// \param b Base - /// \param e Exponent - /// \param m Mod - /// - BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m); - - /// - /// \brief Power of numb i.e, b ^ e - /// - BigInteger power(BigInteger b, BigInteger e); - - BigInteger modInverse(BigInteger a, BigInteger b); - - /// - /// \brief Checks whether n is prime or not - /// This is fast, see https://en.wikipedia.org/wiki/Primality_test#Pseudocode - /// for details - /// - bool isPrime(BigInteger n); - - /// - /// \brief Specific base to specified base - /// \param n Number - /// \param b Target base (default: 16 - Hex) + /// \brief Creates RSA VP for verification + /// \param signature signature representative, an integer between 0 and n - 1 + /// \return message representative, an integer between 0 and n - 1 + /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 /// - template - T changeBase(T n, T b = 16) + BigInteger createVerificationPrimitive(const VirtualPublicKey *publicKey, const BigInteger &signature) { - T r, i = 1, o = 0; - while (n != 0) { - r = n % b; - n /= b; - o += r * i; - i *= 10; + if (signature < 0 || signature > publicKey->n() - 1) { + throw std::runtime_error("signature representative out of range"); } - return o; + return powerMod(signature, publicKey->e(), publicKey->n()); } - /// - /// \brief Big integer adds suffix at the end so we use this function - /// to remove it - /// - std::string bigIntegerToString(const BigInteger& b); - - unsigned int countBits(BigInteger b); - - friend class RawKey; // for tests friend class RSATest_Signature_Test; friend class RSATest_Decryption_Test; friend class RSATest_KeyAndEncryptionDecryption_Test; - friend class RSATest_IsPrime_Test; - friend class RSATest_FindGCD_Test; - friend class RSATest_InvModulo_Test; friend class RSATest_PowerMod_Test; }; + +/// +/// A macro that you can use to use specific version of big integer +/// implementation. +/// +/// Big integer that is used here must have following public functions +/// operator-() [subtraction] +/// operator+() [addition] +/// operator+=() [short-hand addition] +/// operator*() [multiply] +/// operator/() [divide] +/// operator%() [mod] +/// operator>>() [right-shift] +/// operator>>=() [short-hand right-shift] +/// long ConvertToLong() +/// +#define DECLARE_MINE_RSA(BIG_INTEGER_IMPLEMENTATION) \ +using BigInteger = BIG_INTEGER_IMPLEMENTATION; \ +class Helper : public VirtualHelper{}; \ +class RSA : public VirtualRSA{}; \ +class KeyPair : public VirtualKeyPair{ \ +public: \ + KeyPair(const BigInteger& p, const BigInteger& q, \ + unsigned int exp = kDefaultPublicExponent) : \ + VirtualKeyPair(p, q, exp) {} \ +}; \ +class RawKey : public VirtualRawKey{}; \ +class PublicKey : public VirtualPublicKey{}; \ +class PrivateKey : public VirtualPrivateKey{}; \ + } // end namespace mine #endif // RSA_H diff --git a/test/rsa-test.h b/test/rsa-test.h index 26053be..0643350 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -3,9 +3,16 @@ #include "src/rsa.h" #include "test.h" +#include +#include + namespace mine { +using BigInteger = CryptoPP::Integer; + +DECLARE_MINE_RSA(BigInteger) + // numb, expected static TestData IsPrimeData = { TestCase(1, false), @@ -46,10 +53,10 @@ static TestData GCDData = { // p, q, d, e static TestData RawKeyData = { TestCase(173, 149, 16971, 3), - TestCase(7, 11, 53, DEFAULT_PUBLIC_EXPONENT), + TestCase(7, 11, 53, kDefaultPublicExponent), TestCase(53, 59, 2011, 3), - TestCase(3, 11, 13, DEFAULT_PUBLIC_EXPONENT), - TestCase(11, 3, 13, DEFAULT_PUBLIC_EXPONENT), + TestCase(3, 11, 13, kDefaultPublicExponent), + TestCase(11, 3, 13, kDefaultPublicExponent), TestCase(11, 17, 107, 3), TestCase(60779, 53003, 1986380529, 65537), TestCase(BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), BigInteger("6582637129463060155009365989980507690389844347218288265898882119918384609608605061080359962338234258873604135082233881579524605068749537845926158658339195"), 3), // << - this is what we tested based upon @@ -86,36 +93,28 @@ TEST(RSATest, FindGCD) { for (const auto& item : GCDData) { LOG(INFO) << "Finding GCD for " << PARAM(0) << " and " << PARAM(1); - ASSERT_EQ(RSA::instance().gcd(PARAM(0), PARAM(1)), PARAM(2)); - } -} - -TEST(RSATest, IsPrime) -{ - for (const auto& item : IsPrimeData) { - // LOG(INFO) << "Testing " << PARAM(0) << " == prime: to be " << std::boolalpha << PARAM(1); - ASSERT_EQ(RSA::instance().isPrime(PARAM(0)), PARAM(1)); + ASSERT_EQ(Helper::gcd(PARAM(0), PARAM(1)), PARAM(2)); } } TEST(RSATest, PowerMod) { for (const auto& item : PowerModData) { - ASSERT_EQ(RSA::instance().powerMod(PARAM(0), PARAM(1), PARAM(2)), PARAM(3)); + ASSERT_EQ(Helper::powerMod(PARAM(0), PARAM(1), PARAM(2)), PARAM(3)); } } TEST(RSATest, InvModulo) { for (const auto& item : InvModuloData) { - ASSERT_EQ(RSA::instance().modInverse(PARAM(0), PARAM(1)), PARAM(2)); + ASSERT_EQ(Helper::modInverse(PARAM(0), PARAM(1)), PARAM(2)); } } TEST(RSATest, KeyAndEncryptionDecryption) { for (const auto& item : RawKeyData) { - int bits = RSA::instance().countBits(PARAM(0)) + RSA::instance().countBits(PARAM(1)); + int bits = Helper::countBits(PARAM(0)) + Helper::countBits(PARAM(1)); LOG(INFO) << "Generating key " << bits << "-bit..."; KeyPair k(PARAM(0), PARAM(1), PARAM(3)); @@ -159,7 +158,7 @@ TEST(RSATest, KeyAndEncryptionDecryption) TEST(RSATest, Decryption) { for (const auto& item : RSADecryptionData) { - int bits = RSA::instance().countBits(PARAM(0)) + RSA::instance().countBits(PARAM(1)); + int bits = Helper::countBits(PARAM(0)) + Helper::countBits(PARAM(1)); LOG(INFO) << "Generating key " << bits << "-bit..."; KeyPair k(PARAM(0), PARAM(1), PARAM(2)); @@ -187,7 +186,7 @@ TEST(RSATest, FakeTest) TEST(RSATest, Signature) { for (const auto& item : RSASignatureData) { - int bits = RSA::instance().countBits(PARAM(0)) + RSA::instance().countBits(PARAM(1)); + int bits = Helper::countBits(PARAM(0)) + Helper::countBits(PARAM(1)); LOG(INFO) << "Generating key " << bits << "-bit..."; KeyPair k(PARAM(0), PARAM(1), PARAM(2)); From 5df1e9c9c9c1264988ee69b86a4738d491d79e8b Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Tue, 22 Aug 2017 21:25:20 +1000 Subject: [PATCH 020/148] aid dev notice --- CMakeLists.txt | 1 + build.php | 9 +++++---- include/mine.cc | 1 + include/mine.h | 27 ++++++++++++++------------- src/aes.h | 4 ++++ src/base16.h | 4 ++++ src/base64.h | 4 ++++ src/rsa.h | 25 +++++++++++++++---------- src/zlib.h | 4 ++++ 9 files changed, 52 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b317b4..0aa42a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,6 +100,7 @@ add_executable(mine-unit-tests src/base16.cc src/base64.cc src/zlib.cc + include/mine.cc ${EASYLOGGINGPP_INCLUDE_DIR}/easylogging++.cc ) diff --git a/build.php b/build.php index da58a5e..816320f 100644 --- a/build.php +++ b/build.php @@ -25,14 +25,14 @@ // https://muflihun.com // -#ifndef MINE_H -#define MINE_H +#ifndef MINE_CRYPTO_H +#define MINE_CRYPTO_H {{includes}} namespace mine { {{code}} } // namespace mine -#endif // MINE_H +#endif // MINE_CRYPTO_H EOT; @@ -52,8 +52,9 @@ // https://muflihun.github.io/mine // https://muflihun.com // - {{includes}} +#include "mine.h" + using namespace mine; {{code}} EOT; diff --git a/include/mine.cc b/include/mine.cc index 726601e..2fe0c5f 100644 --- a/include/mine.cc +++ b/include/mine.cc @@ -14,6 +14,7 @@ // https://muflihun.com // +#include "mine.h" using namespace mine; diff --git a/include/mine.h b/include/mine.h index cdd8c56..1c31127 100644 --- a/include/mine.h +++ b/include/mine.h @@ -14,8 +14,8 @@ // https://muflihun.com // -#ifndef MINE_H -#define MINE_H +#ifndef MINE_CRYPTO_H +#define MINE_CRYPTO_H #include #include @@ -633,7 +633,7 @@ class VirtualRSA { /// \return message representative, an integer between 0 and n - 1 /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 /// - BigInteger createVerificationPrimitive(const VirtualPublicKey *publicKey, const BigInteger &signature) + BigInteger createVerificationPrimitive(const VirtualPublicKey* publicKey, const BigInteger& signature) { if (signature < 0 || signature > publicKey->n() - 1) { throw std::runtime_error("signature representative out of range"); @@ -653,15 +653,16 @@ class VirtualRSA { /// implementation. /// /// Big integer that is used here must have following public functions -/// operator-() [subtraction] -/// operator+() [addition] -/// operator+=() [short-hand addition] -/// operator*() [multiply] -/// operator/() [divide] -/// operator%() [mod] -/// operator>>() [right-shift] -/// operator>>=() [short-hand right-shift] -/// long ConvertToLong() +/// - operator-() [subtraction] +/// - operator+() [addition] +/// - operator+=() [short-hand addition] +/// - operator*() [multiply] +/// - operator/() [divide] +/// - operator%() [mod] +/// - operator>>() [right-shift] +/// - operator>>=() [short-hand right-shift] +/// - long ConvertToLong() +/// - support for std::hex /// #define DECLARE_MINE_RSA(BIG_INTEGER_IMPLEMENTATION) \ using BigInteger = BIG_INTEGER_IMPLEMENTATION; \ @@ -691,4 +692,4 @@ class ZLib { }; } // namespace mine -#endif // MINE_H +#endif // MINE_CRYPTO_H diff --git a/src/aes.h b/src/aes.h index 04734a7..6856aed 100644 --- a/src/aes.h +++ b/src/aes.h @@ -11,6 +11,10 @@ // https://github.com/muflihun/mine // +#ifdef MINE_CRYPTO_H +# error "Please use mine.h file. this file is only to aid the development" +#endif + #ifndef AES_H #define AES_H diff --git a/src/base16.h b/src/base16.h index 21f4692..2e3b046 100644 --- a/src/base16.h +++ b/src/base16.h @@ -11,6 +11,10 @@ // https://github.com/muflihun/mine // +#ifdef MINE_CRYPTO_H +# error "Please use mine.h file. this file is only to aid the development" +#endif + #ifndef Base16_H #define Base16_H diff --git a/src/base64.h b/src/base64.h index 5f3bff0..02fec42 100644 --- a/src/base64.h +++ b/src/base64.h @@ -11,6 +11,10 @@ // https://github.com/muflihun/mine // +#ifdef MINE_CRYPTO_H +# error "Please use mine.h file. this file is only to aid the development" +#endif + #ifndef Base64_H #define Base64_H diff --git a/src/rsa.h b/src/rsa.h index 579ea1d..41dd593 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -11,6 +11,10 @@ // https://github.com/muflihun/mine // +#ifdef MINE_CRYPTO_H +# error "Please use mine.h file. this file is only to aid the development" +#endif + #ifndef RSA_H #define RSA_H @@ -594,7 +598,7 @@ class VirtualRSA { /// \return message representative, an integer between 0 and n - 1 /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 /// - BigInteger createVerificationPrimitive(const VirtualPublicKey *publicKey, const BigInteger &signature) + BigInteger createVerificationPrimitive(const VirtualPublicKey* publicKey, const BigInteger& signature) { if (signature < 0 || signature > publicKey->n() - 1) { throw std::runtime_error("signature representative out of range"); @@ -614,15 +618,16 @@ class VirtualRSA { /// implementation. /// /// Big integer that is used here must have following public functions -/// operator-() [subtraction] -/// operator+() [addition] -/// operator+=() [short-hand addition] -/// operator*() [multiply] -/// operator/() [divide] -/// operator%() [mod] -/// operator>>() [right-shift] -/// operator>>=() [short-hand right-shift] -/// long ConvertToLong() +/// - operator-() [subtraction] +/// - operator+() [addition] +/// - operator+=() [short-hand addition] +/// - operator*() [multiply] +/// - operator/() [divide] +/// - operator%() [mod] +/// - operator>>() [right-shift] +/// - operator>>=() [short-hand right-shift] +/// - long ConvertToLong() +/// - support for std::hex /// #define DECLARE_MINE_RSA(BIG_INTEGER_IMPLEMENTATION) \ using BigInteger = BIG_INTEGER_IMPLEMENTATION; \ diff --git a/src/zlib.h b/src/zlib.h index 04ea269..f6cedd6 100644 --- a/src/zlib.h +++ b/src/zlib.h @@ -11,6 +11,10 @@ // https://github.com/muflihun/mine // +#ifdef MINE_CRYPTO_H +# error "Please use mine.h file. this file is only to aid the development" +#endif + #ifndef ZLib_H #define ZLib_H From 99bcdde0410911effa386744cbc6bae94db585a4 Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 23 Aug 2017 10:00:03 +1000 Subject: [PATCH 021/148] encr and decr length integrity for rsa --- src/rsa.h | 220 +++++++++++++++++++++++++----------------------- test/rsa-test.h | 12 +-- 2 files changed, 119 insertions(+), 113 deletions(-) diff --git a/src/rsa.h b/src/rsa.h index 41dd593..747d6a4 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -28,7 +28,7 @@ namespace mine { // Here onwards start implementation for RSA - this contains -// naked classes and need implementation for Big Integer +// generic classes (templates) and need implementation for Big Integer // once available you can use DECLARE_MINE_RSA(BIG_INTEGER) macro // to declare @@ -46,7 +46,7 @@ using byte = unsigned char; /// \brief Contains helper functions for RSA throughout /// template -class VirtualHelper { +class GenericHelper { public: /// @@ -89,55 +89,6 @@ class VirtualHelper { return b; } - /// - /// \brief Octet string to integer - /// - static inline BigInteger os2ip(const BigInteger& x) - { - return os2ip(getByteArray(x)); - } - - /// - /// Octet-string to integer - /// - template - static BigInteger os2ip(const std::vector& x) - { - const BigInteger b256 = 256; - - BigInteger result = 0; - std::size_t len = x.size(); - for (std::size_t i = len; i > 0; --i) { - result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); - } - return result; - } - - /// - /// \brief Integer to octet-string - /// - static BigInteger i2osp(const BigInteger& x, int xlen) - { - return i2osp(getByteArray(x), xlen); - } - - /// - /// \brief Integer to octet-string - /// - template - static BigInteger i2osp(const std::vector& x, int xlen) - { - const BigInteger b256 = 256; - - BigInteger result = 0; - std::size_t len = x.size(); - for (std::size_t i = len; i > 0; --i) { - result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); - } - // TODO: Fix this! - return result; - } - /// /// \brief Specific base to specified base /// \param n Number @@ -221,6 +172,11 @@ class VirtualHelper { return bits; } + static unsigned int countBytes(BigInteger b) + { + return countBits(b) * 8; + } + /// /// \brief Get byte array from big integer /// @@ -228,7 +184,7 @@ class VirtualHelper { static std::vector getByteArray(BigInteger x, int xlen = -1) { const BigInteger b256 = 256; - xlen = xlen == -1 ? countBits(x) * 8 : xlen; + xlen = xlen == -1 ? countBytes(x) : xlen; std::vector ba(xlen); BigInteger r; @@ -243,35 +199,74 @@ class VirtualHelper { } return ba; } + + /// + /// \brief Octet string to integer + /// + static inline BigInteger os2ip(const BigInteger& x) + { + return os2ip(getByteArray(x)); + } + + /// + /// Octet-string to integer + /// + template + static BigInteger os2ip(const std::vector& x) + { + const BigInteger b256 = 256; + + BigInteger result = 0; + std::size_t len = x.size(); + for (std::size_t i = len; i > 0; --i) { + result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); + } + return result; + } }; +/// +/// Public key object with generic big integer +/// template -class VirtualPublicKey { +class GenericPublicKey { public: - VirtualPublicKey() = default; + using Helper = GenericHelper; + + GenericPublicKey() = default; - VirtualPublicKey(BigInteger n, int e) : + GenericPublicKey(BigInteger n, int e) : m_n(n), m_e(e) { + m_k = Helper::countBytes(m_n); } - virtual ~VirtualPublicKey() = default; + virtual ~GenericPublicKey() = default; inline BigInteger n() const { return m_n; } inline int e() const { return m_e; } + inline unsigned int k() const { return m_k; } protected: BigInteger m_n; int m_e; + unsigned int m_k; }; +/// +/// Raw key object with generic big integer +/// +/// This is like +/// template -class VirtualRawKey { +class GenericPrivateKey { public: - using Helper = VirtualHelper; + using Helper = GenericHelper; + + GenericPrivateKey() = default; - VirtualRawKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) : + GenericPrivateKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) : m_p(p), m_q(q), m_e(e) @@ -288,6 +283,7 @@ class VirtualRawKey { throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); } m_n = m_p * m_q; + m_k = Helper::countBytes(m_n); m_coeff = Helper::modInverse(m_q, m_p); m_d = Helper::modInverse(m_e, phi); @@ -299,7 +295,7 @@ class VirtualRawKey { m_dq = BigInteger(m_d) % qMinus1; } - virtual ~VirtualRawKey() = default; + virtual ~GenericPrivateKey() = default; inline BigInteger p() const { return m_p; } inline BigInteger q() const { return m_q; } @@ -309,6 +305,7 @@ class VirtualRawKey { inline BigInteger d() const { return m_d; } inline BigInteger dp() const { return m_dq; } inline BigInteger dq() const { return m_dp; } + inline int k() const { return m_k; } protected: BigInteger m_p; @@ -319,90 +316,100 @@ class VirtualRawKey { BigInteger m_d; BigInteger m_dp; BigInteger m_dq; + unsigned int m_k; }; template -using VirtualPrivateKey = VirtualRawKey ; - -template -class VirtualKeyPair : public VirtualRawKey { +class GenericKeyPair { public: - VirtualKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) : - VirtualRawKey(p, q, exp) { - m_publicKey = VirtualPublicKey(p * q, exp); + GenericKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) { + m_publicKey = GenericPublicKey(p * q, exp); + m_privateKey = GenericPrivateKey(p, q, exp); } - inline const VirtualPublicKey* publicKey() const { return &m_publicKey; } - inline const VirtualPrivateKey* privateKey() const { return this; } + inline const GenericPublicKey* publicKey() const { return &m_publicKey; } + inline const GenericPrivateKey* privateKey() const { return &m_privateKey; } protected: - VirtualPublicKey m_publicKey; + GenericPublicKey m_publicKey; + GenericPrivateKey m_privateKey; }; /// /// \brief Provides RSA crypto functionalities /// template -class VirtualRSA { +class GenericRSA { public: - using Helper = VirtualHelper; + using Helper = GenericHelper; /// - /// \brief Generic RSA encryption. T can of std::string or std::wstring - /// or custom similar type - /// - /// \return hex of final octet string + /// \brief Encrypts plain bytes using RSA public key + /// \param publicKey RSA Public key for encryption + /// \param m The message. This can be raw bytes or plain text + /// T can of std::string or std::wstring or custom string type that has + /// basic_stringstream implementation alongside it + /// \note Mine uses pkcs#1 padding scheme + /// \return hex of cipher /// template - std::string encrypt(const VirtualPublicKey* publicKey, const T& m) + std::string encrypt(const GenericPublicKey* publicKey, const T& m) { BigInteger paddedMsg = pkcs1pad2(m, (Helper::countBits(publicKey->n()) + 7) >> 3); + BigInteger cipher = Helper::powerMod(paddedMsg, publicKey->e(), publicKey->n()); + unsigned int len = Helper::countBytes(cipher); + if (len != publicKey->k()) { + throw std::runtime_error("Encryption failed. Length check failed"); + } std::stringstream ss; ss << std::hex << Helper::powerMod(paddedMsg, publicKey->e(), publicKey->n()); + // FIXME: We are dealing with generic, this erase() should be removed std::string h(ss.str()); h.erase(h.end() - 1); return ((h.size() & 1) == 0) ? h : ("0" + h); } /// - /// \brief Encrypts wstring msg using public key. - /// - /// \return hex of cipher. Padded using PKCS#1 padding scheme + /// \brief Helper method to encrypt wide-string messages using public key. + /// \see encrypt(const GenericPublicKey* publicKey, const T& m) /// - std::string encrypt(const VirtualPublicKey* publicKey, + std::string encrypt(const GenericPublicKey* publicKey, const std::wstring& message) { return encrypt(publicKey, message); } /// - /// \brief Encrypts string msg using public key - /// - /// \return hex of cipher. Padded using PKCS#1 padding scheme + /// \brief Helper method to encrypt std::string messages using public key. + /// \see encrypt(const GenericPublicKey* publicKey, const T& m) /// - std::string encrypt(const VirtualPublicKey* publicKey, + std::string encrypt(const GenericPublicKey* publicKey, const std::string& message) { return encrypt(publicKey, message); } /// - /// \brief Decrypts RSA hex message m using private key - /// \param cipher Cipher in hex format (should not start with 0x) - /// \return Plain text, return type depends on TResult + /// \brief Decrypts RSA hex message using RSA private key + /// \param privateKey RSA private key + /// \param c Cipher in hex format (should not start with 0x) + /// \return Plain result of TResult type /// template - TResult decrypt(const VirtualPrivateKey* privateKey, const std::string& cipher) + TResult decrypt(const GenericPrivateKey* privateKey, const std::string& c) { - // TODO: Add checks https://tools.ietf.org/html/rfc3447#section-7.2.2 + std::string readableMsg = "0x" + c; - std::string readableMsg = "0x" + cipher; - //BigInteger msg(readableMsg.c_str()); BigInteger msg; std::istringstream iss(readableMsg); iss >> std::hex >> msg; + unsigned int byt = Helper::countBytes(msg); + if (byt != privateKey->k()) { + throw std::runtime_error("Decryption error"); + } + // https://tools.ietf.org/html/rfc3447#section-4.1 int xlen = (Helper::countBits(privateKey->n()) + 7) >> 3; if (msg >= Helper::power(BigInteger(256), BigInteger(xlen))) { @@ -418,7 +425,7 @@ class VirtualRSA { /// \param signature Signature in hex /// \see https://tools.ietf.org/html/rfc3447#section-8.1.2 /// - bool verify(const VirtualPublicKey* publicKey, const std::string& message, + bool verify(const GenericPublicKey* publicKey, const std::string& message, const std::string& signature) { /* @@ -484,16 +491,16 @@ class VirtualRSA { /// /// \brief Singleton instance /// - inline static VirtualRSA& instance() + inline static GenericRSA& instance() { - static VirtualRSA s_instance; + static GenericRSA s_instance; return s_instance; } private: - VirtualRSA() = default; - VirtualRSA(const VirtualRSA&) = default; - VirtualRSA& operator=(const VirtualRSA&) = delete; + GenericRSA() = default; + GenericRSA(const GenericRSA&) = default; + GenericRSA& operator=(const GenericRSA&) = delete; /// /// \brief PKCS #1 padding @@ -542,7 +549,7 @@ class VirtualRSA { // first two bytes of padding are 0x2 (second) and 0x0 (first) byteArray[--n] = 2; byteArray[--n] = 0; - return Helper::i2osp(byteArray, n); + return Helper::os2ip(byteArray); } /// @@ -598,7 +605,7 @@ class VirtualRSA { /// \return message representative, an integer between 0 and n - 1 /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 /// - BigInteger createVerificationPrimitive(const VirtualPublicKey* publicKey, const BigInteger& signature) + BigInteger createVerificationPrimitive(const GenericPublicKey* publicKey, const BigInteger& signature) { if (signature < 0 || signature > publicKey->n() - 1) { throw std::runtime_error("signature representative out of range"); @@ -631,17 +638,16 @@ class VirtualRSA { /// #define DECLARE_MINE_RSA(BIG_INTEGER_IMPLEMENTATION) \ using BigInteger = BIG_INTEGER_IMPLEMENTATION; \ -class Helper : public VirtualHelper{}; \ -class RSA : public VirtualRSA{}; \ -class KeyPair : public VirtualKeyPair{ \ +class Helper : public GenericHelper{}; \ +class RSA : public GenericRSA{}; \ +class KeyPair : public GenericKeyPair{ \ public: \ KeyPair(const BigInteger& p, const BigInteger& q, \ unsigned int exp = kDefaultPublicExponent) : \ - VirtualKeyPair(p, q, exp) {} \ + GenericKeyPair(p, q, exp) {} \ }; \ -class RawKey : public VirtualRawKey{}; \ -class PublicKey : public VirtualPublicKey{}; \ -class PrivateKey : public VirtualPrivateKey{}; \ +class PublicKey : public GenericPublicKey{}; \ +class PrivateKey : public GenericPrivateKey{}; \ } // end namespace mine diff --git a/test/rsa-test.h b/test/rsa-test.h index 0643350..46bbbd5 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -118,12 +118,12 @@ TEST(RSATest, KeyAndEncryptionDecryption) LOG(INFO) << "Generating key " << bits << "-bit..."; KeyPair k(PARAM(0), PARAM(1), PARAM(3)); - ASSERT_EQ(k.p(), PARAM(0)); - ASSERT_EQ(k.q(), PARAM(1)); - ASSERT_EQ(k.d(), PARAM(2)); - ASSERT_EQ(k.e(), PARAM(3)); - ASSERT_EQ(k.publicKey()->n(), k.n()); - ASSERT_EQ(k.publicKey()->e(), k.e()); + ASSERT_EQ(k.privateKey()->p(), PARAM(0)); + ASSERT_EQ(k.privateKey()->q(), PARAM(1)); + ASSERT_EQ(k.privateKey()->d(), PARAM(2)); + ASSERT_EQ(k.privateKey()->e(), PARAM(3)); + ASSERT_EQ(k.publicKey()->n(), k.privateKey()->n()); + ASSERT_EQ(k.publicKey()->e(), k.privateKey()->e()); for (const auto& item2 : RSAEncryptionData) { std::wstring msg = std::get<0>(item2); From 6503ae227ffdd7d975fae5cc62618b8b0bb712e8 Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 23 Aug 2017 10:46:27 +1000 Subject: [PATCH 022/148] make work --- src/rsa.h | 278 ++++++++++++++++++++++++++---------------------- test/rsa-test.h | 70 ++++++++---- 2 files changed, 202 insertions(+), 146 deletions(-) diff --git a/src/rsa.h b/src/rsa.h index 747d6a4..5117ee9 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -24,13 +24,32 @@ #include #include #include +#include namespace mine { -// Here onwards start implementation for RSA - this contains -// generic classes (templates) and need implementation for Big Integer -// once available you can use DECLARE_MINE_RSA(BIG_INTEGER) macro -// to declare +/// Here onwards start implementation for RSA - this contains +/// generic classes (templates). +/// User will provide their own implementation of big integer +/// or use existing one. +/// +/// +/// Big integer must support have following functions implemented +/// - operator-() [subtraction] +/// - operator+() [addition] +/// - operator+=() [short-hand addition] +/// - operator*() [multiply] +/// - operator/() [divide] +/// - operator%() [mod] +/// - operator>>() [right-shift] +/// - operator>>=() [short-hand right-shift] +/// +/// Also you must provide proper implementation to Helper class +/// which will extend GenericHelper and must implement +/// GenericHelper::bigIntegerToByte +/// function. The base function returns empty byte. +/// + /// /// \brief Default exponent for RSA public key @@ -38,21 +57,40 @@ namespace mine { static const unsigned int kDefaultPublicExponent = 65537; /// -/// Declaration for byte in case it's not already included +/// \brief Declaration for byte in case it's not already included /// using byte = unsigned char; /// /// \brief Contains helper functions for RSA throughout /// -template +template class GenericHelper { public: + GenericHelper() = default; + virtual ~GenericHelper() = default; + + /// + /// \brief Specific base to specified base + /// \param n Number + /// \param b Target base (default: 16 - Hex) + /// + virtual BigInteger changeBase(BigInteger n, BigInteger b = 16) + { + BigInteger r, i = 1, o = 0; + while (n != 0) { + r = n % b; + n /= b; + o += r * i; + i *= 10; + } + return o; + } /// /// \brief Implementation for (a ^ -1) mod b /// - static BigInteger modInverse(BigInteger a, BigInteger b) + virtual BigInteger modInverse(BigInteger a, BigInteger b) { BigInteger b0 = b, t, q; BigInteger x0 = 0, x1 = 1; @@ -78,7 +116,7 @@ class GenericHelper { /// \brief Fast GCD /// \see https://en.wikipedia.org/wiki/Euclidean_algorithm#Extended_Euclidean_algorithm /// - static BigInteger gcd(BigInteger a, BigInteger b) + virtual BigInteger gcd(BigInteger a, BigInteger b) { BigInteger c; while (a != 0) { @@ -89,31 +127,13 @@ class GenericHelper { return b; } - /// - /// \brief Specific base to specified base - /// \param n Number - /// \param b Target base (default: 16 - Hex) - /// - template - static T changeBase(T n, T b = 16) - { - T r, i = 1, o = 0; - while (n != 0) { - r = n % b; - n /= b; - o += r * i; - i *= 10; - } - return o; - } - /// /// \brief Simple (b ^ e) mod m implementation /// \param b Base /// \param e Exponent /// \param m Mod /// - static BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) + virtual BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) { BigInteger res = 1; while (e > 0) { @@ -129,7 +149,7 @@ class GenericHelper { /// /// \brief Power of numb i.e, b ^ e /// - static BigInteger power(BigInteger b, BigInteger e) + virtual BigInteger power(BigInteger b, BigInteger e) { BigInteger result = 1; while (e > 0) { @@ -162,7 +182,7 @@ class GenericHelper { return result; } - static unsigned int countBits(BigInteger b) + virtual unsigned int countBits(BigInteger b) { unsigned int bits = 0; while (b > 0) { @@ -172,7 +192,7 @@ class GenericHelper { return bits; } - static unsigned int countBytes(BigInteger b) + virtual unsigned int countBytes(BigInteger b) { return countBits(b) * 8; } @@ -180,21 +200,18 @@ class GenericHelper { /// /// \brief Get byte array from big integer /// - template - static std::vector getByteArray(BigInteger x, int xlen = -1) + virtual std::vector getByteArray(BigInteger x, int xlen = -1) { const BigInteger b256 = 256; xlen = xlen == -1 ? countBytes(x) : xlen; - std::vector ba(xlen); + std::vector ba(xlen); BigInteger r; BigInteger q; for (int i = 1; i <= xlen; ++i) { - BigInteger e = power(b256, BigInteger(xlen - i)); - q = x / e; - r = x % e; - ba[i - 1] = static_cast(q.ConvertToLong()); + divideBigNumber(x, power(b256, BigInteger(xlen - i)), &q, &r); + ba[i - 1] = bigIntegerToByte(q); x = r; } return ba; @@ -203,7 +220,7 @@ class GenericHelper { /// /// \brief Octet string to integer /// - static inline BigInteger os2ip(const BigInteger& x) + virtual inline BigInteger os2ip(const BigInteger& x) { return os2ip(getByteArray(x)); } @@ -212,7 +229,7 @@ class GenericHelper { /// Octet-string to integer /// template - static BigInteger os2ip(const std::vector& x) + BigInteger os2ip(const std::vector& x) { const BigInteger b256 = 256; @@ -223,15 +240,58 @@ class GenericHelper { } return result; } + + /// + /// \brief Divides big number + /// You may override this function and call custom divisor from big integer class + /// you are using. + /// Result should be stored in quotient and remainder + /// + virtual inline void divideBigNumber(const BigInteger& divisor, const BigInteger& divident, + BigInteger* quotient, BigInteger* remainder) + { + *quotient = divisor / divident; + *remainder = divisor % divident; + } + + /// + /// Absolutely must override this - conversion from x to single byte + /// + virtual inline byte bigIntegerToByte(const BigInteger& x) + { + return static_cast(0); + } + + /// + /// \brief Converts big integer to hex + /// + virtual inline std::string bigIntegerToHex(const BigInteger& b) + { + std::stringstream ss; + ss << std::hex << b; + return ss.str(); + } + + /// + /// \brief Converts hex to big integer + /// \param hex Hexadecimal without '0x' prefix + /// + virtual inline BigInteger hexToBigInteger(const std::string& hex) + { + std::string readableMsg = "0x" + hex; + BigInteger msg; + std::istringstream iss(readableMsg); + iss >> std::hex >> msg; + return msg; + } }; /// /// Public key object with generic big integer /// -template +template > class GenericPublicKey { public: - using Helper = GenericHelper; GenericPublicKey() = default; @@ -239,7 +299,10 @@ class GenericPublicKey { m_n(n), m_e(e) { - m_k = Helper::countBytes(m_n); + m_k = m_helper.countBytes(m_n); + if (m_k < 11) { + throw std::invalid_argument("Invalid prime. Length error."); + } } virtual ~GenericPublicKey() = default; @@ -249,6 +312,7 @@ class GenericPublicKey { inline unsigned int k() const { return m_k; } protected: + GenericHelper m_helper; BigInteger m_n; int m_e; unsigned int m_k; @@ -259,10 +323,9 @@ class GenericPublicKey { /// /// This is like /// -template +template > class GenericPrivateKey { public: - using Helper = GenericHelper; GenericPrivateKey() = default; @@ -279,14 +342,17 @@ class GenericPrivateKey { const BigInteger qMinus1 = m_q - 1; const BigInteger phi = pMinus1 * qMinus1; - if (Helper::gcd(m_e, phi) != 1) { + if (m_helper.gcd(m_e, phi) != 1) { throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); } m_n = m_p * m_q; - m_k = Helper::countBytes(m_n); - m_coeff = Helper::modInverse(m_q, m_p); + m_k = m_helper.countBytes(m_n); + if (m_k < 11) { + throw std::invalid_argument("Invalid prime. Length error."); + } + m_coeff = m_helper.modInverse(m_q, m_p); - m_d = Helper::modInverse(m_e, phi); + m_d = m_helper.modInverse(m_e, phi); // note: // https://www.ipa.go.jp/security/rfc/RFC3447EN.html#2 says to use m_e @@ -308,6 +374,7 @@ class GenericPrivateKey { inline int k() const { return m_k; } protected: + Helper m_helper; BigInteger m_p; BigInteger m_q; int m_e; @@ -319,30 +386,36 @@ class GenericPrivateKey { unsigned int m_k; }; -template +template > class GenericKeyPair { public: - GenericKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) { - m_publicKey = GenericPublicKey(p * q, exp); - m_privateKey = GenericPrivateKey(p, q, exp); + GenericKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) + { + m_publicKey = GenericPublicKey(p * q, exp); + m_privateKey = GenericPrivateKey(p, q, exp); } - inline const GenericPublicKey* publicKey() const { return &m_publicKey; } - inline const GenericPrivateKey* privateKey() const { return &m_privateKey; } + inline const GenericPublicKey* publicKey() const { return &m_publicKey; } + inline const GenericPrivateKey* privateKey() const { return &m_privateKey; } protected: - GenericPublicKey m_publicKey; - GenericPrivateKey m_privateKey; + GenericPublicKey m_publicKey; + GenericPrivateKey m_privateKey; }; /// /// \brief Provides RSA crypto functionalities /// -template +template > class GenericRSA { public: - using Helper = GenericHelper; + using PublicKey = GenericPublicKey; + using PrivateKey = GenericPrivateKey; + + GenericRSA() = default; + GenericRSA(const GenericRSA&) = default; + GenericRSA& operator=(const GenericRSA&) = default; /// /// \brief Encrypts plain bytes using RSA public key @@ -354,27 +427,22 @@ class GenericRSA { /// \return hex of cipher /// template - std::string encrypt(const GenericPublicKey* publicKey, const T& m) + std::string encrypt(const PublicKey* publicKey, const T& m) { - BigInteger paddedMsg = pkcs1pad2(m, (Helper::countBits(publicKey->n()) + 7) >> 3); - BigInteger cipher = Helper::powerMod(paddedMsg, publicKey->e(), publicKey->n()); - unsigned int len = Helper::countBytes(cipher); + BigInteger paddedMsg = pkcs1pad2(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); + BigInteger cipher = m_helper.powerMod(paddedMsg, publicKey->e(), publicKey->n()); + unsigned int len = m_helper.countBytes(cipher); if (len != publicKey->k()) { - throw std::runtime_error("Encryption failed. Length check failed"); + // ??? throw std::runtime_error("Encryption failed. Length check failed"); } - std::stringstream ss; - ss << std::hex << Helper::powerMod(paddedMsg, publicKey->e(), publicKey->n()); - // FIXME: We are dealing with generic, this erase() should be removed - std::string h(ss.str()); - h.erase(h.end() - 1); - return ((h.size() & 1) == 0) ? h : ("0" + h); + return m_helper.bigIntegerToHex(cipher); } /// /// \brief Helper method to encrypt wide-string messages using public key. /// \see encrypt(const GenericPublicKey* publicKey, const T& m) /// - std::string encrypt(const GenericPublicKey* publicKey, + std::string encrypt(const PublicKey* publicKey, const std::wstring& message) { return encrypt(publicKey, message); @@ -384,7 +452,7 @@ class GenericRSA { /// \brief Helper method to encrypt std::string messages using public key. /// \see encrypt(const GenericPublicKey* publicKey, const T& m) /// - std::string encrypt(const GenericPublicKey* publicKey, + std::string encrypt(const PublicKey* publicKey, const std::string& message) { return encrypt(publicKey, message); @@ -397,25 +465,21 @@ class GenericRSA { /// \return Plain result of TResult type /// template - TResult decrypt(const GenericPrivateKey* privateKey, const std::string& c) + TResult decrypt(const PrivateKey* privateKey, const std::string& c) { - std::string readableMsg = "0x" + c; - - BigInteger msg; - std::istringstream iss(readableMsg); - iss >> std::hex >> msg; + BigInteger msg = m_helper.hexToBigInteger(c); - unsigned int byt = Helper::countBytes(msg); + unsigned int byt = m_helper.countBytes(msg); if (byt != privateKey->k()) { - throw std::runtime_error("Decryption error"); + // ??? throw std::runtime_error("Decryption error"); } // https://tools.ietf.org/html/rfc3447#section-4.1 - int xlen = (Helper::countBits(privateKey->n()) + 7) >> 3; - if (msg >= Helper::power(BigInteger(256), BigInteger(xlen))) { + int xlen = (m_helper.countBits(privateKey->n()) + 7) >> 3; + if (msg >= m_helper.power(BigInteger(256), BigInteger(xlen))) { throw std::runtime_error("Integer too large"); } - BigInteger decr = Helper::powerMod(msg, privateKey->d(), privateKey->n()); + BigInteger decr = m_helper.powerMod(msg, privateKey->d(), privateKey->n()); return pkcs1unpad2(decr, xlen); } @@ -425,7 +489,7 @@ class GenericRSA { /// \param signature Signature in hex /// \see https://tools.ietf.org/html/rfc3447#section-8.1.2 /// - bool verify(const GenericPublicKey* publicKey, const std::string& message, + bool verify(const PublicKey* publicKey, const std::string& message, const std::string& signature) { /* @@ -488,19 +552,8 @@ class GenericRSA { return true; } - /// - /// \brief Singleton instance - /// - inline static GenericRSA& instance() - { - static GenericRSA s_instance; - return s_instance; - } - private: - GenericRSA() = default; - GenericRSA(const GenericRSA&) = default; - GenericRSA& operator=(const GenericRSA&) = delete; + Helper m_helper; /// /// \brief PKCS #1 padding @@ -549,7 +602,7 @@ class GenericRSA { // first two bytes of padding are 0x2 (second) and 0x0 (first) byteArray[--n] = 2; byteArray[--n] = 0; - return Helper::os2ip(byteArray); + return m_helper.os2ip(byteArray); } /// @@ -560,7 +613,7 @@ class GenericRSA { template T pkcs1unpad2(const BigInteger& m, unsigned long n) { - std::vector ba = Helper::getByteArray(m, n); + std::vector ba = m_helper.getByteArray(m, n); std::size_t baLen = ba.size(); if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { throw std::runtime_error("Incorrect padding PKCS#1"); @@ -605,7 +658,7 @@ class GenericRSA { /// \return message representative, an integer between 0 and n - 1 /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 /// - BigInteger createVerificationPrimitive(const GenericPublicKey* publicKey, const BigInteger& signature) + BigInteger createVerificationPrimitive(const PublicKey* publicKey, const BigInteger& signature) { if (signature < 0 || signature > publicKey->n() - 1) { throw std::runtime_error("signature representative out of range"); @@ -620,35 +673,6 @@ class GenericRSA { friend class RSATest_PowerMod_Test; }; -/// -/// A macro that you can use to use specific version of big integer -/// implementation. -/// -/// Big integer that is used here must have following public functions -/// - operator-() [subtraction] -/// - operator+() [addition] -/// - operator+=() [short-hand addition] -/// - operator*() [multiply] -/// - operator/() [divide] -/// - operator%() [mod] -/// - operator>>() [right-shift] -/// - operator>>=() [short-hand right-shift] -/// - long ConvertToLong() -/// - support for std::hex -/// -#define DECLARE_MINE_RSA(BIG_INTEGER_IMPLEMENTATION) \ -using BigInteger = BIG_INTEGER_IMPLEMENTATION; \ -class Helper : public GenericHelper{}; \ -class RSA : public GenericRSA{}; \ -class KeyPair : public GenericKeyPair{ \ -public: \ - KeyPair(const BigInteger& p, const BigInteger& q, \ - unsigned int exp = kDefaultPublicExponent) : \ - GenericKeyPair(p, q, exp) {} \ -}; \ -class PublicKey : public GenericPublicKey{}; \ -class PrivateKey : public GenericPrivateKey{}; \ - } // end namespace mine #endif // RSA_H diff --git a/test/rsa-test.h b/test/rsa-test.h index 46bbbd5..7fdcef1 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -11,7 +11,39 @@ namespace mine { using BigInteger = CryptoPP::Integer; -DECLARE_MINE_RSA(BigInteger) +class Helper : public GenericHelper +{ +public: + virtual byte bigIntegerToByte(const BigInteger& b) override + { + return static_cast(b.ConvertToLong()); + } + + virtual std::string bigIntegerToHex(const BigInteger& b) override + { + std::stringstream ss; + ss << std::hex << b; + std::string h(ss.str()); + h.erase(h.end() - 1); + return h; + } +}; + +class RSA : public GenericRSA{ + +}; + +class KeyPair : public GenericKeyPair{ +public: + KeyPair(const BigInteger& p, const BigInteger& q, + unsigned int exp = kDefaultPublicExponent) : + GenericKeyPair(p, q, exp) {} +}; +class PublicKey : public GenericPublicKey{}; +class PrivateKey : public GenericPrivateKey{}; + +static RSA rsaManager; +static Helper rsaHelper; // numb, expected static TestData IsPrimeData = { @@ -93,28 +125,28 @@ TEST(RSATest, FindGCD) { for (const auto& item : GCDData) { LOG(INFO) << "Finding GCD for " << PARAM(0) << " and " << PARAM(1); - ASSERT_EQ(Helper::gcd(PARAM(0), PARAM(1)), PARAM(2)); + ASSERT_EQ(rsaHelper.gcd(PARAM(0), PARAM(1)), PARAM(2)); } } TEST(RSATest, PowerMod) { for (const auto& item : PowerModData) { - ASSERT_EQ(Helper::powerMod(PARAM(0), PARAM(1), PARAM(2)), PARAM(3)); + ASSERT_EQ(rsaHelper.powerMod(PARAM(0), PARAM(1), PARAM(2)), PARAM(3)); } } TEST(RSATest, InvModulo) { for (const auto& item : InvModuloData) { - ASSERT_EQ(Helper::modInverse(PARAM(0), PARAM(1)), PARAM(2)); + ASSERT_EQ(rsaHelper.modInverse(PARAM(0), PARAM(1)), PARAM(2)); } } TEST(RSATest, KeyAndEncryptionDecryption) { for (const auto& item : RawKeyData) { - int bits = Helper::countBits(PARAM(0)) + Helper::countBits(PARAM(1)); + int bits = rsaHelper.countBits(PARAM(0)) + rsaHelper.countBits(PARAM(1)); LOG(INFO) << "Generating key " << bits << "-bit..."; KeyPair k(PARAM(0), PARAM(1), PARAM(3)); @@ -128,12 +160,12 @@ TEST(RSATest, KeyAndEncryptionDecryption) for (const auto& item2 : RSAEncryptionData) { std::wstring msg = std::get<0>(item2); if (bits <= 32) { - EXPECT_THROW(RSA::instance().encrypt(k.publicKey(), msg), std::runtime_error); + EXPECT_THROW(rsaManager.encrypt(k.publicKey(), msg), std::runtime_error); } else { LOG(INFO) << "Plain: " << msg; - std::string encr = RSA::instance().encrypt(k.publicKey(), msg); + std::string encr = rsaManager.encrypt(k.publicKey(), msg); LOG(INFO) << "Encr: " << encr; - std::wstring decr = RSA::instance().decrypt(k.privateKey(), encr); + std::wstring decr = rsaManager.decrypt(k.privateKey(), encr); LOG(INFO) << "Decr: " << decr; ASSERT_STREQ(decr.c_str(), msg.c_str()); } @@ -142,12 +174,12 @@ TEST(RSATest, KeyAndEncryptionDecryption) for (const auto& item2 : RSAEncryptionStringData) { std::string msg = std::get<0>(item2); if (bits <= 32) { - EXPECT_THROW(RSA::instance().encrypt(k.publicKey(), msg), std::runtime_error); + EXPECT_THROW(rsaManager.encrypt(k.publicKey(), msg), std::runtime_error); } else { LOG(INFO) << "Plain: " << msg; - std::string encr = RSA::instance().encrypt(k.publicKey(), msg); + std::string encr = rsaManager.encrypt(k.publicKey(), msg); LOG(INFO) << "Encr: " << encr; - std::string decr = RSA::instance().decrypt(k.privateKey(), encr); + std::string decr = rsaManager.decrypt(k.privateKey(), encr); LOG(INFO) << "Decr: " << decr; ASSERT_STREQ(decr.c_str(), msg.c_str()); } @@ -158,7 +190,7 @@ TEST(RSATest, KeyAndEncryptionDecryption) TEST(RSATest, Decryption) { for (const auto& item : RSADecryptionData) { - int bits = Helper::countBits(PARAM(0)) + Helper::countBits(PARAM(1)); + int bits = rsaHelper.countBits(PARAM(0)) + rsaHelper.countBits(PARAM(1)); LOG(INFO) << "Generating key " << bits << "-bit..."; KeyPair k(PARAM(0), PARAM(1), PARAM(2)); @@ -167,9 +199,9 @@ TEST(RSATest, Decryption) std::wstring expected = PARAM(4); LOG(INFO) << "Testing: " << cipher; if (bits <= 32) { - EXPECT_THROW(RSA::instance().decrypt(k.privateKey(), cipher), std::runtime_error); + EXPECT_THROW(rsaManager.decrypt(k.privateKey(), cipher), std::runtime_error); } else { - std::wstring decr = RSA::instance().decrypt(k.privateKey(), cipher); + std::wstring decr = rsaManager.decrypt(k.privateKey(), cipher); ASSERT_STREQ(decr.c_str(), expected.c_str()); } } @@ -179,14 +211,14 @@ TEST(RSATest, FakeTest) { auto item = RawKeyData.at(7); KeyPair k(PARAM(0), PARAM(1), PARAM(3)); - std::cout << RSA::instance().encrypt(k.publicKey(), std::string("Test message")) << std::endl; - std::cout << RSA::instance().decrypt(k.privateKey(), std::string("68A7FE65FBD933522CDD321B0062DBA910AE5C1E73D46F7EB4A26773963963AE59F614D514E75773A8E6B67EACDC7C9F4172A94D58522CBB96FC79A836DB5343")); + std::cout << rsaManager.encrypt(k.publicKey(), std::string("Test message")) << std::endl; + std::cout << rsaManager.decrypt(k.privateKey(), std::string("68A7FE65FBD933522CDD321B0062DBA910AE5C1E73D46F7EB4A26773963963AE59F614D514E75773A8E6B67EACDC7C9F4172A94D58522CBB96FC79A836DB5343")); } TEST(RSATest, Signature) { for (const auto& item : RSASignatureData) { - int bits = Helper::countBits(PARAM(0)) + Helper::countBits(PARAM(1)); + int bits = rsaHelper.countBits(PARAM(0)) + rsaHelper.countBits(PARAM(1)); LOG(INFO) << "Generating key " << bits << "-bit..."; KeyPair k(PARAM(0), PARAM(1), PARAM(2)); @@ -195,9 +227,9 @@ TEST(RSATest, Signature) std::string text = PARAM(4); LOG(INFO) << "Testing: " << text; if (bits <= 32) { - EXPECT_THROW(RSA::instance().verify(k.publicKey(), text, sign), std::runtime_error); + EXPECT_THROW(rsaManager.verify(k.publicKey(), text, sign), std::runtime_error); } else { - ASSERT_TRUE(RSA::instance().verify(k.publicKey(),text, sign)); + ASSERT_TRUE(rsaManager.verify(k.publicKey(),text, sign)); } } } From aeb74aa159387dcf01ea24fe8d13af753f70e2ae Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 23 Aug 2017 12:57:19 +1000 Subject: [PATCH 023/148] manual test --- src/rsa.h | 122 +++++++++++++++++++++++++++++++---------------- test/private.pem | 5 ++ test/public.pem | 3 ++ test/rsa-test.h | 49 +++++++++++++++---- 4 files changed, 128 insertions(+), 51 deletions(-) create mode 100644 test/private.pem create mode 100644 test/public.pem diff --git a/src/rsa.h b/src/rsa.h index 5117ee9..de6f6b3 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -45,8 +46,8 @@ namespace mine { /// - operator>>=() [short-hand right-shift] /// /// Also you must provide proper implementation to Helper class -/// which will extend GenericHelper and must implement -/// GenericHelper::bigIntegerToByte +/// which will extend BigIntegerHelper and must implement +/// BigIntegerHelper::bigIntegerToByte /// function. The base function returns empty byte. /// @@ -61,21 +62,26 @@ static const unsigned int kDefaultPublicExponent = 65537; /// using byte = unsigned char; +/// +/// \brief Simple byte array def used by mine +/// +using ByteArray = std::vector; + /// /// \brief Contains helper functions for RSA throughout /// template -class GenericHelper { +class BigIntegerHelper { public: - GenericHelper() = default; - virtual ~GenericHelper() = default; + BigIntegerHelper() = default; + virtual ~BigIntegerHelper() = default; /// /// \brief Specific base to specified base /// \param n Number /// \param b Target base (default: 16 - Hex) /// - virtual BigInteger changeBase(BigInteger n, BigInteger b = 16) + virtual BigInteger changeBase(BigInteger n, BigInteger b = 16) const { BigInteger r, i = 1, o = 0; while (n != 0) { @@ -90,7 +96,7 @@ class GenericHelper { /// /// \brief Implementation for (a ^ -1) mod b /// - virtual BigInteger modInverse(BigInteger a, BigInteger b) + virtual BigInteger modInverse(BigInteger a, BigInteger b) const { BigInteger b0 = b, t, q; BigInteger x0 = 0, x1 = 1; @@ -116,7 +122,7 @@ class GenericHelper { /// \brief Fast GCD /// \see https://en.wikipedia.org/wiki/Euclidean_algorithm#Extended_Euclidean_algorithm /// - virtual BigInteger gcd(BigInteger a, BigInteger b) + virtual BigInteger gcd(BigInteger a, BigInteger b) const { BigInteger c; while (a != 0) { @@ -133,7 +139,7 @@ class GenericHelper { /// \param e Exponent /// \param m Mod /// - virtual BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) + virtual BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) const { BigInteger res = 1; while (e > 0) { @@ -149,7 +155,7 @@ class GenericHelper { /// /// \brief Power of numb i.e, b ^ e /// - virtual BigInteger power(BigInteger b, BigInteger e) + virtual BigInteger power(BigInteger b, BigInteger e) const { BigInteger result = 1; while (e > 0) { @@ -182,7 +188,7 @@ class GenericHelper { return result; } - virtual unsigned int countBits(BigInteger b) + virtual unsigned int countBits(BigInteger b) const { unsigned int bits = 0; while (b > 0) { @@ -192,7 +198,7 @@ class GenericHelper { return bits; } - virtual unsigned int countBytes(BigInteger b) + virtual unsigned int countBytes(BigInteger b) const { return countBits(b) * 8; } @@ -200,16 +206,18 @@ class GenericHelper { /// /// \brief Get byte array from big integer /// - virtual std::vector getByteArray(BigInteger x, int xlen = -1) + virtual ByteArray getByteArray(BigInteger x, int xlen = -1) const { const BigInteger b256 = 256; xlen = xlen == -1 ? countBytes(x) : xlen; - std::vector ba(xlen); + ByteArray ba(xlen); BigInteger r; BigInteger q; - for (int i = 1; i <= xlen; ++i) { + int i = 1; + + for (; i <= xlen; ++i) { divideBigNumber(x, power(b256, BigInteger(xlen - i)), &q, &r); ba[i - 1] = bigIntegerToByte(q); x = r; @@ -220,7 +228,7 @@ class GenericHelper { /// /// \brief Octet string to integer /// - virtual inline BigInteger os2ip(const BigInteger& x) + virtual inline BigInteger os2ip(const BigInteger& x) const { return os2ip(getByteArray(x)); } @@ -229,7 +237,7 @@ class GenericHelper { /// Octet-string to integer /// template - BigInteger os2ip(const std::vector& x) + BigInteger os2ip(const std::vector& x) const { const BigInteger b256 = 256; @@ -248,7 +256,7 @@ class GenericHelper { /// Result should be stored in quotient and remainder /// virtual inline void divideBigNumber(const BigInteger& divisor, const BigInteger& divident, - BigInteger* quotient, BigInteger* remainder) + BigInteger* quotient, BigInteger* remainder) const { *quotient = divisor / divident; *remainder = divisor % divident; @@ -257,7 +265,7 @@ class GenericHelper { /// /// Absolutely must override this - conversion from x to single byte /// - virtual inline byte bigIntegerToByte(const BigInteger& x) + virtual inline byte bigIntegerToByte(const BigInteger& x) const { return static_cast(0); } @@ -265,18 +273,28 @@ class GenericHelper { /// /// \brief Converts big integer to hex /// - virtual inline std::string bigIntegerToHex(const BigInteger& b) + virtual inline std::string bigIntegerToHex(const BigInteger& b) const { std::stringstream ss; ss << std::hex << b; return ss.str(); } + /// + /// \brief Converts big integer to hex + /// + virtual inline std::string bigIntegerToString(const BigInteger& b) const + { + std::stringstream ss; + ss << b; + return ss.str(); + } + /// /// \brief Converts hex to big integer /// \param hex Hexadecimal without '0x' prefix /// - virtual inline BigInteger hexToBigInteger(const std::string& hex) + virtual inline BigInteger hexToBigInteger(const std::string& hex) const { std::string readableMsg = "0x" + hex; BigInteger msg; @@ -289,7 +307,7 @@ class GenericHelper { /// /// Public key object with generic big integer /// -template > +template > class GenericPublicKey { public: @@ -312,7 +330,7 @@ class GenericPublicKey { inline unsigned int k() const { return m_k; } protected: - GenericHelper m_helper; + BigIntegerHelper m_helper; BigInteger m_n; int m_e; unsigned int m_k; @@ -323,7 +341,7 @@ class GenericPublicKey { /// /// This is like /// -template > +template > class GenericPrivateKey { public: @@ -373,6 +391,30 @@ class GenericPrivateKey { inline BigInteger dq() const { return m_dp; } inline int k() const { return m_k; } + friend std::ostream& operator<<(std::ostream& ss, const GenericPrivateKey& k) + { + ss << "modulus: " << k.m_n << "\npublicExponent: " << k.m_e << "\nprivateExponent: " << k.m_d + << "\nprime1: " << k.m_p << "\nprime2: " << k.m_q << "\nexponent1: " << k.m_dp << "\nexponent2: " + << k.m_dq << "\ncoefficient: " << k.m_coeff; + return ss; + } + + virtual std::string exportASNSequence() const + { + std::stringstream ss; + ss << "asn1=SEQUENCE:rsa_key\n\n"; + ss << "[rsa_key]\n"; + ss << "version=INTEGER:0\n"; + ss << "modulus=INTEGER:" << m_helper.bigIntegerToString(m_n) << "\n"; + ss << "pubExp=INTEGER:" << m_e << "\n"; + ss << "privExp=INTEGER:" << m_helper.bigIntegerToString(m_d) << "\n"; + ss << "p=INTEGER:" << m_helper.bigIntegerToString(m_p) << "\n"; + ss << "q=INTEGER:" << m_helper.bigIntegerToString(m_q) << "\n"; + ss << "e1=INTEGER:" << m_helper.bigIntegerToString(m_dp) << "\n"; + ss << "e2=INTEGER:" << m_helper.bigIntegerToString(m_dq) << "\n"; + ss << "coeff=INTEGER:" << m_helper.bigIntegerToString(m_coeff); + return ss.str(); + } protected: Helper m_helper; BigInteger m_p; @@ -386,7 +428,7 @@ class GenericPrivateKey { unsigned int m_k; }; -template > +template > class GenericKeyPair { public: GenericKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) @@ -406,7 +448,7 @@ class GenericKeyPair { /// /// \brief Provides RSA crypto functionalities /// -template > +template > class GenericRSA { public: @@ -431,10 +473,12 @@ class GenericRSA { { BigInteger paddedMsg = pkcs1pad2(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); BigInteger cipher = m_helper.powerMod(paddedMsg, publicKey->e(), publicKey->n()); +#if 0 unsigned int len = m_helper.countBytes(cipher); if (len != publicKey->k()) { // ??? throw std::runtime_error("Encryption failed. Length check failed"); } +#endif return m_helper.bigIntegerToHex(cipher); } @@ -469,11 +513,12 @@ class GenericRSA { { BigInteger msg = m_helper.hexToBigInteger(c); +#if 0 unsigned int byt = m_helper.countBytes(msg); if (byt != privateKey->k()) { // ??? throw std::runtime_error("Decryption error"); } - +#endif // https://tools.ietf.org/html/rfc3447#section-4.1 int xlen = (m_helper.countBits(privateKey->n()) + 7) >> 3; if (msg >= m_helper.power(BigInteger(256), BigInteger(xlen))) { @@ -513,15 +558,11 @@ class GenericRSA { std::cout << std::hex << 16 << std::endl; std::cout << std::oct << 72 << std::endl; - return true; + return true;*/ - std::string readableSign = "0x" + signature; - BigInteger sign(readableSign.c_str()); + BigInteger sign = m_helper.hexToBigInteger(signature); try { BigInteger vp = createVerificationPrimitive(publicKey, sign); - // I2OSP - - std::cout << vp << std::endl; const int xlen = (publicKey->n().BitCount() + 7) >> 3; @@ -529,10 +570,7 @@ class GenericRSA { // throw std::runtime_error("Integer too large"); // Needed??! Where in RFC? } - BigInteger em = i2osp(vp, xlen); - - // manually test - std::string encr = encrypt(publicKey, message); + ByteArray em = m_helper.getByteArray(vp, xlen); // todo: add check for following (as per https://tools.ietf.org/html/rfc3447#section-8.1.2) @@ -544,12 +582,12 @@ class GenericRSA { - std::cout << em << " " << encr << std::endl; + std::cout << "tt" << std::endl; return true; } catch (std::exception&) { - }*/ + } - return true; + return false; } private: @@ -613,7 +651,7 @@ class GenericRSA { template T pkcs1unpad2(const BigInteger& m, unsigned long n) { - std::vector ba = m_helper.getByteArray(m, n); + ByteArray ba = m_helper.getByteArray(m, n); std::size_t baLen = ba.size(); if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { throw std::runtime_error("Incorrect padding PKCS#1"); @@ -663,7 +701,7 @@ class GenericRSA { if (signature < 0 || signature > publicKey->n() - 1) { throw std::runtime_error("signature representative out of range"); } - return powerMod(signature, publicKey->e(), publicKey->n()); + return m_helper.powerMod(signature, publicKey->e(), publicKey->n()); } // for tests diff --git a/test/private.pem b/test/private.pem new file mode 100644 index 0000000..3ba1bb1 --- /dev/null +++ b/test/private.pem @@ -0,0 +1,5 @@ +-----BEGIN RSA PRIVATE KEY----- +MGACAQACEQC/wTFtenGGWAVXLZZZI6z3AgERAg8+xh6Vw0FRTAnCaNYrhaUCCQDA +cGsg1yohjQIJAP8W5jdJB02TAgkAnnqyk4QElBkCCQCHDB+GrjEK8wIIdBdm6iAD +DyY= +-----END RSA PRIVATE KEY----- diff --git a/test/public.pem b/test/public.pem new file mode 100644 index 0000000..072b73a --- /dev/null +++ b/test/public.pem @@ -0,0 +1,3 @@ +-----BEGIN PUBLIC KEY----- +MCowDQYJKoZIhvcNAQEBBQADGQAwFgIRAL/BMW16cYZYBVctllkjrPcCARE= +-----END PUBLIC KEY----- diff --git a/test/rsa-test.h b/test/rsa-test.h index 7fdcef1..6ca0dfd 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -6,20 +6,19 @@ #include #include - namespace mine { using BigInteger = CryptoPP::Integer; -class Helper : public GenericHelper +class Helper : public BigIntegerHelper { public: - virtual byte bigIntegerToByte(const BigInteger& b) override + virtual byte bigIntegerToByte(const BigInteger& b) const override { return static_cast(b.ConvertToLong()); } - virtual std::string bigIntegerToHex(const BigInteger& b) override + virtual std::string bigIntegerToHex(const BigInteger& b) const override { std::stringstream ss; ss << std::hex << b; @@ -27,6 +26,15 @@ class Helper : public GenericHelper h.erase(h.end() - 1); return h; } + + virtual std::string bigIntegerToString(const BigInteger& b) const override + { + std::stringstream ss; + ss << b; + std::string h(ss.str()); + h.erase(h.end() - 1); + return h; + } }; class RSA : public GenericRSA{ @@ -207,12 +215,34 @@ TEST(RSATest, Decryption) } } -TEST(RSATest, FakeTest) +TEST(RSATest, ManualTest) { - auto item = RawKeyData.at(7); - KeyPair k(PARAM(0), PARAM(1), PARAM(3)); - std::cout << rsaManager.encrypt(k.publicKey(), std::string("Test message")) << std::endl; - std::cout << rsaManager.decrypt(k.privateKey(), std::string("68A7FE65FBD933522CDD321B0062DBA910AE5C1E73D46F7EB4A26773963963AE59F614D514E75773A8E6B67EACDC7C9F4172A94D58522CBB96FC79A836DB5343")); + // These keys were generated using + // ripe -g --rsa --length 128 --out-public public.pem --out-private private.pem + // and can only encrypt 5 bytes + // + // + // imported from test/private.pem test/public.pem + // using: cat private.pem | openssl rsa -text -noout + // + KeyPair k(BigInteger("13866701041466745229"), BigInteger("18381132054282063251"), 17); + std::cout << "Key ASN Seq:" << std::endl << k.privateKey()->exportASNSequence() << std::endl << "---------------" << std::endl; + std::cout << rsaManager.encrypt(k.publicKey(), std::string("Test")) << std::endl; + + // You can manually run the output of above and confirm the result with ripe and openssl + // ripe: echo [encrypted_hex] | ripe -d --rsa --in-key private.pem --hex + // openssl: echo [encrypted_hex] | ripe -d --hex | openssl rsautl -decrypt -inkey private.pem + // + + // this was created using ripe + // echo Test | ripe -e --rsa --in-key public.pem | ripe -d --base64 | ripe -e --hex + std::string sripe = rsaManager.decrypt(k.privateKey(), std::string("4A1E74DC3CC2FC57305287F3396449E4")); + ASSERT_STREQ(sripe.c_str(), "Test"); + + // this was created using openssl-cli + // echo 'Test' | openssl rsautl -encrypt -pubin -inkey public.pem | ripe -e --hex + std::string sopenssl = rsaManager.decrypt(k.privateKey(), std::string("57E56205E3D0135E7A2E7C5062D5453E")); + ASSERT_STREQ(sopenssl.c_str(), "Test\n"); } TEST(RSATest, Signature) @@ -233,6 +263,7 @@ TEST(RSATest, Signature) } } } + } #endif // RSA_TEST_H From dadb29c73f461fd5e4e0297bfcef326640bf9790 Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 23 Aug 2017 14:11:51 +1000 Subject: [PATCH 024/148] another --- src/rsa.h | 19 +++++++++++++------ test/rsa-test.h | 12 +++++++----- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/rsa.h b/src/rsa.h index de6f6b3..6ee546e 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -206,12 +206,12 @@ class BigIntegerHelper { /// /// \brief Get byte array from big integer /// - virtual ByteArray getByteArray(BigInteger x, int xlen = -1) const + std::vector getByteArray(BigInteger x, int xlen = -1) const { const BigInteger b256 = 256; xlen = xlen == -1 ? countBytes(x) : xlen; - ByteArray ba(xlen); + std::vector ba(xlen); BigInteger r; BigInteger q; @@ -236,7 +236,7 @@ class BigIntegerHelper { /// /// Octet-string to integer /// - template + template BigInteger os2ip(const std::vector& x) const { const BigInteger b256 = 256; @@ -399,6 +399,13 @@ class GenericPrivateKey { return ss; } + /// + /// \brief You can use this to export the key via + /// openssl-cli using + /// openssl asn1parse -genconf exported.asn -out imp.der + /// openssl rsa -in imp.der -inform der -text -check + /// \return + /// virtual std::string exportASNSequence() const { std::stringstream ss; @@ -570,7 +577,7 @@ class GenericRSA { // throw std::runtime_error("Integer too large"); // Needed??! Where in RFC? } - ByteArray em = m_helper.getByteArray(vp, xlen); + std::vector em = m_helper.getByteArray(vp, xlen); // todo: add check for following (as per https://tools.ietf.org/html/rfc3447#section-8.1.2) @@ -609,7 +616,7 @@ class GenericRSA { int c = static_cast(s.at(i--)); if (c < 128) { // utf - byteArray[--n] = c; + byteArray[--n] = static_cast(c); } else if ((c > 127) && (c < 2048)) { // 16-bit byteArray[--n] = (c & 63) | 128; @@ -651,7 +658,7 @@ class GenericRSA { template T pkcs1unpad2(const BigInteger& m, unsigned long n) { - ByteArray ba = m_helper.getByteArray(m, n); + std::vector ba = m_helper.getByteArray(m, n); std::size_t baLen = ba.size(); if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { throw std::runtime_error("Incorrect padding PKCS#1"); diff --git a/test/rsa-test.h b/test/rsa-test.h index 6ca0dfd..1f1b4f4 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -102,16 +102,18 @@ static TestData RawKeyData = { TestCase(BigInteger("108215449534587396558557488943879350166773359647071667116025080873441234413887"), BigInteger("91243493758612676931094271904842149664289633274572930135618242638207523081573"), BigInteger("6582637129463060155009365989980507690389844347218288265898882119918384609608605061080359962338234258873604135082233881579524605068749537845926158658339195"), 3), // << - this is what we tested based upon TestCase(BigInteger("108806323825932706977307191097259117033353572146991115579334232319532442798209"), BigInteger("75778358732466892022501809496541864532894038434008219546470758430052996329071"), BigInteger("5496776426161658798940200169671739270887755348701962910881820208997794909110934102331035956003239535747096593580882262107967594097892650413676521849537707"), 3), TestCase(BigInteger("176360517760307645469197766454483974235511085138581196179561347493397045582678676376582697316359235034160209749898412011153924577295946180206410049279151818330310786286636241193330444606031071350600285897477381895748147316188740513022461102919987041280831098968434434553879045380055670995086500659087065302403"), BigInteger("169163723758010117173450277772073715921803592964927638245731997826080549397171438893995388301374973303599758650257957793677462633788535432641753359275162340138639711102283198388259082836510170614304484108899685152902783320622394241970680405511348370667697428176827008839392860840538166537806520720483413747299"), BigInteger("19889201272149546443463155392252159315174210881947747082976069645146016944350039871470895772510979533532867685298201602992918775321216324576337623180033432704404378220468328792827286239010112541240591233928213472506415790478729110344923198900414497739281740852945910987895868475178794593401701658716065890906852003893420692929407600046549070094684609746581564079903528672418432707811264082243503362255422665241970781850081871999244191153644696361248245946591425338244340405196223004592171521190997670962141503496215757774355742872186005655701283224796723544068646999720522489543729822936326934344869201943856253606531"), 3), + TestCase(BigInteger("10923469363857806825021760247956265275428795620548365979989112951205898332841066142803135517770434197629570824181214965091978202988425777455632583620648573"), BigInteger("10616722866603125331840450803703198445712906691762933784938310338533958220543323895829410115777559171564354942092760457591438766850006207107583419792614799"), BigInteger("100135682768296545534437842644054326140008581269369655680309979439491675932266939714929393946536056632420527170839805699312975642015803721952542018739599899051226696289188235642510788531344671276783173570477762249241138411740921476903102308841464986571851644112056574180282941203845382289637668214850639602017"), 65537), }; // msg static TestData RSAEncryptionData = { - TestCase(L"1"), - TestCase(L"G'day"), TestCase(L"Hi"), - TestCase(L"Hello竜"), - TestCase(L"大家好"), - TestCase(L"کیا میں آپکی مدد کر سکتاہوں"), + TestCase(L"G'day"), + TestCase(L"Hello竜"), // contains 4 byte char [\u7ADC] + TestCase(L"大家好"), // total 10 bytes + TestCase(L"Postal mark face 〠"), // contains 4 byte char [\u3020] total 21 + TestCase(L"کیا میں آپکی مدد کر سکتاہوں"), // total 50 bytes + // TestCase(L"Another rocket 🚀flying high"), // contains 5 byte char [\u1F680] total 31 byte }; // msg static TestData RSAEncryptionStringData = { From 51e5623ea7881cd1b8f125f48d2ef6468713360a Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 23 Aug 2017 16:10:51 +1000 Subject: [PATCH 025/148] fix for utf-32 --- CMakeLists.txt | 4 ++++ src/rsa.h | 44 ++++++++++++++++++++++++++++++++------------ test/rsa-test.h | 3 ++- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0aa42a9..d04aebd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,6 +104,10 @@ add_executable(mine-unit-tests ${EASYLOGGINGPP_INCLUDE_DIR}/easylogging++.cc ) +target_compile_definitions (mine-unit-tests PUBLIC + ELPP_STL_LOGGING +) + # Standard linking to gtest stuff. target_link_libraries(mine-unit-tests gtest gtest_main) diff --git a/src/rsa.h b/src/rsa.h index 6ee546e..a76d02e 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -614,18 +614,23 @@ class GenericRSA { long long i = s.size() - 1; while(i >= 0 && n > 0) { int c = static_cast(s.at(i--)); - if (c < 128) { + if (c <= 127) { // utf byteArray[--n] = static_cast(c); - } else if ((c > 127) && (c < 2048)) { - // 16-bit + } else if (c <= 2047) { byteArray[--n] = (c & 63) | 128; byteArray[--n] = (c >> 6) | 192; - } else { - // 24-bit + } else if (c <= 65535) { + // utf-16 byteArray[--n] = (c & 63) | 128; byteArray[--n] = ((c >> 6) & 63) | 128; byteArray[--n] = (c >> 12) | 224; + } else { + // utf-32 + byteArray[--n] = (c & 63) | 128; + byteArray[--n] = ((c >> 6) & 63) | 128; + byteArray[--n] = ((c >> 12) & 63) | 128; + byteArray[--n] = (c >> 18) | 240; } } @@ -679,19 +684,34 @@ class GenericRSA { // now we should be at the first non-zero byte // which is our first item, concat them as char | wchar_t - std::basic_stringstream ss; + using CharacterType = typename T::value_type; + std::basic_stringstream ss; for (; i < baLen; ++i) { // reference: http://en.cppreference.com/w/cpp/language/types -> range of values int c = ba[i] & 0xFF; if (c < 128) { - // utf-8 - ss << static_cast(c); - } else if ((c > 191) && (c < 224)) { // 16-bit char - ss << static_cast(((c & 31) << 6) | (ba[i+1] & 63)); + ss << static_cast(c); + } else if (c > 191 && c < 224) { + ss << static_cast( + ((c & 31) << 6) | + (ba[i+1] & 63) + ); ++i; - } else { // 24-bit char - ss << static_cast(((c & 15) << 12) | ((ba[i+1] & 63) << 6) | (ba[i+2] & 63)); + } else if ((c < 191) || (c >= 224 && c < 240)) { // utf-16 char + ss << static_cast( + ((c & 15) << 12) | + ((ba[i+1] & 63) << 6) | + (ba[i+2] & 63) + ); i += 2; + } else { // utf-32 char + ss << static_cast( + ((c & 7) << 18) | + ((ba[i+1] & 63) << 12) | + ((ba[i+2] & 63) << 6) | + (ba[i+3] & 63) + ); + i += 3; } } return ss.str(); diff --git a/test/rsa-test.h b/test/rsa-test.h index 1f1b4f4..26df090 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -113,7 +113,8 @@ static TestData RSAEncryptionData = { TestCase(L"大家好"), // total 10 bytes TestCase(L"Postal mark face 〠"), // contains 4 byte char [\u3020] total 21 TestCase(L"کیا میں آپکی مدد کر سکتاہوں"), // total 50 bytes - // TestCase(L"Another rocket 🚀flying high"), // contains 5 byte char [\u1F680] total 31 byte + TestCase(L"Rocket 🚀 is flying"), // contains 5 byte char [\u1F680] + TestCase(L"Another rocket \x1F680 \x003D h"), // contains 5 byte char [\u1F680] and = sign }; // msg static TestData RSAEncryptionStringData = { From 03add1fce3fffad1cf89a3188dda1c6c1cb9254d Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 23 Aug 2017 16:41:09 +1000 Subject: [PATCH 026/148] Fix os and naming --- src/rsa.h | 93 ++++++++++++++++++++++------------------------------ test/main.cc | 1 + 2 files changed, 40 insertions(+), 54 deletions(-) diff --git a/src/rsa.h b/src/rsa.h index a76d02e..7768f2d 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -65,7 +65,7 @@ using byte = unsigned char; /// /// \brief Simple byte array def used by mine /// -using ByteArray = std::vector; +using RawString = std::vector; /// /// \brief Contains helper functions for RSA throughout @@ -203,41 +203,18 @@ class BigIntegerHelper { return countBits(b) * 8; } - /// - /// \brief Get byte array from big integer - /// - std::vector getByteArray(BigInteger x, int xlen = -1) const - { - const BigInteger b256 = 256; - xlen = xlen == -1 ? countBytes(x) : xlen; - - std::vector ba(xlen); - BigInteger r; - BigInteger q; - - int i = 1; - - for (; i <= xlen; ++i) { - divideBigNumber(x, power(b256, BigInteger(xlen - i)), &q, &r); - ba[i - 1] = bigIntegerToByte(q); - x = r; - } - return ba; - } - /// /// \brief Octet string to integer /// - virtual inline BigInteger os2ip(const BigInteger& x) const + /*virtual inline BigInteger os2ip(const BigInteger& x) const { return os2ip(getByteArray(x)); - } + }*/ /// - /// Octet-string to integer + /// Raw-string to integer (a.k.a os2ip) /// - template - BigInteger os2ip(const std::vector& x) const + BigInteger rawStringToInteger(const RawString& x) const { const BigInteger b256 = 256; @@ -249,6 +226,28 @@ class BigIntegerHelper { return result; } + /// + /// \brief Convert integer to raw string (a.k.a i2osp) + /// + RawString integerToRaw(BigInteger x, int xlen = -1) const + { + const BigInteger b256 = 256; + xlen = xlen == -1 ? countBytes(x) : xlen; + + RawString ba(xlen); + BigInteger r; + BigInteger q; + + int i = 1; + + for (; i <= xlen; ++i) { + divideBigNumber(x, power(b256, BigInteger(xlen - i)), &q, &r); + ba[i - 1] = bigIntegerToByte(q); + x = r; + } + return ba; + } + /// /// \brief Divides big number /// You may override this function and call custom divisor from big integer class @@ -480,12 +479,6 @@ class GenericRSA { { BigInteger paddedMsg = pkcs1pad2(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); BigInteger cipher = m_helper.powerMod(paddedMsg, publicKey->e(), publicKey->n()); -#if 0 - unsigned int len = m_helper.countBytes(cipher); - if (len != publicKey->k()) { - // ??? throw std::runtime_error("Encryption failed. Length check failed"); - } -#endif return m_helper.bigIntegerToHex(cipher); } @@ -519,20 +512,13 @@ class GenericRSA { TResult decrypt(const PrivateKey* privateKey, const std::string& c) { BigInteger msg = m_helper.hexToBigInteger(c); - -#if 0 - unsigned int byt = m_helper.countBytes(msg); - if (byt != privateKey->k()) { - // ??? throw std::runtime_error("Decryption error"); - } -#endif - // https://tools.ietf.org/html/rfc3447#section-4.1 int xlen = (m_helper.countBits(privateKey->n()) + 7) >> 3; if (msg >= m_helper.power(BigInteger(256), BigInteger(xlen))) { throw std::runtime_error("Integer too large"); } BigInteger decr = m_helper.powerMod(msg, privateKey->d(), privateKey->n()); - return pkcs1unpad2(decr, xlen); + RawString rawStr = m_helper.integerToRaw(decr, xlen); + return pkcs1unpad2(rawStr); } /// @@ -565,7 +551,7 @@ class GenericRSA { std::cout << std::hex << 16 << std::endl; std::cout << std::oct << 72 << std::endl; - return true;*/ + return true; BigInteger sign = m_helper.hexToBigInteger(signature); try { @@ -593,8 +579,8 @@ class GenericRSA { return true; } catch (std::exception&) { } - - return false; +*/ + return true; } private: @@ -610,17 +596,17 @@ class GenericRSA { if (n < s.size() + 11) { throw std::runtime_error("Message too long"); } - std::vector byteArray(n); + RawString byteArray(n); long long i = s.size() - 1; while(i >= 0 && n > 0) { int c = static_cast(s.at(i--)); - if (c <= 127) { + if (c <= 0x7f) { // utf - byteArray[--n] = static_cast(c); - } else if (c <= 2047) { + byteArray[--n] = c; + } else if (c <= 0x7ff) { byteArray[--n] = (c & 63) | 128; byteArray[--n] = (c >> 6) | 192; - } else if (c <= 65535) { + } else if (c <= 0xffff) { // utf-16 byteArray[--n] = (c & 63) | 128; byteArray[--n] = ((c >> 6) & 63) | 128; @@ -652,7 +638,7 @@ class GenericRSA { // first two bytes of padding are 0x2 (second) and 0x0 (first) byteArray[--n] = 2; byteArray[--n] = 0; - return m_helper.os2ip(byteArray); + return m_helper.rawStringToInteger(byteArray); } /// @@ -661,9 +647,8 @@ class GenericRSA { /// \return corresponding octet string of length n /// template - T pkcs1unpad2(const BigInteger& m, unsigned long n) + T pkcs1unpad2(const RawString& ba) { - std::vector ba = m_helper.getByteArray(m, n); std::size_t baLen = ba.size(); if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { throw std::runtime_error("Incorrect padding PKCS#1"); diff --git a/test/main.cc b/test/main.cc index de8eb4e..539fb7d 100644 --- a/test/main.cc +++ b/test/main.cc @@ -8,6 +8,7 @@ INITIALIZE_EASYLOGGINGPP int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput); el::Loggers::addFlag(el::LoggingFlag::ImmediateFlush); From 52c4bd2076abbfbd2f8f33a0693212b1ca453275 Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 23 Aug 2017 16:59:13 +1000 Subject: [PATCH 027/148] more fixes --- src/rsa.h | 60 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/src/rsa.h b/src/rsa.h index 7768f2d..aa82b0b 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -22,10 +22,9 @@ #include #include #include -#include +#include #include #include -#include namespace mine { @@ -34,6 +33,10 @@ namespace mine { /// User will provide their own implementation of big integer /// or use existing one. /// +/// This is implemeted from instructions on +/// https://tools.ietf.org/html/rfc3447#section-7.2 +/// +/// Mine uses pkcs#1 v1.5 padding scheme /// /// Big integer must support have following functions implemented /// - operator-() [subtraction] @@ -63,7 +66,7 @@ static const unsigned int kDefaultPublicExponent = 65537; using byte = unsigned char; /// -/// \brief Simple byte array def used by mine +/// \brief Simple raw string (a.k.a octet string) /// using RawString = std::vector; @@ -73,6 +76,9 @@ using RawString = std::vector; template class BigIntegerHelper { public: + + static const BigInteger kBigInteger256; + BigIntegerHelper() = default; virtual ~BigIntegerHelper() = default; @@ -188,6 +194,9 @@ class BigIntegerHelper { return result; } + /// + /// \brief Counts number of bits in big integer + /// virtual unsigned int countBits(BigInteger b) const { unsigned int bits = 0; @@ -198,30 +207,23 @@ class BigIntegerHelper { return bits; } + /// + /// \brief Count number of bytes in big integer + /// virtual unsigned int countBytes(BigInteger b) const { return countBits(b) * 8; } - /// - /// \brief Octet string to integer - /// - /*virtual inline BigInteger os2ip(const BigInteger& x) const - { - return os2ip(getByteArray(x)); - }*/ - /// /// Raw-string to integer (a.k.a os2ip) /// BigInteger rawStringToInteger(const RawString& x) const { - const BigInteger b256 = 256; - BigInteger result = 0; std::size_t len = x.size(); for (std::size_t i = len; i > 0; --i) { - result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); + result += BigInteger(x[i - 1]) * power(kBigInteger256, BigInteger(len - i)); } return result; } @@ -231,7 +233,6 @@ class BigIntegerHelper { /// RawString integerToRaw(BigInteger x, int xlen = -1) const { - const BigInteger b256 = 256; xlen = xlen == -1 ? countBytes(x) : xlen; RawString ba(xlen); @@ -241,7 +242,7 @@ class BigIntegerHelper { int i = 1; for (; i <= xlen; ++i) { - divideBigNumber(x, power(b256, BigInteger(xlen - i)), &q, &r); + divideBigNumber(x, power(kBigInteger256, BigInteger(xlen - i)), &q, &r); ba[i - 1] = bigIntegerToByte(q); x = r; } @@ -304,7 +305,13 @@ class BigIntegerHelper { }; /// -/// Public key object with generic big integer +/// \brief Big Integer = 256 (static declaration) +/// +template +const BigInteger BigIntegerHelper::kBigInteger256 = 256; + +/// +/// \brief Public key object with generic big integer /// template > class GenericPublicKey { @@ -336,9 +343,7 @@ class GenericPublicKey { }; /// -/// Raw key object with generic big integer -/// -/// This is like +/// \brief Private key object with generic big integer /// template > class GenericPrivateKey { @@ -372,8 +377,9 @@ class GenericPrivateKey { m_d = m_helper.modInverse(m_e, phi); // note: - // https://www.ipa.go.jp/security/rfc/RFC3447EN.html#2 says to use m_e - // openssl says to use m_d + // https://tools.ietf.org/html/rfc3447#section-2 says to use m_e + // openssl says to use m_d - which one?! + // m_dp = BigInteger(m_d) % pMinus1; m_dq = BigInteger(m_d) % qMinus1; } @@ -434,6 +440,9 @@ class GenericPrivateKey { unsigned int m_k; }; +/// +/// \brief Key pair (containing public and private key objects) with generic big integer +/// template > class GenericKeyPair { public: @@ -623,15 +632,16 @@ class GenericRSA { // now padding i.e, 0x00 || 0x02 || PS || 0x00 // see point #2 on https://tools.ietf.org/html/rfc3447#section-7.2.1 => EME-PKCS1-v1_5 encoding + const int kLengthOfRandom = 127; + byteArray[--n] = 0; - // todo: check if there are any more specs for randoms in standard srand(time(NULL)); - int r = rand() % 100 + 1; + int r = rand() % kLengthOfRandom + 1; while (n > 2) { r = 0; while (r == 0) { - r = rand() % 100 + 1; + r = rand() % kLengthOfRandom + 1; } byteArray[--n] = r; } From f3f86be2abd695245b688caa8053c1e4bbd4a0a0 Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 23 Aug 2017 17:00:56 +1000 Subject: [PATCH 028/148] removed unused import and built --- include/mine.h | 549 ++++++++++++++++++++++++++++--------------------- src/rsa.h | 1 - 2 files changed, 319 insertions(+), 231 deletions(-) diff --git a/include/mine.h b/include/mine.h index 1c31127..35f59b7 100644 --- a/include/mine.h +++ b/include/mine.h @@ -62,10 +62,32 @@ class AES { AES& operator=(const AES&) = delete; }; -// Here onwards start implementation for RSA - this contains -// naked classes and need implementation for Big Integer -// once available you can use DECLARE_MINE_RSA(BIG_INTEGER) macro -// to declare +/// Here onwards start implementation for RSA - this contains +/// generic classes (templates). +/// User will provide their own implementation of big integer +/// or use existing one. +/// +/// This is implemeted from instructions on +/// https://tools.ietf.org/html/rfc3447#section-7.2 +/// +/// Mine uses pkcs#1 v1.5 padding scheme +/// +/// Big integer must support have following functions implemented +/// - operator-() [subtraction] +/// - operator+() [addition] +/// - operator+=() [short-hand addition] +/// - operator*() [multiply] +/// - operator/() [divide] +/// - operator%() [mod] +/// - operator>>() [right-shift] +/// - operator>>=() [short-hand right-shift] +/// +/// Also you must provide proper implementation to Helper class +/// which will extend BigIntegerHelper and must implement +/// BigIntegerHelper::bigIntegerToByte +/// function. The base function returns empty byte. +/// + /// /// \brief Default exponent for RSA public key @@ -73,21 +95,48 @@ class AES { static const unsigned int kDefaultPublicExponent = 65537; /// -/// Declaration for byte in case it's not already included +/// \brief Declaration for byte in case it's not already included /// using byte = unsigned char; +/// +/// \brief Simple raw string (a.k.a octet string) +/// +using RawString = std::vector; + /// /// \brief Contains helper functions for RSA throughout /// -template -class VirtualHelper { +template +class BigIntegerHelper { public: + static const BigInteger kBigInteger256; + + BigIntegerHelper() = default; + virtual ~BigIntegerHelper() = default; + + /// + /// \brief Specific base to specified base + /// \param n Number + /// \param b Target base (default: 16 - Hex) + /// + virtual BigInteger changeBase(BigInteger n, BigInteger b = 16) const + { + BigInteger r, i = 1, o = 0; + while (n != 0) { + r = n % b; + n /= b; + o += r * i; + i *= 10; + } + return o; + } + /// /// \brief Implementation for (a ^ -1) mod b /// - static BigInteger modInverse(BigInteger a, BigInteger b) + virtual BigInteger modInverse(BigInteger a, BigInteger b) const { BigInteger b0 = b, t, q; BigInteger x0 = 0, x1 = 1; @@ -113,7 +162,7 @@ class VirtualHelper { /// \brief Fast GCD /// \see https://en.wikipedia.org/wiki/Euclidean_algorithm#Extended_Euclidean_algorithm /// - static BigInteger gcd(BigInteger a, BigInteger b) + virtual BigInteger gcd(BigInteger a, BigInteger b) const { BigInteger c; while (a != 0) { @@ -124,80 +173,13 @@ class VirtualHelper { return b; } - /// - /// \brief Octet string to integer - /// - static inline BigInteger os2ip(const BigInteger& x) - { - return os2ip(getByteArray(x)); - } - - /// - /// Octet-string to integer - /// - template - static BigInteger os2ip(const std::vector& x) - { - const BigInteger b256 = 256; - - BigInteger result = 0; - std::size_t len = x.size(); - for (std::size_t i = len; i > 0; --i) { - result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); - } - return result; - } - - /// - /// \brief Integer to octet-string - /// - static BigInteger i2osp(const BigInteger& x, int xlen) - { - return i2osp(getByteArray(x), xlen); - } - - /// - /// \brief Integer to octet-string - /// - template - static BigInteger i2osp(const std::vector& x, int xlen) - { - const BigInteger b256 = 256; - - BigInteger result = 0; - std::size_t len = x.size(); - for (std::size_t i = len; i > 0; --i) { - result += BigInteger(x[i - 1]) * power(b256, BigInteger(len - i)); - } - // TODO: Fix this! - return result; - } - - /// - /// \brief Specific base to specified base - /// \param n Number - /// \param b Target base (default: 16 - Hex) - /// - template - static T changeBase(T n, T b = 16) - { - T r, i = 1, o = 0; - while (n != 0) { - r = n % b; - n /= b; - o += r * i; - i *= 10; - } - return o; - } - /// /// \brief Simple (b ^ e) mod m implementation /// \param b Base /// \param e Exponent /// \param m Mod /// - static BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) + virtual BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) const { BigInteger res = 1; while (e > 0) { @@ -213,7 +195,7 @@ class VirtualHelper { /// /// \brief Power of numb i.e, b ^ e /// - static BigInteger power(BigInteger b, BigInteger e) + virtual BigInteger power(BigInteger b, BigInteger e) const { BigInteger result = 1; while (e > 0) { @@ -246,7 +228,10 @@ class VirtualHelper { return result; } - static unsigned int countBits(BigInteger b) + /// + /// \brief Counts number of bits in big integer + /// + virtual unsigned int countBits(BigInteger b) const { unsigned int bits = 0; while (b > 0) { @@ -257,56 +242,150 @@ class VirtualHelper { } /// - /// \brief Get byte array from big integer + /// \brief Count number of bytes in big integer + /// + virtual unsigned int countBytes(BigInteger b) const + { + return countBits(b) * 8; + } + + /// + /// Raw-string to integer (a.k.a os2ip) + /// + BigInteger rawStringToInteger(const RawString& x) const + { + BigInteger result = 0; + std::size_t len = x.size(); + for (std::size_t i = len; i > 0; --i) { + result += BigInteger(x[i - 1]) * power(kBigInteger256, BigInteger(len - i)); + } + return result; + } + + /// + /// \brief Convert integer to raw string (a.k.a i2osp) /// - template - static std::vector getByteArray(BigInteger x, int xlen = -1) + RawString integerToRaw(BigInteger x, int xlen = -1) const { - const BigInteger b256 = 256; - xlen = xlen == -1 ? countBits(x) * 8 : xlen; + xlen = xlen == -1 ? countBytes(x) : xlen; - std::vector ba(xlen); + RawString ba(xlen); BigInteger r; BigInteger q; - for (int i = 1; i <= xlen; ++i) { - BigInteger e = power(b256, BigInteger(xlen - i)); - q = x / e; - r = x % e; - ba[i - 1] = static_cast(q.ConvertToLong()); + int i = 1; + + for (; i <= xlen; ++i) { + divideBigNumber(x, power(kBigInteger256, BigInteger(xlen - i)), &q, &r); + ba[i - 1] = bigIntegerToByte(q); x = r; } return ba; } + + /// + /// \brief Divides big number + /// You may override this function and call custom divisor from big integer class + /// you are using. + /// Result should be stored in quotient and remainder + /// + virtual inline void divideBigNumber(const BigInteger& divisor, const BigInteger& divident, + BigInteger* quotient, BigInteger* remainder) const + { + *quotient = divisor / divident; + *remainder = divisor % divident; + } + + /// + /// Absolutely must override this - conversion from x to single byte + /// + virtual inline byte bigIntegerToByte(const BigInteger& x) const + { + return static_cast(0); + } + + /// + /// \brief Converts big integer to hex + /// + virtual inline std::string bigIntegerToHex(const BigInteger& b) const + { + std::stringstream ss; + ss << std::hex << b; + return ss.str(); + } + + /// + /// \brief Converts big integer to hex + /// + virtual inline std::string bigIntegerToString(const BigInteger& b) const + { + std::stringstream ss; + ss << b; + return ss.str(); + } + + /// + /// \brief Converts hex to big integer + /// \param hex Hexadecimal without '0x' prefix + /// + virtual inline BigInteger hexToBigInteger(const std::string& hex) const + { + std::string readableMsg = "0x" + hex; + BigInteger msg; + std::istringstream iss(readableMsg); + iss >> std::hex >> msg; + return msg; + } }; +/// +/// \brief Big Integer = 256 (static declaration) +/// template -class VirtualPublicKey { +const BigInteger BigIntegerHelper::kBigInteger256 = 256; + +/// +/// \brief Public key object with generic big integer +/// +template > +class GenericPublicKey { public: - VirtualPublicKey() = default; - VirtualPublicKey(BigInteger n, int e) : + GenericPublicKey() = default; + + GenericPublicKey(BigInteger n, int e) : m_n(n), m_e(e) { + m_k = m_helper.countBytes(m_n); + if (m_k < 11) { + throw std::invalid_argument("Invalid prime. Length error."); + } } - virtual ~VirtualPublicKey() = default; + virtual ~GenericPublicKey() = default; inline BigInteger n() const { return m_n; } inline int e() const { return m_e; } + inline unsigned int k() const { return m_k; } protected: + BigIntegerHelper m_helper; BigInteger m_n; int m_e; + unsigned int m_k; }; -template -class VirtualRawKey { +/// +/// \brief Private key object with generic big integer +/// +template > +class GenericPrivateKey { public: - using Helper = VirtualHelper; - VirtualRawKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) : + GenericPrivateKey() = default; + + GenericPrivateKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) : m_p(p), m_q(q), m_e(e) @@ -319,22 +398,27 @@ class VirtualRawKey { const BigInteger qMinus1 = m_q - 1; const BigInteger phi = pMinus1 * qMinus1; - if (Helper::gcd(m_e, phi) != 1) { + if (m_helper.gcd(m_e, phi) != 1) { throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); } m_n = m_p * m_q; - m_coeff = Helper::modInverse(m_q, m_p); + m_k = m_helper.countBytes(m_n); + if (m_k < 11) { + throw std::invalid_argument("Invalid prime. Length error."); + } + m_coeff = m_helper.modInverse(m_q, m_p); - m_d = Helper::modInverse(m_e, phi); + m_d = m_helper.modInverse(m_e, phi); // note: - // https://www.ipa.go.jp/security/rfc/RFC3447EN.html#2 says to use m_e - // openssl says to use m_d + // https://tools.ietf.org/html/rfc3447#section-2 says to use m_e + // openssl says to use m_d - which one?! + // m_dp = BigInteger(m_d) % pMinus1; m_dq = BigInteger(m_d) % qMinus1; } - virtual ~VirtualRawKey() = default; + virtual ~GenericPrivateKey() = default; inline BigInteger p() const { return m_p; } inline BigInteger q() const { return m_q; } @@ -344,8 +428,41 @@ class VirtualRawKey { inline BigInteger d() const { return m_d; } inline BigInteger dp() const { return m_dq; } inline BigInteger dq() const { return m_dp; } + inline int k() const { return m_k; } + + friend std::ostream& operator<<(std::ostream& ss, const GenericPrivateKey& k) + { + ss << "modulus: " << k.m_n << "\npublicExponent: " << k.m_e << "\nprivateExponent: " << k.m_d + << "\nprime1: " << k.m_p << "\nprime2: " << k.m_q << "\nexponent1: " << k.m_dp << "\nexponent2: " + << k.m_dq << "\ncoefficient: " << k.m_coeff; + return ss; + } + /// + /// \brief You can use this to export the key via + /// openssl-cli using + /// openssl asn1parse -genconf exported.asn -out imp.der + /// openssl rsa -in imp.der -inform der -text -check + /// \return + /// + virtual std::string exportASNSequence() const + { + std::stringstream ss; + ss << "asn1=SEQUENCE:rsa_key\n\n"; + ss << "[rsa_key]\n"; + ss << "version=INTEGER:0\n"; + ss << "modulus=INTEGER:" << m_helper.bigIntegerToString(m_n) << "\n"; + ss << "pubExp=INTEGER:" << m_e << "\n"; + ss << "privExp=INTEGER:" << m_helper.bigIntegerToString(m_d) << "\n"; + ss << "p=INTEGER:" << m_helper.bigIntegerToString(m_p) << "\n"; + ss << "q=INTEGER:" << m_helper.bigIntegerToString(m_q) << "\n"; + ss << "e1=INTEGER:" << m_helper.bigIntegerToString(m_dp) << "\n"; + ss << "e2=INTEGER:" << m_helper.bigIntegerToString(m_dq) << "\n"; + ss << "coeff=INTEGER:" << m_helper.bigIntegerToString(m_coeff); + return ss.str(); + } protected: + Helper m_helper; BigInteger m_p; BigInteger m_q; int m_e; @@ -354,97 +471,97 @@ class VirtualRawKey { BigInteger m_d; BigInteger m_dp; BigInteger m_dq; + unsigned int m_k; }; -template -using VirtualPrivateKey = VirtualRawKey ; - -template -class VirtualKeyPair : public VirtualRawKey { +/// +/// \brief Key pair (containing public and private key objects) with generic big integer +/// +template > +class GenericKeyPair { public: - VirtualKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) : - VirtualRawKey(p, q, exp) { - m_publicKey = VirtualPublicKey(p * q, exp); + GenericKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) + { + m_publicKey = GenericPublicKey(p * q, exp); + m_privateKey = GenericPrivateKey(p, q, exp); } - inline const VirtualPublicKey* publicKey() const { return &m_publicKey; } - inline const VirtualPrivateKey* privateKey() const { return this; } + inline const GenericPublicKey* publicKey() const { return &m_publicKey; } + inline const GenericPrivateKey* privateKey() const { return &m_privateKey; } protected: - VirtualPublicKey m_publicKey; + GenericPublicKey m_publicKey; + GenericPrivateKey m_privateKey; }; /// /// \brief Provides RSA crypto functionalities /// -template -class VirtualRSA { +template > +class GenericRSA { public: - using Helper = VirtualHelper; + using PublicKey = GenericPublicKey; + using PrivateKey = GenericPrivateKey; + + GenericRSA() = default; + GenericRSA(const GenericRSA&) = default; + GenericRSA& operator=(const GenericRSA&) = default; /// - /// \brief Generic RSA encryption. T can of std::string or std::wstring - /// or custom similar type - /// - /// \return hex of final octet string + /// \brief Encrypts plain bytes using RSA public key + /// \param publicKey RSA Public key for encryption + /// \param m The message. This can be raw bytes or plain text + /// T can of std::string or std::wstring or custom string type that has + /// basic_stringstream implementation alongside it + /// \note Mine uses pkcs#1 padding scheme + /// \return hex of cipher /// template - std::string encrypt(const VirtualPublicKey* publicKey, const T& m) + std::string encrypt(const PublicKey* publicKey, const T& m) { - BigInteger paddedMsg = pkcs1pad2(m, (Helper::countBits(publicKey->n()) + 7) >> 3); - std::stringstream ss; - ss << std::hex << Helper::powerMod(paddedMsg, publicKey->e(), publicKey->n()); - std::string h(ss.str()); - h.erase(h.end() - 1); - return ((h.size() & 1) == 0) ? h : ("0" + h); + BigInteger paddedMsg = pkcs1pad2(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); + BigInteger cipher = m_helper.powerMod(paddedMsg, publicKey->e(), publicKey->n()); + return m_helper.bigIntegerToHex(cipher); } /// - /// \brief Encrypts wstring msg using public key. - /// - /// \return hex of cipher. Padded using PKCS#1 padding scheme + /// \brief Helper method to encrypt wide-string messages using public key. + /// \see encrypt(const GenericPublicKey* publicKey, const T& m) /// - std::string encrypt(const VirtualPublicKey* publicKey, + std::string encrypt(const PublicKey* publicKey, const std::wstring& message) { return encrypt(publicKey, message); } /// - /// \brief Encrypts string msg using public key + /// \brief Helper method to encrypt std::string messages using public key. + /// \see encrypt(const GenericPublicKey* publicKey, const T& m) /// - /// \return hex of cipher. Padded using PKCS#1 padding scheme - /// - std::string encrypt(const VirtualPublicKey* publicKey, + std::string encrypt(const PublicKey* publicKey, const std::string& message) { return encrypt(publicKey, message); } /// - /// \brief Decrypts RSA hex message m using private key - /// \param cipher Cipher in hex format (should not start with 0x) - /// \return Plain text, return type depends on TResult + /// \brief Decrypts RSA hex message using RSA private key + /// \param privateKey RSA private key + /// \param c Cipher in hex format (should not start with 0x) + /// \return Plain result of TResult type /// template - TResult decrypt(const VirtualPrivateKey* privateKey, const std::string& cipher) + TResult decrypt(const PrivateKey* privateKey, const std::string& c) { - // TODO: Add checks https://tools.ietf.org/html/rfc3447#section-7.2.2 - - std::string readableMsg = "0x" + cipher; - //BigInteger msg(readableMsg.c_str()); - BigInteger msg; - std::istringstream iss(readableMsg); - iss >> std::hex >> msg; - - // https://tools.ietf.org/html/rfc3447#section-4.1 - int xlen = (Helper::countBits(privateKey->n()) + 7) >> 3; - if (msg >= Helper::power(BigInteger(256), BigInteger(xlen))) { + BigInteger msg = m_helper.hexToBigInteger(c); + int xlen = (m_helper.countBits(privateKey->n()) + 7) >> 3; + if (msg >= m_helper.power(BigInteger(256), BigInteger(xlen))) { throw std::runtime_error("Integer too large"); } - BigInteger decr = Helper::powerMod(msg, privateKey->d(), privateKey->n()); - return pkcs1unpad2(decr, xlen); + BigInteger decr = m_helper.powerMod(msg, privateKey->d(), privateKey->n()); + RawString rawStr = m_helper.integerToRaw(decr, xlen); + return pkcs1unpad2(rawStr); } /// @@ -453,7 +570,7 @@ class VirtualRSA { /// \param signature Signature in hex /// \see https://tools.ietf.org/html/rfc3447#section-8.1.2 /// - bool verify(const VirtualPublicKey* publicKey, const std::string& message, + bool verify(const PublicKey* publicKey, const std::string& message, const std::string& signature) { /* @@ -479,13 +596,9 @@ class VirtualRSA { return true; - std::string readableSign = "0x" + signature; - BigInteger sign(readableSign.c_str()); + BigInteger sign = m_helper.hexToBigInteger(signature); try { BigInteger vp = createVerificationPrimitive(publicKey, sign); - // I2OSP - - std::cout << vp << std::endl; const int xlen = (publicKey->n().BitCount() + 7) >> 3; @@ -493,10 +606,7 @@ class VirtualRSA { // throw std::runtime_error("Integer too large"); // Needed??! Where in RFC? } - BigInteger em = i2osp(vp, xlen); - - // manually test - std::string encr = encrypt(publicKey, message); + std::vector em = m_helper.getByteArray(vp, xlen); // todo: add check for following (as per https://tools.ietf.org/html/rfc3447#section-8.1.2) @@ -508,27 +618,16 @@ class VirtualRSA { - std::cout << em << " " << encr << std::endl; + std::cout << "tt" << std::endl; return true; } catch (std::exception&) { - }*/ - + } +*/ return true; } - /// - /// \brief Singleton instance - /// - inline static VirtualRSA& instance() - { - static VirtualRSA s_instance; - return s_instance; - } - private: - VirtualRSA() = default; - VirtualRSA(const VirtualRSA&) = default; - VirtualRSA& operator=(const VirtualRSA&) = delete; + Helper m_helper; /// /// \brief PKCS #1 padding @@ -540,44 +639,50 @@ class VirtualRSA { if (n < s.size() + 11) { throw std::runtime_error("Message too long"); } - std::vector byteArray(n); + RawString byteArray(n); long long i = s.size() - 1; while(i >= 0 && n > 0) { int c = static_cast(s.at(i--)); - if (c < 128) { + if (c <= 0x7f) { // utf byteArray[--n] = c; - } else if ((c > 127) && (c < 2048)) { - // 16-bit + } else if (c <= 0x7ff) { byteArray[--n] = (c & 63) | 128; byteArray[--n] = (c >> 6) | 192; - } else { - // 24-bit + } else if (c <= 0xffff) { + // utf-16 byteArray[--n] = (c & 63) | 128; byteArray[--n] = ((c >> 6) & 63) | 128; byteArray[--n] = (c >> 12) | 224; + } else { + // utf-32 + byteArray[--n] = (c & 63) | 128; + byteArray[--n] = ((c >> 6) & 63) | 128; + byteArray[--n] = ((c >> 12) & 63) | 128; + byteArray[--n] = (c >> 18) | 240; } } // now padding i.e, 0x00 || 0x02 || PS || 0x00 // see point #2 on https://tools.ietf.org/html/rfc3447#section-7.2.1 => EME-PKCS1-v1_5 encoding + const int kLengthOfRandom = 127; + byteArray[--n] = 0; - // todo: check if there are any more specs for randoms in standard srand(time(NULL)); - int r = rand() % 100 + 1; + int r = rand() % kLengthOfRandom + 1; while (n > 2) { r = 0; while (r == 0) { - r = rand() % 100 + 1; + r = rand() % kLengthOfRandom + 1; } byteArray[--n] = r; } // first two bytes of padding are 0x2 (second) and 0x0 (first) byteArray[--n] = 2; byteArray[--n] = 0; - return Helper::i2osp(byteArray, n); + return m_helper.rawStringToInteger(byteArray); } /// @@ -586,9 +691,8 @@ class VirtualRSA { /// \return corresponding octet string of length n /// template - T pkcs1unpad2(const BigInteger& m, unsigned long n) + T pkcs1unpad2(const RawString& ba) { - std::vector ba = Helper::getByteArray(m, n); std::size_t baLen = ba.size(); if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { throw std::runtime_error("Incorrect padding PKCS#1"); @@ -609,19 +713,34 @@ class VirtualRSA { // now we should be at the first non-zero byte // which is our first item, concat them as char | wchar_t - std::basic_stringstream ss; + using CharacterType = typename T::value_type; + std::basic_stringstream ss; for (; i < baLen; ++i) { // reference: http://en.cppreference.com/w/cpp/language/types -> range of values int c = ba[i] & 0xFF; if (c < 128) { - // utf-8 - ss << static_cast(c); - } else if ((c > 191) && (c < 224)) { // 16-bit char - ss << static_cast(((c & 31) << 6) | (ba[i+1] & 63)); + ss << static_cast(c); + } else if (c > 191 && c < 224) { + ss << static_cast( + ((c & 31) << 6) | + (ba[i+1] & 63) + ); ++i; - } else { // 24-bit char - ss << static_cast(((c & 15) << 12) | ((ba[i+1] & 63) << 6) | (ba[i+2] & 63)); + } else if ((c < 191) || (c >= 224 && c < 240)) { // utf-16 char + ss << static_cast( + ((c & 15) << 12) | + ((ba[i+1] & 63) << 6) | + (ba[i+2] & 63) + ); i += 2; + } else { // utf-32 char + ss << static_cast( + ((c & 7) << 18) | + ((ba[i+1] & 63) << 12) | + ((ba[i+2] & 63) << 6) | + (ba[i+3] & 63) + ); + i += 3; } } return ss.str(); @@ -633,12 +752,12 @@ class VirtualRSA { /// \return message representative, an integer between 0 and n - 1 /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 /// - BigInteger createVerificationPrimitive(const VirtualPublicKey* publicKey, const BigInteger& signature) + BigInteger createVerificationPrimitive(const PublicKey* publicKey, const BigInteger& signature) { if (signature < 0 || signature > publicKey->n() - 1) { throw std::runtime_error("signature representative out of range"); } - return powerMod(signature, publicKey->e(), publicKey->n()); + return m_helper.powerMod(signature, publicKey->e(), publicKey->n()); } // for tests @@ -648,36 +767,6 @@ class VirtualRSA { friend class RSATest_PowerMod_Test; }; -/// -/// A macro that you can use to use specific version of big integer -/// implementation. -/// -/// Big integer that is used here must have following public functions -/// - operator-() [subtraction] -/// - operator+() [addition] -/// - operator+=() [short-hand addition] -/// - operator*() [multiply] -/// - operator/() [divide] -/// - operator%() [mod] -/// - operator>>() [right-shift] -/// - operator>>=() [short-hand right-shift] -/// - long ConvertToLong() -/// - support for std::hex -/// -#define DECLARE_MINE_RSA(BIG_INTEGER_IMPLEMENTATION) \ -using BigInteger = BIG_INTEGER_IMPLEMENTATION; \ -class Helper : public VirtualHelper{}; \ -class RSA : public VirtualRSA{}; \ -class KeyPair : public VirtualKeyPair{ \ -public: \ - KeyPair(const BigInteger& p, const BigInteger& q, \ - unsigned int exp = kDefaultPublicExponent) : \ - VirtualKeyPair(p, q, exp) {} \ -}; \ -class RawKey : public VirtualRawKey{}; \ -class PublicKey : public VirtualPublicKey{}; \ -class PrivateKey : public VirtualPrivateKey{}; \ - /// /// \brief Provides Zlib functionality for inflate and deflate diff --git a/src/rsa.h b/src/rsa.h index aa82b0b..5d122d5 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -22,7 +22,6 @@ #include #include #include -#include #include #include From 3415a1446232576340b56c4bb862a415cd15185e Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 23 Aug 2017 17:03:46 +1000 Subject: [PATCH 029/148] change to hex --- src/rsa.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rsa.h b/src/rsa.h index 5d122d5..fe1ad10 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -683,15 +683,15 @@ class GenericRSA { for (; i < baLen; ++i) { // reference: http://en.cppreference.com/w/cpp/language/types -> range of values int c = ba[i] & 0xFF; - if (c < 128) { + if (c <= 0x7f) { ss << static_cast(c); - } else if (c > 191 && c < 224) { + } else if (c > 0xbf && c < 0xe0) { ss << static_cast( ((c & 31) << 6) | (ba[i+1] & 63) ); ++i; - } else if ((c < 191) || (c >= 224 && c < 240)) { // utf-16 char + } else if ((c < 0xbf) || (c >= 0xe0 && c < 0xf0)) { // utf-16 char ss << static_cast( ((c & 15) << 12) | ((ba[i+1] & 63) << 6) | From 95970547a0c47833e538fe0f7225356d26890e75 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Wed, 23 Aug 2017 18:43:54 +1000 Subject: [PATCH 030/148] minor up --- src/rsa.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rsa.h b/src/rsa.h index fe1ad10..f9ad0fe 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -32,7 +32,7 @@ namespace mine { /// User will provide their own implementation of big integer /// or use existing one. /// -/// This is implemeted from instructions on +/// Compliant with PKCS#1 (v2.1) /// https://tools.ietf.org/html/rfc3447#section-7.2 /// /// Mine uses pkcs#1 v1.5 padding scheme From 916a5cdeaf32849bf0a203af73cdd3fe61958f09 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Wed, 23 Aug 2017 21:01:07 +1000 Subject: [PATCH 031/148] minor updates --- src/rsa.h | 130 +++++++++++++++++++++++++++++++++++++----------- test/rsa-test.h | 16 ++---- 2 files changed, 105 insertions(+), 41 deletions(-) diff --git a/src/rsa.h b/src/rsa.h index f9ad0fe..3a5798f 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -301,6 +301,9 @@ class BigIntegerHelper { iss >> std::hex >> msg; return msg; } +private: + BigIntegerHelper(const BigIntegerHelper&) = delete; + BigIntegerHelper& operator=(const BigIntegerHelper&) = delete; }; /// @@ -318,6 +321,23 @@ class GenericPublicKey { GenericPublicKey() = default; + GenericPublicKey(const GenericPublicKey& other) + { + this->m_n = other.m_n; + this->m_e = other.m_e; + this->m_k = other.m_k; + } + + GenericPublicKey& operator=(const GenericPublicKey& other) + { + if (this != &other) { + this->m_n = other.m_n; + this->m_e = other.m_e; + this->m_k = other.m_k; + } + return *this; + } + GenericPublicKey(BigInteger n, int e) : m_n(n), m_e(e) @@ -350,38 +370,68 @@ class GenericPrivateKey { GenericPrivateKey() = default; + GenericPrivateKey(const GenericPrivateKey& other) + { + this->m_p = other.m_p; + this->m_q = other.m_q; + this->m_e = other.m_e; + this->m_n = other.m_n; + this->m_d = other.m_d; + this->m_coeff = other.m_coeff; + this->m_dp = other.m_dp; + this->m_dq = other.m_dq; + this->m_k = other.m_k; + } + + GenericPrivateKey& operator=(const GenericPrivateKey& other) + { + if (this != &other) { + this->m_p = other.m_p; + this->m_q = other.m_q; + this->m_e = other.m_e; + this->m_n = other.m_n; + this->m_d = other.m_d; + this->m_coeff = other.m_coeff; + this->m_dp = other.m_dp; + this->m_dq = other.m_dq; + this->m_k = other.m_k; + } + return *this; + } + GenericPrivateKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) : m_p(p), m_q(q), m_e(e) - { - if (p == q || p == 0 || q == 0) { - throw std::invalid_argument("p and q must be prime numbers unique to each other"); - } - - const BigInteger pMinus1 = m_p - 1; - const BigInteger qMinus1 = m_q - 1; - const BigInteger phi = pMinus1 * qMinus1; - - if (m_helper.gcd(m_e, phi) != 1) { - throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); - } - m_n = m_p * m_q; - m_k = m_helper.countBytes(m_n); - if (m_k < 11) { - throw std::invalid_argument("Invalid prime. Length error."); - } - m_coeff = m_helper.modInverse(m_q, m_p); - - m_d = m_helper.modInverse(m_e, phi); - - // note: - // https://tools.ietf.org/html/rfc3447#section-2 says to use m_e - // openssl says to use m_d - which one?! - // - m_dp = BigInteger(m_d) % pMinus1; - m_dq = BigInteger(m_d) % qMinus1; - } + + { + if (p == q || p == 0 || q == 0) { + throw std::invalid_argument("p and q must be prime numbers unique to each other"); + } + + const BigInteger pMinus1 = m_p - 1; + const BigInteger qMinus1 = m_q - 1; + const BigInteger phi = pMinus1 * qMinus1; + + if (m_helper.gcd(m_e, phi) != 1) { + throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); + } + m_n = m_p * m_q; + m_k = m_helper.countBytes(m_n); + if (m_k < 11) { + throw std::invalid_argument("Invalid prime. Length error."); + } + m_coeff = m_helper.modInverse(m_q, m_p); + + m_d = m_helper.modInverse(m_e, phi); + + // note: + // https://tools.ietf.org/html/rfc3447#section-2 says to use m_e + // openssl says to use m_d - which one?! + // + m_dp = BigInteger(m_d) % pMinus1; + m_dq = BigInteger(m_d) % qMinus1; + } virtual ~GenericPrivateKey() = default; @@ -445,12 +495,31 @@ class GenericPrivateKey { template > class GenericKeyPair { public: + GenericKeyPair() = default; + + GenericKeyPair(const GenericKeyPair& other) + { + this->m_privateKey = other.m_privateKey; + this->m_publicKey = other.m_publicKey; + } + + GenericKeyPair& operator=(const GenericKeyPair& other) + { + if (this != &other) { + this->m_privateKey = other.m_privateKey; + this->m_publicKey = other.m_publicKey; + } + return *this; + } + GenericKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) { m_publicKey = GenericPublicKey(p * q, exp); m_privateKey = GenericPrivateKey(p, q, exp); } + virtual ~GenericKeyPair() = default; + inline const GenericPublicKey* publicKey() const { return &m_publicKey; } inline const GenericPrivateKey* privateKey() const { return &m_privateKey; } @@ -470,8 +539,8 @@ class GenericRSA { using PrivateKey = GenericPrivateKey; GenericRSA() = default; - GenericRSA(const GenericRSA&) = default; - GenericRSA& operator=(const GenericRSA&) = default; + GenericRSA(const GenericRSA&) = delete; + GenericRSA& operator=(const GenericRSA&) = delete; /// /// \brief Encrypts plain bytes using RSA public key @@ -680,6 +749,7 @@ class GenericRSA { using CharacterType = typename T::value_type; std::basic_stringstream ss; + for (; i < baLen; ++i) { // reference: http://en.cppreference.com/w/cpp/language/types -> range of values int c = ba[i] & 0xFF; diff --git a/test/rsa-test.h b/test/rsa-test.h index 26df090..667a2ef 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -37,18 +37,12 @@ class Helper : public BigIntegerHelper } }; -class RSA : public GenericRSA{ - -}; - -class KeyPair : public GenericKeyPair{ -public: - KeyPair(const BigInteger& p, const BigInteger& q, - unsigned int exp = kDefaultPublicExponent) : - GenericKeyPair(p, q, exp) {} +class RSA : public GenericRSA {}; +class PublicKey : public GenericPublicKey {}; +class PrivateKey : public GenericPrivateKey {}; +class KeyPair : public GenericKeyPair { + using GenericKeyPair::GenericKeyPair; }; -class PublicKey : public GenericPublicKey{}; -class PrivateKey : public GenericPrivateKey{}; static RSA rsaManager; static Helper rsaHelper; From fa53e827b115efbb1f6f31bd6ac5dc50645b318d Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Wed, 23 Aug 2017 22:11:19 +1000 Subject: [PATCH 032/148] b64 skelton --- src/base64.cc | 2 ++ src/base64.h | 61 ++++++++++++++++++++++++++++++++++++++++++++++ test/base64-test.h | 46 ++++++++++++++++++++++++++++++++++ test/main.cc | 2 +- 4 files changed, 110 insertions(+), 1 deletion(-) diff --git a/src/base64.cc b/src/base64.cc index 2696aa8..89ee126 100644 --- a/src/base64.cc +++ b/src/base64.cc @@ -14,3 +14,5 @@ #include "src/base64.h" using namespace mine; + +const std::string Base64::kValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; diff --git a/src/base64.h b/src/base64.h index 02fec42..cb6c9eb 100644 --- a/src/base64.h +++ b/src/base64.h @@ -18,14 +18,75 @@ #ifndef Base64_H #define Base64_H +#include +#include +#include + namespace mine { +using byte = unsigned char; /// /// \brief Provides base64 encoding / decoding /// class Base64 { public: + static const std::string kValidChars; // this also include padding char (=) + static const char kPaddingChar = '='; + + /// + /// \brief Encodes input of length to base64 encoding + /// + static std::string base64Encode(const std::string& d) + { + std::stringstream ss; + for (auto it = d.begin(); it != d.end(); ++it) { + + } + return ss.str(); + } + + /// + /// \brief Decodes encoded base64 + /// + static std::string base64Decode(const std::string& e) + { + std::stringstream ss; + for (auto it = e.begin(); it != e.end(); ++it) { + + } + return ss.str(); + } + + /// + /// \brief expectedBase64Length Returns expected base64 length + /// \param n Length of input (plain data) + /// + inline static std::size_t expectedBase64Length(std::size_t n) + { + return ((4 * n / 3) + 3) & ~0x03; + } + + /// + /// \brief Finds whether data is base64 encoded. This is done + /// by finding non-base64 character. So it is not necessary + /// a valid base64 encoding. + /// + inline static bool isBase64(const std::string& data) + { + return data.find_first_not_of(kValidChars) == std::string::npos; + } + + /// + /// \brief Finds whether data is base64 encoded. This is done + /// by finding non-base64 character. So it is not necessary + /// a valid base64 encoding. + /// + inline static bool isBase64(byte c) + { + return std::isalnum(c) || c == 0x2b || c == 0x2b; + } + private: Base64() = delete; Base64(const Base64&) = delete; diff --git a/test/base64-test.h b/test/base64-test.h index 6f26026..afcf2a4 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -6,6 +6,52 @@ namespace mine { +static TestData Base64TestData = { + TestCase("cGxhaW4gdGV4dA==", "plain text"), + TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"), + TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", "this is rocket 🚀 and i love it"), +}; + +static TestData IsBase64Data = { + TestCase("da024686f7f2da49da6c98253b42fe1c:erezutlgudgbtwza:i3eclcagfnUbK1B==", false), + TestCase("da024686f7f2da49da6c98253b42fe1c:i3eclcagfnUbK1B==", false), + TestCase("erezutlgudgbtwza:i3eclcagfnUbK1B==", false), + TestCase("i3eclcagfnUbK1B==", true), +}; + +TEST(Base64Test, Encode) +{ + for (const auto& item : Base64TestData) { + std::string encoded = Base64::base64Encode(PARAM(1)); + ASSERT_EQ(PARAM(0), encoded); + } +} + +TEST(Base64Test, Decode) +{ + for (const auto& item : Base64TestData) { + std::string decoded = Base64::base64Decode(PARAM(0)); + ASSERT_EQ(PARAM(1), decoded); + } +} + +TEST(Base64Test, ExpectedSize) +{ + for (const auto& item : Base64TestData) { + std::size_t s = Base64::expectedBase64Length(PARAM(1).size()); + ASSERT_EQ(PARAM(0).size(), s); + } +} + +TEST(Base64Test, IsBase64) +{ + for (const auto& item : IsBase64Data) { + auto first = PARAM(0); + auto second = PARAM(1); + ASSERT_EQ(Base64::isBase64(first), second); + } +} + } #endif // BASE64_TEST_H diff --git a/test/main.cc b/test/main.cc index 539fb7d..60a821b 100644 --- a/test/main.cc +++ b/test/main.cc @@ -3,7 +3,7 @@ #include "base64-test.h" #include "aes-test.h" #include "zlib-test.h" -#include "rsa-test.h" +//#include "rsa-test.h" INITIALIZE_EASYLOGGINGPP From e38d8d8bc5cd61b2c7ea4118b9e0a96211f5e52d Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Wed, 23 Aug 2017 23:22:47 +1000 Subject: [PATCH 033/148] base64 encoding impl --- src/base64.h | 30 +++++++++++++++++++++++++++--- test/base64-test.h | 17 ++++++++++------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/base64.h b/src/base64.h index cb6c9eb..fea110b 100644 --- a/src/base64.h +++ b/src/base64.h @@ -37,13 +37,37 @@ class Base64 { /// /// \brief Encodes input of length to base64 encoding /// - static std::string base64Encode(const std::string& d) + static std::string base64Encode(const std::wstring& d) { + std::string padding; + const std::size_t len = d.length(); + const int paddingCount = (len % 0x03) + 1; + int i = 0; + for (; i < paddingCount; ++i) { + padding += kPaddingChar; + } + i = 0; std::stringstream ss; - for (auto it = d.begin(); it != d.end(); ++it) { + for (auto it = d.begin(); it <= d.end(); ++it, i += 4) { + int c = static_cast(*it) & 0xff; + if (c >= 240) { + throw std::invalid_argument("UTF-32 base64 encoding not supported yet"); + } + int n24 = (*it << 16); + if (++it != d.end()) { + n24 |= (*it << 8); + } + if (++it != d.end()) { + n24 |= *it; + } + + ss << static_cast(kValidChars[(n24 >> 18) & 0x3f]); + ss << static_cast(kValidChars[(n24 >> 12) & 0x3f]); + ss << static_cast(kValidChars[(n24 >> 6) & 0x3f]); + ss << static_cast(kValidChars[n24 & 0x3f]); } - return ss.str(); + return std::string(ss.str().substr(0, i - paddingCount)) + padding; } /// diff --git a/test/base64-test.h b/test/base64-test.h index afcf2a4..b066b51 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -6,10 +6,12 @@ namespace mine { -static TestData Base64TestData = { - TestCase("cGxhaW4gdGV4dA==", "plain text"), - TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"), - TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", "this is rocket 🚀 and i love it"), +static TestData Base64TestData = { + TestCase("cGxhaW4gdGV4dA==", L"plain text"), + TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"), + TestCase("SGVsbG/nq5wK", L"Hello竜"), + TestCase("5aSn5a625aW9Cg==", L"大家好"), + TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", L"this is rocket 🚀 and i love it"), }; static TestData IsBase64Data = { @@ -23,21 +25,22 @@ TEST(Base64Test, Encode) { for (const auto& item : Base64TestData) { std::string encoded = Base64::base64Encode(PARAM(1)); - ASSERT_EQ(PARAM(0), encoded); + ASSERT_STREQ(PARAM(0).c_str(), encoded.c_str()); } } TEST(Base64Test, Decode) { for (const auto& item : Base64TestData) { - std::string decoded = Base64::base64Decode(PARAM(0)); - ASSERT_EQ(PARAM(1), decoded); + //std::string decoded = Base64::base64Decode(PARAM(0)); + //ASSERT_STREQ(PARAM(1), decoded); } } TEST(Base64Test, ExpectedSize) { for (const auto& item : Base64TestData) { + LOG(INFO) << PARAM(1) << " => " << PARAM(1).size(); std::size_t s = Base64::expectedBase64Length(PARAM(1).size()); ASSERT_EQ(PARAM(0).size(), s); } From 3125da710c2a62284b96f72d9976d4d7db148da4 Mon Sep 17 00:00:00 2001 From: mkhan Date: Thu, 24 Aug 2017 16:23:35 +1000 Subject: [PATCH 034/148] base64 re-impl --- src/base64.cc | 76 ++++++++++++++++++++++++++++++++++++++++++++++ src/base64.h | 48 +++++++++-------------------- test/base64-test.h | 28 +++++++++++------ 3 files changed, 110 insertions(+), 42 deletions(-) diff --git a/src/base64.cc b/src/base64.cc index 89ee126..742b512 100644 --- a/src/base64.cc +++ b/src/base64.cc @@ -16,3 +16,79 @@ using namespace mine; const std::string Base64::kValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + +std::size_t Base64::countChars(const std::string& str) +{ + std::size_t result = 0UL; + for (auto it = str.begin(); it <= str.end();) { + int c = *it & 0xff; + int charCount = 0; + if (c == 0x0) { + // \0 + ++it; // we increment iter manually + } else if (c <= 0x7f) { + charCount = 1; + } else if (c <= 0x7ff) { + charCount = 2; + } else if (c <= 0xffff) { + charCount = 3; + } else { + charCount = 4; + } + result += charCount; + it += charCount; + } + return result; +} + +std::string Base64::base64Encode(const std::string& raw) +{ + std::string padding; + std::stringstream ss; + for (auto it = raw.begin(); it < raw.end(); it += 3) { + + // we use example of abc + // and let's say we're in the beginning of iterator (i.e, 'a') + // 97 98 99 + // Bits 01100001 01100010 01100011 + // Stream 011000010110001001100011 => decimal: 6382179 + // 24-bit Stream: 011000 010110 001001 100011 + // result indices 24 22 9 35 + // result Y W J j + // + // ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/= + // + + int c = static_cast(*it & 0xff); + ss << static_cast(static_cast(kValidChars[(c >> 2) & 0x3f])); // first 6 bits from first bitset + if (it + 1 < raw.end()) { + int c2 = static_cast(*(it + 1) & 0xff); + ss << static_cast(kValidChars[((c << 4) | // remaining 2 bits from first bitset - shift them left to get 4-bit spaces 010000 + (c2 >> 4) // first 4 bits of second bitset - shift them right to get 2 spaces and bitwise + // to add them 000110 + ) & 0x3f]); // must be within 63 -- + // 010000 + // 000110 + // --|--- + // 010110 + // 111111 + // ---&-- + // 010110 ==> 22 + if (it + 2 < raw.end()) { + int c3 = static_cast(*(it + 2) & 0xff); + ss << static_cast(kValidChars[((c2 << 2) | // remaining 4 bits from second bitset - shift them to get 011000 + (c3 >> 6) // the first 2 bits from third bitset - shift them right to get 000001 + ) & 0x3f]); + // the rest of the explanation is same as above + ss << static_cast(kValidChars[c3 & 0x3f]); // all the remaing bits + } else { + ss << static_cast(kValidChars[(c2 << 2) & 0x3f]); // we have 4 bits left from last byte need space for two 0-bits + ss << "="; + } + } else { + ss << static_cast(kValidChars[(c << 4) & 0x3f]); // remaining 2 bits from single byte + ss << "=="; + } + } + return ss.str() + padding; +} diff --git a/src/base64.h b/src/base64.h index fea110b..ebc9cf6 100644 --- a/src/base64.h +++ b/src/base64.h @@ -21,6 +21,7 @@ #include #include #include +#include namespace mine { @@ -35,40 +36,16 @@ class Base64 { static const char kPaddingChar = '='; /// - /// \brief Encodes input of length to base64 encoding + /// \brief Replacement for better d.size() that consider unicode bytes too + /// \see https://en.wikipedia.org/wiki/UTF-8#Description /// - static std::string base64Encode(const std::wstring& d) - { - std::string padding; - const std::size_t len = d.length(); - const int paddingCount = (len % 0x03) + 1; - int i = 0; - for (; i < paddingCount; ++i) { - padding += kPaddingChar; - } - i = 0; - std::stringstream ss; - for (auto it = d.begin(); it <= d.end(); ++it, i += 4) { - int c = static_cast(*it) & 0xff; - if (c >= 240) { - throw std::invalid_argument("UTF-32 base64 encoding not supported yet"); - } - int n24 = (*it << 16); - if (++it != d.end()) { - n24 |= (*it << 8); - } - if (++it != d.end()) { - n24 |= *it; - } - - ss << static_cast(kValidChars[(n24 >> 18) & 0x3f]); - ss << static_cast(kValidChars[(n24 >> 12) & 0x3f]); - ss << static_cast(kValidChars[(n24 >> 6) & 0x3f]); - ss << static_cast(kValidChars[n24 & 0x3f]); + static std::size_t countChars(const std::string& d); - } - return std::string(ss.str().substr(0, i - paddingCount)) + padding; - } + /// + /// \brief Encodes input of length to base64 encoding + /// \see https://tools.ietf.org/html/rfc1421#section-4.3.2.4 + /// + static std::string base64Encode(const std::string& raw); /// /// \brief Decodes encoded base64 @@ -91,6 +68,11 @@ class Base64 { return ((4 * n / 3) + 3) & ~0x03; } + inline static std::size_t expectedBase64Length(const std::string& str) + { + return expectedBase64Length(countChars(str)); + } + /// /// \brief Finds whether data is base64 encoded. This is done /// by finding non-base64 character. So it is not necessary @@ -108,7 +90,7 @@ class Base64 { /// inline static bool isBase64(byte c) { - return std::isalnum(c) || c == 0x2b || c == 0x2b; + return isalnum(c) || c == 0x2b || c == 0x2b; } private: diff --git a/test/base64-test.h b/test/base64-test.h index b066b51..9a8b180 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -6,18 +6,29 @@ namespace mine { -static TestData Base64TestData = { - TestCase("cGxhaW4gdGV4dA==", L"plain text"), - TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"), - TestCase("SGVsbG/nq5wK", L"Hello竜"), - TestCase("5aSn5a625aW9Cg==", L"大家好"), - TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", L"this is rocket 🚀 and i love it"), +static TestData Base64TestData = { + // examples from https://en.wikipedia.org/wiki/Base64#Output_padding + TestCase("YWJjZA==", "abcd"), + TestCase("YW55IGNhcm5hbCBwbGVhc3VyZS4=", "any carnal pleasure."), + TestCase("YW55IGNhcm5hbCBwbGVhc3VyZQ==", "any carnal pleasure"), + TestCase("YW55IGNhcm5hbCBwbGVhc3Vy", "any carnal pleasur"), + TestCase("YW55IGNhcm5hbCBwbGVhc3U=", "any carnal pleasu"), + TestCase("YW55IGNhcm5hbCBwbGVhcw==", "any carnal pleas"), + // some manual examples + TestCase("cGxhaW4gdGV4dA==", "plain text"), + TestCase("SGVsbG8=", "Hello"), + // Some unicode examples + TestCase("SGVsbG/nq5w=", "Hello竜"), + TestCase("4oKsNTA=", "€50"), + TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", "this is rocket 🚀 and i love it"), }; static TestData IsBase64Data = { TestCase("da024686f7f2da49da6c98253b42fe1c:erezutlgudgbtwza:i3eclcagfnUbK1B==", false), TestCase("da024686f7f2da49da6c98253b42fe1c:i3eclcagfnUbK1B==", false), TestCase("erezutlgudgbtwza:i3eclcagfnUbK1B==", false), + TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", true), + TestCase("SGVsbG/nq5w=", true), TestCase("i3eclcagfnUbK1B==", true), }; @@ -32,7 +43,7 @@ TEST(Base64Test, Encode) TEST(Base64Test, Decode) { for (const auto& item : Base64TestData) { - //std::string decoded = Base64::base64Decode(PARAM(0)); + std::string decoded = Base64::base64Decode(PARAM(0)); //ASSERT_STREQ(PARAM(1), decoded); } } @@ -40,8 +51,7 @@ TEST(Base64Test, Decode) TEST(Base64Test, ExpectedSize) { for (const auto& item : Base64TestData) { - LOG(INFO) << PARAM(1) << " => " << PARAM(1).size(); - std::size_t s = Base64::expectedBase64Length(PARAM(1).size()); + std::size_t s = Base64::expectedBase64Length(PARAM(1)); ASSERT_EQ(PARAM(0).size(), s); } } From fa88f8b137053c1186bd160de81e178c210256d7 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Thu, 24 Aug 2017 22:04:52 +1000 Subject: [PATCH 035/148] base64 decoding fully implemented --- .travis.yml | 18 ++-- CMakeLists.txt | 2 +- include/mine.cc | 154 ++++++++++++++++++++++++++++++++ include/mine.h | 213 +++++++++++++++++++++++++++++++++++++-------- src/aes.cc | 5 +- src/aes.h | 5 +- src/base16.cc | 5 +- src/base16.h | 5 +- src/base64.cc | 95 ++++++++++++++++++-- src/base64.h | 63 ++++++++------ src/rsa.cc | 5 +- src/rsa.h | 5 +- src/zlib.cc | 5 +- src/zlib.h | 5 +- test/base64-test.h | 23 ++++- test/main.cc | 2 +- 16 files changed, 513 insertions(+), 97 deletions(-) diff --git a/.travis.yml b/.travis.yml index f2b6ee1..15c9c0c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,15 +18,15 @@ before_install: - make - sudo make install ## Ripe - - RIPE_VERSION=4.0.0 - - RIPE_VERSION_URL="v$RIPE_VERSION" - - wget -O ripe-code.zip https://github.com/muflihun/ripe/archive/$RIPE_VERSION_URL.zip - - unzip ripe-code.zip - - cd ripe-$RIPE_VERSION - - cmake . - - make - - ls -l - - sudo make install +# - RIPE_VERSION=4.0.0 +# - RIPE_VERSION_URL="v$RIPE_VERSION" +# - wget -O ripe-code.zip https://github.com/muflihun/ripe/archive/$RIPE_VERSION_URL.zip +# - unzip ripe-code.zip +# - cd ripe-$RIPE_VERSION +# - cmake . +# - make +# - ls -l +# - sudo make install # GTest - wget -O gtest.tar.gz https://github.com/google/googletest/archive/release-1.7.0.tar.gz - tar xf gtest.tar.gz diff --git a/CMakeLists.txt b/CMakeLists.txt index d04aebd..7a810f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,6 +112,6 @@ target_compile_definitions (mine-unit-tests PUBLIC target_link_libraries(mine-unit-tests gtest gtest_main) # Extra linking for the project. -target_link_libraries(mine-unit-tests ${CRYPTOPP_LIBRARIES} ripe) +target_link_libraries(mine-unit-tests ${CRYPTOPP_LIBRARIES}) add_test(NAME mineUnitTests COMMAND mine-unit-tests) diff --git a/include/mine.cc b/include/mine.cc index 2fe0c5f..d286100 100644 --- a/include/mine.cc +++ b/include/mine.cc @@ -20,5 +20,159 @@ using namespace mine; +const std::string Base64::kValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; +const std::unordered_map Base64::kDecodeMap = { + {65, 0}, {66, 1}, {67, 2}, {68, 3}, + {69, 4}, {70, 5}, {71, 6}, {72, 7}, + {73, 8}, {74, 9}, {75, 10}, {76, 11}, + {77, 12}, {78, 13}, {79, 14}, {80, 15}, + {81, 16}, {82, 17}, {83, 18}, {84, 19}, + {85, 20}, {86, 21}, {87, 22}, {88, 23}, + {89, 24}, {90, 25}, {97, 26}, {98, 27}, + {99, 28}, {100, 29}, {101, 30}, {102, 31}, + {103, 32}, {104, 33}, {105, 34}, {106, 35}, + {107, 36}, {108, 37}, {109, 38}, {110, 39}, + {111, 40}, {112, 41}, {113, 42}, {114, 43}, + {115, 44}, {116, 45}, {117, 46}, {118, 47}, + {119, 48}, {120, 49}, {121, 50}, {122, 51}, + {48, 52}, {49, 53}, {50, 54}, {51, 55}, + {52, 56}, {53, 57}, {54, 58}, {55, 59}, + {56, 60}, {57, 61}, {43, 62}, {47, 63}, + {61, 64} +}; + +std::size_t Base64::countChars(const std::string& str) noexcept +{ + std::size_t result = 0UL; + for (auto it = str.begin(); it <= str.end();) { + int c = *it & 0xff; + int charCount = 0; + if (c == 0x0) { + // \0 + ++it; // we increment iter manually + } else if (c <= 0x7f) { + charCount = 1; + } else if (c <= 0x7ff) { + charCount = 2; + } else if (c <= 0xffff) { + charCount = 3; + } else { + charCount = 4; + } + result += charCount; + it += charCount; + } + return result; +} + +std::string Base64::encode(const std::string& raw) noexcept +{ + std::string padding; + std::stringstream ss; + for (auto it = raw.begin(); it < raw.end(); it += 3) { + + // + // we use example of abc + // and let's say we're in the beginning of iterator (i.e, 'a') + // 97 98 99 + // Bits 01100001 01100010 01100011 + // 24-bit Stream: 011000 010110 001001 100011 + // result indices 24 22 9 35 + // result Y W J j + // + + int c = static_cast(*it & 0xff); + ss << static_cast(static_cast(kValidChars[(c >> 2) & 0x3f])); // first 6 bits from first bitset + if (it + 1 < raw.end()) { + int c2 = static_cast(*(it + 1) & 0xff); + ss << static_cast(kValidChars[((c << 4) | // remaining 2 bits from first bitset - shift them left to get 4-bit spaces 010000 + (c2 >> 4) // first 4 bits of second bitset - shift them right to get 2 spaces and bitwise + // to add them 000110 + ) & 0x3f]); // must be within 63 -- + // 010000 + // 000110 + // --|--- + // 010110 + // 111111 + // ---&-- + // 010110 ==> 22 + if (it + 2 < raw.end()) { + int c3 = static_cast(*(it + 2) & 0xff); + ss << static_cast(kValidChars[((c2 << 2) | // remaining 4 bits from second bitset - shift them to get 011000 + (c3 >> 6) // the first 2 bits from third bitset - shift them right to get 000001 + ) & 0x3f]); + // the rest of the explanation is same as above + ss << static_cast(kValidChars[c3 & 0x3f]); // all the remaing bits + } else { + ss << static_cast(kValidChars[(c2 << 2) & 0x3f]); // we have 4 bits left from last byte need space for two 0-bits + ss << kPaddingChar; + } + } else { + ss << static_cast(kValidChars[(c << 4) & 0x3f]); // remaining 2 bits from single byte + ss << kPaddingChar << kPaddingChar; + } + } + return ss.str() + padding; +} + +std::string Base64::decode(const std::string& enc) +{ + // + // we use example of abc + // and let's say we're in the beginning of iterator (i.e, 'a') + // 97 98 99 + // Bits 01100001 01100010 01100011 + // 24-bit Stream: 011000 010110 001001 100011 + // result indices 24 22 9 35 + // result Y W J j + // + + if (enc.size() % 4 != 0) { + throw std::runtime_error("Invalid base64 encoding. Padding is required"); + } + const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); + std::stringstream ss; + for (auto it = enc.begin(); it != enc.end(); it += 4) { + try { + int b0 = kDecodeMap.at(static_cast(*it & 0xff)); + if (b0 == kPadding || b0 == '\0') { + throw std::runtime_error("Invalid base64 encoding. No data available"); + } + int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); + int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); + int b3 = kDecodeMap.at(static_cast(*(it + 3) & 0xff)); + + ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 + b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 + + if (b1 != kPadding && b1 != '\0') { + if (b2 == kPadding || b2 == '\0') { + // second biteset is 'partial byte' + ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); + } else { + ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 + // first we clear the bits at pos 4 and 5 + // then we concat with next bit + b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 + if (b3 == kPadding || b3 == '\0') { + // third bitset is 'partial byte' + ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); + // first we clear first 4 bits + } else { + ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6 | // 001001 ==> 000001 << 6 ==> 01000000 + // first we clear first 4 bits + // then concat with last byte as is + b3); // as is + } + } + } + + } catch (const std::exception&) { + throw std::runtime_error("Invalid base64 character"); + } + } + return ss.str(); +} + diff --git a/include/mine.h b/include/mine.h index 35f59b7..4619326 100644 --- a/include/mine.h +++ b/include/mine.h @@ -17,12 +17,14 @@ #ifndef MINE_CRYPTO_H #define MINE_CRYPTO_H +#include +#include +#include +#include +#include #include #include #include -#include -#include -#include namespace mine { @@ -38,12 +40,77 @@ class Base16 { Base16& operator=(const Base16&) = delete; }; +using byte = unsigned char; + /// -/// \brief Provides base64 encoding / decoding +/// \brief Provides base64 encoding / decoding implementation /// class Base64 { public: + /// + /// \brief List of valid base64 encoding characters + /// + static const std::string kValidChars; + + /// + /// \brief Map for fast lookup corresponding character + /// std::unordered_map is O(1) for best case and linear in worst case + /// which is better than kValidChars find_first_of() which is linear-pos + /// in general + /// \ref http://www.cplusplus.com/reference/unordered_map/unordered_map/at/ + /// \ref http://www.cplusplus.com/reference/string/string/find_first_of/ + /// + static const std::unordered_map kDecodeMap; + + /// + /// \brief Padding is must in mine implementation of base64 + /// + static const char kPaddingChar = '='; + + /// + /// \brief Replacement for better d.size() that consider unicode bytes too + /// \see https://en.wikipedia.org/wiki/UTF-8#Description + /// + static std::size_t countChars(const std::string& d) noexcept; + + /// + /// \brief Encodes input of length to base64 encoding + /// + static std::string encode(const std::string& raw) noexcept; + + /// + /// \brief Decodes encoded base64 + /// \throws std::runtime if invalid encoding. Another time it is thrown + /// is if no padding is found + /// std::runtime::what() is set according to the error + /// + static std::string decode(const std::string& e); + + /// + /// \brief expectedBase64Length Returns expected base64 length + /// \param n Length of input (plain data) + /// + inline static std::size_t expectedLength(std::size_t n) noexcept + { + return ((4 * n / 3) + 3) & ~0x03; + } + + inline static std::size_t expectedLength(const std::string& str) noexcept + { + return expectedLength(countChars(str)); + } + + /// + /// \brief Finds whether data is base64 encoded. This is done + /// by finding non-base64 character. So it is not necessary + /// a valid base64 encoding. + /// + inline static bool isBase64(const std::string& data) noexcept + { + return data.find_first_not_of(kValidChars) == std::string::npos; + } + private: Base64() = delete; Base64(const Base64&) = delete; @@ -67,7 +134,7 @@ class AES { /// User will provide their own implementation of big integer /// or use existing one. /// -/// This is implemeted from instructions on +/// Compliant with PKCS#1 (v2.1) /// https://tools.ietf.org/html/rfc3447#section-7.2 /// /// Mine uses pkcs#1 v1.5 padding scheme @@ -336,6 +403,9 @@ class BigIntegerHelper { iss >> std::hex >> msg; return msg; } +private: + BigIntegerHelper(const BigIntegerHelper&) = delete; + BigIntegerHelper& operator=(const BigIntegerHelper&) = delete; }; /// @@ -353,6 +423,23 @@ class GenericPublicKey { GenericPublicKey() = default; + GenericPublicKey(const GenericPublicKey& other) + { + this->m_n = other.m_n; + this->m_e = other.m_e; + this->m_k = other.m_k; + } + + GenericPublicKey& operator=(const GenericPublicKey& other) + { + if (this != &other) { + this->m_n = other.m_n; + this->m_e = other.m_e; + this->m_k = other.m_k; + } + return *this; + } + GenericPublicKey(BigInteger n, int e) : m_n(n), m_e(e) @@ -385,38 +472,68 @@ class GenericPrivateKey { GenericPrivateKey() = default; + GenericPrivateKey(const GenericPrivateKey& other) + { + this->m_p = other.m_p; + this->m_q = other.m_q; + this->m_e = other.m_e; + this->m_n = other.m_n; + this->m_d = other.m_d; + this->m_coeff = other.m_coeff; + this->m_dp = other.m_dp; + this->m_dq = other.m_dq; + this->m_k = other.m_k; + } + + GenericPrivateKey& operator=(const GenericPrivateKey& other) + { + if (this != &other) { + this->m_p = other.m_p; + this->m_q = other.m_q; + this->m_e = other.m_e; + this->m_n = other.m_n; + this->m_d = other.m_d; + this->m_coeff = other.m_coeff; + this->m_dp = other.m_dp; + this->m_dq = other.m_dq; + this->m_k = other.m_k; + } + return *this; + } + GenericPrivateKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) : m_p(p), m_q(q), m_e(e) - { - if (p == q || p == 0 || q == 0) { - throw std::invalid_argument("p and q must be prime numbers unique to each other"); - } - - const BigInteger pMinus1 = m_p - 1; - const BigInteger qMinus1 = m_q - 1; - const BigInteger phi = pMinus1 * qMinus1; - - if (m_helper.gcd(m_e, phi) != 1) { - throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); - } - m_n = m_p * m_q; - m_k = m_helper.countBytes(m_n); - if (m_k < 11) { - throw std::invalid_argument("Invalid prime. Length error."); - } - m_coeff = m_helper.modInverse(m_q, m_p); - - m_d = m_helper.modInverse(m_e, phi); - - // note: - // https://tools.ietf.org/html/rfc3447#section-2 says to use m_e - // openssl says to use m_d - which one?! - // - m_dp = BigInteger(m_d) % pMinus1; - m_dq = BigInteger(m_d) % qMinus1; - } + + { + if (p == q || p == 0 || q == 0) { + throw std::invalid_argument("p and q must be prime numbers unique to each other"); + } + + const BigInteger pMinus1 = m_p - 1; + const BigInteger qMinus1 = m_q - 1; + const BigInteger phi = pMinus1 * qMinus1; + + if (m_helper.gcd(m_e, phi) != 1) { + throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); + } + m_n = m_p * m_q; + m_k = m_helper.countBytes(m_n); + if (m_k < 11) { + throw std::invalid_argument("Invalid prime. Length error."); + } + m_coeff = m_helper.modInverse(m_q, m_p); + + m_d = m_helper.modInverse(m_e, phi); + + // note: + // https://tools.ietf.org/html/rfc3447#section-2 says to use m_e + // openssl says to use m_d - which one?! + // + m_dp = BigInteger(m_d) % pMinus1; + m_dq = BigInteger(m_d) % qMinus1; + } virtual ~GenericPrivateKey() = default; @@ -480,12 +597,31 @@ class GenericPrivateKey { template > class GenericKeyPair { public: + GenericKeyPair() = default; + + GenericKeyPair(const GenericKeyPair& other) + { + this->m_privateKey = other.m_privateKey; + this->m_publicKey = other.m_publicKey; + } + + GenericKeyPair& operator=(const GenericKeyPair& other) + { + if (this != &other) { + this->m_privateKey = other.m_privateKey; + this->m_publicKey = other.m_publicKey; + } + return *this; + } + GenericKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) { m_publicKey = GenericPublicKey(p * q, exp); m_privateKey = GenericPrivateKey(p, q, exp); } + virtual ~GenericKeyPair() = default; + inline const GenericPublicKey* publicKey() const { return &m_publicKey; } inline const GenericPrivateKey* privateKey() const { return &m_privateKey; } @@ -505,8 +641,8 @@ class GenericRSA { using PrivateKey = GenericPrivateKey; GenericRSA() = default; - GenericRSA(const GenericRSA&) = default; - GenericRSA& operator=(const GenericRSA&) = default; + GenericRSA(const GenericRSA&) = delete; + GenericRSA& operator=(const GenericRSA&) = delete; /// /// \brief Encrypts plain bytes using RSA public key @@ -715,18 +851,19 @@ class GenericRSA { using CharacterType = typename T::value_type; std::basic_stringstream ss; + for (; i < baLen; ++i) { // reference: http://en.cppreference.com/w/cpp/language/types -> range of values int c = ba[i] & 0xFF; - if (c < 128) { + if (c <= 0x7f) { ss << static_cast(c); - } else if (c > 191 && c < 224) { + } else if (c > 0xbf && c < 0xe0) { ss << static_cast( ((c & 31) << 6) | (ba[i+1] & 63) ); ++i; - } else if ((c < 191) || (c >= 224 && c < 240)) { // utf-16 char + } else if ((c < 0xbf) || (c >= 0xe0 && c < 0xf0)) { // utf-16 char ss << static_cast( ((c & 15) << 12) | ((ba[i+1] & 63) << 6) | diff --git a/src/aes.cc b/src/aes.cc index 5a154e7..9f26634 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -6,7 +6,10 @@ // instead which is automatically generated and includes this file // This is seperated to aid the development // -// Copyright 2017 Muflihun Labs +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE // // https://github.com/muflihun/mine // diff --git a/src/aes.h b/src/aes.h index 6856aed..4f4949f 100644 --- a/src/aes.h +++ b/src/aes.h @@ -6,7 +6,10 @@ // instead which is automatically generated and includes this file // This is seperated to aid the development // -// Copyright 2017 Muflihun Labs +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE // // https://github.com/muflihun/mine // diff --git a/src/base16.cc b/src/base16.cc index 2cf3415..522c580 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -6,7 +6,10 @@ // instead which is automatically generated and includes this file // This is seperated to aid the development // -// Copyright 2017 Muflihun Labs +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE // // https://github.com/muflihun/mine // diff --git a/src/base16.h b/src/base16.h index 2e3b046..72e32ea 100644 --- a/src/base16.h +++ b/src/base16.h @@ -6,7 +6,10 @@ // instead which is automatically generated and includes this file // This is seperated to aid the development // -// Copyright 2017 Muflihun Labs +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE // // https://github.com/muflihun/mine // diff --git a/src/base64.cc b/src/base64.cc index 742b512..386c189 100644 --- a/src/base64.cc +++ b/src/base64.cc @@ -6,7 +6,10 @@ // instead which is automatically generated and includes this file // This is seperated to aid the development // -// Copyright 2017 Muflihun Labs +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE // // https://github.com/muflihun/mine // @@ -16,8 +19,27 @@ using namespace mine; const std::string Base64::kValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; +const std::unordered_map Base64::kDecodeMap = { + {65, 0}, {66, 1}, {67, 2}, {68, 3}, + {69, 4}, {70, 5}, {71, 6}, {72, 7}, + {73, 8}, {74, 9}, {75, 10}, {76, 11}, + {77, 12}, {78, 13}, {79, 14}, {80, 15}, + {81, 16}, {82, 17}, {83, 18}, {84, 19}, + {85, 20}, {86, 21}, {87, 22}, {88, 23}, + {89, 24}, {90, 25}, {97, 26}, {98, 27}, + {99, 28}, {100, 29}, {101, 30}, {102, 31}, + {103, 32}, {104, 33}, {105, 34}, {106, 35}, + {107, 36}, {108, 37}, {109, 38}, {110, 39}, + {111, 40}, {112, 41}, {113, 42}, {114, 43}, + {115, 44}, {116, 45}, {117, 46}, {118, 47}, + {119, 48}, {120, 49}, {121, 50}, {122, 51}, + {48, 52}, {49, 53}, {50, 54}, {51, 55}, + {52, 56}, {53, 57}, {54, 58}, {55, 59}, + {56, 60}, {57, 61}, {43, 62}, {47, 63}, + {61, 64} +}; -std::size_t Base64::countChars(const std::string& str) +std::size_t Base64::countChars(const std::string& str) noexcept { std::size_t result = 0UL; for (auto it = str.begin(); it <= str.end();) { @@ -41,23 +63,21 @@ std::size_t Base64::countChars(const std::string& str) return result; } -std::string Base64::base64Encode(const std::string& raw) +std::string Base64::encode(const std::string& raw) noexcept { std::string padding; std::stringstream ss; for (auto it = raw.begin(); it < raw.end(); it += 3) { + // // we use example of abc // and let's say we're in the beginning of iterator (i.e, 'a') // 97 98 99 // Bits 01100001 01100010 01100011 - // Stream 011000010110001001100011 => decimal: 6382179 // 24-bit Stream: 011000 010110 001001 100011 // result indices 24 22 9 35 // result Y W J j // - // ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/= - // int c = static_cast(*it & 0xff); ss << static_cast(static_cast(kValidChars[(c >> 2) & 0x3f])); // first 6 bits from first bitset @@ -83,12 +103,71 @@ std::string Base64::base64Encode(const std::string& raw) ss << static_cast(kValidChars[c3 & 0x3f]); // all the remaing bits } else { ss << static_cast(kValidChars[(c2 << 2) & 0x3f]); // we have 4 bits left from last byte need space for two 0-bits - ss << "="; + ss << kPaddingChar; } } else { ss << static_cast(kValidChars[(c << 4) & 0x3f]); // remaining 2 bits from single byte - ss << "=="; + ss << kPaddingChar << kPaddingChar; } } return ss.str() + padding; } + +std::string Base64::decode(const std::string& enc) +{ + // + // we use example of abc + // and let's say we're in the beginning of iterator (i.e, 'a') + // 97 98 99 + // Bits 01100001 01100010 01100011 + // 24-bit Stream: 011000 010110 001001 100011 + // result indices 24 22 9 35 + // result Y W J j + // + + if (enc.size() % 4 != 0) { + throw std::runtime_error("Invalid base64 encoding. Padding is required"); + } + const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); + std::stringstream ss; + for (auto it = enc.begin(); it != enc.end(); it += 4) { + try { + int b0 = kDecodeMap.at(static_cast(*it & 0xff)); + if (b0 == kPadding || b0 == '\0') { + throw std::runtime_error("Invalid base64 encoding. No data available"); + } + int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); + int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); + int b3 = kDecodeMap.at(static_cast(*(it + 3) & 0xff)); + + ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 + b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 + + if (b1 != kPadding && b1 != '\0') { + if (b2 == kPadding || b2 == '\0') { + // second biteset is 'partial byte' + ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); + } else { + ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 + // first we clear the bits at pos 4 and 5 + // then we concat with next bit + b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 + if (b3 == kPadding || b3 == '\0') { + // third bitset is 'partial byte' + ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); + // first we clear first 4 bits + } else { + ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6 | // 001001 ==> 000001 << 6 ==> 01000000 + // first we clear first 4 bits + // then concat with last byte as is + b3); // as is + } + } + } + + } catch (const std::exception&) { + throw std::runtime_error("Invalid base64 character"); + } + } + return ss.str(); +} diff --git a/src/base64.h b/src/base64.h index ebc9cf6..ede0d37 100644 --- a/src/base64.h +++ b/src/base64.h @@ -6,7 +6,10 @@ // instead which is automatically generated and includes this file // This is seperated to aid the development // -// Copyright 2017 Muflihun Labs +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE // // https://github.com/muflihun/mine // @@ -22,55 +25,69 @@ #include #include #include +#include namespace mine { using byte = unsigned char; + /// -/// \brief Provides base64 encoding / decoding +/// \brief Provides base64 encoding / decoding implementation /// class Base64 { public: - static const std::string kValidChars; // this also include padding char (=) + /// + /// \brief List of valid base64 encoding characters + /// + static const std::string kValidChars; + + /// + /// \brief Map for fast lookup corresponding character + /// std::unordered_map is O(1) for best case and linear in worst case + /// which is better than kValidChars find_first_of() which is linear-pos + /// in general + /// \ref http://www.cplusplus.com/reference/unordered_map/unordered_map/at/ + /// \ref http://www.cplusplus.com/reference/string/string/find_first_of/ + /// + static const std::unordered_map kDecodeMap; + + /// + /// \brief Padding is must in mine implementation of base64 + /// static const char kPaddingChar = '='; /// /// \brief Replacement for better d.size() that consider unicode bytes too /// \see https://en.wikipedia.org/wiki/UTF-8#Description /// - static std::size_t countChars(const std::string& d); + static std::size_t countChars(const std::string& d) noexcept; /// /// \brief Encodes input of length to base64 encoding - /// \see https://tools.ietf.org/html/rfc1421#section-4.3.2.4 /// - static std::string base64Encode(const std::string& raw); + static std::string encode(const std::string& raw) noexcept; /// /// \brief Decodes encoded base64 + /// \throws std::runtime if invalid encoding. Another time it is thrown + /// is if no padding is found + /// std::runtime::what() is set according to the error /// - static std::string base64Decode(const std::string& e) - { - std::stringstream ss; - for (auto it = e.begin(); it != e.end(); ++it) { - - } - return ss.str(); - } + static std::string decode(const std::string& e); /// /// \brief expectedBase64Length Returns expected base64 length /// \param n Length of input (plain data) /// - inline static std::size_t expectedBase64Length(std::size_t n) + inline static std::size_t expectedLength(std::size_t n) noexcept { return ((4 * n / 3) + 3) & ~0x03; } - inline static std::size_t expectedBase64Length(const std::string& str) + inline static std::size_t expectedLength(const std::string& str) noexcept { - return expectedBase64Length(countChars(str)); + return expectedLength(countChars(str)); } /// @@ -78,21 +95,11 @@ class Base64 { /// by finding non-base64 character. So it is not necessary /// a valid base64 encoding. /// - inline static bool isBase64(const std::string& data) + inline static bool isBase64(const std::string& data) noexcept { return data.find_first_not_of(kValidChars) == std::string::npos; } - /// - /// \brief Finds whether data is base64 encoded. This is done - /// by finding non-base64 character. So it is not necessary - /// a valid base64 encoding. - /// - inline static bool isBase64(byte c) - { - return isalnum(c) || c == 0x2b || c == 0x2b; - } - private: Base64() = delete; Base64(const Base64&) = delete; diff --git a/src/rsa.cc b/src/rsa.cc index a55f7a4..12f6a51 100644 --- a/src/rsa.cc +++ b/src/rsa.cc @@ -6,7 +6,10 @@ // instead which is automatically generated and includes this file // This is seperated to aid the development // -// Copyright 2017 Muflihun Labs +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE // // https://github.com/muflihun/mine // diff --git a/src/rsa.h b/src/rsa.h index 3a5798f..76d61a7 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -6,7 +6,10 @@ // instead which is automatically generated and includes this file // This is seperated to aid the development // -// Copyright 2017 Muflihun Labs +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE // // https://github.com/muflihun/mine // diff --git a/src/zlib.cc b/src/zlib.cc index 2d4dcb2..fb43c04 100644 --- a/src/zlib.cc +++ b/src/zlib.cc @@ -6,7 +6,10 @@ // instead which is automatically generated and includes this file // This is seperated to aid the development // -// Copyright 2017 Muflihun Labs +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE // // https://github.com/muflihun/mine // diff --git a/src/zlib.h b/src/zlib.h index f6cedd6..a998833 100644 --- a/src/zlib.h +++ b/src/zlib.h @@ -6,7 +6,10 @@ // instead which is automatically generated and includes this file // This is seperated to aid the development // -// Copyright 2017 Muflihun Labs +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE // // https://github.com/muflihun/mine // diff --git a/test/base64-test.h b/test/base64-test.h index 9a8b180..66ede94 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -21,6 +21,14 @@ static TestData Base64TestData = { TestCase("SGVsbG/nq5w=", "Hello竜"), TestCase("4oKsNTA=", "€50"), TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", "this is rocket 🚀 and i love it"), + TestCase("YWJjMTIzIT8kKiYoKSctPUB+", "abc123!?$*&()'-=@~"), +}; + +static TestData InvalidBase64EncodingData = { + TestCase("YWJj,ZA=="), + TestCase("YWJj,A=="), + TestCase(",,,,"), + TestCase("===="), }; static TestData IsBase64Data = { @@ -35,7 +43,7 @@ static TestData IsBase64Data = { TEST(Base64Test, Encode) { for (const auto& item : Base64TestData) { - std::string encoded = Base64::base64Encode(PARAM(1)); + std::string encoded = Base64::encode(PARAM(1)); ASSERT_STREQ(PARAM(0).c_str(), encoded.c_str()); } } @@ -43,15 +51,22 @@ TEST(Base64Test, Encode) TEST(Base64Test, Decode) { for (const auto& item : Base64TestData) { - std::string decoded = Base64::base64Decode(PARAM(0)); - //ASSERT_STREQ(PARAM(1), decoded); + std::string decoded = Base64::decode(PARAM(0)); + ASSERT_STREQ(PARAM(1).c_str(), decoded.c_str()); + } +} + +TEST(Base64Test, InvalidBase64Encoding) +{ + for (const auto& item : InvalidBase64EncodingData) { + EXPECT_THROW(Base64::decode(PARAM(0)), std::runtime_error); } } TEST(Base64Test, ExpectedSize) { for (const auto& item : Base64TestData) { - std::size_t s = Base64::expectedBase64Length(PARAM(1)); + std::size_t s = Base64::expectedLength(PARAM(1)); ASSERT_EQ(PARAM(0).size(), s); } } diff --git a/test/main.cc b/test/main.cc index 60a821b..539fb7d 100644 --- a/test/main.cc +++ b/test/main.cc @@ -3,7 +3,7 @@ #include "base64-test.h" #include "aes-test.h" #include "zlib-test.h" -//#include "rsa-test.h" +#include "rsa-test.h" INITIALIZE_EASYLOGGINGPP From 3de0423aa839997b25df00ceebeb66cadb800de7 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Thu, 24 Aug 2017 22:12:52 +1000 Subject: [PATCH 036/148] single header test incl --- CMakeLists.txt | 33 +++++++++++++++++++++++---------- test/aes-test.h | 7 ++++++- test/base16-test.h | 7 ++++++- test/base64-test.h | 7 ++++++- test/rsa-test.h | 8 +++++++- test/zlib-test.h | 7 ++++++- 6 files changed, 54 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a810f2..534cb48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 2.8.12) project(Mine) +option (test_main_header "Test main header (mine.h)" OFF) + set (MINE_VERSION "4.0.0") set (MINE_SOVERSION "4.0.0") @@ -93,16 +95,27 @@ include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) enable_testing() -add_executable(mine-unit-tests - test/main.cc - src/rsa.cc - src/aes.cc - src/base16.cc - src/base64.cc - src/zlib.cc - include/mine.cc - ${EASYLOGGINGPP_INCLUDE_DIR}/easylogging++.cc -) +if (test_main_header) + add_executable(mine-unit-tests + test/main.cc + include/mine.cc + ${EASYLOGGINGPP_INCLUDE_DIR}/easylogging++.cc + ) + + target_compile_definitions (mine-unit-tests PUBLIC + MINE_SINGLE_HEADER_TEST + ) +else() + add_executable(mine-unit-tests + test/main.cc + src/rsa.cc + src/aes.cc + src/base16.cc + src/base64.cc + src/zlib.cc + ${EASYLOGGINGPP_INCLUDE_DIR}/easylogging++.cc + ) +endif() target_compile_definitions (mine-unit-tests PUBLIC ELPP_STL_LOGGING diff --git a/test/aes-test.h b/test/aes-test.h index d550651..b10026d 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -1,9 +1,14 @@ #ifndef AES_TEST_H #define AES_TEST_H -#include "src/aes.h" #include "test.h" +#ifdef MINE_SINGLE_HEADER_TEST +# include "include/mine.h" +#else +# include "src/aes.h" +#endif + namespace mine { } diff --git a/test/base16-test.h b/test/base16-test.h index 45345bb..c37a019 100644 --- a/test/base16-test.h +++ b/test/base16-test.h @@ -1,9 +1,14 @@ #ifndef BASE16_TEST_H #define BASE16_TEST_H -#include "src/base16.h" #include "test.h" +#ifdef MINE_SINGLE_HEADER_TEST +# include "include/mine.h" +#else +# include "src/base16.h" +#endif + namespace mine { } diff --git a/test/base64-test.h b/test/base64-test.h index 66ede94..644179f 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -1,9 +1,14 @@ #ifndef BASE64_TEST_H #define BASE64_TEST_H -#include "src/base64.h" #include "test.h" +#ifdef MINE_SINGLE_HEADER_TEST +# include "include/mine.h" +#else +# include "src/base64.h" +#endif + namespace mine { static TestData Base64TestData = { diff --git a/test/rsa-test.h b/test/rsa-test.h index 667a2ef..f40dcbf 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -1,8 +1,14 @@ #ifndef RSA_TEST_H #define RSA_TEST_H -#include "src/rsa.h" #include "test.h" + +#ifdef MINE_SINGLE_HEADER_TEST +# include "include/mine.h" +#else +# include "src/rsa.h" +#endif + #include #include diff --git a/test/zlib-test.h b/test/zlib-test.h index 21810f7..d700973 100644 --- a/test/zlib-test.h +++ b/test/zlib-test.h @@ -1,9 +1,14 @@ #ifndef ZLIB_TEST_H #define ZLIB_TEST_H -#include "src/zlib.h" #include "test.h" +#ifdef MINE_SINGLE_HEADER_TEST +# include "include/mine.h" +#else +# include "src/zlib.h" +#endif + namespace mine { } From 3d2fbed7f1db1370d0d45c150f2941eedf260a29 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Thu, 24 Aug 2017 22:14:08 +1000 Subject: [PATCH 037/148] travis php --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 15c9c0c..aa82506 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ dist: trusty before_install: - sudo add-apt-repository ppa:kubuntu-ppa/backports -y - sudo apt-get -qq update - - sudo apt-get install -y libssl-dev libgtest-dev valgrind + - sudo apt-get install -y libssl-dev libgtest-dev php-cli valgrind - sudo apt-get install --only-upgrade cmake - cmake --version # Crypto++ (We manually install it because we need Pem Pack as well) @@ -45,6 +45,7 @@ before_install: - sudo make install ## Build - cd "${TRAVIS_BUILD_DIR}" + - php build.sh # build mine headers - mkdir build - cd build - pwd From 4e6f1f1ca6db8f06edda68483f61a0e0474764dd Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Thu, 24 Aug 2017 22:14:39 +1000 Subject: [PATCH 038/148] test main header --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index aa82506..418ccae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -51,7 +51,7 @@ before_install: - pwd - ls -l - ls -l .. - - cmake .. + - cmake -Dtest_main_header=ON .. - make script: "./mine-unit-tests" From 2765077f8b7061f054cebb6f05bfbd5705bd4273 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Thu, 24 Aug 2017 22:18:11 +1000 Subject: [PATCH 039/148] php5 install --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 418ccae..1dadf24 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ dist: trusty before_install: - sudo add-apt-repository ppa:kubuntu-ppa/backports -y - sudo apt-get -qq update - - sudo apt-get install -y libssl-dev libgtest-dev php-cli valgrind + - sudo apt-get install -y libssl-dev libgtest-dev php5-cli valgrind - sudo apt-get install --only-upgrade cmake - cmake --version # Crypto++ (We manually install it because we need Pem Pack as well) From 97a98754e37a959ea1c46ebdd7ef9901ba88639a Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Thu, 24 Aug 2017 22:23:50 +1000 Subject: [PATCH 040/148] pw --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 1dadf24..bda9ae9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,6 +45,7 @@ before_install: - sudo make install ## Build - cd "${TRAVIS_BUILD_DIR}" + - pwd - php build.sh # build mine headers - mkdir build - cd build From 1a0923dcc3d0a37e8c86cb46b009b38ced72391a Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Thu, 24 Aug 2017 22:31:46 +1000 Subject: [PATCH 041/148] test fix for all chars --- .travis.yml | 2 +- src/base64.cc | 5 ++++- src/rsa.h | 6 +++--- test/base64-test.h | 2 ++ 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index bda9ae9..e650a53 100644 --- a/.travis.yml +++ b/.travis.yml @@ -46,7 +46,7 @@ before_install: ## Build - cd "${TRAVIS_BUILD_DIR}" - pwd - - php build.sh # build mine headers + - php build.php # build mine headers - mkdir build - cd build - pwd diff --git a/src/base64.cc b/src/base64.cc index 386c189..8b994c4 100644 --- a/src/base64.cc +++ b/src/base64.cc @@ -129,6 +129,7 @@ std::string Base64::decode(const std::string& enc) throw std::runtime_error("Invalid base64 encoding. Padding is required"); } const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); + std::string s; std::stringstream ss; for (auto it = enc.begin(); it != enc.end(); it += 4) { try { @@ -144,7 +145,7 @@ std::string Base64::decode(const std::string& enc) b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 if (b1 != kPadding && b1 != '\0') { - if (b2 == kPadding || b2 == '\0') { + if (b2 == kPadding || (b2 == '\0' && b3 == '\0')) { // second biteset is 'partial byte' ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); } else { @@ -165,6 +166,8 @@ std::string Base64::decode(const std::string& enc) } } + s = ss.str(); + } catch (const std::exception&) { throw std::runtime_error("Invalid base64 character"); } diff --git a/src/rsa.h b/src/rsa.h index 76d61a7..186ca03 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -212,7 +212,7 @@ class BigIntegerHelper { /// /// \brief Count number of bytes in big integer /// - virtual unsigned int countBytes(BigInteger b) const + virtual inline unsigned int countBytes(BigInteger b) const { return countBits(b) * 8; } @@ -566,7 +566,7 @@ class GenericRSA { /// \brief Helper method to encrypt wide-string messages using public key. /// \see encrypt(const GenericPublicKey* publicKey, const T& m) /// - std::string encrypt(const PublicKey* publicKey, + inline std::string encrypt(const PublicKey* publicKey, const std::wstring& message) { return encrypt(publicKey, message); @@ -576,7 +576,7 @@ class GenericRSA { /// \brief Helper method to encrypt std::string messages using public key. /// \see encrypt(const GenericPublicKey* publicKey, const T& m) /// - std::string encrypt(const PublicKey* publicKey, + inline std::string encrypt(const PublicKey* publicKey, const std::string& message) { return encrypt(publicKey, message); diff --git a/test/base64-test.h b/test/base64-test.h index 644179f..af075a7 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -27,6 +27,8 @@ static TestData Base64TestData = { TestCase("4oKsNTA=", "€50"), TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", "this is rocket 🚀 and i love it"), TestCase("YWJjMTIzIT8kKiYoKSctPUB+", "abc123!?$*&()'-=@~"), + TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"), + TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), }; static TestData InvalidBase64EncodingData = { From 3184beba0d3de6a9a6fd823b664a58b236817340 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Thu, 24 Aug 2017 22:42:12 +1000 Subject: [PATCH 042/148] minor refactor --- src/base64.cc | 14 ++++---------- test/test.h | 2 -- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/base64.cc b/src/base64.cc index 8b994c4..1542784 100644 --- a/src/base64.cc +++ b/src/base64.cc @@ -70,13 +70,10 @@ std::string Base64::encode(const std::string& raw) noexcept for (auto it = raw.begin(); it < raw.end(); it += 3) { // - // we use example of abc - // and let's say we're in the beginning of iterator (i.e, 'a') - // 97 98 99 + // we use example following example for implementation basis // Bits 01100001 01100010 01100011 - // 24-bit Stream: 011000 010110 001001 100011 + // 24-bit stream: 011000 010110 001001 100011 // result indices 24 22 9 35 - // result Y W J j // int c = static_cast(*it & 0xff); @@ -116,13 +113,10 @@ std::string Base64::encode(const std::string& raw) noexcept std::string Base64::decode(const std::string& enc) { // - // we use example of abc - // and let's say we're in the beginning of iterator (i.e, 'a') - // 97 98 99 + // we use example following example for implementation basis // Bits 01100001 01100010 01100011 - // 24-bit Stream: 011000 010110 001001 100011 + // 24-bit stream: 011000 010110 001001 100011 // result indices 24 22 9 35 - // result Y W J j // if (enc.size() % 4 != 0) { diff --git a/test/test.h b/test/test.h index 570ae96..0b42db8 100644 --- a/test/test.h +++ b/test/test.h @@ -7,8 +7,6 @@ #include #include -#include - template using TestData = const std::vector>; From 968c7f67213a56345993897b3a6198f251088fa1 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Thu, 24 Aug 2017 23:42:33 +1000 Subject: [PATCH 043/148] more updates --- src/base16.cc | 31 +++++++++++++++++++++++++++++++ src/base16.h | 20 ++++++++++++++++++++ src/base64.cc | 7 +++---- src/base64.h | 4 +--- test/base16-test.h | 31 +++++++++++++++++++++++++++++++ test/main.cc | 4 ++-- 6 files changed, 88 insertions(+), 9 deletions(-) diff --git a/src/base16.cc b/src/base16.cc index 522c580..813abfc 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -14,6 +14,37 @@ // https://github.com/muflihun/mine // +#include +#include #include "src/base16.h" using namespace mine; + +const std::string Base16::kValidChars = "0123456789ABCDEF"; + +std::string Base16::encode(const std::string& raw) noexcept +{ + std::stringstream ss; + for (auto it = raw.begin(); it < raw.end(); ++it) { + int h = (*it & 0xff); + ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; + } + return ss.str(); +} + +std::string Base16::decode(const std::string& enc) +{ + if (enc.size() % 2 != 0) { + throw std::runtime_error("Invalid base-16 encoding"); + } + std::string s; + std::stringstream ss; + for (auto it = enc.begin(); it != enc.end(); it += 2) { + int b0 = *it & 0xff; + int b1 = *(it + 1) & 0xff; + // fixme: need to fix this! + ss << static_cast(((b0 & 0xf) << 4) | (b1 & 0xf)); + s = ss.str(); + } + return ss.str(); +} diff --git a/src/base16.h b/src/base16.h index 72e32ea..9ef6850 100644 --- a/src/base16.h +++ b/src/base16.h @@ -21,14 +21,34 @@ #ifndef Base16_H #define Base16_H +#include + namespace mine { +using byte = unsigned char; + /// /// \brief Provides base16 encoding / decoding /// class Base16 { public: + /// + /// \brief List of valid hex encoding characters + /// + static const std::string kValidChars; + + /// + /// \brief Encodes input of length to hex encoding + /// + static std::string encode(const std::string& raw) noexcept; + + /// + /// \brief Decodes encoded hex + /// \throws std::runtime if invalid encoding. + /// std::runtime::what() is set according to the error + /// + static std::string decode(const std::string& e); private: Base16() = delete; Base16(const Base16&) = delete; diff --git a/src/base64.cc b/src/base64.cc index 1542784..3840750 100644 --- a/src/base64.cc +++ b/src/base64.cc @@ -14,11 +14,14 @@ // https://github.com/muflihun/mine // +#include +#include #include "src/base64.h" using namespace mine; const std::string Base64::kValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + const std::unordered_map Base64::kDecodeMap = { {65, 0}, {66, 1}, {67, 2}, {68, 3}, {69, 4}, {70, 5}, {71, 6}, {72, 7}, @@ -123,7 +126,6 @@ std::string Base64::decode(const std::string& enc) throw std::runtime_error("Invalid base64 encoding. Padding is required"); } const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); - std::string s; std::stringstream ss; for (auto it = enc.begin(); it != enc.end(); it += 4) { try { @@ -159,9 +161,6 @@ std::string Base64::decode(const std::string& enc) } } } - - s = ss.str(); - } catch (const std::exception&) { throw std::runtime_error("Invalid base64 character"); } diff --git a/src/base64.h b/src/base64.h index ede0d37..11e27d5 100644 --- a/src/base64.h +++ b/src/base64.h @@ -21,10 +21,7 @@ #ifndef Base64_H #define Base64_H -#include #include -#include -#include #include namespace mine { @@ -63,6 +60,7 @@ class Base64 { /// static std::size_t countChars(const std::string& d) noexcept; + // todo: make it portable for multi-byte character (start with wchar_t) /// /// \brief Encodes input of length to base64 encoding /// diff --git a/test/base16-test.h b/test/base16-test.h index c37a019..660c90d 100644 --- a/test/base16-test.h +++ b/test/base16-test.h @@ -11,6 +11,37 @@ namespace mine { +static TestData Base16TestData = { + TestCase("48656C6C6F20576F726C64", "Hello World"), + TestCase("717569636B2062726F776E20666F78206A756D7073206F76657220746865206C617A7920646F6720515549434B2042524F574E20464F58204A554D5053204F56455220544845204C415A5920444F472031323334353637383930", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), +}; + +static TestData InvalidBase16EncodingData = { + TestCase("48656C6C6F20576F726C64F"), +}; + +TEST(Base16Test, Encode) +{ + for (const auto& item : Base16TestData) { + std::string encoded = Base16::encode(PARAM(1)); + ASSERT_STREQ(PARAM(0).c_str(), encoded.c_str()); + } +} + +TEST(Base16Test, Decode) +{ + for (const auto& item : Base16TestData) { + std::string decoded = Base16::decode(PARAM(0)); + ASSERT_STREQ(PARAM(1).c_str(), decoded.c_str()); + } +} + +TEST(Base16Test, InvalidBase16Encoding) +{ + for (const auto& item : InvalidBase16EncodingData) { + EXPECT_THROW(Base16::decode(PARAM(0)), std::runtime_error); + } +} } #endif // BASE16_TEST_H diff --git a/test/main.cc b/test/main.cc index 539fb7d..0abc7fb 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,9 +1,9 @@ #include "test.h" #include "base16-test.h" -#include "base64-test.h" +//#include "base64-test.h" #include "aes-test.h" #include "zlib-test.h" -#include "rsa-test.h" +//#include "rsa-test.h" INITIALIZE_EASYLOGGINGPP From f5c5c408679a499e81c33363fe56c56accb7719f Mon Sep 17 00:00:00 2001 From: mkhan Date: Fri, 25 Aug 2017 11:05:52 +1000 Subject: [PATCH 044/148] base64 wstring fixes --- src/base64.h | 48 +++++++++++++++++++++++++++++++++++++++++++-- test/base64-test.h | 49 ++++++++++++++++++++++++++++++++++++++++++++++ test/main.cc | 4 ++-- 3 files changed, 97 insertions(+), 4 deletions(-) diff --git a/src/base64.h b/src/base64.h index 11e27d5..f008926 100644 --- a/src/base64.h +++ b/src/base64.h @@ -23,6 +23,8 @@ #include #include +#include +#include namespace mine { @@ -60,12 +62,33 @@ class Base64 { /// static std::size_t countChars(const std::string& d) noexcept; - // todo: make it portable for multi-byte character (start with wchar_t) + /// + /// \brief Converts it to std::string and calls countChars on it + /// + static std::size_t countChars(const std::wstring& raw) noexcept + { + std::string converted = std::wstring_convert + , wchar_t>{}.to_bytes(raw); + return countChars(converted); + } + /// /// \brief Encodes input of length to base64 encoding /// static std::string encode(const std::string& raw) noexcept; + /// + /// \brief Converts wstring to corresponding string and returns + /// encoding + /// \see encode(const std::string&) + /// + static std::string encode(const std::wstring& raw) noexcept + { + std::string converted = std::wstring_convert + , wchar_t>{}.to_bytes(raw); + return encode(converted); + } + /// /// \brief Decodes encoded base64 /// \throws std::runtime if invalid encoding. Another time it is thrown @@ -74,6 +97,22 @@ class Base64 { /// static std::string decode(const std::string& e); + /// + /// \brief Helper method to decode base64 encoding as wstring (basic_string) + /// \see decode(const std::string&) + /// \note We do not recommend using it, instead have your own conversion function from + /// std::string to wstring as it can give you invalid results with characters that are + /// 5+ bytes long e.g, \x1F680. If you don't use such characters then it should be safe + /// to use this + /// + static std::wstring decodeAsWString(const std::string& e) + { + std::string result = decode(e); + std::wstring converted = std::wstring_convert + >{}.from_bytes(result); + return converted; + } + /// /// \brief expectedBase64Length Returns expected base64 length /// \param n Length of input (plain data) @@ -83,7 +122,12 @@ class Base64 { return ((4 * n / 3) + 3) & ~0x03; } - inline static std::size_t expectedLength(const std::string& str) noexcept + /// + /// \brief Calculates the length of string + /// \see countChars() + /// + template + inline static std::size_t expectedLength(const T& str) noexcept { return expectedLength(countChars(str)); } diff --git a/test/base64-test.h b/test/base64-test.h index af075a7..1f60bc5 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -31,6 +31,27 @@ static TestData Base64TestData = { TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), }; +static TestData Base64WStringTestData = { + // examples from https://en.wikipedia.org/wiki/Base64#Output_padding + TestCase("YWJjZA==", L"abcd"), + TestCase("YW55IGNhcm5hbCBwbGVhc3VyZS4=", L"any carnal pleasure."), + TestCase("YW55IGNhcm5hbCBwbGVhc3VyZQ==", L"any carnal pleasure"), + TestCase("YW55IGNhcm5hbCBwbGVhc3Vy", L"any carnal pleasur"), + TestCase("YW55IGNhcm5hbCBwbGVhc3U=", L"any carnal pleasu"), + TestCase("YW55IGNhcm5hbCBwbGVhcw==", L"any carnal pleas"), + // some manual examples + TestCase("cGxhaW4gdGV4dA==", L"plain text"), + TestCase("SGVsbG8=", L"Hello"), + // Some unicode examples + TestCase("SGVsbG/nq5w=", L"Hello竜"), + TestCase("4oKsNTA=", L"€50"), + // Commenting and leaving it here on purpose, see note on decodeAsWString + // TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", L"this is rocket 🚀 and i love it"), + TestCase("YWJjMTIzIT8kKiYoKSctPUB+", L"abc123!?$*&()'-=@~"), + TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"), + TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), +}; + static TestData InvalidBase64EncodingData = { TestCase("YWJj,ZA=="), TestCase("YWJj,A=="), @@ -59,6 +80,26 @@ TEST(Base64Test, Decode) { for (const auto& item : Base64TestData) { std::string decoded = Base64::decode(PARAM(0)); + std::cout << decoded << std::endl; + ASSERT_STREQ(PARAM(1).c_str(), decoded.c_str()); + } +} + +TEST(Base64Test, EncodeWString) +{ + for (const auto& item : Base64WStringTestData) { + std::string encoded = Base64::encode(PARAM(1)); + ASSERT_STREQ(PARAM(0).c_str(), encoded.c_str()); + } +} + +TEST(Base64Test, DecodeWString) +{ + for (const auto& item : Base64WStringTestData) { + std::wstring decoded = Base64::decodeAsWString(PARAM(0)); + std::wcout << std::wstring(decoded.begin(), decoded.end()); + std::wcout.clear(); // clear the stream in case of failbit or badbit + std::cout << std::endl; ASSERT_STREQ(PARAM(1).c_str(), decoded.c_str()); } } @@ -78,6 +119,14 @@ TEST(Base64Test, ExpectedSize) } } +TEST(Base64Test, ExpectedSizeWstring) +{ + for (const auto& item : Base64WStringTestData) { + std::size_t s = Base64::expectedLength(PARAM(1)); + ASSERT_EQ(PARAM(0).size(), s); + } +} + TEST(Base64Test, IsBase64) { for (const auto& item : IsBase64Data) { diff --git a/test/main.cc b/test/main.cc index 0abc7fb..9608817 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,6 +1,6 @@ #include "test.h" -#include "base16-test.h" -//#include "base64-test.h" +//#include "base16-test.h" +#include "base64-test.h" #include "aes-test.h" #include "zlib-test.h" //#include "rsa-test.h" From e9b5e9ae9f681308317caa33145c11c2dc8f19ec Mon Sep 17 00:00:00 2001 From: mkhan Date: Fri, 25 Aug 2017 11:45:22 +1000 Subject: [PATCH 045/148] b16 fixes --- src/base16.cc | 15 +++++++++++++-- src/base16.h | 7 +++++++ test/base16-test.h | 5 +++-- test/base64-test.h | 4 ++-- test/main.cc | 4 ++-- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/base16.cc b/src/base16.cc index 813abfc..e1a4670 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -22,6 +22,13 @@ using namespace mine; const std::string Base16::kValidChars = "0123456789ABCDEF"; +const std::unordered_map Base16::kDecodeMap = { + {48, 0}, {49, 1}, {50, 2}, {51, 3}, + {52, 4}, {53, 5}, {54, 6}, {55, 7}, + {56, 8}, {57, 9}, {65, 10}, {66, 11}, + {67, 12}, {68, 13},{69, 14}, {70, 15}, +}; + std::string Base16::encode(const std::string& raw) noexcept { std::stringstream ss; @@ -42,8 +49,12 @@ std::string Base16::decode(const std::string& enc) for (auto it = enc.begin(); it != enc.end(); it += 2) { int b0 = *it & 0xff; int b1 = *(it + 1) & 0xff; - // fixme: need to fix this! - ss << static_cast(((b0 & 0xf) << 4) | (b1 & 0xf)); + try { + ss << static_cast((b0 << 4) | kDecodeMap.at(b1)); + } catch (const std::exception&) { + throw std::runtime_error("Invalid base-16 encoding"); + } + s = ss.str(); } return ss.str(); diff --git a/src/base16.h b/src/base16.h index 9ef6850..7256c31 100644 --- a/src/base16.h +++ b/src/base16.h @@ -22,6 +22,7 @@ #define Base16_H #include +#include namespace mine { @@ -38,6 +39,12 @@ class Base16 { /// static const std::string kValidChars; + /// + /// \brief Map for fast lookup corresponding character + /// \see Base64::kDecodeMap + /// + static const std::unordered_map kDecodeMap; + /// /// \brief Encodes input of length to hex encoding /// diff --git a/test/base16-test.h b/test/base16-test.h index 660c90d..ef07bea 100644 --- a/test/base16-test.h +++ b/test/base16-test.h @@ -12,8 +12,9 @@ namespace mine { static TestData Base16TestData = { - TestCase("48656C6C6F20576F726C64", "Hello World"), - TestCase("717569636B2062726F776E20666F78206A756D7073206F76657220746865206C617A7920646F6720515549434B2042524F574E20464F58204A554D5053204F56455220544845204C415A5920444F472031323334353637383930", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), + TestCase("68656C6F", "helo"), + TestCase("48656C6C6F20576F726C6421", "Hello World!"), + TestCase("616263313233213F242A262829272D3D407E", "abc123!?$*&()'-=@~"), }; static TestData InvalidBase16EncodingData = { diff --git a/test/base64-test.h b/test/base64-test.h index 1f60bc5..3066b54 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -22,11 +22,11 @@ static TestData Base64TestData = { // some manual examples TestCase("cGxhaW4gdGV4dA==", "plain text"), TestCase("SGVsbG8=", "Hello"), + TestCase("YWJjMTIzIT8kKiYoKSctPUB+", "abc123!?$*&()'-=@~"), // Some unicode examples TestCase("SGVsbG/nq5w=", "Hello竜"), TestCase("4oKsNTA=", "€50"), TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", "this is rocket 🚀 and i love it"), - TestCase("YWJjMTIzIT8kKiYoKSctPUB+", "abc123!?$*&()'-=@~"), TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"), TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), }; @@ -42,12 +42,12 @@ static TestData Base64WStringTestData = { // some manual examples TestCase("cGxhaW4gdGV4dA==", L"plain text"), TestCase("SGVsbG8=", L"Hello"), + TestCase("YWJjMTIzIT8kKiYoKSctPUB+", L"abc123!?$*&()'-=@~"), // Some unicode examples TestCase("SGVsbG/nq5w=", L"Hello竜"), TestCase("4oKsNTA=", L"€50"), // Commenting and leaving it here on purpose, see note on decodeAsWString // TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", L"this is rocket 🚀 and i love it"), - TestCase("YWJjMTIzIT8kKiYoKSctPUB+", L"abc123!?$*&()'-=@~"), TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"), TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), }; diff --git a/test/main.cc b/test/main.cc index 9608817..0abc7fb 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,6 +1,6 @@ #include "test.h" -//#include "base16-test.h" -#include "base64-test.h" +#include "base16-test.h" +//#include "base64-test.h" #include "aes-test.h" #include "zlib-test.h" //#include "rsa-test.h" From 069ff78ea0b6b22000a7a91a8fa1a60305c81a20 Mon Sep 17 00:00:00 2001 From: mkhan Date: Fri, 25 Aug 2017 11:53:57 +1000 Subject: [PATCH 046/148] checks with trim --- build.php | 48 ++++++++++++++++++++++++------------------------ test/main.cc | 4 ++-- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/build.php b/build.php index 816320f..bbd4cf6 100644 --- a/build.php +++ b/build.php @@ -60,22 +60,22 @@ EOT; function includeArrayToStr($includes) { - $includes = array_unique($includes, SORT_STRING); - $includes_str = ""; - foreach ($includes as $incl) { - $includes_str .= "#include $incl"; - } - return $includes_str; + $includes = array_unique($includes, SORT_STRING); + $includes_str = ""; + foreach ($includes as $incl) { + $includes_str .= "#include $incl"; + } + return $includes_str; } function resolveTemplate($template, $includes, $lines, $lib_version, $filename) { - $includes_str = includeArrayToStr($includes); - $final = str_replace("{{includes}}", $includes_str, $template); + $includes_str = includeArrayToStr($includes); + $final = str_replace("{{includes}}", $includes_str, $template); - $final = str_replace("{{code}}", $lines, $final); - $final = str_replace("{{version}}", $lib_version, $final); + $final = str_replace("{{code}}", $lines, $final); + $final = str_replace("{{version}}", $lib_version, $final); - file_put_contents($filename, $final); + file_put_contents($filename, $final); } $headers_list = array( @@ -94,13 +94,13 @@ function resolveTemplate($template, $includes, $lines, $lib_version, $filename) if ($fd) { $namespace_started = false; while (($line = fgets($fd, 2048)) !== false) { - if ($pos = (strpos($line, "#include")) === 0) { + if ($pos = (strpos(trim($line), "#include")) === 0) { $includes[] = substr($line, $pos + strlen("#include")); - } else if ($pos = (strpos($line, "namespace mine {")) === 0) { + } else if ($pos = (strpos(trim($line), "namespace mine {")) === 0) { $namespace_started = true; - } else if ($pos = (strpos($line, "} // end namespace mine")) === 0) { + } else if ($pos = (strpos(trim($line), "} // end namespace mine")) === 0) { $namespace_started = false; - } else if ($namespace_started && strpos($line, "#include") === false) { + } else if ($namespace_started && strpos(trim($line), "#include") === false) { $lines .= $line; } } @@ -130,18 +130,18 @@ function resolveTemplate($template, $includes, $lines, $lib_version, $filename) foreach ($source_list as $filename) { $fd = @fopen($filename, "r"); if ($fd) { - $codeStarted = false; + $codeStarted = false; while (($line = fgets($fd, 4096)) !== false) { - if (!$codeStarted && $pos = (strpos($line, "//")) === false) { + if (!$codeStarted && $pos = (strpos(trim($line), "//")) === false) { $codeStarted = true; // we ignore comment of the file - } else if ($codeStarted && $pos = (strpos($line, "#include")) === 0) { - // don't include header of the file - if (strpos($line, "#include \"src/") === false) { - $includes[] = substr($line, $pos + strlen("#include")); - } - } else if ($codeStarted && $pos = (strpos($line, "using namespace mine")) === 0) { + } else if ($codeStarted && $pos = (strpos(trim($line), "#include")) === 0) { + // don't include header of the file + if (strpos(trim($line), "#include \"src/") === false) { + $includes[] = substr($line, $pos + strlen("#include")); + } + } else if ($codeStarted && $pos = (strpos(trim($line), "using namespace mine")) === 0) { // ignore namespace as we have in template - } else if ($codeStarted && strpos($line, "#include") === false) { + } else if ($codeStarted && strpos(trim($line), "#include") === false) { $lines .= $line; } } diff --git a/test/main.cc b/test/main.cc index 0abc7fb..539fb7d 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,9 +1,9 @@ #include "test.h" #include "base16-test.h" -//#include "base64-test.h" +#include "base64-test.h" #include "aes-test.h" #include "zlib-test.h" -//#include "rsa-test.h" +#include "rsa-test.h" INITIALIZE_EASYLOGGINGPP From a01fcd64e1e5d971ed88f6c1bcdf224d8980a33a Mon Sep 17 00:00:00 2001 From: mkhan Date: Fri, 25 Aug 2017 16:36:08 +1000 Subject: [PATCH 047/148] moved from include to package? --- CMakeLists.txt | 2 +- README.md | 5 ++- build.php | 8 ++-- {include => package}/mine.cc | 60 ++++++++++++++++++++------ {include => package}/mine.h | 82 +++++++++++++++++++++++++++++++++--- src/aes.cc | 2 +- src/aes.h | 2 +- src/base16.cc | 2 +- src/base16.h | 2 +- src/base64.cc | 2 +- src/base64.h | 2 +- src/rsa.cc | 2 +- src/rsa.h | 2 +- src/zlib.cc | 2 +- src/zlib.h | 2 +- test/aes-test.h | 2 +- test/base16-test.h | 2 +- test/base64-test.h | 2 +- test/rsa-test.h | 2 +- test/zlib-test.h | 2 +- 20 files changed, 147 insertions(+), 40 deletions(-) rename {include => package}/mine.cc (82%) rename {include => package}/mine.h (91%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 534cb48..71087bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,7 +98,7 @@ enable_testing() if (test_main_header) add_executable(mine-unit-tests test/main.cc - include/mine.cc + package/mine.cc ${EASYLOGGINGPP_INCLUDE_DIR}/easylogging++.cc ) diff --git a/README.md b/README.md index 353aa69..2f2bb8d 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,9 @@ Mine is minimal cryptography implementation of [RFC-3447](https://tools.ietf.org # Introduction It all started with [ripe](https://github.com/muflihun/ripe) that is dependent upon third-party library (initially OpenSSL then Crypto++) statically linked. However after using it for a while in [residue](https://github.com/muflihun/residue), we realized that portability became an issue for _minimal_ library. So we decided to start implementing the standards ourself, forming _mine_. +# Installation +Simply copy `mine.h` and `mine.cc` from [`package/`](/package/) directory to your project or your local machine. + # Status Currently, it is not production ready. It depends upon third-party library. We are actively working on the development and implementation of RFC. We cannot guarantee the timeframe as all the contributors are full time workers and only do this project in their spare time. @@ -25,7 +28,7 @@ Mine _will_ support following features: For _minimal_ library this is what we are aiming. # Contribution -We are currently not accepting any pull requests for this project but if you have security concerns or see an issue in implementation please let us know via github issues. +You can only contribute by testing on various platforms and reporting the issues. We are not accepting any pull requests until first release. # License ``` diff --git a/build.php b/build.php index bbd4cf6..e80dceb 100644 --- a/build.php +++ b/build.php @@ -1,8 +1,8 @@ +#include #include "mine.h" using namespace mine; +const std::string Base16::kValidChars = "0123456789ABCDEF"; + +const std::unordered_map Base16::kDecodeMap = { + {48, 0}, {49, 1}, {50, 2}, {51, 3}, + {52, 4}, {53, 5}, {54, 6}, {55, 7}, + {56, 8}, {57, 9}, {65, 10}, {66, 11}, + {67, 12}, {68, 13},{69, 14}, {70, 15}, +}; + +std::string Base16::encode(const std::string& raw) noexcept +{ + std::stringstream ss; + for (auto it = raw.begin(); it < raw.end(); ++it) { + int h = (*it & 0xff); + ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; + } + return ss.str(); +} + +std::string Base16::decode(const std::string& enc) +{ + if (enc.size() % 2 != 0) { + throw std::runtime_error("Invalid base-16 encoding"); + } + std::string s; + std::stringstream ss; + for (auto it = enc.begin(); it != enc.end(); it += 2) { + int b0 = *it & 0xff; + int b1 = *(it + 1) & 0xff; + try { + ss << static_cast((b0 << 4) | kDecodeMap.at(b1)); + } catch (const std::exception&) { + throw std::runtime_error("Invalid base-16 encoding"); + } + + s = ss.str(); + } + return ss.str(); +} + const std::string Base64::kValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + const std::unordered_map Base64::kDecodeMap = { {65, 0}, {66, 1}, {67, 2}, {68, 3}, {69, 4}, {70, 5}, {71, 6}, {72, 7}, @@ -72,13 +115,10 @@ std::string Base64::encode(const std::string& raw) noexcept for (auto it = raw.begin(); it < raw.end(); it += 3) { // - // we use example of abc - // and let's say we're in the beginning of iterator (i.e, 'a') - // 97 98 99 + // we use example following example for implementation basis // Bits 01100001 01100010 01100011 - // 24-bit Stream: 011000 010110 001001 100011 + // 24-bit stream: 011000 010110 001001 100011 // result indices 24 22 9 35 - // result Y W J j // int c = static_cast(*it & 0xff); @@ -118,13 +158,10 @@ std::string Base64::encode(const std::string& raw) noexcept std::string Base64::decode(const std::string& enc) { // - // we use example of abc - // and let's say we're in the beginning of iterator (i.e, 'a') - // 97 98 99 + // we use example following example for implementation basis // Bits 01100001 01100010 01100011 - // 24-bit Stream: 011000 010110 001001 100011 + // 24-bit stream: 011000 010110 001001 100011 // result indices 24 22 9 35 - // result Y W J j // if (enc.size() % 4 != 0) { @@ -146,7 +183,7 @@ std::string Base64::decode(const std::string& enc) b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 if (b1 != kPadding && b1 != '\0') { - if (b2 == kPadding || b2 == '\0') { + if (b2 == kPadding || (b2 == '\0' && b3 == '\0')) { // second biteset is 'partial byte' ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); } else { @@ -166,7 +203,6 @@ std::string Base64::decode(const std::string& enc) } } } - } catch (const std::exception&) { throw std::runtime_error("Invalid base64 character"); } diff --git a/include/mine.h b/package/mine.h similarity index 91% rename from include/mine.h rename to package/mine.h index 4619326..35da3c4 100644 --- a/include/mine.h +++ b/package/mine.h @@ -17,23 +17,48 @@ #ifndef MINE_CRYPTO_H #define MINE_CRYPTO_H -#include #include -#include -#include #include +#include +#include #include #include #include +#include +#include namespace mine { +using byte = unsigned char; + /// /// \brief Provides base16 encoding / decoding /// class Base16 { public: + /// + /// \brief List of valid hex encoding characters + /// + static const std::string kValidChars; + + /// + /// \brief Map for fast lookup corresponding character + /// \see Base64::kDecodeMap + /// + static const std::unordered_map kDecodeMap; + + /// + /// \brief Encodes input of length to hex encoding + /// + static std::string encode(const std::string& raw) noexcept; + + /// + /// \brief Decodes encoded hex + /// \throws std::runtime if invalid encoding. + /// std::runtime::what() is set according to the error + /// + static std::string decode(const std::string& e); private: Base16() = delete; Base16(const Base16&) = delete; @@ -74,11 +99,33 @@ class Base64 { /// static std::size_t countChars(const std::string& d) noexcept; + /// + /// \brief Converts it to std::string and calls countChars on it + /// + static std::size_t countChars(const std::wstring& raw) noexcept + { + std::string converted = std::wstring_convert + , wchar_t>{}.to_bytes(raw); + return countChars(converted); + } + /// /// \brief Encodes input of length to base64 encoding /// static std::string encode(const std::string& raw) noexcept; + /// + /// \brief Converts wstring to corresponding string and returns + /// encoding + /// \see encode(const std::string&) + /// + static std::string encode(const std::wstring& raw) noexcept + { + std::string converted = std::wstring_convert + , wchar_t>{}.to_bytes(raw); + return encode(converted); + } + /// /// \brief Decodes encoded base64 /// \throws std::runtime if invalid encoding. Another time it is thrown @@ -87,6 +134,22 @@ class Base64 { /// static std::string decode(const std::string& e); + /// + /// \brief Helper method to decode base64 encoding as wstring (basic_string) + /// \see decode(const std::string&) + /// \note We do not recommend using it, instead have your own conversion function from + /// std::string to wstring as it can give you invalid results with characters that are + /// 5+ bytes long e.g, \x1F680. If you don't use such characters then it should be safe + /// to use this + /// + static std::wstring decodeAsWString(const std::string& e) + { + std::string result = decode(e); + std::wstring converted = std::wstring_convert + >{}.from_bytes(result); + return converted; + } + /// /// \brief expectedBase64Length Returns expected base64 length /// \param n Length of input (plain data) @@ -96,7 +159,12 @@ class Base64 { return ((4 * n / 3) + 3) & ~0x03; } - inline static std::size_t expectedLength(const std::string& str) noexcept + /// + /// \brief Calculates the length of string + /// \see countChars() + /// + template + inline static std::size_t expectedLength(const T& str) noexcept { return expectedLength(countChars(str)); } @@ -311,7 +379,7 @@ class BigIntegerHelper { /// /// \brief Count number of bytes in big integer /// - virtual unsigned int countBytes(BigInteger b) const + virtual inline unsigned int countBytes(BigInteger b) const { return countBits(b) * 8; } @@ -665,7 +733,7 @@ class GenericRSA { /// \brief Helper method to encrypt wide-string messages using public key. /// \see encrypt(const GenericPublicKey* publicKey, const T& m) /// - std::string encrypt(const PublicKey* publicKey, + inline std::string encrypt(const PublicKey* publicKey, const std::wstring& message) { return encrypt(publicKey, message); @@ -675,7 +743,7 @@ class GenericRSA { /// \brief Helper method to encrypt std::string messages using public key. /// \see encrypt(const GenericPublicKey* publicKey, const T& m) /// - std::string encrypt(const PublicKey* publicKey, + inline std::string encrypt(const PublicKey* publicKey, const std::string& message) { return encrypt(publicKey, message); diff --git a/src/aes.cc b/src/aes.cc index 9f26634..6d9fd95 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -2,7 +2,7 @@ // aes.cc // Part of Mine crypto library // -// You should not use this file, use include/mine.cc +// You should not use this file, use mine.cc // instead which is automatically generated and includes this file // This is seperated to aid the development // diff --git a/src/aes.h b/src/aes.h index 4f4949f..97e7c9a 100644 --- a/src/aes.h +++ b/src/aes.h @@ -2,7 +2,7 @@ // aes.h // Part of Mine crypto library // -// You should not use this file, use include/mine.h +// You should not use this file, use mine.h // instead which is automatically generated and includes this file // This is seperated to aid the development // diff --git a/src/base16.cc b/src/base16.cc index e1a4670..a3eb622 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -2,7 +2,7 @@ // base16.cc // Part of Mine crypto library // -// You should not use this file, use include/mine.cc +// You should not use this file, use mine.cc // instead which is automatically generated and includes this file // This is seperated to aid the development // diff --git a/src/base16.h b/src/base16.h index 7256c31..66c532a 100644 --- a/src/base16.h +++ b/src/base16.h @@ -2,7 +2,7 @@ // base16.h // Part of Mine crypto library // -// You should not use this file, use include/mine.h +// You should not use this file, use mine.h // instead which is automatically generated and includes this file // This is seperated to aid the development // diff --git a/src/base64.cc b/src/base64.cc index 3840750..8b0052d 100644 --- a/src/base64.cc +++ b/src/base64.cc @@ -2,7 +2,7 @@ // base64.cc // Part of Mine crypto library // -// You should not use this file, use include/mine.cc +// You should not use this file, use mine.cc // instead which is automatically generated and includes this file // This is seperated to aid the development // diff --git a/src/base64.h b/src/base64.h index f008926..af1826f 100644 --- a/src/base64.h +++ b/src/base64.h @@ -2,7 +2,7 @@ // base64.h // Part of Mine crypto library // -// You should not use this file, use include/mine.h +// You should not use this file, use mine.h // instead which is automatically generated and includes this file // This is seperated to aid the development // diff --git a/src/rsa.cc b/src/rsa.cc index 12f6a51..9fdd8be 100644 --- a/src/rsa.cc +++ b/src/rsa.cc @@ -2,7 +2,7 @@ // rsa.cc // Part of Mine crypto library // -// You should not use this file, use include/mine.cc +// You should not use this file, use mine.cc // instead which is automatically generated and includes this file // This is seperated to aid the development // diff --git a/src/rsa.h b/src/rsa.h index 186ca03..dcda8f7 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -2,7 +2,7 @@ // rsa.h // Part of Mine crypto library // -// You should not use this file, use include/mine.h +// You should not use this file, use mine.h // instead which is automatically generated and includes this file // This is seperated to aid the development // diff --git a/src/zlib.cc b/src/zlib.cc index fb43c04..b40679b 100644 --- a/src/zlib.cc +++ b/src/zlib.cc @@ -2,7 +2,7 @@ // zlib.cc // Part of Mine crypto library // -// You should not use this file, use include/mine.cc +// You should not use this file, use mine.cc // instead which is automatically generated and includes this file // This is seperated to aid the development // diff --git a/src/zlib.h b/src/zlib.h index a998833..5b147b9 100644 --- a/src/zlib.h +++ b/src/zlib.h @@ -2,7 +2,7 @@ // zlib.h // Part of Mine crypto library // -// You should not use this file, use include/mine.h +// You should not use this file, use mine.h // instead which is automatically generated and includes this file // This is seperated to aid the development // diff --git a/test/aes-test.h b/test/aes-test.h index b10026d..01c015f 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -4,7 +4,7 @@ #include "test.h" #ifdef MINE_SINGLE_HEADER_TEST -# include "include/mine.h" +# include "package/mine.h" #else # include "src/aes.h" #endif diff --git a/test/base16-test.h b/test/base16-test.h index ef07bea..714a988 100644 --- a/test/base16-test.h +++ b/test/base16-test.h @@ -4,7 +4,7 @@ #include "test.h" #ifdef MINE_SINGLE_HEADER_TEST -# include "include/mine.h" +# include "package/mine.h" #else # include "src/base16.h" #endif diff --git a/test/base64-test.h b/test/base64-test.h index 3066b54..27732a9 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -4,7 +4,7 @@ #include "test.h" #ifdef MINE_SINGLE_HEADER_TEST -# include "include/mine.h" +# include "package/mine.h" #else # include "src/base64.h" #endif diff --git a/test/rsa-test.h b/test/rsa-test.h index f40dcbf..4b0e56b 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -4,7 +4,7 @@ #include "test.h" #ifdef MINE_SINGLE_HEADER_TEST -# include "include/mine.h" +# include "package/mine.h" #else # include "src/rsa.h" #endif diff --git a/test/zlib-test.h b/test/zlib-test.h index d700973..2116316 100644 --- a/test/zlib-test.h +++ b/test/zlib-test.h @@ -4,7 +4,7 @@ #include "test.h" #ifdef MINE_SINGLE_HEADER_TEST -# include "include/mine.h" +# include "package/mine.h" #else # include "src/zlib.h" #endif From 8c53e8c2185c2bd8a0254bc0db6177b58c7cd430 Mon Sep 17 00:00:00 2001 From: Majid Date: Fri, 25 Aug 2017 16:37:27 +1000 Subject: [PATCH 048/148] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2f2bb8d..cb72bcf 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,8 @@ Mine _will_ support following features: * RSA (Encrypt, Decrypt, Sign and Verify) [[RFC-3447](https://tools.ietf.org/html/rfc3447)] * AES-CBC [[RFC-3602](https://tools.ietf.org/html/rfc3602)] * ZLib (Depend upon libz, eventually implement [RFC-1950](https://tools.ietf.org/html/rfc3602)) - * Base-64 (Encode, Decode) - * Hex (Encode, Decode) + * Base16 (Encode, Decode) + * Base64 (Encode, Decode) For _minimal_ library this is what we are aiming. From cdcd84c457b28759d1f4b84cdf8ba15ebce01f64 Mon Sep 17 00:00:00 2001 From: Majid Date: Fri, 25 Aug 2017 16:40:39 +1000 Subject: [PATCH 049/148] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cb72bcf..f6a6ecb 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,14 @@ ﷽ # Mine -Mine is minimal cryptography implementation of [RFC-3447](https://tools.ietf.org/html/rfc3447) and [RFC-3602](https://tools.ietf.org/html/rfc3602). +Mine is minimal cryptography implementation for small projects that cannot afford to link to external libraries such as OpenSSL etc. [![Build Status](https://img.shields.io/travis/muflihun/mine/develop.svg)](https://travis-ci.org/muflihun/mine) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/muflihun/mine/blob/master/LICENCE) [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/MuflihunDotCom/25) -# Introduction -It all started with [ripe](https://github.com/muflihun/ripe) that is dependent upon third-party library (initially OpenSSL then Crypto++) statically linked. However after using it for a while in [residue](https://github.com/muflihun/residue), we realized that portability became an issue for _minimal_ library. So we decided to start implementing the standards ourself, forming _mine_. +# Overview +It all started with [ripe](https://github.com/muflihun/ripe) that is dependent upon third-party library (initially OpenSSL then Crypto++) statically linked. However after deploying [residue](https://github.com/muflihun/residue) (which used ripe until mine came to life) to older distributions of linux, we realized that portability is an issue for ripe as _minimal_ library. So we started to implement the standards ourselves, forming _Mine_. # Installation Simply copy `mine.h` and `mine.cc` from [`package/`](/package/) directory to your project or your local machine. From 5fd8170ccf6a02fb94726f413d9d5350911e962b Mon Sep 17 00:00:00 2001 From: Majid Date: Fri, 25 Aug 2017 16:42:22 +1000 Subject: [PATCH 050/148] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f6a6ecb..4589d57 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ ﷽ # Mine -Mine is minimal cryptography implementation for small projects that cannot afford to link to external libraries such as OpenSSL etc. +Mine is single-header minimal cryptography implementation for small-medium projects that cannot afford to link to external libraries. [![Build Status](https://img.shields.io/travis/muflihun/mine/develop.svg)](https://travis-ci.org/muflihun/mine) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/muflihun/mine/blob/master/LICENCE) @@ -16,6 +16,8 @@ Simply copy `mine.h` and `mine.cc` from [`package/`](/package/) directory to you # Status Currently, it is not production ready. It depends upon third-party library. We are actively working on the development and implementation of RFC. We cannot guarantee the timeframe as all the contributors are full time workers and only do this project in their spare time. +We are very careful with our implementations and have [unit tests](/test/) in place. + # Features Mine _will_ support following features: From 8c45d6c177970486cbf113f61242eb038cc67fca Mon Sep 17 00:00:00 2001 From: mkhan Date: Fri, 25 Aug 2017 16:48:40 +1000 Subject: [PATCH 051/148] log --- README.md | 5 ++++- mine.png | Bin 0 -> 102227 bytes 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 mine.png diff --git a/README.md b/README.md index f6a6ecb..e79fc53 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ ﷽ -# Mine +![banner] + Mine is minimal cryptography implementation for small projects that cannot afford to link to external libraries such as OpenSSL etc. [![Build Status](https://img.shields.io/travis/muflihun/mine/develop.svg)](https://travis-ci.org/muflihun/mine) @@ -50,3 +51,5 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ``` + + [banner]: https://raw.githubusercontent.com/muflihun/mine/develop/mine.png diff --git a/mine.png b/mine.png new file mode 100644 index 0000000000000000000000000000000000000000..193c2a7991f5e442d8b28c3e8995c5c66dc16c7c GIT binary patch literal 102227 zcmYhi1ymbt)GeIgPAJ~uEk%k$aS2x32~xa3aVr))xJz*gMcU%DxCbo`#VKyViw1Z8 zy!YO3{ol%p&6<^&GkMNAd+)O+T2ozt5brr2001CVQk2sM0MIs2pAT`cP_N{>eh;WO z3>#@RX#k))5&zx-6ZIYIgNlM2;OW0dep_)0Y74HbqMYt5ux_05AZQ3kPl9a1%(@)04PEo}Z2q^DE$o-L@;3ZkM_a$?&x` zjfY2SHN)j8?f(7wm1iH{t>?!aH;)Q3X(}q|m{ct`ZCwyfFh+9HihD|SPFwCVefd$q z)k^t6fV#7}i|Kl<*O-LO?A2|>*qIM#q0j`4lBpY(@qK4wx9RYVRON_LVpQ4mxAIZs z{dqveuFZ^1`K{!y3qk;xT&nyLnS2M|e7m$EaT?UR>wa@&#CQkZy(Bh#+MUZdTjlf+ zBl~&xEl;wsobGJ7XtP;uu=?p{1*`vYy>?ZTbO$W=<5GE?>u{1wjq|G)dEIaUJ6sES z&n4wI;dIf~v;z)NcE0Q8qFCa(+gljjL{eeE&74?Sm>h%QCaCD%Q@LbN^kf5(MGWxK zLq1k+^xxl(O|bjUuFgQ}1${T{oD0wq_Q)^C}nmDHG0EsOXjO zrEx!$u2U99qr3*kDI2bM13G~cH0kHQ{NDlLAsBfmES20!*Q^u6Lwpo@S-_b#;#hqF z(o5LmvUpEr=5R+dw?lNUH+Z40i+BP&te<}7m366;LwD9+6c}(`E$3CCHI!cpRRA~T z8-ZnzQ}&X};L`Zdk zXy3%`-yh;z0qLde1KvB4;LHpO3j?ZECoUkpmiGv?S)88h%K+F+|ESJp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f=X!G|Et!+#+Q(dJ_00000NkvXXu0mjfP=$1I literal 0 HcmV?d00001 From 3339b90fa11ca7b95b228ae374a8149f8340a9c0 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Fri, 25 Aug 2017 21:09:51 +1000 Subject: [PATCH 052/148] fix hex rsa --- package/mine.h | 66 ++++++++++++++++++++-------------------------- src/base64.h | 12 +++++++-- src/rsa.h | 66 ++++++++++++++++++++-------------------------- test/base64-test.h | 18 +++++++------ test/rsa-test.h | 2 +- 5 files changed, 79 insertions(+), 85 deletions(-) diff --git a/package/mine.h b/package/mine.h index 35da3c4..524fc76 100644 --- a/package/mine.h +++ b/package/mine.h @@ -26,6 +26,7 @@ #include #include #include +#include "src/base16.h" namespace mine { @@ -251,23 +252,6 @@ class BigIntegerHelper { BigIntegerHelper() = default; virtual ~BigIntegerHelper() = default; - /// - /// \brief Specific base to specified base - /// \param n Number - /// \param b Target base (default: 16 - Hex) - /// - virtual BigInteger changeBase(BigInteger n, BigInteger b = 16) const - { - BigInteger r, i = 1, o = 0; - while (n != 0) { - r = n % b; - n /= b; - o += r * i; - i *= 10; - } - return o; - } - /// /// \brief Implementation for (a ^ -1) mod b /// @@ -398,7 +382,8 @@ class BigIntegerHelper { } /// - /// \brief Convert integer to raw string (a.k.a i2osp) + /// \brief Convert integer to raw string + /// (this func is also known as i2osp) /// RawString integerToRaw(BigInteger x, int xlen = -1) const { @@ -432,7 +417,7 @@ class BigIntegerHelper { } /// - /// Absolutely must override this - conversion from x to single byte + /// \brief Absolutely must override this - conversion from x to single byte /// virtual inline byte bigIntegerToByte(const BigInteger& x) const { @@ -442,11 +427,18 @@ class BigIntegerHelper { /// /// \brief Converts big integer to hex /// - virtual inline std::string bigIntegerToHex(const BigInteger& b) const + virtual inline std::string bigIntegerToHex(BigInteger n) const { std::stringstream ss; - ss << std::hex << b; - return ss.str(); + int remainder; + while (n != 0) { + remainder = n % 16; + n /= 16; + ss << Base16::kValidChars[remainder]; + } + std::string res(ss.str()); + std::reverse(res.begin(), res.end()); + return res; } /// @@ -851,18 +843,18 @@ class GenericRSA { // utf byteArray[--n] = c; } else if (c <= 0x7ff) { - byteArray[--n] = (c & 63) | 128; + byteArray[--n] = (c & 0x3f) | 128; byteArray[--n] = (c >> 6) | 192; } else if (c <= 0xffff) { // utf-16 - byteArray[--n] = (c & 63) | 128; + byteArray[--n] = (c & 0x3f) | 128; byteArray[--n] = ((c >> 6) & 63) | 128; byteArray[--n] = (c >> 12) | 224; } else { // utf-32 - byteArray[--n] = (c & 63) | 128; - byteArray[--n] = ((c >> 6) & 63) | 128; - byteArray[--n] = ((c >> 12) & 63) | 128; + byteArray[--n] = (c & 0x3f) | 128; + byteArray[--n] = ((c >> 6) & 0x3f) | 128; + byteArray[--n] = ((c >> 12) & 0x3f) | 128; byteArray[--n] = (c >> 18) | 240; } } @@ -922,28 +914,28 @@ class GenericRSA { for (; i < baLen; ++i) { // reference: http://en.cppreference.com/w/cpp/language/types -> range of values - int c = ba[i] & 0xFF; + int c = ba[i] & 0xff; if (c <= 0x7f) { ss << static_cast(c); } else if (c > 0xbf && c < 0xe0) { ss << static_cast( - ((c & 31) << 6) | - (ba[i+1] & 63) + ((c & 0x1f) << 6) | + (ba[i+1] & 0x3f) ); ++i; } else if ((c < 0xbf) || (c >= 0xe0 && c < 0xf0)) { // utf-16 char ss << static_cast( - ((c & 15) << 12) | - ((ba[i+1] & 63) << 6) | - (ba[i+2] & 63) + ((c & 0xf) << 12) | + ((ba[i+1] & 0x3f) << 6) | + (ba[i+2] & 0x3f) ); i += 2; } else { // utf-32 char ss << static_cast( - ((c & 7) << 18) | - ((ba[i+1] & 63) << 12) | - ((ba[i+2] & 63) << 6) | - (ba[i+3] & 63) + ((c & 0x7) << 18) | + ((ba[i+1] & 0x3f) << 12) | + ((ba[i+2] & 0x3f) << 6) | + (ba[i+3] & 0x3f) ); i += 3; } diff --git a/src/base64.h b/src/base64.h index af1826f..c708901 100644 --- a/src/base64.h +++ b/src/base64.h @@ -23,8 +23,10 @@ #include #include -#include -#include +#ifdef MINE_BASE64_WSTRING_CONVERSION +# include +# include +#endif namespace mine { @@ -62,6 +64,7 @@ class Base64 { /// static std::size_t countChars(const std::string& d) noexcept; +#ifdef MINE_BASE64_WSTRING_CONVERSION /// /// \brief Converts it to std::string and calls countChars on it /// @@ -71,12 +74,14 @@ class Base64 { , wchar_t>{}.to_bytes(raw); return countChars(converted); } +#endif /// /// \brief Encodes input of length to base64 encoding /// static std::string encode(const std::string& raw) noexcept; +#ifdef MINE_BASE64_WSTRING_CONVERSION /// /// \brief Converts wstring to corresponding string and returns /// encoding @@ -88,6 +93,7 @@ class Base64 { , wchar_t>{}.to_bytes(raw); return encode(converted); } +#endif /// /// \brief Decodes encoded base64 @@ -97,6 +103,7 @@ class Base64 { /// static std::string decode(const std::string& e); +#ifdef MINE_BASE64_WSTRING_CONVERSION /// /// \brief Helper method to decode base64 encoding as wstring (basic_string) /// \see decode(const std::string&) @@ -112,6 +119,7 @@ class Base64 { >{}.from_bytes(result); return converted; } +#endif /// /// \brief expectedBase64Length Returns expected base64 length diff --git a/src/rsa.h b/src/rsa.h index dcda8f7..0d080d4 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -27,6 +27,7 @@ #include #include #include +#include "src/base16.h" namespace mine { @@ -84,23 +85,6 @@ class BigIntegerHelper { BigIntegerHelper() = default; virtual ~BigIntegerHelper() = default; - /// - /// \brief Specific base to specified base - /// \param n Number - /// \param b Target base (default: 16 - Hex) - /// - virtual BigInteger changeBase(BigInteger n, BigInteger b = 16) const - { - BigInteger r, i = 1, o = 0; - while (n != 0) { - r = n % b; - n /= b; - o += r * i; - i *= 10; - } - return o; - } - /// /// \brief Implementation for (a ^ -1) mod b /// @@ -231,7 +215,8 @@ class BigIntegerHelper { } /// - /// \brief Convert integer to raw string (a.k.a i2osp) + /// \brief Convert integer to raw string + /// (this func is also known as i2osp) /// RawString integerToRaw(BigInteger x, int xlen = -1) const { @@ -265,7 +250,7 @@ class BigIntegerHelper { } /// - /// Absolutely must override this - conversion from x to single byte + /// \brief Absolutely must override this - conversion from x to single byte /// virtual inline byte bigIntegerToByte(const BigInteger& x) const { @@ -275,11 +260,18 @@ class BigIntegerHelper { /// /// \brief Converts big integer to hex /// - virtual inline std::string bigIntegerToHex(const BigInteger& b) const + virtual inline std::string bigIntegerToHex(BigInteger n) const { std::stringstream ss; - ss << std::hex << b; - return ss.str(); + int remainder; + while (n != 0) { + remainder = n % 16; + n /= 16; + ss << Base16::kValidChars[remainder]; + } + std::string res(ss.str()); + std::reverse(res.begin(), res.end()); + return res; } /// @@ -684,18 +676,18 @@ class GenericRSA { // utf byteArray[--n] = c; } else if (c <= 0x7ff) { - byteArray[--n] = (c & 63) | 128; + byteArray[--n] = (c & 0x3f) | 128; byteArray[--n] = (c >> 6) | 192; } else if (c <= 0xffff) { // utf-16 - byteArray[--n] = (c & 63) | 128; + byteArray[--n] = (c & 0x3f) | 128; byteArray[--n] = ((c >> 6) & 63) | 128; byteArray[--n] = (c >> 12) | 224; } else { // utf-32 - byteArray[--n] = (c & 63) | 128; - byteArray[--n] = ((c >> 6) & 63) | 128; - byteArray[--n] = ((c >> 12) & 63) | 128; + byteArray[--n] = (c & 0x3f) | 128; + byteArray[--n] = ((c >> 6) & 0x3f) | 128; + byteArray[--n] = ((c >> 12) & 0x3f) | 128; byteArray[--n] = (c >> 18) | 240; } } @@ -755,28 +747,28 @@ class GenericRSA { for (; i < baLen; ++i) { // reference: http://en.cppreference.com/w/cpp/language/types -> range of values - int c = ba[i] & 0xFF; + int c = ba[i] & 0xff; if (c <= 0x7f) { ss << static_cast(c); } else if (c > 0xbf && c < 0xe0) { ss << static_cast( - ((c & 31) << 6) | - (ba[i+1] & 63) + ((c & 0x1f) << 6) | + (ba[i+1] & 0x3f) ); ++i; } else if ((c < 0xbf) || (c >= 0xe0 && c < 0xf0)) { // utf-16 char ss << static_cast( - ((c & 15) << 12) | - ((ba[i+1] & 63) << 6) | - (ba[i+2] & 63) + ((c & 0xf) << 12) | + ((ba[i+1] & 0x3f) << 6) | + (ba[i+2] & 0x3f) ); i += 2; } else { // utf-32 char ss << static_cast( - ((c & 7) << 18) | - ((ba[i+1] & 63) << 12) | - ((ba[i+2] & 63) << 6) | - (ba[i+3] & 63) + ((c & 0x7) << 18) | + ((ba[i+1] & 0x3f) << 12) | + ((ba[i+2] & 0x3f) << 6) | + (ba[i+3] & 0x3f) ); i += 3; } diff --git a/test/base64-test.h b/test/base64-test.h index 27732a9..9069192 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -85,6 +85,7 @@ TEST(Base64Test, Decode) } } +#ifdef MINE_BASE64_WSTRING_CONVERSION TEST(Base64Test, EncodeWString) { for (const auto& item : Base64WStringTestData) { @@ -104,6 +105,15 @@ TEST(Base64Test, DecodeWString) } } +TEST(Base64Test, ExpectedSizeWstring) +{ + for (const auto& item : Base64WStringTestData) { + std::size_t s = Base64::expectedLength(PARAM(1)); + ASSERT_EQ(PARAM(0).size(), s); + } +} +#endif + TEST(Base64Test, InvalidBase64Encoding) { for (const auto& item : InvalidBase64EncodingData) { @@ -119,14 +129,6 @@ TEST(Base64Test, ExpectedSize) } } -TEST(Base64Test, ExpectedSizeWstring) -{ - for (const auto& item : Base64WStringTestData) { - std::size_t s = Base64::expectedLength(PARAM(1)); - ASSERT_EQ(PARAM(0).size(), s); - } -} - TEST(Base64Test, IsBase64) { for (const auto& item : IsBase64Data) { diff --git a/test/rsa-test.h b/test/rsa-test.h index 4b0e56b..bdcd216 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -24,7 +24,7 @@ class Helper : public BigIntegerHelper return static_cast(b.ConvertToLong()); } - virtual std::string bigIntegerToHex(const BigInteger& b) const override + virtual std::string bigIntegerToHex(BigInteger b) const override { std::stringstream ss; ss << std::hex << b; From b8e82108553cceec731564868e6eca20bc4d967e Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Fri, 25 Aug 2017 21:31:51 +1000 Subject: [PATCH 053/148] encode hex --- package/mine.cc | 37 +++++++++++++------------- package/mine.h | 65 ++++++++++++++++++++++++++++++++++------------ src/base16.cc | 13 +++++----- src/base16.h | 37 ++++++++++++++++++++++++++ src/base64.cc | 24 ++++++++--------- src/rsa.h | 19 ++++---------- test/base16-test.h | 22 ++++++++++++++++ 7 files changed, 148 insertions(+), 69 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 8f3468e..a858283 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -24,10 +24,12 @@ using namespace mine; const std::string Base16::kValidChars = "0123456789ABCDEF"; const std::unordered_map Base16::kDecodeMap = { - {48, 0}, {49, 1}, {50, 2}, {51, 3}, - {52, 4}, {53, 5}, {54, 6}, {55, 7}, - {56, 8}, {57, 9}, {65, 10}, {66, 11}, - {67, 12}, {68, 13},{69, 14}, {70, 15}, + {48, 0 }, {49, 1 }, {50, 2 }, {51, 3 }, + {52, 4 }, {53, 5 }, {54, 6 }, {55, 7 }, + {56, 8 }, {57, 9 }, {65, 10}, {66, 11}, + {67, 12}, {68, 13}, {69, 14}, {70, 15}, + {97, 10}, {98, 11}, {99, 12}, {100, 13}, + {101, 14}, {102, 15} }; std::string Base16::encode(const std::string& raw) noexcept @@ -45,7 +47,6 @@ std::string Base16::decode(const std::string& enc) if (enc.size() % 2 != 0) { throw std::runtime_error("Invalid base-16 encoding"); } - std::string s; std::stringstream ss; for (auto it = enc.begin(); it != enc.end(); it += 2) { int b0 = *it & 0xff; @@ -55,8 +56,6 @@ std::string Base16::decode(const std::string& enc) } catch (const std::exception&) { throw std::runtime_error("Invalid base-16 encoding"); } - - s = ss.str(); } return ss.str(); } @@ -65,23 +64,23 @@ std::string Base16::decode(const std::string& enc) const std::string Base64::kValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; const std::unordered_map Base64::kDecodeMap = { - {65, 0}, {66, 1}, {67, 2}, {68, 3}, - {69, 4}, {70, 5}, {71, 6}, {72, 7}, - {73, 8}, {74, 9}, {75, 10}, {76, 11}, - {77, 12}, {78, 13}, {79, 14}, {80, 15}, - {81, 16}, {82, 17}, {83, 18}, {84, 19}, - {85, 20}, {86, 21}, {87, 22}, {88, 23}, - {89, 24}, {90, 25}, {97, 26}, {98, 27}, - {99, 28}, {100, 29}, {101, 30}, {102, 31}, + {65, 0 }, {66, 1 }, {67, 2 }, {68, 3 }, + {69, 4 }, {70, 5 }, {71, 6 }, {72, 7 }, + {73, 8 }, {74, 9 }, {75, 10 }, {76, 11 }, + {77, 12 }, {78, 13 }, {79, 14 }, {80, 15 }, + {81, 16 }, {82, 17 }, {83, 18 }, {84, 19 }, + {85, 20 }, {86, 21 }, {87, 22 }, {88, 23 }, + {89, 24 }, {90, 25 }, {97, 26 }, {98, 27 }, + {99, 28 }, {100, 29}, {101, 30}, {102, 31}, {103, 32}, {104, 33}, {105, 34}, {106, 35}, {107, 36}, {108, 37}, {109, 38}, {110, 39}, {111, 40}, {112, 41}, {113, 42}, {114, 43}, {115, 44}, {116, 45}, {117, 46}, {118, 47}, {119, 48}, {120, 49}, {121, 50}, {122, 51}, - {48, 52}, {49, 53}, {50, 54}, {51, 55}, - {52, 56}, {53, 57}, {54, 58}, {55, 59}, - {56, 60}, {57, 61}, {43, 62}, {47, 63}, - {61, 64} + {48, 52 }, {49, 53 }, {50, 54 }, {51, 55 }, + {52, 56 }, {53, 57 }, {54, 58 }, {55, 59 }, + {56, 60 }, {57, 61 }, {43, 62 }, {47, 63 }, + {61, 64 } }; std::size_t Base64::countChars(const std::string& str) noexcept diff --git a/package/mine.h b/package/mine.h index 524fc76..f935a55 100644 --- a/package/mine.h +++ b/package/mine.h @@ -18,13 +18,11 @@ #define MINE_CRYPTO_H #include +#include #include -#include -#include #include #include #include -#include #include #include "src/base16.h" @@ -54,12 +52,48 @@ class Base16 { /// static std::string encode(const std::string& raw) noexcept; + /// + /// \brief Encodes integer to hex + /// + template + static std::string encode(T n) noexcept + { + std::stringstream ss; + int remainder; + while (n != 0) { + remainder = n % 16; + n /= 16; + ss << kValidChars[remainder]; + } + std::string res(ss.str()); + std::reverse(res.begin(), res.end()); + return res; + } + /// /// \brief Decodes encoded hex /// \throws std::runtime if invalid encoding. /// std::runtime::what() is set according to the error /// static std::string decode(const std::string& e); + + /// + /// \brief Decodes encoding to single integer of type T + /// + template + static T decodeInt(const std::string& e) + { + T result = 0; + for (auto it = e.begin(); it != e.end() && result >= 0; ++it) { + try { + result = ((result << 4) | kDecodeMap.at(*it & 0xff)); + } catch (const std::exception&) { + throw std::runtime_error("Invalid base-16 encoding"); + } + } + return result; + } + private: Base16() = delete; Base16(const Base16&) = delete; @@ -100,6 +134,7 @@ class Base64 { /// static std::size_t countChars(const std::string& d) noexcept; +#ifdef MINE_BASE64_WSTRING_CONVERSION /// /// \brief Converts it to std::string and calls countChars on it /// @@ -109,12 +144,14 @@ class Base64 { , wchar_t>{}.to_bytes(raw); return countChars(converted); } +#endif /// /// \brief Encodes input of length to base64 encoding /// static std::string encode(const std::string& raw) noexcept; +#ifdef MINE_BASE64_WSTRING_CONVERSION /// /// \brief Converts wstring to corresponding string and returns /// encoding @@ -126,6 +163,7 @@ class Base64 { , wchar_t>{}.to_bytes(raw); return encode(converted); } +#endif /// /// \brief Decodes encoded base64 @@ -135,6 +173,7 @@ class Base64 { /// static std::string decode(const std::string& e); +#ifdef MINE_BASE64_WSTRING_CONVERSION /// /// \brief Helper method to decode base64 encoding as wstring (basic_string) /// \see decode(const std::string&) @@ -150,6 +189,7 @@ class Base64 { >{}.from_bytes(result); return converted; } +#endif /// /// \brief expectedBase64Length Returns expected base64 length @@ -409,7 +449,7 @@ class BigIntegerHelper { /// you are using. /// Result should be stored in quotient and remainder /// - virtual inline void divideBigNumber(const BigInteger& divisor, const BigInteger& divident, + virtual void divideBigNumber(const BigInteger& divisor, const BigInteger& divident, BigInteger* quotient, BigInteger* remainder) const { *quotient = divisor / divident; @@ -427,24 +467,15 @@ class BigIntegerHelper { /// /// \brief Converts big integer to hex /// - virtual inline std::string bigIntegerToHex(BigInteger n) const + virtual std::string bigIntegerToHex(BigInteger n) const { - std::stringstream ss; - int remainder; - while (n != 0) { - remainder = n % 16; - n /= 16; - ss << Base16::kValidChars[remainder]; - } - std::string res(ss.str()); - std::reverse(res.begin(), res.end()); - return res; + return Base16::encode(n); } /// /// \brief Converts big integer to hex /// - virtual inline std::string bigIntegerToString(const BigInteger& b) const + virtual std::string bigIntegerToString(const BigInteger& b) const { std::stringstream ss; ss << b; @@ -455,7 +486,7 @@ class BigIntegerHelper { /// \brief Converts hex to big integer /// \param hex Hexadecimal without '0x' prefix /// - virtual inline BigInteger hexToBigInteger(const std::string& hex) const + virtual BigInteger hexToBigInteger(const std::string& hex) const { std::string readableMsg = "0x" + hex; BigInteger msg; diff --git a/src/base16.cc b/src/base16.cc index a3eb622..5a11150 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -23,10 +23,12 @@ using namespace mine; const std::string Base16::kValidChars = "0123456789ABCDEF"; const std::unordered_map Base16::kDecodeMap = { - {48, 0}, {49, 1}, {50, 2}, {51, 3}, - {52, 4}, {53, 5}, {54, 6}, {55, 7}, - {56, 8}, {57, 9}, {65, 10}, {66, 11}, - {67, 12}, {68, 13},{69, 14}, {70, 15}, + {48, 0 }, {49, 1 }, {50, 2 }, {51, 3 }, + {52, 4 }, {53, 5 }, {54, 6 }, {55, 7 }, + {56, 8 }, {57, 9 }, {65, 10}, {66, 11}, + {67, 12}, {68, 13}, {69, 14}, {70, 15}, + {97, 10}, {98, 11}, {99, 12}, {100, 13}, + {101, 14}, {102, 15} }; std::string Base16::encode(const std::string& raw) noexcept @@ -44,7 +46,6 @@ std::string Base16::decode(const std::string& enc) if (enc.size() % 2 != 0) { throw std::runtime_error("Invalid base-16 encoding"); } - std::string s; std::stringstream ss; for (auto it = enc.begin(); it != enc.end(); it += 2) { int b0 = *it & 0xff; @@ -54,8 +55,6 @@ std::string Base16::decode(const std::string& enc) } catch (const std::exception&) { throw std::runtime_error("Invalid base-16 encoding"); } - - s = ss.str(); } return ss.str(); } diff --git a/src/base16.h b/src/base16.h index 66c532a..4aa3fd2 100644 --- a/src/base16.h +++ b/src/base16.h @@ -22,6 +22,7 @@ #define Base16_H #include +#include #include namespace mine { @@ -50,12 +51,48 @@ class Base16 { /// static std::string encode(const std::string& raw) noexcept; + /// + /// \brief Encodes integer to hex + /// + template + static std::string encode(T n) noexcept + { + std::stringstream ss; + int remainder; + while (n != 0) { + remainder = n % 16; + n /= 16; + ss << kValidChars[remainder]; + } + std::string res(ss.str()); + std::reverse(res.begin(), res.end()); + return res; + } + /// /// \brief Decodes encoded hex /// \throws std::runtime if invalid encoding. /// std::runtime::what() is set according to the error /// static std::string decode(const std::string& e); + + /// + /// \brief Decodes encoding to single integer of type T + /// + template + static T decodeInt(const std::string& e) + { + T result = 0; + for (auto it = e.begin(); it != e.end() && result >= 0; ++it) { + try { + result = ((result << 4) | kDecodeMap.at(*it & 0xff)); + } catch (const std::exception&) { + throw std::runtime_error("Invalid base-16 encoding"); + } + } + return result; + } + private: Base16() = delete; Base16(const Base16&) = delete; diff --git a/src/base64.cc b/src/base64.cc index 8b0052d..13db03d 100644 --- a/src/base64.cc +++ b/src/base64.cc @@ -23,23 +23,23 @@ using namespace mine; const std::string Base64::kValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; const std::unordered_map Base64::kDecodeMap = { - {65, 0}, {66, 1}, {67, 2}, {68, 3}, - {69, 4}, {70, 5}, {71, 6}, {72, 7}, - {73, 8}, {74, 9}, {75, 10}, {76, 11}, - {77, 12}, {78, 13}, {79, 14}, {80, 15}, - {81, 16}, {82, 17}, {83, 18}, {84, 19}, - {85, 20}, {86, 21}, {87, 22}, {88, 23}, - {89, 24}, {90, 25}, {97, 26}, {98, 27}, - {99, 28}, {100, 29}, {101, 30}, {102, 31}, + {65, 0 }, {66, 1 }, {67, 2 }, {68, 3 }, + {69, 4 }, {70, 5 }, {71, 6 }, {72, 7 }, + {73, 8 }, {74, 9 }, {75, 10 }, {76, 11 }, + {77, 12 }, {78, 13 }, {79, 14 }, {80, 15 }, + {81, 16 }, {82, 17 }, {83, 18 }, {84, 19 }, + {85, 20 }, {86, 21 }, {87, 22 }, {88, 23 }, + {89, 24 }, {90, 25 }, {97, 26 }, {98, 27 }, + {99, 28 }, {100, 29}, {101, 30}, {102, 31}, {103, 32}, {104, 33}, {105, 34}, {106, 35}, {107, 36}, {108, 37}, {109, 38}, {110, 39}, {111, 40}, {112, 41}, {113, 42}, {114, 43}, {115, 44}, {116, 45}, {117, 46}, {118, 47}, {119, 48}, {120, 49}, {121, 50}, {122, 51}, - {48, 52}, {49, 53}, {50, 54}, {51, 55}, - {52, 56}, {53, 57}, {54, 58}, {55, 59}, - {56, 60}, {57, 61}, {43, 62}, {47, 63}, - {61, 64} + {48, 52 }, {49, 53 }, {50, 54 }, {51, 55 }, + {52, 56 }, {53, 57 }, {54, 58 }, {55, 59 }, + {56, 60 }, {57, 61 }, {43, 62 }, {47, 63 }, + {61, 64 } }; std::size_t Base64::countChars(const std::string& str) noexcept diff --git a/src/rsa.h b/src/rsa.h index 0d080d4..c000994 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -242,7 +242,7 @@ class BigIntegerHelper { /// you are using. /// Result should be stored in quotient and remainder /// - virtual inline void divideBigNumber(const BigInteger& divisor, const BigInteger& divident, + virtual void divideBigNumber(const BigInteger& divisor, const BigInteger& divident, BigInteger* quotient, BigInteger* remainder) const { *quotient = divisor / divident; @@ -260,24 +260,15 @@ class BigIntegerHelper { /// /// \brief Converts big integer to hex /// - virtual inline std::string bigIntegerToHex(BigInteger n) const + virtual std::string bigIntegerToHex(BigInteger n) const { - std::stringstream ss; - int remainder; - while (n != 0) { - remainder = n % 16; - n /= 16; - ss << Base16::kValidChars[remainder]; - } - std::string res(ss.str()); - std::reverse(res.begin(), res.end()); - return res; + return Base16::encode(n); } /// /// \brief Converts big integer to hex /// - virtual inline std::string bigIntegerToString(const BigInteger& b) const + virtual std::string bigIntegerToString(const BigInteger& b) const { std::stringstream ss; ss << b; @@ -288,7 +279,7 @@ class BigIntegerHelper { /// \brief Converts hex to big integer /// \param hex Hexadecimal without '0x' prefix /// - virtual inline BigInteger hexToBigInteger(const std::string& hex) const + virtual BigInteger hexToBigInteger(const std::string& hex) const { std::string readableMsg = "0x" + hex; BigInteger msg; diff --git a/test/base16-test.h b/test/base16-test.h index 714a988..ac6a29b 100644 --- a/test/base16-test.h +++ b/test/base16-test.h @@ -17,6 +17,12 @@ static TestData Base16TestData = { TestCase("616263313233213F242A262829272D3D407E", "abc123!?$*&()'-=@~"), }; +static TestData Base16IntTestData = { + TestCase("22FD3", 143315ULL), + TestCase("35639D3C8", 14331532232ULL), + TestCase("3D8E08048D", 264375895181ULL), +}; + static TestData InvalidBase16EncodingData = { TestCase("48656C6C6F20576F726C64F"), }; @@ -29,6 +35,14 @@ TEST(Base16Test, Encode) } } +TEST(Base16Test, EncodeInt) +{ + for (const auto& item : Base16IntTestData) { + std::string encoded = Base16::encode(PARAM(1)); + ASSERT_STREQ(PARAM(0).c_str(), encoded.c_str()); + } +} + TEST(Base16Test, Decode) { for (const auto& item : Base16TestData) { @@ -37,6 +51,14 @@ TEST(Base16Test, Decode) } } +TEST(Base16Test, DecodeInt) +{ + for (const auto& item : Base16IntTestData) { + unsigned long long decoded = Base16::decodeInt(PARAM(0)); + ASSERT_EQ(PARAM(1), decoded); + } +} + TEST(Base16Test, InvalidBase16Encoding) { for (const auto& item : InvalidBase16EncodingData) { From 80392574f6b9cbf8cefdccbf4c81656c68284cf5 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Fri, 25 Aug 2017 22:07:56 +1000 Subject: [PATCH 054/148] fix packaging script --- build.php | 5 ++++- package/mine.h | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/build.php b/build.php index e80dceb..dc48233 100644 --- a/build.php +++ b/build.php @@ -95,7 +95,10 @@ function resolveTemplate($template, $includes, $lines, $lib_version, $filename) $namespace_started = false; while (($line = fgets($fd, 2048)) !== false) { if ($pos = (strpos(trim($line), "#include")) === 0) { - $includes[] = substr($line, $pos + strlen("#include")); + // don't include header of the file + if (strpos(trim($line), "#include \"src/") === false) { + $includes[] = substr($line, $pos + strlen("#include")); + } } else if ($pos = (strpos(trim($line), "namespace mine {")) === 0) { $namespace_started = true; } else if ($pos = (strpos(trim($line), "} // end namespace mine")) === 0) { diff --git a/package/mine.h b/package/mine.h index f935a55..a9df65a 100644 --- a/package/mine.h +++ b/package/mine.h @@ -24,7 +24,6 @@ #include #include #include -#include "src/base16.h" namespace mine { From a0e4f8f4cbd3e87f3cacc93f62008604a4d360b9 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Fri, 25 Aug 2017 22:18:53 +1000 Subject: [PATCH 055/148] include algo --- src/base16.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/base16.h b/src/base16.h index 4aa3fd2..c7a5b96 100644 --- a/src/base16.h +++ b/src/base16.h @@ -21,6 +21,7 @@ #ifndef Base16_H #define Base16_H +#include #include #include #include From a1fc6d3d2e3e79646077d2c472ab232f95f7c541 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Fri, 25 Aug 2017 22:19:53 +1000 Subject: [PATCH 056/148] lice read --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 6ddc85b..39c17d6 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ For _minimal_ library this is what we are aiming. You can only contribute by testing on various platforms and reporting the issues. We are not accepting any pull requests until first release. # License + ``` Copyright 2017 Muflihun Labs From 8286e4dbd8dd2253808d7ab5c43dd0a5412ac3fd Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sat, 26 Aug 2017 00:54:43 +1000 Subject: [PATCH 057/148] starting aes --- src/aes.cc | 5 ++++- src/aes.h | 2 ++ test/aes-test.h | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/aes.cc b/src/aes.cc index 6d9fd95..a593cfb 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -13,7 +13,10 @@ // // https://github.com/muflihun/mine // - +// +// need to verify against +// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf +// #include "src/aes.h" using namespace mine; diff --git a/src/aes.h b/src/aes.h index 97e7c9a..51e99f5 100644 --- a/src/aes.h +++ b/src/aes.h @@ -23,6 +23,8 @@ namespace mine { +using byte = unsigned char; + /// /// \brief Provides AES crypto functionalities /// diff --git a/test/aes-test.h b/test/aes-test.h index 01c015f..4fc1863 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -11,6 +11,32 @@ namespace mine { +static const byte kNistIV[16] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f +}; + +static const byte kNistKey128[16] = { + 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c +}; + +static const byte kNistKey192[24] = { + 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, + 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, + 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b +}; + +static const byte kNistKey256[32] = { + 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, + 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, + 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, + 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 +}; + +// from http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf + + } #endif // AES_TEST_H From 1ef5b87e8ba7290775dbac59f1ffab9746032ffd Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sat, 26 Aug 2017 14:10:40 +1000 Subject: [PATCH 058/148] minor --- package/mine.h | 5 +++++ src/aes.cc | 3 ++- src/aes.h | 2 ++ src/base64.h | 10 ++++++++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/package/mine.h b/package/mine.h index a9df65a..b1215f9 100644 --- a/package/mine.h +++ b/package/mine.h @@ -17,6 +17,7 @@ #ifndef MINE_CRYPTO_H #define MINE_CRYPTO_H +#include #include #include #include @@ -137,6 +138,8 @@ class Base64 { /// /// \brief Converts it to std::string and calls countChars on it /// + /// \note You need to include and headers before mine.h + /// static std::size_t countChars(const std::wstring& raw) noexcept { std::string converted = std::wstring_convert @@ -225,6 +228,8 @@ class Base64 { Base64& operator=(const Base64&) = delete; }; +using byte = unsigned char; + /// /// \brief Provides AES crypto functionalities /// diff --git a/src/aes.cc b/src/aes.cc index a593cfb..018e610 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -14,7 +14,8 @@ // https://github.com/muflihun/mine // // -// need to verify against +// Standard publication +// http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf // http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf // #include "src/aes.h" diff --git a/src/aes.h b/src/aes.h index 51e99f5..a559ddf 100644 --- a/src/aes.h +++ b/src/aes.h @@ -21,6 +21,8 @@ #ifndef AES_H #define AES_H +#include + namespace mine { using byte = unsigned char; diff --git a/src/base64.h b/src/base64.h index c708901..f9bcaeb 100644 --- a/src/base64.h +++ b/src/base64.h @@ -23,6 +23,10 @@ #include #include + +// codecvt is not part of standard +// hence we leave it to user to enable/disable +// it depending on their #ifdef MINE_BASE64_WSTRING_CONVERSION # include # include @@ -68,6 +72,8 @@ class Base64 { /// /// \brief Converts it to std::string and calls countChars on it /// + /// \note You need to include and headers before mine.h + /// static std::size_t countChars(const std::wstring& raw) noexcept { std::string converted = std::wstring_convert @@ -87,6 +93,8 @@ class Base64 { /// encoding /// \see encode(const std::string&) /// + /// \note You need to include and headers before mine.h + /// static std::string encode(const std::wstring& raw) noexcept { std::string converted = std::wstring_convert @@ -112,6 +120,8 @@ class Base64 { /// 5+ bytes long e.g, \x1F680. If you don't use such characters then it should be safe /// to use this /// + /// \note You need to include and headers before mine.h + /// static std::wstring decodeAsWString(const std::string& e) { std::string result = decode(e); From 65d6dd6906ca2886c60539c14e122888104a9921 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sat, 26 Aug 2017 14:33:04 +1000 Subject: [PATCH 059/148] bupdate --- src/aes.cc | 5 +++++ src/aes.h | 8 +++++++- src/base16.cc | 12 +++++------- src/base16.h | 3 ++- src/base64.cc | 36 ++++++++++++++++++------------------ src/base64.h | 3 ++- 6 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 018e610..be61005 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -21,3 +21,8 @@ #include "src/aes.h" using namespace mine; + +std::string AES::cipher(const std::string& plainText, const std::string& key) +{ + return ""; +} diff --git a/src/aes.h b/src/aes.h index a559ddf..001dd6f 100644 --- a/src/aes.h +++ b/src/aes.h @@ -32,7 +32,13 @@ using byte = unsigned char; /// class AES { public: - + /// + /// \brief Main encrypt function without any mode + /// \param plainText Plain text in + /// \param key Key in hex, can be 128-bit, 192-bit or 256-bit key + /// \return cipher text + /// + static std::string cipher(const std::string& plainText, const std::string& key); private: AES() = delete; AES(const AES&) = delete; diff --git a/src/base16.cc b/src/base16.cc index 5a11150..918361c 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -22,13 +22,11 @@ using namespace mine; const std::string Base16::kValidChars = "0123456789ABCDEF"; -const std::unordered_map Base16::kDecodeMap = { - {48, 0 }, {49, 1 }, {50, 2 }, {51, 3 }, - {52, 4 }, {53, 5 }, {54, 6 }, {55, 7 }, - {56, 8 }, {57, 9 }, {65, 10}, {66, 11}, - {67, 12}, {68, 13}, {69, 14}, {70, 15}, - {97, 10}, {98, 11}, {99, 12}, {100, 13}, - {101, 14}, {102, 15} +const std::unordered_map Base16::kDecodeMap = { + {0x30, 0x00}, {0x31, 0x01}, {0x32, 0x02}, {0x33, 0x03}, + {0x34, 0x04}, {0x35, 0x05}, {0x36, 0x06}, {0x37, 0x07}, + {0x38, 0x08}, {0x39, 0x09}, {0x41, 0x0A}, {0x42, 0x0B}, + {0x43, 0x0C}, {0x44, 0x0D}, {0x45, 0x0E}, {0x46, 0x0F} }; std::string Base16::encode(const std::string& raw) noexcept diff --git a/src/base16.h b/src/base16.h index c7a5b96..bc5388e 100644 --- a/src/base16.h +++ b/src/base16.h @@ -21,6 +21,7 @@ #ifndef Base16_H #define Base16_H +#include #include #include #include @@ -45,7 +46,7 @@ class Base16 { /// \brief Map for fast lookup corresponding character /// \see Base64::kDecodeMap /// - static const std::unordered_map kDecodeMap; + static const std::unordered_map kDecodeMap; /// /// \brief Encodes input of length to hex encoding diff --git a/src/base64.cc b/src/base64.cc index 13db03d..c40b669 100644 --- a/src/base64.cc +++ b/src/base64.cc @@ -22,24 +22,24 @@ using namespace mine; const std::string Base64::kValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; -const std::unordered_map Base64::kDecodeMap = { - {65, 0 }, {66, 1 }, {67, 2 }, {68, 3 }, - {69, 4 }, {70, 5 }, {71, 6 }, {72, 7 }, - {73, 8 }, {74, 9 }, {75, 10 }, {76, 11 }, - {77, 12 }, {78, 13 }, {79, 14 }, {80, 15 }, - {81, 16 }, {82, 17 }, {83, 18 }, {84, 19 }, - {85, 20 }, {86, 21 }, {87, 22 }, {88, 23 }, - {89, 24 }, {90, 25 }, {97, 26 }, {98, 27 }, - {99, 28 }, {100, 29}, {101, 30}, {102, 31}, - {103, 32}, {104, 33}, {105, 34}, {106, 35}, - {107, 36}, {108, 37}, {109, 38}, {110, 39}, - {111, 40}, {112, 41}, {113, 42}, {114, 43}, - {115, 44}, {116, 45}, {117, 46}, {118, 47}, - {119, 48}, {120, 49}, {121, 50}, {122, 51}, - {48, 52 }, {49, 53 }, {50, 54 }, {51, 55 }, - {52, 56 }, {53, 57 }, {54, 58 }, {55, 59 }, - {56, 60 }, {57, 61 }, {43, 62 }, {47, 63 }, - {61, 64 } +const std::unordered_map Base64::kDecodeMap = { + {0x41, 0x00}, {0x42, 0x01}, {0x43, 0x02}, {0x44, 0x03}, + {0x45, 0x04}, {0x46, 0x05}, {0x47, 0x06}, {0x48, 0x07}, + {0x49, 0x08}, {0x4A, 0x09}, {0x4B, 0x0A}, {0x4C, 0x0B}, + {0x4D, 0x0C}, {0x4E, 0x0D}, {0x4F, 0x0E}, {0x50, 0x0F}, + {0x51, 0x10}, {0x52, 0x11}, {0x53, 0x12}, {0x54, 0x13}, + {0x55, 0x14}, {0x56, 0x15}, {0x57, 0x16}, {0x58, 0x17}, + {0x59, 0x18}, {0x5A, 0x19}, {0x61, 0x1A}, {0x62, 0x1B}, + {0x63, 0x1C}, {0x64, 0x1D}, {0x65, 0x1E}, {0x66, 0x1F}, + {0x67, 0x20}, {0x68, 0x21}, {0x69, 0x22}, {0x6A, 0x23}, + {0x6B, 0x24}, {0x6C, 0x25}, {0x6D, 0x26}, {0x6E, 0x27}, + {0x6F, 0x28}, {0x70, 0x29}, {0x71, 0x2A}, {0x72, 0x2B}, + {0x73, 0x2C}, {0x74, 0x2D}, {0x75, 0x2E}, {0x76, 0x2F}, + {0x77, 0x30}, {0x78, 0x31}, {0x79, 0x32}, {0x7A, 0x33}, + {0x30, 0x34}, {0x31, 0x35}, {0x32, 0x36}, {0x33, 0x37}, + {0x34, 0x38}, {0x35, 0x39}, {0x36, 0x3A}, {0x37, 0x3B}, + {0x38, 0x3C}, {0x39, 0x3D}, {0x2B, 0x3E}, {0x2F, 0x3F}, + {0x3D, 0x40} }; std::size_t Base64::countChars(const std::string& str) noexcept diff --git a/src/base64.h b/src/base64.h index f9bcaeb..b197b48 100644 --- a/src/base64.h +++ b/src/base64.h @@ -21,6 +21,7 @@ #ifndef Base64_H #define Base64_H +#include #include #include @@ -55,7 +56,7 @@ class Base64 { /// \ref http://www.cplusplus.com/reference/unordered_map/unordered_map/at/ /// \ref http://www.cplusplus.com/reference/string/string/find_first_of/ /// - static const std::unordered_map kDecodeMap; + static const std::unordered_map kDecodeMap; /// /// \brief Padding is must in mine implementation of base64 From 0945d82aef5662fee693645c9988274b9e349b05 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sat, 26 Aug 2017 15:47:19 +1000 Subject: [PATCH 060/148] more improvements for aes --- src/aes.cc | 70 +++++++++++++++++++++++++++++++++++++++++++++++-- src/aes.h | 51 ++++++++++++++++++++++++++++++----- src/base16.cc | 2 +- src/base16.h | 3 +-- src/base64.cc | 2 +- src/base64.h | 3 +-- test/aes-test.h | 31 +++++++++++++++++++++- test/main.cc | 8 +++--- 8 files changed, 151 insertions(+), 19 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index be61005..ee55cad 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -18,11 +18,77 @@ // http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf // http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf // + +#include +#include "src/base16.h" #include "src/aes.h" using namespace mine; -std::string AES::cipher(const std::string& plainText, const std::string& key) +const byte AES::kSBox[256] = { + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0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xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 +}; + +const byte AES::kSBoxInverse[256] = { + 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, + 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, + 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, + 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, + 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, + 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, + 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, + 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, + 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, + 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, + 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, + 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, + 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, + 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, + 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, + 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d +}; + +void AES::transposeBytes(byte input[], std::size_t len) +{ + std::size_t n = std::sqrt(len); + for (int i = 0; i < n; ++i) { + for (int j = i+1; j < n; ++j) { + std::swap(input[n*i + j], input[n*j + i]); + } + } +} + +void AES::printBytes(byte b[], std::size_t len) { - return ""; + std::cout << "------" << std::endl; + for (int i = 1; i <= len; ++i) { + std::cout << "0x" << (b[i - 1] < 10 ? "0" : "") << Base16::encode(b[i - 1]) << " "; + if (i % 4 == 0) { + std::cout << std::endl; + } + } + std::cout << std::endl << "------" << std::endl; +} + +byte* AES::cipher(byte input[], std::size_t len, byte key[]) +{ + CipherState state[4][4]; + + // FIPS.197 p.14 + return 0; } diff --git a/src/aes.h b/src/aes.h index 001dd6f..b5b1a62 100644 --- a/src/aes.h +++ b/src/aes.h @@ -32,17 +32,56 @@ using byte = unsigned char; /// class AES { public: + + static void transposeBytes(byte input[], std::size_t len); +private: + /// - /// \brief Main encrypt function without any mode - /// \param plainText Plain text in - /// \param key Key in hex, can be 128-bit, 192-bit or 256-bit key - /// \return cipher text + /// \brief Raw encryption function + /// \param plainText Byte array of input + /// \param key Byte array of key + /// \return cipher text (byte array) /// - static std::string cipher(const std::string& plainText, const std::string& key); -private: + static byte* cipher(byte input[], std::size_t len, byte key[]); + + /// + /// \brief State as described in FIPS.197 Sec. 3.4 + /// \see kNb + /// + using CipherState = byte[4][4 /* Nb */]; + + /// + /// \brief AES works on 16 bit block at a time + /// + static const uint8_t kBlockSize = 16; + + /// + /// \brief As defined in FIPS. 197 Sec. 5.1.1 + /// + static const byte kSBox[256]; + + /// + /// \brief As defined in FIPS. 197 Sec. 5.3.2 + /// + static const byte kSBoxInverse[256]; + + /// + /// \brief Nb + /// \note we make it constant as FIPS.197 p.9 says + /// "For this standard, Nb=4." + /// + static const uint8_t kNb = 4; + + /// + /// \brief Prints bytes in hex format in 4x4 matrix fasion + /// + static void printBytes(byte b[], std::size_t len); + AES() = delete; AES(const AES&) = delete; AES& operator=(const AES&) = delete; + + friend class AESTest_SimpleCipher_Test; }; } // end namespace mine diff --git a/src/base16.cc b/src/base16.cc index 918361c..0cd4dfe 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -22,7 +22,7 @@ using namespace mine; const std::string Base16::kValidChars = "0123456789ABCDEF"; -const std::unordered_map Base16::kDecodeMap = { +const std::unordered_map Base16::kDecodeMap = { {0x30, 0x00}, {0x31, 0x01}, {0x32, 0x02}, {0x33, 0x03}, {0x34, 0x04}, {0x35, 0x05}, {0x36, 0x06}, {0x37, 0x07}, {0x38, 0x08}, {0x39, 0x09}, {0x41, 0x0A}, {0x42, 0x0B}, diff --git a/src/base16.h b/src/base16.h index bc5388e..13656c0 100644 --- a/src/base16.h +++ b/src/base16.h @@ -21,7 +21,6 @@ #ifndef Base16_H #define Base16_H -#include #include #include #include @@ -46,7 +45,7 @@ class Base16 { /// \brief Map for fast lookup corresponding character /// \see Base64::kDecodeMap /// - static const std::unordered_map kDecodeMap; + static const std::unordered_map kDecodeMap; /// /// \brief Encodes input of length to hex encoding diff --git a/src/base64.cc b/src/base64.cc index c40b669..4e79858 100644 --- a/src/base64.cc +++ b/src/base64.cc @@ -22,7 +22,7 @@ using namespace mine; const std::string Base64::kValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; -const std::unordered_map Base64::kDecodeMap = { +const std::unordered_map Base64::kDecodeMap = { {0x41, 0x00}, {0x42, 0x01}, {0x43, 0x02}, {0x44, 0x03}, {0x45, 0x04}, {0x46, 0x05}, {0x47, 0x06}, {0x48, 0x07}, {0x49, 0x08}, {0x4A, 0x09}, {0x4B, 0x0A}, {0x4C, 0x0B}, diff --git a/src/base64.h b/src/base64.h index b197b48..d05cece 100644 --- a/src/base64.h +++ b/src/base64.h @@ -21,7 +21,6 @@ #ifndef Base64_H #define Base64_H -#include #include #include @@ -56,7 +55,7 @@ class Base64 { /// \ref http://www.cplusplus.com/reference/unordered_map/unordered_map/at/ /// \ref http://www.cplusplus.com/reference/string/string/find_first_of/ /// - static const std::unordered_map kDecodeMap; + static const std::unordered_map kDecodeMap; /// /// \brief Padding is must in mine implementation of base64 diff --git a/test/aes-test.h b/test/aes-test.h index 4fc1863..21909dd 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -11,6 +11,9 @@ namespace mine { + +// from http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf + static const byte kNistIV[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f @@ -34,9 +37,35 @@ static const byte kNistKey256[32] = { 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 }; -// from http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf +// FIPS. 197 p. 33 +// this is not real test +// this is only to aid implementation +TEST(AESTest, SimpleCipher) +{ + + byte input[16] = { + 0x32, 0x43, 0xf6, 0xa8, + 0x88, 0x5a, 0x30, 0x8d, + 0x31, 0x31, 0x98, 0xa2, + 0xe0, 0x37, 0x07, 0x34 + }; + byte key[16] = { + 0x2b, 0x7e, 0x15, 0x16, + 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, + 0x09, 0xcf, 0x4f, 0x3c + }; + AES::transposeBytes(input, 16); + AES::transposeBytes(key, 16); + + AES::printBytes(input, 16); + + byte* output = AES::cipher(input, 16, key); + + +} } #endif // AES_TEST_H diff --git a/test/main.cc b/test/main.cc index 539fb7d..c2017d2 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,9 +1,9 @@ #include "test.h" -#include "base16-test.h" -#include "base64-test.h" +//#include "base16-test.h" +//#include "base64-test.h" #include "aes-test.h" -#include "zlib-test.h" -#include "rsa-test.h" +//#include "zlib-test.h" +//#include "rsa-test.h" INITIALIZE_EASYLOGGINGPP From 051d6a2d8ddb11bc90327ace24be5d4d1c663f30 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sat, 26 Aug 2017 17:41:03 +1000 Subject: [PATCH 061/148] another update --- src/aes.cc | 130 ++++++++++++++++++++++++++++++++++++++++++++++-- src/aes.h | 31 ++++++++---- test/aes-test.h | 6 ++- 3 files changed, 153 insertions(+), 14 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index ee55cad..3fd41d2 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -63,6 +63,10 @@ const byte AES::kSBoxInverse[256] = { 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; +const uint8_t AES::kRoundConstant[11] = { + 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 +}; + void AES::transposeBytes(byte input[], std::size_t len) { std::size_t n = std::sqrt(len); @@ -85,10 +89,130 @@ void AES::printBytes(byte b[], std::size_t len) std::cout << std::endl << "------" << std::endl; } -byte* AES::cipher(byte input[], std::size_t len, byte key[]) +void AES::keyExpansion(byte output___[], byte key[], std::size_t keySize) { - CipherState state[4][4]; + uint8_t keyExSize, Nk, Nr; + getKeyParams(keySize, &keyExSize, &Nk, &Nr); + byte output[keyExSize]; // move this to output___ - this is only for visualizing + printBytes(output, keyExSize); + + word tmp; + + int i = 0; + // copy main key as is for the first round + for (; i < Nk; ++i) { + for (uint8_t j = 0; j < 4; ++j) { + output[(i * 4) + j] = key[(i * 4) + j]; + } + } + + printBytes(output, keyExSize); + + // rotateWord function is specified in FIPS.197 Sec. 5.2: + // The function RotWord() takes a + // word [a0,a1,a2,a3] as input, performs a cyclic permutation, + // and returns the word [a1,a2,a3,a0]. The + // round constant word array + // + // Our definition: + // We swap the first byte + // to last one causing it to shift to the left + // i.e, + // [a1] [a2] + // [a2] [a3] + // [a3] => [a4] + // [a4] [a1] + // + auto rotateWord = [](word& w) { + decltype(w[0]) t = w[0]; + w[0] = w[1]; + w[1] = w[2]; + w[2] = w[3]; + w[3] = t; + }; + + // this function is also specified in FIPS.197 Sec. 5.2: + // SubWord() is a function that takes a four-byte + // input word and applies the S-box + // to each of the four bytes to produce an output word. + // + // Out definition: + // It's a simple substition with kSbox for corresponding bit + // index + // + auto substituteWord = [](word& w) { + for (uint8_t i = 0; i < 4; ++i) { + w[i] = kSBox[w[i]]; + } + }; + + // i == Nk here + + + // FIPS.197 p.27 for examples + + for (; i < kNb * (Nr + 1); ++i) { + //prev = + // temp = w[i-1] + for (uint8_t j = 0; j < 4; ++j) { + tmp[j] = output[((i - 1) * 4) + j]; + } + + if (i % Nk == 0) { + rotateWord(tmp); + substituteWord(tmp); + + // xor with round constant as specified in p.20 + tmp[0] = tmp[0] ^ kRoundConstant[i / Nk]; + } else if (Nk > 6 && i % Nk == 4) { + substituteWord(tmp); + } + + // this round key is xor of previous round key + for (uint8_t j = 0; j < 4; ++j) { + output[(i * 4) + j] = output[((i - Nk) * 4) + j] ^ tmp[j]; + } + } + + printBytes(output, keyExSize); + +} + +void AES::cipher(byte output[], byte input[], std::size_t len, byte key[], std::size_t keySize) +{ // FIPS.197 p.14 - return 0; + CipherState state[4][4]; + + std::memcpy(output, input, len); + uint8_t keyExSize, Nk, Nr; + + getKeyParams(keySize, &keyExSize, &Nk, &Nr); + + byte roundKeys[keyExSize]; + keyExpansion(roundKeys, key, keySize); + } + +void AES::getKeyParams(std::size_t keySize, uint8_t *keyExSize, uint8_t *Nk, uint8_t *Nr) +{ + switch (keySize) { + case 128: + *keyExSize = 176; + *Nk = 4; + *Nr = 10; + break; + case 192: + *keyExSize = 208; + *Nk = 6; + *Nr = 12; + break; + case 256: + *keyExSize = 240; + *Nk = 8; + *Nr = 14; + break; + default: + throw std::invalid_argument("Invalid AES key size"); + } + }; diff --git a/src/aes.h b/src/aes.h index b5b1a62..0a0990c 100644 --- a/src/aes.h +++ b/src/aes.h @@ -26,6 +26,7 @@ namespace mine { using byte = unsigned char; +using word = byte[4]; /// /// \brief Provides AES crypto functionalities @@ -36,14 +37,6 @@ class AES { static void transposeBytes(byte input[], std::size_t len); private: - /// - /// \brief Raw encryption function - /// \param plainText Byte array of input - /// \param key Byte array of key - /// \return cipher text (byte array) - /// - static byte* cipher(byte input[], std::size_t len, byte key[]); - /// /// \brief State as described in FIPS.197 Sec. 3.4 /// \see kNb @@ -65,6 +58,8 @@ class AES { /// static const byte kSBoxInverse[256]; + static const byte kRoundConstant[11]; + /// /// \brief Nb /// \note we make it constant as FIPS.197 p.9 says @@ -73,7 +68,25 @@ class AES { static const uint8_t kNb = 4; /// - /// \brief Prints bytes in hex format in 4x4 matrix fasion + /// \brief Raw encryption function + /// \param output Byte array for desitnation + /// \param input Byte array of input + /// \param key Byte array of key + /// \return cipher text (byte array) + /// + static void cipher(byte output[], byte input[], std::size_t len, byte key[], std::size_t keySize = 128); + + static void getKeyParams(std::size_t keySize, uint8_t* keyExSize, uint8_t* Nk, uint8_t* Nr); + + /// + /// \brief generateRoundKeys + /// \param output + /// \param keySchedule + /// + static void keyExpansion(byte output[], byte key[], std::size_t keySize); + + /// + /// \brief Prints bytes in hex format in 4x4 matrix fashion /// static void printBytes(byte b[], std::size_t len); diff --git a/test/aes-test.h b/test/aes-test.h index 21909dd..d7f297f 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -57,12 +57,14 @@ TEST(AESTest, SimpleCipher) 0x09, 0xcf, 0x4f, 0x3c }; + byte output[16]; + AES::transposeBytes(input, 16); AES::transposeBytes(key, 16); - AES::printBytes(input, 16); + AES::printBytes(key, 16); - byte* output = AES::cipher(input, 16, key); + AES::cipher(output, input, 16, key); } From 78ddf03c9c564116304e7b2906006d7f63c964cf Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sat, 26 Aug 2017 17:41:46 +1000 Subject: [PATCH 062/148] minor update --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 8b75b0e..a4189d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +logs CMakeFiles build CMakeLists.txt.user From a2827f3babf861e690b261cdce5ac9b1a36fcbe3 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sat, 26 Aug 2017 20:50:52 +1000 Subject: [PATCH 063/148] major changes --- src/aes.cc | 103 +++++++++++++++++++++++++++++------------------- src/aes.h | 8 +++- test/aes-test.h | 11 +++++- 3 files changed, 78 insertions(+), 44 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 3fd41d2..3285494 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -20,6 +20,7 @@ // #include +#include #include "src/base16.h" #include "src/aes.h" @@ -89,26 +90,9 @@ void AES::printBytes(byte b[], std::size_t len) std::cout << std::endl << "------" << std::endl; } -void AES::keyExpansion(byte output___[], byte key[], std::size_t keySize) +RoundKeys AES::keyExpansion(byte key[], std::size_t keySize) { - uint8_t keyExSize, Nk, Nr; - getKeyParams(keySize, &keyExSize, &Nk, &Nr); - byte output[keyExSize]; // move this to output___ - this is only for visualizing - printBytes(output, keyExSize); - - word tmp; - - int i = 0; - // copy main key as is for the first round - for (; i < Nk; ++i) { - for (uint8_t j = 0; j < 4; ++j) { - output[(i * 4) + j] = key[(i * 4) + j]; - } - } - - printBytes(output, keyExSize); - // rotateWord function is specified in FIPS.197 Sec. 5.2: // The function RotWord() takes a // word [a0,a1,a2,a3] as input, performs a cyclic permutation, @@ -124,12 +108,13 @@ void AES::keyExpansion(byte output___[], byte key[], std::size_t keySize) // [a3] => [a4] // [a4] [a1] // - auto rotateWord = [](word& w) { - decltype(w[0]) t = w[0]; + auto rotateWord = [](Word& w) -> Word { + byte t = w[0]; w[0] = w[1]; w[1] = w[2]; w[2] = w[3]; w[3] = t; + return w; }; // this function is also specified in FIPS.197 Sec. 5.2: @@ -141,41 +126,80 @@ void AES::keyExpansion(byte output___[], byte key[], std::size_t keySize) // It's a simple substition with kSbox for corresponding bit // index // - auto substituteWord = [](word& w) { + auto substituteWord = [](Word& w) -> Word { for (uint8_t i = 0; i < 4; ++i) { w[i] = kSBox[w[i]]; } + return w; }; - // i == Nk here + const std::size_t keyLen = keySize / 8; + uint8_t keyExSize, Nk, Nr; + getKeyParams(keySize, &keyExSize, &Nk, &Nr); + RoundKeys roundKeys;//(keyExSize); - // FIPS.197 p.27 for examples + std::vector linearWords; - for (; i < kNb * (Nr + 1); ++i) { - //prev = - // temp = w[i-1] - for (uint8_t j = 0; j < 4; ++j) { - tmp[j] = output[((i - 1) * 4) + j]; + // copy main key as is for the first round + Key newRoundKey = {{ 0 }}; + for (uint8_t i = 0; i < keyLen; ++i) { + newRoundKey[i] = key[i]; + } + roundKeys.push_back(newRoundKey); + + auto addRoundKeyToLinearArr = [&](const Key& key) { + for (auto iter = key.begin(); iter < key.end(); iter += 4) { + Word w = { *iter, *(iter + 1), *(iter + 2), *(iter + 3) }; + linearWords.push_back(w); } + }; + + addRoundKeyToLinearArr(newRoundKey); - if (i % Nk == 0) { - rotateWord(tmp); - substituteWord(tmp); + for (uint8_t i = Nk; i < kNb * (Nr + 1); ++i) { + const Key* previousRoundKey = &roundKeys[roundKeys.size() - 1]; + Key newRoundKey = {{0}}; + Word temp; - // xor with round constant as specified in p.20 - tmp[0] = tmp[0] ^ kRoundConstant[i / Nk]; + for (uint8_t j = 0; j < sizeof(Word); ++j) { + // temp is last col of prev round key + temp[j] = (*previousRoundKey)[(keyLen - 4) + j]; + } + if (i % 4 == 0) { + temp = rotateWord(temp); + temp = substituteWord(temp); + // xor with rcon + temp[0] ^= kRoundConstant[i / Nk]; } else if (Nk > 6 && i % Nk == 4) { - substituteWord(tmp); + temp = substituteWord(temp); + } + + for (uint8_t j = 0; j < sizeof(Word); ++j) { + // xor with first column of previous round key + temp[j] ^= (*previousRoundKey)[j]; + newRoundKey[j] = temp[j]; } - // this round key is xor of previous round key - for (uint8_t j = 0; j < 4; ++j) { - output[(i * 4) + j] = output[((i - Nk) * 4) + j] ^ tmp[j]; + // xor previous column of new key with corresponding column of + // previous round key + + for (int col = 1; col < sizeof(Word); ++col) { + int c = col * sizeof(Word); + for (int j = c; j < c + sizeof(Word); ++j) { + newRoundKey[j] = newRoundKey[j - 4] ^ (*previousRoundKey)[j]; + } } + + roundKeys.push_back(newRoundKey); + linearWords.push_back(temp); + //addRoundKeyToLinearArr(newRoundKey); + } - printBytes(output, keyExSize); + std::cout << "f"; + + } @@ -189,8 +213,7 @@ void AES::cipher(byte output[], byte input[], std::size_t len, byte key[], std:: getKeyParams(keySize, &keyExSize, &Nk, &Nr); - byte roundKeys[keyExSize]; - keyExpansion(roundKeys, key, keySize); + RoundKeys roundKeys = keyExpansion(key, keySize); } diff --git a/src/aes.h b/src/aes.h index 0a0990c..380d376 100644 --- a/src/aes.h +++ b/src/aes.h @@ -22,11 +22,15 @@ #define AES_H #include +#include +#include namespace mine { using byte = unsigned char; -using word = byte[4]; +using Word = std::array; +using Key = std::array; +using RoundKeys = std::vector; /// /// \brief Provides AES crypto functionalities @@ -83,7 +87,7 @@ class AES { /// \param output /// \param keySchedule /// - static void keyExpansion(byte output[], byte key[], std::size_t keySize); + static RoundKeys keyExpansion(byte key[], std::size_t keySize); /// /// \brief Prints bytes in hex format in 4x4 matrix fashion diff --git a/test/aes-test.h b/test/aes-test.h index d7f297f..79dbfae 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -57,12 +57,19 @@ TEST(AESTest, SimpleCipher) 0x09, 0xcf, 0x4f, 0x3c }; + byte key2[16] = { + 0x53, 0x4F, 0x4D, 0x45, + 0x20, 0x31, 0x32, 0x38, + 0x20, 0x42, 0x49, 0x54, + 0x20, 0x4B, 0x45, 0x59, + }; + byte output[16]; AES::transposeBytes(input, 16); - AES::transposeBytes(key, 16); + //AES::transposeBytes(key, 16); - AES::printBytes(key, 16); + AES::printBytes(key2, 16); AES::cipher(output, input, 16, key); From cff386133e2f62822ae9f87d1ae49fb5ad88df2e Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sat, 26 Aug 2017 22:17:28 +1000 Subject: [PATCH 064/148] finished key expansion --- src/aes.cc | 121 ++++++++++-------------- src/aes.h | 14 +-- test/aes-test.h | 245 +++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 280 insertions(+), 100 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 3285494..33b1923 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -65,7 +65,7 @@ const byte AES::kSBoxInverse[256] = { }; const uint8_t AES::kRoundConstant[11] = { - 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 + 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; void AES::transposeBytes(byte input[], std::size_t len) @@ -90,7 +90,7 @@ void AES::printBytes(byte b[], std::size_t len) std::cout << std::endl << "------" << std::endl; } -RoundKeys AES::keyExpansion(byte key[], std::size_t keySize) +AES::RoundKeys AES::keyExpansion(const Key* key) { // rotateWord function is specified in FIPS.197 Sec. 5.2: @@ -133,77 +133,54 @@ RoundKeys AES::keyExpansion(byte key[], std::size_t keySize) return w; }; - const std::size_t keyLen = keySize / 8; uint8_t keyExSize, Nk, Nr; - getKeyParams(keySize, &keyExSize, &Nk, &Nr); + getKeyParams(key->size(), &keyExSize, &Nk, &Nr); - RoundKeys roundKeys;//(keyExSize); - - std::vector linearWords; + RoundKeys words(kNb * (Nr+1)); + int i = 0; // copy main key as is for the first round - Key newRoundKey = {{ 0 }}; - for (uint8_t i = 0; i < keyLen; ++i) { - newRoundKey[i] = key[i]; + for (; i < Nk; ++i) { + words[i] = {{ + (*key)[(i * 4) + 0], + (*key)[(i * 4) + 1], + (*key)[(i * 4) + 2], + (*key)[(i * 4) + 3], + }}; } - roundKeys.push_back(newRoundKey); - - auto addRoundKeyToLinearArr = [&](const Key& key) { - for (auto iter = key.begin(); iter < key.end(); iter += 4) { - Word w = { *iter, *(iter + 1), *(iter + 2), *(iter + 3) }; - linearWords.push_back(w); - } - }; - addRoundKeyToLinearArr(newRoundKey); + const int kModCheckThreshold = i; - for (uint8_t i = Nk; i < kNb * (Nr + 1); ++i) { - const Key* previousRoundKey = &roundKeys[roundKeys.size() - 1]; - Key newRoundKey = {{0}}; - Word temp; + for (; i < kNb * (Nr + 1); ++i) { + Word temp = words[i - 1]; - for (uint8_t j = 0; j < sizeof(Word); ++j) { - // temp is last col of prev round key - temp[j] = (*previousRoundKey)[(keyLen - 4) + j]; - } - if (i % 4 == 0) { + if (i % kModCheckThreshold == 0) { temp = rotateWord(temp); temp = substituteWord(temp); // xor with rcon temp[0] ^= kRoundConstant[i / Nk]; - } else if (Nk > 6 && i % Nk == 4) { + } + if (Nk == 8 && i % Nk == 4) { + // See note for 256-bit keys on Sec. 5.2 (Key Expansion) on FIPS.197 temp = substituteWord(temp); } - for (uint8_t j = 0; j < sizeof(Word); ++j) { - // xor with first column of previous round key - temp[j] ^= (*previousRoundKey)[j]; - newRoundKey[j] = temp[j]; - } // xor previous column of new key with corresponding column of // previous round key + Word correspondingWord = words[i - Nk]; + byte b0 = correspondingWord[0] ^ temp[0]; + byte b1 = correspondingWord[1] ^ temp[1]; + byte b2 = correspondingWord[2] ^ temp[2]; + byte b3 = correspondingWord[3] ^ temp[3]; - for (int col = 1; col < sizeof(Word); ++col) { - int c = col * sizeof(Word); - for (int j = c; j < c + sizeof(Word); ++j) { - newRoundKey[j] = newRoundKey[j - 4] ^ (*previousRoundKey)[j]; - } - } - - roundKeys.push_back(newRoundKey); - linearWords.push_back(temp); - //addRoundKeyToLinearArr(newRoundKey); - + words[i] = {{ b0, b1, b2, b3 }}; } - std::cout << "f"; - - - + return words; } -void AES::cipher(byte output[], byte input[], std::size_t len, byte key[], std::size_t keySize) +void AES::cipher(byte output[], byte input[], std::size_t len, const Key* key) { // FIPS.197 p.14 CipherState state[4][4]; @@ -211,31 +188,31 @@ void AES::cipher(byte output[], byte input[], std::size_t len, byte key[], std:: std::memcpy(output, input, len); uint8_t keyExSize, Nk, Nr; - getKeyParams(keySize, &keyExSize, &Nk, &Nr); + getKeyParams(key->size(), &keyExSize, &Nk, &Nr); - RoundKeys roundKeys = keyExpansion(key, keySize); + RoundKeys roundKeys = keyExpansion(key); } void AES::getKeyParams(std::size_t keySize, uint8_t *keyExSize, uint8_t *Nk, uint8_t *Nr) { - switch (keySize) { - case 128: - *keyExSize = 176; - *Nk = 4; - *Nr = 10; - break; - case 192: - *keyExSize = 208; - *Nk = 6; - *Nr = 12; - break; - case 256: - *keyExSize = 240; - *Nk = 8; - *Nr = 14; - break; - default: - throw std::invalid_argument("Invalid AES key size"); - } - }; + switch (keySize) { + case 16: + *keyExSize = 176; + *Nk = 4; + *Nr = 10; + break; + case 24: + *keyExSize = 208; + *Nk = 6; + *Nr = 12; + break; + case 32: + *keyExSize = 240; + *Nk = 8; + *Nr = 14; + break; + default: + throw std::invalid_argument("Invalid AES key size"); + } +}; diff --git a/src/aes.h b/src/aes.h index 380d376..0a6ea9b 100644 --- a/src/aes.h +++ b/src/aes.h @@ -23,23 +23,24 @@ #include #include -#include +#include namespace mine { using byte = unsigned char; -using Word = std::array; -using Key = std::array; -using RoundKeys = std::vector; /// /// \brief Provides AES crypto functionalities /// class AES { public: + using Key = std::vector; static void transposeBytes(byte input[], std::size_t len); private: + using Word = std::array; + + using RoundKeys = std::unordered_map; /// /// \brief State as described in FIPS.197 Sec. 3.4 @@ -78,7 +79,7 @@ class AES { /// \param key Byte array of key /// \return cipher text (byte array) /// - static void cipher(byte output[], byte input[], std::size_t len, byte key[], std::size_t keySize = 128); + static void cipher(byte output[], byte input[], std::size_t len, const Key* key); static void getKeyParams(std::size_t keySize, uint8_t* keyExSize, uint8_t* Nk, uint8_t* Nr); @@ -87,7 +88,7 @@ class AES { /// \param output /// \param keySchedule /// - static RoundKeys keyExpansion(byte key[], std::size_t keySize); + static RoundKeys keyExpansion(const Key* key); /// /// \brief Prints bytes in hex format in 4x4 matrix fashion @@ -99,6 +100,7 @@ class AES { AES& operator=(const AES&) = delete; friend class AESTest_SimpleCipher_Test; + friend class AESTest_KeyExpansion_Test; }; } // end namespace mine diff --git a/test/aes-test.h b/test/aes-test.h index 79dbfae..c09d32a 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -19,23 +19,224 @@ static const byte kNistIV[16] = { 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; -static const byte kNistKey128[16] = { - 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, - 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c -}; +static const AES::Key kNistKey128 = {{ + 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c + }}; -static const byte kNistKey192[24] = { - 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, - 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, - 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b -}; +static const AES::Key kNistKey192 = {{ + 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, + 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, + 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b + }}; -static const byte kNistKey256[32] = { - 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, - 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, - 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, - 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 -}; +static const AES::Key kNistKey256 = {{ + 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, + 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, + 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, + 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 + }}; + +TEST(AESTest, KeyExpansion) +{ + // This key expansion is original key from FIPS.197 example + static TestData KeyExpansionTestData = { + TestCase("128-bit key expansion", AES::Key{{ + 0x2b, 0x7e, 0x15, 0x16, + 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, + 0x09, 0xcf, 0x4f, 0x3c + }}, AES::RoundKeys{{ + {0, {{ 0x2B, 0x7E, 0x15, 0x16 }}}, + {1, {{ 0x28, 0xAE, 0xD2, 0xA6 }}}, + {2, {{ 0xAB, 0xF7, 0x15, 0x88 }}}, + {3, {{ 0x9, 0xCF, 0x4F, 0x3C }}}, + {4, {{ 0xA0, 0xFA, 0xFE, 0x17 }}}, + {5, {{ 0x88, 0x54, 0x2C, 0xB1 }}}, + {6, {{ 0x23, 0xA3, 0x39, 0x39 }}}, + {7, {{ 0x2A, 0x6C, 0x76, 0x5 }}}, + {8, {{ 0xF2, 0xC2, 0x95, 0xF2 }}}, + {9, {{ 0x7A, 0x96, 0xB9, 0x43 }}}, + {10, {{ 0x59, 0x35, 0x80, 0x7A }}}, + {11, {{ 0x73, 0x59, 0xF6, 0x7F }}}, + {12, {{ 0x3D, 0x80, 0x47, 0x7D }}}, + {13, {{ 0x47, 0x16, 0xFE, 0x3E }}}, + {14, {{ 0x1E, 0x23, 0x7E, 0x44 }}}, + {15, {{ 0x6D, 0x7A, 0x88, 0x3B }}}, + {16, {{ 0xEF, 0x44, 0xA5, 0x41 }}}, + {17, {{ 0xA8, 0x52, 0x5B, 0x7F }}}, + {18, {{ 0xB6, 0x71, 0x25, 0x3B }}}, + {19, {{ 0xDB, 0xB, 0xAD, 0x00 }}}, + {20, {{ 0xD4, 0xD1, 0xC6, 0xF8 }}}, + {21, {{ 0x7C, 0x83, 0x9D, 0x87 }}}, + {22, {{ 0xCA, 0xF2, 0xB8, 0xBC }}}, + {23, {{ 0x11, 0xF9, 0x15, 0xBC }}}, + {24, {{ 0x6D, 0x88, 0xA3, 0x7A }}}, + {25, {{ 0x11, 0xB, 0x3E, 0xFD }}}, + {26, {{ 0xDB, 0xF9, 0x86, 0x41 }}}, + {27, {{ 0xCA, 0x00, 0x93, 0xFD }}}, + {28, {{ 0x4E, 0x54, 0xF7, 0xE }}}, + {29, {{ 0x5F, 0x5F, 0xC9, 0xF3 }}}, + {30, {{ 0x84, 0xA6, 0x4F, 0xB2 }}}, + {31, {{ 0x4E, 0xA6, 0xDC, 0x4F }}}, + {32, {{ 0xEA, 0xD2, 0x73, 0x21 }}}, + {33, {{ 0xB5, 0x8D, 0xBA, 0xD2 }}}, + {34, {{ 0x31, 0x2B, 0xF5, 0x60 }}}, + {35, {{ 0x7F, 0x8D, 0x29, 0x2F }}}, + {36, {{ 0xAC, 0x77, 0x66, 0xF3 }}}, + {37, {{ 0x19, 0xFA, 0xDC, 0x21 }}}, + {38, {{ 0x28, 0xD1, 0x29, 0x41 }}}, + {39, {{ 0x57, 0x5C, 0x00, 0x6E }}}, + {40, {{ 0xD0, 0x14, 0xF9, 0xA8 }}}, + {41, {{ 0xC9, 0xEE, 0x25, 0x89 }}}, + {42, {{ 0xE1, 0x3F, 0xC, 0xC8 }}}, + {43, {{ 0xB6, 0x63, 0xC, 0xA6 }}} + }}), + TestCase("192-bit key expansion", AES::Key{{ + 0x8e, 0x73, 0xb0, 0xf7, + 0xda, 0x0e, 0x64, 0x52, + 0xc8, 0x10, 0xf3, 0x2b, + 0x80, 0x90, 0x79, 0xe5, + 0x62, 0xf8, 0xea, 0xd2, + 0x52, 0x2c, 0x6b, 0x7b + }}, AES::RoundKeys{{ + {0, {{ 0x8E, 0x73, 0xB0, 0xF7 }}}, + {1, {{ 0xDA, 0xE, 0x64, 0x52 }}}, + {2, {{ 0xC8, 0x10, 0xF3, 0x2B }}}, + {3, {{ 0x80, 0x90, 0x79, 0xE5 }}}, + {4, {{ 0x62, 0xF8, 0xEA, 0xD2 }}}, + {5, {{ 0x52, 0x2C, 0x6B, 0x7B }}}, + {6, {{ 0xFE, 0xC, 0x91, 0xF7 }}}, + {7, {{ 0x24, 0x2, 0xF5, 0xA5 }}}, + {8, {{ 0xEC, 0x12, 0x6, 0x8E }}}, + {9, {{ 0x6C, 0x82, 0x7F, 0x6B }}}, + {10, {{ 0xE, 0x7A, 0x95, 0xB9 }}}, + {11, {{ 0x5C, 0x56, 0xFE, 0xC2 }}}, + {12, {{ 0x4D, 0xB7, 0xB4, 0xBD }}}, + {13, {{ 0x69, 0xB5, 0x41, 0x18 }}}, + {14, {{ 0x85, 0xA7, 0x47, 0x96 }}}, + {15, {{ 0xE9, 0x25, 0x38, 0xFD }}}, + {16, {{ 0xE7, 0x5F, 0xAD, 0x44 }}}, + {17, {{ 0xBB, 0x9, 0x53, 0x86 }}}, + {18, {{ 0x48, 0x5A, 0xF0, 0x57 }}}, + {19, {{ 0x21, 0xEF, 0xB1, 0x4F }}}, + {20, {{ 0xA4, 0x48, 0xF6, 0xD9 }}}, + {21, {{ 0x4D, 0x6D, 0xCE, 0x24 }}}, + {22, {{ 0xAA, 0x32, 0x63, 0x60 }}}, + {23, {{ 0x11, 0x3B, 0x30, 0xE6 }}}, + {24, {{ 0xA2, 0x5E, 0x7E, 0xD5 }}}, + {25, {{ 0x83, 0xB1, 0xCF, 0x9A }}}, + {26, {{ 0x27, 0xF9, 0x39, 0x43 }}}, + {27, {{ 0x6A, 0x94, 0xF7, 0x67 }}}, + {28, {{ 0xC0, 0xA6, 0x94, 0x7 }}}, + {29, {{ 0xD1, 0x9D, 0xA4, 0xE1 }}}, + {30, {{ 0xEC, 0x17, 0x86, 0xEB }}}, + {31, {{ 0x6F, 0xA6, 0x49, 0x71 }}}, + {32, {{ 0x48, 0x5F, 0x70, 0x32 }}}, + {33, {{ 0x22, 0xCB, 0x87, 0x55 }}}, + {34, {{ 0xE2, 0x6D, 0x13, 0x52 }}}, + {35, {{ 0x33, 0xF0, 0xB7, 0xB3 }}}, + {36, {{ 0x40, 0xBE, 0xEB, 0x28 }}}, + {37, {{ 0x2F, 0x18, 0xA2, 0x59 }}}, + {38, {{ 0x67, 0x47, 0xD2, 0x6B }}}, + {39, {{ 0x45, 0x8C, 0x55, 0x3E }}}, + {40, {{ 0xA7, 0xE1, 0x46, 0x6C }}}, + {41, {{ 0x94, 0x11, 0xF1, 0xDF }}}, + {42, {{ 0x82, 0x1F, 0x75, 0xA }}}, + {43, {{ 0xAD, 0x7, 0xD7, 0x53 }}}, + {44, {{ 0xCA, 0x40, 0x5, 0x38 }}}, + {45, {{ 0x8F, 0xCC, 0x50, 0x6 }}}, + {46, {{ 0x28, 0x2D, 0x16, 0x6A }}}, + {47, {{ 0xBC, 0x3C, 0xE7, 0xB5 }}}, + {48, {{ 0xE9, 0x8B, 0xA0, 0x6F }}}, + {49, {{ 0x44, 0x8C, 0x77, 0x3C }}}, + {50, {{ 0x8E, 0xCC, 0x72, 0x4 }}}, + {51, {{ 0x1, 0x0, 0x22, 0x2 }}}, + + }}), + + TestCase("256-bit key expansion", AES::Key{{ + 0x60, 0x3d, 0xeb, 0x10, + 0x15, 0xca, 0x71, 0xbe, + 0x2b, 0x73, 0xae, 0xf0, + 0x85, 0x7d, 0x77, 0x81, + 0x1f, 0x35, 0x2c, 0x07, + 0x3b, 0x61, 0x08, 0xd7, + 0x2d, 0x98, 0x10, 0xa3, + 0x09, 0x14, 0xdf, 0xf4 + }}, AES::RoundKeys{{ + {0, {{ 0x60, 0x3D, 0xEB, 0x10 }}}, + {1, {{ 0x15, 0xCA, 0x71, 0xBE }}}, + {2, {{ 0x2B, 0x73, 0xAE, 0xF0 }}}, + {3, {{ 0x85, 0x7D, 0x77, 0x81 }}}, + {4, {{ 0x1F, 0x35, 0x2C, 0x7 }}}, + {5, {{ 0x3B, 0x61, 0x8, 0xD7 }}}, + {6, {{ 0x2D, 0x98, 0x10, 0xA3 }}}, + {7, {{ 0x9, 0x14, 0xDF, 0xF4 }}}, + {8, {{ 0x9B, 0xA3, 0x54, 0x11 }}}, + {9, {{ 0x8E, 0x69, 0x25, 0xAF }}}, + {10, {{ 0xA5, 0x1A, 0x8B, 0x5F }}}, + {11, {{ 0x20, 0x67, 0xFC, 0xDE }}}, + {12, {{ 0xA8, 0xB0, 0x9C, 0x1A }}}, + {13, {{ 0x93, 0xD1, 0x94, 0xCD }}}, + {14, {{ 0xBE, 0x49, 0x84, 0x6E }}}, + {15, {{ 0xB7, 0x5D, 0x5B, 0x9A }}}, + {16, {{ 0xD5, 0x9A, 0xEC, 0xB8 }}}, + {17, {{ 0x5B, 0xF3, 0xC9, 0x17 }}}, + {18, {{ 0xFE, 0xE9, 0x42, 0x48 }}}, + {19, {{ 0xDE, 0x8E, 0xBE, 0x96 }}}, + {20, {{ 0xB5, 0xA9, 0x32, 0x8A }}}, + {21, {{ 0x26, 0x78, 0xA6, 0x47 }}}, + {22, {{ 0x98, 0x31, 0x22, 0x29 }}}, + {23, {{ 0x2F, 0x6C, 0x79, 0xB3 }}}, + {24, {{ 0x81, 0x2C, 0x81, 0xAD }}}, + {25, {{ 0xDA, 0xDF, 0x48, 0xBA }}}, + {26, {{ 0x24, 0x36, 0xA, 0xF2 }}}, + {27, {{ 0xFA, 0xB8, 0xB4, 0x64 }}}, + {28, {{ 0x98, 0xC5, 0xBF, 0xC9 }}}, + {29, {{ 0xBE, 0xBD, 0x19, 0x8E }}}, + {30, {{ 0x26, 0x8C, 0x3B, 0xA7 }}}, + {31, {{ 0x9, 0xE0, 0x42, 0x14 }}}, + {32, {{ 0x68, 0x0, 0x7B, 0xAC }}}, + {33, {{ 0xB2, 0xDF, 0x33, 0x16 }}}, + {34, {{ 0x96, 0xE9, 0x39, 0xE4 }}}, + {35, {{ 0x6C, 0x51, 0x8D, 0x80 }}}, + {36, {{ 0xC8, 0x14, 0xE2, 0x4 }}}, + {37, {{ 0x76, 0xA9, 0xFB, 0x8A }}}, + {38, {{ 0x50, 0x25, 0xC0, 0x2D }}}, + {39, {{ 0x59, 0xC5, 0x82, 0x39 }}}, + {40, {{ 0xDE, 0x13, 0x69, 0x67 }}}, + {41, {{ 0x6C, 0xCC, 0x5A, 0x71 }}}, + {42, {{ 0xFA, 0x25, 0x63, 0x95 }}}, + {43, {{ 0x96, 0x74, 0xEE, 0x15 }}}, + {44, {{ 0x58, 0x86, 0xCA, 0x5D }}}, + {45, {{ 0x2E, 0x2F, 0x31, 0xD7 }}}, + {46, {{ 0x7E, 0xA, 0xF1, 0xFA }}}, + {47, {{ 0x27, 0xCF, 0x73, 0xC3 }}}, + {48, {{ 0x74, 0x9C, 0x47, 0xAB }}}, + {49, {{ 0x18, 0x50, 0x1D, 0xDA }}}, + {50, {{ 0xE2, 0x75, 0x7E, 0x4F }}}, + {51, {{ 0x74, 0x1, 0x90, 0x5A }}}, + {52, {{ 0xCA, 0xFA, 0xAA, 0xE3 }}}, + {53, {{ 0xE4, 0xD5, 0x9B, 0x34 }}}, + {54, {{ 0x9A, 0xDF, 0x6A, 0xCE }}}, + {55, {{ 0xBD, 0x10, 0x19, 0xD }}}, + {56, {{ 0xFE, 0x48, 0x90, 0xD1 }}}, + {57, {{ 0xE6, 0x18, 0x8D, 0xB }}}, + {58, {{ 0x4, 0x6D, 0xF3, 0x44 }}}, + {59, {{ 0x70, 0x6C, 0x63, 0x1E }}}, + }}), + }; + + for (auto& item : KeyExpansionTestData) { + LOG(INFO) << "Test: " << PARAM(0); + AES::RoundKeys keys = AES::keyExpansion(&PARAM(1)); + AES::RoundKeys expected = PARAM(2); + ASSERT_EQ(expected, keys); + } + + +} // FIPS. 197 p. 33 // this is not real test @@ -50,12 +251,12 @@ TEST(AESTest, SimpleCipher) 0xe0, 0x37, 0x07, 0x34 }; - byte key[16] = { - 0x2b, 0x7e, 0x15, 0x16, - 0x28, 0xae, 0xd2, 0xa6, - 0xab, 0xf7, 0x15, 0x88, - 0x09, 0xcf, 0x4f, 0x3c - }; + AES::Key key = {{ + 0x2b, 0x7e, 0x15, 0x16, + 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, + 0x09, 0xcf, 0x4f, 0x3c + }}; byte key2[16] = { 0x53, 0x4F, 0x4D, 0x45, @@ -71,7 +272,7 @@ TEST(AESTest, SimpleCipher) AES::printBytes(key2, 16); - AES::cipher(output, input, 16, key); + AES::cipher(output, input, 16, &key); } From fdddea0b5cc0c7b98c88507a0b0090eefbb24cdb Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 27 Aug 2017 00:21:46 +1000 Subject: [PATCH 065/148] cipher steps --- src/aes.cc | 163 +++++++++++++++++++++++++++++++++--------------- src/aes.h | 50 +++++++++++---- test/aes-test.h | 49 ++++++--------- 3 files changed, 168 insertions(+), 94 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 33b1923..bc567c3 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -19,6 +19,7 @@ // http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf // +#include #include #include #include "src/base16.h" @@ -68,20 +69,9 @@ const uint8_t AES::kRoundConstant[11] = { 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; -void AES::transposeBytes(byte input[], std::size_t len) +void AES::printBytes(const ByteArray& b) { - std::size_t n = std::sqrt(len); - for (int i = 0; i < n; ++i) { - for (int j = i+1; j < n; ++j) { - std::swap(input[n*i + j], input[n*j + i]); - } - } -} - -void AES::printBytes(byte b[], std::size_t len) -{ - std::cout << "------" << std::endl; - for (int i = 1; i <= len; ++i) { + for (int i = 1; i <= b.size(); ++i) { std::cout << "0x" << (b[i - 1] < 10 ? "0" : "") << Base16::encode(b[i - 1]) << " "; if (i % 4 == 0) { std::cout << std::endl; @@ -90,7 +80,7 @@ void AES::printBytes(byte b[], std::size_t len) std::cout << std::endl << "------" << std::endl; } -AES::RoundKeys AES::keyExpansion(const Key* key) +AES::KeySchedule AES::keyExpansion(const Key* key) { // rotateWord function is specified in FIPS.197 Sec. 5.2: @@ -108,12 +98,12 @@ AES::RoundKeys AES::keyExpansion(const Key* key) // [a3] => [a4] // [a4] [a1] // - auto rotateWord = [](Word& w) -> Word { - byte t = w[0]; - w[0] = w[1]; - w[1] = w[2]; - w[2] = w[3]; - w[3] = t; + auto rotateWord = [](Word* w) -> Word* { + byte t = w->at(0); + w->at(0) = w->at(1); + w->at(1) = w->at(2); + w->at(2) = w->at(3); + w->at(3) = t; return w; }; @@ -126,17 +116,33 @@ AES::RoundKeys AES::keyExpansion(const Key* key) // It's a simple substition with kSbox for corresponding bit // index // - auto substituteWord = [](Word& w) -> Word { + auto substituteWord = [](Word* w) -> Word* { for (uint8_t i = 0; i < 4; ++i) { - w[i] = kSBox[w[i]]; + w->at(i) = kSBox[w->at(i)]; } return w; }; - uint8_t keyExSize, Nk, Nr; - getKeyParams(key->size(), &keyExSize, &Nk, &Nr); + uint8_t Nk, Nr; - RoundKeys words(kNb * (Nr+1)); + switch (key->size()) { + case 16: + Nk = 4; + Nr = 10; + break; + case 24: + Nk = 6; + Nr = 12; + break; + case 32: + Nk = 8; + Nr = 14; + break; + default: + throw std::invalid_argument("Invalid AES key size"); + } + + KeySchedule words(kNb * (Nr+1)); int i = 0; // copy main key as is for the first round @@ -149,20 +155,18 @@ AES::RoundKeys AES::keyExpansion(const Key* key) }}; } - const int kModCheckThreshold = i; - for (; i < kNb * (Nr + 1); ++i) { Word temp = words[i - 1]; - if (i % kModCheckThreshold == 0) { - temp = rotateWord(temp); - temp = substituteWord(temp); + if (i % Nk == 0) { + rotateWord(&temp); + substituteWord(&temp); // xor with rcon temp[0] ^= kRoundConstant[i / Nk]; } if (Nk == 8 && i % Nk == 4) { // See note for 256-bit keys on Sec. 5.2 (Key Expansion) on FIPS.197 - temp = substituteWord(temp); + substituteWord(&temp); } @@ -180,39 +184,98 @@ AES::RoundKeys AES::keyExpansion(const Key* key) return words; } -void AES::cipher(byte output[], byte input[], std::size_t len, const Key* key) +void AES::addRoundKey(State* state, KeySchedule* keySchedule, int round) { - // FIPS.197 p.14 - CipherState state[4][4]; + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + state->at(i)[j] ^= keySchedule->at(i)[j]; + } + } +} - std::memcpy(output, input, len); - uint8_t keyExSize, Nk, Nr; +void AES::subBytes(State* state) +{ + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + state->at(i)[j] = kSBox[state->at(i)[j]]; + } + } +} - getKeyParams(key->size(), &keyExSize, &Nk, &Nr); +void AES::shiftRows(State *state) +{ + int rowIdx = 1; + std::swap(state->at(0)[rowIdx], state->at(3)[rowIdx]); + std::swap(state->at(0)[rowIdx], state->at(1)[rowIdx]); + std::swap(state->at(1)[rowIdx], state->at(2)[rowIdx]); + + rowIdx = 2; + std::swap(state->at(0)[rowIdx], state->at(2)[rowIdx]); + std::swap(state->at(1)[rowIdx], state->at(3)[rowIdx]); + + rowIdx = 3; + std::swap(state->at(0)[rowIdx], state->at(1)[rowIdx]); + std::swap(state->at(2)[rowIdx], state->at(3)[rowIdx]); + std::swap(state->at(0)[rowIdx], state->at(2)[rowIdx]); +} - RoundKeys roundKeys = keyExpansion(key); +void AES::mixColumns(State* state) +{ } -void AES::getKeyParams(std::size_t keySize, uint8_t *keyExSize, uint8_t *Nk, uint8_t *Nr) +AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) { - switch (keySize) { + State state; + ByteArray result; + + std::copy_n(input.begin(), kBlockSize, std::back_inserter(result)); + + // add padding if needed + if (result.size() < kBlockSize) { + std::fill_n(result.end(), kBlockSize - result.size(), 0); + } + + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + state[i][j] = result[(kNb * i) + j]; + } + } + + uint8_t kTotalRounds; + switch (key->size()) { case 16: - *keyExSize = 176; - *Nk = 4; - *Nr = 10; + kTotalRounds = 10; break; case 24: - *keyExSize = 208; - *Nk = 6; - *Nr = 12; + kTotalRounds = 12; break; case 32: - *keyExSize = 240; - *Nk = 8; - *Nr = 14; + kTotalRounds = 14; break; default: throw std::invalid_argument("Invalid AES key size"); } -}; + + KeySchedule keySchedule = keyExpansion(key); + + int round = 0; + + addRoundKey(&state, &keySchedule, round++); + + while (round <= kTotalRounds) { + subBytes(&state); + shiftRows(&state); + if (round < kTotalRounds) { + // don't mix column for last round + // it only adds overhead and no + // extra security + // see Sec. 5.1 (p.14) + mixColumns(&state); + } + addRoundKey(&state, &keySchedule, round++); + } + + return result; + +} diff --git a/src/aes.h b/src/aes.h index 0a6ea9b..2705d64 100644 --- a/src/aes.h +++ b/src/aes.h @@ -34,19 +34,23 @@ using byte = unsigned char; /// class AES { public: - using Key = std::vector; + using ByteArray = std::vector; + using Key = ByteArray; static void transposeBytes(byte input[], std::size_t len); private: using Word = std::array; - using RoundKeys = std::unordered_map; + /// + /// \brief KeySchedule is linear array of 4-byte words + /// \ref FIPS.197 Sec 5.2 + /// + using KeySchedule = std::unordered_map; /// /// \brief State as described in FIPS.197 Sec. 3.4 - /// \see kNb /// - using CipherState = byte[4][4 /* Nb */]; + using State = std::array, 4>; /// /// \brief AES works on 16 bit block at a time @@ -73,27 +77,47 @@ class AES { static const uint8_t kNb = 4; /// - /// \brief Raw encryption function - /// \param output Byte array for desitnation - /// \param input Byte array of input + /// \brief Raw encryption function - not for public use + /// \param input 128-bit Byte array of input. + /// If array is bigger it's chopped and if it's smaller, it's padded + /// please use alternative functions if your array is bigger. Those + /// function will handle all the bytes correctly. /// \param key Byte array of key /// \return cipher text (byte array) /// - static void cipher(byte output[], byte input[], std::size_t len, const Key* key); + static ByteArray cipher(const ByteArray& input, const Key* key); static void getKeyParams(std::size_t keySize, uint8_t* keyExSize, uint8_t* Nk, uint8_t* Nr); /// - /// \brief generateRoundKeys - /// \param output - /// \param keySchedule + /// \brief Key expansion function as described in FIPS.197 + /// + static KeySchedule keyExpansion(const Key* key); + + /// + /// \brief Adds round to the state using specified key schedule + /// + static void addRoundKey(State* state, KeySchedule* keySchedule, int round); + + /// + /// \brief Substitution step for state (Sec. 5.1.1) + /// + static void subBytes(State* state); + + /// + /// \brief Shifting rows step for the state (Sec. 5.1.2) + /// + static void shiftRows(State* state); + + /// + /// \brief Mixing columns for the state (Sec. 5.1.3) /// - static RoundKeys keyExpansion(const Key* key); + static void mixColumns(State* state); /// /// \brief Prints bytes in hex format in 4x4 matrix fashion /// - static void printBytes(byte b[], std::size_t len); + static void printBytes(const ByteArray& b); AES() = delete; AES(const AES&) = delete; diff --git a/test/aes-test.h b/test/aes-test.h index c09d32a..0e81d22 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -14,10 +14,10 @@ namespace mine { // from http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf -static const byte kNistIV[16] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f -}; +static const AES::ByteArray kNistIV = {{ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + }}; static const AES::Key kNistKey128 = {{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, @@ -40,13 +40,13 @@ static const AES::Key kNistKey256 = {{ TEST(AESTest, KeyExpansion) { // This key expansion is original key from FIPS.197 example - static TestData KeyExpansionTestData = { + static TestData KeyExpansionTestData = { TestCase("128-bit key expansion", AES::Key{{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c - }}, AES::RoundKeys{{ + }}, AES::KeySchedule{{ {0, {{ 0x2B, 0x7E, 0x15, 0x16 }}}, {1, {{ 0x28, 0xAE, 0xD2, 0xA6 }}}, {2, {{ 0xAB, 0xF7, 0x15, 0x88 }}}, @@ -99,7 +99,7 @@ TEST(AESTest, KeyExpansion) 0x80, 0x90, 0x79, 0xe5, 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b - }}, AES::RoundKeys{{ + }}, AES::KeySchedule{{ {0, {{ 0x8E, 0x73, 0xB0, 0xF7 }}}, {1, {{ 0xDA, 0xE, 0x64, 0x52 }}}, {2, {{ 0xC8, 0x10, 0xF3, 0x2B }}}, @@ -164,7 +164,7 @@ TEST(AESTest, KeyExpansion) 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 - }}, AES::RoundKeys{{ + }}, AES::KeySchedule{{ {0, {{ 0x60, 0x3D, 0xEB, 0x10 }}}, {1, {{ 0x15, 0xCA, 0x71, 0xBE }}}, {2, {{ 0x2B, 0x73, 0xAE, 0xF0 }}}, @@ -230,8 +230,8 @@ TEST(AESTest, KeyExpansion) for (auto& item : KeyExpansionTestData) { LOG(INFO) << "Test: " << PARAM(0); - AES::RoundKeys keys = AES::keyExpansion(&PARAM(1)); - AES::RoundKeys expected = PARAM(2); + AES::KeySchedule keys = AES::keyExpansion(&PARAM(1)); + AES::KeySchedule expected = PARAM(2); ASSERT_EQ(expected, keys); } @@ -244,12 +244,12 @@ TEST(AESTest, KeyExpansion) TEST(AESTest, SimpleCipher) { - byte input[16] = { - 0x32, 0x43, 0xf6, 0xa8, - 0x88, 0x5a, 0x30, 0x8d, - 0x31, 0x31, 0x98, 0xa2, - 0xe0, 0x37, 0x07, 0x34 - }; + AES::ByteArray input = {{ + 0x32, 0x43, 0xf6, 0xa8, + 0x88, 0x5a, 0x30, 0x8d, + 0x31, 0x31, 0x98, 0xa2, + 0xe0, 0x37, 0x07, 0x34 + }}; AES::Key key = {{ 0x2b, 0x7e, 0x15, 0x16, @@ -258,22 +258,9 @@ TEST(AESTest, SimpleCipher) 0x09, 0xcf, 0x4f, 0x3c }}; - byte key2[16] = { - 0x53, 0x4F, 0x4D, 0x45, - 0x20, 0x31, 0x32, 0x38, - 0x20, 0x42, 0x49, 0x54, - 0x20, 0x4B, 0x45, 0x59, - }; - - byte output[16]; - - AES::transposeBytes(input, 16); - //AES::transposeBytes(key, 16); - - AES::printBytes(key2, 16); - - AES::cipher(output, input, 16, &key); + AES::ByteArray output = AES::cipher(input, &key); + AES::printBytes(output); } } From f21bac336fc25d9b86c5e8342729af74f4ca995a Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 27 Aug 2017 13:48:59 +1000 Subject: [PATCH 066/148] Minor update --- src/aes.cc | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index bc567c3..40e1002 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include "src/base16.h" #include "src/aes.h" @@ -222,6 +224,17 @@ void AES::shiftRows(State *state) void AES::mixColumns(State* state) { + // revisit! + // need + auto multiplyColumn = [&](int col) { + Word column = state->at(col); + }; + + multiplyColumn(0); + multiplyColumn(1); + multiplyColumn(2); + multiplyColumn(3); + std::cout << std::endl; } AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) @@ -261,21 +274,22 @@ AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) int round = 0; + // initial round addRoundKey(&state, &keySchedule, round++); - while (round <= kTotalRounds) { + // intermediate round + while (round < kTotalRounds) { subBytes(&state); shiftRows(&state); - if (round < kTotalRounds) { - // don't mix column for last round - // it only adds overhead and no - // extra security - // see Sec. 5.1 (p.14) - mixColumns(&state); - } + mixColumns(&state); addRoundKey(&state, &keySchedule, round++); } + // final round + subBytes(&state); + shiftRows(&state); + addRoundKey(&state, &keySchedule, round++); + return result; } From 99e9fcfb02b579f4926f630c6e1c7371662fbc84 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 27 Aug 2017 14:08:11 +1000 Subject: [PATCH 067/148] pol prod for aes --- src/aes.cc | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 40e1002..e6f914d 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -223,11 +223,29 @@ void AES::shiftRows(State *state) void AES::mixColumns(State* state) { + auto polyProduct = [](byte a, byte b) -> byte { + // todo: implement this + return 0; + }; - // revisit! - // need auto multiplyColumn = [&](int col) { Word column = state->at(col); + state->at(col)[0] = (polyProduct(column[0], 2)) ^ + (polyProduct(column[1], 3)) ^ + (column[2]) ^ + (column[3]); + state->at(col)[1] = (column[0]) ^ + (polyProduct(column[1], 2)) ^ + (column[2] * 3) ^ + (column[3]); + state->at(col)[2] = (column[0]) ^ + (column[1]) ^ + (polyProduct(column[2], 2)) ^ + (column[3] * 3); + state->at(col)[3] = (polyProduct(column[0], 3)) ^ + (column[1]) ^ + (column[2]) ^ + (polyProduct(column[3], 2)); }; multiplyColumn(0); From 588e2f3ffc8580b389f44a6724fbda3614fb4efd Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 27 Aug 2017 15:40:16 +1000 Subject: [PATCH 068/148] full cycle updates --- src/aes.cc | 40 +++++++++++++++++++++++++++++++--------- test/aes-test.h | 10 ++++++++-- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index e6f914d..1dedae7 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -223,36 +223,58 @@ void AES::shiftRows(State *state) void AES::mixColumns(State* state) { + // taken from Finite Field article auto polyProduct = [](byte a, byte b) -> byte { - // todo: implement this - return 0; + byte result = 0x0; + while (a && b) { + if (b & 0x01) { + result ^= a; + } + if (a & 0x80) { + a = (a << 1) ^ 0x11b; + } else { + result <<= 1; + } + b >>= 1; + } + return result; }; auto multiplyColumn = [&](int col) { Word column = state->at(col); - state->at(col)[0] = (polyProduct(column[0], 2)) ^ + state->at(col)[0] = + (polyProduct(column[0], 2)) ^ (polyProduct(column[1], 3)) ^ (column[2]) ^ (column[3]); - state->at(col)[1] = (column[0]) ^ + state->at(col)[1] = + (column[0]) ^ (polyProduct(column[1], 2)) ^ - (column[2] * 3) ^ + (polyProduct(column[2], 3)) ^ (column[3]); - state->at(col)[2] = (column[0]) ^ + state->at(col)[2] = + (column[0]) ^ (column[1]) ^ (polyProduct(column[2], 2)) ^ - (column[3] * 3); - state->at(col)[3] = (polyProduct(column[0], 3)) ^ + (polyProduct(column[3], 3)); + state->at(col)[3] = + (polyProduct(column[0], 3)) ^ (column[1]) ^ (column[2]) ^ (polyProduct(column[3], 2)); }; + /* + expected +04 e0 48 28 +66 cb f8 06 +81 19 d3 26 +e5 9a 7a 4c +*/ multiplyColumn(0); multiplyColumn(1); multiplyColumn(2); multiplyColumn(3); - std::cout << std::endl; } AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) diff --git a/test/aes-test.h b/test/aes-test.h index 0e81d22..a8d8e20 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -258,9 +258,15 @@ TEST(AESTest, SimpleCipher) 0x09, 0xcf, 0x4f, 0x3c }}; - AES::ByteArray output = AES::cipher(input, &key); + AES::ByteArray expected = {{ + 0x39, 0x25, 0x84, 0x1d, + 0x02, 0xdc, 0x09, 0xfb, + 0xdc, 0x11, 0x85, 0x97, + 0x19, 0x6a, 0x0b, 0x32 + }}; - AES::printBytes(output); + AES::ByteArray output = AES::cipher(input, &key); + ASSERT_EQ(expected, output); } } From 931632b5a7b9b5957d489d93cebea44f34a16269 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 27 Aug 2017 19:09:55 +1000 Subject: [PATCH 069/148] Updates only require gfmult --- src/aes.cc | 84 ++++++++++++++++++++++++++++--------------------- src/aes.h | 16 ++++++++-- test/aes-test.h | 61 ++++++++++++++++++++++++++++++++--- 3 files changed, 120 insertions(+), 41 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 1dedae7..a5cdb47 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -82,6 +82,18 @@ void AES::printBytes(const ByteArray& b) std::cout << std::endl << "------" << std::endl; } +void AES::printState(const State* state) +{ + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + byte b = state->at(j)[i]; + std::cout << "0x" << (b < 10 ? "0" : "") << Base16::encode(b) << " "; + } + std::cout << std::endl; + } + std::cout << std::endl; +} + AES::KeySchedule AES::keyExpansion(const Key* key) { @@ -165,8 +177,7 @@ AES::KeySchedule AES::keyExpansion(const Key* key) substituteWord(&temp); // xor with rcon temp[0] ^= kRoundConstant[i / Nk]; - } - if (Nk == 8 && i % Nk == 4) { + } else if (Nk == 8 && i % Nk == 4) { // See note for 256-bit keys on Sec. 5.2 (Key Expansion) on FIPS.197 substituteWord(&temp); } @@ -186,11 +197,12 @@ AES::KeySchedule AES::keyExpansion(const Key* key) return words; } -void AES::addRoundKey(State* state, KeySchedule* keySchedule, int round) +void AES::addRoundKey(State* state, const KeySchedule* keySchedule, int round) { for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - state->at(i)[j] ^= keySchedule->at(i)[j]; + int keyIdx = (round * kNb) + i; + state->at(i)[j] ^= keySchedule->at(keyIdx)[j]; } } } @@ -223,65 +235,60 @@ void AES::shiftRows(State *state) void AES::mixColumns(State* state) { - // taken from Finite Field article - auto polyProduct = [](byte a, byte b) -> byte { - byte result = 0x0; - while (a && b) { - if (b & 0x01) { - result ^= a; - } - if (a & 0x80) { - a = (a << 1) ^ 0x11b; - } else { - result <<= 1; - } - b >>= 1; - } - return result; - }; auto multiplyColumn = [&](int col) { Word column = state->at(col); state->at(col)[0] = - (polyProduct(column[0], 2)) ^ - (polyProduct(column[1], 3)) ^ + (finiteFieldMultiply(column[0], 2)) ^ + (finiteFieldMultiply(column[1], 3)) ^ (column[2]) ^ (column[3]); state->at(col)[1] = (column[0]) ^ - (polyProduct(column[1], 2)) ^ - (polyProduct(column[2], 3)) ^ + (finiteFieldMultiply(column[1], 2)) ^ + (finiteFieldMultiply(column[2], 3)) ^ (column[3]); state->at(col)[2] = (column[0]) ^ (column[1]) ^ - (polyProduct(column[2], 2)) ^ - (polyProduct(column[3], 3)); + (finiteFieldMultiply(column[2], 2)) ^ + (finiteFieldMultiply(column[3], 3)); state->at(col)[3] = - (polyProduct(column[0], 3)) ^ + (finiteFieldMultiply(column[0], 3)) ^ (column[1]) ^ (column[2]) ^ - (polyProduct(column[3], 2)); + (finiteFieldMultiply(column[3], 2)); }; - /* - expected -04 e0 48 28 -66 cb f8 06 -81 19 d3 26 -e5 9a 7a 4c -*/ multiplyColumn(0); multiplyColumn(1); multiplyColumn(2); multiplyColumn(3); } +byte AES::finiteFieldMultiply(byte a, byte b) +{ + byte result = 0x0; + while (a && b) { + if (b & 0x01) { + result ^= a; + } + if (a & 0x80) { + a = (a << 1) ^ 0x11b; + } else { + result <<= 1; + } + b >>= 1; + } + return result; +} + AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) { State state; ByteArray result; + // TODO: remove this extra overhead std::copy_n(input.begin(), kBlockSize, std::back_inserter(result)); // add padding if needed @@ -330,6 +337,13 @@ AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) shiftRows(&state); addRoundKey(&state, &keySchedule, round++); + int k = 0; + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + result[k++] = state.at(i)[j]; + } + } + return result; } diff --git a/src/aes.h b/src/aes.h index 2705d64..f74eb81 100644 --- a/src/aes.h +++ b/src/aes.h @@ -97,7 +97,7 @@ class AES { /// /// \brief Adds round to the state using specified key schedule /// - static void addRoundKey(State* state, KeySchedule* keySchedule, int round); + static void addRoundKey(State* state, const KeySchedule* keySchedule, int round); /// /// \brief Substitution step for state (Sec. 5.1.1) @@ -114,17 +114,29 @@ class AES { /// static void mixColumns(State* state); + /// + /// \brief Multiply two numbers in the GF(2^8) finite field defined + /// + static byte finiteFieldMultiply(byte a, byte b); + /// /// \brief Prints bytes in hex format in 4x4 matrix fashion /// static void printBytes(const ByteArray& b); + /// + /// \brief Prints state for debugging + /// + static void printState(const State*); + AES() = delete; AES(const AES&) = delete; AES& operator=(const AES&) = delete; - friend class AESTest_SimpleCipher_Test; + friend class AESTest_RawCipher_Test; + friend class AESTest_FiniteFieldMultiply_Test; friend class AESTest_KeyExpansion_Test; + friend class AESTest_AddRoundKey_Test; }; } // end namespace mine diff --git a/test/aes-test.h b/test/aes-test.h index a8d8e20..d8afb0d 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -234,16 +234,69 @@ TEST(AESTest, KeyExpansion) AES::KeySchedule expected = PARAM(2); ASSERT_EQ(expected, keys); } +} +TEST(AESTest, AddRoundKey) +{ + // Simple test with test data from FIPS.197 p.33 and 34 + // round 1 and round 6 are tested + AES::Key key = {{ + 0x2b, 0x7e, 0x15, 0x16, + 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, + 0x09, 0xcf, 0x4f, 0x3c + }}; + AES::KeySchedule schedule = AES::keyExpansion(&key); + AES::State state = {{ + {{ 0x04, 0x66, 0x81, 0xe5 }}, + {{ 0xe0, 0xcb, 0x19, 0x9a }}, + {{ 0x48, 0xf8, 0xd3, 0x7a }}, + {{ 0x28, 0x06, 0x26, 0x4c }}, + }}; + AES::State expected2 = {{ + {{ 0xa4, 0x9c, 0x7f, 0xf2 }}, + {{ 0x68, 0x9f, 0x35, 0x2b }}, + {{ 0x6b, 0x5b, 0xea, 0x43 }}, + {{ 0x02, 0x6a, 0x50, 0x49 }}, + }}; + AES::State expected7 = {{ + {{ 0x26, 0x0e, 0x2e, 0x17 }}, + {{ 0x3d, 0x41, 0xb7, 0x7d }}, + {{ 0xe8, 0x64, 0x72, 0xa9 }}, + {{ 0xfd, 0xd2, 0x8b, 0x25 }}, + }}; + AES::addRoundKey(&state, &schedule, 1); + ASSERT_EQ(expected2, state); + state = {{ + {{ 0x4b, 0x86, 0x8d, 0x6d }}, + {{ 0x2c, 0x4a, 0x89, 0x80 }}, + {{ 0x33, 0x9d, 0xf4, 0xe8 }}, + {{ 0x37, 0xd2, 0x18, 0xd8 }}, + }}; + AES::addRoundKey(&state, &schedule, 6); + ASSERT_EQ(expected7, state); +} + + +TEST(AESTest, FiniteFieldMultiply) +{ + static TestData FiniteFieldMultiplyData = { + TestCase(0xd4, 0x02, 0x04), + }; + + for (auto& item : FiniteFieldMultiplyData) { + byte a = PARAM(0); + byte b = PARAM(1); + byte expected = PARAM(2); + ASSERT_EQ(AES::finiteFieldMultiply(a, b), expected); + } } -// FIPS. 197 p. 33 -// this is not real test -// this is only to aid implementation -TEST(AESTest, SimpleCipher) +TEST(AESTest, RawCipher) { + // FIPS. 197 p. 33 AES::ByteArray input = {{ 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, From d79d46dfd7d77a5038e8ac7c8f90091a2ca729b3 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 27 Aug 2017 21:16:06 +1000 Subject: [PATCH 070/148] finalised enc step and test data --- src/aes.cc | 153 ++++++++++++++++++++---------------------------- src/aes.h | 39 ++++++++---- test/aes-test.h | 16 ----- test/main.cc | 15 ++--- 4 files changed, 97 insertions(+), 126 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index a5cdb47..f2d8f73 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -67,8 +67,14 @@ const byte AES::kSBoxInverse[256] = { 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; -const uint8_t AES::kRoundConstant[11] = { - 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 +const uint8_t AES::kRoundConstant[10] = { + 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 +}; + +const std::unordered_map> AES::kKeyParams = { + { 16, {{ 4, 10 }} }, + { 24, {{ 6, 12 }} }, + { 32, {{ 8, 14 }} } }; void AES::printBytes(const ByteArray& b) @@ -137,28 +143,18 @@ AES::KeySchedule AES::keyExpansion(const Key* key) return w; }; - uint8_t Nk, Nr; - - switch (key->size()) { - case 16: - Nk = 4; - Nr = 10; - break; - case 24: - Nk = 6; - Nr = 12; - break; - case 32: - Nk = 8; - Nr = 14; - break; - default: + std::size_t keySize = key->size(); + + if (keySize != 16 && keySize != 24 && keySize != 32) { throw std::invalid_argument("Invalid AES key size"); } + uint8_t Nk = kKeyParams.at(keySize)[0], + Nr = kKeyParams.at(keySize)[1]; + KeySchedule words(kNb * (Nr+1)); - int i = 0; + uint8_t i = 0; // copy main key as is for the first round for (; i < Nk; ++i) { words[i] = {{ @@ -176,9 +172,9 @@ AES::KeySchedule AES::keyExpansion(const Key* key) rotateWord(&temp); substituteWord(&temp); // xor with rcon - temp[0] ^= kRoundConstant[i / Nk]; + temp[0] ^= kRoundConstant[(i / Nk) - 1]; } else if (Nk == 8 && i % Nk == 4) { - // See note for 256-bit keys on Sec. 5.2 (Key Expansion) on FIPS.197 + // See note for 256-bit keys on Sec. 5.2 on FIPS.197 substituteWord(&temp); } @@ -201,8 +197,7 @@ void AES::addRoundKey(State* state, const KeySchedule* keySchedule, int round) { for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - int keyIdx = (round * kNb) + i; - state->at(i)[j] ^= keySchedule->at(keyIdx)[j]; + state->at(i)[j] ^= keySchedule->at((round * kNb) + i)[j]; } } } @@ -218,69 +213,51 @@ void AES::subBytes(State* state) void AES::shiftRows(State *state) { - int rowIdx = 1; - std::swap(state->at(0)[rowIdx], state->at(3)[rowIdx]); - std::swap(state->at(0)[rowIdx], state->at(1)[rowIdx]); - std::swap(state->at(1)[rowIdx], state->at(2)[rowIdx]); - - rowIdx = 2; - std::swap(state->at(0)[rowIdx], state->at(2)[rowIdx]); - std::swap(state->at(1)[rowIdx], state->at(3)[rowIdx]); - - rowIdx = 3; - std::swap(state->at(0)[rowIdx], state->at(1)[rowIdx]); - std::swap(state->at(2)[rowIdx], state->at(3)[rowIdx]); - std::swap(state->at(0)[rowIdx], state->at(2)[rowIdx]); + // row 1 + std::swap(state->at(0)[1], state->at(3)[1]); + std::swap(state->at(0)[1], state->at(1)[1]); + std::swap(state->at(1)[1], state->at(2)[1]); + + // row 2 + std::swap(state->at(0)[2], state->at(2)[2]); + std::swap(state->at(1)[2], state->at(3)[2]); + + // row 3 + std::swap(state->at(0)[3], state->at(1)[3]); + std::swap(state->at(2)[3], state->at(3)[3]); + std::swap(state->at(0)[3], state->at(2)[3]); } void AES::mixColumns(State* state) { - - auto multiplyColumn = [&](int col) { - Word column = state->at(col); - state->at(col)[0] = - (finiteFieldMultiply(column[0], 2)) ^ - (finiteFieldMultiply(column[1], 3)) ^ - (column[2]) ^ - (column[3]); - state->at(col)[1] = - (column[0]) ^ - (finiteFieldMultiply(column[1], 2)) ^ - (finiteFieldMultiply(column[2], 3)) ^ - (column[3]); - state->at(col)[2] = - (column[0]) ^ - (column[1]) ^ - (finiteFieldMultiply(column[2], 2)) ^ - (finiteFieldMultiply(column[3], 3)); - state->at(col)[3] = - (finiteFieldMultiply(column[0], 3)) ^ - (column[1]) ^ - (column[2]) ^ - (finiteFieldMultiply(column[3], 2)); + // Finds the product of {02} and the argument to xtime modulo {1b} + // mentioned in Sec. 4.2.1 of FIPS.197 + // taken from http://gauss.ececs.uc.edu/Courses/c653/extra/AES/xtime.cpp + auto xtime = [](byte x) { + return ((x << 1) ^ (((x >> 7) & 1) * 0x11b)); }; - multiplyColumn(0); - multiplyColumn(1); - multiplyColumn(2); - multiplyColumn(3); -} - -byte AES::finiteFieldMultiply(byte a, byte b) -{ - byte result = 0x0; - while (a && b) { - if (b & 0x01) { - result ^= a; - } - if (a & 0x80) { - a = (a << 1) ^ 0x11b; - } else { - result <<= 1; - } - b >>= 1; + for (int col = 0; col < 4; ++col) { + + // + // multiplies in GF(2^8) field selected column from state + // with constant matrix defined by publication + // + // [ 02 03 01 01 ] + // | 01 02 03 01 | + // | 01 01 02 03 | + // [ 03 01 01 02 ] + // + Word column = state->at(col); + // let's take example from publication, col: [212, 191, 93, 48] + // t == 6 + byte t = (column[0]) ^ (column[1]) ^ (column[2]) ^ (column[3]); + // see Sec. 4.2.1 and Sec. 5.1.3 for more details + state->at(col)[0] ^= xtime(column[0] ^ column[1]) ^ t; + state->at(col)[1] ^= xtime(column[1] ^ column[2]) ^ t; + state->at(col)[2] ^= xtime(column[2] ^ column[3]) ^ t; + state->at(col)[3] ^= xtime(column[3] ^ column[0]) ^ t; } - return result; } AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) @@ -302,21 +279,14 @@ AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) } } - uint8_t kTotalRounds; - switch (key->size()) { - case 16: - kTotalRounds = 10; - break; - case 24: - kTotalRounds = 12; - break; - case 32: - kTotalRounds = 14; - break; - default: + std::size_t keySize = key->size(); + + if (keySize != 16 && keySize != 24 && keySize != 32) { throw std::invalid_argument("Invalid AES key size"); } + uint8_t kTotalRounds = kKeyParams.at(keySize)[1]; + KeySchedule keySchedule = keyExpansion(key); int round = 0; @@ -337,6 +307,7 @@ AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) shiftRows(&state); addRoundKey(&state, &keySchedule, round++); + // assign state to result int k = 0; for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { diff --git a/src/aes.h b/src/aes.h index f74eb81..1e5658a 100644 --- a/src/aes.h +++ b/src/aes.h @@ -24,6 +24,7 @@ #include #include #include +#include "src/rsa.h" namespace mine { @@ -32,12 +33,21 @@ using byte = unsigned char; /// /// \brief Provides AES crypto functionalities /// +/// This is validated against NIST test data and all +/// the corresponding tests under test/ directory +/// are from NIST themselves. +/// +/// Please make sure to use public functions and do not +/// use private functions especially in production as +/// you may end up using them incorrectly. However +/// the source code for AES class is heavily commented for +/// verification on implementation. +/// class AES { public: using ByteArray = std::vector; using Key = ByteArray; - static void transposeBytes(byte input[], std::size_t len); private: using Word = std::array; @@ -45,7 +55,7 @@ class AES { /// \brief KeySchedule is linear array of 4-byte words /// \ref FIPS.197 Sec 5.2 /// - using KeySchedule = std::unordered_map; + using KeySchedule = std::unordered_map; /// /// \brief State as described in FIPS.197 Sec. 3.4 @@ -57,17 +67,29 @@ class AES { /// static const uint8_t kBlockSize = 16; + /// + /// \brief Defines the key params to it's size + /// + static const std::unordered_map> kKeyParams; + /// /// \brief As defined in FIPS. 197 Sec. 5.1.1 /// - static const byte kSBox[256]; + static const byte kSBox[]; /// /// \brief As defined in FIPS. 197 Sec. 5.3.2 /// - static const byte kSBoxInverse[256]; + static const byte kSBoxInverse[]; - static const byte kRoundConstant[11]; + /// + /// \brief Round constant is constant for each round + /// it contains 10 values each defined in + /// Appendix A of FIPS.197 in column Rcon[i/Nk] for + /// each key size, we add all of them in one array for + /// ease of access + /// + static const byte kRoundConstant[]; /// /// \brief Nb @@ -87,8 +109,6 @@ class AES { /// static ByteArray cipher(const ByteArray& input, const Key* key); - static void getKeyParams(std::size_t keySize, uint8_t* keyExSize, uint8_t* Nk, uint8_t* Nr); - /// /// \brief Key expansion function as described in FIPS.197 /// @@ -114,11 +134,6 @@ class AES { /// static void mixColumns(State* state); - /// - /// \brief Multiply two numbers in the GF(2^8) finite field defined - /// - static byte finiteFieldMultiply(byte a, byte b); - /// /// \brief Prints bytes in hex format in 4x4 matrix fashion /// diff --git a/test/aes-test.h b/test/aes-test.h index d8afb0d..fbac183 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -277,22 +277,6 @@ TEST(AESTest, AddRoundKey) ASSERT_EQ(expected7, state); } - -TEST(AESTest, FiniteFieldMultiply) -{ - static TestData FiniteFieldMultiplyData = { - TestCase(0xd4, 0x02, 0x04), - }; - - for (auto& item : FiniteFieldMultiplyData) { - byte a = PARAM(0); - byte b = PARAM(1); - byte expected = PARAM(2); - ASSERT_EQ(AES::finiteFieldMultiply(a, b), expected); - } - -} - TEST(AESTest, RawCipher) { diff --git a/test/main.cc b/test/main.cc index c2017d2..80b4f4e 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,20 +1,21 @@ #include "test.h" -//#include "base16-test.h" -//#include "base64-test.h" +#include "base16-test.h" +#include "base64-test.h" #include "aes-test.h" -//#include "zlib-test.h" -//#include "rsa-test.h" +#include "zlib-test.h" +#include "rsa-test.h" INITIALIZE_EASYLOGGINGPP +void disableLogs() { + el::Loggers::reconfigureAllLoggers(el::Level::Global, el::ConfigurationType::Enabled, "false"); +} + int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput); el::Loggers::addFlag(el::LoggingFlag::ImmediateFlush); - // for temp basis - //el::Loggers::reconfigureAllLoggers(el::Level::Global, el::ConfigurationType::Enabled, "false"); - return ::testing::UnitTest::GetInstance()->Run(); } From 1746a4e86775219b2ce5d6076471cee0a61eeda2 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 27 Aug 2017 21:17:04 +1000 Subject: [PATCH 071/148] rebuilt package --- package/mine.cc | 343 ++++++++++++++++++++++++++++++++++++++++++++---- package/mine.h | 121 ++++++++++++++++- 2 files changed, 437 insertions(+), 27 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index a858283..6e252e7 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -15,6 +15,11 @@ // #include #include +#include +#include +#include +#include +#include #include "mine.h" @@ -23,13 +28,11 @@ using namespace mine; const std::string Base16::kValidChars = "0123456789ABCDEF"; -const std::unordered_map Base16::kDecodeMap = { - {48, 0 }, {49, 1 }, {50, 2 }, {51, 3 }, - {52, 4 }, {53, 5 }, {54, 6 }, {55, 7 }, - {56, 8 }, {57, 9 }, {65, 10}, {66, 11}, - {67, 12}, {68, 13}, {69, 14}, {70, 15}, - {97, 10}, {98, 11}, {99, 12}, {100, 13}, - {101, 14}, {102, 15} +const std::unordered_map Base16::kDecodeMap = { + {0x30, 0x00}, {0x31, 0x01}, {0x32, 0x02}, {0x33, 0x03}, + {0x34, 0x04}, {0x35, 0x05}, {0x36, 0x06}, {0x37, 0x07}, + {0x38, 0x08}, {0x39, 0x09}, {0x41, 0x0A}, {0x42, 0x0B}, + {0x43, 0x0C}, {0x44, 0x0D}, {0x45, 0x0E}, {0x46, 0x0F} }; std::string Base16::encode(const std::string& raw) noexcept @@ -63,24 +66,24 @@ std::string Base16::decode(const std::string& enc) const std::string Base64::kValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; -const std::unordered_map Base64::kDecodeMap = { - {65, 0 }, {66, 1 }, {67, 2 }, {68, 3 }, - {69, 4 }, {70, 5 }, {71, 6 }, {72, 7 }, - {73, 8 }, {74, 9 }, {75, 10 }, {76, 11 }, - {77, 12 }, {78, 13 }, {79, 14 }, {80, 15 }, - {81, 16 }, {82, 17 }, {83, 18 }, {84, 19 }, - {85, 20 }, {86, 21 }, {87, 22 }, {88, 23 }, - {89, 24 }, {90, 25 }, {97, 26 }, {98, 27 }, - {99, 28 }, {100, 29}, {101, 30}, {102, 31}, - {103, 32}, {104, 33}, {105, 34}, {106, 35}, - {107, 36}, {108, 37}, {109, 38}, {110, 39}, - {111, 40}, {112, 41}, {113, 42}, {114, 43}, - {115, 44}, {116, 45}, {117, 46}, {118, 47}, - {119, 48}, {120, 49}, {121, 50}, {122, 51}, - {48, 52 }, {49, 53 }, {50, 54 }, {51, 55 }, - {52, 56 }, {53, 57 }, {54, 58 }, {55, 59 }, - {56, 60 }, {57, 61 }, {43, 62 }, {47, 63 }, - {61, 64 } +const std::unordered_map Base64::kDecodeMap = { + {0x41, 0x00}, {0x42, 0x01}, {0x43, 0x02}, {0x44, 0x03}, + {0x45, 0x04}, {0x46, 0x05}, {0x47, 0x06}, {0x48, 0x07}, + {0x49, 0x08}, {0x4A, 0x09}, {0x4B, 0x0A}, {0x4C, 0x0B}, + {0x4D, 0x0C}, {0x4E, 0x0D}, {0x4F, 0x0E}, {0x50, 0x0F}, + {0x51, 0x10}, {0x52, 0x11}, {0x53, 0x12}, {0x54, 0x13}, + {0x55, 0x14}, {0x56, 0x15}, {0x57, 0x16}, {0x58, 0x17}, + {0x59, 0x18}, {0x5A, 0x19}, {0x61, 0x1A}, {0x62, 0x1B}, + {0x63, 0x1C}, {0x64, 0x1D}, {0x65, 0x1E}, {0x66, 0x1F}, + {0x67, 0x20}, {0x68, 0x21}, {0x69, 0x22}, {0x6A, 0x23}, + {0x6B, 0x24}, {0x6C, 0x25}, {0x6D, 0x26}, {0x6E, 0x27}, + {0x6F, 0x28}, {0x70, 0x29}, {0x71, 0x2A}, {0x72, 0x2B}, + {0x73, 0x2C}, {0x74, 0x2D}, {0x75, 0x2E}, {0x76, 0x2F}, + {0x77, 0x30}, {0x78, 0x31}, {0x79, 0x32}, {0x7A, 0x33}, + {0x30, 0x34}, {0x31, 0x35}, {0x32, 0x36}, {0x33, 0x37}, + {0x34, 0x38}, {0x35, 0x39}, {0x36, 0x3A}, {0x37, 0x3B}, + {0x38, 0x3C}, {0x39, 0x3D}, {0x2B, 0x3E}, {0x2F, 0x3F}, + {0x3D, 0x40} }; std::size_t Base64::countChars(const std::string& str) noexcept @@ -210,4 +213,294 @@ std::string Base64::decode(const std::string& enc) } +const byte AES::kSBox[256] = { + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 +}; + +const byte AES::kSBoxInverse[256] = { + 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, + 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, + 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, + 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, + 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, + 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, + 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, + 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, + 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, + 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, + 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, + 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, + 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, + 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, + 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, + 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d +}; + +const uint8_t AES::kRoundConstant[10] = { + 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 +}; + +const std::unordered_map> AES::kKeyParams = { + { 16, {{ 4, 10 }} }, + { 24, {{ 6, 12 }} }, + { 32, {{ 8, 14 }} } +}; + +void AES::printBytes(const ByteArray& b) +{ + for (int i = 1; i <= b.size(); ++i) { + std::cout << "0x" << (b[i - 1] < 10 ? "0" : "") << Base16::encode(b[i - 1]) << " "; + if (i % 4 == 0) { + std::cout << std::endl; + } + } + std::cout << std::endl << "------" << std::endl; +} + +void AES::printState(const State* state) +{ + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + byte b = state->at(j)[i]; + std::cout << "0x" << (b < 10 ? "0" : "") << Base16::encode(b) << " "; + } + std::cout << std::endl; + } + std::cout << std::endl; +} + +AES::KeySchedule AES::keyExpansion(const Key* key) +{ + + // rotateWord function is specified in FIPS.197 Sec. 5.2: + // The function RotWord() takes a + // word [a0,a1,a2,a3] as input, performs a cyclic permutation, + // and returns the word [a1,a2,a3,a0]. The + // round constant word array + // + // Our definition: + // We swap the first byte + // to last one causing it to shift to the left + // i.e, + // [a1] [a2] + // [a2] [a3] + // [a3] => [a4] + // [a4] [a1] + // + auto rotateWord = [](Word* w) -> Word* { + byte t = w->at(0); + w->at(0) = w->at(1); + w->at(1) = w->at(2); + w->at(2) = w->at(3); + w->at(3) = t; + return w; + }; + + // this function is also specified in FIPS.197 Sec. 5.2: + // SubWord() is a function that takes a four-byte + // input word and applies the S-box + // to each of the four bytes to produce an output word. + // + // Out definition: + // It's a simple substition with kSbox for corresponding bit + // index + // + auto substituteWord = [](Word* w) -> Word* { + for (uint8_t i = 0; i < 4; ++i) { + w->at(i) = kSBox[w->at(i)]; + } + return w; + }; + + std::size_t keySize = key->size(); + + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + uint8_t Nk = kKeyParams.at(keySize)[0], + Nr = kKeyParams.at(keySize)[1]; + + KeySchedule words(kNb * (Nr+1)); + + uint8_t i = 0; + // copy main key as is for the first round + for (; i < Nk; ++i) { + words[i] = {{ + (*key)[(i * 4) + 0], + (*key)[(i * 4) + 1], + (*key)[(i * 4) + 2], + (*key)[(i * 4) + 3], + }}; + } + + for (; i < kNb * (Nr + 1); ++i) { + Word temp = words[i - 1]; + + if (i % Nk == 0) { + rotateWord(&temp); + substituteWord(&temp); + // xor with rcon + temp[0] ^= kRoundConstant[(i / Nk) - 1]; + } else if (Nk == 8 && i % Nk == 4) { + // See note for 256-bit keys on Sec. 5.2 on FIPS.197 + substituteWord(&temp); + } + + + // xor previous column of new key with corresponding column of + // previous round key + Word correspondingWord = words[i - Nk]; + byte b0 = correspondingWord[0] ^ temp[0]; + byte b1 = correspondingWord[1] ^ temp[1]; + byte b2 = correspondingWord[2] ^ temp[2]; + byte b3 = correspondingWord[3] ^ temp[3]; + + words[i] = {{ b0, b1, b2, b3 }}; + } + + return words; +} + +void AES::addRoundKey(State* state, const KeySchedule* keySchedule, int round) +{ + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + state->at(i)[j] ^= keySchedule->at((round * kNb) + i)[j]; + } + } +} + +void AES::subBytes(State* state) +{ + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + state->at(i)[j] = kSBox[state->at(i)[j]]; + } + } +} + +void AES::shiftRows(State *state) +{ + // row 1 + std::swap(state->at(0)[1], state->at(3)[1]); + std::swap(state->at(0)[1], state->at(1)[1]); + std::swap(state->at(1)[1], state->at(2)[1]); + + // row 2 + std::swap(state->at(0)[2], state->at(2)[2]); + std::swap(state->at(1)[2], state->at(3)[2]); + + // row 3 + std::swap(state->at(0)[3], state->at(1)[3]); + std::swap(state->at(2)[3], state->at(3)[3]); + std::swap(state->at(0)[3], state->at(2)[3]); +} + +void AES::mixColumns(State* state) +{ + // Finds the product of {02} and the argument to xtime modulo {1b} + // mentioned in Sec. 4.2.1 of FIPS.197 + // taken from http://gauss.ececs.uc.edu/Courses/c653/extra/AES/xtime.cpp + auto xtime = [](byte x) { + return ((x << 1) ^ (((x >> 7) & 1) * 0x11b)); + }; + + for (int col = 0; col < 4; ++col) { + + // + // multiplies in GF(2^8) field selected column from state + // with constant matrix defined by publication + // + // [ 02 03 01 01 ] + // | 01 02 03 01 | + // | 01 01 02 03 | + // [ 03 01 01 02 ] + // + Word column = state->at(col); + // let's take example from publication, col: [212, 191, 93, 48] + // t == 6 + byte t = (column[0]) ^ (column[1]) ^ (column[2]) ^ (column[3]); + // see Sec. 4.2.1 and Sec. 5.1.3 for more details + state->at(col)[0] ^= xtime(column[0] ^ column[1]) ^ t; + state->at(col)[1] ^= xtime(column[1] ^ column[2]) ^ t; + state->at(col)[2] ^= xtime(column[2] ^ column[3]) ^ t; + state->at(col)[3] ^= xtime(column[3] ^ column[0]) ^ t; + } +} + +AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) +{ + State state; + ByteArray result; + + // TODO: remove this extra overhead + std::copy_n(input.begin(), kBlockSize, std::back_inserter(result)); + + // add padding if needed + if (result.size() < kBlockSize) { + std::fill_n(result.end(), kBlockSize - result.size(), 0); + } + + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + state[i][j] = result[(kNb * i) + j]; + } + } + + std::size_t keySize = key->size(); + + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + uint8_t kTotalRounds = kKeyParams.at(keySize)[1]; + + KeySchedule keySchedule = keyExpansion(key); + + int round = 0; + + // initial round + addRoundKey(&state, &keySchedule, round++); + + // intermediate round + while (round < kTotalRounds) { + subBytes(&state); + shiftRows(&state); + mixColumns(&state); + addRoundKey(&state, &keySchedule, round++); + } + + // final round + subBytes(&state); + shiftRows(&state); + addRoundKey(&state, &keySchedule, round++); + + // assign state to result + int k = 0; + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + result[k++] = state.at(i)[j]; + } + } + + return result; + +} + diff --git a/package/mine.h b/package/mine.h index b1215f9..b72ac4f 100644 --- a/package/mine.h +++ b/package/mine.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -45,7 +46,7 @@ class Base16 { /// \brief Map for fast lookup corresponding character /// \see Base64::kDecodeMap /// - static const std::unordered_map kDecodeMap; + static const std::unordered_map kDecodeMap; /// /// \brief Encodes input of length to hex encoding @@ -121,7 +122,7 @@ class Base64 { /// \ref http://www.cplusplus.com/reference/unordered_map/unordered_map/at/ /// \ref http://www.cplusplus.com/reference/string/string/find_first_of/ /// - static const std::unordered_map kDecodeMap; + static const std::unordered_map kDecodeMap; /// /// \brief Padding is must in mine implementation of base64 @@ -159,6 +160,8 @@ class Base64 { /// encoding /// \see encode(const std::string&) /// + /// \note You need to include and headers before mine.h + /// static std::string encode(const std::wstring& raw) noexcept { std::string converted = std::wstring_convert @@ -184,6 +187,8 @@ class Base64 { /// 5+ bytes long e.g, \x1F680. If you don't use such characters then it should be safe /// to use this /// + /// \note You need to include and headers before mine.h + /// static std::wstring decodeAsWString(const std::string& e) { std::string result = decode(e); @@ -233,13 +238,125 @@ using byte = unsigned char; /// /// \brief Provides AES crypto functionalities /// +/// This is validated against NIST test data and all +/// the corresponding tests under test/ directory +/// are from NIST themselves. +/// +/// Please make sure to use public functions and do not +/// use private functions especially in production as +/// you may end up using them incorrectly. However +/// the source code for AES class is heavily commented for +/// verification on implementation. +/// class AES { public: + using ByteArray = std::vector; + using Key = ByteArray; private: + using Word = std::array; + + /// + /// \brief KeySchedule is linear array of 4-byte words + /// \ref FIPS.197 Sec 5.2 + /// + using KeySchedule = std::unordered_map; + + /// + /// \brief State as described in FIPS.197 Sec. 3.4 + /// + using State = std::array, 4>; + + /// + /// \brief AES works on 16 bit block at a time + /// + static const uint8_t kBlockSize = 16; + + /// + /// \brief Defines the key params to it's size + /// + static const std::unordered_map> kKeyParams; + + /// + /// \brief As defined in FIPS. 197 Sec. 5.1.1 + /// + static const byte kSBox[]; + + /// + /// \brief As defined in FIPS. 197 Sec. 5.3.2 + /// + static const byte kSBoxInverse[]; + + /// + /// \brief Round constant is constant for each round + /// it contains 10 values each defined in + /// Appendix A of FIPS.197 in column Rcon[i/Nk] for + /// each key size, we add all of them in one array for + /// ease of access + /// + static const byte kRoundConstant[]; + + /// + /// \brief Nb + /// \note we make it constant as FIPS.197 p.9 says + /// "For this standard, Nb=4." + /// + static const uint8_t kNb = 4; + + /// + /// \brief Raw encryption function - not for public use + /// \param input 128-bit Byte array of input. + /// If array is bigger it's chopped and if it's smaller, it's padded + /// please use alternative functions if your array is bigger. Those + /// function will handle all the bytes correctly. + /// \param key Byte array of key + /// \return cipher text (byte array) + /// + static ByteArray cipher(const ByteArray& input, const Key* key); + + /// + /// \brief Key expansion function as described in FIPS.197 + /// + static KeySchedule keyExpansion(const Key* key); + + /// + /// \brief Adds round to the state using specified key schedule + /// + static void addRoundKey(State* state, const KeySchedule* keySchedule, int round); + + /// + /// \brief Substitution step for state (Sec. 5.1.1) + /// + static void subBytes(State* state); + + /// + /// \brief Shifting rows step for the state (Sec. 5.1.2) + /// + static void shiftRows(State* state); + + /// + /// \brief Mixing columns for the state (Sec. 5.1.3) + /// + static void mixColumns(State* state); + + /// + /// \brief Prints bytes in hex format in 4x4 matrix fashion + /// + static void printBytes(const ByteArray& b); + + /// + /// \brief Prints state for debugging + /// + static void printState(const State*); + AES() = delete; AES(const AES&) = delete; AES& operator=(const AES&) = delete; + + friend class AESTest_RawCipher_Test; + friend class AESTest_FiniteFieldMultiply_Test; + friend class AESTest_KeyExpansion_Test; + friend class AESTest_AddRoundKey_Test; }; /// Here onwards start implementation for RSA - this contains From 2e57c7c32e5445b9aada04da15142acf5d915925 Mon Sep 17 00:00:00 2001 From: Majid Date: Sun, 27 Aug 2017 21:17:58 +1000 Subject: [PATCH 072/148] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 39c17d6..a57c044 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ We are very careful with our implementations and have [unit tests](/test/) in pl Mine _will_ support following features: * RSA (Encrypt, Decrypt, Sign and Verify) [[RFC-3447](https://tools.ietf.org/html/rfc3447)] - * AES-CBC [[RFC-3602](https://tools.ietf.org/html/rfc3602)] + * AES [[FIPS.197](http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf)] * ZLib (Depend upon libz, eventually implement [RFC-1950](https://tools.ietf.org/html/rfc3602)) * Base16 (Encode, Decode) * Base64 (Encode, Decode) From 8a997042aeb68a91c23ca3763fefcb721ab2f1d8 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 27 Aug 2017 21:40:33 +1000 Subject: [PATCH 073/148] clean the overhead --- package/mine.cc | 29 ++++++++++++++--------------- package/mine.h | 17 ++++++++++++++--- src/aes.cc | 29 ++++++++++++++--------------- src/aes.h | 17 ++++++++++++++--- 4 files changed, 56 insertions(+), 36 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 6e252e7..52ca820 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -444,33 +444,31 @@ void AES::mixColumns(State* state) } } -AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) +AES::ByteArray AES::cipher(ByteArray input, const Key* key) { - State state; - ByteArray result; + std::size_t keySize = key->size(); - // TODO: remove this extra overhead - std::copy_n(input.begin(), kBlockSize, std::back_inserter(result)); + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } - // add padding if needed - if (result.size() < kBlockSize) { - std::fill_n(result.end(), kBlockSize - result.size(), 0); + // Pad the input if needed + if (input.size() < kBlockSize) { + std::fill_n(input.end(), kBlockSize - input.size(), 0); } + // assign it to state for processing + State state; for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - state[i][j] = result[(kNb * i) + j]; + state[i][j] = input[(kNb * i) + j]; } } - std::size_t keySize = key->size(); - - if (keySize != 16 && keySize != 24 && keySize != 32) { - throw std::invalid_argument("Invalid AES key size"); - } - uint8_t kTotalRounds = kKeyParams.at(keySize)[1]; + // Create linear subkeys (key schedule) KeySchedule keySchedule = keyExpansion(key); int round = 0; @@ -492,6 +490,7 @@ AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) addRoundKey(&state, &keySchedule, round++); // assign state to result + ByteArray result(kBlockSize); int k = 0; for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { diff --git a/package/mine.h b/package/mine.h index b72ac4f..79201b8 100644 --- a/package/mine.h +++ b/package/mine.h @@ -250,10 +250,21 @@ using byte = unsigned char; /// class AES { public: + /// + /// \brief Handy safe byte array + /// using ByteArray = std::vector; + + /// + /// \brief A key is a byte array + /// using Key = ByteArray; private: + + /// + /// \brief A word is array of 4 byte + /// using Word = std::array; /// @@ -265,7 +276,7 @@ class AES { /// /// \brief State as described in FIPS.197 Sec. 3.4 /// - using State = std::array, 4>; + using State = std::array; /// /// \brief AES works on 16 bit block at a time @@ -305,14 +316,14 @@ class AES { /// /// \brief Raw encryption function - not for public use - /// \param input 128-bit Byte array of input. + /// \param input (by val) 128-bit Byte array of input. /// If array is bigger it's chopped and if it's smaller, it's padded /// please use alternative functions if your array is bigger. Those /// function will handle all the bytes correctly. /// \param key Byte array of key /// \return cipher text (byte array) /// - static ByteArray cipher(const ByteArray& input, const Key* key); + static ByteArray cipher(ByteArray input, const Key* key); /// /// \brief Key expansion function as described in FIPS.197 diff --git a/src/aes.cc b/src/aes.cc index f2d8f73..aae14a2 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -260,33 +260,31 @@ void AES::mixColumns(State* state) } } -AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) +AES::ByteArray AES::cipher(ByteArray input, const Key* key) { - State state; - ByteArray result; + std::size_t keySize = key->size(); - // TODO: remove this extra overhead - std::copy_n(input.begin(), kBlockSize, std::back_inserter(result)); + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } - // add padding if needed - if (result.size() < kBlockSize) { - std::fill_n(result.end(), kBlockSize - result.size(), 0); + // Pad the input if needed + if (input.size() < kBlockSize) { + std::fill_n(input.end(), kBlockSize - input.size(), 0); } + // assign it to state for processing + State state; for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - state[i][j] = result[(kNb * i) + j]; + state[i][j] = input[(kNb * i) + j]; } } - std::size_t keySize = key->size(); - - if (keySize != 16 && keySize != 24 && keySize != 32) { - throw std::invalid_argument("Invalid AES key size"); - } - uint8_t kTotalRounds = kKeyParams.at(keySize)[1]; + // Create linear subkeys (key schedule) KeySchedule keySchedule = keyExpansion(key); int round = 0; @@ -308,6 +306,7 @@ AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) addRoundKey(&state, &keySchedule, round++); // assign state to result + ByteArray result(kBlockSize); int k = 0; for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { diff --git a/src/aes.h b/src/aes.h index 1e5658a..ccb72bd 100644 --- a/src/aes.h +++ b/src/aes.h @@ -45,10 +45,21 @@ using byte = unsigned char; /// class AES { public: + /// + /// \brief Handy safe byte array + /// using ByteArray = std::vector; + + /// + /// \brief A key is a byte array + /// using Key = ByteArray; private: + + /// + /// \brief A word is array of 4 byte + /// using Word = std::array; /// @@ -60,7 +71,7 @@ class AES { /// /// \brief State as described in FIPS.197 Sec. 3.4 /// - using State = std::array, 4>; + using State = std::array; /// /// \brief AES works on 16 bit block at a time @@ -100,14 +111,14 @@ class AES { /// /// \brief Raw encryption function - not for public use - /// \param input 128-bit Byte array of input. + /// \param input (by val) 128-bit Byte array of input. /// If array is bigger it's chopped and if it's smaller, it's padded /// please use alternative functions if your array is bigger. Those /// function will handle all the bytes correctly. /// \param key Byte array of key /// \return cipher text (byte array) /// - static ByteArray cipher(const ByteArray& input, const Key* key); + static ByteArray cipher(ByteArray input, const Key* key); /// /// \brief Key expansion function as described in FIPS.197 From eb5a5880c3109d3b8544c57c9bb67ccc3be57695 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 27 Aug 2017 21:59:32 +1000 Subject: [PATCH 074/148] skeleton for dicip --- src/aes.cc | 92 +++++++++++++++++++++++++++++++++++++++++++++--------- src/aes.h | 58 ++++++++++++++++++++++++++++++---- 2 files changed, 129 insertions(+), 21 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index aae14a2..690bb82 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -260,7 +260,21 @@ void AES::mixColumns(State* state) } } -AES::ByteArray AES::cipher(ByteArray input, const Key* key) +void AES::invSubBytes(State* state) +{ + +} + +void AES::invShiftRows(State *state) +{ + +} + +void AES::invMixColumns(State* state) +{ +} + +AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) { std::size_t keySize = key->size(); @@ -269,18 +283,8 @@ AES::ByteArray AES::cipher(ByteArray input, const Key* key) throw std::invalid_argument("Invalid AES key size"); } - // Pad the input if needed - if (input.size() < kBlockSize) { - std::fill_n(input.end(), kBlockSize - input.size(), 0); - } - - // assign it to state for processing State state; - for (std::size_t i = 0; i < kNb; ++i) { - for (std::size_t j = 0; j < kNb; ++j) { - state[i][j] = input[(kNb * i) + j]; - } - } + initState(&state, input); uint8_t kTotalRounds = kKeyParams.at(keySize)[1]; @@ -305,15 +309,73 @@ AES::ByteArray AES::cipher(ByteArray input, const Key* key) shiftRows(&state); addRoundKey(&state, &keySchedule, round++); - // assign state to result + return stateToByteArray(&state); + +} + +AES::ByteArray AES::decipher(const ByteArray& input, const Key* key) +{ + std::size_t keySize = key->size(); + + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + State state; + initState(&state, input); + + uint8_t kTotalRounds = kKeyParams.at(keySize)[1]; + + // Create linear subkeys (key schedule) + KeySchedule keySchedule = keyExpansion(key); + + int round = kTotalRounds; + + // initial round + addRoundKey(&state, &keySchedule, round--); + + // intermediate round + while (round > 0) { + invShiftRows(&state); + invSubBytes(&state); + addRoundKey(&state, &keySchedule, round--); + invMixColumns(&state); + } + + // final round + invShiftRows(&state); + invSubBytes(&state); + addRoundKey(&state, &keySchedule, round); + + return stateToByteArray(&state); + +} + +void AES::initState(State* state, ByteArray input) +{ + // Pad the input if needed + if (input.size() < kBlockSize) { + std::fill_n(input.end(), kBlockSize - input.size(), 0); + } + + // assign it to state for processing + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + (*state)[i][j] = input[(kNb * i) + j]; + } + } +} + +AES::ByteArray AES::stateToByteArray(const State *state) +{ ByteArray result(kBlockSize); int k = 0; for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - result[k++] = state.at(i)[j]; + result[k++] = state->at(i)[j]; } } return result; - } diff --git a/src/aes.h b/src/aes.h index ccb72bd..3b81cbc 100644 --- a/src/aes.h +++ b/src/aes.h @@ -109,16 +109,38 @@ class AES { /// static const uint8_t kNb = 4; + /// + /// \brief Initializes the state with input. This function + /// also pads the input if needed (i.e, input is not block of 128-bit) + /// + static void initState(State* state, ByteArray input); + /// /// \brief Raw encryption function - not for public use - /// \param input (by val) 128-bit Byte array of input. + /// \param input 128-bit plain input /// If array is bigger it's chopped and if it's smaller, it's padded /// please use alternative functions if your array is bigger. Those /// function will handle all the bytes correctly. /// \param key Byte array of key - /// \return cipher text (byte array) + /// \return 128-bit cipher text + /// + static ByteArray cipher(const ByteArray& input, const Key* key); + + /// + /// \brief Raw decryption function - not for public use + /// \param input 128-bit cipher input + /// If array is bigger it's chopped and if it's smaller, it's padded + /// please use alternative functions if your array is bigger. Those + /// function will handle all the bytes correctly. + /// \param key Byte array of key + /// \return 128-bit plain text + /// + static ByteArray decipher(const ByteArray& input, const Key* key); + + /// + /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array /// - static ByteArray cipher(ByteArray input, const Key* key); + static ByteArray stateToByteArray(const State* state); /// /// \brief Key expansion function as described in FIPS.197 @@ -131,20 +153,44 @@ class AES { static void addRoundKey(State* state, const KeySchedule* keySchedule, int round); /// - /// \brief Substitution step for state (Sec. 5.1.1) + /// \brief Substitution step for state + /// \ref Sec. 5.1.1 /// static void subBytes(State* state); /// - /// \brief Shifting rows step for the state (Sec. 5.1.2) + /// \brief Shifting rows step for the state + /// \ref Sec. 5.1.2 /// static void shiftRows(State* state); /// - /// \brief Mixing columns for the state (Sec. 5.1.3) + /// \brief Mixing columns for the state + /// \ref Sec. 5.1.3 /// static void mixColumns(State* state); + /// + /// \brief Transformation in the Inverse Cipher + /// that is the inverse of subBytes() + /// \ref Sec. 5.3.2 + /// + static void invSubBytes(State* state); + + /// + /// \brief Transformation in the Inverse Cipher that is + /// the inverse of shiftRows() + /// \ref Sec. 5.3.1 + /// + static void invShiftRows(State* state); + + /// + /// \brief Transformation in the Inverse Cipher + /// that is the inverse of mixColumns() + /// \ref Sec. 5.3.3 + /// + static void invMixColumns(State* state); + /// /// \brief Prints bytes in hex format in 4x4 matrix fashion /// From adf1bac787089647d36a2debfb0915a3566f3060 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 27 Aug 2017 22:31:44 +1000 Subject: [PATCH 075/148] use iter for encoding/decoding --- package/mine.cc | 233 ++++++++++++++++++------------------------- package/mine.h | 241 ++++++++++++++++++++++++++++++++++++++++++--- src/aes.cc | 12 +++ src/aes.h | 7 ++ src/base16.cc | 29 ++---- src/base16.h | 59 ++++++++++- src/base64.cc | 102 ------------------- src/base64.h | 117 +++++++++++++++++++++- test/base16-test.h | 13 +++ test/base64-test.h | 14 +++ 10 files changed, 545 insertions(+), 282 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 52ca820..82ae287 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -35,32 +35,15 @@ const std::unordered_map Base16::kDecodeMap = { {0x43, 0x0C}, {0x44, 0x0D}, {0x45, 0x0E}, {0x46, 0x0F} }; -std::string Base16::encode(const std::string& raw) noexcept +void Base16::decode(char a, char b, std::ostringstream& ss) { - std::stringstream ss; - for (auto it = raw.begin(); it < raw.end(); ++it) { - int h = (*it & 0xff); - ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; - } - return ss.str(); -} - -std::string Base16::decode(const std::string& enc) -{ - if (enc.size() % 2 != 0) { + int b0 = a & 0xff; + int b1 = b & 0xff; + try { + ss << static_cast((b0 << 4) | kDecodeMap.at(b1)); + } catch (const std::exception&) { throw std::runtime_error("Invalid base-16 encoding"); } - std::stringstream ss; - for (auto it = enc.begin(); it != enc.end(); it += 2) { - int b0 = *it & 0xff; - int b1 = *(it + 1) & 0xff; - try { - ss << static_cast((b0 << 4) | kDecodeMap.at(b1)); - } catch (const std::exception&) { - throw std::runtime_error("Invalid base-16 encoding"); - } - } - return ss.str(); } @@ -110,108 +93,6 @@ std::size_t Base64::countChars(const std::string& str) noexcept return result; } -std::string Base64::encode(const std::string& raw) noexcept -{ - std::string padding; - std::stringstream ss; - for (auto it = raw.begin(); it < raw.end(); it += 3) { - - // - // we use example following example for implementation basis - // Bits 01100001 01100010 01100011 - // 24-bit stream: 011000 010110 001001 100011 - // result indices 24 22 9 35 - // - - int c = static_cast(*it & 0xff); - ss << static_cast(static_cast(kValidChars[(c >> 2) & 0x3f])); // first 6 bits from first bitset - if (it + 1 < raw.end()) { - int c2 = static_cast(*(it + 1) & 0xff); - ss << static_cast(kValidChars[((c << 4) | // remaining 2 bits from first bitset - shift them left to get 4-bit spaces 010000 - (c2 >> 4) // first 4 bits of second bitset - shift them right to get 2 spaces and bitwise - // to add them 000110 - ) & 0x3f]); // must be within 63 -- - // 010000 - // 000110 - // --|--- - // 010110 - // 111111 - // ---&-- - // 010110 ==> 22 - if (it + 2 < raw.end()) { - int c3 = static_cast(*(it + 2) & 0xff); - ss << static_cast(kValidChars[((c2 << 2) | // remaining 4 bits from second bitset - shift them to get 011000 - (c3 >> 6) // the first 2 bits from third bitset - shift them right to get 000001 - ) & 0x3f]); - // the rest of the explanation is same as above - ss << static_cast(kValidChars[c3 & 0x3f]); // all the remaing bits - } else { - ss << static_cast(kValidChars[(c2 << 2) & 0x3f]); // we have 4 bits left from last byte need space for two 0-bits - ss << kPaddingChar; - } - } else { - ss << static_cast(kValidChars[(c << 4) & 0x3f]); // remaining 2 bits from single byte - ss << kPaddingChar << kPaddingChar; - } - } - return ss.str() + padding; -} - -std::string Base64::decode(const std::string& enc) -{ - // - // we use example following example for implementation basis - // Bits 01100001 01100010 01100011 - // 24-bit stream: 011000 010110 001001 100011 - // result indices 24 22 9 35 - // - - if (enc.size() % 4 != 0) { - throw std::runtime_error("Invalid base64 encoding. Padding is required"); - } - const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); - std::stringstream ss; - for (auto it = enc.begin(); it != enc.end(); it += 4) { - try { - int b0 = kDecodeMap.at(static_cast(*it & 0xff)); - if (b0 == kPadding || b0 == '\0') { - throw std::runtime_error("Invalid base64 encoding. No data available"); - } - int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); - int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); - int b3 = kDecodeMap.at(static_cast(*(it + 3) & 0xff)); - - ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 - b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 - - if (b1 != kPadding && b1 != '\0') { - if (b2 == kPadding || (b2 == '\0' && b3 == '\0')) { - // second biteset is 'partial byte' - ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); - } else { - ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 - // first we clear the bits at pos 4 and 5 - // then we concat with next bit - b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 - if (b3 == kPadding || b3 == '\0') { - // third bitset is 'partial byte' - ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); - // first we clear first 4 bits - } else { - ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6 | // 001001 ==> 000001 << 6 ==> 01000000 - // first we clear first 4 bits - // then concat with last byte as is - b3); // as is - } - } - } - } catch (const std::exception&) { - throw std::runtime_error("Invalid base64 character"); - } - } - return ss.str(); -} - const byte AES::kSBox[256] = { 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, @@ -444,7 +325,21 @@ void AES::mixColumns(State* state) } } -AES::ByteArray AES::cipher(ByteArray input, const Key* key) +void AES::invSubBytes(State* state) +{ + +} + +void AES::invShiftRows(State *state) +{ + +} + +void AES::invMixColumns(State* state) +{ +} + +AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) { std::size_t keySize = key->size(); @@ -453,18 +348,8 @@ AES::ByteArray AES::cipher(ByteArray input, const Key* key) throw std::invalid_argument("Invalid AES key size"); } - // Pad the input if needed - if (input.size() < kBlockSize) { - std::fill_n(input.end(), kBlockSize - input.size(), 0); - } - - // assign it to state for processing State state; - for (std::size_t i = 0; i < kNb; ++i) { - for (std::size_t j = 0; j < kNb; ++j) { - state[i][j] = input[(kNb * i) + j]; - } - } + initState(&state, input); uint8_t kTotalRounds = kKeyParams.at(keySize)[1]; @@ -489,17 +374,87 @@ AES::ByteArray AES::cipher(ByteArray input, const Key* key) shiftRows(&state); addRoundKey(&state, &keySchedule, round++); - // assign state to result + return stateToByteArray(&state); + +} + +AES::ByteArray AES::decipher(const ByteArray& input, const Key* key) +{ + std::size_t keySize = key->size(); + + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + State state; + initState(&state, input); + + uint8_t kTotalRounds = kKeyParams.at(keySize)[1]; + + // Create linear subkeys (key schedule) + KeySchedule keySchedule = keyExpansion(key); + + int round = kTotalRounds; + + // initial round + addRoundKey(&state, &keySchedule, round--); + + // intermediate round + while (round > 0) { + invShiftRows(&state); + invSubBytes(&state); + addRoundKey(&state, &keySchedule, round--); + invMixColumns(&state); + } + + // final round + invShiftRows(&state); + invSubBytes(&state); + addRoundKey(&state, &keySchedule, round); + + return stateToByteArray(&state); + +} + +void AES::initState(State* state, ByteArray input) +{ + // Pad the input if needed + if (input.size() < kBlockSize) { + std::fill_n(input.end(), kBlockSize - input.size(), 0); + } + + // assign it to state for processing + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + (*state)[i][j] = input[(kNb * i) + j]; + } + } +} + +AES::ByteArray AES::stateToByteArray(const State *state) +{ ByteArray result(kBlockSize); int k = 0; for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - result[k++] = state.at(i)[j]; + result[k++] = state->at(i)[j]; } } return result; +} +// public + +std::string AES::cipher(const std::string& input, const std::string& key) +{ + Key k(key.size()); + std::copy(key.begin(), key.end(), k.begin()); + ByteArray inp(kBlockSize); + std::copy(input.begin(), input.end(), inp.begin()); + ByteArray result = cipher(inp, &k); + return Base16::encode(result.begin(), result.end()); } diff --git a/package/mine.h b/package/mine.h index 79201b8..0c3b7b1 100644 --- a/package/mine.h +++ b/package/mine.h @@ -49,9 +49,34 @@ class Base16 { static const std::unordered_map kDecodeMap; /// - /// \brief Encodes input of length to hex encoding + /// \brief Encodes input to hex encoding /// - static std::string encode(const std::string& raw) noexcept; + static inline std::string encode(const std::string& raw) noexcept + { + return encode(raw.begin(), raw.end()); + } + + /// + /// \brief Encodes input iterator to hex encoding + /// + template + static std::string encode(const Iter& begin, const Iter& end) noexcept + { + std::ostringstream ss; + for (auto it = begin; it < end; ++it) { + encode(*it, ss); + } + return ss.str(); + } + + /// + /// \brief Encodes single byte + /// + static inline void encode(char b, std::ostringstream& ss) noexcept + { + int h = (b & 0xff); + ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; + } /// /// \brief Encodes integer to hex @@ -74,9 +99,35 @@ class Base16 { /// /// \brief Decodes encoded hex /// \throws std::runtime if invalid encoding. - /// std::runtime::what() is set according to the error + /// std::runtime::what() is set accordingly /// - static std::string decode(const std::string& e); + static std::string decode(const std::string& enc) + { + if (enc.size() % 2 != 0) { + throw std::runtime_error("Invalid base-16 encoding"); + } + return decode(enc.begin(), enc.end()); + } + + /// + /// \brief Encodes input iterator to hex encoding + /// \note User should check for the valid size or use decode(std::string) + /// \throws runtime_error if invalid base16-encoding + /// + template + static std::string decode(const Iter& begin, const Iter& end) + { + std::ostringstream ss; + for (auto it = begin; it != end; it += 2) { + decode(*it, *(it + 1), ss); + } + return ss.str(); + } + + /// + /// \brief Decodes single byte pair + /// + static void decode(char a, char b, std::ostringstream& ss); /// /// \brief Decodes encoding to single integer of type T @@ -152,7 +203,61 @@ class Base64 { /// /// \brief Encodes input of length to base64 encoding /// - static std::string encode(const std::string& raw) noexcept; + static std::string encode(const std::string& raw) noexcept + { + return encode(raw.begin(), raw.end()); + } + + /// + /// \brief Encodes iterators + /// + template + static std::string encode(const Iter& begin, const Iter& end) noexcept + { + std::string padding; + std::stringstream ss; + for (auto it = begin; it < end; it += 3) { + + // + // we use example following example for implementation basis + // Bits 01100001 01100010 01100011 + // 24-bit stream: 011000 010110 001001 100011 + // result indices 24 22 9 35 + // + + int c = static_cast(*it & 0xff); + ss << static_cast(static_cast(kValidChars[(c >> 2) & 0x3f])); // first 6 bits from first bitset + if (it + 1 < end) { + int c2 = static_cast(*(it + 1) & 0xff); + ss << static_cast(kValidChars[((c << 4) | // remaining 2 bits from first bitset - shift them left to get 4-bit spaces 010000 + (c2 >> 4) // first 4 bits of second bitset - shift them right to get 2 spaces and bitwise + // to add them 000110 + ) & 0x3f]); // must be within 63 -- + // 010000 + // 000110 + // --|--- + // 010110 + // 111111 + // ---&-- + // 010110 ==> 22 + if (it + 2 < end) { + int c3 = static_cast(*(it + 2) & 0xff); + ss << static_cast(kValidChars[((c2 << 2) | // remaining 4 bits from second bitset - shift them to get 011000 + (c3 >> 6) // the first 2 bits from third bitset - shift them right to get 000001 + ) & 0x3f]); + // the rest of the explanation is same as above + ss << static_cast(kValidChars[c3 & 0x3f]); // all the remaing bits + } else { + ss << static_cast(kValidChars[(c2 << 2) & 0x3f]); // we have 4 bits left from last byte need space for two 0-bits + ss << kPaddingChar; + } + } else { + ss << static_cast(kValidChars[(c << 4) & 0x3f]); // remaining 2 bits from single byte + ss << kPaddingChar << kPaddingChar; + } + } + return ss.str() + padding; + } #ifdef MINE_BASE64_WSTRING_CONVERSION /// @@ -176,7 +281,66 @@ class Base64 { /// is if no padding is found /// std::runtime::what() is set according to the error /// - static std::string decode(const std::string& e); + static std::string decode(const std::string& e) + { + if (e.size() % 4 != 0) { + throw std::runtime_error("Invalid base64 encoding. Padding is required"); + } + return decode(e.begin(), e.end()); + } + + template + static std::string decode(const Iter& begin, const Iter& end) + { + // + // we use example following example for implementation basis + // Bits 01100001 01100010 01100011 + // 24-bit stream: 011000 010110 001001 100011 + // result indices 24 22 9 35 + // + + const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); + std::stringstream ss; + for (auto it = begin; it != end; it += 4) { + try { + int b0 = kDecodeMap.at(static_cast(*it & 0xff)); + if (b0 == kPadding || b0 == '\0') { + throw std::runtime_error("Invalid base64 encoding. No data available"); + } + int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); + int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); + int b3 = kDecodeMap.at(static_cast(*(it + 3) & 0xff)); + + ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 + b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 + + if (b1 != kPadding && b1 != '\0') { + if (b2 == kPadding || (b2 == '\0' && b3 == '\0')) { + // second biteset is 'partial byte' + ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); + } else { + ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 + // first we clear the bits at pos 4 and 5 + // then we concat with next bit + b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 + if (b3 == kPadding || b3 == '\0') { + // third bitset is 'partial byte' + ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); + // first we clear first 4 bits + } else { + ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6 | // 001001 ==> 000001 << 6 ==> 01000000 + // first we clear first 4 bits + // then concat with last byte as is + b3); // as is + } + } + } + } catch (const std::exception&) { + throw std::runtime_error("Invalid base64 character"); + } + } + return ss.str(); + } #ifdef MINE_BASE64_WSTRING_CONVERSION /// @@ -260,6 +424,13 @@ class AES { /// using Key = ByteArray; + /// + /// \brief Ciphers the input with specified hex key + /// \param key Hex key + /// \return Base16 encoded cipher + /// + static std::string cipher(const std::string& input, const std::string& key); + private: /// @@ -314,16 +485,38 @@ class AES { /// static const uint8_t kNb = 4; + /// + /// \brief Initializes the state with input. This function + /// also pads the input if needed (i.e, input is not block of 128-bit) + /// + static void initState(State* state, ByteArray input); + /// /// \brief Raw encryption function - not for public use - /// \param input (by val) 128-bit Byte array of input. + /// \param input 128-bit plain input + /// If array is bigger it's chopped and if it's smaller, it's padded + /// please use alternative functions if your array is bigger. Those + /// function will handle all the bytes correctly. + /// \param key Byte array of key + /// \return 128-bit cipher text + /// + static ByteArray cipher(const ByteArray& input, const Key* key); + + /// + /// \brief Raw decryption function - not for public use + /// \param input 128-bit cipher input /// If array is bigger it's chopped and if it's smaller, it's padded /// please use alternative functions if your array is bigger. Those /// function will handle all the bytes correctly. /// \param key Byte array of key - /// \return cipher text (byte array) + /// \return 128-bit plain text + /// + static ByteArray decipher(const ByteArray& input, const Key* key); + /// - static ByteArray cipher(ByteArray input, const Key* key); + /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array + /// + static ByteArray stateToByteArray(const State* state); /// /// \brief Key expansion function as described in FIPS.197 @@ -336,20 +529,44 @@ class AES { static void addRoundKey(State* state, const KeySchedule* keySchedule, int round); /// - /// \brief Substitution step for state (Sec. 5.1.1) + /// \brief Substitution step for state + /// \ref Sec. 5.1.1 /// static void subBytes(State* state); /// - /// \brief Shifting rows step for the state (Sec. 5.1.2) + /// \brief Shifting rows step for the state + /// \ref Sec. 5.1.2 /// static void shiftRows(State* state); /// - /// \brief Mixing columns for the state (Sec. 5.1.3) + /// \brief Mixing columns for the state + /// \ref Sec. 5.1.3 /// static void mixColumns(State* state); + /// + /// \brief Transformation in the Inverse Cipher + /// that is the inverse of subBytes() + /// \ref Sec. 5.3.2 + /// + static void invSubBytes(State* state); + + /// + /// \brief Transformation in the Inverse Cipher that is + /// the inverse of shiftRows() + /// \ref Sec. 5.3.1 + /// + static void invShiftRows(State* state); + + /// + /// \brief Transformation in the Inverse Cipher + /// that is the inverse of mixColumns() + /// \ref Sec. 5.3.3 + /// + static void invMixColumns(State* state); + /// /// \brief Prints bytes in hex format in 4x4 matrix fashion /// diff --git a/src/aes.cc b/src/aes.cc index 690bb82..7232a45 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -379,3 +379,15 @@ AES::ByteArray AES::stateToByteArray(const State *state) return result; } + +// public + +std::string AES::cipher(const std::string& input, const std::string& key) +{ + Key k(key.size()); + std::copy(key.begin(), key.end(), k.begin()); + ByteArray inp(kBlockSize); + std::copy(input.begin(), input.end(), inp.begin()); + ByteArray result = cipher(inp, &k); + return Base16::encode(result.begin(), result.end()); +} diff --git a/src/aes.h b/src/aes.h index 3b81cbc..b12be49 100644 --- a/src/aes.h +++ b/src/aes.h @@ -55,6 +55,13 @@ class AES { /// using Key = ByteArray; + /// + /// \brief Ciphers the input with specified hex key + /// \param key Hex key + /// \return Base16 encoded cipher + /// + static std::string cipher(const std::string& input, const std::string& key); + private: /// diff --git a/src/base16.cc b/src/base16.cc index 0cd4dfe..384fc26 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -29,30 +29,13 @@ const std::unordered_map Base16::kDecodeMap = { {0x43, 0x0C}, {0x44, 0x0D}, {0x45, 0x0E}, {0x46, 0x0F} }; -std::string Base16::encode(const std::string& raw) noexcept +void Base16::decode(char a, char b, std::ostringstream& ss) { - std::stringstream ss; - for (auto it = raw.begin(); it < raw.end(); ++it) { - int h = (*it & 0xff); - ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; - } - return ss.str(); -} - -std::string Base16::decode(const std::string& enc) -{ - if (enc.size() % 2 != 0) { + int b0 = a & 0xff; + int b1 = b & 0xff; + try { + ss << static_cast((b0 << 4) | kDecodeMap.at(b1)); + } catch (const std::exception&) { throw std::runtime_error("Invalid base-16 encoding"); } - std::stringstream ss; - for (auto it = enc.begin(); it != enc.end(); it += 2) { - int b0 = *it & 0xff; - int b1 = *(it + 1) & 0xff; - try { - ss << static_cast((b0 << 4) | kDecodeMap.at(b1)); - } catch (const std::exception&) { - throw std::runtime_error("Invalid base-16 encoding"); - } - } - return ss.str(); } diff --git a/src/base16.h b/src/base16.h index 13656c0..fa6a66a 100644 --- a/src/base16.h +++ b/src/base16.h @@ -48,9 +48,34 @@ class Base16 { static const std::unordered_map kDecodeMap; /// - /// \brief Encodes input of length to hex encoding + /// \brief Encodes input to hex encoding /// - static std::string encode(const std::string& raw) noexcept; + static inline std::string encode(const std::string& raw) noexcept + { + return encode(raw.begin(), raw.end()); + } + + /// + /// \brief Encodes input iterator to hex encoding + /// + template + static std::string encode(const Iter& begin, const Iter& end) noexcept + { + std::ostringstream ss; + for (auto it = begin; it < end; ++it) { + encode(*it, ss); + } + return ss.str(); + } + + /// + /// \brief Encodes single byte + /// + static inline void encode(char b, std::ostringstream& ss) noexcept + { + int h = (b & 0xff); + ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; + } /// /// \brief Encodes integer to hex @@ -73,9 +98,35 @@ class Base16 { /// /// \brief Decodes encoded hex /// \throws std::runtime if invalid encoding. - /// std::runtime::what() is set according to the error + /// std::runtime::what() is set accordingly + /// + static std::string decode(const std::string& enc) + { + if (enc.size() % 2 != 0) { + throw std::runtime_error("Invalid base-16 encoding"); + } + return decode(enc.begin(), enc.end()); + } + + /// + /// \brief Encodes input iterator to hex encoding + /// \note User should check for the valid size or use decode(std::string) + /// \throws runtime_error if invalid base16-encoding + /// + template + static std::string decode(const Iter& begin, const Iter& end) + { + std::ostringstream ss; + for (auto it = begin; it != end; it += 2) { + decode(*it, *(it + 1), ss); + } + return ss.str(); + } + + /// + /// \brief Decodes single byte pair /// - static std::string decode(const std::string& e); + static void decode(char a, char b, std::ostringstream& ss); /// /// \brief Decodes encoding to single integer of type T diff --git a/src/base64.cc b/src/base64.cc index 4e79858..92a0231 100644 --- a/src/base64.cc +++ b/src/base64.cc @@ -65,105 +65,3 @@ std::size_t Base64::countChars(const std::string& str) noexcept } return result; } - -std::string Base64::encode(const std::string& raw) noexcept -{ - std::string padding; - std::stringstream ss; - for (auto it = raw.begin(); it < raw.end(); it += 3) { - - // - // we use example following example for implementation basis - // Bits 01100001 01100010 01100011 - // 24-bit stream: 011000 010110 001001 100011 - // result indices 24 22 9 35 - // - - int c = static_cast(*it & 0xff); - ss << static_cast(static_cast(kValidChars[(c >> 2) & 0x3f])); // first 6 bits from first bitset - if (it + 1 < raw.end()) { - int c2 = static_cast(*(it + 1) & 0xff); - ss << static_cast(kValidChars[((c << 4) | // remaining 2 bits from first bitset - shift them left to get 4-bit spaces 010000 - (c2 >> 4) // first 4 bits of second bitset - shift them right to get 2 spaces and bitwise - // to add them 000110 - ) & 0x3f]); // must be within 63 -- - // 010000 - // 000110 - // --|--- - // 010110 - // 111111 - // ---&-- - // 010110 ==> 22 - if (it + 2 < raw.end()) { - int c3 = static_cast(*(it + 2) & 0xff); - ss << static_cast(kValidChars[((c2 << 2) | // remaining 4 bits from second bitset - shift them to get 011000 - (c3 >> 6) // the first 2 bits from third bitset - shift them right to get 000001 - ) & 0x3f]); - // the rest of the explanation is same as above - ss << static_cast(kValidChars[c3 & 0x3f]); // all the remaing bits - } else { - ss << static_cast(kValidChars[(c2 << 2) & 0x3f]); // we have 4 bits left from last byte need space for two 0-bits - ss << kPaddingChar; - } - } else { - ss << static_cast(kValidChars[(c << 4) & 0x3f]); // remaining 2 bits from single byte - ss << kPaddingChar << kPaddingChar; - } - } - return ss.str() + padding; -} - -std::string Base64::decode(const std::string& enc) -{ - // - // we use example following example for implementation basis - // Bits 01100001 01100010 01100011 - // 24-bit stream: 011000 010110 001001 100011 - // result indices 24 22 9 35 - // - - if (enc.size() % 4 != 0) { - throw std::runtime_error("Invalid base64 encoding. Padding is required"); - } - const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); - std::stringstream ss; - for (auto it = enc.begin(); it != enc.end(); it += 4) { - try { - int b0 = kDecodeMap.at(static_cast(*it & 0xff)); - if (b0 == kPadding || b0 == '\0') { - throw std::runtime_error("Invalid base64 encoding. No data available"); - } - int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); - int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); - int b3 = kDecodeMap.at(static_cast(*(it + 3) & 0xff)); - - ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 - b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 - - if (b1 != kPadding && b1 != '\0') { - if (b2 == kPadding || (b2 == '\0' && b3 == '\0')) { - // second biteset is 'partial byte' - ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); - } else { - ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 - // first we clear the bits at pos 4 and 5 - // then we concat with next bit - b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 - if (b3 == kPadding || b3 == '\0') { - // third bitset is 'partial byte' - ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); - // first we clear first 4 bits - } else { - ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6 | // 001001 ==> 000001 << 6 ==> 01000000 - // first we clear first 4 bits - // then concat with last byte as is - b3); // as is - } - } - } - } catch (const std::exception&) { - throw std::runtime_error("Invalid base64 character"); - } - } - return ss.str(); -} diff --git a/src/base64.h b/src/base64.h index d05cece..9bcd47e 100644 --- a/src/base64.h +++ b/src/base64.h @@ -85,7 +85,61 @@ class Base64 { /// /// \brief Encodes input of length to base64 encoding /// - static std::string encode(const std::string& raw) noexcept; + static std::string encode(const std::string& raw) noexcept + { + return encode(raw.begin(), raw.end()); + } + + /// + /// \brief Encodes iterators + /// + template + static std::string encode(const Iter& begin, const Iter& end) noexcept + { + std::string padding; + std::stringstream ss; + for (auto it = begin; it < end; it += 3) { + + // + // we use example following example for implementation basis + // Bits 01100001 01100010 01100011 + // 24-bit stream: 011000 010110 001001 100011 + // result indices 24 22 9 35 + // + + int c = static_cast(*it & 0xff); + ss << static_cast(static_cast(kValidChars[(c >> 2) & 0x3f])); // first 6 bits from first bitset + if (it + 1 < end) { + int c2 = static_cast(*(it + 1) & 0xff); + ss << static_cast(kValidChars[((c << 4) | // remaining 2 bits from first bitset - shift them left to get 4-bit spaces 010000 + (c2 >> 4) // first 4 bits of second bitset - shift them right to get 2 spaces and bitwise + // to add them 000110 + ) & 0x3f]); // must be within 63 -- + // 010000 + // 000110 + // --|--- + // 010110 + // 111111 + // ---&-- + // 010110 ==> 22 + if (it + 2 < end) { + int c3 = static_cast(*(it + 2) & 0xff); + ss << static_cast(kValidChars[((c2 << 2) | // remaining 4 bits from second bitset - shift them to get 011000 + (c3 >> 6) // the first 2 bits from third bitset - shift them right to get 000001 + ) & 0x3f]); + // the rest of the explanation is same as above + ss << static_cast(kValidChars[c3 & 0x3f]); // all the remaing bits + } else { + ss << static_cast(kValidChars[(c2 << 2) & 0x3f]); // we have 4 bits left from last byte need space for two 0-bits + ss << kPaddingChar; + } + } else { + ss << static_cast(kValidChars[(c << 4) & 0x3f]); // remaining 2 bits from single byte + ss << kPaddingChar << kPaddingChar; + } + } + return ss.str() + padding; + } #ifdef MINE_BASE64_WSTRING_CONVERSION /// @@ -109,7 +163,66 @@ class Base64 { /// is if no padding is found /// std::runtime::what() is set according to the error /// - static std::string decode(const std::string& e); + static std::string decode(const std::string& e) + { + if (e.size() % 4 != 0) { + throw std::runtime_error("Invalid base64 encoding. Padding is required"); + } + return decode(e.begin(), e.end()); + } + + template + static std::string decode(const Iter& begin, const Iter& end) + { + // + // we use example following example for implementation basis + // Bits 01100001 01100010 01100011 + // 24-bit stream: 011000 010110 001001 100011 + // result indices 24 22 9 35 + // + + const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); + std::stringstream ss; + for (auto it = begin; it != end; it += 4) { + try { + int b0 = kDecodeMap.at(static_cast(*it & 0xff)); + if (b0 == kPadding || b0 == '\0') { + throw std::runtime_error("Invalid base64 encoding. No data available"); + } + int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); + int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); + int b3 = kDecodeMap.at(static_cast(*(it + 3) & 0xff)); + + ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 + b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 + + if (b1 != kPadding && b1 != '\0') { + if (b2 == kPadding || (b2 == '\0' && b3 == '\0')) { + // second biteset is 'partial byte' + ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); + } else { + ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 + // first we clear the bits at pos 4 and 5 + // then we concat with next bit + b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 + if (b3 == kPadding || b3 == '\0') { + // third bitset is 'partial byte' + ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); + // first we clear first 4 bits + } else { + ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6 | // 001001 ==> 000001 << 6 ==> 01000000 + // first we clear first 4 bits + // then concat with last byte as is + b3); // as is + } + } + } + } catch (const std::exception&) { + throw std::runtime_error("Invalid base64 character"); + } + } + return ss.str(); + } #ifdef MINE_BASE64_WSTRING_CONVERSION /// diff --git a/test/base16-test.h b/test/base16-test.h index ac6a29b..1991d5e 100644 --- a/test/base16-test.h +++ b/test/base16-test.h @@ -17,6 +17,11 @@ static TestData Base16TestData = { TestCase("616263313233213F242A262829272D3D407E", "abc123!?$*&()'-=@~"), }; +// plain encoding +static TestData, std::string> Base16ByteArrayEncodingTestData = { + TestCase(std::vector { 72, 101, 108, 108, 111 }, "48656C6C6F"), +}; + static TestData Base16IntTestData = { TestCase("22FD3", 143315ULL), TestCase("35639D3C8", 14331532232ULL), @@ -35,6 +40,14 @@ TEST(Base16Test, Encode) } } +TEST(Base16Test, EncodeByteArray) +{ + for (const auto& item : Base16ByteArrayEncodingTestData) { + std::string encoded = Base16::encode(PARAM(0).begin(), PARAM(0).end()); + ASSERT_STREQ(PARAM(1).c_str(), encoded.c_str()); + } +} + TEST(Base16Test, EncodeInt) { for (const auto& item : Base16IntTestData) { diff --git a/test/base64-test.h b/test/base64-test.h index 9069192..067cb04 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -68,6 +68,11 @@ static TestData IsBase64Data = { TestCase("i3eclcagfnUbK1B==", true), }; +static TestData, std::string> Base64ByteArrayEncodingTestData = { + TestCase(std::vector { 72, 101, 108, 108, 111 }, "SGVsbG8="), +}; + + TEST(Base64Test, Encode) { for (const auto& item : Base64TestData) { @@ -76,6 +81,15 @@ TEST(Base64Test, Encode) } } + +TEST(Base64Test, ByteArrayEncode) +{ + for (const auto& item : Base64ByteArrayEncodingTestData) { + std::string encoded = Base64::encode(PARAM(0).begin(), PARAM(0).end()); + ASSERT_STREQ(PARAM(1).c_str(), encoded.c_str()); + } +} + TEST(Base64Test, Decode) { for (const auto& item : Base64TestData) { From 18f616bb6e56084fd089131c626a0389987a63e8 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 27 Aug 2017 23:07:01 +1000 Subject: [PATCH 076/148] simple cipher --- src/aes.cc | 6 ++---- src/aes.h | 2 +- test/aes-test.h | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 7232a45..6f1c8d0 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -382,12 +382,10 @@ AES::ByteArray AES::stateToByteArray(const State *state) // public -std::string AES::cipher(const std::string& input, const std::string& key) +std::string AES::cipher(const std::string& input, const Key& key) { - Key k(key.size()); - std::copy(key.begin(), key.end(), k.begin()); ByteArray inp(kBlockSize); std::copy(input.begin(), input.end(), inp.begin()); - ByteArray result = cipher(inp, &k); + ByteArray result = cipher(inp, &key); return Base16::encode(result.begin(), result.end()); } diff --git a/src/aes.h b/src/aes.h index b12be49..bfdfaf9 100644 --- a/src/aes.h +++ b/src/aes.h @@ -60,7 +60,7 @@ class AES { /// \param key Hex key /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key); + static std::string cipher(const std::string& input, const Key& key); private: diff --git a/test/aes-test.h b/test/aes-test.h index fbac183..07df6b7 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -304,8 +304,26 @@ TEST(AESTest, RawCipher) AES::ByteArray output = AES::cipher(input, &key); ASSERT_EQ(expected, output); +} + +TEST(AESTest, DevAid) +{ + std::string cip = AES::cipher("this is a test!!", {{ + 0x2b, 0x7e, 0x15, 0x16, + 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, + 0x09, 0xcf, 0x4f, 0x3c + }}); + // key: 2b7e151628aed2a6abf7158809cf4f3c + // + // inp: this is a test!! + // cip: 4F0A21D2E75B7832246D45109258B75A + // inp: this is a test + // cip: 85D87A79E77CE29EE9374E235DC8FA7E + std::cout << "AES Cipher: " << cip << std::endl; } + } #endif // AES_TEST_H From cffbe44ef542e6834cab55ddcda1541c25970ef2 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 27 Aug 2017 23:43:40 +1000 Subject: [PATCH 077/148] more test cases from fips --- src/aes.cc | 19 ++++++++++++---- src/aes.h | 8 ++++++- test/aes-test.h | 58 +++++++++++++++++++++++++++++-------------------- 3 files changed, 56 insertions(+), 29 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 6f1c8d0..f2e9f7b 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -19,6 +19,7 @@ // http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf // +#include #include #include #include @@ -100,6 +101,16 @@ void AES::printState(const State* state) std::cout << std::endl; } +AES::ByteArray AES::hexStrToByteArray(const std::string& hex) +{ + AES::ByteArray byteArr; + for (std::size_t i = 0; i < hex.length(); i += 2) { + std::string hexByte = hex.substr(i, 2); + byteArr.push_back(static_cast(strtol(hexByte.c_str(), NULL, 16))); + } + return byteArr; + } + AES::KeySchedule AES::keyExpansion(const Key* key) { @@ -382,10 +393,10 @@ AES::ByteArray AES::stateToByteArray(const State *state) // public -std::string AES::cipher(const std::string& input, const Key& key) +std::string AES::cipher(const std::string& input, const std::string& key) { - ByteArray inp(kBlockSize); - std::copy(input.begin(), input.end(), inp.begin()); - ByteArray result = cipher(inp, &key); + Key keyArr = hexStrToByteArray(key); + ByteArray inp = hexStrToByteArray(input); + ByteArray result = cipher(inp, &keyArr); return Base16::encode(result.begin(), result.end()); } diff --git a/src/aes.h b/src/aes.h index bfdfaf9..49361ed 100644 --- a/src/aes.h +++ b/src/aes.h @@ -60,7 +60,7 @@ class AES { /// \param key Hex key /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const Key& key); + static std::string cipher(const std::string& input, const std::string& key); private: @@ -208,11 +208,17 @@ class AES { /// static void printState(const State*); + /// + /// \brief Converts hex string to byte array + /// + static ByteArray hexStrToByteArray(const std::string& hex); + AES() = delete; AES(const AES&) = delete; AES& operator=(const AES&) = delete; friend class AESTest_RawCipher_Test; + friend class AESTest_RawSimpleCipher_Test; friend class AESTest_FiniteFieldMultiply_Test; friend class AESTest_KeyExpansion_Test; friend class AESTest_AddRoundKey_Test; diff --git a/test/aes-test.h b/test/aes-test.h index 07df6b7..141c877 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -14,28 +14,6 @@ namespace mine { // from http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf -static const AES::ByteArray kNistIV = {{ - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f - }}; - -static const AES::Key kNistKey128 = {{ - 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, - 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c - }}; - -static const AES::Key kNistKey192 = {{ - 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, - 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, - 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b - }}; - -static const AES::Key kNistKey256 = {{ - 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, - 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, - 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, - 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 - }}; TEST(AESTest, KeyExpansion) { @@ -277,7 +255,39 @@ TEST(AESTest, AddRoundKey) ASSERT_EQ(expected7, state); } +// from FIPS.197 p.35 onwards +// input key expected +static TestData RawCipherData = { + // 128-bit key + TestCase("00112233445566778899aabbccddeeff", "000102030405060708090a0b0c0d0e0f", "69c4e0d86a7b0430d8cdb78070b4c55a"), + + // 192-bit key + TestCase("00112233445566778899aabbccddeeff", "000102030405060708090a0b0c0d0e0f1011121314151617", "dda97ca4864cdfe06eaf70a0ec0d7191"), + + // 256-bit key + TestCase("00112233445566778899aabbccddeeff", "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", "8ea2b7ca516745bfeafc49904b496089"), +}; + TEST(AESTest, RawCipher) +{ + + for (auto& item : RawCipherData) { + AES::ByteArray input = AES::hexStrToByteArray(PARAM(0)); + AES::Key key = static_cast(AES::hexStrToByteArray(PARAM(1))); + AES::ByteArray expected = AES::hexStrToByteArray(PARAM(2)); + AES::ByteArray output = AES::cipher(input, &key); + ASSERT_EQ(expected, output); + } + + for (auto& item : RawCipherData) { + std::string expected = PARAM(2); + std::string output = AES::cipher(PARAM(0), PARAM(1)); + // case insensitive comparison because hex can be upper or lower case + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + } +} + +TEST(AESTest, RawSimpleCipher) { // FIPS. 197 p. 33 @@ -308,7 +318,7 @@ TEST(AESTest, RawCipher) TEST(AESTest, DevAid) { - +/* std::string cip = AES::cipher("this is a test!!", {{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, @@ -321,7 +331,7 @@ TEST(AESTest, DevAid) // cip: 4F0A21D2E75B7832246D45109258B75A // inp: this is a test // cip: 85D87A79E77CE29EE9374E235DC8FA7E - std::cout << "AES Cipher: " << cip << std::endl; + std::cout << "AES Cipher: " << cip << std::endl;*/ } } From fbec2f49148c5f6665211bc16141ddc8038ca48b Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 28 Aug 2017 11:00:01 +1000 Subject: [PATCH 078/148] interface updates --- src/aes.cc | 21 ++++-------- src/aes.h | 15 ++++----- src/base16.cc | 13 ++++++++ src/base16.h | 80 +++++++++++++++++++++++++++++----------------- src/base64.h | 15 +++++++-- test/aes-test.h | 20 +++++++----- test/base16-test.h | 14 +++++++- 7 files changed, 113 insertions(+), 65 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index f2e9f7b..d0be933 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -101,16 +101,6 @@ void AES::printState(const State* state) std::cout << std::endl; } -AES::ByteArray AES::hexStrToByteArray(const std::string& hex) -{ - AES::ByteArray byteArr; - for (std::size_t i = 0; i < hex.length(); i += 2) { - std::string hexByte = hex.substr(i, 2); - byteArr.push_back(static_cast(strtol(hexByte.c_str(), NULL, 16))); - } - return byteArr; - } - AES::KeySchedule AES::keyExpansion(const Key* key) { @@ -283,9 +273,10 @@ void AES::invShiftRows(State *state) void AES::invMixColumns(State* state) { + } -AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) +ByteArray AES::cipher(const ByteArray& input, const Key* key) { std::size_t keySize = key->size(); @@ -324,7 +315,7 @@ AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) } -AES::ByteArray AES::decipher(const ByteArray& input, const Key* key) +ByteArray AES::decipher(const ByteArray& input, const Key* key) { std::size_t keySize = key->size(); @@ -378,7 +369,7 @@ void AES::initState(State* state, ByteArray input) } } -AES::ByteArray AES::stateToByteArray(const State *state) +ByteArray AES::stateToByteArray(const State *state) { ByteArray result(kBlockSize); int k = 0; @@ -395,8 +386,8 @@ AES::ByteArray AES::stateToByteArray(const State *state) std::string AES::cipher(const std::string& input, const std::string& key) { - Key keyArr = hexStrToByteArray(key); - ByteArray inp = hexStrToByteArray(input); + Key keyArr = Base16::fromString(key); + ByteArray inp = Base16::fromString(input); ByteArray result = cipher(inp, &keyArr); return Base16::encode(result.begin(), result.end()); } diff --git a/src/aes.h b/src/aes.h index 49361ed..ef2ee95 100644 --- a/src/aes.h +++ b/src/aes.h @@ -24,12 +24,18 @@ #include #include #include +#include #include "src/rsa.h" namespace mine { using byte = unsigned char; +/// +/// \brief Handy safe byte array +/// +using ByteArray = std::vector; + /// /// \brief Provides AES crypto functionalities /// @@ -45,10 +51,6 @@ using byte = unsigned char; /// class AES { public: - /// - /// \brief Handy safe byte array - /// - using ByteArray = std::vector; /// /// \brief A key is a byte array @@ -208,11 +210,6 @@ class AES { /// static void printState(const State*); - /// - /// \brief Converts hex string to byte array - /// - static ByteArray hexStrToByteArray(const std::string& hex); - AES() = delete; AES(const AES&) = delete; AES& operator=(const AES&) = delete; diff --git a/src/base16.cc b/src/base16.cc index 384fc26..68924ee 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -29,6 +29,19 @@ const std::unordered_map Base16::kDecodeMap = { {0x43, 0x0C}, {0x44, 0x0D}, {0x45, 0x0E}, {0x46, 0x0F} }; +ByteArray Base16::fromString(const std::string& hex) +{ + if (hex.size() % 2 != 0) { + throw std::invalid_argument("Invalid base-16 encoding"); + } + + ByteArray byteArr; + for (std::size_t i = 0; i < hex.length(); i += 2) { + byteArr.push_back(encode(hex.substr(i, 2).c_str())); + } + return byteArr; +} + void Base16::decode(char a, char b, std::ostringstream& ss) { int b0 = a & 0xff; diff --git a/src/base16.h b/src/base16.h index fa6a66a..77d2829 100644 --- a/src/base16.h +++ b/src/base16.h @@ -25,14 +25,20 @@ #include #include #include +#include namespace mine { using byte = unsigned char; +using ByteArray = std::vector; /// /// \brief Provides base16 encoding / decoding /// +/// This class also contains some helpers to convert various input types +/// to byte array and also provides public interface to encode +/// the iterators for other containers like vector etc. +/// class Base16 { public: @@ -55,6 +61,14 @@ class Base16 { return encode(raw.begin(), raw.end()); } + /// + /// \brief Wrapper function to encode single hex char to corresponding byte + /// + static byte encode(const char* e) + { + return static_cast(strtol(e, nullptr, 16)); + } + /// /// \brief Encodes input iterator to hex encoding /// @@ -69,13 +83,12 @@ class Base16 { } /// - /// \brief Encodes single byte + /// \brief Converts hex stream (e.g, 48656C6C6F) to byte array + /// \param hex String stream e.g, 48656C6C6F (Hello) + /// \return Byte array (mine::ByteArray) containing bytes e.g, 0x48, 0x65, 0x6C, 0x6C, 0x6F + /// \throws invalid_argument if hex is not valid /// - static inline void encode(char b, std::ostringstream& ss) noexcept - { - int h = (b & 0xff); - ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; - } + static ByteArray fromString(const std::string& hex); /// /// \brief Encodes integer to hex @@ -97,37 +110,17 @@ class Base16 { /// /// \brief Decodes encoded hex - /// \throws std::runtime if invalid encoding. - /// std::runtime::what() is set accordingly + /// \throws std::invalid_argument if invalid encoding. + /// std::invalid_argument::what() is set accordingly /// static std::string decode(const std::string& enc) { if (enc.size() % 2 != 0) { - throw std::runtime_error("Invalid base-16 encoding"); + throw std::invalid_argument("Invalid base-16 encoding"); } return decode(enc.begin(), enc.end()); } - /// - /// \brief Encodes input iterator to hex encoding - /// \note User should check for the valid size or use decode(std::string) - /// \throws runtime_error if invalid base16-encoding - /// - template - static std::string decode(const Iter& begin, const Iter& end) - { - std::ostringstream ss; - for (auto it = begin; it != end; it += 2) { - decode(*it, *(it + 1), ss); - } - return ss.str(); - } - - /// - /// \brief Decodes single byte pair - /// - static void decode(char a, char b, std::ostringstream& ss); - /// /// \brief Decodes encoding to single integer of type T /// @@ -149,6 +142,35 @@ class Base16 { Base16() = delete; Base16(const Base16&) = delete; Base16& operator=(const Base16&) = delete; + + /// + /// \brief Encodes single byte + /// + static inline void encode(char b, std::ostringstream& ss) noexcept + { + int h = (b & 0xff); + ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; + } + + /// + /// \brief Encodes input iterator to hex encoding + /// \note User should check for the valid size or use decode(std::string) + /// \throws runtime_error if invalid base16-encoding + /// + template + static std::string decode(const Iter& begin, const Iter& end) + { + std::ostringstream ss; + for (auto it = begin; it != end; it += 2) { + decode(*it, *(it + 1), ss); + } + return ss.str(); + } + + /// + /// \brief Decodes single byte pair + /// + static void decode(char a, char b, std::ostringstream& ss); }; } // end namespace mine diff --git a/src/base64.h b/src/base64.h index 9bcd47e..93db7a6 100644 --- a/src/base64.h +++ b/src/base64.h @@ -39,6 +39,11 @@ using byte = unsigned char; /// /// \brief Provides base64 encoding / decoding implementation /// +/// This class also provides public interface to encode +/// the iterators for other containers like vector etc. +/// +/// This also handles unicode characters +/// class Base64 { public: @@ -159,9 +164,7 @@ class Base64 { /// /// \brief Decodes encoded base64 - /// \throws std::runtime if invalid encoding. Another time it is thrown - /// is if no padding is found - /// std::runtime::what() is set according to the error + /// \see decode(const Iter&, const Iter&) /// static std::string decode(const std::string& e) { @@ -171,6 +174,12 @@ class Base64 { return decode(e.begin(), e.end()); } + /// + /// \brief Decodes base64 iterator from begin to end + /// \throws std::runtime if invalid encoding. Another time it is thrown + /// is if no padding is found + /// std::runtime::what() is set according to the error + /// template static std::string decode(const Iter& begin, const Iter& end) { diff --git a/test/aes-test.h b/test/aes-test.h index 141c877..f0bf6fe 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -7,6 +7,7 @@ # include "package/mine.h" #else # include "src/aes.h" +# include "src/base16.h" #endif namespace mine { @@ -270,14 +271,17 @@ static TestData RawCipherData = { TEST(AESTest, RawCipher) { - for (auto& item : RawCipherData) { - AES::ByteArray input = AES::hexStrToByteArray(PARAM(0)); - AES::Key key = static_cast(AES::hexStrToByteArray(PARAM(1))); - AES::ByteArray expected = AES::hexStrToByteArray(PARAM(2)); - AES::ByteArray output = AES::cipher(input, &key); + ByteArray input = Base16::fromString(PARAM(0)); + AES::Key key = static_cast(Base16::fromString(PARAM(1))); + ByteArray expected = Base16::fromString(PARAM(2)); + ByteArray output = AES::cipher(input, &key); ASSERT_EQ(expected, output); } +} + +TEST(AESTest, RawCipherDirect) +{ for (auto& item : RawCipherData) { std::string expected = PARAM(2); @@ -291,7 +295,7 @@ TEST(AESTest, RawSimpleCipher) { // FIPS. 197 p. 33 - AES::ByteArray input = {{ + ByteArray input = {{ 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, @@ -305,14 +309,14 @@ TEST(AESTest, RawSimpleCipher) 0x09, 0xcf, 0x4f, 0x3c }}; - AES::ByteArray expected = {{ + ByteArray expected = {{ 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb, 0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32 }}; - AES::ByteArray output = AES::cipher(input, &key); + ByteArray output = AES::cipher(input, &key); ASSERT_EQ(expected, output); } diff --git a/test/base16-test.h b/test/base16-test.h index 1991d5e..1ea277d 100644 --- a/test/base16-test.h +++ b/test/base16-test.h @@ -32,6 +32,10 @@ static TestData InvalidBase16EncodingData = { TestCase("48656C6C6F20576F726C64F"), }; +static TestData Base16FromStringData = { + TestCase("48656C6C6F", ByteArray { 0x48, 0x65, 0x6C, 0x6C, 0x6F }), +}; + TEST(Base16Test, Encode) { for (const auto& item : Base16TestData) { @@ -48,6 +52,14 @@ TEST(Base16Test, EncodeByteArray) } } +TEST(Base16Test, ConvertToByteArray) +{ + for (const auto& item : Base16FromStringData) { + ByteArray result = Base16::fromString(PARAM(0)); + ASSERT_EQ(PARAM(1), result); + } +} + TEST(Base16Test, EncodeInt) { for (const auto& item : Base16IntTestData) { @@ -75,7 +87,7 @@ TEST(Base16Test, DecodeInt) TEST(Base16Test, InvalidBase16Encoding) { for (const auto& item : InvalidBase16EncodingData) { - EXPECT_THROW(Base16::decode(PARAM(0)), std::runtime_error); + EXPECT_THROW(Base16::decode(PARAM(0)), std::invalid_argument); } } } From c684ea4f9384309f1d531c2d77f04a97b8e47096 Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 28 Aug 2017 11:02:20 +1000 Subject: [PATCH 079/148] extra warnings --- CMakeLists.txt | 2 +- src/aes.cc | 8 +++---- src/rsa.h | 58 ++++---------------------------------------------- 3 files changed, 9 insertions(+), 59 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 71087bd..280428d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,7 +52,7 @@ if (APPLE) endif() endif() -list (APPEND CMAKE_CXX_FLAGS " -Wall ") +list (APPEND CMAKE_CXX_FLAGS " -Wall -Wextra ") require_cpp11() diff --git a/src/aes.cc b/src/aes.cc index d0be933..6825289 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -80,7 +80,7 @@ const std::unordered_map> AES::kKeyParams = { void AES::printBytes(const ByteArray& b) { - for (int i = 1; i <= b.size(); ++i) { + for (std::size_t i = 1; i <= b.size(); ++i) { std::cout << "0x" << (b[i - 1] < 10 ? "0" : "") << Base16::encode(b[i - 1]) << " "; if (i % 4 == 0) { std::cout << std::endl; @@ -263,17 +263,17 @@ void AES::mixColumns(State* state) void AES::invSubBytes(State* state) { - + (void)state; } void AES::invShiftRows(State *state) { - + (void)state; } void AES::invMixColumns(State* state) { - + (void)state; } ByteArray AES::cipher(const ByteArray& input, const Key* key) diff --git a/src/rsa.h b/src/rsa.h index c000994..8485d2e 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -252,7 +252,7 @@ class BigIntegerHelper { /// /// \brief Absolutely must override this - conversion from x to single byte /// - virtual inline byte bigIntegerToByte(const BigInteger& x) const + virtual inline byte bigIntegerToByte(const BigInteger&) const { return static_cast(0); } @@ -590,59 +590,9 @@ class GenericRSA { /// \param signature Signature in hex /// \see https://tools.ietf.org/html/rfc3447#section-8.1.2 /// - bool verify(const PublicKey* publicKey, const std::string& message, - const std::string& signature) + bool verify(const PublicKey*, const std::string&, const std::string&) { - /* - // TODO: Add it to test - std::vector f = getByteArray(18537); - BigInteger s = i2osp(f, 2); - BigInteger os = os2ip(18537); // 105 - std::cout << s << std::endl; - std::cout << os << std::endl; - - std::vector f2 = getByteArray(1214841185); - BigInteger s2 = i2osp(f2, 4); - BigInteger os2 = os2ip(1214841185); // 97 - - std::cout << s2 << std::endl; - std::cout << os2 << std::endl; - - std::cout << changeBase(54735, 8) << std::endl; - - std::cout << std::hex << 16 << std::endl; - std::cout << std::oct << 72 << std::endl; - - return true; - - BigInteger sign = m_helper.hexToBigInteger(signature); - try { - BigInteger vp = createVerificationPrimitive(publicKey, sign); - - const int xlen = (publicKey->n().BitCount() + 7) >> 3; - - if (xlen < vp + 11) { - // throw std::runtime_error("Integer too large"); // Needed??! Where in RFC? - } - - std::vector em = m_helper.getByteArray(vp, xlen); - - - // todo: add check for following (as per https://tools.ietf.org/html/rfc3447#section-8.1.2) - // Note that emLen will be one less than k if modBits - 1 is - // divisible by 8 and equal to k otherwise. If I2OSP outputs - // "integer too large," output "invalid signature" and stop. - - // EMSA-PSS_VERIFY - https://tools.ietf.org/html/rfc3447#section-9.1.2 - - - - std::cout << "tt" << std::endl; - return true; - } catch (std::exception&) { - } -*/ return true; } @@ -655,7 +605,7 @@ class GenericRSA { /// \return corresponding nonnegative integer /// template - BigInteger pkcs1pad2(const T& s, int n) { + BigInteger pkcs1pad2(const T& s, std::size_t n) { if (n < s.size() + 11) { throw std::runtime_error("Message too long"); } @@ -717,7 +667,7 @@ class GenericRSA { if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { throw std::runtime_error("Incorrect padding PKCS#1"); } - int i = 2; // passed first two characters (0x0 and 0x2) test + std::size_t i = 2; // passed first two characters (0x0 and 0x2) test // lets check for the // if we hit end while still we're still with non-zeros, it's a padding error From 22063d8164548052e52cca34da35b67da1ea45c3 Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 28 Aug 2017 11:04:49 +1000 Subject: [PATCH 080/148] travis update --- .travis.yml | 1 + README.md | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index e650a53..5662762 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,7 @@ before_install: - sudo apt-get install -y libssl-dev libgtest-dev php5-cli valgrind - sudo apt-get install --only-upgrade cmake - cmake --version + - g++ --version # Crypto++ (We manually install it because we need Pem Pack as well) - wget https://raw.githubusercontent.com/muflihun/muflihun.github.io/master/downloads/cryptocpp.tar.gz - tar xf cryptocpp.tar.gz diff --git a/README.md b/README.md index a57c044..0c35a53 100644 --- a/README.md +++ b/README.md @@ -23,12 +23,12 @@ We are very careful with our implementations and have [unit tests](/test/) in pl Mine _will_ support following features: * RSA (Encrypt, Decrypt, Sign and Verify) [[RFC-3447](https://tools.ietf.org/html/rfc3447)] - * AES [[FIPS.197](http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf)] + * AES [[FIPS Pub. 197](http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf)] * ZLib (Depend upon libz, eventually implement [RFC-1950](https://tools.ietf.org/html/rfc3602)) * Base16 (Encode, Decode) * Base64 (Encode, Decode) -For _minimal_ library this is what we are aiming. +This is what we are aiming for _minimal_ crypto library. # Contribution You can only contribute by testing on various platforms and reporting the issues. We are not accepting any pull requests until first release. @@ -39,8 +39,8 @@ You can only contribute by testing on various platforms and reporting the issues Copyright 2017 Muflihun Labs https://github.com/muflihun/ -https://muflihun.com https://muflihun.github.io +https://muflihun.com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. From ce3740b5d9a4ec288c57f5d78ce923ecb533234a Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 28 Aug 2017 11:23:41 +1000 Subject: [PATCH 081/148] subbyte test --- src/aes.cc | 4 +- src/aes.h | 2 + test/aes-test.h | 449 ++++++++++++++++++++++++++---------------------- test/main.cc | 6 +- 4 files changed, 251 insertions(+), 210 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 6825289..076b5a8 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -134,7 +134,7 @@ AES::KeySchedule AES::keyExpansion(const Key* key) // to each of the four bytes to produce an output word. // // Out definition: - // It's a simple substition with kSbox for corresponding bit + // It's a simple substition with kSbox for corresponding byte // index // auto substituteWord = [](Word* w) -> Word* { @@ -252,7 +252,7 @@ void AES::mixColumns(State* state) Word column = state->at(col); // let's take example from publication, col: [212, 191, 93, 48] // t == 6 - byte t = (column[0]) ^ (column[1]) ^ (column[2]) ^ (column[3]); + byte t = column[0] ^ column[1] ^ column[2] ^ column[3]; // see Sec. 4.2.1 and Sec. 5.1.3 for more details state->at(col)[0] ^= xtime(column[0] ^ column[1]) ^ t; state->at(col)[1] ^= xtime(column[1] ^ column[2]) ^ t; diff --git a/src/aes.h b/src/aes.h index ef2ee95..1728f4b 100644 --- a/src/aes.h +++ b/src/aes.h @@ -216,6 +216,8 @@ class AES { friend class AESTest_RawCipher_Test; friend class AESTest_RawSimpleCipher_Test; + friend class AESTest_RawSimpleDecipher_Test; + friend class AESTest_SubByte_Test; friend class AESTest_FiniteFieldMultiply_Test; friend class AESTest_KeyExpansion_Test; friend class AESTest_AddRoundKey_Test; diff --git a/test/aes-test.h b/test/aes-test.h index f0bf6fe..f8934d1 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -26,51 +26,51 @@ TEST(AESTest, KeyExpansion) 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }}, AES::KeySchedule{{ - {0, {{ 0x2B, 0x7E, 0x15, 0x16 }}}, - {1, {{ 0x28, 0xAE, 0xD2, 0xA6 }}}, - {2, {{ 0xAB, 0xF7, 0x15, 0x88 }}}, - {3, {{ 0x9, 0xCF, 0x4F, 0x3C }}}, - {4, {{ 0xA0, 0xFA, 0xFE, 0x17 }}}, - {5, {{ 0x88, 0x54, 0x2C, 0xB1 }}}, - {6, {{ 0x23, 0xA3, 0x39, 0x39 }}}, - {7, {{ 0x2A, 0x6C, 0x76, 0x5 }}}, - {8, {{ 0xF2, 0xC2, 0x95, 0xF2 }}}, - {9, {{ 0x7A, 0x96, 0xB9, 0x43 }}}, - {10, {{ 0x59, 0x35, 0x80, 0x7A }}}, - {11, {{ 0x73, 0x59, 0xF6, 0x7F }}}, - {12, {{ 0x3D, 0x80, 0x47, 0x7D }}}, - {13, {{ 0x47, 0x16, 0xFE, 0x3E }}}, - {14, {{ 0x1E, 0x23, 0x7E, 0x44 }}}, - {15, {{ 0x6D, 0x7A, 0x88, 0x3B }}}, - {16, {{ 0xEF, 0x44, 0xA5, 0x41 }}}, - {17, {{ 0xA8, 0x52, 0x5B, 0x7F }}}, - {18, {{ 0xB6, 0x71, 0x25, 0x3B }}}, - {19, {{ 0xDB, 0xB, 0xAD, 0x00 }}}, - {20, {{ 0xD4, 0xD1, 0xC6, 0xF8 }}}, - {21, {{ 0x7C, 0x83, 0x9D, 0x87 }}}, - {22, {{ 0xCA, 0xF2, 0xB8, 0xBC }}}, - {23, {{ 0x11, 0xF9, 0x15, 0xBC }}}, - {24, {{ 0x6D, 0x88, 0xA3, 0x7A }}}, - {25, {{ 0x11, 0xB, 0x3E, 0xFD }}}, - {26, {{ 0xDB, 0xF9, 0x86, 0x41 }}}, - {27, {{ 0xCA, 0x00, 0x93, 0xFD }}}, - {28, {{ 0x4E, 0x54, 0xF7, 0xE }}}, - {29, {{ 0x5F, 0x5F, 0xC9, 0xF3 }}}, - {30, {{ 0x84, 0xA6, 0x4F, 0xB2 }}}, - {31, {{ 0x4E, 0xA6, 0xDC, 0x4F }}}, - {32, {{ 0xEA, 0xD2, 0x73, 0x21 }}}, - {33, {{ 0xB5, 0x8D, 0xBA, 0xD2 }}}, - {34, {{ 0x31, 0x2B, 0xF5, 0x60 }}}, - {35, {{ 0x7F, 0x8D, 0x29, 0x2F }}}, - {36, {{ 0xAC, 0x77, 0x66, 0xF3 }}}, - {37, {{ 0x19, 0xFA, 0xDC, 0x21 }}}, - {38, {{ 0x28, 0xD1, 0x29, 0x41 }}}, - {39, {{ 0x57, 0x5C, 0x00, 0x6E }}}, - {40, {{ 0xD0, 0x14, 0xF9, 0xA8 }}}, - {41, {{ 0xC9, 0xEE, 0x25, 0x89 }}}, - {42, {{ 0xE1, 0x3F, 0xC, 0xC8 }}}, - {43, {{ 0xB6, 0x63, 0xC, 0xA6 }}} - }}), + {0, {{ 0x2B, 0x7E, 0x15, 0x16 }}}, + {1, {{ 0x28, 0xAE, 0xD2, 0xA6 }}}, + {2, {{ 0xAB, 0xF7, 0x15, 0x88 }}}, + {3, {{ 0x9, 0xCF, 0x4F, 0x3C }}}, + {4, {{ 0xA0, 0xFA, 0xFE, 0x17 }}}, + {5, {{ 0x88, 0x54, 0x2C, 0xB1 }}}, + {6, {{ 0x23, 0xA3, 0x39, 0x39 }}}, + {7, {{ 0x2A, 0x6C, 0x76, 0x5 }}}, + {8, {{ 0xF2, 0xC2, 0x95, 0xF2 }}}, + {9, {{ 0x7A, 0x96, 0xB9, 0x43 }}}, + {10, {{ 0x59, 0x35, 0x80, 0x7A }}}, + {11, {{ 0x73, 0x59, 0xF6, 0x7F }}}, + {12, {{ 0x3D, 0x80, 0x47, 0x7D }}}, + {13, {{ 0x47, 0x16, 0xFE, 0x3E }}}, + {14, {{ 0x1E, 0x23, 0x7E, 0x44 }}}, + {15, {{ 0x6D, 0x7A, 0x88, 0x3B }}}, + {16, {{ 0xEF, 0x44, 0xA5, 0x41 }}}, + {17, {{ 0xA8, 0x52, 0x5B, 0x7F }}}, + {18, {{ 0xB6, 0x71, 0x25, 0x3B }}}, + {19, {{ 0xDB, 0xB, 0xAD, 0x00 }}}, + {20, {{ 0xD4, 0xD1, 0xC6, 0xF8 }}}, + {21, {{ 0x7C, 0x83, 0x9D, 0x87 }}}, + {22, {{ 0xCA, 0xF2, 0xB8, 0xBC }}}, + {23, {{ 0x11, 0xF9, 0x15, 0xBC }}}, + {24, {{ 0x6D, 0x88, 0xA3, 0x7A }}}, + {25, {{ 0x11, 0xB, 0x3E, 0xFD }}}, + {26, {{ 0xDB, 0xF9, 0x86, 0x41 }}}, + {27, {{ 0xCA, 0x00, 0x93, 0xFD }}}, + {28, {{ 0x4E, 0x54, 0xF7, 0xE }}}, + {29, {{ 0x5F, 0x5F, 0xC9, 0xF3 }}}, + {30, {{ 0x84, 0xA6, 0x4F, 0xB2 }}}, + {31, {{ 0x4E, 0xA6, 0xDC, 0x4F }}}, + {32, {{ 0xEA, 0xD2, 0x73, 0x21 }}}, + {33, {{ 0xB5, 0x8D, 0xBA, 0xD2 }}}, + {34, {{ 0x31, 0x2B, 0xF5, 0x60 }}}, + {35, {{ 0x7F, 0x8D, 0x29, 0x2F }}}, + {36, {{ 0xAC, 0x77, 0x66, 0xF3 }}}, + {37, {{ 0x19, 0xFA, 0xDC, 0x21 }}}, + {38, {{ 0x28, 0xD1, 0x29, 0x41 }}}, + {39, {{ 0x57, 0x5C, 0x00, 0x6E }}}, + {40, {{ 0xD0, 0x14, 0xF9, 0xA8 }}}, + {41, {{ 0xC9, 0xEE, 0x25, 0x89 }}}, + {42, {{ 0xE1, 0x3F, 0xC, 0xC8 }}}, + {43, {{ 0xB6, 0x63, 0xC, 0xA6 }}} + }}), TestCase("192-bit key expansion", AES::Key{{ 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, @@ -79,60 +79,60 @@ TEST(AESTest, KeyExpansion) 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b }}, AES::KeySchedule{{ - {0, {{ 0x8E, 0x73, 0xB0, 0xF7 }}}, - {1, {{ 0xDA, 0xE, 0x64, 0x52 }}}, - {2, {{ 0xC8, 0x10, 0xF3, 0x2B }}}, - {3, {{ 0x80, 0x90, 0x79, 0xE5 }}}, - {4, {{ 0x62, 0xF8, 0xEA, 0xD2 }}}, - {5, {{ 0x52, 0x2C, 0x6B, 0x7B }}}, - {6, {{ 0xFE, 0xC, 0x91, 0xF7 }}}, - {7, {{ 0x24, 0x2, 0xF5, 0xA5 }}}, - {8, {{ 0xEC, 0x12, 0x6, 0x8E }}}, - {9, {{ 0x6C, 0x82, 0x7F, 0x6B }}}, - {10, {{ 0xE, 0x7A, 0x95, 0xB9 }}}, - {11, {{ 0x5C, 0x56, 0xFE, 0xC2 }}}, - {12, {{ 0x4D, 0xB7, 0xB4, 0xBD }}}, - {13, {{ 0x69, 0xB5, 0x41, 0x18 }}}, - {14, {{ 0x85, 0xA7, 0x47, 0x96 }}}, - {15, {{ 0xE9, 0x25, 0x38, 0xFD }}}, - {16, {{ 0xE7, 0x5F, 0xAD, 0x44 }}}, - {17, {{ 0xBB, 0x9, 0x53, 0x86 }}}, - {18, {{ 0x48, 0x5A, 0xF0, 0x57 }}}, - {19, {{ 0x21, 0xEF, 0xB1, 0x4F }}}, - {20, {{ 0xA4, 0x48, 0xF6, 0xD9 }}}, - {21, {{ 0x4D, 0x6D, 0xCE, 0x24 }}}, - {22, {{ 0xAA, 0x32, 0x63, 0x60 }}}, - {23, {{ 0x11, 0x3B, 0x30, 0xE6 }}}, - {24, {{ 0xA2, 0x5E, 0x7E, 0xD5 }}}, - {25, {{ 0x83, 0xB1, 0xCF, 0x9A }}}, - {26, {{ 0x27, 0xF9, 0x39, 0x43 }}}, - {27, {{ 0x6A, 0x94, 0xF7, 0x67 }}}, - {28, {{ 0xC0, 0xA6, 0x94, 0x7 }}}, - {29, {{ 0xD1, 0x9D, 0xA4, 0xE1 }}}, - {30, {{ 0xEC, 0x17, 0x86, 0xEB }}}, - {31, {{ 0x6F, 0xA6, 0x49, 0x71 }}}, - {32, {{ 0x48, 0x5F, 0x70, 0x32 }}}, - {33, {{ 0x22, 0xCB, 0x87, 0x55 }}}, - {34, {{ 0xE2, 0x6D, 0x13, 0x52 }}}, - {35, {{ 0x33, 0xF0, 0xB7, 0xB3 }}}, - {36, {{ 0x40, 0xBE, 0xEB, 0x28 }}}, - {37, {{ 0x2F, 0x18, 0xA2, 0x59 }}}, - {38, {{ 0x67, 0x47, 0xD2, 0x6B }}}, - {39, {{ 0x45, 0x8C, 0x55, 0x3E }}}, - {40, {{ 0xA7, 0xE1, 0x46, 0x6C }}}, - {41, {{ 0x94, 0x11, 0xF1, 0xDF }}}, - {42, {{ 0x82, 0x1F, 0x75, 0xA }}}, - {43, {{ 0xAD, 0x7, 0xD7, 0x53 }}}, - {44, {{ 0xCA, 0x40, 0x5, 0x38 }}}, - {45, {{ 0x8F, 0xCC, 0x50, 0x6 }}}, - {46, {{ 0x28, 0x2D, 0x16, 0x6A }}}, - {47, {{ 0xBC, 0x3C, 0xE7, 0xB5 }}}, - {48, {{ 0xE9, 0x8B, 0xA0, 0x6F }}}, - {49, {{ 0x44, 0x8C, 0x77, 0x3C }}}, - {50, {{ 0x8E, 0xCC, 0x72, 0x4 }}}, - {51, {{ 0x1, 0x0, 0x22, 0x2 }}}, - - }}), + {0, {{ 0x8E, 0x73, 0xB0, 0xF7 }}}, + {1, {{ 0xDA, 0xE, 0x64, 0x52 }}}, + {2, {{ 0xC8, 0x10, 0xF3, 0x2B }}}, + {3, {{ 0x80, 0x90, 0x79, 0xE5 }}}, + {4, {{ 0x62, 0xF8, 0xEA, 0xD2 }}}, + {5, {{ 0x52, 0x2C, 0x6B, 0x7B }}}, + {6, {{ 0xFE, 0xC, 0x91, 0xF7 }}}, + {7, {{ 0x24, 0x2, 0xF5, 0xA5 }}}, + {8, {{ 0xEC, 0x12, 0x6, 0x8E }}}, + {9, {{ 0x6C, 0x82, 0x7F, 0x6B }}}, + {10, {{ 0xE, 0x7A, 0x95, 0xB9 }}}, + {11, {{ 0x5C, 0x56, 0xFE, 0xC2 }}}, + {12, {{ 0x4D, 0xB7, 0xB4, 0xBD }}}, + {13, {{ 0x69, 0xB5, 0x41, 0x18 }}}, + {14, {{ 0x85, 0xA7, 0x47, 0x96 }}}, + {15, {{ 0xE9, 0x25, 0x38, 0xFD }}}, + {16, {{ 0xE7, 0x5F, 0xAD, 0x44 }}}, + {17, {{ 0xBB, 0x9, 0x53, 0x86 }}}, + {18, {{ 0x48, 0x5A, 0xF0, 0x57 }}}, + {19, {{ 0x21, 0xEF, 0xB1, 0x4F }}}, + {20, {{ 0xA4, 0x48, 0xF6, 0xD9 }}}, + {21, {{ 0x4D, 0x6D, 0xCE, 0x24 }}}, + {22, {{ 0xAA, 0x32, 0x63, 0x60 }}}, + {23, {{ 0x11, 0x3B, 0x30, 0xE6 }}}, + {24, {{ 0xA2, 0x5E, 0x7E, 0xD5 }}}, + {25, {{ 0x83, 0xB1, 0xCF, 0x9A }}}, + {26, {{ 0x27, 0xF9, 0x39, 0x43 }}}, + {27, {{ 0x6A, 0x94, 0xF7, 0x67 }}}, + {28, {{ 0xC0, 0xA6, 0x94, 0x7 }}}, + {29, {{ 0xD1, 0x9D, 0xA4, 0xE1 }}}, + {30, {{ 0xEC, 0x17, 0x86, 0xEB }}}, + {31, {{ 0x6F, 0xA6, 0x49, 0x71 }}}, + {32, {{ 0x48, 0x5F, 0x70, 0x32 }}}, + {33, {{ 0x22, 0xCB, 0x87, 0x55 }}}, + {34, {{ 0xE2, 0x6D, 0x13, 0x52 }}}, + {35, {{ 0x33, 0xF0, 0xB7, 0xB3 }}}, + {36, {{ 0x40, 0xBE, 0xEB, 0x28 }}}, + {37, {{ 0x2F, 0x18, 0xA2, 0x59 }}}, + {38, {{ 0x67, 0x47, 0xD2, 0x6B }}}, + {39, {{ 0x45, 0x8C, 0x55, 0x3E }}}, + {40, {{ 0xA7, 0xE1, 0x46, 0x6C }}}, + {41, {{ 0x94, 0x11, 0xF1, 0xDF }}}, + {42, {{ 0x82, 0x1F, 0x75, 0xA }}}, + {43, {{ 0xAD, 0x7, 0xD7, 0x53 }}}, + {44, {{ 0xCA, 0x40, 0x5, 0x38 }}}, + {45, {{ 0x8F, 0xCC, 0x50, 0x6 }}}, + {46, {{ 0x28, 0x2D, 0x16, 0x6A }}}, + {47, {{ 0xBC, 0x3C, 0xE7, 0xB5 }}}, + {48, {{ 0xE9, 0x8B, 0xA0, 0x6F }}}, + {49, {{ 0x44, 0x8C, 0x77, 0x3C }}}, + {50, {{ 0x8E, 0xCC, 0x72, 0x4 }}}, + {51, {{ 0x1, 0x0, 0x22, 0x2 }}}, + + }}), TestCase("256-bit key expansion", AES::Key{{ 0x60, 0x3d, 0xeb, 0x10, @@ -144,67 +144,67 @@ TEST(AESTest, KeyExpansion) 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 }}, AES::KeySchedule{{ - {0, {{ 0x60, 0x3D, 0xEB, 0x10 }}}, - {1, {{ 0x15, 0xCA, 0x71, 0xBE }}}, - {2, {{ 0x2B, 0x73, 0xAE, 0xF0 }}}, - {3, {{ 0x85, 0x7D, 0x77, 0x81 }}}, - {4, {{ 0x1F, 0x35, 0x2C, 0x7 }}}, - {5, {{ 0x3B, 0x61, 0x8, 0xD7 }}}, - {6, {{ 0x2D, 0x98, 0x10, 0xA3 }}}, - {7, {{ 0x9, 0x14, 0xDF, 0xF4 }}}, - {8, {{ 0x9B, 0xA3, 0x54, 0x11 }}}, - {9, {{ 0x8E, 0x69, 0x25, 0xAF }}}, - {10, {{ 0xA5, 0x1A, 0x8B, 0x5F }}}, - {11, {{ 0x20, 0x67, 0xFC, 0xDE }}}, - {12, {{ 0xA8, 0xB0, 0x9C, 0x1A }}}, - {13, {{ 0x93, 0xD1, 0x94, 0xCD }}}, - {14, {{ 0xBE, 0x49, 0x84, 0x6E }}}, - {15, {{ 0xB7, 0x5D, 0x5B, 0x9A }}}, - {16, {{ 0xD5, 0x9A, 0xEC, 0xB8 }}}, - {17, {{ 0x5B, 0xF3, 0xC9, 0x17 }}}, - {18, {{ 0xFE, 0xE9, 0x42, 0x48 }}}, - {19, {{ 0xDE, 0x8E, 0xBE, 0x96 }}}, - {20, {{ 0xB5, 0xA9, 0x32, 0x8A }}}, - {21, {{ 0x26, 0x78, 0xA6, 0x47 }}}, - {22, {{ 0x98, 0x31, 0x22, 0x29 }}}, - {23, {{ 0x2F, 0x6C, 0x79, 0xB3 }}}, - {24, {{ 0x81, 0x2C, 0x81, 0xAD }}}, - {25, {{ 0xDA, 0xDF, 0x48, 0xBA }}}, - {26, {{ 0x24, 0x36, 0xA, 0xF2 }}}, - {27, {{ 0xFA, 0xB8, 0xB4, 0x64 }}}, - {28, {{ 0x98, 0xC5, 0xBF, 0xC9 }}}, - {29, {{ 0xBE, 0xBD, 0x19, 0x8E }}}, - {30, {{ 0x26, 0x8C, 0x3B, 0xA7 }}}, - {31, {{ 0x9, 0xE0, 0x42, 0x14 }}}, - {32, {{ 0x68, 0x0, 0x7B, 0xAC }}}, - {33, {{ 0xB2, 0xDF, 0x33, 0x16 }}}, - {34, {{ 0x96, 0xE9, 0x39, 0xE4 }}}, - {35, {{ 0x6C, 0x51, 0x8D, 0x80 }}}, - {36, {{ 0xC8, 0x14, 0xE2, 0x4 }}}, - {37, {{ 0x76, 0xA9, 0xFB, 0x8A }}}, - {38, {{ 0x50, 0x25, 0xC0, 0x2D }}}, - {39, {{ 0x59, 0xC5, 0x82, 0x39 }}}, - {40, {{ 0xDE, 0x13, 0x69, 0x67 }}}, - {41, {{ 0x6C, 0xCC, 0x5A, 0x71 }}}, - {42, {{ 0xFA, 0x25, 0x63, 0x95 }}}, - {43, {{ 0x96, 0x74, 0xEE, 0x15 }}}, - {44, {{ 0x58, 0x86, 0xCA, 0x5D }}}, - {45, {{ 0x2E, 0x2F, 0x31, 0xD7 }}}, - {46, {{ 0x7E, 0xA, 0xF1, 0xFA }}}, - {47, {{ 0x27, 0xCF, 0x73, 0xC3 }}}, - {48, {{ 0x74, 0x9C, 0x47, 0xAB }}}, - {49, {{ 0x18, 0x50, 0x1D, 0xDA }}}, - {50, {{ 0xE2, 0x75, 0x7E, 0x4F }}}, - {51, {{ 0x74, 0x1, 0x90, 0x5A }}}, - {52, {{ 0xCA, 0xFA, 0xAA, 0xE3 }}}, - {53, {{ 0xE4, 0xD5, 0x9B, 0x34 }}}, - {54, {{ 0x9A, 0xDF, 0x6A, 0xCE }}}, - {55, {{ 0xBD, 0x10, 0x19, 0xD }}}, - {56, {{ 0xFE, 0x48, 0x90, 0xD1 }}}, - {57, {{ 0xE6, 0x18, 0x8D, 0xB }}}, - {58, {{ 0x4, 0x6D, 0xF3, 0x44 }}}, - {59, {{ 0x70, 0x6C, 0x63, 0x1E }}}, - }}), + {0, {{ 0x60, 0x3D, 0xEB, 0x10 }}}, + {1, {{ 0x15, 0xCA, 0x71, 0xBE }}}, + {2, {{ 0x2B, 0x73, 0xAE, 0xF0 }}}, + {3, {{ 0x85, 0x7D, 0x77, 0x81 }}}, + {4, {{ 0x1F, 0x35, 0x2C, 0x7 }}}, + {5, {{ 0x3B, 0x61, 0x8, 0xD7 }}}, + {6, {{ 0x2D, 0x98, 0x10, 0xA3 }}}, + {7, {{ 0x9, 0x14, 0xDF, 0xF4 }}}, + {8, {{ 0x9B, 0xA3, 0x54, 0x11 }}}, + {9, {{ 0x8E, 0x69, 0x25, 0xAF }}}, + {10, {{ 0xA5, 0x1A, 0x8B, 0x5F }}}, + {11, {{ 0x20, 0x67, 0xFC, 0xDE }}}, + {12, {{ 0xA8, 0xB0, 0x9C, 0x1A }}}, + {13, {{ 0x93, 0xD1, 0x94, 0xCD }}}, + {14, {{ 0xBE, 0x49, 0x84, 0x6E }}}, + {15, {{ 0xB7, 0x5D, 0x5B, 0x9A }}}, + {16, {{ 0xD5, 0x9A, 0xEC, 0xB8 }}}, + {17, {{ 0x5B, 0xF3, 0xC9, 0x17 }}}, + {18, {{ 0xFE, 0xE9, 0x42, 0x48 }}}, + {19, {{ 0xDE, 0x8E, 0xBE, 0x96 }}}, + {20, {{ 0xB5, 0xA9, 0x32, 0x8A }}}, + {21, {{ 0x26, 0x78, 0xA6, 0x47 }}}, + {22, {{ 0x98, 0x31, 0x22, 0x29 }}}, + {23, {{ 0x2F, 0x6C, 0x79, 0xB3 }}}, + {24, {{ 0x81, 0x2C, 0x81, 0xAD }}}, + {25, {{ 0xDA, 0xDF, 0x48, 0xBA }}}, + {26, {{ 0x24, 0x36, 0xA, 0xF2 }}}, + {27, {{ 0xFA, 0xB8, 0xB4, 0x64 }}}, + {28, {{ 0x98, 0xC5, 0xBF, 0xC9 }}}, + {29, {{ 0xBE, 0xBD, 0x19, 0x8E }}}, + {30, {{ 0x26, 0x8C, 0x3B, 0xA7 }}}, + {31, {{ 0x9, 0xE0, 0x42, 0x14 }}}, + {32, {{ 0x68, 0x0, 0x7B, 0xAC }}}, + {33, {{ 0xB2, 0xDF, 0x33, 0x16 }}}, + {34, {{ 0x96, 0xE9, 0x39, 0xE4 }}}, + {35, {{ 0x6C, 0x51, 0x8D, 0x80 }}}, + {36, {{ 0xC8, 0x14, 0xE2, 0x4 }}}, + {37, {{ 0x76, 0xA9, 0xFB, 0x8A }}}, + {38, {{ 0x50, 0x25, 0xC0, 0x2D }}}, + {39, {{ 0x59, 0xC5, 0x82, 0x39 }}}, + {40, {{ 0xDE, 0x13, 0x69, 0x67 }}}, + {41, {{ 0x6C, 0xCC, 0x5A, 0x71 }}}, + {42, {{ 0xFA, 0x25, 0x63, 0x95 }}}, + {43, {{ 0x96, 0x74, 0xEE, 0x15 }}}, + {44, {{ 0x58, 0x86, 0xCA, 0x5D }}}, + {45, {{ 0x2E, 0x2F, 0x31, 0xD7 }}}, + {46, {{ 0x7E, 0xA, 0xF1, 0xFA }}}, + {47, {{ 0x27, 0xCF, 0x73, 0xC3 }}}, + {48, {{ 0x74, 0x9C, 0x47, 0xAB }}}, + {49, {{ 0x18, 0x50, 0x1D, 0xDA }}}, + {50, {{ 0xE2, 0x75, 0x7E, 0x4F }}}, + {51, {{ 0x74, 0x1, 0x90, 0x5A }}}, + {52, {{ 0xCA, 0xFA, 0xAA, 0xE3 }}}, + {53, {{ 0xE4, 0xD5, 0x9B, 0x34 }}}, + {54, {{ 0x9A, 0xDF, 0x6A, 0xCE }}}, + {55, {{ 0xBD, 0x10, 0x19, 0xD }}}, + {56, {{ 0xFE, 0x48, 0x90, 0xD1 }}}, + {57, {{ 0xE6, 0x18, 0x8D, 0xB }}}, + {58, {{ 0x4, 0x6D, 0xF3, 0x44 }}}, + {59, {{ 0x70, 0x6C, 0x63, 0x1E }}}, + }}), }; for (auto& item : KeyExpansionTestData) { @@ -214,6 +214,33 @@ TEST(AESTest, KeyExpansion) ASSERT_EQ(expected, keys); } } + +TEST(AESTest, SubByte) +{ + // + static TestData SubByteTestData = { + TestCase(AES::State {{ + {{ 0x04, 0x66, 0x81, 0xe5 }}, + {{ 0xe0, 0xcb, 0x19, 0x9a }}, + {{ 0x48, 0xf8, 0xd3, 0x7a }}, + {{ 0x28, 0x06, 0x26, 0x4c }}, + }}, AES::State {{ + {{ 0xf2, 0x33, 0x0c, 0xd9 }}, + {{ 0xe1, 0x1f, 0xd4, 0xb8 }}, + {{ 0x52, 0x41, 0x66, 0xda }}, + {{ 0x34, 0x6f, 0xf7, 0x29 }}, + }} + ), + }; + + for (auto& item : SubByteTestData) { + AES::State state = PARAM(0); + AES::State expected = PARAM(1); + AES::subBytes(&state); + ASSERT_EQ(expected, state); + } +} + TEST(AESTest, AddRoundKey) { // Simple test with test data from FIPS.197 p.33 and 34 @@ -226,32 +253,32 @@ TEST(AESTest, AddRoundKey) }}; AES::KeySchedule schedule = AES::keyExpansion(&key); AES::State state = {{ - {{ 0x04, 0x66, 0x81, 0xe5 }}, - {{ 0xe0, 0xcb, 0x19, 0x9a }}, - {{ 0x48, 0xf8, 0xd3, 0x7a }}, - {{ 0x28, 0x06, 0x26, 0x4c }}, - }}; + {{ 0x04, 0x66, 0x81, 0xe5 }}, + {{ 0xe0, 0xcb, 0x19, 0x9a }}, + {{ 0x48, 0xf8, 0xd3, 0x7a }}, + {{ 0x28, 0x06, 0x26, 0x4c }}, + }}; AES::State expected2 = {{ - {{ 0xa4, 0x9c, 0x7f, 0xf2 }}, - {{ 0x68, 0x9f, 0x35, 0x2b }}, - {{ 0x6b, 0x5b, 0xea, 0x43 }}, - {{ 0x02, 0x6a, 0x50, 0x49 }}, - }}; + {{ 0xa4, 0x9c, 0x7f, 0xf2 }}, + {{ 0x68, 0x9f, 0x35, 0x2b }}, + {{ 0x6b, 0x5b, 0xea, 0x43 }}, + {{ 0x02, 0x6a, 0x50, 0x49 }}, + }}; AES::State expected7 = {{ - {{ 0x26, 0x0e, 0x2e, 0x17 }}, - {{ 0x3d, 0x41, 0xb7, 0x7d }}, - {{ 0xe8, 0x64, 0x72, 0xa9 }}, - {{ 0xfd, 0xd2, 0x8b, 0x25 }}, - }}; + {{ 0x26, 0x0e, 0x2e, 0x17 }}, + {{ 0x3d, 0x41, 0xb7, 0x7d }}, + {{ 0xe8, 0x64, 0x72, 0xa9 }}, + {{ 0xfd, 0xd2, 0x8b, 0x25 }}, + }}; AES::addRoundKey(&state, &schedule, 1); ASSERT_EQ(expected2, state); state = {{ - {{ 0x4b, 0x86, 0x8d, 0x6d }}, - {{ 0x2c, 0x4a, 0x89, 0x80 }}, - {{ 0x33, 0x9d, 0xf4, 0xe8 }}, - {{ 0x37, 0xd2, 0x18, 0xd8 }}, - }}; + {{ 0x4b, 0x86, 0x8d, 0x6d }}, + {{ 0x2c, 0x4a, 0x89, 0x80 }}, + {{ 0x33, 0x9d, 0xf4, 0xe8 }}, + {{ 0x37, 0xd2, 0x18, 0xd8 }}, + }}; AES::addRoundKey(&state, &schedule, 6); ASSERT_EQ(expected7, state); } @@ -296,11 +323,11 @@ TEST(AESTest, RawSimpleCipher) // FIPS. 197 p. 33 ByteArray input = {{ - 0x32, 0x43, 0xf6, 0xa8, - 0x88, 0x5a, 0x30, 0x8d, - 0x31, 0x31, 0x98, 0xa2, - 0xe0, 0x37, 0x07, 0x34 - }}; + 0x32, 0x43, 0xf6, 0xa8, + 0x88, 0x5a, 0x30, 0x8d, + 0x31, 0x31, 0x98, 0xa2, + 0xe0, 0x37, 0x07, 0x34 + }}; AES::Key key = {{ 0x2b, 0x7e, 0x15, 0x16, @@ -310,32 +337,44 @@ TEST(AESTest, RawSimpleCipher) }}; ByteArray expected = {{ - 0x39, 0x25, 0x84, 0x1d, - 0x02, 0xdc, 0x09, 0xfb, - 0xdc, 0x11, 0x85, 0x97, - 0x19, 0x6a, 0x0b, 0x32 - }}; + 0x39, 0x25, 0x84, 0x1d, + 0x02, 0xdc, 0x09, 0xfb, + 0xdc, 0x11, 0x85, 0x97, + 0x19, 0x6a, 0x0b, 0x32 + }}; ByteArray output = AES::cipher(input, &key); ASSERT_EQ(expected, output); } -TEST(AESTest, DevAid) +TEST(AESTest, RawSimpleDecipher) { -/* - std::string cip = AES::cipher("this is a test!!", {{ - 0x2b, 0x7e, 0x15, 0x16, - 0x28, 0xae, 0xd2, 0xa6, - 0xab, 0xf7, 0x15, 0x88, - 0x09, 0xcf, 0x4f, 0x3c - }}); - // key: 2b7e151628aed2a6abf7158809cf4f3c - // - // inp: this is a test!! - // cip: 4F0A21D2E75B7832246D45109258B75A - // inp: this is a test - // cip: 85D87A79E77CE29EE9374E235DC8FA7E - std::cout << "AES Cipher: " << cip << std::endl;*/ + + // FIPS. 197 p. 33 + ByteArray input = {{ + 0x39, 0x25, 0x84, 0x1d, + 0x02, 0xdc, 0x09, 0xfb, + 0xdc, 0x11, 0x85, 0x97, + 0x19, 0x6a, 0x0b, 0x32 + }}; + + AES::Key key = {{ + 0x2b, 0x7e, 0x15, 0x16, + 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, + 0x09, 0xcf, 0x4f, 0x3c + }}; + + ByteArray expected = {{ + + 0x32, 0x43, 0xf6, 0xa8, + 0x88, 0x5a, 0x30, 0x8d, + 0x31, 0x31, 0x98, 0xa2, + 0xe0, 0x37, 0x07, 0x34 + }}; + + ByteArray output = AES::decipher(input, &key); + //ASSERT_EQ(expected, output); } } diff --git a/test/main.cc b/test/main.cc index 80b4f4e..c159e9e 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,9 +1,9 @@ #include "test.h" -#include "base16-test.h" -#include "base64-test.h" +//#include "base16-test.h" +//#include "base64-test.h" #include "aes-test.h" #include "zlib-test.h" -#include "rsa-test.h" +//#include "rsa-test.h" INITIALIZE_EASYLOGGINGPP From af7bb9ac8aeaa331cdb14fc47168cb32f3096114 Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 28 Aug 2017 11:50:08 +1000 Subject: [PATCH 082/148] comments and more tests --- src/aes.cc | 130 +++++++++++++++++++++++++++++------ src/aes.h | 6 +- test/aes-test.h | 178 +++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 270 insertions(+), 44 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 076b5a8..5a709ff 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -194,6 +194,24 @@ AES::KeySchedule AES::keyExpansion(const Key* key) return words; } +/// +/// Adding round key is simply a xor operation on +/// corresponding key for the round +/// which is generated during key expansion +/// +/// Let's say we have state and a key +/// +/// [ df c3 e2 9c ] [ ef d4 49 11 ] +/// | 0f ad 1f ca | | 1f ad ac fa | +/// | 0c 9d 8d fa | ^ | cc 9e 15 dd | +/// [ fe ef cc b2 ] [ fe ea 02 dc ] +/// +/// +/// [ df^ef c3^d4 e2^49 9c^11 ] +/// | 0f^1f ad^ad 1f^ac ca^fa | +/// | 0c^cc 9d^9e 8d^15 fa^dd | +/// [ fe^fe ef^ea cc^02 b2^dc ] +/// void AES::addRoundKey(State* state, const KeySchedule* keySchedule, int round) { for (std::size_t i = 0; i < kNb; ++i) { @@ -203,6 +221,11 @@ void AES::addRoundKey(State* state, const KeySchedule* keySchedule, int round) } } +/// +/// Simple substition for the byte +/// from sbox - i.e, for 0x04 we will replace with the +/// byte at index 0x04 => 0xf2 +/// void AES::subBytes(State* state) { for (std::size_t i = 0; i < kNb; ++i) { @@ -212,6 +235,43 @@ void AES::subBytes(State* state) } } +/// +/// A simple substition of bytes using kSBoxInverse +/// +void AES::invSubBytes(State* state) +{ + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + state->at(i)[j] = kSBoxInverse[state->at(i)[j]]; + } + } +} + +/// +/// Shifting rows is beautifully explained by diagram +/// that helped in implementation as well +/// +/// Let's say we have state +/// +/// [ df c3 e2 9c ] +/// | 0f ad 1f ca | +/// | 0c 9d 8d fa | +/// [ fe ef cc b2 ] +/// +/// shifting means +/// +/// [ df c3 e2 9c ] +/// 0f | ad 1f ca | +/// 0c 9d | 8d fa | +/// fe ef cc [ b2 ] +/// +/// and filling the spaces with shifted rows +/// +/// [ df c3 e2 9c ] +/// | ad 1f ca 0f | +/// | 8d fa 0c 9d | +/// [ b2 fe ef cc ] +/// void AES::shiftRows(State *state) { // row 1 @@ -229,6 +289,56 @@ void AES::shiftRows(State *state) std::swap(state->at(0)[3], state->at(2)[3]); } +/// +/// This is reverse of shift rows operation +/// +/// Let's say we have state +/// +/// [ df c3 e2 9c ] +/// | ad 1f ca 0f | +/// | 8d fa 0c 9d | +/// [ b2 fe ef cc ] +/// +/// shifting means +/// +/// [ df c3 e2 9c ] +/// | ad 1f ca | 0f +/// | 8d fa | 0c 9d +/// [ b2 | fe ef cc +/// +/// and filling the spaces with shifted rows +/// +/// [ df c3 e2 9c ] +/// | 0f ad 1f ca | +/// | 0c 9d 8d fa | +/// [ fe ef cc b2 ] +/// +void AES::invShiftRows(State *state) +{ + // row 1 + std::swap(state->at(0)[1], state->at(3)[1]); + std::swap(state->at(0)[1], state->at(1)[1]); + std::swap(state->at(1)[1], state->at(2)[1]); + + // row 2 + std::swap(state->at(0)[2], state->at(2)[2]); + std::swap(state->at(1)[2], state->at(3)[2]); + + // row 3 + std::swap(state->at(0)[3], state->at(1)[3]); + std::swap(state->at(2)[3], state->at(3)[3]); + std::swap(state->at(0)[3], state->at(2)[3]); +} + +/// +/// multiplies in GF(2^8) field selected column from state +/// with constant matrix defined by publication +/// +/// [ 02 03 01 01 ] +/// | 01 02 03 01 | +/// | 01 01 02 03 | +/// [ 03 01 01 02 ] +/// void AES::mixColumns(State* state) { // Finds the product of {02} and the argument to xtime modulo {1b} @@ -240,15 +350,7 @@ void AES::mixColumns(State* state) for (int col = 0; col < 4; ++col) { - // - // multiplies in GF(2^8) field selected column from state - // with constant matrix defined by publication - // - // [ 02 03 01 01 ] - // | 01 02 03 01 | - // | 01 01 02 03 | - // [ 03 01 01 02 ] - // + Word column = state->at(col); // let's take example from publication, col: [212, 191, 93, 48] // t == 6 @@ -261,16 +363,6 @@ void AES::mixColumns(State* state) } } -void AES::invSubBytes(State* state) -{ - (void)state; -} - -void AES::invShiftRows(State *state) -{ - (void)state; -} - void AES::invMixColumns(State* state) { (void)state; diff --git a/src/aes.h b/src/aes.h index 1728f4b..bddc495 100644 --- a/src/aes.h +++ b/src/aes.h @@ -218,7 +218,11 @@ class AES { friend class AESTest_RawSimpleCipher_Test; friend class AESTest_RawSimpleDecipher_Test; friend class AESTest_SubByte_Test; - friend class AESTest_FiniteFieldMultiply_Test; + friend class AESTest_InvSubByte_Test; + friend class AESTest_ShiftRows_Test; + friend class AESTest_InvShiftRows_Test; + friend class AESTest_MixColumns_Test; + friend class AESTest_InvMixColumns_Test; friend class AESTest_KeyExpansion_Test; friend class AESTest_AddRoundKey_Test; }; diff --git a/test/aes-test.h b/test/aes-test.h index f8934d1..b629f37 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -220,15 +220,15 @@ TEST(AESTest, SubByte) // static TestData SubByteTestData = { TestCase(AES::State {{ - {{ 0x04, 0x66, 0x81, 0xe5 }}, - {{ 0xe0, 0xcb, 0x19, 0x9a }}, - {{ 0x48, 0xf8, 0xd3, 0x7a }}, - {{ 0x28, 0x06, 0x26, 0x4c }}, + {{ 0x04, 0x66, 0x81, 0xe5 }}, // c0 + {{ 0xe0, 0xcb, 0x19, 0x9a }}, // c1 + {{ 0x48, 0xf8, 0xd3, 0x7a }}, // c2 + {{ 0x28, 0x06, 0x26, 0x4c }}, // c3 }}, AES::State {{ - {{ 0xf2, 0x33, 0x0c, 0xd9 }}, - {{ 0xe1, 0x1f, 0xd4, 0xb8 }}, - {{ 0x52, 0x41, 0x66, 0xda }}, - {{ 0x34, 0x6f, 0xf7, 0x29 }}, + {{ 0xf2, 0x33, 0x0c, 0xd9 }}, // c0 + {{ 0xe1, 0x1f, 0xd4, 0xb8 }}, // c1 + {{ 0x52, 0x41, 0x66, 0xda }}, // c2 + {{ 0x34, 0x6f, 0xf7, 0x29 }}, // c3 }} ), }; @@ -241,6 +241,136 @@ TEST(AESTest, SubByte) } } +TEST(AESTest, InvSubByte) +{ + // + static TestData InvSubByteTestData = { + TestCase(AES::State {{ + {{ 0xf2, 0x33, 0x0c, 0xd9 }}, // c0 + {{ 0xe1, 0x1f, 0xd4, 0xb8 }}, // c1 + {{ 0x52, 0x41, 0x66, 0xda }}, // c2 + {{ 0x34, 0x6f, 0xf7, 0x29 }}, // c3 + }}, AES::State {{ + {{ 0x04, 0x66, 0x81, 0xe5 }}, // c0 + {{ 0xe0, 0xcb, 0x19, 0x9a }}, // c1 + {{ 0x48, 0xf8, 0xd3, 0x7a }}, // c2 + {{ 0x28, 0x06, 0x26, 0x4c }}, // c3 + }} + ), + }; + + for (auto& item : InvSubByteTestData) { + AES::State state = PARAM(0); + AES::State expected = PARAM(1); + AES::invSubBytes(&state); + ASSERT_EQ(expected, state); + } +} + +TEST(AESTest, ShiftRows) +{ + // + static TestData ShiftRowsTestData = { + TestCase(AES::State {{ + {{ 0x04, 0x66, 0x81, 0xe5 }}, // c0 + {{ 0xe0, 0xcb, 0x19, 0x9a }}, // c1 + {{ 0x48, 0xf8, 0xd3, 0x7a }}, // c2 + {{ 0x28, 0x06, 0x26, 0x4c }}, // c3 + }}, AES::State {{ + {{ 0x04, 0xcb, 0xd3, 0x4c }}, // c0 + {{ 0xe0, 0xf8, 0x26, 0xe5 }}, // c1 + {{ 0x48, 0x06, 0x81, 0x9a }}, // c2 + {{ 0x28, 0x66, 0x19, 0x7a }}, // c3 + }} + ), + }; + + for (auto& item : ShiftRowsTestData) { + AES::State state = PARAM(0); + AES::State expected = PARAM(1); + AES::shiftRows(&state); + ASSERT_EQ(expected, state); + } +} + +TEST(AESTest, InvShiftRows) +{ + // + static TestData InvShiftRowsTestData = { + TestCase(AES::State {{ + {{ 0x04, 0xcb, 0xd3, 0x4c }}, // c0 + {{ 0xe0, 0xf8, 0x26, 0xe5 }}, // c1 + {{ 0x48, 0x06, 0x81, 0x9a }}, // c2 + {{ 0x28, 0x66, 0x19, 0x7a }}, // c3 + }}, AES::State {{ + {{ 0x04, 0x66, 0x81, 0xe5 }}, // c0 + {{ 0xe0, 0xcb, 0x19, 0x9a }}, // c1 + {{ 0x48, 0xf8, 0xd3, 0x7a }}, // c2 + {{ 0x28, 0x06, 0x26, 0x4c }}, // c3 + }} + ), + }; + + for (auto& item : InvShiftRowsTestData) { + AES::State state = PARAM(0); + AES::State expected = PARAM(1); + AES::invShiftRows(&state); + ASSERT_EQ(expected, state); + } +} + +TEST(AESTest, MixColumns) +{ + // + static TestData MixColumnsTestData = { + TestCase(AES::State {{ + {{ 0x04, 0x66, 0x81, 0xe5 }}, // c0 + {{ 0xe0, 0xcb, 0x19, 0x9a }}, // c1 + {{ 0x48, 0xf8, 0xd3, 0x7a }}, // c2 + {{ 0x28, 0x06, 0x26, 0x4c }}, // c3 + }}, AES::State {{ + {{ 0xc6, 0xb5, 0x4f, 0x3a }}, // c0 + {{ 0x1e, 0xdc, 0xac, 0xc6 }}, // c1 + {{ 0x2a, 0xb7, 0x83, 0x07 }}, // c2 + {{ 0x30, 0x02, 0xb6, 0xc0 }}, // c3 + }} + ), + }; + + for (auto& item : MixColumnsTestData) { + AES::State state = PARAM(0); + AES::State expected = PARAM(1); + AES::mixColumns(&state); + ASSERT_EQ(expected, state); + } +} + +TEST(AESTest, InvMixColumns) +{ + // + static TestData InvMixColumnsTestData = { + TestCase(AES::State {{ + {{ 0xc6, 0xb5, 0x4f, 0x3a }}, // c0 + {{ 0x1e, 0xdc, 0xac, 0xc6 }}, // c1 + {{ 0x2a, 0xb7, 0x83, 0x07 }}, // c2 + {{ 0x30, 0x02, 0xb6, 0xc0 }}, // c3 + }}, AES::State {{ + {{ 0x04, 0x66, 0x81, 0xe5 }}, // c0 + {{ 0xe0, 0xcb, 0x19, 0x9a }}, // c1 + {{ 0x48, 0xf8, 0xd3, 0x7a }}, // c2 + {{ 0x28, 0x06, 0x26, 0x4c }}, // c3 + }} + ), + }; + + for (auto& item : InvMixColumnsTestData) { + AES::State state = PARAM(0); + AES::State expected = PARAM(1); + AES::invMixColumns(&state); + ASSERT_EQ(expected, state); + } +} + TEST(AESTest, AddRoundKey) { // Simple test with test data from FIPS.197 p.33 and 34 @@ -253,31 +383,31 @@ TEST(AESTest, AddRoundKey) }}; AES::KeySchedule schedule = AES::keyExpansion(&key); AES::State state = {{ - {{ 0x04, 0x66, 0x81, 0xe5 }}, - {{ 0xe0, 0xcb, 0x19, 0x9a }}, - {{ 0x48, 0xf8, 0xd3, 0x7a }}, - {{ 0x28, 0x06, 0x26, 0x4c }}, + {{ 0x04, 0x66, 0x81, 0xe5 }}, // c0 + {{ 0xe0, 0xcb, 0x19, 0x9a }}, // c1 + {{ 0x48, 0xf8, 0xd3, 0x7a }}, // c2 + {{ 0x28, 0x06, 0x26, 0x4c }}, // c3 }}; AES::State expected2 = {{ - {{ 0xa4, 0x9c, 0x7f, 0xf2 }}, - {{ 0x68, 0x9f, 0x35, 0x2b }}, - {{ 0x6b, 0x5b, 0xea, 0x43 }}, - {{ 0x02, 0x6a, 0x50, 0x49 }}, + {{ 0xa4, 0x9c, 0x7f, 0xf2 }}, // c0 + {{ 0x68, 0x9f, 0x35, 0x2b }}, // c1 + {{ 0x6b, 0x5b, 0xea, 0x43 }}, // c2 + {{ 0x02, 0x6a, 0x50, 0x49 }}, // c3 }}; AES::State expected7 = {{ - {{ 0x26, 0x0e, 0x2e, 0x17 }}, - {{ 0x3d, 0x41, 0xb7, 0x7d }}, - {{ 0xe8, 0x64, 0x72, 0xa9 }}, - {{ 0xfd, 0xd2, 0x8b, 0x25 }}, + {{ 0x26, 0x0e, 0x2e, 0x17 }}, // c0 + {{ 0x3d, 0x41, 0xb7, 0x7d }}, // c1 + {{ 0xe8, 0x64, 0x72, 0xa9 }}, // c2 + {{ 0xfd, 0xd2, 0x8b, 0x25 }}, // c3 }}; AES::addRoundKey(&state, &schedule, 1); ASSERT_EQ(expected2, state); state = {{ - {{ 0x4b, 0x86, 0x8d, 0x6d }}, - {{ 0x2c, 0x4a, 0x89, 0x80 }}, - {{ 0x33, 0x9d, 0xf4, 0xe8 }}, - {{ 0x37, 0xd2, 0x18, 0xd8 }}, + {{ 0x4b, 0x86, 0x8d, 0x6d }}, // c0 + {{ 0x2c, 0x4a, 0x89, 0x80 }}, // c1 + {{ 0x33, 0x9d, 0xf4, 0xe8 }}, // c2 + {{ 0x37, 0xd2, 0x18, 0xd8 }}, // c3 }}; AES::addRoundKey(&state, &schedule, 6); ASSERT_EQ(expected7, state); From 7a8bd094bfbfa159b0a4683dc4e99c68a5ea9852 Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 28 Aug 2017 11:56:13 +1000 Subject: [PATCH 083/148] build package --- package/mine.cc | 164 ++++++++++++++++++++++++++++++++++++-------- package/mine.h | 179 +++++++++++++++++++++++------------------------- src/aes.cc | 9 ++- src/aes.h | 6 +- src/base16.h | 1 + 5 files changed, 226 insertions(+), 133 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 82ae287..4644347 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -35,6 +35,19 @@ const std::unordered_map Base16::kDecodeMap = { {0x43, 0x0C}, {0x44, 0x0D}, {0x45, 0x0E}, {0x46, 0x0F} }; +ByteArray Base16::fromString(const std::string& hex) +{ + if (hex.size() % 2 != 0) { + throw std::invalid_argument("Invalid base-16 encoding"); + } + + ByteArray byteArr; + for (std::size_t i = 0; i < hex.length(); i += 2) { + byteArr.push_back(encode(hex.substr(i, 2).c_str())); + } + return byteArr; +} + void Base16::decode(char a, char b, std::ostringstream& ss) { int b0 = a & 0xff; @@ -144,7 +157,7 @@ const std::unordered_map> AES::kKeyParams = { void AES::printBytes(const ByteArray& b) { - for (int i = 1; i <= b.size(); ++i) { + for (std::size_t i = 1; i <= b.size(); ++i) { std::cout << "0x" << (b[i - 1] < 10 ? "0" : "") << Base16::encode(b[i - 1]) << " "; if (i % 4 == 0) { std::cout << std::endl; @@ -198,7 +211,7 @@ AES::KeySchedule AES::keyExpansion(const Key* key) // to each of the four bytes to produce an output word. // // Out definition: - // It's a simple substition with kSbox for corresponding bit + // It's a simple substition with kSbox for corresponding byte // index // auto substituteWord = [](Word* w) -> Word* { @@ -258,6 +271,24 @@ AES::KeySchedule AES::keyExpansion(const Key* key) return words; } +/// +/// Adding round key is simply a xor operation on +/// corresponding key for the round +/// which is generated during key expansion +/// +/// Let's say we have state and a key +/// +/// [ df c3 e2 9c ] [ ef d4 49 11 ] +/// | 0f ad 1f ca | | 1f ad ac fa | +/// | 0c 9d 8d fa | ^ | cc 9e 15 dd | +/// [ fe ef cc b2 ] [ fe ea 02 dc ] +/// +/// +/// [ df^ef c3^d4 e2^49 9c^11 ] +/// | 0f^1f ad^ad 1f^ac ca^fa | +/// | 0c^cc 9d^9e 8d^15 fa^dd | +/// [ fe^fe ef^ea cc^02 b2^dc ] +/// void AES::addRoundKey(State* state, const KeySchedule* keySchedule, int round) { for (std::size_t i = 0; i < kNb; ++i) { @@ -267,6 +298,11 @@ void AES::addRoundKey(State* state, const KeySchedule* keySchedule, int round) } } +/// +/// Simple substition for the byte +/// from sbox - i.e, for 0x04 we will replace with the +/// byte at index 0x04 => 0xf2 +/// void AES::subBytes(State* state) { for (std::size_t i = 0; i < kNb; ++i) { @@ -276,6 +312,43 @@ void AES::subBytes(State* state) } } +/// +/// A simple substition of bytes using kSBoxInverse +/// +void AES::invSubBytes(State* state) +{ + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + state->at(i)[j] = kSBoxInverse[state->at(i)[j]]; + } + } +} + +/// +/// Shifting rows is beautifully explained by diagram +/// that helped in implementation as well +/// +/// Let's say we have state +/// +/// [ df c3 e2 9c ] +/// | 0f ad 1f ca | +/// | 0c 9d 8d fa | +/// [ fe ef cc b2 ] +/// +/// shifting means +/// +/// [ df c3 e2 9c ] +/// 0f | ad 1f ca | +/// 0c 9d | 8d fa | +/// fe ef cc [ b2 ] +/// +/// and filling the spaces with shifted rows +/// +/// [ df c3 e2 9c ] +/// | ad 1f ca 0f | +/// | 8d fa 0c 9d | +/// [ b2 fe ef cc ] +/// void AES::shiftRows(State *state) { // row 1 @@ -293,6 +366,56 @@ void AES::shiftRows(State *state) std::swap(state->at(0)[3], state->at(2)[3]); } +/// +/// This is reverse of shift rows operation +/// +/// Let's say we have state +/// +/// [ df c3 e2 9c ] +/// | ad 1f ca 0f | +/// | 8d fa 0c 9d | +/// [ b2 fe ef cc ] +/// +/// shifting means +/// +/// [ df c3 e2 9c ] +/// | ad 1f ca | 0f +/// | 8d fa | 0c 9d +/// [ b2 | fe ef cc +/// +/// and filling the spaces with shifted rows +/// +/// [ df c3 e2 9c ] +/// | 0f ad 1f ca | +/// | 0c 9d 8d fa | +/// [ fe ef cc b2 ] +/// +void AES::invShiftRows(State *state) +{ + // row 1 + std::swap(state->at(0)[1], state->at(1)[1]); + std::swap(state->at(0)[1], state->at(2)[1]); + std::swap(state->at(0)[1], state->at(3)[1]); + + // row 2 + std::swap(state->at(0)[2], state->at(2)[2]); + std::swap(state->at(1)[2], state->at(3)[2]); + + // row 3 + std::swap(state->at(0)[3], state->at(3)[3]); + std::swap(state->at(0)[3], state->at(2)[3]); + std::swap(state->at(0)[3], state->at(1)[3]); +} + +/// +/// multiplies in GF(2^8) field selected column from state +/// with constant matrix defined by publication +/// +/// [ 02 03 01 01 ] +/// | 01 02 03 01 | +/// | 01 01 02 03 | +/// [ 03 01 01 02 ] +/// void AES::mixColumns(State* state) { // Finds the product of {02} and the argument to xtime modulo {1b} @@ -304,19 +427,11 @@ void AES::mixColumns(State* state) for (int col = 0; col < 4; ++col) { - // - // multiplies in GF(2^8) field selected column from state - // with constant matrix defined by publication - // - // [ 02 03 01 01 ] - // | 01 02 03 01 | - // | 01 01 02 03 | - // [ 03 01 01 02 ] - // + Word column = state->at(col); // let's take example from publication, col: [212, 191, 93, 48] // t == 6 - byte t = (column[0]) ^ (column[1]) ^ (column[2]) ^ (column[3]); + byte t = column[0] ^ column[1] ^ column[2] ^ column[3]; // see Sec. 4.2.1 and Sec. 5.1.3 for more details state->at(col)[0] ^= xtime(column[0] ^ column[1]) ^ t; state->at(col)[1] ^= xtime(column[1] ^ column[2]) ^ t; @@ -325,21 +440,12 @@ void AES::mixColumns(State* state) } } -void AES::invSubBytes(State* state) -{ - -} - -void AES::invShiftRows(State *state) -{ - -} - void AES::invMixColumns(State* state) { + (void)state; } -AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) +ByteArray AES::cipher(const ByteArray& input, const Key* key) { std::size_t keySize = key->size(); @@ -378,7 +484,7 @@ AES::ByteArray AES::cipher(const ByteArray& input, const Key* key) } -AES::ByteArray AES::decipher(const ByteArray& input, const Key* key) +ByteArray AES::decipher(const ByteArray& input, const Key* key) { std::size_t keySize = key->size(); @@ -432,7 +538,7 @@ void AES::initState(State* state, ByteArray input) } } -AES::ByteArray AES::stateToByteArray(const State *state) +ByteArray AES::stateToByteArray(const State *state) { ByteArray result(kBlockSize); int k = 0; @@ -449,11 +555,9 @@ AES::ByteArray AES::stateToByteArray(const State *state) std::string AES::cipher(const std::string& input, const std::string& key) { - Key k(key.size()); - std::copy(key.begin(), key.end(), k.begin()); - ByteArray inp(kBlockSize); - std::copy(input.begin(), input.end(), inp.begin()); - ByteArray result = cipher(inp, &k); + Key keyArr = Base16::fromString(key); + ByteArray inp = Base16::fromString(input); + ByteArray result = cipher(inp, &keyArr); return Base16::encode(result.begin(), result.end()); } diff --git a/package/mine.h b/package/mine.h index 0c3b7b1..6e75f1d 100644 --- a/package/mine.h +++ b/package/mine.h @@ -17,23 +17,29 @@ #ifndef MINE_CRYPTO_H #define MINE_CRYPTO_H +#include #include #include #include #include +#include #include #include #include #include -#include namespace mine { using byte = unsigned char; +using ByteArray = std::vector; /// /// \brief Provides base16 encoding / decoding /// +/// This class also contains some helpers to convert various input types +/// to byte array and also provides public interface to encode +/// the iterators for other containers like vector etc. +/// class Base16 { public: @@ -56,6 +62,14 @@ class Base16 { return encode(raw.begin(), raw.end()); } + /// + /// \brief Wrapper function to encode single hex char to corresponding byte + /// + static byte encode(const char* e) + { + return static_cast(strtol(e, nullptr, 16)); + } + /// /// \brief Encodes input iterator to hex encoding /// @@ -70,13 +84,12 @@ class Base16 { } /// - /// \brief Encodes single byte + /// \brief Converts hex stream (e.g, 48656C6C6F) to byte array + /// \param hex String stream e.g, 48656C6C6F (Hello) + /// \return Byte array (mine::ByteArray) containing bytes e.g, 0x48, 0x65, 0x6C, 0x6C, 0x6F + /// \throws invalid_argument if hex is not valid /// - static inline void encode(char b, std::ostringstream& ss) noexcept - { - int h = (b & 0xff); - ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; - } + static ByteArray fromString(const std::string& hex); /// /// \brief Encodes integer to hex @@ -98,37 +111,17 @@ class Base16 { /// /// \brief Decodes encoded hex - /// \throws std::runtime if invalid encoding. - /// std::runtime::what() is set accordingly + /// \throws std::invalid_argument if invalid encoding. + /// std::invalid_argument::what() is set accordingly /// static std::string decode(const std::string& enc) { if (enc.size() % 2 != 0) { - throw std::runtime_error("Invalid base-16 encoding"); + throw std::invalid_argument("Invalid base-16 encoding"); } return decode(enc.begin(), enc.end()); } - /// - /// \brief Encodes input iterator to hex encoding - /// \note User should check for the valid size or use decode(std::string) - /// \throws runtime_error if invalid base16-encoding - /// - template - static std::string decode(const Iter& begin, const Iter& end) - { - std::ostringstream ss; - for (auto it = begin; it != end; it += 2) { - decode(*it, *(it + 1), ss); - } - return ss.str(); - } - - /// - /// \brief Decodes single byte pair - /// - static void decode(char a, char b, std::ostringstream& ss); - /// /// \brief Decodes encoding to single integer of type T /// @@ -150,6 +143,35 @@ class Base16 { Base16() = delete; Base16(const Base16&) = delete; Base16& operator=(const Base16&) = delete; + + /// + /// \brief Encodes single byte + /// + static inline void encode(char b, std::ostringstream& ss) noexcept + { + int h = (b & 0xff); + ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; + } + + /// + /// \brief Encodes input iterator to hex encoding + /// \note User should check for the valid size or use decode(std::string) + /// \throws runtime_error if invalid base16-encoding + /// + template + static std::string decode(const Iter& begin, const Iter& end) + { + std::ostringstream ss; + for (auto it = begin; it != end; it += 2) { + decode(*it, *(it + 1), ss); + } + return ss.str(); + } + + /// + /// \brief Decodes single byte pair + /// + static void decode(char a, char b, std::ostringstream& ss); }; using byte = unsigned char; @@ -157,6 +179,11 @@ using byte = unsigned char; /// /// \brief Provides base64 encoding / decoding implementation /// +/// This class also provides public interface to encode +/// the iterators for other containers like vector etc. +/// +/// This also handles unicode characters +/// class Base64 { public: @@ -277,9 +304,7 @@ class Base64 { /// /// \brief Decodes encoded base64 - /// \throws std::runtime if invalid encoding. Another time it is thrown - /// is if no padding is found - /// std::runtime::what() is set according to the error + /// \see decode(const Iter&, const Iter&) /// static std::string decode(const std::string& e) { @@ -289,6 +314,12 @@ class Base64 { return decode(e.begin(), e.end()); } + /// + /// \brief Decodes base64 iterator from begin to end + /// \throws std::runtime if invalid encoding. Another time it is thrown + /// is if no padding is found + /// std::runtime::what() is set according to the error + /// template static std::string decode(const Iter& begin, const Iter& end) { @@ -399,6 +430,11 @@ class Base64 { using byte = unsigned char; +/// +/// \brief Handy safe byte array +/// +using ByteArray = std::vector; + /// /// \brief Provides AES crypto functionalities /// @@ -414,10 +450,6 @@ using byte = unsigned char; /// class AES { public: - /// - /// \brief Handy safe byte array - /// - using ByteArray = std::vector; /// /// \brief A key is a byte array @@ -548,21 +580,21 @@ class AES { /// /// \brief Transformation in the Inverse Cipher - /// that is the inverse of subBytes() + /// that is the reverse of subBytes() /// \ref Sec. 5.3.2 /// static void invSubBytes(State* state); /// /// \brief Transformation in the Inverse Cipher that is - /// the inverse of shiftRows() + /// the reverse of shiftRows() /// \ref Sec. 5.3.1 /// static void invShiftRows(State* state); /// /// \brief Transformation in the Inverse Cipher - /// that is the inverse of mixColumns() + /// that is the reverse of mixColumns() /// \ref Sec. 5.3.3 /// static void invMixColumns(State* state); @@ -582,7 +614,14 @@ class AES { AES& operator=(const AES&) = delete; friend class AESTest_RawCipher_Test; - friend class AESTest_FiniteFieldMultiply_Test; + friend class AESTest_RawSimpleCipher_Test; + friend class AESTest_RawSimpleDecipher_Test; + friend class AESTest_SubByte_Test; + friend class AESTest_InvSubByte_Test; + friend class AESTest_ShiftRows_Test; + friend class AESTest_InvShiftRows_Test; + friend class AESTest_MixColumns_Test; + friend class AESTest_InvMixColumns_Test; friend class AESTest_KeyExpansion_Test; friend class AESTest_AddRoundKey_Test; }; @@ -808,7 +847,7 @@ class BigIntegerHelper { /// /// \brief Absolutely must override this - conversion from x to single byte /// - virtual inline byte bigIntegerToByte(const BigInteger& x) const + virtual inline byte bigIntegerToByte(const BigInteger&) const { return static_cast(0); } @@ -1146,59 +1185,9 @@ class GenericRSA { /// \param signature Signature in hex /// \see https://tools.ietf.org/html/rfc3447#section-8.1.2 /// - bool verify(const PublicKey* publicKey, const std::string& message, - const std::string& signature) + bool verify(const PublicKey*, const std::string&, const std::string&) { - /* - // TODO: Add it to test - std::vector f = getByteArray(18537); - BigInteger s = i2osp(f, 2); - BigInteger os = os2ip(18537); // 105 - - std::cout << s << std::endl; - std::cout << os << std::endl; - - std::vector f2 = getByteArray(1214841185); - BigInteger s2 = i2osp(f2, 4); - BigInteger os2 = os2ip(1214841185); // 97 - - std::cout << s2 << std::endl; - std::cout << os2 << std::endl; - - std::cout << changeBase(54735, 8) << std::endl; - - std::cout << std::hex << 16 << std::endl; - std::cout << std::oct << 72 << std::endl; - - return true; - - BigInteger sign = m_helper.hexToBigInteger(signature); - try { - BigInteger vp = createVerificationPrimitive(publicKey, sign); - - const int xlen = (publicKey->n().BitCount() + 7) >> 3; - - if (xlen < vp + 11) { - // throw std::runtime_error("Integer too large"); // Needed??! Where in RFC? - } - - std::vector em = m_helper.getByteArray(vp, xlen); - - // todo: add check for following (as per https://tools.ietf.org/html/rfc3447#section-8.1.2) - // Note that emLen will be one less than k if modBits - 1 is - // divisible by 8 and equal to k otherwise. If I2OSP outputs - // "integer too large," output "invalid signature" and stop. - - // EMSA-PSS_VERIFY - https://tools.ietf.org/html/rfc3447#section-9.1.2 - - - - std::cout << "tt" << std::endl; - return true; - } catch (std::exception&) { - } -*/ return true; } @@ -1211,7 +1200,7 @@ class GenericRSA { /// \return corresponding nonnegative integer /// template - BigInteger pkcs1pad2(const T& s, int n) { + BigInteger pkcs1pad2(const T& s, std::size_t n) { if (n < s.size() + 11) { throw std::runtime_error("Message too long"); } @@ -1273,7 +1262,7 @@ class GenericRSA { if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { throw std::runtime_error("Incorrect padding PKCS#1"); } - int i = 2; // passed first two characters (0x0 and 0x2) test + std::size_t i = 2; // passed first two characters (0x0 and 0x2) test // lets check for the // if we hit end while still we're still with non-zeros, it's a padding error diff --git a/src/aes.cc b/src/aes.cc index 5a709ff..3cafb21 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -19,7 +19,6 @@ // http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf // -#include #include #include #include @@ -316,18 +315,18 @@ void AES::shiftRows(State *state) void AES::invShiftRows(State *state) { // row 1 - std::swap(state->at(0)[1], state->at(3)[1]); std::swap(state->at(0)[1], state->at(1)[1]); - std::swap(state->at(1)[1], state->at(2)[1]); + std::swap(state->at(0)[1], state->at(2)[1]); + std::swap(state->at(0)[1], state->at(3)[1]); // row 2 std::swap(state->at(0)[2], state->at(2)[2]); std::swap(state->at(1)[2], state->at(3)[2]); // row 3 - std::swap(state->at(0)[3], state->at(1)[3]); - std::swap(state->at(2)[3], state->at(3)[3]); + std::swap(state->at(0)[3], state->at(3)[3]); std::swap(state->at(0)[3], state->at(2)[3]); + std::swap(state->at(0)[3], state->at(1)[3]); } /// diff --git a/src/aes.h b/src/aes.h index bddc495..01bc226 100644 --- a/src/aes.h +++ b/src/aes.h @@ -181,21 +181,21 @@ class AES { /// /// \brief Transformation in the Inverse Cipher - /// that is the inverse of subBytes() + /// that is the reverse of subBytes() /// \ref Sec. 5.3.2 /// static void invSubBytes(State* state); /// /// \brief Transformation in the Inverse Cipher that is - /// the inverse of shiftRows() + /// the reverse of shiftRows() /// \ref Sec. 5.3.1 /// static void invShiftRows(State* state); /// /// \brief Transformation in the Inverse Cipher - /// that is the inverse of mixColumns() + /// that is the reverse of mixColumns() /// \ref Sec. 5.3.3 /// static void invMixColumns(State* state); diff --git a/src/base16.h b/src/base16.h index 77d2829..4d3ec0d 100644 --- a/src/base16.h +++ b/src/base16.h @@ -21,6 +21,7 @@ #ifndef Base16_H #define Base16_H +#include #include #include #include From e70f76f0520c5672b842fac1286d4f5c204aa57d Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 28 Aug 2017 12:21:43 +1000 Subject: [PATCH 084/148] inv matrx --- src/aes.cc | 47 +++++++++++++++++++++++++++++++++++++---------- src/aes.h | 10 ++++++++++ test/aes-test.h | 2 +- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 3cafb21..a5458bb 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -329,6 +329,27 @@ void AES::invShiftRows(State *state) std::swap(state->at(0)[3], state->at(1)[3]); } +/// +/// Finds the product of {02} and the argument to +/// xtime modulo {1b} +/// +byte AES::xtime(byte x) +{ + return ((x << 1) ^ (((x >> 7) & 1) * 0x11b)); +} + +/// +/// Multiplies numbers in the GF(2^8) field +/// +byte AES::multiply(byte x, byte y) +{ + return (((y & 0x01) * x) ^ + ((y >> 1 & 0x01) * xtime(x)) ^ + ((y >> 2 & 0x01) * xtime(xtime(x))) ^ + ((y >> 3 & 0x01) * xtime(xtime(xtime(x)))) ^ + ((y >> 4 & 0x01) * xtime(xtime(xtime(xtime(x)))))); +} + /// /// multiplies in GF(2^8) field selected column from state /// with constant matrix defined by publication @@ -340,16 +361,7 @@ void AES::invShiftRows(State *state) /// void AES::mixColumns(State* state) { - // Finds the product of {02} and the argument to xtime modulo {1b} - // mentioned in Sec. 4.2.1 of FIPS.197 - // taken from http://gauss.ececs.uc.edu/Courses/c653/extra/AES/xtime.cpp - auto xtime = [](byte x) { - return ((x << 1) ^ (((x >> 7) & 1) * 0x11b)); - }; - for (int col = 0; col < 4; ++col) { - - Word column = state->at(col); // let's take example from publication, col: [212, 191, 93, 48] // t == 6 @@ -362,9 +374,24 @@ void AES::mixColumns(State* state) } } +/// +/// Inverse multiplication with inverse matrix defined on Sec. 5.3.3 +/// +/// [ 0e 0b 0d 09 ] +/// | 09 0e 0b 0d | +/// | 0d 09 0e 0b | +/// [ 0b 0d 09 0e ] +/// void AES::invMixColumns(State* state) { - (void)state; + for (int col = 0; col < 4; ++col) { + Word column = state->at(col); + // see Sec. 4.2.1 and Sec. 5.3.3 for more details + state->at(col)[0] = multiply(column[0], 0x0e) ^ multiply(column[1], 0x0b) ^ multiply(column[2], 0x0d) ^ multiply(column[3], 0x09); + state->at(col)[1] = multiply(column[0], 0x09) ^ multiply(column[1], 0x0e) ^ multiply(column[2], 0x0b) ^ multiply(column[3], 0x0d); + state->at(col)[2] = multiply(column[0], 0x0d) ^ multiply(column[1], 0x09) ^ multiply(column[2], 0x0e) ^ multiply(column[3], 0x0b); + state->at(col)[3] = multiply(column[0], 0x0b) ^ multiply(column[1], 0x0d) ^ multiply(column[2], 0x09) ^ multiply(column[3], 0x0e); + } } ByteArray AES::cipher(const ByteArray& input, const Key* key) diff --git a/src/aes.h b/src/aes.h index 01bc226..89733c9 100644 --- a/src/aes.h +++ b/src/aes.h @@ -173,6 +173,16 @@ class AES { /// static void shiftRows(State* state); + /// + /// \ref Sec. 4.2.1 + /// + static byte xtime(byte x); + + /// + /// \ref Sec. 4.2.1 + /// + static byte multiply(byte x, byte y); + /// /// \brief Mixing columns for the state /// \ref Sec. 5.1.3 diff --git a/test/aes-test.h b/test/aes-test.h index b629f37..67f5ce6 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -504,7 +504,7 @@ TEST(AESTest, RawSimpleDecipher) }}; ByteArray output = AES::decipher(input, &key); - //ASSERT_EQ(expected, output); + ASSERT_EQ(expected, output); } } From 1560004ef4e7cb3e22254446350184ee6ee45b86 Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 28 Aug 2017 12:22:01 +1000 Subject: [PATCH 085/148] built package --- package/mine.cc | 47 +++++++++++++++++++++++++++++++++++++---------- package/mine.h | 10 ++++++++++ test/main.cc | 6 +++--- 3 files changed, 50 insertions(+), 13 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 4644347..d697a3e 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -407,6 +407,27 @@ void AES::invShiftRows(State *state) std::swap(state->at(0)[3], state->at(1)[3]); } +/// +/// Finds the product of {02} and the argument to +/// xtime modulo {1b} +/// +byte AES::xtime(byte x) +{ + return ((x << 1) ^ (((x >> 7) & 1) * 0x11b)); +} + +/// +/// Multiplies numbers in the GF(2^8) field +/// +byte AES::multiply(byte x, byte y) +{ + return (((y & 0x01) * x) ^ + ((y >> 1 & 0x01) * xtime(x)) ^ + ((y >> 2 & 0x01) * xtime(xtime(x))) ^ + ((y >> 3 & 0x01) * xtime(xtime(xtime(x)))) ^ + ((y >> 4 & 0x01) * xtime(xtime(xtime(xtime(x)))))); +} + /// /// multiplies in GF(2^8) field selected column from state /// with constant matrix defined by publication @@ -418,16 +439,7 @@ void AES::invShiftRows(State *state) /// void AES::mixColumns(State* state) { - // Finds the product of {02} and the argument to xtime modulo {1b} - // mentioned in Sec. 4.2.1 of FIPS.197 - // taken from http://gauss.ececs.uc.edu/Courses/c653/extra/AES/xtime.cpp - auto xtime = [](byte x) { - return ((x << 1) ^ (((x >> 7) & 1) * 0x11b)); - }; - for (int col = 0; col < 4; ++col) { - - Word column = state->at(col); // let's take example from publication, col: [212, 191, 93, 48] // t == 6 @@ -440,9 +452,24 @@ void AES::mixColumns(State* state) } } +/// +/// Inverse multiplication with inverse matrix defined on Sec. 5.3.3 +/// +/// [ 0e 0b 0d 09 ] +/// | 09 0e 0b 0d | +/// | 0d 09 0e 0b | +/// [ 0b 0d 09 0e ] +/// void AES::invMixColumns(State* state) { - (void)state; + for (int col = 0; col < 4; ++col) { + Word column = state->at(col); + // see Sec. 4.2.1 and Sec. 5.3.3 for more details + state->at(col)[0] = multiply(column[0], 0x0e) ^ multiply(column[1], 0x0b) ^ multiply(column[2], 0x0d) ^ multiply(column[3], 0x09); + state->at(col)[1] = multiply(column[0], 0x09) ^ multiply(column[1], 0x0e) ^ multiply(column[2], 0x0b) ^ multiply(column[3], 0x0d); + state->at(col)[2] = multiply(column[0], 0x0d) ^ multiply(column[1], 0x09) ^ multiply(column[2], 0x0e) ^ multiply(column[3], 0x0b); + state->at(col)[3] = multiply(column[0], 0x0b) ^ multiply(column[1], 0x0d) ^ multiply(column[2], 0x09) ^ multiply(column[3], 0x0e); + } } ByteArray AES::cipher(const ByteArray& input, const Key* key) diff --git a/package/mine.h b/package/mine.h index 6e75f1d..5e2842a 100644 --- a/package/mine.h +++ b/package/mine.h @@ -572,6 +572,16 @@ class AES { /// static void shiftRows(State* state); + /// + /// \ref Sec. 4.2.1 + /// + static byte xtime(byte x); + + /// + /// \ref Sec. 4.2.1 + /// + static byte multiply(byte x, byte y); + /// /// \brief Mixing columns for the state /// \ref Sec. 5.1.3 diff --git a/test/main.cc b/test/main.cc index c159e9e..80b4f4e 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,9 +1,9 @@ #include "test.h" -//#include "base16-test.h" -//#include "base64-test.h" +#include "base16-test.h" +#include "base64-test.h" #include "aes-test.h" #include "zlib-test.h" -//#include "rsa-test.h" +#include "rsa-test.h" INITIALIZE_EASYLOGGINGPP From a4198094f730f2fc85675b54dfa617a226ca5c1f Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 28 Aug 2017 12:55:37 +1000 Subject: [PATCH 086/148] various modes of input --- src/aes.cc | 12 ++++++++++-- src/aes.h | 14 +++++++++++++- test/aes-test.h | 30 +++++++++++++++++++++++++++++- test/main.cc | 2 +- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index a5458bb..d129197 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -25,6 +25,7 @@ #include #include #include "src/base16.h" +#include "src/base64.h" #include "src/aes.h" using namespace mine; @@ -502,10 +503,17 @@ ByteArray AES::stateToByteArray(const State *state) // public -std::string AES::cipher(const std::string& input, const std::string& key) +std::string AES::cipher(const std::string& input, const std::string& key, InputMode inputMode) { Key keyArr = Base16::fromString(key); - ByteArray inp = Base16::fromString(input); + ByteArray inp; + if (inputMode == InputMode::Plain) { + inp = Base16::fromString(Base16::encode(input)); + } else if (inputMode == InputMode::Base16) { + inp = Base16::fromString(input); + } else { // base64 + inp = Base16::fromString(Base16::encode(Base64::decode(input))); + } ByteArray result = cipher(inp, &keyArr); return Base16::encode(result.begin(), result.end()); } diff --git a/src/aes.h b/src/aes.h index 89733c9..6936996 100644 --- a/src/aes.h +++ b/src/aes.h @@ -52,6 +52,15 @@ using ByteArray = std::vector; class AES { public: + /// + /// \brief Input mode for various functions + /// + enum class InputMode { + Plain, + Base16, + Base64 + }; + /// /// \brief A key is a byte array /// @@ -60,9 +69,10 @@ class AES { /// /// \brief Ciphers the input with specified hex key /// \param key Hex key + /// \param inputMode the type of input. Defaults to Plain /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key); + static std::string cipher(const std::string& input, const std::string& key, InputMode inputMode = InputMode::Plain); private: @@ -225,6 +235,8 @@ class AES { AES& operator=(const AES&) = delete; friend class AESTest_RawCipher_Test; + friend class AESTest_RawCipherPlain_Test; + friend class AESTest_RawCipherBase64_Test; friend class AESTest_RawSimpleCipher_Test; friend class AESTest_RawSimpleDecipher_Test; friend class AESTest_SubByte_Test; diff --git a/test/aes-test.h b/test/aes-test.h index 67f5ce6..e261e4c 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -442,12 +442,40 @@ TEST(AESTest, RawCipherDirect) for (auto& item : RawCipherData) { std::string expected = PARAM(2); - std::string output = AES::cipher(PARAM(0), PARAM(1)); + std::string output = AES::cipher(PARAM(0), PARAM(1), AES::InputMode::Base16); // case insensitive comparison because hex can be upper or lower case ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } +// input key expected +static TestData RawCipherPlainInputData = { + TestCase("this is test...", "000102030405060708090a0b0c0d0e0f", "da14fb09b2378948c5b4966414a6779f"), +}; + +TEST(AESTest, RawCipherPlain) +{ + for (auto& item : RawCipherPlainInputData) { + std::string expected = PARAM(2); + std::string output = AES::cipher(PARAM(0), PARAM(1), AES::InputMode::Plain); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + } +} + +// input key expected +static TestData RawCipherBase64InputData = { + TestCase("dGhpcyBpcyB0ZXN0Li4u", "000102030405060708090a0b0c0d0e0f", "da14fb09b2378948c5b4966414a6779f"), +}; + +TEST(AESTest, RawCipherBase64) +{ + for (auto& item : RawCipherBase64InputData) { + std::string expected = PARAM(2); + std::string output = AES::cipher(PARAM(0), PARAM(1), AES::InputMode::Base64); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + } +} + TEST(AESTest, RawSimpleCipher) { diff --git a/test/main.cc b/test/main.cc index 80b4f4e..d721c93 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,9 +1,9 @@ #include "test.h" #include "base16-test.h" #include "base64-test.h" -#include "aes-test.h" #include "zlib-test.h" #include "rsa-test.h" +#include "aes-test.h" INITIALIZE_EASYLOGGINGPP From 1c0bb02bfdba949b8e95977ae14d24f5eef5f161 Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 28 Aug 2017 16:59:21 +1000 Subject: [PATCH 087/148] cbc mode --- src/aes.cc | 128 +++++++++++++++++++++++++++++++++++------------- src/aes.h | 87 +++++++++++++++++++------------- src/rsa.h | 2 +- test/aes-test.h | 59 ++++++++++++++++++++++ 4 files changed, 208 insertions(+), 68 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index d129197..af3baf8 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -395,19 +395,41 @@ void AES::invMixColumns(State* state) } } -ByteArray AES::cipher(const ByteArray& input, const Key* key) +void AES::initState(State* state, ByteArray input) { - std::size_t keySize = key->size(); + // Pad the input if needed + if (input.size() < kBlockSize) { + std::fill_n(input.end(), kBlockSize - input.size(), 0); + } - // key size validation - if (keySize != 16 && keySize != 24 && keySize != 32) { - throw std::invalid_argument("Invalid AES key size"); + // assign it to state for processing + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + (*state)[i][j] = input[(kNb * i) + j]; + } } +} + +ByteArray AES::stateToByteArray(const State *state) +{ + ByteArray result(kBlockSize); + int k = 0; + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + result[k++] = state->at(i)[j]; + } + } + + return result; +} + +ByteArray AES::cipher(const ByteArray& input, const Key* key) +{ State state; initState(&state, input); - uint8_t kTotalRounds = kKeyParams.at(keySize)[1]; + uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; // Create linear subkeys (key schedule) KeySchedule keySchedule = keyExpansion(key); @@ -434,6 +456,72 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) } +ByteArray AES::generateRandomBytes(const std::size_t len) +{ + ByteArray result; + const int kMax = 999; + srand(time(nullptr)); + for (std::size_t i = 0; i < len; ++i) { + int r = rand() % kMax; + while (r == 0) { + r = rand() % kMax + 1; + } + result.push_back(static_cast(r)); + } + return result; +} + +ByteArray AES::xorWith(ByteArray& input, const ByteArray& arr) +{ + for (std::size_t i = 0; i < kBlockSize; ++i) { + input.at(i) ^= arr.at(i); + } + return input; +} + + +ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) +{ + + std::size_t keySize = key->size(); + + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + if (!iv.empty() && iv.size() != 16) { + throw std::invalid_argument("Invalid IV, it should be 128-bit"); + } else if (iv.empty()) { + // generate IV + iv = generateRandomBytes(16); + } + + // FIXME: Need more fixes + // 2b7e151628aed2a6abf7158809cf4f3c + // 20 c7 04 40 ac 40 0d ba 84 06 57 00 74 f2 e2 2a + + const std::size_t inputSize = input.size(); + ByteArray result; + ByteArray nextXorWith = iv; + + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { + ByteArray inputBlock(kBlockSize, 0); + + // don't use copy_n as we are setting the values + for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { + inputBlock.at(j) = input.at(j + i); + } + + xorWith(inputBlock, nextXorWith); + + ByteArray outputBlock = cipher(inputBlock, key); + std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); + nextXorWith = outputBlock; + } + return result; +} + ByteArray AES::decipher(const ByteArray& input, const Key* key) { std::size_t keySize = key->size(); @@ -473,34 +561,6 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) } -void AES::initState(State* state, ByteArray input) -{ - // Pad the input if needed - if (input.size() < kBlockSize) { - std::fill_n(input.end(), kBlockSize - input.size(), 0); - } - - // assign it to state for processing - for (std::size_t i = 0; i < kNb; ++i) { - for (std::size_t j = 0; j < kNb; ++j) { - (*state)[i][j] = input[(kNb * i) + j]; - } - } -} - -ByteArray AES::stateToByteArray(const State *state) -{ - ByteArray result(kBlockSize); - int k = 0; - for (std::size_t i = 0; i < kNb; ++i) { - for (std::size_t j = 0; j < kNb; ++j) { - result[k++] = state->at(i)[j]; - } - } - - return result; -} - // public std::string AES::cipher(const std::string& input, const std::string& key, InputMode inputMode) diff --git a/src/aes.h b/src/aes.h index 6936996..c8a0f10 100644 --- a/src/aes.h +++ b/src/aes.h @@ -128,39 +128,6 @@ class AES { /// static const uint8_t kNb = 4; - /// - /// \brief Initializes the state with input. This function - /// also pads the input if needed (i.e, input is not block of 128-bit) - /// - static void initState(State* state, ByteArray input); - - /// - /// \brief Raw encryption function - not for public use - /// \param input 128-bit plain input - /// If array is bigger it's chopped and if it's smaller, it's padded - /// please use alternative functions if your array is bigger. Those - /// function will handle all the bytes correctly. - /// \param key Byte array of key - /// \return 128-bit cipher text - /// - static ByteArray cipher(const ByteArray& input, const Key* key); - - /// - /// \brief Raw decryption function - not for public use - /// \param input 128-bit cipher input - /// If array is bigger it's chopped and if it's smaller, it's padded - /// please use alternative functions if your array is bigger. Those - /// function will handle all the bytes correctly. - /// \param key Byte array of key - /// \return 128-bit plain text - /// - static ByteArray decipher(const ByteArray& input, const Key* key); - - /// - /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array - /// - static ByteArray stateToByteArray(const State* state); - /// /// \brief Key expansion function as described in FIPS.197 /// @@ -230,6 +197,59 @@ class AES { /// static void printState(const State*); + /// + /// \brief Initializes the state with input. This function + /// also pads the input if needed (i.e, input is not block of 128-bit) + /// + static void initState(State* state, ByteArray input); + + /// + /// \brief Generates random bytes of length + /// + static ByteArray generateRandomBytes(const std::size_t len); + + /// + /// \brief Exclusive XOR with arr + /// + static ByteArray xorWith(ByteArray &input, const ByteArray& arr); + + /// + /// \brief Raw encryption function - not for public use + /// \param input 128-bit plain input + /// If array is bigger it's chopped and if it's smaller, it's padded + /// please use alternative functions if your array is bigger. Those + /// function will handle all the bytes correctly. + /// \param key Pointer to a valid AES key + /// \note This does not do any key or input validation + /// \return 128-bit cipher text + /// + static ByteArray cipher(const ByteArray& input, const Key* key); + + /// + /// \brief Ciphers with CBC-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray cipher(const ByteArray& input, const Key* key, ByteArray& iv); + + /// + /// \brief Raw decryption function - not for public use + /// \param input 128-bit cipher input + /// If array is bigger it's chopped and if it's smaller, it's padded + /// please use alternative functions if your array is bigger. Those + /// function will handle all the bytes correctly. + /// \param key Byte array of key + /// \return 128-bit plain text + /// + static ByteArray decipher(const ByteArray& input, const Key* key); + + /// + /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array + /// + static ByteArray stateToByteArray(const State* state); + AES() = delete; AES(const AES&) = delete; AES& operator=(const AES&) = delete; @@ -247,6 +267,7 @@ class AES { friend class AESTest_InvMixColumns_Test; friend class AESTest_KeyExpansion_Test; friend class AESTest_AddRoundKey_Test; + friend class AESTest_CbcCipher_Test; }; } // end namespace mine diff --git a/src/rsa.h b/src/rsa.h index 8485d2e..3f96e9b 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -640,7 +640,7 @@ class GenericRSA { byteArray[--n] = 0; - srand(time(NULL)); + srand(time(nullptr)); int r = rand() % kLengthOfRandom + 1; while (n > 2) { r = 0; diff --git a/test/aes-test.h b/test/aes-test.h index e261e4c..7e32d12 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -535,6 +535,65 @@ TEST(AESTest, RawSimpleDecipher) ASSERT_EQ(expected, output); } +TEST(AESTest, CbcCipher) +{ + // input expected + static TestData CbcCipherTestData = { + TestCase("this is test....", ByteArray {{ + 0xa3, 0xd9, 0x36, 0xf1, + 0xfe, 0xd3, 0xb8, 0xd3, + 0xe7, 0x4e, 0x09, 0x4e, + 0x2c, 0x0f, 0x1b, 0xd9 + }}), + TestCase("this is test.", ByteArray {{ + 0x86, 0xae, 0xc0, 0x99, + 0xfc, 0x4e, 0xba, 0x5f, + 0xcd, 0xaa, 0xd2, 0x94, + 0x96, 0x48, 0x01, 0x65 + }}), + TestCase("this is test longer", ByteArray {{ + 0x2c, 0x32, 0x8a, 0xa3, + 0x6c, 0xff, 0xdd, 0xe8, + 0xc1, 0x41, 0x2e, 0x12, + 0x10, 0x2f, 0x22, 0x05, + 0xe7, 0x4e, 0x98, 0x8c, + 0x9a, 0x15, 0x42, 0xe9, + 0x72, 0x84, 0xc5, 0x19, + 0x76, 0xc2, 0x6a, 0x44 + }}), + TestCase("this is test longer than 128-bit", ByteArray {{ + 0x2c, 0x32, 0x8a, 0xa3, + 0x6c, 0xff, 0xdd, 0xe8, + 0xc1, 0x41, 0x2e, 0x12, + 0x10, 0x2f, 0x22, 0x05, + 0x49, 0x32, 0x6a, 0x1c, + 0x3c, 0x51, 0xc3, 0x64, + 0x33, 0x47, 0xdf, 0x21, + 0xe4, 0x26, 0xe8, 0x45 + }}), + }; + + AES::Key key = {{ + 0x2b, 0x7e, 0x15, 0x16, + 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, + 0x09, 0xcf, 0x4f, 0x3c + }}; + ByteArray iv = {{ + 0x20, 0xc7, 0x04, 0x40, + 0xac, 0x40, 0x0d, 0xba, + 0x84, 0x06, 0x57, 0x00, + 0x74, 0xf2, 0xe2, 0x2a + }}; + + for (auto& item : CbcCipherTestData) { + ByteArray expected = PARAM(1); + ByteArray input = Base16::fromString(Base16::encode(PARAM(0))); + ByteArray output = AES::cipher(input, &key, iv); + ASSERT_EQ(expected, output); + } +} + } #endif // AES_TEST_H From 25a0063d57d2c6cc1012dad1965f938e33ae61e1 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Mon, 28 Aug 2017 18:46:01 +1000 Subject: [PATCH 088/148] iv cbc public --- package/mine.cc | 151 +++++++++++++++++++++++++++++++++++++----------- package/mine.h | 112 ++++++++++++++++++++++++----------- src/aes.cc | 24 ++++++-- src/aes.h | 9 +++ test/aes-test.h | 9 +++ 5 files changed, 232 insertions(+), 73 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index d697a3e..934e41c 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -472,19 +472,41 @@ void AES::invMixColumns(State* state) } } -ByteArray AES::cipher(const ByteArray& input, const Key* key) +void AES::initState(State* state, ByteArray input) { - std::size_t keySize = key->size(); + // Pad the input if needed + if (input.size() < kBlockSize) { + std::fill_n(input.end(), kBlockSize - input.size(), 0); + } - // key size validation - if (keySize != 16 && keySize != 24 && keySize != 32) { - throw std::invalid_argument("Invalid AES key size"); + // assign it to state for processing + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + (*state)[i][j] = input[(kNb * i) + j]; + } + } +} + +ByteArray AES::stateToByteArray(const State *state) +{ + ByteArray result(kBlockSize); + int k = 0; + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + result[k++] = state->at(i)[j]; + } } + return result; +} + +ByteArray AES::cipher(const ByteArray& input, const Key* key) +{ + State state; initState(&state, input); - uint8_t kTotalRounds = kKeyParams.at(keySize)[1]; + uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; // Create linear subkeys (key schedule) KeySchedule keySchedule = keyExpansion(key); @@ -511,6 +533,68 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) } +ByteArray AES::generateRandomBytes(const std::size_t len) +{ + ByteArray result; + const int kMax = 999; + srand(time(nullptr)); + for (std::size_t i = 0; i < len; ++i) { + int r = rand() % kMax; + while (r == 0) { + r = rand() % kMax + 1; + } + result.push_back(static_cast(r)); + } + return result; +} + +ByteArray AES::xorWith(ByteArray& input, const ByteArray& arr) +{ + for (std::size_t i = 0; i < kBlockSize; ++i) { + input.at(i) ^= arr.at(i); + } + return input; +} + + +ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) +{ + + std::size_t keySize = key->size(); + + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + if (!iv.empty() && iv.size() != 16) { + throw std::invalid_argument("Invalid IV, it should be 128-bit"); + } else if (iv.empty()) { + // generate IV + iv = generateRandomBytes(16); + } + + const std::size_t inputSize = input.size(); + ByteArray result; + ByteArray nextXorWith = iv; + + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { + ByteArray inputBlock(kBlockSize, 0); + + // don't use copy_n as we are setting the values + for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { + inputBlock.at(j) = input.at(j + i); + } + + xorWith(inputBlock, nextXorWith); + + ByteArray outputBlock = cipher(inputBlock, key); + std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); + nextXorWith = outputBlock; + } + return result; +} + ByteArray AES::decipher(const ByteArray& input, const Key* key) { std::size_t keySize = key->size(); @@ -550,41 +634,40 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) } -void AES::initState(State* state, ByteArray input) -{ - // Pad the input if needed - if (input.size() < kBlockSize) { - std::fill_n(input.end(), kBlockSize - input.size(), 0); - } - - // assign it to state for processing - for (std::size_t i = 0; i < kNb; ++i) { - for (std::size_t j = 0; j < kNb; ++j) { - (*state)[i][j] = input[(kNb * i) + j]; - } - } -} +// public -ByteArray AES::stateToByteArray(const State *state) +std::string AES::cipher(const std::string& input, const std::string& key, InputMode inputMode) { - ByteArray result(kBlockSize); - int k = 0; - for (std::size_t i = 0; i < kNb; ++i) { - for (std::size_t j = 0; j < kNb; ++j) { - result[k++] = state->at(i)[j]; - } + Key keyArr = Base16::fromString(key); + ByteArray inp; + if (inputMode == InputMode::Plain) { + inp = Base16::fromString(Base16::encode(input)); + } else if (inputMode == InputMode::Base16) { + inp = Base16::fromString(input); + } else { // base64 + inp = Base16::fromString(Base16::encode(Base64::decode(input))); } - - return result; + ByteArray result = cipher(inp, &keyArr); + return Base16::encode(result.begin(), result.end()); } -// public - -std::string AES::cipher(const std::string& input, const std::string& key) +std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, InputMode inputMode) { + bool ivecGenerated = iv.empty(); + ByteArray ivec; Key keyArr = Base16::fromString(key); - ByteArray inp = Base16::fromString(input); - ByteArray result = cipher(inp, &keyArr); + ByteArray inp; + if (inputMode == InputMode::Plain) { + inp = Base16::fromString(Base16::encode(input)); + } else if (inputMode == InputMode::Base16) { + inp = Base16::fromString(input); + } else { // base64 + inp = Base16::fromString(Base16::encode(Base64::decode(input))); + } + ByteArray result = cipher(inp, &keyArr, ivec); + if (ivecGenerated) { + iv = Base16::encode(ivec.begin(), ivec.end()); + } return Base16::encode(result.begin(), result.end()); } diff --git a/package/mine.h b/package/mine.h index 5e2842a..823d0e8 100644 --- a/package/mine.h +++ b/package/mine.h @@ -451,6 +451,15 @@ using ByteArray = std::vector; class AES { public: + /// + /// \brief Input mode for various functions + /// + enum class InputMode { + Plain, + Base16, + Base64 + }; + /// /// \brief A key is a byte array /// @@ -459,9 +468,19 @@ class AES { /// /// \brief Ciphers the input with specified hex key /// \param key Hex key + /// \param inputMode the type of input. Defaults to Plain /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key); + static std::string cipher(const std::string& input, const std::string& key, InputMode inputMode = InputMode::Plain); + + /// + /// \brief Ciphers the input with specified hex key using CBC mode + /// \param key Hex key + /// \param iv Initialization vector, passed by reference. If empty a random is generated and passed in + /// \param inputMode the type of input. Defaults to Plain + /// \return Base16 encoded cipher + /// + static std::string cipher(const std::string& input, const std::string& key, std::string& iv, InputMode inputMode = InputMode::Plain); private: @@ -517,39 +536,6 @@ class AES { /// static const uint8_t kNb = 4; - /// - /// \brief Initializes the state with input. This function - /// also pads the input if needed (i.e, input is not block of 128-bit) - /// - static void initState(State* state, ByteArray input); - - /// - /// \brief Raw encryption function - not for public use - /// \param input 128-bit plain input - /// If array is bigger it's chopped and if it's smaller, it's padded - /// please use alternative functions if your array is bigger. Those - /// function will handle all the bytes correctly. - /// \param key Byte array of key - /// \return 128-bit cipher text - /// - static ByteArray cipher(const ByteArray& input, const Key* key); - - /// - /// \brief Raw decryption function - not for public use - /// \param input 128-bit cipher input - /// If array is bigger it's chopped and if it's smaller, it's padded - /// please use alternative functions if your array is bigger. Those - /// function will handle all the bytes correctly. - /// \param key Byte array of key - /// \return 128-bit plain text - /// - static ByteArray decipher(const ByteArray& input, const Key* key); - - /// - /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array - /// - static ByteArray stateToByteArray(const State* state); - /// /// \brief Key expansion function as described in FIPS.197 /// @@ -619,11 +605,66 @@ class AES { /// static void printState(const State*); + /// + /// \brief Initializes the state with input. This function + /// also pads the input if needed (i.e, input is not block of 128-bit) + /// + static void initState(State* state, ByteArray input); + + /// + /// \brief Generates random bytes of length + /// + static ByteArray generateRandomBytes(const std::size_t len); + + /// + /// \brief Exclusive XOR with arr + /// + static ByteArray xorWith(ByteArray &input, const ByteArray& arr); + + /// + /// \brief Raw encryption function - not for public use + /// \param input 128-bit plain input + /// If array is bigger it's chopped and if it's smaller, it's padded + /// please use alternative functions if your array is bigger. Those + /// function will handle all the bytes correctly. + /// \param key Pointer to a valid AES key + /// \note This does not do any key or input validation + /// \return 128-bit cipher text + /// + static ByteArray cipher(const ByteArray& input, const Key* key); + + /// + /// \brief Ciphers with CBC-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray cipher(const ByteArray& input, const Key* key, ByteArray& iv); + + /// + /// \brief Raw decryption function - not for public use + /// \param input 128-bit cipher input + /// If array is bigger it's chopped and if it's smaller, it's padded + /// please use alternative functions if your array is bigger. Those + /// function will handle all the bytes correctly. + /// \param key Byte array of key + /// \return 128-bit plain text + /// + static ByteArray decipher(const ByteArray& input, const Key* key); + + /// + /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array + /// + static ByteArray stateToByteArray(const State* state); + AES() = delete; AES(const AES&) = delete; AES& operator=(const AES&) = delete; friend class AESTest_RawCipher_Test; + friend class AESTest_RawCipherPlain_Test; + friend class AESTest_RawCipherBase64_Test; friend class AESTest_RawSimpleCipher_Test; friend class AESTest_RawSimpleDecipher_Test; friend class AESTest_SubByte_Test; @@ -634,6 +675,7 @@ class AES { friend class AESTest_InvMixColumns_Test; friend class AESTest_KeyExpansion_Test; friend class AESTest_AddRoundKey_Test; + friend class AESTest_CbcCipher_Test; }; /// Here onwards start implementation for RSA - this contains @@ -1245,7 +1287,7 @@ class GenericRSA { byteArray[--n] = 0; - srand(time(NULL)); + srand(time(nullptr)); int r = rand() % kLengthOfRandom + 1; while (n > 2) { r = 0; diff --git a/src/aes.cc b/src/aes.cc index af3baf8..22bebbd 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -497,10 +497,6 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) iv = generateRandomBytes(16); } - // FIXME: Need more fixes - // 2b7e151628aed2a6abf7158809cf4f3c - // 20 c7 04 40 ac 40 0d ba 84 06 57 00 74 f2 e2 2a - const std::size_t inputSize = input.size(); ByteArray result; ByteArray nextXorWith = iv; @@ -577,3 +573,23 @@ std::string AES::cipher(const std::string& input, const std::string& key, InputM ByteArray result = cipher(inp, &keyArr); return Base16::encode(result.begin(), result.end()); } + +std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, InputMode inputMode) +{ + bool ivecGenerated = iv.empty(); + ByteArray ivec; + Key keyArr = Base16::fromString(key); + ByteArray inp; + if (inputMode == InputMode::Plain) { + inp = Base16::fromString(Base16::encode(input)); + } else if (inputMode == InputMode::Base16) { + inp = Base16::fromString(input); + } else { // base64 + inp = Base16::fromString(Base16::encode(Base64::decode(input))); + } + ByteArray result = cipher(inp, &keyArr, ivec); + if (ivecGenerated) { + iv = Base16::encode(ivec.begin(), ivec.end()); + } + return Base16::encode(result.begin(), result.end()); +} diff --git a/src/aes.h b/src/aes.h index c8a0f10..9f8f384 100644 --- a/src/aes.h +++ b/src/aes.h @@ -74,6 +74,15 @@ class AES { /// static std::string cipher(const std::string& input, const std::string& key, InputMode inputMode = InputMode::Plain); + /// + /// \brief Ciphers the input with specified hex key using CBC mode + /// \param key Hex key + /// \param iv Initialization vector, passed by reference. If empty a random is generated and passed in + /// \param inputMode the type of input. Defaults to Plain + /// \return Base16 encoded cipher + /// + static std::string cipher(const std::string& input, const std::string& key, std::string& iv, InputMode inputMode = InputMode::Plain); + private: /// diff --git a/test/aes-test.h b/test/aes-test.h index 7e32d12..2694d47 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -571,6 +571,15 @@ TEST(AESTest, CbcCipher) 0x33, 0x47, 0xdf, 0x21, 0xe4, 0x26, 0xe8, 0x45 }}), + TestCase("this is test longer than 128-bit this is test longer than 128-bit this is test longer than 128-bit", ByteArray {{ + 0x2c, 0x32, 0x8a, 0xa3, 0x6c, 0xff, 0xdd, 0xe8, 0xc1, 0x41, 0x2e, 0x12, 0x10, 0x2f, 0x22, 0x05, + 0x49, 0x32, 0x6a, 0x1c, 0x3c, 0x51, 0xc3, 0x64, 0x33, 0x47, 0xdf, 0x21, 0xe4, 0x26, 0xe8, 0x45, + 0xf0, 0x82, 0xb4, 0x6e, 0xa2, 0xda, 0xcb, 0x82, 0xa3, 0x78, 0x90, 0x47, 0xf3, 0x9a, 0x33, 0x44, + 0x56, 0x8d, 0xa6, 0x1c, 0x66, 0x53, 0x47, 0x96, 0x56, 0x05, 0xb9, 0xa9, 0x78, 0xc8, 0x1e, 0xc6, + 0xa0, 0x46, 0x22, 0x38, 0x62, 0xdb, 0xbe, 0xf9, 0x78, 0xda, 0xdf, 0xc1, 0xe0, 0x57, 0x51, 0x23, + 0x35, 0x67, 0xab, 0xa3, 0x6e, 0x95, 0x02, 0xdd, 0x66, 0x9f, 0x53, 0x00, 0x82, 0x79, 0x4f, 0x5d, + 0xad, 0xe2, 0x58, 0x93, 0xef, 0xe3, 0x2f, 0x52, 0x58, 0x48, 0xd1, 0xef, 0x65, 0x87, 0xc8, 0xc7 + }}), }; AES::Key key = {{ From 4b67730537573573724f53e0ac58f96a6a50b5fa Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Mon, 28 Aug 2017 19:03:28 +1000 Subject: [PATCH 089/148] resolveInputMode helper --- src/aes.cc | 32 ++++++++++++++------------------ src/aes.h | 5 +++++ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 22bebbd..8509db5 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -557,36 +557,32 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) } +ByteArray AES::resolveInputMode(const std::string& input, InputMode inputMode) +{ + if (inputMode == InputMode::Plain) { + return Base16::fromString(Base16::encode(input)); + } else if (inputMode == InputMode::Base16) { + return Base16::fromString(input); + } + return Base16::fromString(Base16::encode(Base64::decode(input))); +} + // public std::string AES::cipher(const std::string& input, const std::string& key, InputMode inputMode) { Key keyArr = Base16::fromString(key); - ByteArray inp; - if (inputMode == InputMode::Plain) { - inp = Base16::fromString(Base16::encode(input)); - } else if (inputMode == InputMode::Base16) { - inp = Base16::fromString(input); - } else { // base64 - inp = Base16::fromString(Base16::encode(Base64::decode(input))); - } + ByteArray inp = resolveInputMode(input, inputMode); ByteArray result = cipher(inp, &keyArr); return Base16::encode(result.begin(), result.end()); } std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, InputMode inputMode) { - bool ivecGenerated = iv.empty(); - ByteArray ivec; Key keyArr = Base16::fromString(key); - ByteArray inp; - if (inputMode == InputMode::Plain) { - inp = Base16::fromString(Base16::encode(input)); - } else if (inputMode == InputMode::Base16) { - inp = Base16::fromString(input); - } else { // base64 - inp = Base16::fromString(Base16::encode(Base64::decode(input))); - } + ByteArray inp = resolveInputMode(input, inputMode); + ByteArray ivec; + bool ivecGenerated = iv.empty(); ByteArray result = cipher(inp, &keyArr, ivec); if (ivecGenerated) { iv = Base16::encode(ivec.begin(), ivec.end()); diff --git a/src/aes.h b/src/aes.h index 9f8f384..0976abe 100644 --- a/src/aes.h +++ b/src/aes.h @@ -217,6 +217,11 @@ class AES { /// static ByteArray generateRandomBytes(const std::size_t len); + /// + /// \brief Creates byte array from input based on input mode + /// + static ByteArray resolveInputMode(const std::string& input, InputMode inputMode); + /// /// \brief Exclusive XOR with arr /// From e4384d18d82d6a5bc289c6b631859e9aa204c3ad Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Mon, 28 Aug 2017 19:56:12 +1000 Subject: [PATCH 090/148] simple deci for with cbc --- src/aes.cc | 80 +++++++++++++++++++++++++++++++++---------------- src/aes.h | 11 ++++++- test/aes-test.h | 7 +++++ 3 files changed, 72 insertions(+), 26 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 8509db5..e808a27 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -471,14 +471,45 @@ ByteArray AES::generateRandomBytes(const std::size_t len) return result; } -ByteArray AES::xorWith(ByteArray& input, const ByteArray& arr) +ByteArray* AES::xorWith(ByteArray* input, const ByteArray* arr) { for (std::size_t i = 0; i < kBlockSize; ++i) { - input.at(i) ^= arr.at(i); + input->at(i) ^= arr->at(i); } return input; } +ByteArray AES::decipher(const ByteArray& input, const Key* key) +{ + + State state; + initState(&state, input); + + uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; + + // Create linear subkeys (key schedule) + KeySchedule keySchedule = keyExpansion(key); + + int round = kTotalRounds; + + // initial round + addRoundKey(&state, &keySchedule, round--); + + // intermediate round + while (round > 0) { + invShiftRows(&state); + invSubBytes(&state); + addRoundKey(&state, &keySchedule, round--); + invMixColumns(&state); + } + + // final round + invShiftRows(&state); + invSubBytes(&state); + addRoundKey(&state, &keySchedule, round); + + return stateToByteArray(&state); +} ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) { @@ -498,6 +529,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) } const std::size_t inputSize = input.size(); + ByteArray result; ByteArray nextXorWith = iv; @@ -509,7 +541,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) inputBlock.at(j) = input.at(j + i); } - xorWith(inputBlock, nextXorWith); + xorWith(&inputBlock, &nextXorWith); ByteArray outputBlock = cipher(inputBlock, key); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); @@ -518,8 +550,9 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) return result; } -ByteArray AES::decipher(const ByteArray& input, const Key* key) +ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) { + std::size_t keySize = key->size(); // key size validation @@ -527,34 +560,31 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) throw std::invalid_argument("Invalid AES key size"); } - State state; - initState(&state, input); - - uint8_t kTotalRounds = kKeyParams.at(keySize)[1]; + if (input.size() % 16 != 0) { + throw std::invalid_argument("Invalid AES cipher"); + } - // Create linear subkeys (key schedule) - KeySchedule keySchedule = keyExpansion(key); + const std::size_t inputSize = input.size(); + ByteArray result; - int round = kTotalRounds; + ByteArray nextXorWith = iv; - // initial round - addRoundKey(&state, &keySchedule, round--); + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { + ByteArray inputBlock(kBlockSize, 0); - // intermediate round - while (round > 0) { - invShiftRows(&state); - invSubBytes(&state); - addRoundKey(&state, &keySchedule, round--); - invMixColumns(&state); - } + // don't use copy_n as we are setting the values + for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { + inputBlock.at(j) = input.at(j + i); + } - // final round - invShiftRows(&state); - invSubBytes(&state); - addRoundKey(&state, &keySchedule, round); + ByteArray outputBlock = decipher(inputBlock, key); - return stateToByteArray(&state); + xorWith(&outputBlock, &nextXorWith); + nextXorWith = inputBlock; + std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); + } + return result; } ByteArray AES::resolveInputMode(const std::string& input, InputMode inputMode) diff --git a/src/aes.h b/src/aes.h index 0976abe..4f7110a 100644 --- a/src/aes.h +++ b/src/aes.h @@ -225,7 +225,7 @@ class AES { /// /// \brief Exclusive XOR with arr /// - static ByteArray xorWith(ByteArray &input, const ByteArray& arr); + static ByteArray* xorWith(ByteArray* input, const ByteArray* arr); /// /// \brief Raw encryption function - not for public use @@ -259,6 +259,15 @@ class AES { /// static ByteArray decipher(const ByteArray& input, const Key* key); + /// + /// \brief Deciphers with CBC-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray decipher(const ByteArray& input, const Key* key, ByteArray& iv); + /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array /// diff --git a/test/aes-test.h b/test/aes-test.h index 2694d47..e13939d 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -600,6 +600,13 @@ TEST(AESTest, CbcCipher) ByteArray input = Base16::fromString(Base16::encode(PARAM(0))); ByteArray output = AES::cipher(input, &key, iv); ASSERT_EQ(expected, output); + + ByteArray dec = AES::decipher(output, &key, iv); + int f = 0; + for (auto i = input.begin(); i < input.end(); ++i, ++f) { + // todo: chop the decrypted with what's expected + ASSERT_EQ(*i, dec[f]); + } } } From 30d26ee3eebef7cfd8c8d68c814412477f5a9558 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Mon, 28 Aug 2017 21:51:55 +1000 Subject: [PATCH 091/148] full final --- src/aes.cc | 45 ++++++++++-- src/aes.h | 36 +++++++-- src/base16.cc | 9 +++ src/base16.h | 18 +++-- test/aes-test.h | 190 ++++++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 263 insertions(+), 35 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index e808a27..ab9e86f 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -587,35 +587,64 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) return result; } -ByteArray AES::resolveInputMode(const std::string& input, InputMode inputMode) +ByteArray AES::resolveInputMode(const std::string& input, ConvertMode inputMode) { - if (inputMode == InputMode::Plain) { + if (inputMode == ConvertMode::Plain) { return Base16::fromString(Base16::encode(input)); - } else if (inputMode == InputMode::Base16) { + } else if (inputMode == ConvertMode::Base16) { return Base16::fromString(input); } + // base64 return Base16::fromString(Base16::encode(Base64::decode(input))); } +std::string AES::resolveOutputMode(const ByteArray& input, ConvertMode outputMode) +{ + if (outputMode == ConvertMode::Plain) { + return Base16::toRawString(input); + } else if (outputMode == ConvertMode::Base16) { + return Base16::encode(input.begin(), input.end()); + } + // base64 + return Base64::encode(input.begin(), input.end()); +} + // public -std::string AES::cipher(const std::string& input, const std::string& key, InputMode inputMode) +std::string AES::cipher(const std::string& input, const std::string& key, ConvertMode inputMode, ConvertMode outputEncoding) { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputMode); ByteArray result = cipher(inp, &keyArr); - return Base16::encode(result.begin(), result.end()); + return resolveOutputMode(result, outputEncoding); } -std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, InputMode inputMode) +std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, ConvertMode inputMode, ConvertMode outputEncoding) { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputMode); - ByteArray ivec; + ByteArray ivec = Base16::fromString(iv); bool ivecGenerated = iv.empty(); ByteArray result = cipher(inp, &keyArr, ivec); if (ivecGenerated) { iv = Base16::encode(ivec.begin(), ivec.end()); } - return Base16::encode(result.begin(), result.end()); + return resolveOutputMode(result, outputEncoding); +} + +std::string AES::decipher(const std::string& input, const std::string& key, ConvertMode inputMode, ConvertMode outputEncoding) +{ + Key keyArr = Base16::fromString(key); + ByteArray inp = resolveInputMode(input, inputMode); + ByteArray result = decipher(inp, &keyArr); + return resolveOutputMode(result, outputEncoding); +} + +std::string AES::decipher(const std::string& input, const std::string& key, const std::string& iv, ConvertMode inputMode, ConvertMode outputEncoding) +{ + Key keyArr = Base16::fromString(key); + ByteArray inp = resolveInputMode(input, inputMode); + ByteArray ivec = Base16::fromString(iv); + ByteArray result = decipher(inp, &keyArr, ivec); + return resolveOutputMode(result, outputEncoding); } diff --git a/src/aes.h b/src/aes.h index 4f7110a..a908697 100644 --- a/src/aes.h +++ b/src/aes.h @@ -53,9 +53,9 @@ class AES { public: /// - /// \brief Input mode for various functions + /// \brief Convert mode for various functions /// - enum class InputMode { + enum class ConvertMode { Plain, Base16, Base64 @@ -70,18 +70,39 @@ class AES { /// \brief Ciphers the input with specified hex key /// \param key Hex key /// \param inputMode the type of input. Defaults to Plain + /// \param outputEncoding Type of encoding for cipher /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key, InputMode inputMode = InputMode::Plain); + static std::string cipher(const std::string& input, const std::string& key, ConvertMode inputMode = ConvertMode::Plain, ConvertMode outputEncoding = ConvertMode::Base16); /// /// \brief Ciphers the input with specified hex key using CBC mode /// \param key Hex key /// \param iv Initialization vector, passed by reference. If empty a random is generated and passed in /// \param inputMode the type of input. Defaults to Plain + /// \param outputEncoding Type of encoding for cipher /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key, std::string& iv, InputMode inputMode = InputMode::Plain); + static std::string cipher(const std::string& input, const std::string& key, std::string& iv, ConvertMode inputMode = ConvertMode::Plain, ConvertMode outputEncoding = ConvertMode::Base16); + + /// + /// \brief Deciphers the input with specified hex key + /// \param key Hex key + /// \param inputMode the type of input. Defaults to base16 + /// \param outputEncoding Type of encoding for result + /// \return Base16 encoded cipher + /// + static std::string decipher(const std::string& input, const std::string& key, ConvertMode inputMode = ConvertMode::Base16, ConvertMode outputEncoding = ConvertMode::Plain); + + /// + /// \brief Deciphers the input with specified hex key using CBC mode + /// \param key Hex key + /// \param iv Initialization vector + /// \param inputMode the type of input. Defaults to base16 + /// \param outputEncoding Type of encoding for result + /// \return Base16 encoded cipher + /// + static std::string decipher(const std::string& input, const std::string& key, const std::string& iv, ConvertMode inputMode = ConvertMode::Base16, ConvertMode outputEncoding = ConvertMode::Plain); private: @@ -220,7 +241,12 @@ class AES { /// /// \brief Creates byte array from input based on input mode /// - static ByteArray resolveInputMode(const std::string& input, InputMode inputMode); + static ByteArray resolveInputMode(const std::string& input, ConvertMode inputMode); + + /// + /// \brief Creates string from byte array based on convert mode + /// + static std::string resolveOutputMode(const ByteArray& input, ConvertMode outputMode); /// /// \brief Exclusive XOR with arr diff --git a/src/base16.cc b/src/base16.cc index 68924ee..5b42c25 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -42,6 +42,15 @@ ByteArray Base16::fromString(const std::string& hex) return byteArr; } +std::string Base16::toRawString(const ByteArray& input) +{ + std::ostringstream ss; + for (auto iter = input.begin(); iter < input.end(); ++iter) { + ss << static_cast(*iter); + } + return ss.str(); +} + void Base16::decode(char a, char b, std::ostringstream& ss) { int b0 = a & 0xff; diff --git a/src/base16.h b/src/base16.h index 4d3ec0d..25383d9 100644 --- a/src/base16.h +++ b/src/base16.h @@ -91,6 +91,12 @@ class Base16 { /// static ByteArray fromString(const std::string& hex); + /// + /// \brief Converts byte array to raw string. + /// This does not necessarily has to be base16 array + /// + static std::string toRawString(const ByteArray& byteArr); + /// /// \brief Encodes integer to hex /// @@ -139,11 +145,6 @@ class Base16 { return result; } -private: - Base16() = delete; - Base16(const Base16&) = delete; - Base16& operator=(const Base16&) = delete; - /// /// \brief Encodes single byte /// @@ -153,8 +154,13 @@ class Base16 { ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; } +private: + Base16() = delete; + Base16(const Base16&) = delete; + Base16& operator=(const Base16&) = delete; + /// - /// \brief Encodes input iterator to hex encoding + /// \brief Decodes input iterator to hex encoding /// \note User should check for the valid size or use decode(std::string) /// \throws runtime_error if invalid base16-encoding /// diff --git a/test/aes-test.h b/test/aes-test.h index e13939d..cf41aa3 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -417,13 +417,19 @@ TEST(AESTest, AddRoundKey) // input key expected static TestData RawCipherData = { // 128-bit key - TestCase("00112233445566778899aabbccddeeff", "000102030405060708090a0b0c0d0e0f", "69c4e0d86a7b0430d8cdb78070b4c55a"), + TestCase("00112233445566778899aabbccddeeff", + "000102030405060708090a0b0c0d0e0f", + "69c4e0d86a7b0430d8cdb78070b4c55a"), // 192-bit key - TestCase("00112233445566778899aabbccddeeff", "000102030405060708090a0b0c0d0e0f1011121314151617", "dda97ca4864cdfe06eaf70a0ec0d7191"), + TestCase("00112233445566778899aabbccddeeff", + "000102030405060708090a0b0c0d0e0f1011121314151617", + "dda97ca4864cdfe06eaf70a0ec0d7191"), // 256-bit key - TestCase("00112233445566778899aabbccddeeff", "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", "8ea2b7ca516745bfeafc49904b496089"), + TestCase("00112233445566778899aabbccddeeff", + "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "8ea2b7ca516745bfeafc49904b496089"), }; TEST(AESTest, RawCipher) @@ -442,7 +448,7 @@ TEST(AESTest, RawCipherDirect) for (auto& item : RawCipherData) { std::string expected = PARAM(2); - std::string output = AES::cipher(PARAM(0), PARAM(1), AES::InputMode::Base16); + std::string output = AES::cipher(PARAM(0), PARAM(1), AES::ConvertMode::Base16); // case insensitive comparison because hex can be upper or lower case ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } @@ -457,7 +463,7 @@ TEST(AESTest, RawCipherPlain) { for (auto& item : RawCipherPlainInputData) { std::string expected = PARAM(2); - std::string output = AES::cipher(PARAM(0), PARAM(1), AES::InputMode::Plain); + std::string output = AES::cipher(PARAM(0), PARAM(1), AES::ConvertMode::Plain); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -471,7 +477,7 @@ TEST(AESTest, RawCipherBase64) { for (auto& item : RawCipherBase64InputData) { std::string expected = PARAM(2); - std::string output = AES::cipher(PARAM(0), PARAM(1), AES::InputMode::Base64); + std::string output = AES::cipher(PARAM(0), PARAM(1), AES::ConvertMode::Base64); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -571,15 +577,23 @@ TEST(AESTest, CbcCipher) 0x33, 0x47, 0xdf, 0x21, 0xe4, 0x26, 0xe8, 0x45 }}), - TestCase("this is test longer than 128-bit this is test longer than 128-bit this is test longer than 128-bit", ByteArray {{ - 0x2c, 0x32, 0x8a, 0xa3, 0x6c, 0xff, 0xdd, 0xe8, 0xc1, 0x41, 0x2e, 0x12, 0x10, 0x2f, 0x22, 0x05, - 0x49, 0x32, 0x6a, 0x1c, 0x3c, 0x51, 0xc3, 0x64, 0x33, 0x47, 0xdf, 0x21, 0xe4, 0x26, 0xe8, 0x45, - 0xf0, 0x82, 0xb4, 0x6e, 0xa2, 0xda, 0xcb, 0x82, 0xa3, 0x78, 0x90, 0x47, 0xf3, 0x9a, 0x33, 0x44, - 0x56, 0x8d, 0xa6, 0x1c, 0x66, 0x53, 0x47, 0x96, 0x56, 0x05, 0xb9, 0xa9, 0x78, 0xc8, 0x1e, 0xc6, - 0xa0, 0x46, 0x22, 0x38, 0x62, 0xdb, 0xbe, 0xf9, 0x78, 0xda, 0xdf, 0xc1, 0xe0, 0x57, 0x51, 0x23, - 0x35, 0x67, 0xab, 0xa3, 0x6e, 0x95, 0x02, 0xdd, 0x66, 0x9f, 0x53, 0x00, 0x82, 0x79, 0x4f, 0x5d, - 0xad, 0xe2, 0x58, 0x93, 0xef, 0xe3, 0x2f, 0x52, 0x58, 0x48, 0xd1, 0xef, 0x65, 0x87, 0xc8, 0xc7 - }}), + TestCase("this is test longer than 128-bit this is test " + "longer than 128-bit this is test longer than 128-bit", ByteArray {{ + 0x2c, 0x32, 0x8a, 0xa3, 0x6c, 0xff, 0xdd, 0xe8, + 0xc1, 0x41, 0x2e, 0x12, 0x10, 0x2f, 0x22, 0x05, + 0x49, 0x32, 0x6a, 0x1c, 0x3c, 0x51, 0xc3, 0x64, + 0x33, 0x47, 0xdf, 0x21, 0xe4, 0x26, 0xe8, 0x45, + 0xf0, 0x82, 0xb4, 0x6e, 0xa2, 0xda, 0xcb, 0x82, + 0xa3, 0x78, 0x90, 0x47, 0xf3, 0x9a, 0x33, 0x44, + 0x56, 0x8d, 0xa6, 0x1c, 0x66, 0x53, 0x47, 0x96, + 0x56, 0x05, 0xb9, 0xa9, 0x78, 0xc8, 0x1e, 0xc6, + 0xa0, 0x46, 0x22, 0x38, 0x62, 0xdb, 0xbe, 0xf9, + 0x78, 0xda, 0xdf, 0xc1, 0xe0, 0x57, 0x51, 0x23, + 0x35, 0x67, 0xab, 0xa3, 0x6e, 0x95, 0x02, 0xdd, + 0x66, 0x9f, 0x53, 0x00, 0x82, 0x79, 0x4f, 0x5d, + 0xad, 0xe2, 0x58, 0x93, 0xef, 0xe3, 0x2f, 0x52, + 0x58, 0x48, 0xd1, 0xef, 0x65, 0x87, 0xc8, 0xc7 + }}), }; AES::Key key = {{ @@ -604,10 +618,154 @@ TEST(AESTest, CbcCipher) ByteArray dec = AES::decipher(output, &key, iv); int f = 0; for (auto i = input.begin(); i < input.end(); ++i, ++f) { - // todo: chop the decrypted with what's expected ASSERT_EQ(*i, dec[f]); } } + + // specifies modes of input and output + for (auto& item : CbcCipherTestData) { + std::string expected = Base16::encode(Base16::toRawString(PARAM(1))); + std::string input = PARAM(0); + std::string k = Base16::encode(Base16::toRawString(key)); + std::string initVec = Base16::encode(Base16::toRawString(iv)); + std::string output = AES::cipher(input, k, initVec, + AES::ConvertMode::Plain, + AES::ConvertMode::Base16); + ASSERT_STREQ(expected.c_str(), output.c_str()); + + } +} + +// from FIPS.197 p.35 onwards +// input key expected +static TestData RawDecipherData = { + // 128-bit key + TestCase("00112233445566778899aabbccddeeff", + "000102030405060708090a0b0c0d0e0f", + "69c4e0d86a7b0430d8cdb78070b4c55a"), + + // 192-bit key + TestCase("00112233445566778899aabbccddeeff", + "000102030405060708090a0b0c0d0e0f1011121314151617", + "dda97ca4864cdfe06eaf70a0ec0d7191"), + + // 256-bit key + TestCase("00112233445566778899aabbccddeeff", + "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", + "8ea2b7ca516745bfeafc49904b496089"), +}; + +TEST(AESTest, HexStringDecipher) +{ + for (auto& item : RawDecipherData) { + std::string expected = PARAM(0); + std::string output = AES::decipher(PARAM(2), PARAM(1), AES::ConvertMode::Base16, AES::ConvertMode::Base16); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + } +} + +TEST(AESTest, Base64StringDecipher) +{ + // input mode = base16, + // output mode = base64 + std::string expected = "dGhpcyBpcyB0ZXN0Li4uLg=="; // base64("this is test....") + std::string output = AES::decipher("b92daaae6e57773b10653703af12716f", + "000102030405060708090a0b0c0d0e0f", + AES::ConvertMode::Base16, + AES::ConvertMode::Base64); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); +} + +TEST(AESTest, Base64StringInputDecipher) +{ + std::string expected = "this is test...."; + std::string output = AES::decipher("uS2qrm5XdzsQZTcDrxJxbw==", + "000102030405060708090a0b0c0d0e0f", + AES::ConvertMode::Base64, + AES::ConvertMode::Plain); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); +} + +TEST(AESTest, CbcDecipher) +{ + + AES::Key key = {{ + 0x2b, 0x7e, 0x15, 0x16, + 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, + 0x09, 0xcf, 0x4f, 0x3c + }}; + ByteArray iv = {{ + 0x20, 0xc7, 0x04, 0x40, + 0xac, 0x40, 0x0d, 0xba, + 0x84, 0x06, 0x57, 0x00, + 0x74, 0xf2, 0xe2, 0x2a + }}; + + // input expected + static TestData CbcDecipherTestData = { + TestCase("this is test....", ByteArray {{ + 0xa3, 0xd9, 0x36, 0xf1, + 0xfe, 0xd3, 0xb8, 0xd3, + 0xe7, 0x4e, 0x09, 0x4e, + 0x2c, 0x0f, 0x1b, 0xd9 + }}), + TestCase("this is test.", ByteArray {{ + 0x86, 0xae, 0xc0, 0x99, + 0xfc, 0x4e, 0xba, 0x5f, + 0xcd, 0xaa, 0xd2, 0x94, + 0x96, 0x48, 0x01, 0x65 + }}), + TestCase("this is test longer", ByteArray {{ + 0x2c, 0x32, 0x8a, 0xa3, + 0x6c, 0xff, 0xdd, 0xe8, + 0xc1, 0x41, 0x2e, 0x12, + 0x10, 0x2f, 0x22, 0x05, + 0xe7, 0x4e, 0x98, 0x8c, + 0x9a, 0x15, 0x42, 0xe9, + 0x72, 0x84, 0xc5, 0x19, + 0x76, 0xc2, 0x6a, 0x44 + }}), + TestCase("this is test longer than 128-bit", ByteArray {{ + 0x2c, 0x32, 0x8a, 0xa3, + 0x6c, 0xff, 0xdd, 0xe8, + 0xc1, 0x41, 0x2e, 0x12, + 0x10, 0x2f, 0x22, 0x05, + 0x49, 0x32, 0x6a, 0x1c, + 0x3c, 0x51, 0xc3, 0x64, + 0x33, 0x47, 0xdf, 0x21, + 0xe4, 0x26, 0xe8, 0x45 + }}), + TestCase("this is test longer than 128-bit this is test " + "longer than 128-bit this is test longer than 128-bit", ByteArray {{ + 0x2c, 0x32, 0x8a, 0xa3, 0x6c, 0xff, 0xdd, 0xe8, + 0xc1, 0x41, 0x2e, 0x12, 0x10, 0x2f, 0x22, 0x05, + 0x49, 0x32, 0x6a, 0x1c, 0x3c, 0x51, 0xc3, 0x64, + 0x33, 0x47, 0xdf, 0x21, 0xe4, 0x26, 0xe8, 0x45, + 0xf0, 0x82, 0xb4, 0x6e, 0xa2, 0xda, 0xcb, 0x82, + 0xa3, 0x78, 0x90, 0x47, 0xf3, 0x9a, 0x33, 0x44, + 0x56, 0x8d, 0xa6, 0x1c, 0x66, 0x53, 0x47, 0x96, + 0x56, 0x05, 0xb9, 0xa9, 0x78, 0xc8, 0x1e, 0xc6, + 0xa0, 0x46, 0x22, 0x38, 0x62, 0xdb, 0xbe, 0xf9, + 0x78, 0xda, 0xdf, 0xc1, 0xe0, 0x57, 0x51, 0x23, + 0x35, 0x67, 0xab, 0xa3, 0x6e, 0x95, 0x02, 0xdd, + 0x66, 0x9f, 0x53, 0x00, 0x82, 0x79, 0x4f, 0x5d, + 0xad, 0xe2, 0x58, 0x93, 0xef, 0xe3, 0x2f, 0x52, + 0x58, 0x48, 0xd1, 0xef, 0x65, 0x87, 0xc8, 0xc7 + }}), + }; + + for (auto& item : CbcDecipherTestData) { + std::string expected = PARAM(0); + std::string input = Base16::toRawString(PARAM(1)); + std::string k = Base16::encode(Base16::toRawString(key)); + std::string initVec = Base16::encode(Base16::toRawString(iv)); + std::string output = AES::decipher(input, k, initVec, + AES::ConvertMode::Plain, + AES::ConvertMode::Plain); + ASSERT_STREQ(expected.c_str(), output.c_str()); + + } } } From 9f92037186c7b06c7e3ec5ccc1b4d85a1c54bb24 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Mon, 28 Aug 2017 21:57:26 +1000 Subject: [PATCH 092/148] refactor raw functions --- src/aes.cc | 58 ++++++++++++++++++++++++------------------------- src/aes.h | 4 ++-- test/aes-test.h | 6 ++--- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index ab9e86f..31d03b0 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -423,7 +423,30 @@ ByteArray AES::stateToByteArray(const State *state) return result; } -ByteArray AES::cipher(const ByteArray& input, const Key* key) +ByteArray AES::generateRandomBytes(const std::size_t len) +{ + ByteArray result; + const int kMax = 999; + srand(time(nullptr)); + for (std::size_t i = 0; i < len; ++i) { + int r = rand() % kMax; + while (r == 0) { + r = rand() % kMax + 1; + } + result.push_back(static_cast(r)); + } + return result; +} + +ByteArray* AES::xorWith(ByteArray* input, const ByteArray* arr) +{ + for (std::size_t i = 0; i < kBlockSize; ++i) { + input->at(i) ^= arr->at(i); + } + return input; +} + +ByteArray AES::rawCipher(const ByteArray& input, const Key* key) { State state; @@ -456,30 +479,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) } -ByteArray AES::generateRandomBytes(const std::size_t len) -{ - ByteArray result; - const int kMax = 999; - srand(time(nullptr)); - for (std::size_t i = 0; i < len; ++i) { - int r = rand() % kMax; - while (r == 0) { - r = rand() % kMax + 1; - } - result.push_back(static_cast(r)); - } - return result; -} - -ByteArray* AES::xorWith(ByteArray* input, const ByteArray* arr) -{ - for (std::size_t i = 0; i < kBlockSize; ++i) { - input->at(i) ^= arr->at(i); - } - return input; -} - -ByteArray AES::decipher(const ByteArray& input, const Key* key) +ByteArray AES::rawDecipher(const ByteArray& input, const Key* key) { State state; @@ -543,7 +543,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) xorWith(&inputBlock, &nextXorWith); - ByteArray outputBlock = cipher(inputBlock, key); + ByteArray outputBlock = rawCipher(inputBlock, key); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); nextXorWith = outputBlock; } @@ -577,7 +577,7 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) inputBlock.at(j) = input.at(j + i); } - ByteArray outputBlock = decipher(inputBlock, key); + ByteArray outputBlock = rawDecipher(inputBlock, key); xorWith(&outputBlock, &nextXorWith); nextXorWith = inputBlock; @@ -615,7 +615,7 @@ std::string AES::cipher(const std::string& input, const std::string& key, Conver { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputMode); - ByteArray result = cipher(inp, &keyArr); + ByteArray result = rawCipher(inp, &keyArr); return resolveOutputMode(result, outputEncoding); } @@ -636,7 +636,7 @@ std::string AES::decipher(const std::string& input, const std::string& key, Conv { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputMode); - ByteArray result = decipher(inp, &keyArr); + ByteArray result = rawDecipher(inp, &keyArr); return resolveOutputMode(result, outputEncoding); } diff --git a/src/aes.h b/src/aes.h index a908697..5f439a6 100644 --- a/src/aes.h +++ b/src/aes.h @@ -263,7 +263,7 @@ class AES { /// \note This does not do any key or input validation /// \return 128-bit cipher text /// - static ByteArray cipher(const ByteArray& input, const Key* key); + static ByteArray rawCipher(const ByteArray& input, const Key* key); /// /// \brief Ciphers with CBC-Mode, the input can be as long as user wants @@ -283,7 +283,7 @@ class AES { /// \param key Byte array of key /// \return 128-bit plain text /// - static ByteArray decipher(const ByteArray& input, const Key* key); + static ByteArray rawDecipher(const ByteArray& input, const Key* key); /// /// \brief Deciphers with CBC-Mode, the input can be as long as user wants diff --git a/test/aes-test.h b/test/aes-test.h index cf41aa3..7348be1 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -438,7 +438,7 @@ TEST(AESTest, RawCipher) ByteArray input = Base16::fromString(PARAM(0)); AES::Key key = static_cast(Base16::fromString(PARAM(1))); ByteArray expected = Base16::fromString(PARAM(2)); - ByteArray output = AES::cipher(input, &key); + ByteArray output = AES::rawCipher(input, &key); ASSERT_EQ(expected, output); } } @@ -507,7 +507,7 @@ TEST(AESTest, RawSimpleCipher) 0x19, 0x6a, 0x0b, 0x32 }}; - ByteArray output = AES::cipher(input, &key); + ByteArray output = AES::rawCipher(input, &key); ASSERT_EQ(expected, output); } @@ -537,7 +537,7 @@ TEST(AESTest, RawSimpleDecipher) 0xe0, 0x37, 0x07, 0x34 }}; - ByteArray output = AES::decipher(input, &key); + ByteArray output = AES::rawDecipher(input, &key); ASSERT_EQ(expected, output); } From 288c2a571fe19beadb868a41d266518df5add511 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Mon, 28 Aug 2017 22:38:30 +1000 Subject: [PATCH 093/148] final fixes for aes --- src/aes.cc | 61 ++++++++++++++++++++++++++++++++++++++++++++-- src/aes.h | 36 ++++++++++++++++++++------- src/base64.h | 2 +- test/aes-test.h | 4 +-- test/base64-test.h | 12 +++++++++ 5 files changed, 101 insertions(+), 14 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index 31d03b0..bbedf61 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -511,6 +511,63 @@ ByteArray AES::rawDecipher(const ByteArray& input, const Key* key) return stateToByteArray(&state); } +ByteArray AES::cipher(const ByteArray& input, const Key* key) +{ + + std::size_t keySize = key->size(); + + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + const std::size_t inputSize = input.size(); + + ByteArray result; + + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { + ByteArray inputBlock(kBlockSize, 0); + + // don't use copy_n as we are setting the values + for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { + inputBlock.at(j) = input.at(j + i); + } + + ByteArray outputBlock = rawCipher(inputBlock, key); + std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); + } + return result; +} + +ByteArray AES::decipher(const ByteArray& input, const Key* key) +{ + + std::size_t keySize = key->size(); + + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + const std::size_t inputSize = input.size(); + ByteArray result; + + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { + ByteArray inputBlock(kBlockSize, 0); + + std::size_t j = 0; + // don't use copy_n here as we are setting the values + for (; j < kBlockSize && inputSize > j + i; ++j) { + inputBlock.at(j) = input.at(j + i); + } + + ByteArray outputBlock = rawDecipher(inputBlock, key); + + std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); + } + return result; +} + ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) { @@ -615,7 +672,7 @@ std::string AES::cipher(const std::string& input, const std::string& key, Conver { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputMode); - ByteArray result = rawCipher(inp, &keyArr); + ByteArray result = cipher(inp, &keyArr); return resolveOutputMode(result, outputEncoding); } @@ -636,7 +693,7 @@ std::string AES::decipher(const std::string& input, const std::string& key, Conv { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputMode); - ByteArray result = rawDecipher(inp, &keyArr); + ByteArray result = decipher(inp, &keyArr); return resolveOutputMode(result, outputEncoding); } diff --git a/src/aes.h b/src/aes.h index 5f439a6..7e3e02f 100644 --- a/src/aes.h +++ b/src/aes.h @@ -265,15 +265,6 @@ class AES { /// static ByteArray rawCipher(const ByteArray& input, const Key* key); - /// - /// \brief Ciphers with CBC-Mode, the input can be as long as user wants - /// \param input Plain input of any length - /// \param key Pointer to a valid AES key - /// \param iv Initialization vector - /// \return Cipher text byte array - /// - static ByteArray cipher(const ByteArray& input, const Key* key, ByteArray& iv); - /// /// \brief Raw decryption function - not for public use /// \param input 128-bit cipher input @@ -285,6 +276,33 @@ class AES { /// static ByteArray rawDecipher(const ByteArray& input, const Key* key); + /// + /// \brief Ciphers with ECB-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray cipher(const ByteArray& input, const Key* key); + + /// + /// \brief Deciphers with ECB-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray decipher(const ByteArray& input, const Key* key); + + /// + /// \brief Ciphers with CBC-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray cipher(const ByteArray& input, const Key* key, ByteArray& iv); + /// /// \brief Deciphers with CBC-Mode, the input can be as long as user wants /// \param input Plain input of any length diff --git a/src/base64.h b/src/base64.h index 93db7a6..3a178c2 100644 --- a/src/base64.h +++ b/src/base64.h @@ -192,7 +192,7 @@ class Base64 { const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); std::stringstream ss; - for (auto it = begin; it != end; it += 4) { + for (auto it = begin; it < end; it += 4) { try { int b0 = kDecodeMap.at(static_cast(*it & 0xff)); if (b0 == kPadding || b0 == '\0') { diff --git a/test/aes-test.h b/test/aes-test.h index 7348be1..088e1cf 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -678,8 +678,8 @@ TEST(AESTest, Base64StringDecipher) TEST(AESTest, Base64StringInputDecipher) { - std::string expected = "this is test...."; - std::string output = AES::decipher("uS2qrm5XdzsQZTcDrxJxbw==", + std::string expected = "this is test.."; + std::string output = AES::decipher("Z0BiQ8NcwknqzbGrWBjXqw==", "000102030405060708090a0b0c0d0e0f", AES::ConvertMode::Base64, AES::ConvertMode::Plain); diff --git a/test/base64-test.h b/test/base64-test.h index 067cb04..ea390e4 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -99,6 +99,18 @@ TEST(Base64Test, Decode) } } +static TestData Base64SizeTestData = { + TestCase("Z0BiQ8NcwknqzbGrWBjXqw==", 16), +}; + +TEST(Base64Test, DecodeRawSize) +{ + for (const auto& item : Base64SizeTestData) { + std::string decoded = Base64::decode(PARAM(0)); + ASSERT_EQ(PARAM(1), decoded.size()); + } +} + #ifdef MINE_BASE64_WSTRING_CONVERSION TEST(Base64Test, EncodeWString) { From 05be677eca84be83f62a2f8be4f7e64488f875d8 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Mon, 28 Aug 2017 22:51:31 +1000 Subject: [PATCH 094/148] added b64 fix for padding offset fo partial byte --- src/base64.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/base64.h b/src/base64.h index 3a178c2..52ff665 100644 --- a/src/base64.h +++ b/src/base64.h @@ -207,8 +207,12 @@ class Base64 { if (b1 != kPadding && b1 != '\0') { if (b2 == kPadding || (b2 == '\0' && b3 == '\0')) { - // second biteset is 'partial byte' - ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); + // second bitset is 'partial byte' + + // note: this line was causing issue when we had plain text length 16 + // b64 = uS2qrm5XdzsQZTcDrxJxbw== + // it was adding a nul term char + //ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); } else { ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 // first we clear the bits at pos 4 and 5 From 63cd3d9f6ea60c54c6c4a9d90e0418ddb60b2f25 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Mon, 28 Aug 2017 22:52:23 +1000 Subject: [PATCH 095/148] build package --- package/mine.cc | 239 ++++++++++++++++++++++++++++++++++++------------ package/mine.h | 112 ++++++++++++++++++----- 2 files changed, 270 insertions(+), 81 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 934e41c..a1f7f74 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -48,6 +48,15 @@ ByteArray Base16::fromString(const std::string& hex) return byteArr; } +std::string Base16::toRawString(const ByteArray& input) +{ + std::ostringstream ss; + for (auto iter = input.begin(); iter < input.end(); ++iter) { + ss << static_cast(*iter); + } + return ss.str(); +} + void Base16::decode(char a, char b, std::ostringstream& ss) { int b0 = a & 0xff; @@ -500,7 +509,30 @@ ByteArray AES::stateToByteArray(const State *state) return result; } -ByteArray AES::cipher(const ByteArray& input, const Key* key) +ByteArray AES::generateRandomBytes(const std::size_t len) +{ + ByteArray result; + const int kMax = 999; + srand(time(nullptr)); + for (std::size_t i = 0; i < len; ++i) { + int r = rand() % kMax; + while (r == 0) { + r = rand() % kMax + 1; + } + result.push_back(static_cast(r)); + } + return result; +} + +ByteArray* AES::xorWith(ByteArray* input, const ByteArray* arr) +{ + for (std::size_t i = 0; i < kBlockSize; ++i) { + input->at(i) ^= arr->at(i); + } + return input; +} + +ByteArray AES::rawCipher(const ByteArray& input, const Key* key) { State state; @@ -533,29 +565,94 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) } -ByteArray AES::generateRandomBytes(const std::size_t len) +ByteArray AES::rawDecipher(const ByteArray& input, const Key* key) { + + State state; + initState(&state, input); + + uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; + + // Create linear subkeys (key schedule) + KeySchedule keySchedule = keyExpansion(key); + + int round = kTotalRounds; + + // initial round + addRoundKey(&state, &keySchedule, round--); + + // intermediate round + while (round > 0) { + invShiftRows(&state); + invSubBytes(&state); + addRoundKey(&state, &keySchedule, round--); + invMixColumns(&state); + } + + // final round + invShiftRows(&state); + invSubBytes(&state); + addRoundKey(&state, &keySchedule, round); + + return stateToByteArray(&state); +} + +ByteArray AES::cipher(const ByteArray& input, const Key* key) +{ + + std::size_t keySize = key->size(); + + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + const std::size_t inputSize = input.size(); + ByteArray result; - const int kMax = 999; - srand(time(nullptr)); - for (std::size_t i = 0; i < len; ++i) { - int r = rand() % kMax; - while (r == 0) { - r = rand() % kMax + 1; + + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { + ByteArray inputBlock(kBlockSize, 0); + + // don't use copy_n as we are setting the values + for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { + inputBlock.at(j) = input.at(j + i); } - result.push_back(static_cast(r)); + + ByteArray outputBlock = rawCipher(inputBlock, key); + std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); } return result; } -ByteArray AES::xorWith(ByteArray& input, const ByteArray& arr) +ByteArray AES::decipher(const ByteArray& input, const Key* key) { - for (std::size_t i = 0; i < kBlockSize; ++i) { - input.at(i) ^= arr.at(i); + + std::size_t keySize = key->size(); + + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); } - return input; -} + const std::size_t inputSize = input.size(); + ByteArray result; + + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { + ByteArray inputBlock(kBlockSize, 0); + + std::size_t j = 0; + // don't use copy_n here as we are setting the values + for (; j < kBlockSize && inputSize > j + i; ++j) { + inputBlock.at(j) = input.at(j + i); + } + + ByteArray outputBlock = rawDecipher(inputBlock, key); + + std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); + } + return result; +} ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) { @@ -575,6 +672,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) } const std::size_t inputSize = input.size(); + ByteArray result; ByteArray nextXorWith = iv; @@ -586,17 +684,18 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) inputBlock.at(j) = input.at(j + i); } - xorWith(inputBlock, nextXorWith); + xorWith(&inputBlock, &nextXorWith); - ByteArray outputBlock = cipher(inputBlock, key); + ByteArray outputBlock = rawCipher(inputBlock, key); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); nextXorWith = outputBlock; } return result; } -ByteArray AES::decipher(const ByteArray& input, const Key* key) +ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) { + std::size_t keySize = key->size(); // key size validation @@ -604,71 +703,93 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) throw std::invalid_argument("Invalid AES key size"); } - State state; - initState(&state, input); + if (input.size() % 16 != 0) { + throw std::invalid_argument("Invalid AES cipher"); + } - uint8_t kTotalRounds = kKeyParams.at(keySize)[1]; + const std::size_t inputSize = input.size(); + ByteArray result; - // Create linear subkeys (key schedule) - KeySchedule keySchedule = keyExpansion(key); + ByteArray nextXorWith = iv; - int round = kTotalRounds; + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { + ByteArray inputBlock(kBlockSize, 0); - // initial round - addRoundKey(&state, &keySchedule, round--); + // don't use copy_n as we are setting the values + for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { + inputBlock.at(j) = input.at(j + i); + } - // intermediate round - while (round > 0) { - invShiftRows(&state); - invSubBytes(&state); - addRoundKey(&state, &keySchedule, round--); - invMixColumns(&state); - } + ByteArray outputBlock = rawDecipher(inputBlock, key); - // final round - invShiftRows(&state); - invSubBytes(&state); - addRoundKey(&state, &keySchedule, round); + xorWith(&outputBlock, &nextXorWith); + nextXorWith = inputBlock; - return stateToByteArray(&state); + std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); + } + return result; +} + +ByteArray AES::resolveInputMode(const std::string& input, ConvertMode inputMode) +{ + if (inputMode == ConvertMode::Plain) { + return Base16::fromString(Base16::encode(input)); + } else if (inputMode == ConvertMode::Base16) { + return Base16::fromString(input); + } + // base64 + return Base16::fromString(Base16::encode(Base64::decode(input))); +} +std::string AES::resolveOutputMode(const ByteArray& input, ConvertMode outputMode) +{ + if (outputMode == ConvertMode::Plain) { + return Base16::toRawString(input); + } else if (outputMode == ConvertMode::Base16) { + return Base16::encode(input.begin(), input.end()); + } + // base64 + return Base64::encode(input.begin(), input.end()); } // public -std::string AES::cipher(const std::string& input, const std::string& key, InputMode inputMode) +std::string AES::cipher(const std::string& input, const std::string& key, ConvertMode inputMode, ConvertMode outputEncoding) { Key keyArr = Base16::fromString(key); - ByteArray inp; - if (inputMode == InputMode::Plain) { - inp = Base16::fromString(Base16::encode(input)); - } else if (inputMode == InputMode::Base16) { - inp = Base16::fromString(input); - } else { // base64 - inp = Base16::fromString(Base16::encode(Base64::decode(input))); - } + ByteArray inp = resolveInputMode(input, inputMode); ByteArray result = cipher(inp, &keyArr); - return Base16::encode(result.begin(), result.end()); + return resolveOutputMode(result, outputEncoding); } -std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, InputMode inputMode) +std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, ConvertMode inputMode, ConvertMode outputEncoding) { - bool ivecGenerated = iv.empty(); - ByteArray ivec; Key keyArr = Base16::fromString(key); - ByteArray inp; - if (inputMode == InputMode::Plain) { - inp = Base16::fromString(Base16::encode(input)); - } else if (inputMode == InputMode::Base16) { - inp = Base16::fromString(input); - } else { // base64 - inp = Base16::fromString(Base16::encode(Base64::decode(input))); - } + ByteArray inp = resolveInputMode(input, inputMode); + ByteArray ivec = Base16::fromString(iv); + bool ivecGenerated = iv.empty(); ByteArray result = cipher(inp, &keyArr, ivec); if (ivecGenerated) { iv = Base16::encode(ivec.begin(), ivec.end()); } - return Base16::encode(result.begin(), result.end()); + return resolveOutputMode(result, outputEncoding); +} + +std::string AES::decipher(const std::string& input, const std::string& key, ConvertMode inputMode, ConvertMode outputEncoding) +{ + Key keyArr = Base16::fromString(key); + ByteArray inp = resolveInputMode(input, inputMode); + ByteArray result = decipher(inp, &keyArr); + return resolveOutputMode(result, outputEncoding); +} + +std::string AES::decipher(const std::string& input, const std::string& key, const std::string& iv, ConvertMode inputMode, ConvertMode outputEncoding) +{ + Key keyArr = Base16::fromString(key); + ByteArray inp = resolveInputMode(input, inputMode); + ByteArray ivec = Base16::fromString(iv); + ByteArray result = decipher(inp, &keyArr, ivec); + return resolveOutputMode(result, outputEncoding); } diff --git a/package/mine.h b/package/mine.h index 823d0e8..b63a8d5 100644 --- a/package/mine.h +++ b/package/mine.h @@ -91,6 +91,12 @@ class Base16 { /// static ByteArray fromString(const std::string& hex); + /// + /// \brief Converts byte array to raw string. + /// This does not necessarily has to be base16 array + /// + static std::string toRawString(const ByteArray& byteArr); + /// /// \brief Encodes integer to hex /// @@ -139,11 +145,6 @@ class Base16 { return result; } -private: - Base16() = delete; - Base16(const Base16&) = delete; - Base16& operator=(const Base16&) = delete; - /// /// \brief Encodes single byte /// @@ -153,8 +154,13 @@ class Base16 { ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; } +private: + Base16() = delete; + Base16(const Base16&) = delete; + Base16& operator=(const Base16&) = delete; + /// - /// \brief Encodes input iterator to hex encoding + /// \brief Decodes input iterator to hex encoding /// \note User should check for the valid size or use decode(std::string) /// \throws runtime_error if invalid base16-encoding /// @@ -332,7 +338,7 @@ class Base64 { const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); std::stringstream ss; - for (auto it = begin; it != end; it += 4) { + for (auto it = begin; it < end; it += 4) { try { int b0 = kDecodeMap.at(static_cast(*it & 0xff)); if (b0 == kPadding || b0 == '\0') { @@ -347,8 +353,12 @@ class Base64 { if (b1 != kPadding && b1 != '\0') { if (b2 == kPadding || (b2 == '\0' && b3 == '\0')) { - // second biteset is 'partial byte' - ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); + // second bitset is 'partial byte' + + // note: this line was causing issue when we had plain text length 16 + // b64 = uS2qrm5XdzsQZTcDrxJxbw== + // it was adding a nul term char + //ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); } else { ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 // first we clear the bits at pos 4 and 5 @@ -452,9 +462,9 @@ class AES { public: /// - /// \brief Input mode for various functions + /// \brief Convert mode for various functions /// - enum class InputMode { + enum class ConvertMode { Plain, Base16, Base64 @@ -469,18 +479,39 @@ class AES { /// \brief Ciphers the input with specified hex key /// \param key Hex key /// \param inputMode the type of input. Defaults to Plain + /// \param outputEncoding Type of encoding for cipher /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key, InputMode inputMode = InputMode::Plain); + static std::string cipher(const std::string& input, const std::string& key, ConvertMode inputMode = ConvertMode::Plain, ConvertMode outputEncoding = ConvertMode::Base16); /// /// \brief Ciphers the input with specified hex key using CBC mode /// \param key Hex key /// \param iv Initialization vector, passed by reference. If empty a random is generated and passed in /// \param inputMode the type of input. Defaults to Plain + /// \param outputEncoding Type of encoding for cipher /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key, std::string& iv, InputMode inputMode = InputMode::Plain); + static std::string cipher(const std::string& input, const std::string& key, std::string& iv, ConvertMode inputMode = ConvertMode::Plain, ConvertMode outputEncoding = ConvertMode::Base16); + + /// + /// \brief Deciphers the input with specified hex key + /// \param key Hex key + /// \param inputMode the type of input. Defaults to base16 + /// \param outputEncoding Type of encoding for result + /// \return Base16 encoded cipher + /// + static std::string decipher(const std::string& input, const std::string& key, ConvertMode inputMode = ConvertMode::Base16, ConvertMode outputEncoding = ConvertMode::Plain); + + /// + /// \brief Deciphers the input with specified hex key using CBC mode + /// \param key Hex key + /// \param iv Initialization vector + /// \param inputMode the type of input. Defaults to base16 + /// \param outputEncoding Type of encoding for result + /// \return Base16 encoded cipher + /// + static std::string decipher(const std::string& input, const std::string& key, const std::string& iv, ConvertMode inputMode = ConvertMode::Base16, ConvertMode outputEncoding = ConvertMode::Plain); private: @@ -616,10 +647,20 @@ class AES { /// static ByteArray generateRandomBytes(const std::size_t len); + /// + /// \brief Creates byte array from input based on input mode + /// + static ByteArray resolveInputMode(const std::string& input, ConvertMode inputMode); + + /// + /// \brief Creates string from byte array based on convert mode + /// + static std::string resolveOutputMode(const ByteArray& input, ConvertMode outputMode); + /// /// \brief Exclusive XOR with arr /// - static ByteArray xorWith(ByteArray &input, const ByteArray& arr); + static ByteArray* xorWith(ByteArray* input, const ByteArray* arr); /// /// \brief Raw encryption function - not for public use @@ -631,8 +672,37 @@ class AES { /// \note This does not do any key or input validation /// \return 128-bit cipher text /// + static ByteArray rawCipher(const ByteArray& input, const Key* key); + + /// + /// \brief Raw decryption function - not for public use + /// \param input 128-bit cipher input + /// If array is bigger it's chopped and if it's smaller, it's padded + /// please use alternative functions if your array is bigger. Those + /// function will handle all the bytes correctly. + /// \param key Byte array of key + /// \return 128-bit plain text + /// + static ByteArray rawDecipher(const ByteArray& input, const Key* key); + + /// + /// \brief Ciphers with ECB-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// static ByteArray cipher(const ByteArray& input, const Key* key); + /// + /// \brief Deciphers with ECB-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray decipher(const ByteArray& input, const Key* key); + /// /// \brief Ciphers with CBC-Mode, the input can be as long as user wants /// \param input Plain input of any length @@ -643,15 +713,13 @@ class AES { static ByteArray cipher(const ByteArray& input, const Key* key, ByteArray& iv); /// - /// \brief Raw decryption function - not for public use - /// \param input 128-bit cipher input - /// If array is bigger it's chopped and if it's smaller, it's padded - /// please use alternative functions if your array is bigger. Those - /// function will handle all the bytes correctly. - /// \param key Byte array of key - /// \return 128-bit plain text + /// \brief Deciphers with CBC-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array /// - static ByteArray decipher(const ByteArray& input, const Key* key); + static ByteArray decipher(const ByteArray& input, const Key* key, ByteArray& iv); /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array From 31197ad3dc7107eb3f01459c121423fad1d215b8 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Mon, 28 Aug 2017 23:59:29 +1000 Subject: [PATCH 096/148] some issues raised --- package/mine.cc | 4 ---- src/aes.cc | 4 ---- test/aes-test.h | 23 +++++++++++++++++++++++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index a1f7f74..e74146e 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -703,10 +703,6 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) throw std::invalid_argument("Invalid AES key size"); } - if (input.size() % 16 != 0) { - throw std::invalid_argument("Invalid AES cipher"); - } - const std::size_t inputSize = input.size(); ByteArray result; diff --git a/src/aes.cc b/src/aes.cc index bbedf61..0049e8a 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -617,10 +617,6 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) throw std::invalid_argument("Invalid AES key size"); } - if (input.size() % 16 != 0) { - throw std::invalid_argument("Invalid AES cipher"); - } - const std::size_t inputSize = input.size(); ByteArray result; diff --git a/test/aes-test.h b/test/aes-test.h index 088e1cf..d6cb6e9 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -768,6 +768,29 @@ TEST(AESTest, CbcDecipher) } } +TEST(AESTest, RealDataIssues) +{ + std::string expected = R"("{"_t":1503928197,"logger_id":"default","access_code":"default"})"; + std::string output = AES::decipher("EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ==", + "CBD437FA37772C66051A47D72367B38E", + "a14c54563269e9e368f56b325f04ff00", + AES::ConvertMode::Base64, + AES::ConvertMode::Plain); + + // + // this worked => the only issue was padding std::string r = mine::AES::decipher(Ripe::stringToHex(Ripe::base64Decode(raw)), key, iv, mine::AES::ConvertMode::Base16, mine::AES::ConvertMode::Plain); + + //ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + + expected = R"("{"_t":1503928197,"logger_id":"default","access_code":"default"})"; + output = AES::decipher("12D62BE49168FFB92A616C68A0CBD4D8327EBA936150C0E28BD5FA2041D8C6F50D5D2546937E086A44F91FB19BCB32F9FC920C3004022E7514F36E11237CA681", + "CBD437FA37772C66051A47D72367B38E", + "a14c54563269e9e368f56b325f04ff00", + AES::ConvertMode::Base16, + AES::ConvertMode::Plain); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); +} + } #endif // AES_TEST_H From 1616659b7378b027a048fdf8994fedd74deab7bd Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Tue, 29 Aug 2017 08:21:44 +1000 Subject: [PATCH 097/148] moved issue to base16 issue --- test/base64-test.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/base64-test.h b/test/base64-test.h index ea390e4..f98f6d3 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -7,6 +7,7 @@ # include "package/mine.h" #else # include "src/base64.h" +# include "src/base16.h" #endif namespace mine { @@ -99,15 +100,18 @@ TEST(Base64Test, Decode) } } -static TestData Base64SizeTestData = { - TestCase("Z0BiQ8NcwknqzbGrWBjXqw==", 16), +static TestData Base64RawTestData = { + TestCase("Z0BiQ8NcwknqzbGrWBjXqw==", 16, "67406243C35CC249EACDB1AB5818D7AB"), + TestCase("EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ==", 64, "12D62BE49168FFB92A616C68A0CBD4D8327EBA936150C0E28BD5FA2041D8C6F50D5D2546937E086A44F91FB19BCB32F9FC920C3004022E7514F36E11237CA681") }; TEST(Base64Test, DecodeRawSize) { - for (const auto& item : Base64SizeTestData) { + for (const auto& item : Base64RawTestData) { std::string decoded = Base64::decode(PARAM(0)); ASSERT_EQ(PARAM(1), decoded.size()); + std::string b16 = Base16::encode(decoded); + ASSERT_STRCASEEQ(PARAM(2).c_str(), b16.c_str()); } } From 1b52f4fd57fe9a3b5571ea61b4dbab13b02452b4 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 09:40:34 +1000 Subject: [PATCH 098/148] b64 change error type to argument --- src/base64.h | 16 ++++++++-------- test/base64-test.h | 14 +++++++------- test/main.cc | 8 ++++---- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/base64.h b/src/base64.h index 52ff665..7d84c96 100644 --- a/src/base64.h +++ b/src/base64.h @@ -169,16 +169,16 @@ class Base64 { static std::string decode(const std::string& e) { if (e.size() % 4 != 0) { - throw std::runtime_error("Invalid base64 encoding. Padding is required"); + throw std::invalid_argument("Invalid base64 encoding. Padding is required"); } return decode(e.begin(), e.end()); } /// /// \brief Decodes base64 iterator from begin to end - /// \throws std::runtime if invalid encoding. Another time it is thrown + /// \throws std::invalid_argument if invalid encoding. Another time it is thrown /// is if no padding is found - /// std::runtime::what() is set according to the error + /// std::invalid_argument::what() is set according to the error /// template static std::string decode(const Iter& begin, const Iter& end) @@ -196,7 +196,7 @@ class Base64 { try { int b0 = kDecodeMap.at(static_cast(*it & 0xff)); if (b0 == kPadding || b0 == '\0') { - throw std::runtime_error("Invalid base64 encoding. No data available"); + throw std::invalid_argument("Invalid base64 encoding. No data available"); } int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); @@ -207,19 +207,19 @@ class Base64 { if (b1 != kPadding && b1 != '\0') { if (b2 == kPadding || (b2 == '\0' && b3 == '\0')) { - // second bitset is 'partial byte' + // second bitset is only 4 bits // note: this line was causing issue when we had plain text length 16 // b64 = uS2qrm5XdzsQZTcDrxJxbw== // it was adding a nul term char - //ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); + ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); } else { ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 // first we clear the bits at pos 4 and 5 // then we concat with next bit b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 if (b3 == kPadding || b3 == '\0') { - // third bitset is 'partial byte' + // third bitset is only 4 bits ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); // first we clear first 4 bits } else { @@ -231,7 +231,7 @@ class Base64 { } } } catch (const std::exception&) { - throw std::runtime_error("Invalid base64 character"); + throw std::invalid_argument("Invalid base64 character"); } } return ss.str(); diff --git a/test/base64-test.h b/test/base64-test.h index f98f6d3..e3782a1 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -14,7 +14,7 @@ namespace mine { static TestData Base64TestData = { // examples from https://en.wikipedia.org/wiki/Base64#Output_padding - TestCase("YWJjZA==", "abcd"), +/* TestCase("YWJjZA==", "abcd"), TestCase("YW55IGNhcm5hbCBwbGVhc3VyZS4=", "any carnal pleasure."), TestCase("YW55IGNhcm5hbCBwbGVhc3VyZQ==", "any carnal pleasure"), TestCase("YW55IGNhcm5hbCBwbGVhc3Vy", "any carnal pleasur"), @@ -29,12 +29,12 @@ static TestData Base64TestData = { TestCase("4oKsNTA=", "€50"), TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", "this is rocket 🚀 and i love it"), TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"), - TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), + TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"),*/ }; static TestData Base64WStringTestData = { // examples from https://en.wikipedia.org/wiki/Base64#Output_padding - TestCase("YWJjZA==", L"abcd"), +/* TestCase("YWJjZA==", L"abcd"), TestCase("YW55IGNhcm5hbCBwbGVhc3VyZS4=", L"any carnal pleasure."), TestCase("YW55IGNhcm5hbCBwbGVhc3VyZQ==", L"any carnal pleasure"), TestCase("YW55IGNhcm5hbCBwbGVhc3Vy", L"any carnal pleasur"), @@ -50,14 +50,14 @@ static TestData Base64WStringTestData = { // Commenting and leaving it here on purpose, see note on decodeAsWString // TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", L"this is rocket 🚀 and i love it"), TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"), - TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), + TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"),*/ }; static TestData InvalidBase64EncodingData = { - TestCase("YWJj,ZA=="), +/* TestCase("YWJj,ZA=="), TestCase("YWJj,A=="), TestCase(",,,,"), - TestCase("===="), + TestCase("===="),*/ }; static TestData IsBase64Data = { @@ -147,7 +147,7 @@ TEST(Base64Test, ExpectedSizeWstring) TEST(Base64Test, InvalidBase64Encoding) { for (const auto& item : InvalidBase64EncodingData) { - EXPECT_THROW(Base64::decode(PARAM(0)), std::runtime_error); + EXPECT_THROW(Base64::decode(PARAM(0)), std::invalid_argument); } } diff --git a/test/main.cc b/test/main.cc index d721c93..c0f3ee5 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,9 +1,9 @@ #include "test.h" -#include "base16-test.h" +//#include "base16-test.h" #include "base64-test.h" -#include "zlib-test.h" -#include "rsa-test.h" -#include "aes-test.h" +//#include "zlib-test.h" +//#include "rsa-test.h" +//#include "aes-test.h" INITIALIZE_EASYLOGGINGPP From 04875d5d31eb056b9bd92ac50dbe46265b12dd86 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 10:27:56 +1000 Subject: [PATCH 099/148] fix base64 decoding - issue is: 0 is valid embedded data (especially with raw string) --- src/base64.h | 15 +++++++-------- test/aes-test.h | 13 +++++++------ test/base64-test.h | 10 ++++++---- test/main.cc | 2 +- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/base64.h b/src/base64.h index 7d84c96..c452e60 100644 --- a/src/base64.h +++ b/src/base64.h @@ -204,23 +204,22 @@ class Base64 { ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 + // std::string s(ss.str()); - if (b1 != kPadding && b1 != '\0') { - if (b2 == kPadding || (b2 == '\0' && b3 == '\0')) { + if (b1 != kPadding/* && b1 != '\0'*/) { + if (b2 == kPadding/* || (b2 == '\0' && b3 == '\0')*/) { // second bitset is only 4 bits - // note: this line was causing issue when we had plain text length 16 - // b64 = uS2qrm5XdzsQZTcDrxJxbw== - // it was adding a nul term char - ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); + //ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); } else { ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 // first we clear the bits at pos 4 and 5 // then we concat with next bit b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 - if (b3 == kPadding || b3 == '\0') { + if (b3 == kPadding/* || b3 == '\0'*/) { // third bitset is only 4 bits - ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); + // std::string s(ss.str()); + //ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); // first we clear first 4 bits } else { ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6 | // 001001 ==> 000001 << 6 ==> 01000000 diff --git a/test/aes-test.h b/test/aes-test.h index d6cb6e9..18fcbcd 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -13,8 +13,8 @@ namespace mine { -// from http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf - +// many test data is from NIST special publication +// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf TEST(AESTest, KeyExpansion) { @@ -768,9 +768,11 @@ TEST(AESTest, CbcDecipher) } } -TEST(AESTest, RealDataIssues) +TEST(AESTest, RealDataIssuesTest) { - std::string expected = R"("{"_t":1503928197,"logger_id":"default","access_code":"default"})"; + // this is real data from residue logging server (development) + // + const std::string expected = R"({"_t":1503928197,"logger_id":"default","access_code":"default"})"; std::string output = AES::decipher("EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ==", "CBD437FA37772C66051A47D72367B38E", "a14c54563269e9e368f56b325f04ff00", @@ -780,9 +782,8 @@ TEST(AESTest, RealDataIssues) // // this worked => the only issue was padding std::string r = mine::AES::decipher(Ripe::stringToHex(Ripe::base64Decode(raw)), key, iv, mine::AES::ConvertMode::Base16, mine::AES::ConvertMode::Plain); - //ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); - expected = R"("{"_t":1503928197,"logger_id":"default","access_code":"default"})"; output = AES::decipher("12D62BE49168FFB92A616C68A0CBD4D8327EBA936150C0E28BD5FA2041D8C6F50D5D2546937E086A44F91FB19BCB32F9FC920C3004022E7514F36E11237CA681", "CBD437FA37772C66051A47D72367B38E", "a14c54563269e9e368f56b325f04ff00", diff --git a/test/base64-test.h b/test/base64-test.h index e3782a1..c2210cd 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -14,7 +14,7 @@ namespace mine { static TestData Base64TestData = { // examples from https://en.wikipedia.org/wiki/Base64#Output_padding -/* TestCase("YWJjZA==", "abcd"), + TestCase("YWJjZA==", "abcd"), TestCase("YW55IGNhcm5hbCBwbGVhc3VyZS4=", "any carnal pleasure."), TestCase("YW55IGNhcm5hbCBwbGVhc3VyZQ==", "any carnal pleasure"), TestCase("YW55IGNhcm5hbCBwbGVhc3Vy", "any carnal pleasur"), @@ -29,12 +29,12 @@ static TestData Base64TestData = { TestCase("4oKsNTA=", "€50"), TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", "this is rocket 🚀 and i love it"), TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"), - TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"),*/ + TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), }; static TestData Base64WStringTestData = { // examples from https://en.wikipedia.org/wiki/Base64#Output_padding -/* TestCase("YWJjZA==", L"abcd"), + TestCase("YWJjZA==", L"abcd"), TestCase("YW55IGNhcm5hbCBwbGVhc3VyZS4=", L"any carnal pleasure."), TestCase("YW55IGNhcm5hbCBwbGVhc3VyZQ==", L"any carnal pleasure"), TestCase("YW55IGNhcm5hbCBwbGVhc3Vy", L"any carnal pleasur"), @@ -50,7 +50,7 @@ static TestData Base64WStringTestData = { // Commenting and leaving it here on purpose, see note on decodeAsWString // TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", L"this is rocket 🚀 and i love it"), TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"), - TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"),*/ + TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), }; static TestData InvalidBase64EncodingData = { @@ -101,6 +101,7 @@ TEST(Base64Test, Decode) } static TestData Base64RawTestData = { + TestCase("dGhpcyBjb250YWlucyA9IHBhZGRpbmc=", 23, "7468697320636F6E7461696E73203D2070616464696E67"), // contains padding char TestCase("Z0BiQ8NcwknqzbGrWBjXqw==", 16, "67406243C35CC249EACDB1AB5818D7AB"), TestCase("EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ==", 64, "12D62BE49168FFB92A616C68A0CBD4D8327EBA936150C0E28BD5FA2041D8C6F50D5D2546937E086A44F91FB19BCB32F9FC920C3004022E7514F36E11237CA681") }; @@ -111,6 +112,7 @@ TEST(Base64Test, DecodeRawSize) std::string decoded = Base64::decode(PARAM(0)); ASSERT_EQ(PARAM(1), decoded.size()); std::string b16 = Base16::encode(decoded); + std::cout << decoded << std::endl; ASSERT_STRCASEEQ(PARAM(2).c_str(), b16.c_str()); } } diff --git a/test/main.cc b/test/main.cc index c0f3ee5..d1007d8 100644 --- a/test/main.cc +++ b/test/main.cc @@ -3,7 +3,7 @@ #include "base64-test.h" //#include "zlib-test.h" //#include "rsa-test.h" -//#include "aes-test.h" +#include "aes-test.h" INITIALIZE_EASYLOGGINGPP From 545e2aef8ea16cbd71f9852bf70dfa7f89f890a6 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 10:52:33 +1000 Subject: [PATCH 100/148] A lot of fixes --- .travis.yml | 2 +- CMakeLists.txt | 7 +++++ src/aes.cc | 53 +++++++++++++++++----------------- src/aes.h | 71 +++++++++++++++++++++++----------------------- src/base16.cc | 6 ++-- src/base64.h | 2 +- test/aes-test.h | 9 ++++++ test/base16-test.h | 18 +++++++++--- test/base64-test.h | 10 +++---- test/main.cc | 3 +- 10 files changed, 103 insertions(+), 78 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5662762..841bbc1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -53,7 +53,7 @@ before_install: - pwd - ls -l - ls -l .. - - cmake -Dtest_main_header=ON .. + - cmake -Dtest_main_header=ON -Dtest_wstring_conversions=OFF .. - make script: "./mine-unit-tests" diff --git a/CMakeLists.txt b/CMakeLists.txt index 280428d..b47ae1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 2.8.12) project(Mine) option (test_main_header "Test main header (mine.h)" OFF) +option (test_wstring_conversions "Test std::wstring (wchar_t*) conversions for encodings" ON) set (MINE_VERSION "4.0.0") set (MINE_SOVERSION "4.0.0") @@ -117,6 +118,12 @@ else() ) endif() +if (test_wstring_conversions) + target_compile_definitions (mine-unit-tests PUBLIC + MINE_BASE64_WSTRING_CONVERSION + ) +endif() + target_compile_definitions (mine-unit-tests PUBLIC ELPP_STL_LOGGING ) diff --git a/src/aes.cc b/src/aes.cc index 0049e8a..86fb9d7 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -511,6 +511,30 @@ ByteArray AES::rawDecipher(const ByteArray& input, const Key* key) return stateToByteArray(&state); } +ByteArray AES::resolveInputMode(const std::string& input, ConvertMode inputMode) +{ + if (inputMode == ConvertMode::Plain) { + return Base16::fromString(Base16::encode(input)); + } else if (inputMode == ConvertMode::Base16) { + return Base16::fromString(input); + } + // base64 + return Base16::fromString(Base16::encode(Base64::decode(input))); +} + +std::string AES::resolveOutputMode(const ByteArray& input, ConvertMode outputMode) +{ + if (outputMode == ConvertMode::Plain) { + return Base16::toRawString(input); + } else if (outputMode == ConvertMode::Base16) { + return Base16::encode(input.begin(), input.end()); + } + // base64 + return Base64::encode(input.begin(), input.end()); +} + +// public + ByteArray AES::cipher(const ByteArray& input, const Key* key) { @@ -625,8 +649,9 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); + std::size_t j = 0; // don't use copy_n as we are setting the values - for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { + for (; j < kBlockSize && inputSize > j + i; ++j) { inputBlock.at(j) = input.at(j + i); } @@ -635,35 +660,11 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) xorWith(&outputBlock, &nextXorWith); nextXorWith = inputBlock; - std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); + std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } return result; } -ByteArray AES::resolveInputMode(const std::string& input, ConvertMode inputMode) -{ - if (inputMode == ConvertMode::Plain) { - return Base16::fromString(Base16::encode(input)); - } else if (inputMode == ConvertMode::Base16) { - return Base16::fromString(input); - } - // base64 - return Base16::fromString(Base16::encode(Base64::decode(input))); -} - -std::string AES::resolveOutputMode(const ByteArray& input, ConvertMode outputMode) -{ - if (outputMode == ConvertMode::Plain) { - return Base16::toRawString(input); - } else if (outputMode == ConvertMode::Base16) { - return Base16::encode(input.begin(), input.end()); - } - // base64 - return Base64::encode(input.begin(), input.end()); -} - -// public - std::string AES::cipher(const std::string& input, const std::string& key, ConvertMode inputMode, ConvertMode outputEncoding) { Key keyArr = Base16::fromString(key); diff --git a/src/aes.h b/src/aes.h index 7e3e02f..af1d382 100644 --- a/src/aes.h +++ b/src/aes.h @@ -104,6 +104,41 @@ class AES { /// static std::string decipher(const std::string& input, const std::string& key, const std::string& iv, ConvertMode inputMode = ConvertMode::Base16, ConvertMode outputEncoding = ConvertMode::Plain); + /// + /// \brief Ciphers with ECB-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray cipher(const ByteArray& input, const Key* key); + + /// + /// \brief Deciphers with ECB-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray decipher(const ByteArray& input, const Key* key); + + /// + /// \brief Ciphers with CBC-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray cipher(const ByteArray& input, const Key* key, ByteArray& iv); + + /// + /// \brief Deciphers with CBC-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray decipher(const ByteArray& input, const Key* key, ByteArray& iv); private: /// @@ -276,42 +311,6 @@ class AES { /// static ByteArray rawDecipher(const ByteArray& input, const Key* key); - /// - /// \brief Ciphers with ECB-Mode, the input can be as long as user wants - /// \param input Plain input of any length - /// \param key Pointer to a valid AES key - /// \param iv Initialization vector - /// \return Cipher text byte array - /// - static ByteArray cipher(const ByteArray& input, const Key* key); - - /// - /// \brief Deciphers with ECB-Mode, the input can be as long as user wants - /// \param input Plain input of any length - /// \param key Pointer to a valid AES key - /// \param iv Initialization vector - /// \return Cipher text byte array - /// - static ByteArray decipher(const ByteArray& input, const Key* key); - - /// - /// \brief Ciphers with CBC-Mode, the input can be as long as user wants - /// \param input Plain input of any length - /// \param key Pointer to a valid AES key - /// \param iv Initialization vector - /// \return Cipher text byte array - /// - static ByteArray cipher(const ByteArray& input, const Key* key, ByteArray& iv); - - /// - /// \brief Deciphers with CBC-Mode, the input can be as long as user wants - /// \param input Plain input of any length - /// \param key Pointer to a valid AES key - /// \param iv Initialization vector - /// \return Cipher text byte array - /// - static ByteArray decipher(const ByteArray& input, const Key* key, ByteArray& iv); - /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array /// diff --git a/src/base16.cc b/src/base16.cc index 5b42c25..25560a5 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -45,9 +45,7 @@ ByteArray Base16::fromString(const std::string& hex) std::string Base16::toRawString(const ByteArray& input) { std::ostringstream ss; - for (auto iter = input.begin(); iter < input.end(); ++iter) { - ss << static_cast(*iter); - } + std::copy(input.begin(), input.end(), std::ostream_iterator(ss)); return ss.str(); } @@ -58,6 +56,6 @@ void Base16::decode(char a, char b, std::ostringstream& ss) try { ss << static_cast((b0 << 4) | kDecodeMap.at(b1)); } catch (const std::exception&) { - throw std::runtime_error("Invalid base-16 encoding"); + throw std::invalid_argument("Invalid base-16 encoding"); } } diff --git a/src/base64.h b/src/base64.h index c452e60..3e59a51 100644 --- a/src/base64.h +++ b/src/base64.h @@ -42,7 +42,7 @@ using byte = unsigned char; /// This class also provides public interface to encode /// the iterators for other containers like vector etc. /// -/// This also handles unicode characters +/// This also handles 16-bit, 24-bit and 32-bit characters /// class Base64 { public: diff --git a/test/aes-test.h b/test/aes-test.h index 18fcbcd..e94cf31 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -686,6 +686,15 @@ TEST(AESTest, Base64StringInputDecipher) ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } +TEST(AESTest, EcbDecipher) +{ + const std::string key = "F1EF6477CC39E65DE106C33BB0EC651386CD0932A9DE491CF960BC3EB79EBE78"; + const std::string cipherHex = "b939427f4231593f5cbf73449439a847726b1898b03db028a6f0824108678f78"; + const std::string expected = "this is slightly longer"; + std::string output = AES::decipher(cipherHex, key); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); +} + TEST(AESTest, CbcDecipher) { diff --git a/test/base16-test.h b/test/base16-test.h index 1ea277d..e1a8dc6 100644 --- a/test/base16-test.h +++ b/test/base16-test.h @@ -32,10 +32,6 @@ static TestData InvalidBase16EncodingData = { TestCase("48656C6C6F20576F726C64F"), }; -static TestData Base16FromStringData = { - TestCase("48656C6C6F", ByteArray { 0x48, 0x65, 0x6C, 0x6C, 0x6F }), -}; - TEST(Base16Test, Encode) { for (const auto& item : Base16TestData) { @@ -52,6 +48,12 @@ TEST(Base16Test, EncodeByteArray) } } + +// hex str hex arr raw str +static TestData Base16FromStringData = { + TestCase("48656C6C6F", ByteArray { 0x48, 0x65, 0x6C, 0x6C, 0x6F }, "Hello"), +}; + TEST(Base16Test, ConvertToByteArray) { for (const auto& item : Base16FromStringData) { @@ -60,6 +62,14 @@ TEST(Base16Test, ConvertToByteArray) } } +TEST(Base16Test, ConvertToRaw) +{ + for (const auto& item : Base16FromStringData) { + std::string result = Base16::toRawString(PARAM(1)); + ASSERT_EQ(PARAM(2), result); + } +} + TEST(Base16Test, EncodeInt) { for (const auto& item : Base16IntTestData) { diff --git a/test/base64-test.h b/test/base64-test.h index c2210cd..6bdb1d6 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -95,7 +95,7 @@ TEST(Base64Test, Decode) { for (const auto& item : Base64TestData) { std::string decoded = Base64::decode(PARAM(0)); - std::cout << decoded << std::endl; + //std::cout << decoded << std::endl; ASSERT_STREQ(PARAM(1).c_str(), decoded.c_str()); } } @@ -112,7 +112,7 @@ TEST(Base64Test, DecodeRawSize) std::string decoded = Base64::decode(PARAM(0)); ASSERT_EQ(PARAM(1), decoded.size()); std::string b16 = Base16::encode(decoded); - std::cout << decoded << std::endl; + //std::cout << decoded << std::endl; ASSERT_STRCASEEQ(PARAM(2).c_str(), b16.c_str()); } } @@ -130,9 +130,9 @@ TEST(Base64Test, DecodeWString) { for (const auto& item : Base64WStringTestData) { std::wstring decoded = Base64::decodeAsWString(PARAM(0)); - std::wcout << std::wstring(decoded.begin(), decoded.end()); - std::wcout.clear(); // clear the stream in case of failbit or badbit - std::cout << std::endl; + //std::wcout << std::wstring(decoded.begin(), decoded.end()); + //std::wcout.clear(); // clear the stream in case of failbit or badbit + //std::cout << std::endl; ASSERT_STREQ(PARAM(1).c_str(), decoded.c_str()); } } diff --git a/test/main.cc b/test/main.cc index d1007d8..f7aaa9d 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,5 +1,5 @@ #include "test.h" -//#include "base16-test.h" +#include "base16-test.h" #include "base64-test.h" //#include "zlib-test.h" //#include "rsa-test.h" @@ -16,6 +16,7 @@ int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); el::Loggers::addFlag(el::LoggingFlag::ColoredTerminalOutput); el::Loggers::addFlag(el::LoggingFlag::ImmediateFlush); + disableLogs(); return ::testing::UnitTest::GetInstance()->Run(); } From fe1068aa53877ec929ee06867a077130e187da31 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 11:17:49 +1000 Subject: [PATCH 101/148] cli tool based on ripe --- CMakeLists.txt | 9 ++ cli/mine.cc | 226 ++++++++++++++++++++++++++++++++++++++++++++++++ package/mine.cc | 84 ++++++++++-------- package/mine.h | 129 +++++++++++++-------------- src/aes.cc | 37 +++++--- src/aes.h | 29 ++++--- test/aes-test.h | 32 +++---- 7 files changed, 403 insertions(+), 143 deletions(-) create mode 100644 cli/mine.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index b47ae1e..1819dc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,6 +83,15 @@ set (CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE}) file (GLOB_RECURSE all_headers ${CMAKE_SOURCE_DIR}/*.h) add_custom_target (all_placeholder SOURCES ${all_headers}) +########################################## CLI Tool ################################### + +add_executable (mine-bin cli/mine.cc package/mine.cc) + +set_target_properties (mine-bin PROPERTIES + OUTPUT_NAME "mine" + VERSION ${MINE_VERSION} +) +install (TARGETS mine-bin DESTINATION bin) ########################################## Unit Testing ################################### diff --git a/cli/mine.cc b/cli/mine.cc new file mode 100644 index 0000000..bbe8b7d --- /dev/null +++ b/cli/mine.cc @@ -0,0 +1,226 @@ +// +// Bismillah ar-Rahmaan ar-Raheem +// +// CLI Tool for Mine +// +// Mine is single header minimal cryptography library +// +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE +// +// https://github.com/muflihun/mine +// https://muflihun.github.io/mine +// https://muflihun.com +// + +#include +#include +#include +#include +#include +#include +#include +#include +#include "package/mine.h" + +using namespace mine; + +void displayUsage() +{ + // we want to keep the order so can't use std::map or std::unordered_map + std::vector> options = { + {"--version", "Display version information"}, + {"-e", "Encrypt / encode / inflate the data"}, + {"-d", "Decrypt / decrypt / deflate the data"}, + {"-g", "Generate a random key"}, + {"--aes", "AES operations"}, + {"--key", "Symmetric key for encryption / decryption"}, + {"--iv", "Initializaion vector for decription"}, + {"--base64", "Base64 operations"}, + {"--hex", "Base16 operations"}, + {"--length", "Specify key length"}, + }; + + std::cout << "mine [-d | -e | -g | -s | -v] [--in ] [--key ] [--in-key ] [--out-public ] [--out-private ] [--iv ] [--base64] [--length ] [--aes []] [--hex]" << std::endl; + std::cout << std::endl; + const std::size_t LONGEST = 20; + for (auto& option : options) { + std::cout << " " << option.first; + for (std::size_t i = 0; i < LONGEST - option.first.length(); ++i) { + std::cout << " "; + } + std::cout << option.second << std::endl; + } + std::cout << std::endl; +} + +void displayVersion() +{ + std::cout << "Mine - Minimal cryptography library" << std::endl << "Version: " << MINE_VERSION << std::endl << "https://muflihun.com" << std::endl; +} + +#define TRY try { +#define CATCH } catch (const std::exception& e) { std::cout << "ERROR: " << e.what() << std::endl; } + +void encryptAES(std::string& data, const std::string& key, std::string& iv) +{ + TRY + bool newIv = iv.empty(); + std::cout << AES::cipher(data, key, iv); + + if (newIv) { + std::cout << std::endl << "IV: " << iv << std::endl; + } + CATCH +} + +void decryptAES(std::string& data, const std::string& key, std::string& iv) +{ + TRY + std::cout << AES::decipher(data, key, iv); + CATCH +} + +void generateAESKey(int length) +{ + TRY + std::cout << AES::generateRandomKey(length); + CATCH +} + +void encodeBase64(std::string& data) +{ + TRY + std::cout << Base64::encode(data); + CATCH +} + +void decodeBase64(std::string& data) +{ + TRY + std::cout << Base64::decode(data); + CATCH +} + +void encodeHex(std::string& data) +{ + TRY + std::cout << Base16::encode(data); + CATCH +} + +void decodeHex(std::string& data) +{ + TRY + std::cout << Base16::decode(data); + CATCH +} + +int main(int argc, char* argv[]) +{ + if (argc < 2) { + displayUsage(); + return 1; + } + + if (strcmp(argv[1], "--version") == 0) { + displayVersion(); + return 0; + } + + // This is quick check for args, use getopt in future + int type = -1; // Decryption encryption, generate, verify or sign + + std::string key; + std::string iv; + int keyLength = 256; + std::string data; + bool isAES = false; + bool isBase64 = false; + bool isHex = false; + bool fileArgSpecified = false; + + for (int i = 0; i < argc; i++) { + std::string arg(argv[i]); + bool hasNext = i + 1 < argc; + if (arg == "-d" && type == -1) { + type = 1; + } else if (arg == "-e" && type == -1) { + type = 2; + } else if (arg == "-g" && type == -1) { + type = 3; + } else if (arg == "--base64") { + isBase64 = true; + } else if (arg == "--hex") { + isHex = true; + } else if (arg == "--key" && hasNext) { + key = argv[++i]; + } else if (arg == "--aes") { + isAES = true; + if (i + 1 < argc) { + int k = atoi(argv[++i]); + if (k > 0) { + keyLength = k; + } else { + --i; + } + } + } else if (arg == "--length" && hasNext) { + keyLength = atoi(argv[++i]); + } else if (arg == "--iv" && hasNext) { + iv = argv[++i]; + } else if (arg == "--in" && hasNext) { + fileArgSpecified = true; + std::fstream fs; + // Do not increment i here as we are only changing 'data' + fs.open (argv[i + 1], std::fstream::binary | std::fstream::in); + data = std::string((std::istreambuf_iterator(fs) ), + (std::istreambuf_iterator())); + fs.close(); + } + } + + if ((type == 1 || type == 2 || type == 3) && !fileArgSpecified) { + std::stringstream ss; + for (std::string line; std::getline(std::cin, line);) { + ss << line << std::endl; + } + data = ss.str(); + // Remove last 'new line' + data.erase(data.size() - 1); + } + + if (type == 1) { // Decrypt / Decode + if (isBase64 && key.empty() && iv.empty()) { + // base64 decode + decodeBase64(data); + } else if (isHex && key.empty() && iv.empty()) { + // hex to ascii + decodeHex(data); + } else { + // AES decrypt (base64-flexible) + decryptAES(data, key, iv); + } + } else if (type == 2) { // Encrypt / Encode + if (isBase64 && key.empty() && iv.empty()) { + encodeBase64(data); + } else if (isHex && key.empty() && iv.empty()) { + encodeHex(data); + } else { + encryptAES(data, key, iv); + } + } else if (type == 3) { // Generate + if (isAES) { + generateAESKey(keyLength); + } else { + std::cout << "ERROR: Please provide method (you probably forgot '--rsa' or '--aes')" << std::endl; + } + } else { + displayUsage(); + return 1; + } + + return 0; +} diff --git a/package/mine.cc b/package/mine.cc index e74146e..cbe0545 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -51,9 +51,7 @@ ByteArray Base16::fromString(const std::string& hex) std::string Base16::toRawString(const ByteArray& input) { std::ostringstream ss; - for (auto iter = input.begin(); iter < input.end(); ++iter) { - ss << static_cast(*iter); - } + std::copy(input.begin(), input.end(), std::ostream_iterator(ss)); return ss.str(); } @@ -64,7 +62,7 @@ void Base16::decode(char a, char b, std::ostringstream& ss) try { ss << static_cast((b0 << 4) | kDecodeMap.at(b1)); } catch (const std::exception&) { - throw std::runtime_error("Invalid base-16 encoding"); + throw std::invalid_argument("Invalid base-16 encoding"); } } @@ -597,6 +595,30 @@ ByteArray AES::rawDecipher(const ByteArray& input, const Key* key) return stateToByteArray(&state); } +ByteArray AES::resolveInputMode(const std::string& input, Encoding inputMode) +{ + if (inputMode == Encoding::Raw) { + return Base16::fromString(Base16::encode(input)); + } else if (inputMode == Encoding::Base16) { + return Base16::fromString(input); + } + // base64 + return Base16::fromString(Base16::encode(Base64::decode(input))); +} + +std::string AES::resolveOutputMode(const ByteArray& input, Encoding outputMode) +{ + if (outputMode == Encoding::Raw) { + return Base16::toRawString(input); + } else if (outputMode == Encoding::Base16) { + return Base16::encode(input.begin(), input.end()); + } + // base64 + return Base64::encode(input.begin(), input.end()); +} + +// public + ByteArray AES::cipher(const ByteArray& input, const Key* key) { @@ -711,8 +733,9 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); + std::size_t j = 0; // don't use copy_n as we are setting the values - for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { + for (; j < kBlockSize && inputSize > j + i; ++j) { inputBlock.at(j) = input.at(j + i); } @@ -721,47 +744,23 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) xorWith(&outputBlock, &nextXorWith); nextXorWith = inputBlock; - std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); + std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } return result; } -ByteArray AES::resolveInputMode(const std::string& input, ConvertMode inputMode) -{ - if (inputMode == ConvertMode::Plain) { - return Base16::fromString(Base16::encode(input)); - } else if (inputMode == ConvertMode::Base16) { - return Base16::fromString(input); - } - // base64 - return Base16::fromString(Base16::encode(Base64::decode(input))); -} - -std::string AES::resolveOutputMode(const ByteArray& input, ConvertMode outputMode) -{ - if (outputMode == ConvertMode::Plain) { - return Base16::toRawString(input); - } else if (outputMode == ConvertMode::Base16) { - return Base16::encode(input.begin(), input.end()); - } - // base64 - return Base64::encode(input.begin(), input.end()); -} - -// public - -std::string AES::cipher(const std::string& input, const std::string& key, ConvertMode inputMode, ConvertMode outputEncoding) +std::string AES::cipher(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(key); - ByteArray inp = resolveInputMode(input, inputMode); + ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray result = cipher(inp, &keyArr); return resolveOutputMode(result, outputEncoding); } -std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, ConvertMode inputMode, ConvertMode outputEncoding) +std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(key); - ByteArray inp = resolveInputMode(input, inputMode); + ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray ivec = Base16::fromString(iv); bool ivecGenerated = iv.empty(); ByteArray result = cipher(inp, &keyArr, ivec); @@ -771,21 +770,30 @@ std::string AES::cipher(const std::string& input, const std::string& key, std::s return resolveOutputMode(result, outputEncoding); } -std::string AES::decipher(const std::string& input, const std::string& key, ConvertMode inputMode, ConvertMode outputEncoding) +std::string AES::decipher(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(key); - ByteArray inp = resolveInputMode(input, inputMode); + ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray result = decipher(inp, &keyArr); return resolveOutputMode(result, outputEncoding); } -std::string AES::decipher(const std::string& input, const std::string& key, const std::string& iv, ConvertMode inputMode, ConvertMode outputEncoding) +std::string AES::decipher(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(key); - ByteArray inp = resolveInputMode(input, inputMode); + ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray ivec = Base16::fromString(iv); ByteArray result = decipher(inp, &keyArr, ivec); return resolveOutputMode(result, outputEncoding); } +std::string AES::generateRandomKey(const std::size_t len) +{ + if (len != 128 && len != 192 && len != 256) { + throw std::invalid_argument("Please choose valid key length of 128, 192 or 256 bits"); + } + ByteArray bytes = generateRandomBytes(len); + return Base16::encode(bytes.begin(), bytes.end()); +} + diff --git a/package/mine.h b/package/mine.h index b63a8d5..13aca64 100644 --- a/package/mine.h +++ b/package/mine.h @@ -188,7 +188,7 @@ using byte = unsigned char; /// This class also provides public interface to encode /// the iterators for other containers like vector etc. /// -/// This also handles unicode characters +/// This also handles 16-bit, 24-bit and 32-bit characters /// class Base64 { public: @@ -315,16 +315,16 @@ class Base64 { static std::string decode(const std::string& e) { if (e.size() % 4 != 0) { - throw std::runtime_error("Invalid base64 encoding. Padding is required"); + throw std::invalid_argument("Invalid base64 encoding. Padding is required"); } return decode(e.begin(), e.end()); } /// /// \brief Decodes base64 iterator from begin to end - /// \throws std::runtime if invalid encoding. Another time it is thrown + /// \throws std::invalid_argument if invalid encoding. Another time it is thrown /// is if no padding is found - /// std::runtime::what() is set according to the error + /// std::invalid_argument::what() is set according to the error /// template static std::string decode(const Iter& begin, const Iter& end) @@ -342,7 +342,7 @@ class Base64 { try { int b0 = kDecodeMap.at(static_cast(*it & 0xff)); if (b0 == kPadding || b0 == '\0') { - throw std::runtime_error("Invalid base64 encoding. No data available"); + throw std::invalid_argument("Invalid base64 encoding. No data available"); } int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); @@ -350,23 +350,22 @@ class Base64 { ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 + // std::string s(ss.str()); - if (b1 != kPadding && b1 != '\0') { - if (b2 == kPadding || (b2 == '\0' && b3 == '\0')) { - // second bitset is 'partial byte' + if (b1 != kPadding/* && b1 != '\0'*/) { + if (b2 == kPadding/* || (b2 == '\0' && b3 == '\0')*/) { + // second bitset is only 4 bits - // note: this line was causing issue when we had plain text length 16 - // b64 = uS2qrm5XdzsQZTcDrxJxbw== - // it was adding a nul term char //ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); } else { ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 // first we clear the bits at pos 4 and 5 // then we concat with next bit b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 - if (b3 == kPadding || b3 == '\0') { - // third bitset is 'partial byte' - ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); + if (b3 == kPadding/* || b3 == '\0'*/) { + // third bitset is only 4 bits + // std::string s(ss.str()); + //ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); // first we clear first 4 bits } else { ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6 | // 001001 ==> 000001 << 6 ==> 01000000 @@ -377,7 +376,7 @@ class Base64 { } } } catch (const std::exception&) { - throw std::runtime_error("Invalid base64 character"); + throw std::invalid_argument("Invalid base64 character"); } } return ss.str(); @@ -464,8 +463,8 @@ class AES { /// /// \brief Convert mode for various functions /// - enum class ConvertMode { - Plain, + enum class Encoding { + Raw, Base16, Base64 }; @@ -478,41 +477,81 @@ class AES { /// /// \brief Ciphers the input with specified hex key /// \param key Hex key - /// \param inputMode the type of input. Defaults to Plain + /// \param inputEncoding the type of input. Defaults to Plain /// \param outputEncoding Type of encoding for cipher /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key, ConvertMode inputMode = ConvertMode::Plain, ConvertMode outputEncoding = ConvertMode::Base16); + static std::string cipher(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); /// /// \brief Ciphers the input with specified hex key using CBC mode /// \param key Hex key /// \param iv Initialization vector, passed by reference. If empty a random is generated and passed in - /// \param inputMode the type of input. Defaults to Plain + /// \param inputEncoding the type of input. Defaults to Plain /// \param outputEncoding Type of encoding for cipher /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key, std::string& iv, ConvertMode inputMode = ConvertMode::Plain, ConvertMode outputEncoding = ConvertMode::Base16); + static std::string cipher(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); /// /// \brief Deciphers the input with specified hex key /// \param key Hex key - /// \param inputMode the type of input. Defaults to base16 + /// \param inputEncoding the type of input. Defaults to base16 /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - static std::string decipher(const std::string& input, const std::string& key, ConvertMode inputMode = ConvertMode::Base16, ConvertMode outputEncoding = ConvertMode::Plain); + static std::string decipher(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); /// /// \brief Deciphers the input with specified hex key using CBC mode /// \param key Hex key /// \param iv Initialization vector - /// \param inputMode the type of input. Defaults to base16 + /// \param inputEncoding the type of input. Defaults to base16 /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - static std::string decipher(const std::string& input, const std::string& key, const std::string& iv, ConvertMode inputMode = ConvertMode::Base16, ConvertMode outputEncoding = ConvertMode::Plain); + static std::string decipher(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + /// + /// \brief Ciphers with ECB-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray cipher(const ByteArray& input, const Key* key); + + /// + /// \brief Deciphers with ECB-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray decipher(const ByteArray& input, const Key* key); + + /// + /// \brief Ciphers with CBC-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray cipher(const ByteArray& input, const Key* key, ByteArray& iv); + + /// + /// \brief Deciphers with CBC-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + static ByteArray decipher(const ByteArray& input, const Key* key, ByteArray& iv); + + /// + /// \brief Generates random key of valid length + /// + static std::string generateRandomKey(const std::size_t len); private: /// @@ -650,12 +689,12 @@ class AES { /// /// \brief Creates byte array from input based on input mode /// - static ByteArray resolveInputMode(const std::string& input, ConvertMode inputMode); + static ByteArray resolveInputMode(const std::string& input, Encoding inputMode); /// /// \brief Creates string from byte array based on convert mode /// - static std::string resolveOutputMode(const ByteArray& input, ConvertMode outputMode); + static std::string resolveOutputMode(const ByteArray& input, Encoding outputMode); /// /// \brief Exclusive XOR with arr @@ -685,42 +724,6 @@ class AES { /// static ByteArray rawDecipher(const ByteArray& input, const Key* key); - /// - /// \brief Ciphers with ECB-Mode, the input can be as long as user wants - /// \param input Plain input of any length - /// \param key Pointer to a valid AES key - /// \param iv Initialization vector - /// \return Cipher text byte array - /// - static ByteArray cipher(const ByteArray& input, const Key* key); - - /// - /// \brief Deciphers with ECB-Mode, the input can be as long as user wants - /// \param input Plain input of any length - /// \param key Pointer to a valid AES key - /// \param iv Initialization vector - /// \return Cipher text byte array - /// - static ByteArray decipher(const ByteArray& input, const Key* key); - - /// - /// \brief Ciphers with CBC-Mode, the input can be as long as user wants - /// \param input Plain input of any length - /// \param key Pointer to a valid AES key - /// \param iv Initialization vector - /// \return Cipher text byte array - /// - static ByteArray cipher(const ByteArray& input, const Key* key, ByteArray& iv); - - /// - /// \brief Deciphers with CBC-Mode, the input can be as long as user wants - /// \param input Plain input of any length - /// \param key Pointer to a valid AES key - /// \param iv Initialization vector - /// \return Cipher text byte array - /// - static ByteArray decipher(const ByteArray& input, const Key* key, ByteArray& iv); - /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array /// diff --git a/src/aes.cc b/src/aes.cc index 86fb9d7..2a874d3 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -511,22 +511,22 @@ ByteArray AES::rawDecipher(const ByteArray& input, const Key* key) return stateToByteArray(&state); } -ByteArray AES::resolveInputMode(const std::string& input, ConvertMode inputMode) +ByteArray AES::resolveInputMode(const std::string& input, Encoding inputMode) { - if (inputMode == ConvertMode::Plain) { + if (inputMode == Encoding::Raw) { return Base16::fromString(Base16::encode(input)); - } else if (inputMode == ConvertMode::Base16) { + } else if (inputMode == Encoding::Base16) { return Base16::fromString(input); } // base64 return Base16::fromString(Base16::encode(Base64::decode(input))); } -std::string AES::resolveOutputMode(const ByteArray& input, ConvertMode outputMode) +std::string AES::resolveOutputMode(const ByteArray& input, Encoding outputMode) { - if (outputMode == ConvertMode::Plain) { + if (outputMode == Encoding::Raw) { return Base16::toRawString(input); - } else if (outputMode == ConvertMode::Base16) { + } else if (outputMode == Encoding::Base16) { return Base16::encode(input.begin(), input.end()); } // base64 @@ -665,18 +665,18 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) return result; } -std::string AES::cipher(const std::string& input, const std::string& key, ConvertMode inputMode, ConvertMode outputEncoding) +std::string AES::cipher(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(key); - ByteArray inp = resolveInputMode(input, inputMode); + ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray result = cipher(inp, &keyArr); return resolveOutputMode(result, outputEncoding); } -std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, ConvertMode inputMode, ConvertMode outputEncoding) +std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(key); - ByteArray inp = resolveInputMode(input, inputMode); + ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray ivec = Base16::fromString(iv); bool ivecGenerated = iv.empty(); ByteArray result = cipher(inp, &keyArr, ivec); @@ -686,19 +686,28 @@ std::string AES::cipher(const std::string& input, const std::string& key, std::s return resolveOutputMode(result, outputEncoding); } -std::string AES::decipher(const std::string& input, const std::string& key, ConvertMode inputMode, ConvertMode outputEncoding) +std::string AES::decipher(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(key); - ByteArray inp = resolveInputMode(input, inputMode); + ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray result = decipher(inp, &keyArr); return resolveOutputMode(result, outputEncoding); } -std::string AES::decipher(const std::string& input, const std::string& key, const std::string& iv, ConvertMode inputMode, ConvertMode outputEncoding) +std::string AES::decipher(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(key); - ByteArray inp = resolveInputMode(input, inputMode); + ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray ivec = Base16::fromString(iv); ByteArray result = decipher(inp, &keyArr, ivec); return resolveOutputMode(result, outputEncoding); } + +std::string AES::generateRandomKey(const std::size_t len) +{ + if (len != 128 && len != 192 && len != 256) { + throw std::invalid_argument("Please choose valid key length of 128, 192 or 256 bits"); + } + ByteArray bytes = generateRandomBytes(len); + return Base16::encode(bytes.begin(), bytes.end()); +} diff --git a/src/aes.h b/src/aes.h index af1d382..3313b2f 100644 --- a/src/aes.h +++ b/src/aes.h @@ -55,8 +55,8 @@ class AES { /// /// \brief Convert mode for various functions /// - enum class ConvertMode { - Plain, + enum class Encoding { + Raw, Base16, Base64 }; @@ -69,40 +69,40 @@ class AES { /// /// \brief Ciphers the input with specified hex key /// \param key Hex key - /// \param inputMode the type of input. Defaults to Plain + /// \param inputEncoding the type of input. Defaults to Plain /// \param outputEncoding Type of encoding for cipher /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key, ConvertMode inputMode = ConvertMode::Plain, ConvertMode outputEncoding = ConvertMode::Base16); + static std::string cipher(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); /// /// \brief Ciphers the input with specified hex key using CBC mode /// \param key Hex key /// \param iv Initialization vector, passed by reference. If empty a random is generated and passed in - /// \param inputMode the type of input. Defaults to Plain + /// \param inputEncoding the type of input. Defaults to Plain /// \param outputEncoding Type of encoding for cipher /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key, std::string& iv, ConvertMode inputMode = ConvertMode::Plain, ConvertMode outputEncoding = ConvertMode::Base16); + static std::string cipher(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); /// /// \brief Deciphers the input with specified hex key /// \param key Hex key - /// \param inputMode the type of input. Defaults to base16 + /// \param inputEncoding the type of input. Defaults to base16 /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - static std::string decipher(const std::string& input, const std::string& key, ConvertMode inputMode = ConvertMode::Base16, ConvertMode outputEncoding = ConvertMode::Plain); + static std::string decipher(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); /// /// \brief Deciphers the input with specified hex key using CBC mode /// \param key Hex key /// \param iv Initialization vector - /// \param inputMode the type of input. Defaults to base16 + /// \param inputEncoding the type of input. Defaults to base16 /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - static std::string decipher(const std::string& input, const std::string& key, const std::string& iv, ConvertMode inputMode = ConvertMode::Base16, ConvertMode outputEncoding = ConvertMode::Plain); + static std::string decipher(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); /// /// \brief Ciphers with ECB-Mode, the input can be as long as user wants @@ -139,6 +139,11 @@ class AES { /// \return Cipher text byte array /// static ByteArray decipher(const ByteArray& input, const Key* key, ByteArray& iv); + + /// + /// \brief Generates random key of valid length + /// + static std::string generateRandomKey(const std::size_t len); private: /// @@ -276,12 +281,12 @@ class AES { /// /// \brief Creates byte array from input based on input mode /// - static ByteArray resolveInputMode(const std::string& input, ConvertMode inputMode); + static ByteArray resolveInputMode(const std::string& input, Encoding inputMode); /// /// \brief Creates string from byte array based on convert mode /// - static std::string resolveOutputMode(const ByteArray& input, ConvertMode outputMode); + static std::string resolveOutputMode(const ByteArray& input, Encoding outputMode); /// /// \brief Exclusive XOR with arr diff --git a/test/aes-test.h b/test/aes-test.h index e94cf31..5adfb64 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -448,7 +448,7 @@ TEST(AESTest, RawCipherDirect) for (auto& item : RawCipherData) { std::string expected = PARAM(2); - std::string output = AES::cipher(PARAM(0), PARAM(1), AES::ConvertMode::Base16); + std::string output = AES::cipher(PARAM(0), PARAM(1), AES::Encoding::Base16); // case insensitive comparison because hex can be upper or lower case ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } @@ -463,7 +463,7 @@ TEST(AESTest, RawCipherPlain) { for (auto& item : RawCipherPlainInputData) { std::string expected = PARAM(2); - std::string output = AES::cipher(PARAM(0), PARAM(1), AES::ConvertMode::Plain); + std::string output = AES::cipher(PARAM(0), PARAM(1), AES::Encoding::Raw); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -477,7 +477,7 @@ TEST(AESTest, RawCipherBase64) { for (auto& item : RawCipherBase64InputData) { std::string expected = PARAM(2); - std::string output = AES::cipher(PARAM(0), PARAM(1), AES::ConvertMode::Base64); + std::string output = AES::cipher(PARAM(0), PARAM(1), AES::Encoding::Base64); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -629,8 +629,8 @@ TEST(AESTest, CbcCipher) std::string k = Base16::encode(Base16::toRawString(key)); std::string initVec = Base16::encode(Base16::toRawString(iv)); std::string output = AES::cipher(input, k, initVec, - AES::ConvertMode::Plain, - AES::ConvertMode::Base16); + AES::Encoding::Raw, + AES::Encoding::Base16); ASSERT_STREQ(expected.c_str(), output.c_str()); } @@ -659,7 +659,7 @@ TEST(AESTest, HexStringDecipher) { for (auto& item : RawDecipherData) { std::string expected = PARAM(0); - std::string output = AES::decipher(PARAM(2), PARAM(1), AES::ConvertMode::Base16, AES::ConvertMode::Base16); + std::string output = AES::decipher(PARAM(2), PARAM(1), AES::Encoding::Base16, AES::Encoding::Base16); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -671,8 +671,8 @@ TEST(AESTest, Base64StringDecipher) std::string expected = "dGhpcyBpcyB0ZXN0Li4uLg=="; // base64("this is test....") std::string output = AES::decipher("b92daaae6e57773b10653703af12716f", "000102030405060708090a0b0c0d0e0f", - AES::ConvertMode::Base16, - AES::ConvertMode::Base64); + AES::Encoding::Base16, + AES::Encoding::Base64); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } @@ -681,8 +681,8 @@ TEST(AESTest, Base64StringInputDecipher) std::string expected = "this is test.."; std::string output = AES::decipher("Z0BiQ8NcwknqzbGrWBjXqw==", "000102030405060708090a0b0c0d0e0f", - AES::ConvertMode::Base64, - AES::ConvertMode::Plain); + AES::Encoding::Base64, + AES::Encoding::Raw); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } @@ -770,8 +770,8 @@ TEST(AESTest, CbcDecipher) std::string k = Base16::encode(Base16::toRawString(key)); std::string initVec = Base16::encode(Base16::toRawString(iv)); std::string output = AES::decipher(input, k, initVec, - AES::ConvertMode::Plain, - AES::ConvertMode::Plain); + AES::Encoding::Raw, + AES::Encoding::Raw); ASSERT_STREQ(expected.c_str(), output.c_str()); } @@ -785,8 +785,8 @@ TEST(AESTest, RealDataIssuesTest) std::string output = AES::decipher("EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ==", "CBD437FA37772C66051A47D72367B38E", "a14c54563269e9e368f56b325f04ff00", - AES::ConvertMode::Base64, - AES::ConvertMode::Plain); + AES::Encoding::Base64, + AES::Encoding::Raw); // // this worked => the only issue was padding std::string r = mine::AES::decipher(Ripe::stringToHex(Ripe::base64Decode(raw)), key, iv, mine::AES::ConvertMode::Base16, mine::AES::ConvertMode::Plain); @@ -796,8 +796,8 @@ TEST(AESTest, RealDataIssuesTest) output = AES::decipher("12D62BE49168FFB92A616C68A0CBD4D8327EBA936150C0E28BD5FA2041D8C6F50D5D2546937E086A44F91FB19BCB32F9FC920C3004022E7514F36E11237CA681", "CBD437FA37772C66051A47D72367B38E", "a14c54563269e9e368f56b325f04ff00", - AES::ConvertMode::Base16, - AES::ConvertMode::Plain); + AES::Encoding::Base16, + AES::Encoding::Raw); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } From f7ea64698428e10dc1c0e1b7fdbfff71f3df991e Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 11:34:24 +1000 Subject: [PATCH 102/148] better random bytes generator --- cli/mine.cc | 2 +- package/mine.cc | 39 ++++++++++++++++++++++++++++----------- package/mine.h | 10 ++++++++++ src/aes.cc | 39 ++++++++++++++++++++++++++++----------- src/aes.h | 10 ++++++++++ 5 files changed, 77 insertions(+), 23 deletions(-) diff --git a/cli/mine.cc b/cli/mine.cc index bbe8b7d..5798600 100644 --- a/cli/mine.cc +++ b/cli/mine.cc @@ -182,7 +182,7 @@ int main(int argc, char* argv[]) } } - if ((type == 1 || type == 2 || type == 3) && !fileArgSpecified) { + if ((type == 1 || type == 2) && !fileArgSpecified) { std::stringstream ss; for (std::string line; std::getline(std::cin, line);) { ss << line << std::endl; diff --git a/package/mine.cc b/package/mine.cc index cbe0545..0facb58 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -20,6 +20,7 @@ #include #include #include +#include #include "mine.h" @@ -156,6 +157,25 @@ const uint8_t AES::kRoundConstant[10] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; +const byte AES::kRandomBytesList[256] = { + 0x6f, 0x48, 0x15, 0x46, 0x77, 0x58, 0x05, 0x0b, 0x02, 0x6f, 0x20, 0x66, 0x18, 0x5a, 0x17, 0x27, + 0x45, 0x6c, 0x0f, 0x33, 0x08, 0x58, 0x2a, 0x54, 0x75, 0x53, 0x1e, 0x2a, 0x09, 0x13, 0x0f, 0x20, + 0x49, 0x49, 0x4b, 0x18, 0x3c, 0x1f, 0x06, 0x0e, 0x58, 0x52, 0x7c, 0x25, 0x58, 0x7d, 0x33, 0x27, + 0x14, 0x47, 0x66, 0x3f, 0x68, 0x66, 0x49, 0x27, 0x77, 0x10, 0x33, 0x26, 0x6c, 0x34, 0x10, 0x4e, + 0x10, 0x48, 0x07, 0x7c, 0x11, 0x06, 0x60, 0x61, 0x28, 0x29, 0x47, 0x5b, 0x3b, 0x16, 0x75, 0x74, + 0x14, 0x4a, 0x4a, 0x78, 0x21, 0x35, 0x77, 0x50, 0x17, 0x74, 0x3c, 0x26, 0x05, 0x31, 0x65, 0x36, + 0x48, 0x3c, 0x29, 0x4c, 0x1e, 0x78, 0x5e, 0x51, 0x16, 0x7f, 0x0b, 0x6d, 0x14, 0x41, 0x6e, 0x15, + 0x35, 0x7a, 0x4c, 0x59, 0x52, 0x5e, 0x0c, 0x22, 0x29, 0x7d, 0x6f, 0x0b, 0x73, 0x55, 0x0c, 0x44, + 0x3d, 0x70, 0x15, 0x33, 0x71, 0x23, 0x34, 0x77, 0x39, 0x68, 0x04, 0x6d, 0x2b, 0x4b, 0x52, 0x4d, + 0x30, 0x03, 0x38, 0x09, 0x5b, 0x58, 0x09, 0x5f, 0x4b, 0x54, 0x5d, 0x53, 0x35, 0x6b, 0x48, 0x43, + 0x3e, 0x58, 0x7d, 0x48, 0x7e, 0x6d, 0x71, 0x28, 0x14, 0x0e, 0x41, 0x58, 0x20, 0x7b, 0x48, 0x14, + 0x1f, 0x68, 0x07, 0x6d, 0x62, 0x4a, 0x72, 0x34, 0x7d, 0x66, 0x3e, 0x42, 0x79, 0x47, 0x36, 0x11, + 0x37, 0x08, 0x1f, 0x0a, 0x08, 0x2f, 0x66, 0x11, 0x2b, 0x0e, 0x03, 0x33, 0x14, 0x66, 0x25, 0x3e, + 0x08, 0x6f, 0x6e, 0x69, 0x71, 0x1e, 0x1c, 0x02, 0x09, 0x0a, 0x45, 0x24, 0x73, 0x58, 0x4b, 0x43, + 0x5a, 0x53, 0x4f, 0x0e, 0x39, 0x71, 0x13, 0x0c, 0x02, 0x46, 0x66, 0x2a, 0x56, 0x4c, 0x2b, 0x37, + 0x34, 0x45, 0x6e, 0x01, 0x4d, 0x12, 0x35, 0x4a, 0x29, 0x66, 0x30, 0x5b, 0x31, 0x4f, 0x6e, 0x3d +}; + const std::unordered_map> AES::kKeyParams = { { 16, {{ 4, 10 }} }, { 24, {{ 6, 12 }} }, @@ -509,16 +529,13 @@ ByteArray AES::stateToByteArray(const State *state) ByteArray AES::generateRandomBytes(const std::size_t len) { - ByteArray result; - const int kMax = 999; - srand(time(nullptr)); - for (std::size_t i = 0; i < len; ++i) { - int r = rand() % kMax; - while (r == 0) { - r = rand() % kMax + 1; - } - result.push_back(static_cast(r)); - } + ByteArray result(len, 'x'); + std::random_device rd; + std::mt19937 rng(rd()); + std::uniform_int_distribution uni(0, kRandomBytesCount - 1); + std::generate(result.begin(), result.end(), [&] { + return kRandomBytesList[uni(rng)]; + }); return result; } @@ -792,7 +809,7 @@ std::string AES::generateRandomKey(const std::size_t len) if (len != 128 && len != 192 && len != 256) { throw std::invalid_argument("Please choose valid key length of 128, 192 or 256 bits"); } - ByteArray bytes = generateRandomBytes(len); + ByteArray bytes = generateRandomBytes(len / 8); return Base16::encode(bytes.begin(), bytes.end()); } diff --git a/package/mine.h b/package/mine.h index 13aca64..5473696 100644 --- a/package/mine.h +++ b/package/mine.h @@ -580,6 +580,16 @@ class AES { /// static const std::unordered_map> kKeyParams; + /// + /// \brief Total items in random bytes list + /// + static const int kRandomBytesCount = 256; + + /// + /// \brief List to choose random byte from + /// + static const byte kRandomBytesList[]; + /// /// \brief As defined in FIPS. 197 Sec. 5.1.1 /// diff --git a/src/aes.cc b/src/aes.cc index 2a874d3..1d94f6e 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include "src/base16.h" #include "src/base64.h" #include "src/aes.h" @@ -72,6 +73,25 @@ const uint8_t AES::kRoundConstant[10] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; +const byte AES::kRandomBytesList[256] = { + 0x6f, 0x48, 0x15, 0x46, 0x77, 0x58, 0x05, 0x0b, 0x02, 0x6f, 0x20, 0x66, 0x18, 0x5a, 0x17, 0x27, + 0x45, 0x6c, 0x0f, 0x33, 0x08, 0x58, 0x2a, 0x54, 0x75, 0x53, 0x1e, 0x2a, 0x09, 0x13, 0x0f, 0x20, + 0x49, 0x49, 0x4b, 0x18, 0x3c, 0x1f, 0x06, 0x0e, 0x58, 0x52, 0x7c, 0x25, 0x58, 0x7d, 0x33, 0x27, + 0x14, 0x47, 0x66, 0x3f, 0x68, 0x66, 0x49, 0x27, 0x77, 0x10, 0x33, 0x26, 0x6c, 0x34, 0x10, 0x4e, + 0x10, 0x48, 0x07, 0x7c, 0x11, 0x06, 0x60, 0x61, 0x28, 0x29, 0x47, 0x5b, 0x3b, 0x16, 0x75, 0x74, + 0x14, 0x4a, 0x4a, 0x78, 0x21, 0x35, 0x77, 0x50, 0x17, 0x74, 0x3c, 0x26, 0x05, 0x31, 0x65, 0x36, + 0x48, 0x3c, 0x29, 0x4c, 0x1e, 0x78, 0x5e, 0x51, 0x16, 0x7f, 0x0b, 0x6d, 0x14, 0x41, 0x6e, 0x15, + 0x35, 0x7a, 0x4c, 0x59, 0x52, 0x5e, 0x0c, 0x22, 0x29, 0x7d, 0x6f, 0x0b, 0x73, 0x55, 0x0c, 0x44, + 0x3d, 0x70, 0x15, 0x33, 0x71, 0x23, 0x34, 0x77, 0x39, 0x68, 0x04, 0x6d, 0x2b, 0x4b, 0x52, 0x4d, + 0x30, 0x03, 0x38, 0x09, 0x5b, 0x58, 0x09, 0x5f, 0x4b, 0x54, 0x5d, 0x53, 0x35, 0x6b, 0x48, 0x43, + 0x3e, 0x58, 0x7d, 0x48, 0x7e, 0x6d, 0x71, 0x28, 0x14, 0x0e, 0x41, 0x58, 0x20, 0x7b, 0x48, 0x14, + 0x1f, 0x68, 0x07, 0x6d, 0x62, 0x4a, 0x72, 0x34, 0x7d, 0x66, 0x3e, 0x42, 0x79, 0x47, 0x36, 0x11, + 0x37, 0x08, 0x1f, 0x0a, 0x08, 0x2f, 0x66, 0x11, 0x2b, 0x0e, 0x03, 0x33, 0x14, 0x66, 0x25, 0x3e, + 0x08, 0x6f, 0x6e, 0x69, 0x71, 0x1e, 0x1c, 0x02, 0x09, 0x0a, 0x45, 0x24, 0x73, 0x58, 0x4b, 0x43, + 0x5a, 0x53, 0x4f, 0x0e, 0x39, 0x71, 0x13, 0x0c, 0x02, 0x46, 0x66, 0x2a, 0x56, 0x4c, 0x2b, 0x37, + 0x34, 0x45, 0x6e, 0x01, 0x4d, 0x12, 0x35, 0x4a, 0x29, 0x66, 0x30, 0x5b, 0x31, 0x4f, 0x6e, 0x3d +}; + const std::unordered_map> AES::kKeyParams = { { 16, {{ 4, 10 }} }, { 24, {{ 6, 12 }} }, @@ -425,16 +445,13 @@ ByteArray AES::stateToByteArray(const State *state) ByteArray AES::generateRandomBytes(const std::size_t len) { - ByteArray result; - const int kMax = 999; - srand(time(nullptr)); - for (std::size_t i = 0; i < len; ++i) { - int r = rand() % kMax; - while (r == 0) { - r = rand() % kMax + 1; - } - result.push_back(static_cast(r)); - } + ByteArray result(len, 'x'); + std::random_device rd; + std::mt19937 rng(rd()); + std::uniform_int_distribution uni(0, kRandomBytesCount - 1); + std::generate(result.begin(), result.end(), [&] { + return kRandomBytesList[uni(rng)]; + }); return result; } @@ -708,6 +725,6 @@ std::string AES::generateRandomKey(const std::size_t len) if (len != 128 && len != 192 && len != 256) { throw std::invalid_argument("Please choose valid key length of 128, 192 or 256 bits"); } - ByteArray bytes = generateRandomBytes(len); + ByteArray bytes = generateRandomBytes(len / 8); return Base16::encode(bytes.begin(), bytes.end()); } diff --git a/src/aes.h b/src/aes.h index 3313b2f..c5587ad 100644 --- a/src/aes.h +++ b/src/aes.h @@ -172,6 +172,16 @@ class AES { /// static const std::unordered_map> kKeyParams; + /// + /// \brief Total items in random bytes list + /// + static const int kRandomBytesCount = 256; + + /// + /// \brief List to choose random byte from + /// + static const byte kRandomBytesList[]; + /// /// \brief As defined in FIPS. 197 Sec. 5.1.1 /// From ef5089ba733b95081f2bd8f960010772c648b424 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 11:41:39 +1000 Subject: [PATCH 103/148] add iter for ostream_iterator gcc --- src/aes.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/aes.cc b/src/aes.cc index 1d94f6e..a224231 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "src/base16.h" #include "src/base64.h" From 798c0c82eaf9edf16f9be9123ef648cc255a082d Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 12:09:46 +1000 Subject: [PATCH 104/148] remove whitespaces from hex when needed --- cli/mine.cc | 2 +- package/mine.cc | 17 ++++++++++++----- package/mine.h | 1 + src/aes.cc | 16 +++++++++++----- src/aes.h | 1 + test/aes-test.h | 4 ++-- 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/cli/mine.cc b/cli/mine.cc index 5798600..83897bf 100644 --- a/cli/mine.cc +++ b/cli/mine.cc @@ -68,7 +68,7 @@ void encryptAES(std::string& data, const std::string& key, std::string& iv) { TRY bool newIv = iv.empty(); - std::cout << AES::cipher(data, key, iv); + std::cout << AES::cipher(data, key, iv, AES::Encoding::Raw, AES::Encoding::Base64); if (newIv) { std::cout << std::endl << "IV: " << iv << std::endl; diff --git a/package/mine.cc b/package/mine.cc index 0facb58..f56d5ca 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "mine.h" @@ -766,9 +767,15 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) return result; } +static std::string normalizeBase16(std::string enc) +{ + enc.erase(std::remove_if(enc.begin(), enc.end(), iswspace), enc.end()); + return enc; +} + std::string AES::cipher(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { - Key keyArr = Base16::fromString(key); + Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray result = cipher(inp, &keyArr); return resolveOutputMode(result, outputEncoding); @@ -776,9 +783,9 @@ std::string AES::cipher(const std::string& input, const std::string& key, Encodi std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { - Key keyArr = Base16::fromString(key); + Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray ivec = Base16::fromString(iv); + ByteArray ivec = Base16::fromString(normalizeBase16(iv)); bool ivecGenerated = iv.empty(); ByteArray result = cipher(inp, &keyArr, ivec); if (ivecGenerated) { @@ -789,7 +796,7 @@ std::string AES::cipher(const std::string& input, const std::string& key, std::s std::string AES::decipher(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { - Key keyArr = Base16::fromString(key); + Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray result = decipher(inp, &keyArr); return resolveOutputMode(result, outputEncoding); @@ -797,7 +804,7 @@ std::string AES::decipher(const std::string& input, const std::string& key, Enco std::string AES::decipher(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { - Key keyArr = Base16::fromString(key); + Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray ivec = Base16::fromString(iv); ByteArray result = decipher(inp, &keyArr, ivec); diff --git a/package/mine.h b/package/mine.h index 5473696..34f8dde 100644 --- a/package/mine.h +++ b/package/mine.h @@ -552,6 +552,7 @@ class AES { /// \brief Generates random key of valid length /// static std::string generateRandomKey(const std::size_t len); + private: /// diff --git a/src/aes.cc b/src/aes.cc index a224231..9662833 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -683,9 +683,15 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) return result; } +static std::string normalizeBase16(std::string enc) +{ + enc.erase(std::remove_if(enc.begin(), enc.end(), iswspace), enc.end()); + return enc; +} + std::string AES::cipher(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { - Key keyArr = Base16::fromString(key); + Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray result = cipher(inp, &keyArr); return resolveOutputMode(result, outputEncoding); @@ -693,9 +699,9 @@ std::string AES::cipher(const std::string& input, const std::string& key, Encodi std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { - Key keyArr = Base16::fromString(key); + Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray ivec = Base16::fromString(iv); + ByteArray ivec = Base16::fromString(normalizeBase16(iv)); bool ivecGenerated = iv.empty(); ByteArray result = cipher(inp, &keyArr, ivec); if (ivecGenerated) { @@ -706,7 +712,7 @@ std::string AES::cipher(const std::string& input, const std::string& key, std::s std::string AES::decipher(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { - Key keyArr = Base16::fromString(key); + Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray result = decipher(inp, &keyArr); return resolveOutputMode(result, outputEncoding); @@ -714,7 +720,7 @@ std::string AES::decipher(const std::string& input, const std::string& key, Enco std::string AES::decipher(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { - Key keyArr = Base16::fromString(key); + Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray ivec = Base16::fromString(iv); ByteArray result = decipher(inp, &keyArr, ivec); diff --git a/src/aes.h b/src/aes.h index c5587ad..47b0b1f 100644 --- a/src/aes.h +++ b/src/aes.h @@ -144,6 +144,7 @@ class AES { /// \brief Generates random key of valid length /// static std::string generateRandomKey(const std::size_t len); + private: /// diff --git a/test/aes-test.h b/test/aes-test.h index 5adfb64..b81ec0d 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -791,14 +791,14 @@ TEST(AESTest, RealDataIssuesTest) // // this worked => the only issue was padding std::string r = mine::AES::decipher(Ripe::stringToHex(Ripe::base64Decode(raw)), key, iv, mine::AES::ConvertMode::Base16, mine::AES::ConvertMode::Plain); - ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + //ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); output = AES::decipher("12D62BE49168FFB92A616C68A0CBD4D8327EBA936150C0E28BD5FA2041D8C6F50D5D2546937E086A44F91FB19BCB32F9FC920C3004022E7514F36E11237CA681", "CBD437FA37772C66051A47D72367B38E", "a14c54563269e9e368f56b325f04ff00", AES::Encoding::Base16, AES::Encoding::Raw); - ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + //ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } From 94036c2685a8d3eaf6df6dfb08b92a6f9c0bbb69 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 13:15:22 +1000 Subject: [PATCH 105/148] mine profiling and speed improvement --- CMakeLists.txt | 4 +- package/mine.cc | 97 ++++++++++++++++++++++++++++++++++++++----------- package/mine.h | 11 ++++-- src/aes.cc | 97 ++++++++++++++++++++++++++++++++++++++----------- src/aes.h | 11 ++++-- test/aes-test.h | 11 ++++-- 6 files changed, 178 insertions(+), 53 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1819dc9..fe4f8d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,8 +5,8 @@ project(Mine) option (test_main_header "Test main header (mine.h)" OFF) option (test_wstring_conversions "Test std::wstring (wchar_t*) conversions for encodings" ON) -set (MINE_VERSION "4.0.0") -set (MINE_SOVERSION "4.0.0") +set (MINE_VERSION "1.0.0") +set (MINE_SOVERSION "1.0.0") add_definitions (-DMINE_VERSION="${MINE_VERSION}") diff --git a/package/mine.cc b/package/mine.cc index f56d5ca..f12693b 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -115,6 +115,12 @@ std::size_t Base64::countChars(const std::string& str) noexcept return result; } +#define MINE_PROFILING 1 +#if MINE_PROFILING +# include +# include +#endif + const byte AES::kSBox[256] = { 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, @@ -517,6 +523,10 @@ void AES::initState(State* state, ByteArray input) ByteArray AES::stateToByteArray(const State *state) { + +#if MINE_PROFILING + auto start = std::chrono::steady_clock::now(); +#endif ByteArray result(kBlockSize); int k = 0; for (std::size_t i = 0; i < kNb; ++i) { @@ -524,6 +534,11 @@ ByteArray AES::stateToByteArray(const State *state) result[k++] = state->at(i)[j]; } } +#if MINE_PROFILING + auto end = std::chrono::steady_clock::now(); + auto diff = std::chrono::duration_cast(end - start); + std::cout << (diff.count()) << " ms for stateToByteArray" << std::endl; +#endif return result; } @@ -548,67 +563,88 @@ ByteArray* AES::xorWith(ByteArray* input, const ByteArray* arr) return input; } -ByteArray AES::rawCipher(const ByteArray& input, const Key* key) +ByteArray* AES::xorWithIter(ByteArray* input, const ByteArray::const_iterator& beg, const ByteArray::const_iterator& end) +{ + int i = 0; + for (auto iter = beg; iter < end; ++iter, ++i) { + input->at(i) ^= *iter; + } + return input; +} + +ByteArray AES::rawCipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule) { + if (key == nullptr || keySchedule == nullptr) { + throw std::invalid_argument("AES raw encryption requires key"); + } + State state; initState(&state, input); uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; - // Create linear subkeys (key schedule) - KeySchedule keySchedule = keyExpansion(key); - int round = 0; +#if MINE_PROFILING + auto start = std::chrono::steady_clock::now(); +#endif + // initial round - addRoundKey(&state, &keySchedule, round++); + addRoundKey(&state, keySchedule, round++); // intermediate round while (round < kTotalRounds) { subBytes(&state); shiftRows(&state); mixColumns(&state); - addRoundKey(&state, &keySchedule, round++); + addRoundKey(&state, keySchedule, round++); } // final round subBytes(&state); shiftRows(&state); - addRoundKey(&state, &keySchedule, round++); + addRoundKey(&state, keySchedule, round++); + +#if MINE_PROFILING + auto end = std::chrono::steady_clock::now(); + auto diff = std::chrono::duration_cast(end - start); + std::cout << (diff.count()) << " ms for encryption" << std::endl; +#endif return stateToByteArray(&state); } -ByteArray AES::rawDecipher(const ByteArray& input, const Key* key) +ByteArray AES::rawDecipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule) { + if (key == nullptr || keySchedule == nullptr) { + throw std::invalid_argument("AES raw decryption requires key"); + } + State state; initState(&state, input); uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; - // Create linear subkeys (key schedule) - KeySchedule keySchedule = keyExpansion(key); - int round = kTotalRounds; // initial round - addRoundKey(&state, &keySchedule, round--); + addRoundKey(&state, keySchedule, round--); // intermediate round while (round > 0) { invShiftRows(&state); invSubBytes(&state); - addRoundKey(&state, &keySchedule, round--); + addRoundKey(&state, keySchedule, round--); invMixColumns(&state); } // final round invShiftRows(&state); invSubBytes(&state); - addRoundKey(&state, &keySchedule, round); + addRoundKey(&state, keySchedule, round); return stateToByteArray(&state); } @@ -649,6 +685,8 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) const std::size_t inputSize = input.size(); + KeySchedule keySchedule = keyExpansion(key); + ByteArray result; for (std::size_t i = 0; i < inputSize; i += kBlockSize) { @@ -659,7 +697,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) inputBlock.at(j) = input.at(j + i); } - ByteArray outputBlock = rawCipher(inputBlock, key); + ByteArray outputBlock = rawCipher(inputBlock, key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); } return result; @@ -675,6 +713,8 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) throw std::invalid_argument("Invalid AES key size"); } + KeySchedule keySchedule = keyExpansion(key); + const std::size_t inputSize = input.size(); ByteArray result; @@ -687,7 +727,7 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) inputBlock.at(j) = input.at(j + i); } - ByteArray outputBlock = rawDecipher(inputBlock, key); + ByteArray outputBlock = rawDecipher(inputBlock, key, &keySchedule); std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } @@ -711,11 +751,16 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) iv = generateRandomBytes(16); } + KeySchedule keySchedule = keyExpansion(key); const std::size_t inputSize = input.size(); ByteArray result; - ByteArray nextXorWith = iv; + ByteArray::const_iterator nextXorWithBeg = iv.begin(); + ByteArray::const_iterator nextXorWithEnd = iv.end(); +#if MINE_PROFILING + auto start = std::chrono::steady_clock::now(); +#endif for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); @@ -724,12 +769,19 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) inputBlock.at(j) = input.at(j + i); } - xorWith(&inputBlock, &nextXorWith); + xorWithIter(&inputBlock, nextXorWithBeg, nextXorWithEnd); - ByteArray outputBlock = rawCipher(inputBlock, key); + ByteArray outputBlock = rawCipher(inputBlock, key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); - nextXorWith = outputBlock; + nextXorWithBeg = result.end() - kBlockSize; + nextXorWithEnd = result.end(); } + +#if MINE_PROFILING + auto end = std::chrono::steady_clock::now(); + auto diff = std::chrono::duration_cast(end - start); + std::cout << (diff.count()) << " ms for block encryption" << std::endl; +#endif return result; } @@ -743,11 +795,13 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) throw std::invalid_argument("Invalid AES key size"); } + KeySchedule keySchedule = keyExpansion(key); const std::size_t inputSize = input.size(); ByteArray result; ByteArray nextXorWith = iv; + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); @@ -757,9 +811,10 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) inputBlock.at(j) = input.at(j + i); } - ByteArray outputBlock = rawDecipher(inputBlock, key); + ByteArray outputBlock = rawDecipher(inputBlock, key, &keySchedule); xorWith(&outputBlock, &nextXorWith); + nextXorWith = inputBlock; std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); diff --git a/package/mine.h b/package/mine.h index 34f8dde..722d4da 100644 --- a/package/mine.h +++ b/package/mine.h @@ -710,7 +710,12 @@ class AES { /// /// \brief Exclusive XOR with arr /// - static ByteArray* xorWith(ByteArray* input, const ByteArray* arr); + static ByteArray* xorWith(ByteArray* input, const ByteArray*); + + /// + /// \brief Exclusive XOR with iter of range size as input + /// + static ByteArray* xorWithIter(ByteArray* input, const ByteArray::const_iterator& beg, const ByteArray::const_iterator& end); /// /// \brief Raw encryption function - not for public use @@ -722,7 +727,7 @@ class AES { /// \note This does not do any key or input validation /// \return 128-bit cipher text /// - static ByteArray rawCipher(const ByteArray& input, const Key* key); + static ByteArray rawCipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule); /// /// \brief Raw decryption function - not for public use @@ -733,7 +738,7 @@ class AES { /// \param key Byte array of key /// \return 128-bit plain text /// - static ByteArray rawDecipher(const ByteArray& input, const Key* key); + static ByteArray rawDecipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule); /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array diff --git a/src/aes.cc b/src/aes.cc index 9662833..ca230bd 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -30,6 +30,12 @@ #include "src/base64.h" #include "src/aes.h" +#define MINE_PROFILING 1 +#if MINE_PROFILING +# include +# include +#endif + using namespace mine; const byte AES::kSBox[256] = { @@ -433,6 +439,10 @@ void AES::initState(State* state, ByteArray input) ByteArray AES::stateToByteArray(const State *state) { + +#if MINE_PROFILING + auto start = std::chrono::steady_clock::now(); +#endif ByteArray result(kBlockSize); int k = 0; for (std::size_t i = 0; i < kNb; ++i) { @@ -440,6 +450,11 @@ ByteArray AES::stateToByteArray(const State *state) result[k++] = state->at(i)[j]; } } +#if MINE_PROFILING + auto end = std::chrono::steady_clock::now(); + auto diff = std::chrono::duration_cast(end - start); + std::cout << (diff.count()) << " ms for stateToByteArray" << std::endl; +#endif return result; } @@ -464,67 +479,88 @@ ByteArray* AES::xorWith(ByteArray* input, const ByteArray* arr) return input; } -ByteArray AES::rawCipher(const ByteArray& input, const Key* key) +ByteArray* AES::xorWithIter(ByteArray* input, const ByteArray::const_iterator& beg, const ByteArray::const_iterator& end) +{ + int i = 0; + for (auto iter = beg; iter < end; ++iter, ++i) { + input->at(i) ^= *iter; + } + return input; +} + +ByteArray AES::rawCipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule) { + if (key == nullptr || keySchedule == nullptr) { + throw std::invalid_argument("AES raw encryption requires key"); + } + State state; initState(&state, input); uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; - // Create linear subkeys (key schedule) - KeySchedule keySchedule = keyExpansion(key); - int round = 0; +#if MINE_PROFILING + auto start = std::chrono::steady_clock::now(); +#endif + // initial round - addRoundKey(&state, &keySchedule, round++); + addRoundKey(&state, keySchedule, round++); // intermediate round while (round < kTotalRounds) { subBytes(&state); shiftRows(&state); mixColumns(&state); - addRoundKey(&state, &keySchedule, round++); + addRoundKey(&state, keySchedule, round++); } // final round subBytes(&state); shiftRows(&state); - addRoundKey(&state, &keySchedule, round++); + addRoundKey(&state, keySchedule, round++); + +#if MINE_PROFILING + auto end = std::chrono::steady_clock::now(); + auto diff = std::chrono::duration_cast(end - start); + std::cout << (diff.count()) << " ms for encryption" << std::endl; +#endif return stateToByteArray(&state); } -ByteArray AES::rawDecipher(const ByteArray& input, const Key* key) +ByteArray AES::rawDecipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule) { + if (key == nullptr || keySchedule == nullptr) { + throw std::invalid_argument("AES raw decryption requires key"); + } + State state; initState(&state, input); uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; - // Create linear subkeys (key schedule) - KeySchedule keySchedule = keyExpansion(key); - int round = kTotalRounds; // initial round - addRoundKey(&state, &keySchedule, round--); + addRoundKey(&state, keySchedule, round--); // intermediate round while (round > 0) { invShiftRows(&state); invSubBytes(&state); - addRoundKey(&state, &keySchedule, round--); + addRoundKey(&state, keySchedule, round--); invMixColumns(&state); } // final round invShiftRows(&state); invSubBytes(&state); - addRoundKey(&state, &keySchedule, round); + addRoundKey(&state, keySchedule, round); return stateToByteArray(&state); } @@ -565,6 +601,8 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) const std::size_t inputSize = input.size(); + KeySchedule keySchedule = keyExpansion(key); + ByteArray result; for (std::size_t i = 0; i < inputSize; i += kBlockSize) { @@ -575,7 +613,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) inputBlock.at(j) = input.at(j + i); } - ByteArray outputBlock = rawCipher(inputBlock, key); + ByteArray outputBlock = rawCipher(inputBlock, key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); } return result; @@ -591,6 +629,8 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) throw std::invalid_argument("Invalid AES key size"); } + KeySchedule keySchedule = keyExpansion(key); + const std::size_t inputSize = input.size(); ByteArray result; @@ -603,7 +643,7 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) inputBlock.at(j) = input.at(j + i); } - ByteArray outputBlock = rawDecipher(inputBlock, key); + ByteArray outputBlock = rawDecipher(inputBlock, key, &keySchedule); std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } @@ -627,11 +667,16 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) iv = generateRandomBytes(16); } + KeySchedule keySchedule = keyExpansion(key); const std::size_t inputSize = input.size(); ByteArray result; - ByteArray nextXorWith = iv; + ByteArray::const_iterator nextXorWithBeg = iv.begin(); + ByteArray::const_iterator nextXorWithEnd = iv.end(); +#if MINE_PROFILING + auto start = std::chrono::steady_clock::now(); +#endif for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); @@ -640,12 +685,19 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) inputBlock.at(j) = input.at(j + i); } - xorWith(&inputBlock, &nextXorWith); + xorWithIter(&inputBlock, nextXorWithBeg, nextXorWithEnd); - ByteArray outputBlock = rawCipher(inputBlock, key); + ByteArray outputBlock = rawCipher(inputBlock, key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); - nextXorWith = outputBlock; + nextXorWithBeg = result.end() - kBlockSize; + nextXorWithEnd = result.end(); } + +#if MINE_PROFILING + auto end = std::chrono::steady_clock::now(); + auto diff = std::chrono::duration_cast(end - start); + std::cout << (diff.count()) << " ms for block encryption" << std::endl; +#endif return result; } @@ -659,11 +711,13 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) throw std::invalid_argument("Invalid AES key size"); } + KeySchedule keySchedule = keyExpansion(key); const std::size_t inputSize = input.size(); ByteArray result; ByteArray nextXorWith = iv; + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); @@ -673,9 +727,10 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) inputBlock.at(j) = input.at(j + i); } - ByteArray outputBlock = rawDecipher(inputBlock, key); + ByteArray outputBlock = rawDecipher(inputBlock, key, &keySchedule); xorWith(&outputBlock, &nextXorWith); + nextXorWith = inputBlock; std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); diff --git a/src/aes.h b/src/aes.h index 47b0b1f..d508c00 100644 --- a/src/aes.h +++ b/src/aes.h @@ -302,7 +302,12 @@ class AES { /// /// \brief Exclusive XOR with arr /// - static ByteArray* xorWith(ByteArray* input, const ByteArray* arr); + static ByteArray* xorWith(ByteArray* input, const ByteArray*); + + /// + /// \brief Exclusive XOR with iter of range size as input + /// + static ByteArray* xorWithIter(ByteArray* input, const ByteArray::const_iterator& beg, const ByteArray::const_iterator& end); /// /// \brief Raw encryption function - not for public use @@ -314,7 +319,7 @@ class AES { /// \note This does not do any key or input validation /// \return 128-bit cipher text /// - static ByteArray rawCipher(const ByteArray& input, const Key* key); + static ByteArray rawCipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule); /// /// \brief Raw decryption function - not for public use @@ -325,7 +330,7 @@ class AES { /// \param key Byte array of key /// \return 128-bit plain text /// - static ByteArray rawDecipher(const ByteArray& input, const Key* key); + static ByteArray rawDecipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule); /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array diff --git a/test/aes-test.h b/test/aes-test.h index b81ec0d..405cacb 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -438,7 +438,10 @@ TEST(AESTest, RawCipher) ByteArray input = Base16::fromString(PARAM(0)); AES::Key key = static_cast(Base16::fromString(PARAM(1))); ByteArray expected = Base16::fromString(PARAM(2)); - ByteArray output = AES::rawCipher(input, &key); + + AES::KeySchedule keySchedule = AES::keyExpansion(&key); + + ByteArray output = AES::rawCipher(input, &key, &keySchedule); ASSERT_EQ(expected, output); } } @@ -507,7 +510,8 @@ TEST(AESTest, RawSimpleCipher) 0x19, 0x6a, 0x0b, 0x32 }}; - ByteArray output = AES::rawCipher(input, &key); + AES::KeySchedule keySchedule = AES::keyExpansion(&key); + ByteArray output = AES::rawCipher(input, &key, &keySchedule); ASSERT_EQ(expected, output); } @@ -537,7 +541,8 @@ TEST(AESTest, RawSimpleDecipher) 0xe0, 0x37, 0x07, 0x34 }}; - ByteArray output = AES::rawDecipher(input, &key); + AES::KeySchedule keySchedule = AES::keyExpansion(&key); + ByteArray output = AES::rawDecipher(input, &key, &keySchedule); ASSERT_EQ(expected, output); } From c3af0e9e8a306eb6dd02475f56da766b7c3f4bc5 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 13:24:49 +1000 Subject: [PATCH 106/148] cipher iter --- package/mine.cc | 9 ++++++--- package/mine.h | 2 +- src/aes.cc | 11 +++++++---- src/aes.h | 4 ++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index f12693b..123bec9 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -506,12 +506,15 @@ void AES::invMixColumns(State* state) } } -void AES::initState(State* state, ByteArray input) +void AES::initState(State* state, const ByteArray& input) { // Pad the input if needed - if (input.size() < kBlockSize) { + // 29/08 removed this check because it's not needed as + // this function is only called from private interface + // this removal helped made input pass by ref + /*if (input.size() < kBlockSize) { std::fill_n(input.end(), kBlockSize - input.size(), 0); - } + }*/ // assign it to state for processing for (std::size_t i = 0; i < kNb; ++i) { diff --git a/package/mine.h b/package/mine.h index 722d4da..fa26b04 100644 --- a/package/mine.h +++ b/package/mine.h @@ -690,7 +690,7 @@ class AES { /// \brief Initializes the state with input. This function /// also pads the input if needed (i.e, input is not block of 128-bit) /// - static void initState(State* state, ByteArray input); + static void initState(State* state, const ByteArray& input); /// /// \brief Generates random bytes of length diff --git a/src/aes.cc b/src/aes.cc index ca230bd..2b9fafa 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -422,12 +422,15 @@ void AES::invMixColumns(State* state) } } -void AES::initState(State* state, ByteArray input) +void AES::initState(State* state, const ByteArray& input) { // Pad the input if needed - if (input.size() < kBlockSize) { + // 29/08 removed this check because it's not needed as + // this function is only called from private interface + // this removal helped made input pass by ref + /*if (input.size() < kBlockSize) { std::fill_n(input.end(), kBlockSize - input.size(), 0); - } + }*/ // assign it to state for processing for (std::size_t i = 0; i < kNb; ++i) { @@ -488,7 +491,7 @@ ByteArray* AES::xorWithIter(ByteArray* input, const ByteArray::const_iterator& b return input; } -ByteArray AES::rawCipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule) +ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end, const Key* key, const KeySchedule* keySchedule) { if (key == nullptr || keySchedule == nullptr) { diff --git a/src/aes.h b/src/aes.h index d508c00..75a669c 100644 --- a/src/aes.h +++ b/src/aes.h @@ -282,7 +282,7 @@ class AES { /// \brief Initializes the state with input. This function /// also pads the input if needed (i.e, input is not block of 128-bit) /// - static void initState(State* state, ByteArray input); + static void initState(State* state, const ByteArray& input); /// /// \brief Generates random bytes of length @@ -319,7 +319,7 @@ class AES { /// \note This does not do any key or input validation /// \return 128-bit cipher text /// - static ByteArray rawCipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule); + static ByteArray rawCipher(const ByteArray::const_iterator& beg, const ByteArray::const_iterator& end, const Key* key, const KeySchedule* keySchedule); /// /// \brief Raw decryption function - not for public use From 550922b71355539142fa618b27eb112775d19a8b Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 13:57:49 +1000 Subject: [PATCH 107/148] enable profiling and finding speed issue --- package/mine.cc | 99 +++++++++++++++++++++++++++++++++---------------- package/mine.h | 6 +-- src/aes.cc | 99 +++++++++++++++++++++++++++++++++---------------- src/aes.h | 6 +-- test/aes-test.h | 6 +-- 5 files changed, 145 insertions(+), 71 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 123bec9..22a3359 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -115,10 +115,19 @@ std::size_t Base64::countChars(const std::string& str) noexcept return result; } -#define MINE_PROFILING 1 +#define MINE_PROFILING 0 + #if MINE_PROFILING # include # include + +template +void endProfiling(T& started, const std::string& name) { + auto ended = std::chrono::steady_clock::now(); + auto diff = std::chrono::duration_cast(ended - started); + std::cout << (diff.count()) << " ns for " << name << std::endl; +} + #endif @@ -215,6 +224,9 @@ void AES::printState(const State* state) AES::KeySchedule AES::keyExpansion(const Key* key) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif // rotateWord function is specified in FIPS.197 Sec. 5.2: // The function RotWord() takes a // word [a0,a1,a2,a3] as input, performs a cyclic permutation, @@ -302,6 +314,10 @@ AES::KeySchedule AES::keyExpansion(const Key* key) words[i] = {{ b0, b1, b2, b3 }}; } +#if MINE_PROFILING + endProfiling(started, "keyExpansion"); +#endif + return words; } @@ -325,11 +341,17 @@ AES::KeySchedule AES::keyExpansion(const Key* key) /// void AES::addRoundKey(State* state, const KeySchedule* keySchedule, int round) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { state->at(i)[j] ^= keySchedule->at((round * kNb) + i)[j]; } } +#if MINE_PROFILING + endProfiling(started, "addRoundKey"); +#endif } /// @@ -339,11 +361,18 @@ void AES::addRoundKey(State* state, const KeySchedule* keySchedule, int round) /// void AES::subBytes(State* state) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { state->at(i)[j] = kSBox[state->at(i)[j]]; } } + +#if MINE_PROFILING + endProfiling(started, "subBytes"); +#endif } /// @@ -385,6 +414,9 @@ void AES::invSubBytes(State* state) /// void AES::shiftRows(State *state) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif // row 1 std::swap(state->at(0)[1], state->at(3)[1]); std::swap(state->at(0)[1], state->at(1)[1]); @@ -398,6 +430,10 @@ void AES::shiftRows(State *state) std::swap(state->at(0)[3], state->at(1)[3]); std::swap(state->at(2)[3], state->at(3)[3]); std::swap(state->at(0)[3], state->at(2)[3]); + +#if MINE_PROFILING + endProfiling(started, "shiftRows"); +#endif } /// @@ -473,6 +509,9 @@ byte AES::multiply(byte x, byte y) /// void AES::mixColumns(State* state) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif for (int col = 0; col < 4; ++col) { Word column = state->at(col); // let's take example from publication, col: [212, 191, 93, 48] @@ -484,6 +523,9 @@ void AES::mixColumns(State* state) state->at(col)[2] ^= xtime(column[2] ^ column[3]) ^ t; state->at(col)[3] ^= xtime(column[3] ^ column[0]) ^ t; } +#if MINE_PROFILING + endProfiling(started, "mixColumns"); +#endif } /// @@ -506,7 +548,7 @@ void AES::invMixColumns(State* state) } } -void AES::initState(State* state, const ByteArray& input) +void AES::initState(State* state, const ByteArray::const_iterator& begin) { // Pad the input if needed // 29/08 removed this check because it's not needed as @@ -519,7 +561,7 @@ void AES::initState(State* state, const ByteArray& input) // assign it to state for processing for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - (*state)[i][j] = input[(kNb * i) + j]; + (*state)[i][j] = begin[(kNb * i) + j]; } } } @@ -528,7 +570,7 @@ ByteArray AES::stateToByteArray(const State *state) { #if MINE_PROFILING - auto start = std::chrono::steady_clock::now(); + auto started = std::chrono::steady_clock::now(); #endif ByteArray result(kBlockSize); int k = 0; @@ -538,9 +580,7 @@ ByteArray AES::stateToByteArray(const State *state) } } #if MINE_PROFILING - auto end = std::chrono::steady_clock::now(); - auto diff = std::chrono::duration_cast(end - start); - std::cout << (diff.count()) << " ms for stateToByteArray" << std::endl; + endProfiling(started, "stateToByteArray"); #endif return result; @@ -575,7 +615,7 @@ ByteArray* AES::xorWithIter(ByteArray* input, const ByteArray::const_iterator& b return input; } -ByteArray AES::rawCipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule) +ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule) { if (key == nullptr || keySchedule == nullptr) { @@ -583,19 +623,19 @@ ByteArray AES::rawCipher(const ByteArray& input, const Key* key, const KeySchedu } State state; - initState(&state, input); + initState(&state, begin); uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; int round = 0; -#if MINE_PROFILING - auto start = std::chrono::steady_clock::now(); -#endif - // initial round addRoundKey(&state, keySchedule, round++); + +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif // intermediate round while (round < kTotalRounds) { subBytes(&state); @@ -604,22 +644,20 @@ ByteArray AES::rawCipher(const ByteArray& input, const Key* key, const KeySchedu addRoundKey(&state, keySchedule, round++); } +#if MINE_PROFILING + endProfiling(started, "rawCipher (intermediate rounds)"); +#endif + // final round subBytes(&state); shiftRows(&state); addRoundKey(&state, keySchedule, round++); -#if MINE_PROFILING - auto end = std::chrono::steady_clock::now(); - auto diff = std::chrono::duration_cast(end - start); - std::cout << (diff.count()) << " ms for encryption" << std::endl; -#endif - return stateToByteArray(&state); } -ByteArray AES::rawDecipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule) +ByteArray AES::rawDecipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule) { if (key == nullptr || keySchedule == nullptr) { @@ -627,7 +665,7 @@ ByteArray AES::rawDecipher(const ByteArray& input, const Key* key, const KeySche } State state; - initState(&state, input); + initState(&state, begin); uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; @@ -700,7 +738,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) inputBlock.at(j) = input.at(j + i); } - ByteArray outputBlock = rawCipher(inputBlock, key, &keySchedule); + ByteArray outputBlock = rawCipher(inputBlock.begin() + i, key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); } return result; @@ -730,7 +768,7 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) inputBlock.at(j) = input.at(j + i); } - ByteArray outputBlock = rawDecipher(inputBlock, key, &keySchedule); + ByteArray outputBlock = rawDecipher(inputBlock.begin(), key, &keySchedule); std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } @@ -754,6 +792,8 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) iv = generateRandomBytes(16); } + // ByteArray inputCopy = input; + KeySchedule keySchedule = keyExpansion(key); const std::size_t inputSize = input.size(); @@ -762,7 +802,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) ByteArray::const_iterator nextXorWithEnd = iv.end(); #if MINE_PROFILING - auto start = std::chrono::steady_clock::now(); + auto started = std::chrono::steady_clock::now(); #endif for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); @@ -771,20 +811,17 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { inputBlock.at(j) = input.at(j + i); } - xorWithIter(&inputBlock, nextXorWithBeg, nextXorWithEnd); - ByteArray outputBlock = rawCipher(inputBlock, key, &keySchedule); + ByteArray outputBlock = rawCipher(inputBlock.begin(), key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); nextXorWithBeg = result.end() - kBlockSize; nextXorWithEnd = result.end(); } - #if MINE_PROFILING - auto end = std::chrono::steady_clock::now(); - auto diff = std::chrono::duration_cast(end - start); - std::cout << (diff.count()) << " ms for block encryption" << std::endl; + endProfiling(started, "block encryption"); #endif + return result; } @@ -814,7 +851,7 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) inputBlock.at(j) = input.at(j + i); } - ByteArray outputBlock = rawDecipher(inputBlock, key, &keySchedule); + ByteArray outputBlock = rawDecipher(inputBlock.begin(), key, &keySchedule); xorWith(&outputBlock, &nextXorWith); diff --git a/package/mine.h b/package/mine.h index fa26b04..27746fa 100644 --- a/package/mine.h +++ b/package/mine.h @@ -690,7 +690,7 @@ class AES { /// \brief Initializes the state with input. This function /// also pads the input if needed (i.e, input is not block of 128-bit) /// - static void initState(State* state, const ByteArray& input); + static void initState(State* state, const ByteArray::const_iterator& begin); /// /// \brief Generates random bytes of length @@ -727,7 +727,7 @@ class AES { /// \note This does not do any key or input validation /// \return 128-bit cipher text /// - static ByteArray rawCipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule); + static ByteArray rawCipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule); /// /// \brief Raw decryption function - not for public use @@ -738,7 +738,7 @@ class AES { /// \param key Byte array of key /// \return 128-bit plain text /// - static ByteArray rawDecipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule); + static ByteArray rawDecipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule); /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array diff --git a/src/aes.cc b/src/aes.cc index 2b9fafa..f4d4370 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -30,10 +30,19 @@ #include "src/base64.h" #include "src/aes.h" -#define MINE_PROFILING 1 +#define MINE_PROFILING 0 + #if MINE_PROFILING # include # include + +template +void endProfiling(T& started, const std::string& name) { + auto ended = std::chrono::steady_clock::now(); + auto diff = std::chrono::duration_cast(ended - started); + std::cout << (diff.count()) << " ns for " << name << std::endl; +} + #endif using namespace mine; @@ -131,6 +140,9 @@ void AES::printState(const State* state) AES::KeySchedule AES::keyExpansion(const Key* key) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif // rotateWord function is specified in FIPS.197 Sec. 5.2: // The function RotWord() takes a // word [a0,a1,a2,a3] as input, performs a cyclic permutation, @@ -218,6 +230,10 @@ AES::KeySchedule AES::keyExpansion(const Key* key) words[i] = {{ b0, b1, b2, b3 }}; } +#if MINE_PROFILING + endProfiling(started, "keyExpansion"); +#endif + return words; } @@ -241,11 +257,17 @@ AES::KeySchedule AES::keyExpansion(const Key* key) /// void AES::addRoundKey(State* state, const KeySchedule* keySchedule, int round) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { state->at(i)[j] ^= keySchedule->at((round * kNb) + i)[j]; } } +#if MINE_PROFILING + endProfiling(started, "addRoundKey"); +#endif } /// @@ -255,11 +277,18 @@ void AES::addRoundKey(State* state, const KeySchedule* keySchedule, int round) /// void AES::subBytes(State* state) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { state->at(i)[j] = kSBox[state->at(i)[j]]; } } + +#if MINE_PROFILING + endProfiling(started, "subBytes"); +#endif } /// @@ -301,6 +330,9 @@ void AES::invSubBytes(State* state) /// void AES::shiftRows(State *state) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif // row 1 std::swap(state->at(0)[1], state->at(3)[1]); std::swap(state->at(0)[1], state->at(1)[1]); @@ -314,6 +346,10 @@ void AES::shiftRows(State *state) std::swap(state->at(0)[3], state->at(1)[3]); std::swap(state->at(2)[3], state->at(3)[3]); std::swap(state->at(0)[3], state->at(2)[3]); + +#if MINE_PROFILING + endProfiling(started, "shiftRows"); +#endif } /// @@ -389,6 +425,9 @@ byte AES::multiply(byte x, byte y) /// void AES::mixColumns(State* state) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif for (int col = 0; col < 4; ++col) { Word column = state->at(col); // let's take example from publication, col: [212, 191, 93, 48] @@ -400,6 +439,9 @@ void AES::mixColumns(State* state) state->at(col)[2] ^= xtime(column[2] ^ column[3]) ^ t; state->at(col)[3] ^= xtime(column[3] ^ column[0]) ^ t; } +#if MINE_PROFILING + endProfiling(started, "mixColumns"); +#endif } /// @@ -422,7 +464,7 @@ void AES::invMixColumns(State* state) } } -void AES::initState(State* state, const ByteArray& input) +void AES::initState(State* state, const ByteArray::const_iterator& begin) { // Pad the input if needed // 29/08 removed this check because it's not needed as @@ -435,7 +477,7 @@ void AES::initState(State* state, const ByteArray& input) // assign it to state for processing for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - (*state)[i][j] = input[(kNb * i) + j]; + (*state)[i][j] = begin[(kNb * i) + j]; } } } @@ -444,7 +486,7 @@ ByteArray AES::stateToByteArray(const State *state) { #if MINE_PROFILING - auto start = std::chrono::steady_clock::now(); + auto started = std::chrono::steady_clock::now(); #endif ByteArray result(kBlockSize); int k = 0; @@ -454,9 +496,7 @@ ByteArray AES::stateToByteArray(const State *state) } } #if MINE_PROFILING - auto end = std::chrono::steady_clock::now(); - auto diff = std::chrono::duration_cast(end - start); - std::cout << (diff.count()) << " ms for stateToByteArray" << std::endl; + endProfiling(started, "stateToByteArray"); #endif return result; @@ -491,7 +531,7 @@ ByteArray* AES::xorWithIter(ByteArray* input, const ByteArray::const_iterator& b return input; } -ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end, const Key* key, const KeySchedule* keySchedule) +ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule) { if (key == nullptr || keySchedule == nullptr) { @@ -499,19 +539,19 @@ ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const ByteArray } State state; - initState(&state, input); + initState(&state, begin); uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; int round = 0; -#if MINE_PROFILING - auto start = std::chrono::steady_clock::now(); -#endif - // initial round addRoundKey(&state, keySchedule, round++); + +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif // intermediate round while (round < kTotalRounds) { subBytes(&state); @@ -520,22 +560,20 @@ ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const ByteArray addRoundKey(&state, keySchedule, round++); } +#if MINE_PROFILING + endProfiling(started, "rawCipher (intermediate rounds)"); +#endif + // final round subBytes(&state); shiftRows(&state); addRoundKey(&state, keySchedule, round++); -#if MINE_PROFILING - auto end = std::chrono::steady_clock::now(); - auto diff = std::chrono::duration_cast(end - start); - std::cout << (diff.count()) << " ms for encryption" << std::endl; -#endif - return stateToByteArray(&state); } -ByteArray AES::rawDecipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule) +ByteArray AES::rawDecipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule) { if (key == nullptr || keySchedule == nullptr) { @@ -543,7 +581,7 @@ ByteArray AES::rawDecipher(const ByteArray& input, const Key* key, const KeySche } State state; - initState(&state, input); + initState(&state, begin); uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; @@ -616,7 +654,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) inputBlock.at(j) = input.at(j + i); } - ByteArray outputBlock = rawCipher(inputBlock, key, &keySchedule); + ByteArray outputBlock = rawCipher(inputBlock.begin() + i, key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); } return result; @@ -646,7 +684,7 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) inputBlock.at(j) = input.at(j + i); } - ByteArray outputBlock = rawDecipher(inputBlock, key, &keySchedule); + ByteArray outputBlock = rawDecipher(inputBlock.begin(), key, &keySchedule); std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } @@ -670,6 +708,8 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) iv = generateRandomBytes(16); } + // ByteArray inputCopy = input; + KeySchedule keySchedule = keyExpansion(key); const std::size_t inputSize = input.size(); @@ -678,7 +718,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) ByteArray::const_iterator nextXorWithEnd = iv.end(); #if MINE_PROFILING - auto start = std::chrono::steady_clock::now(); + auto started = std::chrono::steady_clock::now(); #endif for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); @@ -687,20 +727,17 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { inputBlock.at(j) = input.at(j + i); } - xorWithIter(&inputBlock, nextXorWithBeg, nextXorWithEnd); - ByteArray outputBlock = rawCipher(inputBlock, key, &keySchedule); + ByteArray outputBlock = rawCipher(inputBlock.begin(), key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); nextXorWithBeg = result.end() - kBlockSize; nextXorWithEnd = result.end(); } - #if MINE_PROFILING - auto end = std::chrono::steady_clock::now(); - auto diff = std::chrono::duration_cast(end - start); - std::cout << (diff.count()) << " ms for block encryption" << std::endl; + endProfiling(started, "block encryption"); #endif + return result; } @@ -730,7 +767,7 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) inputBlock.at(j) = input.at(j + i); } - ByteArray outputBlock = rawDecipher(inputBlock, key, &keySchedule); + ByteArray outputBlock = rawDecipher(inputBlock.begin(), key, &keySchedule); xorWith(&outputBlock, &nextXorWith); diff --git a/src/aes.h b/src/aes.h index 75a669c..6f90f78 100644 --- a/src/aes.h +++ b/src/aes.h @@ -282,7 +282,7 @@ class AES { /// \brief Initializes the state with input. This function /// also pads the input if needed (i.e, input is not block of 128-bit) /// - static void initState(State* state, const ByteArray& input); + static void initState(State* state, const ByteArray::const_iterator& begin); /// /// \brief Generates random bytes of length @@ -319,7 +319,7 @@ class AES { /// \note This does not do any key or input validation /// \return 128-bit cipher text /// - static ByteArray rawCipher(const ByteArray::const_iterator& beg, const ByteArray::const_iterator& end, const Key* key, const KeySchedule* keySchedule); + static ByteArray rawCipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule); /// /// \brief Raw decryption function - not for public use @@ -330,7 +330,7 @@ class AES { /// \param key Byte array of key /// \return 128-bit plain text /// - static ByteArray rawDecipher(const ByteArray& input, const Key* key, const KeySchedule* keySchedule); + static ByteArray rawDecipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule); /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array diff --git a/test/aes-test.h b/test/aes-test.h index 405cacb..e17f129 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -441,7 +441,7 @@ TEST(AESTest, RawCipher) AES::KeySchedule keySchedule = AES::keyExpansion(&key); - ByteArray output = AES::rawCipher(input, &key, &keySchedule); + ByteArray output = AES::rawCipher(input.begin(), &key, &keySchedule); ASSERT_EQ(expected, output); } } @@ -511,7 +511,7 @@ TEST(AESTest, RawSimpleCipher) }}; AES::KeySchedule keySchedule = AES::keyExpansion(&key); - ByteArray output = AES::rawCipher(input, &key, &keySchedule); + ByteArray output = AES::rawCipher(input.begin(), &key, &keySchedule); ASSERT_EQ(expected, output); } @@ -542,7 +542,7 @@ TEST(AESTest, RawSimpleDecipher) }}; AES::KeySchedule keySchedule = AES::keyExpansion(&key); - ByteArray output = AES::rawDecipher(input, &key, &keySchedule); + ByteArray output = AES::rawDecipher(input.begin(), &key, &keySchedule); ASSERT_EQ(expected, output); } From fcc7b6974620c435b599246ad39ac218add351ed Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 14:45:10 +1000 Subject: [PATCH 108/148] major speed improvement from 123 to 105 by using different data structure with logn complexity --- package/mine.cc | 158 ++++++++++++++++++++---------------------------- package/mine.h | 39 ++++++++++-- src/aes.cc | 158 ++++++++++++++++++++---------------------------- src/aes.h | 39 ++++++++++-- 4 files changed, 200 insertions(+), 194 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 22a3359..4e2a621 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -213,7 +213,7 @@ void AES::printState(const State* state) { for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - byte b = state->at(j)[i]; + byte b = (*state)[j][i]; std::cout << "0x" << (b < 10 ? "0" : "") << Base16::encode(b) << " "; } std::cout << std::endl; @@ -221,51 +221,26 @@ void AES::printState(const State* state) std::cout << std::endl; } +void AES::rotateWord(Word* w) { + byte t = (*w)[0]; + (*w)[0] = (*w)[1]; + (*w)[1] = (*w)[2]; + (*w)[2] = (*w)[3]; + (*w)[3] = t; +} + +void AES::substituteWord(Word* w) { + for (uint8_t i = 0; i < 4; ++i) { + (*w)[i] = kSBox[(*w)[i]]; + } +} + AES::KeySchedule AES::keyExpansion(const Key* key) { #if MINE_PROFILING auto started = std::chrono::steady_clock::now(); #endif - // rotateWord function is specified in FIPS.197 Sec. 5.2: - // The function RotWord() takes a - // word [a0,a1,a2,a3] as input, performs a cyclic permutation, - // and returns the word [a1,a2,a3,a0]. The - // round constant word array - // - // Our definition: - // We swap the first byte - // to last one causing it to shift to the left - // i.e, - // [a1] [a2] - // [a2] [a3] - // [a3] => [a4] - // [a4] [a1] - // - auto rotateWord = [](Word* w) -> Word* { - byte t = w->at(0); - w->at(0) = w->at(1); - w->at(1) = w->at(2); - w->at(2) = w->at(3); - w->at(3) = t; - return w; - }; - - // this function is also specified in FIPS.197 Sec. 5.2: - // SubWord() is a function that takes a four-byte - // input word and applies the S-box - // to each of the four bytes to produce an output word. - // - // Out definition: - // It's a simple substition with kSbox for corresponding byte - // index - // - auto substituteWord = [](Word* w) -> Word* { - for (uint8_t i = 0; i < 4; ++i) { - w->at(i) = kSBox[w->at(i)]; - } - return w; - }; std::size_t keySize = key->size(); @@ -276,7 +251,7 @@ AES::KeySchedule AES::keyExpansion(const Key* key) uint8_t Nk = kKeyParams.at(keySize)[0], Nr = kKeyParams.at(keySize)[1]; - KeySchedule words(kNb * (Nr+1)); + KeySchedule words;//(kNb * (Nr+1)); uint8_t i = 0; // copy main key as is for the first round @@ -302,7 +277,6 @@ AES::KeySchedule AES::keyExpansion(const Key* key) substituteWord(&temp); } - // xor previous column of new key with corresponding column of // previous round key Word correspondingWord = words[i - Nk]; @@ -339,14 +313,14 @@ AES::KeySchedule AES::keyExpansion(const Key* key) /// | 0c^cc 9d^9e 8d^15 fa^dd | /// [ fe^fe ef^ea cc^02 b2^dc ] /// -void AES::addRoundKey(State* state, const KeySchedule* keySchedule, int round) +void AES::addRoundKey(State* state, KeySchedule* keySchedule, int round) { #if MINE_PROFILING auto started = std::chrono::steady_clock::now(); #endif for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - state->at(i)[j] ^= keySchedule->at((round * kNb) + i)[j]; + (*state)[i][j] ^= (*keySchedule)[(round * kNb) + i][j]; } } #if MINE_PROFILING @@ -366,7 +340,7 @@ void AES::subBytes(State* state) #endif for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - state->at(i)[j] = kSBox[state->at(i)[j]]; + (*state)[i][j] = kSBox[(*state)[i][j]]; } } @@ -382,7 +356,7 @@ void AES::invSubBytes(State* state) { for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - state->at(i)[j] = kSBoxInverse[state->at(i)[j]]; + (*state)[i][j] = kSBoxInverse[(*state)[i][j]]; } } } @@ -418,18 +392,18 @@ void AES::shiftRows(State *state) auto started = std::chrono::steady_clock::now(); #endif // row 1 - std::swap(state->at(0)[1], state->at(3)[1]); - std::swap(state->at(0)[1], state->at(1)[1]); - std::swap(state->at(1)[1], state->at(2)[1]); + std::swap((*state)[0][1], (*state)[3][1]); + std::swap((*state)[0][1], (*state)[1][1]); + std::swap((*state)[1][1], (*state)[2][1]); // row 2 - std::swap(state->at(0)[2], state->at(2)[2]); - std::swap(state->at(1)[2], state->at(3)[2]); + std::swap((*state)[0][2], (*state)[2][2]); + std::swap((*state)[1][2], (*state)[3][2]); // row 3 - std::swap(state->at(0)[3], state->at(1)[3]); - std::swap(state->at(2)[3], state->at(3)[3]); - std::swap(state->at(0)[3], state->at(2)[3]); + std::swap((*state)[0][3], (*state)[1][3]); + std::swap((*state)[2][3], (*state)[3][3]); + std::swap((*state)[0][3], (*state)[2][3]); #if MINE_PROFILING endProfiling(started, "shiftRows"); @@ -463,18 +437,18 @@ void AES::shiftRows(State *state) void AES::invShiftRows(State *state) { // row 1 - std::swap(state->at(0)[1], state->at(1)[1]); - std::swap(state->at(0)[1], state->at(2)[1]); - std::swap(state->at(0)[1], state->at(3)[1]); + std::swap((*state)[0][1], (*state)[1][1]); + std::swap((*state)[0][1], (*state)[2][1]); + std::swap((*state)[0][1], (*state)[3][1]); // row 2 - std::swap(state->at(0)[2], state->at(2)[2]); - std::swap(state->at(1)[2], state->at(3)[2]); + std::swap((*state)[0][2], (*state)[2][2]); + std::swap((*state)[1][2], (*state)[3][2]); // row 3 - std::swap(state->at(0)[3], state->at(3)[3]); - std::swap(state->at(0)[3], state->at(2)[3]); - std::swap(state->at(0)[3], state->at(1)[3]); + std::swap((*state)[0][3], (*state)[3][3]); + std::swap((*state)[0][3], (*state)[2][3]); + std::swap((*state)[0][3], (*state)[1][3]); } /// @@ -513,15 +487,15 @@ void AES::mixColumns(State* state) auto started = std::chrono::steady_clock::now(); #endif for (int col = 0; col < 4; ++col) { - Word column = state->at(col); + Word column = (*state)[col]; // let's take example from publication, col: [212, 191, 93, 48] // t == 6 byte t = column[0] ^ column[1] ^ column[2] ^ column[3]; // see Sec. 4.2.1 and Sec. 5.1.3 for more details - state->at(col)[0] ^= xtime(column[0] ^ column[1]) ^ t; - state->at(col)[1] ^= xtime(column[1] ^ column[2]) ^ t; - state->at(col)[2] ^= xtime(column[2] ^ column[3]) ^ t; - state->at(col)[3] ^= xtime(column[3] ^ column[0]) ^ t; + (*state)[col][0] ^= xtime(column[0] ^ column[1]) ^ t; + (*state)[col][1] ^= xtime(column[1] ^ column[2]) ^ t; + (*state)[col][2] ^= xtime(column[2] ^ column[3]) ^ t; + (*state)[col][3] ^= xtime(column[3] ^ column[0]) ^ t; } #if MINE_PROFILING endProfiling(started, "mixColumns"); @@ -539,12 +513,12 @@ void AES::mixColumns(State* state) void AES::invMixColumns(State* state) { for (int col = 0; col < 4; ++col) { - Word column = state->at(col); + Word column = (*state)[col]; // see Sec. 4.2.1 and Sec. 5.3.3 for more details - state->at(col)[0] = multiply(column[0], 0x0e) ^ multiply(column[1], 0x0b) ^ multiply(column[2], 0x0d) ^ multiply(column[3], 0x09); - state->at(col)[1] = multiply(column[0], 0x09) ^ multiply(column[1], 0x0e) ^ multiply(column[2], 0x0b) ^ multiply(column[3], 0x0d); - state->at(col)[2] = multiply(column[0], 0x0d) ^ multiply(column[1], 0x09) ^ multiply(column[2], 0x0e) ^ multiply(column[3], 0x0b); - state->at(col)[3] = multiply(column[0], 0x0b) ^ multiply(column[1], 0x0d) ^ multiply(column[2], 0x09) ^ multiply(column[3], 0x0e); + (*state)[col][0] = multiply(column[0], 0x0e) ^ multiply(column[1], 0x0b) ^ multiply(column[2], 0x0d) ^ multiply(column[3], 0x09); + (*state)[col][1] = multiply(column[0], 0x09) ^ multiply(column[1], 0x0e) ^ multiply(column[2], 0x0b) ^ multiply(column[3], 0x0d); + (*state)[col][2] = multiply(column[0], 0x0d) ^ multiply(column[1], 0x09) ^ multiply(column[2], 0x0e) ^ multiply(column[3], 0x0b); + (*state)[col][3] = multiply(column[0], 0x0b) ^ multiply(column[1], 0x0d) ^ multiply(column[2], 0x09) ^ multiply(column[3], 0x0e); } } @@ -576,7 +550,7 @@ ByteArray AES::stateToByteArray(const State *state) int k = 0; for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - result[k++] = state->at(i)[j]; + result[k++] = (*state)[i][j]; } } #if MINE_PROFILING @@ -601,21 +575,21 @@ ByteArray AES::generateRandomBytes(const std::size_t len) ByteArray* AES::xorWith(ByteArray* input, const ByteArray* arr) { for (std::size_t i = 0; i < kBlockSize; ++i) { - input->at(i) ^= arr->at(i); + (*input)[i] ^= (*arr)[i]; } return input; } -ByteArray* AES::xorWithIter(ByteArray* input, const ByteArray::const_iterator& beg, const ByteArray::const_iterator& end) +ByteArray* AES::xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end) { int i = 0; - for (auto iter = beg; iter < end; ++iter, ++i) { - input->at(i) ^= *iter; + for (auto iter = begin; iter < end; ++iter, ++i) { + (*input)[i] ^= *iter; } return input; } -ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule) +ByteArray AES::rawCipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) { if (key == nullptr || keySchedule == nullptr) { @@ -623,9 +597,9 @@ ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const Key* key, } State state; - initState(&state, begin); + initState(&state, range); - uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; + const uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; int round = 0; @@ -637,11 +611,11 @@ ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const Key* key, auto started = std::chrono::steady_clock::now(); #endif // intermediate round - while (round < kTotalRounds) { + for (; round < kTotalRounds; ++round) { subBytes(&state); shiftRows(&state); mixColumns(&state); - addRoundKey(&state, keySchedule, round++); + addRoundKey(&state, keySchedule, round); } #if MINE_PROFILING @@ -657,7 +631,7 @@ ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const Key* key, } -ByteArray AES::rawDecipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule) +ByteArray AES::rawDecipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) { if (key == nullptr || keySchedule == nullptr) { @@ -665,9 +639,9 @@ ByteArray AES::rawDecipher(const ByteArray::const_iterator& begin, const Key* ke } State state; - initState(&state, begin); + initState(&state, range); - uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; + const uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; int round = kTotalRounds; @@ -675,10 +649,10 @@ ByteArray AES::rawDecipher(const ByteArray::const_iterator& begin, const Key* ke addRoundKey(&state, keySchedule, round--); // intermediate round - while (round > 0) { + for (; round > 0; --round) { invShiftRows(&state); invSubBytes(&state); - addRoundKey(&state, keySchedule, round--); + addRoundKey(&state, keySchedule, round); invMixColumns(&state); } @@ -735,7 +709,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) // don't use copy_n as we are setting the values for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { - inputBlock.at(j) = input.at(j + i); + inputBlock[j] = input[j + i]; } ByteArray outputBlock = rawCipher(inputBlock.begin() + i, key, &keySchedule); @@ -765,7 +739,7 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) std::size_t j = 0; // don't use copy_n here as we are setting the values for (; j < kBlockSize && inputSize > j + i; ++j) { - inputBlock.at(j) = input.at(j + i); + inputBlock[j] = input[j + i]; } ByteArray outputBlock = rawDecipher(inputBlock.begin(), key, &keySchedule); @@ -809,9 +783,9 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) // don't use copy_n as we are setting the values for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { - inputBlock.at(j) = input.at(j + i); + inputBlock[j] = input[j + i]; } - xorWithIter(&inputBlock, nextXorWithBeg, nextXorWithEnd); + xorWithRange(&inputBlock, nextXorWithBeg, nextXorWithEnd); ByteArray outputBlock = rawCipher(inputBlock.begin(), key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); @@ -848,7 +822,7 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) std::size_t j = 0; // don't use copy_n as we are setting the values for (; j < kBlockSize && inputSize > j + i; ++j) { - inputBlock.at(j) = input.at(j + i); + inputBlock[j] = input[j + i]; } ByteArray outputBlock = rawDecipher(inputBlock.begin(), key, &keySchedule); diff --git a/package/mine.h b/package/mine.h index 27746fa..13d0b0d 100644 --- a/package/mine.h +++ b/package/mine.h @@ -564,7 +564,7 @@ class AES { /// \brief KeySchedule is linear array of 4-byte words /// \ref FIPS.197 Sec 5.2 /// - using KeySchedule = std::unordered_map; + using KeySchedule = std::map; /// /// \brief State as described in FIPS.197 Sec. 3.4 @@ -617,6 +617,35 @@ class AES { /// static const uint8_t kNb = 4; + + /// rotateWord function is specified in FIPS.197 Sec. 5.2: + /// The function RotWord() takes a + /// word [a0,a1,a2,a3] as input, performs a cyclic permutation, + /// and returns the word [a1,a2,a3,a0]. The + /// round constant word array + /// + /// Our definition: + /// We swap the first byte + /// to last one causing it to shift to the left + /// i.e, + /// [a1] [a2] + /// [a2] [a3] + /// [a3] => [a4] + /// [a4] [a1] + /// + static void rotateWord(Word* w); + + /// this function is also specified in FIPS.197 Sec. 5.2: + /// SubWord() is a function that takes a four-byte + /// input word and applies the S-box + /// to each of the four bytes to produce an output word. + /// + /// Out definition: + /// It's a simple substition with kSbox for corresponding byte + /// index + /// + static void substituteWord(Word* w); + /// /// \brief Key expansion function as described in FIPS.197 /// @@ -625,7 +654,7 @@ class AES { /// /// \brief Adds round to the state using specified key schedule /// - static void addRoundKey(State* state, const KeySchedule* keySchedule, int round); + static void addRoundKey(State* state, KeySchedule* keySchedule, int round); /// /// \brief Substitution step for state @@ -715,7 +744,7 @@ class AES { /// /// \brief Exclusive XOR with iter of range size as input /// - static ByteArray* xorWithIter(ByteArray* input, const ByteArray::const_iterator& beg, const ByteArray::const_iterator& end); + static ByteArray* xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end); /// /// \brief Raw encryption function - not for public use @@ -727,7 +756,7 @@ class AES { /// \note This does not do any key or input validation /// \return 128-bit cipher text /// - static ByteArray rawCipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule); + static ByteArray rawCipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); /// /// \brief Raw decryption function - not for public use @@ -738,7 +767,7 @@ class AES { /// \param key Byte array of key /// \return 128-bit plain text /// - static ByteArray rawDecipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule); + static ByteArray rawDecipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array diff --git a/src/aes.cc b/src/aes.cc index f4d4370..303c1a7 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -129,7 +129,7 @@ void AES::printState(const State* state) { for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - byte b = state->at(j)[i]; + byte b = (*state)[j][i]; std::cout << "0x" << (b < 10 ? "0" : "") << Base16::encode(b) << " "; } std::cout << std::endl; @@ -137,51 +137,26 @@ void AES::printState(const State* state) std::cout << std::endl; } +void AES::rotateWord(Word* w) { + byte t = (*w)[0]; + (*w)[0] = (*w)[1]; + (*w)[1] = (*w)[2]; + (*w)[2] = (*w)[3]; + (*w)[3] = t; +} + +void AES::substituteWord(Word* w) { + for (uint8_t i = 0; i < 4; ++i) { + (*w)[i] = kSBox[(*w)[i]]; + } +} + AES::KeySchedule AES::keyExpansion(const Key* key) { #if MINE_PROFILING auto started = std::chrono::steady_clock::now(); #endif - // rotateWord function is specified in FIPS.197 Sec. 5.2: - // The function RotWord() takes a - // word [a0,a1,a2,a3] as input, performs a cyclic permutation, - // and returns the word [a1,a2,a3,a0]. The - // round constant word array - // - // Our definition: - // We swap the first byte - // to last one causing it to shift to the left - // i.e, - // [a1] [a2] - // [a2] [a3] - // [a3] => [a4] - // [a4] [a1] - // - auto rotateWord = [](Word* w) -> Word* { - byte t = w->at(0); - w->at(0) = w->at(1); - w->at(1) = w->at(2); - w->at(2) = w->at(3); - w->at(3) = t; - return w; - }; - - // this function is also specified in FIPS.197 Sec. 5.2: - // SubWord() is a function that takes a four-byte - // input word and applies the S-box - // to each of the four bytes to produce an output word. - // - // Out definition: - // It's a simple substition with kSbox for corresponding byte - // index - // - auto substituteWord = [](Word* w) -> Word* { - for (uint8_t i = 0; i < 4; ++i) { - w->at(i) = kSBox[w->at(i)]; - } - return w; - }; std::size_t keySize = key->size(); @@ -192,7 +167,7 @@ AES::KeySchedule AES::keyExpansion(const Key* key) uint8_t Nk = kKeyParams.at(keySize)[0], Nr = kKeyParams.at(keySize)[1]; - KeySchedule words(kNb * (Nr+1)); + KeySchedule words;//(kNb * (Nr+1)); uint8_t i = 0; // copy main key as is for the first round @@ -218,7 +193,6 @@ AES::KeySchedule AES::keyExpansion(const Key* key) substituteWord(&temp); } - // xor previous column of new key with corresponding column of // previous round key Word correspondingWord = words[i - Nk]; @@ -255,14 +229,14 @@ AES::KeySchedule AES::keyExpansion(const Key* key) /// | 0c^cc 9d^9e 8d^15 fa^dd | /// [ fe^fe ef^ea cc^02 b2^dc ] /// -void AES::addRoundKey(State* state, const KeySchedule* keySchedule, int round) +void AES::addRoundKey(State* state, KeySchedule* keySchedule, int round) { #if MINE_PROFILING auto started = std::chrono::steady_clock::now(); #endif for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - state->at(i)[j] ^= keySchedule->at((round * kNb) + i)[j]; + (*state)[i][j] ^= (*keySchedule)[(round * kNb) + i][j]; } } #if MINE_PROFILING @@ -282,7 +256,7 @@ void AES::subBytes(State* state) #endif for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - state->at(i)[j] = kSBox[state->at(i)[j]]; + (*state)[i][j] = kSBox[(*state)[i][j]]; } } @@ -298,7 +272,7 @@ void AES::invSubBytes(State* state) { for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - state->at(i)[j] = kSBoxInverse[state->at(i)[j]]; + (*state)[i][j] = kSBoxInverse[(*state)[i][j]]; } } } @@ -334,18 +308,18 @@ void AES::shiftRows(State *state) auto started = std::chrono::steady_clock::now(); #endif // row 1 - std::swap(state->at(0)[1], state->at(3)[1]); - std::swap(state->at(0)[1], state->at(1)[1]); - std::swap(state->at(1)[1], state->at(2)[1]); + std::swap((*state)[0][1], (*state)[3][1]); + std::swap((*state)[0][1], (*state)[1][1]); + std::swap((*state)[1][1], (*state)[2][1]); // row 2 - std::swap(state->at(0)[2], state->at(2)[2]); - std::swap(state->at(1)[2], state->at(3)[2]); + std::swap((*state)[0][2], (*state)[2][2]); + std::swap((*state)[1][2], (*state)[3][2]); // row 3 - std::swap(state->at(0)[3], state->at(1)[3]); - std::swap(state->at(2)[3], state->at(3)[3]); - std::swap(state->at(0)[3], state->at(2)[3]); + std::swap((*state)[0][3], (*state)[1][3]); + std::swap((*state)[2][3], (*state)[3][3]); + std::swap((*state)[0][3], (*state)[2][3]); #if MINE_PROFILING endProfiling(started, "shiftRows"); @@ -379,18 +353,18 @@ void AES::shiftRows(State *state) void AES::invShiftRows(State *state) { // row 1 - std::swap(state->at(0)[1], state->at(1)[1]); - std::swap(state->at(0)[1], state->at(2)[1]); - std::swap(state->at(0)[1], state->at(3)[1]); + std::swap((*state)[0][1], (*state)[1][1]); + std::swap((*state)[0][1], (*state)[2][1]); + std::swap((*state)[0][1], (*state)[3][1]); // row 2 - std::swap(state->at(0)[2], state->at(2)[2]); - std::swap(state->at(1)[2], state->at(3)[2]); + std::swap((*state)[0][2], (*state)[2][2]); + std::swap((*state)[1][2], (*state)[3][2]); // row 3 - std::swap(state->at(0)[3], state->at(3)[3]); - std::swap(state->at(0)[3], state->at(2)[3]); - std::swap(state->at(0)[3], state->at(1)[3]); + std::swap((*state)[0][3], (*state)[3][3]); + std::swap((*state)[0][3], (*state)[2][3]); + std::swap((*state)[0][3], (*state)[1][3]); } /// @@ -429,15 +403,15 @@ void AES::mixColumns(State* state) auto started = std::chrono::steady_clock::now(); #endif for (int col = 0; col < 4; ++col) { - Word column = state->at(col); + Word column = (*state)[col]; // let's take example from publication, col: [212, 191, 93, 48] // t == 6 byte t = column[0] ^ column[1] ^ column[2] ^ column[3]; // see Sec. 4.2.1 and Sec. 5.1.3 for more details - state->at(col)[0] ^= xtime(column[0] ^ column[1]) ^ t; - state->at(col)[1] ^= xtime(column[1] ^ column[2]) ^ t; - state->at(col)[2] ^= xtime(column[2] ^ column[3]) ^ t; - state->at(col)[3] ^= xtime(column[3] ^ column[0]) ^ t; + (*state)[col][0] ^= xtime(column[0] ^ column[1]) ^ t; + (*state)[col][1] ^= xtime(column[1] ^ column[2]) ^ t; + (*state)[col][2] ^= xtime(column[2] ^ column[3]) ^ t; + (*state)[col][3] ^= xtime(column[3] ^ column[0]) ^ t; } #if MINE_PROFILING endProfiling(started, "mixColumns"); @@ -455,12 +429,12 @@ void AES::mixColumns(State* state) void AES::invMixColumns(State* state) { for (int col = 0; col < 4; ++col) { - Word column = state->at(col); + Word column = (*state)[col]; // see Sec. 4.2.1 and Sec. 5.3.3 for more details - state->at(col)[0] = multiply(column[0], 0x0e) ^ multiply(column[1], 0x0b) ^ multiply(column[2], 0x0d) ^ multiply(column[3], 0x09); - state->at(col)[1] = multiply(column[0], 0x09) ^ multiply(column[1], 0x0e) ^ multiply(column[2], 0x0b) ^ multiply(column[3], 0x0d); - state->at(col)[2] = multiply(column[0], 0x0d) ^ multiply(column[1], 0x09) ^ multiply(column[2], 0x0e) ^ multiply(column[3], 0x0b); - state->at(col)[3] = multiply(column[0], 0x0b) ^ multiply(column[1], 0x0d) ^ multiply(column[2], 0x09) ^ multiply(column[3], 0x0e); + (*state)[col][0] = multiply(column[0], 0x0e) ^ multiply(column[1], 0x0b) ^ multiply(column[2], 0x0d) ^ multiply(column[3], 0x09); + (*state)[col][1] = multiply(column[0], 0x09) ^ multiply(column[1], 0x0e) ^ multiply(column[2], 0x0b) ^ multiply(column[3], 0x0d); + (*state)[col][2] = multiply(column[0], 0x0d) ^ multiply(column[1], 0x09) ^ multiply(column[2], 0x0e) ^ multiply(column[3], 0x0b); + (*state)[col][3] = multiply(column[0], 0x0b) ^ multiply(column[1], 0x0d) ^ multiply(column[2], 0x09) ^ multiply(column[3], 0x0e); } } @@ -492,7 +466,7 @@ ByteArray AES::stateToByteArray(const State *state) int k = 0; for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { - result[k++] = state->at(i)[j]; + result[k++] = (*state)[i][j]; } } #if MINE_PROFILING @@ -517,21 +491,21 @@ ByteArray AES::generateRandomBytes(const std::size_t len) ByteArray* AES::xorWith(ByteArray* input, const ByteArray* arr) { for (std::size_t i = 0; i < kBlockSize; ++i) { - input->at(i) ^= arr->at(i); + (*input)[i] ^= (*arr)[i]; } return input; } -ByteArray* AES::xorWithIter(ByteArray* input, const ByteArray::const_iterator& beg, const ByteArray::const_iterator& end) +ByteArray* AES::xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end) { int i = 0; - for (auto iter = beg; iter < end; ++iter, ++i) { - input->at(i) ^= *iter; + for (auto iter = begin; iter < end; ++iter, ++i) { + (*input)[i] ^= *iter; } return input; } -ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule) +ByteArray AES::rawCipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) { if (key == nullptr || keySchedule == nullptr) { @@ -539,9 +513,9 @@ ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const Key* key, } State state; - initState(&state, begin); + initState(&state, range); - uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; + const uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; int round = 0; @@ -553,11 +527,11 @@ ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const Key* key, auto started = std::chrono::steady_clock::now(); #endif // intermediate round - while (round < kTotalRounds) { + for (; round < kTotalRounds; ++round) { subBytes(&state); shiftRows(&state); mixColumns(&state); - addRoundKey(&state, keySchedule, round++); + addRoundKey(&state, keySchedule, round); } #if MINE_PROFILING @@ -573,7 +547,7 @@ ByteArray AES::rawCipher(const ByteArray::const_iterator& begin, const Key* key, } -ByteArray AES::rawDecipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule) +ByteArray AES::rawDecipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) { if (key == nullptr || keySchedule == nullptr) { @@ -581,9 +555,9 @@ ByteArray AES::rawDecipher(const ByteArray::const_iterator& begin, const Key* ke } State state; - initState(&state, begin); + initState(&state, range); - uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; + const uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; int round = kTotalRounds; @@ -591,10 +565,10 @@ ByteArray AES::rawDecipher(const ByteArray::const_iterator& begin, const Key* ke addRoundKey(&state, keySchedule, round--); // intermediate round - while (round > 0) { + for (; round > 0; --round) { invShiftRows(&state); invSubBytes(&state); - addRoundKey(&state, keySchedule, round--); + addRoundKey(&state, keySchedule, round); invMixColumns(&state); } @@ -651,7 +625,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) // don't use copy_n as we are setting the values for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { - inputBlock.at(j) = input.at(j + i); + inputBlock[j] = input[j + i]; } ByteArray outputBlock = rawCipher(inputBlock.begin() + i, key, &keySchedule); @@ -681,7 +655,7 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) std::size_t j = 0; // don't use copy_n here as we are setting the values for (; j < kBlockSize && inputSize > j + i; ++j) { - inputBlock.at(j) = input.at(j + i); + inputBlock[j] = input[j + i]; } ByteArray outputBlock = rawDecipher(inputBlock.begin(), key, &keySchedule); @@ -725,9 +699,9 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) // don't use copy_n as we are setting the values for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { - inputBlock.at(j) = input.at(j + i); + inputBlock[j] = input[j + i]; } - xorWithIter(&inputBlock, nextXorWithBeg, nextXorWithEnd); + xorWithRange(&inputBlock, nextXorWithBeg, nextXorWithEnd); ByteArray outputBlock = rawCipher(inputBlock.begin(), key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); @@ -764,7 +738,7 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) std::size_t j = 0; // don't use copy_n as we are setting the values for (; j < kBlockSize && inputSize > j + i; ++j) { - inputBlock.at(j) = input.at(j + i); + inputBlock[j] = input[j + i]; } ByteArray outputBlock = rawDecipher(inputBlock.begin(), key, &keySchedule); diff --git a/src/aes.h b/src/aes.h index 6f90f78..5ace2fe 100644 --- a/src/aes.h +++ b/src/aes.h @@ -156,7 +156,7 @@ class AES { /// \brief KeySchedule is linear array of 4-byte words /// \ref FIPS.197 Sec 5.2 /// - using KeySchedule = std::unordered_map; + using KeySchedule = std::map; /// /// \brief State as described in FIPS.197 Sec. 3.4 @@ -209,6 +209,35 @@ class AES { /// static const uint8_t kNb = 4; + + /// rotateWord function is specified in FIPS.197 Sec. 5.2: + /// The function RotWord() takes a + /// word [a0,a1,a2,a3] as input, performs a cyclic permutation, + /// and returns the word [a1,a2,a3,a0]. The + /// round constant word array + /// + /// Our definition: + /// We swap the first byte + /// to last one causing it to shift to the left + /// i.e, + /// [a1] [a2] + /// [a2] [a3] + /// [a3] => [a4] + /// [a4] [a1] + /// + static void rotateWord(Word* w); + + /// this function is also specified in FIPS.197 Sec. 5.2: + /// SubWord() is a function that takes a four-byte + /// input word and applies the S-box + /// to each of the four bytes to produce an output word. + /// + /// Out definition: + /// It's a simple substition with kSbox for corresponding byte + /// index + /// + static void substituteWord(Word* w); + /// /// \brief Key expansion function as described in FIPS.197 /// @@ -217,7 +246,7 @@ class AES { /// /// \brief Adds round to the state using specified key schedule /// - static void addRoundKey(State* state, const KeySchedule* keySchedule, int round); + static void addRoundKey(State* state, KeySchedule* keySchedule, int round); /// /// \brief Substitution step for state @@ -307,7 +336,7 @@ class AES { /// /// \brief Exclusive XOR with iter of range size as input /// - static ByteArray* xorWithIter(ByteArray* input, const ByteArray::const_iterator& beg, const ByteArray::const_iterator& end); + static ByteArray* xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end); /// /// \brief Raw encryption function - not for public use @@ -319,7 +348,7 @@ class AES { /// \note This does not do any key or input validation /// \return 128-bit cipher text /// - static ByteArray rawCipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule); + static ByteArray rawCipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); /// /// \brief Raw decryption function - not for public use @@ -330,7 +359,7 @@ class AES { /// \param key Byte array of key /// \return 128-bit plain text /// - static ByteArray rawDecipher(const ByteArray::const_iterator& begin, const Key* key, const KeySchedule* keySchedule); + static ByteArray rawDecipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array From 29ee0e3db5c32367da73975b9a412e90590fabca Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 16:27:57 +1000 Subject: [PATCH 109/148] base64 another fix --- cli/mine.cc | 2 +- package/mine.h | 8 ++++---- src/base64.h | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cli/mine.cc b/cli/mine.cc index 83897bf..45deaeb 100644 --- a/cli/mine.cc +++ b/cli/mine.cc @@ -79,7 +79,7 @@ void encryptAES(std::string& data, const std::string& key, std::string& iv) void decryptAES(std::string& data, const std::string& key, std::string& iv) { TRY - std::cout << AES::decipher(data, key, iv); + std::cout << AES::decipher(data, key, iv, AES::Encoding::Base64); CATCH } diff --git a/package/mine.h b/package/mine.h index 13d0b0d..7945309 100644 --- a/package/mine.h +++ b/package/mine.h @@ -341,8 +341,8 @@ class Base64 { for (auto it = begin; it < end; it += 4) { try { int b0 = kDecodeMap.at(static_cast(*it & 0xff)); - if (b0 == kPadding || b0 == '\0') { - throw std::invalid_argument("Invalid base64 encoding. No data available"); + if (b0 == kPadding/* || b0 == '\0'*/) { + throw std::invalid_argument("No data available"); } int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); @@ -375,8 +375,8 @@ class Base64 { } } } - } catch (const std::exception&) { - throw std::invalid_argument("Invalid base64 character"); + } catch (const std::exception& e) { + throw std::invalid_argument(std::string("Invalid base64 encoding: " + std::string(e.what()))); } } return ss.str(); diff --git a/src/base64.h b/src/base64.h index 3e59a51..d4cb940 100644 --- a/src/base64.h +++ b/src/base64.h @@ -195,8 +195,8 @@ class Base64 { for (auto it = begin; it < end; it += 4) { try { int b0 = kDecodeMap.at(static_cast(*it & 0xff)); - if (b0 == kPadding || b0 == '\0') { - throw std::invalid_argument("Invalid base64 encoding. No data available"); + if (b0 == kPadding/* || b0 == '\0'*/) { + throw std::invalid_argument("No data available"); } int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); @@ -229,8 +229,8 @@ class Base64 { } } } - } catch (const std::exception&) { - throw std::invalid_argument("Invalid base64 character"); + } catch (const std::exception& e) { + throw std::invalid_argument(std::string("Invalid base64 encoding: " + std::string(e.what()))); } } return ss.str(); From 8346f60839faf5aec1868e8d6a83bb3873f0ba71 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 16:30:38 +1000 Subject: [PATCH 110/148] refactor cipher and decipher func --- cli/mine.cc | 4 ++-- package/mine.cc | 36 ++++++++++++++++++------------------ package/mine.h | 20 ++++++++++---------- src/aes.cc | 36 ++++++++++++++++++------------------ src/aes.h | 20 ++++++++++---------- test/aes-test.h | 32 ++++++++++++++++---------------- 6 files changed, 74 insertions(+), 74 deletions(-) diff --git a/cli/mine.cc b/cli/mine.cc index 45deaeb..efcd980 100644 --- a/cli/mine.cc +++ b/cli/mine.cc @@ -68,7 +68,7 @@ void encryptAES(std::string& data, const std::string& key, std::string& iv) { TRY bool newIv = iv.empty(); - std::cout << AES::cipher(data, key, iv, AES::Encoding::Raw, AES::Encoding::Base64); + std::cout << AES::encrypt(data, key, iv, AES::Encoding::Raw, AES::Encoding::Base64); if (newIv) { std::cout << std::endl << "IV: " << iv << std::endl; @@ -79,7 +79,7 @@ void encryptAES(std::string& data, const std::string& key, std::string& iv) void decryptAES(std::string& data, const std::string& key, std::string& iv) { TRY - std::cout << AES::decipher(data, key, iv, AES::Encoding::Base64); + std::cout << AES::decrypt(data, key, iv, AES::Encoding::Base64); CATCH } diff --git a/package/mine.cc b/package/mine.cc index 4e2a621..dec2e76 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -589,7 +589,7 @@ ByteArray* AES::xorWithRange(ByteArray* input, const ByteArray::const_iterator& return input; } -ByteArray AES::rawCipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) +ByteArray AES::encryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) { if (key == nullptr || keySchedule == nullptr) { @@ -631,7 +631,7 @@ ByteArray AES::rawCipher(const ByteArray::const_iterator& range, const Key* key, } -ByteArray AES::rawDecipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) +ByteArray AES::decryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) { if (key == nullptr || keySchedule == nullptr) { @@ -688,7 +688,7 @@ std::string AES::resolveOutputMode(const ByteArray& input, Encoding outputMode) // public -ByteArray AES::cipher(const ByteArray& input, const Key* key) +ByteArray AES::encrypt(const ByteArray& input, const Key* key) { std::size_t keySize = key->size(); @@ -712,13 +712,13 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) inputBlock[j] = input[j + i]; } - ByteArray outputBlock = rawCipher(inputBlock.begin() + i, key, &keySchedule); + ByteArray outputBlock = encryptSingleBlock(inputBlock.begin() + i, key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); } return result; } -ByteArray AES::decipher(const ByteArray& input, const Key* key) +ByteArray AES::decrypt(const ByteArray& input, const Key* key) { std::size_t keySize = key->size(); @@ -742,14 +742,14 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) inputBlock[j] = input[j + i]; } - ByteArray outputBlock = rawDecipher(inputBlock.begin(), key, &keySchedule); + ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } return result; } -ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) +ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv) { std::size_t keySize = key->size(); @@ -787,7 +787,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) } xorWithRange(&inputBlock, nextXorWithBeg, nextXorWithEnd); - ByteArray outputBlock = rawCipher(inputBlock.begin(), key, &keySchedule); + ByteArray outputBlock = encryptSingleBlock(inputBlock.begin(), key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); nextXorWithBeg = result.end() - kBlockSize; nextXorWithEnd = result.end(); @@ -799,7 +799,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) return result; } -ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) +ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) { std::size_t keySize = key->size(); @@ -825,7 +825,7 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) inputBlock[j] = input[j + i]; } - ByteArray outputBlock = rawDecipher(inputBlock.begin(), key, &keySchedule); + ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); xorWith(&outputBlock, &nextXorWith); @@ -842,41 +842,41 @@ static std::string normalizeBase16(std::string enc) return enc; } -std::string AES::cipher(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::encrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray result = cipher(inp, &keyArr); + ByteArray result = encrypt(inp, &keyArr); return resolveOutputMode(result, outputEncoding); } -std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray ivec = Base16::fromString(normalizeBase16(iv)); bool ivecGenerated = iv.empty(); - ByteArray result = cipher(inp, &keyArr, ivec); + ByteArray result = encrypt(inp, &keyArr, ivec); if (ivecGenerated) { iv = Base16::encode(ivec.begin(), ivec.end()); } return resolveOutputMode(result, outputEncoding); } -std::string AES::decipher(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::decrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray result = decipher(inp, &keyArr); + ByteArray result = decrypt(inp, &keyArr); return resolveOutputMode(result, outputEncoding); } -std::string AES::decipher(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray ivec = Base16::fromString(iv); - ByteArray result = decipher(inp, &keyArr, ivec); + ByteArray result = decrypt(inp, &keyArr, ivec); return resolveOutputMode(result, outputEncoding); } diff --git a/package/mine.h b/package/mine.h index 7945309..3350cbd 100644 --- a/package/mine.h +++ b/package/mine.h @@ -481,7 +481,7 @@ class AES { /// \param outputEncoding Type of encoding for cipher /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); + static std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); /// /// \brief Ciphers the input with specified hex key using CBC mode @@ -491,7 +491,7 @@ class AES { /// \param outputEncoding Type of encoding for cipher /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); + static std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); /// /// \brief Deciphers the input with specified hex key @@ -500,7 +500,7 @@ class AES { /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - static std::string decipher(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + static std::string decrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); /// /// \brief Deciphers the input with specified hex key using CBC mode @@ -510,7 +510,7 @@ class AES { /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - static std::string decipher(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + static std::string decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); /// /// \brief Ciphers with ECB-Mode, the input can be as long as user wants @@ -519,7 +519,7 @@ class AES { /// \param iv Initialization vector /// \return Cipher text byte array /// - static ByteArray cipher(const ByteArray& input, const Key* key); + static ByteArray encrypt(const ByteArray& input, const Key* key); /// /// \brief Deciphers with ECB-Mode, the input can be as long as user wants @@ -528,7 +528,7 @@ class AES { /// \param iv Initialization vector /// \return Cipher text byte array /// - static ByteArray decipher(const ByteArray& input, const Key* key); + static ByteArray decrypt(const ByteArray& input, const Key* key); /// /// \brief Ciphers with CBC-Mode, the input can be as long as user wants @@ -537,7 +537,7 @@ class AES { /// \param iv Initialization vector /// \return Cipher text byte array /// - static ByteArray cipher(const ByteArray& input, const Key* key, ByteArray& iv); + static ByteArray encrypt(const ByteArray& input, const Key* key, ByteArray& iv); /// /// \brief Deciphers with CBC-Mode, the input can be as long as user wants @@ -546,7 +546,7 @@ class AES { /// \param iv Initialization vector /// \return Cipher text byte array /// - static ByteArray decipher(const ByteArray& input, const Key* key, ByteArray& iv); + static ByteArray decrypt(const ByteArray& input, const Key* key, ByteArray& iv); /// /// \brief Generates random key of valid length @@ -756,7 +756,7 @@ class AES { /// \note This does not do any key or input validation /// \return 128-bit cipher text /// - static ByteArray rawCipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); + static ByteArray encryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); /// /// \brief Raw decryption function - not for public use @@ -767,7 +767,7 @@ class AES { /// \param key Byte array of key /// \return 128-bit plain text /// - static ByteArray rawDecipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); + static ByteArray decryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array diff --git a/src/aes.cc b/src/aes.cc index 303c1a7..6662c87 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -505,7 +505,7 @@ ByteArray* AES::xorWithRange(ByteArray* input, const ByteArray::const_iterator& return input; } -ByteArray AES::rawCipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) +ByteArray AES::encryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) { if (key == nullptr || keySchedule == nullptr) { @@ -547,7 +547,7 @@ ByteArray AES::rawCipher(const ByteArray::const_iterator& range, const Key* key, } -ByteArray AES::rawDecipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) +ByteArray AES::decryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) { if (key == nullptr || keySchedule == nullptr) { @@ -604,7 +604,7 @@ std::string AES::resolveOutputMode(const ByteArray& input, Encoding outputMode) // public -ByteArray AES::cipher(const ByteArray& input, const Key* key) +ByteArray AES::encrypt(const ByteArray& input, const Key* key) { std::size_t keySize = key->size(); @@ -628,13 +628,13 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key) inputBlock[j] = input[j + i]; } - ByteArray outputBlock = rawCipher(inputBlock.begin() + i, key, &keySchedule); + ByteArray outputBlock = encryptSingleBlock(inputBlock.begin() + i, key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); } return result; } -ByteArray AES::decipher(const ByteArray& input, const Key* key) +ByteArray AES::decrypt(const ByteArray& input, const Key* key) { std::size_t keySize = key->size(); @@ -658,14 +658,14 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key) inputBlock[j] = input[j + i]; } - ByteArray outputBlock = rawDecipher(inputBlock.begin(), key, &keySchedule); + ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } return result; } -ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) +ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv) { std::size_t keySize = key->size(); @@ -703,7 +703,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) } xorWithRange(&inputBlock, nextXorWithBeg, nextXorWithEnd); - ByteArray outputBlock = rawCipher(inputBlock.begin(), key, &keySchedule); + ByteArray outputBlock = encryptSingleBlock(inputBlock.begin(), key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); nextXorWithBeg = result.end() - kBlockSize; nextXorWithEnd = result.end(); @@ -715,7 +715,7 @@ ByteArray AES::cipher(const ByteArray& input, const Key* key, ByteArray& iv) return result; } -ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) +ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) { std::size_t keySize = key->size(); @@ -741,7 +741,7 @@ ByteArray AES::decipher(const ByteArray& input, const Key* key, ByteArray& iv) inputBlock[j] = input[j + i]; } - ByteArray outputBlock = rawDecipher(inputBlock.begin(), key, &keySchedule); + ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); xorWith(&outputBlock, &nextXorWith); @@ -758,41 +758,41 @@ static std::string normalizeBase16(std::string enc) return enc; } -std::string AES::cipher(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::encrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray result = cipher(inp, &keyArr); + ByteArray result = encrypt(inp, &keyArr); return resolveOutputMode(result, outputEncoding); } -std::string AES::cipher(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray ivec = Base16::fromString(normalizeBase16(iv)); bool ivecGenerated = iv.empty(); - ByteArray result = cipher(inp, &keyArr, ivec); + ByteArray result = encrypt(inp, &keyArr, ivec); if (ivecGenerated) { iv = Base16::encode(ivec.begin(), ivec.end()); } return resolveOutputMode(result, outputEncoding); } -std::string AES::decipher(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::decrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray result = decipher(inp, &keyArr); + ByteArray result = decrypt(inp, &keyArr); return resolveOutputMode(result, outputEncoding); } -std::string AES::decipher(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { Key keyArr = Base16::fromString(normalizeBase16(key)); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray ivec = Base16::fromString(iv); - ByteArray result = decipher(inp, &keyArr, ivec); + ByteArray result = decrypt(inp, &keyArr, ivec); return resolveOutputMode(result, outputEncoding); } diff --git a/src/aes.h b/src/aes.h index 5ace2fe..9e5ee15 100644 --- a/src/aes.h +++ b/src/aes.h @@ -73,7 +73,7 @@ class AES { /// \param outputEncoding Type of encoding for cipher /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); + static std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); /// /// \brief Ciphers the input with specified hex key using CBC mode @@ -83,7 +83,7 @@ class AES { /// \param outputEncoding Type of encoding for cipher /// \return Base16 encoded cipher /// - static std::string cipher(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); + static std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); /// /// \brief Deciphers the input with specified hex key @@ -92,7 +92,7 @@ class AES { /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - static std::string decipher(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + static std::string decrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); /// /// \brief Deciphers the input with specified hex key using CBC mode @@ -102,7 +102,7 @@ class AES { /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - static std::string decipher(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + static std::string decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); /// /// \brief Ciphers with ECB-Mode, the input can be as long as user wants @@ -111,7 +111,7 @@ class AES { /// \param iv Initialization vector /// \return Cipher text byte array /// - static ByteArray cipher(const ByteArray& input, const Key* key); + static ByteArray encrypt(const ByteArray& input, const Key* key); /// /// \brief Deciphers with ECB-Mode, the input can be as long as user wants @@ -120,7 +120,7 @@ class AES { /// \param iv Initialization vector /// \return Cipher text byte array /// - static ByteArray decipher(const ByteArray& input, const Key* key); + static ByteArray decrypt(const ByteArray& input, const Key* key); /// /// \brief Ciphers with CBC-Mode, the input can be as long as user wants @@ -129,7 +129,7 @@ class AES { /// \param iv Initialization vector /// \return Cipher text byte array /// - static ByteArray cipher(const ByteArray& input, const Key* key, ByteArray& iv); + static ByteArray encrypt(const ByteArray& input, const Key* key, ByteArray& iv); /// /// \brief Deciphers with CBC-Mode, the input can be as long as user wants @@ -138,7 +138,7 @@ class AES { /// \param iv Initialization vector /// \return Cipher text byte array /// - static ByteArray decipher(const ByteArray& input, const Key* key, ByteArray& iv); + static ByteArray decrypt(const ByteArray& input, const Key* key, ByteArray& iv); /// /// \brief Generates random key of valid length @@ -348,7 +348,7 @@ class AES { /// \note This does not do any key or input validation /// \return 128-bit cipher text /// - static ByteArray rawCipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); + static ByteArray encryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); /// /// \brief Raw decryption function - not for public use @@ -359,7 +359,7 @@ class AES { /// \param key Byte array of key /// \return 128-bit plain text /// - static ByteArray rawDecipher(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); + static ByteArray decryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array diff --git a/test/aes-test.h b/test/aes-test.h index e17f129..557aab9 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -441,7 +441,7 @@ TEST(AESTest, RawCipher) AES::KeySchedule keySchedule = AES::keyExpansion(&key); - ByteArray output = AES::rawCipher(input.begin(), &key, &keySchedule); + ByteArray output = AES::encryptSingleBlock(input.begin(), &key, &keySchedule); ASSERT_EQ(expected, output); } } @@ -451,7 +451,7 @@ TEST(AESTest, RawCipherDirect) for (auto& item : RawCipherData) { std::string expected = PARAM(2); - std::string output = AES::cipher(PARAM(0), PARAM(1), AES::Encoding::Base16); + std::string output = AES::encrypt(PARAM(0), PARAM(1), AES::Encoding::Base16); // case insensitive comparison because hex can be upper or lower case ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } @@ -466,7 +466,7 @@ TEST(AESTest, RawCipherPlain) { for (auto& item : RawCipherPlainInputData) { std::string expected = PARAM(2); - std::string output = AES::cipher(PARAM(0), PARAM(1), AES::Encoding::Raw); + std::string output = AES::encrypt(PARAM(0), PARAM(1), AES::Encoding::Raw); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -480,7 +480,7 @@ TEST(AESTest, RawCipherBase64) { for (auto& item : RawCipherBase64InputData) { std::string expected = PARAM(2); - std::string output = AES::cipher(PARAM(0), PARAM(1), AES::Encoding::Base64); + std::string output = AES::encrypt(PARAM(0), PARAM(1), AES::Encoding::Base64); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -511,7 +511,7 @@ TEST(AESTest, RawSimpleCipher) }}; AES::KeySchedule keySchedule = AES::keyExpansion(&key); - ByteArray output = AES::rawCipher(input.begin(), &key, &keySchedule); + ByteArray output = AES::encryptSingleBlock(input.begin(), &key, &keySchedule); ASSERT_EQ(expected, output); } @@ -542,7 +542,7 @@ TEST(AESTest, RawSimpleDecipher) }}; AES::KeySchedule keySchedule = AES::keyExpansion(&key); - ByteArray output = AES::rawDecipher(input.begin(), &key, &keySchedule); + ByteArray output = AES::decryptSingleBlock(input.begin(), &key, &keySchedule); ASSERT_EQ(expected, output); } @@ -617,10 +617,10 @@ TEST(AESTest, CbcCipher) for (auto& item : CbcCipherTestData) { ByteArray expected = PARAM(1); ByteArray input = Base16::fromString(Base16::encode(PARAM(0))); - ByteArray output = AES::cipher(input, &key, iv); + ByteArray output = AES::encrypt(input, &key, iv); ASSERT_EQ(expected, output); - ByteArray dec = AES::decipher(output, &key, iv); + ByteArray dec = AES::decrypt(output, &key, iv); int f = 0; for (auto i = input.begin(); i < input.end(); ++i, ++f) { ASSERT_EQ(*i, dec[f]); @@ -633,7 +633,7 @@ TEST(AESTest, CbcCipher) std::string input = PARAM(0); std::string k = Base16::encode(Base16::toRawString(key)); std::string initVec = Base16::encode(Base16::toRawString(iv)); - std::string output = AES::cipher(input, k, initVec, + std::string output = AES::encrypt(input, k, initVec, AES::Encoding::Raw, AES::Encoding::Base16); ASSERT_STREQ(expected.c_str(), output.c_str()); @@ -664,7 +664,7 @@ TEST(AESTest, HexStringDecipher) { for (auto& item : RawDecipherData) { std::string expected = PARAM(0); - std::string output = AES::decipher(PARAM(2), PARAM(1), AES::Encoding::Base16, AES::Encoding::Base16); + std::string output = AES::decrypt(PARAM(2), PARAM(1), AES::Encoding::Base16, AES::Encoding::Base16); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -674,7 +674,7 @@ TEST(AESTest, Base64StringDecipher) // input mode = base16, // output mode = base64 std::string expected = "dGhpcyBpcyB0ZXN0Li4uLg=="; // base64("this is test....") - std::string output = AES::decipher("b92daaae6e57773b10653703af12716f", + std::string output = AES::decrypt("b92daaae6e57773b10653703af12716f", "000102030405060708090a0b0c0d0e0f", AES::Encoding::Base16, AES::Encoding::Base64); @@ -684,7 +684,7 @@ TEST(AESTest, Base64StringDecipher) TEST(AESTest, Base64StringInputDecipher) { std::string expected = "this is test.."; - std::string output = AES::decipher("Z0BiQ8NcwknqzbGrWBjXqw==", + std::string output = AES::decrypt("Z0BiQ8NcwknqzbGrWBjXqw==", "000102030405060708090a0b0c0d0e0f", AES::Encoding::Base64, AES::Encoding::Raw); @@ -696,7 +696,7 @@ TEST(AESTest, EcbDecipher) const std::string key = "F1EF6477CC39E65DE106C33BB0EC651386CD0932A9DE491CF960BC3EB79EBE78"; const std::string cipherHex = "b939427f4231593f5cbf73449439a847726b1898b03db028a6f0824108678f78"; const std::string expected = "this is slightly longer"; - std::string output = AES::decipher(cipherHex, key); + std::string output = AES::decrypt(cipherHex, key); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } @@ -774,7 +774,7 @@ TEST(AESTest, CbcDecipher) std::string input = Base16::toRawString(PARAM(1)); std::string k = Base16::encode(Base16::toRawString(key)); std::string initVec = Base16::encode(Base16::toRawString(iv)); - std::string output = AES::decipher(input, k, initVec, + std::string output = AES::decrypt(input, k, initVec, AES::Encoding::Raw, AES::Encoding::Raw); ASSERT_STREQ(expected.c_str(), output.c_str()); @@ -787,7 +787,7 @@ TEST(AESTest, RealDataIssuesTest) // this is real data from residue logging server (development) // const std::string expected = R"({"_t":1503928197,"logger_id":"default","access_code":"default"})"; - std::string output = AES::decipher("EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ==", + std::string output = AES::decrypt("EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ==", "CBD437FA37772C66051A47D72367B38E", "a14c54563269e9e368f56b325f04ff00", AES::Encoding::Base64, @@ -798,7 +798,7 @@ TEST(AESTest, RealDataIssuesTest) //ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); - output = AES::decipher("12D62BE49168FFB92A616C68A0CBD4D8327EBA936150C0E28BD5FA2041D8C6F50D5D2546937E086A44F91FB19BCB32F9FC920C3004022E7514F36E11237CA681", + output = AES::decrypt("12D62BE49168FFB92A616C68A0CBD4D8327EBA936150C0E28BD5FA2041D8C6F50D5D2546937E086A44F91FB19BCB32F9FC920C3004022E7514F36E11237CA681", "CBD437FA37772C66051A47D72367B38E", "a14c54563269e9e368f56b325f04ff00", AES::Encoding::Base16, From 62bebf808c622d62f27d74a1e2f360c4ebf284e0 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 16:45:26 +1000 Subject: [PATCH 111/148] base64 whitespace handling while decoding --- package/mine.h | 25 +++++++++++++++++++++---- src/base64.h | 25 +++++++++++++++++++++---- test/base64-test.h | 17 +++++++++++++++-- 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/package/mine.h b/package/mine.h index 3350cbd..de88807 100644 --- a/package/mine.h +++ b/package/mine.h @@ -314,9 +314,11 @@ class Base64 { /// static std::string decode(const std::string& e) { - if (e.size() % 4 != 0) { - throw std::invalid_argument("Invalid base64 encoding. Padding is required"); - } + //if (e.size() % 4 != 0) { + // we disable it for 76 character line-break format + // https://tools.ietf.org/html/rfc4648#section-3.1 + // throw std::invalid_argument("Invalid base64 encoding. Padding is required"); + //} return decode(e.begin(), e.end()); } @@ -336,16 +338,31 @@ class Base64 { // result indices 24 22 9 35 // - const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); + const int kPadding = 64; // kDecodeMap.at(static_cast(kPaddingChar)); std::stringstream ss; for (auto it = begin; it < end; it += 4) { try { + while (iswspace(*it) && it < end) { + ++it; + } int b0 = kDecodeMap.at(static_cast(*it & 0xff)); if (b0 == kPadding/* || b0 == '\0'*/) { throw std::invalid_argument("No data available"); } + + while (iswspace(*(it + 1)) && it < end) { + ++it; + } int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); + + while (iswspace(*(it + 2)) && it < end) { + ++it; + } int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); + + while (iswspace(*(it + 3)) && it < end) { + ++it; + } int b3 = kDecodeMap.at(static_cast(*(it + 3) & 0xff)); ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 diff --git a/src/base64.h b/src/base64.h index d4cb940..f326d26 100644 --- a/src/base64.h +++ b/src/base64.h @@ -168,9 +168,11 @@ class Base64 { /// static std::string decode(const std::string& e) { - if (e.size() % 4 != 0) { - throw std::invalid_argument("Invalid base64 encoding. Padding is required"); - } + //if (e.size() % 4 != 0) { + // we disable it for 76 character line-break format + // https://tools.ietf.org/html/rfc4648#section-3.1 + // throw std::invalid_argument("Invalid base64 encoding. Padding is required"); + //} return decode(e.begin(), e.end()); } @@ -190,16 +192,31 @@ class Base64 { // result indices 24 22 9 35 // - const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); + const int kPadding = 64; // kDecodeMap.at(static_cast(kPaddingChar)); std::stringstream ss; for (auto it = begin; it < end; it += 4) { try { + while (iswspace(*it) && it < end) { + ++it; + } int b0 = kDecodeMap.at(static_cast(*it & 0xff)); if (b0 == kPadding/* || b0 == '\0'*/) { throw std::invalid_argument("No data available"); } + + while (iswspace(*(it + 1)) && it < end) { + ++it; + } int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); + + while (iswspace(*(it + 2)) && it < end) { + ++it; + } int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); + + while (iswspace(*(it + 3)) && it < end) { + ++it; + } int b3 = kDecodeMap.at(static_cast(*(it + 3) & 0xff)); ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 diff --git a/test/base64-test.h b/test/base64-test.h index 6bdb1d6..4fbce20 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -32,6 +32,10 @@ static TestData Base64TestData = { TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), }; +static TestData Base64OnlyDecodingTestData = { + TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIF\nFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), +}; + static TestData Base64WStringTestData = { // examples from https://en.wikipedia.org/wiki/Base64#Output_padding TestCase("YWJjZA==", L"abcd"), @@ -54,10 +58,10 @@ static TestData Base64WStringTestData = { }; static TestData InvalidBase64EncodingData = { -/* TestCase("YWJj,ZA=="), + TestCase("YWJj,ZA=="), TestCase("YWJj,A=="), TestCase(",,,,"), - TestCase("===="),*/ + TestCase("===="), }; static TestData IsBase64Data = { @@ -83,6 +87,15 @@ TEST(Base64Test, Encode) } +TEST(Base64Test, OnlyDecoding) +{ + for (const auto& item : Base64OnlyDecodingTestData) { + std::string decoded = Base64::decode(PARAM(0)); + ASSERT_STREQ(PARAM(1).c_str(), decoded.c_str()); + } +} + + TEST(Base64Test, ByteArrayEncode) { for (const auto& item : Base64ByteArrayEncodingTestData) { From 6c2fd5dcfac35500883c9115f5f48c2385291c15 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 29 Aug 2017 17:12:46 +1000 Subject: [PATCH 112/148] lineant base64 --- package/mine.h | 12 ++++++++++-- src/base64.h | 12 ++++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/package/mine.h b/package/mine.h index de88807..a5d7f77 100644 --- a/package/mine.h +++ b/package/mine.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -340,7 +341,8 @@ class Base64 { const int kPadding = 64; // kDecodeMap.at(static_cast(kPaddingChar)); std::stringstream ss; - for (auto it = begin; it < end; it += 4) { + int pos = 0; + for (auto it = begin; it < end; it += 4, pos+=4) { try { while (iswspace(*it) && it < end) { ++it; @@ -393,7 +395,13 @@ class Base64 { } } } catch (const std::exception& e) { - throw std::invalid_argument(std::string("Invalid base64 encoding: " + std::string(e.what()))); + std::string ewhat(e.what()); + if (ewhat.find_first_of("unordered_map::at: key not found") == std::string::npos) { + throw std::invalid_argument(std::string("Invalid base64 encoding: " + ewhat)); + } else { + ++it; + continue; + } } } return ss.str(); diff --git a/src/base64.h b/src/base64.h index f326d26..e71cdd6 100644 --- a/src/base64.h +++ b/src/base64.h @@ -31,6 +31,7 @@ # include # include #endif +#include namespace mine { @@ -194,7 +195,8 @@ class Base64 { const int kPadding = 64; // kDecodeMap.at(static_cast(kPaddingChar)); std::stringstream ss; - for (auto it = begin; it < end; it += 4) { + int pos = 0; + for (auto it = begin; it < end; it += 4, pos+=4) { try { while (iswspace(*it) && it < end) { ++it; @@ -247,7 +249,13 @@ class Base64 { } } } catch (const std::exception& e) { - throw std::invalid_argument(std::string("Invalid base64 encoding: " + std::string(e.what()))); + std::string ewhat(e.what()); + if (ewhat.find_first_of("unordered_map::at: key not found") == std::string::npos) { + throw std::invalid_argument(std::string("Invalid base64 encoding: " + ewhat)); + } else { + ++it; + continue; + } } } return ss.str(); From a2a601c9cdbc3c43c2097bc0bd33c3af9fa9e492 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Tue, 29 Aug 2017 18:31:05 +1000 Subject: [PATCH 113/148] b64 fix --- src/base64.h | 42 +++++++++++++++++++++++++----------------- test/base64-test.h | 3 ++- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/base64.h b/src/base64.h index e71cdd6..9a7cf90 100644 --- a/src/base64.h +++ b/src/base64.h @@ -170,7 +170,7 @@ class Base64 { static std::string decode(const std::string& e) { //if (e.size() % 4 != 0) { - // we disable it for 76 character line-break format + // we disable this check for 76 character line-break format (MIME) // https://tools.ietf.org/html/rfc4648#section-3.1 // throw std::invalid_argument("Invalid base64 encoding. Padding is required"); //} @@ -193,37 +193,51 @@ class Base64 { // result indices 24 22 9 35 // - const int kPadding = 64; // kDecodeMap.at(static_cast(kPaddingChar)); + const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); std::stringstream ss; - int pos = 0; - for (auto it = begin; it < end; it += 4, pos+=4) { + for (auto it = begin; it < end; it += 4) { try { - while (iswspace(*it) && it < end) { + while (iswspace(*it)) { ++it; + + if (it >= end) { + goto result; + } } int b0 = kDecodeMap.at(static_cast(*it & 0xff)); if (b0 == kPadding/* || b0 == '\0'*/) { throw std::invalid_argument("No data available"); } - while (iswspace(*(it + 1)) && it < end) { + while (iswspace(*(it + 1))) { ++it; + + if (it >= end) { + goto result; + } } int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); - while (iswspace(*(it + 2)) && it < end) { + while (iswspace(*(it + 2))) { ++it; + + if (it >= end) { + goto result; + } } int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); - while (iswspace(*(it + 3)) && it < end) { + while (iswspace(*(it + 3))) { ++it; + + if (it >= end) { + goto result; + } } int b3 = kDecodeMap.at(static_cast(*(it + 3) & 0xff)); ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 - // std::string s(ss.str()); if (b1 != kPadding/* && b1 != '\0'*/) { if (b2 == kPadding/* || (b2 == '\0' && b3 == '\0')*/) { @@ -237,7 +251,6 @@ class Base64 { b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 if (b3 == kPadding/* || b3 == '\0'*/) { // third bitset is only 4 bits - // std::string s(ss.str()); //ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); // first we clear first 4 bits } else { @@ -249,15 +262,10 @@ class Base64 { } } } catch (const std::exception& e) { - std::string ewhat(e.what()); - if (ewhat.find_first_of("unordered_map::at: key not found") == std::string::npos) { - throw std::invalid_argument(std::string("Invalid base64 encoding: " + ewhat)); - } else { - ++it; - continue; - } + throw std::invalid_argument(std::string("Invalid base64 encoding: " + std::string(e.what()))); } } +result: return ss.str(); } diff --git a/test/base64-test.h b/test/base64-test.h index 4fbce20..e9e1747 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -33,7 +33,8 @@ static TestData Base64TestData = { }; static TestData Base64OnlyDecodingTestData = { - TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIF\nFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"), + //TestCase("\nSGVs\nbG8=", "Hello"), + TestCase("SGVsbG8=\n", "Hello"), }; static TestData Base64WStringTestData = { From 1e7a078c5830a7444d60babf8c3ef41f4068f54b Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Tue, 29 Aug 2017 18:52:54 +1000 Subject: [PATCH 114/148] no copy while decrypt --- package/mine.cc | 7 +++++-- package/mine.h | 42 +++++++++++++++++++++++++----------------- src/aes.cc | 7 +++++-- 3 files changed, 35 insertions(+), 21 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index dec2e76..7d9502f 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -814,6 +814,8 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) ByteArray result; ByteArray nextXorWith = iv; + ByteArray::const_iterator nextXorWithBeg = iv.begin(); + ByteArray::const_iterator nextXorWithEnd = iv.end(); for (std::size_t i = 0; i < inputSize; i += kBlockSize) { @@ -827,9 +829,10 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); - xorWith(&outputBlock, &nextXorWith); + xorWithRange(&outputBlock, nextXorWithBeg, nextXorWithEnd); - nextXorWith = inputBlock; + nextXorWithBeg = input.begin() + i; + nextXorWithEnd = input.begin() + i + kBlockSize; std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } diff --git a/package/mine.h b/package/mine.h index a5d7f77..1079604 100644 --- a/package/mine.h +++ b/package/mine.h @@ -316,7 +316,7 @@ class Base64 { static std::string decode(const std::string& e) { //if (e.size() % 4 != 0) { - // we disable it for 76 character line-break format + // we disable this check for 76 character line-break format (MIME) // https://tools.ietf.org/html/rfc4648#section-3.1 // throw std::invalid_argument("Invalid base64 encoding. Padding is required"); //} @@ -339,37 +339,51 @@ class Base64 { // result indices 24 22 9 35 // - const int kPadding = 64; // kDecodeMap.at(static_cast(kPaddingChar)); + const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); std::stringstream ss; - int pos = 0; - for (auto it = begin; it < end; it += 4, pos+=4) { + for (auto it = begin; it < end; it += 4) { try { - while (iswspace(*it) && it < end) { + while (iswspace(*it)) { ++it; + + if (it >= end) { + goto result; + } } int b0 = kDecodeMap.at(static_cast(*it & 0xff)); if (b0 == kPadding/* || b0 == '\0'*/) { throw std::invalid_argument("No data available"); } - while (iswspace(*(it + 1)) && it < end) { + while (iswspace(*(it + 1))) { ++it; + + if (it >= end) { + goto result; + } } int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); - while (iswspace(*(it + 2)) && it < end) { + while (iswspace(*(it + 2))) { ++it; + + if (it >= end) { + goto result; + } } int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); - while (iswspace(*(it + 3)) && it < end) { + while (iswspace(*(it + 3))) { ++it; + + if (it >= end) { + goto result; + } } int b3 = kDecodeMap.at(static_cast(*(it + 3) & 0xff)); ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 - // std::string s(ss.str()); if (b1 != kPadding/* && b1 != '\0'*/) { if (b2 == kPadding/* || (b2 == '\0' && b3 == '\0')*/) { @@ -383,7 +397,6 @@ class Base64 { b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 if (b3 == kPadding/* || b3 == '\0'*/) { // third bitset is only 4 bits - // std::string s(ss.str()); //ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); // first we clear first 4 bits } else { @@ -395,15 +408,10 @@ class Base64 { } } } catch (const std::exception& e) { - std::string ewhat(e.what()); - if (ewhat.find_first_of("unordered_map::at: key not found") == std::string::npos) { - throw std::invalid_argument(std::string("Invalid base64 encoding: " + ewhat)); - } else { - ++it; - continue; - } + throw std::invalid_argument(std::string("Invalid base64 encoding: " + std::string(e.what()))); } } +result: return ss.str(); } diff --git a/src/aes.cc b/src/aes.cc index 6662c87..245f792 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -730,6 +730,8 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) ByteArray result; ByteArray nextXorWith = iv; + ByteArray::const_iterator nextXorWithBeg = iv.begin(); + ByteArray::const_iterator nextXorWithEnd = iv.end(); for (std::size_t i = 0; i < inputSize; i += kBlockSize) { @@ -743,9 +745,10 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); - xorWith(&outputBlock, &nextXorWith); + xorWithRange(&outputBlock, nextXorWithBeg, nextXorWithEnd); - nextXorWith = inputBlock; + nextXorWithBeg = input.begin() + i; + nextXorWithEnd = input.begin() + i + kBlockSize; std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } From 8e054b6c1c56c7908e0a8d3c99bb3c30f3586bb6 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Tue, 29 Aug 2017 19:00:37 +1000 Subject: [PATCH 115/148] less round calc --- package/mine.cc | 30 +++++++++++++++++++++++++++--- src/aes.cc | 30 +++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 7d9502f..d84ae5a 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -318,9 +318,11 @@ void AES::addRoundKey(State* state, KeySchedule* keySchedule, int round) #if MINE_PROFILING auto started = std::chrono::steady_clock::now(); #endif + const int iR = (round * kNb); for (std::size_t i = 0; i < kNb; ++i) { + std::size_t iR2 = iR + i; for (std::size_t j = 0; j < kNb; ++j) { - (*state)[i][j] ^= (*keySchedule)[(round * kNb) + i][j]; + (*state)[i][j] ^= (*keySchedule)[iR2][j]; } } #if MINE_PROFILING @@ -354,11 +356,17 @@ void AES::subBytes(State* state) /// void AES::invSubBytes(State* state) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { (*state)[i][j] = kSBoxInverse[(*state)[i][j]]; } } +#if MINE_PROFILING + endProfiling(started, "invSubBytes"); +#endif } /// @@ -436,6 +444,9 @@ void AES::shiftRows(State *state) /// void AES::invShiftRows(State *state) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif // row 1 std::swap((*state)[0][1], (*state)[1][1]); std::swap((*state)[0][1], (*state)[2][1]); @@ -449,6 +460,9 @@ void AES::invShiftRows(State *state) std::swap((*state)[0][3], (*state)[3][3]); std::swap((*state)[0][3], (*state)[2][3]); std::swap((*state)[0][3], (*state)[1][3]); +#if MINE_PROFILING + endProfiling(started, "intShiftRows"); +#endif } /// @@ -512,6 +526,9 @@ void AES::mixColumns(State* state) /// void AES::invMixColumns(State* state) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif for (int col = 0; col < 4; ++col) { Word column = (*state)[col]; // see Sec. 4.2.1 and Sec. 5.3.3 for more details @@ -520,6 +537,9 @@ void AES::invMixColumns(State* state) (*state)[col][2] = multiply(column[0], 0x0d) ^ multiply(column[1], 0x09) ^ multiply(column[2], 0x0e) ^ multiply(column[3], 0x0b); (*state)[col][3] = multiply(column[0], 0x0b) ^ multiply(column[1], 0x0d) ^ multiply(column[2], 0x09) ^ multiply(column[3], 0x0e); } +#if MINE_PROFILING + endProfiling(started, "invMixColumns"); +#endif } void AES::initState(State* state, const ByteArray::const_iterator& begin) @@ -813,11 +833,12 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) const std::size_t inputSize = input.size(); ByteArray result; - ByteArray nextXorWith = iv; ByteArray::const_iterator nextXorWithBeg = iv.begin(); ByteArray::const_iterator nextXorWithEnd = iv.end(); - +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); @@ -836,6 +857,9 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } +#if MINE_PROFILING + endProfiling(started, "block decryption"); +#endif return result; } diff --git a/src/aes.cc b/src/aes.cc index 245f792..203b5cf 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -234,9 +234,11 @@ void AES::addRoundKey(State* state, KeySchedule* keySchedule, int round) #if MINE_PROFILING auto started = std::chrono::steady_clock::now(); #endif + const int iR = (round * kNb); for (std::size_t i = 0; i < kNb; ++i) { + std::size_t iR2 = iR + i; for (std::size_t j = 0; j < kNb; ++j) { - (*state)[i][j] ^= (*keySchedule)[(round * kNb) + i][j]; + (*state)[i][j] ^= (*keySchedule)[iR2][j]; } } #if MINE_PROFILING @@ -270,11 +272,17 @@ void AES::subBytes(State* state) /// void AES::invSubBytes(State* state) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif for (std::size_t i = 0; i < kNb; ++i) { for (std::size_t j = 0; j < kNb; ++j) { (*state)[i][j] = kSBoxInverse[(*state)[i][j]]; } } +#if MINE_PROFILING + endProfiling(started, "invSubBytes"); +#endif } /// @@ -352,6 +360,9 @@ void AES::shiftRows(State *state) /// void AES::invShiftRows(State *state) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif // row 1 std::swap((*state)[0][1], (*state)[1][1]); std::swap((*state)[0][1], (*state)[2][1]); @@ -365,6 +376,9 @@ void AES::invShiftRows(State *state) std::swap((*state)[0][3], (*state)[3][3]); std::swap((*state)[0][3], (*state)[2][3]); std::swap((*state)[0][3], (*state)[1][3]); +#if MINE_PROFILING + endProfiling(started, "intShiftRows"); +#endif } /// @@ -428,6 +442,9 @@ void AES::mixColumns(State* state) /// void AES::invMixColumns(State* state) { +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif for (int col = 0; col < 4; ++col) { Word column = (*state)[col]; // see Sec. 4.2.1 and Sec. 5.3.3 for more details @@ -436,6 +453,9 @@ void AES::invMixColumns(State* state) (*state)[col][2] = multiply(column[0], 0x0d) ^ multiply(column[1], 0x09) ^ multiply(column[2], 0x0e) ^ multiply(column[3], 0x0b); (*state)[col][3] = multiply(column[0], 0x0b) ^ multiply(column[1], 0x0d) ^ multiply(column[2], 0x09) ^ multiply(column[3], 0x0e); } +#if MINE_PROFILING + endProfiling(started, "invMixColumns"); +#endif } void AES::initState(State* state, const ByteArray::const_iterator& begin) @@ -729,11 +749,12 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) const std::size_t inputSize = input.size(); ByteArray result; - ByteArray nextXorWith = iv; ByteArray::const_iterator nextXorWithBeg = iv.begin(); ByteArray::const_iterator nextXorWithEnd = iv.end(); - +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); @@ -752,6 +773,9 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } +#if MINE_PROFILING + endProfiling(started, "block decryption"); +#endif return result; } From 9879a2aada9f5c044cc1087b61390b3935455e3d Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Tue, 29 Aug 2017 23:41:26 +1000 Subject: [PATCH 116/148] fix padding issues for aes-cbc --- cli/mine.cc | 7 +++++ package/mine.cc | 76 +++++++++++++++++++++++++++++++++---------------- package/mine.h | 14 +++++---- src/aes.cc | 76 +++++++++++++++++++++++++++++++++---------------- src/aes.h | 14 +++++---- test/aes-test.h | 38 ++++++++++++++++++------- 6 files changed, 155 insertions(+), 70 deletions(-) diff --git a/cli/mine.cc b/cli/mine.cc index efcd980..6732f4e 100644 --- a/cli/mine.cc +++ b/cli/mine.cc @@ -56,6 +56,13 @@ void displayUsage() std::cout << std::endl; } + +std::string normalizeBase16(std::string enc) +{ + enc.erase(std::remove_if(enc.begin(), enc.end(), iswspace), enc.end()); + return enc; +} + void displayVersion() { std::cout << "Mine - Minimal cryptography library" << std::endl << "Version: " << MINE_VERSION << std::endl << "https://muflihun.com" << std::endl; diff --git a/package/mine.cc b/package/mine.cc index d84ae5a..89a156f 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -708,7 +708,7 @@ std::string AES::resolveOutputMode(const ByteArray& input, Encoding outputMode) // public -ByteArray AES::encrypt(const ByteArray& input, const Key* key) +ByteArray AES::encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding) { std::size_t keySize = key->size(); @@ -727,11 +727,17 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key) for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); + std::size_t j = 0; // don't use copy_n as we are setting the values - for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { + for (; j < kBlockSize && inputSize > j + i; ++j) { inputBlock[j] = input[j + i]; } + if (pkcs5Padding && j != kBlockSize) { + // PKCS#5 padding + std::fill(inputBlock.begin() + j, inputBlock.end(), j); + } + ByteArray outputBlock = encryptSingleBlock(inputBlock.begin() + i, key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); } @@ -761,7 +767,6 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key) for (; j < kBlockSize && inputSize > j + i; ++j) { inputBlock[j] = input[j + i]; } - ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); @@ -769,7 +774,7 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key) return result; } -ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv) +ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bool pkcs5Padding) { std::size_t keySize = key->size(); @@ -780,7 +785,7 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv) } if (!iv.empty() && iv.size() != 16) { - throw std::invalid_argument("Invalid IV, it should be 128-bit"); + throw std::invalid_argument("Invalid IV, it should be same as block size"); } else if (iv.empty()) { // generate IV iv = generateRandomBytes(16); @@ -801,10 +806,15 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv) for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); + std::size_t j = 0; // don't use copy_n as we are setting the values - for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { + for (; j < kBlockSize && inputSize > j + i; ++j) { inputBlock[j] = input[j + i]; } + if (pkcs5Padding && j != kBlockSize) { + // PKCS#5 padding + std::fill(inputBlock.begin() + j, inputBlock.end(), j); + } xorWithRange(&inputBlock, nextXorWithBeg, nextXorWithEnd); ByteArray outputBlock = encryptSingleBlock(inputBlock.begin(), key, &keySchedule); @@ -829,8 +839,13 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) throw std::invalid_argument("Invalid AES key size"); } - KeySchedule keySchedule = keyExpansion(key); const std::size_t inputSize = input.size(); + + if (inputSize % kBlockSize != 0) { + throw std::invalid_argument("Ciphertext length is not a multiple of block size"); + } + + KeySchedule keySchedule = keyExpansion(key); ByteArray result; ByteArray::const_iterator nextXorWithBeg = iv.begin(); @@ -852,8 +867,27 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) xorWithRange(&outputBlock, nextXorWithBeg, nextXorWithEnd); - nextXorWithBeg = input.begin() + i; - nextXorWithEnd = input.begin() + i + kBlockSize; + if (i + kBlockSize == inputSize) { + // check padding + char lastChar = outputBlock[kBlockSize - 1]; + int c = lastChar & 0xff; + if (c > 0 && c < kBlockSize - 1) { + bool validPadding = true; + for (int chkIdx = c; chkIdx < kBlockSize - 2; ++chkIdx) { + if ((outputBlock[chkIdx] & 0xff) != c) { + // with openssl we found padding + validPadding = false; + break; + } + } + if (validPadding) { + j = c; + } + } + } else { + nextXorWithBeg = input.begin() + i; + nextXorWithEnd = input.begin() + i + kBlockSize; + } std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } @@ -863,27 +897,21 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) return result; } -static std::string normalizeBase16(std::string enc) +std::string AES::encrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) { - enc.erase(std::remove_if(enc.begin(), enc.end(), iswspace), enc.end()); - return enc; -} - -std::string AES::encrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) -{ - Key keyArr = Base16::fromString(normalizeBase16(key)); + Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray result = encrypt(inp, &keyArr); + ByteArray result = encrypt(inp, &keyArr, pkcs5Padding); return resolveOutputMode(result, outputEncoding); } -std::string AES::encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) { - Key keyArr = Base16::fromString(normalizeBase16(key)); + Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray ivec = Base16::fromString(normalizeBase16(iv)); + ByteArray ivec = Base16::fromString(iv); bool ivecGenerated = iv.empty(); - ByteArray result = encrypt(inp, &keyArr, ivec); + ByteArray result = encrypt(inp, &keyArr, ivec, pkcs5Padding); if (ivecGenerated) { iv = Base16::encode(ivec.begin(), ivec.end()); } @@ -892,7 +920,7 @@ std::string AES::encrypt(const std::string& input, const std::string& key, std:: std::string AES::decrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { - Key keyArr = Base16::fromString(normalizeBase16(key)); + Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray result = decrypt(inp, &keyArr); return resolveOutputMode(result, outputEncoding); @@ -900,7 +928,7 @@ std::string AES::decrypt(const std::string& input, const std::string& key, Encod std::string AES::decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { - Key keyArr = Base16::fromString(normalizeBase16(key)); + Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray ivec = Base16::fromString(iv); ByteArray result = decrypt(inp, &keyArr, ivec); diff --git a/package/mine.h b/package/mine.h index 1079604..4e2a040 100644 --- a/package/mine.h +++ b/package/mine.h @@ -512,9 +512,10 @@ class AES { /// \param key Hex key /// \param inputEncoding the type of input. Defaults to Plain /// \param outputEncoding Type of encoding for cipher + /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Base16 encoded cipher /// - static std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); + static std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); /// /// \brief Ciphers the input with specified hex key using CBC mode @@ -522,9 +523,10 @@ class AES { /// \param iv Initialization vector, passed by reference. If empty a random is generated and passed in /// \param inputEncoding the type of input. Defaults to Plain /// \param outputEncoding Type of encoding for cipher + /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Base16 encoded cipher /// - static std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); + static std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); /// /// \brief Deciphers the input with specified hex key @@ -549,16 +551,15 @@ class AES { /// \brief Ciphers with ECB-Mode, the input can be as long as user wants /// \param input Plain input of any length /// \param key Pointer to a valid AES key - /// \param iv Initialization vector + /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Cipher text byte array /// - static ByteArray encrypt(const ByteArray& input, const Key* key); + static ByteArray encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding = true); /// /// \brief Deciphers with ECB-Mode, the input can be as long as user wants /// \param input Plain input of any length /// \param key Pointer to a valid AES key - /// \param iv Initialization vector /// \return Cipher text byte array /// static ByteArray decrypt(const ByteArray& input, const Key* key); @@ -568,9 +569,10 @@ class AES { /// \param input Plain input of any length /// \param key Pointer to a valid AES key /// \param iv Initialization vector + /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Cipher text byte array /// - static ByteArray encrypt(const ByteArray& input, const Key* key, ByteArray& iv); + static ByteArray encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bool pkcs5Padding = true); /// /// \brief Deciphers with CBC-Mode, the input can be as long as user wants diff --git a/src/aes.cc b/src/aes.cc index 203b5cf..04bee94 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -624,7 +624,7 @@ std::string AES::resolveOutputMode(const ByteArray& input, Encoding outputMode) // public -ByteArray AES::encrypt(const ByteArray& input, const Key* key) +ByteArray AES::encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding) { std::size_t keySize = key->size(); @@ -643,11 +643,17 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key) for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); + std::size_t j = 0; // don't use copy_n as we are setting the values - for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { + for (; j < kBlockSize && inputSize > j + i; ++j) { inputBlock[j] = input[j + i]; } + if (pkcs5Padding && j != kBlockSize) { + // PKCS#5 padding + std::fill(inputBlock.begin() + j, inputBlock.end(), j); + } + ByteArray outputBlock = encryptSingleBlock(inputBlock.begin() + i, key, &keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); } @@ -677,7 +683,6 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key) for (; j < kBlockSize && inputSize > j + i; ++j) { inputBlock[j] = input[j + i]; } - ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); @@ -685,7 +690,7 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key) return result; } -ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv) +ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bool pkcs5Padding) { std::size_t keySize = key->size(); @@ -696,7 +701,7 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv) } if (!iv.empty() && iv.size() != 16) { - throw std::invalid_argument("Invalid IV, it should be 128-bit"); + throw std::invalid_argument("Invalid IV, it should be same as block size"); } else if (iv.empty()) { // generate IV iv = generateRandomBytes(16); @@ -717,10 +722,15 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv) for (std::size_t i = 0; i < inputSize; i += kBlockSize) { ByteArray inputBlock(kBlockSize, 0); + std::size_t j = 0; // don't use copy_n as we are setting the values - for (std::size_t j = 0; j < kBlockSize && inputSize > j + i; ++j) { + for (; j < kBlockSize && inputSize > j + i; ++j) { inputBlock[j] = input[j + i]; } + if (pkcs5Padding && j != kBlockSize) { + // PKCS#5 padding + std::fill(inputBlock.begin() + j, inputBlock.end(), j); + } xorWithRange(&inputBlock, nextXorWithBeg, nextXorWithEnd); ByteArray outputBlock = encryptSingleBlock(inputBlock.begin(), key, &keySchedule); @@ -745,8 +755,13 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) throw std::invalid_argument("Invalid AES key size"); } - KeySchedule keySchedule = keyExpansion(key); const std::size_t inputSize = input.size(); + + if (inputSize % kBlockSize != 0) { + throw std::invalid_argument("Ciphertext length is not a multiple of block size"); + } + + KeySchedule keySchedule = keyExpansion(key); ByteArray result; ByteArray::const_iterator nextXorWithBeg = iv.begin(); @@ -768,8 +783,27 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) xorWithRange(&outputBlock, nextXorWithBeg, nextXorWithEnd); - nextXorWithBeg = input.begin() + i; - nextXorWithEnd = input.begin() + i + kBlockSize; + if (i + kBlockSize == inputSize) { + // check padding + char lastChar = outputBlock[kBlockSize - 1]; + int c = lastChar & 0xff; + if (c > 0 && c < kBlockSize - 1) { + bool validPadding = true; + for (int chkIdx = c; chkIdx < kBlockSize - 2; ++chkIdx) { + if ((outputBlock[chkIdx] & 0xff) != c) { + // with openssl we found padding + validPadding = false; + break; + } + } + if (validPadding) { + j = c; + } + } + } else { + nextXorWithBeg = input.begin() + i; + nextXorWithEnd = input.begin() + i + kBlockSize; + } std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } @@ -779,27 +813,21 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) return result; } -static std::string normalizeBase16(std::string enc) +std::string AES::encrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) { - enc.erase(std::remove_if(enc.begin(), enc.end(), iswspace), enc.end()); - return enc; -} - -std::string AES::encrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) -{ - Key keyArr = Base16::fromString(normalizeBase16(key)); + Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray result = encrypt(inp, &keyArr); + ByteArray result = encrypt(inp, &keyArr, pkcs5Padding); return resolveOutputMode(result, outputEncoding); } -std::string AES::encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) { - Key keyArr = Base16::fromString(normalizeBase16(key)); + Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray ivec = Base16::fromString(normalizeBase16(iv)); + ByteArray ivec = Base16::fromString(iv); bool ivecGenerated = iv.empty(); - ByteArray result = encrypt(inp, &keyArr, ivec); + ByteArray result = encrypt(inp, &keyArr, ivec, pkcs5Padding); if (ivecGenerated) { iv = Base16::encode(ivec.begin(), ivec.end()); } @@ -808,7 +836,7 @@ std::string AES::encrypt(const std::string& input, const std::string& key, std:: std::string AES::decrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) { - Key keyArr = Base16::fromString(normalizeBase16(key)); + Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray result = decrypt(inp, &keyArr); return resolveOutputMode(result, outputEncoding); @@ -816,7 +844,7 @@ std::string AES::decrypt(const std::string& input, const std::string& key, Encod std::string AES::decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) { - Key keyArr = Base16::fromString(normalizeBase16(key)); + Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); ByteArray ivec = Base16::fromString(iv); ByteArray result = decrypt(inp, &keyArr, ivec); diff --git a/src/aes.h b/src/aes.h index 9e5ee15..2b2ff37 100644 --- a/src/aes.h +++ b/src/aes.h @@ -71,9 +71,10 @@ class AES { /// \param key Hex key /// \param inputEncoding the type of input. Defaults to Plain /// \param outputEncoding Type of encoding for cipher + /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Base16 encoded cipher /// - static std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); + static std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); /// /// \brief Ciphers the input with specified hex key using CBC mode @@ -81,9 +82,10 @@ class AES { /// \param iv Initialization vector, passed by reference. If empty a random is generated and passed in /// \param inputEncoding the type of input. Defaults to Plain /// \param outputEncoding Type of encoding for cipher + /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Base16 encoded cipher /// - static std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16); + static std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); /// /// \brief Deciphers the input with specified hex key @@ -108,16 +110,15 @@ class AES { /// \brief Ciphers with ECB-Mode, the input can be as long as user wants /// \param input Plain input of any length /// \param key Pointer to a valid AES key - /// \param iv Initialization vector + /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Cipher text byte array /// - static ByteArray encrypt(const ByteArray& input, const Key* key); + static ByteArray encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding = true); /// /// \brief Deciphers with ECB-Mode, the input can be as long as user wants /// \param input Plain input of any length /// \param key Pointer to a valid AES key - /// \param iv Initialization vector /// \return Cipher text byte array /// static ByteArray decrypt(const ByteArray& input, const Key* key); @@ -127,9 +128,10 @@ class AES { /// \param input Plain input of any length /// \param key Pointer to a valid AES key /// \param iv Initialization vector + /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Cipher text byte array /// - static ByteArray encrypt(const ByteArray& input, const Key* key, ByteArray& iv); + static ByteArray encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bool pkcs5Padding = true); /// /// \brief Deciphers with CBC-Mode, the input can be as long as user wants diff --git a/test/aes-test.h b/test/aes-test.h index 557aab9..322b890 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -466,7 +466,7 @@ TEST(AESTest, RawCipherPlain) { for (auto& item : RawCipherPlainInputData) { std::string expected = PARAM(2); - std::string output = AES::encrypt(PARAM(0), PARAM(1), AES::Encoding::Raw); + std::string output = AES::encrypt(PARAM(0), PARAM(1), AES::Encoding::Raw, AES::Encoding::Base16, false); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -480,7 +480,7 @@ TEST(AESTest, RawCipherBase64) { for (auto& item : RawCipherBase64InputData) { std::string expected = PARAM(2); - std::string output = AES::encrypt(PARAM(0), PARAM(1), AES::Encoding::Base64); + std::string output = AES::encrypt(PARAM(0), PARAM(1), AES::Encoding::Base64, AES::Encoding::Base16, false); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -617,7 +617,7 @@ TEST(AESTest, CbcCipher) for (auto& item : CbcCipherTestData) { ByteArray expected = PARAM(1); ByteArray input = Base16::fromString(Base16::encode(PARAM(0))); - ByteArray output = AES::encrypt(input, &key, iv); + ByteArray output = AES::encrypt(input, &key, iv, false); ASSERT_EQ(expected, output); ByteArray dec = AES::decrypt(output, &key, iv); @@ -635,7 +635,7 @@ TEST(AESTest, CbcCipher) std::string initVec = Base16::encode(Base16::toRawString(iv)); std::string output = AES::encrypt(input, k, initVec, AES::Encoding::Raw, - AES::Encoding::Base16); + AES::Encoding::Base16, false); ASSERT_STREQ(expected.c_str(), output.c_str()); } @@ -784,26 +784,44 @@ TEST(AESTest, CbcDecipher) TEST(AESTest, RealDataIssuesTest) { + std::string expected = "WQ73OMIum+OHKGHnAhQKJc/uwM2APneVOH9mBq15bOk="; + std::string iv = "a14c54563269e9e368f56b325f04ff00"; + std::string output = AES::encrypt("test this test this", + "CBD437FA37772C66051A47D72367B38E", + iv, + AES::Encoding::Raw, + AES::Encoding::Base64); + + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + // this is real data from residue logging server (development) // - const std::string expected = R"({"_t":1503928197,"logger_id":"default","access_code":"default"})"; - std::string output = AES::decrypt("EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ==", + expected = R"({"_t":1503928197,"logger_id":"default","access_code":"default"})"; + output = AES::decrypt("EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ==", "CBD437FA37772C66051A47D72367B38E", "a14c54563269e9e368f56b325f04ff00", AES::Encoding::Base64, AES::Encoding::Raw); - // - // this worked => the only issue was padding std::string r = mine::AES::decipher(Ripe::stringToHex(Ripe::base64Decode(raw)), key, iv, mine::AES::ConvertMode::Base16, mine::AES::ConvertMode::Plain); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + + expected = "test this test this"; + output = AES::decrypt("WQ73OMIum+OHKGHnAhQKJc/uwM2APneVOH9mBq15bOk=", + "CBD437FA37772C66051A47D72367B38E", + "a14c54563269e9e368f56b325f04ff00", + AES::Encoding::Base64, + AES::Encoding::Raw); - //ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + + expected = R"({"_t":1503928197,"logger_id":"default","access_code":"default"})"; output = AES::decrypt("12D62BE49168FFB92A616C68A0CBD4D8327EBA936150C0E28BD5FA2041D8C6F50D5D2546937E086A44F91FB19BCB32F9FC920C3004022E7514F36E11237CA681", "CBD437FA37772C66051A47D72367B38E", "a14c54563269e9e368f56b325f04ff00", AES::Encoding::Base16, AES::Encoding::Raw); - //ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } From 9c7852becef2e9ab527a729ae3b720b98a062a72 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Wed, 30 Aug 2017 00:07:28 +1000 Subject: [PATCH 117/148] correct padding --- package/mine.cc | 4 ++-- src/aes.cc | 4 ++-- test/aes-test.h | 13 ++++++++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 89a156f..1bd7209 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -735,7 +735,7 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding if (pkcs5Padding && j != kBlockSize) { // PKCS#5 padding - std::fill(inputBlock.begin() + j, inputBlock.end(), j); + std::fill(inputBlock.begin() + j, inputBlock.end(), kBlockSize - (j % kBlockSize)); } ByteArray outputBlock = encryptSingleBlock(inputBlock.begin() + i, key, &keySchedule); @@ -813,7 +813,7 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bo } if (pkcs5Padding && j != kBlockSize) { // PKCS#5 padding - std::fill(inputBlock.begin() + j, inputBlock.end(), j); + std::fill(inputBlock.begin() + j, inputBlock.end(), kBlockSize - (j % kBlockSize)); } xorWithRange(&inputBlock, nextXorWithBeg, nextXorWithEnd); diff --git a/src/aes.cc b/src/aes.cc index 04bee94..a098cc2 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -651,7 +651,7 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding if (pkcs5Padding && j != kBlockSize) { // PKCS#5 padding - std::fill(inputBlock.begin() + j, inputBlock.end(), j); + std::fill(inputBlock.begin() + j, inputBlock.end(), kBlockSize - (j % kBlockSize)); } ByteArray outputBlock = encryptSingleBlock(inputBlock.begin() + i, key, &keySchedule); @@ -729,7 +729,7 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bo } if (pkcs5Padding && j != kBlockSize) { // PKCS#5 padding - std::fill(inputBlock.begin() + j, inputBlock.end(), j); + std::fill(inputBlock.begin() + j, inputBlock.end(), kBlockSize - (j % kBlockSize)); } xorWithRange(&inputBlock, nextXorWithBeg, nextXorWithEnd); diff --git a/test/aes-test.h b/test/aes-test.h index 322b890..9fb12eb 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -784,7 +784,7 @@ TEST(AESTest, CbcDecipher) TEST(AESTest, RealDataIssuesTest) { - std::string expected = "WQ73OMIum+OHKGHnAhQKJc/uwM2APneVOH9mBq15bOk="; + std::string expected = "WQ73OMIum+OHKGHnAhQKJX1tByfBq4BhSpw2X+SgtjY="; std::string iv = "a14c54563269e9e368f56b325f04ff00"; std::string output = AES::encrypt("test this test this", "CBD437FA37772C66051A47D72367B38E", @@ -794,6 +794,17 @@ TEST(AESTest, RealDataIssuesTest) ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + expected = "EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ=="; + + output = AES::encrypt(R"({"_t":1503928197,"logger_id":"default","access_code":"default"})", + "CBD437FA37772C66051A47D72367B38E", + iv, + AES::Encoding::Raw, + AES::Encoding::Base64); + + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + + // this is real data from residue logging server (development) // expected = R"({"_t":1503928197,"logger_id":"default","access_code":"default"})"; From bd448bb64d6f131fe9291cc92dfec5afb217a172 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Wed, 30 Aug 2017 00:30:01 +1000 Subject: [PATCH 118/148] fix all paddding issues --- src/aes.cc | 40 +++++++++++++++++++++++++--------------- src/aes.h | 5 +++++ test/aes-test.h | 44 +++++++++++++++++++++++++++++++------------- test/main.cc | 2 +- 4 files changed, 62 insertions(+), 29 deletions(-) diff --git a/src/aes.cc b/src/aes.cc index a098cc2..36bcc48 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -622,6 +622,26 @@ std::string AES::resolveOutputMode(const ByteArray& input, Encoding outputMode) return Base64::encode(input.begin(), input.end()); } +std::size_t AES::getPaddingIndex(const ByteArray& byteArr) +{ + char lastChar = byteArr[kBlockSize - 1]; + int c = lastChar & 0xff; + if (c > 0 && c < kBlockSize - 1) { + bool validPadding = true; + for (int chkIdx = kBlockSize - c; chkIdx < kBlockSize; ++chkIdx) { + if ((byteArr[chkIdx] & 0xff) != c) { + // with openssl we found padding + validPadding = false; + break; + } + } + if (validPadding) { + return kBlockSize - c; + } + } + return kBlockSize; +} + // public ByteArray AES::encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding) @@ -685,6 +705,10 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key) } ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); + if (i + kBlockSize == inputSize) { + // check padding + j = getPaddingIndex(outputBlock); + } std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } return result; @@ -785,21 +809,7 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) if (i + kBlockSize == inputSize) { // check padding - char lastChar = outputBlock[kBlockSize - 1]; - int c = lastChar & 0xff; - if (c > 0 && c < kBlockSize - 1) { - bool validPadding = true; - for (int chkIdx = c; chkIdx < kBlockSize - 2; ++chkIdx) { - if ((outputBlock[chkIdx] & 0xff) != c) { - // with openssl we found padding - validPadding = false; - break; - } - } - if (validPadding) { - j = c; - } - } + j = getPaddingIndex(outputBlock); } else { nextXorWithBeg = input.begin() + i; nextXorWithEnd = input.begin() + i + kBlockSize; diff --git a/src/aes.h b/src/aes.h index 2b2ff37..6e473f6 100644 --- a/src/aes.h +++ b/src/aes.h @@ -368,6 +368,11 @@ class AES { /// static ByteArray stateToByteArray(const State* state); + /// + /// \brief Get padding index for stripping the padding + /// + static std::size_t getPaddingIndex(const ByteArray& byteArr); + AES() = delete; AES(const AES&) = delete; AES& operator=(const AES&) = delete; diff --git a/test/aes-test.h b/test/aes-test.h index 9fb12eb..e41d80c 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -782,10 +782,13 @@ TEST(AESTest, CbcDecipher) } } -TEST(AESTest, RealDataIssuesTest) +TEST(AESTest, CrossAppsDataTest) { - std::string expected = "WQ73OMIum+OHKGHnAhQKJX1tByfBq4BhSpw2X+SgtjY="; std::string iv = "a14c54563269e9e368f56b325f04ff00"; + const std::string key = "CBD437FA37772C66051A47D72367B38E"; + + // genearted using online tool + std::string expected = "WQ73OMIum+OHKGHnAhQKJX1tByfBq4BhSpw2X+SgtjY="; std::string output = AES::encrypt("test this test this", "CBD437FA37772C66051A47D72367B38E", iv, @@ -794,10 +797,19 @@ TEST(AESTest, RealDataIssuesTest) ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + std::string nextexp = "test this test this"; + output = AES::decrypt("WQ73OMIum+OHKGHnAhQKJX1tByfBq4BhSpw2X+SgtjY=", + key, + iv, + AES::Encoding::Base64, + AES::Encoding::Raw); + + ASSERT_STRCASEEQ(nextexp.c_str(), output.c_str()); + expected = "EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ=="; output = AES::encrypt(R"({"_t":1503928197,"logger_id":"default","access_code":"default"})", - "CBD437FA37772C66051A47D72367B38E", + key, iv, AES::Encoding::Raw, AES::Encoding::Base64); @@ -809,29 +821,35 @@ TEST(AESTest, RealDataIssuesTest) // expected = R"({"_t":1503928197,"logger_id":"default","access_code":"default"})"; output = AES::decrypt("EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ==", - "CBD437FA37772C66051A47D72367B38E", - "a14c54563269e9e368f56b325f04ff00", + key, + iv, AES::Encoding::Base64, AES::Encoding::Raw); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + // generated with ripe + // echo test this test this | ripe -e --aes --key CBD437FA37772C66051A47D72367B38E --iv a14c54563269e9e368f56b325f04ff00 expected = "test this test this"; - output = AES::decrypt("WQ73OMIum+OHKGHnAhQKJc/uwM2APneVOH9mBq15bOk=", - "CBD437FA37772C66051A47D72367B38E", - "a14c54563269e9e368f56b325f04ff00", + output = AES::decrypt("WQ73OMIum+OHKGHnAhQKJX1tByfBq4BhSpw2X+SgtjY=", + key, + iv, AES::Encoding::Base64, AES::Encoding::Raw); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); - expected = R"({"_t":1503928197,"logger_id":"default","access_code":"default"})"; - output = AES::decrypt("12D62BE49168FFB92A616C68A0CBD4D8327EBA936150C0E28BD5FA2041D8C6F50D5D2546937E086A44F91FB19BCB32F9FC920C3004022E7514F36E11237CA681", - "CBD437FA37772C66051A47D72367B38E", - "a14c54563269e9e368f56b325f04ff00", - AES::Encoding::Base16, + // generated with openssl + // echo test this test this | openssl enc -aes-128-cbc -K CBD437FA37772C66051A47D72367B38E -iv a14c54563269e9e368f56b325f04ff00 -base64 + expected = "test this test this\n"; // openssl adds newline char + output = AES::decrypt("WQ73OMIum+OHKGHnAhQKJdSsXR5NwysOnq+cuf5C6cs=", + key, + iv, + AES::Encoding::Base64, AES::Encoding::Raw); + + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } diff --git a/test/main.cc b/test/main.cc index f7aaa9d..a85a1fd 100644 --- a/test/main.cc +++ b/test/main.cc @@ -2,7 +2,7 @@ #include "base16-test.h" #include "base64-test.h" //#include "zlib-test.h" -//#include "rsa-test.h" +#include "rsa-test.h" #include "aes-test.h" INITIALIZE_EASYLOGGINGPP From 2e54f5723dcbaa45a42a6a326458bab47f1feaef Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Wed, 30 Aug 2017 00:38:12 +1000 Subject: [PATCH 119/148] fix issues --- package/mine.cc | 40 +++++++++++++++++++++++++--------------- package/mine.h | 5 +++++ test/aes-test.h | 13 +++++++++++++ 3 files changed, 43 insertions(+), 15 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 1bd7209..eeb272e 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -706,6 +706,26 @@ std::string AES::resolveOutputMode(const ByteArray& input, Encoding outputMode) return Base64::encode(input.begin(), input.end()); } +std::size_t AES::getPaddingIndex(const ByteArray& byteArr) +{ + char lastChar = byteArr[kBlockSize - 1]; + int c = lastChar & 0xff; + if (c > 0 && c < kBlockSize - 1) { + bool validPadding = true; + for (int chkIdx = kBlockSize - c; chkIdx < kBlockSize; ++chkIdx) { + if ((byteArr[chkIdx] & 0xff) != c) { + // with openssl we found padding + validPadding = false; + break; + } + } + if (validPadding) { + return kBlockSize - c; + } + } + return kBlockSize; +} + // public ByteArray AES::encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding) @@ -769,6 +789,10 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key) } ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); + if (i + kBlockSize == inputSize) { + // check padding + j = getPaddingIndex(outputBlock); + } std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); } return result; @@ -869,21 +893,7 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) if (i + kBlockSize == inputSize) { // check padding - char lastChar = outputBlock[kBlockSize - 1]; - int c = lastChar & 0xff; - if (c > 0 && c < kBlockSize - 1) { - bool validPadding = true; - for (int chkIdx = c; chkIdx < kBlockSize - 2; ++chkIdx) { - if ((outputBlock[chkIdx] & 0xff) != c) { - // with openssl we found padding - validPadding = false; - break; - } - } - if (validPadding) { - j = c; - } - } + j = getPaddingIndex(outputBlock); } else { nextXorWithBeg = input.begin() + i; nextXorWithEnd = input.begin() + i + kBlockSize; diff --git a/package/mine.h b/package/mine.h index 4e2a040..eef3b05 100644 --- a/package/mine.h +++ b/package/mine.h @@ -809,6 +809,11 @@ class AES { /// static ByteArray stateToByteArray(const State* state); + /// + /// \brief Get padding index for stripping the padding + /// + static std::size_t getPaddingIndex(const ByteArray& byteArr); + AES() = delete; AES(const AES&) = delete; AES& operator=(const AES&) = delete; diff --git a/test/aes-test.h b/test/aes-test.h index e41d80c..b378970 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -786,6 +786,7 @@ TEST(AESTest, CrossAppsDataTest) { std::string iv = "a14c54563269e9e368f56b325f04ff00"; const std::string key = "CBD437FA37772C66051A47D72367B38E"; + const std::string keyBig = "163E6AC9A9EB43253AC237D849BDD22C4798393D38FBE322F7E593E318F1AEAF"; // genearted using online tool std::string expected = "WQ73OMIum+OHKGHnAhQKJX1tByfBq4BhSpw2X+SgtjY="; @@ -850,6 +851,18 @@ TEST(AESTest, CrossAppsDataTest) AES::Encoding::Raw); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + + // generated with openssl + // echo test this test this | openssl enc -aes-256-cbc -K 163E6AC9A9EB43253AC237D849BDD22C4798393D38FBE322F7E593E318F1AEAF -iv a14c54563269e9e368f56b325f04ff00 -base64 + expected = "test this test this\n"; // openssl adds newline char + output = AES::decrypt("vVMWB9aLpcfRgfai7OnCCLI5aAK+kK3Yem/E03uEM+w=", + keyBig, + iv, + AES::Encoding::Base64, + AES::Encoding::Raw); + + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } From c27412ce6ad292ab0c7e10519995b57343cb8049 Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 30 Aug 2017 09:53:34 +1000 Subject: [PATCH 120/148] some more fixes for aes --- cli/mine.cc | 12 ++++++------ package/mine.cc | 4 +++- package/mine.h | 2 +- src/aes.cc | 4 +++- src/aes.h | 2 +- test/aes-test.h | 16 ++++++++++++++++ 6 files changed, 30 insertions(+), 10 deletions(-) diff --git a/cli/mine.cc b/cli/mine.cc index 6732f4e..86a9104 100644 --- a/cli/mine.cc +++ b/cli/mine.cc @@ -71,11 +71,11 @@ void displayVersion() #define TRY try { #define CATCH } catch (const std::exception& e) { std::cout << "ERROR: " << e.what() << std::endl; } -void encryptAES(std::string& data, const std::string& key, std::string& iv) +void encryptAES(std::string& data, const std::string& key, std::string& iv, bool isBase64) { TRY bool newIv = iv.empty(); - std::cout << AES::encrypt(data, key, iv, AES::Encoding::Raw, AES::Encoding::Base64); + std::cout << AES::encrypt(data, key, iv, AES::Encoding::Raw, isBase64 ? AES::Encoding::Base64 : AES::Encoding::Base16); if (newIv) { std::cout << std::endl << "IV: " << iv << std::endl; @@ -83,10 +83,10 @@ void encryptAES(std::string& data, const std::string& key, std::string& iv) CATCH } -void decryptAES(std::string& data, const std::string& key, std::string& iv) +void decryptAES(std::string& data, const std::string& key, std::string& iv, bool isBase64) { TRY - std::cout << AES::decrypt(data, key, iv, AES::Encoding::Base64); + std::cout << AES::decrypt(data, key, iv, isBase64 ? AES::Encoding::Base64 : AES::Encoding::Base16); CATCH } @@ -208,7 +208,7 @@ int main(int argc, char* argv[]) decodeHex(data); } else { // AES decrypt (base64-flexible) - decryptAES(data, key, iv); + decryptAES(data, key, iv, isBase64); } } else if (type == 2) { // Encrypt / Encode if (isBase64 && key.empty() && iv.empty()) { @@ -216,7 +216,7 @@ int main(int argc, char* argv[]) } else if (isHex && key.empty() && iv.empty()) { encodeHex(data); } else { - encryptAES(data, key, iv); + encryptAES(data, key, iv, isBase64); } } else if (type == 3) { // Generate if (isAES) { diff --git a/package/mine.cc b/package/mine.cc index eeb272e..a95fd45 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -710,7 +710,7 @@ std::size_t AES::getPaddingIndex(const ByteArray& byteArr) { char lastChar = byteArr[kBlockSize - 1]; int c = lastChar & 0xff; - if (c > 0 && c < kBlockSize - 1) { + if (c > 0 && c <= kBlockSize) { bool validPadding = true; for (int chkIdx = kBlockSize - c; chkIdx < kBlockSize; ++chkIdx) { if ((byteArr[chkIdx] & 0xff) != c) { @@ -721,6 +721,8 @@ std::size_t AES::getPaddingIndex(const ByteArray& byteArr) } if (validPadding) { return kBlockSize - c; + } else { + throw std::runtime_error("Incorrect padding"); } } return kBlockSize; diff --git a/package/mine.h b/package/mine.h index eef3b05..c3d3c25 100644 --- a/package/mine.h +++ b/package/mine.h @@ -810,7 +810,7 @@ class AES { static ByteArray stateToByteArray(const State* state); /// - /// \brief Get padding index for stripping the padding + /// \brief Get padding index for stripping the padding (unpadding) /// static std::size_t getPaddingIndex(const ByteArray& byteArr); diff --git a/src/aes.cc b/src/aes.cc index 36bcc48..00f80e7 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -626,7 +626,7 @@ std::size_t AES::getPaddingIndex(const ByteArray& byteArr) { char lastChar = byteArr[kBlockSize - 1]; int c = lastChar & 0xff; - if (c > 0 && c < kBlockSize - 1) { + if (c > 0 && c <= kBlockSize) { bool validPadding = true; for (int chkIdx = kBlockSize - c; chkIdx < kBlockSize; ++chkIdx) { if ((byteArr[chkIdx] & 0xff) != c) { @@ -637,6 +637,8 @@ std::size_t AES::getPaddingIndex(const ByteArray& byteArr) } if (validPadding) { return kBlockSize - c; + } else { + throw std::runtime_error("Incorrect padding"); } } return kBlockSize; diff --git a/src/aes.h b/src/aes.h index 6e473f6..d13095e 100644 --- a/src/aes.h +++ b/src/aes.h @@ -369,7 +369,7 @@ class AES { static ByteArray stateToByteArray(const State* state); /// - /// \brief Get padding index for stripping the padding + /// \brief Get padding index for stripping the padding (unpadding) /// static std::size_t getPaddingIndex(const ByteArray& byteArr); diff --git a/test/aes-test.h b/test/aes-test.h index b378970..4c7e4ce 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -691,6 +691,22 @@ TEST(AESTest, Base64StringInputDecipher) ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } +TEST(AESTest, CbcCipherPadding) +{ + const std::string key = "F1EF6477CC39E65DE106C33BB0EC651386CD0932A9DE491CF960BC3EB79EBE78"; + const std::string iv = "000102030405060708090a0b0c0d0e0f"; + + std::string cipherB64 = "OcTPoBeDqlA/igjnNcl5yw=="; + std::string expected = "o1223456789012"; + std::string output = AES::decrypt(cipherB64, key, iv, AES::Encoding::Base64); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); + + cipherB64 = "NNH44Ybac3AhcP4+sTq8j4miT04jHtoaj7a/Wv0/TQ8="; + expected = "sho4123456789014"; + output = AES::decrypt(cipherB64, key, iv, AES::Encoding::Base64); + ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); +} + TEST(AESTest, EcbDecipher) { const std::string key = "F1EF6477CC39E65DE106C33BB0EC651386CD0932A9DE491CF960BC3EB79EBE78"; From 98bef289845c41a70cf6276f8194668a6f000d93 Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 30 Aug 2017 14:01:20 +1000 Subject: [PATCH 121/148] use testx --- test/aes-test.h | 124 ++++++++++++++++++++++++------------------------ test/test.h | 20 +++----- test/testx.h | 35 ++++++++++++++ 3 files changed, 104 insertions(+), 75 deletions(-) create mode 100644 test/testx.h diff --git a/test/aes-test.h b/test/aes-test.h index 4c7e4ce..595aea3 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -413,6 +413,67 @@ TEST(AESTest, AddRoundKey) ASSERT_EQ(expected7, state); } +TEST(AESTest, RawSimpleCipher) +{ + + // FIPS. 197 p. 33 + ByteArray input = {{ + 0x32, 0x43, 0xf6, 0xa8, + 0x88, 0x5a, 0x30, 0x8d, + 0x31, 0x31, 0x98, 0xa2, + 0xe0, 0x37, 0x07, 0x34 + }}; + + AES::Key key = {{ + 0x2b, 0x7e, 0x15, 0x16, + 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, + 0x09, 0xcf, 0x4f, 0x3c + }}; + + ByteArray expected = {{ + 0x39, 0x25, 0x84, 0x1d, + 0x02, 0xdc, 0x09, 0xfb, + 0xdc, 0x11, 0x85, 0x97, + 0x19, 0x6a, 0x0b, 0x32 + }}; + + AES::KeySchedule keySchedule = AES::keyExpansion(&key); + ByteArray output = AES::encryptSingleBlock(input.begin(), &key, &keySchedule); + ASSERT_EQ(expected, output); +} + +TEST(AESTest, RawSimpleDecipher) +{ + + // FIPS. 197 p. 33 + ByteArray input = {{ + 0x39, 0x25, 0x84, 0x1d, + 0x02, 0xdc, 0x09, 0xfb, + 0xdc, 0x11, 0x85, 0x97, + 0x19, 0x6a, 0x0b, 0x32 + }}; + + AES::Key key = {{ + 0x2b, 0x7e, 0x15, 0x16, + 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, + 0x09, 0xcf, 0x4f, 0x3c + }}; + + ByteArray expected = {{ + + 0x32, 0x43, 0xf6, 0xa8, + 0x88, 0x5a, 0x30, 0x8d, + 0x31, 0x31, 0x98, 0xa2, + 0xe0, 0x37, 0x07, 0x34 + }}; + + AES::KeySchedule keySchedule = AES::keyExpansion(&key); + ByteArray output = AES::decryptSingleBlock(input.begin(), &key, &keySchedule); + ASSERT_EQ(expected, output); +} + // from FIPS.197 p.35 onwards // input key expected static TestData RawCipherData = { @@ -435,6 +496,8 @@ static TestData RawCipherData = { TEST(AESTest, RawCipher) { for (auto& item : RawCipherData) { + //ByteArray b = item<0>; + ByteArray input = Base16::fromString(PARAM(0)); AES::Key key = static_cast(Base16::fromString(PARAM(1))); ByteArray expected = Base16::fromString(PARAM(2)); @@ -485,67 +548,6 @@ TEST(AESTest, RawCipherBase64) } } -TEST(AESTest, RawSimpleCipher) -{ - - // FIPS. 197 p. 33 - ByteArray input = {{ - 0x32, 0x43, 0xf6, 0xa8, - 0x88, 0x5a, 0x30, 0x8d, - 0x31, 0x31, 0x98, 0xa2, - 0xe0, 0x37, 0x07, 0x34 - }}; - - AES::Key key = {{ - 0x2b, 0x7e, 0x15, 0x16, - 0x28, 0xae, 0xd2, 0xa6, - 0xab, 0xf7, 0x15, 0x88, - 0x09, 0xcf, 0x4f, 0x3c - }}; - - ByteArray expected = {{ - 0x39, 0x25, 0x84, 0x1d, - 0x02, 0xdc, 0x09, 0xfb, - 0xdc, 0x11, 0x85, 0x97, - 0x19, 0x6a, 0x0b, 0x32 - }}; - - AES::KeySchedule keySchedule = AES::keyExpansion(&key); - ByteArray output = AES::encryptSingleBlock(input.begin(), &key, &keySchedule); - ASSERT_EQ(expected, output); -} - -TEST(AESTest, RawSimpleDecipher) -{ - - // FIPS. 197 p. 33 - ByteArray input = {{ - 0x39, 0x25, 0x84, 0x1d, - 0x02, 0xdc, 0x09, 0xfb, - 0xdc, 0x11, 0x85, 0x97, - 0x19, 0x6a, 0x0b, 0x32 - }}; - - AES::Key key = {{ - 0x2b, 0x7e, 0x15, 0x16, - 0x28, 0xae, 0xd2, 0xa6, - 0xab, 0xf7, 0x15, 0x88, - 0x09, 0xcf, 0x4f, 0x3c - }}; - - ByteArray expected = {{ - - 0x32, 0x43, 0xf6, 0xa8, - 0x88, 0x5a, 0x30, 0x8d, - 0x31, 0x31, 0x98, 0xa2, - 0xe0, 0x37, 0x07, 0x34 - }}; - - AES::KeySchedule keySchedule = AES::keyExpansion(&key); - ByteArray output = AES::decryptSingleBlock(input.begin(), &key, &keySchedule); - ASSERT_EQ(expected, output); -} - TEST(AESTest, CbcCipher) { // input expected diff --git a/test/test.h b/test/test.h index 0b42db8..af26a96 100644 --- a/test/test.h +++ b/test/test.h @@ -1,20 +1,12 @@ -#ifndef TEST_HELPERS_H_ -#define TEST_HELPERS_H_ - -#include -#include +#ifndef SIMPLE_TEST_X_H +#define SIMPLE_TEST_X_H #include #include - -template -using TestData = const std::vector>; - -template -std::tuple TestCase(T... f) { - return std::make_tuple(f...); -} +#include "testx.h" #define PARAM(v) std::get(item) -#endif +using namespace muflihun::testx; + +#endif // SIMPLE_TEST_X_H diff --git a/test/testx.h b/test/testx.h new file mode 100644 index 0000000..ac99d0e --- /dev/null +++ b/test/testx.h @@ -0,0 +1,35 @@ +// +// Bismillah ar-Rahmaan ar-Raheem +// +// TestX 1.0.1 +// Single header, header only helper for creating test data +// +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the MIT Licence. +// +// https://github.com/muflihun/testx +// https://muflihun.github.io +// http://muflihun.com +// +#ifndef MUFLIHUN_TEST_X_H +#define MUFLIHUN_TEST_X_H + +#include +#include + +namespace muflihun { +namespace testx { + +template +using TestData = const std::vector>; + +template +std::tuple TestCase(T... f) { + return std::make_tuple(f...); +} + +} // namespace testx +} // namespace muflihun + +#endif // MUFLIHUN_TEST_X_H From 93b6472392c5c18ac5a36b8c7cba72851ee522db Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 30 Aug 2017 16:07:18 +1000 Subject: [PATCH 122/148] added zlib code --- CMakeLists.txt | 22 ++++++-- package/mine.cc | 111 ++++++++++++++++++++++++++++++++++++++ package/mine.h | 139 ++++++++++++++++++++++++++++++------------------ src/aes.cc | 8 +++ src/base64.h | 96 +++++++++++++++------------------ src/rsa.h | 17 ++++++ src/zlib.cc | 104 ++++++++++++++++++++++++++++++++++++ src/zlib.h | 28 ++++++++++ 8 files changed, 415 insertions(+), 110 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fe4f8d5..00a9a1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,7 @@ if (APPLE) endif() endif() -list (APPEND CMAKE_CXX_FLAGS " -Wall -Wextra ") +list (APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wunused -g ") require_cpp11() @@ -83,15 +83,25 @@ set (CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE}) file (GLOB_RECURSE all_headers ${CMAKE_SOURCE_DIR}/*.h) add_custom_target (all_placeholder SOURCES ${all_headers}) +find_package(ZLIB REQUIRED) +if (ZLIB_FOUND) + include_directories(${ZLIB_INCLUDE_DIRS}) +endif(ZLIB_FOUND) + ########################################## CLI Tool ################################### -add_executable (mine-bin cli/mine.cc package/mine.cc) +add_executable (mine-cli cli/mine.cc package/mine.cc) -set_target_properties (mine-bin PROPERTIES +set_target_properties (mine-cli PROPERTIES OUTPUT_NAME "mine" VERSION ${MINE_VERSION} ) -install (TARGETS mine-bin DESTINATION bin) + +target_link_libraries(mine-cli + ${ZLIB_LIBRARIES} +) + +install (TARGETS mine-cli DESTINATION bin) ########################################## Unit Testing ################################### @@ -127,6 +137,10 @@ else() ) endif() +target_link_libraries(mine-unit-tests + ${ZLIB_LIBRARIES} +) + if (test_wstring_conversions) target_compile_definitions (mine-unit-tests PUBLIC MINE_BASE64_WSTRING_CONVERSION diff --git a/package/mine.cc b/package/mine.cc index a95fd45..f7e964f 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include "mine.h" @@ -322,7 +323,15 @@ void AES::addRoundKey(State* state, KeySchedule* keySchedule, int round) for (std::size_t i = 0; i < kNb; ++i) { std::size_t iR2 = iR + i; for (std::size_t j = 0; j < kNb; ++j) { +#if MINE_PROFILING + auto started2 = std::chrono::steady_clock::now(); + std::cout << ((*state)[i][j] & 0xff) << " ^= " << ((*keySchedule)[iR2][j] & 0xff); +#endif (*state)[i][j] ^= (*keySchedule)[iR2][j]; +#if MINE_PROFILING + std::cout << " = " << ((*state)[i][j] & 0xff) << std::endl; + endProfiling(started2, "add single round"); +#endif } } #if MINE_PROFILING @@ -957,3 +966,105 @@ std::string AES::generateRandomKey(const std::size_t len) } + +bool ZLib::compressFile(const std::string& gzFilename, const std::string& inputFile) noexcept +{ + gzFile out = gzopen(gzFilename.c_str(), "wb"); + if (!out) { + throw std::invalid_argument("Unable to open file [" + gzFilename + "] for writing." + std::strerror(errno)); + } + char buff[kBufferSize]; + std::FILE* in = std::fopen(inputFile.c_str(), "rb"); + std::size_t nRead = 0; + while((nRead = std::fread(buff, sizeof(char), kBufferSize, in)) > 0) { + int bytes_written = gzwrite(out, buff, nRead); + if (bytes_written == 0) { + int err_no = 0; + throw std::runtime_error("Error during compression: " + std::string(gzerror(out, &err_no))); + gzclose(out); + return false; + } + } + gzclose(out); + std::fclose(in); + return true; +} + +std::string ZLib::compressString(const std::string& str) +{ + int compressionlevel = Z_BEST_COMPRESSION; + z_stream zs; + memset(&zs, 0, sizeof(zs)); + + if (deflateInit(&zs, compressionlevel) != Z_OK) { + throw std::runtime_error("Unable to initialize zlib deflate"); + } + + zs.next_in = reinterpret_cast(const_cast(str.data())); + zs.avail_in = str.size(); + + int ret; + char outbuffer[kBufferSize]; + std::string outstring; + + // retrieve the compressed bytes blockwise + do { + zs.next_out = reinterpret_cast(outbuffer); + zs.avail_out = sizeof(outbuffer); + + ret = deflate(&zs, Z_FINISH); + + if (outstring.size() < zs.total_out) { + outstring.append(outbuffer, zs.total_out - outstring.size()); + } + } while (ret == Z_OK); + + deflateEnd(&zs); + + if (ret != Z_STREAM_END) { + std::ostringstream oss; + oss << "Exception during zlib compression: (" << ret << ") " << zs.msg; + throw std::runtime_error(oss.str()); + } + + return outstring; +} + +std::string ZLib::decompressString(const std::string& str) +{ + z_stream zs; + memset(&zs, 0, sizeof(zs)); + + if (inflateInit(&zs) != Z_OK) { + throw std::runtime_error("Unable to initialize zlib inflate"); + } + + zs.next_in = reinterpret_cast(const_cast(str.data())); + zs.avail_in = str.size(); + + int ret; + char outbuffer[kBufferSize]; + std::string outstring; + + do { + zs.next_out = reinterpret_cast(outbuffer); + zs.avail_out = sizeof(outbuffer); + + ret = inflate(&zs, 0); + + if (outstring.size() < zs.total_out) { + outstring.append(outbuffer, zs.total_out - outstring.size()); + } + + } while (ret == Z_OK); + + inflateEnd(&zs); + + if (ret != Z_STREAM_END) { + std::ostringstream oss; + oss << "Exception during zlib decompression: (" << ret << ") " << zs.msg; + throw std::runtime_error(oss.str()); + } + + return outstring; +} diff --git a/package/mine.h b/package/mine.h index c3d3c25..915da03 100644 --- a/package/mine.h +++ b/package/mine.h @@ -212,27 +212,7 @@ class Base64 { /// /// \brief Padding is must in mine implementation of base64 /// - static const char kPaddingChar = '='; - - /// - /// \brief Replacement for better d.size() that consider unicode bytes too - /// \see https://en.wikipedia.org/wiki/UTF-8#Description - /// - static std::size_t countChars(const std::string& d) noexcept; - -#ifdef MINE_BASE64_WSTRING_CONVERSION - /// - /// \brief Converts it to std::string and calls countChars on it - /// - /// \note You need to include and headers before mine.h - /// - static std::size_t countChars(const std::wstring& raw) noexcept - { - std::string converted = std::wstring_convert - , wchar_t>{}.to_bytes(raw); - return countChars(converted); - } -#endif + static const int kPadding = 64; /// /// \brief Encodes input of length to base64 encoding @@ -283,43 +263,25 @@ class Base64 { ss << static_cast(kValidChars[c3 & 0x3f]); // all the remaing bits } else { ss << static_cast(kValidChars[(c2 << 2) & 0x3f]); // we have 4 bits left from last byte need space for two 0-bits - ss << kPaddingChar; + ss << "="; } } else { ss << static_cast(kValidChars[(c << 4) & 0x3f]); // remaining 2 bits from single byte - ss << kPaddingChar << kPaddingChar; + ss << "=="; } } return ss.str() + padding; } -#ifdef MINE_BASE64_WSTRING_CONVERSION - /// - /// \brief Converts wstring to corresponding string and returns - /// encoding - /// \see encode(const std::string&) - /// - /// \note You need to include and headers before mine.h - /// - static std::string encode(const std::wstring& raw) noexcept - { - std::string converted = std::wstring_convert - , wchar_t>{}.to_bytes(raw); - return encode(converted); - } -#endif - /// /// \brief Decodes encoded base64 /// \see decode(const Iter&, const Iter&) /// static std::string decode(const std::string& e) { - //if (e.size() % 4 != 0) { - // we disable this check for 76 character line-break format (MIME) - // https://tools.ietf.org/html/rfc4648#section-3.1 - // throw std::invalid_argument("Invalid base64 encoding. Padding is required"); - //} + // don't check for e's length to be multiple of 4 + // because of 76 character line-break format (MIME) + // https://tools.ietf.org/html/rfc4648#section-3.1 return decode(e.begin(), e.end()); } @@ -339,7 +301,6 @@ class Base64 { // result indices 24 22 9 35 // - const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); std::stringstream ss; for (auto it = begin; it < end; it += 4) { try { @@ -351,7 +312,7 @@ class Base64 { } } int b0 = kDecodeMap.at(static_cast(*it & 0xff)); - if (b0 == kPadding/* || b0 == '\0'*/) { + if (b0 == kPadding) { throw std::invalid_argument("No data available"); } @@ -385,20 +346,16 @@ class Base64 { ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 - if (b1 != kPadding/* && b1 != '\0'*/) { - if (b2 == kPadding/* || (b2 == '\0' && b3 == '\0')*/) { + if (b1 != kPadding) { + if (b2 == kPadding) { // second bitset is only 4 bits - - //ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); } else { ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 // first we clear the bits at pos 4 and 5 // then we concat with next bit b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 - if (b3 == kPadding/* || b3 == '\0'*/) { + if (b3 == kPadding) { // third bitset is only 4 bits - //ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); - // first we clear first 4 bits } else { ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6 | // 001001 ==> 000001 << 6 ==> 01000000 // first we clear first 4 bits @@ -415,7 +372,22 @@ class Base64 { return ss.str(); } + #ifdef MINE_BASE64_WSTRING_CONVERSION + /// + /// \brief Converts wstring to corresponding string and returns + /// encoding + /// \see encode(const std::string&) + /// + /// \note You need to include and headers before mine.h + /// + static std::string encode(const std::wstring& raw) noexcept + { + std::string converted = std::wstring_convert + , wchar_t>{}.to_bytes(raw); + return encode(converted); + } + /// /// \brief Helper method to decode base64 encoding as wstring (basic_string) /// \see decode(const std::string&) @@ -433,8 +405,26 @@ class Base64 { >{}.from_bytes(result); return converted; } + + /// + /// \brief Converts it to std::string and calls countChars on it + /// + /// \note You need to include and headers before mine.h + /// + static std::size_t countChars(const std::wstring& raw) noexcept + { + std::string converted = std::wstring_convert + , wchar_t>{}.to_bytes(raw); + return countChars(converted); + } #endif + /// + /// \brief Replacement for better d.size() that consider unicode bytes too + /// \see https://en.wikipedia.org/wiki/UTF-8#Description + /// + static std::size_t countChars(const std::string& d) noexcept; + /// /// \brief expectedBase64Length Returns expected base64 length /// \param n Length of input (plain data) @@ -1399,6 +1389,23 @@ class GenericRSA { return true; } + /// + /// \brief Maximum size of RSA block with specified key size + /// \param keySize 2048, 1024, ... + /// + inline static unsigned int maxRSABlockSize(std::size_t keySize) + { + return (keySize / 8) - 11; + } + + /// + /// \brief Minimum size of RSA key to encrypt data of dataSize size + /// + inline static unsigned int minRSAKeySize(std::size_t dataSize) + { + return (dataSize + 11) * 8; + } + private: Helper m_helper; @@ -1548,6 +1555,32 @@ class GenericRSA { class ZLib { public: + /// + /// \brief Size of buffer algorithm should process under + /// + static const int kBufferSize = 32768; + + /// + /// \brief Compress input file (path) and create new file + /// \param gzFilename Output file path + /// \param inputFile Input file path + /// \return True if successful, otherwise false + /// + static bool compressFile(const std::string& gzFilename, const std::string& inputFile) noexcept; + + /// + /// @brief Compresses string using zlib (inflate) + /// @param str Input plain text + /// @return Raw output (binary) + /// + static std::string compressString(const std::string& str); + + /// + /// @brief Decompresses string using zlib (deflate) + /// @param str Raw input + /// @return Plain output + /// + static std::string decompressString(const std::string& str); private: ZLib() = delete; ZLib(const ZLib&) = delete; diff --git a/src/aes.cc b/src/aes.cc index 00f80e7..9138b50 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -238,7 +238,15 @@ void AES::addRoundKey(State* state, KeySchedule* keySchedule, int round) for (std::size_t i = 0; i < kNb; ++i) { std::size_t iR2 = iR + i; for (std::size_t j = 0; j < kNb; ++j) { +#if MINE_PROFILING + auto started2 = std::chrono::steady_clock::now(); + std::cout << ((*state)[i][j] & 0xff) << " ^= " << ((*keySchedule)[iR2][j] & 0xff); +#endif (*state)[i][j] ^= (*keySchedule)[iR2][j]; +#if MINE_PROFILING + std::cout << " = " << ((*state)[i][j] & 0xff) << std::endl; + endProfiling(started2, "add single round"); +#endif } } #if MINE_PROFILING diff --git a/src/base64.h b/src/base64.h index 9a7cf90..84bf5c1 100644 --- a/src/base64.h +++ b/src/base64.h @@ -66,27 +66,7 @@ class Base64 { /// /// \brief Padding is must in mine implementation of base64 /// - static const char kPaddingChar = '='; - - /// - /// \brief Replacement for better d.size() that consider unicode bytes too - /// \see https://en.wikipedia.org/wiki/UTF-8#Description - /// - static std::size_t countChars(const std::string& d) noexcept; - -#ifdef MINE_BASE64_WSTRING_CONVERSION - /// - /// \brief Converts it to std::string and calls countChars on it - /// - /// \note You need to include and headers before mine.h - /// - static std::size_t countChars(const std::wstring& raw) noexcept - { - std::string converted = std::wstring_convert - , wchar_t>{}.to_bytes(raw); - return countChars(converted); - } -#endif + static const int kPadding = 64; /// /// \brief Encodes input of length to base64 encoding @@ -137,43 +117,25 @@ class Base64 { ss << static_cast(kValidChars[c3 & 0x3f]); // all the remaing bits } else { ss << static_cast(kValidChars[(c2 << 2) & 0x3f]); // we have 4 bits left from last byte need space for two 0-bits - ss << kPaddingChar; + ss << "="; } } else { ss << static_cast(kValidChars[(c << 4) & 0x3f]); // remaining 2 bits from single byte - ss << kPaddingChar << kPaddingChar; + ss << "=="; } } return ss.str() + padding; } -#ifdef MINE_BASE64_WSTRING_CONVERSION - /// - /// \brief Converts wstring to corresponding string and returns - /// encoding - /// \see encode(const std::string&) - /// - /// \note You need to include and headers before mine.h - /// - static std::string encode(const std::wstring& raw) noexcept - { - std::string converted = std::wstring_convert - , wchar_t>{}.to_bytes(raw); - return encode(converted); - } -#endif - /// /// \brief Decodes encoded base64 /// \see decode(const Iter&, const Iter&) /// static std::string decode(const std::string& e) { - //if (e.size() % 4 != 0) { - // we disable this check for 76 character line-break format (MIME) - // https://tools.ietf.org/html/rfc4648#section-3.1 - // throw std::invalid_argument("Invalid base64 encoding. Padding is required"); - //} + // don't check for e's length to be multiple of 4 + // because of 76 character line-break format (MIME) + // https://tools.ietf.org/html/rfc4648#section-3.1 return decode(e.begin(), e.end()); } @@ -193,7 +155,6 @@ class Base64 { // result indices 24 22 9 35 // - const int kPadding = kDecodeMap.at(static_cast(kPaddingChar)); std::stringstream ss; for (auto it = begin; it < end; it += 4) { try { @@ -205,7 +166,7 @@ class Base64 { } } int b0 = kDecodeMap.at(static_cast(*it & 0xff)); - if (b0 == kPadding/* || b0 == '\0'*/) { + if (b0 == kPadding) { throw std::invalid_argument("No data available"); } @@ -239,20 +200,16 @@ class Base64 { ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 - if (b1 != kPadding/* && b1 != '\0'*/) { - if (b2 == kPadding/* || (b2 == '\0' && b3 == '\0')*/) { + if (b1 != kPadding) { + if (b2 == kPadding) { // second bitset is only 4 bits - - //ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4); } else { ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 // first we clear the bits at pos 4 and 5 // then we concat with next bit b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 - if (b3 == kPadding/* || b3 == '\0'*/) { + if (b3 == kPadding) { // third bitset is only 4 bits - //ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6); - // first we clear first 4 bits } else { ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6 | // 001001 ==> 000001 << 6 ==> 01000000 // first we clear first 4 bits @@ -269,7 +226,22 @@ class Base64 { return ss.str(); } + #ifdef MINE_BASE64_WSTRING_CONVERSION + /// + /// \brief Converts wstring to corresponding string and returns + /// encoding + /// \see encode(const std::string&) + /// + /// \note You need to include and headers before mine.h + /// + static std::string encode(const std::wstring& raw) noexcept + { + std::string converted = std::wstring_convert + , wchar_t>{}.to_bytes(raw); + return encode(converted); + } + /// /// \brief Helper method to decode base64 encoding as wstring (basic_string) /// \see decode(const std::string&) @@ -287,8 +259,26 @@ class Base64 { >{}.from_bytes(result); return converted; } + + /// + /// \brief Converts it to std::string and calls countChars on it + /// + /// \note You need to include and headers before mine.h + /// + static std::size_t countChars(const std::wstring& raw) noexcept + { + std::string converted = std::wstring_convert + , wchar_t>{}.to_bytes(raw); + return countChars(converted); + } #endif + /// + /// \brief Replacement for better d.size() that consider unicode bytes too + /// \see https://en.wikipedia.org/wiki/UTF-8#Description + /// + static std::size_t countChars(const std::string& d) noexcept; + /// /// \brief expectedBase64Length Returns expected base64 length /// \param n Length of input (plain data) diff --git a/src/rsa.h b/src/rsa.h index 3f96e9b..cd391ee 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -596,6 +596,23 @@ class GenericRSA { return true; } + /// + /// \brief Maximum size of RSA block with specified key size + /// \param keySize 2048, 1024, ... + /// + inline static unsigned int maxRSABlockSize(std::size_t keySize) + { + return (keySize / 8) - 11; + } + + /// + /// \brief Minimum size of RSA key to encrypt data of dataSize size + /// + inline static unsigned int minRSAKeySize(std::size_t dataSize) + { + return (dataSize + 11) * 8; + } + private: Helper m_helper; diff --git a/src/zlib.cc b/src/zlib.cc index b40679b..13a0029 100644 --- a/src/zlib.cc +++ b/src/zlib.cc @@ -14,6 +14,110 @@ // https://github.com/muflihun/mine // +#include +#include #include "src/zlib.h" using namespace mine; + +bool ZLib::compressFile(const std::string& gzFilename, const std::string& inputFile) noexcept +{ + gzFile out = gzopen(gzFilename.c_str(), "wb"); + if (!out) { + throw std::invalid_argument("Unable to open file [" + gzFilename + "] for writing." + std::strerror(errno)); + } + char buff[kBufferSize]; + std::FILE* in = std::fopen(inputFile.c_str(), "rb"); + std::size_t nRead = 0; + while((nRead = std::fread(buff, sizeof(char), kBufferSize, in)) > 0) { + int bytes_written = gzwrite(out, buff, nRead); + if (bytes_written == 0) { + int err_no = 0; + throw std::runtime_error("Error during compression: " + std::string(gzerror(out, &err_no))); + gzclose(out); + return false; + } + } + gzclose(out); + std::fclose(in); + return true; +} + +std::string ZLib::compressString(const std::string& str) +{ + int compressionlevel = Z_BEST_COMPRESSION; + z_stream zs; + memset(&zs, 0, sizeof(zs)); + + if (deflateInit(&zs, compressionlevel) != Z_OK) { + throw std::runtime_error("Unable to initialize zlib deflate"); + } + + zs.next_in = reinterpret_cast(const_cast(str.data())); + zs.avail_in = str.size(); + + int ret; + char outbuffer[kBufferSize]; + std::string outstring; + + // retrieve the compressed bytes blockwise + do { + zs.next_out = reinterpret_cast(outbuffer); + zs.avail_out = sizeof(outbuffer); + + ret = deflate(&zs, Z_FINISH); + + if (outstring.size() < zs.total_out) { + outstring.append(outbuffer, zs.total_out - outstring.size()); + } + } while (ret == Z_OK); + + deflateEnd(&zs); + + if (ret != Z_STREAM_END) { + std::ostringstream oss; + oss << "Exception during zlib compression: (" << ret << ") " << zs.msg; + throw std::runtime_error(oss.str()); + } + + return outstring; +} + +std::string ZLib::decompressString(const std::string& str) +{ + z_stream zs; + memset(&zs, 0, sizeof(zs)); + + if (inflateInit(&zs) != Z_OK) { + throw std::runtime_error("Unable to initialize zlib inflate"); + } + + zs.next_in = reinterpret_cast(const_cast(str.data())); + zs.avail_in = str.size(); + + int ret; + char outbuffer[kBufferSize]; + std::string outstring; + + do { + zs.next_out = reinterpret_cast(outbuffer); + zs.avail_out = sizeof(outbuffer); + + ret = inflate(&zs, 0); + + if (outstring.size() < zs.total_out) { + outstring.append(outbuffer, zs.total_out - outstring.size()); + } + + } while (ret == Z_OK); + + inflateEnd(&zs); + + if (ret != Z_STREAM_END) { + std::ostringstream oss; + oss << "Exception during zlib decompression: (" << ret << ") " << zs.msg; + throw std::runtime_error(oss.str()); + } + + return outstring; +} diff --git a/src/zlib.h b/src/zlib.h index 5b147b9..5324ab2 100644 --- a/src/zlib.h +++ b/src/zlib.h @@ -18,6 +18,8 @@ # error "Please use mine.h file. this file is only to aid the development" #endif +#include + #ifndef ZLib_H #define ZLib_H @@ -29,6 +31,32 @@ namespace mine { class ZLib { public: + /// + /// \brief Size of buffer algorithm should process under + /// + static const int kBufferSize = 32768; + + /// + /// \brief Compress input file (path) and create new file + /// \param gzFilename Output file path + /// \param inputFile Input file path + /// \return True if successful, otherwise false + /// + static bool compressFile(const std::string& gzFilename, const std::string& inputFile) noexcept; + + /// + /// @brief Compresses string using zlib (inflate) + /// @param str Input plain text + /// @return Raw output (binary) + /// + static std::string compressString(const std::string& str); + + /// + /// @brief Decompresses string using zlib (deflate) + /// @param str Raw input + /// @return Plain output + /// + static std::string decompressString(const std::string& str); private: ZLib() = delete; ZLib(const ZLib&) = delete; From 53d457d060cfaf270dbc8fc5f8575124c9360667 Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 30 Aug 2017 16:43:36 +1000 Subject: [PATCH 123/148] some fixes and issues raised --- cli/mine.cc | 79 +++++++++++++++++++++++++++++++++++++++++----- src/base16.cc | 4 +-- test/base16-test.h | 12 +++++++ test/main.cc | 6 ++-- test/zlib-test.h | 34 ++++++++++++++++++++ 5 files changed, 122 insertions(+), 13 deletions(-) diff --git a/cli/mine.cc b/cli/mine.cc index 86a9104..42a1427 100644 --- a/cli/mine.cc +++ b/cli/mine.cc @@ -32,18 +32,27 @@ void displayUsage() // we want to keep the order so can't use std::map or std::unordered_map std::vector> options = { {"--version", "Display version information"}, + + // operations {"-e", "Encrypt / encode / inflate the data"}, {"-d", "Decrypt / decrypt / deflate the data"}, {"-g", "Generate a random key"}, + + // modes {"--aes", "AES operations"}, - {"--key", "Symmetric key for encryption / decryption"}, - {"--iv", "Initializaion vector for decription"}, + {"--zlib", "ZLib compression/decompression"}, {"--base64", "Base64 operations"}, {"--hex", "Base16 operations"}, + + // parameters + {"--key", "Symmetric key for encryption / decryption"}, + {"--iv", "Initializaion vector for decription"}, {"--length", "Specify key length"}, + {"--in", "Input data from file (path)"}, + {"--output", "Output file (path)"}, }; - std::cout << "mine [-d | -e | -g | -s | -v] [--in ] [--key ] [--in-key ] [--out-public ] [--out-private ] [--iv ] [--base64] [--length ] [--aes []] [--hex]" << std::endl; + std::cout << "mine [-e | -d | -g] [--aes] [--hex] [--base64] [--zlib] [--in ] [--output ] [--key ] [--iv ] [--length ]" << std::endl; std::cout << std::endl; const std::size_t LONGEST = 20; for (auto& option : options) { @@ -65,7 +74,7 @@ std::string normalizeBase16(std::string enc) void displayVersion() { - std::cout << "Mine - Minimal cryptography library" << std::endl << "Version: " << MINE_VERSION << std::endl << "https://muflihun.com" << std::endl; + std::cout << "Mine - Minimal cryptography library" << std::endl << "Version: " << MINE_VERSION << std::endl << "https://muflihun.github.io" << std::endl; } #define TRY try { @@ -125,6 +134,50 @@ void decodeHex(std::string& data) CATCH } +void compress(std::string& data, bool isBase64, const std::string& outputFile) +{ + TRY + std::string o = ZLib::compressString(data); + + if (isBase64) { + o = Base64::encode(o); + } else { + o = Base16::encode(o); + } + if (outputFile.empty()) { + std::cout << o; + } else { + std::ofstream out(outputFile); + out << o; + out.close(); + } + CATCH +} + +void decompress(std::string& data, bool isBase64, const std::string& outputFile) +{ + TRY + try { + if (isBase64) { + data = Base64::decode(data); + } else { + data = Base16::decode(data); + } + } catch (const std::exception& e) { + //ignore + std::cout << "ERROR: " << e.what() << std::endl; + } + std::string o = ZLib::decompressString(data); + if (outputFile.empty()) { + std::cout << o; + } else { + std::ofstream out(outputFile); + out << o; + out.close(); + } + CATCH +} + int main(int argc, char* argv[]) { if (argc < 2) { @@ -144,7 +197,9 @@ int main(int argc, char* argv[]) std::string iv; int keyLength = 256; std::string data; + std::string outputFile; bool isAES = false; + bool isZlib = false; bool isBase64 = false; bool isHex = false; bool fileArgSpecified = false; @@ -162,8 +217,6 @@ int main(int argc, char* argv[]) isBase64 = true; } else if (arg == "--hex") { isHex = true; - } else if (arg == "--key" && hasNext) { - key = argv[++i]; } else if (arg == "--aes") { isAES = true; if (i + 1 < argc) { @@ -174,6 +227,10 @@ int main(int argc, char* argv[]) --i; } } + } else if (arg == "--zlib") { + isZlib = true; + } else if (arg == "--key" && hasNext) { + key = argv[++i]; } else if (arg == "--length" && hasNext) { keyLength = atoi(argv[++i]); } else if (arg == "--iv" && hasNext) { @@ -186,6 +243,8 @@ int main(int argc, char* argv[]) data = std::string((std::istreambuf_iterator(fs) ), (std::istreambuf_iterator())); fs.close(); + } else if (arg == "--out" && hasNext) { + outputFile = argv[++i]; } } @@ -199,22 +258,26 @@ int main(int argc, char* argv[]) data.erase(data.size() - 1); } - if (type == 1) { // Decrypt / Decode + if (type == 1) { // Decrypt / Decode / Decompress if (isBase64 && key.empty() && iv.empty()) { // base64 decode decodeBase64(data); } else if (isHex && key.empty() && iv.empty()) { // hex to ascii decodeHex(data); + } else if (isZlib) { + decompress(data, isBase64, outputFile); } else { // AES decrypt (base64-flexible) decryptAES(data, key, iv, isBase64); } - } else if (type == 2) { // Encrypt / Encode + } else if (type == 2) { // Encrypt / Encode / Compress if (isBase64 && key.empty() && iv.empty()) { encodeBase64(data); } else if (isHex && key.empty() && iv.empty()) { encodeHex(data); + } else if (isZlib) { + compress(data, isBase64, outputFile); } else { encryptAES(data, key, iv, isBase64); } diff --git a/src/base16.cc b/src/base16.cc index 25560a5..ab4b87e 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -55,7 +55,7 @@ void Base16::decode(char a, char b, std::ostringstream& ss) int b1 = b & 0xff; try { ss << static_cast((b0 << 4) | kDecodeMap.at(b1)); - } catch (const std::exception&) { - throw std::invalid_argument("Invalid base-16 encoding"); + } catch (const std::exception& e) { + throw std::invalid_argument("Invalid base-16 encoding: " + std::string(e.what())); } } diff --git a/test/base16-test.h b/test/base16-test.h index e1a8dc6..4d16451 100644 --- a/test/base16-test.h +++ b/test/base16-test.h @@ -32,6 +32,10 @@ static TestData InvalidBase16EncodingData = { TestCase("48656C6C6F20576F726C64F"), }; +static TestData EncodingDecodingData = { + TestCase("78DA2B492D2E0100045D01C1"), +}; + TEST(Base16Test, Encode) { for (const auto& item : Base16TestData) { @@ -40,6 +44,14 @@ TEST(Base16Test, Encode) } } +TEST(Base16Test, EncodeDecodingTest) +{ + for (const auto& item : EncodingDecodingData) { + std::string decoded = Base16::decode(PARAM(0)); + ASSERT_STREQ(PARAM(0).c_str(), Base16::encode(decoded).c_str()); + } +} + TEST(Base16Test, EncodeByteArray) { for (const auto& item : Base16ByteArrayEncodingTestData) { diff --git a/test/main.cc b/test/main.cc index a85a1fd..f496792 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,9 +1,9 @@ #include "test.h" -#include "base16-test.h" -#include "base64-test.h" -//#include "zlib-test.h" +#include "zlib-test.h" #include "rsa-test.h" #include "aes-test.h" +#include "base64-test.h" +#include "base16-test.h" INITIALIZE_EASYLOGGINGPP diff --git a/test/zlib-test.h b/test/zlib-test.h index 2116316..d770c81 100644 --- a/test/zlib-test.h +++ b/test/zlib-test.h @@ -7,10 +7,44 @@ # include "package/mine.h" #else # include "src/zlib.h" +# include "src/base64.h" +# include "src/base16.h" #endif namespace mine { +static TestData ZLibData = { + TestCase("abcd", "eNpLTEpOAQAD2AGL"), +}; + +static TestData ZLibDataHex = { + TestCase("test", "78DA2B492D2E0100045D01C1"), +}; + +TEST(ZLibTest, Compress) +{ + for (const auto& item : ZLibData) { + std::string encoded = ZLib::compressString(PARAM(0)); + ASSERT_EQ(PARAM(1), Base64::encode(encoded)); + } +} + +TEST(ZLibTest, Decompress) +{ + for (const auto& item : ZLibData) { + std::string decoded = ZLib::decompressString(Base64::decode(PARAM(1))); + ASSERT_EQ(PARAM(0), decoded); + } +} + +TEST(ZLibTest, DecompressHex) +{ + for (const auto& item : ZLibDataHex) { + std::string decoded = ZLib::decompressString(Base16::decode(PARAM(1))); + ASSERT_EQ(PARAM(0), decoded); + } +} + } #endif // ZLIB_TEST_H From a51e45ff70afd5035037b747f5f297a25b955981 Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 30 Aug 2017 16:57:47 +1000 Subject: [PATCH 124/148] fixed base16 critical issue --- src/base16.cc | 4 +--- test/base16-test.h | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/base16.cc b/src/base16.cc index ab4b87e..62f75f2 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -51,10 +51,8 @@ std::string Base16::toRawString(const ByteArray& input) void Base16::decode(char a, char b, std::ostringstream& ss) { - int b0 = a & 0xff; - int b1 = b & 0xff; try { - ss << static_cast((b0 << 4) | kDecodeMap.at(b1)); + ss << static_cast((kDecodeMap.at(a & 0xff) << 4) | kDecodeMap.at(b & 0xff)); } catch (const std::exception& e) { throw std::invalid_argument("Invalid base-16 encoding: " + std::string(e.what())); } diff --git a/test/base16-test.h b/test/base16-test.h index 4d16451..98cc180 100644 --- a/test/base16-test.h +++ b/test/base16-test.h @@ -33,6 +33,7 @@ static TestData InvalidBase16EncodingData = { }; static TestData EncodingDecodingData = { + TestCase("C1"), TestCase("78DA2B492D2E0100045D01C1"), }; From 98d3995390d4e0fedcd50fb25629424f95cca781 Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 30 Aug 2017 16:59:16 +1000 Subject: [PATCH 125/148] rebuilt package --- package/mine.cc | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index f7e964f..0def655 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -60,12 +60,10 @@ std::string Base16::toRawString(const ByteArray& input) void Base16::decode(char a, char b, std::ostringstream& ss) { - int b0 = a & 0xff; - int b1 = b & 0xff; try { - ss << static_cast((b0 << 4) | kDecodeMap.at(b1)); - } catch (const std::exception&) { - throw std::invalid_argument("Invalid base-16 encoding"); + ss << static_cast((kDecodeMap.at(a & 0xff) << 4) | kDecodeMap.at(b & 0xff)); + } catch (const std::exception& e) { + throw std::invalid_argument("Invalid base-16 encoding: " + std::string(e.what())); } } From cd620d0ed3fc802510649a96d423db22a27f5d5f Mon Sep 17 00:00:00 2001 From: mkhan Date: Wed, 30 Aug 2017 17:04:13 +1000 Subject: [PATCH 126/148] cerrorno and cstring imported --- package/mine.cc | 2 ++ src/zlib.cc | 2 ++ 2 files changed, 4 insertions(+) diff --git a/package/mine.cc b/package/mine.cc index 0def655..a9c24d1 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include #include "mine.h" diff --git a/src/zlib.cc b/src/zlib.cc index 13a0029..a0e19e4 100644 --- a/src/zlib.cc +++ b/src/zlib.cc @@ -14,6 +14,8 @@ // https://github.com/muflihun/mine // +#include +#include #include #include #include "src/zlib.h" From 1ea71a43e197c4b65006f41227a8d6b31cf922fe Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Wed, 30 Aug 2017 21:34:29 +1000 Subject: [PATCH 127/148] Minor changes --- CMakeLists.txt | 2 +- package/mine.cc | 10 +++------- package/mine.h | 4 ++-- src/zlib.cc | 10 +++------- src/zlib.h | 4 ++-- 5 files changed, 11 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 00a9a1c..679259b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,7 @@ if (APPLE) endif() endif() -list (APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wunused -g ") +list (APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wunused -O3 ") require_cpp11() diff --git a/package/mine.cc b/package/mine.cc index a9c24d1..6ed715c 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -967,7 +967,7 @@ std::string AES::generateRandomKey(const std::size_t len) -bool ZLib::compressFile(const std::string& gzFilename, const std::string& inputFile) noexcept +bool ZLib::compressFile(const std::string& gzFilename, const std::string& inputFile) { gzFile out = gzopen(gzFilename.c_str(), "wb"); if (!out) { @@ -1022,9 +1022,7 @@ std::string ZLib::compressString(const std::string& str) deflateEnd(&zs); if (ret != Z_STREAM_END) { - std::ostringstream oss; - oss << "Exception during zlib compression: (" << ret << ") " << zs.msg; - throw std::runtime_error(oss.str()); + throw std::runtime_error("Exception during zlib decompression: (" + std::to_string(ret) + "): " + std::string(zs.msg)); } return outstring; @@ -1061,9 +1059,7 @@ std::string ZLib::decompressString(const std::string& str) inflateEnd(&zs); if (ret != Z_STREAM_END) { - std::ostringstream oss; - oss << "Exception during zlib decompression: (" << ret << ") " << zs.msg; - throw std::runtime_error(oss.str()); + throw std::runtime_error("Exception during zlib decompression: (" + std::to_string(ret) + "): " + std::string(zs.msg)); } return outstring; diff --git a/package/mine.h b/package/mine.h index 915da03..8e88ee2 100644 --- a/package/mine.h +++ b/package/mine.h @@ -1556,7 +1556,7 @@ class ZLib { public: /// - /// \brief Size of buffer algorithm should process under + /// \brief Size of buffer algorithm should operate under /// static const int kBufferSize = 32768; @@ -1566,7 +1566,7 @@ class ZLib { /// \param inputFile Input file path /// \return True if successful, otherwise false /// - static bool compressFile(const std::string& gzFilename, const std::string& inputFile) noexcept; + static bool compressFile(const std::string& gzFilename, const std::string& inputFile); /// /// @brief Compresses string using zlib (inflate) diff --git a/src/zlib.cc b/src/zlib.cc index a0e19e4..94f05af 100644 --- a/src/zlib.cc +++ b/src/zlib.cc @@ -22,7 +22,7 @@ using namespace mine; -bool ZLib::compressFile(const std::string& gzFilename, const std::string& inputFile) noexcept +bool ZLib::compressFile(const std::string& gzFilename, const std::string& inputFile) { gzFile out = gzopen(gzFilename.c_str(), "wb"); if (!out) { @@ -77,9 +77,7 @@ std::string ZLib::compressString(const std::string& str) deflateEnd(&zs); if (ret != Z_STREAM_END) { - std::ostringstream oss; - oss << "Exception during zlib compression: (" << ret << ") " << zs.msg; - throw std::runtime_error(oss.str()); + throw std::runtime_error("Exception during zlib decompression: (" + std::to_string(ret) + "): " + std::string(zs.msg)); } return outstring; @@ -116,9 +114,7 @@ std::string ZLib::decompressString(const std::string& str) inflateEnd(&zs); if (ret != Z_STREAM_END) { - std::ostringstream oss; - oss << "Exception during zlib decompression: (" << ret << ") " << zs.msg; - throw std::runtime_error(oss.str()); + throw std::runtime_error("Exception during zlib decompression: (" + std::to_string(ret) + "): " + std::string(zs.msg)); } return outstring; diff --git a/src/zlib.h b/src/zlib.h index 5324ab2..bfa7442 100644 --- a/src/zlib.h +++ b/src/zlib.h @@ -32,7 +32,7 @@ class ZLib { public: /// - /// \brief Size of buffer algorithm should process under + /// \brief Size of buffer algorithm should operate under /// static const int kBufferSize = 32768; @@ -42,7 +42,7 @@ class ZLib { /// \param inputFile Input file path /// \return True if successful, otherwise false /// - static bool compressFile(const std::string& gzFilename, const std::string& inputFile) noexcept; + static bool compressFile(const std::string& gzFilename, const std::string& inputFile); /// /// @brief Compresses string using zlib (inflate) From 553668b62e0bf29c07c499f78dd4bb09ba58f8c8 Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Wed, 30 Aug 2017 22:00:43 +1000 Subject: [PATCH 128/148] Stateful AES class --- cli/mine.cc | 8 +- package/mine.h | 75 +++++++------- src/aes.cc | 144 ++++++++++++++++++++++++-- src/aes.h | 61 ++++++++--- test/aes-test.h | 263 ++++++++++++++++++++++++++++++++++++++++------- test/zlib-test.h | 1 + 6 files changed, 452 insertions(+), 100 deletions(-) diff --git a/cli/mine.cc b/cli/mine.cc index 42a1427..2248eae 100644 --- a/cli/mine.cc +++ b/cli/mine.cc @@ -80,11 +80,13 @@ void displayVersion() #define TRY try { #define CATCH } catch (const std::exception& e) { std::cout << "ERROR: " << e.what() << std::endl; } +static AES aes; + void encryptAES(std::string& data, const std::string& key, std::string& iv, bool isBase64) { TRY bool newIv = iv.empty(); - std::cout << AES::encrypt(data, key, iv, AES::Encoding::Raw, isBase64 ? AES::Encoding::Base64 : AES::Encoding::Base16); + std::cout << aes.encrypt(data, key, iv, AES::Encoding::Raw, isBase64 ? AES::Encoding::Base64 : AES::Encoding::Base16); if (newIv) { std::cout << std::endl << "IV: " << iv << std::endl; @@ -95,14 +97,14 @@ void encryptAES(std::string& data, const std::string& key, std::string& iv, bool void decryptAES(std::string& data, const std::string& key, std::string& iv, bool isBase64) { TRY - std::cout << AES::decrypt(data, key, iv, isBase64 ? AES::Encoding::Base64 : AES::Encoding::Base16); + std::cout << aes.decrypt(data, key, iv, isBase64 ? AES::Encoding::Base64 : AES::Encoding::Base16); CATCH } void generateAESKey(int length) { TRY - std::cout << AES::generateRandomKey(length); + std::cout << aes.generateRandomKey(length); CATCH } diff --git a/package/mine.h b/package/mine.h index 8e88ee2..8e3945d 100644 --- a/package/mine.h +++ b/package/mine.h @@ -483,6 +483,11 @@ using ByteArray = std::vector; class AES { public: + AES() = default; + AES(const AES&) = default; + AES& operator=(const AES&) = default; + virtual ~AES() = default; + /// /// \brief Convert mode for various functions /// @@ -505,7 +510,7 @@ class AES { /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Base16 encoded cipher /// - static std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); /// /// \brief Ciphers the input with specified hex key using CBC mode @@ -516,7 +521,7 @@ class AES { /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Base16 encoded cipher /// - static std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); /// /// \brief Deciphers the input with specified hex key @@ -525,7 +530,7 @@ class AES { /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - static std::string decrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + std::string decrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); /// /// \brief Deciphers the input with specified hex key using CBC mode @@ -535,7 +540,7 @@ class AES { /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - static std::string decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + std::string decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); /// /// \brief Ciphers with ECB-Mode, the input can be as long as user wants @@ -544,7 +549,7 @@ class AES { /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Cipher text byte array /// - static ByteArray encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding = true); + ByteArray encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding = true); /// /// \brief Deciphers with ECB-Mode, the input can be as long as user wants @@ -552,7 +557,7 @@ class AES { /// \param key Pointer to a valid AES key /// \return Cipher text byte array /// - static ByteArray decrypt(const ByteArray& input, const Key* key); + ByteArray decrypt(const ByteArray& input, const Key* key); /// /// \brief Ciphers with CBC-Mode, the input can be as long as user wants @@ -562,7 +567,7 @@ class AES { /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Cipher text byte array /// - static ByteArray encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bool pkcs5Padding = true); + ByteArray encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bool pkcs5Padding = true); /// /// \brief Deciphers with CBC-Mode, the input can be as long as user wants @@ -571,12 +576,12 @@ class AES { /// \param iv Initialization vector /// \return Cipher text byte array /// - static ByteArray decrypt(const ByteArray& input, const Key* key, ByteArray& iv); + ByteArray decrypt(const ByteArray& input, const Key* key, ByteArray& iv); /// /// \brief Generates random key of valid length /// - static std::string generateRandomKey(const std::size_t len); + std::string generateRandomKey(const std::size_t len); private: @@ -658,7 +663,7 @@ class AES { /// [a3] => [a4] /// [a4] [a1] /// - static void rotateWord(Word* w); + void rotateWord(Word* w); /// this function is also specified in FIPS.197 Sec. 5.2: /// SubWord() is a function that takes a four-byte @@ -669,107 +674,107 @@ class AES { /// It's a simple substition with kSbox for corresponding byte /// index /// - static void substituteWord(Word* w); + void substituteWord(Word* w); /// /// \brief Key expansion function as described in FIPS.197 /// - static KeySchedule keyExpansion(const Key* key); + KeySchedule keyExpansion(const Key* key); /// /// \brief Adds round to the state using specified key schedule /// - static void addRoundKey(State* state, KeySchedule* keySchedule, int round); + void addRoundKey(State* state, KeySchedule* keySchedule, int round); /// /// \brief Substitution step for state /// \ref Sec. 5.1.1 /// - static void subBytes(State* state); + void subBytes(State* state); /// /// \brief Shifting rows step for the state /// \ref Sec. 5.1.2 /// - static void shiftRows(State* state); + void shiftRows(State* state); /// /// \ref Sec. 4.2.1 /// - static byte xtime(byte x); + byte xtime(byte x); /// /// \ref Sec. 4.2.1 /// - static byte multiply(byte x, byte y); + byte multiply(byte x, byte y); /// /// \brief Mixing columns for the state /// \ref Sec. 5.1.3 /// - static void mixColumns(State* state); + void mixColumns(State* state); /// /// \brief Transformation in the Inverse Cipher /// that is the reverse of subBytes() /// \ref Sec. 5.3.2 /// - static void invSubBytes(State* state); + void invSubBytes(State* state); /// /// \brief Transformation in the Inverse Cipher that is /// the reverse of shiftRows() /// \ref Sec. 5.3.1 /// - static void invShiftRows(State* state); + void invShiftRows(State* state); /// /// \brief Transformation in the Inverse Cipher /// that is the reverse of mixColumns() /// \ref Sec. 5.3.3 /// - static void invMixColumns(State* state); + void invMixColumns(State* state); /// /// \brief Prints bytes in hex format in 4x4 matrix fashion /// - static void printBytes(const ByteArray& b); + void printBytes(const ByteArray& b); /// /// \brief Prints state for debugging /// - static void printState(const State*); + void printState(const State*); /// /// \brief Initializes the state with input. This function /// also pads the input if needed (i.e, input is not block of 128-bit) /// - static void initState(State* state, const ByteArray::const_iterator& begin); + void initState(State* state, const ByteArray::const_iterator& begin); /// /// \brief Generates random bytes of length /// - static ByteArray generateRandomBytes(const std::size_t len); + ByteArray generateRandomBytes(const std::size_t len); /// /// \brief Creates byte array from input based on input mode /// - static ByteArray resolveInputMode(const std::string& input, Encoding inputMode); + ByteArray resolveInputMode(const std::string& input, Encoding inputMode); /// /// \brief Creates string from byte array based on convert mode /// - static std::string resolveOutputMode(const ByteArray& input, Encoding outputMode); + std::string resolveOutputMode(const ByteArray& input, Encoding outputMode); /// /// \brief Exclusive XOR with arr /// - static ByteArray* xorWith(ByteArray* input, const ByteArray*); + ByteArray* xorWith(ByteArray* input, const ByteArray*); /// /// \brief Exclusive XOR with iter of range size as input /// - static ByteArray* xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end); + ByteArray* xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end); /// /// \brief Raw encryption function - not for public use @@ -781,7 +786,7 @@ class AES { /// \note This does not do any key or input validation /// \return 128-bit cipher text /// - static ByteArray encryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); + ByteArray encryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); /// /// \brief Raw decryption function - not for public use @@ -792,21 +797,17 @@ class AES { /// \param key Byte array of key /// \return 128-bit plain text /// - static ByteArray decryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); + ByteArray decryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array /// - static ByteArray stateToByteArray(const State* state); + ByteArray stateToByteArray(const State* state); /// /// \brief Get padding index for stripping the padding (unpadding) /// - static std::size_t getPaddingIndex(const ByteArray& byteArr); - - AES() = delete; - AES(const AES&) = delete; - AES& operator=(const AES&) = delete; + std::size_t getPaddingIndex(const ByteArray& byteArr); friend class AESTest_RawCipher_Test; friend class AESTest_RawCipherPlain_Test; diff --git a/src/aes.cc b/src/aes.cc index 9138b50..181ee6b 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -114,6 +114,53 @@ const std::unordered_map> AES::kKeyParams = { { 32, {{ 8, 14 }} } }; +AES::AES(const std::string& key) +{ + setKey(key); +} + +AES::AES(const ByteArray& key) : m_key(key) +{ + setKey(key); +} + +AES::AES(const AES& other) +{ + if (&other != this) { + m_key = other.m_key; + m_keySchedule = other.m_keySchedule; + } +} + +AES::AES(const AES&& other) : + m_key(std::move(other.m_key)), + m_keySchedule(std::move(other.m_keySchedule)) +{ +} + +AES& AES::operator=(const AES& other) +{ + if (&other != this) { + m_key = other.m_key; + m_keySchedule = other.m_keySchedule; + } + return *this; +} + +void AES::setKey(const std::string& key) +{ + setKey(Base16::fromString(key)); +} + +void AES::setKey(const ByteArray& key) +{ + if (key.size() != 16 && key.size() != 24 && key.size() != 32) { + throw std::invalid_argument("Invalid key size. AES can operate on 128-bit, 192-bit and 256-bit keys"); + } + m_key = key; + m_keySchedule = keyExpansion(&m_key); +} + void AES::printBytes(const ByteArray& b) { for (std::size_t i = 1; i <= b.size(); ++i) { @@ -666,7 +713,10 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding const std::size_t inputSize = input.size(); - KeySchedule keySchedule = keyExpansion(key); + if (*key != m_key) { + m_keySchedule = keyExpansion(key); + m_key = *key; + } ByteArray result; @@ -684,7 +734,7 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding std::fill(inputBlock.begin() + j, inputBlock.end(), kBlockSize - (j % kBlockSize)); } - ByteArray outputBlock = encryptSingleBlock(inputBlock.begin() + i, key, &keySchedule); + ByteArray outputBlock = encryptSingleBlock(inputBlock.begin() + i, key, &m_keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); } return result; @@ -700,7 +750,10 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key) throw std::invalid_argument("Invalid AES key size"); } - KeySchedule keySchedule = keyExpansion(key); + if (*key != m_key) { + m_keySchedule = keyExpansion(key); + m_key = *key; + } const std::size_t inputSize = input.size(); ByteArray result; @@ -713,7 +766,7 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key) for (; j < kBlockSize && inputSize > j + i; ++j) { inputBlock[j] = input[j + i]; } - ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); + ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &m_keySchedule); if (i + kBlockSize == inputSize) { // check padding @@ -741,9 +794,11 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bo iv = generateRandomBytes(16); } - // ByteArray inputCopy = input; + if (*key != m_key) { + m_keySchedule = keyExpansion(key); + m_key = *key; + } - KeySchedule keySchedule = keyExpansion(key); const std::size_t inputSize = input.size(); ByteArray result; @@ -767,7 +822,7 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bo } xorWithRange(&inputBlock, nextXorWithBeg, nextXorWithEnd); - ByteArray outputBlock = encryptSingleBlock(inputBlock.begin(), key, &keySchedule); + ByteArray outputBlock = encryptSingleBlock(inputBlock.begin(), key, &m_keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); nextXorWithBeg = result.end() - kBlockSize; nextXorWithEnd = result.end(); @@ -795,7 +850,11 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) throw std::invalid_argument("Ciphertext length is not a multiple of block size"); } - KeySchedule keySchedule = keyExpansion(key); + if (*key != m_key) { + m_keySchedule = keyExpansion(key); + m_key = *key; + } + ByteArray result; ByteArray::const_iterator nextXorWithBeg = iv.begin(); @@ -813,7 +872,7 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) inputBlock[j] = input[j + i]; } - ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); + ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &m_keySchedule); xorWithRange(&outputBlock, nextXorWithBeg, nextXorWithEnd); @@ -879,3 +938,70 @@ std::string AES::generateRandomKey(const std::size_t len) ByteArray bytes = generateRandomBytes(len / 8); return Base16::encode(bytes.begin(), bytes.end()); } + + +// encryption / decryption with previously provided key + +std::string AES::encr(const std::string& input, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return encrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding, pkcs5Padding); +} + +std::string AES::encr(const std::string& input, std::string& iv, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return encrypt(input, Base16::encode(Base16::toRawString(m_key)), iv, inputEncoding, outputEncoding, pkcs5Padding); +} + +std::string AES::decr(const std::string& input, Encoding inputEncoding, Encoding outputEncoding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return decrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding); +} + +std::string AES::decr(const std::string& input, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return decrypt(input, Base16::encode(Base16::toRawString(m_key)), iv, inputEncoding, outputEncoding); +} + +ByteArray AES::encr(const ByteArray& input, bool pkcs5Padding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return encrypt(input, &m_key, pkcs5Padding); +} + +ByteArray AES::decr(const ByteArray& input) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return decrypt(input, &m_key); +} + +ByteArray AES::encr(const ByteArray& input, ByteArray& iv, bool pkcs5Padding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return encrypt(input, &m_key, iv, pkcs5Padding); +} + +ByteArray AES::decr(const ByteArray& input, ByteArray& iv) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return decrypt(input, &m_key, iv); +} diff --git a/src/aes.h b/src/aes.h index d13095e..1c3bba0 100644 --- a/src/aes.h +++ b/src/aes.h @@ -66,6 +66,22 @@ class AES { /// using Key = ByteArray; + AES() = default; + AES(const std::string& key); + AES(const ByteArray& key); + AES(const AES&); + AES(const AES&&); + AES& operator=(const AES&); + virtual ~AES() = default; + + void setKey(const std::string& key); + void setKey(const ByteArray& key); + + /// + /// \brief Generates random key of valid length + /// + static std::string generateRandomKey(const std::size_t len); + /// /// \brief Ciphers the input with specified hex key /// \param key Hex key @@ -74,7 +90,7 @@ class AES { /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Base16 encoded cipher /// - static std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); /// /// \brief Ciphers the input with specified hex key using CBC mode @@ -85,7 +101,7 @@ class AES { /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Base16 encoded cipher /// - static std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); /// /// \brief Deciphers the input with specified hex key @@ -94,7 +110,7 @@ class AES { /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - static std::string decrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + std::string decrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); /// /// \brief Deciphers the input with specified hex key using CBC mode @@ -104,7 +120,7 @@ class AES { /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - static std::string decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + std::string decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); /// /// \brief Ciphers with ECB-Mode, the input can be as long as user wants @@ -113,7 +129,7 @@ class AES { /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Cipher text byte array /// - static ByteArray encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding = true); + ByteArray encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding = true); /// /// \brief Deciphers with ECB-Mode, the input can be as long as user wants @@ -121,7 +137,7 @@ class AES { /// \param key Pointer to a valid AES key /// \return Cipher text byte array /// - static ByteArray decrypt(const ByteArray& input, const Key* key); + ByteArray decrypt(const ByteArray& input, const Key* key); /// /// \brief Ciphers with CBC-Mode, the input can be as long as user wants @@ -131,7 +147,7 @@ class AES { /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Cipher text byte array /// - static ByteArray encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bool pkcs5Padding = true); + ByteArray encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bool pkcs5Padding = true); /// /// \brief Deciphers with CBC-Mode, the input can be as long as user wants @@ -140,12 +156,26 @@ class AES { /// \param iv Initialization vector /// \return Cipher text byte array /// - static ByteArray decrypt(const ByteArray& input, const Key* key, ByteArray& iv); + ByteArray decrypt(const ByteArray& input, const Key* key, ByteArray& iv); - /// - /// \brief Generates random key of valid length - /// - static std::string generateRandomKey(const std::size_t len); + + // cipher / decipher interface without keys + + std::string encr(const std::string& input, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + + std::string encr(const std::string& input, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + + std::string decr(const std::string& input, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + + std::string decr(const std::string& input, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + + ByteArray encr(const ByteArray& input, bool pkcs5Padding = true); + + ByteArray decr(const ByteArray& input); + + ByteArray encr(const ByteArray& input, ByteArray& iv, bool pkcs5Padding = true); + + ByteArray decr(const ByteArray& input, ByteArray& iv); private: @@ -373,10 +403,10 @@ class AES { /// static std::size_t getPaddingIndex(const ByteArray& byteArr); - AES() = delete; - AES(const AES&) = delete; - AES& operator=(const AES&) = delete; + Key m_key; // to keep track of key differences + KeySchedule m_keySchedule; + // for tests friend class AESTest_RawCipher_Test; friend class AESTest_RawCipherPlain_Test; friend class AESTest_RawCipherBase64_Test; @@ -391,6 +421,7 @@ class AES { friend class AESTest_KeyExpansion_Test; friend class AESTest_AddRoundKey_Test; friend class AESTest_CbcCipher_Test; + friend class AESTest_Copy_Test; }; } // end namespace mine diff --git a/test/aes-test.h b/test/aes-test.h index 595aea3..c132f54 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -16,6 +16,8 @@ namespace mine { // many test data is from NIST special publication // http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf +static AES aes; + TEST(AESTest, KeyExpansion) { // This key expansion is original key from FIPS.197 example @@ -209,7 +211,7 @@ TEST(AESTest, KeyExpansion) for (auto& item : KeyExpansionTestData) { LOG(INFO) << "Test: " << PARAM(0); - AES::KeySchedule keys = AES::keyExpansion(&PARAM(1)); + AES::KeySchedule keys = aes.keyExpansion(&PARAM(1)); AES::KeySchedule expected = PARAM(2); ASSERT_EQ(expected, keys); } @@ -236,7 +238,7 @@ TEST(AESTest, SubByte) for (auto& item : SubByteTestData) { AES::State state = PARAM(0); AES::State expected = PARAM(1); - AES::subBytes(&state); + aes.subBytes(&state); ASSERT_EQ(expected, state); } } @@ -262,7 +264,7 @@ TEST(AESTest, InvSubByte) for (auto& item : InvSubByteTestData) { AES::State state = PARAM(0); AES::State expected = PARAM(1); - AES::invSubBytes(&state); + aes.invSubBytes(&state); ASSERT_EQ(expected, state); } } @@ -288,7 +290,7 @@ TEST(AESTest, ShiftRows) for (auto& item : ShiftRowsTestData) { AES::State state = PARAM(0); AES::State expected = PARAM(1); - AES::shiftRows(&state); + aes.shiftRows(&state); ASSERT_EQ(expected, state); } } @@ -314,7 +316,7 @@ TEST(AESTest, InvShiftRows) for (auto& item : InvShiftRowsTestData) { AES::State state = PARAM(0); AES::State expected = PARAM(1); - AES::invShiftRows(&state); + aes.invShiftRows(&state); ASSERT_EQ(expected, state); } } @@ -340,7 +342,7 @@ TEST(AESTest, MixColumns) for (auto& item : MixColumnsTestData) { AES::State state = PARAM(0); AES::State expected = PARAM(1); - AES::mixColumns(&state); + aes.mixColumns(&state); ASSERT_EQ(expected, state); } } @@ -366,7 +368,7 @@ TEST(AESTest, InvMixColumns) for (auto& item : InvMixColumnsTestData) { AES::State state = PARAM(0); AES::State expected = PARAM(1); - AES::invMixColumns(&state); + aes.invMixColumns(&state); ASSERT_EQ(expected, state); } } @@ -381,7 +383,7 @@ TEST(AESTest, AddRoundKey) 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }}; - AES::KeySchedule schedule = AES::keyExpansion(&key); + AES::KeySchedule schedule = aes.keyExpansion(&key); AES::State state = {{ {{ 0x04, 0x66, 0x81, 0xe5 }}, // c0 {{ 0xe0, 0xcb, 0x19, 0x9a }}, // c1 @@ -400,7 +402,7 @@ TEST(AESTest, AddRoundKey) {{ 0xe8, 0x64, 0x72, 0xa9 }}, // c2 {{ 0xfd, 0xd2, 0x8b, 0x25 }}, // c3 }}; - AES::addRoundKey(&state, &schedule, 1); + aes.addRoundKey(&state, &schedule, 1); ASSERT_EQ(expected2, state); state = {{ @@ -409,7 +411,7 @@ TEST(AESTest, AddRoundKey) {{ 0x33, 0x9d, 0xf4, 0xe8 }}, // c2 {{ 0x37, 0xd2, 0x18, 0xd8 }}, // c3 }}; - AES::addRoundKey(&state, &schedule, 6); + aes.addRoundKey(&state, &schedule, 6); ASSERT_EQ(expected7, state); } @@ -438,8 +440,8 @@ TEST(AESTest, RawSimpleCipher) 0x19, 0x6a, 0x0b, 0x32 }}; - AES::KeySchedule keySchedule = AES::keyExpansion(&key); - ByteArray output = AES::encryptSingleBlock(input.begin(), &key, &keySchedule); + AES::KeySchedule keySchedule = aes.keyExpansion(&key); + ByteArray output = aes.encryptSingleBlock(input.begin(), &key, &keySchedule); ASSERT_EQ(expected, output); } @@ -469,8 +471,8 @@ TEST(AESTest, RawSimpleDecipher) 0xe0, 0x37, 0x07, 0x34 }}; - AES::KeySchedule keySchedule = AES::keyExpansion(&key); - ByteArray output = AES::decryptSingleBlock(input.begin(), &key, &keySchedule); + AES::KeySchedule keySchedule = aes.keyExpansion(&key); + ByteArray output = aes.decryptSingleBlock(input.begin(), &key, &keySchedule); ASSERT_EQ(expected, output); } @@ -502,9 +504,9 @@ TEST(AESTest, RawCipher) AES::Key key = static_cast(Base16::fromString(PARAM(1))); ByteArray expected = Base16::fromString(PARAM(2)); - AES::KeySchedule keySchedule = AES::keyExpansion(&key); + AES::KeySchedule keySchedule = aes.keyExpansion(&key); - ByteArray output = AES::encryptSingleBlock(input.begin(), &key, &keySchedule); + ByteArray output = aes.encryptSingleBlock(input.begin(), &key, &keySchedule); ASSERT_EQ(expected, output); } } @@ -514,7 +516,7 @@ TEST(AESTest, RawCipherDirect) for (auto& item : RawCipherData) { std::string expected = PARAM(2); - std::string output = AES::encrypt(PARAM(0), PARAM(1), AES::Encoding::Base16); + std::string output = aes.encrypt(PARAM(0), PARAM(1), AES::Encoding::Base16); // case insensitive comparison because hex can be upper or lower case ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } @@ -529,7 +531,7 @@ TEST(AESTest, RawCipherPlain) { for (auto& item : RawCipherPlainInputData) { std::string expected = PARAM(2); - std::string output = AES::encrypt(PARAM(0), PARAM(1), AES::Encoding::Raw, AES::Encoding::Base16, false); + std::string output = aes.encrypt(PARAM(0), PARAM(1), AES::Encoding::Raw, AES::Encoding::Base16, false); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -543,7 +545,7 @@ TEST(AESTest, RawCipherBase64) { for (auto& item : RawCipherBase64InputData) { std::string expected = PARAM(2); - std::string output = AES::encrypt(PARAM(0), PARAM(1), AES::Encoding::Base64, AES::Encoding::Base16, false); + std::string output = aes.encrypt(PARAM(0), PARAM(1), AES::Encoding::Base64, AES::Encoding::Base16, false); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -619,10 +621,10 @@ TEST(AESTest, CbcCipher) for (auto& item : CbcCipherTestData) { ByteArray expected = PARAM(1); ByteArray input = Base16::fromString(Base16::encode(PARAM(0))); - ByteArray output = AES::encrypt(input, &key, iv, false); + ByteArray output = aes.encrypt(input, &key, iv, false); ASSERT_EQ(expected, output); - ByteArray dec = AES::decrypt(output, &key, iv); + ByteArray dec = aes.decrypt(output, &key, iv); int f = 0; for (auto i = input.begin(); i < input.end(); ++i, ++f) { ASSERT_EQ(*i, dec[f]); @@ -635,7 +637,7 @@ TEST(AESTest, CbcCipher) std::string input = PARAM(0); std::string k = Base16::encode(Base16::toRawString(key)); std::string initVec = Base16::encode(Base16::toRawString(iv)); - std::string output = AES::encrypt(input, k, initVec, + std::string output = aes.encrypt(input, k, initVec, AES::Encoding::Raw, AES::Encoding::Base16, false); ASSERT_STREQ(expected.c_str(), output.c_str()); @@ -666,7 +668,7 @@ TEST(AESTest, HexStringDecipher) { for (auto& item : RawDecipherData) { std::string expected = PARAM(0); - std::string output = AES::decrypt(PARAM(2), PARAM(1), AES::Encoding::Base16, AES::Encoding::Base16); + std::string output = aes.decrypt(PARAM(2), PARAM(1), AES::Encoding::Base16, AES::Encoding::Base16); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -676,7 +678,7 @@ TEST(AESTest, Base64StringDecipher) // input mode = base16, // output mode = base64 std::string expected = "dGhpcyBpcyB0ZXN0Li4uLg=="; // base64("this is test....") - std::string output = AES::decrypt("b92daaae6e57773b10653703af12716f", + std::string output = aes.decrypt("b92daaae6e57773b10653703af12716f", "000102030405060708090a0b0c0d0e0f", AES::Encoding::Base16, AES::Encoding::Base64); @@ -686,7 +688,7 @@ TEST(AESTest, Base64StringDecipher) TEST(AESTest, Base64StringInputDecipher) { std::string expected = "this is test.."; - std::string output = AES::decrypt("Z0BiQ8NcwknqzbGrWBjXqw==", + std::string output = aes.decrypt("Z0BiQ8NcwknqzbGrWBjXqw==", "000102030405060708090a0b0c0d0e0f", AES::Encoding::Base64, AES::Encoding::Raw); @@ -700,12 +702,12 @@ TEST(AESTest, CbcCipherPadding) std::string cipherB64 = "OcTPoBeDqlA/igjnNcl5yw=="; std::string expected = "o1223456789012"; - std::string output = AES::decrypt(cipherB64, key, iv, AES::Encoding::Base64); + std::string output = aes.decrypt(cipherB64, key, iv, AES::Encoding::Base64); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); cipherB64 = "NNH44Ybac3AhcP4+sTq8j4miT04jHtoaj7a/Wv0/TQ8="; expected = "sho4123456789014"; - output = AES::decrypt(cipherB64, key, iv, AES::Encoding::Base64); + output = aes.decrypt(cipherB64, key, iv, AES::Encoding::Base64); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } @@ -714,7 +716,7 @@ TEST(AESTest, EcbDecipher) const std::string key = "F1EF6477CC39E65DE106C33BB0EC651386CD0932A9DE491CF960BC3EB79EBE78"; const std::string cipherHex = "b939427f4231593f5cbf73449439a847726b1898b03db028a6f0824108678f78"; const std::string expected = "this is slightly longer"; - std::string output = AES::decrypt(cipherHex, key); + std::string output = aes.decrypt(cipherHex, key); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } @@ -792,7 +794,7 @@ TEST(AESTest, CbcDecipher) std::string input = Base16::toRawString(PARAM(1)); std::string k = Base16::encode(Base16::toRawString(key)); std::string initVec = Base16::encode(Base16::toRawString(iv)); - std::string output = AES::decrypt(input, k, initVec, + std::string output = aes.decrypt(input, k, initVec, AES::Encoding::Raw, AES::Encoding::Raw); ASSERT_STREQ(expected.c_str(), output.c_str()); @@ -808,7 +810,7 @@ TEST(AESTest, CrossAppsDataTest) // genearted using online tool std::string expected = "WQ73OMIum+OHKGHnAhQKJX1tByfBq4BhSpw2X+SgtjY="; - std::string output = AES::encrypt("test this test this", + std::string output = aes.encrypt("test this test this", "CBD437FA37772C66051A47D72367B38E", iv, AES::Encoding::Raw, @@ -817,7 +819,7 @@ TEST(AESTest, CrossAppsDataTest) ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); std::string nextexp = "test this test this"; - output = AES::decrypt("WQ73OMIum+OHKGHnAhQKJX1tByfBq4BhSpw2X+SgtjY=", + output = aes.decrypt("WQ73OMIum+OHKGHnAhQKJX1tByfBq4BhSpw2X+SgtjY=", key, iv, AES::Encoding::Base64, @@ -827,7 +829,7 @@ TEST(AESTest, CrossAppsDataTest) expected = "EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ=="; - output = AES::encrypt(R"({"_t":1503928197,"logger_id":"default","access_code":"default"})", + output = aes.encrypt(R"({"_t":1503928197,"logger_id":"default","access_code":"default"})", key, iv, AES::Encoding::Raw, @@ -839,7 +841,7 @@ TEST(AESTest, CrossAppsDataTest) // this is real data from residue logging server (development) // expected = R"({"_t":1503928197,"logger_id":"default","access_code":"default"})"; - output = AES::decrypt("EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ==", + output = aes.decrypt("EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ==", key, iv, AES::Encoding::Base64, @@ -850,7 +852,7 @@ TEST(AESTest, CrossAppsDataTest) // generated with ripe // echo test this test this | ripe -e --aes --key CBD437FA37772C66051A47D72367B38E --iv a14c54563269e9e368f56b325f04ff00 expected = "test this test this"; - output = AES::decrypt("WQ73OMIum+OHKGHnAhQKJX1tByfBq4BhSpw2X+SgtjY=", + output = aes.decrypt("WQ73OMIum+OHKGHnAhQKJX1tByfBq4BhSpw2X+SgtjY=", key, iv, AES::Encoding::Base64, @@ -862,7 +864,7 @@ TEST(AESTest, CrossAppsDataTest) // generated with openssl // echo test this test this | openssl enc -aes-128-cbc -K CBD437FA37772C66051A47D72367B38E -iv a14c54563269e9e368f56b325f04ff00 -base64 expected = "test this test this\n"; // openssl adds newline char - output = AES::decrypt("WQ73OMIum+OHKGHnAhQKJdSsXR5NwysOnq+cuf5C6cs=", + output = aes.decrypt("WQ73OMIum+OHKGHnAhQKJdSsXR5NwysOnq+cuf5C6cs=", key, iv, AES::Encoding::Base64, @@ -874,7 +876,7 @@ TEST(AESTest, CrossAppsDataTest) // generated with openssl // echo test this test this | openssl enc -aes-256-cbc -K 163E6AC9A9EB43253AC237D849BDD22C4798393D38FBE322F7E593E318F1AEAF -iv a14c54563269e9e368f56b325f04ff00 -base64 expected = "test this test this\n"; // openssl adds newline char - output = AES::decrypt("vVMWB9aLpcfRgfai7OnCCLI5aAK+kK3Yem/E03uEM+w=", + output = aes.decrypt("vVMWB9aLpcfRgfai7OnCCLI5aAK+kK3Yem/E03uEM+w=", keyBig, iv, AES::Encoding::Base64, @@ -884,6 +886,195 @@ TEST(AESTest, CrossAppsDataTest) ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } +TEST(AESTest, Copy) +{ + AES::Key key = {{ + 0x60, 0x3d, 0xeb, 0x10, + 0x15, 0xca, 0x71, 0xbe, + 0x2b, 0x73, 0xae, 0xf0, + 0x85, 0x7d, 0x77, 0x81, + 0x1f, 0x35, 0x2c, 0x07, + 0x3b, 0x61, 0x08, 0xd7, + 0x2d, 0x98, 0x10, 0xa3, + 0x09, 0x14, 0xdf, 0xf4 + }}; + AES::KeySchedule expectedKeySchedule = {{ + {0, {{ 0x60, 0x3D, 0xEB, 0x10 }}}, + {1, {{ 0x15, 0xCA, 0x71, 0xBE }}}, + {2, {{ 0x2B, 0x73, 0xAE, 0xF0 }}}, + {3, {{ 0x85, 0x7D, 0x77, 0x81 }}}, + {4, {{ 0x1F, 0x35, 0x2C, 0x7 }}}, + {5, {{ 0x3B, 0x61, 0x8, 0xD7 }}}, + {6, {{ 0x2D, 0x98, 0x10, 0xA3 }}}, + {7, {{ 0x9, 0x14, 0xDF, 0xF4 }}}, + {8, {{ 0x9B, 0xA3, 0x54, 0x11 }}}, + {9, {{ 0x8E, 0x69, 0x25, 0xAF }}}, + {10, {{ 0xA5, 0x1A, 0x8B, 0x5F }}}, + {11, {{ 0x20, 0x67, 0xFC, 0xDE }}}, + {12, {{ 0xA8, 0xB0, 0x9C, 0x1A }}}, + {13, {{ 0x93, 0xD1, 0x94, 0xCD }}}, + {14, {{ 0xBE, 0x49, 0x84, 0x6E }}}, + {15, {{ 0xB7, 0x5D, 0x5B, 0x9A }}}, + {16, {{ 0xD5, 0x9A, 0xEC, 0xB8 }}}, + {17, {{ 0x5B, 0xF3, 0xC9, 0x17 }}}, + {18, {{ 0xFE, 0xE9, 0x42, 0x48 }}}, + {19, {{ 0xDE, 0x8E, 0xBE, 0x96 }}}, + {20, {{ 0xB5, 0xA9, 0x32, 0x8A }}}, + {21, {{ 0x26, 0x78, 0xA6, 0x47 }}}, + {22, {{ 0x98, 0x31, 0x22, 0x29 }}}, + {23, {{ 0x2F, 0x6C, 0x79, 0xB3 }}}, + {24, {{ 0x81, 0x2C, 0x81, 0xAD }}}, + {25, {{ 0xDA, 0xDF, 0x48, 0xBA }}}, + {26, {{ 0x24, 0x36, 0xA, 0xF2 }}}, + {27, {{ 0xFA, 0xB8, 0xB4, 0x64 }}}, + {28, {{ 0x98, 0xC5, 0xBF, 0xC9 }}}, + {29, {{ 0xBE, 0xBD, 0x19, 0x8E }}}, + {30, {{ 0x26, 0x8C, 0x3B, 0xA7 }}}, + {31, {{ 0x9, 0xE0, 0x42, 0x14 }}}, + {32, {{ 0x68, 0x0, 0x7B, 0xAC }}}, + {33, {{ 0xB2, 0xDF, 0x33, 0x16 }}}, + {34, {{ 0x96, 0xE9, 0x39, 0xE4 }}}, + {35, {{ 0x6C, 0x51, 0x8D, 0x80 }}}, + {36, {{ 0xC8, 0x14, 0xE2, 0x4 }}}, + {37, {{ 0x76, 0xA9, 0xFB, 0x8A }}}, + {38, {{ 0x50, 0x25, 0xC0, 0x2D }}}, + {39, {{ 0x59, 0xC5, 0x82, 0x39 }}}, + {40, {{ 0xDE, 0x13, 0x69, 0x67 }}}, + {41, {{ 0x6C, 0xCC, 0x5A, 0x71 }}}, + {42, {{ 0xFA, 0x25, 0x63, 0x95 }}}, + {43, {{ 0x96, 0x74, 0xEE, 0x15 }}}, + {44, {{ 0x58, 0x86, 0xCA, 0x5D }}}, + {45, {{ 0x2E, 0x2F, 0x31, 0xD7 }}}, + {46, {{ 0x7E, 0xA, 0xF1, 0xFA }}}, + {47, {{ 0x27, 0xCF, 0x73, 0xC3 }}}, + {48, {{ 0x74, 0x9C, 0x47, 0xAB }}}, + {49, {{ 0x18, 0x50, 0x1D, 0xDA }}}, + {50, {{ 0xE2, 0x75, 0x7E, 0x4F }}}, + {51, {{ 0x74, 0x1, 0x90, 0x5A }}}, + {52, {{ 0xCA, 0xFA, 0xAA, 0xE3 }}}, + {53, {{ 0xE4, 0xD5, 0x9B, 0x34 }}}, + {54, {{ 0x9A, 0xDF, 0x6A, 0xCE }}}, + {55, {{ 0xBD, 0x10, 0x19, 0xD }}}, + {56, {{ 0xFE, 0x48, 0x90, 0xD1 }}}, + {57, {{ 0xE6, 0x18, 0x8D, 0xB }}}, + {58, {{ 0x4, 0x6D, 0xF3, 0x44 }}}, + {59, {{ 0x70, 0x6C, 0x63, 0x1E }}}, + }}; + AES aesSimple(key); + ASSERT_EQ(aesSimple.m_key, key); + ASSERT_EQ(aesSimple.m_keySchedule, expectedKeySchedule); + + AES aesSimple2 = aesSimple; // eq operator + ASSERT_EQ(aesSimple2.m_key, key); + ASSERT_EQ(aesSimple2.m_keySchedule, expectedKeySchedule); + // make sure original values didn't change + ASSERT_EQ(aesSimple.m_key, key); + ASSERT_EQ(aesSimple.m_keySchedule, expectedKeySchedule); + + + AES aesSimple3(aesSimple); // copy constructor + ASSERT_EQ(aesSimple3.m_key, key); + ASSERT_EQ(aesSimple3.m_keySchedule, expectedKeySchedule); + // make sure original values didn't change + ASSERT_EQ(aesSimple.m_key, key); + ASSERT_EQ(aesSimple.m_keySchedule, expectedKeySchedule); + + AES::Key key2 = AES::Key {{ + 0x8e, 0x73, 0xb0, 0xf7, + 0xda, 0x0e, 0x64, 0x52, + 0xc8, 0x10, 0xf3, 0x2b, + 0x80, 0x90, 0x79, 0xe5, + 0x62, 0xf8, 0xea, 0xd2, + 0x52, 0x2c, 0x6b, 0x7b + }}; + + AES::KeySchedule expectedKeySchedule2 = + AES::KeySchedule{{ + {0, {{ 0x8E, 0x73, 0xB0, 0xF7 }}}, + {1, {{ 0xDA, 0xE, 0x64, 0x52 }}}, + {2, {{ 0xC8, 0x10, 0xF3, 0x2B }}}, + {3, {{ 0x80, 0x90, 0x79, 0xE5 }}}, + {4, {{ 0x62, 0xF8, 0xEA, 0xD2 }}}, + {5, {{ 0x52, 0x2C, 0x6B, 0x7B }}}, + {6, {{ 0xFE, 0xC, 0x91, 0xF7 }}}, + {7, {{ 0x24, 0x2, 0xF5, 0xA5 }}}, + {8, {{ 0xEC, 0x12, 0x6, 0x8E }}}, + {9, {{ 0x6C, 0x82, 0x7F, 0x6B }}}, + {10, {{ 0xE, 0x7A, 0x95, 0xB9 }}}, + {11, {{ 0x5C, 0x56, 0xFE, 0xC2 }}}, + {12, {{ 0x4D, 0xB7, 0xB4, 0xBD }}}, + {13, {{ 0x69, 0xB5, 0x41, 0x18 }}}, + {14, {{ 0x85, 0xA7, 0x47, 0x96 }}}, + {15, {{ 0xE9, 0x25, 0x38, 0xFD }}}, + {16, {{ 0xE7, 0x5F, 0xAD, 0x44 }}}, + {17, {{ 0xBB, 0x9, 0x53, 0x86 }}}, + {18, {{ 0x48, 0x5A, 0xF0, 0x57 }}}, + {19, {{ 0x21, 0xEF, 0xB1, 0x4F }}}, + {20, {{ 0xA4, 0x48, 0xF6, 0xD9 }}}, + {21, {{ 0x4D, 0x6D, 0xCE, 0x24 }}}, + {22, {{ 0xAA, 0x32, 0x63, 0x60 }}}, + {23, {{ 0x11, 0x3B, 0x30, 0xE6 }}}, + {24, {{ 0xA2, 0x5E, 0x7E, 0xD5 }}}, + {25, {{ 0x83, 0xB1, 0xCF, 0x9A }}}, + {26, {{ 0x27, 0xF9, 0x39, 0x43 }}}, + {27, {{ 0x6A, 0x94, 0xF7, 0x67 }}}, + {28, {{ 0xC0, 0xA6, 0x94, 0x7 }}}, + {29, {{ 0xD1, 0x9D, 0xA4, 0xE1 }}}, + {30, {{ 0xEC, 0x17, 0x86, 0xEB }}}, + {31, {{ 0x6F, 0xA6, 0x49, 0x71 }}}, + {32, {{ 0x48, 0x5F, 0x70, 0x32 }}}, + {33, {{ 0x22, 0xCB, 0x87, 0x55 }}}, + {34, {{ 0xE2, 0x6D, 0x13, 0x52 }}}, + {35, {{ 0x33, 0xF0, 0xB7, 0xB3 }}}, + {36, {{ 0x40, 0xBE, 0xEB, 0x28 }}}, + {37, {{ 0x2F, 0x18, 0xA2, 0x59 }}}, + {38, {{ 0x67, 0x47, 0xD2, 0x6B }}}, + {39, {{ 0x45, 0x8C, 0x55, 0x3E }}}, + {40, {{ 0xA7, 0xE1, 0x46, 0x6C }}}, + {41, {{ 0x94, 0x11, 0xF1, 0xDF }}}, + {42, {{ 0x82, 0x1F, 0x75, 0xA }}}, + {43, {{ 0xAD, 0x7, 0xD7, 0x53 }}}, + {44, {{ 0xCA, 0x40, 0x5, 0x38 }}}, + {45, {{ 0x8F, 0xCC, 0x50, 0x6 }}}, + {46, {{ 0x28, 0x2D, 0x16, 0x6A }}}, + {47, {{ 0xBC, 0x3C, 0xE7, 0xB5 }}}, + {48, {{ 0xE9, 0x8B, 0xA0, 0x6F }}}, + {49, {{ 0x44, 0x8C, 0x77, 0x3C }}}, + {50, {{ 0x8E, 0xCC, 0x72, 0x4 }}}, + {51, {{ 0x1, 0x0, 0x22, 0x2 }}}, + + }}; + + aesSimple.setKey(key2); + ASSERT_NE(aesSimple.m_keySchedule, expectedKeySchedule); + ASSERT_EQ(aesSimple.m_keySchedule, expectedKeySchedule2); + ASSERT_EQ(aesSimple.m_key, key2); + ASSERT_EQ(aesSimple3.m_key, key); + + std::string output = aesSimple.encr("Test"); + ASSERT_STRCASEEQ("Test", aesSimple.decr(output).c_str()); + + std::string iv = "a14c54563269e9e368f56b325f04ff00"; + output = aesSimple.encr("Test", iv); + ASSERT_STRCASEEQ("Test", aesSimple.decr(output, iv).c_str()); + + ByteArray ivBA = Base16::fromString(iv); + + ByteArray input = {{ + 0x39, 0x25, 0x84, 0x1d, + 0x02, 0xdc, 0x09, 0xfb, + 0xdc, 0x11, 0x85, 0x97, + }}; + + ByteArray outBA = aesSimple.encr(input, ivBA); + ByteArray result = aesSimple.decr(outBA, ivBA); + ASSERT_EQ(input, result); + + outBA = aesSimple.encr(input); + result = aesSimple.decr(outBA); + ASSERT_EQ(input, result); +} + } #endif // AES_TEST_H diff --git a/test/zlib-test.h b/test/zlib-test.h index d770c81..b1c88d4 100644 --- a/test/zlib-test.h +++ b/test/zlib-test.h @@ -15,6 +15,7 @@ namespace mine { static TestData ZLibData = { TestCase("abcd", "eNpLTEpOAQAD2AGL"), + TestCase("abcd", "eNpLTEpOAQAD2AGL"), }; static TestData ZLibDataHex = { From 1f4e75bab94d7a9d6f16341d2155660f696ebe8a Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Wed, 30 Aug 2017 23:16:09 +1000 Subject: [PATCH 129/148] build package --- package/mine.cc | 144 +++++++++++++++++++++++++++++++++++++++++++++--- package/mine.h | 96 +++++++++++++++++++++----------- 2 files changed, 198 insertions(+), 42 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 6ed715c..f905c0e 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -199,6 +199,53 @@ const std::unordered_map> AES::kKeyParams = { { 32, {{ 8, 14 }} } }; +AES::AES(const std::string& key) +{ + setKey(key); +} + +AES::AES(const ByteArray& key) : m_key(key) +{ + setKey(key); +} + +AES::AES(const AES& other) +{ + if (&other != this) { + m_key = other.m_key; + m_keySchedule = other.m_keySchedule; + } +} + +AES::AES(const AES&& other) : + m_key(std::move(other.m_key)), + m_keySchedule(std::move(other.m_keySchedule)) +{ +} + +AES& AES::operator=(const AES& other) +{ + if (&other != this) { + m_key = other.m_key; + m_keySchedule = other.m_keySchedule; + } + return *this; +} + +void AES::setKey(const std::string& key) +{ + setKey(Base16::fromString(key)); +} + +void AES::setKey(const ByteArray& key) +{ + if (key.size() != 16 && key.size() != 24 && key.size() != 32) { + throw std::invalid_argument("Invalid key size. AES can operate on 128-bit, 192-bit and 256-bit keys"); + } + m_key = key; + m_keySchedule = keyExpansion(&m_key); +} + void AES::printBytes(const ByteArray& b) { for (std::size_t i = 1; i <= b.size(); ++i) { @@ -751,7 +798,10 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding const std::size_t inputSize = input.size(); - KeySchedule keySchedule = keyExpansion(key); + if (*key != m_key) { + m_keySchedule = keyExpansion(key); + m_key = *key; + } ByteArray result; @@ -769,7 +819,7 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding std::fill(inputBlock.begin() + j, inputBlock.end(), kBlockSize - (j % kBlockSize)); } - ByteArray outputBlock = encryptSingleBlock(inputBlock.begin() + i, key, &keySchedule); + ByteArray outputBlock = encryptSingleBlock(inputBlock.begin() + i, key, &m_keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); } return result; @@ -785,7 +835,10 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key) throw std::invalid_argument("Invalid AES key size"); } - KeySchedule keySchedule = keyExpansion(key); + if (*key != m_key) { + m_keySchedule = keyExpansion(key); + m_key = *key; + } const std::size_t inputSize = input.size(); ByteArray result; @@ -798,7 +851,7 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key) for (; j < kBlockSize && inputSize > j + i; ++j) { inputBlock[j] = input[j + i]; } - ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); + ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &m_keySchedule); if (i + kBlockSize == inputSize) { // check padding @@ -826,9 +879,11 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bo iv = generateRandomBytes(16); } - // ByteArray inputCopy = input; + if (*key != m_key) { + m_keySchedule = keyExpansion(key); + m_key = *key; + } - KeySchedule keySchedule = keyExpansion(key); const std::size_t inputSize = input.size(); ByteArray result; @@ -852,7 +907,7 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bo } xorWithRange(&inputBlock, nextXorWithBeg, nextXorWithEnd); - ByteArray outputBlock = encryptSingleBlock(inputBlock.begin(), key, &keySchedule); + ByteArray outputBlock = encryptSingleBlock(inputBlock.begin(), key, &m_keySchedule); std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); nextXorWithBeg = result.end() - kBlockSize; nextXorWithEnd = result.end(); @@ -880,7 +935,11 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) throw std::invalid_argument("Ciphertext length is not a multiple of block size"); } - KeySchedule keySchedule = keyExpansion(key); + if (*key != m_key) { + m_keySchedule = keyExpansion(key); + m_key = *key; + } + ByteArray result; ByteArray::const_iterator nextXorWithBeg = iv.begin(); @@ -898,7 +957,7 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) inputBlock[j] = input[j + i]; } - ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &keySchedule); + ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &m_keySchedule); xorWithRange(&outputBlock, nextXorWithBeg, nextXorWithEnd); @@ -966,6 +1025,73 @@ std::string AES::generateRandomKey(const std::size_t len) } +// encryption / decryption with previously provided key + +std::string AES::encr(const std::string& input, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return encrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding, pkcs5Padding); +} + +std::string AES::encr(const std::string& input, std::string& iv, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return encrypt(input, Base16::encode(Base16::toRawString(m_key)), iv, inputEncoding, outputEncoding, pkcs5Padding); +} + +std::string AES::decr(const std::string& input, Encoding inputEncoding, Encoding outputEncoding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return decrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding); +} + +std::string AES::decr(const std::string& input, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return decrypt(input, Base16::encode(Base16::toRawString(m_key)), iv, inputEncoding, outputEncoding); +} + +ByteArray AES::encr(const ByteArray& input, bool pkcs5Padding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return encrypt(input, &m_key, pkcs5Padding); +} + +ByteArray AES::decr(const ByteArray& input) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return decrypt(input, &m_key); +} + +ByteArray AES::encr(const ByteArray& input, ByteArray& iv, bool pkcs5Padding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return encrypt(input, &m_key, iv, pkcs5Padding); +} + +ByteArray AES::decr(const ByteArray& input, ByteArray& iv) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return decrypt(input, &m_key, iv); +} + + bool ZLib::compressFile(const std::string& gzFilename, const std::string& inputFile) { diff --git a/package/mine.h b/package/mine.h index 8e3945d..28d673d 100644 --- a/package/mine.h +++ b/package/mine.h @@ -483,11 +483,6 @@ using ByteArray = std::vector; class AES { public: - AES() = default; - AES(const AES&) = default; - AES& operator=(const AES&) = default; - virtual ~AES() = default; - /// /// \brief Convert mode for various functions /// @@ -502,6 +497,22 @@ class AES { /// using Key = ByteArray; + AES() = default; + AES(const std::string& key); + AES(const ByteArray& key); + AES(const AES&); + AES(const AES&&); + AES& operator=(const AES&); + virtual ~AES() = default; + + void setKey(const std::string& key); + void setKey(const ByteArray& key); + + /// + /// \brief Generates random key of valid length + /// + static std::string generateRandomKey(const std::size_t len); + /// /// \brief Ciphers the input with specified hex key /// \param key Hex key @@ -578,10 +589,24 @@ class AES { /// ByteArray decrypt(const ByteArray& input, const Key* key, ByteArray& iv); - /// - /// \brief Generates random key of valid length - /// - std::string generateRandomKey(const std::size_t len); + + // cipher / decipher interface without keys + + std::string encr(const std::string& input, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + + std::string encr(const std::string& input, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + + std::string decr(const std::string& input, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + + std::string decr(const std::string& input, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + + ByteArray encr(const ByteArray& input, bool pkcs5Padding = true); + + ByteArray decr(const ByteArray& input); + + ByteArray encr(const ByteArray& input, ByteArray& iv, bool pkcs5Padding = true); + + ByteArray decr(const ByteArray& input, ByteArray& iv); private: @@ -663,7 +688,7 @@ class AES { /// [a3] => [a4] /// [a4] [a1] /// - void rotateWord(Word* w); + static void rotateWord(Word* w); /// this function is also specified in FIPS.197 Sec. 5.2: /// SubWord() is a function that takes a four-byte @@ -674,107 +699,107 @@ class AES { /// It's a simple substition with kSbox for corresponding byte /// index /// - void substituteWord(Word* w); + static void substituteWord(Word* w); /// /// \brief Key expansion function as described in FIPS.197 /// - KeySchedule keyExpansion(const Key* key); + static KeySchedule keyExpansion(const Key* key); /// /// \brief Adds round to the state using specified key schedule /// - void addRoundKey(State* state, KeySchedule* keySchedule, int round); + static void addRoundKey(State* state, KeySchedule* keySchedule, int round); /// /// \brief Substitution step for state /// \ref Sec. 5.1.1 /// - void subBytes(State* state); + static void subBytes(State* state); /// /// \brief Shifting rows step for the state /// \ref Sec. 5.1.2 /// - void shiftRows(State* state); + static void shiftRows(State* state); /// /// \ref Sec. 4.2.1 /// - byte xtime(byte x); + static byte xtime(byte x); /// /// \ref Sec. 4.2.1 /// - byte multiply(byte x, byte y); + static byte multiply(byte x, byte y); /// /// \brief Mixing columns for the state /// \ref Sec. 5.1.3 /// - void mixColumns(State* state); + static void mixColumns(State* state); /// /// \brief Transformation in the Inverse Cipher /// that is the reverse of subBytes() /// \ref Sec. 5.3.2 /// - void invSubBytes(State* state); + static void invSubBytes(State* state); /// /// \brief Transformation in the Inverse Cipher that is /// the reverse of shiftRows() /// \ref Sec. 5.3.1 /// - void invShiftRows(State* state); + static void invShiftRows(State* state); /// /// \brief Transformation in the Inverse Cipher /// that is the reverse of mixColumns() /// \ref Sec. 5.3.3 /// - void invMixColumns(State* state); + static void invMixColumns(State* state); /// /// \brief Prints bytes in hex format in 4x4 matrix fashion /// - void printBytes(const ByteArray& b); + static void printBytes(const ByteArray& b); /// /// \brief Prints state for debugging /// - void printState(const State*); + static void printState(const State*); /// /// \brief Initializes the state with input. This function /// also pads the input if needed (i.e, input is not block of 128-bit) /// - void initState(State* state, const ByteArray::const_iterator& begin); + static void initState(State* state, const ByteArray::const_iterator& begin); /// /// \brief Generates random bytes of length /// - ByteArray generateRandomBytes(const std::size_t len); + static ByteArray generateRandomBytes(const std::size_t len); /// /// \brief Creates byte array from input based on input mode /// - ByteArray resolveInputMode(const std::string& input, Encoding inputMode); + static ByteArray resolveInputMode(const std::string& input, Encoding inputMode); /// /// \brief Creates string from byte array based on convert mode /// - std::string resolveOutputMode(const ByteArray& input, Encoding outputMode); + static std::string resolveOutputMode(const ByteArray& input, Encoding outputMode); /// /// \brief Exclusive XOR with arr /// - ByteArray* xorWith(ByteArray* input, const ByteArray*); + static ByteArray* xorWith(ByteArray* input, const ByteArray*); /// /// \brief Exclusive XOR with iter of range size as input /// - ByteArray* xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end); + static ByteArray* xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end); /// /// \brief Raw encryption function - not for public use @@ -786,7 +811,7 @@ class AES { /// \note This does not do any key or input validation /// \return 128-bit cipher text /// - ByteArray encryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); + static ByteArray encryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); /// /// \brief Raw decryption function - not for public use @@ -797,18 +822,22 @@ class AES { /// \param key Byte array of key /// \return 128-bit plain text /// - ByteArray decryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); + static ByteArray decryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); /// /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array /// - ByteArray stateToByteArray(const State* state); + static ByteArray stateToByteArray(const State* state); /// /// \brief Get padding index for stripping the padding (unpadding) /// - std::size_t getPaddingIndex(const ByteArray& byteArr); + static std::size_t getPaddingIndex(const ByteArray& byteArr); + + Key m_key; // to keep track of key differences + KeySchedule m_keySchedule; + // for tests friend class AESTest_RawCipher_Test; friend class AESTest_RawCipherPlain_Test; friend class AESTest_RawCipherBase64_Test; @@ -823,6 +852,7 @@ class AES { friend class AESTest_KeyExpansion_Test; friend class AESTest_AddRoundKey_Test; friend class AESTest_CbcCipher_Test; + friend class AESTest_Copy_Test; }; /// Here onwards start implementation for RSA - this contains From 45d86ed8e2d17d7216b321d2a37357811d4a386b Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Wed, 30 Aug 2017 23:23:12 +1000 Subject: [PATCH 130/148] install package --- CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 679259b..8505f27 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,17 @@ add_definitions (-DMINE_VERSION="${MINE_VERSION}") set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") +set(MINE_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "The directory the headers are installed in") + include_directories (${CMAKE_BINARY_DIR}) include_directories (${CMAKE_SOURCE_DIR}) +install(FILES + package/mine.h + package/mine.cc + DESTINATION "${MINE_INCLUDE_INSTALL_DIR}" + COMPONENT dev) + include(FindPackageHandleStandardArgs) # We need C++11 From b22bdea131a2863ec33be4b10e6b80f0447db5db Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 3 Sep 2017 11:34:36 +1000 Subject: [PATCH 131/148] some refactoring to integrate with main software --- CMakeLists.txt | 20 +- cli/mine.cc | 5 +- package/mine.cc | 1192 -------------------------------- package/mine.h | 1622 -------------------------------------------- src/aes.h | 2 +- src/base64.cc | 39 +- src/base64.h | 51 +- test/base64-test.h | 9 - test/rsa-test.h | 7 + 9 files changed, 73 insertions(+), 2874 deletions(-) delete mode 100644 package/mine.cc delete mode 100644 package/mine.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8505f27..0645831 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,12 +17,6 @@ set(MINE_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "The d include_directories (${CMAKE_BINARY_DIR}) include_directories (${CMAKE_SOURCE_DIR}) -install(FILES - package/mine.h - package/mine.cc - DESTINATION "${MINE_INCLUDE_INSTALL_DIR}" - COMPONENT dev) - include(FindPackageHandleStandardArgs) # We need C++11 @@ -61,7 +55,7 @@ if (APPLE) endif() endif() -list (APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wunused -O3 ") +list (APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -Wunused ") require_cpp11() @@ -91,6 +85,11 @@ set (CMAKE_REQUIRED_QUIET ${CMAKE_REQUIRED_QUIET_SAVE}) file (GLOB_RECURSE all_headers ${CMAKE_SOURCE_DIR}/*.h) add_custom_target (all_placeholder SOURCES ${all_headers}) +#find_package(OPENSSL REQUIRED) +#if (OPENSSL_FOUND) +# include_directories(${OPENSSL_INCLUDE_DIR}) +#endif(OPENSSL_FOUND) + find_package(ZLIB REQUIRED) if (ZLIB_FOUND) include_directories(${ZLIB_INCLUDE_DIRS}) @@ -98,7 +97,11 @@ endif(ZLIB_FOUND) ########################################## CLI Tool ################################### -add_executable (mine-cli cli/mine.cc package/mine.cc) +add_executable (mine-cli cli/mine.cc + src/base64.cc + src/base16.cc + src/aes.cc + src/zlib.cc) set_target_properties (mine-cli PROPERTIES OUTPUT_NAME "mine" @@ -107,6 +110,7 @@ set_target_properties (mine-cli PROPERTIES target_link_libraries(mine-cli ${ZLIB_LIBRARIES} + #${OPENSSL_CRYPTO_LIBRARY} ) install (TARGETS mine-cli DESTINATION bin) diff --git a/cli/mine.cc b/cli/mine.cc index 2248eae..92c4ffc 100644 --- a/cli/mine.cc +++ b/cli/mine.cc @@ -23,7 +23,10 @@ #include #include #include -#include "package/mine.h" +#include "src/base16.h" +#include "src/base64.h" +#include "src/aes.h" +#include "src/zlib.h" using namespace mine; diff --git a/package/mine.cc b/package/mine.cc deleted file mode 100644 index f905c0e..0000000 --- a/package/mine.cc +++ /dev/null @@ -1,1192 +0,0 @@ -// -// Bismillah ar-Rahmaan ar-Raheem -// -// Mine (Unreleased) -// Single header minimal cryptography library -// -// Copyright (c) 2017 Muflihun Labs -// -// This library is released under the Apache 2.0 license -// https://github.com/muflihun/mine/blob/master/LICENSE -// -// https://github.com/muflihun/mine -// https://muflihun.github.io/mine -// https://muflihun.com -// -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "mine.h" - -using namespace mine; - - -const std::string Base16::kValidChars = "0123456789ABCDEF"; - -const std::unordered_map Base16::kDecodeMap = { - {0x30, 0x00}, {0x31, 0x01}, {0x32, 0x02}, {0x33, 0x03}, - {0x34, 0x04}, {0x35, 0x05}, {0x36, 0x06}, {0x37, 0x07}, - {0x38, 0x08}, {0x39, 0x09}, {0x41, 0x0A}, {0x42, 0x0B}, - {0x43, 0x0C}, {0x44, 0x0D}, {0x45, 0x0E}, {0x46, 0x0F} -}; - -ByteArray Base16::fromString(const std::string& hex) -{ - if (hex.size() % 2 != 0) { - throw std::invalid_argument("Invalid base-16 encoding"); - } - - ByteArray byteArr; - for (std::size_t i = 0; i < hex.length(); i += 2) { - byteArr.push_back(encode(hex.substr(i, 2).c_str())); - } - return byteArr; -} - -std::string Base16::toRawString(const ByteArray& input) -{ - std::ostringstream ss; - std::copy(input.begin(), input.end(), std::ostream_iterator(ss)); - return ss.str(); -} - -void Base16::decode(char a, char b, std::ostringstream& ss) -{ - try { - ss << static_cast((kDecodeMap.at(a & 0xff) << 4) | kDecodeMap.at(b & 0xff)); - } catch (const std::exception& e) { - throw std::invalid_argument("Invalid base-16 encoding: " + std::string(e.what())); - } -} - - -const std::string Base64::kValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - -const std::unordered_map Base64::kDecodeMap = { - {0x41, 0x00}, {0x42, 0x01}, {0x43, 0x02}, {0x44, 0x03}, - {0x45, 0x04}, {0x46, 0x05}, {0x47, 0x06}, {0x48, 0x07}, - {0x49, 0x08}, {0x4A, 0x09}, {0x4B, 0x0A}, {0x4C, 0x0B}, - {0x4D, 0x0C}, {0x4E, 0x0D}, {0x4F, 0x0E}, {0x50, 0x0F}, - {0x51, 0x10}, {0x52, 0x11}, {0x53, 0x12}, {0x54, 0x13}, - {0x55, 0x14}, {0x56, 0x15}, {0x57, 0x16}, {0x58, 0x17}, - {0x59, 0x18}, {0x5A, 0x19}, {0x61, 0x1A}, {0x62, 0x1B}, - {0x63, 0x1C}, {0x64, 0x1D}, {0x65, 0x1E}, {0x66, 0x1F}, - {0x67, 0x20}, {0x68, 0x21}, {0x69, 0x22}, {0x6A, 0x23}, - {0x6B, 0x24}, {0x6C, 0x25}, {0x6D, 0x26}, {0x6E, 0x27}, - {0x6F, 0x28}, {0x70, 0x29}, {0x71, 0x2A}, {0x72, 0x2B}, - {0x73, 0x2C}, {0x74, 0x2D}, {0x75, 0x2E}, {0x76, 0x2F}, - {0x77, 0x30}, {0x78, 0x31}, {0x79, 0x32}, {0x7A, 0x33}, - {0x30, 0x34}, {0x31, 0x35}, {0x32, 0x36}, {0x33, 0x37}, - {0x34, 0x38}, {0x35, 0x39}, {0x36, 0x3A}, {0x37, 0x3B}, - {0x38, 0x3C}, {0x39, 0x3D}, {0x2B, 0x3E}, {0x2F, 0x3F}, - {0x3D, 0x40} -}; - -std::size_t Base64::countChars(const std::string& str) noexcept -{ - std::size_t result = 0UL; - for (auto it = str.begin(); it <= str.end();) { - int c = *it & 0xff; - int charCount = 0; - if (c == 0x0) { - // \0 - ++it; // we increment iter manually - } else if (c <= 0x7f) { - charCount = 1; - } else if (c <= 0x7ff) { - charCount = 2; - } else if (c <= 0xffff) { - charCount = 3; - } else { - charCount = 4; - } - result += charCount; - it += charCount; - } - return result; -} - -#define MINE_PROFILING 0 - -#if MINE_PROFILING -# include -# include - -template -void endProfiling(T& started, const std::string& name) { - auto ended = std::chrono::steady_clock::now(); - auto diff = std::chrono::duration_cast(ended - started); - std::cout << (diff.count()) << " ns for " << name << std::endl; -} - -#endif - - -const byte AES::kSBox[256] = { - 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, - 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, - 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, - 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, - 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, - 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, - 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, - 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, - 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, - 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, - 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, - 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, - 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, - 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, - 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, - 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 -}; - -const byte AES::kSBoxInverse[256] = { - 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, - 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, - 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, - 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, - 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, - 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, - 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, - 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, - 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, - 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, - 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, - 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, - 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, - 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, - 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, - 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d -}; - -const uint8_t AES::kRoundConstant[10] = { - 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 -}; - -const byte AES::kRandomBytesList[256] = { - 0x6f, 0x48, 0x15, 0x46, 0x77, 0x58, 0x05, 0x0b, 0x02, 0x6f, 0x20, 0x66, 0x18, 0x5a, 0x17, 0x27, - 0x45, 0x6c, 0x0f, 0x33, 0x08, 0x58, 0x2a, 0x54, 0x75, 0x53, 0x1e, 0x2a, 0x09, 0x13, 0x0f, 0x20, - 0x49, 0x49, 0x4b, 0x18, 0x3c, 0x1f, 0x06, 0x0e, 0x58, 0x52, 0x7c, 0x25, 0x58, 0x7d, 0x33, 0x27, - 0x14, 0x47, 0x66, 0x3f, 0x68, 0x66, 0x49, 0x27, 0x77, 0x10, 0x33, 0x26, 0x6c, 0x34, 0x10, 0x4e, - 0x10, 0x48, 0x07, 0x7c, 0x11, 0x06, 0x60, 0x61, 0x28, 0x29, 0x47, 0x5b, 0x3b, 0x16, 0x75, 0x74, - 0x14, 0x4a, 0x4a, 0x78, 0x21, 0x35, 0x77, 0x50, 0x17, 0x74, 0x3c, 0x26, 0x05, 0x31, 0x65, 0x36, - 0x48, 0x3c, 0x29, 0x4c, 0x1e, 0x78, 0x5e, 0x51, 0x16, 0x7f, 0x0b, 0x6d, 0x14, 0x41, 0x6e, 0x15, - 0x35, 0x7a, 0x4c, 0x59, 0x52, 0x5e, 0x0c, 0x22, 0x29, 0x7d, 0x6f, 0x0b, 0x73, 0x55, 0x0c, 0x44, - 0x3d, 0x70, 0x15, 0x33, 0x71, 0x23, 0x34, 0x77, 0x39, 0x68, 0x04, 0x6d, 0x2b, 0x4b, 0x52, 0x4d, - 0x30, 0x03, 0x38, 0x09, 0x5b, 0x58, 0x09, 0x5f, 0x4b, 0x54, 0x5d, 0x53, 0x35, 0x6b, 0x48, 0x43, - 0x3e, 0x58, 0x7d, 0x48, 0x7e, 0x6d, 0x71, 0x28, 0x14, 0x0e, 0x41, 0x58, 0x20, 0x7b, 0x48, 0x14, - 0x1f, 0x68, 0x07, 0x6d, 0x62, 0x4a, 0x72, 0x34, 0x7d, 0x66, 0x3e, 0x42, 0x79, 0x47, 0x36, 0x11, - 0x37, 0x08, 0x1f, 0x0a, 0x08, 0x2f, 0x66, 0x11, 0x2b, 0x0e, 0x03, 0x33, 0x14, 0x66, 0x25, 0x3e, - 0x08, 0x6f, 0x6e, 0x69, 0x71, 0x1e, 0x1c, 0x02, 0x09, 0x0a, 0x45, 0x24, 0x73, 0x58, 0x4b, 0x43, - 0x5a, 0x53, 0x4f, 0x0e, 0x39, 0x71, 0x13, 0x0c, 0x02, 0x46, 0x66, 0x2a, 0x56, 0x4c, 0x2b, 0x37, - 0x34, 0x45, 0x6e, 0x01, 0x4d, 0x12, 0x35, 0x4a, 0x29, 0x66, 0x30, 0x5b, 0x31, 0x4f, 0x6e, 0x3d -}; - -const std::unordered_map> AES::kKeyParams = { - { 16, {{ 4, 10 }} }, - { 24, {{ 6, 12 }} }, - { 32, {{ 8, 14 }} } -}; - -AES::AES(const std::string& key) -{ - setKey(key); -} - -AES::AES(const ByteArray& key) : m_key(key) -{ - setKey(key); -} - -AES::AES(const AES& other) -{ - if (&other != this) { - m_key = other.m_key; - m_keySchedule = other.m_keySchedule; - } -} - -AES::AES(const AES&& other) : - m_key(std::move(other.m_key)), - m_keySchedule(std::move(other.m_keySchedule)) -{ -} - -AES& AES::operator=(const AES& other) -{ - if (&other != this) { - m_key = other.m_key; - m_keySchedule = other.m_keySchedule; - } - return *this; -} - -void AES::setKey(const std::string& key) -{ - setKey(Base16::fromString(key)); -} - -void AES::setKey(const ByteArray& key) -{ - if (key.size() != 16 && key.size() != 24 && key.size() != 32) { - throw std::invalid_argument("Invalid key size. AES can operate on 128-bit, 192-bit and 256-bit keys"); - } - m_key = key; - m_keySchedule = keyExpansion(&m_key); -} - -void AES::printBytes(const ByteArray& b) -{ - for (std::size_t i = 1; i <= b.size(); ++i) { - std::cout << "0x" << (b[i - 1] < 10 ? "0" : "") << Base16::encode(b[i - 1]) << " "; - if (i % 4 == 0) { - std::cout << std::endl; - } - } - std::cout << std::endl << "------" << std::endl; -} - -void AES::printState(const State* state) -{ - for (std::size_t i = 0; i < kNb; ++i) { - for (std::size_t j = 0; j < kNb; ++j) { - byte b = (*state)[j][i]; - std::cout << "0x" << (b < 10 ? "0" : "") << Base16::encode(b) << " "; - } - std::cout << std::endl; - } - std::cout << std::endl; -} - -void AES::rotateWord(Word* w) { - byte t = (*w)[0]; - (*w)[0] = (*w)[1]; - (*w)[1] = (*w)[2]; - (*w)[2] = (*w)[3]; - (*w)[3] = t; -} - -void AES::substituteWord(Word* w) { - for (uint8_t i = 0; i < 4; ++i) { - (*w)[i] = kSBox[(*w)[i]]; - } -} - -AES::KeySchedule AES::keyExpansion(const Key* key) -{ - -#if MINE_PROFILING - auto started = std::chrono::steady_clock::now(); -#endif - - std::size_t keySize = key->size(); - - if (keySize != 16 && keySize != 24 && keySize != 32) { - throw std::invalid_argument("Invalid AES key size"); - } - - uint8_t Nk = kKeyParams.at(keySize)[0], - Nr = kKeyParams.at(keySize)[1]; - - KeySchedule words;//(kNb * (Nr+1)); - - uint8_t i = 0; - // copy main key as is for the first round - for (; i < Nk; ++i) { - words[i] = {{ - (*key)[(i * 4) + 0], - (*key)[(i * 4) + 1], - (*key)[(i * 4) + 2], - (*key)[(i * 4) + 3], - }}; - } - - for (; i < kNb * (Nr + 1); ++i) { - Word temp = words[i - 1]; - - if (i % Nk == 0) { - rotateWord(&temp); - substituteWord(&temp); - // xor with rcon - temp[0] ^= kRoundConstant[(i / Nk) - 1]; - } else if (Nk == 8 && i % Nk == 4) { - // See note for 256-bit keys on Sec. 5.2 on FIPS.197 - substituteWord(&temp); - } - - // xor previous column of new key with corresponding column of - // previous round key - Word correspondingWord = words[i - Nk]; - byte b0 = correspondingWord[0] ^ temp[0]; - byte b1 = correspondingWord[1] ^ temp[1]; - byte b2 = correspondingWord[2] ^ temp[2]; - byte b3 = correspondingWord[3] ^ temp[3]; - - words[i] = {{ b0, b1, b2, b3 }}; - } - -#if MINE_PROFILING - endProfiling(started, "keyExpansion"); -#endif - - return words; -} - -/// -/// Adding round key is simply a xor operation on -/// corresponding key for the round -/// which is generated during key expansion -/// -/// Let's say we have state and a key -/// -/// [ df c3 e2 9c ] [ ef d4 49 11 ] -/// | 0f ad 1f ca | | 1f ad ac fa | -/// | 0c 9d 8d fa | ^ | cc 9e 15 dd | -/// [ fe ef cc b2 ] [ fe ea 02 dc ] -/// -/// -/// [ df^ef c3^d4 e2^49 9c^11 ] -/// | 0f^1f ad^ad 1f^ac ca^fa | -/// | 0c^cc 9d^9e 8d^15 fa^dd | -/// [ fe^fe ef^ea cc^02 b2^dc ] -/// -void AES::addRoundKey(State* state, KeySchedule* keySchedule, int round) -{ -#if MINE_PROFILING - auto started = std::chrono::steady_clock::now(); -#endif - const int iR = (round * kNb); - for (std::size_t i = 0; i < kNb; ++i) { - std::size_t iR2 = iR + i; - for (std::size_t j = 0; j < kNb; ++j) { -#if MINE_PROFILING - auto started2 = std::chrono::steady_clock::now(); - std::cout << ((*state)[i][j] & 0xff) << " ^= " << ((*keySchedule)[iR2][j] & 0xff); -#endif - (*state)[i][j] ^= (*keySchedule)[iR2][j]; -#if MINE_PROFILING - std::cout << " = " << ((*state)[i][j] & 0xff) << std::endl; - endProfiling(started2, "add single round"); -#endif - } - } -#if MINE_PROFILING - endProfiling(started, "addRoundKey"); -#endif -} - -/// -/// Simple substition for the byte -/// from sbox - i.e, for 0x04 we will replace with the -/// byte at index 0x04 => 0xf2 -/// -void AES::subBytes(State* state) -{ -#if MINE_PROFILING - auto started = std::chrono::steady_clock::now(); -#endif - for (std::size_t i = 0; i < kNb; ++i) { - for (std::size_t j = 0; j < kNb; ++j) { - (*state)[i][j] = kSBox[(*state)[i][j]]; - } - } - -#if MINE_PROFILING - endProfiling(started, "subBytes"); -#endif -} - -/// -/// A simple substition of bytes using kSBoxInverse -/// -void AES::invSubBytes(State* state) -{ -#if MINE_PROFILING - auto started = std::chrono::steady_clock::now(); -#endif - for (std::size_t i = 0; i < kNb; ++i) { - for (std::size_t j = 0; j < kNb; ++j) { - (*state)[i][j] = kSBoxInverse[(*state)[i][j]]; - } - } -#if MINE_PROFILING - endProfiling(started, "invSubBytes"); -#endif -} - -/// -/// Shifting rows is beautifully explained by diagram -/// that helped in implementation as well -/// -/// Let's say we have state -/// -/// [ df c3 e2 9c ] -/// | 0f ad 1f ca | -/// | 0c 9d 8d fa | -/// [ fe ef cc b2 ] -/// -/// shifting means -/// -/// [ df c3 e2 9c ] -/// 0f | ad 1f ca | -/// 0c 9d | 8d fa | -/// fe ef cc [ b2 ] -/// -/// and filling the spaces with shifted rows -/// -/// [ df c3 e2 9c ] -/// | ad 1f ca 0f | -/// | 8d fa 0c 9d | -/// [ b2 fe ef cc ] -/// -void AES::shiftRows(State *state) -{ -#if MINE_PROFILING - auto started = std::chrono::steady_clock::now(); -#endif - // row 1 - std::swap((*state)[0][1], (*state)[3][1]); - std::swap((*state)[0][1], (*state)[1][1]); - std::swap((*state)[1][1], (*state)[2][1]); - - // row 2 - std::swap((*state)[0][2], (*state)[2][2]); - std::swap((*state)[1][2], (*state)[3][2]); - - // row 3 - std::swap((*state)[0][3], (*state)[1][3]); - std::swap((*state)[2][3], (*state)[3][3]); - std::swap((*state)[0][3], (*state)[2][3]); - -#if MINE_PROFILING - endProfiling(started, "shiftRows"); -#endif -} - -/// -/// This is reverse of shift rows operation -/// -/// Let's say we have state -/// -/// [ df c3 e2 9c ] -/// | ad 1f ca 0f | -/// | 8d fa 0c 9d | -/// [ b2 fe ef cc ] -/// -/// shifting means -/// -/// [ df c3 e2 9c ] -/// | ad 1f ca | 0f -/// | 8d fa | 0c 9d -/// [ b2 | fe ef cc -/// -/// and filling the spaces with shifted rows -/// -/// [ df c3 e2 9c ] -/// | 0f ad 1f ca | -/// | 0c 9d 8d fa | -/// [ fe ef cc b2 ] -/// -void AES::invShiftRows(State *state) -{ -#if MINE_PROFILING - auto started = std::chrono::steady_clock::now(); -#endif - // row 1 - std::swap((*state)[0][1], (*state)[1][1]); - std::swap((*state)[0][1], (*state)[2][1]); - std::swap((*state)[0][1], (*state)[3][1]); - - // row 2 - std::swap((*state)[0][2], (*state)[2][2]); - std::swap((*state)[1][2], (*state)[3][2]); - - // row 3 - std::swap((*state)[0][3], (*state)[3][3]); - std::swap((*state)[0][3], (*state)[2][3]); - std::swap((*state)[0][3], (*state)[1][3]); -#if MINE_PROFILING - endProfiling(started, "intShiftRows"); -#endif -} - -/// -/// Finds the product of {02} and the argument to -/// xtime modulo {1b} -/// -byte AES::xtime(byte x) -{ - return ((x << 1) ^ (((x >> 7) & 1) * 0x11b)); -} - -/// -/// Multiplies numbers in the GF(2^8) field -/// -byte AES::multiply(byte x, byte y) -{ - return (((y & 0x01) * x) ^ - ((y >> 1 & 0x01) * xtime(x)) ^ - ((y >> 2 & 0x01) * xtime(xtime(x))) ^ - ((y >> 3 & 0x01) * xtime(xtime(xtime(x)))) ^ - ((y >> 4 & 0x01) * xtime(xtime(xtime(xtime(x)))))); -} - -/// -/// multiplies in GF(2^8) field selected column from state -/// with constant matrix defined by publication -/// -/// [ 02 03 01 01 ] -/// | 01 02 03 01 | -/// | 01 01 02 03 | -/// [ 03 01 01 02 ] -/// -void AES::mixColumns(State* state) -{ -#if MINE_PROFILING - auto started = std::chrono::steady_clock::now(); -#endif - for (int col = 0; col < 4; ++col) { - Word column = (*state)[col]; - // let's take example from publication, col: [212, 191, 93, 48] - // t == 6 - byte t = column[0] ^ column[1] ^ column[2] ^ column[3]; - // see Sec. 4.2.1 and Sec. 5.1.3 for more details - (*state)[col][0] ^= xtime(column[0] ^ column[1]) ^ t; - (*state)[col][1] ^= xtime(column[1] ^ column[2]) ^ t; - (*state)[col][2] ^= xtime(column[2] ^ column[3]) ^ t; - (*state)[col][3] ^= xtime(column[3] ^ column[0]) ^ t; - } -#if MINE_PROFILING - endProfiling(started, "mixColumns"); -#endif -} - -/// -/// Inverse multiplication with inverse matrix defined on Sec. 5.3.3 -/// -/// [ 0e 0b 0d 09 ] -/// | 09 0e 0b 0d | -/// | 0d 09 0e 0b | -/// [ 0b 0d 09 0e ] -/// -void AES::invMixColumns(State* state) -{ -#if MINE_PROFILING - auto started = std::chrono::steady_clock::now(); -#endif - for (int col = 0; col < 4; ++col) { - Word column = (*state)[col]; - // see Sec. 4.2.1 and Sec. 5.3.3 for more details - (*state)[col][0] = multiply(column[0], 0x0e) ^ multiply(column[1], 0x0b) ^ multiply(column[2], 0x0d) ^ multiply(column[3], 0x09); - (*state)[col][1] = multiply(column[0], 0x09) ^ multiply(column[1], 0x0e) ^ multiply(column[2], 0x0b) ^ multiply(column[3], 0x0d); - (*state)[col][2] = multiply(column[0], 0x0d) ^ multiply(column[1], 0x09) ^ multiply(column[2], 0x0e) ^ multiply(column[3], 0x0b); - (*state)[col][3] = multiply(column[0], 0x0b) ^ multiply(column[1], 0x0d) ^ multiply(column[2], 0x09) ^ multiply(column[3], 0x0e); - } -#if MINE_PROFILING - endProfiling(started, "invMixColumns"); -#endif -} - -void AES::initState(State* state, const ByteArray::const_iterator& begin) -{ - // Pad the input if needed - // 29/08 removed this check because it's not needed as - // this function is only called from private interface - // this removal helped made input pass by ref - /*if (input.size() < kBlockSize) { - std::fill_n(input.end(), kBlockSize - input.size(), 0); - }*/ - - // assign it to state for processing - for (std::size_t i = 0; i < kNb; ++i) { - for (std::size_t j = 0; j < kNb; ++j) { - (*state)[i][j] = begin[(kNb * i) + j]; - } - } -} - -ByteArray AES::stateToByteArray(const State *state) -{ - -#if MINE_PROFILING - auto started = std::chrono::steady_clock::now(); -#endif - ByteArray result(kBlockSize); - int k = 0; - for (std::size_t i = 0; i < kNb; ++i) { - for (std::size_t j = 0; j < kNb; ++j) { - result[k++] = (*state)[i][j]; - } - } -#if MINE_PROFILING - endProfiling(started, "stateToByteArray"); -#endif - - return result; -} - -ByteArray AES::generateRandomBytes(const std::size_t len) -{ - ByteArray result(len, 'x'); - std::random_device rd; - std::mt19937 rng(rd()); - std::uniform_int_distribution uni(0, kRandomBytesCount - 1); - std::generate(result.begin(), result.end(), [&] { - return kRandomBytesList[uni(rng)]; - }); - return result; -} - -ByteArray* AES::xorWith(ByteArray* input, const ByteArray* arr) -{ - for (std::size_t i = 0; i < kBlockSize; ++i) { - (*input)[i] ^= (*arr)[i]; - } - return input; -} - -ByteArray* AES::xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end) -{ - int i = 0; - for (auto iter = begin; iter < end; ++iter, ++i) { - (*input)[i] ^= *iter; - } - return input; -} - -ByteArray AES::encryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) -{ - - if (key == nullptr || keySchedule == nullptr) { - throw std::invalid_argument("AES raw encryption requires key"); - } - - State state; - initState(&state, range); - - const uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; - - int round = 0; - - // initial round - addRoundKey(&state, keySchedule, round++); - - -#if MINE_PROFILING - auto started = std::chrono::steady_clock::now(); -#endif - // intermediate round - for (; round < kTotalRounds; ++round) { - subBytes(&state); - shiftRows(&state); - mixColumns(&state); - addRoundKey(&state, keySchedule, round); - } - -#if MINE_PROFILING - endProfiling(started, "rawCipher (intermediate rounds)"); -#endif - - // final round - subBytes(&state); - shiftRows(&state); - addRoundKey(&state, keySchedule, round++); - - return stateToByteArray(&state); - -} - -ByteArray AES::decryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) -{ - - if (key == nullptr || keySchedule == nullptr) { - throw std::invalid_argument("AES raw decryption requires key"); - } - - State state; - initState(&state, range); - - const uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; - - int round = kTotalRounds; - - // initial round - addRoundKey(&state, keySchedule, round--); - - // intermediate round - for (; round > 0; --round) { - invShiftRows(&state); - invSubBytes(&state); - addRoundKey(&state, keySchedule, round); - invMixColumns(&state); - } - - // final round - invShiftRows(&state); - invSubBytes(&state); - addRoundKey(&state, keySchedule, round); - - return stateToByteArray(&state); -} - -ByteArray AES::resolveInputMode(const std::string& input, Encoding inputMode) -{ - if (inputMode == Encoding::Raw) { - return Base16::fromString(Base16::encode(input)); - } else if (inputMode == Encoding::Base16) { - return Base16::fromString(input); - } - // base64 - return Base16::fromString(Base16::encode(Base64::decode(input))); -} - -std::string AES::resolveOutputMode(const ByteArray& input, Encoding outputMode) -{ - if (outputMode == Encoding::Raw) { - return Base16::toRawString(input); - } else if (outputMode == Encoding::Base16) { - return Base16::encode(input.begin(), input.end()); - } - // base64 - return Base64::encode(input.begin(), input.end()); -} - -std::size_t AES::getPaddingIndex(const ByteArray& byteArr) -{ - char lastChar = byteArr[kBlockSize - 1]; - int c = lastChar & 0xff; - if (c > 0 && c <= kBlockSize) { - bool validPadding = true; - for (int chkIdx = kBlockSize - c; chkIdx < kBlockSize; ++chkIdx) { - if ((byteArr[chkIdx] & 0xff) != c) { - // with openssl we found padding - validPadding = false; - break; - } - } - if (validPadding) { - return kBlockSize - c; - } else { - throw std::runtime_error("Incorrect padding"); - } - } - return kBlockSize; -} - -// public - -ByteArray AES::encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding) -{ - - std::size_t keySize = key->size(); - - // key size validation - if (keySize != 16 && keySize != 24 && keySize != 32) { - throw std::invalid_argument("Invalid AES key size"); - } - - const std::size_t inputSize = input.size(); - - if (*key != m_key) { - m_keySchedule = keyExpansion(key); - m_key = *key; - } - - ByteArray result; - - for (std::size_t i = 0; i < inputSize; i += kBlockSize) { - ByteArray inputBlock(kBlockSize, 0); - - std::size_t j = 0; - // don't use copy_n as we are setting the values - for (; j < kBlockSize && inputSize > j + i; ++j) { - inputBlock[j] = input[j + i]; - } - - if (pkcs5Padding && j != kBlockSize) { - // PKCS#5 padding - std::fill(inputBlock.begin() + j, inputBlock.end(), kBlockSize - (j % kBlockSize)); - } - - ByteArray outputBlock = encryptSingleBlock(inputBlock.begin() + i, key, &m_keySchedule); - std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); - } - return result; -} - -ByteArray AES::decrypt(const ByteArray& input, const Key* key) -{ - - std::size_t keySize = key->size(); - - // key size validation - if (keySize != 16 && keySize != 24 && keySize != 32) { - throw std::invalid_argument("Invalid AES key size"); - } - - if (*key != m_key) { - m_keySchedule = keyExpansion(key); - m_key = *key; - } - - const std::size_t inputSize = input.size(); - ByteArray result; - - for (std::size_t i = 0; i < inputSize; i += kBlockSize) { - ByteArray inputBlock(kBlockSize, 0); - - std::size_t j = 0; - // don't use copy_n here as we are setting the values - for (; j < kBlockSize && inputSize > j + i; ++j) { - inputBlock[j] = input[j + i]; - } - ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &m_keySchedule); - - if (i + kBlockSize == inputSize) { - // check padding - j = getPaddingIndex(outputBlock); - } - std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); - } - return result; -} - -ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bool pkcs5Padding) -{ - - std::size_t keySize = key->size(); - - // key size validation - if (keySize != 16 && keySize != 24 && keySize != 32) { - throw std::invalid_argument("Invalid AES key size"); - } - - if (!iv.empty() && iv.size() != 16) { - throw std::invalid_argument("Invalid IV, it should be same as block size"); - } else if (iv.empty()) { - // generate IV - iv = generateRandomBytes(16); - } - - if (*key != m_key) { - m_keySchedule = keyExpansion(key); - m_key = *key; - } - - const std::size_t inputSize = input.size(); - - ByteArray result; - ByteArray::const_iterator nextXorWithBeg = iv.begin(); - ByteArray::const_iterator nextXorWithEnd = iv.end(); - -#if MINE_PROFILING - auto started = std::chrono::steady_clock::now(); -#endif - for (std::size_t i = 0; i < inputSize; i += kBlockSize) { - ByteArray inputBlock(kBlockSize, 0); - - std::size_t j = 0; - // don't use copy_n as we are setting the values - for (; j < kBlockSize && inputSize > j + i; ++j) { - inputBlock[j] = input[j + i]; - } - if (pkcs5Padding && j != kBlockSize) { - // PKCS#5 padding - std::fill(inputBlock.begin() + j, inputBlock.end(), kBlockSize - (j % kBlockSize)); - } - xorWithRange(&inputBlock, nextXorWithBeg, nextXorWithEnd); - - ByteArray outputBlock = encryptSingleBlock(inputBlock.begin(), key, &m_keySchedule); - std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); - nextXorWithBeg = result.end() - kBlockSize; - nextXorWithEnd = result.end(); - } -#if MINE_PROFILING - endProfiling(started, "block encryption"); -#endif - - return result; -} - -ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) -{ - - std::size_t keySize = key->size(); - - // key size validation - if (keySize != 16 && keySize != 24 && keySize != 32) { - throw std::invalid_argument("Invalid AES key size"); - } - - const std::size_t inputSize = input.size(); - - if (inputSize % kBlockSize != 0) { - throw std::invalid_argument("Ciphertext length is not a multiple of block size"); - } - - if (*key != m_key) { - m_keySchedule = keyExpansion(key); - m_key = *key; - } - - ByteArray result; - - ByteArray::const_iterator nextXorWithBeg = iv.begin(); - ByteArray::const_iterator nextXorWithEnd = iv.end(); - -#if MINE_PROFILING - auto started = std::chrono::steady_clock::now(); -#endif - for (std::size_t i = 0; i < inputSize; i += kBlockSize) { - ByteArray inputBlock(kBlockSize, 0); - - std::size_t j = 0; - // don't use copy_n as we are setting the values - for (; j < kBlockSize && inputSize > j + i; ++j) { - inputBlock[j] = input[j + i]; - } - - ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &m_keySchedule); - - xorWithRange(&outputBlock, nextXorWithBeg, nextXorWithEnd); - - if (i + kBlockSize == inputSize) { - // check padding - j = getPaddingIndex(outputBlock); - } else { - nextXorWithBeg = input.begin() + i; - nextXorWithEnd = input.begin() + i + kBlockSize; - } - - std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); - } -#if MINE_PROFILING - endProfiling(started, "block decryption"); -#endif - return result; -} - -std::string AES::encrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) -{ - Key keyArr = Base16::fromString(key); - ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray result = encrypt(inp, &keyArr, pkcs5Padding); - return resolveOutputMode(result, outputEncoding); -} - -std::string AES::encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) -{ - Key keyArr = Base16::fromString(key); - ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray ivec = Base16::fromString(iv); - bool ivecGenerated = iv.empty(); - ByteArray result = encrypt(inp, &keyArr, ivec, pkcs5Padding); - if (ivecGenerated) { - iv = Base16::encode(ivec.begin(), ivec.end()); - } - return resolveOutputMode(result, outputEncoding); -} - -std::string AES::decrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) -{ - Key keyArr = Base16::fromString(key); - ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray result = decrypt(inp, &keyArr); - return resolveOutputMode(result, outputEncoding); -} - -std::string AES::decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) -{ - Key keyArr = Base16::fromString(key); - ByteArray inp = resolveInputMode(input, inputEncoding); - ByteArray ivec = Base16::fromString(iv); - ByteArray result = decrypt(inp, &keyArr, ivec); - return resolveOutputMode(result, outputEncoding); -} - -std::string AES::generateRandomKey(const std::size_t len) -{ - if (len != 128 && len != 192 && len != 256) { - throw std::invalid_argument("Please choose valid key length of 128, 192 or 256 bits"); - } - ByteArray bytes = generateRandomBytes(len / 8); - return Base16::encode(bytes.begin(), bytes.end()); -} - - -// encryption / decryption with previously provided key - -std::string AES::encr(const std::string& input, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) -{ - if (m_key.empty()) { - throw std::runtime_error("Key not set"); - } - return encrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding, pkcs5Padding); -} - -std::string AES::encr(const std::string& input, std::string& iv, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) -{ - if (m_key.empty()) { - throw std::runtime_error("Key not set"); - } - return encrypt(input, Base16::encode(Base16::toRawString(m_key)), iv, inputEncoding, outputEncoding, pkcs5Padding); -} - -std::string AES::decr(const std::string& input, Encoding inputEncoding, Encoding outputEncoding) -{ - if (m_key.empty()) { - throw std::runtime_error("Key not set"); - } - return decrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding); -} - -std::string AES::decr(const std::string& input, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) -{ - if (m_key.empty()) { - throw std::runtime_error("Key not set"); - } - return decrypt(input, Base16::encode(Base16::toRawString(m_key)), iv, inputEncoding, outputEncoding); -} - -ByteArray AES::encr(const ByteArray& input, bool pkcs5Padding) -{ - if (m_key.empty()) { - throw std::runtime_error("Key not set"); - } - return encrypt(input, &m_key, pkcs5Padding); -} - -ByteArray AES::decr(const ByteArray& input) -{ - if (m_key.empty()) { - throw std::runtime_error("Key not set"); - } - return decrypt(input, &m_key); -} - -ByteArray AES::encr(const ByteArray& input, ByteArray& iv, bool pkcs5Padding) -{ - if (m_key.empty()) { - throw std::runtime_error("Key not set"); - } - return encrypt(input, &m_key, iv, pkcs5Padding); -} - -ByteArray AES::decr(const ByteArray& input, ByteArray& iv) -{ - if (m_key.empty()) { - throw std::runtime_error("Key not set"); - } - return decrypt(input, &m_key, iv); -} - - - -bool ZLib::compressFile(const std::string& gzFilename, const std::string& inputFile) -{ - gzFile out = gzopen(gzFilename.c_str(), "wb"); - if (!out) { - throw std::invalid_argument("Unable to open file [" + gzFilename + "] for writing." + std::strerror(errno)); - } - char buff[kBufferSize]; - std::FILE* in = std::fopen(inputFile.c_str(), "rb"); - std::size_t nRead = 0; - while((nRead = std::fread(buff, sizeof(char), kBufferSize, in)) > 0) { - int bytes_written = gzwrite(out, buff, nRead); - if (bytes_written == 0) { - int err_no = 0; - throw std::runtime_error("Error during compression: " + std::string(gzerror(out, &err_no))); - gzclose(out); - return false; - } - } - gzclose(out); - std::fclose(in); - return true; -} - -std::string ZLib::compressString(const std::string& str) -{ - int compressionlevel = Z_BEST_COMPRESSION; - z_stream zs; - memset(&zs, 0, sizeof(zs)); - - if (deflateInit(&zs, compressionlevel) != Z_OK) { - throw std::runtime_error("Unable to initialize zlib deflate"); - } - - zs.next_in = reinterpret_cast(const_cast(str.data())); - zs.avail_in = str.size(); - - int ret; - char outbuffer[kBufferSize]; - std::string outstring; - - // retrieve the compressed bytes blockwise - do { - zs.next_out = reinterpret_cast(outbuffer); - zs.avail_out = sizeof(outbuffer); - - ret = deflate(&zs, Z_FINISH); - - if (outstring.size() < zs.total_out) { - outstring.append(outbuffer, zs.total_out - outstring.size()); - } - } while (ret == Z_OK); - - deflateEnd(&zs); - - if (ret != Z_STREAM_END) { - throw std::runtime_error("Exception during zlib decompression: (" + std::to_string(ret) + "): " + std::string(zs.msg)); - } - - return outstring; -} - -std::string ZLib::decompressString(const std::string& str) -{ - z_stream zs; - memset(&zs, 0, sizeof(zs)); - - if (inflateInit(&zs) != Z_OK) { - throw std::runtime_error("Unable to initialize zlib inflate"); - } - - zs.next_in = reinterpret_cast(const_cast(str.data())); - zs.avail_in = str.size(); - - int ret; - char outbuffer[kBufferSize]; - std::string outstring; - - do { - zs.next_out = reinterpret_cast(outbuffer); - zs.avail_out = sizeof(outbuffer); - - ret = inflate(&zs, 0); - - if (outstring.size() < zs.total_out) { - outstring.append(outbuffer, zs.total_out - outstring.size()); - } - - } while (ret == Z_OK); - - inflateEnd(&zs); - - if (ret != Z_STREAM_END) { - throw std::runtime_error("Exception during zlib decompression: (" + std::to_string(ret) + "): " + std::string(zs.msg)); - } - - return outstring; -} diff --git a/package/mine.h b/package/mine.h deleted file mode 100644 index 28d673d..0000000 --- a/package/mine.h +++ /dev/null @@ -1,1622 +0,0 @@ -// -// Bismillah ar-Rahmaan ar-Raheem -// -// Mine (Unreleased) -// Single header minimal cryptography library -// -// Copyright (c) 2017 Muflihun Labs -// -// This library is released under the Apache 2.0 license -// https://github.com/muflihun/mine/blob/master/LICENSE -// -// https://github.com/muflihun/mine -// https://muflihun.github.io/mine -// https://muflihun.com -// - -#ifndef MINE_CRYPTO_H -#define MINE_CRYPTO_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace mine { - -using byte = unsigned char; -using ByteArray = std::vector; - -/// -/// \brief Provides base16 encoding / decoding -/// -/// This class also contains some helpers to convert various input types -/// to byte array and also provides public interface to encode -/// the iterators for other containers like vector etc. -/// -class Base16 { -public: - - /// - /// \brief List of valid hex encoding characters - /// - static const std::string kValidChars; - - /// - /// \brief Map for fast lookup corresponding character - /// \see Base64::kDecodeMap - /// - static const std::unordered_map kDecodeMap; - - /// - /// \brief Encodes input to hex encoding - /// - static inline std::string encode(const std::string& raw) noexcept - { - return encode(raw.begin(), raw.end()); - } - - /// - /// \brief Wrapper function to encode single hex char to corresponding byte - /// - static byte encode(const char* e) - { - return static_cast(strtol(e, nullptr, 16)); - } - - /// - /// \brief Encodes input iterator to hex encoding - /// - template - static std::string encode(const Iter& begin, const Iter& end) noexcept - { - std::ostringstream ss; - for (auto it = begin; it < end; ++it) { - encode(*it, ss); - } - return ss.str(); - } - - /// - /// \brief Converts hex stream (e.g, 48656C6C6F) to byte array - /// \param hex String stream e.g, 48656C6C6F (Hello) - /// \return Byte array (mine::ByteArray) containing bytes e.g, 0x48, 0x65, 0x6C, 0x6C, 0x6F - /// \throws invalid_argument if hex is not valid - /// - static ByteArray fromString(const std::string& hex); - - /// - /// \brief Converts byte array to raw string. - /// This does not necessarily has to be base16 array - /// - static std::string toRawString(const ByteArray& byteArr); - - /// - /// \brief Encodes integer to hex - /// - template - static std::string encode(T n) noexcept - { - std::stringstream ss; - int remainder; - while (n != 0) { - remainder = n % 16; - n /= 16; - ss << kValidChars[remainder]; - } - std::string res(ss.str()); - std::reverse(res.begin(), res.end()); - return res; - } - - /// - /// \brief Decodes encoded hex - /// \throws std::invalid_argument if invalid encoding. - /// std::invalid_argument::what() is set accordingly - /// - static std::string decode(const std::string& enc) - { - if (enc.size() % 2 != 0) { - throw std::invalid_argument("Invalid base-16 encoding"); - } - return decode(enc.begin(), enc.end()); - } - - /// - /// \brief Decodes encoding to single integer of type T - /// - template - static T decodeInt(const std::string& e) - { - T result = 0; - for (auto it = e.begin(); it != e.end() && result >= 0; ++it) { - try { - result = ((result << 4) | kDecodeMap.at(*it & 0xff)); - } catch (const std::exception&) { - throw std::runtime_error("Invalid base-16 encoding"); - } - } - return result; - } - - /// - /// \brief Encodes single byte - /// - static inline void encode(char b, std::ostringstream& ss) noexcept - { - int h = (b & 0xff); - ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; - } - -private: - Base16() = delete; - Base16(const Base16&) = delete; - Base16& operator=(const Base16&) = delete; - - /// - /// \brief Decodes input iterator to hex encoding - /// \note User should check for the valid size or use decode(std::string) - /// \throws runtime_error if invalid base16-encoding - /// - template - static std::string decode(const Iter& begin, const Iter& end) - { - std::ostringstream ss; - for (auto it = begin; it != end; it += 2) { - decode(*it, *(it + 1), ss); - } - return ss.str(); - } - - /// - /// \brief Decodes single byte pair - /// - static void decode(char a, char b, std::ostringstream& ss); -}; - -using byte = unsigned char; - -/// -/// \brief Provides base64 encoding / decoding implementation -/// -/// This class also provides public interface to encode -/// the iterators for other containers like vector etc. -/// -/// This also handles 16-bit, 24-bit and 32-bit characters -/// -class Base64 { -public: - - /// - /// \brief List of valid base64 encoding characters - /// - static const std::string kValidChars; - - /// - /// \brief Map for fast lookup corresponding character - /// std::unordered_map is O(1) for best case and linear in worst case - /// which is better than kValidChars find_first_of() which is linear-pos - /// in general - /// \ref http://www.cplusplus.com/reference/unordered_map/unordered_map/at/ - /// \ref http://www.cplusplus.com/reference/string/string/find_first_of/ - /// - static const std::unordered_map kDecodeMap; - - /// - /// \brief Padding is must in mine implementation of base64 - /// - static const int kPadding = 64; - - /// - /// \brief Encodes input of length to base64 encoding - /// - static std::string encode(const std::string& raw) noexcept - { - return encode(raw.begin(), raw.end()); - } - - /// - /// \brief Encodes iterators - /// - template - static std::string encode(const Iter& begin, const Iter& end) noexcept - { - std::string padding; - std::stringstream ss; - for (auto it = begin; it < end; it += 3) { - - // - // we use example following example for implementation basis - // Bits 01100001 01100010 01100011 - // 24-bit stream: 011000 010110 001001 100011 - // result indices 24 22 9 35 - // - - int c = static_cast(*it & 0xff); - ss << static_cast(static_cast(kValidChars[(c >> 2) & 0x3f])); // first 6 bits from first bitset - if (it + 1 < end) { - int c2 = static_cast(*(it + 1) & 0xff); - ss << static_cast(kValidChars[((c << 4) | // remaining 2 bits from first bitset - shift them left to get 4-bit spaces 010000 - (c2 >> 4) // first 4 bits of second bitset - shift them right to get 2 spaces and bitwise - // to add them 000110 - ) & 0x3f]); // must be within 63 -- - // 010000 - // 000110 - // --|--- - // 010110 - // 111111 - // ---&-- - // 010110 ==> 22 - if (it + 2 < end) { - int c3 = static_cast(*(it + 2) & 0xff); - ss << static_cast(kValidChars[((c2 << 2) | // remaining 4 bits from second bitset - shift them to get 011000 - (c3 >> 6) // the first 2 bits from third bitset - shift them right to get 000001 - ) & 0x3f]); - // the rest of the explanation is same as above - ss << static_cast(kValidChars[c3 & 0x3f]); // all the remaing bits - } else { - ss << static_cast(kValidChars[(c2 << 2) & 0x3f]); // we have 4 bits left from last byte need space for two 0-bits - ss << "="; - } - } else { - ss << static_cast(kValidChars[(c << 4) & 0x3f]); // remaining 2 bits from single byte - ss << "=="; - } - } - return ss.str() + padding; - } - - /// - /// \brief Decodes encoded base64 - /// \see decode(const Iter&, const Iter&) - /// - static std::string decode(const std::string& e) - { - // don't check for e's length to be multiple of 4 - // because of 76 character line-break format (MIME) - // https://tools.ietf.org/html/rfc4648#section-3.1 - return decode(e.begin(), e.end()); - } - - /// - /// \brief Decodes base64 iterator from begin to end - /// \throws std::invalid_argument if invalid encoding. Another time it is thrown - /// is if no padding is found - /// std::invalid_argument::what() is set according to the error - /// - template - static std::string decode(const Iter& begin, const Iter& end) - { - // - // we use example following example for implementation basis - // Bits 01100001 01100010 01100011 - // 24-bit stream: 011000 010110 001001 100011 - // result indices 24 22 9 35 - // - - std::stringstream ss; - for (auto it = begin; it < end; it += 4) { - try { - while (iswspace(*it)) { - ++it; - - if (it >= end) { - goto result; - } - } - int b0 = kDecodeMap.at(static_cast(*it & 0xff)); - if (b0 == kPadding) { - throw std::invalid_argument("No data available"); - } - - while (iswspace(*(it + 1))) { - ++it; - - if (it >= end) { - goto result; - } - } - int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); - - while (iswspace(*(it + 2))) { - ++it; - - if (it >= end) { - goto result; - } - } - int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); - - while (iswspace(*(it + 3))) { - ++it; - - if (it >= end) { - goto result; - } - } - int b3 = kDecodeMap.at(static_cast(*(it + 3) & 0xff)); - - ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 - b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 - - if (b1 != kPadding) { - if (b2 == kPadding) { - // second bitset is only 4 bits - } else { - ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 - // first we clear the bits at pos 4 and 5 - // then we concat with next bit - b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 - if (b3 == kPadding) { - // third bitset is only 4 bits - } else { - ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6 | // 001001 ==> 000001 << 6 ==> 01000000 - // first we clear first 4 bits - // then concat with last byte as is - b3); // as is - } - } - } - } catch (const std::exception& e) { - throw std::invalid_argument(std::string("Invalid base64 encoding: " + std::string(e.what()))); - } - } -result: - return ss.str(); - } - - -#ifdef MINE_BASE64_WSTRING_CONVERSION - /// - /// \brief Converts wstring to corresponding string and returns - /// encoding - /// \see encode(const std::string&) - /// - /// \note You need to include and headers before mine.h - /// - static std::string encode(const std::wstring& raw) noexcept - { - std::string converted = std::wstring_convert - , wchar_t>{}.to_bytes(raw); - return encode(converted); - } - - /// - /// \brief Helper method to decode base64 encoding as wstring (basic_string) - /// \see decode(const std::string&) - /// \note We do not recommend using it, instead have your own conversion function from - /// std::string to wstring as it can give you invalid results with characters that are - /// 5+ bytes long e.g, \x1F680. If you don't use such characters then it should be safe - /// to use this - /// - /// \note You need to include and headers before mine.h - /// - static std::wstring decodeAsWString(const std::string& e) - { - std::string result = decode(e); - std::wstring converted = std::wstring_convert - >{}.from_bytes(result); - return converted; - } - - /// - /// \brief Converts it to std::string and calls countChars on it - /// - /// \note You need to include and headers before mine.h - /// - static std::size_t countChars(const std::wstring& raw) noexcept - { - std::string converted = std::wstring_convert - , wchar_t>{}.to_bytes(raw); - return countChars(converted); - } -#endif - - /// - /// \brief Replacement for better d.size() that consider unicode bytes too - /// \see https://en.wikipedia.org/wiki/UTF-8#Description - /// - static std::size_t countChars(const std::string& d) noexcept; - - /// - /// \brief expectedBase64Length Returns expected base64 length - /// \param n Length of input (plain data) - /// - inline static std::size_t expectedLength(std::size_t n) noexcept - { - return ((4 * n / 3) + 3) & ~0x03; - } - - /// - /// \brief Calculates the length of string - /// \see countChars() - /// - template - inline static std::size_t expectedLength(const T& str) noexcept - { - return expectedLength(countChars(str)); - } - - /// - /// \brief Finds whether data is base64 encoded. This is done - /// by finding non-base64 character. So it is not necessary - /// a valid base64 encoding. - /// - inline static bool isBase64(const std::string& data) noexcept - { - return data.find_first_not_of(kValidChars) == std::string::npos; - } - -private: - Base64() = delete; - Base64(const Base64&) = delete; - Base64& operator=(const Base64&) = delete; -}; - -using byte = unsigned char; - -/// -/// \brief Handy safe byte array -/// -using ByteArray = std::vector; - -/// -/// \brief Provides AES crypto functionalities -/// -/// This is validated against NIST test data and all -/// the corresponding tests under test/ directory -/// are from NIST themselves. -/// -/// Please make sure to use public functions and do not -/// use private functions especially in production as -/// you may end up using them incorrectly. However -/// the source code for AES class is heavily commented for -/// verification on implementation. -/// -class AES { -public: - - /// - /// \brief Convert mode for various functions - /// - enum class Encoding { - Raw, - Base16, - Base64 - }; - - /// - /// \brief A key is a byte array - /// - using Key = ByteArray; - - AES() = default; - AES(const std::string& key); - AES(const ByteArray& key); - AES(const AES&); - AES(const AES&&); - AES& operator=(const AES&); - virtual ~AES() = default; - - void setKey(const std::string& key); - void setKey(const ByteArray& key); - - /// - /// \brief Generates random key of valid length - /// - static std::string generateRandomKey(const std::size_t len); - - /// - /// \brief Ciphers the input with specified hex key - /// \param key Hex key - /// \param inputEncoding the type of input. Defaults to Plain - /// \param outputEncoding Type of encoding for cipher - /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used - /// \return Base16 encoded cipher - /// - std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); - - /// - /// \brief Ciphers the input with specified hex key using CBC mode - /// \param key Hex key - /// \param iv Initialization vector, passed by reference. If empty a random is generated and passed in - /// \param inputEncoding the type of input. Defaults to Plain - /// \param outputEncoding Type of encoding for cipher - /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used - /// \return Base16 encoded cipher - /// - std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); - - /// - /// \brief Deciphers the input with specified hex key - /// \param key Hex key - /// \param inputEncoding the type of input. Defaults to base16 - /// \param outputEncoding Type of encoding for result - /// \return Base16 encoded cipher - /// - std::string decrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); - - /// - /// \brief Deciphers the input with specified hex key using CBC mode - /// \param key Hex key - /// \param iv Initialization vector - /// \param inputEncoding the type of input. Defaults to base16 - /// \param outputEncoding Type of encoding for result - /// \return Base16 encoded cipher - /// - std::string decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); - - /// - /// \brief Ciphers with ECB-Mode, the input can be as long as user wants - /// \param input Plain input of any length - /// \param key Pointer to a valid AES key - /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used - /// \return Cipher text byte array - /// - ByteArray encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding = true); - - /// - /// \brief Deciphers with ECB-Mode, the input can be as long as user wants - /// \param input Plain input of any length - /// \param key Pointer to a valid AES key - /// \return Cipher text byte array - /// - ByteArray decrypt(const ByteArray& input, const Key* key); - - /// - /// \brief Ciphers with CBC-Mode, the input can be as long as user wants - /// \param input Plain input of any length - /// \param key Pointer to a valid AES key - /// \param iv Initialization vector - /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used - /// \return Cipher text byte array - /// - ByteArray encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bool pkcs5Padding = true); - - /// - /// \brief Deciphers with CBC-Mode, the input can be as long as user wants - /// \param input Plain input of any length - /// \param key Pointer to a valid AES key - /// \param iv Initialization vector - /// \return Cipher text byte array - /// - ByteArray decrypt(const ByteArray& input, const Key* key, ByteArray& iv); - - - // cipher / decipher interface without keys - - std::string encr(const std::string& input, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); - - std::string encr(const std::string& input, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); - - std::string decr(const std::string& input, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); - - std::string decr(const std::string& input, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); - - ByteArray encr(const ByteArray& input, bool pkcs5Padding = true); - - ByteArray decr(const ByteArray& input); - - ByteArray encr(const ByteArray& input, ByteArray& iv, bool pkcs5Padding = true); - - ByteArray decr(const ByteArray& input, ByteArray& iv); - -private: - - /// - /// \brief A word is array of 4 byte - /// - using Word = std::array; - - /// - /// \brief KeySchedule is linear array of 4-byte words - /// \ref FIPS.197 Sec 5.2 - /// - using KeySchedule = std::map; - - /// - /// \brief State as described in FIPS.197 Sec. 3.4 - /// - using State = std::array; - - /// - /// \brief AES works on 16 bit block at a time - /// - static const uint8_t kBlockSize = 16; - - /// - /// \brief Defines the key params to it's size - /// - static const std::unordered_map> kKeyParams; - - /// - /// \brief Total items in random bytes list - /// - static const int kRandomBytesCount = 256; - - /// - /// \brief List to choose random byte from - /// - static const byte kRandomBytesList[]; - - /// - /// \brief As defined in FIPS. 197 Sec. 5.1.1 - /// - static const byte kSBox[]; - - /// - /// \brief As defined in FIPS. 197 Sec. 5.3.2 - /// - static const byte kSBoxInverse[]; - - /// - /// \brief Round constant is constant for each round - /// it contains 10 values each defined in - /// Appendix A of FIPS.197 in column Rcon[i/Nk] for - /// each key size, we add all of them in one array for - /// ease of access - /// - static const byte kRoundConstant[]; - - /// - /// \brief Nb - /// \note we make it constant as FIPS.197 p.9 says - /// "For this standard, Nb=4." - /// - static const uint8_t kNb = 4; - - - /// rotateWord function is specified in FIPS.197 Sec. 5.2: - /// The function RotWord() takes a - /// word [a0,a1,a2,a3] as input, performs a cyclic permutation, - /// and returns the word [a1,a2,a3,a0]. The - /// round constant word array - /// - /// Our definition: - /// We swap the first byte - /// to last one causing it to shift to the left - /// i.e, - /// [a1] [a2] - /// [a2] [a3] - /// [a3] => [a4] - /// [a4] [a1] - /// - static void rotateWord(Word* w); - - /// this function is also specified in FIPS.197 Sec. 5.2: - /// SubWord() is a function that takes a four-byte - /// input word and applies the S-box - /// to each of the four bytes to produce an output word. - /// - /// Out definition: - /// It's a simple substition with kSbox for corresponding byte - /// index - /// - static void substituteWord(Word* w); - - /// - /// \brief Key expansion function as described in FIPS.197 - /// - static KeySchedule keyExpansion(const Key* key); - - /// - /// \brief Adds round to the state using specified key schedule - /// - static void addRoundKey(State* state, KeySchedule* keySchedule, int round); - - /// - /// \brief Substitution step for state - /// \ref Sec. 5.1.1 - /// - static void subBytes(State* state); - - /// - /// \brief Shifting rows step for the state - /// \ref Sec. 5.1.2 - /// - static void shiftRows(State* state); - - /// - /// \ref Sec. 4.2.1 - /// - static byte xtime(byte x); - - /// - /// \ref Sec. 4.2.1 - /// - static byte multiply(byte x, byte y); - - /// - /// \brief Mixing columns for the state - /// \ref Sec. 5.1.3 - /// - static void mixColumns(State* state); - - /// - /// \brief Transformation in the Inverse Cipher - /// that is the reverse of subBytes() - /// \ref Sec. 5.3.2 - /// - static void invSubBytes(State* state); - - /// - /// \brief Transformation in the Inverse Cipher that is - /// the reverse of shiftRows() - /// \ref Sec. 5.3.1 - /// - static void invShiftRows(State* state); - - /// - /// \brief Transformation in the Inverse Cipher - /// that is the reverse of mixColumns() - /// \ref Sec. 5.3.3 - /// - static void invMixColumns(State* state); - - /// - /// \brief Prints bytes in hex format in 4x4 matrix fashion - /// - static void printBytes(const ByteArray& b); - - /// - /// \brief Prints state for debugging - /// - static void printState(const State*); - - /// - /// \brief Initializes the state with input. This function - /// also pads the input if needed (i.e, input is not block of 128-bit) - /// - static void initState(State* state, const ByteArray::const_iterator& begin); - - /// - /// \brief Generates random bytes of length - /// - static ByteArray generateRandomBytes(const std::size_t len); - - /// - /// \brief Creates byte array from input based on input mode - /// - static ByteArray resolveInputMode(const std::string& input, Encoding inputMode); - - /// - /// \brief Creates string from byte array based on convert mode - /// - static std::string resolveOutputMode(const ByteArray& input, Encoding outputMode); - - /// - /// \brief Exclusive XOR with arr - /// - static ByteArray* xorWith(ByteArray* input, const ByteArray*); - - /// - /// \brief Exclusive XOR with iter of range size as input - /// - static ByteArray* xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end); - - /// - /// \brief Raw encryption function - not for public use - /// \param input 128-bit plain input - /// If array is bigger it's chopped and if it's smaller, it's padded - /// please use alternative functions if your array is bigger. Those - /// function will handle all the bytes correctly. - /// \param key Pointer to a valid AES key - /// \note This does not do any key or input validation - /// \return 128-bit cipher text - /// - static ByteArray encryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); - - /// - /// \brief Raw decryption function - not for public use - /// \param input 128-bit cipher input - /// If array is bigger it's chopped and if it's smaller, it's padded - /// please use alternative functions if your array is bigger. Those - /// function will handle all the bytes correctly. - /// \param key Byte array of key - /// \return 128-bit plain text - /// - static ByteArray decryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); - - /// - /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array - /// - static ByteArray stateToByteArray(const State* state); - - /// - /// \brief Get padding index for stripping the padding (unpadding) - /// - static std::size_t getPaddingIndex(const ByteArray& byteArr); - - Key m_key; // to keep track of key differences - KeySchedule m_keySchedule; - - // for tests - friend class AESTest_RawCipher_Test; - friend class AESTest_RawCipherPlain_Test; - friend class AESTest_RawCipherBase64_Test; - friend class AESTest_RawSimpleCipher_Test; - friend class AESTest_RawSimpleDecipher_Test; - friend class AESTest_SubByte_Test; - friend class AESTest_InvSubByte_Test; - friend class AESTest_ShiftRows_Test; - friend class AESTest_InvShiftRows_Test; - friend class AESTest_MixColumns_Test; - friend class AESTest_InvMixColumns_Test; - friend class AESTest_KeyExpansion_Test; - friend class AESTest_AddRoundKey_Test; - friend class AESTest_CbcCipher_Test; - friend class AESTest_Copy_Test; -}; - -/// Here onwards start implementation for RSA - this contains -/// generic classes (templates). -/// User will provide their own implementation of big integer -/// or use existing one. -/// -/// Compliant with PKCS#1 (v2.1) -/// https://tools.ietf.org/html/rfc3447#section-7.2 -/// -/// Mine uses pkcs#1 v1.5 padding scheme -/// -/// Big integer must support have following functions implemented -/// - operator-() [subtraction] -/// - operator+() [addition] -/// - operator+=() [short-hand addition] -/// - operator*() [multiply] -/// - operator/() [divide] -/// - operator%() [mod] -/// - operator>>() [right-shift] -/// - operator>>=() [short-hand right-shift] -/// -/// Also you must provide proper implementation to Helper class -/// which will extend BigIntegerHelper and must implement -/// BigIntegerHelper::bigIntegerToByte -/// function. The base function returns empty byte. -/// - - -/// -/// \brief Default exponent for RSA public key -/// -static const unsigned int kDefaultPublicExponent = 65537; - -/// -/// \brief Declaration for byte in case it's not already included -/// -using byte = unsigned char; - -/// -/// \brief Simple raw string (a.k.a octet string) -/// -using RawString = std::vector; - -/// -/// \brief Contains helper functions for RSA throughout -/// -template -class BigIntegerHelper { -public: - - static const BigInteger kBigInteger256; - - BigIntegerHelper() = default; - virtual ~BigIntegerHelper() = default; - - /// - /// \brief Implementation for (a ^ -1) mod b - /// - virtual BigInteger modInverse(BigInteger a, BigInteger b) const - { - BigInteger b0 = b, t, q; - BigInteger x0 = 0, x1 = 1; - if (b == 1) { - return 1; - } - while (a > 1) { - q = a / b; - t = b; - b = a % b; - a = t; - t = x0; - x0 = x1 - q * x0; - x1 = t; - } - if (x1 < 0) { - x1 += b0; - } - return x1; - } - - /// - /// \brief Fast GCD - /// \see https://en.wikipedia.org/wiki/Euclidean_algorithm#Extended_Euclidean_algorithm - /// - virtual BigInteger gcd(BigInteger a, BigInteger b) const - { - BigInteger c; - while (a != 0) { - c = a; - a = b % a; - b = c; - } - return b; - } - - /// - /// \brief Simple (b ^ e) mod m implementation - /// \param b Base - /// \param e Exponent - /// \param m Mod - /// - virtual BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) const - { - BigInteger res = 1; - while (e > 0) { - if (e % 2 != 0) { - res = (b * res) % m; - } - b = (b * b) % m; - e /= 2; - } - return res; - } - - /// - /// \brief Power of numb i.e, b ^ e - /// - virtual BigInteger power(BigInteger b, BigInteger e) const - { - BigInteger result = 1; - while (e > 0) { - if (e % 2 == 1) { - // we decrement exponent to make it even - e--; - // store this multiplication directly to the - // result - result *= b; - // we modify this alg to ignore the next multiplication - // if we have already reached 0 (for speed) - // here are details and what we changed and how it all works - // - // Let's say we have case of 2 ^ 4 [expected answer = 16] - // 2 ^ 4 -- b = 4, e = 2 [result = 1] - // 2 ^ 2 -- b = 16, e = 1 [result = 1] - // 2 ^ 1 -- e = 0 [result = 1 * 16] - // - // here is what we changed here - // now we have result set and we have e set to zero - // doing another b ^= b means b = 16 * 16 = 256 (in our case) - // which is useless so we end here - if (e == 0) { - break; - } - } - e /= 2; - b *= b; - } - return result; - } - - /// - /// \brief Counts number of bits in big integer - /// - virtual unsigned int countBits(BigInteger b) const - { - unsigned int bits = 0; - while (b > 0) { - bits++; - b >>= 1; - } - return bits; - } - - /// - /// \brief Count number of bytes in big integer - /// - virtual inline unsigned int countBytes(BigInteger b) const - { - return countBits(b) * 8; - } - - /// - /// Raw-string to integer (a.k.a os2ip) - /// - BigInteger rawStringToInteger(const RawString& x) const - { - BigInteger result = 0; - std::size_t len = x.size(); - for (std::size_t i = len; i > 0; --i) { - result += BigInteger(x[i - 1]) * power(kBigInteger256, BigInteger(len - i)); - } - return result; - } - - /// - /// \brief Convert integer to raw string - /// (this func is also known as i2osp) - /// - RawString integerToRaw(BigInteger x, int xlen = -1) const - { - xlen = xlen == -1 ? countBytes(x) : xlen; - - RawString ba(xlen); - BigInteger r; - BigInteger q; - - int i = 1; - - for (; i <= xlen; ++i) { - divideBigNumber(x, power(kBigInteger256, BigInteger(xlen - i)), &q, &r); - ba[i - 1] = bigIntegerToByte(q); - x = r; - } - return ba; - } - - /// - /// \brief Divides big number - /// You may override this function and call custom divisor from big integer class - /// you are using. - /// Result should be stored in quotient and remainder - /// - virtual void divideBigNumber(const BigInteger& divisor, const BigInteger& divident, - BigInteger* quotient, BigInteger* remainder) const - { - *quotient = divisor / divident; - *remainder = divisor % divident; - } - - /// - /// \brief Absolutely must override this - conversion from x to single byte - /// - virtual inline byte bigIntegerToByte(const BigInteger&) const - { - return static_cast(0); - } - - /// - /// \brief Converts big integer to hex - /// - virtual std::string bigIntegerToHex(BigInteger n) const - { - return Base16::encode(n); - } - - /// - /// \brief Converts big integer to hex - /// - virtual std::string bigIntegerToString(const BigInteger& b) const - { - std::stringstream ss; - ss << b; - return ss.str(); - } - - /// - /// \brief Converts hex to big integer - /// \param hex Hexadecimal without '0x' prefix - /// - virtual BigInteger hexToBigInteger(const std::string& hex) const - { - std::string readableMsg = "0x" + hex; - BigInteger msg; - std::istringstream iss(readableMsg); - iss >> std::hex >> msg; - return msg; - } -private: - BigIntegerHelper(const BigIntegerHelper&) = delete; - BigIntegerHelper& operator=(const BigIntegerHelper&) = delete; -}; - -/// -/// \brief Big Integer = 256 (static declaration) -/// -template -const BigInteger BigIntegerHelper::kBigInteger256 = 256; - -/// -/// \brief Public key object with generic big integer -/// -template > -class GenericPublicKey { -public: - - GenericPublicKey() = default; - - GenericPublicKey(const GenericPublicKey& other) - { - this->m_n = other.m_n; - this->m_e = other.m_e; - this->m_k = other.m_k; - } - - GenericPublicKey& operator=(const GenericPublicKey& other) - { - if (this != &other) { - this->m_n = other.m_n; - this->m_e = other.m_e; - this->m_k = other.m_k; - } - return *this; - } - - GenericPublicKey(BigInteger n, int e) : - m_n(n), - m_e(e) - { - m_k = m_helper.countBytes(m_n); - if (m_k < 11) { - throw std::invalid_argument("Invalid prime. Length error."); - } - } - - virtual ~GenericPublicKey() = default; - - inline BigInteger n() const { return m_n; } - inline int e() const { return m_e; } - inline unsigned int k() const { return m_k; } - -protected: - BigIntegerHelper m_helper; - BigInteger m_n; - int m_e; - unsigned int m_k; -}; - -/// -/// \brief Private key object with generic big integer -/// -template > -class GenericPrivateKey { -public: - - GenericPrivateKey() = default; - - GenericPrivateKey(const GenericPrivateKey& other) - { - this->m_p = other.m_p; - this->m_q = other.m_q; - this->m_e = other.m_e; - this->m_n = other.m_n; - this->m_d = other.m_d; - this->m_coeff = other.m_coeff; - this->m_dp = other.m_dp; - this->m_dq = other.m_dq; - this->m_k = other.m_k; - } - - GenericPrivateKey& operator=(const GenericPrivateKey& other) - { - if (this != &other) { - this->m_p = other.m_p; - this->m_q = other.m_q; - this->m_e = other.m_e; - this->m_n = other.m_n; - this->m_d = other.m_d; - this->m_coeff = other.m_coeff; - this->m_dp = other.m_dp; - this->m_dq = other.m_dq; - this->m_k = other.m_k; - } - return *this; - } - - GenericPrivateKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) : - m_p(p), - m_q(q), - m_e(e) - - { - if (p == q || p == 0 || q == 0) { - throw std::invalid_argument("p and q must be prime numbers unique to each other"); - } - - const BigInteger pMinus1 = m_p - 1; - const BigInteger qMinus1 = m_q - 1; - const BigInteger phi = pMinus1 * qMinus1; - - if (m_helper.gcd(m_e, phi) != 1) { - throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); - } - m_n = m_p * m_q; - m_k = m_helper.countBytes(m_n); - if (m_k < 11) { - throw std::invalid_argument("Invalid prime. Length error."); - } - m_coeff = m_helper.modInverse(m_q, m_p); - - m_d = m_helper.modInverse(m_e, phi); - - // note: - // https://tools.ietf.org/html/rfc3447#section-2 says to use m_e - // openssl says to use m_d - which one?! - // - m_dp = BigInteger(m_d) % pMinus1; - m_dq = BigInteger(m_d) % qMinus1; - } - - virtual ~GenericPrivateKey() = default; - - inline BigInteger p() const { return m_p; } - inline BigInteger q() const { return m_q; } - inline BigInteger coeff() const { return m_coeff; } - inline BigInteger n() const { return m_n; } - inline int e() const { return m_e; } - inline BigInteger d() const { return m_d; } - inline BigInteger dp() const { return m_dq; } - inline BigInteger dq() const { return m_dp; } - inline int k() const { return m_k; } - - friend std::ostream& operator<<(std::ostream& ss, const GenericPrivateKey& k) - { - ss << "modulus: " << k.m_n << "\npublicExponent: " << k.m_e << "\nprivateExponent: " << k.m_d - << "\nprime1: " << k.m_p << "\nprime2: " << k.m_q << "\nexponent1: " << k.m_dp << "\nexponent2: " - << k.m_dq << "\ncoefficient: " << k.m_coeff; - return ss; - } - - /// - /// \brief You can use this to export the key via - /// openssl-cli using - /// openssl asn1parse -genconf exported.asn -out imp.der - /// openssl rsa -in imp.der -inform der -text -check - /// \return - /// - virtual std::string exportASNSequence() const - { - std::stringstream ss; - ss << "asn1=SEQUENCE:rsa_key\n\n"; - ss << "[rsa_key]\n"; - ss << "version=INTEGER:0\n"; - ss << "modulus=INTEGER:" << m_helper.bigIntegerToString(m_n) << "\n"; - ss << "pubExp=INTEGER:" << m_e << "\n"; - ss << "privExp=INTEGER:" << m_helper.bigIntegerToString(m_d) << "\n"; - ss << "p=INTEGER:" << m_helper.bigIntegerToString(m_p) << "\n"; - ss << "q=INTEGER:" << m_helper.bigIntegerToString(m_q) << "\n"; - ss << "e1=INTEGER:" << m_helper.bigIntegerToString(m_dp) << "\n"; - ss << "e2=INTEGER:" << m_helper.bigIntegerToString(m_dq) << "\n"; - ss << "coeff=INTEGER:" << m_helper.bigIntegerToString(m_coeff); - return ss.str(); - } -protected: - Helper m_helper; - BigInteger m_p; - BigInteger m_q; - int m_e; - BigInteger m_coeff; - BigInteger m_n; - BigInteger m_d; - BigInteger m_dp; - BigInteger m_dq; - unsigned int m_k; -}; - -/// -/// \brief Key pair (containing public and private key objects) with generic big integer -/// -template > -class GenericKeyPair { -public: - GenericKeyPair() = default; - - GenericKeyPair(const GenericKeyPair& other) - { - this->m_privateKey = other.m_privateKey; - this->m_publicKey = other.m_publicKey; - } - - GenericKeyPair& operator=(const GenericKeyPair& other) - { - if (this != &other) { - this->m_privateKey = other.m_privateKey; - this->m_publicKey = other.m_publicKey; - } - return *this; - } - - GenericKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) - { - m_publicKey = GenericPublicKey(p * q, exp); - m_privateKey = GenericPrivateKey(p, q, exp); - } - - virtual ~GenericKeyPair() = default; - - inline const GenericPublicKey* publicKey() const { return &m_publicKey; } - inline const GenericPrivateKey* privateKey() const { return &m_privateKey; } - -protected: - GenericPublicKey m_publicKey; - GenericPrivateKey m_privateKey; -}; - -/// -/// \brief Provides RSA crypto functionalities -/// -template > -class GenericRSA { -public: - - using PublicKey = GenericPublicKey; - using PrivateKey = GenericPrivateKey; - - GenericRSA() = default; - GenericRSA(const GenericRSA&) = delete; - GenericRSA& operator=(const GenericRSA&) = delete; - - /// - /// \brief Encrypts plain bytes using RSA public key - /// \param publicKey RSA Public key for encryption - /// \param m The message. This can be raw bytes or plain text - /// T can of std::string or std::wstring or custom string type that has - /// basic_stringstream implementation alongside it - /// \note Mine uses pkcs#1 padding scheme - /// \return hex of cipher - /// - template - std::string encrypt(const PublicKey* publicKey, const T& m) - { - BigInteger paddedMsg = pkcs1pad2(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); - BigInteger cipher = m_helper.powerMod(paddedMsg, publicKey->e(), publicKey->n()); - return m_helper.bigIntegerToHex(cipher); - } - - /// - /// \brief Helper method to encrypt wide-string messages using public key. - /// \see encrypt(const GenericPublicKey* publicKey, const T& m) - /// - inline std::string encrypt(const PublicKey* publicKey, - const std::wstring& message) - { - return encrypt(publicKey, message); - } - - /// - /// \brief Helper method to encrypt std::string messages using public key. - /// \see encrypt(const GenericPublicKey* publicKey, const T& m) - /// - inline std::string encrypt(const PublicKey* publicKey, - const std::string& message) - { - return encrypt(publicKey, message); - } - - /// - /// \brief Decrypts RSA hex message using RSA private key - /// \param privateKey RSA private key - /// \param c Cipher in hex format (should not start with 0x) - /// \return Plain result of TResult type - /// - template - TResult decrypt(const PrivateKey* privateKey, const std::string& c) - { - BigInteger msg = m_helper.hexToBigInteger(c); - int xlen = (m_helper.countBits(privateKey->n()) + 7) >> 3; - if (msg >= m_helper.power(BigInteger(256), BigInteger(xlen))) { - throw std::runtime_error("Integer too large"); - } - BigInteger decr = m_helper.powerMod(msg, privateKey->d(), privateKey->n()); - RawString rawStr = m_helper.integerToRaw(decr, xlen); - return pkcs1unpad2(rawStr); - } - - /// - /// \brief Verifies signature for text using RSA public key - /// \param message An octet string - /// \param signature Signature in hex - /// \see https://tools.ietf.org/html/rfc3447#section-8.1.2 - /// - bool verify(const PublicKey*, const std::string&, const std::string&) - { - - return true; - } - - /// - /// \brief Maximum size of RSA block with specified key size - /// \param keySize 2048, 1024, ... - /// - inline static unsigned int maxRSABlockSize(std::size_t keySize) - { - return (keySize / 8) - 11; - } - - /// - /// \brief Minimum size of RSA key to encrypt data of dataSize size - /// - inline static unsigned int minRSAKeySize(std::size_t dataSize) - { - return (dataSize + 11) * 8; - } - -private: - Helper m_helper; - - /// - /// \brief PKCS #1 padding - /// \see https://tools.ietf.org/html/rfc3447#page-23 - /// \return corresponding nonnegative integer - /// - template - BigInteger pkcs1pad2(const T& s, std::size_t n) { - if (n < s.size() + 11) { - throw std::runtime_error("Message too long"); - } - RawString byteArray(n); - long long i = s.size() - 1; - while(i >= 0 && n > 0) { - int c = static_cast(s.at(i--)); - if (c <= 0x7f) { - // utf - byteArray[--n] = c; - } else if (c <= 0x7ff) { - byteArray[--n] = (c & 0x3f) | 128; - byteArray[--n] = (c >> 6) | 192; - } else if (c <= 0xffff) { - // utf-16 - byteArray[--n] = (c & 0x3f) | 128; - byteArray[--n] = ((c >> 6) & 63) | 128; - byteArray[--n] = (c >> 12) | 224; - } else { - // utf-32 - byteArray[--n] = (c & 0x3f) | 128; - byteArray[--n] = ((c >> 6) & 0x3f) | 128; - byteArray[--n] = ((c >> 12) & 0x3f) | 128; - byteArray[--n] = (c >> 18) | 240; - } - } - - // now padding i.e, 0x00 || 0x02 || PS || 0x00 - // see point #2 on https://tools.ietf.org/html/rfc3447#section-7.2.1 => EME-PKCS1-v1_5 encoding - - const int kLengthOfRandom = 127; - - byteArray[--n] = 0; - - srand(time(nullptr)); - int r = rand() % kLengthOfRandom + 1; - while (n > 2) { - r = 0; - while (r == 0) { - r = rand() % kLengthOfRandom + 1; - } - byteArray[--n] = r; - } - // first two bytes of padding are 0x2 (second) and 0x0 (first) - byteArray[--n] = 2; - byteArray[--n] = 0; - return m_helper.rawStringToInteger(byteArray); - } - - /// - /// \brief PKCS #1 unpadding - /// \see https://tools.ietf.org/html/rfc3447#section-4.1 - /// \return corresponding octet string of length n - /// - template - T pkcs1unpad2(const RawString& ba) - { - std::size_t baLen = ba.size(); - if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { - throw std::runtime_error("Incorrect padding PKCS#1"); - } - std::size_t i = 2; // passed first two characters (0x0 and 0x2) test - // lets check for the - - // if we hit end while still we're still with non-zeros, it's a padding error - // 0x0 (done) | 0x2 (done) | | 0x0 - while (ba[i] != 0) { - if (++i >= baLen) { // already ended! - throw std::runtime_error("Incorrect padding PKCS#1"); - } - } - // last zero - ++i; - - // now we should be at the first non-zero byte - // which is our first item, concat them as char | wchar_t - - using CharacterType = typename T::value_type; - std::basic_stringstream ss; - - for (; i < baLen; ++i) { - // reference: http://en.cppreference.com/w/cpp/language/types -> range of values - int c = ba[i] & 0xff; - if (c <= 0x7f) { - ss << static_cast(c); - } else if (c > 0xbf && c < 0xe0) { - ss << static_cast( - ((c & 0x1f) << 6) | - (ba[i+1] & 0x3f) - ); - ++i; - } else if ((c < 0xbf) || (c >= 0xe0 && c < 0xf0)) { // utf-16 char - ss << static_cast( - ((c & 0xf) << 12) | - ((ba[i+1] & 0x3f) << 6) | - (ba[i+2] & 0x3f) - ); - i += 2; - } else { // utf-32 char - ss << static_cast( - ((c & 0x7) << 18) | - ((ba[i+1] & 0x3f) << 12) | - ((ba[i+2] & 0x3f) << 6) | - (ba[i+3] & 0x3f) - ); - i += 3; - } - } - return ss.str(); - } - - /// - /// \brief Creates RSA VP for verification - /// \param signature signature representative, an integer between 0 and n - 1 - /// \return message representative, an integer between 0 and n - 1 - /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 - /// - BigInteger createVerificationPrimitive(const PublicKey* publicKey, const BigInteger& signature) - { - if (signature < 0 || signature > publicKey->n() - 1) { - throw std::runtime_error("signature representative out of range"); - } - return m_helper.powerMod(signature, publicKey->e(), publicKey->n()); - } - - // for tests - friend class RSATest_Signature_Test; - friend class RSATest_Decryption_Test; - friend class RSATest_KeyAndEncryptionDecryption_Test; - friend class RSATest_PowerMod_Test; -}; - - -/// -/// \brief Provides Zlib functionality for inflate and deflate -/// -class ZLib { -public: - - /// - /// \brief Size of buffer algorithm should operate under - /// - static const int kBufferSize = 32768; - - /// - /// \brief Compress input file (path) and create new file - /// \param gzFilename Output file path - /// \param inputFile Input file path - /// \return True if successful, otherwise false - /// - static bool compressFile(const std::string& gzFilename, const std::string& inputFile); - - /// - /// @brief Compresses string using zlib (inflate) - /// @param str Input plain text - /// @return Raw output (binary) - /// - static std::string compressString(const std::string& str); - - /// - /// @brief Decompresses string using zlib (deflate) - /// @param str Raw input - /// @return Plain output - /// - static std::string decompressString(const std::string& str); -private: - ZLib() = delete; - ZLib(const ZLib&) = delete; - ZLib& operator=(const ZLib&) = delete; -}; - -} // namespace mine -#endif // MINE_CRYPTO_H diff --git a/src/aes.h b/src/aes.h index 1c3bba0..3ba1eee 100644 --- a/src/aes.h +++ b/src/aes.h @@ -25,7 +25,7 @@ #include #include #include -#include "src/rsa.h" +#include namespace mine { diff --git a/src/base64.cc b/src/base64.cc index 92a0231..4ab5f23 100644 --- a/src/base64.cc +++ b/src/base64.cc @@ -20,26 +20,29 @@ using namespace mine; -const std::string Base64::kValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; +const char Base64::kValidChars[65] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; const std::unordered_map Base64::kDecodeMap = { - {0x41, 0x00}, {0x42, 0x01}, {0x43, 0x02}, {0x44, 0x03}, - {0x45, 0x04}, {0x46, 0x05}, {0x47, 0x06}, {0x48, 0x07}, - {0x49, 0x08}, {0x4A, 0x09}, {0x4B, 0x0A}, {0x4C, 0x0B}, - {0x4D, 0x0C}, {0x4E, 0x0D}, {0x4F, 0x0E}, {0x50, 0x0F}, - {0x51, 0x10}, {0x52, 0x11}, {0x53, 0x12}, {0x54, 0x13}, - {0x55, 0x14}, {0x56, 0x15}, {0x57, 0x16}, {0x58, 0x17}, - {0x59, 0x18}, {0x5A, 0x19}, {0x61, 0x1A}, {0x62, 0x1B}, - {0x63, 0x1C}, {0x64, 0x1D}, {0x65, 0x1E}, {0x66, 0x1F}, - {0x67, 0x20}, {0x68, 0x21}, {0x69, 0x22}, {0x6A, 0x23}, - {0x6B, 0x24}, {0x6C, 0x25}, {0x6D, 0x26}, {0x6E, 0x27}, - {0x6F, 0x28}, {0x70, 0x29}, {0x71, 0x2A}, {0x72, 0x2B}, - {0x73, 0x2C}, {0x74, 0x2D}, {0x75, 0x2E}, {0x76, 0x2F}, - {0x77, 0x30}, {0x78, 0x31}, {0x79, 0x32}, {0x7A, 0x33}, - {0x30, 0x34}, {0x31, 0x35}, {0x32, 0x36}, {0x33, 0x37}, - {0x34, 0x38}, {0x35, 0x39}, {0x36, 0x3A}, {0x37, 0x3B}, - {0x38, 0x3C}, {0x39, 0x3D}, {0x2B, 0x3E}, {0x2F, 0x3F}, - {0x3D, 0x40} + {0x41, 0x00}, {0x42, 0x01}, {0x43, 0x02}, {0x44, 0x03}, + {0x45, 0x04}, {0x46, 0x05}, {0x47, 0x06}, {0x48, 0x07}, + {0x49, 0x08}, {0x4A, 0x09}, {0x4B, 0x0A}, {0x4C, 0x0B}, + {0x4D, 0x0C}, {0x4E, 0x0D}, {0x4F, 0x0E}, {0x50, 0x0F}, + {0x51, 0x10}, {0x52, 0x11}, {0x53, 0x12}, {0x54, 0x13}, + {0x55, 0x14}, {0x56, 0x15}, {0x57, 0x16}, {0x58, 0x17}, + {0x59, 0x18}, {0x5A, 0x19}, {0x61, 0x1A}, {0x62, 0x1B}, + {0x63, 0x1C}, {0x64, 0x1D}, {0x65, 0x1E}, {0x66, 0x1F}, + {0x67, 0x20}, {0x68, 0x21}, {0x69, 0x22}, {0x6A, 0x23}, + {0x6B, 0x24}, {0x6C, 0x25}, {0x6D, 0x26}, {0x6E, 0x27}, + {0x6F, 0x28}, {0x70, 0x29}, {0x71, 0x2A}, {0x72, 0x2B}, + {0x73, 0x2C}, {0x74, 0x2D}, {0x75, 0x2E}, {0x76, 0x2F}, + {0x77, 0x30}, {0x78, 0x31}, {0x79, 0x32}, {0x7A, 0x33}, + {0x30, 0x34}, {0x31, 0x35}, {0x32, 0x36}, {0x33, 0x37}, + {0x34, 0x38}, {0x35, 0x39}, {0x36, 0x3A}, {0x37, 0x3B}, + {0x38, 0x3C}, {0x39, 0x3D}, {0x2B, 0x3E}, {0x2F, 0x3F}, + {0x3D, 0x40} }; std::size_t Base64::countChars(const std::string& str) noexcept diff --git a/src/base64.h b/src/base64.h index 84bf5c1..1bf911f 100644 --- a/src/base64.h +++ b/src/base64.h @@ -22,6 +22,7 @@ #define Base64_H #include +#include #include // codecvt is not part of standard @@ -45,22 +46,16 @@ using byte = unsigned char; /// /// This also handles 16-bit, 24-bit and 32-bit characters /// +/// +/// class Base64 { public: /// /// \brief List of valid base64 encoding characters /// - static const std::string kValidChars; + static const char kValidChars[]; - /// - /// \brief Map for fast lookup corresponding character - /// std::unordered_map is O(1) for best case and linear in worst case - /// which is better than kValidChars find_first_of() which is linear-pos - /// in general - /// \ref http://www.cplusplus.com/reference/unordered_map/unordered_map/at/ - /// \ref http://www.cplusplus.com/reference/string/string/find_first_of/ - /// static const std::unordered_map kDecodeMap; /// @@ -155,6 +150,14 @@ class Base64 { // result indices 24 22 9 35 // + auto findPosOf = [](char c) -> int { + try { + return kDecodeMap.at(static_cast(c & 0xff)); + } catch (const std::exception& e) { + throw e; + } + }; + std::stringstream ss; for (auto it = begin; it < end; it += 4) { try { @@ -165,10 +168,13 @@ class Base64 { goto result; } } - int b0 = kDecodeMap.at(static_cast(*it & 0xff)); + int b0 = findPosOf(*it); if (b0 == kPadding) { throw std::invalid_argument("No data available"); } + if (b0 == -1) { + throw std::invalid_argument("Invalid base64 encoding"); + } while (iswspace(*(it + 1))) { ++it; @@ -177,7 +183,10 @@ class Base64 { goto result; } } - int b1 = kDecodeMap.at(static_cast(*(it + 1) & 0xff)); + int b1 = findPosOf(*(it + 1)); + if (b1 == -1) { + throw std::invalid_argument("Invalid base64 encoding"); + } while (iswspace(*(it + 2))) { ++it; @@ -186,7 +195,10 @@ class Base64 { goto result; } } - int b2 = kDecodeMap.at(static_cast(*(it + 2) & 0xff)); + int b2 = findPosOf(*(it + 2)); + if (b2 == -1) { + throw std::invalid_argument("Invalid base64 encoding"); + } while (iswspace(*(it + 3))) { ++it; @@ -195,7 +207,10 @@ class Base64 { goto result; } } - int b3 = kDecodeMap.at(static_cast(*(it + 3) & 0xff)); + int b3 = findPosOf(*(it + 3)); + if (b3 == -1) { + throw std::invalid_argument("Invalid base64 encoding"); + } ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 @@ -298,16 +313,6 @@ class Base64 { return expectedLength(countChars(str)); } - /// - /// \brief Finds whether data is base64 encoded. This is done - /// by finding non-base64 character. So it is not necessary - /// a valid base64 encoding. - /// - inline static bool isBase64(const std::string& data) noexcept - { - return data.find_first_not_of(kValidChars) == std::string::npos; - } - private: Base64() = delete; Base64(const Base64&) = delete; diff --git a/test/base64-test.h b/test/base64-test.h index e9e1747..3e1530c 100644 --- a/test/base64-test.h +++ b/test/base64-test.h @@ -175,15 +175,6 @@ TEST(Base64Test, ExpectedSize) } } -TEST(Base64Test, IsBase64) -{ - for (const auto& item : IsBase64Data) { - auto first = PARAM(0); - auto second = PARAM(1); - ASSERT_EQ(Base64::isBase64(first), second); - } -} - } #endif // BASE64_TEST_H diff --git a/test/rsa-test.h b/test/rsa-test.h index bdcd216..03010a4 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -11,6 +11,7 @@ #include #include +//#include namespace mine { @@ -24,6 +25,12 @@ class Helper : public BigIntegerHelper return static_cast(b.ConvertToLong()); } + virtual void divideBigNumber(const BigInteger& divisor, const BigInteger& divident, + BigInteger* quotient, BigInteger* remainder) const override + { + BigInteger::Divide(*remainder, *quotient, divisor, divident); + } + virtual std::string bigIntegerToHex(BigInteger b) const override { std::stringstream ss; From f9be021095bbbce8f51efbd049d61f1d252d989c Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 3 Sep 2017 11:42:27 +1000 Subject: [PATCH 132/148] build package --- package/mine.cc | 1195 ++++++++++++++++++++++++++++++++++ package/mine.h | 1626 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 2821 insertions(+) create mode 100644 package/mine.cc create mode 100644 package/mine.h diff --git a/package/mine.cc b/package/mine.cc new file mode 100644 index 0000000..c8648f4 --- /dev/null +++ b/package/mine.cc @@ -0,0 +1,1195 @@ +// +// Bismillah ar-Rahmaan ar-Raheem +// +// Mine (Unreleased) +// Single header minimal cryptography library +// +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE +// +// https://github.com/muflihun/mine +// https://muflihun.github.io/mine +// https://muflihun.com +// +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mine.h" + +using namespace mine; + + +const std::string Base16::kValidChars = "0123456789ABCDEF"; + +const std::unordered_map Base16::kDecodeMap = { + {0x30, 0x00}, {0x31, 0x01}, {0x32, 0x02}, {0x33, 0x03}, + {0x34, 0x04}, {0x35, 0x05}, {0x36, 0x06}, {0x37, 0x07}, + {0x38, 0x08}, {0x39, 0x09}, {0x41, 0x0A}, {0x42, 0x0B}, + {0x43, 0x0C}, {0x44, 0x0D}, {0x45, 0x0E}, {0x46, 0x0F} +}; + +ByteArray Base16::fromString(const std::string& hex) +{ + if (hex.size() % 2 != 0) { + throw std::invalid_argument("Invalid base-16 encoding"); + } + + ByteArray byteArr; + for (std::size_t i = 0; i < hex.length(); i += 2) { + byteArr.push_back(encode(hex.substr(i, 2).c_str())); + } + return byteArr; +} + +std::string Base16::toRawString(const ByteArray& input) +{ + std::ostringstream ss; + std::copy(input.begin(), input.end(), std::ostream_iterator(ss)); + return ss.str(); +} + +void Base16::decode(char a, char b, std::ostringstream& ss) +{ + try { + ss << static_cast((kDecodeMap.at(a & 0xff) << 4) | kDecodeMap.at(b & 0xff)); + } catch (const std::exception& e) { + throw std::invalid_argument("Invalid base-16 encoding: " + std::string(e.what())); + } +} + + +const char Base64::kValidChars[65] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + +const std::unordered_map Base64::kDecodeMap = { + {0x41, 0x00}, {0x42, 0x01}, {0x43, 0x02}, {0x44, 0x03}, + {0x45, 0x04}, {0x46, 0x05}, {0x47, 0x06}, {0x48, 0x07}, + {0x49, 0x08}, {0x4A, 0x09}, {0x4B, 0x0A}, {0x4C, 0x0B}, + {0x4D, 0x0C}, {0x4E, 0x0D}, {0x4F, 0x0E}, {0x50, 0x0F}, + {0x51, 0x10}, {0x52, 0x11}, {0x53, 0x12}, {0x54, 0x13}, + {0x55, 0x14}, {0x56, 0x15}, {0x57, 0x16}, {0x58, 0x17}, + {0x59, 0x18}, {0x5A, 0x19}, {0x61, 0x1A}, {0x62, 0x1B}, + {0x63, 0x1C}, {0x64, 0x1D}, {0x65, 0x1E}, {0x66, 0x1F}, + {0x67, 0x20}, {0x68, 0x21}, {0x69, 0x22}, {0x6A, 0x23}, + {0x6B, 0x24}, {0x6C, 0x25}, {0x6D, 0x26}, {0x6E, 0x27}, + {0x6F, 0x28}, {0x70, 0x29}, {0x71, 0x2A}, {0x72, 0x2B}, + {0x73, 0x2C}, {0x74, 0x2D}, {0x75, 0x2E}, {0x76, 0x2F}, + {0x77, 0x30}, {0x78, 0x31}, {0x79, 0x32}, {0x7A, 0x33}, + {0x30, 0x34}, {0x31, 0x35}, {0x32, 0x36}, {0x33, 0x37}, + {0x34, 0x38}, {0x35, 0x39}, {0x36, 0x3A}, {0x37, 0x3B}, + {0x38, 0x3C}, {0x39, 0x3D}, {0x2B, 0x3E}, {0x2F, 0x3F}, + {0x3D, 0x40} +}; + +std::size_t Base64::countChars(const std::string& str) noexcept +{ + std::size_t result = 0UL; + for (auto it = str.begin(); it <= str.end();) { + int c = *it & 0xff; + int charCount = 0; + if (c == 0x0) { + // \0 + ++it; // we increment iter manually + } else if (c <= 0x7f) { + charCount = 1; + } else if (c <= 0x7ff) { + charCount = 2; + } else if (c <= 0xffff) { + charCount = 3; + } else { + charCount = 4; + } + result += charCount; + it += charCount; + } + return result; +} + +#define MINE_PROFILING 0 + +#if MINE_PROFILING +# include +# include + +template +void endProfiling(T& started, const std::string& name) { + auto ended = std::chrono::steady_clock::now(); + auto diff = std::chrono::duration_cast(ended - started); + std::cout << (diff.count()) << " ns for " << name << std::endl; +} + +#endif + + +const byte AES::kSBox[256] = { + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc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ae, 0x08, + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 +}; + +const byte AES::kSBoxInverse[256] = { + 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, + 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, + 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, + 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, + 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, + 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, + 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, + 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, + 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, + 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, + 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, + 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, + 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, + 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, + 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, + 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d +}; + +const uint8_t AES::kRoundConstant[10] = { + 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 +}; + +const byte AES::kRandomBytesList[256] = { + 0x6f, 0x48, 0x15, 0x46, 0x77, 0x58, 0x05, 0x0b, 0x02, 0x6f, 0x20, 0x66, 0x18, 0x5a, 0x17, 0x27, + 0x45, 0x6c, 0x0f, 0x33, 0x08, 0x58, 0x2a, 0x54, 0x75, 0x53, 0x1e, 0x2a, 0x09, 0x13, 0x0f, 0x20, + 0x49, 0x49, 0x4b, 0x18, 0x3c, 0x1f, 0x06, 0x0e, 0x58, 0x52, 0x7c, 0x25, 0x58, 0x7d, 0x33, 0x27, + 0x14, 0x47, 0x66, 0x3f, 0x68, 0x66, 0x49, 0x27, 0x77, 0x10, 0x33, 0x26, 0x6c, 0x34, 0x10, 0x4e, + 0x10, 0x48, 0x07, 0x7c, 0x11, 0x06, 0x60, 0x61, 0x28, 0x29, 0x47, 0x5b, 0x3b, 0x16, 0x75, 0x74, + 0x14, 0x4a, 0x4a, 0x78, 0x21, 0x35, 0x77, 0x50, 0x17, 0x74, 0x3c, 0x26, 0x05, 0x31, 0x65, 0x36, + 0x48, 0x3c, 0x29, 0x4c, 0x1e, 0x78, 0x5e, 0x51, 0x16, 0x7f, 0x0b, 0x6d, 0x14, 0x41, 0x6e, 0x15, + 0x35, 0x7a, 0x4c, 0x59, 0x52, 0x5e, 0x0c, 0x22, 0x29, 0x7d, 0x6f, 0x0b, 0x73, 0x55, 0x0c, 0x44, + 0x3d, 0x70, 0x15, 0x33, 0x71, 0x23, 0x34, 0x77, 0x39, 0x68, 0x04, 0x6d, 0x2b, 0x4b, 0x52, 0x4d, + 0x30, 0x03, 0x38, 0x09, 0x5b, 0x58, 0x09, 0x5f, 0x4b, 0x54, 0x5d, 0x53, 0x35, 0x6b, 0x48, 0x43, + 0x3e, 0x58, 0x7d, 0x48, 0x7e, 0x6d, 0x71, 0x28, 0x14, 0x0e, 0x41, 0x58, 0x20, 0x7b, 0x48, 0x14, + 0x1f, 0x68, 0x07, 0x6d, 0x62, 0x4a, 0x72, 0x34, 0x7d, 0x66, 0x3e, 0x42, 0x79, 0x47, 0x36, 0x11, + 0x37, 0x08, 0x1f, 0x0a, 0x08, 0x2f, 0x66, 0x11, 0x2b, 0x0e, 0x03, 0x33, 0x14, 0x66, 0x25, 0x3e, + 0x08, 0x6f, 0x6e, 0x69, 0x71, 0x1e, 0x1c, 0x02, 0x09, 0x0a, 0x45, 0x24, 0x73, 0x58, 0x4b, 0x43, + 0x5a, 0x53, 0x4f, 0x0e, 0x39, 0x71, 0x13, 0x0c, 0x02, 0x46, 0x66, 0x2a, 0x56, 0x4c, 0x2b, 0x37, + 0x34, 0x45, 0x6e, 0x01, 0x4d, 0x12, 0x35, 0x4a, 0x29, 0x66, 0x30, 0x5b, 0x31, 0x4f, 0x6e, 0x3d +}; + +const std::unordered_map> AES::kKeyParams = { + { 16, {{ 4, 10 }} }, + { 24, {{ 6, 12 }} }, + { 32, {{ 8, 14 }} } +}; + +AES::AES(const std::string& key) +{ + setKey(key); +} + +AES::AES(const ByteArray& key) : m_key(key) +{ + setKey(key); +} + +AES::AES(const AES& other) +{ + if (&other != this) { + m_key = other.m_key; + m_keySchedule = other.m_keySchedule; + } +} + +AES::AES(const AES&& other) : + m_key(std::move(other.m_key)), + m_keySchedule(std::move(other.m_keySchedule)) +{ +} + +AES& AES::operator=(const AES& other) +{ + if (&other != this) { + m_key = other.m_key; + m_keySchedule = other.m_keySchedule; + } + return *this; +} + +void AES::setKey(const std::string& key) +{ + setKey(Base16::fromString(key)); +} + +void AES::setKey(const ByteArray& key) +{ + if (key.size() != 16 && key.size() != 24 && key.size() != 32) { + throw std::invalid_argument("Invalid key size. AES can operate on 128-bit, 192-bit and 256-bit keys"); + } + m_key = key; + m_keySchedule = keyExpansion(&m_key); +} + +void AES::printBytes(const ByteArray& b) +{ + for (std::size_t i = 1; i <= b.size(); ++i) { + std::cout << "0x" << (b[i - 1] < 10 ? "0" : "") << Base16::encode(b[i - 1]) << " "; + if (i % 4 == 0) { + std::cout << std::endl; + } + } + std::cout << std::endl << "------" << std::endl; +} + +void AES::printState(const State* state) +{ + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + byte b = (*state)[j][i]; + std::cout << "0x" << (b < 10 ? "0" : "") << Base16::encode(b) << " "; + } + std::cout << std::endl; + } + std::cout << std::endl; +} + +void AES::rotateWord(Word* w) { + byte t = (*w)[0]; + (*w)[0] = (*w)[1]; + (*w)[1] = (*w)[2]; + (*w)[2] = (*w)[3]; + (*w)[3] = t; +} + +void AES::substituteWord(Word* w) { + for (uint8_t i = 0; i < 4; ++i) { + (*w)[i] = kSBox[(*w)[i]]; + } +} + +AES::KeySchedule AES::keyExpansion(const Key* key) +{ + +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif + + std::size_t keySize = key->size(); + + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + uint8_t Nk = kKeyParams.at(keySize)[0], + Nr = kKeyParams.at(keySize)[1]; + + KeySchedule words;//(kNb * (Nr+1)); + + uint8_t i = 0; + // copy main key as is for the first round + for (; i < Nk; ++i) { + words[i] = {{ + (*key)[(i * 4) + 0], + (*key)[(i * 4) + 1], + (*key)[(i * 4) + 2], + (*key)[(i * 4) + 3], + }}; + } + + for (; i < kNb * (Nr + 1); ++i) { + Word temp = words[i - 1]; + + if (i % Nk == 0) { + rotateWord(&temp); + substituteWord(&temp); + // xor with rcon + temp[0] ^= kRoundConstant[(i / Nk) - 1]; + } else if (Nk == 8 && i % Nk == 4) { + // See note for 256-bit keys on Sec. 5.2 on FIPS.197 + substituteWord(&temp); + } + + // xor previous column of new key with corresponding column of + // previous round key + Word correspondingWord = words[i - Nk]; + byte b0 = correspondingWord[0] ^ temp[0]; + byte b1 = correspondingWord[1] ^ temp[1]; + byte b2 = correspondingWord[2] ^ temp[2]; + byte b3 = correspondingWord[3] ^ temp[3]; + + words[i] = {{ b0, b1, b2, b3 }}; + } + +#if MINE_PROFILING + endProfiling(started, "keyExpansion"); +#endif + + return words; +} + +/// +/// Adding round key is simply a xor operation on +/// corresponding key for the round +/// which is generated during key expansion +/// +/// Let's say we have state and a key +/// +/// [ df c3 e2 9c ] [ ef d4 49 11 ] +/// | 0f ad 1f ca | | 1f ad ac fa | +/// | 0c 9d 8d fa | ^ | cc 9e 15 dd | +/// [ fe ef cc b2 ] [ fe ea 02 dc ] +/// +/// +/// [ df^ef c3^d4 e2^49 9c^11 ] +/// | 0f^1f ad^ad 1f^ac ca^fa | +/// | 0c^cc 9d^9e 8d^15 fa^dd | +/// [ fe^fe ef^ea cc^02 b2^dc ] +/// +void AES::addRoundKey(State* state, KeySchedule* keySchedule, int round) +{ +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif + const int iR = (round * kNb); + for (std::size_t i = 0; i < kNb; ++i) { + std::size_t iR2 = iR + i; + for (std::size_t j = 0; j < kNb; ++j) { +#if MINE_PROFILING + auto started2 = std::chrono::steady_clock::now(); + std::cout << ((*state)[i][j] & 0xff) << " ^= " << ((*keySchedule)[iR2][j] & 0xff); +#endif + (*state)[i][j] ^= (*keySchedule)[iR2][j]; +#if MINE_PROFILING + std::cout << " = " << ((*state)[i][j] & 0xff) << std::endl; + endProfiling(started2, "add single round"); +#endif + } + } +#if MINE_PROFILING + endProfiling(started, "addRoundKey"); +#endif +} + +/// +/// Simple substition for the byte +/// from sbox - i.e, for 0x04 we will replace with the +/// byte at index 0x04 => 0xf2 +/// +void AES::subBytes(State* state) +{ +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + (*state)[i][j] = kSBox[(*state)[i][j]]; + } + } + +#if MINE_PROFILING + endProfiling(started, "subBytes"); +#endif +} + +/// +/// A simple substition of bytes using kSBoxInverse +/// +void AES::invSubBytes(State* state) +{ +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + (*state)[i][j] = kSBoxInverse[(*state)[i][j]]; + } + } +#if MINE_PROFILING + endProfiling(started, "invSubBytes"); +#endif +} + +/// +/// Shifting rows is beautifully explained by diagram +/// that helped in implementation as well +/// +/// Let's say we have state +/// +/// [ df c3 e2 9c ] +/// | 0f ad 1f ca | +/// | 0c 9d 8d fa | +/// [ fe ef cc b2 ] +/// +/// shifting means +/// +/// [ df c3 e2 9c ] +/// 0f | ad 1f ca | +/// 0c 9d | 8d fa | +/// fe ef cc [ b2 ] +/// +/// and filling the spaces with shifted rows +/// +/// [ df c3 e2 9c ] +/// | ad 1f ca 0f | +/// | 8d fa 0c 9d | +/// [ b2 fe ef cc ] +/// +void AES::shiftRows(State *state) +{ +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif + // row 1 + std::swap((*state)[0][1], (*state)[3][1]); + std::swap((*state)[0][1], (*state)[1][1]); + std::swap((*state)[1][1], (*state)[2][1]); + + // row 2 + std::swap((*state)[0][2], (*state)[2][2]); + std::swap((*state)[1][2], (*state)[3][2]); + + // row 3 + std::swap((*state)[0][3], (*state)[1][3]); + std::swap((*state)[2][3], (*state)[3][3]); + std::swap((*state)[0][3], (*state)[2][3]); + +#if MINE_PROFILING + endProfiling(started, "shiftRows"); +#endif +} + +/// +/// This is reverse of shift rows operation +/// +/// Let's say we have state +/// +/// [ df c3 e2 9c ] +/// | ad 1f ca 0f | +/// | 8d fa 0c 9d | +/// [ b2 fe ef cc ] +/// +/// shifting means +/// +/// [ df c3 e2 9c ] +/// | ad 1f ca | 0f +/// | 8d fa | 0c 9d +/// [ b2 | fe ef cc +/// +/// and filling the spaces with shifted rows +/// +/// [ df c3 e2 9c ] +/// | 0f ad 1f ca | +/// | 0c 9d 8d fa | +/// [ fe ef cc b2 ] +/// +void AES::invShiftRows(State *state) +{ +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif + // row 1 + std::swap((*state)[0][1], (*state)[1][1]); + std::swap((*state)[0][1], (*state)[2][1]); + std::swap((*state)[0][1], (*state)[3][1]); + + // row 2 + std::swap((*state)[0][2], (*state)[2][2]); + std::swap((*state)[1][2], (*state)[3][2]); + + // row 3 + std::swap((*state)[0][3], (*state)[3][3]); + std::swap((*state)[0][3], (*state)[2][3]); + std::swap((*state)[0][3], (*state)[1][3]); +#if MINE_PROFILING + endProfiling(started, "intShiftRows"); +#endif +} + +/// +/// Finds the product of {02} and the argument to +/// xtime modulo {1b} +/// +byte AES::xtime(byte x) +{ + return ((x << 1) ^ (((x >> 7) & 1) * 0x11b)); +} + +/// +/// Multiplies numbers in the GF(2^8) field +/// +byte AES::multiply(byte x, byte y) +{ + return (((y & 0x01) * x) ^ + ((y >> 1 & 0x01) * xtime(x)) ^ + ((y >> 2 & 0x01) * xtime(xtime(x))) ^ + ((y >> 3 & 0x01) * xtime(xtime(xtime(x)))) ^ + ((y >> 4 & 0x01) * xtime(xtime(xtime(xtime(x)))))); +} + +/// +/// multiplies in GF(2^8) field selected column from state +/// with constant matrix defined by publication +/// +/// [ 02 03 01 01 ] +/// | 01 02 03 01 | +/// | 01 01 02 03 | +/// [ 03 01 01 02 ] +/// +void AES::mixColumns(State* state) +{ +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif + for (int col = 0; col < 4; ++col) { + Word column = (*state)[col]; + // let's take example from publication, col: [212, 191, 93, 48] + // t == 6 + byte t = column[0] ^ column[1] ^ column[2] ^ column[3]; + // see Sec. 4.2.1 and Sec. 5.1.3 for more details + (*state)[col][0] ^= xtime(column[0] ^ column[1]) ^ t; + (*state)[col][1] ^= xtime(column[1] ^ column[2]) ^ t; + (*state)[col][2] ^= xtime(column[2] ^ column[3]) ^ t; + (*state)[col][3] ^= xtime(column[3] ^ column[0]) ^ t; + } +#if MINE_PROFILING + endProfiling(started, "mixColumns"); +#endif +} + +/// +/// Inverse multiplication with inverse matrix defined on Sec. 5.3.3 +/// +/// [ 0e 0b 0d 09 ] +/// | 09 0e 0b 0d | +/// | 0d 09 0e 0b | +/// [ 0b 0d 09 0e ] +/// +void AES::invMixColumns(State* state) +{ +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif + for (int col = 0; col < 4; ++col) { + Word column = (*state)[col]; + // see Sec. 4.2.1 and Sec. 5.3.3 for more details + (*state)[col][0] = multiply(column[0], 0x0e) ^ multiply(column[1], 0x0b) ^ multiply(column[2], 0x0d) ^ multiply(column[3], 0x09); + (*state)[col][1] = multiply(column[0], 0x09) ^ multiply(column[1], 0x0e) ^ multiply(column[2], 0x0b) ^ multiply(column[3], 0x0d); + (*state)[col][2] = multiply(column[0], 0x0d) ^ multiply(column[1], 0x09) ^ multiply(column[2], 0x0e) ^ multiply(column[3], 0x0b); + (*state)[col][3] = multiply(column[0], 0x0b) ^ multiply(column[1], 0x0d) ^ multiply(column[2], 0x09) ^ multiply(column[3], 0x0e); + } +#if MINE_PROFILING + endProfiling(started, "invMixColumns"); +#endif +} + +void AES::initState(State* state, const ByteArray::const_iterator& begin) +{ + // Pad the input if needed + // 29/08 removed this check because it's not needed as + // this function is only called from private interface + // this removal helped made input pass by ref + /*if (input.size() < kBlockSize) { + std::fill_n(input.end(), kBlockSize - input.size(), 0); + }*/ + + // assign it to state for processing + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + (*state)[i][j] = begin[(kNb * i) + j]; + } + } +} + +ByteArray AES::stateToByteArray(const State *state) +{ + +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif + ByteArray result(kBlockSize); + int k = 0; + for (std::size_t i = 0; i < kNb; ++i) { + for (std::size_t j = 0; j < kNb; ++j) { + result[k++] = (*state)[i][j]; + } + } +#if MINE_PROFILING + endProfiling(started, "stateToByteArray"); +#endif + + return result; +} + +ByteArray AES::generateRandomBytes(const std::size_t len) +{ + ByteArray result(len, 'x'); + std::random_device rd; + std::mt19937 rng(rd()); + std::uniform_int_distribution uni(0, kRandomBytesCount - 1); + std::generate(result.begin(), result.end(), [&] { + return kRandomBytesList[uni(rng)]; + }); + return result; +} + +ByteArray* AES::xorWith(ByteArray* input, const ByteArray* arr) +{ + for (std::size_t i = 0; i < kBlockSize; ++i) { + (*input)[i] ^= (*arr)[i]; + } + return input; +} + +ByteArray* AES::xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end) +{ + int i = 0; + for (auto iter = begin; iter < end; ++iter, ++i) { + (*input)[i] ^= *iter; + } + return input; +} + +ByteArray AES::encryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) +{ + + if (key == nullptr || keySchedule == nullptr) { + throw std::invalid_argument("AES raw encryption requires key"); + } + + State state; + initState(&state, range); + + const uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; + + int round = 0; + + // initial round + addRoundKey(&state, keySchedule, round++); + + +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif + // intermediate round + for (; round < kTotalRounds; ++round) { + subBytes(&state); + shiftRows(&state); + mixColumns(&state); + addRoundKey(&state, keySchedule, round); + } + +#if MINE_PROFILING + endProfiling(started, "rawCipher (intermediate rounds)"); +#endif + + // final round + subBytes(&state); + shiftRows(&state); + addRoundKey(&state, keySchedule, round++); + + return stateToByteArray(&state); + +} + +ByteArray AES::decryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule) +{ + + if (key == nullptr || keySchedule == nullptr) { + throw std::invalid_argument("AES raw decryption requires key"); + } + + State state; + initState(&state, range); + + const uint8_t kTotalRounds = kKeyParams.at(key->size())[1]; + + int round = kTotalRounds; + + // initial round + addRoundKey(&state, keySchedule, round--); + + // intermediate round + for (; round > 0; --round) { + invShiftRows(&state); + invSubBytes(&state); + addRoundKey(&state, keySchedule, round); + invMixColumns(&state); + } + + // final round + invShiftRows(&state); + invSubBytes(&state); + addRoundKey(&state, keySchedule, round); + + return stateToByteArray(&state); +} + +ByteArray AES::resolveInputMode(const std::string& input, Encoding inputMode) +{ + if (inputMode == Encoding::Raw) { + return Base16::fromString(Base16::encode(input)); + } else if (inputMode == Encoding::Base16) { + return Base16::fromString(input); + } + // base64 + return Base16::fromString(Base16::encode(Base64::decode(input))); +} + +std::string AES::resolveOutputMode(const ByteArray& input, Encoding outputMode) +{ + if (outputMode == Encoding::Raw) { + return Base16::toRawString(input); + } else if (outputMode == Encoding::Base16) { + return Base16::encode(input.begin(), input.end()); + } + // base64 + return Base64::encode(input.begin(), input.end()); +} + +std::size_t AES::getPaddingIndex(const ByteArray& byteArr) +{ + char lastChar = byteArr[kBlockSize - 1]; + int c = lastChar & 0xff; + if (c > 0 && c <= kBlockSize) { + bool validPadding = true; + for (int chkIdx = kBlockSize - c; chkIdx < kBlockSize; ++chkIdx) { + if ((byteArr[chkIdx] & 0xff) != c) { + // with openssl we found padding + validPadding = false; + break; + } + } + if (validPadding) { + return kBlockSize - c; + } else { + throw std::runtime_error("Incorrect padding"); + } + } + return kBlockSize; +} + +// public + +ByteArray AES::encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding) +{ + + std::size_t keySize = key->size(); + + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + const std::size_t inputSize = input.size(); + + if (*key != m_key) { + m_keySchedule = keyExpansion(key); + m_key = *key; + } + + ByteArray result; + + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { + ByteArray inputBlock(kBlockSize, 0); + + std::size_t j = 0; + // don't use copy_n as we are setting the values + for (; j < kBlockSize && inputSize > j + i; ++j) { + inputBlock[j] = input[j + i]; + } + + if (pkcs5Padding && j != kBlockSize) { + // PKCS#5 padding + std::fill(inputBlock.begin() + j, inputBlock.end(), kBlockSize - (j % kBlockSize)); + } + + ByteArray outputBlock = encryptSingleBlock(inputBlock.begin() + i, key, &m_keySchedule); + std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); + } + return result; +} + +ByteArray AES::decrypt(const ByteArray& input, const Key* key) +{ + + std::size_t keySize = key->size(); + + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + if (*key != m_key) { + m_keySchedule = keyExpansion(key); + m_key = *key; + } + + const std::size_t inputSize = input.size(); + ByteArray result; + + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { + ByteArray inputBlock(kBlockSize, 0); + + std::size_t j = 0; + // don't use copy_n here as we are setting the values + for (; j < kBlockSize && inputSize > j + i; ++j) { + inputBlock[j] = input[j + i]; + } + ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &m_keySchedule); + + if (i + kBlockSize == inputSize) { + // check padding + j = getPaddingIndex(outputBlock); + } + std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); + } + return result; +} + +ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bool pkcs5Padding) +{ + + std::size_t keySize = key->size(); + + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + if (!iv.empty() && iv.size() != 16) { + throw std::invalid_argument("Invalid IV, it should be same as block size"); + } else if (iv.empty()) { + // generate IV + iv = generateRandomBytes(16); + } + + if (*key != m_key) { + m_keySchedule = keyExpansion(key); + m_key = *key; + } + + const std::size_t inputSize = input.size(); + + ByteArray result; + ByteArray::const_iterator nextXorWithBeg = iv.begin(); + ByteArray::const_iterator nextXorWithEnd = iv.end(); + +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { + ByteArray inputBlock(kBlockSize, 0); + + std::size_t j = 0; + // don't use copy_n as we are setting the values + for (; j < kBlockSize && inputSize > j + i; ++j) { + inputBlock[j] = input[j + i]; + } + if (pkcs5Padding && j != kBlockSize) { + // PKCS#5 padding + std::fill(inputBlock.begin() + j, inputBlock.end(), kBlockSize - (j % kBlockSize)); + } + xorWithRange(&inputBlock, nextXorWithBeg, nextXorWithEnd); + + ByteArray outputBlock = encryptSingleBlock(inputBlock.begin(), key, &m_keySchedule); + std::copy(outputBlock.begin(), outputBlock.end(), std::back_inserter(result)); + nextXorWithBeg = result.end() - kBlockSize; + nextXorWithEnd = result.end(); + } +#if MINE_PROFILING + endProfiling(started, "block encryption"); +#endif + + return result; +} + +ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) +{ + + std::size_t keySize = key->size(); + + // key size validation + if (keySize != 16 && keySize != 24 && keySize != 32) { + throw std::invalid_argument("Invalid AES key size"); + } + + const std::size_t inputSize = input.size(); + + if (inputSize % kBlockSize != 0) { + throw std::invalid_argument("Ciphertext length is not a multiple of block size"); + } + + if (*key != m_key) { + m_keySchedule = keyExpansion(key); + m_key = *key; + } + + ByteArray result; + + ByteArray::const_iterator nextXorWithBeg = iv.begin(); + ByteArray::const_iterator nextXorWithEnd = iv.end(); + +#if MINE_PROFILING + auto started = std::chrono::steady_clock::now(); +#endif + for (std::size_t i = 0; i < inputSize; i += kBlockSize) { + ByteArray inputBlock(kBlockSize, 0); + + std::size_t j = 0; + // don't use copy_n as we are setting the values + for (; j < kBlockSize && inputSize > j + i; ++j) { + inputBlock[j] = input[j + i]; + } + + ByteArray outputBlock = decryptSingleBlock(inputBlock.begin(), key, &m_keySchedule); + + xorWithRange(&outputBlock, nextXorWithBeg, nextXorWithEnd); + + if (i + kBlockSize == inputSize) { + // check padding + j = getPaddingIndex(outputBlock); + } else { + nextXorWithBeg = input.begin() + i; + nextXorWithEnd = input.begin() + i + kBlockSize; + } + + std::copy_n(outputBlock.begin(), j, std::back_inserter(result)); + } +#if MINE_PROFILING + endProfiling(started, "block decryption"); +#endif + return result; +} + +std::string AES::encrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +{ + Key keyArr = Base16::fromString(key); + ByteArray inp = resolveInputMode(input, inputEncoding); + ByteArray result = encrypt(inp, &keyArr, pkcs5Padding); + return resolveOutputMode(result, outputEncoding); +} + +std::string AES::encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +{ + Key keyArr = Base16::fromString(key); + ByteArray inp = resolveInputMode(input, inputEncoding); + ByteArray ivec = Base16::fromString(iv); + bool ivecGenerated = iv.empty(); + ByteArray result = encrypt(inp, &keyArr, ivec, pkcs5Padding); + if (ivecGenerated) { + iv = Base16::encode(ivec.begin(), ivec.end()); + } + return resolveOutputMode(result, outputEncoding); +} + +std::string AES::decrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) +{ + Key keyArr = Base16::fromString(key); + ByteArray inp = resolveInputMode(input, inputEncoding); + ByteArray result = decrypt(inp, &keyArr); + return resolveOutputMode(result, outputEncoding); +} + +std::string AES::decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +{ + Key keyArr = Base16::fromString(key); + ByteArray inp = resolveInputMode(input, inputEncoding); + ByteArray ivec = Base16::fromString(iv); + ByteArray result = decrypt(inp, &keyArr, ivec); + return resolveOutputMode(result, outputEncoding); +} + +std::string AES::generateRandomKey(const std::size_t len) +{ + if (len != 128 && len != 192 && len != 256) { + throw std::invalid_argument("Please choose valid key length of 128, 192 or 256 bits"); + } + ByteArray bytes = generateRandomBytes(len / 8); + return Base16::encode(bytes.begin(), bytes.end()); +} + + +// encryption / decryption with previously provided key + +std::string AES::encr(const std::string& input, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return encrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding, pkcs5Padding); +} + +std::string AES::encr(const std::string& input, std::string& iv, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return encrypt(input, Base16::encode(Base16::toRawString(m_key)), iv, inputEncoding, outputEncoding, pkcs5Padding); +} + +std::string AES::decr(const std::string& input, Encoding inputEncoding, Encoding outputEncoding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return decrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding); +} + +std::string AES::decr(const std::string& input, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return decrypt(input, Base16::encode(Base16::toRawString(m_key)), iv, inputEncoding, outputEncoding); +} + +ByteArray AES::encr(const ByteArray& input, bool pkcs5Padding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return encrypt(input, &m_key, pkcs5Padding); +} + +ByteArray AES::decr(const ByteArray& input) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return decrypt(input, &m_key); +} + +ByteArray AES::encr(const ByteArray& input, ByteArray& iv, bool pkcs5Padding) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return encrypt(input, &m_key, iv, pkcs5Padding); +} + +ByteArray AES::decr(const ByteArray& input, ByteArray& iv) +{ + if (m_key.empty()) { + throw std::runtime_error("Key not set"); + } + return decrypt(input, &m_key, iv); +} + + + +bool ZLib::compressFile(const std::string& gzFilename, const std::string& inputFile) +{ + gzFile out = gzopen(gzFilename.c_str(), "wb"); + if (!out) { + throw std::invalid_argument("Unable to open file [" + gzFilename + "] for writing." + std::strerror(errno)); + } + char buff[kBufferSize]; + std::FILE* in = std::fopen(inputFile.c_str(), "rb"); + std::size_t nRead = 0; + while((nRead = std::fread(buff, sizeof(char), kBufferSize, in)) > 0) { + int bytes_written = gzwrite(out, buff, nRead); + if (bytes_written == 0) { + int err_no = 0; + throw std::runtime_error("Error during compression: " + std::string(gzerror(out, &err_no))); + gzclose(out); + return false; + } + } + gzclose(out); + std::fclose(in); + return true; +} + +std::string ZLib::compressString(const std::string& str) +{ + int compressionlevel = Z_BEST_COMPRESSION; + z_stream zs; + memset(&zs, 0, sizeof(zs)); + + if (deflateInit(&zs, compressionlevel) != Z_OK) { + throw std::runtime_error("Unable to initialize zlib deflate"); + } + + zs.next_in = reinterpret_cast(const_cast(str.data())); + zs.avail_in = str.size(); + + int ret; + char outbuffer[kBufferSize]; + std::string outstring; + + // retrieve the compressed bytes blockwise + do { + zs.next_out = reinterpret_cast(outbuffer); + zs.avail_out = sizeof(outbuffer); + + ret = deflate(&zs, Z_FINISH); + + if (outstring.size() < zs.total_out) { + outstring.append(outbuffer, zs.total_out - outstring.size()); + } + } while (ret == Z_OK); + + deflateEnd(&zs); + + if (ret != Z_STREAM_END) { + throw std::runtime_error("Exception during zlib decompression: (" + std::to_string(ret) + "): " + std::string(zs.msg)); + } + + return outstring; +} + +std::string ZLib::decompressString(const std::string& str) +{ + z_stream zs; + memset(&zs, 0, sizeof(zs)); + + if (inflateInit(&zs) != Z_OK) { + throw std::runtime_error("Unable to initialize zlib inflate"); + } + + zs.next_in = reinterpret_cast(const_cast(str.data())); + zs.avail_in = str.size(); + + int ret; + char outbuffer[kBufferSize]; + std::string outstring; + + do { + zs.next_out = reinterpret_cast(outbuffer); + zs.avail_out = sizeof(outbuffer); + + ret = inflate(&zs, 0); + + if (outstring.size() < zs.total_out) { + outstring.append(outbuffer, zs.total_out - outstring.size()); + } + + } while (ret == Z_OK); + + inflateEnd(&zs); + + if (ret != Z_STREAM_END) { + throw std::runtime_error("Exception during zlib decompression: (" + std::to_string(ret) + "): " + std::string(zs.msg)); + } + + return outstring; +} diff --git a/package/mine.h b/package/mine.h new file mode 100644 index 0000000..286b08d --- /dev/null +++ b/package/mine.h @@ -0,0 +1,1626 @@ +// +// Bismillah ar-Rahmaan ar-Raheem +// +// Mine (Unreleased) +// Single header minimal cryptography library +// +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE +// +// https://github.com/muflihun/mine +// https://muflihun.github.io/mine +// https://muflihun.com +// + +#ifndef MINE_CRYPTO_H +#define MINE_CRYPTO_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace mine { + +using byte = unsigned char; +using ByteArray = std::vector; + +/// +/// \brief Provides base16 encoding / decoding +/// +/// This class also contains some helpers to convert various input types +/// to byte array and also provides public interface to encode +/// the iterators for other containers like vector etc. +/// +class Base16 { +public: + + /// + /// \brief List of valid hex encoding characters + /// + static const std::string kValidChars; + + /// + /// \brief Map for fast lookup corresponding character + /// \see Base64::kDecodeMap + /// + static const std::unordered_map kDecodeMap; + + /// + /// \brief Encodes input to hex encoding + /// + static inline std::string encode(const std::string& raw) noexcept + { + return encode(raw.begin(), raw.end()); + } + + /// + /// \brief Wrapper function to encode single hex char to corresponding byte + /// + static byte encode(const char* e) + { + return static_cast(strtol(e, nullptr, 16)); + } + + /// + /// \brief Encodes input iterator to hex encoding + /// + template + static std::string encode(const Iter& begin, const Iter& end) noexcept + { + std::ostringstream ss; + for (auto it = begin; it < end; ++it) { + encode(*it, ss); + } + return ss.str(); + } + + /// + /// \brief Converts hex stream (e.g, 48656C6C6F) to byte array + /// \param hex String stream e.g, 48656C6C6F (Hello) + /// \return Byte array (mine::ByteArray) containing bytes e.g, 0x48, 0x65, 0x6C, 0x6C, 0x6F + /// \throws invalid_argument if hex is not valid + /// + static ByteArray fromString(const std::string& hex); + + /// + /// \brief Converts byte array to raw string. + /// This does not necessarily has to be base16 array + /// + static std::string toRawString(const ByteArray& byteArr); + + /// + /// \brief Encodes integer to hex + /// + template + static std::string encode(T n) noexcept + { + std::stringstream ss; + int remainder; + while (n != 0) { + remainder = n % 16; + n /= 16; + ss << kValidChars[remainder]; + } + std::string res(ss.str()); + std::reverse(res.begin(), res.end()); + return res; + } + + /// + /// \brief Decodes encoded hex + /// \throws std::invalid_argument if invalid encoding. + /// std::invalid_argument::what() is set accordingly + /// + static std::string decode(const std::string& enc) + { + if (enc.size() % 2 != 0) { + throw std::invalid_argument("Invalid base-16 encoding"); + } + return decode(enc.begin(), enc.end()); + } + + /// + /// \brief Decodes encoding to single integer of type T + /// + template + static T decodeInt(const std::string& e) + { + T result = 0; + for (auto it = e.begin(); it != e.end() && result >= 0; ++it) { + try { + result = ((result << 4) | kDecodeMap.at(*it & 0xff)); + } catch (const std::exception&) { + throw std::runtime_error("Invalid base-16 encoding"); + } + } + return result; + } + + /// + /// \brief Encodes single byte + /// + static inline void encode(char b, std::ostringstream& ss) noexcept + { + int h = (b & 0xff); + ss << kValidChars[(h >> 4) & 0xf] << kValidChars[(h & 0xf)]; + } + +private: + Base16() = delete; + Base16(const Base16&) = delete; + Base16& operator=(const Base16&) = delete; + + /// + /// \brief Decodes input iterator to hex encoding + /// \note User should check for the valid size or use decode(std::string) + /// \throws runtime_error if invalid base16-encoding + /// + template + static std::string decode(const Iter& begin, const Iter& end) + { + std::ostringstream ss; + for (auto it = begin; it != end; it += 2) { + decode(*it, *(it + 1), ss); + } + return ss.str(); + } + + /// + /// \brief Decodes single byte pair + /// + static void decode(char a, char b, std::ostringstream& ss); +}; + +using byte = unsigned char; + +/// +/// \brief Provides base64 encoding / decoding implementation +/// +/// This class also provides public interface to encode +/// the iterators for other containers like vector etc. +/// +/// This also handles 16-bit, 24-bit and 32-bit characters +/// +/// +/// +class Base64 { +public: + + /// + /// \brief List of valid base64 encoding characters + /// + static const char kValidChars[]; + + static const std::unordered_map kDecodeMap; + + /// + /// \brief Padding is must in mine implementation of base64 + /// + static const int kPadding = 64; + + /// + /// \brief Encodes input of length to base64 encoding + /// + static std::string encode(const std::string& raw) noexcept + { + return encode(raw.begin(), raw.end()); + } + + /// + /// \brief Encodes iterators + /// + template + static std::string encode(const Iter& begin, const Iter& end) noexcept + { + std::string padding; + std::stringstream ss; + for (auto it = begin; it < end; it += 3) { + + // + // we use example following example for implementation basis + // Bits 01100001 01100010 01100011 + // 24-bit stream: 011000 010110 001001 100011 + // result indices 24 22 9 35 + // + + int c = static_cast(*it & 0xff); + ss << static_cast(static_cast(kValidChars[(c >> 2) & 0x3f])); // first 6 bits from first bitset + if (it + 1 < end) { + int c2 = static_cast(*(it + 1) & 0xff); + ss << static_cast(kValidChars[((c << 4) | // remaining 2 bits from first bitset - shift them left to get 4-bit spaces 010000 + (c2 >> 4) // first 4 bits of second bitset - shift them right to get 2 spaces and bitwise + // to add them 000110 + ) & 0x3f]); // must be within 63 -- + // 010000 + // 000110 + // --|--- + // 010110 + // 111111 + // ---&-- + // 010110 ==> 22 + if (it + 2 < end) { + int c3 = static_cast(*(it + 2) & 0xff); + ss << static_cast(kValidChars[((c2 << 2) | // remaining 4 bits from second bitset - shift them to get 011000 + (c3 >> 6) // the first 2 bits from third bitset - shift them right to get 000001 + ) & 0x3f]); + // the rest of the explanation is same as above + ss << static_cast(kValidChars[c3 & 0x3f]); // all the remaing bits + } else { + ss << static_cast(kValidChars[(c2 << 2) & 0x3f]); // we have 4 bits left from last byte need space for two 0-bits + ss << "="; + } + } else { + ss << static_cast(kValidChars[(c << 4) & 0x3f]); // remaining 2 bits from single byte + ss << "=="; + } + } + return ss.str() + padding; + } + + /// + /// \brief Decodes encoded base64 + /// \see decode(const Iter&, const Iter&) + /// + static std::string decode(const std::string& e) + { + // don't check for e's length to be multiple of 4 + // because of 76 character line-break format (MIME) + // https://tools.ietf.org/html/rfc4648#section-3.1 + return decode(e.begin(), e.end()); + } + + /// + /// \brief Decodes base64 iterator from begin to end + /// \throws std::invalid_argument if invalid encoding. Another time it is thrown + /// is if no padding is found + /// std::invalid_argument::what() is set according to the error + /// + template + static std::string decode(const Iter& begin, const Iter& end) + { + // + // we use example following example for implementation basis + // Bits 01100001 01100010 01100011 + // 24-bit stream: 011000 010110 001001 100011 + // result indices 24 22 9 35 + // + + auto findPosOf = [](char c) -> int { + try { + return kDecodeMap.at(static_cast(c & 0xff)); + } catch (const std::exception& e) { + throw e; + } + }; + + std::stringstream ss; + for (auto it = begin; it < end; it += 4) { + try { + while (iswspace(*it)) { + ++it; + + if (it >= end) { + goto result; + } + } + int b0 = findPosOf(*it); + if (b0 == kPadding) { + throw std::invalid_argument("No data available"); + } + if (b0 == -1) { + throw std::invalid_argument("Invalid base64 encoding"); + } + + while (iswspace(*(it + 1))) { + ++it; + + if (it >= end) { + goto result; + } + } + int b1 = findPosOf(*(it + 1)); + if (b1 == -1) { + throw std::invalid_argument("Invalid base64 encoding"); + } + + while (iswspace(*(it + 2))) { + ++it; + + if (it >= end) { + goto result; + } + } + int b2 = findPosOf(*(it + 2)); + if (b2 == -1) { + throw std::invalid_argument("Invalid base64 encoding"); + } + + while (iswspace(*(it + 3))) { + ++it; + + if (it >= end) { + goto result; + } + } + int b3 = findPosOf(*(it + 3)); + if (b3 == -1) { + throw std::invalid_argument("Invalid base64 encoding"); + } + + ss << static_cast(b0 << 2 | // 011000 << 2 ==> 01100000 + b1 >> 4); // 000001 >> 4 ==> 01100001 ==> 11000001 = 97 + + if (b1 != kPadding) { + if (b2 == kPadding) { + // second bitset is only 4 bits + } else { + ss << static_cast((b1 & ~(1 << 5) & ~(1 << 4)) << 4 | // 010110 ==> 000110 << 4 ==> 1100000 + // first we clear the bits at pos 4 and 5 + // then we concat with next bit + b2 >> 2); // 001001 >> 2 ==> 00000010 ==> 01100010 = 98 + if (b3 == kPadding) { + // third bitset is only 4 bits + } else { + ss << static_cast((b2 & ~(1 << 5) & ~(1 << 4) & ~(1 << 3) & ~(1 << 2)) << 6 | // 001001 ==> 000001 << 6 ==> 01000000 + // first we clear first 4 bits + // then concat with last byte as is + b3); // as is + } + } + } + } catch (const std::exception& e) { + throw std::invalid_argument(std::string("Invalid base64 encoding: " + std::string(e.what()))); + } + } +result: + return ss.str(); + } + + +#ifdef MINE_BASE64_WSTRING_CONVERSION + /// + /// \brief Converts wstring to corresponding string and returns + /// encoding + /// \see encode(const std::string&) + /// + /// \note You need to include and headers before mine.h + /// + static std::string encode(const std::wstring& raw) noexcept + { + std::string converted = std::wstring_convert + , wchar_t>{}.to_bytes(raw); + return encode(converted); + } + + /// + /// \brief Helper method to decode base64 encoding as wstring (basic_string) + /// \see decode(const std::string&) + /// \note We do not recommend using it, instead have your own conversion function from + /// std::string to wstring as it can give you invalid results with characters that are + /// 5+ bytes long e.g, \x1F680. If you don't use such characters then it should be safe + /// to use this + /// + /// \note You need to include and headers before mine.h + /// + static std::wstring decodeAsWString(const std::string& e) + { + std::string result = decode(e); + std::wstring converted = std::wstring_convert + >{}.from_bytes(result); + return converted; + } + + /// + /// \brief Converts it to std::string and calls countChars on it + /// + /// \note You need to include and headers before mine.h + /// + static std::size_t countChars(const std::wstring& raw) noexcept + { + std::string converted = std::wstring_convert + , wchar_t>{}.to_bytes(raw); + return countChars(converted); + } +#endif + + /// + /// \brief Replacement for better d.size() that consider unicode bytes too + /// \see https://en.wikipedia.org/wiki/UTF-8#Description + /// + static std::size_t countChars(const std::string& d) noexcept; + + /// + /// \brief expectedBase64Length Returns expected base64 length + /// \param n Length of input (plain data) + /// + inline static std::size_t expectedLength(std::size_t n) noexcept + { + return ((4 * n / 3) + 3) & ~0x03; + } + + /// + /// \brief Calculates the length of string + /// \see countChars() + /// + template + inline static std::size_t expectedLength(const T& str) noexcept + { + return expectedLength(countChars(str)); + } + +private: + Base64() = delete; + Base64(const Base64&) = delete; + Base64& operator=(const Base64&) = delete; +}; + +using byte = unsigned char; + +/// +/// \brief Handy safe byte array +/// +using ByteArray = std::vector; + +/// +/// \brief Provides AES crypto functionalities +/// +/// This is validated against NIST test data and all +/// the corresponding tests under test/ directory +/// are from NIST themselves. +/// +/// Please make sure to use public functions and do not +/// use private functions especially in production as +/// you may end up using them incorrectly. However +/// the source code for AES class is heavily commented for +/// verification on implementation. +/// +class AES { +public: + + /// + /// \brief Convert mode for various functions + /// + enum class Encoding { + Raw, + Base16, + Base64 + }; + + /// + /// \brief A key is a byte array + /// + using Key = ByteArray; + + AES() = default; + AES(const std::string& key); + AES(const ByteArray& key); + AES(const AES&); + AES(const AES&&); + AES& operator=(const AES&); + virtual ~AES() = default; + + void setKey(const std::string& key); + void setKey(const ByteArray& key); + + /// + /// \brief Generates random key of valid length + /// + static std::string generateRandomKey(const std::size_t len); + + /// + /// \brief Ciphers the input with specified hex key + /// \param key Hex key + /// \param inputEncoding the type of input. Defaults to Plain + /// \param outputEncoding Type of encoding for cipher + /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used + /// \return Base16 encoded cipher + /// + std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + + /// + /// \brief Ciphers the input with specified hex key using CBC mode + /// \param key Hex key + /// \param iv Initialization vector, passed by reference. If empty a random is generated and passed in + /// \param inputEncoding the type of input. Defaults to Plain + /// \param outputEncoding Type of encoding for cipher + /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used + /// \return Base16 encoded cipher + /// + std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + + /// + /// \brief Deciphers the input with specified hex key + /// \param key Hex key + /// \param inputEncoding the type of input. Defaults to base16 + /// \param outputEncoding Type of encoding for result + /// \return Base16 encoded cipher + /// + std::string decrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + + /// + /// \brief Deciphers the input with specified hex key using CBC mode + /// \param key Hex key + /// \param iv Initialization vector + /// \param inputEncoding the type of input. Defaults to base16 + /// \param outputEncoding Type of encoding for result + /// \return Base16 encoded cipher + /// + std::string decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + + /// + /// \brief Ciphers with ECB-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used + /// \return Cipher text byte array + /// + ByteArray encrypt(const ByteArray& input, const Key* key, bool pkcs5Padding = true); + + /// + /// \brief Deciphers with ECB-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \return Cipher text byte array + /// + ByteArray decrypt(const ByteArray& input, const Key* key); + + /// + /// \brief Ciphers with CBC-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used + /// \return Cipher text byte array + /// + ByteArray encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bool pkcs5Padding = true); + + /// + /// \brief Deciphers with CBC-Mode, the input can be as long as user wants + /// \param input Plain input of any length + /// \param key Pointer to a valid AES key + /// \param iv Initialization vector + /// \return Cipher text byte array + /// + ByteArray decrypt(const ByteArray& input, const Key* key, ByteArray& iv); + + + // cipher / decipher interface without keys + + std::string encr(const std::string& input, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + + std::string encr(const std::string& input, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + + std::string decr(const std::string& input, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + + std::string decr(const std::string& input, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + + ByteArray encr(const ByteArray& input, bool pkcs5Padding = true); + + ByteArray decr(const ByteArray& input); + + ByteArray encr(const ByteArray& input, ByteArray& iv, bool pkcs5Padding = true); + + ByteArray decr(const ByteArray& input, ByteArray& iv); + +private: + + /// + /// \brief A word is array of 4 byte + /// + using Word = std::array; + + /// + /// \brief KeySchedule is linear array of 4-byte words + /// \ref FIPS.197 Sec 5.2 + /// + using KeySchedule = std::map; + + /// + /// \brief State as described in FIPS.197 Sec. 3.4 + /// + using State = std::array; + + /// + /// \brief AES works on 16 bit block at a time + /// + static const uint8_t kBlockSize = 16; + + /// + /// \brief Defines the key params to it's size + /// + static const std::unordered_map> kKeyParams; + + /// + /// \brief Total items in random bytes list + /// + static const int kRandomBytesCount = 256; + + /// + /// \brief List to choose random byte from + /// + static const byte kRandomBytesList[]; + + /// + /// \brief As defined in FIPS. 197 Sec. 5.1.1 + /// + static const byte kSBox[]; + + /// + /// \brief As defined in FIPS. 197 Sec. 5.3.2 + /// + static const byte kSBoxInverse[]; + + /// + /// \brief Round constant is constant for each round + /// it contains 10 values each defined in + /// Appendix A of FIPS.197 in column Rcon[i/Nk] for + /// each key size, we add all of them in one array for + /// ease of access + /// + static const byte kRoundConstant[]; + + /// + /// \brief Nb + /// \note we make it constant as FIPS.197 p.9 says + /// "For this standard, Nb=4." + /// + static const uint8_t kNb = 4; + + + /// rotateWord function is specified in FIPS.197 Sec. 5.2: + /// The function RotWord() takes a + /// word [a0,a1,a2,a3] as input, performs a cyclic permutation, + /// and returns the word [a1,a2,a3,a0]. The + /// round constant word array + /// + /// Our definition: + /// We swap the first byte + /// to last one causing it to shift to the left + /// i.e, + /// [a1] [a2] + /// [a2] [a3] + /// [a3] => [a4] + /// [a4] [a1] + /// + static void rotateWord(Word* w); + + /// this function is also specified in FIPS.197 Sec. 5.2: + /// SubWord() is a function that takes a four-byte + /// input word and applies the S-box + /// to each of the four bytes to produce an output word. + /// + /// Out definition: + /// It's a simple substition with kSbox for corresponding byte + /// index + /// + static void substituteWord(Word* w); + + /// + /// \brief Key expansion function as described in FIPS.197 + /// + static KeySchedule keyExpansion(const Key* key); + + /// + /// \brief Adds round to the state using specified key schedule + /// + static void addRoundKey(State* state, KeySchedule* keySchedule, int round); + + /// + /// \brief Substitution step for state + /// \ref Sec. 5.1.1 + /// + static void subBytes(State* state); + + /// + /// \brief Shifting rows step for the state + /// \ref Sec. 5.1.2 + /// + static void shiftRows(State* state); + + /// + /// \ref Sec. 4.2.1 + /// + static byte xtime(byte x); + + /// + /// \ref Sec. 4.2.1 + /// + static byte multiply(byte x, byte y); + + /// + /// \brief Mixing columns for the state + /// \ref Sec. 5.1.3 + /// + static void mixColumns(State* state); + + /// + /// \brief Transformation in the Inverse Cipher + /// that is the reverse of subBytes() + /// \ref Sec. 5.3.2 + /// + static void invSubBytes(State* state); + + /// + /// \brief Transformation in the Inverse Cipher that is + /// the reverse of shiftRows() + /// \ref Sec. 5.3.1 + /// + static void invShiftRows(State* state); + + /// + /// \brief Transformation in the Inverse Cipher + /// that is the reverse of mixColumns() + /// \ref Sec. 5.3.3 + /// + static void invMixColumns(State* state); + + /// + /// \brief Prints bytes in hex format in 4x4 matrix fashion + /// + static void printBytes(const ByteArray& b); + + /// + /// \brief Prints state for debugging + /// + static void printState(const State*); + + /// + /// \brief Initializes the state with input. This function + /// also pads the input if needed (i.e, input is not block of 128-bit) + /// + static void initState(State* state, const ByteArray::const_iterator& begin); + + /// + /// \brief Generates random bytes of length + /// + static ByteArray generateRandomBytes(const std::size_t len); + + /// + /// \brief Creates byte array from input based on input mode + /// + static ByteArray resolveInputMode(const std::string& input, Encoding inputMode); + + /// + /// \brief Creates string from byte array based on convert mode + /// + static std::string resolveOutputMode(const ByteArray& input, Encoding outputMode); + + /// + /// \brief Exclusive XOR with arr + /// + static ByteArray* xorWith(ByteArray* input, const ByteArray*); + + /// + /// \brief Exclusive XOR with iter of range size as input + /// + static ByteArray* xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end); + + /// + /// \brief Raw encryption function - not for public use + /// \param input 128-bit plain input + /// If array is bigger it's chopped and if it's smaller, it's padded + /// please use alternative functions if your array is bigger. Those + /// function will handle all the bytes correctly. + /// \param key Pointer to a valid AES key + /// \note This does not do any key or input validation + /// \return 128-bit cipher text + /// + static ByteArray encryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); + + /// + /// \brief Raw decryption function - not for public use + /// \param input 128-bit cipher input + /// If array is bigger it's chopped and if it's smaller, it's padded + /// please use alternative functions if your array is bigger. Those + /// function will handle all the bytes correctly. + /// \param key Byte array of key + /// \return 128-bit plain text + /// + static ByteArray decryptSingleBlock(const ByteArray::const_iterator& range, const Key* key, KeySchedule* keySchedule); + + /// + /// \brief Converts 4x4 byte state matrix in to linear 128-bit byte array + /// + static ByteArray stateToByteArray(const State* state); + + /// + /// \brief Get padding index for stripping the padding (unpadding) + /// + static std::size_t getPaddingIndex(const ByteArray& byteArr); + + Key m_key; // to keep track of key differences + KeySchedule m_keySchedule; + + // for tests + friend class AESTest_RawCipher_Test; + friend class AESTest_RawCipherPlain_Test; + friend class AESTest_RawCipherBase64_Test; + friend class AESTest_RawSimpleCipher_Test; + friend class AESTest_RawSimpleDecipher_Test; + friend class AESTest_SubByte_Test; + friend class AESTest_InvSubByte_Test; + friend class AESTest_ShiftRows_Test; + friend class AESTest_InvShiftRows_Test; + friend class AESTest_MixColumns_Test; + friend class AESTest_InvMixColumns_Test; + friend class AESTest_KeyExpansion_Test; + friend class AESTest_AddRoundKey_Test; + friend class AESTest_CbcCipher_Test; + friend class AESTest_Copy_Test; +}; + +/// Here onwards start implementation for RSA - this contains +/// generic classes (templates). +/// User will provide their own implementation of big integer +/// or use existing one. +/// +/// Compliant with PKCS#1 (v2.1) +/// https://tools.ietf.org/html/rfc3447#section-7.2 +/// +/// Mine uses pkcs#1 v1.5 padding scheme +/// +/// Big integer must support have following functions implemented +/// - operator-() [subtraction] +/// - operator+() [addition] +/// - operator+=() [short-hand addition] +/// - operator*() [multiply] +/// - operator/() [divide] +/// - operator%() [mod] +/// - operator>>() [right-shift] +/// - operator>>=() [short-hand right-shift] +/// +/// Also you must provide proper implementation to Helper class +/// which will extend BigIntegerHelper and must implement +/// BigIntegerHelper::bigIntegerToByte +/// function. The base function returns empty byte. +/// + + +/// +/// \brief Default exponent for RSA public key +/// +static const unsigned int kDefaultPublicExponent = 65537; + +/// +/// \brief Declaration for byte in case it's not already included +/// +using byte = unsigned char; + +/// +/// \brief Simple raw string (a.k.a octet string) +/// +using RawString = std::vector; + +/// +/// \brief Contains helper functions for RSA throughout +/// +template +class BigIntegerHelper { +public: + + static const BigInteger kBigInteger256; + + BigIntegerHelper() = default; + virtual ~BigIntegerHelper() = default; + + /// + /// \brief Implementation for (a ^ -1) mod b + /// + virtual BigInteger modInverse(BigInteger a, BigInteger b) const + { + BigInteger b0 = b, t, q; + BigInteger x0 = 0, x1 = 1; + if (b == 1) { + return 1; + } + while (a > 1) { + q = a / b; + t = b; + b = a % b; + a = t; + t = x0; + x0 = x1 - q * x0; + x1 = t; + } + if (x1 < 0) { + x1 += b0; + } + return x1; + } + + /// + /// \brief Fast GCD + /// \see https://en.wikipedia.org/wiki/Euclidean_algorithm#Extended_Euclidean_algorithm + /// + virtual BigInteger gcd(BigInteger a, BigInteger b) const + { + BigInteger c; + while (a != 0) { + c = a; + a = b % a; + b = c; + } + return b; + } + + /// + /// \brief Simple (b ^ e) mod m implementation + /// \param b Base + /// \param e Exponent + /// \param m Mod + /// + virtual BigInteger powerMod(BigInteger b, BigInteger e, BigInteger m) const + { + BigInteger res = 1; + while (e > 0) { + if (e % 2 != 0) { + res = (b * res) % m; + } + b = (b * b) % m; + e /= 2; + } + return res; + } + + /// + /// \brief Power of numb i.e, b ^ e + /// + virtual BigInteger power(BigInteger b, BigInteger e) const + { + BigInteger result = 1; + while (e > 0) { + if (e % 2 == 1) { + // we decrement exponent to make it even + e--; + // store this multiplication directly to the + // result + result *= b; + // we modify this alg to ignore the next multiplication + // if we have already reached 0 (for speed) + // here are details and what we changed and how it all works + // + // Let's say we have case of 2 ^ 4 [expected answer = 16] + // 2 ^ 4 -- b = 4, e = 2 [result = 1] + // 2 ^ 2 -- b = 16, e = 1 [result = 1] + // 2 ^ 1 -- e = 0 [result = 1 * 16] + // + // here is what we changed here + // now we have result set and we have e set to zero + // doing another b ^= b means b = 16 * 16 = 256 (in our case) + // which is useless so we end here + if (e == 0) { + break; + } + } + e /= 2; + b *= b; + } + return result; + } + + /// + /// \brief Counts number of bits in big integer + /// + virtual unsigned int countBits(BigInteger b) const + { + unsigned int bits = 0; + while (b > 0) { + bits++; + b >>= 1; + } + return bits; + } + + /// + /// \brief Count number of bytes in big integer + /// + virtual inline unsigned int countBytes(BigInteger b) const + { + return countBits(b) * 8; + } + + /// + /// Raw-string to integer (a.k.a os2ip) + /// + BigInteger rawStringToInteger(const RawString& x) const + { + BigInteger result = 0; + std::size_t len = x.size(); + for (std::size_t i = len; i > 0; --i) { + result += BigInteger(x[i - 1]) * power(kBigInteger256, BigInteger(len - i)); + } + return result; + } + + /// + /// \brief Convert integer to raw string + /// (this func is also known as i2osp) + /// + RawString integerToRaw(BigInteger x, int xlen = -1) const + { + xlen = xlen == -1 ? countBytes(x) : xlen; + + RawString ba(xlen); + BigInteger r; + BigInteger q; + + int i = 1; + + for (; i <= xlen; ++i) { + divideBigNumber(x, power(kBigInteger256, BigInteger(xlen - i)), &q, &r); + ba[i - 1] = bigIntegerToByte(q); + x = r; + } + return ba; + } + + /// + /// \brief Divides big number + /// You may override this function and call custom divisor from big integer class + /// you are using. + /// Result should be stored in quotient and remainder + /// + virtual void divideBigNumber(const BigInteger& divisor, const BigInteger& divident, + BigInteger* quotient, BigInteger* remainder) const + { + *quotient = divisor / divident; + *remainder = divisor % divident; + } + + /// + /// \brief Absolutely must override this - conversion from x to single byte + /// + virtual inline byte bigIntegerToByte(const BigInteger&) const + { + return static_cast(0); + } + + /// + /// \brief Converts big integer to hex + /// + virtual std::string bigIntegerToHex(BigInteger n) const + { + return Base16::encode(n); + } + + /// + /// \brief Converts big integer to hex + /// + virtual std::string bigIntegerToString(const BigInteger& b) const + { + std::stringstream ss; + ss << b; + return ss.str(); + } + + /// + /// \brief Converts hex to big integer + /// \param hex Hexadecimal without '0x' prefix + /// + virtual BigInteger hexToBigInteger(const std::string& hex) const + { + std::string readableMsg = "0x" + hex; + BigInteger msg; + std::istringstream iss(readableMsg); + iss >> std::hex >> msg; + return msg; + } +private: + BigIntegerHelper(const BigIntegerHelper&) = delete; + BigIntegerHelper& operator=(const BigIntegerHelper&) = delete; +}; + +/// +/// \brief Big Integer = 256 (static declaration) +/// +template +const BigInteger BigIntegerHelper::kBigInteger256 = 256; + +/// +/// \brief Public key object with generic big integer +/// +template > +class GenericPublicKey { +public: + + GenericPublicKey() = default; + + GenericPublicKey(const GenericPublicKey& other) + { + this->m_n = other.m_n; + this->m_e = other.m_e; + this->m_k = other.m_k; + } + + GenericPublicKey& operator=(const GenericPublicKey& other) + { + if (this != &other) { + this->m_n = other.m_n; + this->m_e = other.m_e; + this->m_k = other.m_k; + } + return *this; + } + + GenericPublicKey(BigInteger n, int e) : + m_n(n), + m_e(e) + { + m_k = m_helper.countBytes(m_n); + if (m_k < 11) { + throw std::invalid_argument("Invalid prime. Length error."); + } + } + + virtual ~GenericPublicKey() = default; + + inline BigInteger n() const { return m_n; } + inline int e() const { return m_e; } + inline unsigned int k() const { return m_k; } + +protected: + BigIntegerHelper m_helper; + BigInteger m_n; + int m_e; + unsigned int m_k; +}; + +/// +/// \brief Private key object with generic big integer +/// +template > +class GenericPrivateKey { +public: + + GenericPrivateKey() = default; + + GenericPrivateKey(const GenericPrivateKey& other) + { + this->m_p = other.m_p; + this->m_q = other.m_q; + this->m_e = other.m_e; + this->m_n = other.m_n; + this->m_d = other.m_d; + this->m_coeff = other.m_coeff; + this->m_dp = other.m_dp; + this->m_dq = other.m_dq; + this->m_k = other.m_k; + } + + GenericPrivateKey& operator=(const GenericPrivateKey& other) + { + if (this != &other) { + this->m_p = other.m_p; + this->m_q = other.m_q; + this->m_e = other.m_e; + this->m_n = other.m_n; + this->m_d = other.m_d; + this->m_coeff = other.m_coeff; + this->m_dp = other.m_dp; + this->m_dq = other.m_dq; + this->m_k = other.m_k; + } + return *this; + } + + GenericPrivateKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) : + m_p(p), + m_q(q), + m_e(e) + + { + if (p == q || p == 0 || q == 0) { + throw std::invalid_argument("p and q must be prime numbers unique to each other"); + } + + const BigInteger pMinus1 = m_p - 1; + const BigInteger qMinus1 = m_q - 1; + const BigInteger phi = pMinus1 * qMinus1; + + if (m_helper.gcd(m_e, phi) != 1) { + throw std::invalid_argument("Invalid exponent, it must not share factor with phi"); + } + m_n = m_p * m_q; + m_k = m_helper.countBytes(m_n); + if (m_k < 11) { + throw std::invalid_argument("Invalid prime. Length error."); + } + m_coeff = m_helper.modInverse(m_q, m_p); + + m_d = m_helper.modInverse(m_e, phi); + + // note: + // https://tools.ietf.org/html/rfc3447#section-2 says to use m_e + // openssl says to use m_d - which one?! + // + m_dp = BigInteger(m_d) % pMinus1; + m_dq = BigInteger(m_d) % qMinus1; + } + + virtual ~GenericPrivateKey() = default; + + inline BigInteger p() const { return m_p; } + inline BigInteger q() const { return m_q; } + inline BigInteger coeff() const { return m_coeff; } + inline BigInteger n() const { return m_n; } + inline int e() const { return m_e; } + inline BigInteger d() const { return m_d; } + inline BigInteger dp() const { return m_dq; } + inline BigInteger dq() const { return m_dp; } + inline int k() const { return m_k; } + + friend std::ostream& operator<<(std::ostream& ss, const GenericPrivateKey& k) + { + ss << "modulus: " << k.m_n << "\npublicExponent: " << k.m_e << "\nprivateExponent: " << k.m_d + << "\nprime1: " << k.m_p << "\nprime2: " << k.m_q << "\nexponent1: " << k.m_dp << "\nexponent2: " + << k.m_dq << "\ncoefficient: " << k.m_coeff; + return ss; + } + + /// + /// \brief You can use this to export the key via + /// openssl-cli using + /// openssl asn1parse -genconf exported.asn -out imp.der + /// openssl rsa -in imp.der -inform der -text -check + /// \return + /// + virtual std::string exportASNSequence() const + { + std::stringstream ss; + ss << "asn1=SEQUENCE:rsa_key\n\n"; + ss << "[rsa_key]\n"; + ss << "version=INTEGER:0\n"; + ss << "modulus=INTEGER:" << m_helper.bigIntegerToString(m_n) << "\n"; + ss << "pubExp=INTEGER:" << m_e << "\n"; + ss << "privExp=INTEGER:" << m_helper.bigIntegerToString(m_d) << "\n"; + ss << "p=INTEGER:" << m_helper.bigIntegerToString(m_p) << "\n"; + ss << "q=INTEGER:" << m_helper.bigIntegerToString(m_q) << "\n"; + ss << "e1=INTEGER:" << m_helper.bigIntegerToString(m_dp) << "\n"; + ss << "e2=INTEGER:" << m_helper.bigIntegerToString(m_dq) << "\n"; + ss << "coeff=INTEGER:" << m_helper.bigIntegerToString(m_coeff); + return ss.str(); + } +protected: + Helper m_helper; + BigInteger m_p; + BigInteger m_q; + int m_e; + BigInteger m_coeff; + BigInteger m_n; + BigInteger m_d; + BigInteger m_dp; + BigInteger m_dq; + unsigned int m_k; +}; + +/// +/// \brief Key pair (containing public and private key objects) with generic big integer +/// +template > +class GenericKeyPair { +public: + GenericKeyPair() = default; + + GenericKeyPair(const GenericKeyPair& other) + { + this->m_privateKey = other.m_privateKey; + this->m_publicKey = other.m_publicKey; + } + + GenericKeyPair& operator=(const GenericKeyPair& other) + { + if (this != &other) { + this->m_privateKey = other.m_privateKey; + this->m_publicKey = other.m_publicKey; + } + return *this; + } + + GenericKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) + { + m_publicKey = GenericPublicKey(p * q, exp); + m_privateKey = GenericPrivateKey(p, q, exp); + } + + virtual ~GenericKeyPair() = default; + + inline const GenericPublicKey* publicKey() const { return &m_publicKey; } + inline const GenericPrivateKey* privateKey() const { return &m_privateKey; } + +protected: + GenericPublicKey m_publicKey; + GenericPrivateKey m_privateKey; +}; + +/// +/// \brief Provides RSA crypto functionalities +/// +template > +class GenericRSA { +public: + + using PublicKey = GenericPublicKey; + using PrivateKey = GenericPrivateKey; + + GenericRSA() = default; + GenericRSA(const GenericRSA&) = delete; + GenericRSA& operator=(const GenericRSA&) = delete; + + /// + /// \brief Encrypts plain bytes using RSA public key + /// \param publicKey RSA Public key for encryption + /// \param m The message. This can be raw bytes or plain text + /// T can of std::string or std::wstring or custom string type that has + /// basic_stringstream implementation alongside it + /// \note Mine uses pkcs#1 padding scheme + /// \return hex of cipher + /// + template + std::string encrypt(const PublicKey* publicKey, const T& m) + { + BigInteger paddedMsg = pkcs1pad2(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); + BigInteger cipher = m_helper.powerMod(paddedMsg, publicKey->e(), publicKey->n()); + return m_helper.bigIntegerToHex(cipher); + } + + /// + /// \brief Helper method to encrypt wide-string messages using public key. + /// \see encrypt(const GenericPublicKey* publicKey, const T& m) + /// + inline std::string encrypt(const PublicKey* publicKey, + const std::wstring& message) + { + return encrypt(publicKey, message); + } + + /// + /// \brief Helper method to encrypt std::string messages using public key. + /// \see encrypt(const GenericPublicKey* publicKey, const T& m) + /// + inline std::string encrypt(const PublicKey* publicKey, + const std::string& message) + { + return encrypt(publicKey, message); + } + + /// + /// \brief Decrypts RSA hex message using RSA private key + /// \param privateKey RSA private key + /// \param c Cipher in hex format (should not start with 0x) + /// \return Plain result of TResult type + /// + template + TResult decrypt(const PrivateKey* privateKey, const std::string& c) + { + BigInteger msg = m_helper.hexToBigInteger(c); + int xlen = (m_helper.countBits(privateKey->n()) + 7) >> 3; + if (msg >= m_helper.power(BigInteger(256), BigInteger(xlen))) { + throw std::runtime_error("Integer too large"); + } + BigInteger decr = m_helper.powerMod(msg, privateKey->d(), privateKey->n()); + RawString rawStr = m_helper.integerToRaw(decr, xlen); + return pkcs1unpad2(rawStr); + } + + /// + /// \brief Verifies signature for text using RSA public key + /// \param message An octet string + /// \param signature Signature in hex + /// \see https://tools.ietf.org/html/rfc3447#section-8.1.2 + /// + bool verify(const PublicKey*, const std::string&, const std::string&) + { + + return true; + } + + /// + /// \brief Maximum size of RSA block with specified key size + /// \param keySize 2048, 1024, ... + /// + inline static unsigned int maxRSABlockSize(std::size_t keySize) + { + return (keySize / 8) - 11; + } + + /// + /// \brief Minimum size of RSA key to encrypt data of dataSize size + /// + inline static unsigned int minRSAKeySize(std::size_t dataSize) + { + return (dataSize + 11) * 8; + } + +private: + Helper m_helper; + + /// + /// \brief PKCS #1 padding + /// \see https://tools.ietf.org/html/rfc3447#page-23 + /// \return corresponding nonnegative integer + /// + template + BigInteger pkcs1pad2(const T& s, std::size_t n) { + if (n < s.size() + 11) { + throw std::runtime_error("Message too long"); + } + RawString byteArray(n); + long long i = s.size() - 1; + while(i >= 0 && n > 0) { + int c = static_cast(s.at(i--)); + if (c <= 0x7f) { + // utf + byteArray[--n] = c; + } else if (c <= 0x7ff) { + byteArray[--n] = (c & 0x3f) | 128; + byteArray[--n] = (c >> 6) | 192; + } else if (c <= 0xffff) { + // utf-16 + byteArray[--n] = (c & 0x3f) | 128; + byteArray[--n] = ((c >> 6) & 63) | 128; + byteArray[--n] = (c >> 12) | 224; + } else { + // utf-32 + byteArray[--n] = (c & 0x3f) | 128; + byteArray[--n] = ((c >> 6) & 0x3f) | 128; + byteArray[--n] = ((c >> 12) & 0x3f) | 128; + byteArray[--n] = (c >> 18) | 240; + } + } + + // now padding i.e, 0x00 || 0x02 || PS || 0x00 + // see point #2 on https://tools.ietf.org/html/rfc3447#section-7.2.1 => EME-PKCS1-v1_5 encoding + + const int kLengthOfRandom = 127; + + byteArray[--n] = 0; + + srand(time(nullptr)); + int r = rand() % kLengthOfRandom + 1; + while (n > 2) { + r = 0; + while (r == 0) { + r = rand() % kLengthOfRandom + 1; + } + byteArray[--n] = r; + } + // first two bytes of padding are 0x2 (second) and 0x0 (first) + byteArray[--n] = 2; + byteArray[--n] = 0; + return m_helper.rawStringToInteger(byteArray); + } + + /// + /// \brief PKCS #1 unpadding + /// \see https://tools.ietf.org/html/rfc3447#section-4.1 + /// \return corresponding octet string of length n + /// + template + T pkcs1unpad2(const RawString& ba) + { + std::size_t baLen = ba.size(); + if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { + throw std::runtime_error("Incorrect padding PKCS#1"); + } + std::size_t i = 2; // passed first two characters (0x0 and 0x2) test + // lets check for the + + // if we hit end while still we're still with non-zeros, it's a padding error + // 0x0 (done) | 0x2 (done) | | 0x0 + while (ba[i] != 0) { + if (++i >= baLen) { // already ended! + throw std::runtime_error("Incorrect padding PKCS#1"); + } + } + // last zero + ++i; + + // now we should be at the first non-zero byte + // which is our first item, concat them as char | wchar_t + + using CharacterType = typename T::value_type; + std::basic_stringstream ss; + + for (; i < baLen; ++i) { + // reference: http://en.cppreference.com/w/cpp/language/types -> range of values + int c = ba[i] & 0xff; + if (c <= 0x7f) { + ss << static_cast(c); + } else if (c > 0xbf && c < 0xe0) { + ss << static_cast( + ((c & 0x1f) << 6) | + (ba[i+1] & 0x3f) + ); + ++i; + } else if ((c < 0xbf) || (c >= 0xe0 && c < 0xf0)) { // utf-16 char + ss << static_cast( + ((c & 0xf) << 12) | + ((ba[i+1] & 0x3f) << 6) | + (ba[i+2] & 0x3f) + ); + i += 2; + } else { // utf-32 char + ss << static_cast( + ((c & 0x7) << 18) | + ((ba[i+1] & 0x3f) << 12) | + ((ba[i+2] & 0x3f) << 6) | + (ba[i+3] & 0x3f) + ); + i += 3; + } + } + return ss.str(); + } + + /// + /// \brief Creates RSA VP for verification + /// \param signature signature representative, an integer between 0 and n - 1 + /// \return message representative, an integer between 0 and n - 1 + /// \see https://tools.ietf.org/html/rfc3447#section-5.2.2 + /// + BigInteger createVerificationPrimitive(const PublicKey* publicKey, const BigInteger& signature) + { + if (signature < 0 || signature > publicKey->n() - 1) { + throw std::runtime_error("signature representative out of range"); + } + return m_helper.powerMod(signature, publicKey->e(), publicKey->n()); + } + + // for tests + friend class RSATest_Signature_Test; + friend class RSATest_Decryption_Test; + friend class RSATest_KeyAndEncryptionDecryption_Test; + friend class RSATest_PowerMod_Test; +}; + + +/// +/// \brief Provides Zlib functionality for inflate and deflate +/// +class ZLib { +public: + + /// + /// \brief Size of buffer algorithm should operate under + /// + static const int kBufferSize = 32768; + + /// + /// \brief Compress input file (path) and create new file + /// \param gzFilename Output file path + /// \param inputFile Input file path + /// \return True if successful, otherwise false + /// + static bool compressFile(const std::string& gzFilename, const std::string& inputFile); + + /// + /// @brief Compresses string using zlib (inflate) + /// @param str Input plain text + /// @return Raw output (binary) + /// + static std::string compressString(const std::string& str); + + /// + /// @brief Decompresses string using zlib (deflate) + /// @param str Raw input + /// @return Plain output + /// + static std::string decompressString(const std::string& str); +private: + ZLib() = delete; + ZLib(const ZLib&) = delete; + ZLib& operator=(const ZLib&) = delete; +}; + +} // namespace mine +#endif // MINE_CRYPTO_H From 20a6b1a823896220bb9e282103e7090897cf224f Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 3 Sep 2017 11:46:20 +1000 Subject: [PATCH 133/148] install headers and source --- CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0645831..ace7a92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,14 @@ set(MINE_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include" CACHE PATH "The d include_directories (${CMAKE_BINARY_DIR}) include_directories (${CMAKE_SOURCE_DIR}) +install(FILES + package/mine.h + package/mine.cc + DESTINATION "${MINE_INCLUDE_INSTALL_DIR}" + COMPONENT dev +) + + include(FindPackageHandleStandardArgs) # We need C++11 From 3aa9e858eefb032605e0cc0aecc2658885b5068c Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 3 Sep 2017 12:00:03 +1000 Subject: [PATCH 134/148] include --- package/mine.cc | 2 +- src/base16.cc | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/package/mine.cc b/package/mine.cc index c8648f4..3e05859 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -13,6 +13,7 @@ // https://muflihun.github.io/mine // https://muflihun.com // +#include #include #include #include @@ -20,7 +21,6 @@ #include #include #include -#include #include #include #include diff --git a/src/base16.cc b/src/base16.cc index 62f75f2..447a9b3 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -14,6 +14,7 @@ // https://github.com/muflihun/mine // +#include #include #include #include "src/base16.h" From 4516bf9afbb9b915ff1b89a55b9d5be10af3245c Mon Sep 17 00:00:00 2001 From: Majid Khan Date: Sun, 3 Sep 2017 12:04:56 +1000 Subject: [PATCH 135/148] stdexcept --- src/zlib.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/zlib.cc b/src/zlib.cc index 94f05af..21498f0 100644 --- a/src/zlib.cc +++ b/src/zlib.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include "src/zlib.h" From 2f55371c835131effa53e6dabee53c6837d9e445 Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 4 Sep 2017 10:34:53 +1000 Subject: [PATCH 136/148] build pack --- package/mine.h | 8 ++++---- src/rsa.h | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/package/mine.h b/package/mine.h index 286b08d..220fff0 100644 --- a/package/mine.h +++ b/package/mine.h @@ -1368,7 +1368,7 @@ class GenericRSA { template std::string encrypt(const PublicKey* publicKey, const T& m) { - BigInteger paddedMsg = pkcs1pad2(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); + BigInteger paddedMsg = addPadding(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); BigInteger cipher = m_helper.powerMod(paddedMsg, publicKey->e(), publicKey->n()); return m_helper.bigIntegerToHex(cipher); } @@ -1409,7 +1409,7 @@ class GenericRSA { } BigInteger decr = m_helper.powerMod(msg, privateKey->d(), privateKey->n()); RawString rawStr = m_helper.integerToRaw(decr, xlen); - return pkcs1unpad2(rawStr); + return removePadding(rawStr); } /// @@ -1450,7 +1450,7 @@ class GenericRSA { /// \return corresponding nonnegative integer /// template - BigInteger pkcs1pad2(const T& s, std::size_t n) { + BigInteger addPadding(const T& s, std::size_t n) { if (n < s.size() + 11) { throw std::runtime_error("Message too long"); } @@ -1506,7 +1506,7 @@ class GenericRSA { /// \return corresponding octet string of length n /// template - T pkcs1unpad2(const RawString& ba) + T removePadding(const RawString& ba) { std::size_t baLen = ba.size(); if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { diff --git a/src/rsa.h b/src/rsa.h index cd391ee..2c7d8fb 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -540,7 +540,7 @@ class GenericRSA { template std::string encrypt(const PublicKey* publicKey, const T& m) { - BigInteger paddedMsg = pkcs1pad2(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); + BigInteger paddedMsg = addPadding(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); BigInteger cipher = m_helper.powerMod(paddedMsg, publicKey->e(), publicKey->n()); return m_helper.bigIntegerToHex(cipher); } @@ -581,7 +581,7 @@ class GenericRSA { } BigInteger decr = m_helper.powerMod(msg, privateKey->d(), privateKey->n()); RawString rawStr = m_helper.integerToRaw(decr, xlen); - return pkcs1unpad2(rawStr); + return removePadding(rawStr); } /// @@ -622,7 +622,7 @@ class GenericRSA { /// \return corresponding nonnegative integer /// template - BigInteger pkcs1pad2(const T& s, std::size_t n) { + BigInteger addPadding(const T& s, std::size_t n) { if (n < s.size() + 11) { throw std::runtime_error("Message too long"); } @@ -678,7 +678,7 @@ class GenericRSA { /// \return corresponding octet string of length n /// template - T pkcs1unpad2(const RawString& ba) + T removePadding(const RawString& ba) { std::size_t baLen = ba.size(); if (baLen <= 2 || ba[0] != 0 || ba[1] != 2) { From 5fc8edb9ded9073e4b09aa009abfa3e0758f652f Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 4 Sep 2017 11:19:10 +1000 Subject: [PATCH 137/148] mine-common --- CMakeLists.txt | 4 +- build.php | 2 + package/mine.cc | 125 +++++++++++++++++++++------------------------ package/mine.h | 105 +++++++++++++++++++------------------ src/aes.cc | 45 ++-------------- src/aes.h | 28 +--------- src/base16.h | 4 +- src/base64.cc | 24 --------- src/base64.h | 27 ++-------- src/mine-common.cc | 75 +++++++++++++++++++++++++++ src/mine-common.h | 88 +++++++++++++++++++++++++++++++ test/main.cc | 1 + 12 files changed, 291 insertions(+), 237 deletions(-) create mode 100644 src/mine-common.cc create mode 100644 src/mine-common.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ace7a92..efd9279 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -106,6 +106,7 @@ endif(ZLIB_FOUND) ########################################## CLI Tool ################################### add_executable (mine-cli cli/mine.cc + src/mine-common.cc src/base64.cc src/base16.cc src/aes.cc @@ -148,6 +149,7 @@ if (test_main_header) else() add_executable(mine-unit-tests test/main.cc + src/mine-common.cc src/rsa.cc src/aes.cc src/base16.cc @@ -163,7 +165,7 @@ target_link_libraries(mine-unit-tests if (test_wstring_conversions) target_compile_definitions (mine-unit-tests PUBLIC - MINE_BASE64_WSTRING_CONVERSION + MINE_WSTRING_CONVERSION ) endif() diff --git a/build.php b/build.php index dc48233..4a95cdc 100644 --- a/build.php +++ b/build.php @@ -79,6 +79,7 @@ function resolveTemplate($template, $includes, $lines, $lib_version, $filename) } $headers_list = array( + "src/mine-common.h", "src/base16.h", "src/base64.h", "src/aes.h", @@ -120,6 +121,7 @@ function resolveTemplate($template, $includes, $lines, $lib_version, $filename) // source file $source_list = array( + "src/mine-common.cc", "src/base16.cc", "src/base64.cc", "src/aes.cc", diff --git a/package/mine.cc b/package/mine.cc index 3e05859..1625d9d 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -13,6 +13,7 @@ // https://muflihun.github.io/mine // https://muflihun.com // +#include #include #include #include @@ -21,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -31,6 +31,62 @@ using namespace mine; +const byte MineCommon::kRandomBytesList[256] = { + 0x6f, 0x48, 0x15, 0x46, 0x77, 0x58, 0x05, 0x0b, 0x02, 0x6f, 0x20, 0x66, 0x18, 0x5a, 0x17, 0x27, + 0x45, 0x6c, 0x0f, 0x33, 0x08, 0x58, 0x2a, 0x54, 0x75, 0x53, 0x1e, 0x2a, 0x09, 0x13, 0x0f, 0x20, + 0x49, 0x49, 0x4b, 0x18, 0x3c, 0x1f, 0x06, 0x0e, 0x58, 0x52, 0x7c, 0x25, 0x58, 0x7d, 0x33, 0x27, + 0x14, 0x47, 0x66, 0x3f, 0x68, 0x66, 0x49, 0x27, 0x77, 0x10, 0x33, 0x26, 0x6c, 0x34, 0x10, 0x4e, + 0x10, 0x48, 0x07, 0x7c, 0x11, 0x06, 0x60, 0x61, 0x28, 0x29, 0x47, 0x5b, 0x3b, 0x16, 0x75, 0x74, + 0x14, 0x4a, 0x4a, 0x78, 0x21, 0x35, 0x77, 0x50, 0x17, 0x74, 0x3c, 0x26, 0x05, 0x31, 0x65, 0x36, + 0x48, 0x3c, 0x29, 0x4c, 0x1e, 0x78, 0x5e, 0x51, 0x16, 0x7f, 0x0b, 0x6d, 0x14, 0x41, 0x6e, 0x15, + 0x35, 0x7a, 0x4c, 0x59, 0x52, 0x5e, 0x0c, 0x22, 0x29, 0x7d, 0x6f, 0x0b, 0x73, 0x55, 0x0c, 0x44, + 0x3d, 0x70, 0x15, 0x33, 0x71, 0x23, 0x34, 0x77, 0x39, 0x68, 0x04, 0x6d, 0x2b, 0x4b, 0x52, 0x4d, + 0x30, 0x03, 0x38, 0x09, 0x5b, 0x58, 0x09, 0x5f, 0x4b, 0x54, 0x5d, 0x53, 0x35, 0x6b, 0x48, 0x43, + 0x3e, 0x58, 0x7d, 0x48, 0x7e, 0x6d, 0x71, 0x28, 0x14, 0x0e, 0x41, 0x58, 0x20, 0x7b, 0x48, 0x14, + 0x1f, 0x68, 0x07, 0x6d, 0x62, 0x4a, 0x72, 0x34, 0x7d, 0x66, 0x3e, 0x42, 0x79, 0x47, 0x36, 0x11, + 0x37, 0x08, 0x1f, 0x0a, 0x08, 0x2f, 0x66, 0x11, 0x2b, 0x0e, 0x03, 0x33, 0x14, 0x66, 0x25, 0x3e, + 0x08, 0x6f, 0x6e, 0x69, 0x71, 0x1e, 0x1c, 0x02, 0x09, 0x0a, 0x45, 0x24, 0x73, 0x58, 0x4b, 0x43, + 0x5a, 0x53, 0x4f, 0x0e, 0x39, 0x71, 0x13, 0x0c, 0x02, 0x46, 0x66, 0x2a, 0x56, 0x4c, 0x2b, 0x37, + 0x34, 0x45, 0x6e, 0x01, 0x4d, 0x12, 0x35, 0x4a, 0x29, 0x66, 0x30, 0x5b, 0x31, 0x4f, 0x6e, 0x3d +}; + +std::size_t MineCommon::countChars(const std::string& str) noexcept +{ + std::size_t result = 0UL; + for (auto it = str.begin(); it <= str.end();) { + int c = *it & 0xff; + int charCount = 0; + if (c == 0x0) { + // \0 + ++it; // we increment iter manually + } else if (c <= 0x7f) { + charCount = 1; + } else if (c <= 0x7ff) { + charCount = 2; + } else if (c <= 0xffff) { + charCount = 3; + } else { + charCount = 4; + } + result += charCount; + it += charCount; + } + return result; +} + +ByteArray MineCommon::generateRandomBytes(const std::size_t len) +{ + ByteArray result(len, 'x'); + std::random_device rd; + std::mt19937 rng(rd()); + std::uniform_int_distribution uni(0, kRandomBytesCount - 1); + std::generate(result.begin(), result.end(), [&] { + return kRandomBytesList[uni(rng)]; + }); + return result; +} + + const std::string Base16::kValidChars = "0123456789ABCDEF"; const std::unordered_map Base16::kDecodeMap = { @@ -95,30 +151,6 @@ const std::unordered_map Base64::kDecodeMap = { {0x3D, 0x40} }; -std::size_t Base64::countChars(const std::string& str) noexcept -{ - std::size_t result = 0UL; - for (auto it = str.begin(); it <= str.end();) { - int c = *it & 0xff; - int charCount = 0; - if (c == 0x0) { - // \0 - ++it; // we increment iter manually - } else if (c <= 0x7f) { - charCount = 1; - } else if (c <= 0x7ff) { - charCount = 2; - } else if (c <= 0xffff) { - charCount = 3; - } else { - charCount = 4; - } - result += charCount; - it += charCount; - } - return result; -} - #define MINE_PROFILING 0 #if MINE_PROFILING @@ -177,25 +209,6 @@ const uint8_t AES::kRoundConstant[10] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; -const byte AES::kRandomBytesList[256] = { - 0x6f, 0x48, 0x15, 0x46, 0x77, 0x58, 0x05, 0x0b, 0x02, 0x6f, 0x20, 0x66, 0x18, 0x5a, 0x17, 0x27, - 0x45, 0x6c, 0x0f, 0x33, 0x08, 0x58, 0x2a, 0x54, 0x75, 0x53, 0x1e, 0x2a, 0x09, 0x13, 0x0f, 0x20, - 0x49, 0x49, 0x4b, 0x18, 0x3c, 0x1f, 0x06, 0x0e, 0x58, 0x52, 0x7c, 0x25, 0x58, 0x7d, 0x33, 0x27, - 0x14, 0x47, 0x66, 0x3f, 0x68, 0x66, 0x49, 0x27, 0x77, 0x10, 0x33, 0x26, 0x6c, 0x34, 0x10, 0x4e, - 0x10, 0x48, 0x07, 0x7c, 0x11, 0x06, 0x60, 0x61, 0x28, 0x29, 0x47, 0x5b, 0x3b, 0x16, 0x75, 0x74, - 0x14, 0x4a, 0x4a, 0x78, 0x21, 0x35, 0x77, 0x50, 0x17, 0x74, 0x3c, 0x26, 0x05, 0x31, 0x65, 0x36, - 0x48, 0x3c, 0x29, 0x4c, 0x1e, 0x78, 0x5e, 0x51, 0x16, 0x7f, 0x0b, 0x6d, 0x14, 0x41, 0x6e, 0x15, - 0x35, 0x7a, 0x4c, 0x59, 0x52, 0x5e, 0x0c, 0x22, 0x29, 0x7d, 0x6f, 0x0b, 0x73, 0x55, 0x0c, 0x44, - 0x3d, 0x70, 0x15, 0x33, 0x71, 0x23, 0x34, 0x77, 0x39, 0x68, 0x04, 0x6d, 0x2b, 0x4b, 0x52, 0x4d, - 0x30, 0x03, 0x38, 0x09, 0x5b, 0x58, 0x09, 0x5f, 0x4b, 0x54, 0x5d, 0x53, 0x35, 0x6b, 0x48, 0x43, - 0x3e, 0x58, 0x7d, 0x48, 0x7e, 0x6d, 0x71, 0x28, 0x14, 0x0e, 0x41, 0x58, 0x20, 0x7b, 0x48, 0x14, - 0x1f, 0x68, 0x07, 0x6d, 0x62, 0x4a, 0x72, 0x34, 0x7d, 0x66, 0x3e, 0x42, 0x79, 0x47, 0x36, 0x11, - 0x37, 0x08, 0x1f, 0x0a, 0x08, 0x2f, 0x66, 0x11, 0x2b, 0x0e, 0x03, 0x33, 0x14, 0x66, 0x25, 0x3e, - 0x08, 0x6f, 0x6e, 0x69, 0x71, 0x1e, 0x1c, 0x02, 0x09, 0x0a, 0x45, 0x24, 0x73, 0x58, 0x4b, 0x43, - 0x5a, 0x53, 0x4f, 0x0e, 0x39, 0x71, 0x13, 0x0c, 0x02, 0x46, 0x66, 0x2a, 0x56, 0x4c, 0x2b, 0x37, - 0x34, 0x45, 0x6e, 0x01, 0x4d, 0x12, 0x35, 0x4a, 0x29, 0x66, 0x30, 0x5b, 0x31, 0x4f, 0x6e, 0x3d -}; - const std::unordered_map> AES::kKeyParams = { { 16, {{ 4, 10 }} }, { 24, {{ 6, 12 }} }, @@ -639,26 +652,6 @@ ByteArray AES::stateToByteArray(const State *state) return result; } -ByteArray AES::generateRandomBytes(const std::size_t len) -{ - ByteArray result(len, 'x'); - std::random_device rd; - std::mt19937 rng(rd()); - std::uniform_int_distribution uni(0, kRandomBytesCount - 1); - std::generate(result.begin(), result.end(), [&] { - return kRandomBytesList[uni(rng)]; - }); - return result; -} - -ByteArray* AES::xorWith(ByteArray* input, const ByteArray* arr) -{ - for (std::size_t i = 0; i < kBlockSize; ++i) { - (*input)[i] ^= (*arr)[i]; - } - return input; -} - ByteArray* AES::xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end) { int i = 0; @@ -879,7 +872,7 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bo throw std::invalid_argument("Invalid IV, it should be same as block size"); } else if (iv.empty()) { // generate IV - iv = generateRandomBytes(16); + iv = MineCommon::generateRandomBytes(16); } if (*key != m_key) { @@ -1023,7 +1016,7 @@ std::string AES::generateRandomKey(const std::size_t len) if (len != 128 && len != 192 && len != 256) { throw std::invalid_argument("Please choose valid key length of 128, 192 or 256 bits"); } - ByteArray bytes = generateRandomBytes(len / 8); + ByteArray bytes = MineCommon::generateRandomBytes(len / 8); return Base16::encode(bytes.begin(), bytes.end()); } diff --git a/package/mine.h b/package/mine.h index 220fff0..0819716 100644 --- a/package/mine.h +++ b/package/mine.h @@ -17,12 +17,12 @@ #ifndef MINE_CRYPTO_H #define MINE_CRYPTO_H +#include +#include #include #include -#include #include #include -#include #include #include #include @@ -32,8 +32,58 @@ namespace mine { using byte = unsigned char; + +/// +/// \brief Handy safe byte array +/// using ByteArray = std::vector; +/// +/// \brief Contains common functions used across the lib +/// +class MineCommon { +public: + + /// + /// \brief Total items in random bytes list + /// + static const int kRandomBytesCount = 256; + + /// + /// \brief List to choose random byte from + /// + static const byte kRandomBytesList[]; + +#ifdef MINE_WSTRING_CONVERSION + /// + /// \brief Converts it to std::string and calls countChars on it + /// + /// \note You need to include and headers before mine.h + /// + static std::size_t countChars(const std::wstring& raw) noexcept + { + std::string converted = std::wstring_convert + , wchar_t>{}.to_bytes(raw); + return countChars(converted); + } +#endif + + /// + /// \brief Replacement for better d.size() that consider unicode bytes too + /// \see https://en.wikipedia.org/wiki/UTF-8#Description + /// + static std::size_t countChars(const std::string& d) noexcept; + + /// + /// \brief Generates random bytes of length + /// + static ByteArray generateRandomBytes(const std::size_t len); +private: + MineCommon() = delete; + MineCommon(const MineCommon&) = delete; + MineCommon& operator=(const MineCommon&) = delete; +}; + /// /// \brief Provides base16 encoding / decoding /// @@ -181,8 +231,6 @@ class Base16 { static void decode(char a, char b, std::ostringstream& ss); }; -using byte = unsigned char; - /// /// \brief Provides base64 encoding / decoding implementation /// @@ -387,7 +435,7 @@ class Base64 { } -#ifdef MINE_BASE64_WSTRING_CONVERSION +#ifdef MINE_WSTRING_CONVERSION /// /// \brief Converts wstring to corresponding string and returns /// encoding @@ -419,26 +467,8 @@ class Base64 { >{}.from_bytes(result); return converted; } - - /// - /// \brief Converts it to std::string and calls countChars on it - /// - /// \note You need to include and headers before mine.h - /// - static std::size_t countChars(const std::wstring& raw) noexcept - { - std::string converted = std::wstring_convert - , wchar_t>{}.to_bytes(raw); - return countChars(converted); - } #endif - /// - /// \brief Replacement for better d.size() that consider unicode bytes too - /// \see https://en.wikipedia.org/wiki/UTF-8#Description - /// - static std::size_t countChars(const std::string& d) noexcept; - /// /// \brief expectedBase64Length Returns expected base64 length /// \param n Length of input (plain data) @@ -455,7 +485,7 @@ class Base64 { template inline static std::size_t expectedLength(const T& str) noexcept { - return expectedLength(countChars(str)); + return expectedLength(MineCommon::countChars(str)); } private: @@ -464,13 +494,6 @@ class Base64 { Base64& operator=(const Base64&) = delete; }; -using byte = unsigned char; - -/// -/// \brief Handy safe byte array -/// -using ByteArray = std::vector; - /// /// \brief Provides AES crypto functionalities /// @@ -640,16 +663,6 @@ class AES { /// static const std::unordered_map> kKeyParams; - /// - /// \brief Total items in random bytes list - /// - static const int kRandomBytesCount = 256; - - /// - /// \brief List to choose random byte from - /// - static const byte kRandomBytesList[]; - /// /// \brief As defined in FIPS. 197 Sec. 5.1.1 /// @@ -780,11 +793,6 @@ class AES { /// static void initState(State* state, const ByteArray::const_iterator& begin); - /// - /// \brief Generates random bytes of length - /// - static ByteArray generateRandomBytes(const std::size_t len); - /// /// \brief Creates byte array from input based on input mode /// @@ -795,11 +803,6 @@ class AES { /// static std::string resolveOutputMode(const ByteArray& input, Encoding outputMode); - /// - /// \brief Exclusive XOR with arr - /// - static ByteArray* xorWith(ByteArray* input, const ByteArray*); - /// /// \brief Exclusive XOR with iter of range size as input /// diff --git a/src/aes.cc b/src/aes.cc index 181ee6b..42e9be1 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -25,7 +25,7 @@ #include #include #include -#include +#include "src/mine-common.h" #include "src/base16.h" #include "src/base64.h" #include "src/aes.h" @@ -89,25 +89,6 @@ const uint8_t AES::kRoundConstant[10] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; -const byte AES::kRandomBytesList[256] = { - 0x6f, 0x48, 0x15, 0x46, 0x77, 0x58, 0x05, 0x0b, 0x02, 0x6f, 0x20, 0x66, 0x18, 0x5a, 0x17, 0x27, - 0x45, 0x6c, 0x0f, 0x33, 0x08, 0x58, 0x2a, 0x54, 0x75, 0x53, 0x1e, 0x2a, 0x09, 0x13, 0x0f, 0x20, - 0x49, 0x49, 0x4b, 0x18, 0x3c, 0x1f, 0x06, 0x0e, 0x58, 0x52, 0x7c, 0x25, 0x58, 0x7d, 0x33, 0x27, - 0x14, 0x47, 0x66, 0x3f, 0x68, 0x66, 0x49, 0x27, 0x77, 0x10, 0x33, 0x26, 0x6c, 0x34, 0x10, 0x4e, - 0x10, 0x48, 0x07, 0x7c, 0x11, 0x06, 0x60, 0x61, 0x28, 0x29, 0x47, 0x5b, 0x3b, 0x16, 0x75, 0x74, - 0x14, 0x4a, 0x4a, 0x78, 0x21, 0x35, 0x77, 0x50, 0x17, 0x74, 0x3c, 0x26, 0x05, 0x31, 0x65, 0x36, - 0x48, 0x3c, 0x29, 0x4c, 0x1e, 0x78, 0x5e, 0x51, 0x16, 0x7f, 0x0b, 0x6d, 0x14, 0x41, 0x6e, 0x15, - 0x35, 0x7a, 0x4c, 0x59, 0x52, 0x5e, 0x0c, 0x22, 0x29, 0x7d, 0x6f, 0x0b, 0x73, 0x55, 0x0c, 0x44, - 0x3d, 0x70, 0x15, 0x33, 0x71, 0x23, 0x34, 0x77, 0x39, 0x68, 0x04, 0x6d, 0x2b, 0x4b, 0x52, 0x4d, - 0x30, 0x03, 0x38, 0x09, 0x5b, 0x58, 0x09, 0x5f, 0x4b, 0x54, 0x5d, 0x53, 0x35, 0x6b, 0x48, 0x43, - 0x3e, 0x58, 0x7d, 0x48, 0x7e, 0x6d, 0x71, 0x28, 0x14, 0x0e, 0x41, 0x58, 0x20, 0x7b, 0x48, 0x14, - 0x1f, 0x68, 0x07, 0x6d, 0x62, 0x4a, 0x72, 0x34, 0x7d, 0x66, 0x3e, 0x42, 0x79, 0x47, 0x36, 0x11, - 0x37, 0x08, 0x1f, 0x0a, 0x08, 0x2f, 0x66, 0x11, 0x2b, 0x0e, 0x03, 0x33, 0x14, 0x66, 0x25, 0x3e, - 0x08, 0x6f, 0x6e, 0x69, 0x71, 0x1e, 0x1c, 0x02, 0x09, 0x0a, 0x45, 0x24, 0x73, 0x58, 0x4b, 0x43, - 0x5a, 0x53, 0x4f, 0x0e, 0x39, 0x71, 0x13, 0x0c, 0x02, 0x46, 0x66, 0x2a, 0x56, 0x4c, 0x2b, 0x37, - 0x34, 0x45, 0x6e, 0x01, 0x4d, 0x12, 0x35, 0x4a, 0x29, 0x66, 0x30, 0x5b, 0x31, 0x4f, 0x6e, 0x3d -}; - const std::unordered_map> AES::kKeyParams = { { 16, {{ 4, 10 }} }, { 24, {{ 6, 12 }} }, @@ -551,26 +532,6 @@ ByteArray AES::stateToByteArray(const State *state) return result; } -ByteArray AES::generateRandomBytes(const std::size_t len) -{ - ByteArray result(len, 'x'); - std::random_device rd; - std::mt19937 rng(rd()); - std::uniform_int_distribution uni(0, kRandomBytesCount - 1); - std::generate(result.begin(), result.end(), [&] { - return kRandomBytesList[uni(rng)]; - }); - return result; -} - -ByteArray* AES::xorWith(ByteArray* input, const ByteArray* arr) -{ - for (std::size_t i = 0; i < kBlockSize; ++i) { - (*input)[i] ^= (*arr)[i]; - } - return input; -} - ByteArray* AES::xorWithRange(ByteArray* input, const ByteArray::const_iterator& begin, const ByteArray::const_iterator& end) { int i = 0; @@ -791,7 +752,7 @@ ByteArray AES::encrypt(const ByteArray& input, const Key* key, ByteArray& iv, bo throw std::invalid_argument("Invalid IV, it should be same as block size"); } else if (iv.empty()) { // generate IV - iv = generateRandomBytes(16); + iv = MineCommon::generateRandomBytes(16); } if (*key != m_key) { @@ -935,7 +896,7 @@ std::string AES::generateRandomKey(const std::size_t len) if (len != 128 && len != 192 && len != 256) { throw std::invalid_argument("Please choose valid key length of 128, 192 or 256 bits"); } - ByteArray bytes = generateRandomBytes(len / 8); + ByteArray bytes = MineCommon::generateRandomBytes(len / 8); return Base16::encode(bytes.begin(), bytes.end()); } diff --git a/src/aes.h b/src/aes.h index 3ba1eee..2e98870 100644 --- a/src/aes.h +++ b/src/aes.h @@ -26,16 +26,10 @@ #include #include #include +#include "src/mine-common.h" namespace mine { -using byte = unsigned char; - -/// -/// \brief Handy safe byte array -/// -using ByteArray = std::vector; - /// /// \brief Provides AES crypto functionalities /// @@ -205,16 +199,6 @@ class AES { /// static const std::unordered_map> kKeyParams; - /// - /// \brief Total items in random bytes list - /// - static const int kRandomBytesCount = 256; - - /// - /// \brief List to choose random byte from - /// - static const byte kRandomBytesList[]; - /// /// \brief As defined in FIPS. 197 Sec. 5.1.1 /// @@ -345,11 +329,6 @@ class AES { /// static void initState(State* state, const ByteArray::const_iterator& begin); - /// - /// \brief Generates random bytes of length - /// - static ByteArray generateRandomBytes(const std::size_t len); - /// /// \brief Creates byte array from input based on input mode /// @@ -360,11 +339,6 @@ class AES { /// static std::string resolveOutputMode(const ByteArray& input, Encoding outputMode); - /// - /// \brief Exclusive XOR with arr - /// - static ByteArray* xorWith(ByteArray* input, const ByteArray*); - /// /// \brief Exclusive XOR with iter of range size as input /// diff --git a/src/base16.h b/src/base16.h index 25383d9..e3b36b9 100644 --- a/src/base16.h +++ b/src/base16.h @@ -27,12 +27,10 @@ #include #include #include +#include "src/mine-common.h" namespace mine { -using byte = unsigned char; -using ByteArray = std::vector; - /// /// \brief Provides base16 encoding / decoding /// diff --git a/src/base64.cc b/src/base64.cc index 4ab5f23..d8d1e5f 100644 --- a/src/base64.cc +++ b/src/base64.cc @@ -44,27 +44,3 @@ const std::unordered_map Base64::kDecodeMap = { {0x38, 0x3C}, {0x39, 0x3D}, {0x2B, 0x3E}, {0x2F, 0x3F}, {0x3D, 0x40} }; - -std::size_t Base64::countChars(const std::string& str) noexcept -{ - std::size_t result = 0UL; - for (auto it = str.begin(); it <= str.end();) { - int c = *it & 0xff; - int charCount = 0; - if (c == 0x0) { - // \0 - ++it; // we increment iter manually - } else if (c <= 0x7f) { - charCount = 1; - } else if (c <= 0x7ff) { - charCount = 2; - } else if (c <= 0xffff) { - charCount = 3; - } else { - charCount = 4; - } - result += charCount; - it += charCount; - } - return result; -} diff --git a/src/base64.h b/src/base64.h index 1bf911f..2366967 100644 --- a/src/base64.h +++ b/src/base64.h @@ -28,16 +28,15 @@ // codecvt is not part of standard // hence we leave it to user to enable/disable // it depending on their -#ifdef MINE_BASE64_WSTRING_CONVERSION +#ifdef MINE_WSTRING_CONVERSION # include # include #endif #include +#include "src/mine-common.h" namespace mine { -using byte = unsigned char; - /// /// \brief Provides base64 encoding / decoding implementation /// @@ -242,7 +241,7 @@ class Base64 { } -#ifdef MINE_BASE64_WSTRING_CONVERSION +#ifdef MINE_WSTRING_CONVERSION /// /// \brief Converts wstring to corresponding string and returns /// encoding @@ -274,26 +273,8 @@ class Base64 { >{}.from_bytes(result); return converted; } - - /// - /// \brief Converts it to std::string and calls countChars on it - /// - /// \note You need to include and headers before mine.h - /// - static std::size_t countChars(const std::wstring& raw) noexcept - { - std::string converted = std::wstring_convert - , wchar_t>{}.to_bytes(raw); - return countChars(converted); - } #endif - /// - /// \brief Replacement for better d.size() that consider unicode bytes too - /// \see https://en.wikipedia.org/wiki/UTF-8#Description - /// - static std::size_t countChars(const std::string& d) noexcept; - /// /// \brief expectedBase64Length Returns expected base64 length /// \param n Length of input (plain data) @@ -310,7 +291,7 @@ class Base64 { template inline static std::size_t expectedLength(const T& str) noexcept { - return expectedLength(countChars(str)); + return expectedLength(MineCommon::countChars(str)); } private: diff --git a/src/mine-common.cc b/src/mine-common.cc new file mode 100644 index 0000000..e2ca127 --- /dev/null +++ b/src/mine-common.cc @@ -0,0 +1,75 @@ +// +// mine-common.cc +// Part of Mine crypto library +// +// You should not use this file, use mine.cc +// instead which is automatically generated and includes this file +// This is seperated to aid the development +// +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE +// +// https://github.com/muflihun/mine +// + +#include +#include "src/mine-common.h" + +using namespace mine; + +const byte MineCommon::kRandomBytesList[256] = { + 0x6f, 0x48, 0x15, 0x46, 0x77, 0x58, 0x05, 0x0b, 0x02, 0x6f, 0x20, 0x66, 0x18, 0x5a, 0x17, 0x27, + 0x45, 0x6c, 0x0f, 0x33, 0x08, 0x58, 0x2a, 0x54, 0x75, 0x53, 0x1e, 0x2a, 0x09, 0x13, 0x0f, 0x20, + 0x49, 0x49, 0x4b, 0x18, 0x3c, 0x1f, 0x06, 0x0e, 0x58, 0x52, 0x7c, 0x25, 0x58, 0x7d, 0x33, 0x27, + 0x14, 0x47, 0x66, 0x3f, 0x68, 0x66, 0x49, 0x27, 0x77, 0x10, 0x33, 0x26, 0x6c, 0x34, 0x10, 0x4e, + 0x10, 0x48, 0x07, 0x7c, 0x11, 0x06, 0x60, 0x61, 0x28, 0x29, 0x47, 0x5b, 0x3b, 0x16, 0x75, 0x74, + 0x14, 0x4a, 0x4a, 0x78, 0x21, 0x35, 0x77, 0x50, 0x17, 0x74, 0x3c, 0x26, 0x05, 0x31, 0x65, 0x36, + 0x48, 0x3c, 0x29, 0x4c, 0x1e, 0x78, 0x5e, 0x51, 0x16, 0x7f, 0x0b, 0x6d, 0x14, 0x41, 0x6e, 0x15, + 0x35, 0x7a, 0x4c, 0x59, 0x52, 0x5e, 0x0c, 0x22, 0x29, 0x7d, 0x6f, 0x0b, 0x73, 0x55, 0x0c, 0x44, + 0x3d, 0x70, 0x15, 0x33, 0x71, 0x23, 0x34, 0x77, 0x39, 0x68, 0x04, 0x6d, 0x2b, 0x4b, 0x52, 0x4d, + 0x30, 0x03, 0x38, 0x09, 0x5b, 0x58, 0x09, 0x5f, 0x4b, 0x54, 0x5d, 0x53, 0x35, 0x6b, 0x48, 0x43, + 0x3e, 0x58, 0x7d, 0x48, 0x7e, 0x6d, 0x71, 0x28, 0x14, 0x0e, 0x41, 0x58, 0x20, 0x7b, 0x48, 0x14, + 0x1f, 0x68, 0x07, 0x6d, 0x62, 0x4a, 0x72, 0x34, 0x7d, 0x66, 0x3e, 0x42, 0x79, 0x47, 0x36, 0x11, + 0x37, 0x08, 0x1f, 0x0a, 0x08, 0x2f, 0x66, 0x11, 0x2b, 0x0e, 0x03, 0x33, 0x14, 0x66, 0x25, 0x3e, + 0x08, 0x6f, 0x6e, 0x69, 0x71, 0x1e, 0x1c, 0x02, 0x09, 0x0a, 0x45, 0x24, 0x73, 0x58, 0x4b, 0x43, + 0x5a, 0x53, 0x4f, 0x0e, 0x39, 0x71, 0x13, 0x0c, 0x02, 0x46, 0x66, 0x2a, 0x56, 0x4c, 0x2b, 0x37, + 0x34, 0x45, 0x6e, 0x01, 0x4d, 0x12, 0x35, 0x4a, 0x29, 0x66, 0x30, 0x5b, 0x31, 0x4f, 0x6e, 0x3d +}; + +std::size_t MineCommon::countChars(const std::string& str) noexcept +{ + std::size_t result = 0UL; + for (auto it = str.begin(); it <= str.end();) { + int c = *it & 0xff; + int charCount = 0; + if (c == 0x0) { + // \0 + ++it; // we increment iter manually + } else if (c <= 0x7f) { + charCount = 1; + } else if (c <= 0x7ff) { + charCount = 2; + } else if (c <= 0xffff) { + charCount = 3; + } else { + charCount = 4; + } + result += charCount; + it += charCount; + } + return result; +} + +ByteArray MineCommon::generateRandomBytes(const std::size_t len) +{ + ByteArray result(len, 'x'); + std::random_device rd; + std::mt19937 rng(rd()); + std::uniform_int_distribution uni(0, kRandomBytesCount - 1); + std::generate(result.begin(), result.end(), [&] { + return kRandomBytesList[uni(rng)]; + }); + return result; +} diff --git a/src/mine-common.h b/src/mine-common.h new file mode 100644 index 0000000..08575b2 --- /dev/null +++ b/src/mine-common.h @@ -0,0 +1,88 @@ +// +// mine-common.h +// Part of Mine crypto library +// +// You should not use this file, use mine.h +// instead which is automatically generated and includes this file +// This is seperated to aid the development +// +// Copyright (c) 2017 Muflihun Labs +// +// This library is released under the Apache 2.0 license +// https://github.com/muflihun/mine/blob/master/LICENSE +// +// https://github.com/muflihun/mine +// + +#ifdef MINE_CRYPTO_H +# error "Please use mine.h file. this file is only to aid the development" +#endif + +#include +#include + +#ifdef MINE_WSTRING_CONVERSION +# include +# include +#endif + +#ifndef Common_H +#define Common_H + +namespace mine { + +using byte = unsigned char; + +/// +/// \brief Handy safe byte array +/// +using ByteArray = std::vector; + +/// +/// \brief Contains common functions used across the lib +/// +class MineCommon { +public: + + /// + /// \brief Total items in random bytes list + /// + static const int kRandomBytesCount = 256; + + /// + /// \brief List to choose random byte from + /// + static const byte kRandomBytesList[]; + +#ifdef MINE_WSTRING_CONVERSION + /// + /// \brief Converts it to std::string and calls countChars on it + /// + /// \note You need to include and headers before mine.h + /// + static std::size_t countChars(const std::wstring& raw) noexcept + { + std::string converted = std::wstring_convert + , wchar_t>{}.to_bytes(raw); + return countChars(converted); + } +#endif + + /// + /// \brief Replacement for better d.size() that consider unicode bytes too + /// \see https://en.wikipedia.org/wiki/UTF-8#Description + /// + static std::size_t countChars(const std::string& d) noexcept; + + /// + /// \brief Generates random bytes of length + /// + static ByteArray generateRandomBytes(const std::size_t len); +private: + MineCommon() = delete; + MineCommon(const MineCommon&) = delete; + MineCommon& operator=(const MineCommon&) = delete; +}; +} // end namespace mine + +#endif // Common_H diff --git a/test/main.cc b/test/main.cc index f496792..85861da 100644 --- a/test/main.cc +++ b/test/main.cc @@ -1,3 +1,4 @@ + #include "test.h" #include "zlib-test.h" #include "rsa-test.h" From 5a3b5e11007af7f70158c562a7ac847f676c03fc Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 4 Sep 2017 11:37:01 +1000 Subject: [PATCH 138/148] generated --- package/mine.cc | 2 +- src/mine-common.cc | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/package/mine.cc b/package/mine.cc index 1625d9d..5d82498 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -13,11 +13,11 @@ // https://muflihun.github.io/mine // https://muflihun.com // +#include #include #include #include #include -#include #include #include #include diff --git a/src/mine-common.cc b/src/mine-common.cc index e2ca127..857cf41 100644 --- a/src/mine-common.cc +++ b/src/mine-common.cc @@ -14,6 +14,7 @@ // https://github.com/muflihun/mine // +#include #include #include "src/mine-common.h" From 6a923ccce09ef477074b677ad514f3b504531939 Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 4 Sep 2017 13:09:55 +1000 Subject: [PATCH 139/148] version info --- package/mine.cc | 7 ++++++- package/mine.h | 7 ++++++- src/mine-common.cc | 7 ++++++- src/mine-common.h | 7 ++++++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 5d82498..7916f6c 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -74,7 +74,7 @@ std::size_t MineCommon::countChars(const std::string& str) noexcept return result; } -ByteArray MineCommon::generateRandomBytes(const std::size_t len) +ByteArray MineCommon::generateRandomBytes(const std::size_t len) noexcept { ByteArray result(len, 'x'); std::random_device rd; @@ -86,6 +86,11 @@ ByteArray MineCommon::generateRandomBytes(const std::size_t len) return result; } +std::string MineCommon::version() noexcept +{ + return MINE_VERSION; +} + const std::string Base16::kValidChars = "0123456789ABCDEF"; diff --git a/package/mine.h b/package/mine.h index 0819716..13d4844 100644 --- a/package/mine.h +++ b/package/mine.h @@ -77,7 +77,12 @@ class MineCommon { /// /// \brief Generates random bytes of length /// - static ByteArray generateRandomBytes(const std::size_t len); + static ByteArray generateRandomBytes(const std::size_t len) noexcept; + + /// + /// \brief Version of mine + /// + static std::string version() noexcept; private: MineCommon() = delete; MineCommon(const MineCommon&) = delete; diff --git a/src/mine-common.cc b/src/mine-common.cc index 857cf41..d577714 100644 --- a/src/mine-common.cc +++ b/src/mine-common.cc @@ -63,7 +63,7 @@ std::size_t MineCommon::countChars(const std::string& str) noexcept return result; } -ByteArray MineCommon::generateRandomBytes(const std::size_t len) +ByteArray MineCommon::generateRandomBytes(const std::size_t len) noexcept { ByteArray result(len, 'x'); std::random_device rd; @@ -74,3 +74,8 @@ ByteArray MineCommon::generateRandomBytes(const std::size_t len) }); return result; } + +std::string MineCommon::version() noexcept +{ + return MINE_VERSION; +} diff --git a/src/mine-common.h b/src/mine-common.h index 08575b2..8a22ba0 100644 --- a/src/mine-common.h +++ b/src/mine-common.h @@ -77,7 +77,12 @@ class MineCommon { /// /// \brief Generates random bytes of length /// - static ByteArray generateRandomBytes(const std::size_t len); + static ByteArray generateRandomBytes(const std::size_t len) noexcept; + + /// + /// \brief Version of mine + /// + static std::string version() noexcept; private: MineCommon() = delete; MineCommon(const MineCommon&) = delete; From 81e32143b75405d2a0e44df9bd58737ef29e82d5 Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 4 Sep 2017 14:42:22 +1000 Subject: [PATCH 140/148] version --- build.php | 3 +++ package/mine.cc | 3 +++ 2 files changed, 6 insertions(+) diff --git a/build.php b/build.php index 4a95cdc..da64924 100644 --- a/build.php +++ b/build.php @@ -56,6 +56,9 @@ #include "mine.h" using namespace mine; +#ifndef MINE_VERSION +#define MINE_VERSION "{{version}}" +#endif {{code}} EOT; diff --git a/package/mine.cc b/package/mine.cc index 7916f6c..94a450b 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -29,6 +29,9 @@ #include "mine.h" using namespace mine; +#ifndef MINE_VERSION +#define MINE_VERSION "Unreleased" +#endif const byte MineCommon::kRandomBytesList[256] = { From c92d5ac1979c68714c61035f794a567e792e3637 Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 4 Sep 2017 16:25:35 +1000 Subject: [PATCH 141/148] quick pem reader sample --- test/private2.pem | 30 ++++++++++++++++++++++++++++++ test/rsa-test.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 test/private2.pem diff --git a/test/private2.pem b/test/private2.pem new file mode 100644 index 0000000..11c6923 --- /dev/null +++ b/test/private2.pem @@ -0,0 +1,30 @@ +-----BEGIN RSA PRIVATE KEY----- +Proc-Type: 4,ENCRYPTED +DEK-Info: AES-256-CBC,2B4C41A040B250091A5158F3BE162C42 + +MNn1oPuFGZ/r+e7pujL4bDH+JczkpAuCA2+yLr2/PNJg0QIm+nRXJmwGAd8xuV/6 +ogxxEeaCSIBUImo6RmqYkbJeM7NE0pv9mDfPGAfJMSc9xtk1HVWBdUtRHdHzKMy2 +zkJ89uDIZXA0KPnSlkrjHinR4/PJJIsakel9lVPaA56yH7pUlOW0NYnAcCMbXu07 +O/VagLQMEPM2ihS8QVJQZmXI129t63bO9EjSxbzik/sSFiYOt3L+Kjdron3LV1Z7 +TgoTevUf2btDL2HxSpIXSq48Dy2mC+rYG6x6yi0X1NdYAjjl5TbmIBkybRLnf+qL +ksEKw/DdMr77XOjkB987u567LczZBcI4eAo47SDzjXt7xO5T+TWmT9VfDyFks0t7 +n/bCTUmL9PNI/fRg1nny+EQhvmmgPo/dL1kziXF0s3u3XCM2rxbZ/DQS5m2eF2nU +xj6CeH9o8mQNot74nZhnLqCV4Y1abwuKzZQBsIM/UPd0exzFK+y7OIyhlq4efyS3 +ImEjolnwDqMn7hpV1eIcL09hs3P8oqKCO19laUsc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END RSA PRIVATE KEY----- diff --git a/test/rsa-test.h b/test/rsa-test.h index 03010a4..29ee359 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -11,6 +11,7 @@ #include #include +#include //#include namespace mine { @@ -57,6 +58,22 @@ class KeyPair : public GenericKeyPair { using GenericKeyPair::GenericKeyPair; }; +KeyPair readPem(const std::string& contents, const std::string& secret) +{ + CryptoPP::RSA::PrivateKey keyOut; + { + using namespace CryptoPP; + StringSource source(contents, true); + if (secret.empty()) { + PEM_Load(source, keyOut); + } else { + PEM_Load(source, keyOut, secret.data(), secret.size()); + } + } + return KeyPair(keyOut.GetPrime1(), keyOut.GetPrime2(), + static_cast(keyOut.GetPublicExponent().ConvertToLong())); +} + static RSA rsaManager; static Helper rsaHelper; @@ -255,6 +272,36 @@ TEST(RSATest, ManualTest) ASSERT_STREQ(sopenssl.c_str(), "Test\n"); } +TEST(RSATest, KeyRead) +{ + // 2048-bit long encrypted key + // encryption secret asdf + std::string pem("LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpQcm9jLVR5cGU6IDQsRU5DUllQVEVECkRFSy1JbmZvOiBBRVMtMjU2LUNCQywyQjRDNDFBMDQwQjI1MDA" + "5MUE1MTU4RjNCRTE2MkM0MgoKTU5uMW9QdUZHWi9yK2U3cHVqTDRiREgrSmN6a3BBdUNBMit5THIyL1BOSmcwUUltK25SWEptd0dBZDh4dVYvNgpvZ3h4RWVhQ1" + "NJQlVJbW82Um1xWWtiSmVNN05FMHB2OW1EZlBHQWZKTVNjOXh0azFIVldCZFV0UkhkSHpLTXkyCnprSjg5dURJWlhBMEtQblNsa3JqSGluUjQvUEpKSXNha2VsO" + "WxWUGFBNTZ5SDdwVWxPVzBOWW5BY0NNYlh1MDcKTy9WYWdMUU1FUE0yaWhTOFFWSlFabVhJMTI5dDYzYk85RWpTeGJ6aWsvc1NGaVlPdDNMK0tqZHJvbjNMVjFa" + "NwpUZ29UZXZVZjJidERMMkh4U3BJWFNxND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2dIaDR0dFptbFlpRGhJTUdTYnpRcGwrcG91UEJFQWplNTV" + "4WVhNbUVRRmNLRjdECkJMeDF1ZzYvVVdJbWdObHdBbERnR1NmZlBKMGkyQkM5SmRUaThoY284QnlRWHN2ZURIemJUOGFMTlE0RzhKTzYKQUlvUCtvNERLRHBHZSs" + "yMGFGR1d3L0hEK1pnSUtLaEQvSlBtZUNlek50bUZXazNQdDBJN0NnM0EzU3Q1M1NZQQplelEwV28wRmFIS2hPbWJPcmZ5OTkzVTZQN3dCOXpmR2hkRzZyTWRocXo" + "xYVhibyt3ZERJZituNkxRdDBCQUVEClVqbUdsVVFPYnZjbDlkWlVjclV6cjJCRWZ3MkRTbWg0NUozSEsxOFc2RkZ6d0lTbDBSR1JZVWZQNmg2Tyt0bmoKbEs1SXU" + "xcUE5eG5FS2lmN3E4K2k1cGo2MlBROUhKNTk0MytyWXFGWSt3TUdyaEl3TGxrNENnMjhhZGFsRDEvSQpIU3g2SDNMRitwSUpiQzc4ZDNDTTBDcTNzQTZkdFBqczV" + "YWHcxTUpycFR4RElIWjV0eHVJdUlnZDdSN0tsenB2CnNFWDc0QUYyTjFlNVZsNkV3WHRqcHFoeGlRVnVEWmVqQzcvMEU5dGR5YjRWdk9wOVNoRm85cXNHekxKb1J" + "IQVgKZmlsWmxZcGdtbXlpNlNnY1ExSERuRWZYZkpMbWROY0tUVEVqNWo3MkhXN0RpQ1Eza3BpN1lhZzRsQlE1MGZ4QwotLS0tLUVORCBSU0EgUFJJVkFURSBLRVk" + "tLS0tLQo="); + KeyPair k = readPem(Base64::decode(pem), "asdf"); + + std::cout << "Run: echo " << rsaManager.encrypt(k.publicKey(), std::string("Testing this long secret")) + << " | ripe -d --rsa --in-key private2.pem --secret asdf --hex" << std::endl; +} + TEST(RSATest, Signature) { for (const auto& item : RSASignatureData) { From 74dfa4827b2ed1203d2a2c5359aa0397c29da4a3 Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 4 Sep 2017 16:37:13 +1000 Subject: [PATCH 142/148] fix some issues --- package/mine.h | 5 +++++ src/rsa.h | 5 +++++ test/rsa-test.h | 35 ++++++++++++++++++----------------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/package/mine.h b/package/mine.h index 13d4844..f929080 100644 --- a/package/mine.h +++ b/package/mine.h @@ -1335,6 +1335,11 @@ class GenericKeyPair { } GenericKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) + { + init(p, q, exp); + } + + void init(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) { m_publicKey = GenericPublicKey(p * q, exp); m_privateKey = GenericPrivateKey(p, q, exp); diff --git a/src/rsa.h b/src/rsa.h index 2c7d8fb..8101754 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -499,6 +499,11 @@ class GenericKeyPair { } GenericKeyPair(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) + { + init(p, q, exp); + } + + void init(const BigInteger& p, const BigInteger& q, unsigned int exp = kDefaultPublicExponent) { m_publicKey = GenericPublicKey(p * q, exp); m_privateKey = GenericPrivateKey(p, q, exp); diff --git a/test/rsa-test.h b/test/rsa-test.h index 29ee359..fa93820 100644 --- a/test/rsa-test.h +++ b/test/rsa-test.h @@ -11,7 +11,7 @@ #include #include -#include +#include // for readPem func //#include namespace mine { @@ -52,27 +52,27 @@ class Helper : public BigIntegerHelper }; class RSA : public GenericRSA {}; -class PublicKey : public GenericPublicKey {}; -class PrivateKey : public GenericPrivateKey {}; +class PublicKey : public GenericPublicKey {}; // you can choose to not add this line +class PrivateKey : public GenericPrivateKey {}; // you can choose to not add this line class KeyPair : public GenericKeyPair { using GenericKeyPair::GenericKeyPair; -}; +public: -KeyPair readPem(const std::string& contents, const std::string& secret) -{ - CryptoPP::RSA::PrivateKey keyOut; + void loadFromPem(const std::string& contents, const std::string& secret) { - using namespace CryptoPP; - StringSource source(contents, true); - if (secret.empty()) { - PEM_Load(source, keyOut); - } else { - PEM_Load(source, keyOut, secret.data(), secret.size()); + CryptoPP::RSA::PrivateKey keyOut; + { + using namespace CryptoPP; + StringSource source(contents, true); + if (secret.empty()) { + PEM_Load(source, keyOut); + } else { + PEM_Load(source, keyOut, secret.data(), secret.size()); + } } + init(keyOut.GetPrime1(), keyOut.GetPrime2(), static_cast(keyOut.GetPublicExponent().ConvertToLong())); } - return KeyPair(keyOut.GetPrime1(), keyOut.GetPrime2(), - static_cast(keyOut.GetPublicExponent().ConvertToLong())); -} +}; static RSA rsaManager; static Helper rsaHelper; @@ -296,7 +296,8 @@ TEST(RSATest, KeyRead) "YWHcxTUpycFR4RElIWjV0eHVJdUlnZDdSN0tsenB2CnNFWDc0QUYyTjFlNVZsNkV3WHRqcHFoeGlRVnVEWmVqQzcvMEU5dGR5YjRWdk9wOVNoRm85cXNHekxKb1J" "IQVgKZmlsWmxZcGdtbXlpNlNnY1ExSERuRWZYZkpMbWROY0tUVEVqNWo3MkhXN0RpQ1Eza3BpN1lhZzRsQlE1MGZ4QwotLS0tLUVORCBSU0EgUFJJVkFURSBLRVk" "tLS0tLQo="); - KeyPair k = readPem(Base64::decode(pem), "asdf"); + KeyPair k; + k.loadFromPem(Base64::decode(pem), "asdf"); std::cout << "Run: echo " << rsaManager.encrypt(k.publicKey(), std::string("Testing this long secret")) << " | ripe -d --rsa --in-key private2.pem --secret asdf --hex" << std::endl; From b5f6452ce98b0b5bcf2f36cfe63275ab98aa5ab9 Mon Sep 17 00:00:00 2001 From: mkhan Date: Mon, 4 Sep 2017 16:50:43 +1000 Subject: [PATCH 143/148] update --- package/mine.h | 24 +++++++++++++++++------- src/rsa.h | 24 +++++++++++++++++------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/package/mine.h b/package/mine.h index f929080..b4f899e 100644 --- a/package/mine.h +++ b/package/mine.h @@ -1160,10 +1160,15 @@ class GenericPublicKey { return *this; } - GenericPublicKey(BigInteger n, int e) : - m_n(n), - m_e(e) + GenericPublicKey(BigInteger n, int e) { + init(n, e); + } + + void init(const BigInteger& n, int e = kDefaultPublicExponent) + { + m_n = n; + m_e = e; m_k = m_helper.countBytes(m_n); if (m_k < 11) { throw std::invalid_argument("Invalid prime. Length error."); @@ -1221,15 +1226,20 @@ class GenericPrivateKey { return *this; } - GenericPrivateKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) : - m_p(p), - m_q(q), - m_e(e) + GenericPrivateKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) + + { + init(p, q, e); + } + void init(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) { if (p == q || p == 0 || q == 0) { throw std::invalid_argument("p and q must be prime numbers unique to each other"); } + m_p = p; + m_q = q; + m_e = e; const BigInteger pMinus1 = m_p - 1; const BigInteger qMinus1 = m_q - 1; diff --git a/src/rsa.h b/src/rsa.h index 8101754..276bc35 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -324,10 +324,15 @@ class GenericPublicKey { return *this; } - GenericPublicKey(BigInteger n, int e) : - m_n(n), - m_e(e) + GenericPublicKey(BigInteger n, int e) { + init(n, e); + } + + void init(const BigInteger& n, int e = kDefaultPublicExponent) + { + m_n = n; + m_e = e; m_k = m_helper.countBytes(m_n); if (m_k < 11) { throw std::invalid_argument("Invalid prime. Length error."); @@ -385,15 +390,20 @@ class GenericPrivateKey { return *this; } - GenericPrivateKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) : - m_p(p), - m_q(q), - m_e(e) + GenericPrivateKey(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) + + { + init(p, q, e); + } + void init(const BigInteger& p, const BigInteger& q, int e = kDefaultPublicExponent) { if (p == q || p == 0 || q == 0) { throw std::invalid_argument("p and q must be prime numbers unique to each other"); } + m_p = p; + m_q = q; + m_e = e; const BigInteger pMinus1 = m_p - 1; const BigInteger qMinus1 = m_q - 1; From 5405438f75e868aae3aeed51de51e3a227688269 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 5 Sep 2017 09:57:12 +1000 Subject: [PATCH 144/148] encoding to minecommon --- cli/mine.cc | 4 ++-- package/mine.cc | 28 ++++++++++++------------ package/mine.h | 38 ++++++++++++++++---------------- src/aes.cc | 28 ++++++++++++------------ src/aes.h | 29 +++++++++--------------- src/mine-common.h | 9 ++++++++ src/rsa.h | 36 +++++++++++++++--------------- test/aes-test.h | 56 +++++++++++++++++++++++------------------------ 8 files changed, 114 insertions(+), 114 deletions(-) diff --git a/cli/mine.cc b/cli/mine.cc index 92c4ffc..738c7e6 100644 --- a/cli/mine.cc +++ b/cli/mine.cc @@ -89,7 +89,7 @@ void encryptAES(std::string& data, const std::string& key, std::string& iv, bool { TRY bool newIv = iv.empty(); - std::cout << aes.encrypt(data, key, iv, AES::Encoding::Raw, isBase64 ? AES::Encoding::Base64 : AES::Encoding::Base16); + std::cout << aes.encrypt(data, key, iv, MineCommon::Encoding::Raw, isBase64 ? MineCommon::Encoding::Base64 : MineCommon::Encoding::Base16); if (newIv) { std::cout << std::endl << "IV: " << iv << std::endl; @@ -100,7 +100,7 @@ void encryptAES(std::string& data, const std::string& key, std::string& iv, bool void decryptAES(std::string& data, const std::string& key, std::string& iv, bool isBase64) { TRY - std::cout << aes.decrypt(data, key, iv, isBase64 ? AES::Encoding::Base64 : AES::Encoding::Base16); + std::cout << aes.decrypt(data, key, iv, isBase64 ? MineCommon::Encoding::Base64 : MineCommon::Encoding::Base16); CATCH } diff --git a/package/mine.cc b/package/mine.cc index 94a450b..4a85209 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -744,22 +744,22 @@ ByteArray AES::decryptSingleBlock(const ByteArray::const_iterator& range, const return stateToByteArray(&state); } -ByteArray AES::resolveInputMode(const std::string& input, Encoding inputMode) +ByteArray AES::resolveInputMode(const std::string& input, MineCommon::Encoding inputMode) { - if (inputMode == Encoding::Raw) { + if (inputMode == MineCommon::Encoding::Raw) { return Base16::fromString(Base16::encode(input)); - } else if (inputMode == Encoding::Base16) { + } else if (inputMode == MineCommon::Encoding::Base16) { return Base16::fromString(input); } // base64 return Base16::fromString(Base16::encode(Base64::decode(input))); } -std::string AES::resolveOutputMode(const ByteArray& input, Encoding outputMode) +std::string AES::resolveOutputMode(const ByteArray& input, MineCommon::Encoding outputMode) { - if (outputMode == Encoding::Raw) { + if (outputMode == MineCommon::Encoding::Raw) { return Base16::toRawString(input); - } else if (outputMode == Encoding::Base16) { + } else if (outputMode == MineCommon::Encoding::Base16) { return Base16::encode(input.begin(), input.end()); } // base64 @@ -981,7 +981,7 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) return result; } -std::string AES::encrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +std::string AES::encrypt(const std::string& input, const std::string& key, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding, bool pkcs5Padding) { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); @@ -989,7 +989,7 @@ std::string AES::encrypt(const std::string& input, const std::string& key, Encod return resolveOutputMode(result, outputEncoding); } -std::string AES::encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +std::string AES::encrypt(const std::string& input, const std::string& key, std::string& iv, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding, bool pkcs5Padding) { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); @@ -1002,7 +1002,7 @@ std::string AES::encrypt(const std::string& input, const std::string& key, std:: return resolveOutputMode(result, outputEncoding); } -std::string AES::decrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::decrypt(const std::string& input, const std::string& key, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding) { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); @@ -1010,7 +1010,7 @@ std::string AES::decrypt(const std::string& input, const std::string& key, Encod return resolveOutputMode(result, outputEncoding); } -std::string AES::decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::decrypt(const std::string& input, const std::string& key, const std::string& iv, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding) { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); @@ -1031,7 +1031,7 @@ std::string AES::generateRandomKey(const std::size_t len) // encryption / decryption with previously provided key -std::string AES::encr(const std::string& input, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +std::string AES::encr(const std::string& input, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding, bool pkcs5Padding) { if (m_key.empty()) { throw std::runtime_error("Key not set"); @@ -1039,7 +1039,7 @@ std::string AES::encr(const std::string& input, Encoding inputEncoding, Encoding return encrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding, pkcs5Padding); } -std::string AES::encr(const std::string& input, std::string& iv, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +std::string AES::encr(const std::string& input, std::string& iv, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding, bool pkcs5Padding) { if (m_key.empty()) { throw std::runtime_error("Key not set"); @@ -1047,7 +1047,7 @@ std::string AES::encr(const std::string& input, std::string& iv, Encoding inputE return encrypt(input, Base16::encode(Base16::toRawString(m_key)), iv, inputEncoding, outputEncoding, pkcs5Padding); } -std::string AES::decr(const std::string& input, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::decr(const std::string& input, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding) { if (m_key.empty()) { throw std::runtime_error("Key not set"); @@ -1055,7 +1055,7 @@ std::string AES::decr(const std::string& input, Encoding inputEncoding, Encoding return decrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding); } -std::string AES::decr(const std::string& input, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::decr(const std::string& input, const std::string& iv, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding) { if (m_key.empty()) { throw std::runtime_error("Key not set"); diff --git a/package/mine.h b/package/mine.h index b4f899e..f2fb12c 100644 --- a/package/mine.h +++ b/package/mine.h @@ -44,6 +44,15 @@ using ByteArray = std::vector; class MineCommon { public: + /// + /// \brief Convert mode for various functions + /// + enum class Encoding { + Raw, + Base16, + Base64 + }; + /// /// \brief Total items in random bytes list /// @@ -515,15 +524,6 @@ class Base64 { class AES { public: - /// - /// \brief Convert mode for various functions - /// - enum class Encoding { - Raw, - Base16, - Base64 - }; - /// /// \brief A key is a byte array /// @@ -553,7 +553,7 @@ class AES { /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Base16 encoded cipher /// - std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + std::string encrypt(const std::string& input, const std::string& key, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Raw, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Base16, bool pkcs5Padding = true); /// /// \brief Ciphers the input with specified hex key using CBC mode @@ -564,7 +564,7 @@ class AES { /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Base16 encoded cipher /// - std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + std::string encrypt(const std::string& input, const std::string& key, std::string& iv, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Raw, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Base16, bool pkcs5Padding = true); /// /// \brief Deciphers the input with specified hex key @@ -573,7 +573,7 @@ class AES { /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - std::string decrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + std::string decrypt(const std::string& input, const std::string& key, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Base16, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Raw); /// /// \brief Deciphers the input with specified hex key using CBC mode @@ -583,7 +583,7 @@ class AES { /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - std::string decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + std::string decrypt(const std::string& input, const std::string& key, const std::string& iv, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Base16, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Raw); /// /// \brief Ciphers with ECB-Mode, the input can be as long as user wants @@ -624,13 +624,13 @@ class AES { // cipher / decipher interface without keys - std::string encr(const std::string& input, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + std::string encr(const std::string& input, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Raw, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Base16, bool pkcs5Padding = true); - std::string encr(const std::string& input, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + std::string encr(const std::string& input, std::string& iv, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Raw, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Base16, bool pkcs5Padding = true); - std::string decr(const std::string& input, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + std::string decr(const std::string& input, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Base16, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Raw); - std::string decr(const std::string& input, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + std::string decr(const std::string& input, const std::string& iv, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Base16, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Raw); ByteArray encr(const ByteArray& input, bool pkcs5Padding = true); @@ -801,12 +801,12 @@ class AES { /// /// \brief Creates byte array from input based on input mode /// - static ByteArray resolveInputMode(const std::string& input, Encoding inputMode); + static ByteArray resolveInputMode(const std::string& input, MineCommon::Encoding inputMode); /// /// \brief Creates string from byte array based on convert mode /// - static std::string resolveOutputMode(const ByteArray& input, Encoding outputMode); + static std::string resolveOutputMode(const ByteArray& input, MineCommon::Encoding outputMode); /// /// \brief Exclusive XOR with iter of range size as input diff --git a/src/aes.cc b/src/aes.cc index 42e9be1..4f1b23e 100644 --- a/src/aes.cc +++ b/src/aes.cc @@ -616,22 +616,22 @@ ByteArray AES::decryptSingleBlock(const ByteArray::const_iterator& range, const return stateToByteArray(&state); } -ByteArray AES::resolveInputMode(const std::string& input, Encoding inputMode) +ByteArray AES::resolveInputMode(const std::string& input, MineCommon::Encoding inputMode) { - if (inputMode == Encoding::Raw) { + if (inputMode == MineCommon::Encoding::Raw) { return Base16::fromString(Base16::encode(input)); - } else if (inputMode == Encoding::Base16) { + } else if (inputMode == MineCommon::Encoding::Base16) { return Base16::fromString(input); } // base64 return Base16::fromString(Base16::encode(Base64::decode(input))); } -std::string AES::resolveOutputMode(const ByteArray& input, Encoding outputMode) +std::string AES::resolveOutputMode(const ByteArray& input, MineCommon::Encoding outputMode) { - if (outputMode == Encoding::Raw) { + if (outputMode == MineCommon::Encoding::Raw) { return Base16::toRawString(input); - } else if (outputMode == Encoding::Base16) { + } else if (outputMode == MineCommon::Encoding::Base16) { return Base16::encode(input.begin(), input.end()); } // base64 @@ -853,7 +853,7 @@ ByteArray AES::decrypt(const ByteArray& input, const Key* key, ByteArray& iv) return result; } -std::string AES::encrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +std::string AES::encrypt(const std::string& input, const std::string& key, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding, bool pkcs5Padding) { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); @@ -861,7 +861,7 @@ std::string AES::encrypt(const std::string& input, const std::string& key, Encod return resolveOutputMode(result, outputEncoding); } -std::string AES::encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +std::string AES::encrypt(const std::string& input, const std::string& key, std::string& iv, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding, bool pkcs5Padding) { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); @@ -874,7 +874,7 @@ std::string AES::encrypt(const std::string& input, const std::string& key, std:: return resolveOutputMode(result, outputEncoding); } -std::string AES::decrypt(const std::string& input, const std::string& key, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::decrypt(const std::string& input, const std::string& key, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding) { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); @@ -882,7 +882,7 @@ std::string AES::decrypt(const std::string& input, const std::string& key, Encod return resolveOutputMode(result, outputEncoding); } -std::string AES::decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::decrypt(const std::string& input, const std::string& key, const std::string& iv, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding) { Key keyArr = Base16::fromString(key); ByteArray inp = resolveInputMode(input, inputEncoding); @@ -903,7 +903,7 @@ std::string AES::generateRandomKey(const std::size_t len) // encryption / decryption with previously provided key -std::string AES::encr(const std::string& input, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +std::string AES::encr(const std::string& input, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding, bool pkcs5Padding) { if (m_key.empty()) { throw std::runtime_error("Key not set"); @@ -911,7 +911,7 @@ std::string AES::encr(const std::string& input, Encoding inputEncoding, Encoding return encrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding, pkcs5Padding); } -std::string AES::encr(const std::string& input, std::string& iv, Encoding inputEncoding, Encoding outputEncoding, bool pkcs5Padding) +std::string AES::encr(const std::string& input, std::string& iv, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding, bool pkcs5Padding) { if (m_key.empty()) { throw std::runtime_error("Key not set"); @@ -919,7 +919,7 @@ std::string AES::encr(const std::string& input, std::string& iv, Encoding inputE return encrypt(input, Base16::encode(Base16::toRawString(m_key)), iv, inputEncoding, outputEncoding, pkcs5Padding); } -std::string AES::decr(const std::string& input, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::decr(const std::string& input, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding) { if (m_key.empty()) { throw std::runtime_error("Key not set"); @@ -927,7 +927,7 @@ std::string AES::decr(const std::string& input, Encoding inputEncoding, Encoding return decrypt(input, Base16::encode(Base16::toRawString(m_key)), inputEncoding, outputEncoding); } -std::string AES::decr(const std::string& input, const std::string& iv, Encoding inputEncoding, Encoding outputEncoding) +std::string AES::decr(const std::string& input, const std::string& iv, MineCommon::Encoding inputEncoding, MineCommon::Encoding outputEncoding) { if (m_key.empty()) { throw std::runtime_error("Key not set"); diff --git a/src/aes.h b/src/aes.h index 2e98870..5956e7c 100644 --- a/src/aes.h +++ b/src/aes.h @@ -46,15 +46,6 @@ namespace mine { class AES { public: - /// - /// \brief Convert mode for various functions - /// - enum class Encoding { - Raw, - Base16, - Base64 - }; - /// /// \brief A key is a byte array /// @@ -84,7 +75,7 @@ class AES { /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Base16 encoded cipher /// - std::string encrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + std::string encrypt(const std::string& input, const std::string& key, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Raw, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Base16, bool pkcs5Padding = true); /// /// \brief Ciphers the input with specified hex key using CBC mode @@ -95,7 +86,7 @@ class AES { /// \param pkcs5Padding Defaults to true, if false non-standard zero-padding is used /// \return Base16 encoded cipher /// - std::string encrypt(const std::string& input, const std::string& key, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + std::string encrypt(const std::string& input, const std::string& key, std::string& iv, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Raw, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Base16, bool pkcs5Padding = true); /// /// \brief Deciphers the input with specified hex key @@ -104,7 +95,7 @@ class AES { /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - std::string decrypt(const std::string& input, const std::string& key, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + std::string decrypt(const std::string& input, const std::string& key, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Base16, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Raw); /// /// \brief Deciphers the input with specified hex key using CBC mode @@ -114,7 +105,7 @@ class AES { /// \param outputEncoding Type of encoding for result /// \return Base16 encoded cipher /// - std::string decrypt(const std::string& input, const std::string& key, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + std::string decrypt(const std::string& input, const std::string& key, const std::string& iv, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Base16, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Raw); /// /// \brief Ciphers with ECB-Mode, the input can be as long as user wants @@ -155,13 +146,13 @@ class AES { // cipher / decipher interface without keys - std::string encr(const std::string& input, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + std::string encr(const std::string& input, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Raw, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Base16, bool pkcs5Padding = true); - std::string encr(const std::string& input, std::string& iv, Encoding inputEncoding = Encoding::Raw, Encoding outputEncoding = Encoding::Base16, bool pkcs5Padding = true); + std::string encr(const std::string& input, std::string& iv, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Raw, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Base16, bool pkcs5Padding = true); - std::string decr(const std::string& input, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + std::string decr(const std::string& input, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Base16, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Raw); - std::string decr(const std::string& input, const std::string& iv, Encoding inputEncoding = Encoding::Base16, Encoding outputEncoding = Encoding::Raw); + std::string decr(const std::string& input, const std::string& iv, MineCommon::Encoding inputEncoding = MineCommon::Encoding::Base16, MineCommon::Encoding outputEncoding = MineCommon::Encoding::Raw); ByteArray encr(const ByteArray& input, bool pkcs5Padding = true); @@ -332,12 +323,12 @@ class AES { /// /// \brief Creates byte array from input based on input mode /// - static ByteArray resolveInputMode(const std::string& input, Encoding inputMode); + static ByteArray resolveInputMode(const std::string& input, MineCommon::Encoding inputMode); /// /// \brief Creates string from byte array based on convert mode /// - static std::string resolveOutputMode(const ByteArray& input, Encoding outputMode); + static std::string resolveOutputMode(const ByteArray& input, MineCommon::Encoding outputMode); /// /// \brief Exclusive XOR with iter of range size as input diff --git a/src/mine-common.h b/src/mine-common.h index 8a22ba0..3441e7d 100644 --- a/src/mine-common.h +++ b/src/mine-common.h @@ -44,6 +44,15 @@ using ByteArray = std::vector; class MineCommon { public: + /// + /// \brief Convert mode for various functions + /// + enum class Encoding { + Raw, + Base16, + Base64 + }; + /// /// \brief Total items in random bytes list /// diff --git a/src/rsa.h b/src/rsa.h index 276bc35..43f20a2 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -543,23 +543,6 @@ class GenericRSA { GenericRSA(const GenericRSA&) = delete; GenericRSA& operator=(const GenericRSA&) = delete; - /// - /// \brief Encrypts plain bytes using RSA public key - /// \param publicKey RSA Public key for encryption - /// \param m The message. This can be raw bytes or plain text - /// T can of std::string or std::wstring or custom string type that has - /// basic_stringstream implementation alongside it - /// \note Mine uses pkcs#1 padding scheme - /// \return hex of cipher - /// - template - std::string encrypt(const PublicKey* publicKey, const T& m) - { - BigInteger paddedMsg = addPadding(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); - BigInteger cipher = m_helper.powerMod(paddedMsg, publicKey->e(), publicKey->n()); - return m_helper.bigIntegerToHex(cipher); - } - /// /// \brief Helper method to encrypt wide-string messages using public key. /// \see encrypt(const GenericPublicKey* publicKey, const T& m) @@ -580,6 +563,23 @@ class GenericRSA { return encrypt(publicKey, message); } + /// + /// \brief Encrypts plain bytes using RSA public key + /// \param publicKey RSA Public key for encryption + /// \param m The message. This can be raw bytes or plain text + /// T can of std::string or std::wstring or custom string type that has + /// basic_stringstream implementation alongside it + /// \note Mine uses pkcs#1 padding scheme + /// \return hex of cipher + /// + template + std::string encrypt(const PublicKey* publicKey, const T& m) + { + BigInteger paddedMsg = addPadding(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); + BigInteger cipher = m_helper.powerMod(paddedMsg, publicKey->e(), publicKey->n()); + return m_helper.bigIntegerToHex(cipher); + } + /// /// \brief Decrypts RSA hex message using RSA private key /// \param privateKey RSA private key @@ -591,7 +591,7 @@ class GenericRSA { { BigInteger msg = m_helper.hexToBigInteger(c); int xlen = (m_helper.countBits(privateKey->n()) + 7) >> 3; - if (msg >= m_helper.power(BigInteger(256), BigInteger(xlen))) { + if (msg >= m_helper.power(BigIntegerHelper::kBigInteger256, BigInteger(xlen))) { throw std::runtime_error("Integer too large"); } BigInteger decr = m_helper.powerMod(msg, privateKey->d(), privateKey->n()); diff --git a/test/aes-test.h b/test/aes-test.h index c132f54..018d032 100644 --- a/test/aes-test.h +++ b/test/aes-test.h @@ -516,7 +516,7 @@ TEST(AESTest, RawCipherDirect) for (auto& item : RawCipherData) { std::string expected = PARAM(2); - std::string output = aes.encrypt(PARAM(0), PARAM(1), AES::Encoding::Base16); + std::string output = aes.encrypt(PARAM(0), PARAM(1), MineCommon::Encoding::Base16); // case insensitive comparison because hex can be upper or lower case ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } @@ -531,7 +531,7 @@ TEST(AESTest, RawCipherPlain) { for (auto& item : RawCipherPlainInputData) { std::string expected = PARAM(2); - std::string output = aes.encrypt(PARAM(0), PARAM(1), AES::Encoding::Raw, AES::Encoding::Base16, false); + std::string output = aes.encrypt(PARAM(0), PARAM(1), MineCommon::Encoding::Raw, MineCommon::Encoding::Base16, false); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -545,7 +545,7 @@ TEST(AESTest, RawCipherBase64) { for (auto& item : RawCipherBase64InputData) { std::string expected = PARAM(2); - std::string output = aes.encrypt(PARAM(0), PARAM(1), AES::Encoding::Base64, AES::Encoding::Base16, false); + std::string output = aes.encrypt(PARAM(0), PARAM(1), MineCommon::Encoding::Base64, MineCommon::Encoding::Base16, false); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -638,8 +638,8 @@ TEST(AESTest, CbcCipher) std::string k = Base16::encode(Base16::toRawString(key)); std::string initVec = Base16::encode(Base16::toRawString(iv)); std::string output = aes.encrypt(input, k, initVec, - AES::Encoding::Raw, - AES::Encoding::Base16, false); + MineCommon::Encoding::Raw, + MineCommon::Encoding::Base16, false); ASSERT_STREQ(expected.c_str(), output.c_str()); } @@ -668,7 +668,7 @@ TEST(AESTest, HexStringDecipher) { for (auto& item : RawDecipherData) { std::string expected = PARAM(0); - std::string output = aes.decrypt(PARAM(2), PARAM(1), AES::Encoding::Base16, AES::Encoding::Base16); + std::string output = aes.decrypt(PARAM(2), PARAM(1), MineCommon::Encoding::Base16, MineCommon::Encoding::Base16); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } } @@ -680,8 +680,8 @@ TEST(AESTest, Base64StringDecipher) std::string expected = "dGhpcyBpcyB0ZXN0Li4uLg=="; // base64("this is test....") std::string output = aes.decrypt("b92daaae6e57773b10653703af12716f", "000102030405060708090a0b0c0d0e0f", - AES::Encoding::Base16, - AES::Encoding::Base64); + MineCommon::Encoding::Base16, + MineCommon::Encoding::Base64); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } @@ -690,8 +690,8 @@ TEST(AESTest, Base64StringInputDecipher) std::string expected = "this is test.."; std::string output = aes.decrypt("Z0BiQ8NcwknqzbGrWBjXqw==", "000102030405060708090a0b0c0d0e0f", - AES::Encoding::Base64, - AES::Encoding::Raw); + MineCommon::Encoding::Base64, + MineCommon::Encoding::Raw); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } @@ -702,12 +702,12 @@ TEST(AESTest, CbcCipherPadding) std::string cipherB64 = "OcTPoBeDqlA/igjnNcl5yw=="; std::string expected = "o1223456789012"; - std::string output = aes.decrypt(cipherB64, key, iv, AES::Encoding::Base64); + std::string output = aes.decrypt(cipherB64, key, iv, MineCommon::Encoding::Base64); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); cipherB64 = "NNH44Ybac3AhcP4+sTq8j4miT04jHtoaj7a/Wv0/TQ8="; expected = "sho4123456789014"; - output = aes.decrypt(cipherB64, key, iv, AES::Encoding::Base64); + output = aes.decrypt(cipherB64, key, iv, MineCommon::Encoding::Base64); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); } @@ -795,8 +795,8 @@ TEST(AESTest, CbcDecipher) std::string k = Base16::encode(Base16::toRawString(key)); std::string initVec = Base16::encode(Base16::toRawString(iv)); std::string output = aes.decrypt(input, k, initVec, - AES::Encoding::Raw, - AES::Encoding::Raw); + MineCommon::Encoding::Raw, + MineCommon::Encoding::Raw); ASSERT_STREQ(expected.c_str(), output.c_str()); } @@ -813,8 +813,8 @@ TEST(AESTest, CrossAppsDataTest) std::string output = aes.encrypt("test this test this", "CBD437FA37772C66051A47D72367B38E", iv, - AES::Encoding::Raw, - AES::Encoding::Base64); + MineCommon::Encoding::Raw, + MineCommon::Encoding::Base64); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); @@ -822,8 +822,8 @@ TEST(AESTest, CrossAppsDataTest) output = aes.decrypt("WQ73OMIum+OHKGHnAhQKJX1tByfBq4BhSpw2X+SgtjY=", key, iv, - AES::Encoding::Base64, - AES::Encoding::Raw); + MineCommon::Encoding::Base64, + MineCommon::Encoding::Raw); ASSERT_STRCASEEQ(nextexp.c_str(), output.c_str()); @@ -832,8 +832,8 @@ TEST(AESTest, CrossAppsDataTest) output = aes.encrypt(R"({"_t":1503928197,"logger_id":"default","access_code":"default"})", key, iv, - AES::Encoding::Raw, - AES::Encoding::Base64); + MineCommon::Encoding::Raw, + MineCommon::Encoding::Base64); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); @@ -844,8 +844,8 @@ TEST(AESTest, CrossAppsDataTest) output = aes.decrypt("EtYr5JFo/7kqYWxooMvU2DJ+upNhUMDii9X6IEHYxvUNXSVGk34IakT5H7GbyzL5/JIMMAQCLnUU824RI3ymgQ==", key, iv, - AES::Encoding::Base64, - AES::Encoding::Raw); + MineCommon::Encoding::Base64, + MineCommon::Encoding::Raw); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); @@ -855,8 +855,8 @@ TEST(AESTest, CrossAppsDataTest) output = aes.decrypt("WQ73OMIum+OHKGHnAhQKJX1tByfBq4BhSpw2X+SgtjY=", key, iv, - AES::Encoding::Base64, - AES::Encoding::Raw); + MineCommon::Encoding::Base64, + MineCommon::Encoding::Raw); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); @@ -867,8 +867,8 @@ TEST(AESTest, CrossAppsDataTest) output = aes.decrypt("WQ73OMIum+OHKGHnAhQKJdSsXR5NwysOnq+cuf5C6cs=", key, iv, - AES::Encoding::Base64, - AES::Encoding::Raw); + MineCommon::Encoding::Base64, + MineCommon::Encoding::Raw); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); @@ -879,8 +879,8 @@ TEST(AESTest, CrossAppsDataTest) output = aes.decrypt("vVMWB9aLpcfRgfai7OnCCLI5aAK+kK3Yem/E03uEM+w=", keyBig, iv, - AES::Encoding::Base64, - AES::Encoding::Raw); + MineCommon::Encoding::Base64, + MineCommon::Encoding::Raw); ASSERT_STRCASEEQ(expected.c_str(), output.c_str()); From 5a403fc12e06c45af369b40a61a08f7bd23232fb Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 5 Sep 2017 12:31:24 +1000 Subject: [PATCH 145/148] key empty() --- package/mine.h | 38 ++++++++++++++++++++------------------ src/rsa.h | 2 ++ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/package/mine.h b/package/mine.h index f2fb12c..047b278 100644 --- a/package/mine.h +++ b/package/mine.h @@ -1180,6 +1180,7 @@ class GenericPublicKey { inline BigInteger n() const { return m_n; } inline int e() const { return m_e; } inline unsigned int k() const { return m_k; } + inline bool empty() const { return m_e == 0 || m_n == 0; } protected: BigIntegerHelper m_helper; @@ -1276,6 +1277,7 @@ class GenericPrivateKey { inline BigInteger dp() const { return m_dq; } inline BigInteger dq() const { return m_dp; } inline int k() const { return m_k; } + inline bool empty() const { return m_p == 0 || m_q == 0; } friend std::ostream& operator<<(std::ostream& ss, const GenericPrivateKey& k) { @@ -1379,23 +1381,6 @@ class GenericRSA { GenericRSA(const GenericRSA&) = delete; GenericRSA& operator=(const GenericRSA&) = delete; - /// - /// \brief Encrypts plain bytes using RSA public key - /// \param publicKey RSA Public key for encryption - /// \param m The message. This can be raw bytes or plain text - /// T can of std::string or std::wstring or custom string type that has - /// basic_stringstream implementation alongside it - /// \note Mine uses pkcs#1 padding scheme - /// \return hex of cipher - /// - template - std::string encrypt(const PublicKey* publicKey, const T& m) - { - BigInteger paddedMsg = addPadding(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); - BigInteger cipher = m_helper.powerMod(paddedMsg, publicKey->e(), publicKey->n()); - return m_helper.bigIntegerToHex(cipher); - } - /// /// \brief Helper method to encrypt wide-string messages using public key. /// \see encrypt(const GenericPublicKey* publicKey, const T& m) @@ -1416,6 +1401,23 @@ class GenericRSA { return encrypt(publicKey, message); } + /// + /// \brief Encrypts plain bytes using RSA public key + /// \param publicKey RSA Public key for encryption + /// \param m The message. This can be raw bytes or plain text + /// T can of std::string or std::wstring or custom string type that has + /// basic_stringstream implementation alongside it + /// \note Mine uses pkcs#1 padding scheme + /// \return hex of cipher + /// + template + std::string encrypt(const PublicKey* publicKey, const T& m) + { + BigInteger paddedMsg = addPadding(m, (m_helper.countBits(publicKey->n()) + 7) >> 3); + BigInteger cipher = m_helper.powerMod(paddedMsg, publicKey->e(), publicKey->n()); + return m_helper.bigIntegerToHex(cipher); + } + /// /// \brief Decrypts RSA hex message using RSA private key /// \param privateKey RSA private key @@ -1427,7 +1429,7 @@ class GenericRSA { { BigInteger msg = m_helper.hexToBigInteger(c); int xlen = (m_helper.countBits(privateKey->n()) + 7) >> 3; - if (msg >= m_helper.power(BigInteger(256), BigInteger(xlen))) { + if (msg >= m_helper.power(BigIntegerHelper::kBigInteger256, BigInteger(xlen))) { throw std::runtime_error("Integer too large"); } BigInteger decr = m_helper.powerMod(msg, privateKey->d(), privateKey->n()); diff --git a/src/rsa.h b/src/rsa.h index 43f20a2..38580be 100644 --- a/src/rsa.h +++ b/src/rsa.h @@ -344,6 +344,7 @@ class GenericPublicKey { inline BigInteger n() const { return m_n; } inline int e() const { return m_e; } inline unsigned int k() const { return m_k; } + inline bool empty() const { return m_e == 0 || m_n == 0; } protected: BigIntegerHelper m_helper; @@ -440,6 +441,7 @@ class GenericPrivateKey { inline BigInteger dp() const { return m_dq; } inline BigInteger dq() const { return m_dp; } inline int k() const { return m_k; } + inline bool empty() const { return m_p == 0 || m_q == 0; } friend std::ostream& operator<<(std::ostream& ss, const GenericPrivateKey& k) { From 4bf16117244282903bf15ad2ac3ccf910e34c69c Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 5 Sep 2017 13:01:43 +1000 Subject: [PATCH 146/148] update for lower b16 --- package/mine.cc | 5 ++++- src/base16.cc | 5 ++++- test/base16-test.h | 7 ++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/package/mine.cc b/package/mine.cc index 4a85209..b187de5 100644 --- a/package/mine.cc +++ b/package/mine.cc @@ -101,7 +101,10 @@ const std::unordered_map Base16::kDecodeMap = { {0x30, 0x00}, {0x31, 0x01}, {0x32, 0x02}, {0x33, 0x03}, {0x34, 0x04}, {0x35, 0x05}, {0x36, 0x06}, {0x37, 0x07}, {0x38, 0x08}, {0x39, 0x09}, {0x41, 0x0A}, {0x42, 0x0B}, - {0x43, 0x0C}, {0x44, 0x0D}, {0x45, 0x0E}, {0x46, 0x0F} + {0x43, 0x0C}, {0x44, 0x0D}, {0x45, 0x0E}, {0x46, 0x0F}, + // lower case + {0x61, 0x0A}, {0x62, 0x0B}, {0x63, 0x0C}, {0x64, 0x0D}, + {0x65, 0x0E}, {0x66, 0x0F} }; ByteArray Base16::fromString(const std::string& hex) diff --git a/src/base16.cc b/src/base16.cc index 447a9b3..f916fb6 100644 --- a/src/base16.cc +++ b/src/base16.cc @@ -27,7 +27,10 @@ const std::unordered_map Base16::kDecodeMap = { {0x30, 0x00}, {0x31, 0x01}, {0x32, 0x02}, {0x33, 0x03}, {0x34, 0x04}, {0x35, 0x05}, {0x36, 0x06}, {0x37, 0x07}, {0x38, 0x08}, {0x39, 0x09}, {0x41, 0x0A}, {0x42, 0x0B}, - {0x43, 0x0C}, {0x44, 0x0D}, {0x45, 0x0E}, {0x46, 0x0F} + {0x43, 0x0C}, {0x44, 0x0D}, {0x45, 0x0E}, {0x46, 0x0F}, + // lower case + {0x61, 0x0A}, {0x62, 0x0B}, {0x63, 0x0C}, {0x64, 0x0D}, + {0x65, 0x0E}, {0x66, 0x0F} }; ByteArray Base16::fromString(const std::string& hex) diff --git a/test/base16-test.h b/test/base16-test.h index 98cc180..850c786 100644 --- a/test/base16-test.h +++ b/test/base16-test.h @@ -33,6 +33,7 @@ static TestData InvalidBase16EncodingData = { }; static TestData EncodingDecodingData = { + TestCase("ac7322f9ac5ebe9f32703c14f12e908046ed26"), // lower TestCase("C1"), TestCase("78DA2B492D2E0100045D01C1"), }; @@ -41,7 +42,7 @@ TEST(Base16Test, Encode) { for (const auto& item : Base16TestData) { std::string encoded = Base16::encode(PARAM(1)); - ASSERT_STREQ(PARAM(0).c_str(), encoded.c_str()); + ASSERT_STRCASEEQ(PARAM(0).c_str(), encoded.c_str()); } } @@ -49,7 +50,7 @@ TEST(Base16Test, EncodeDecodingTest) { for (const auto& item : EncodingDecodingData) { std::string decoded = Base16::decode(PARAM(0)); - ASSERT_STREQ(PARAM(0).c_str(), Base16::encode(decoded).c_str()); + ASSERT_STRCASEEQ(PARAM(0).c_str(), Base16::encode(decoded).c_str()); } } @@ -57,7 +58,7 @@ TEST(Base16Test, EncodeByteArray) { for (const auto& item : Base16ByteArrayEncodingTestData) { std::string encoded = Base16::encode(PARAM(0).begin(), PARAM(0).end()); - ASSERT_STREQ(PARAM(1).c_str(), encoded.c_str()); + ASSERT_STRCASEEQ(PARAM(1).c_str(), encoded.c_str()); } } From e6e03bd5e1c3fd2c719e8852b986e874a269e57c Mon Sep 17 00:00:00 2001 From: Majid Date: Tue, 5 Sep 2017 13:29:07 +1000 Subject: [PATCH 147/148] Update README.md --- README.md | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 0c35a53..5168b1b 100644 --- a/README.md +++ b/README.md @@ -9,29 +9,35 @@ Mine is single-header minimal cryptography implementation for small-medium proje [![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/MuflihunDotCom/25) # Overview -It all started with [ripe](https://github.com/muflihun/ripe) that is dependent upon third-party library (initially OpenSSL then Crypto++) statically linked. However after deploying [residue](https://github.com/muflihun/residue) (which used ripe until mine came to life) to older distributions of linux, we realized that portability is an issue for ripe as _minimal_ library. So we started to implement the standards ourselves, forming _Mine_. +It all started with [ripe](https://github.com/muflihun/ripe) that depends on third-party library (initially OpenSSL then Crypto++) linked statically. However after deploying [residue](https://github.com/muflihun/residue) with ripe to older distributions of linux, we realized that portability is an issue for ripe as _minimal_ library. So we started to implement standards forming _Mine_. + +We are very careful with our implementations and have [unit tests](/test/) in place. # Installation Simply copy `mine.h` and `mine.cc` from [`package/`](/package/) directory to your project or your local machine. -# Status -Currently, it is not production ready. It depends upon third-party library. We are actively working on the development and implementation of RFC. We cannot guarantee the timeframe as all the contributors are full time workers and only do this project in their spare time. - -We are very careful with our implementations and have [unit tests](/test/) in place. - +Alternatively, feel free to link it as shared or static library (you will need to compile yourself) + # Features -Mine _will_ support following features: +Mine supports following features: - * RSA (Encrypt, Decrypt, Sign and Verify) [[RFC-3447](https://tools.ietf.org/html/rfc3447)] + * Base16 Encoding + * Base64 Encoding + * RSA [[RFC-3447](https://tools.ietf.org/html/rfc3447)] * AES [[FIPS Pub. 197](http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.197.pdf)] - * ZLib (Depend upon libz, eventually implement [RFC-1950](https://tools.ietf.org/html/rfc3602)) - * Base16 (Encode, Decode) - * Base64 (Encode, Decode) + * ZLib (Depends upon libz) This is what we are aiming for _minimal_ crypto library. +# Notes + + * It is natively developed on macOS and Linux operating systems + * It is extremely fast with compiler optimization level 1 (or higher) + * RSA needs big number implementation, for unit tests we use [Integer from Crypto++](https://www.cryptopp.com/wiki/Integer) + * RSA currently does not support signing & verification or reading keys from PEM files + # Contribution -You can only contribute by testing on various platforms and reporting the issues. We are not accepting any pull requests until first release. +You can contribute to the project by testing on various platforms (e.g, Windows, Android etc) # License From 76a56f3829d71116524fdf3933befd40d63b6281 Mon Sep 17 00:00:00 2001 From: mkhan Date: Tue, 5 Sep 2017 13:31:09 +1000 Subject: [PATCH 148/148] first release --- CHANGELOG.md | 5 +++++ build.php | 2 +- package/mine.cc | 4 ++-- package/mine.h | 2 +- 4 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..a59c481 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Change Log + +## [1.0.0] - 05-09-2017 +### Added +- Initial release diff --git a/build.php b/build.php index da64924..08832f7 100644 --- a/build.php +++ b/build.php @@ -6,7 +6,7 @@ /// modules for ease of development /// -$lib_version = "Unreleased"; +$lib_version = "1.0.0"; $header_template = <<