Cipher.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/Optional.h>
  9. #include <AK/Span.h>
  10. #include <AK/Types.h>
  11. namespace Crypto::Cipher {
  12. enum class Intent {
  13. Encryption,
  14. Decryption,
  15. };
  16. enum class PaddingMode {
  17. CMS, // RFC 1423
  18. RFC5246, // very similar to CMS, but filled with |length - 1|, instead of |length|
  19. Null,
  20. // FIXME: We do not implement these yet
  21. Bit,
  22. Random,
  23. Space,
  24. ZeroLength,
  25. };
  26. template<typename B, typename T>
  27. class Cipher;
  28. struct CipherBlock {
  29. public:
  30. explicit CipherBlock(PaddingMode mode)
  31. : m_padding_mode(mode)
  32. {
  33. }
  34. virtual ReadonlyBytes bytes() const = 0;
  35. virtual void overwrite(ReadonlyBytes) = 0;
  36. virtual void overwrite(u8 const* data, size_t size) { overwrite({ data, size }); }
  37. virtual void apply_initialization_vector(ReadonlyBytes ivec) = 0;
  38. PaddingMode padding_mode() const { return m_padding_mode; }
  39. void set_padding_mode(PaddingMode mode) { m_padding_mode = mode; }
  40. template<typename T>
  41. void put(size_t offset, T value)
  42. {
  43. VERIFY(offset + sizeof(T) <= bytes().size());
  44. auto* ptr = bytes().offset_pointer(offset);
  45. auto index { 0 };
  46. VERIFY(sizeof(T) <= 4);
  47. if constexpr (sizeof(T) > 3)
  48. ptr[index++] = (u8)(value >> 24);
  49. if constexpr (sizeof(T) > 2)
  50. ptr[index++] = (u8)(value >> 16);
  51. if constexpr (sizeof(T) > 1)
  52. ptr[index++] = (u8)(value >> 8);
  53. ptr[index] = (u8)value;
  54. }
  55. protected:
  56. virtual ~CipherBlock() = default;
  57. private:
  58. virtual Bytes bytes() = 0;
  59. PaddingMode m_padding_mode;
  60. };
  61. struct CipherKey {
  62. virtual ReadonlyBytes bytes() const = 0;
  63. static bool is_valid_key_size(size_t) { return false; }
  64. virtual ~CipherKey() = default;
  65. protected:
  66. virtual void expand_encrypt_key(ReadonlyBytes user_key, size_t bits) = 0;
  67. virtual void expand_decrypt_key(ReadonlyBytes user_key, size_t bits) = 0;
  68. size_t bits { 0 };
  69. };
  70. template<typename KeyT = CipherKey, typename BlockT = CipherBlock>
  71. class Cipher {
  72. public:
  73. using KeyType = KeyT;
  74. using BlockType = BlockT;
  75. explicit Cipher<KeyT, BlockT>(PaddingMode mode)
  76. : m_padding_mode(mode)
  77. {
  78. }
  79. virtual KeyType const& key() const = 0;
  80. virtual KeyType& key() = 0;
  81. constexpr static size_t block_size() { return BlockType::block_size(); }
  82. PaddingMode padding_mode() const { return m_padding_mode; }
  83. virtual void encrypt_block(BlockType const& in, BlockType& out) = 0;
  84. virtual void decrypt_block(BlockType const& in, BlockType& out) = 0;
  85. #ifndef KERNEL
  86. virtual ByteString class_name() const = 0;
  87. #endif
  88. protected:
  89. virtual ~Cipher() = default;
  90. private:
  91. PaddingMode m_padding_mode;
  92. };
  93. }