From 408ae95120745bb028a24df112b4b40e61666226 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Thu, 4 Apr 2019 10:30:29 -0700 Subject: [PATCH] cleanup headers, add crc32 acceleration --- nx/include/switch.h | 12 +++++- nx/include/switch/crypto.h | 17 -------- nx/include/switch/crypto/crc.h | 74 ++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 18 deletions(-) delete mode 100644 nx/include/switch/crypto.h create mode 100644 nx/include/switch/crypto/crc.h diff --git a/nx/include/switch.h b/nx/include/switch.h index 785c9d0c..3d457de0 100644 --- a/nx/include/switch.h +++ b/nx/include/switch.h @@ -124,7 +124,17 @@ extern "C" { #include "switch/runtime/devices/romfs_dev.h" #include "switch/runtime/devices/socket.h" -#include "switch/crypto.h" +#include "switch/crypto/aes.h" +#include "switch/crypto/aes_cbc.h" +#include "switch/crypto/aes_ctr.h" +#include "switch/crypto/aes_xts.h" +#include "switch/crypto/cmac.h" + +#include "switch/crypto/sha256.h" +#include "switch/crypto/sha1.h" +#include "switch/crypto/hmac.h" + +#include "switch/crypto/crc.h" #ifdef __cplusplus } diff --git a/nx/include/switch/crypto.h b/nx/include/switch/crypto.h deleted file mode 100644 index 70e6a45e..00000000 --- a/nx/include/switch/crypto.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file crypto.h - * @brief Switch (accelerated) crypto includes. - * @copyright libnx Authors - */ -#pragma once -#include "types.h" - -#include "crypto/aes.h" -#include "crypto/aes_cbc.h" -#include "crypto/aes_ctr.h" -#include "crypto/aes_xts.h" -#include "crypto/cmac.h" - -#include "crypto/sha256.h" -#include "crypto/sha1.h" -#include "crypto/hmac.h" \ No newline at end of file diff --git a/nx/include/switch/crypto/crc.h b/nx/include/switch/crypto/crc.h new file mode 100644 index 00000000..9b4aa59c --- /dev/null +++ b/nx/include/switch/crypto/crc.h @@ -0,0 +1,74 @@ +/** + * @file crc.h + * @brief Hardware accelerated CRC32 implementation. + * @copyright libnx Authors + */ +#pragma once +#include +#include "../types.h" + +#define _CRC_ALIGN(sz, insn) \ +do { \ + if (((uintptr_t)src_u8 & sizeof(sz)) && len >= sizeof(sz)) { \ + crc = __crc32##insn(crc, *((const sz *)src_u8)); \ + src_u8 += sizeof(sz); \ + len -= sizeof(sz); \ + } \ +} while (0) + +#define _CRC_REMAINDER(sz, insn) \ +do { \ + if (len & sizeof(sz)) { \ + crc = __crc32##insn(crc, *((const sz *)src_u8)); \ + src_u8 += sizeof(sz); \ + } \ +} while (0) + +/// Calculate a CRC32 over data. +static inline u32 crc32Calculate(const void *src, size_t size) { + const u8 *src_u8 = (const u8 *)src; + + u32 crc = 0xFFFFFFFF; + s64 len = size; + + _CRC_ALIGN(u8, b); + _CRC_ALIGN(u16, h); + _CRC_ALIGN(u32, w); + + while ((len -= sizeof(u64)) >= 0) { + crc = __crc32d(crc, *((const u64 *)src_u8)); + src_u8 += sizeof(u64); + } + + _CRC_REMAINDER(u32, w); + _CRC_REMAINDER(u16, h); + _CRC_REMAINDER(u8, b); + + return crc ^ 0xFFFFFFFF; +} + +/// Calculate a CRC32C over data. +static inline u32 crc32cCalculate(const void *src, size_t size) { + const u8 *src_u8 = (const u8 *)src; + + u32 crc = 0xFFFFFFFF; + s64 len = size; + + _CRC_ALIGN(u8, cb); + _CRC_ALIGN(u16, ch); + _CRC_ALIGN(u32, cw); + + while ((len -= sizeof(u64)) >= 0) { + crc = __crc32cd(crc, *((const u64 *)src_u8)); + src_u8 += sizeof(u64); + } + + _CRC_REMAINDER(u32, cw); + _CRC_REMAINDER(u16, ch); + _CRC_REMAINDER(u8, cb); + + return crc ^ 0xFFFFFFFF; +} + +#undef _CRC_REMAINDER +#undef _CRC_ALIGN