Cipher.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 ByteBuffer&) = 0;
  58. virtual void overwrite(const u8*, size_t) = 0;
  59. virtual void apply_initialization_vector(const u8* ivec) = 0;
  60. PaddingMode padding_mode() const { return m_padding_mode; }
  61. template<typename T>
  62. void put(size_t offset, T value)
  63. {
  64. ASSERT(offset + sizeof(T) <= data().size());
  65. auto* ptr = data().data() + offset;
  66. auto index { 0 };
  67. ASSERT(sizeof(T) <= 4);
  68. if constexpr (sizeof(T) > 3)
  69. ptr[index++] = (u8)(value >> 24);
  70. if constexpr (sizeof(T) > 2)
  71. ptr[index++] = (u8)(value >> 16);
  72. if constexpr (sizeof(T) > 1)
  73. ptr[index++] = (u8)(value >> 8);
  74. ptr[index] = (u8)value;
  75. }
  76. private:
  77. virtual ByteBuffer& data() = 0;
  78. PaddingMode m_padding_mode;
  79. };
  80. struct CipherKey {
  81. virtual ByteBuffer data() const = 0;
  82. static bool is_valid_key_size(size_t) { return false; };
  83. virtual ~CipherKey() { }
  84. protected:
  85. virtual void expand_encrypt_key(const ByteBuffer& user_key, size_t bits) = 0;
  86. virtual void expand_decrypt_key(const ByteBuffer& user_key, size_t bits) = 0;
  87. size_t bits { 0 };
  88. };
  89. template<typename KeyT = CipherKey, typename BlockT = CipherBlock>
  90. class Cipher {
  91. public:
  92. using KeyType = KeyT;
  93. using BlockType = BlockT;
  94. explicit Cipher<KeyT, BlockT>(PaddingMode mode)
  95. : m_padding_mode(mode)
  96. {
  97. }
  98. virtual const KeyType& key() const = 0;
  99. virtual KeyType& key() = 0;
  100. static size_t block_size() { return BlockType::block_size(); }
  101. PaddingMode padding_mode() const { return m_padding_mode; }
  102. virtual void encrypt_block(const BlockType& in, BlockType& out) = 0;
  103. virtual void decrypt_block(const BlockType& in, BlockType& out) = 0;
  104. virtual String class_name() const = 0;
  105. private:
  106. PaddingMode m_padding_mode;
  107. };
  108. }
  109. }