Cipher.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2020, Ali Mohammad Pur <ali.mpfard@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/Optional.h>
  29. #include <AK/Types.h>
  30. namespace Crypto {
  31. namespace Cipher {
  32. enum class Intent {
  33. Encryption,
  34. Decryption,
  35. };
  36. enum class PaddingMode {
  37. CMS, // RFC 1423
  38. RFC5246, // very similar to CMS, but filled with |length - 1|, instead of |length|
  39. Null,
  40. // FIXME: We do not implement these yet
  41. Bit,
  42. Random,
  43. Space,
  44. ZeroLength,
  45. };
  46. template<typename B, typename T>
  47. class Cipher;
  48. struct CipherBlock {
  49. public:
  50. explicit CipherBlock(PaddingMode mode)
  51. : m_padding_mode(mode)
  52. {
  53. }
  54. static size_t block_size() { ASSERT_NOT_REACHED(); }
  55. virtual ByteBuffer get() const = 0;
  56. virtual const ByteBuffer& data() const = 0;
  57. virtual void overwrite(const ReadonlyBytes&) = 0;
  58. virtual void overwrite(const ByteBuffer& buffer) { overwrite(buffer.bytes()); }
  59. virtual void overwrite(const u8* data, size_t size) { overwrite({ data, size }); }
  60. virtual void apply_initialization_vector(const u8* ivec) = 0;
  61. PaddingMode padding_mode() const { return m_padding_mode; }
  62. void set_padding_mode(PaddingMode mode) { m_padding_mode = mode; }
  63. template<typename T>
  64. void put(size_t offset, T value)
  65. {
  66. ASSERT(offset + sizeof(T) <= data().size());
  67. auto* ptr = data().data() + offset;
  68. auto index { 0 };
  69. ASSERT(sizeof(T) <= 4);
  70. if constexpr (sizeof(T) > 3)
  71. ptr[index++] = (u8)(value >> 24);
  72. if constexpr (sizeof(T) > 2)
  73. ptr[index++] = (u8)(value >> 16);
  74. if constexpr (sizeof(T) > 1)
  75. ptr[index++] = (u8)(value >> 8);
  76. ptr[index] = (u8)value;
  77. }
  78. private:
  79. virtual ByteBuffer& data() = 0;
  80. PaddingMode m_padding_mode;
  81. };
  82. struct CipherKey {
  83. virtual ByteBuffer data() const = 0;
  84. static bool is_valid_key_size(size_t) { return false; };
  85. virtual ~CipherKey() { }
  86. protected:
  87. virtual void expand_encrypt_key(const ByteBuffer& user_key, size_t bits) = 0;
  88. virtual void expand_decrypt_key(const ByteBuffer& user_key, size_t bits) = 0;
  89. size_t bits { 0 };
  90. };
  91. template<typename KeyT = CipherKey, typename BlockT = CipherBlock>
  92. class Cipher {
  93. public:
  94. using KeyType = KeyT;
  95. using BlockType = BlockT;
  96. explicit Cipher<KeyT, BlockT>(PaddingMode mode)
  97. : m_padding_mode(mode)
  98. {
  99. }
  100. virtual const KeyType& key() const = 0;
  101. virtual KeyType& key() = 0;
  102. static size_t block_size() { return BlockType::block_size(); }
  103. PaddingMode padding_mode() const { return m_padding_mode; }
  104. virtual void encrypt_block(const BlockType& in, BlockType& out) = 0;
  105. virtual void decrypt_block(const BlockType& in, BlockType& out) = 0;
  106. virtual String class_name() const = 0;
  107. private:
  108. PaddingMode m_padding_mode;
  109. };
  110. }
  111. }