mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:37:46 +00:00
LibCrypto+LibTLS: Reformat everything
I have no idea how I'll squash _this_ one...
This commit is contained in:
parent
a1e1570552
commit
05e2c7d9cf
14 changed files with 1434 additions and 1426 deletions
|
@ -32,7 +32,8 @@
|
|||
namespace Crypto {
|
||||
|
||||
namespace ASN1 {
|
||||
enum class Kind {
|
||||
|
||||
enum class Kind {
|
||||
Eol,
|
||||
Boolean,
|
||||
Integer,
|
||||
|
@ -49,10 +50,10 @@ namespace ASN1 {
|
|||
Sequence,
|
||||
Set,
|
||||
SetOf
|
||||
};
|
||||
};
|
||||
|
||||
static StringView kind_name(Kind kind)
|
||||
{
|
||||
static StringView kind_name(Kind kind)
|
||||
{
|
||||
switch (kind) {
|
||||
case Kind::Eol:
|
||||
return "EndOfList";
|
||||
|
@ -89,23 +90,23 @@ namespace ASN1 {
|
|||
}
|
||||
|
||||
return "InvalidKind";
|
||||
}
|
||||
}
|
||||
|
||||
struct List {
|
||||
struct List {
|
||||
Kind kind;
|
||||
void* data;
|
||||
size_t size;
|
||||
bool used;
|
||||
List *prev, *next, *child, *parent;
|
||||
};
|
||||
};
|
||||
|
||||
static constexpr void set(List& list, Kind type, void* data, size_t size)
|
||||
{
|
||||
static constexpr void set(List& list, Kind type, void* data, size_t size)
|
||||
{
|
||||
list.kind = type;
|
||||
list.data = data;
|
||||
list.size = size;
|
||||
list.used = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -376,7 +376,7 @@ static bool der_decode_sequence(const u8* in, size_t in_length, ASN1::List* list
|
|||
return true;
|
||||
}
|
||||
|
||||
template <size_t element_count>
|
||||
template<size_t element_count>
|
||||
struct der_decode_sequence_many_base {
|
||||
constexpr void set(size_t index, ASN1::Kind kind, size_t size, void* data)
|
||||
{
|
||||
|
@ -399,10 +399,10 @@ protected:
|
|||
size_t m_in_length;
|
||||
};
|
||||
|
||||
template <size_t element_count>
|
||||
template<size_t element_count>
|
||||
struct der_decode_sequence_many : public der_decode_sequence_many_base<element_count> {
|
||||
|
||||
template <typename ElementType, typename... Args>
|
||||
template<typename ElementType, typename... Args>
|
||||
constexpr void construct(size_t index, ASN1::Kind kind, size_t size, ElementType data, Args... args)
|
||||
{
|
||||
der_decode_sequence_many_base<element_count>::set(index, kind, size, (void*)data);
|
||||
|
@ -414,7 +414,7 @@ struct der_decode_sequence_many : public der_decode_sequence_many_base<element_c
|
|||
ASSERT(index == element_count);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
template<typename... Args>
|
||||
constexpr der_decode_sequence_many(const u8* in, size_t in_length, Args... args)
|
||||
: der_decode_sequence_many_base<element_count>(in, in_length)
|
||||
{
|
||||
|
|
|
@ -38,15 +38,16 @@ constexpr static auto OPAD = 0x5c;
|
|||
|
||||
namespace Crypto {
|
||||
namespace Authentication {
|
||||
template <typename HashT>
|
||||
class HMAC {
|
||||
public:
|
||||
|
||||
template<typename HashT>
|
||||
class HMAC {
|
||||
public:
|
||||
using HashType = HashT;
|
||||
using TagType = typename HashType::DigestType;
|
||||
static constexpr size_t BlockSize = HashType::BlockSize;
|
||||
static constexpr size_t DigestSize = HashType::DigestSize;
|
||||
|
||||
template <typename KeyBufferType, typename... Args>
|
||||
template<typename KeyBufferType, typename... Args>
|
||||
HMAC(KeyBufferType key, Args... args)
|
||||
: m_inner_hasher(args...)
|
||||
, m_outer_hasher(args...)
|
||||
|
@ -96,7 +97,7 @@ namespace Authentication {
|
|||
return builder.build();
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
void derive_key(const u8* key, size_t length)
|
||||
{
|
||||
u8 v_key[BlockSize];
|
||||
|
@ -129,6 +130,7 @@ namespace Authentication {
|
|||
|
||||
HashType m_inner_hasher, m_outer_hasher;
|
||||
u8 m_key_data[BlockSize * 2];
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,9 +33,9 @@
|
|||
namespace Crypto {
|
||||
namespace Hash {
|
||||
|
||||
template <size_t BlockS, typename DigestT>
|
||||
class HashFunction {
|
||||
public:
|
||||
template<size_t BlockS, typename DigestT>
|
||||
class HashFunction {
|
||||
public:
|
||||
static constexpr auto BlockSize = BlockS / 8;
|
||||
static constexpr auto DigestSize = sizeof(DigestT);
|
||||
|
||||
|
@ -54,6 +54,6 @@ namespace Hash {
|
|||
virtual void reset() = 0;
|
||||
|
||||
virtual String class_name() const = 0;
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,8 +67,8 @@ static constexpr inline void round_4(u32& a, u32 b, u32 c, u32 d, u32 x, u32 s,
|
|||
namespace Crypto {
|
||||
namespace Hash {
|
||||
|
||||
void MD5::update(const u8* input, size_t length)
|
||||
{
|
||||
void MD5::update(const u8* input, size_t length)
|
||||
{
|
||||
auto index = (u32)(m_count[0] >> 3) & 0x3f;
|
||||
size_t offset { 0 };
|
||||
m_count[0] += (u32)length << 3;
|
||||
|
@ -87,18 +87,19 @@ namespace Hash {
|
|||
|
||||
index = 0;
|
||||
}
|
||||
|
||||
ASSERT(length < part_length || length - offset <= 64);
|
||||
m_buffer.overwrite(index, &input[offset], length - offset);
|
||||
}
|
||||
MD5::DigestType MD5::digest()
|
||||
{
|
||||
}
|
||||
MD5::DigestType MD5::digest()
|
||||
{
|
||||
auto digest = peek();
|
||||
reset();
|
||||
return digest;
|
||||
}
|
||||
}
|
||||
|
||||
MD5::DigestType MD5::peek()
|
||||
{
|
||||
MD5::DigestType MD5::peek()
|
||||
{
|
||||
DigestType digest;
|
||||
u8 bits[8];
|
||||
|
||||
|
@ -116,26 +117,26 @@ namespace Hash {
|
|||
encode(&m_A, digest.data, 4 * sizeof(m_A));
|
||||
|
||||
return digest;
|
||||
}
|
||||
}
|
||||
|
||||
void MD5::encode(const u32* from, u8* to, size_t length)
|
||||
{
|
||||
void MD5::encode(const u32* from, u8* to, size_t length)
|
||||
{
|
||||
for (size_t i = 0, j = 0; j < length; ++i, j += 4) {
|
||||
to[j] = (u8)(from[i] & 0xff);
|
||||
to[j + 1] = (u8)((from[i] >> 8) & 0xff);
|
||||
to[j + 2] = (u8)((from[i] >> 16) & 0xff);
|
||||
to[j + 3] = (u8)((from[i] >> 24) & 0xff);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MD5::decode(const u8* from, u32* to, size_t length)
|
||||
{
|
||||
void MD5::decode(const u8* from, u32* to, size_t length)
|
||||
{
|
||||
for (size_t i = 0, j = 0; j < length; ++i, j += 4)
|
||||
to[i] = (((u32)from[j]) | (((u32)from[j + 1]) << 8) | (((u32)from[j + 2]) << 16) | (((u32)from[j + 3]) << 24));
|
||||
}
|
||||
}
|
||||
|
||||
void MD5::transform(const u8* block)
|
||||
{
|
||||
void MD5::transform(const u8* block)
|
||||
{
|
||||
auto a = m_A;
|
||||
auto b = m_B;
|
||||
auto c = m_C;
|
||||
|
@ -218,7 +219,7 @@ namespace Hash {
|
|||
m_D += d;
|
||||
|
||||
__builtin_memset(x, 0, sizeof(x));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,43 +33,44 @@
|
|||
namespace Crypto {
|
||||
namespace Hash {
|
||||
|
||||
struct MD5Digest {
|
||||
struct MD5Digest {
|
||||
u8 data[16];
|
||||
};
|
||||
};
|
||||
|
||||
namespace MD5Constants {
|
||||
namespace MD5Constants {
|
||||
|
||||
constexpr u32 init_A = 0x67452301;
|
||||
constexpr u32 init_B = 0xefcdab89;
|
||||
constexpr u32 init_C = 0x98badcfe;
|
||||
constexpr u32 init_D = 0x10325476;
|
||||
constexpr u32 S11 = 7;
|
||||
constexpr u32 S12 = 12;
|
||||
constexpr u32 S13 = 17;
|
||||
constexpr u32 S14 = 22;
|
||||
constexpr u32 S21 = 5;
|
||||
constexpr u32 S22 = 9;
|
||||
constexpr u32 S23 = 14;
|
||||
constexpr u32 S24 = 20;
|
||||
constexpr u32 S31 = 4;
|
||||
constexpr u32 S32 = 11;
|
||||
constexpr u32 S33 = 16;
|
||||
constexpr u32 S34 = 23;
|
||||
constexpr u32 S41 = 6;
|
||||
constexpr u32 S42 = 10;
|
||||
constexpr u32 S43 = 15;
|
||||
constexpr u32 S44 = 21;
|
||||
constexpr u8 PADDING[] = {
|
||||
constexpr u32 init_A = 0x67452301;
|
||||
constexpr u32 init_B = 0xefcdab89;
|
||||
constexpr u32 init_C = 0x98badcfe;
|
||||
constexpr u32 init_D = 0x10325476;
|
||||
constexpr u32 S11 = 7;
|
||||
constexpr u32 S12 = 12;
|
||||
constexpr u32 S13 = 17;
|
||||
constexpr u32 S14 = 22;
|
||||
constexpr u32 S21 = 5;
|
||||
constexpr u32 S22 = 9;
|
||||
constexpr u32 S23 = 14;
|
||||
constexpr u32 S24 = 20;
|
||||
constexpr u32 S31 = 4;
|
||||
constexpr u32 S32 = 11;
|
||||
constexpr u32 S33 = 16;
|
||||
constexpr u32 S34 = 23;
|
||||
constexpr u32 S41 = 6;
|
||||
constexpr u32 S42 = 10;
|
||||
constexpr u32 S43 = 15;
|
||||
constexpr u32 S44 = 21;
|
||||
constexpr u8 PADDING[] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
class MD5 final : public HashFunction<512, MD5Digest> {
|
||||
public:
|
||||
}
|
||||
|
||||
class MD5 final : public HashFunction<512, MD5Digest> {
|
||||
public:
|
||||
MD5()
|
||||
{
|
||||
m_buffer = ByteBuffer::wrap(m_data_buffer, sizeof(m_data_buffer));
|
||||
|
@ -105,7 +106,7 @@ namespace Hash {
|
|||
__builtin_memset(m_data_buffer, 0, sizeof(m_data_buffer));
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
inline void transform(const u8*);
|
||||
|
||||
static void encode(const u32* from, u8* to, size_t length);
|
||||
|
@ -116,7 +117,7 @@ namespace Hash {
|
|||
ByteBuffer m_buffer;
|
||||
|
||||
u8 m_data_buffer[64];
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -29,24 +29,24 @@
|
|||
|
||||
namespace Crypto {
|
||||
namespace Hash {
|
||||
constexpr inline static auto ROTRIGHT(u32 a, size_t b) { return (a >> b) | (a << (32 - b)); }
|
||||
constexpr inline static auto CH(u32 x, u32 y, u32 z) { return (x & y) ^ (z & ~x); }
|
||||
constexpr inline static auto MAJ(u32 x, u32 y, u32 z) { return (x & y) ^ (x & z) ^ (y & z); }
|
||||
constexpr inline static auto EP0(u32 x) { return ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22); }
|
||||
constexpr inline static auto EP1(u32 x) { return ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25); }
|
||||
constexpr inline static auto SIGN0(u32 x) { return ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ (x >> 3); }
|
||||
constexpr inline static auto SIGN1(u32 x) { return ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ (x >> 10); }
|
||||
constexpr inline static auto ROTRIGHT(u32 a, size_t b) { return (a >> b) | (a << (32 - b)); }
|
||||
constexpr inline static auto CH(u32 x, u32 y, u32 z) { return (x & y) ^ (z & ~x); }
|
||||
constexpr inline static auto MAJ(u32 x, u32 y, u32 z) { return (x & y) ^ (x & z) ^ (y & z); }
|
||||
constexpr inline static auto EP0(u32 x) { return ROTRIGHT(x, 2) ^ ROTRIGHT(x, 13) ^ ROTRIGHT(x, 22); }
|
||||
constexpr inline static auto EP1(u32 x) { return ROTRIGHT(x, 6) ^ ROTRIGHT(x, 11) ^ ROTRIGHT(x, 25); }
|
||||
constexpr inline static auto SIGN0(u32 x) { return ROTRIGHT(x, 7) ^ ROTRIGHT(x, 18) ^ (x >> 3); }
|
||||
constexpr inline static auto SIGN1(u32 x) { return ROTRIGHT(x, 17) ^ ROTRIGHT(x, 19) ^ (x >> 10); }
|
||||
|
||||
constexpr inline static auto ROTRIGHT(u64 a, size_t b) { return (a >> b) | (a << (64 - b)); }
|
||||
constexpr inline static auto CH(u64 x, u64 y, u64 z) { return (x & y) ^ (z & ~x); }
|
||||
constexpr inline static auto MAJ(u64 x, u64 y, u64 z) { return (x & y) ^ (x & z) ^ (y & z); }
|
||||
constexpr inline static auto EP0(u64 x) { return ROTRIGHT(x, 28) ^ ROTRIGHT(x, 34) ^ ROTRIGHT(x, 39); }
|
||||
constexpr inline static auto EP1(u64 x) { return ROTRIGHT(x, 14) ^ ROTRIGHT(x, 18) ^ ROTRIGHT(x, 41); }
|
||||
constexpr inline static auto SIGN0(u64 x) { return ROTRIGHT(x, 1) ^ ROTRIGHT(x, 8) ^ (x >> 7); }
|
||||
constexpr inline static auto SIGN1(u64 x) { return ROTRIGHT(x, 19) ^ ROTRIGHT(x, 61) ^ (x >> 6); }
|
||||
constexpr inline static auto ROTRIGHT(u64 a, size_t b) { return (a >> b) | (a << (64 - b)); }
|
||||
constexpr inline static auto CH(u64 x, u64 y, u64 z) { return (x & y) ^ (z & ~x); }
|
||||
constexpr inline static auto MAJ(u64 x, u64 y, u64 z) { return (x & y) ^ (x & z) ^ (y & z); }
|
||||
constexpr inline static auto EP0(u64 x) { return ROTRIGHT(x, 28) ^ ROTRIGHT(x, 34) ^ ROTRIGHT(x, 39); }
|
||||
constexpr inline static auto EP1(u64 x) { return ROTRIGHT(x, 14) ^ ROTRIGHT(x, 18) ^ ROTRIGHT(x, 41); }
|
||||
constexpr inline static auto SIGN0(u64 x) { return ROTRIGHT(x, 1) ^ ROTRIGHT(x, 8) ^ (x >> 7); }
|
||||
constexpr inline static auto SIGN1(u64 x) { return ROTRIGHT(x, 19) ^ ROTRIGHT(x, 61) ^ (x >> 6); }
|
||||
|
||||
inline void SHA256::transform(const u8* data)
|
||||
{
|
||||
inline void SHA256::transform(const u8* data)
|
||||
{
|
||||
u32 m[64];
|
||||
|
||||
size_t i = 0;
|
||||
|
@ -84,10 +84,10 @@ namespace Hash {
|
|||
m_state[5] += f;
|
||||
m_state[6] += g;
|
||||
m_state[7] += h;
|
||||
}
|
||||
}
|
||||
|
||||
void SHA256::update(const u8* message, size_t length)
|
||||
{
|
||||
void SHA256::update(const u8* message, size_t length)
|
||||
{
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
if (m_data_length == BlockSize) {
|
||||
transform(m_data_buffer);
|
||||
|
@ -96,17 +96,17 @@ namespace Hash {
|
|||
}
|
||||
m_data_buffer[m_data_length++] = message[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SHA256::DigestType SHA256::digest()
|
||||
{
|
||||
SHA256::DigestType SHA256::digest()
|
||||
{
|
||||
auto digest = peek();
|
||||
reset();
|
||||
return digest;
|
||||
}
|
||||
}
|
||||
|
||||
SHA256::DigestType SHA256::peek()
|
||||
{
|
||||
SHA256::DigestType SHA256::peek()
|
||||
{
|
||||
DigestType digest;
|
||||
size_t i = m_data_length;
|
||||
|
||||
|
@ -151,10 +151,10 @@ namespace Hash {
|
|||
digest.data[i + 28] = (m_state[7] >> (24 - i * 8)) & 0x000000ff;
|
||||
}
|
||||
return digest;
|
||||
}
|
||||
}
|
||||
|
||||
inline void SHA512::transform(const u8* data)
|
||||
{
|
||||
inline void SHA512::transform(const u8* data)
|
||||
{
|
||||
u64 m[80];
|
||||
|
||||
size_t i = 0;
|
||||
|
@ -192,10 +192,10 @@ namespace Hash {
|
|||
m_state[5] += f;
|
||||
m_state[6] += g;
|
||||
m_state[7] += h;
|
||||
}
|
||||
}
|
||||
|
||||
void SHA512::update(const u8* message, size_t length)
|
||||
{
|
||||
void SHA512::update(const u8* message, size_t length)
|
||||
{
|
||||
for (size_t i = 0; i < length; ++i) {
|
||||
if (m_data_length == BlockSize) {
|
||||
transform(m_data_buffer);
|
||||
|
@ -204,17 +204,17 @@ namespace Hash {
|
|||
}
|
||||
m_data_buffer[m_data_length++] = message[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SHA512::DigestType SHA512::digest()
|
||||
{
|
||||
SHA512::DigestType SHA512::digest()
|
||||
{
|
||||
auto digest = peek();
|
||||
reset();
|
||||
return digest;
|
||||
}
|
||||
}
|
||||
|
||||
SHA512::DigestType SHA512::peek()
|
||||
{
|
||||
SHA512::DigestType SHA512::peek()
|
||||
{
|
||||
DigestType digest;
|
||||
size_t i = m_data_length;
|
||||
|
||||
|
@ -259,6 +259,6 @@ namespace Hash {
|
|||
digest.data[i + 56] = (m_state[7] >> (56 - i * 8)) & 0x000000ff;
|
||||
}
|
||||
return digest;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,8 +33,8 @@
|
|||
namespace Crypto {
|
||||
namespace Hash {
|
||||
|
||||
namespace SHA256Constants {
|
||||
constexpr static u32 RoundConstants[64] {
|
||||
namespace SHA256Constants {
|
||||
constexpr static u32 RoundConstants[64] {
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
||||
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
||||
|
@ -51,16 +51,16 @@ namespace Hash {
|
|||
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
||||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
||||
};
|
||||
};
|
||||
|
||||
constexpr static u32 InitializationHashes[8] = {
|
||||
constexpr static u32 InitializationHashes[8] = {
|
||||
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
||||
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
namespace SHA512Constants {
|
||||
constexpr static u64 RoundConstants[80] {
|
||||
namespace SHA512Constants {
|
||||
constexpr static u64 RoundConstants[80] {
|
||||
0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538,
|
||||
0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe,
|
||||
0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
|
||||
|
@ -77,22 +77,22 @@ namespace Hash {
|
|||
0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba, 0x0a637dc5a2c898a6,
|
||||
0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
|
||||
0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817
|
||||
};
|
||||
};
|
||||
|
||||
constexpr static u64 InitializationHashes[8] = {
|
||||
constexpr static u64 InitializationHashes[8] = {
|
||||
0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1,
|
||||
0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
template <size_t Bytes>
|
||||
struct SHA2Digest {
|
||||
template<size_t Bytes>
|
||||
struct SHA2Digest {
|
||||
u8 data[Bytes];
|
||||
};
|
||||
};
|
||||
|
||||
// FIXME: I want template<size_t BlockSize> but the compiler gets confused
|
||||
class SHA256 final : public HashFunction<512, SHA2Digest<256 / 8>> {
|
||||
public:
|
||||
// FIXME: I want template<size_t BlockSize> but the compiler gets confused
|
||||
class SHA256 final : public HashFunction<512, SHA2Digest<256 / 8>> {
|
||||
public:
|
||||
SHA256()
|
||||
{
|
||||
reset();
|
||||
|
@ -131,7 +131,7 @@ namespace Hash {
|
|||
m_state[i] = SHA256Constants::InitializationHashes[i];
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
inline void transform(const u8*);
|
||||
|
||||
u8 m_data_buffer[BlockSize];
|
||||
|
@ -142,10 +142,10 @@ namespace Hash {
|
|||
|
||||
constexpr static auto FinalBlockDataSize = BlockSize - 8;
|
||||
constexpr static auto Rounds = 64;
|
||||
};
|
||||
};
|
||||
|
||||
class SHA512 final : public HashFunction<1024, SHA2Digest<512 / 8>> {
|
||||
public:
|
||||
class SHA512 final : public HashFunction<1024, SHA2Digest<512 / 8>> {
|
||||
public:
|
||||
SHA512()
|
||||
{
|
||||
reset();
|
||||
|
@ -184,7 +184,7 @@ namespace Hash {
|
|||
m_state[i] = SHA512Constants::InitializationHashes[i];
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
inline void transform(const u8*);
|
||||
|
||||
u8 m_data_buffer[BlockSize];
|
||||
|
@ -195,7 +195,7 @@ namespace Hash {
|
|||
|
||||
constexpr static auto FinalBlockDataSize = BlockSize - 8;
|
||||
constexpr static auto Rounds = 80;
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,8 +32,9 @@
|
|||
|
||||
namespace Crypto {
|
||||
namespace NumberTheory {
|
||||
static auto ModularInverse(const UnsignedBigInteger& a_, const UnsignedBigInteger& b) -> UnsignedBigInteger
|
||||
{
|
||||
|
||||
static auto ModularInverse(const UnsignedBigInteger& a_, const UnsignedBigInteger& b) -> UnsignedBigInteger
|
||||
{
|
||||
if (b == 1)
|
||||
return { 1 };
|
||||
|
||||
|
@ -69,10 +70,10 @@ namespace NumberTheory {
|
|||
}
|
||||
}
|
||||
return x.divide(b).remainder;
|
||||
}
|
||||
}
|
||||
|
||||
static auto ModularPower(const UnsignedBigInteger& b, const UnsignedBigInteger& e, const UnsignedBigInteger& m) -> UnsignedBigInteger
|
||||
{
|
||||
static auto ModularPower(const UnsignedBigInteger& b, const UnsignedBigInteger& e, const UnsignedBigInteger& m) -> UnsignedBigInteger
|
||||
{
|
||||
if (m == 1)
|
||||
return 0;
|
||||
|
||||
|
@ -91,10 +92,10 @@ namespace NumberTheory {
|
|||
base = base.multiply(base).divide(m).remainder;
|
||||
}
|
||||
return exp;
|
||||
}
|
||||
}
|
||||
|
||||
static auto GCD(const UnsignedBigInteger& a, const UnsignedBigInteger& b) -> UnsignedBigInteger
|
||||
{
|
||||
static auto GCD(const UnsignedBigInteger& a, const UnsignedBigInteger& b) -> UnsignedBigInteger
|
||||
{
|
||||
UnsignedBigInteger a_ { a }, b_ { b };
|
||||
for (;;) {
|
||||
if (a_ == 0)
|
||||
|
@ -104,10 +105,10 @@ namespace NumberTheory {
|
|||
return a_;
|
||||
a_ = a_.divide(b_).remainder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static auto LCM(const UnsignedBigInteger& a, const UnsignedBigInteger& b) -> UnsignedBigInteger
|
||||
{
|
||||
static auto LCM(const UnsignedBigInteger& a, const UnsignedBigInteger& b) -> UnsignedBigInteger
|
||||
{
|
||||
auto temp = GCD(a, b);
|
||||
|
||||
auto div = a.divide(temp);
|
||||
|
@ -116,11 +117,11 @@ namespace NumberTheory {
|
|||
dbg() << "quot: " << div.quotient << " rem: " << div.remainder;
|
||||
#endif
|
||||
return temp == 0 ? 0 : (a.divide(temp).quotient.multiply(b));
|
||||
}
|
||||
}
|
||||
|
||||
template <size_t test_count>
|
||||
static bool MR_primality_test(UnsignedBigInteger n, const Vector<UnsignedBigInteger, test_count>& tests)
|
||||
{
|
||||
template<size_t test_count>
|
||||
static bool MR_primality_test(UnsignedBigInteger n, const Vector<UnsignedBigInteger, test_count>& tests)
|
||||
{
|
||||
auto prev = n.sub({ 1 });
|
||||
auto b = prev;
|
||||
auto r = 0;
|
||||
|
@ -153,10 +154,10 @@ namespace NumberTheory {
|
|||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static UnsignedBigInteger random_number(const UnsignedBigInteger& min, const UnsignedBigInteger& max)
|
||||
{
|
||||
static UnsignedBigInteger random_number(const UnsignedBigInteger& min, const UnsignedBigInteger& max)
|
||||
{
|
||||
ASSERT(min < max);
|
||||
auto range = max.minus(min);
|
||||
UnsignedBigInteger base;
|
||||
|
@ -170,10 +171,10 @@ namespace NumberTheory {
|
|||
}
|
||||
UnsignedBigInteger offset { move(vec) };
|
||||
return offset.add(min);
|
||||
}
|
||||
}
|
||||
|
||||
static bool is_probably_prime(const UnsignedBigInteger& p)
|
||||
{
|
||||
static bool is_probably_prime(const UnsignedBigInteger& p)
|
||||
{
|
||||
if (p == 2 || p == 3 || p == 5)
|
||||
return true;
|
||||
if (p < 49)
|
||||
|
@ -185,10 +186,10 @@ namespace NumberTheory {
|
|||
tests.append(random_number(seven, p.sub(2)));
|
||||
|
||||
return MR_primality_test(p, tests);
|
||||
}
|
||||
}
|
||||
|
||||
static UnsignedBigInteger random_big_prime(size_t bits)
|
||||
{
|
||||
static UnsignedBigInteger random_big_prime(size_t bits)
|
||||
{
|
||||
ASSERT(bits >= 33);
|
||||
UnsignedBigInteger min = UnsignedBigInteger::from_base10("6074001000").shift_left(bits - 33);
|
||||
UnsignedBigInteger max = UnsignedBigInteger { 1 }.shift_left(bits).sub(1);
|
||||
|
@ -197,6 +198,7 @@ namespace NumberTheory {
|
|||
if (is_probably_prime(p))
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,15 +31,15 @@
|
|||
namespace Crypto {
|
||||
namespace PK {
|
||||
|
||||
enum class VerificationConsistency {
|
||||
enum class VerificationConsistency {
|
||||
Consistent,
|
||||
Inconsistent
|
||||
};
|
||||
};
|
||||
|
||||
template <typename HashFunction>
|
||||
class Code {
|
||||
public:
|
||||
template <typename... Args>
|
||||
template<typename HashFunction>
|
||||
class Code {
|
||||
public:
|
||||
template<typename... Args>
|
||||
Code(Args... args)
|
||||
: m_hasher(args...)
|
||||
{
|
||||
|
@ -51,9 +51,9 @@ namespace PK {
|
|||
const HashFunction& hasher() const { return m_hasher; }
|
||||
HashFunction& hasher() { return m_hasher; }
|
||||
|
||||
protected:
|
||||
protected:
|
||||
HashFunction m_hasher;
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,10 +33,10 @@ static constexpr u8 zeros[] { 0, 0, 0, 0, 0, 0, 0, 0 };
|
|||
namespace Crypto {
|
||||
namespace PK {
|
||||
|
||||
template <typename HashFunction, size_t SaltSize>
|
||||
class EMSA_PSS : public Code<HashFunction> {
|
||||
public:
|
||||
template <typename... Args>
|
||||
template<typename HashFunction, size_t SaltSize>
|
||||
class EMSA_PSS : public Code<HashFunction> {
|
||||
public:
|
||||
template<typename... Args>
|
||||
EMSA_PSS(Args... args)
|
||||
: Code<HashFunction>(args...)
|
||||
{
|
||||
|
@ -134,9 +134,10 @@ namespace PK {
|
|||
DB[0] &= 0xff >> (8 - length_to_check);
|
||||
|
||||
auto check_octets = emsg.size() - HashFunction::DigestSize - SaltLength - 2;
|
||||
for (size_t i = 0; i < check_octets; ++i)
|
||||
for (size_t i = 0; i < check_octets; ++i) {
|
||||
if (DB[i])
|
||||
return VerificationConsistency::Inconsistent;
|
||||
}
|
||||
|
||||
if (DB[check_octets + 1] != 0x01)
|
||||
return VerificationConsistency::Inconsistent;
|
||||
|
@ -170,10 +171,10 @@ namespace PK {
|
|||
out.overwrite(0, T.data(), length);
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
u8 m_data_buffer[8 + HashFunction::DigestSize + SaltLength];
|
||||
ByteBuffer m_buffer;
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,10 +32,10 @@
|
|||
namespace Crypto {
|
||||
namespace PK {
|
||||
|
||||
// FIXME: Fixing name up for grabs
|
||||
template <typename PrivKeyT, typename PubKeyT>
|
||||
class PKSystem {
|
||||
public:
|
||||
// FIXME: Fixing name up for grabs
|
||||
template<typename PrivKeyT, typename PubKeyT>
|
||||
class PKSystem {
|
||||
public:
|
||||
using PublicKeyType = PubKeyT;
|
||||
using PrivateKeyType = PrivKeyT;
|
||||
|
||||
|
@ -59,10 +59,10 @@ namespace PK {
|
|||
|
||||
virtual size_t output_size() const = 0;
|
||||
|
||||
protected:
|
||||
protected:
|
||||
PublicKeyType m_public_key;
|
||||
PrivateKeyType m_private_key;
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,8 +32,8 @@
|
|||
namespace Crypto {
|
||||
namespace PK {
|
||||
|
||||
RSA::KeyPairType RSA::parse_rsa_key(const ByteBuffer& in)
|
||||
{
|
||||
RSA::KeyPairType RSA::parse_rsa_key(const ByteBuffer& in)
|
||||
{
|
||||
// we are going to assign to at least one of these
|
||||
KeyPairType keypair;
|
||||
// TODO: move ASN parsing logic out
|
||||
|
@ -110,10 +110,10 @@ namespace PK {
|
|||
// it's a broken public key
|
||||
keypair.public_key.set(n, 65537);
|
||||
return keypair;
|
||||
}
|
||||
}
|
||||
|
||||
void RSA::encrypt(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
void RSA::encrypt(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
dbg() << "in size: " << in.size();
|
||||
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
|
||||
if (!(in_integer < m_public_key.modulus())) {
|
||||
|
@ -126,10 +126,10 @@ namespace PK {
|
|||
// FIXME: We should probably not do this...
|
||||
if (size != out.size())
|
||||
out = out.slice(out.size() - size, size);
|
||||
}
|
||||
}
|
||||
|
||||
void RSA::decrypt(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
void RSA::decrypt(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
// FIXME: Actually use the private key properly
|
||||
|
||||
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
|
||||
|
@ -142,26 +142,26 @@ namespace PK {
|
|||
for (auto i = size; i < aligned_size; ++i)
|
||||
out[out.size() - i - 1] = 0; // zero the non-aligned values
|
||||
out = out.slice(out.size() - aligned_size, aligned_size);
|
||||
}
|
||||
}
|
||||
|
||||
void RSA::sign(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
void RSA::sign(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
|
||||
auto exp = NumberTheory::ModularPower(in_integer, m_private_key.private_exponent(), m_private_key.modulus());
|
||||
auto size = exp.export_data(out);
|
||||
out = out.slice(out.size() - size, size);
|
||||
}
|
||||
}
|
||||
|
||||
void RSA::verify(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
void RSA::verify(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
auto in_integer = UnsignedBigInteger::import_data(in.data(), in.size());
|
||||
auto exp = NumberTheory::ModularPower(in_integer, m_public_key.public_exponent(), m_public_key.modulus());
|
||||
auto size = exp.export_data(out);
|
||||
out = out.slice(out.size() - size, size);
|
||||
}
|
||||
}
|
||||
|
||||
void RSA::import_private_key(const ByteBuffer& buffer, bool pem)
|
||||
{
|
||||
void RSA::import_private_key(const ByteBuffer& buffer, bool pem)
|
||||
{
|
||||
// so gods help me, I hate DER
|
||||
auto decoded_buffer = pem ? decode_pem(buffer) : buffer;
|
||||
auto key = parse_rsa_key(decoded_buffer);
|
||||
|
@ -170,10 +170,10 @@ namespace PK {
|
|||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
m_private_key = key.private_key;
|
||||
}
|
||||
}
|
||||
|
||||
void RSA::import_public_key(const ByteBuffer& buffer, bool pem)
|
||||
{
|
||||
void RSA::import_public_key(const ByteBuffer& buffer, bool pem)
|
||||
{
|
||||
// so gods help me, I hate DER
|
||||
auto decoded_buffer = pem ? decode_pem(buffer) : buffer;
|
||||
auto key = parse_rsa_key(decoded_buffer);
|
||||
|
@ -182,11 +182,11 @@ namespace PK {
|
|||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
m_public_key = key.public_key;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename HashFunction>
|
||||
void RSA_EMSA_PSS<HashFunction>::sign(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
template<typename HashFunction>
|
||||
void RSA_EMSA_PSS<HashFunction>::sign(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
// -- encode via EMSA_PSS
|
||||
auto mod_bits = m_rsa.private_key().modulus().trimmed_length() * sizeof(u32) * 8;
|
||||
|
||||
|
@ -196,11 +196,11 @@ namespace PK {
|
|||
|
||||
// -- sign via RSA
|
||||
m_rsa.sign(EM_buf, out);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename HashFunction>
|
||||
VerificationConsistency RSA_EMSA_PSS<HashFunction>::verify(const ByteBuffer& in)
|
||||
{
|
||||
template<typename HashFunction>
|
||||
VerificationConsistency RSA_EMSA_PSS<HashFunction>::verify(const ByteBuffer& in)
|
||||
{
|
||||
auto mod_bytes = m_rsa.public_key().modulus().trimmed_length() * sizeof(u32);
|
||||
if (in.size() != mod_bytes)
|
||||
return VerificationConsistency::Inconsistent;
|
||||
|
@ -213,10 +213,10 @@ namespace PK {
|
|||
|
||||
// -- verify via EMSA_PSS
|
||||
return m_emsa_pss.verify(in, EM, mod_bytes * 8 - 1);
|
||||
}
|
||||
}
|
||||
|
||||
void RSA_PKCS1_EME::encrypt(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
void RSA_PKCS1_EME::encrypt(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
auto mod_len = (m_public_key.modulus().trimmed_length() * sizeof(u32) * 8 + 7) / 8;
|
||||
dbg() << "key size: " << mod_len;
|
||||
if (in.size() > mod_len - 11) {
|
||||
|
@ -244,9 +244,9 @@ namespace PK {
|
|||
dbg() << "padded output size: " << 3 + ps_length + in.size() << " buffer size: " << out.size();
|
||||
|
||||
RSA::encrypt(out, out);
|
||||
}
|
||||
void RSA_PKCS1_EME::decrypt(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
}
|
||||
void RSA_PKCS1_EME::decrypt(const ByteBuffer& in, ByteBuffer& out)
|
||||
{
|
||||
auto mod_len = (m_public_key.modulus().trimmed_length() * sizeof(u32) * 8 + 7) / 8;
|
||||
if (in.size() != mod_len) {
|
||||
dbg() << "decryption error: wrong amount of data: " << in.size();
|
||||
|
@ -289,15 +289,15 @@ namespace PK {
|
|||
}
|
||||
|
||||
out = out.slice(offset, out.size() - offset);
|
||||
}
|
||||
}
|
||||
|
||||
void RSA_PKCS1_EME::sign(const ByteBuffer&, ByteBuffer&)
|
||||
{
|
||||
void RSA_PKCS1_EME::sign(const ByteBuffer&, ByteBuffer&)
|
||||
{
|
||||
dbg() << "FIXME: RSA_PKCS_EME::sign";
|
||||
}
|
||||
void RSA_PKCS1_EME::verify(const ByteBuffer&, ByteBuffer&)
|
||||
{
|
||||
}
|
||||
void RSA_PKCS1_EME::verify(const ByteBuffer&, ByteBuffer&)
|
||||
{
|
||||
dbg() << "FIXME: RSA_PKCS_EME::verify";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,9 +34,9 @@
|
|||
|
||||
namespace Crypto {
|
||||
namespace PK {
|
||||
template <typename Integer = u64>
|
||||
class RSAPublicKey {
|
||||
public:
|
||||
template<typename Integer = u64>
|
||||
class RSAPublicKey {
|
||||
public:
|
||||
RSAPublicKey(const Integer& n, const Integer& e)
|
||||
: m_modulus(n)
|
||||
, m_public_exponent(e)
|
||||
|
@ -63,15 +63,15 @@ namespace PK {
|
|||
m_length = (n.trimmed_length() * sizeof(u32));
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
Integer m_modulus;
|
||||
Integer m_public_exponent;
|
||||
size_t m_length { 0 };
|
||||
};
|
||||
};
|
||||
|
||||
template <typename Integer = UnsignedBigInteger>
|
||||
class RSAPrivateKey {
|
||||
public:
|
||||
template<typename Integer = UnsignedBigInteger>
|
||||
class RSAPrivateKey {
|
||||
public:
|
||||
RSAPrivateKey(const Integer& n, const Integer& d, const Integer& e)
|
||||
: m_modulus(n)
|
||||
, m_private_exponent(d)
|
||||
|
@ -98,25 +98,25 @@ namespace PK {
|
|||
m_length = (n.length() * sizeof(u32));
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
Integer m_modulus;
|
||||
Integer m_private_exponent;
|
||||
Integer m_public_exponent;
|
||||
size_t m_length { 0 };
|
||||
};
|
||||
};
|
||||
|
||||
template <typename PubKey, typename PrivKey>
|
||||
struct RSAKeyPair {
|
||||
template<typename PubKey, typename PrivKey>
|
||||
struct RSAKeyPair {
|
||||
PubKey public_key;
|
||||
PrivKey private_key;
|
||||
};
|
||||
};
|
||||
|
||||
using IntegerType = UnsignedBigInteger;
|
||||
class RSA : public PKSystem<RSAPrivateKey<IntegerType>, RSAPublicKey<IntegerType>> {
|
||||
template <typename T>
|
||||
using IntegerType = UnsignedBigInteger;
|
||||
class RSA : public PKSystem<RSAPrivateKey<IntegerType>, RSAPublicKey<IntegerType>> {
|
||||
template<typename T>
|
||||
friend class RSA_EMSA_PSS;
|
||||
|
||||
public:
|
||||
public:
|
||||
using KeyPairType = RSAKeyPair<PublicKeyType, PrivateKeyType>;
|
||||
|
||||
static KeyPairType parse_rsa_key(const ByteBuffer&);
|
||||
|
@ -192,11 +192,11 @@ namespace PK {
|
|||
|
||||
const PrivateKeyType& private_key() const { return m_private_key; }
|
||||
const PublicKeyType& public_key() const { return m_public_key; }
|
||||
};
|
||||
};
|
||||
|
||||
template <typename HashFunction>
|
||||
class RSA_EMSA_PSS {
|
||||
public:
|
||||
template<typename HashFunction>
|
||||
class RSA_EMSA_PSS {
|
||||
public:
|
||||
RSA_EMSA_PSS(RSA& rsa)
|
||||
: m_rsa(rsa)
|
||||
{
|
||||
|
@ -205,15 +205,15 @@ namespace PK {
|
|||
void sign(const ByteBuffer& in, ByteBuffer& out);
|
||||
VerificationConsistency verify(const ByteBuffer& in);
|
||||
|
||||
private:
|
||||
private:
|
||||
EMSA_PSS<HashFunction, HashFunction::DigestSize> m_emsa_pss;
|
||||
RSA m_rsa;
|
||||
};
|
||||
};
|
||||
|
||||
class RSA_PKCS1_EME : public RSA {
|
||||
public:
|
||||
class RSA_PKCS1_EME : public RSA {
|
||||
public:
|
||||
// forward all constructions to RSA
|
||||
template <typename... Args>
|
||||
template<typename... Args>
|
||||
RSA_PKCS1_EME(Args... args)
|
||||
: RSA(args...)
|
||||
{
|
||||
|
@ -229,6 +229,6 @@ namespace PK {
|
|||
|
||||
virtual String class_name() const override { return "RSA_PKCS1-EME"; }
|
||||
virtual size_t output_size() const override { return m_public_key.length(); }
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue