diff --git a/Tests/LibCrypto/CMakeLists.txt b/Tests/LibCrypto/CMakeLists.txt index 190335243d..7279dba979 100644 --- a/Tests/LibCrypto/CMakeLists.txt +++ b/Tests/LibCrypto/CMakeLists.txt @@ -4,6 +4,7 @@ set(TEST_SOURCES TestBigInteger.cpp TestChecksum.cpp TestChaCha20.cpp + TestChacha20Poly1305.cpp TestCurves.cpp TestEd25519.cpp TestHash.cpp diff --git a/Tests/LibCrypto/TestChacha20Poly1305.cpp b/Tests/LibCrypto/TestChacha20Poly1305.cpp new file mode 100644 index 0000000000..980a4a06d0 --- /dev/null +++ b/Tests/LibCrypto/TestChacha20Poly1305.cpp @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2023, stelar7 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +// https://datatracker.ietf.org/doc/html/rfc8439#section-2.6.2 +TEST_CASE(test_keygen_vector_1) +{ + u8 key[32] = { + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f + }; + u8 nonce[12] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; + u8 expected_raw[32] = { + 0x8a, 0xd5, 0xa0, 0x8b, 0x90, 0x5f, 0x81, 0xcc, 0x81, 0x50, 0x40, 0x27, 0x4a, 0xb2, 0x94, 0x71, + 0xa8, 0x33, 0xb6, 0x37, 0xe3, 0xfd, 0x0d, 0xa5, 0x08, 0xdb, 0xb8, 0xe2, 0xfd, 0xd1, 0xa6, 0x46 + }; + + Crypto::AEAD::ChaCha20Poly1305 aead(ReadonlyBytes { key, 32 }, ReadonlyBytes { nonce, 12 }); + auto otk = MUST(aead.poly1305_key()); + auto expected = ReadonlyBytes { expected_raw, 32 }; + + EXPECT_EQ(otk.bytes(), expected); +} + +// https://datatracker.ietf.org/doc/html/rfc8439#appendix-A.4 +TEST_CASE(test_keygen_vector_2) +{ + u8 key[32] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + u8 nonce[12] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + u8 expected_raw[32] = { + 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 + }; + + Crypto::AEAD::ChaCha20Poly1305 aead(ReadonlyBytes { key, 32 }, ReadonlyBytes { nonce, 12 }); + auto otk = MUST(aead.poly1305_key()); + auto expected = ReadonlyBytes { expected_raw, 32 }; + + EXPECT_EQ(otk.bytes(), expected); +} + +// https://datatracker.ietf.org/doc/html/rfc8439#appendix-A.4 +TEST_CASE(test_keygen_vector_3) +{ + u8 key[32] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 + }; + u8 nonce[12] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }; + u8 expected_raw[32] = { + 0xec, 0xfa, 0x25, 0x4f, 0x84, 0x5f, 0x64, 0x74, 0x73, 0xd3, 0xcb, 0x14, 0x0d, 0xa9, 0xe8, 0x76, + 0x06, 0xcb, 0x33, 0x06, 0x6c, 0x44, 0x7b, 0x87, 0xbc, 0x26, 0x66, 0xdd, 0xe3, 0xfb, 0xb7, 0x39 + }; + + Crypto::AEAD::ChaCha20Poly1305 aead(ReadonlyBytes { key, 32 }, ReadonlyBytes { nonce, 12 }); + auto otk = MUST(aead.poly1305_key()); + auto expected = ReadonlyBytes { expected_raw, 32 }; + + EXPECT_EQ(otk.bytes(), expected); +} + +// https://datatracker.ietf.org/doc/html/rfc8439#appendix-A.4 +TEST_CASE(test_keygen_vector_4) +{ + 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] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }; + u8 expected_raw[32] = { + 0x96, 0x5e, 0x3b, 0xc6, 0xf9, 0xec, 0x7e, 0xd9, 0x56, 0x08, 0x08, 0xf4, 0xd2, 0x29, 0xf9, 0x4b, + 0x13, 0x7f, 0xf2, 0x75, 0xca, 0x9b, 0x3f, 0xcb, 0xdd, 0x59, 0xde, 0xaa, 0xd2, 0x33, 0x10, 0xae + }; + + Crypto::AEAD::ChaCha20Poly1305 aead(ReadonlyBytes { key, 32 }, ReadonlyBytes { nonce, 12 }); + auto otk = MUST(aead.poly1305_key()); + auto expected = ReadonlyBytes { expected_raw, 32 }; + + EXPECT_EQ(otk.bytes(), expected); +} + +// https://datatracker.ietf.org/doc/html/rfc8439#section-2.8.2 +TEST_CASE(test_aead_encrypt_1) +{ + auto plaintext = MUST(String::from_utf8("Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it."sv)); + u8 aad[12] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; + u8 key[32] = { + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f + }; + u8 nonce[12] = { 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 }; + + u8 expected_ciphertext[114] = { + 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb, 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2, + 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe, 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6, + 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12, 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b, + 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29, 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36, + 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c, 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58, + 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94, 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc, + 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d, 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b, + 0x61, 0x16 + }; + + u8 expected_tag[16] = { + 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a, 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91 + }; + + auto ciphertext = ReadonlyBytes { expected_ciphertext, 114 }; + auto tag = ReadonlyBytes { expected_tag, 16 }; + + Crypto::AEAD::ChaCha20Poly1305 aead(ReadonlyBytes { key, 32 }, ReadonlyBytes { nonce, 12 }); + auto encrypted = MUST(aead.encrypt(ReadonlyBytes { aad, 12 }, plaintext.bytes())); + + EXPECT_EQ(encrypted.size(), plaintext.bytes().size() + 16); + EXPECT_EQ(encrypted.bytes().slice(0, plaintext.bytes().size()), ciphertext); + EXPECT_EQ(encrypted.bytes().slice_from_end(16), tag); +} + +// https://datatracker.ietf.org/doc/html/rfc8439#appendix-A.5 +TEST_CASE(test_aead_decrypt_1) +{ + u8 aad[12] = { 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4e, 0x91 }; + 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] = { 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }; + u8 ciphertext[265] = { + 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4, 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd, + 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89, 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2, + 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee, 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0, + 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00, 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf, + 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce, 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81, + 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd, 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55, + 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61, 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38, + 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0, 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4, + 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46, 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9, + 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e, 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e, + 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15, 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a, + 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea, 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a, + 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99, 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e, + 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10, 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10, + 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94, 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30, + 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf, 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29, + 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70, 0x9b + }; + + u8 expected_plaintext[265] = { + 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20, + 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66, 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, + 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20, + 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20, 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d, + 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65, + 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63, + 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f, 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64, + 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, + 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61, 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e, + 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69, 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72, + 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20, 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65, + 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72, + 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65, 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61, + 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20, 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65, + 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20, + 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b, 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67, + 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80, 0x9d + }; + + u8 expected_tag[16] = { + 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22, 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38 + }; + + auto plaintext = ReadonlyBytes { expected_plaintext, 265 }; + auto tag = ReadonlyBytes { expected_tag, 16 }; + + Crypto::AEAD::ChaCha20Poly1305 aead(ReadonlyBytes { key, 32 }, ReadonlyBytes { nonce, 12 }); + auto decrypted = MUST(aead.decrypt(ReadonlyBytes { aad, 12 }, ReadonlyBytes { ciphertext, 265 })); + + EXPECT_EQ(decrypted.size(), plaintext.size() + 16); + EXPECT_EQ(decrypted.bytes().slice(0, plaintext.size()), plaintext); + EXPECT_EQ(decrypted.bytes().slice_from_end(16), tag); +} + +TEST_CASE(test_aead_encrypt_and_decrypt) +{ + auto plaintext = MUST(String::from_utf8("Well, hello friends :)"sv)); + u8 aad[12] = { 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7 }; + u8 key[32] = { + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f + }; + u8 nonce[12] = { 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 }; + + Crypto::AEAD::ChaCha20Poly1305 aead(ReadonlyBytes { key, 32 }, ReadonlyBytes { nonce, 12 }); + auto encrypted = MUST(aead.encrypt(ReadonlyBytes { aad, 12 }, plaintext.bytes())); + auto decrypted = MUST(aead.decrypt(ReadonlyBytes { aad, 12 }, encrypted.bytes().slice(0, encrypted.bytes().size() - 16))); + + EXPECT(Crypto::AEAD::ChaCha20Poly1305::verify_tag(encrypted, decrypted)); + EXPECT_EQ(decrypted.bytes().slice(0, encrypted.bytes().size() - 16), plaintext.bytes()); +} diff --git a/Userland/Libraries/LibCrypto/AEAD/ChaCha20Poly1305.cpp b/Userland/Libraries/LibCrypto/AEAD/ChaCha20Poly1305.cpp new file mode 100644 index 0000000000..92c632c1e8 --- /dev/null +++ b/Userland/Libraries/LibCrypto/AEAD/ChaCha20Poly1305.cpp @@ -0,0 +1,182 @@ +/* + * Copyright (c) 2023, stelar7 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include + +namespace Crypto::AEAD { + +// https://datatracker.ietf.org/doc/html/rfc8439#section-2.6 +ErrorOr ChaCha20Poly1305::poly1305_key() +{ + Crypto::Cipher::ChaCha20 cipher(m_key, m_nonce, 0); + cipher.generate_block(); + auto state = cipher.block(); + return TRY(ByteBuffer::copy(state.slice(0, 32))); +} + +// https://datatracker.ietf.org/doc/html/rfc8439#section-2.8 +ErrorOr ChaCha20Poly1305::encrypt(ReadonlyBytes aad, ReadonlyBytes input_plaintext) +{ + // First, a Poly1305 one-time key is generated from the 256-bit key + // and nonce using the procedure described in Section 2.6. + auto otk = TRY(poly1305_key()); + + // Next, the ChaCha20 encryption function is called to encrypt the + // plaintext, using the same key and nonce, and with the initial + // counter set to 1. + auto ciphertext_buffer = TRY(ByteBuffer::create_zeroed(input_plaintext.size())); + auto ciphertext = ciphertext_buffer.bytes(); + auto chacha = Crypto::Cipher::ChaCha20(m_key, m_nonce, 1); + chacha.encrypt(input_plaintext, ciphertext); + + // Finally, the Poly1305 function is called with the Poly1305 key + // calculated above, and a message constructed as a concatenation of + // the following: + auto mac_data = TRY(ByteBuffer::create_zeroed(0)); + auto buffer_size = aad.size() + pad_to_16(aad) + ciphertext_buffer.size() + pad_to_16(ciphertext_buffer) + sizeof(u64) + sizeof(u64); + mac_data.ensure_capacity(buffer_size); + + // The AAD + mac_data.append(aad); + + // padding1 -- the padding is up to 15 zero bytes, and it brings + // the total length so far to an integral multiple of 16. If the + // length of the AAD was already an integral multiple of 16 bytes, + // this field is zero-length. + for (size_t i = 0; i < pad_to_16(aad); ++i) + mac_data.append(0); + + // The ciphertext + mac_data.append(ciphertext); + + // padding2 -- the padding is up to 15 zero bytes, and it brings + // the total length so far to an integral multiple of 16. If the + // length of the ciphertext was already an integral multiple of 16 + // bytes, this field is zero-length. + for (size_t i = 0; i < pad_to_16(ciphertext); ++i) + mac_data.append(0); + + u8 placeholder[8] = { 0 }; + // The length of the additional data in octets (as a 64-bit little-endian integer). + mac_data.append(ReadonlyBytes { placeholder, 8 }); + ByteReader::store(static_cast(mac_data.end_pointer()) - sizeof(u64), AK::convert_between_host_and_little_endian(static_cast(aad.size()))); + + // The length of the ciphertext in octets (as a 64-bit little-endian integer). + mac_data.append(ReadonlyBytes { placeholder, 8 }); + ByteReader::store(static_cast(mac_data.end_pointer()) - sizeof(u64), AK::convert_between_host_and_little_endian(static_cast(ciphertext.size()))); + + Crypto::Authentication::Poly1305 mac_function(otk); + mac_function.update(mac_data.bytes()); + auto tag = TRY(mac_function.digest()); + + // The output from the AEAD is the concatenation of: + auto result = TRY(ByteBuffer::create_zeroed(0)); + result.ensure_capacity(ciphertext.size() + tag.size()); + + // A ciphertext of the same length as the plaintext. + result.append(ciphertext); + + // A 128-bit tag, which is the output of the Poly1305 function. + result.append(tag); + return result; +} + +// https://datatracker.ietf.org/doc/html/rfc8439#section-2.8 +ErrorOr ChaCha20Poly1305::decrypt(ReadonlyBytes aad, ReadonlyBytes ciphertext) +{ + // Decryption is similar with the following differences: + // o The roles of ciphertext and plaintext are reversed, so the + // ChaCha20 encryption function is applied to the ciphertext, + // producing the plaintext. + // o The Poly1305 function is still run on the AAD and the ciphertext, + // not the plaintext. + + // First, a Poly1305 one-time key is generated from the 256-bit key + // and nonce using the procedure described in Section 2.6. + auto otk = TRY(poly1305_key()); + + // Next, the ChaCha20 encryption function is called to decrypt the + // ciphertext, using the same key and nonce, and with the initial + // counter set to 1. + auto chacha = Crypto::Cipher::ChaCha20(m_key, m_nonce, 1); + auto plaintext_buffer = TRY(ByteBuffer::create_zeroed(ciphertext.size())); + auto plaintext = plaintext_buffer.bytes(); + chacha.encrypt(ciphertext, plaintext); + + // Finally, the Poly1305 function is called with the Poly1305 key + // calculated above, and a message constructed as a concatenation of + // the following: + auto mac_data = TRY(ByteBuffer::create_zeroed(0)); + auto buffer_size = aad.size() + pad_to_16(aad) + ciphertext.size() + pad_to_16(ciphertext) + sizeof(u64) + sizeof(u64); + mac_data.ensure_capacity(buffer_size); + + // The AAD + mac_data.append(aad); + + // padding1 -- the padding is up to 15 zero bytes, and it brings + // the total length so far to an integral multiple of 16. If the + // length of the AAD was already an integral multiple of 16 bytes, + // this field is zero-length. + for (size_t i = 0; i < pad_to_16(aad); ++i) + mac_data.append(0); + + // The ciphertext + mac_data.append(ciphertext); + + // padding2 -- the padding is up to 15 zero bytes, and it brings + // the total length so far to an integral multiple of 16. If the + // length of the ciphertext was already an integral multiple of 16 + // bytes, this field is zero-length. + for (size_t i = 0; i < pad_to_16(ciphertext); ++i) + mac_data.append(0); + + u8 placeholder[8] = { 0 }; + // The length of the additional data in octets (as a 64-bit little-endian integer). + mac_data.append(ReadonlyBytes { placeholder, 8 }); + ByteReader::store(static_cast(mac_data.end_pointer()) - sizeof(u64), AK::convert_between_host_and_little_endian(static_cast(aad.size()))); + + // The length of the ciphertext in octets (as a 64-bit little-endian integer). + mac_data.append(ReadonlyBytes { placeholder, 8 }); + ByteReader::store(static_cast(mac_data.end_pointer()) - sizeof(u64), AK::convert_between_host_and_little_endian(static_cast(ciphertext.size()))); + + Crypto::Authentication::Poly1305 mac_function(otk); + mac_function.update(mac_data.bytes()); + auto tag = TRY(mac_function.digest()); + + // The output from the AEAD is the concatenation of: + auto result = TRY(ByteBuffer::create_zeroed(0)); + result.ensure_capacity(plaintext.size() + tag.size()); + + // A plaintext of the same length as the ciphertext. + result.append(plaintext); + + // A 128-bit tag, which is the output of the Poly1305 function. + result.append(tag); + return result; +} + +// https://datatracker.ietf.org/doc/html/rfc8439#section-4 +bool ChaCha20Poly1305::verify_tag(ReadonlyBytes encrypted, ReadonlyBytes decrypted) +{ + // With online protocols, implementation MUST use a constant-time comparison function rather + // than relying on optimized but insecure library functions such as the C language's memcmp(). + auto encrypted_tag = encrypted.slice_from_end(16); + auto decrypted_tag = decrypted.slice_from_end(16); + + if (encrypted_tag.size() != decrypted_tag.size()) + return false; + + auto result = 0; + for (size_t i = 0; i < encrypted_tag.size(); ++i) + result |= encrypted_tag[i] ^ decrypted_tag[i]; + + return result == 0; +} +} diff --git a/Userland/Libraries/LibCrypto/AEAD/ChaCha20Poly1305.h b/Userland/Libraries/LibCrypto/AEAD/ChaCha20Poly1305.h new file mode 100644 index 0000000000..08b1a1e0aa --- /dev/null +++ b/Userland/Libraries/LibCrypto/AEAD/ChaCha20Poly1305.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023, stelar7 + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Crypto::AEAD { + +class ChaCha20Poly1305 { +public: + explicit ChaCha20Poly1305(ReadonlyBytes key, ReadonlyBytes nonce) + { + m_key = MUST(ByteBuffer::copy(key)); + m_nonce = MUST(ByteBuffer::copy(nonce)); + } + + ErrorOr encrypt(ReadonlyBytes aad, ReadonlyBytes plaintext); + ErrorOr decrypt(ReadonlyBytes aad, ReadonlyBytes ciphertext); + ErrorOr poly1305_key(); + static bool verify_tag(ReadonlyBytes encrypted, ReadonlyBytes decrypted); + +private: + u8 pad_to_16(ReadonlyBytes data) + { + return 16 - (data.size() % 16); + } + + ByteBuffer m_key; + ByteBuffer m_nonce; +}; +} diff --git a/Userland/Libraries/LibCrypto/CMakeLists.txt b/Userland/Libraries/LibCrypto/CMakeLists.txt index 184239bcc1..21ab5436de 100644 --- a/Userland/Libraries/LibCrypto/CMakeLists.txt +++ b/Userland/Libraries/LibCrypto/CMakeLists.txt @@ -1,6 +1,7 @@ add_compile_options(-Wvla) set(SOURCES + AEAD/ChaCha20Poly1305.cpp ASN1/ASN1.cpp ASN1/DER.cpp ASN1/PEM.cpp diff --git a/Userland/Libraries/LibCrypto/Cipher/ChaCha20.h b/Userland/Libraries/LibCrypto/Cipher/ChaCha20.h index 74cf573575..333bb6d6b2 100644 --- a/Userland/Libraries/LibCrypto/Cipher/ChaCha20.h +++ b/Userland/Libraries/LibCrypto/Cipher/ChaCha20.h @@ -19,10 +19,11 @@ public: void encrypt(ReadonlyBytes input, Bytes& output); void decrypt(ReadonlyBytes input, Bytes& output); + void generate_block(); + ReadonlyBytes block() const { return { m_block, 64 }; } 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] {};