From b92cddc18230e9890eb3df307290bfdc60661f46 Mon Sep 17 00:00:00 2001 From: "Dr. Chat" Date: Sat, 18 Jul 2015 14:50:30 -0500 Subject: [PATCH] Implement SHA-1 in xboxkrnl (correctness not verified though) Stubs for some weird crypto functions --- src/xenia/kernel/xboxkrnl_crypt.cc | 110 ++++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 9 deletions(-) diff --git a/src/xenia/kernel/xboxkrnl_crypt.cc b/src/xenia/kernel/xboxkrnl_crypt.cc index c41914610..b52053191 100644 --- a/src/xenia/kernel/xboxkrnl_crypt.cc +++ b/src/xenia/kernel/xboxkrnl_crypt.cc @@ -13,27 +13,119 @@ #include "xenia/kernel/xboxkrnl_private.h" #include "xenia/xbox.h" +#include "third_party/crypto/TinySHA1.hpp" + namespace xe { namespace kernel { typedef struct { - xe::be count; - xe::be state[5]; - uint8_t buffer[64]; + xe::be count; // 0x0 + xe::be state[5]; // 0x4 + uint8_t buffer[64]; // 0x18 } XECRYPT_SHA_STATE; +void InitSha1(sha1::SHA1& sha, const XECRYPT_SHA_STATE* state) { + uint32_t digest[5]; + for (int i = 0; i < 5; i++) { + digest[i] = state->state[i]; + } + + sha.init(digest, state->buffer, state->count); +} + +void StoreSha1(sha1::SHA1& sha, XECRYPT_SHA_STATE* state) { + for (int i = 0; i < 5; i++) { + state->state[i] = sha.getDigest()[i]; + } + + state->count = (uint32_t)sha.getByteCount(); + std::memcpy(state->buffer, sha.getBlock(), sha.getBlockByteIndex()); +} + void XeCryptShaInit(pointer_t sha_state) { sha_state.Zero(); + + sha_state->state[0] = 0x67452301; + sha_state->state[1] = 0xEFCDAB89; + sha_state->state[2] = 0x98BADCFE; + sha_state->state[3] = 0x10325476; + sha_state->state[4] = 0xC3D2E1F0; } -DECLARE_XBOXKRNL_EXPORT(XeCryptShaInit, ExportTag::kStub); +DECLARE_XBOXKRNL_EXPORT(XeCryptShaInit, ExportTag::kImplemented); void XeCryptShaUpdate(pointer_t sha_state, lpvoid_t input, - dword_t input_size) {} -DECLARE_XBOXKRNL_EXPORT(XeCryptShaUpdate, ExportTag::kStub); + dword_t input_size) { + sha1::SHA1 sha; + InitSha1(sha, sha_state); -void XeCryptShaFinal(pointer_t sha_state, lpvoid_t out, - dword_t out_size) {} -DECLARE_XBOXKRNL_EXPORT(XeCryptShaFinal, ExportTag::kStub); + sha.processBytes(input, input_size); + + StoreSha1(sha, sha_state); +} +DECLARE_XBOXKRNL_EXPORT(XeCryptShaUpdate, ExportTag::kImplemented); + +void XeCryptShaFinal(pointer_t sha_state, + pointer_t> out, dword_t out_size) { + sha1::SHA1 sha; + InitSha1(sha, sha_state); + + uint32_t digest[5]; + sha.finalize(digest); + + for (int i = 0; i < 5; i++) { + sha_state->state[i] = digest[i]; + } + + for (uint32_t i = 0; i < out_size / 4; i++) { + out[i] = digest[i]; + } +} +DECLARE_XBOXKRNL_EXPORT(XeCryptShaFinal, ExportTag::kImplemented); + +void XeCryptSha(lpvoid_t input_1, dword_t input_1_size, lpvoid_t input_2, + dword_t input_2_size, lpvoid_t input_3, dword_t input_3_size, + pointer_t> output, dword_t output_size) { + sha1::SHA1 sha; + + if (input_1 && input_1_size) { + sha.processBytes(input_1, input_1_size); + } + if (input_2 && input_2_size) { + sha.processBytes(input_2, input_2_size); + } + if (input_3 && input_3_size) { + sha.processBytes(input_3, input_3_size); + } + + uint32_t digest[5]; + sha.finalize(digest); + + for (uint32_t i = 0; i < output_size / 4; i++) { + output[i] = digest[i]; + } +} +DECLARE_XBOXKRNL_EXPORT(XeCryptSha, ExportTag::kImplemented); + +// Byteswap? +dword_result_t XeCryptBnQw_SwapDwQwLeBe(const lpqword_t qw_inp, + lpqword_t qw_out, dword_t size) { + return 0; +} +DECLARE_XBOXKRNL_EXPORT(XeCryptBnQw_SwapDwQwLeBe, ExportTag::kStub); + +dword_result_t XeCryptBnQwNeRsaPubCrypt(const lpqword_t qw_a, lpqword_t qw_b, + const lpvoid_t rsa) { + // 0 indicates failure (but not a BOOL return value) + return 1; +} +DECLARE_XBOXKRNL_EXPORT(XeCryptBnQwNeRsaPubCrypt, ExportTag::kStub); + +dword_result_t XeCryptBnDwLePkcs1Verify(const lpvoid_t hash, const lpvoid_t sig, + const dword_t size) { + // BOOL return value + return 1; +} +DECLARE_XBOXKRNL_EXPORT(XeCryptBnDwLePkcs1Verify, ExportTag::kStub); void xe::kernel::xboxkrnl::RegisterCryptExports( xe::cpu::ExportResolver* export_resolver, KernelState* kernel_state) {}