diff options
| author | 2017-02-25 00:12:33 -0500 | |
|---|---|---|
| committer | 2017-02-25 00:12:33 -0500 | |
| commit | 892888ed9e7c683150493c7c17f567d3c569e62e (patch) | |
| tree | fffbfb393fc1da4b6c69a9c0593d2fe8ef9a10a7 | |
| parent | Merge pull request #2421 from Subv/timers (diff) | |
| parent | externals: remove -march=native for crypto++ (diff) | |
| download | yuzu-892888ed9e7c683150493c7c17f567d3c569e62e.tar.gz yuzu-892888ed9e7c683150493c7c17f567d3c569e62e.tar.xz yuzu-892888ed9e7c683150493c7c17f567d3c569e62e.zip | |
Merge pull request #2569 from wwylele/wrap-unwrap
APT: implemented Wrap and Unwrap
| -rw-r--r-- | externals/cryptopp/CMakeLists.txt | 9 | ||||
| -rw-r--r-- | src/common/common_paths.h | 1 | ||||
| -rw-r--r-- | src/common/logging/backend.cpp | 1 | ||||
| -rw-r--r-- | src/common/logging/log.h | 1 | ||||
| -rw-r--r-- | src/core/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | src/core/hle/service/apt/apt.cpp | 103 | ||||
| -rw-r--r-- | src/core/hle/service/apt/apt.h | 40 | ||||
| -rw-r--r-- | src/core/hle/service/apt/apt_a.cpp | 4 | ||||
| -rw-r--r-- | src/core/hle/service/apt/apt_s.cpp | 4 | ||||
| -rw-r--r-- | src/core/hle/service/apt/apt_u.cpp | 4 | ||||
| -rw-r--r-- | src/core/hw/aes/arithmetic128.cpp | 47 | ||||
| -rw-r--r-- | src/core/hw/aes/arithmetic128.h | 17 | ||||
| -rw-r--r-- | src/core/hw/aes/ccm.cpp | 95 | ||||
| -rw-r--r-- | src/core/hw/aes/ccm.h | 40 | ||||
| -rw-r--r-- | src/core/hw/aes/key.cpp | 173 | ||||
| -rw-r--r-- | src/core/hw/aes/key.h | 35 | ||||
| -rw-r--r-- | src/core/hw/hw.cpp | 2 |
17 files changed, 568 insertions, 14 deletions
diff --git a/externals/cryptopp/CMakeLists.txt b/externals/cryptopp/CMakeLists.txt index bbac71bb9..653af1e4b 100644 --- a/externals/cryptopp/CMakeLists.txt +++ b/externals/cryptopp/CMakeLists.txt | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | # The differences are: | 3 | # The differences are: |
| 4 | # - removed support for legacy CMake versions | 4 | # - removed support for legacy CMake versions |
| 5 | # - removed support for 32-bit | 5 | # - removed support for 32-bit |
| 6 | # - removed -march=native flag | ||
| 6 | # - removed rdrand module.asm as a workaround for an issue (see below) | 7 | # - removed rdrand module.asm as a workaround for an issue (see below) |
| 7 | # - added prefix "CRYPTOPP_" to all option names | 8 | # - added prefix "CRYPTOPP_" to all option names |
| 8 | # - disabled testing | 9 | # - disabled testing |
| @@ -96,14 +97,6 @@ if ((NOT CRYPTOPP_CROSS_COMPILE) AND (NOT (WINDOWS OR WINDOWS_STORE OR WINDOWS_P | |||
| 96 | endif() | 97 | endif() |
| 97 | endif() | 98 | endif() |
| 98 | 99 | ||
| 99 | # -march=native for GCC, Clang and ICC in any version that does support it. | ||
| 100 | if ((NOT CRYPTOPP_DISABLE_CXXFLAGS_OPTIMIZATIONS) AND (NOT CRYPTOPP_CROSS_COMPILE) AND (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU|Intel")) | ||
| 101 | CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_OPT_ARCH_NATIVE_SUPPORTED) | ||
| 102 | if (COMPILER_OPT_ARCH_NATIVE_SUPPORTED AND NOT CMAKE_CXX_FLAGS MATCHES "-march=") | ||
| 103 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") | ||
| 104 | endif() | ||
| 105 | endif() | ||
| 106 | |||
| 107 | # Link is driven through the compiler, but CXXFLAGS are not used. Also see | 100 | # Link is driven through the compiler, but CXXFLAGS are not used. Also see |
| 108 | # http://public.kitware.com/pipermail/cmake/2003-June/003967.html | 101 | # http://public.kitware.com/pipermail/cmake/2003-June/003967.html |
| 109 | if (NOT (WINDOWS OR WINDOWS_STORE OR WINDOWS_PHONE)) | 102 | if (NOT (WINDOWS OR WINDOWS_STORE OR WINDOWS_PHONE)) |
diff --git a/src/common/common_paths.h b/src/common/common_paths.h index b56105306..d5b510cdb 100644 --- a/src/common/common_paths.h +++ b/src/common/common_paths.h | |||
| @@ -45,3 +45,4 @@ | |||
| 45 | 45 | ||
| 46 | // Sys files | 46 | // Sys files |
| 47 | #define SHARED_FONT "shared_font.bin" | 47 | #define SHARED_FONT "shared_font.bin" |
| 48 | #define AES_KEYS "aes_keys.txt" | ||
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 0f0354821..737e1d57f 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp | |||
| @@ -63,6 +63,7 @@ namespace Log { | |||
| 63 | SUB(HW, Memory) \ | 63 | SUB(HW, Memory) \ |
| 64 | SUB(HW, LCD) \ | 64 | SUB(HW, LCD) \ |
| 65 | SUB(HW, GPU) \ | 65 | SUB(HW, GPU) \ |
| 66 | SUB(HW, AES) \ | ||
| 66 | CLS(Frontend) \ | 67 | CLS(Frontend) \ |
| 67 | CLS(Render) \ | 68 | CLS(Render) \ |
| 68 | SUB(Render, Software) \ | 69 | SUB(Render, Software) \ |
diff --git a/src/common/logging/log.h b/src/common/logging/log.h index f0ec922d2..4b0f8ff03 100644 --- a/src/common/logging/log.h +++ b/src/common/logging/log.h | |||
| @@ -80,6 +80,7 @@ enum class Class : ClassType { | |||
| 80 | HW_Memory, ///< Memory-map and address translation | 80 | HW_Memory, ///< Memory-map and address translation |
| 81 | HW_LCD, ///< LCD register emulation | 81 | HW_LCD, ///< LCD register emulation |
| 82 | HW_GPU, ///< GPU control emulation | 82 | HW_GPU, ///< GPU control emulation |
| 83 | HW_AES, ///< AES engine emulation | ||
| 83 | Frontend, ///< Emulator UI | 84 | Frontend, ///< Emulator UI |
| 84 | Render, ///< Emulator video output and hardware acceleration | 85 | Render, ///< Emulator video output and hardware acceleration |
| 85 | Render_Software, ///< Software renderer backend | 86 | Render_Software, ///< Software renderer backend |
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 5332e35a3..8334fece9 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -159,6 +159,9 @@ set(SRCS | |||
| 159 | hle/service/y2r_u.cpp | 159 | hle/service/y2r_u.cpp |
| 160 | hle/shared_page.cpp | 160 | hle/shared_page.cpp |
| 161 | hle/svc.cpp | 161 | hle/svc.cpp |
| 162 | hw/aes/arithmetic128.cpp | ||
| 163 | hw/aes/ccm.cpp | ||
| 164 | hw/aes/key.cpp | ||
| 162 | hw/gpu.cpp | 165 | hw/gpu.cpp |
| 163 | hw/hw.cpp | 166 | hw/hw.cpp |
| 164 | hw/lcd.cpp | 167 | hw/lcd.cpp |
| @@ -343,6 +346,9 @@ set(HEADERS | |||
| 343 | hle/service/y2r_u.h | 346 | hle/service/y2r_u.h |
| 344 | hle/shared_page.h | 347 | hle/shared_page.h |
| 345 | hle/svc.h | 348 | hle/svc.h |
| 349 | hw/aes/arithmetic128.h | ||
| 350 | hw/aes/ccm.h | ||
| 351 | hw/aes/key.h | ||
| 346 | hw/gpu.h | 352 | hw/gpu.h |
| 347 | hw/hw.h | 353 | hw/hw.h |
| 348 | hw/lcd.h | 354 | hw/lcd.h |
diff --git a/src/core/hle/service/apt/apt.cpp b/src/core/hle/service/apt/apt.cpp index 615fe31ea..e57b19c2d 100644 --- a/src/core/hle/service/apt/apt.cpp +++ b/src/core/hle/service/apt/apt.cpp | |||
| @@ -18,6 +18,8 @@ | |||
| 18 | #include "core/hle/service/fs/archive.h" | 18 | #include "core/hle/service/fs/archive.h" |
| 19 | #include "core/hle/service/ptm/ptm.h" | 19 | #include "core/hle/service/ptm/ptm.h" |
| 20 | #include "core/hle/service/service.h" | 20 | #include "core/hle/service/service.h" |
| 21 | #include "core/hw/aes/ccm.h" | ||
| 22 | #include "core/hw/aes/key.h" | ||
| 21 | 23 | ||
| 22 | namespace Service { | 24 | namespace Service { |
| 23 | namespace APT { | 25 | namespace APT { |
| @@ -470,6 +472,107 @@ void GetStartupArgument(Service::Interface* self) { | |||
| 470 | cmd_buff[2] = 0; | 472 | cmd_buff[2] = 0; |
| 471 | } | 473 | } |
| 472 | 474 | ||
| 475 | void Wrap(Service::Interface* self) { | ||
| 476 | IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x46, 4, 4); | ||
| 477 | const u32 output_size = rp.Pop<u32>(); | ||
| 478 | const u32 input_size = rp.Pop<u32>(); | ||
| 479 | const u32 nonce_offset = rp.Pop<u32>(); | ||
| 480 | u32 nonce_size = rp.Pop<u32>(); | ||
| 481 | size_t desc_size; | ||
| 482 | IPC::MappedBufferPermissions desc_permission; | ||
| 483 | const VAddr input = rp.PopMappedBuffer(&desc_size, &desc_permission); | ||
| 484 | ASSERT(desc_size == input_size && desc_permission == IPC::MappedBufferPermissions::R); | ||
| 485 | const VAddr output = rp.PopMappedBuffer(&desc_size, &desc_permission); | ||
| 486 | ASSERT(desc_size == output_size && desc_permission == IPC::MappedBufferPermissions::W); | ||
| 487 | |||
| 488 | // Note: real 3DS still returns SUCCESS when the sizes don't match. It seems that it doesn't | ||
| 489 | // check the buffer size and writes data with potential overflow. | ||
| 490 | ASSERT_MSG(output_size == input_size + HW::AES::CCM_MAC_SIZE, | ||
| 491 | "input_size (%d) doesn't match to output_size (%d)", input_size, output_size); | ||
| 492 | |||
| 493 | LOG_DEBUG(Service_APT, "called, output_size=%u, input_size=%u, nonce_offset=%u, nonce_size=%u", | ||
| 494 | output_size, input_size, nonce_offset, nonce_size); | ||
| 495 | |||
| 496 | // Note: This weird nonce size modification is verified against real 3DS | ||
| 497 | nonce_size = std::min<u32>(nonce_size & ~3, HW::AES::CCM_NONCE_SIZE); | ||
| 498 | |||
| 499 | // Reads nonce and concatenates the rest of the input as plaintext | ||
| 500 | HW::AES::CCMNonce nonce{}; | ||
| 501 | Memory::ReadBlock(input + nonce_offset, nonce.data(), nonce_size); | ||
| 502 | u32 pdata_size = input_size - nonce_size; | ||
| 503 | std::vector<u8> pdata(pdata_size); | ||
| 504 | Memory::ReadBlock(input, pdata.data(), nonce_offset); | ||
| 505 | Memory::ReadBlock(input + nonce_offset + nonce_size, pdata.data() + nonce_offset, | ||
| 506 | pdata_size - nonce_offset); | ||
| 507 | |||
| 508 | // Encrypts the plaintext using AES-CCM | ||
| 509 | auto cipher = HW::AES::EncryptSignCCM(pdata, nonce, HW::AES::KeySlotID::APTWrap); | ||
| 510 | |||
| 511 | // Puts the nonce to the beginning of the output, with ciphertext followed | ||
| 512 | Memory::WriteBlock(output, nonce.data(), nonce_size); | ||
| 513 | Memory::WriteBlock(output + nonce_size, cipher.data(), cipher.size()); | ||
| 514 | |||
| 515 | IPC::RequestBuilder rb = rp.MakeBuilder(1, 4); | ||
| 516 | rb.Push(RESULT_SUCCESS); | ||
| 517 | |||
| 518 | // Unmap buffer | ||
| 519 | rb.PushMappedBuffer(input, input_size, IPC::MappedBufferPermissions::R); | ||
| 520 | rb.PushMappedBuffer(output, output_size, IPC::MappedBufferPermissions::W); | ||
| 521 | } | ||
| 522 | |||
| 523 | void Unwrap(Service::Interface* self) { | ||
| 524 | IPC::RequestParser rp(Kernel::GetCommandBuffer(), 0x47, 4, 4); | ||
| 525 | const u32 output_size = rp.Pop<u32>(); | ||
| 526 | const u32 input_size = rp.Pop<u32>(); | ||
| 527 | const u32 nonce_offset = rp.Pop<u32>(); | ||
| 528 | u32 nonce_size = rp.Pop<u32>(); | ||
| 529 | size_t desc_size; | ||
| 530 | IPC::MappedBufferPermissions desc_permission; | ||
| 531 | const VAddr input = rp.PopMappedBuffer(&desc_size, &desc_permission); | ||
| 532 | ASSERT(desc_size == input_size && desc_permission == IPC::MappedBufferPermissions::R); | ||
| 533 | const VAddr output = rp.PopMappedBuffer(&desc_size, &desc_permission); | ||
| 534 | ASSERT(desc_size == output_size && desc_permission == IPC::MappedBufferPermissions::W); | ||
| 535 | |||
| 536 | // Note: real 3DS still returns SUCCESS when the sizes don't match. It seems that it doesn't | ||
| 537 | // check the buffer size and writes data with potential overflow. | ||
| 538 | ASSERT_MSG(output_size == input_size - HW::AES::CCM_MAC_SIZE, | ||
| 539 | "input_size (%d) doesn't match to output_size (%d)", input_size, output_size); | ||
| 540 | |||
| 541 | LOG_DEBUG(Service_APT, "called, output_size=%u, input_size=%u, nonce_offset=%u, nonce_size=%u", | ||
| 542 | output_size, input_size, nonce_offset, nonce_size); | ||
| 543 | |||
| 544 | // Note: This weird nonce size modification is verified against real 3DS | ||
| 545 | nonce_size = std::min<u32>(nonce_size & ~3, HW::AES::CCM_NONCE_SIZE); | ||
| 546 | |||
| 547 | // Reads nonce and cipher text | ||
| 548 | HW::AES::CCMNonce nonce{}; | ||
| 549 | Memory::ReadBlock(input, nonce.data(), nonce_size); | ||
| 550 | u32 cipher_size = input_size - nonce_size; | ||
| 551 | std::vector<u8> cipher(cipher_size); | ||
| 552 | Memory::ReadBlock(input + nonce_size, cipher.data(), cipher_size); | ||
| 553 | |||
| 554 | // Decrypts the ciphertext using AES-CCM | ||
| 555 | auto pdata = HW::AES::DecryptVerifyCCM(cipher, nonce, HW::AES::KeySlotID::APTWrap); | ||
| 556 | |||
| 557 | IPC::RequestBuilder rb = rp.MakeBuilder(1, 0); | ||
| 558 | if (!pdata.empty()) { | ||
| 559 | // Splits the plaintext and put the nonce in between | ||
| 560 | Memory::WriteBlock(output, pdata.data(), nonce_offset); | ||
| 561 | Memory::WriteBlock(output + nonce_offset, nonce.data(), nonce_size); | ||
| 562 | Memory::WriteBlock(output + nonce_offset + nonce_size, pdata.data() + nonce_offset, | ||
| 563 | pdata.size() - nonce_offset); | ||
| 564 | rb.Push(RESULT_SUCCESS); | ||
| 565 | } else { | ||
| 566 | LOG_ERROR(Service_APT, "Failed to decrypt data"); | ||
| 567 | rb.Push(ResultCode(static_cast<ErrorDescription>(1), ErrorModule::PS, | ||
| 568 | ErrorSummary::WrongArgument, ErrorLevel::Status)); | ||
| 569 | } | ||
| 570 | |||
| 571 | // Unmap buffer | ||
| 572 | rb.PushMappedBuffer(input, input_size, IPC::MappedBufferPermissions::R); | ||
| 573 | rb.PushMappedBuffer(output, output_size, IPC::MappedBufferPermissions::W); | ||
| 574 | } | ||
| 575 | |||
| 473 | void CheckNew3DSApp(Service::Interface* self) { | 576 | void CheckNew3DSApp(Service::Interface* self) { |
| 474 | u32* cmd_buff = Kernel::GetCommandBuffer(); | 577 | u32* cmd_buff = Kernel::GetCommandBuffer(); |
| 475 | 578 | ||
diff --git a/src/core/hle/service/apt/apt.h b/src/core/hle/service/apt/apt.h index 80325361f..e63b61450 100644 --- a/src/core/hle/service/apt/apt.h +++ b/src/core/hle/service/apt/apt.h | |||
| @@ -137,6 +137,46 @@ void Initialize(Service::Interface* self); | |||
| 137 | void GetSharedFont(Service::Interface* self); | 137 | void GetSharedFont(Service::Interface* self); |
| 138 | 138 | ||
| 139 | /** | 139 | /** |
| 140 | * APT::Wrap service function | ||
| 141 | * Inputs: | ||
| 142 | * 1 : Output buffer size | ||
| 143 | * 2 : Input buffer size | ||
| 144 | * 3 : Nonce offset to the input buffer | ||
| 145 | * 4 : Nonce size | ||
| 146 | * 5 : Buffer mapping descriptor ((input_buffer_size << 4) | 0xA) | ||
| 147 | * 6 : Input buffer address | ||
| 148 | * 7 : Buffer mapping descriptor ((input_buffer_size << 4) | 0xC) | ||
| 149 | * 8 : Output buffer address | ||
| 150 | * Outputs: | ||
| 151 | * 1 : Result of function, 0 on success, otherwise error code | ||
| 152 | * 2 : Buffer unmapping descriptor ((input_buffer_size << 4) | 0xA) | ||
| 153 | * 3 : Input buffer address | ||
| 154 | * 4 : Buffer unmapping descriptor ((input_buffer_size << 4) | 0xC) | ||
| 155 | * 5 : Output buffer address | ||
| 156 | */ | ||
| 157 | void Wrap(Service::Interface* self); | ||
| 158 | |||
| 159 | /** | ||
| 160 | * APT::Unwrap service function | ||
| 161 | * Inputs: | ||
| 162 | * 1 : Output buffer size | ||
| 163 | * 2 : Input buffer size | ||
| 164 | * 3 : Nonce offset to the output buffer | ||
| 165 | * 4 : Nonce size | ||
| 166 | * 5 : Buffer mapping descriptor ((input_buffer_size << 4) | 0xA) | ||
| 167 | * 6 : Input buffer address | ||
| 168 | * 7 : Buffer mapping descriptor ((input_buffer_size << 4) | 0xC) | ||
| 169 | * 8 : Output buffer address | ||
| 170 | * Outputs: | ||
| 171 | * 1 : Result of function, 0 on success, otherwise error code | ||
| 172 | * 2 : Buffer unmapping descriptor ((input_buffer_size << 4) | 0xA) | ||
| 173 | * 3 : Input buffer address | ||
| 174 | * 4 : Buffer unmapping descriptor ((input_buffer_size << 4) | 0xC) | ||
| 175 | * 5 : Output buffer address | ||
| 176 | */ | ||
| 177 | void Unwrap(Service::Interface* self); | ||
| 178 | |||
| 179 | /** | ||
| 140 | * APT::NotifyToWait service function | 180 | * APT::NotifyToWait service function |
| 141 | * Inputs: | 181 | * Inputs: |
| 142 | * 1 : AppID | 182 | * 1 : AppID |
diff --git a/src/core/hle/service/apt/apt_a.cpp b/src/core/hle/service/apt/apt_a.cpp index 62dc2d61d..c496cba8d 100644 --- a/src/core/hle/service/apt/apt_a.cpp +++ b/src/core/hle/service/apt/apt_a.cpp | |||
| @@ -78,8 +78,8 @@ const Interface::FunctionInfo FunctionTable[] = { | |||
| 78 | {0x00430040, NotifyToWait, "NotifyToWait"}, | 78 | {0x00430040, NotifyToWait, "NotifyToWait"}, |
| 79 | {0x00440000, GetSharedFont, "GetSharedFont"}, | 79 | {0x00440000, GetSharedFont, "GetSharedFont"}, |
| 80 | {0x00450040, nullptr, "GetWirelessRebootInfo"}, | 80 | {0x00450040, nullptr, "GetWirelessRebootInfo"}, |
| 81 | {0x00460104, nullptr, "Wrap"}, | 81 | {0x00460104, Wrap, "Wrap"}, |
| 82 | {0x00470104, nullptr, "Unwrap"}, | 82 | {0x00470104, Unwrap, "Unwrap"}, |
| 83 | {0x00480100, nullptr, "GetProgramInfo"}, | 83 | {0x00480100, nullptr, "GetProgramInfo"}, |
| 84 | {0x00490180, nullptr, "Reboot"}, | 84 | {0x00490180, nullptr, "Reboot"}, |
| 85 | {0x004A0040, nullptr, "GetCaptureInfo"}, | 85 | {0x004A0040, nullptr, "GetCaptureInfo"}, |
diff --git a/src/core/hle/service/apt/apt_s.cpp b/src/core/hle/service/apt/apt_s.cpp index effd23dce..ec5668d05 100644 --- a/src/core/hle/service/apt/apt_s.cpp +++ b/src/core/hle/service/apt/apt_s.cpp | |||
| @@ -78,8 +78,8 @@ const Interface::FunctionInfo FunctionTable[] = { | |||
| 78 | {0x00430040, NotifyToWait, "NotifyToWait"}, | 78 | {0x00430040, NotifyToWait, "NotifyToWait"}, |
| 79 | {0x00440000, GetSharedFont, "GetSharedFont"}, | 79 | {0x00440000, GetSharedFont, "GetSharedFont"}, |
| 80 | {0x00450040, nullptr, "GetWirelessRebootInfo"}, | 80 | {0x00450040, nullptr, "GetWirelessRebootInfo"}, |
| 81 | {0x00460104, nullptr, "Wrap"}, | 81 | {0x00460104, Wrap, "Wrap"}, |
| 82 | {0x00470104, nullptr, "Unwrap"}, | 82 | {0x00470104, Unwrap, "Unwrap"}, |
| 83 | {0x00480100, nullptr, "GetProgramInfo"}, | 83 | {0x00480100, nullptr, "GetProgramInfo"}, |
| 84 | {0x00490180, nullptr, "Reboot"}, | 84 | {0x00490180, nullptr, "Reboot"}, |
| 85 | {0x004A0040, nullptr, "GetCaptureInfo"}, | 85 | {0x004A0040, nullptr, "GetCaptureInfo"}, |
diff --git a/src/core/hle/service/apt/apt_u.cpp b/src/core/hle/service/apt/apt_u.cpp index e06084a1e..9dd002590 100644 --- a/src/core/hle/service/apt/apt_u.cpp +++ b/src/core/hle/service/apt/apt_u.cpp | |||
| @@ -78,8 +78,8 @@ const Interface::FunctionInfo FunctionTable[] = { | |||
| 78 | {0x00430040, NotifyToWait, "NotifyToWait"}, | 78 | {0x00430040, NotifyToWait, "NotifyToWait"}, |
| 79 | {0x00440000, GetSharedFont, "GetSharedFont"}, | 79 | {0x00440000, GetSharedFont, "GetSharedFont"}, |
| 80 | {0x00450040, nullptr, "GetWirelessRebootInfo"}, | 80 | {0x00450040, nullptr, "GetWirelessRebootInfo"}, |
| 81 | {0x00460104, nullptr, "Wrap"}, | 81 | {0x00460104, Wrap, "Wrap"}, |
| 82 | {0x00470104, nullptr, "Unwrap"}, | 82 | {0x00470104, Unwrap, "Unwrap"}, |
| 83 | {0x00480100, nullptr, "GetProgramInfo"}, | 83 | {0x00480100, nullptr, "GetProgramInfo"}, |
| 84 | {0x00490180, nullptr, "Reboot"}, | 84 | {0x00490180, nullptr, "Reboot"}, |
| 85 | {0x004A0040, nullptr, "GetCaptureInfo"}, | 85 | {0x004A0040, nullptr, "GetCaptureInfo"}, |
diff --git a/src/core/hw/aes/arithmetic128.cpp b/src/core/hw/aes/arithmetic128.cpp new file mode 100644 index 000000000..55b954a52 --- /dev/null +++ b/src/core/hw/aes/arithmetic128.cpp | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <functional> | ||
| 7 | #include "core/hw/aes/arithmetic128.h" | ||
| 8 | |||
| 9 | namespace HW { | ||
| 10 | namespace AES { | ||
| 11 | |||
| 12 | AESKey Lrot128(const AESKey& in, u32 rot) { | ||
| 13 | AESKey out; | ||
| 14 | rot %= 128; | ||
| 15 | const u32 byte_shift = rot / 8; | ||
| 16 | const u32 bit_shift = rot % 8; | ||
| 17 | |||
| 18 | for (u32 i = 0; i < 16; i++) { | ||
| 19 | const u32 wrap_index_a = (i + byte_shift) % 16; | ||
| 20 | const u32 wrap_index_b = (i + byte_shift + 1) % 16; | ||
| 21 | out[i] = ((in[wrap_index_a] << bit_shift) | (in[wrap_index_b] >> (8 - bit_shift))) & 0xFF; | ||
| 22 | } | ||
| 23 | return out; | ||
| 24 | } | ||
| 25 | |||
| 26 | AESKey Add128(const AESKey& a, const AESKey& b) { | ||
| 27 | AESKey out; | ||
| 28 | u32 carry = 0; | ||
| 29 | u32 sum = 0; | ||
| 30 | |||
| 31 | for (int i = 15; i >= 0; i--) { | ||
| 32 | sum = a[i] + b[i] + carry; | ||
| 33 | carry = sum >> 8; | ||
| 34 | out[i] = static_cast<u8>(sum & 0xff); | ||
| 35 | } | ||
| 36 | |||
| 37 | return out; | ||
| 38 | } | ||
| 39 | |||
| 40 | AESKey Xor128(const AESKey& a, const AESKey& b) { | ||
| 41 | AESKey out; | ||
| 42 | std::transform(a.cbegin(), a.cend(), b.cbegin(), out.begin(), std::bit_xor<>()); | ||
| 43 | return out; | ||
| 44 | } | ||
| 45 | |||
| 46 | } // namespace AES | ||
| 47 | } // namespace HW | ||
diff --git a/src/core/hw/aes/arithmetic128.h b/src/core/hw/aes/arithmetic128.h new file mode 100644 index 000000000..d670e2ce2 --- /dev/null +++ b/src/core/hw/aes/arithmetic128.h | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "common/common_types.h" | ||
| 8 | #include "core/hw/aes/key.h" | ||
| 9 | |||
| 10 | namespace HW { | ||
| 11 | namespace AES { | ||
| 12 | AESKey Lrot128(const AESKey& in, u32 rot); | ||
| 13 | AESKey Add128(const AESKey& a, const AESKey& b); | ||
| 14 | AESKey Xor128(const AESKey& a, const AESKey& b); | ||
| 15 | |||
| 16 | } // namspace AES | ||
| 17 | } // namespace HW | ||
diff --git a/src/core/hw/aes/ccm.cpp b/src/core/hw/aes/ccm.cpp new file mode 100644 index 000000000..dc7035ab6 --- /dev/null +++ b/src/core/hw/aes/ccm.cpp | |||
| @@ -0,0 +1,95 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <cryptopp/aes.h> | ||
| 7 | #include <cryptopp/ccm.h> | ||
| 8 | #include <cryptopp/cryptlib.h> | ||
| 9 | #include <cryptopp/filters.h> | ||
| 10 | #include "common/alignment.h" | ||
| 11 | #include "common/logging/log.h" | ||
| 12 | #include "core/hw/aes/ccm.h" | ||
| 13 | #include "core/hw/aes/key.h" | ||
| 14 | |||
| 15 | namespace HW { | ||
| 16 | namespace AES { | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | // 3DS uses a non-standard AES-CCM algorithm, so we need to derive a sub class from the standard one | ||
| 21 | // and override with the non-standard part. | ||
| 22 | using CryptoPP::lword; | ||
| 23 | using CryptoPP::AES; | ||
| 24 | using CryptoPP::CCM_Final; | ||
| 25 | using CryptoPP::CCM_Base; | ||
| 26 | template <bool T_IsEncryption> | ||
| 27 | class CCM_3DSVariant_Final : public CCM_Final<AES, CCM_MAC_SIZE, T_IsEncryption> { | ||
| 28 | public: | ||
| 29 | void UncheckedSpecifyDataLengths(lword header_length, lword message_length, | ||
| 30 | lword footer_length) override { | ||
| 31 | // 3DS uses the aligned size to generate B0 for authentication, instead of the original size | ||
| 32 | lword aligned_message_length = Common::AlignUp(message_length, AES_BLOCK_SIZE); | ||
| 33 | CCM_Base::UncheckedSpecifyDataLengths(header_length, aligned_message_length, footer_length); | ||
| 34 | CCM_Base::m_messageLength = message_length; // restore the actual message size | ||
| 35 | } | ||
| 36 | }; | ||
| 37 | |||
| 38 | class CCM_3DSVariant { | ||
| 39 | public: | ||
| 40 | using Encryption = CCM_3DSVariant_Final<true>; | ||
| 41 | using Decryption = CCM_3DSVariant_Final<false>; | ||
| 42 | }; | ||
| 43 | |||
| 44 | } // namespace | ||
| 45 | |||
| 46 | std::vector<u8> EncryptSignCCM(const std::vector<u8>& pdata, const CCMNonce& nonce, | ||
| 47 | size_t slot_id) { | ||
| 48 | if (!IsNormalKeyAvailable(slot_id)) { | ||
| 49 | LOG_ERROR(HW_AES, "Key slot %d not available. Will use zero key.", slot_id); | ||
| 50 | } | ||
| 51 | const AESKey normal = GetNormalKey(slot_id); | ||
| 52 | std::vector<u8> cipher(pdata.size() + CCM_MAC_SIZE); | ||
| 53 | |||
| 54 | try { | ||
| 55 | CCM_3DSVariant::Encryption e; | ||
| 56 | e.SetKeyWithIV(normal.data(), AES_BLOCK_SIZE, nonce.data(), CCM_NONCE_SIZE); | ||
| 57 | e.SpecifyDataLengths(0, pdata.size(), 0); | ||
| 58 | CryptoPP::ArraySource as(pdata.data(), pdata.size(), true, | ||
| 59 | new CryptoPP::AuthenticatedEncryptionFilter( | ||
| 60 | e, new CryptoPP::ArraySink(cipher.data(), cipher.size()))); | ||
| 61 | } catch (const CryptoPP::Exception& e) { | ||
| 62 | LOG_ERROR(HW_AES, "FAILED with: %s", e.what()); | ||
| 63 | } | ||
| 64 | return cipher; | ||
| 65 | } | ||
| 66 | |||
| 67 | std::vector<u8> DecryptVerifyCCM(const std::vector<u8>& cipher, const CCMNonce& nonce, | ||
| 68 | size_t slot_id) { | ||
| 69 | if (!IsNormalKeyAvailable(slot_id)) { | ||
| 70 | LOG_ERROR(HW_AES, "Key slot %d not available. Will use zero key.", slot_id); | ||
| 71 | } | ||
| 72 | const AESKey normal = GetNormalKey(slot_id); | ||
| 73 | const std::size_t pdata_size = cipher.size() - CCM_MAC_SIZE; | ||
| 74 | std::vector<u8> pdata(pdata_size); | ||
| 75 | |||
| 76 | try { | ||
| 77 | CCM_3DSVariant::Decryption d; | ||
| 78 | d.SetKeyWithIV(normal.data(), AES_BLOCK_SIZE, nonce.data(), CCM_NONCE_SIZE); | ||
| 79 | d.SpecifyDataLengths(0, pdata_size, 0); | ||
| 80 | CryptoPP::AuthenticatedDecryptionFilter df( | ||
| 81 | d, new CryptoPP::ArraySink(pdata.data(), pdata_size)); | ||
| 82 | CryptoPP::ArraySource as(cipher.data(), cipher.size(), true, new CryptoPP::Redirector(df)); | ||
| 83 | if (!df.GetLastResult()) { | ||
| 84 | LOG_ERROR(HW_AES, "FAILED"); | ||
| 85 | return {}; | ||
| 86 | } | ||
| 87 | } catch (const CryptoPP::Exception& e) { | ||
| 88 | LOG_ERROR(HW_AES, "FAILED with: %s", e.what()); | ||
| 89 | return {}; | ||
| 90 | } | ||
| 91 | return pdata; | ||
| 92 | } | ||
| 93 | |||
| 94 | } // namespace AES | ||
| 95 | } // namespace HW | ||
diff --git a/src/core/hw/aes/ccm.h b/src/core/hw/aes/ccm.h new file mode 100644 index 000000000..bf4146e80 --- /dev/null +++ b/src/core/hw/aes/ccm.h | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <cstddef> | ||
| 9 | #include <vector> | ||
| 10 | #include "common/common_types.h" | ||
| 11 | |||
| 12 | namespace HW { | ||
| 13 | namespace AES { | ||
| 14 | |||
| 15 | constexpr size_t CCM_NONCE_SIZE = 12; | ||
| 16 | constexpr size_t CCM_MAC_SIZE = 16; | ||
| 17 | |||
| 18 | using CCMNonce = std::array<u8, CCM_NONCE_SIZE>; | ||
| 19 | |||
| 20 | /** | ||
| 21 | * Encrypts and adds a MAC to the given data using AES-CCM algorithm. | ||
| 22 | * @param pdata The plain text data to encrypt | ||
| 23 | * @param nonce The nonce data to use for encryption | ||
| 24 | * @param slot_id The slot ID of the key to use for encryption | ||
| 25 | * @returns a vector of u8 containing the encrypted data with MAC at the end | ||
| 26 | */ | ||
| 27 | std::vector<u8> EncryptSignCCM(const std::vector<u8>& pdata, const CCMNonce& nonce, size_t slot_id); | ||
| 28 | |||
| 29 | /** | ||
| 30 | * Decrypts and verify the MAC of the given data using AES-CCM algorithm. | ||
| 31 | * @param cipher The cipher text data to decrypt, with MAC at the end to verify | ||
| 32 | * @param nonce The nonce data to use for decryption | ||
| 33 | * @param slot_id The slot ID of the key to use for decryption | ||
| 34 | * @returns a vector of u8 containing the decrypted data; an empty vector if the verification fails | ||
| 35 | */ | ||
| 36 | std::vector<u8> DecryptVerifyCCM(const std::vector<u8>& cipher, const CCMNonce& nonce, | ||
| 37 | size_t slot_id); | ||
| 38 | |||
| 39 | } // namespace AES | ||
| 40 | } // namespace HW | ||
diff --git a/src/core/hw/aes/key.cpp b/src/core/hw/aes/key.cpp new file mode 100644 index 000000000..4e8a8a59a --- /dev/null +++ b/src/core/hw/aes/key.cpp | |||
| @@ -0,0 +1,173 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <exception> | ||
| 7 | #include <sstream> | ||
| 8 | #include <boost/optional.hpp> | ||
| 9 | #include "common/common_paths.h" | ||
| 10 | #include "common/file_util.h" | ||
| 11 | #include "common/logging/log.h" | ||
| 12 | #include "common/string_util.h" | ||
| 13 | #include "core/hw/aes/arithmetic128.h" | ||
| 14 | #include "core/hw/aes/key.h" | ||
| 15 | |||
| 16 | namespace HW { | ||
| 17 | namespace AES { | ||
| 18 | |||
| 19 | namespace { | ||
| 20 | |||
| 21 | boost::optional<AESKey> generator_constant; | ||
| 22 | |||
| 23 | struct KeySlot { | ||
| 24 | boost::optional<AESKey> x; | ||
| 25 | boost::optional<AESKey> y; | ||
| 26 | boost::optional<AESKey> normal; | ||
| 27 | |||
| 28 | void SetKeyX(const AESKey& key) { | ||
| 29 | x = key; | ||
| 30 | if (y && generator_constant) { | ||
| 31 | GenerateNormalKey(); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | void SetKeyY(const AESKey& key) { | ||
| 36 | y = key; | ||
| 37 | if (x && generator_constant) { | ||
| 38 | GenerateNormalKey(); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | void SetNormalKey(const AESKey& key) { | ||
| 43 | normal = key; | ||
| 44 | } | ||
| 45 | |||
| 46 | void GenerateNormalKey() { | ||
| 47 | normal = Lrot128(Add128(Xor128(Lrot128(*x, 2), *y), *generator_constant), 87); | ||
| 48 | } | ||
| 49 | |||
| 50 | void Clear() { | ||
| 51 | x.reset(); | ||
| 52 | y.reset(); | ||
| 53 | normal.reset(); | ||
| 54 | } | ||
| 55 | }; | ||
| 56 | |||
| 57 | std::array<KeySlot, KeySlotID::MaxKeySlotID> key_slots; | ||
| 58 | |||
| 59 | void ClearAllKeys() { | ||
| 60 | for (KeySlot& slot : key_slots) { | ||
| 61 | slot.Clear(); | ||
| 62 | } | ||
| 63 | generator_constant.reset(); | ||
| 64 | } | ||
| 65 | |||
| 66 | AESKey HexToKey(const std::string& hex) { | ||
| 67 | if (hex.size() < 32) { | ||
| 68 | throw std::invalid_argument("hex string is too short"); | ||
| 69 | } | ||
| 70 | |||
| 71 | AESKey key; | ||
| 72 | for (size_t i = 0; i < key.size(); ++i) { | ||
| 73 | key[i] = static_cast<u8>(std::stoi(hex.substr(i * 2, 2), 0, 16)); | ||
| 74 | } | ||
| 75 | |||
| 76 | return key; | ||
| 77 | } | ||
| 78 | |||
| 79 | void LoadPresetKeys() { | ||
| 80 | const std::string filepath = FileUtil::GetUserPath(D_SYSDATA_IDX) + AES_KEYS; | ||
| 81 | FileUtil::CreateFullPath(filepath); // Create path if not already created | ||
| 82 | std::ifstream file; | ||
| 83 | OpenFStream(file, filepath, std::ios_base::in); | ||
| 84 | if (!file) { | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | |||
| 88 | while (!file.eof()) { | ||
| 89 | std::string line; | ||
| 90 | std::getline(file, line); | ||
| 91 | std::vector<std::string> parts; | ||
| 92 | Common::SplitString(line, '=', parts); | ||
| 93 | if (parts.size() != 2) { | ||
| 94 | LOG_ERROR(HW_AES, "Failed to parse %s", line.c_str()); | ||
| 95 | continue; | ||
| 96 | } | ||
| 97 | |||
| 98 | const std::string& name = parts[0]; | ||
| 99 | AESKey key; | ||
| 100 | try { | ||
| 101 | key = HexToKey(parts[1]); | ||
| 102 | } catch (const std::logic_error& e) { | ||
| 103 | LOG_ERROR(HW_AES, "Invalid key %s: %s", parts[1].c_str(), e.what()); | ||
| 104 | continue; | ||
| 105 | } | ||
| 106 | |||
| 107 | if (name == "generator") { | ||
| 108 | generator_constant = key; | ||
| 109 | continue; | ||
| 110 | } | ||
| 111 | |||
| 112 | size_t slot_id; | ||
| 113 | char key_type; | ||
| 114 | if (std::sscanf(name.c_str(), "slot0x%zXKey%c", &slot_id, &key_type) != 2) { | ||
| 115 | LOG_ERROR(HW_AES, "Invalid key name %s", name.c_str()); | ||
| 116 | continue; | ||
| 117 | } | ||
| 118 | |||
| 119 | if (slot_id >= MaxKeySlotID) { | ||
| 120 | LOG_ERROR(HW_AES, "Out of range slot ID 0x%zX", slot_id); | ||
| 121 | continue; | ||
| 122 | } | ||
| 123 | |||
| 124 | switch (key_type) { | ||
| 125 | case 'X': | ||
| 126 | key_slots.at(slot_id).SetKeyX(key); | ||
| 127 | break; | ||
| 128 | case 'Y': | ||
| 129 | key_slots.at(slot_id).SetKeyY(key); | ||
| 130 | break; | ||
| 131 | case 'N': | ||
| 132 | key_slots.at(slot_id).SetNormalKey(key); | ||
| 133 | break; | ||
| 134 | default: | ||
| 135 | LOG_ERROR(HW_AES, "Invalid key type %c", key_type); | ||
| 136 | break; | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | } // namespace | ||
| 142 | |||
| 143 | void InitKeys() { | ||
| 144 | ClearAllKeys(); | ||
| 145 | LoadPresetKeys(); | ||
| 146 | } | ||
| 147 | |||
| 148 | void SetGeneratorConstant(const AESKey& key) { | ||
| 149 | generator_constant = key; | ||
| 150 | } | ||
| 151 | |||
| 152 | void SetKeyX(size_t slot_id, const AESKey& key) { | ||
| 153 | key_slots.at(slot_id).SetKeyX(key); | ||
| 154 | } | ||
| 155 | |||
| 156 | void SetKeyY(size_t slot_id, const AESKey& key) { | ||
| 157 | key_slots.at(slot_id).SetKeyY(key); | ||
| 158 | } | ||
| 159 | |||
| 160 | void SetNormalKey(size_t slot_id, const AESKey& key) { | ||
| 161 | key_slots.at(slot_id).SetNormalKey(key); | ||
| 162 | } | ||
| 163 | |||
| 164 | bool IsNormalKeyAvailable(size_t slot_id) { | ||
| 165 | return key_slots.at(slot_id).normal.is_initialized(); | ||
| 166 | } | ||
| 167 | |||
| 168 | AESKey GetNormalKey(size_t slot_id) { | ||
| 169 | return key_slots.at(slot_id).normal.value_or(AESKey{}); | ||
| 170 | } | ||
| 171 | |||
| 172 | } // namespace AES | ||
| 173 | } // namespace HW | ||
diff --git a/src/core/hw/aes/key.h b/src/core/hw/aes/key.h new file mode 100644 index 000000000..b01d04f13 --- /dev/null +++ b/src/core/hw/aes/key.h | |||
| @@ -0,0 +1,35 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <cstddef> | ||
| 9 | #include "common/common_types.h" | ||
| 10 | |||
| 11 | namespace HW { | ||
| 12 | namespace AES { | ||
| 13 | |||
| 14 | enum KeySlotID : size_t { | ||
| 15 | APTWrap = 0x31, | ||
| 16 | |||
| 17 | MaxKeySlotID = 0x40, | ||
| 18 | }; | ||
| 19 | |||
| 20 | constexpr size_t AES_BLOCK_SIZE = 16; | ||
| 21 | |||
| 22 | using AESKey = std::array<u8, AES_BLOCK_SIZE>; | ||
| 23 | |||
| 24 | void InitKeys(); | ||
| 25 | |||
| 26 | void SetGeneratorConstant(const AESKey& key); | ||
| 27 | void SetKeyX(size_t slot_id, const AESKey& key); | ||
| 28 | void SetKeyY(size_t slot_id, const AESKey& key); | ||
| 29 | void SetNormalKey(size_t slot_id, const AESKey& key); | ||
| 30 | |||
| 31 | bool IsNormalKeyAvailable(size_t slot_id); | ||
| 32 | AESKey GetNormalKey(size_t slot_id); | ||
| 33 | |||
| 34 | } // namspace AES | ||
| 35 | } // namespace HW | ||
diff --git a/src/core/hw/hw.cpp b/src/core/hw/hw.cpp index 9ff8825b2..8499f2ce6 100644 --- a/src/core/hw/hw.cpp +++ b/src/core/hw/hw.cpp | |||
| @@ -4,6 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #include "common/common_types.h" | 5 | #include "common/common_types.h" |
| 6 | #include "common/logging/log.h" | 6 | #include "common/logging/log.h" |
| 7 | #include "core/hw/aes/key.h" | ||
| 7 | #include "core/hw/gpu.h" | 8 | #include "core/hw/gpu.h" |
| 8 | #include "core/hw/hw.h" | 9 | #include "core/hw/hw.h" |
| 9 | #include "core/hw/lcd.h" | 10 | #include "core/hw/lcd.h" |
| @@ -85,6 +86,7 @@ void Update() {} | |||
| 85 | 86 | ||
| 86 | /// Initialize hardware | 87 | /// Initialize hardware |
| 87 | void Init() { | 88 | void Init() { |
| 89 | AES::InitKeys(); | ||
| 88 | GPU::Init(); | 90 | GPU::Init(); |
| 89 | LCD::Init(); | 91 | LCD::Init(); |
| 90 | LOG_DEBUG(HW, "initialized OK"); | 92 | LOG_DEBUG(HW, "initialized OK"); |