LibCrypto: Add ChaCha20

This commit is contained in:
stelar7 2022-04-10 13:39:56 +02:00 committed by Ali Mohammad Pur
parent 5397278bfc
commit 7bd0ebb1ab
5 changed files with 334 additions and 0 deletions

View file

@ -2,6 +2,7 @@ set(TEST_SOURCES
TestAES.cpp
TestBigInteger.cpp
TestChecksum.cpp
TestChaCha20.cpp
TestCurves.cpp
TestHash.cpp
TestHMAC.cpp

View file

@ -0,0 +1,141 @@
/*
* Copyright (c) 2022, stelar7 <dudedbz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteBuffer.h>
#include <LibCrypto/Cipher/ChaCha20.h>
#include <LibTest/TestCase.h>
// https://datatracker.ietf.org/doc/html/rfc7539#appendix-A.2
TEST_CASE(test_vector_1)
{
u8 key[32] = {};
u8 nonce[12] = {};
u32 initial_block_counter = {};
u8 plaintext[64] = {};
u8 ciphertext[64] {
0x76, 0xb8, 0xe0, 0xad, 0xa0, 0xf1, 0x3d, 0x90, 0x40, 0x5d, 0x6a, 0xe5, 0x53, 0x86, 0xbd, 0x28,
0xbd, 0xd2, 0x19, 0xb8, 0xa0, 0x8d, 0xed, 0x1a, 0xa8, 0x36, 0xef, 0xcc, 0x8b, 0x77, 0x0d, 0xc7,
0xda, 0x41, 0x59, 0x7c, 0x51, 0x57, 0x48, 0x8d, 0x77, 0x24, 0xe0, 0x3f, 0xb8, 0xd8, 0x4a, 0x37,
0x6a, 0x43, 0xb8, 0xf4, 0x15, 0x18, 0xa1, 0x1c, 0xc3, 0x87, 0xb6, 0x69, 0xb2, 0xee, 0x65, 0x86
};
auto result = MUST(ByteBuffer::create_uninitialized(64));
auto output = result.bytes();
Crypto::Cipher::ChaCha20 cipher(ReadonlyBytes { key, 32 }, ReadonlyBytes { nonce, 12 }, initial_block_counter);
cipher.encrypt(ReadonlyBytes { plaintext, 64 }, output);
auto expected = ReadonlyBytes { ciphertext, 64 };
EXPECT_EQ(result, expected);
}
TEST_CASE(test_vector_2)
{
u8 key[32] {
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, 1
};
u8 nonce[12] {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2
};
u32 initial_block_counter { 1 };
u8 plaintext[375] {
0x41, 0x6e, 0x79, 0x20, 0x73, 0x75, 0x62, 0x6d, 0x69, 0x73, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x74,
0x6f, 0x20, 0x74, 0x68, 0x65, 0x20, 0x49, 0x45, 0x54, 0x46, 0x20, 0x69, 0x6e, 0x74, 0x65, 0x6e,
0x64, 0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x74, 0x68, 0x65, 0x20, 0x43, 0x6f, 0x6e, 0x74, 0x72,
0x69, 0x62, 0x75, 0x74, 0x6f, 0x72, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x70, 0x75, 0x62, 0x6c, 0x69,
0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x61, 0x73, 0x20, 0x61, 0x6c, 0x6c, 0x20, 0x6f, 0x72,
0x20, 0x70, 0x61, 0x72, 0x74, 0x20, 0x6f, 0x66, 0x20, 0x61, 0x6e, 0x20, 0x49, 0x45, 0x54, 0x46,
0x20, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x20,
0x6f, 0x72, 0x20, 0x52, 0x46, 0x43, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x61, 0x6e, 0x79, 0x20, 0x73,
0x74, 0x61, 0x74, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x6d, 0x61, 0x64, 0x65, 0x20, 0x77, 0x69,
0x74, 0x68, 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x78, 0x74,
0x20, 0x6f, 0x66, 0x20, 0x61, 0x6e, 0x20, 0x49, 0x45, 0x54, 0x46, 0x20, 0x61, 0x63, 0x74, 0x69,
0x76, 0x69, 0x74, 0x79, 0x20, 0x69, 0x73, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x69, 0x64, 0x65, 0x72,
0x65, 0x64, 0x20, 0x61, 0x6e, 0x20, 0x22, 0x49, 0x45, 0x54, 0x46, 0x20, 0x43, 0x6f, 0x6e, 0x74,
0x72, 0x69, 0x62, 0x75, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x2e, 0x20, 0x53, 0x75, 0x63, 0x68, 0x20,
0x73, 0x74, 0x61, 0x74, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x20, 0x69, 0x6e, 0x63, 0x6c, 0x75,
0x64, 0x65, 0x20, 0x6f, 0x72, 0x61, 0x6c, 0x20, 0x73, 0x74, 0x61, 0x74, 0x65, 0x6d, 0x65, 0x6e,
0x74, 0x73, 0x20, 0x69, 0x6e, 0x20, 0x49, 0x45, 0x54, 0x46, 0x20, 0x73, 0x65, 0x73, 0x73, 0x69,
0x6f, 0x6e, 0x73, 0x2c, 0x20, 0x61, 0x73, 0x20, 0x77, 0x65, 0x6c, 0x6c, 0x20, 0x61, 0x73, 0x20,
0x77, 0x72, 0x69, 0x74, 0x74, 0x65, 0x6e, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x65, 0x6c, 0x65, 0x63,
0x74, 0x72, 0x6f, 0x6e, 0x69, 0x63, 0x20, 0x63, 0x6f, 0x6d, 0x6d, 0x75, 0x6e, 0x69, 0x63, 0x61,
0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x6d, 0x61, 0x64, 0x65, 0x20, 0x61, 0x74, 0x20, 0x61, 0x6e,
0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20, 0x6f, 0x72, 0x20, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x2c,
0x20, 0x77, 0x68, 0x69, 0x63, 0x68, 0x20, 0x61, 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0xab,
0x7a, 0xc6, 0x1d, 0xd2, 0x9c, 0x6f, 0x21, 0xba, 0x5b, 0x86, 0x2f, 0x37, 0x30, 0xe3, 0x7c, 0xfd,
0xc4, 0xfd, 0x80, 0x6c, 0x22, 0xf2, 0x21
};
auto result = MUST(ByteBuffer::create_uninitialized(375));
auto output = result.bytes();
Crypto::Cipher::ChaCha20 cipher(ReadonlyBytes { key, 32 }, ReadonlyBytes { nonce, 12 }, initial_block_counter);
cipher.encrypt(ReadonlyBytes { plaintext, 375 }, output);
auto expected = ReadonlyBytes { ciphertext, 375 };
EXPECT_EQ(result, expected);
}
TEST_CASE(test_vector_3)
{
u8 key[32] {
0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a, 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09, 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
};
u8 nonce[12] {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2
};
u32 initial_block_counter { 42 };
u8 plaintext[127] {
0x27, 0x54, 0x77, 0x61, 0x73, 0x20, 0x62, 0x72, 0x69, 0x6c, 0x6c, 0x69, 0x67, 0x2c, 0x20, 0x61,
0x6e, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73, 0x6c, 0x69, 0x74, 0x68, 0x79, 0x20, 0x74, 0x6f,
0x76, 0x65, 0x73, 0x0a, 0x44, 0x69, 0x64, 0x20, 0x67, 0x79, 0x72, 0x65, 0x20, 0x61, 0x6e, 0x64,
0x20, 0x67, 0x69, 0x6d, 0x62, 0x6c, 0x65, 0x20, 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77,
0x61, 0x62, 0x65, 0x3a, 0x0a, 0x41, 0x6c, 0x6c, 0x20, 0x6d, 0x69, 0x6d, 0x73, 0x79, 0x20, 0x77,
0x65, 0x72, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 0x6f, 0x72, 0x6f, 0x67, 0x6f, 0x76, 0x65,
0x73, 0x2c, 0x0a, 0x41, 0x6e, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6d, 0x6f, 0x6d, 0x65, 0x20,
0x72, 0x61, 0x74, 0x68, 0x73, 0x20, 0x6f, 0x75, 0x74, 0x67, 0x72, 0x61, 0x62, 0x65, 0x2e
};
u8 ciphertext[127] {
0x62, 0xe6, 0x34, 0x7f, 0x95, 0xed, 0x87, 0xa4, 0x5f, 0xfa, 0xe7, 0x42, 0x6f, 0x27, 0xa1, 0xdf,
0x5f, 0xb6, 0x91, 0x10, 0x04, 0x4c, 0x0d, 0x73, 0x11, 0x8e, 0xff, 0xa9, 0x5b, 0x01, 0xe5, 0xcf,
0x16, 0x6d, 0x3d, 0xf2, 0xd7, 0x21, 0xca, 0xf9, 0xb2, 0x1e, 0x5f, 0xb1, 0x4c, 0x61, 0x68, 0x71,
0xfd, 0x84, 0xc5, 0x4f, 0x9d, 0x65, 0xb2, 0x83, 0x19, 0x6c, 0x7f, 0xe4, 0xf6, 0x05, 0x53, 0xeb,
0xf3, 0x9c, 0x64, 0x02, 0xc4, 0x22, 0x34, 0xe3, 0x2a, 0x35, 0x6b, 0x3e, 0x76, 0x43, 0x12, 0xa6,
0x1a, 0x55, 0x32, 0x05, 0x57, 0x16, 0xea, 0xd6, 0x96, 0x25, 0x68, 0xf8, 0x7d, 0x3f, 0x3f, 0x77,
0x04, 0xc6, 0xa8, 0xd1, 0xbc, 0xd1, 0xbf, 0x4d, 0x50, 0xd6, 0x15, 0x4b, 0x6d, 0xa7, 0x31, 0xb1,
0x87, 0xb5, 0x8d, 0xfd, 0x72, 0x8a, 0xfa, 0x36, 0x75, 0x7a, 0x79, 0x7a, 0xc1, 0x88, 0xd1
};
auto result = MUST(ByteBuffer::create_uninitialized(127));
auto output = result.bytes();
Crypto::Cipher::ChaCha20 cipher(ReadonlyBytes { key, 32 }, ReadonlyBytes { nonce, 12 }, initial_block_counter);
cipher.encrypt(ReadonlyBytes { plaintext, 127 }, output);
auto expected = ReadonlyBytes { ciphertext, 127 };
EXPECT_EQ(result, expected);
}

View file

@ -18,6 +18,7 @@ set(SOURCES
Checksum/Adler32.cpp
Checksum/CRC32.cpp
Cipher/AES.cpp
Cipher/ChaCha20.cpp
Curves/SECP256r1.cpp
Curves/X25519.cpp
Curves/X448.cpp

View file

@ -0,0 +1,160 @@
/*
* Copyright (c) 2022, stelar7 <dudedbz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteReader.h>
#include <AK/Endian.h>
#include <LibCrypto/Cipher/ChaCha20.h>
namespace Crypto::Cipher {
ChaCha20::ChaCha20(ReadonlyBytes key, ReadonlyBytes nonce, u32 initial_counter)
{
VERIFY(key.size() == 16 || key.size() == 32);
VERIFY(nonce.size() == 8 || nonce.size() == 12);
// The first four words (0-3) are constants
if (key.size() == 32) {
m_state[0] = CONSTANT_32_BYTES[0];
m_state[1] = CONSTANT_32_BYTES[1];
m_state[2] = CONSTANT_32_BYTES[2];
m_state[3] = CONSTANT_32_BYTES[3];
} else {
m_state[0] = CONSTANT_16_BYTES[0];
m_state[1] = CONSTANT_16_BYTES[1];
m_state[2] = CONSTANT_16_BYTES[2];
m_state[3] = CONSTANT_16_BYTES[3];
}
// The next eight words (4-11) are taken from the key by reading the bytes in little-endian order, in 4-byte chunks.
for (u32 i = 0; i < 16; i += 4) {
m_state[(i / 4) + 4] = AK::convert_between_host_and_little_endian(ByteReader::load32(key.offset(i)));
}
// NOTE: For the 128-bit keys we read the same bytes twice to fill the state
u32 key_offset = key.size() == 32 ? 16 : 0;
for (u32 i = 0; i < 16; i += 4) {
m_state[(i / 4) + 8] = AK::convert_between_host_and_little_endian(ByteReader::load32(key.offset(key_offset + i)));
}
// Word 12 is a block counter. Since each block is 64-bytes, a 32-bit word is enough for 256 gigabytes of data.
m_state[12] = initial_counter;
// Words 13-15 are a nonce, which should not be repeated for the same key.
// The 13th word is the first 32 bits of the input nonce taken as a little-endian integer,
// while the 15th word is the last 32 bits.
// NOTE: In the case of an 8-byte nonce, we skip the 13th word
u32 nonce_offset = nonce.size() == 8 ? 1 : 0;
for (u32 i = 0; i < 12; i += 4) {
m_state[(i / 4) + 13 + nonce_offset] = AK::convert_between_host_and_little_endian(ByteReader::load32(nonce.offset(i)));
}
}
// https://datatracker.ietf.org/doc/html/rfc7539#section-2.3
void ChaCha20::generate_block()
{
// Copy the current state into the block
memcpy(m_block, m_state, 16 * sizeof(u32));
// ChaCha20 runs 20 rounds, alternating between "column rounds" and "diagonal rounds".
// Each round consists of four quarter-rounds
for (u32 i = 0; i < 20; i += 2) {
// Column rounds
do_quarter_round(m_block[0], m_block[4], m_block[8], m_block[12]);
do_quarter_round(m_block[1], m_block[5], m_block[9], m_block[13]);
do_quarter_round(m_block[2], m_block[6], m_block[10], m_block[14]);
do_quarter_round(m_block[3], m_block[7], m_block[11], m_block[15]);
// Diagonal rounds
do_quarter_round(m_block[0], m_block[5], m_block[10], m_block[15]);
do_quarter_round(m_block[1], m_block[6], m_block[11], m_block[12]);
do_quarter_round(m_block[2], m_block[7], m_block[8], m_block[13]);
do_quarter_round(m_block[3], m_block[4], m_block[9], m_block[14]);
}
// At the end of 20 rounds, we add the original input words to the output words,
for (u32 i = 0; i < 16; i++) {
m_block[i] += m_state[i];
}
// and serialize the result by sequencing the words one-by-one in little-endian order.
for (u32 i = 0; i < 16; i++) {
m_block[i] = AK::convert_between_host_and_little_endian(m_block[i]);
}
}
ALWAYS_INLINE static void rotl(u32& x, u32 n)
{
x = (x << n) | (x >> (32 - n));
}
// https://datatracker.ietf.org/doc/html/rfc8439#section-2.1
void ChaCha20::do_quarter_round(u32& a, u32& b, u32& c, u32& d)
{
a += b;
d ^= a;
rotl(d, 16);
c += d;
b ^= c;
rotl(b, 12);
a += b;
d ^= a;
rotl(d, 8);
c += d;
b ^= c;
rotl(b, 7);
}
void ChaCha20::run_cipher(ReadonlyBytes input, Bytes& output)
{
size_t offset = 0;
size_t block_offset = 0;
while (offset < input.size()) {
if (block_offset == 0 || block_offset >= 64) {
// Generate a new XOR block
generate_block();
// Increment the block counter, and carry over to block 13
m_state[12]++;
if (m_state[12] == 0) {
m_state[13]++;
}
block_offset = 0;
}
// XOR the input and the current block
u32 n = min(input.size() - offset, 64 - block_offset);
u8* key_block = (u8*)m_block + block_offset;
for (u32 i = 0; i < n; i++) {
u8 input_byte = input.offset_pointer(offset)[i];
u8 key_byte = key_block[i];
u8 output_byte = input_byte ^ key_byte;
ByteReader::store(output.offset_pointer(offset + i), output_byte);
}
offset += n;
block_offset += n;
}
}
void ChaCha20::encrypt(ReadonlyBytes input, Bytes& output)
{
VERIFY(input.size() <= output.size());
this->run_cipher(input, output);
}
void ChaCha20::decrypt(ReadonlyBytes input, Bytes& output)
{
VERIFY(input.size() <= output.size());
this->run_cipher(input, output);
}
}

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2022, stelar7 <dudedbz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/ByteBuffer.h>
namespace Crypto::Cipher {
class ChaCha20 {
static constexpr u32 CONSTANT_16_BYTES[] { 0x61707865, 0x3120646E, 0x79622D36, 0x6B206574 };
static constexpr u32 CONSTANT_32_BYTES[] { 0x61707865, 0x3320646E, 0x79622D32, 0x6B206574 };
public:
ChaCha20(ReadonlyBytes key, ReadonlyBytes nonce, u32 initial_counter = 0);
void encrypt(ReadonlyBytes input, Bytes& output);
void decrypt(ReadonlyBytes input, Bytes& output);
private:
void run_cipher(ReadonlyBytes input, Bytes& output);
void generate_block();
ALWAYS_INLINE void do_quarter_round(u32& a, u32& b, u32& c, u32& d);
u32 m_state[16] {};
u32 m_block[16] {};
};
}