mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:57:45 +00:00
LibCrypto: Move each subsection into its own namespace
This commit is contained in:
parent
96dd7c2996
commit
4f89a377a4
9 changed files with 3256 additions and 3228 deletions
|
@ -28,395 +28,399 @@
|
|||
#include <LibCrypto/Cipher/AES.h>
|
||||
|
||||
namespace Crypto {
|
||||
namespace Cipher {
|
||||
|
||||
template <typename T>
|
||||
constexpr u32 get_key(T pt)
|
||||
{
|
||||
return ((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3]);
|
||||
}
|
||||
|
||||
constexpr void swap_keys(u32* keys, size_t i, size_t j)
|
||||
{
|
||||
u32 temp = keys[i];
|
||||
keys[i] = keys[j];
|
||||
keys[j] = temp;
|
||||
}
|
||||
|
||||
String AESCipherBlock::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
for (size_t i = 0; i < BLOCK_SIZE / 8; ++i)
|
||||
builder.appendf("%02x", m_data[i]);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
String AESCipherKey::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
for (size_t i = 0; i < (rounds() + 1) * 4; ++i)
|
||||
builder.appendf("%02x", m_rd_keys[i]);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
void AESCipherKey::expand_encrypt_key(const StringView& user_key, size_t bits)
|
||||
{
|
||||
u32* round_key;
|
||||
u32 temp;
|
||||
size_t i { 0 };
|
||||
|
||||
ASSERT(!user_key.is_null());
|
||||
ASSERT(is_valid_key_size(bits));
|
||||
|
||||
round_key = round_keys();
|
||||
|
||||
if (bits == 128) {
|
||||
m_rounds = 10;
|
||||
} else if (bits == 192) {
|
||||
m_rounds = 12;
|
||||
} else {
|
||||
m_rounds = 14;
|
||||
template <typename T>
|
||||
constexpr u32 get_key(T pt)
|
||||
{
|
||||
return ((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] << 8) ^ ((u32)(pt)[3]);
|
||||
}
|
||||
|
||||
round_key[0] = get_key(user_key.substring_view(0, 4).characters_without_null_termination());
|
||||
round_key[1] = get_key(user_key.substring_view(4, 4).characters_without_null_termination());
|
||||
round_key[2] = get_key(user_key.substring_view(8, 4).characters_without_null_termination());
|
||||
round_key[3] = get_key(user_key.substring_view(12, 4).characters_without_null_termination());
|
||||
if (bits == 128) {
|
||||
for (;;) {
|
||||
temp = round_key[3];
|
||||
// clang-format off
|
||||
constexpr void swap_keys(u32* keys, size_t i, size_t j)
|
||||
{
|
||||
u32 temp = keys[i];
|
||||
keys[i] = keys[j];
|
||||
keys[j] = temp;
|
||||
}
|
||||
|
||||
String AESCipherBlock::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
for (size_t i = 0; i < BLOCK_SIZE / 8; ++i)
|
||||
builder.appendf("%02x", m_data[i]);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
String AESCipherKey::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
for (size_t i = 0; i < (rounds() + 1) * 4; ++i)
|
||||
builder.appendf("%02x", m_rd_keys[i]);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
void AESCipherKey::expand_encrypt_key(const StringView& user_key, size_t bits)
|
||||
{
|
||||
u32* round_key;
|
||||
u32 temp;
|
||||
size_t i { 0 };
|
||||
|
||||
ASSERT(!user_key.is_null());
|
||||
ASSERT(is_valid_key_size(bits));
|
||||
|
||||
round_key = round_keys();
|
||||
|
||||
if (bits == 128) {
|
||||
m_rounds = 10;
|
||||
} else if (bits == 192) {
|
||||
m_rounds = 12;
|
||||
} else {
|
||||
m_rounds = 14;
|
||||
}
|
||||
|
||||
round_key[0] = get_key(user_key.substring_view(0, 4).characters_without_null_termination());
|
||||
round_key[1] = get_key(user_key.substring_view(4, 4).characters_without_null_termination());
|
||||
round_key[2] = get_key(user_key.substring_view(8, 4).characters_without_null_termination());
|
||||
round_key[3] = get_key(user_key.substring_view(12, 4).characters_without_null_termination());
|
||||
if (bits == 128) {
|
||||
for (;;) {
|
||||
temp = round_key[3];
|
||||
// clang-format off
|
||||
round_key[4] = round_key[0] ^
|
||||
(Tables::Encode2[(temp >> 16) & 0xff] & 0xff000000) ^
|
||||
(Tables::Encode3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
||||
(Tables::Encode0[(temp ) & 0xff] & 0x0000ff00) ^
|
||||
(Tables::Encode1[(temp >> 24) ] & 0x000000ff) ^ Tables::RCON[i];
|
||||
// clang-format on
|
||||
round_key[5] = round_key[1] ^ round_key[4];
|
||||
round_key[6] = round_key[2] ^ round_key[5];
|
||||
round_key[7] = round_key[3] ^ round_key[6];
|
||||
++i;
|
||||
if (i == 10)
|
||||
break;
|
||||
round_key += 4;
|
||||
(AESTables::Encode2[(temp >> 16) & 0xff] & 0xff000000) ^
|
||||
(AESTables::Encode3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
||||
(AESTables::Encode0[(temp ) & 0xff] & 0x0000ff00) ^
|
||||
(AESTables::Encode1[(temp >> 24) ] & 0x000000ff) ^ AESTables::RCON[i];
|
||||
// clang-format on
|
||||
round_key[5] = round_key[1] ^ round_key[4];
|
||||
round_key[6] = round_key[2] ^ round_key[5];
|
||||
round_key[7] = round_key[3] ^ round_key[6];
|
||||
++i;
|
||||
if (i == 10)
|
||||
break;
|
||||
round_key += 4;
|
||||
}
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
round_key[4] = get_key(user_key.substring_view(16, 4).characters_without_null_termination());
|
||||
round_key[5] = get_key(user_key.substring_view(20, 4).characters_without_null_termination());
|
||||
if (bits == 192) {
|
||||
for (;;) {
|
||||
temp = round_key[5];
|
||||
// clang-format off
|
||||
round_key[4] = get_key(user_key.substring_view(16, 4).characters_without_null_termination());
|
||||
round_key[5] = get_key(user_key.substring_view(20, 4).characters_without_null_termination());
|
||||
if (bits == 192) {
|
||||
for (;;) {
|
||||
temp = round_key[5];
|
||||
// clang-format off
|
||||
round_key[6] = round_key[0] ^
|
||||
(Tables::Encode2[(temp >> 16) & 0xff] & 0xff000000) ^
|
||||
(Tables::Encode3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
||||
(Tables::Encode0[(temp ) & 0xff] & 0x0000ff00) ^
|
||||
(Tables::Encode1[(temp >> 24) ] & 0x000000ff) ^ Tables::RCON[i];
|
||||
// clang-format on
|
||||
round_key[7] = round_key[1] ^ round_key[6];
|
||||
round_key[8] = round_key[2] ^ round_key[7];
|
||||
round_key[9] = round_key[3] ^ round_key[8];
|
||||
(AESTables::Encode2[(temp >> 16) & 0xff] & 0xff000000) ^
|
||||
(AESTables::Encode3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
||||
(AESTables::Encode0[(temp ) & 0xff] & 0x0000ff00) ^
|
||||
(AESTables::Encode1[(temp >> 24) ] & 0x000000ff) ^ AESTables::RCON[i];
|
||||
// clang-format on
|
||||
round_key[7] = round_key[1] ^ round_key[6];
|
||||
round_key[8] = round_key[2] ^ round_key[7];
|
||||
round_key[9] = round_key[3] ^ round_key[8];
|
||||
|
||||
++i;
|
||||
if (i == 8)
|
||||
break;
|
||||
++i;
|
||||
if (i == 8)
|
||||
break;
|
||||
|
||||
round_key[10] = round_key[4] ^ round_key[9];
|
||||
round_key[11] = round_key[5] ^ round_key[10];
|
||||
round_key[10] = round_key[4] ^ round_key[9];
|
||||
round_key[11] = round_key[5] ^ round_key[10];
|
||||
|
||||
round_key += 6;
|
||||
round_key += 6;
|
||||
}
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
round_key[6] = get_key(user_key.substring_view(24, 4).characters_without_null_termination());
|
||||
round_key[7] = get_key(user_key.substring_view(28, 4).characters_without_null_termination());
|
||||
if (true) { // bits == 256
|
||||
for (;;) {
|
||||
temp = round_key[7];
|
||||
// clang-format off
|
||||
round_key[6] = get_key(user_key.substring_view(24, 4).characters_without_null_termination());
|
||||
round_key[7] = get_key(user_key.substring_view(28, 4).characters_without_null_termination());
|
||||
if (true) { // bits == 256
|
||||
for (;;) {
|
||||
temp = round_key[7];
|
||||
// clang-format off
|
||||
round_key[8] = round_key[0] ^
|
||||
(Tables::Encode2[(temp >> 16) & 0xff] & 0xff000000) ^
|
||||
(Tables::Encode3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
||||
(Tables::Encode0[(temp ) & 0xff] & 0x0000ff00) ^
|
||||
(Tables::Encode1[(temp >> 24) ] & 0x000000ff) ^ Tables::RCON[i];
|
||||
// clang-format on
|
||||
round_key[9] = round_key[1] ^ round_key[8];
|
||||
round_key[10] = round_key[2] ^ round_key[9];
|
||||
round_key[11] = round_key[3] ^ round_key[10];
|
||||
(AESTables::Encode2[(temp >> 16) & 0xff] & 0xff000000) ^
|
||||
(AESTables::Encode3[(temp >> 8) & 0xff] & 0x00ff0000) ^
|
||||
(AESTables::Encode0[(temp ) & 0xff] & 0x0000ff00) ^
|
||||
(AESTables::Encode1[(temp >> 24) ] & 0x000000ff) ^ AESTables::RCON[i];
|
||||
// clang-format on
|
||||
round_key[9] = round_key[1] ^ round_key[8];
|
||||
round_key[10] = round_key[2] ^ round_key[9];
|
||||
round_key[11] = round_key[3] ^ round_key[10];
|
||||
|
||||
++i;
|
||||
if (i == 7)
|
||||
break;
|
||||
|
||||
temp = round_key[11];
|
||||
// clang-format off
|
||||
round_key[12] = round_key[4] ^
|
||||
(AESTables::Encode2[(temp >> 24) ] & 0xff000000) ^
|
||||
(AESTables::Encode3[(temp >> 16) & 0xff] & 0x00ff0000) ^
|
||||
(AESTables::Encode0[(temp >> 8) & 0xff] & 0x0000ff00) ^
|
||||
(AESTables::Encode1[(temp ) & 0xff] & 0x000000ff) ;
|
||||
// clang-format on
|
||||
round_key[13] = round_key[5] ^ round_key[12];
|
||||
round_key[14] = round_key[6] ^ round_key[13];
|
||||
round_key[15] = round_key[7] ^ round_key[14];
|
||||
|
||||
round_key += 8;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void AESCipherKey::expand_decrypt_key(const StringView& user_key, size_t bits)
|
||||
{
|
||||
u32* round_key;
|
||||
|
||||
expand_encrypt_key(user_key, bits);
|
||||
|
||||
round_key = round_keys();
|
||||
|
||||
// reorder round keys
|
||||
for (size_t i = 0, j = 4 * rounds(); i < j; i += 4, j -= 4) {
|
||||
swap_keys(round_key, i, j);
|
||||
swap_keys(round_key, i + 1, j + 1);
|
||||
swap_keys(round_key, i + 2, j + 2);
|
||||
swap_keys(round_key, i + 3, j + 3);
|
||||
}
|
||||
|
||||
// apply inverse mix-column to middle rounds
|
||||
for (size_t i = 1; i < rounds(); ++i) {
|
||||
round_key += 4;
|
||||
// clang-format off
|
||||
round_key[0] =
|
||||
AESTables::Decode0[AESTables::Encode1[(round_key[0] >> 24) ] & 0xff] ^
|
||||
AESTables::Decode1[AESTables::Encode1[(round_key[0] >> 16) & 0xff] & 0xff] ^
|
||||
AESTables::Decode2[AESTables::Encode1[(round_key[0] >> 8) & 0xff] & 0xff] ^
|
||||
AESTables::Decode3[AESTables::Encode1[(round_key[0] ) & 0xff] & 0xff] ;
|
||||
round_key[1] =
|
||||
AESTables::Decode0[AESTables::Encode1[(round_key[1] >> 24) ] & 0xff] ^
|
||||
AESTables::Decode1[AESTables::Encode1[(round_key[1] >> 16) & 0xff] & 0xff] ^
|
||||
AESTables::Decode2[AESTables::Encode1[(round_key[1] >> 8) & 0xff] & 0xff] ^
|
||||
AESTables::Decode3[AESTables::Encode1[(round_key[1] ) & 0xff] & 0xff] ;
|
||||
round_key[2] =
|
||||
AESTables::Decode0[AESTables::Encode1[(round_key[2] >> 24) ] & 0xff] ^
|
||||
AESTables::Decode1[AESTables::Encode1[(round_key[2] >> 16) & 0xff] & 0xff] ^
|
||||
AESTables::Decode2[AESTables::Encode1[(round_key[2] >> 8) & 0xff] & 0xff] ^
|
||||
AESTables::Decode3[AESTables::Encode1[(round_key[2] ) & 0xff] & 0xff] ;
|
||||
round_key[3] =
|
||||
AESTables::Decode0[AESTables::Encode1[(round_key[3] >> 24) ] & 0xff] ^
|
||||
AESTables::Decode1[AESTables::Encode1[(round_key[3] >> 16) & 0xff] & 0xff] ^
|
||||
AESTables::Decode2[AESTables::Encode1[(round_key[3] >> 8) & 0xff] & 0xff] ^
|
||||
AESTables::Decode3[AESTables::Encode1[(round_key[3] ) & 0xff] & 0xff] ;
|
||||
// clang-format on
|
||||
}
|
||||
}
|
||||
|
||||
void AESCipher::encrypt_block(const AESCipherBlock& in, AESCipherBlock& out)
|
||||
{
|
||||
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
||||
size_t r { 0 };
|
||||
|
||||
const auto& dec_key = key();
|
||||
const auto* round_keys = dec_key.round_keys();
|
||||
|
||||
s0 = get_key(in.data().offset_pointer(0)) ^ round_keys[0];
|
||||
s1 = get_key(in.data().offset_pointer(4)) ^ round_keys[1];
|
||||
s2 = get_key(in.data().offset_pointer(8)) ^ round_keys[2];
|
||||
s3 = get_key(in.data().offset_pointer(12)) ^ round_keys[3];
|
||||
|
||||
r = dec_key.rounds() >> 1;
|
||||
|
||||
// apply the first |r - 1| rounds
|
||||
auto i { 0 };
|
||||
for (;;) {
|
||||
++i;
|
||||
if (i == 7)
|
||||
// clang-format off
|
||||
t0 = AESTables::Encode0[(s0 >> 24) ] ^
|
||||
AESTables::Encode1[(s1 >> 16) & 0xff] ^
|
||||
AESTables::Encode2[(s2 >> 8) & 0xff] ^
|
||||
AESTables::Encode3[(s3 ) & 0xff] ^ round_keys[4];
|
||||
t1 = AESTables::Encode0[(s1 >> 24) ] ^
|
||||
AESTables::Encode1[(s2 >> 16) & 0xff] ^
|
||||
AESTables::Encode2[(s3 >> 8) & 0xff] ^
|
||||
AESTables::Encode3[(s0 ) & 0xff] ^ round_keys[5];
|
||||
t2 = AESTables::Encode0[(s2 >> 24) ] ^
|
||||
AESTables::Encode1[(s3 >> 16) & 0xff] ^
|
||||
AESTables::Encode2[(s0 >> 8) & 0xff] ^
|
||||
AESTables::Encode3[(s1 ) & 0xff] ^ round_keys[6];
|
||||
t3 = AESTables::Encode0[(s3 >> 24) ] ^
|
||||
AESTables::Encode1[(s0 >> 16) & 0xff] ^
|
||||
AESTables::Encode2[(s1 >> 8) & 0xff] ^
|
||||
AESTables::Encode3[(s2 ) & 0xff] ^ round_keys[7];
|
||||
// clang-format on
|
||||
|
||||
round_keys += 8;
|
||||
--r;
|
||||
++i;
|
||||
if (r == 0)
|
||||
break;
|
||||
|
||||
temp = round_key[11];
|
||||
// clang-format off
|
||||
round_key[12] = round_key[4] ^
|
||||
(Tables::Encode2[(temp >> 24) ] & 0xff000000) ^
|
||||
(Tables::Encode3[(temp >> 16) & 0xff] & 0x00ff0000) ^
|
||||
(Tables::Encode0[(temp >> 8) & 0xff] & 0x0000ff00) ^
|
||||
(Tables::Encode1[(temp ) & 0xff] & 0x000000ff) ;
|
||||
s0 = AESTables::Encode0[(t0 >> 24) ] ^
|
||||
AESTables::Encode1[(t1 >> 16) & 0xff] ^
|
||||
AESTables::Encode2[(t2 >> 8) & 0xff] ^
|
||||
AESTables::Encode3[(t3 ) & 0xff] ^ round_keys[0];
|
||||
s1 = AESTables::Encode0[(t1 >> 24) ] ^
|
||||
AESTables::Encode1[(t2 >> 16) & 0xff] ^
|
||||
AESTables::Encode2[(t3 >> 8) & 0xff] ^
|
||||
AESTables::Encode3[(t0 ) & 0xff] ^ round_keys[1];
|
||||
s2 = AESTables::Encode0[(t2 >> 24) ] ^
|
||||
AESTables::Encode1[(t3 >> 16) & 0xff] ^
|
||||
AESTables::Encode2[(t0 >> 8) & 0xff] ^
|
||||
AESTables::Encode3[(t1 ) & 0xff] ^ round_keys[2];
|
||||
s3 = AESTables::Encode0[(t3 >> 24) ] ^
|
||||
AESTables::Encode1[(t0 >> 16) & 0xff] ^
|
||||
AESTables::Encode2[(t1 >> 8) & 0xff] ^
|
||||
AESTables::Encode3[(t2 ) & 0xff] ^ round_keys[3];
|
||||
// clang-format on
|
||||
round_key[13] = round_key[5] ^ round_key[12];
|
||||
round_key[14] = round_key[6] ^ round_key[13];
|
||||
round_key[15] = round_key[7] ^ round_key[14];
|
||||
|
||||
round_key += 8;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void AESCipherKey::expand_decrypt_key(const StringView& user_key, size_t bits)
|
||||
{
|
||||
u32* round_key;
|
||||
|
||||
expand_encrypt_key(user_key, bits);
|
||||
|
||||
round_key = round_keys();
|
||||
|
||||
// reorder round keys
|
||||
for (size_t i = 0, j = 4 * rounds(); i < j; i += 4, j -= 4) {
|
||||
swap_keys(round_key, i, j);
|
||||
swap_keys(round_key, i + 1, j + 1);
|
||||
swap_keys(round_key, i + 2, j + 2);
|
||||
swap_keys(round_key, i + 3, j + 3);
|
||||
}
|
||||
|
||||
// apply inverse mix-column to middle rounds
|
||||
for (size_t i = 1; i < rounds(); ++i) {
|
||||
round_key += 4;
|
||||
// apply the last round and put the encrypted data into out
|
||||
// clang-format off
|
||||
round_key[0] =
|
||||
Tables::Decode0[Tables::Encode1[(round_key[0] >> 24) ] & 0xff] ^
|
||||
Tables::Decode1[Tables::Encode1[(round_key[0] >> 16) & 0xff] & 0xff] ^
|
||||
Tables::Decode2[Tables::Encode1[(round_key[0] >> 8) & 0xff] & 0xff] ^
|
||||
Tables::Decode3[Tables::Encode1[(round_key[0] ) & 0xff] & 0xff] ;
|
||||
round_key[1] =
|
||||
Tables::Decode0[Tables::Encode1[(round_key[1] >> 24) ] & 0xff] ^
|
||||
Tables::Decode1[Tables::Encode1[(round_key[1] >> 16) & 0xff] & 0xff] ^
|
||||
Tables::Decode2[Tables::Encode1[(round_key[1] >> 8) & 0xff] & 0xff] ^
|
||||
Tables::Decode3[Tables::Encode1[(round_key[1] ) & 0xff] & 0xff] ;
|
||||
round_key[2] =
|
||||
Tables::Decode0[Tables::Encode1[(round_key[2] >> 24) ] & 0xff] ^
|
||||
Tables::Decode1[Tables::Encode1[(round_key[2] >> 16) & 0xff] & 0xff] ^
|
||||
Tables::Decode2[Tables::Encode1[(round_key[2] >> 8) & 0xff] & 0xff] ^
|
||||
Tables::Decode3[Tables::Encode1[(round_key[2] ) & 0xff] & 0xff] ;
|
||||
round_key[3] =
|
||||
Tables::Decode0[Tables::Encode1[(round_key[3] >> 24) ] & 0xff] ^
|
||||
Tables::Decode1[Tables::Encode1[(round_key[3] >> 16) & 0xff] & 0xff] ^
|
||||
Tables::Decode2[Tables::Encode1[(round_key[3] >> 8) & 0xff] & 0xff] ^
|
||||
Tables::Decode3[Tables::Encode1[(round_key[3] ) & 0xff] & 0xff] ;
|
||||
// clang-format on
|
||||
}
|
||||
}
|
||||
|
||||
void AESCipher::encrypt_block(const AESCipherBlock& in, AESCipherBlock& out)
|
||||
{
|
||||
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
||||
size_t r { 0 };
|
||||
|
||||
const auto& dec_key = key();
|
||||
const auto* round_keys = dec_key.round_keys();
|
||||
|
||||
s0 = get_key(in.data().offset_pointer(0)) ^ round_keys[0];
|
||||
s1 = get_key(in.data().offset_pointer(4)) ^ round_keys[1];
|
||||
s2 = get_key(in.data().offset_pointer(8)) ^ round_keys[2];
|
||||
s3 = get_key(in.data().offset_pointer(12)) ^ round_keys[3];
|
||||
|
||||
r = dec_key.rounds() >> 1;
|
||||
|
||||
// apply the first |r - 1| rounds
|
||||
auto i { 0 };
|
||||
for (;;) {
|
||||
++i;
|
||||
// clang-format off
|
||||
t0 = Tables::Encode0[(s0 >> 24) ] ^
|
||||
Tables::Encode1[(s1 >> 16) & 0xff] ^
|
||||
Tables::Encode2[(s2 >> 8) & 0xff] ^
|
||||
Tables::Encode3[(s3 ) & 0xff] ^ round_keys[4];
|
||||
t1 = Tables::Encode0[(s1 >> 24) ] ^
|
||||
Tables::Encode1[(s2 >> 16) & 0xff] ^
|
||||
Tables::Encode2[(s3 >> 8) & 0xff] ^
|
||||
Tables::Encode3[(s0 ) & 0xff] ^ round_keys[5];
|
||||
t2 = Tables::Encode0[(s2 >> 24) ] ^
|
||||
Tables::Encode1[(s3 >> 16) & 0xff] ^
|
||||
Tables::Encode2[(s0 >> 8) & 0xff] ^
|
||||
Tables::Encode3[(s1 ) & 0xff] ^ round_keys[6];
|
||||
t3 = Tables::Encode0[(s3 >> 24) ] ^
|
||||
Tables::Encode1[(s0 >> 16) & 0xff] ^
|
||||
Tables::Encode2[(s1 >> 8) & 0xff] ^
|
||||
Tables::Encode3[(s2 ) & 0xff] ^ round_keys[7];
|
||||
// clang-format on
|
||||
|
||||
round_keys += 8;
|
||||
--r;
|
||||
++i;
|
||||
if (r == 0)
|
||||
break;
|
||||
|
||||
// clang-format off
|
||||
s0 = Tables::Encode0[(t0 >> 24) ] ^
|
||||
Tables::Encode1[(t1 >> 16) & 0xff] ^
|
||||
Tables::Encode2[(t2 >> 8) & 0xff] ^
|
||||
Tables::Encode3[(t3 ) & 0xff] ^ round_keys[0];
|
||||
s1 = Tables::Encode0[(t1 >> 24) ] ^
|
||||
Tables::Encode1[(t2 >> 16) & 0xff] ^
|
||||
Tables::Encode2[(t3 >> 8) & 0xff] ^
|
||||
Tables::Encode3[(t0 ) & 0xff] ^ round_keys[1];
|
||||
s2 = Tables::Encode0[(t2 >> 24) ] ^
|
||||
Tables::Encode1[(t3 >> 16) & 0xff] ^
|
||||
Tables::Encode2[(t0 >> 8) & 0xff] ^
|
||||
Tables::Encode3[(t1 ) & 0xff] ^ round_keys[2];
|
||||
s3 = Tables::Encode0[(t3 >> 24) ] ^
|
||||
Tables::Encode1[(t0 >> 16) & 0xff] ^
|
||||
Tables::Encode2[(t1 >> 8) & 0xff] ^
|
||||
Tables::Encode3[(t2 ) & 0xff] ^ round_keys[3];
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
// apply the last round and put the encrypted data into out
|
||||
// clang-format off
|
||||
s0 = (Tables::Encode2[(t0 >> 24) ] & 0xff000000) ^
|
||||
(Tables::Encode3[(t1 >> 16) & 0xff] & 0x00ff0000) ^
|
||||
(Tables::Encode0[(t2 >> 8) & 0xff] & 0x0000ff00) ^
|
||||
(Tables::Encode1[(t3 ) & 0xff] & 0x000000ff) ^ round_keys[0];
|
||||
s0 = (AESTables::Encode2[(t0 >> 24) ] & 0xff000000) ^
|
||||
(AESTables::Encode3[(t1 >> 16) & 0xff] & 0x00ff0000) ^
|
||||
(AESTables::Encode0[(t2 >> 8) & 0xff] & 0x0000ff00) ^
|
||||
(AESTables::Encode1[(t3 ) & 0xff] & 0x000000ff) ^ round_keys[0];
|
||||
out.put(0, s0);
|
||||
|
||||
s1 = (Tables::Encode2[(t1 >> 24) ] & 0xff000000) ^
|
||||
(Tables::Encode3[(t2 >> 16) & 0xff] & 0x00ff0000) ^
|
||||
(Tables::Encode0[(t3 >> 8) & 0xff] & 0x0000ff00) ^
|
||||
(Tables::Encode1[(t0 ) & 0xff] & 0x000000ff) ^ round_keys[1];
|
||||
s1 = (AESTables::Encode2[(t1 >> 24) ] & 0xff000000) ^
|
||||
(AESTables::Encode3[(t2 >> 16) & 0xff] & 0x00ff0000) ^
|
||||
(AESTables::Encode0[(t3 >> 8) & 0xff] & 0x0000ff00) ^
|
||||
(AESTables::Encode1[(t0 ) & 0xff] & 0x000000ff) ^ round_keys[1];
|
||||
out.put(4, s1);
|
||||
|
||||
s2 = (Tables::Encode2[(t2 >> 24) ] & 0xff000000) ^
|
||||
(Tables::Encode3[(t3 >> 16) & 0xff] & 0x00ff0000) ^
|
||||
(Tables::Encode0[(t0 >> 8) & 0xff] & 0x0000ff00) ^
|
||||
(Tables::Encode1[(t1 ) & 0xff] & 0x000000ff) ^ round_keys[2];
|
||||
s2 = (AESTables::Encode2[(t2 >> 24) ] & 0xff000000) ^
|
||||
(AESTables::Encode3[(t3 >> 16) & 0xff] & 0x00ff0000) ^
|
||||
(AESTables::Encode0[(t0 >> 8) & 0xff] & 0x0000ff00) ^
|
||||
(AESTables::Encode1[(t1 ) & 0xff] & 0x000000ff) ^ round_keys[2];
|
||||
out.put(8, s2);
|
||||
|
||||
s3 = (Tables::Encode2[(t3 >> 24) ] & 0xff000000) ^
|
||||
(Tables::Encode3[(t0 >> 16) & 0xff] & 0x00ff0000) ^
|
||||
(Tables::Encode0[(t1 >> 8) & 0xff] & 0x0000ff00) ^
|
||||
(Tables::Encode1[(t2 ) & 0xff] & 0x000000ff) ^ round_keys[3];
|
||||
s3 = (AESTables::Encode2[(t3 >> 24) ] & 0xff000000) ^
|
||||
(AESTables::Encode3[(t0 >> 16) & 0xff] & 0x00ff0000) ^
|
||||
(AESTables::Encode0[(t1 >> 8) & 0xff] & 0x0000ff00) ^
|
||||
(AESTables::Encode1[(t2 ) & 0xff] & 0x000000ff) ^ round_keys[3];
|
||||
out.put(12, s3);
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
void AESCipher::decrypt_block(const AESCipherBlock& in, AESCipherBlock& out)
|
||||
{
|
||||
|
||||
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
||||
size_t r { 0 };
|
||||
|
||||
const auto& dec_key = key();
|
||||
const auto* round_keys = dec_key.round_keys();
|
||||
|
||||
s0 = get_key(in.data().offset_pointer(0)) ^ round_keys[0];
|
||||
s1 = get_key(in.data().offset_pointer(4)) ^ round_keys[1];
|
||||
s2 = get_key(in.data().offset_pointer(8)) ^ round_keys[2];
|
||||
s3 = get_key(in.data().offset_pointer(12)) ^ round_keys[3];
|
||||
|
||||
r = dec_key.rounds() >> 1;
|
||||
|
||||
// apply the first |r - 1| rounds
|
||||
for (;;) {
|
||||
// clang-format off
|
||||
t0 = Tables::Decode0[(s0 >> 24) ] ^
|
||||
Tables::Decode1[(s3 >> 16) & 0xff] ^
|
||||
Tables::Decode2[(s2 >> 8) & 0xff] ^
|
||||
Tables::Decode3[(s1 ) & 0xff] ^ round_keys[4];
|
||||
t1 = Tables::Decode0[(s1 >> 24) ] ^
|
||||
Tables::Decode1[(s0 >> 16) & 0xff] ^
|
||||
Tables::Decode2[(s3 >> 8) & 0xff] ^
|
||||
Tables::Decode3[(s2 ) & 0xff] ^ round_keys[5];
|
||||
t2 = Tables::Decode0[(s2 >> 24) ] ^
|
||||
Tables::Decode1[(s1 >> 16) & 0xff] ^
|
||||
Tables::Decode2[(s0 >> 8) & 0xff] ^
|
||||
Tables::Decode3[(s3 ) & 0xff] ^ round_keys[6];
|
||||
t3 = Tables::Decode0[(s3 >> 24) ] ^
|
||||
Tables::Decode1[(s2 >> 16) & 0xff] ^
|
||||
Tables::Decode2[(s1 >> 8) & 0xff] ^
|
||||
Tables::Decode3[(s0 ) & 0xff] ^ round_keys[7];
|
||||
// clang-format on
|
||||
|
||||
round_keys += 8;
|
||||
--r;
|
||||
if (r == 0)
|
||||
break;
|
||||
|
||||
// clang-format off
|
||||
s0 = Tables::Decode0[(t0 >> 24) ] ^
|
||||
Tables::Decode1[(t3 >> 16) & 0xff] ^
|
||||
Tables::Decode2[(t2 >> 8) & 0xff] ^
|
||||
Tables::Decode3[(t1 ) & 0xff] ^ round_keys[0];
|
||||
s1 = Tables::Decode0[(t1 >> 24) ] ^
|
||||
Tables::Decode1[(t0 >> 16) & 0xff] ^
|
||||
Tables::Decode2[(t3 >> 8) & 0xff] ^
|
||||
Tables::Decode3[(t2 ) & 0xff] ^ round_keys[1];
|
||||
s2 = Tables::Decode0[(t2 >> 24) ] ^
|
||||
Tables::Decode1[(t1 >> 16) & 0xff] ^
|
||||
Tables::Decode2[(t0 >> 8) & 0xff] ^
|
||||
Tables::Decode3[(t3 ) & 0xff] ^ round_keys[2];
|
||||
s3 = Tables::Decode0[(t3 >> 24) ] ^
|
||||
Tables::Decode1[(t2 >> 16) & 0xff] ^
|
||||
Tables::Decode2[(t1 >> 8) & 0xff] ^
|
||||
Tables::Decode3[(t0 ) & 0xff] ^ round_keys[3];
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
// apply the last round and put the decrypted data into out
|
||||
// clang-format off
|
||||
s0 = ((u32)Tables::Decode4[(t0 >> 24) ] << 24) ^
|
||||
((u32)Tables::Decode4[(t3 >> 16) & 0xff] << 16) ^
|
||||
((u32)Tables::Decode4[(t2 >> 8) & 0xff] << 8) ^
|
||||
((u32)Tables::Decode4[(t1 ) & 0xff] ) ^ round_keys[0];
|
||||
void AESCipher::decrypt_block(const AESCipherBlock& in, AESCipherBlock& out)
|
||||
{
|
||||
|
||||
u32 s0, s1, s2, s3, t0, t1, t2, t3;
|
||||
size_t r { 0 };
|
||||
|
||||
const auto& dec_key = key();
|
||||
const auto* round_keys = dec_key.round_keys();
|
||||
|
||||
s0 = get_key(in.data().offset_pointer(0)) ^ round_keys[0];
|
||||
s1 = get_key(in.data().offset_pointer(4)) ^ round_keys[1];
|
||||
s2 = get_key(in.data().offset_pointer(8)) ^ round_keys[2];
|
||||
s3 = get_key(in.data().offset_pointer(12)) ^ round_keys[3];
|
||||
|
||||
r = dec_key.rounds() >> 1;
|
||||
|
||||
// apply the first |r - 1| rounds
|
||||
for (;;) {
|
||||
// clang-format off
|
||||
t0 = AESTables::Decode0[(s0 >> 24) ] ^
|
||||
AESTables::Decode1[(s3 >> 16) & 0xff] ^
|
||||
AESTables::Decode2[(s2 >> 8) & 0xff] ^
|
||||
AESTables::Decode3[(s1 ) & 0xff] ^ round_keys[4];
|
||||
t1 = AESTables::Decode0[(s1 >> 24) ] ^
|
||||
AESTables::Decode1[(s0 >> 16) & 0xff] ^
|
||||
AESTables::Decode2[(s3 >> 8) & 0xff] ^
|
||||
AESTables::Decode3[(s2 ) & 0xff] ^ round_keys[5];
|
||||
t2 = AESTables::Decode0[(s2 >> 24) ] ^
|
||||
AESTables::Decode1[(s1 >> 16) & 0xff] ^
|
||||
AESTables::Decode2[(s0 >> 8) & 0xff] ^
|
||||
AESTables::Decode3[(s3 ) & 0xff] ^ round_keys[6];
|
||||
t3 = AESTables::Decode0[(s3 >> 24) ] ^
|
||||
AESTables::Decode1[(s2 >> 16) & 0xff] ^
|
||||
AESTables::Decode2[(s1 >> 8) & 0xff] ^
|
||||
AESTables::Decode3[(s0 ) & 0xff] ^ round_keys[7];
|
||||
// clang-format on
|
||||
|
||||
round_keys += 8;
|
||||
--r;
|
||||
if (r == 0)
|
||||
break;
|
||||
|
||||
// clang-format off
|
||||
s0 = AESTables::Decode0[(t0 >> 24) ] ^
|
||||
AESTables::Decode1[(t3 >> 16) & 0xff] ^
|
||||
AESTables::Decode2[(t2 >> 8) & 0xff] ^
|
||||
AESTables::Decode3[(t1 ) & 0xff] ^ round_keys[0];
|
||||
s1 = AESTables::Decode0[(t1 >> 24) ] ^
|
||||
AESTables::Decode1[(t0 >> 16) & 0xff] ^
|
||||
AESTables::Decode2[(t3 >> 8) & 0xff] ^
|
||||
AESTables::Decode3[(t2 ) & 0xff] ^ round_keys[1];
|
||||
s2 = AESTables::Decode0[(t2 >> 24) ] ^
|
||||
AESTables::Decode1[(t1 >> 16) & 0xff] ^
|
||||
AESTables::Decode2[(t0 >> 8) & 0xff] ^
|
||||
AESTables::Decode3[(t3 ) & 0xff] ^ round_keys[2];
|
||||
s3 = AESTables::Decode0[(t3 >> 24) ] ^
|
||||
AESTables::Decode1[(t2 >> 16) & 0xff] ^
|
||||
AESTables::Decode2[(t1 >> 8) & 0xff] ^
|
||||
AESTables::Decode3[(t0 ) & 0xff] ^ round_keys[3];
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
// apply the last round and put the decrypted data into out
|
||||
// clang-format off
|
||||
s0 = ((u32)AESTables::Decode4[(t0 >> 24) ] << 24) ^
|
||||
((u32)AESTables::Decode4[(t3 >> 16) & 0xff] << 16) ^
|
||||
((u32)AESTables::Decode4[(t2 >> 8) & 0xff] << 8) ^
|
||||
((u32)AESTables::Decode4[(t1 ) & 0xff] ) ^ round_keys[0];
|
||||
out.put(0, s0);
|
||||
|
||||
s1 = ((u32)Tables::Decode4[(t1 >> 24) ] << 24) ^
|
||||
((u32)Tables::Decode4[(t0 >> 16) & 0xff] << 16) ^
|
||||
((u32)Tables::Decode4[(t3 >> 8) & 0xff] << 8) ^
|
||||
((u32)Tables::Decode4[(t2 ) & 0xff] ) ^ round_keys[1];
|
||||
s1 = ((u32)AESTables::Decode4[(t1 >> 24) ] << 24) ^
|
||||
((u32)AESTables::Decode4[(t0 >> 16) & 0xff] << 16) ^
|
||||
((u32)AESTables::Decode4[(t3 >> 8) & 0xff] << 8) ^
|
||||
((u32)AESTables::Decode4[(t2 ) & 0xff] ) ^ round_keys[1];
|
||||
out.put(4, s1);
|
||||
|
||||
s2 = ((u32)Tables::Decode4[(t2 >> 24) ] << 24) ^
|
||||
((u32)Tables::Decode4[(t1 >> 16) & 0xff] << 16) ^
|
||||
((u32)Tables::Decode4[(t0 >> 8) & 0xff] << 8) ^
|
||||
((u32)Tables::Decode4[(t3 ) & 0xff] ) ^ round_keys[2];
|
||||
s2 = ((u32)AESTables::Decode4[(t2 >> 24) ] << 24) ^
|
||||
((u32)AESTables::Decode4[(t1 >> 16) & 0xff] << 16) ^
|
||||
((u32)AESTables::Decode4[(t0 >> 8) & 0xff] << 8) ^
|
||||
((u32)AESTables::Decode4[(t3 ) & 0xff] ) ^ round_keys[2];
|
||||
out.put(8, s2);
|
||||
|
||||
s3 = ((u32)Tables::Decode4[(t3 >> 24) ] << 24) ^
|
||||
((u32)Tables::Decode4[(t2 >> 16) & 0xff] << 16) ^
|
||||
((u32)Tables::Decode4[(t1 >> 8) & 0xff] << 8) ^
|
||||
((u32)Tables::Decode4[(t0 ) & 0xff] ) ^ round_keys[3];
|
||||
s3 = ((u32)AESTables::Decode4[(t3 >> 24) ] << 24) ^
|
||||
((u32)AESTables::Decode4[(t2 >> 16) & 0xff] << 16) ^
|
||||
((u32)AESTables::Decode4[(t1 >> 8) & 0xff] << 8) ^
|
||||
((u32)AESTables::Decode4[(t0 ) & 0xff] ) ^ round_keys[3];
|
||||
out.put(12, s3);
|
||||
// clang-format on
|
||||
}
|
||||
// clang-format on
|
||||
}
|
||||
|
||||
void AESCipherBlock::overwrite(const ByteBuffer& buffer)
|
||||
{
|
||||
overwrite(buffer.data(), buffer.size());
|
||||
}
|
||||
void AESCipherBlock::overwrite(const ByteBuffer& buffer)
|
||||
{
|
||||
overwrite(buffer.data(), buffer.size());
|
||||
}
|
||||
|
||||
void AESCipherBlock::overwrite(const u8* data, size_t length)
|
||||
{
|
||||
ASSERT(length <= m_data.size());
|
||||
m_data.overwrite(0, data, length);
|
||||
if (length < m_data.size()) {
|
||||
switch (padding_mode()) {
|
||||
case PaddingMode::Null:
|
||||
// fill with zeros
|
||||
__builtin_memset(m_data.data() + length, 0, m_data.size() - length);
|
||||
break;
|
||||
case PaddingMode::CMS:
|
||||
// fill with the length of the padding bytes
|
||||
__builtin_memset(m_data.data() + length, m_data.size() - length, m_data.size() - length);
|
||||
break;
|
||||
default:
|
||||
// FIXME: We should handle the rest of the common padding modes
|
||||
ASSERT_NOT_REACHED();
|
||||
void AESCipherBlock::overwrite(const u8* data, size_t length)
|
||||
{
|
||||
ASSERT(length <= m_data.size());
|
||||
m_data.overwrite(0, data, length);
|
||||
if (length < m_data.size()) {
|
||||
switch (padding_mode()) {
|
||||
case PaddingMode::Null:
|
||||
// fill with zeros
|
||||
__builtin_memset(m_data.data() + length, 0, m_data.size() - length);
|
||||
break;
|
||||
case PaddingMode::CMS:
|
||||
// fill with the length of the padding bytes
|
||||
__builtin_memset(m_data.data() + length, m_data.size() - length, m_data.size() - length);
|
||||
break;
|
||||
default:
|
||||
// FIXME: We should handle the rest of the common padding modes
|
||||
ASSERT_NOT_REACHED();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -28,109 +28,110 @@
|
|||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
namespace Crypto {
|
||||
namespace Cipher {
|
||||
|
||||
enum class Intent {
|
||||
Encryption,
|
||||
Decryption,
|
||||
};
|
||||
enum class Intent {
|
||||
Encryption,
|
||||
Decryption,
|
||||
};
|
||||
|
||||
enum class PaddingMode {
|
||||
CMS, // RFC 1423
|
||||
Null,
|
||||
// FIXME: We do not implement these yet
|
||||
Bit,
|
||||
Random,
|
||||
Space,
|
||||
ZeroLength,
|
||||
};
|
||||
enum class PaddingMode {
|
||||
CMS, // RFC 1423
|
||||
Null,
|
||||
// FIXME: We do not implement these yet
|
||||
Bit,
|
||||
Random,
|
||||
Space,
|
||||
ZeroLength,
|
||||
};
|
||||
|
||||
template <typename B, typename T>
|
||||
class Cipher;
|
||||
template <typename B, typename T>
|
||||
class Cipher;
|
||||
|
||||
struct CipherBlock {
|
||||
public:
|
||||
explicit CipherBlock(PaddingMode mode)
|
||||
: m_padding_mode(mode)
|
||||
{
|
||||
}
|
||||
struct CipherBlock {
|
||||
public:
|
||||
explicit CipherBlock(PaddingMode mode)
|
||||
: m_padding_mode(mode)
|
||||
{
|
||||
}
|
||||
|
||||
static size_t block_size() { ASSERT_NOT_REACHED(); }
|
||||
static size_t block_size() { ASSERT_NOT_REACHED(); }
|
||||
|
||||
virtual ByteBuffer get() const = 0;
|
||||
virtual const ByteBuffer& data() const = 0;
|
||||
virtual ByteBuffer get() const = 0;
|
||||
virtual const ByteBuffer& data() const = 0;
|
||||
|
||||
virtual void overwrite(const ByteBuffer&) = 0;
|
||||
virtual void overwrite(const u8*, size_t) = 0;
|
||||
virtual void overwrite(const ByteBuffer&) = 0;
|
||||
virtual void overwrite(const u8*, size_t) = 0;
|
||||
|
||||
virtual void apply_initialization_vector(const u8* ivec) = 0;
|
||||
virtual void apply_initialization_vector(const u8* ivec) = 0;
|
||||
|
||||
PaddingMode padding_mode() const { return m_padding_mode; }
|
||||
PaddingMode padding_mode() const { return m_padding_mode; }
|
||||
|
||||
template <typename T>
|
||||
void put(size_t offset, T value)
|
||||
{
|
||||
ASSERT(offset + sizeof(T) <= data().size());
|
||||
auto* ptr = data().data() + offset;
|
||||
auto index { 0 };
|
||||
template <typename T>
|
||||
void put(size_t offset, T value)
|
||||
{
|
||||
ASSERT(offset + sizeof(T) <= data().size());
|
||||
auto* ptr = data().data() + offset;
|
||||
auto index { 0 };
|
||||
|
||||
ASSERT(sizeof(T) <= 4);
|
||||
ASSERT(sizeof(T) <= 4);
|
||||
|
||||
if constexpr (sizeof(T) > 3)
|
||||
ptr[index++] = (u8)(value >> 24);
|
||||
if constexpr (sizeof(T) > 3)
|
||||
ptr[index++] = (u8)(value >> 24);
|
||||
|
||||
if constexpr (sizeof(T) > 2)
|
||||
ptr[index++] = (u8)(value >> 16);
|
||||
if constexpr (sizeof(T) > 2)
|
||||
ptr[index++] = (u8)(value >> 16);
|
||||
|
||||
if constexpr (sizeof(T) > 1)
|
||||
ptr[index++] = (u8)(value >> 8);
|
||||
if constexpr (sizeof(T) > 1)
|
||||
ptr[index++] = (u8)(value >> 8);
|
||||
|
||||
ptr[index] = (u8)value;
|
||||
}
|
||||
ptr[index] = (u8)value;
|
||||
}
|
||||
|
||||
private:
|
||||
virtual ByteBuffer& data() = 0;
|
||||
PaddingMode m_padding_mode;
|
||||
};
|
||||
private:
|
||||
virtual ByteBuffer& data() = 0;
|
||||
PaddingMode m_padding_mode;
|
||||
};
|
||||
|
||||
struct CipherKey {
|
||||
virtual ByteBuffer data() const = 0;
|
||||
static bool is_valid_key_size(size_t) { return false; };
|
||||
struct CipherKey {
|
||||
virtual ByteBuffer data() const = 0;
|
||||
static bool is_valid_key_size(size_t) { return false; };
|
||||
|
||||
virtual ~CipherKey() { }
|
||||
virtual ~CipherKey() { }
|
||||
|
||||
protected:
|
||||
virtual void expand_encrypt_key(const ByteBuffer& user_key, size_t bits) = 0;
|
||||
virtual void expand_decrypt_key(const ByteBuffer& user_key, size_t bits) = 0;
|
||||
size_t bits { 0 };
|
||||
};
|
||||
protected:
|
||||
virtual void expand_encrypt_key(const ByteBuffer& user_key, size_t bits) = 0;
|
||||
virtual void expand_decrypt_key(const ByteBuffer& user_key, size_t bits) = 0;
|
||||
size_t bits { 0 };
|
||||
};
|
||||
|
||||
template <typename KeyT = CipherKey, typename BlockT = CipherBlock>
|
||||
class Cipher {
|
||||
public:
|
||||
using KeyType = KeyT;
|
||||
using BlockType = BlockT;
|
||||
template <typename KeyT = CipherKey, typename BlockT = CipherBlock>
|
||||
class Cipher {
|
||||
public:
|
||||
using KeyType = KeyT;
|
||||
using BlockType = BlockT;
|
||||
|
||||
explicit Cipher<KeyT, BlockT>(PaddingMode mode)
|
||||
: m_padding_mode(mode)
|
||||
{
|
||||
}
|
||||
explicit Cipher<KeyT, BlockT>(PaddingMode mode)
|
||||
: m_padding_mode(mode)
|
||||
{
|
||||
}
|
||||
|
||||
virtual const KeyType& key() const = 0;
|
||||
virtual KeyType& key() = 0;
|
||||
virtual const KeyType& key() const = 0;
|
||||
virtual KeyType& key() = 0;
|
||||
|
||||
static size_t block_size() { return BlockType::block_size(); }
|
||||
static size_t block_size() { return BlockType::block_size(); }
|
||||
|
||||
PaddingMode padding_mode() const { return m_padding_mode; }
|
||||
PaddingMode padding_mode() const { return m_padding_mode; }
|
||||
|
||||
virtual void encrypt_block(const BlockType& in, BlockType& out) = 0;
|
||||
virtual void decrypt_block(const BlockType& in, BlockType& out) = 0;
|
||||
virtual void encrypt_block(const BlockType& in, BlockType& out) = 0;
|
||||
virtual void decrypt_block(const BlockType& in, BlockType& out) = 0;
|
||||
|
||||
private:
|
||||
PaddingMode m_padding_mode { PaddingMode::CMS };
|
||||
};
|
||||
private:
|
||||
PaddingMode m_padding_mode;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -29,86 +29,89 @@
|
|||
#include <LibCrypto/Cipher/Mode/Mode.h>
|
||||
|
||||
namespace Crypto {
|
||||
namespace Cipher {
|
||||
|
||||
template <typename T>
|
||||
class CBC : public Mode<T> {
|
||||
public:
|
||||
template <typename... Args>
|
||||
explicit constexpr CBC<T>(Args... args)
|
||||
: Mode<T>(args...)
|
||||
{
|
||||
}
|
||||
|
||||
virtual Optional<ByteBuffer> encrypt(const ByteBuffer& in, ByteBuffer& out, Optional<ByteBuffer> ivec = {}) override
|
||||
{
|
||||
auto length = in.size();
|
||||
if (length == 0)
|
||||
return {};
|
||||
|
||||
auto& cipher = this->cipher();
|
||||
|
||||
// FIXME: We should have two of these encrypt/decrypt functions that
|
||||
// we SFINAE out based on whether the Cipher mode needs an ivec
|
||||
ASSERT(ivec.has_value());
|
||||
const auto* iv = ivec.value().data();
|
||||
|
||||
typename T::BlockType block { cipher.padding_mode() };
|
||||
size_t offset { 0 };
|
||||
auto block_size = cipher.block_size();
|
||||
|
||||
while (length >= block_size) {
|
||||
block.overwrite(in.slice_view(offset, block_size));
|
||||
block.apply_initialization_vector(iv);
|
||||
cipher.encrypt_block(block, block);
|
||||
out.overwrite(offset, block.get().data(), block_size);
|
||||
iv = out.offset_pointer(offset);
|
||||
length -= block_size;
|
||||
offset += block_size;
|
||||
template <typename T>
|
||||
class CBC : public Mode<T> {
|
||||
public:
|
||||
template <typename... Args>
|
||||
explicit constexpr CBC<T>(Args... args)
|
||||
: Mode<T>(args...)
|
||||
{
|
||||
}
|
||||
|
||||
if (length > 0) {
|
||||
block.overwrite(in.slice_view(offset, length));
|
||||
block.apply_initialization_vector(iv);
|
||||
cipher.encrypt_block(block, block);
|
||||
out.overwrite(offset, block.get().data(), block_size);
|
||||
iv = out.offset_pointer(offset);
|
||||
virtual Optional<ByteBuffer> encrypt(const ByteBuffer& in, ByteBuffer& out, Optional<ByteBuffer> ivec = {}) override
|
||||
{
|
||||
auto length = in.size();
|
||||
if (length == 0)
|
||||
return {};
|
||||
|
||||
auto& cipher = this->cipher();
|
||||
|
||||
// FIXME: We should have two of these encrypt/decrypt functions that
|
||||
// we SFINAE out based on whether the Cipher mode needs an ivec
|
||||
ASSERT(ivec.has_value());
|
||||
const auto* iv = ivec.value().data();
|
||||
|
||||
typename T::BlockType block { cipher.padding_mode() };
|
||||
size_t offset { 0 };
|
||||
auto block_size = cipher.block_size();
|
||||
|
||||
while (length >= block_size) {
|
||||
block.overwrite(in.slice_view(offset, block_size));
|
||||
block.apply_initialization_vector(iv);
|
||||
cipher.encrypt_block(block, block);
|
||||
out.overwrite(offset, block.get().data(), block_size);
|
||||
iv = out.offset_pointer(offset);
|
||||
length -= block_size;
|
||||
offset += block_size;
|
||||
}
|
||||
|
||||
if (length > 0) {
|
||||
block.overwrite(in.slice_view(offset, length));
|
||||
block.apply_initialization_vector(iv);
|
||||
cipher.encrypt_block(block, block);
|
||||
out.overwrite(offset, block.get().data(), block_size);
|
||||
iv = out.offset_pointer(offset);
|
||||
}
|
||||
|
||||
return ByteBuffer::copy(iv, block_size);
|
||||
}
|
||||
virtual void decrypt(const ByteBuffer& in, ByteBuffer& out, Optional<ByteBuffer> ivec = {}) override
|
||||
{
|
||||
auto length = in.size();
|
||||
if (length == 0)
|
||||
return;
|
||||
|
||||
return ByteBuffer::copy(iv, block_size);
|
||||
}
|
||||
virtual void decrypt(const ByteBuffer& in, ByteBuffer& out, Optional<ByteBuffer> ivec = {}) override
|
||||
{
|
||||
auto length = in.size();
|
||||
if (length == 0)
|
||||
return;
|
||||
auto& cipher = this->cipher();
|
||||
|
||||
auto& cipher = this->cipher();
|
||||
ASSERT(ivec.has_value());
|
||||
const auto* iv = ivec.value().data();
|
||||
|
||||
ASSERT(ivec.has_value());
|
||||
const auto* iv = ivec.value().data();
|
||||
auto block_size = cipher.block_size();
|
||||
|
||||
auto block_size = cipher.block_size();
|
||||
// if the data is not aligned, it's not correct encrypted data
|
||||
// FIXME (ponder): Should we simply decrypt as much as we can?
|
||||
ASSERT(length % block_size == 0);
|
||||
|
||||
// if the data is not aligned, it's not correct encrypted data
|
||||
// FIXME (ponder): Should we simply decrypt as much as we can?
|
||||
ASSERT(length % block_size == 0);
|
||||
typename T::BlockType block { cipher.padding_mode() };
|
||||
size_t offset { 0 };
|
||||
|
||||
typename T::BlockType block { cipher.padding_mode() };
|
||||
size_t offset { 0 };
|
||||
|
||||
while (length > 0) {
|
||||
auto* slice = in.offset_pointer(offset);
|
||||
block.overwrite(slice, block_size);
|
||||
cipher.decrypt_block(block, block);
|
||||
block.apply_initialization_vector(iv);
|
||||
auto decrypted = block.get();
|
||||
out.overwrite(offset, decrypted.data(), decrypted.size());
|
||||
iv = slice;
|
||||
length -= block_size;
|
||||
offset += block_size;
|
||||
while (length > 0) {
|
||||
auto* slice = in.offset_pointer(offset);
|
||||
block.overwrite(slice, block_size);
|
||||
cipher.decrypt_block(block, block);
|
||||
block.apply_initialization_vector(iv);
|
||||
auto decrypted = block.get();
|
||||
out.overwrite(offset, decrypted.data(), decrypted.size());
|
||||
iv = slice;
|
||||
length -= block_size;
|
||||
offset += block_size;
|
||||
}
|
||||
this->prune_padding(out);
|
||||
}
|
||||
this->prune_padding(out);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -30,68 +30,71 @@
|
|||
#include <LibCrypto/Cipher/Cipher.h>
|
||||
|
||||
namespace Crypto {
|
||||
namespace Cipher {
|
||||
|
||||
template <typename T>
|
||||
class Mode {
|
||||
public:
|
||||
// FIXME: Somehow communicate that encrypt returns the last initialization vector (if the mode supports it)
|
||||
virtual Optional<ByteBuffer> encrypt(const ByteBuffer& in, ByteBuffer& out, Optional<ByteBuffer> ivec = {}) = 0;
|
||||
virtual void decrypt(const ByteBuffer& in, ByteBuffer& out, Optional<ByteBuffer> ivec = {}) = 0;
|
||||
template <typename T>
|
||||
class Mode {
|
||||
public:
|
||||
// FIXME: Somehow communicate that encrypt returns the last initialization vector (if the mode supports it)
|
||||
virtual Optional<ByteBuffer> encrypt(const ByteBuffer& in, ByteBuffer& out, Optional<ByteBuffer> ivec = {}) = 0;
|
||||
virtual void decrypt(const ByteBuffer& in, ByteBuffer& out, Optional<ByteBuffer> ivec = {}) = 0;
|
||||
|
||||
const T& cipher() const { return m_cipher; }
|
||||
const T& cipher() const { return m_cipher; }
|
||||
|
||||
ByteBuffer create_aligned_buffer(size_t input_size) const
|
||||
{
|
||||
size_t remainder = (input_size + T::block_size()) % T::block_size();
|
||||
if (remainder == 0)
|
||||
return ByteBuffer::create_uninitialized(input_size);
|
||||
else
|
||||
return ByteBuffer::create_uninitialized(input_size + T::block_size() - remainder);
|
||||
}
|
||||
ByteBuffer create_aligned_buffer(size_t input_size) const
|
||||
{
|
||||
size_t remainder = (input_size + T::block_size()) % T::block_size();
|
||||
if (remainder == 0)
|
||||
return ByteBuffer::create_uninitialized(input_size);
|
||||
else
|
||||
return ByteBuffer::create_uninitialized(input_size + T::block_size() - remainder);
|
||||
}
|
||||
|
||||
protected:
|
||||
T& cipher() { return m_cipher; }
|
||||
protected:
|
||||
T& cipher() { return m_cipher; }
|
||||
|
||||
virtual void prune_padding(ByteBuffer& data)
|
||||
{
|
||||
auto size = data.size();
|
||||
switch (m_cipher.padding_mode()) {
|
||||
case PaddingMode::CMS: {
|
||||
auto maybe_padding_length = data[size - 1];
|
||||
if (maybe_padding_length >= T::block_size()) {
|
||||
// cannot be padding (the entire block cannot be padding)
|
||||
return;
|
||||
}
|
||||
for (auto i = maybe_padding_length; i > 0; --i) {
|
||||
if (data[size - i] != maybe_padding_length) {
|
||||
// not padding, part of data
|
||||
virtual void prune_padding(ByteBuffer& data)
|
||||
{
|
||||
auto size = data.size();
|
||||
switch (m_cipher.padding_mode()) {
|
||||
case PaddingMode::CMS: {
|
||||
auto maybe_padding_length = data[size - 1];
|
||||
if (maybe_padding_length >= T::block_size()) {
|
||||
// cannot be padding (the entire block cannot be padding)
|
||||
return;
|
||||
}
|
||||
for (auto i = maybe_padding_length; i > 0; --i) {
|
||||
if (data[size - i] != maybe_padding_length) {
|
||||
// not padding, part of data
|
||||
return;
|
||||
}
|
||||
}
|
||||
data.trim(size - maybe_padding_length);
|
||||
break;
|
||||
}
|
||||
case PaddingMode::Null: {
|
||||
while (data[size - 1] == 0)
|
||||
--size;
|
||||
data.trim(size);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
// FIXME: support other padding modes
|
||||
ASSERT_NOT_REACHED();
|
||||
break;
|
||||
}
|
||||
data.trim(size - maybe_padding_length);
|
||||
break;
|
||||
}
|
||||
case PaddingMode::Null: {
|
||||
while (data[size - 1] == 0)
|
||||
--size;
|
||||
data.trim(size);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
// FIXME: support other padding modes
|
||||
ASSERT_NOT_REACHED();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: Somehow add a reference version of this
|
||||
template <typename... Args>
|
||||
Mode(Args... args)
|
||||
: m_cipher(args...)
|
||||
{
|
||||
}
|
||||
// FIXME: Somehow add a reference version of this
|
||||
template <typename... Args>
|
||||
Mode(Args... args)
|
||||
: m_cipher(args...)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
T m_cipher;
|
||||
};
|
||||
}
|
||||
|
||||
private:
|
||||
T m_cipher;
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue