encryptor.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CRYPTO_ENCRYPTOR_H_
  5. #define CRYPTO_ENCRYPTOR_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include "base/containers/span.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/strings/string_piece.h"
  13. #include "build/build_config.h"
  14. #include "crypto/crypto_export.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. namespace crypto {
  17. class SymmetricKey;
  18. // This class implements encryption without authentication, which is usually
  19. // unsafe. Prefer crypto::Aead for new code. If using this class, prefer the
  20. // base::span and std::vector overloads over the base::StringPiece and
  21. // std::string overloads.
  22. class CRYPTO_EXPORT Encryptor {
  23. public:
  24. enum Mode {
  25. CBC,
  26. CTR,
  27. };
  28. Encryptor();
  29. ~Encryptor();
  30. // Initializes the encryptor using |key| and |iv|. Returns false if either the
  31. // key or the initialization vector cannot be used.
  32. //
  33. // If |mode| is CBC, |iv| must not be empty; if it is CTR, then |iv| must be
  34. // empty.
  35. bool Init(const SymmetricKey* key, Mode mode, base::StringPiece iv);
  36. bool Init(const SymmetricKey* key, Mode mode, base::span<const uint8_t> iv);
  37. // Encrypts |plaintext| into |ciphertext|. |plaintext| may only be empty if
  38. // the mode is CBC.
  39. bool Encrypt(base::StringPiece plaintext, std::string* ciphertext);
  40. bool Encrypt(base::span<const uint8_t> plaintext,
  41. std::vector<uint8_t>* ciphertext);
  42. // Decrypts |ciphertext| into |plaintext|. |ciphertext| must not be empty.
  43. //
  44. // WARNING: In CBC mode, Decrypt() returns false if it detects the padding
  45. // in the decrypted plaintext is wrong. Padding errors can result from
  46. // tampered ciphertext or a wrong decryption key. But successful decryption
  47. // does not imply the authenticity of the data. The caller of Decrypt()
  48. // must either authenticate the ciphertext before decrypting it, or take
  49. // care to not report decryption failure. Otherwise it could inadvertently
  50. // be used as a padding oracle to attack the cryptosystem.
  51. bool Decrypt(base::StringPiece ciphertext, std::string* plaintext);
  52. bool Decrypt(base::span<const uint8_t> ciphertext,
  53. std::vector<uint8_t>* plaintext);
  54. // Sets the counter value when in CTR mode. Currently only 128-bits
  55. // counter value is supported.
  56. //
  57. // Returns true only if update was successful.
  58. bool SetCounter(base::StringPiece counter);
  59. bool SetCounter(base::span<const uint8_t> counter);
  60. // TODO(albertb): Support streaming encryption.
  61. private:
  62. raw_ptr<const SymmetricKey> key_;
  63. Mode mode_;
  64. bool CryptString(bool do_encrypt,
  65. base::StringPiece input,
  66. std::string* output);
  67. bool CryptBytes(bool do_encrypt,
  68. base::span<const uint8_t> input,
  69. std::vector<uint8_t>* output);
  70. // On success, these helper functions return the number of bytes written to
  71. // |output|.
  72. size_t MaxOutput(bool do_encrypt, size_t length);
  73. absl::optional<size_t> Crypt(bool do_encrypt,
  74. base::span<const uint8_t> input,
  75. base::span<uint8_t> output);
  76. absl::optional<size_t> CryptCTR(bool do_encrypt,
  77. base::span<const uint8_t> input,
  78. base::span<uint8_t> output);
  79. // In CBC mode, the IV passed to Init(). In CTR mode, the counter value passed
  80. // to SetCounter().
  81. std::vector<uint8_t> iv_;
  82. };
  83. } // namespace crypto
  84. #endif // CRYPTO_ENCRYPTOR_H_