From 8e51c61dbce925e0992e27c2c33311583645bd6f Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Fri, 12 Jan 2018 19:36:41 -0500
Subject: [PATCH] core: Gut out cryptop, since it doesn't compile with C++17.

---
 .gitmodules                          |   3 -
 externals/CMakeLists.txt             |   3 -
 externals/cryptopp/CMakeLists.txt    | 168 ---------------------------
 externals/cryptopp/cryptopp          |   1 -
 src/core/CMakeLists.txt              |   2 +-
 src/core/file_sys/title_metadata.cpp |  51 +-------
 src/core/hw/aes/ccm.cpp              |  77 +-----------
 src/core/telemetry_session.cpp       |   3 -
 8 files changed, 7 insertions(+), 301 deletions(-)
 delete mode 100644 externals/cryptopp/CMakeLists.txt
 delete mode 160000 externals/cryptopp/cryptopp

diff --git a/.gitmodules b/.gitmodules
index 1ebbd57cd9..bffbb9ecb6 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -19,9 +19,6 @@
 [submodule "xbyak"]
     path = externals/xbyak
     url = https://github.com/herumi/xbyak.git
-[submodule "cryptopp"]
-    path = externals/cryptopp/cryptopp
-    url = https://github.com/weidai11/cryptopp.git
 [submodule "fmt"]
     path = externals/fmt
     url = https://github.com/fmtlib/fmt.git
diff --git a/externals/CMakeLists.txt b/externals/CMakeLists.txt
index dcf6c460fb..347641c106 100644
--- a/externals/CMakeLists.txt
+++ b/externals/CMakeLists.txt
@@ -7,9 +7,6 @@ include(DownloadExternals)
 add_library(catch-single-include INTERFACE)
 target_include_directories(catch-single-include INTERFACE catch/single_include)
 
-# Crypto++
-add_subdirectory(cryptopp)
-
 # Dynarmic
 if (ARCHITECTURE_x86_64)
     add_library(xbyak INTERFACE)
