Mode.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <LibCrypto/Cipher/Cipher.h>
  29. namespace Crypto {
  30. namespace Cipher {
  31. template<typename T>
  32. class Mode {
  33. public:
  34. virtual ~Mode() { }
  35. // FIXME: Somehow communicate that encrypt returns the last initialization vector (if the mode supports it)
  36. virtual Optional<ByteBuffer> encrypt(const ByteBuffer& in, ByteBuffer& out, Optional<ByteBuffer> ivec = {}) = 0;
  37. virtual void decrypt(const ByteBuffer& in, ByteBuffer& out, Optional<ByteBuffer> ivec = {}) = 0;
  38. virtual size_t IV_length() const = 0;
  39. const T& cipher() const { return m_cipher; }
  40. ByteBuffer create_aligned_buffer(size_t input_size) const
  41. {
  42. size_t remainder = (input_size + T::block_size()) % T::block_size();
  43. if (remainder == 0)
  44. return ByteBuffer::create_uninitialized(input_size);
  45. else
  46. return ByteBuffer::create_uninitialized(input_size + T::block_size() - remainder);
  47. }
  48. virtual String class_name() const = 0;
  49. T& cipher() { return m_cipher; }
  50. protected:
  51. virtual void prune_padding(ByteBuffer& data)
  52. {
  53. auto size = data.size();
  54. switch (m_cipher.padding_mode()) {
  55. case PaddingMode::CMS: {
  56. auto maybe_padding_length = data[size - 1];
  57. if (maybe_padding_length >= T::block_size()) {
  58. // cannot be padding (the entire block cannot be padding)
  59. return;
  60. }
  61. for (auto i = maybe_padding_length; i > 0; --i) {
  62. if (data[size - i] != maybe_padding_length) {
  63. // not padding, part of data
  64. return;
  65. }
  66. }
  67. data.trim(size - maybe_padding_length);
  68. break;
  69. }
  70. case PaddingMode::RFC5246: {
  71. auto maybe_padding_length = data[size - 1];
  72. if (maybe_padding_length >= T::block_size() - 1) {
  73. // cannot be padding (the entire block cannot be padding)
  74. return;
  75. }
  76. // FIXME: If we want to constant-time operations, this loop should not stop
  77. for (auto i = maybe_padding_length; i > 0; --i) {
  78. if (data[size - i - 1] != maybe_padding_length) {
  79. // note that this is likely invalid padding
  80. return;
  81. }
  82. }
  83. data.trim(size - maybe_padding_length - 1);
  84. break;
  85. }
  86. case PaddingMode::Null: {
  87. while (data[size - 1] == 0)
  88. --size;
  89. data.trim(size);
  90. break;
  91. }
  92. default:
  93. // FIXME: support other padding modes
  94. ASSERT_NOT_REACHED();
  95. break;
  96. }
  97. }
  98. // FIXME: Somehow add a reference version of this
  99. template<typename... Args>
  100. Mode(Args... args)
  101. : m_cipher(args...)
  102. {
  103. }
  104. private:
  105. T m_cipher;
  106. };
  107. }
  108. }