CTR.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2020, Peter Elliott <pelliott@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/StringBuilder.h>
  8. #include <AK/StringView.h>
  9. #include <LibCrypto/Cipher/Mode/Mode.h>
  10. #ifndef KERNEL
  11. # include <AK/DeprecatedString.h>
  12. #endif
  13. namespace Crypto {
  14. namespace Cipher {
  15. /*
  16. * Heads up: CTR is a *family* of modes, because the "counter" function is
  17. * implementation-defined. This makes interoperability a pain in the neurons.
  18. * Here are several contradicting(!) interpretations:
  19. *
  20. * "The counter can be *any function* which produces a sequence which is
  21. * guaranteed not to repeat for a long time, although an actual increment-by-one
  22. * counter is the simplest and most popular."
  23. * The illustrations show that first increment should happen *after* the first
  24. * round. I call this variant BIGINT_INCR_0.
  25. * The AESAVS goes a step further and requires only that "counters" do not
  26. * repeat, leaving the method of counting completely open.
  27. * See: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Counter_(CTR)
  28. * See: https://csrc.nist.gov/csrc/media/projects/cryptographic-algorithm-validation-program/documents/aes/aesavs.pdf
  29. *
  30. * BIGINT_INCR_0 is the behavior of the OpenSSL command "openssl enc -aes-128-ctr",
  31. * and the behavior of CRYPTO_ctr128_encrypt(). OpenSSL is not alone in the
  32. * assumption that BIGINT_INCR_0 is all there is; even some NIST
  33. * specification/survey(?) doesn't consider counting any other way.
  34. * See: https://github.com/openssl/openssl/blob/33388b44b67145af2181b1e9528c381c8ea0d1b6/crypto/modes/ctr128.c#L71
  35. * See: http://www.cryptogrium.com/aes-ctr.html
  36. * See: https://web.archive.org/web/20150226072817/http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/ctr/ctr-spec.pdf
  37. *
  38. * "[T]he successive counter blocks are derived by applying an incrementing
  39. * function."
  40. * It defines a *family* of functions called "Standard Incrementing Function"
  41. * which only increment the lower-m bits, for some number 0<m<=blocksize.
  42. * The included test vectors suggest that the first increment should happen
  43. * *after* the first round. I call this INT32_INCR_0, or in general INTm_INCR_0.
  44. * This in particular is the behavior of CRYPTO_ctr128_encrypt_ctr32() in OpenSSL.
  45. * See: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf
  46. * See: https://github.com/openssl/openssl/blob/33388b44b67145af2181b1e9528c381c8ea0d1b6/crypto/modes/ctr128.c#L147
  47. *
  48. * The python package "cryptography" and RFC 3686 (which appears among the
  49. * first online search results when searching for "AES CTR 128 test vector")
  50. * share a peculiar interpretation of CTR mode: the counter is incremented *before*
  51. * the first round. RFC 3686 does not consider any other interpretation. I call
  52. * this variant BIGINT_INCR_1.
  53. * See: https://tools.ietf.org/html/rfc3686.html#section-6
  54. * See: https://cryptography.io/en/latest/development/test-vectors/#symmetric-ciphers
  55. *
  56. * And finally, because the method is left open, a different increment could be
  57. * used, for example little endian, or host endian, or mixed endian. Or any crazy
  58. * LSFR with sufficiently large period. That is the reason for the constant part
  59. * "INCR" in the previous counters.
  60. *
  61. * Due to this plethora of mutually-incompatible counters,
  62. * the method of counting should be a template parameter.
  63. * This currently implements BIGINT_INCR_0, which means perfect
  64. * interoperability with openssl. The test vectors from RFC 3686 just need to be
  65. * incremented by 1.
  66. * TODO: Implement other counters?
  67. */
  68. struct IncrementInplace {
  69. void operator()(Bytes& in) const
  70. {
  71. for (size_t i = in.size(); i > 0;) {
  72. --i;
  73. if (in[i] == (u8)-1) {
  74. in[i] = 0;
  75. } else {
  76. in[i]++;
  77. break;
  78. }
  79. }
  80. }
  81. };
  82. template<typename T, typename IncrementFunctionType = IncrementInplace>
  83. class CTR : public Mode<T> {
  84. public:
  85. constexpr static size_t IVSizeInBits = 128;
  86. virtual ~CTR() = default;
  87. // Must intercept `Intent`, because AES must always be set to
  88. // Encryption, even when decrypting AES-CTR.
  89. // TODO: How to deal with ciphers that take different arguments?
  90. // FIXME: Add back the default intent parameter once clang-11 is the default in GitHub Actions.
  91. // Once added back, remove the parameter where it's constructed in get_random_bytes in Kernel/Random.h.
  92. template<typename KeyType, typename... Args>
  93. explicit constexpr CTR(KeyType const& user_key, size_t key_bits, Intent, Args... args)
  94. : Mode<T>(user_key, key_bits, Intent::Encryption, args...)
  95. {
  96. }
  97. #ifndef KERNEL
  98. virtual DeprecatedString class_name() const override
  99. {
  100. StringBuilder builder;
  101. builder.append(this->cipher().class_name());
  102. builder.append("_CTR"sv);
  103. return builder.to_deprecated_string();
  104. }
  105. #endif
  106. virtual size_t IV_length() const override
  107. {
  108. return IVSizeInBits / 8;
  109. }
  110. virtual void encrypt(ReadonlyBytes in, Bytes& out, ReadonlyBytes ivec = {}, Bytes* ivec_out = nullptr) override
  111. {
  112. // Our interpretation of "ivec" is what AES-CTR
  113. // would define as nonce + IV + 4 zero bytes.
  114. this->encrypt_or_stream(&in, out, ivec, ivec_out);
  115. }
  116. void key_stream(Bytes& out, Bytes const& ivec = {}, Bytes* ivec_out = nullptr)
  117. {
  118. this->encrypt_or_stream(nullptr, out, ivec, ivec_out);
  119. }
  120. virtual void decrypt(ReadonlyBytes in, Bytes& out, ReadonlyBytes ivec = {}) override
  121. {
  122. // XOR (and thus CTR) is the most symmetric mode.
  123. this->encrypt(in, out, ivec);
  124. }
  125. private:
  126. u8 m_ivec_storage[IVSizeInBits / 8];
  127. typename T::BlockType m_cipher_block {};
  128. protected:
  129. constexpr static IncrementFunctionType increment {};
  130. void encrypt_or_stream(ReadonlyBytes const* in, Bytes& out, ReadonlyBytes ivec, Bytes* ivec_out = nullptr)
  131. {
  132. size_t length;
  133. if (in) {
  134. VERIFY(in->size() <= out.size());
  135. length = in->size();
  136. if (length == 0)
  137. return;
  138. } else {
  139. length = out.size();
  140. }
  141. auto& cipher = this->cipher();
  142. // FIXME: We should have two of these encrypt/decrypt functions that
  143. // we SFINAE out based on whether the Cipher mode needs an ivec
  144. VERIFY(!ivec.is_empty());
  145. VERIFY(ivec.size() >= IV_length());
  146. m_cipher_block.set_padding_mode(cipher.padding_mode());
  147. __builtin_memcpy(m_ivec_storage, ivec.data(), IV_length());
  148. Bytes iv { m_ivec_storage, IV_length() };
  149. size_t offset { 0 };
  150. auto block_size = cipher.block_size();
  151. while (length > 0) {
  152. m_cipher_block.overwrite(iv.slice(0, block_size));
  153. cipher.encrypt_block(m_cipher_block, m_cipher_block);
  154. if (in) {
  155. m_cipher_block.apply_initialization_vector(in->slice(offset));
  156. }
  157. auto write_size = min(block_size, length);
  158. VERIFY(offset + write_size <= out.size());
  159. __builtin_memcpy(out.offset(offset), m_cipher_block.bytes().data(), write_size);
  160. increment(iv);
  161. length -= write_size;
  162. offset += write_size;
  163. }
  164. if (ivec_out)
  165. __builtin_memcpy(ivec_out->data(), iv.data(), min(ivec_out->size(), IV_length()));
  166. }
  167. };
  168. }
  169. }