AES.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Vector.h>
  9. #include <LibCrypto/Cipher/Cipher.h>
  10. #include <LibCrypto/Cipher/Mode/CBC.h>
  11. #include <LibCrypto/Cipher/Mode/CTR.h>
  12. #include <LibCrypto/Cipher/Mode/GCM.h>
  13. #ifndef KERNEL
  14. # include <AK/DeprecatedString.h>
  15. #endif
  16. namespace Crypto {
  17. namespace Cipher {
  18. struct AESCipherBlock : public CipherBlock {
  19. public:
  20. static constexpr size_t BlockSizeInBits = 128;
  21. explicit AESCipherBlock(PaddingMode mode = PaddingMode::CMS)
  22. : CipherBlock(mode)
  23. {
  24. }
  25. AESCipherBlock(u8 const* data, size_t length, PaddingMode mode = PaddingMode::CMS)
  26. : AESCipherBlock(mode)
  27. {
  28. CipherBlock::overwrite(data, length);
  29. }
  30. constexpr static size_t block_size() { return BlockSizeInBits / 8; };
  31. virtual ReadonlyBytes bytes() const override { return ReadonlyBytes { m_data, sizeof(m_data) }; }
  32. virtual Bytes bytes() override { return Bytes { m_data, sizeof(m_data) }; }
  33. virtual void overwrite(ReadonlyBytes) override;
  34. virtual void overwrite(u8 const* data, size_t size) override { overwrite({ data, size }); }
  35. virtual void apply_initialization_vector(ReadonlyBytes ivec) override
  36. {
  37. for (size_t i = 0; i < min(block_size(), ivec.size()); ++i)
  38. m_data[i] ^= ivec[i];
  39. }
  40. #ifndef KERNEL
  41. DeprecatedString to_deprecated_string() const;
  42. #endif
  43. private:
  44. constexpr static size_t data_size() { return sizeof(m_data); }
  45. u8 m_data[BlockSizeInBits / 8] {};
  46. };
  47. struct AESCipherKey : public CipherKey {
  48. virtual ReadonlyBytes bytes() const override { return ReadonlyBytes { m_rd_keys, sizeof(m_rd_keys) }; };
  49. virtual void expand_encrypt_key(ReadonlyBytes user_key, size_t bits) override;
  50. virtual void expand_decrypt_key(ReadonlyBytes user_key, size_t bits) override;
  51. static bool is_valid_key_size(size_t bits) { return bits == 128 || bits == 192 || bits == 256; };
  52. #ifndef KERNEL
  53. DeprecatedString to_deprecated_string() const;
  54. #endif
  55. u32 const* round_keys() const
  56. {
  57. return (u32 const*)m_rd_keys;
  58. }
  59. AESCipherKey(ReadonlyBytes user_key, size_t key_bits, Intent intent)
  60. : m_bits(key_bits)
  61. {
  62. if (intent == Intent::Encryption)
  63. expand_encrypt_key(user_key, key_bits);
  64. else
  65. expand_decrypt_key(user_key, key_bits);
  66. }
  67. virtual ~AESCipherKey() override = default;
  68. size_t rounds() const { return m_rounds; }
  69. size_t length() const { return m_bits / 8; }
  70. protected:
  71. u32* round_keys()
  72. {
  73. return (u32*)m_rd_keys;
  74. }
  75. private:
  76. static constexpr size_t MAX_ROUND_COUNT = 14;
  77. u32 m_rd_keys[(MAX_ROUND_COUNT + 1) * 4] { 0 };
  78. size_t m_rounds;
  79. size_t m_bits;
  80. };
  81. class AESCipher final : public Cipher<AESCipherKey, AESCipherBlock> {
  82. public:
  83. using CBCMode = CBC<AESCipher>;
  84. using CTRMode = CTR<AESCipher>;
  85. using GCMMode = GCM<AESCipher>;
  86. constexpr static size_t BlockSizeInBits = BlockType::BlockSizeInBits;
  87. AESCipher(ReadonlyBytes user_key, size_t key_bits, Intent intent = Intent::Encryption, PaddingMode mode = PaddingMode::CMS)
  88. : Cipher<AESCipherKey, AESCipherBlock>(mode)
  89. , m_key(user_key, key_bits, intent)
  90. {
  91. }
  92. virtual AESCipherKey const& key() const override { return m_key; };
  93. virtual AESCipherKey& key() override { return m_key; };
  94. virtual void encrypt_block(BlockType const& in, BlockType& out) override;
  95. virtual void decrypt_block(BlockType const& in, BlockType& out) override;
  96. #ifndef KERNEL
  97. virtual DeprecatedString class_name() const override
  98. {
  99. return "AES";
  100. }
  101. #endif
  102. protected:
  103. AESCipherKey m_key;
  104. };
  105. }
  106. }