ChaCha20Poly1305.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteReader.h>
  7. #include <AK/Endian.h>
  8. #include <LibCrypto/AEAD/ChaCha20Poly1305.h>
  9. #include <LibCrypto/Authentication/Poly1305.h>
  10. #include <LibCrypto/Cipher/ChaCha20.h>
  11. namespace Crypto::AEAD {
  12. // https://datatracker.ietf.org/doc/html/rfc8439#section-2.6
  13. ErrorOr<ByteBuffer> ChaCha20Poly1305::poly1305_key()
  14. {
  15. Crypto::Cipher::ChaCha20 cipher(m_key, m_nonce, 0);
  16. cipher.generate_block();
  17. auto state = cipher.block();
  18. return TRY(ByteBuffer::copy(state.slice(0, 32)));
  19. }
  20. // https://datatracker.ietf.org/doc/html/rfc8439#section-2.8
  21. ErrorOr<ByteBuffer> ChaCha20Poly1305::encrypt(ReadonlyBytes aad, ReadonlyBytes input_plaintext)
  22. {
  23. // First, a Poly1305 one-time key is generated from the 256-bit key
  24. // and nonce using the procedure described in Section 2.6.
  25. auto otk = TRY(poly1305_key());
  26. // Next, the ChaCha20 encryption function is called to encrypt the
  27. // plaintext, using the same key and nonce, and with the initial
  28. // counter set to 1.
  29. auto ciphertext_buffer = TRY(ByteBuffer::create_zeroed(input_plaintext.size()));
  30. auto ciphertext = ciphertext_buffer.bytes();
  31. auto chacha = Crypto::Cipher::ChaCha20(m_key, m_nonce, 1);
  32. chacha.encrypt(input_plaintext, ciphertext);
  33. // Finally, the Poly1305 function is called with the Poly1305 key
  34. // calculated above, and a message constructed as a concatenation of
  35. // the following:
  36. auto mac_data = TRY(ByteBuffer::create_zeroed(0));
  37. auto buffer_size = aad.size() + pad_to_16(aad) + ciphertext_buffer.size() + pad_to_16(ciphertext_buffer) + sizeof(u64) + sizeof(u64);
  38. mac_data.ensure_capacity(buffer_size);
  39. // The AAD
  40. mac_data.append(aad);
  41. // padding1 -- the padding is up to 15 zero bytes, and it brings
  42. // the total length so far to an integral multiple of 16. If the
  43. // length of the AAD was already an integral multiple of 16 bytes,
  44. // this field is zero-length.
  45. for (size_t i = 0; i < pad_to_16(aad); ++i)
  46. mac_data.append(0);
  47. // The ciphertext
  48. mac_data.append(ciphertext);
  49. // padding2 -- the padding is up to 15 zero bytes, and it brings
  50. // the total length so far to an integral multiple of 16. If the
  51. // length of the ciphertext was already an integral multiple of 16
  52. // bytes, this field is zero-length.
  53. for (size_t i = 0; i < pad_to_16(ciphertext); ++i)
  54. mac_data.append(0);
  55. u8 placeholder[8] = { 0 };
  56. // The length of the additional data in octets (as a 64-bit little-endian integer).
  57. mac_data.append(ReadonlyBytes { placeholder, 8 });
  58. ByteReader::store(static_cast<u8*>(mac_data.end_pointer()) - sizeof(u64), AK::convert_between_host_and_little_endian(static_cast<u64>(aad.size())));
  59. // The length of the ciphertext in octets (as a 64-bit little-endian integer).
  60. mac_data.append(ReadonlyBytes { placeholder, 8 });
  61. ByteReader::store(static_cast<u8*>(mac_data.end_pointer()) - sizeof(u64), AK::convert_between_host_and_little_endian(static_cast<u64>(ciphertext.size())));
  62. Crypto::Authentication::Poly1305 mac_function(otk);
  63. mac_function.update(mac_data.bytes());
  64. auto tag = TRY(mac_function.digest());
  65. // The output from the AEAD is the concatenation of:
  66. auto result = TRY(ByteBuffer::create_zeroed(0));
  67. result.ensure_capacity(ciphertext.size() + tag.size());
  68. // A ciphertext of the same length as the plaintext.
  69. result.append(ciphertext);
  70. // A 128-bit tag, which is the output of the Poly1305 function.
  71. result.append(tag);
  72. return result;
  73. }
  74. // https://datatracker.ietf.org/doc/html/rfc8439#section-2.8
  75. ErrorOr<ByteBuffer> ChaCha20Poly1305::decrypt(ReadonlyBytes aad, ReadonlyBytes ciphertext)
  76. {
  77. // Decryption is similar with the following differences:
  78. // o The roles of ciphertext and plaintext are reversed, so the
  79. // ChaCha20 encryption function is applied to the ciphertext,
  80. // producing the plaintext.
  81. // o The Poly1305 function is still run on the AAD and the ciphertext,
  82. // not the plaintext.
  83. // First, a Poly1305 one-time key is generated from the 256-bit key
  84. // and nonce using the procedure described in Section 2.6.
  85. auto otk = TRY(poly1305_key());
  86. // Next, the ChaCha20 encryption function is called to decrypt the
  87. // ciphertext, using the same key and nonce, and with the initial
  88. // counter set to 1.
  89. auto chacha = Crypto::Cipher::ChaCha20(m_key, m_nonce, 1);
  90. auto plaintext_buffer = TRY(ByteBuffer::create_zeroed(ciphertext.size()));
  91. auto plaintext = plaintext_buffer.bytes();
  92. chacha.encrypt(ciphertext, plaintext);
  93. // Finally, the Poly1305 function is called with the Poly1305 key
  94. // calculated above, and a message constructed as a concatenation of
  95. // the following:
  96. auto mac_data = TRY(ByteBuffer::create_zeroed(0));
  97. auto buffer_size = aad.size() + pad_to_16(aad) + ciphertext.size() + pad_to_16(ciphertext) + sizeof(u64) + sizeof(u64);
  98. mac_data.ensure_capacity(buffer_size);
  99. // The AAD
  100. mac_data.append(aad);
  101. // padding1 -- the padding is up to 15 zero bytes, and it brings
  102. // the total length so far to an integral multiple of 16. If the
  103. // length of the AAD was already an integral multiple of 16 bytes,
  104. // this field is zero-length.
  105. for (size_t i = 0; i < pad_to_16(aad); ++i)
  106. mac_data.append(0);
  107. // The ciphertext
  108. mac_data.append(ciphertext);
  109. // padding2 -- the padding is up to 15 zero bytes, and it brings
  110. // the total length so far to an integral multiple of 16. If the
  111. // length of the ciphertext was already an integral multiple of 16
  112. // bytes, this field is zero-length.
  113. for (size_t i = 0; i < pad_to_16(ciphertext); ++i)
  114. mac_data.append(0);
  115. u8 placeholder[8] = { 0 };
  116. // The length of the additional data in octets (as a 64-bit little-endian integer).
  117. mac_data.append(ReadonlyBytes { placeholder, 8 });
  118. ByteReader::store(static_cast<u8*>(mac_data.end_pointer()) - sizeof(u64), AK::convert_between_host_and_little_endian(static_cast<u64>(aad.size())));
  119. // The length of the ciphertext in octets (as a 64-bit little-endian integer).
  120. mac_data.append(ReadonlyBytes { placeholder, 8 });
  121. ByteReader::store(static_cast<u8*>(mac_data.end_pointer()) - sizeof(u64), AK::convert_between_host_and_little_endian(static_cast<u64>(ciphertext.size())));
  122. Crypto::Authentication::Poly1305 mac_function(otk);
  123. mac_function.update(mac_data.bytes());
  124. auto tag = TRY(mac_function.digest());
  125. // The output from the AEAD is the concatenation of:
  126. auto result = TRY(ByteBuffer::create_zeroed(0));
  127. result.ensure_capacity(plaintext.size() + tag.size());
  128. // A plaintext of the same length as the ciphertext.
  129. result.append(plaintext);
  130. // A 128-bit tag, which is the output of the Poly1305 function.
  131. result.append(tag);
  132. return result;
  133. }
  134. // https://datatracker.ietf.org/doc/html/rfc8439#section-4
  135. bool ChaCha20Poly1305::verify_tag(ReadonlyBytes encrypted, ReadonlyBytes decrypted)
  136. {
  137. // With online protocols, implementation MUST use a constant-time comparison function rather
  138. // than relying on optimized but insecure library functions such as the C language's memcmp().
  139. auto encrypted_tag = encrypted.slice_from_end(16);
  140. auto decrypted_tag = decrypted.slice_from_end(16);
  141. if (encrypted_tag.size() != decrypted_tag.size())
  142. return false;
  143. auto result = 0;
  144. for (size_t i = 0; i < encrypted_tag.size(); ++i)
  145. result |= encrypted_tag[i] ^ decrypted_tag[i];
  146. return result == 0;
  147. }
  148. }