2021-02-03 11:54:41 +01:00
|
|
|
#include <hex/helpers/crypto.hpp>
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-01-13 17:28:27 +01:00
|
|
|
#include <hex/providers/provider.hpp>
|
2021-09-03 02:34:40 +02:00
|
|
|
#include <hex/helpers/utils.hpp>
|
2022-06-25 12:19:59 +02:00
|
|
|
#include <hex/helpers/concepts.hpp>
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
#include <mbedtls/version.h>
|
2021-02-02 23:11:23 +01:00
|
|
|
#include <mbedtls/base64.h>
|
2021-09-03 02:34:40 +02:00
|
|
|
#include <mbedtls/bignum.h>
|
2021-02-02 23:11:23 +01:00
|
|
|
#include <mbedtls/md5.h>
|
|
|
|
#include <mbedtls/sha1.h>
|
|
|
|
#include <mbedtls/sha256.h>
|
|
|
|
#include <mbedtls/sha512.h>
|
2021-02-03 11:54:41 +01:00
|
|
|
#include <mbedtls/aes.h>
|
|
|
|
#include <mbedtls/cipher.h>
|
2020-11-24 02:00:22 +01:00
|
|
|
|
2020-11-12 12:00:50 +01:00
|
|
|
#include <array>
|
|
|
|
#include <span>
|
2021-10-26 17:21:48 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
2022-03-27 00:01:28 +01:00
|
|
|
#include <bit>
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
#if MBEDTLS_VERSION_MAJOR <= 2
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#define mbedtls_md5_starts mbedtls_md5_starts_ret
|
|
|
|
#define mbedtls_md5_update mbedtls_md5_update_ret
|
|
|
|
#define mbedtls_md5_finish mbedtls_md5_finish_ret
|
2021-08-17 17:28:33 +02:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#define mbedtls_sha1_starts mbedtls_sha1_starts_ret
|
|
|
|
#define mbedtls_sha1_update mbedtls_sha1_update_ret
|
|
|
|
#define mbedtls_sha1_finish mbedtls_sha1_finish_ret
|
2021-08-17 17:28:33 +02:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#define mbedtls_sha256_starts mbedtls_sha256_starts_ret
|
|
|
|
#define mbedtls_sha256_update mbedtls_sha256_update_ret
|
|
|
|
#define mbedtls_sha256_finish mbedtls_sha256_finish_ret
|
2021-08-17 17:28:33 +02:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
#define mbedtls_sha512_starts mbedtls_sha512_starts_ret
|
|
|
|
#define mbedtls_sha512_update mbedtls_sha512_update_ret
|
|
|
|
#define mbedtls_sha512_finish mbedtls_sha512_finish_ret
|
2021-08-17 17:28:33 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-02-02 23:11:23 +01:00
|
|
|
namespace hex::crypt {
|
2021-10-26 17:21:48 +02:00
|
|
|
using namespace std::placeholders;
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
template<std::invocable<unsigned char *, size_t> Func>
|
|
|
|
void processDataByChunks(prv::Provider *data, u64 offset, size_t size, Func func) {
|
2021-10-26 17:21:48 +02:00
|
|
|
std::array<u8, 512> buffer = { 0 };
|
|
|
|
for (size_t bufferOffset = 0; bufferOffset < size; bufferOffset += buffer.size()) {
|
|
|
|
const auto readSize = std::min(buffer.size(), size - bufferOffset);
|
|
|
|
data->read(offset + bufferOffset, buffer.data(), readSize);
|
|
|
|
func(buffer.data(), readSize);
|
|
|
|
}
|
|
|
|
}
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
template<typename T>
|
2022-01-24 20:53:17 +01:00
|
|
|
T reflect(T in, std::size_t bits) {
|
|
|
|
T out {};
|
2021-10-26 17:21:48 +02:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
for (std::size_t i = 0; i < bits; i++) {
|
2021-10-26 17:21:48 +02:00
|
|
|
out <<= 1;
|
|
|
|
if (in & 0b1)
|
|
|
|
out |= 1;
|
|
|
|
in >>= 1;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
template<typename T>
|
2022-01-24 20:53:17 +01:00
|
|
|
T reflect(T in) {
|
|
|
|
if constexpr (sizeof(T) == 1) {
|
|
|
|
T out { in };
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
out = ((out & 0xf0u) >> 4) | ((out & 0x0fu) << 4);
|
|
|
|
out = ((out & 0xccu) >> 2) | ((out & 0x33u) << 2);
|
|
|
|
out = ((out & 0xaau) >> 1) | ((out & 0x55u) << 1);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
return out;
|
2022-01-24 20:53:17 +01:00
|
|
|
} else {
|
|
|
|
return reflect(in, sizeof(T) * 8);
|
2021-10-26 17:21:48 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
template<size_t NumBits> requires (std::has_single_bit(NumBits))
|
2021-10-26 17:21:48 +02:00
|
|
|
class Crc {
|
|
|
|
// use reflected algorithm, so we reflect only if refin / refout is FALSE
|
|
|
|
// mask values, 0b1 << 64 is UB, so use 0b10 << 63
|
|
|
|
|
|
|
|
public:
|
2022-03-27 00:01:28 +01:00
|
|
|
constexpr Crc(u64 polynomial, u64 init, u64 xorOut, bool reflectInput, bool reflectOutput)
|
|
|
|
: m_value(0x00), m_init(init & ((0b10ull << (NumBits - 1)) - 1)), m_xorOut(xorOut & ((0b10ull << (NumBits - 1)) - 1)),
|
|
|
|
m_reflectInput(reflectInput), m_reflectOutput(reflectOutput),
|
|
|
|
m_table([polynomial]() {
|
|
|
|
auto reflectedPoly = reflect(polynomial & ((0b10ull << (NumBits - 1)) - 1), NumBits);
|
|
|
|
std::array<uint64_t, 256> table = { 0 };
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < 256; i++) {
|
|
|
|
uint64_t c = i;
|
|
|
|
for (std::size_t j = 0; j < 8; j++) {
|
|
|
|
if (c & 0b1)
|
|
|
|
c = reflectedPoly ^ (c >> 1);
|
|
|
|
else
|
|
|
|
c >>= 1;
|
|
|
|
}
|
|
|
|
table[i] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return table;
|
|
|
|
}()) {
|
2021-10-26 17:21:48 +02:00
|
|
|
reset();
|
|
|
|
};
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
constexpr void reset() {
|
|
|
|
this->m_value = reflect(m_init, NumBits);
|
2021-10-26 17:21:48 +02:00
|
|
|
}
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
constexpr void processBytes(const unsigned char *data, std::size_t size) {
|
2021-10-26 17:21:48 +02:00
|
|
|
for (std::size_t i = 0; i < size; i++) {
|
2022-03-27 00:01:28 +01:00
|
|
|
u8 byte;
|
|
|
|
if (this->m_reflectInput)
|
|
|
|
byte = data[i];
|
2021-10-26 17:21:48 +02:00
|
|
|
else
|
2022-03-27 00:01:28 +01:00
|
|
|
byte = reflect(data[i]);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
this->m_value = this->m_table[(this->m_value ^ byte) & 0xFFL] ^ (this->m_value >> 8);
|
2020-11-12 12:00:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
[[nodiscard]]
|
|
|
|
constexpr u64 checksum() const {
|
|
|
|
if (this->m_reflectOutput)
|
|
|
|
return this->m_value ^ m_xorOut;
|
2021-10-26 17:21:48 +02:00
|
|
|
else
|
2022-03-27 00:01:28 +01:00
|
|
|
return reflect(this->m_value, NumBits) ^ m_xorOut;
|
2021-10-26 17:21:48 +02:00
|
|
|
}
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
private:
|
2022-03-27 00:01:28 +01:00
|
|
|
u64 m_value;
|
|
|
|
|
|
|
|
u64 m_init;
|
|
|
|
u64 m_xorOut;
|
|
|
|
bool m_reflectInput;
|
|
|
|
bool m_reflectOutput;
|
|
|
|
|
|
|
|
std::array<uint64_t, 256> m_table;
|
2021-10-26 17:21:48 +02:00
|
|
|
};
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
template<size_t NumBits>
|
2022-01-24 20:53:17 +01:00
|
|
|
auto calcCrc(prv::Provider *data, u64 offset, std::size_t size, u32 polynomial, u32 init, u32 xorout, bool reflectIn, bool reflectOut) {
|
2022-03-27 00:01:28 +01:00
|
|
|
using Crc = Crc<NumBits>;
|
|
|
|
Crc crc(polynomial, init, xorout, reflectIn, reflectOut);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
processDataByChunks(data, offset, size, std::bind(&Crc::processBytes, &crc, _1, _2));
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
return crc.checksum();
|
|
|
|
}
|
|
|
|
|
2023-01-25 00:06:48 +01:00
|
|
|
u8 crc8(prv::Provider *&data, u64 offset, size_t size, u32 polynomial, u32 init, u32 xorOut, bool reflectIn, bool reflectOut) {
|
2022-03-27 00:01:28 +01:00
|
|
|
return calcCrc<8>(data, offset, size, polynomial, init, xorOut, reflectIn, reflectOut);
|
2021-10-26 17:21:48 +02:00
|
|
|
}
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
u16 crc16(prv::Provider *&data, u64 offset, size_t size, u32 polynomial, u32 init, u32 xorOut, bool reflectIn, bool reflectOut) {
|
|
|
|
return calcCrc<16>(data, offset, size, polynomial, init, xorOut, reflectIn, reflectOut);
|
2021-10-26 17:21:48 +02:00
|
|
|
}
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
u32 crc32(prv::Provider *&data, u64 offset, size_t size, u32 polynomial, u32 init, u32 xorOut, bool reflectIn, bool reflectOut) {
|
|
|
|
return calcCrc<32>(data, offset, size, polynomial, init, xorOut, reflectIn, reflectOut);
|
2020-11-12 12:00:50 +01:00
|
|
|
}
|
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::array<u8, 16> md5(prv::Provider *&data, u64 offset, size_t size) {
|
2021-02-02 23:11:23 +01:00
|
|
|
std::array<u8, 16> result = { 0 };
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-02-02 23:11:23 +01:00
|
|
|
mbedtls_md5_context ctx;
|
|
|
|
mbedtls_md5_init(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
mbedtls_md5_starts(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
processDataByChunks(data, offset, size, std::bind(mbedtls_md5_update, &ctx, _1, _2));
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
mbedtls_md5_finish(&ctx, result.data());
|
2021-02-02 23:11:23 +01:00
|
|
|
|
|
|
|
mbedtls_md5_free(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
std::array<u8, 16> md5(const std::vector<u8> &data) {
|
|
|
|
std::array<u8, 16> result = { 0 };
|
|
|
|
|
|
|
|
mbedtls_md5_context ctx;
|
|
|
|
mbedtls_md5_init(&ctx);
|
|
|
|
|
|
|
|
mbedtls_md5_starts(&ctx);
|
|
|
|
mbedtls_md5_update(&ctx, data.data(), data.size());
|
|
|
|
mbedtls_md5_finish(&ctx, result.data());
|
|
|
|
|
|
|
|
mbedtls_md5_free(&ctx);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::array<u8, 20> sha1(prv::Provider *&data, u64 offset, size_t size) {
|
2021-02-02 23:11:23 +01:00
|
|
|
std::array<u8, 20> result = { 0 };
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-02-02 23:11:23 +01:00
|
|
|
mbedtls_sha1_context ctx;
|
|
|
|
mbedtls_sha1_init(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
mbedtls_sha1_starts(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
processDataByChunks(data, offset, size, std::bind(mbedtls_sha1_update, &ctx, _1, _2));
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
mbedtls_sha1_finish(&ctx, result.data());
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-02-02 23:11:23 +01:00
|
|
|
mbedtls_sha1_free(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
std::array<u8, 20> sha1(const std::vector<u8> &data) {
|
|
|
|
std::array<u8, 20> result = { 0 };
|
|
|
|
|
|
|
|
mbedtls_sha1_context ctx;
|
|
|
|
mbedtls_sha1_init(&ctx);
|
|
|
|
|
|
|
|
mbedtls_sha1_starts(&ctx);
|
|
|
|
mbedtls_sha1_update(&ctx, data.data(), data.size());
|
|
|
|
mbedtls_sha1_finish(&ctx, result.data());
|
|
|
|
|
|
|
|
mbedtls_sha1_free(&ctx);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::array<u8, 28> sha224(prv::Provider *&data, u64 offset, size_t size) {
|
2021-02-02 23:11:23 +01:00
|
|
|
std::array<u8, 28> result = { 0 };
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-02-02 23:11:23 +01:00
|
|
|
mbedtls_sha256_context ctx;
|
|
|
|
mbedtls_sha256_init(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
mbedtls_sha256_starts(&ctx, true);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
processDataByChunks(data, offset, size, std::bind(mbedtls_sha256_update, &ctx, _1, _2));
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
mbedtls_sha256_finish(&ctx, result.data());
|
2021-02-02 23:11:23 +01:00
|
|
|
|
|
|
|
mbedtls_sha256_free(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
std::array<u8, 28> sha224(const std::vector<u8> &data) {
|
|
|
|
std::array<u8, 28> result = { 0 };
|
|
|
|
|
|
|
|
mbedtls_sha256_context ctx;
|
|
|
|
mbedtls_sha256_init(&ctx);
|
|
|
|
|
|
|
|
mbedtls_sha256_starts(&ctx, true);
|
|
|
|
mbedtls_sha256_update(&ctx, data.data(), data.size());
|
|
|
|
mbedtls_sha256_finish(&ctx, result.data());
|
|
|
|
|
|
|
|
mbedtls_sha256_free(&ctx);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::array<u8, 32> sha256(prv::Provider *&data, u64 offset, size_t size) {
|
2021-02-02 23:11:23 +01:00
|
|
|
std::array<u8, 32> result = { 0 };
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-02-02 23:11:23 +01:00
|
|
|
mbedtls_sha256_context ctx;
|
|
|
|
mbedtls_sha256_init(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
mbedtls_sha256_starts(&ctx, false);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
processDataByChunks(data, offset, size, std::bind(mbedtls_sha256_update, &ctx, _1, _2));
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
mbedtls_sha256_finish(&ctx, result.data());
|
2021-02-02 23:11:23 +01:00
|
|
|
|
|
|
|
mbedtls_sha256_free(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
std::array<u8, 32> sha256(const std::vector<u8> &data) {
|
|
|
|
std::array<u8, 32> result = { 0 };
|
|
|
|
|
|
|
|
mbedtls_sha256_context ctx;
|
|
|
|
mbedtls_sha256_init(&ctx);
|
|
|
|
|
|
|
|
mbedtls_sha256_starts(&ctx, false);
|
|
|
|
mbedtls_sha256_update(&ctx, data.data(), data.size());
|
|
|
|
mbedtls_sha256_finish(&ctx, result.data());
|
|
|
|
|
|
|
|
mbedtls_sha256_free(&ctx);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::array<u8, 48> sha384(prv::Provider *&data, u64 offset, size_t size) {
|
2021-02-02 23:11:23 +01:00
|
|
|
std::array<u8, 48> result = { 0 };
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-02-02 23:11:23 +01:00
|
|
|
mbedtls_sha512_context ctx;
|
|
|
|
mbedtls_sha512_init(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
mbedtls_sha512_starts(&ctx, true);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
processDataByChunks(data, offset, size, std::bind(mbedtls_sha512_update, &ctx, _1, _2));
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
mbedtls_sha512_finish(&ctx, result.data());
|
2021-02-02 23:11:23 +01:00
|
|
|
|
|
|
|
mbedtls_sha512_free(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
std::array<u8, 48> sha384(const std::vector<u8> &data) {
|
|
|
|
std::array<u8, 48> result = { 0 };
|
|
|
|
|
|
|
|
mbedtls_sha512_context ctx;
|
|
|
|
mbedtls_sha512_init(&ctx);
|
|
|
|
|
|
|
|
mbedtls_sha512_starts(&ctx, true);
|
|
|
|
mbedtls_sha512_update(&ctx, data.data(), data.size());
|
|
|
|
mbedtls_sha512_finish(&ctx, result.data());
|
|
|
|
|
|
|
|
mbedtls_sha512_free(&ctx);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
std::array<u8, 64> sha512(prv::Provider *&data, u64 offset, size_t size) {
|
2021-02-02 23:11:23 +01:00
|
|
|
std::array<u8, 64> result = { 0 };
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-02-02 23:11:23 +01:00
|
|
|
mbedtls_sha512_context ctx;
|
|
|
|
mbedtls_sha512_init(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
mbedtls_sha512_starts(&ctx, false);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
processDataByChunks(data, offset, size, std::bind(mbedtls_sha512_update, &ctx, _1, _2));
|
2020-11-12 12:00:50 +01:00
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
mbedtls_sha512_finish(&ctx, result.data());
|
2021-02-02 23:11:23 +01:00
|
|
|
|
|
|
|
mbedtls_sha512_free(&ctx);
|
2020-11-12 12:00:50 +01:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
std::array<u8, 64> sha512(const std::vector<u8> &data) {
|
|
|
|
std::array<u8, 64> result = { 0 };
|
|
|
|
|
|
|
|
mbedtls_sha512_context ctx;
|
|
|
|
mbedtls_sha512_init(&ctx);
|
|
|
|
|
|
|
|
mbedtls_sha512_starts(&ctx, false);
|
|
|
|
mbedtls_sha512_update(&ctx, data.data(), data.size());
|
|
|
|
mbedtls_sha512_finish(&ctx, result.data());
|
|
|
|
|
|
|
|
mbedtls_sha512_free(&ctx);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-24 02:00:22 +01:00
|
|
|
std::vector<u8> decode64(const std::vector<u8> &input) {
|
|
|
|
|
2021-02-02 23:11:23 +01:00
|
|
|
size_t written = 0;
|
Tests for the CRC and hash algorithms (#335)
* Update TEST_ASSERT to do nothing if condition is true
The TEST_ASSERT should not return if the condition is true, because:
- it prevents the usage of multiple TEST_ASSERT in a single test case,
- that behavior differs from how the assert in the standard library
works, and thus may give unexpected results.
Make the TEST_ASSERT to print an error message (with an formatted
optional user part) when it fails to make debugging easier.
* Fix some bugs in TestProvider, add unit tests
Use pointer-to-vector in TestProvider so writes can be tested, too.
* Add test EncodeDecode16, fix some encode16 bugs
The function mbedtls_mpi_write_string needs a bit longer buffer than the
resulting string actually will be.
Known bug: mbedtls_mpi_read_binary ingores initial null bytes
* Add test EncodeDecode64, fix some bugs
The functions mbedtls_base64_encode and mbedtls_base64_decode needs a
bit longer buffer than the resulting string actually will be.
* Remove check for empty data from TestProvider
It can be valid to get the hash of empty string.
* Add tests for CRC calculation
Two type of thests:
- compare the result of the CRC calculation to a known to be good
results,
- generate random data as message, calculate of it's CRC and append that
to the message, the CRC of this new data should be 0.
* Add test for hash algorithms
* Add includes in tests
* Remove the use of C++20 ranges
It seems that Apple Clang does not support range-based constrained
algorithms at this time.
* Replace encode16 implementation
To encode the zero bytes at the begining of the input vector, too.
2021-11-26 22:14:44 +01:00
|
|
|
mbedtls_base64_decode(nullptr, 0, &written, reinterpret_cast<const unsigned char *>(input.data()), input.size());
|
|
|
|
std::vector<u8> output(written, 0x00);
|
2021-02-02 23:11:23 +01:00
|
|
|
if (mbedtls_base64_decode(output.data(), output.size(), &written, reinterpret_cast<const unsigned char *>(input.data()), input.size()))
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2020-11-24 02:00:22 +01:00
|
|
|
|
Tests for the CRC and hash algorithms (#335)
* Update TEST_ASSERT to do nothing if condition is true
The TEST_ASSERT should not return if the condition is true, because:
- it prevents the usage of multiple TEST_ASSERT in a single test case,
- that behavior differs from how the assert in the standard library
works, and thus may give unexpected results.
Make the TEST_ASSERT to print an error message (with an formatted
optional user part) when it fails to make debugging easier.
* Fix some bugs in TestProvider, add unit tests
Use pointer-to-vector in TestProvider so writes can be tested, too.
* Add test EncodeDecode16, fix some encode16 bugs
The function mbedtls_mpi_write_string needs a bit longer buffer than the
resulting string actually will be.
Known bug: mbedtls_mpi_read_binary ingores initial null bytes
* Add test EncodeDecode64, fix some bugs
The functions mbedtls_base64_encode and mbedtls_base64_decode needs a
bit longer buffer than the resulting string actually will be.
* Remove check for empty data from TestProvider
It can be valid to get the hash of empty string.
* Add tests for CRC calculation
Two type of thests:
- compare the result of the CRC calculation to a known to be good
results,
- generate random data as message, calculate of it's CRC and append that
to the message, the CRC of this new data should be 0.
* Add test for hash algorithms
* Add includes in tests
* Remove the use of C++20 ranges
It seems that Apple Clang does not support range-based constrained
algorithms at this time.
* Replace encode16 implementation
To encode the zero bytes at the begining of the input vector, too.
2021-11-26 22:14:44 +01:00
|
|
|
output.resize(written);
|
|
|
|
|
2020-11-24 02:00:22 +01:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> encode64(const std::vector<u8> &input) {
|
|
|
|
|
2021-02-02 23:11:23 +01:00
|
|
|
size_t written = 0;
|
Tests for the CRC and hash algorithms (#335)
* Update TEST_ASSERT to do nothing if condition is true
The TEST_ASSERT should not return if the condition is true, because:
- it prevents the usage of multiple TEST_ASSERT in a single test case,
- that behavior differs from how the assert in the standard library
works, and thus may give unexpected results.
Make the TEST_ASSERT to print an error message (with an formatted
optional user part) when it fails to make debugging easier.
* Fix some bugs in TestProvider, add unit tests
Use pointer-to-vector in TestProvider so writes can be tested, too.
* Add test EncodeDecode16, fix some encode16 bugs
The function mbedtls_mpi_write_string needs a bit longer buffer than the
resulting string actually will be.
Known bug: mbedtls_mpi_read_binary ingores initial null bytes
* Add test EncodeDecode64, fix some bugs
The functions mbedtls_base64_encode and mbedtls_base64_decode needs a
bit longer buffer than the resulting string actually will be.
* Remove check for empty data from TestProvider
It can be valid to get the hash of empty string.
* Add tests for CRC calculation
Two type of thests:
- compare the result of the CRC calculation to a known to be good
results,
- generate random data as message, calculate of it's CRC and append that
to the message, the CRC of this new data should be 0.
* Add test for hash algorithms
* Add includes in tests
* Remove the use of C++20 ranges
It seems that Apple Clang does not support range-based constrained
algorithms at this time.
* Replace encode16 implementation
To encode the zero bytes at the begining of the input vector, too.
2021-11-26 22:14:44 +01:00
|
|
|
mbedtls_base64_encode(nullptr, 0, &written, reinterpret_cast<const unsigned char *>(input.data()), input.size());
|
|
|
|
|
|
|
|
std::vector<u8> output(written, 0x00);
|
2021-02-02 23:11:23 +01:00
|
|
|
if (mbedtls_base64_encode(output.data(), output.size(), &written, reinterpret_cast<const unsigned char *>(input.data()), input.size()))
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2020-11-24 02:00:22 +01:00
|
|
|
|
Tests for the CRC and hash algorithms (#335)
* Update TEST_ASSERT to do nothing if condition is true
The TEST_ASSERT should not return if the condition is true, because:
- it prevents the usage of multiple TEST_ASSERT in a single test case,
- that behavior differs from how the assert in the standard library
works, and thus may give unexpected results.
Make the TEST_ASSERT to print an error message (with an formatted
optional user part) when it fails to make debugging easier.
* Fix some bugs in TestProvider, add unit tests
Use pointer-to-vector in TestProvider so writes can be tested, too.
* Add test EncodeDecode16, fix some encode16 bugs
The function mbedtls_mpi_write_string needs a bit longer buffer than the
resulting string actually will be.
Known bug: mbedtls_mpi_read_binary ingores initial null bytes
* Add test EncodeDecode64, fix some bugs
The functions mbedtls_base64_encode and mbedtls_base64_decode needs a
bit longer buffer than the resulting string actually will be.
* Remove check for empty data from TestProvider
It can be valid to get the hash of empty string.
* Add tests for CRC calculation
Two type of thests:
- compare the result of the CRC calculation to a known to be good
results,
- generate random data as message, calculate of it's CRC and append that
to the message, the CRC of this new data should be 0.
* Add test for hash algorithms
* Add includes in tests
* Remove the use of C++20 ranges
It seems that Apple Clang does not support range-based constrained
algorithms at this time.
* Replace encode16 implementation
To encode the zero bytes at the begining of the input vector, too.
2021-11-26 22:14:44 +01:00
|
|
|
output.resize(written);
|
|
|
|
|
2020-11-24 02:00:22 +01:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2021-09-03 02:34:40 +02:00
|
|
|
std::vector<u8> decode16(const std::string &input) {
|
|
|
|
std::vector<u8> output(input.length() / 2, 0x00);
|
|
|
|
|
|
|
|
mbedtls_mpi ctx;
|
|
|
|
mbedtls_mpi_init(&ctx);
|
|
|
|
|
|
|
|
ON_SCOPE_EXIT { mbedtls_mpi_free(&ctx); };
|
|
|
|
|
|
|
|
if (mbedtls_mpi_read_string(&ctx, 16, input.c_str()))
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
if (mbedtls_mpi_write_binary(&ctx, output.data(), output.size()))
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string encode16(const std::vector<u8> &input) {
|
|
|
|
|
Tests for the CRC and hash algorithms (#335)
* Update TEST_ASSERT to do nothing if condition is true
The TEST_ASSERT should not return if the condition is true, because:
- it prevents the usage of multiple TEST_ASSERT in a single test case,
- that behavior differs from how the assert in the standard library
works, and thus may give unexpected results.
Make the TEST_ASSERT to print an error message (with an formatted
optional user part) when it fails to make debugging easier.
* Fix some bugs in TestProvider, add unit tests
Use pointer-to-vector in TestProvider so writes can be tested, too.
* Add test EncodeDecode16, fix some encode16 bugs
The function mbedtls_mpi_write_string needs a bit longer buffer than the
resulting string actually will be.
Known bug: mbedtls_mpi_read_binary ingores initial null bytes
* Add test EncodeDecode64, fix some bugs
The functions mbedtls_base64_encode and mbedtls_base64_decode needs a
bit longer buffer than the resulting string actually will be.
* Remove check for empty data from TestProvider
It can be valid to get the hash of empty string.
* Add tests for CRC calculation
Two type of thests:
- compare the result of the CRC calculation to a known to be good
results,
- generate random data as message, calculate of it's CRC and append that
to the message, the CRC of this new data should be 0.
* Add test for hash algorithms
* Add includes in tests
* Remove the use of C++20 ranges
It seems that Apple Clang does not support range-based constrained
algorithms at this time.
* Replace encode16 implementation
To encode the zero bytes at the begining of the input vector, too.
2021-11-26 22:14:44 +01:00
|
|
|
if (input.empty())
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2021-09-03 02:34:40 +02:00
|
|
|
|
Tests for the CRC and hash algorithms (#335)
* Update TEST_ASSERT to do nothing if condition is true
The TEST_ASSERT should not return if the condition is true, because:
- it prevents the usage of multiple TEST_ASSERT in a single test case,
- that behavior differs from how the assert in the standard library
works, and thus may give unexpected results.
Make the TEST_ASSERT to print an error message (with an formatted
optional user part) when it fails to make debugging easier.
* Fix some bugs in TestProvider, add unit tests
Use pointer-to-vector in TestProvider so writes can be tested, too.
* Add test EncodeDecode16, fix some encode16 bugs
The function mbedtls_mpi_write_string needs a bit longer buffer than the
resulting string actually will be.
Known bug: mbedtls_mpi_read_binary ingores initial null bytes
* Add test EncodeDecode64, fix some bugs
The functions mbedtls_base64_encode and mbedtls_base64_decode needs a
bit longer buffer than the resulting string actually will be.
* Remove check for empty data from TestProvider
It can be valid to get the hash of empty string.
* Add tests for CRC calculation
Two type of thests:
- compare the result of the CRC calculation to a known to be good
results,
- generate random data as message, calculate of it's CRC and append that
to the message, the CRC of this new data should be 0.
* Add test for hash algorithms
* Add includes in tests
* Remove the use of C++20 ranges
It seems that Apple Clang does not support range-based constrained
algorithms at this time.
* Replace encode16 implementation
To encode the zero bytes at the begining of the input vector, too.
2021-11-26 22:14:44 +01:00
|
|
|
std::string output(input.size() * 2, '\0');
|
|
|
|
|
2022-03-27 00:01:28 +01:00
|
|
|
for (size_t i = 0; i < input.size(); i++) {
|
2022-01-24 20:53:17 +01:00
|
|
|
output[2 * i + 0] = "0123456789ABCDEF"[input[i] / 16];
|
|
|
|
output[2 * i + 1] = "0123456789ABCDEF"[input[i] % 16];
|
Tests for the CRC and hash algorithms (#335)
* Update TEST_ASSERT to do nothing if condition is true
The TEST_ASSERT should not return if the condition is true, because:
- it prevents the usage of multiple TEST_ASSERT in a single test case,
- that behavior differs from how the assert in the standard library
works, and thus may give unexpected results.
Make the TEST_ASSERT to print an error message (with an formatted
optional user part) when it fails to make debugging easier.
* Fix some bugs in TestProvider, add unit tests
Use pointer-to-vector in TestProvider so writes can be tested, too.
* Add test EncodeDecode16, fix some encode16 bugs
The function mbedtls_mpi_write_string needs a bit longer buffer than the
resulting string actually will be.
Known bug: mbedtls_mpi_read_binary ingores initial null bytes
* Add test EncodeDecode64, fix some bugs
The functions mbedtls_base64_encode and mbedtls_base64_decode needs a
bit longer buffer than the resulting string actually will be.
* Remove check for empty data from TestProvider
It can be valid to get the hash of empty string.
* Add tests for CRC calculation
Two type of thests:
- compare the result of the CRC calculation to a known to be good
results,
- generate random data as message, calculate of it's CRC and append that
to the message, the CRC of this new data should be 0.
* Add test for hash algorithms
* Add includes in tests
* Remove the use of C++20 ranges
It seems that Apple Clang does not support range-based constrained
algorithms at this time.
* Replace encode16 implementation
To encode the zero bytes at the begining of the input vector, too.
2021-11-26 22:14:44 +01:00
|
|
|
}
|
2021-09-03 02:34:40 +02:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2022-08-01 13:20:20 +02:00
|
|
|
template<typename T>
|
|
|
|
static T safeLeftShift(T t, u32 shift) {
|
|
|
|
if (shift >= sizeof(t) * 8) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return t << shift;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static T decodeLeb128(const std::vector<u8> &bytes) {
|
|
|
|
T value = 0;
|
|
|
|
u32 shift = 0;
|
|
|
|
u8 b = 0;
|
|
|
|
for (u8 byte : bytes) {
|
|
|
|
b = byte;
|
|
|
|
value |= safeLeftShift(static_cast<T>(byte & 0x7F), shift);
|
|
|
|
shift += 7;
|
|
|
|
if ((byte & 0x80) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if constexpr(std::is_signed<T>::value) {
|
|
|
|
if ((b & 0x40) != 0) {
|
|
|
|
value |= safeLeftShift(~static_cast<T>(0), shift);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
u128 decodeUleb128(const std::vector<u8> &bytes) {
|
|
|
|
return decodeLeb128<u128>(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
i128 decodeSleb128(const std::vector<u8> &bytes) {
|
|
|
|
return decodeLeb128<i128>(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static std::vector<u8> encodeLeb128(T value) {
|
|
|
|
std::vector<u8> bytes;
|
|
|
|
u8 byte;
|
|
|
|
while (true) {
|
|
|
|
byte = value & 0x7F;
|
|
|
|
value >>= 7;
|
|
|
|
if constexpr(std::is_signed<T>::value) {
|
|
|
|
if (value == 0 && (byte & 0x40) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (value == -1 && (byte & 0x40) != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (value == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bytes.push_back(byte | 0x80);
|
|
|
|
}
|
|
|
|
bytes.push_back(byte);
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> encodeUleb128(u128 value) {
|
|
|
|
return encodeLeb128<u128>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<u8> encodeSleb128(i128 value) {
|
|
|
|
return encodeLeb128<i128>(value);
|
|
|
|
}
|
|
|
|
|
2021-02-04 00:21:53 +01:00
|
|
|
static std::vector<u8> aes(mbedtls_cipher_type_t type, mbedtls_operation_t operation, const std::vector<u8> &key, std::array<u8, 8> nonce, std::array<u8, 8> iv, const std::vector<u8> &input) {
|
|
|
|
std::vector<u8> output;
|
|
|
|
|
|
|
|
if (input.empty())
|
2022-01-24 20:53:17 +01:00
|
|
|
return {};
|
2022-03-27 00:01:28 +01:00
|
|
|
if (key.size() > 256)
|
|
|
|
return {};
|
2021-02-04 00:21:53 +01:00
|
|
|
|
2021-02-03 11:54:41 +01:00
|
|
|
mbedtls_cipher_context_t ctx;
|
2021-02-04 00:21:53 +01:00
|
|
|
auto cipherInfo = mbedtls_cipher_info_from_type(type);
|
2021-02-03 11:54:41 +01:00
|
|
|
|
|
|
|
|
2021-02-04 00:21:53 +01:00
|
|
|
mbedtls_cipher_setup(&ctx, cipherInfo);
|
2022-03-27 00:01:28 +01:00
|
|
|
mbedtls_cipher_setkey(&ctx, key.data(), static_cast<int>(key.size() * 8), operation);
|
2021-02-03 11:54:41 +01:00
|
|
|
|
|
|
|
std::array<u8, 16> nonceCounter = { 0 };
|
|
|
|
std::copy(nonce.begin(), nonce.end(), nonceCounter.begin());
|
|
|
|
std::copy(iv.begin(), iv.end(), nonceCounter.begin() + 8);
|
|
|
|
|
2021-08-17 17:28:33 +02:00
|
|
|
size_t outputSize = input.size() + mbedtls_cipher_get_block_size(&ctx);
|
2021-02-04 00:21:53 +01:00
|
|
|
output.resize(outputSize, 0x00);
|
2021-02-03 11:54:41 +01:00
|
|
|
mbedtls_cipher_crypt(&ctx, nonceCounter.data(), nonceCounter.size(), input.data(), input.size(), output.data(), &outputSize);
|
|
|
|
|
|
|
|
mbedtls_cipher_free(&ctx);
|
|
|
|
|
2021-02-04 00:21:53 +01:00
|
|
|
output.resize(input.size());
|
|
|
|
|
2021-02-03 11:54:41 +01:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2021-02-04 00:21:53 +01:00
|
|
|
std::vector<u8> aesDecrypt(AESMode mode, KeyLength keyLength, const std::vector<u8> &key, std::array<u8, 8> nonce, std::array<u8, 8> iv, const std::vector<u8> &input) {
|
|
|
|
switch (keyLength) {
|
2022-02-01 22:09:44 +01:00
|
|
|
case KeyLength::Key128Bits:
|
|
|
|
if (key.size() != 128 / 8) return {};
|
|
|
|
break;
|
|
|
|
case KeyLength::Key192Bits:
|
|
|
|
if (key.size() != 192 / 8) return {};
|
|
|
|
break;
|
|
|
|
case KeyLength::Key256Bits:
|
|
|
|
if (key.size() != 256 / 8) return {};
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return {};
|
2021-02-04 00:21:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_cipher_type_t type;
|
|
|
|
switch (mode) {
|
2022-02-01 22:09:44 +01:00
|
|
|
case AESMode::ECB:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_ECB;
|
|
|
|
break;
|
|
|
|
case AESMode::CBC:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_CBC;
|
|
|
|
break;
|
|
|
|
case AESMode::CFB128:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_CFB128;
|
|
|
|
break;
|
|
|
|
case AESMode::CTR:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_CTR;
|
|
|
|
break;
|
|
|
|
case AESMode::GCM:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_GCM;
|
|
|
|
break;
|
|
|
|
case AESMode::CCM:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_CCM;
|
|
|
|
break;
|
|
|
|
case AESMode::OFB:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_OFB;
|
|
|
|
break;
|
|
|
|
case AESMode::XTS:
|
|
|
|
type = MBEDTLS_CIPHER_AES_128_XTS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return {};
|
2021-02-04 00:21:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type = mbedtls_cipher_type_t(type + u8(keyLength));
|
|
|
|
|
|
|
|
return aes(type, MBEDTLS_DECRYPT, key, nonce, iv, input);
|
|
|
|
}
|
|
|
|
|
2021-10-26 17:21:48 +02:00
|
|
|
}
|