AES.h 3.7 KB

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