diff --git a/externals/cryptopp/CMakeLists.txt b/externals/cryptopp/CMakeLists.txt
deleted file mode 100644
index 8a626e44a3..0000000000
--- a/externals/cryptopp/CMakeLists.txt
+++ /dev/null
@@ -1,168 +0,0 @@
-# The CMakeLists.txt shipped with cryptopp pollutes our option list and installation list,
-# so we made our own one. This is basically a trimmed down version of the shipped CMakeLists.txt
-# The differences are:
-#  - removed support for legacy CMake versions
-#  - removed support for 32-bit
-#  - removed -march=native flag
-#  - removed rdrand module.asm as a workaround for an issue (see below)
-#  - added prefix "CRYPTOPP_" to all option names
-#  - disabled testing
-#  - disabled installation
-#  - disabled documentation
-#  - configured to build a static library only
-#  - adds include directories to the library target
-
-include(TestBigEndian)
-include(CheckCXXCompilerFlag)
-
-#============================================================================
-# Settable options
-#============================================================================
-
-option(CRYPTOPP_DISABLE_ASM "Disable ASM" OFF)
-option(CRYPTOPP_DISABLE_SSSE3 "Disable SSSE3" OFF)
-option(CRYPTOPP_DISABLE_AESNI "Disable AES-NI" OFF)
-option(CRYPTOPP_DISABLE_CXXFLAGS_OPTIMIZATIONS "Disable CXXFLAGS optimizations" OFF)
-
-#============================================================================
-# Internal compiler options
-#============================================================================
-
-# Only set when cross-compiling, http://www.vtk.org/Wiki/CMake_Cross_Compiling
-if (NOT (CMAKE_SYSTEM_VERSION AND CMAKE_SYSTEM_PROCESSOR))
-    set(CRYPTOPP_CROSS_COMPILE 1)
-else()
-    set(CRYPTOPP_CROSS_COMPILE 0)
-endif()
-
-# Don't use RPATH's. The resulting binary could fail a security audit.
-if (NOT CMAKE_VERSION VERSION_LESS 2.8.12)
-  set(CMAKE_MACOSX_RPATH 0)
-endif()
-
-if(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
-    add_definitions(-wd68 -wd186 -wd279 -wd327 -wd161 -wd3180)
-endif()
-
-if(MSVC)
-  # Disable C4390: empty controlled statement found: is this the intent?
-  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4390")
-endif()
-
-# Endianness
-TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
-if(IS_BIG_ENDIAN)
-    add_definitions(-DIS_BIG_ENDIAN)
-endif()
-
-if(CRYPTOPP_DISABLE_ASM)
-    add_definitions(-DCRYPTOPP_DISABLE_ASM)
-endif()
-if(CRYPTOPP_DISABLE_SSSE3)
-    add_definitions(-DCRYPTOPP_DISABLE_SSSE3)
-endif()
-if(CRYPTOPP_DISABLE_AESNI)
-    add_definitions(-DCRYPTOPP_DISABLE_AESNI)
-endif()
-
-# We need the output 'uname -s' for Unix and Linux system detection
-if (NOT CRYPTOPP_CROSS_COMPILE)
-    set (UNAME_CMD "uname")
-    set (UNAME_ARG "-s")
-    execute_process(COMMAND ${UNAME_CMD} ${UNAME_ARG}
-        WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
-        RESULT_VARIABLE UNAME_RESULT
-        OUTPUT_VARIABLE UNAME_SYSTEM)
-        string(REGEX REPLACE "\n$" "" UNAME_SYSTEM "${UNAME_SYSTEM}")
-endif()
-
-# We need the output 'uname -m' for Unix and Linux platform detection
-if (NOT CRYPTOPP_CROSS_COMPILE)
-    set (UNAME_CMD "uname")
-    set (UNAME_ARG "-m")
-    execute_process(COMMAND ${UNAME_CMD} ${UNAME_ARG}
-        WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
-        RESULT_VARIABLE UNAME_RESULT
-        OUTPUT_VARIABLE UNAME_MACHINE)
-    string(REGEX REPLACE "\n$" "" UNAME_MACHINE "${UNAME_MACHINE}")
-endif()
-
-if(WINDOWS_STORE OR WINDOWS_PHONE)
-    if("${CMAKE_SYSTEM_VERSION}" MATCHES "10\\.0.*")
-        SET( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} /D\"_WIN32_WINNT=0x0A00\"" )
-    endif()
-    SET( CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} /FI\"winapifamily.h\"" )
-endif()
-
-# Enable PIC for all targets except Windows and 32-bit x86.
-#   Avoid on 32-bit x86 due to register pressures.
-if ((NOT CRYPTOPP_CROSS_COMPILE) AND (NOT (WINDOWS OR WINDOWS_STORE OR WINDOWS_PHONE)))
-    # Use Regex; match i386, i486, i586 and i686
-    if (NOT (${UNAME_MACHINE} MATCHES "i.86"))
-        SET(CMAKE_POSITION_INDEPENDENT_CODE 1)
-    endif()
-endif()
-
-# Link is driven through the compiler, but CXXFLAGS are not used. Also see
-#   http://public.kitware.com/pipermail/cmake/2003-June/003967.html
-if (NOT (WINDOWS OR WINDOWS_STORE OR WINDOWS_PHONE))
-    SET(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_FLAGS}")
-endif()
-
-#============================================================================
-# Sources & headers
-#============================================================================
-
-# Library headers
-file(GLOB cryptopp_HEADERS cryptopp/*.h)
-
-# Library sources. You can use the GNUmakefile to generate the list: `make sources`.
-file(GLOB cryptopp_SOURCES cryptopp/*.cpp)
-list(REMOVE_ITEM cryptopp_SOURCES
-        # These are removed in the original CMakeLists.txt
-        ${CMAKE_CURRENT_SOURCE_DIR}/cryptopp/pch.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/cryptopp/simple.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/cryptopp/winpipes.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/cryptopp/cryptlib_bds.cpp
-        ${cryptopp_SOURCES_TEST}
-        )
-
-if(MINGW OR WIN32)
-    list(APPEND cryptopp_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/cryptopp/winpipes.cpp)
-endif()
-
-if(MSVC AND NOT CRYPTOPP_DISABLE_ASM)
-    if(${CMAKE_GENERATOR} MATCHES ".*ARM")
-        message(STATUS "Disabling ASM because ARM is specified as target platform.")
-    else()
-        # Note that we removed rdrand.asm. This is a workaround for the issue that rdrand.asm cannnot compiled properly
-        # on MSVC. Because there is also a rdrand.S file in the submodule, CMake will specify the target path for
-        # rdrand.asm as "/crytopp.dir/{Debug|Release}/cryptopp/rdrand.asm.obj". The additional target folder "cryptopp"
-        # is specified because the file rdrand.asm is in the source folder "cryptopp". But MSVC assembler can't build
-        # target file to an non-existing folder("cryptopp").
-        list(APPEND cryptopp_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/cryptopp/x64dll.asm)
-        list(APPEND cryptopp_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/cryptopp/x64masm.asm)
-        # list(APPEND cryptopp_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/cryptopp/rdrand.asm)
-        set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/cryptopp/x64dll.asm PROPERTIES COMPILE_FLAGS "/D_M_X64")
-        set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/cryptopp/x64masm.asm PROPERTIES COMPILE_FLAGS "/D_M_X64")
-        # set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/cryptopp/rdrand.asm PROPERTIES COMPILE_FLAGS "/D_M_X64")
-        enable_language(ASM_MASM)
-    endif()
-endif()
-
-#============================================================================
-# Compile targets
-#============================================================================
-add_library(cryptopp STATIC ${cryptopp_SOURCES})
-target_include_directories(cryptopp INTERFACE .)
-
-#============================================================================
-# Third-party libraries
-#============================================================================
-
-if(WIN32)
-    target_link_libraries(cryptopp PRIVATE ws2_32)
-endif()
-
-find_package(Threads)
-target_link_libraries(cryptopp PRIVATE ${CMAKE_THREAD_LIBS_INIT})
diff --git a/externals/cryptopp/cryptopp b/externals/cryptopp/cryptopp
deleted file mode 160000
index 24bc2b8567..0000000000
--- a/externals/cryptopp/cryptopp
+++ /dev/null
@@ -1 +0,0 @@
-Subproject commit 24bc2b85674254fb294e717eb5b47d9f53e786b8
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index e8e98a095a..90772d0dbc 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -172,7 +172,7 @@ set(HEADERS
 create_directory_groups(${SRCS} ${HEADERS})
 add_library(core STATIC ${SRCS} ${HEADERS})
 target_link_libraries(core PUBLIC common PRIVATE audio_core dynarmic network video_core)
-target_link_libraries(core PUBLIC Boost::boost PRIVATE cryptopp fmt lz4_static unicorn)
+target_link_libraries(core PUBLIC Boost::boost PRIVATE fmt lz4_static unicorn)
 if (ENABLE_WEB_SERVICE)
     target_link_libraries(core PUBLIC json-headers web_service)
 endif()
diff --git a/src/core/file_sys/title_metadata.cpp b/src/core/file_sys/title_metadata.cpp
index 1ef8840a0a..e29ba60645 100644
--- a/src/core/file_sys/title_metadata.cpp
+++ b/src/core/file_sys/title_metadata.cpp
@@ -3,7 +3,6 @@
 // Refer to the license.txt file included.
 
 #include <cinttypes>
-#include <cryptopp/sha.h>
 #include "common/alignment.h"
 #include "common/file_util.h"
 #include "common/logging/log.h"
@@ -69,55 +68,7 @@ Loader::ResultStatus TitleMetadata::Load() {
 }
 
 Loader::ResultStatus TitleMetadata::Save() {
-    FileUtil::IOFile file(filepath, "wb");
-    if (!file.IsOpen())
-        return Loader::ResultStatus::Error;
-
-    if (!file.WriteBytes(&signature_type, sizeof(u32_be)))
-        return Loader::ResultStatus::Error;
-
-    // Signature lengths are variable, and the body follows the signature
-    u32 signature_size = GetSignatureSize(signature_type);
-
-    if (!file.WriteBytes(tmd_signature.data(), signature_size))
-        return Loader::ResultStatus::Error;
-
-    // The TMD body start position is rounded to the nearest 0x40 after the signature
-    size_t body_start = Common::AlignUp(signature_size + sizeof(u32), 0x40);
-    file.Seek(body_start, SEEK_SET);
-
-    // Update our TMD body values and hashes
-    tmd_body.content_count = static_cast<u16>(tmd_chunks.size());
-
-    // TODO(shinyquagsire23): Do TMDs with more than one contentinfo exist?
-    // For now we'll just adjust the first index to hold all content chunks
-    // and ensure that no further content info data exists.
-    tmd_body.contentinfo = {};
-    tmd_body.contentinfo[0].index = 0;
-    tmd_body.contentinfo[0].command_count = static_cast<u16>(tmd_chunks.size());
-
-    CryptoPP::SHA256 chunk_hash;
-    for (u16 i = 0; i < tmd_body.content_count; i++) {
-        chunk_hash.Update(reinterpret_cast<u8*>(&tmd_chunks[i]), sizeof(ContentChunk));
-    }
-    chunk_hash.Final(tmd_body.contentinfo[0].hash.data());
-
-    CryptoPP::SHA256 contentinfo_hash;
-    for (size_t i = 0; i < tmd_body.contentinfo.size(); i++) {
-        chunk_hash.Update(reinterpret_cast<u8*>(&tmd_body.contentinfo[i]), sizeof(ContentInfo));
-    }
-    chunk_hash.Final(tmd_body.contentinfo_hash.data());
-
-    // Write our TMD body, then write each of our ContentChunks
-    if (file.WriteBytes(&tmd_body, sizeof(TitleMetadata::Body)) != sizeof(TitleMetadata::Body))
-        return Loader::ResultStatus::Error;
-
-    for (u16 i = 0; i < tmd_body.content_count; i++) {
-        ContentChunk chunk = tmd_chunks[i];
-        if (file.WriteBytes(&chunk, sizeof(ContentChunk)) != sizeof(ContentChunk))
-            return Loader::ResultStatus::Error;
-    }
-
+    UNIMPLEMENTED();
     return Loader::ResultStatus::Success;
 }
 
diff --git a/src/core/hw/aes/ccm.cpp b/src/core/hw/aes/ccm.cpp
index dc7035ab62..1ee37aaa4d 100644
--- a/src/core/hw/aes/ccm.cpp
+++ b/src/core/hw/aes/ccm.cpp
@@ -3,11 +3,8 @@
 // Refer to the license.txt file included.
 
 #include <algorithm>
-#include <cryptopp/aes.h>
-#include <cryptopp/ccm.h>
-#include <cryptopp/cryptlib.h>
-#include <cryptopp/filters.h>
 #include "common/alignment.h"
+#include "common/assert.h"
 #include "common/logging/log.h"
 #include "core/hw/aes/ccm.h"
 #include "core/hw/aes/key.h"
@@ -15,80 +12,16 @@
 namespace HW {
 namespace AES {
 
-namespace {
-
-// 3DS uses a non-standard AES-CCM algorithm, so we need to derive a sub class from the standard one
-// and override with the non-standard part.
-using CryptoPP::lword;
-using CryptoPP::AES;
-using CryptoPP::CCM_Final;
-using CryptoPP::CCM_Base;
-template <bool T_IsEncryption>
-class CCM_3DSVariant_Final : public CCM_Final<AES, CCM_MAC_SIZE, T_IsEncryption> {
-public:
-    void UncheckedSpecifyDataLengths(lword header_length, lword message_length,
-                                     lword footer_length) override {
-        // 3DS uses the aligned size to generate B0 for authentication, instead of the original size
-        lword aligned_message_length = Common::AlignUp(message_length, AES_BLOCK_SIZE);
-        CCM_Base::UncheckedSpecifyDataLengths(header_length, aligned_message_length, footer_length);
-        CCM_Base::m_messageLength = message_length; // restore the actual message size
-    }
-};
-
-class CCM_3DSVariant {
-public:
-    using Encryption = CCM_3DSVariant_Final<true>;
-    using Decryption = CCM_3DSVariant_Final<false>;
-};
-
-} // namespace
-
 std::vector<u8> EncryptSignCCM(const std::vector<u8>& pdata, const CCMNonce& nonce,
                                size_t slot_id) {
-    if (!IsNormalKeyAvailable(slot_id)) {
-        LOG_ERROR(HW_AES, "Key slot %d not available. Will use zero key.", slot_id);
-    }
-    const AESKey normal = GetNormalKey(slot_id);
-    std::vector<u8> cipher(pdata.size() + CCM_MAC_SIZE);
-
-    try {
-        CCM_3DSVariant::Encryption e;
-        e.SetKeyWithIV(normal.data(), AES_BLOCK_SIZE, nonce.data(), CCM_NONCE_SIZE);
-        e.SpecifyDataLengths(0, pdata.size(), 0);
-        CryptoPP::ArraySource as(pdata.data(), pdata.size(), true,
-                                 new CryptoPP::AuthenticatedEncryptionFilter(
-                                     e, new CryptoPP::ArraySink(cipher.data(), cipher.size())));
-    } catch (const CryptoPP::Exception& e) {
-        LOG_ERROR(HW_AES, "FAILED with: %s", e.what());
-    }
-    return cipher;
+    UNIMPLEMENTED();
+    return {};
 }
 
 std::vector<u8> DecryptVerifyCCM(const std::vector<u8>& cipher, const CCMNonce& nonce,
                                  size_t slot_id) {
-    if (!IsNormalKeyAvailable(slot_id)) {
-        LOG_ERROR(HW_AES, "Key slot %d not available. Will use zero key.", slot_id);
-    }
-    const AESKey normal = GetNormalKey(slot_id);
-    const std::size_t pdata_size = cipher.size() - CCM_MAC_SIZE;
-    std::vector<u8> pdata(pdata_size);
-
-    try {
-        CCM_3DSVariant::Decryption d;
-        d.SetKeyWithIV(normal.data(), AES_BLOCK_SIZE, nonce.data(), CCM_NONCE_SIZE);
-        d.SpecifyDataLengths(0, pdata_size, 0);
-        CryptoPP::AuthenticatedDecryptionFilter df(
-            d, new CryptoPP::ArraySink(pdata.data(), pdata_size));
-        CryptoPP::ArraySource as(cipher.data(), cipher.size(), true, new CryptoPP::Redirector(df));
-        if (!df.GetLastResult()) {
-            LOG_ERROR(HW_AES, "FAILED");
-            return {};
-        }
-    } catch (const CryptoPP::Exception& e) {
-        LOG_ERROR(HW_AES, "FAILED with: %s", e.what());
-        return {};
-    }
-    return pdata;
+    UNIMPLEMENTED();
+    return {};
 }
 
 } // namespace AES
diff --git a/src/core/telemetry_session.cpp b/src/core/telemetry_session.cpp
index f9725b5900..560c8af550 100644
--- a/src/core/telemetry_session.cpp
+++ b/src/core/telemetry_session.cpp
@@ -3,7 +3,6 @@
 // Refer to the license.txt file included.
 
 #include <cstring>
-#include <cryptopp/osrng.h>
 
 #include "common/assert.h"
 #include "common/file_util.h"
@@ -34,8 +33,6 @@ static const char* CpuVendorToStr(Common::CPUVendor vendor) {
 
 static u64 GenerateTelemetryId() {
     u64 telemetry_id{};
-    CryptoPP::AutoSeededRandomPool rng;
-    rng.GenerateBlock(reinterpret_cast<CryptoPP::byte*>(&telemetry_id), sizeof(u64));
     return telemetry_id;
 }