Cipher.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. Null,
  39. // FIXME: We do not implement these yet
  40. Bit,
  41. Random,
  42. Space,
  43. ZeroLength,
  44. };
  45. template <typename B, typename T>
  46. class Cipher;
  47. struct CipherBlock {
  48. public:
  49. explicit CipherBlock(PaddingMode mode)
  50. : m_padding_mode(mode)
  51. {
  52. }
  53. static size_t block_size() { ASSERT_NOT_REACHED(); }
  54. virtual ByteBuffer get() const = 0;
  55. virtual const ByteBuffer& data() const = 0;
  56. virtual void overwrite(const ByteBuffer&) = 0;
  57. virtual void overwrite(const u8*, size_t) = 0;
  58. virtual void apply_initialization_vector(const u8* ivec) = 0;
  59. PaddingMode padding_mode() const { return m_padding_mode; }
  60. template <typename T>
  61. void put(size_t offset, T value)
  62. {
  63. ASSERT(offset + sizeof(T) <= data().size());
  64. auto* ptr = data().data() + offset;
  65. auto index { 0 };
  66. ASSERT(sizeof(T) <= 4);
  67. if constexpr (sizeof(T) > 3)
  68. ptr[index++] = (u8)(value >> 24);
  69. if constexpr (sizeof(T) > 2)
  70. ptr[index++] = (u8)(value >> 16);
  71. if constexpr (sizeof(T) > 1)
  72. ptr[index++] = (u8)(value >> 8);
  73. ptr[index] = (u8)value;
  74. }
  75. private:
  76. virtual ByteBuffer& data() = 0;
  77. PaddingMode m_padding_mode;
  78. };
  79. struct CipherKey {
  80. virtual ByteBuffer data() const = 0;
  81. static bool is_valid_key_size(size_t) { return false; };
  82. virtual ~CipherKey() { }
  83. protected:
  84. virtual void expand_encrypt_key(const ByteBuffer& user_key, size_t bits) = 0;
  85. virtual void expand_decrypt_key(const ByteBuffer& user_key, size_t bits) = 0;
  86. size_t bits { 0 };
  87. };
  88. template <typename KeyT = CipherKey, typename BlockT = CipherBlock>
  89. class Cipher {
  90. public:
  91. using KeyType = KeyT;
  92. using BlockType = BlockT;
  93. explicit Cipher<KeyT, BlockT>(PaddingMode mode)
  94. : m_padding_mode(mode)
  95. {
  96. }
  97. virtual const KeyType& key() const = 0;
  98. virtual KeyType& key() = 0;
  99. static size_t block_size() { return BlockType::block_size(); }
  100. PaddingMode padding_mode() const { return m_padding_mode; }
  101. virtual void encrypt_block(const BlockType& in, BlockType& out) = 0;
  102. virtual void decrypt_block(const BlockType& in, BlockType& out) = 0;
  103. private:
  104. PaddingMode m_padding_mode;
  105. };
  106. }
  107. }