aead.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2015 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_AEAD_H_
  5. #define CRYPTO_AEAD_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <string>
  9. #include <vector>
  10. #include "base/containers/span.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/strings/string_piece.h"
  13. #include "crypto/crypto_export.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. struct evp_aead_st;
  16. namespace crypto {
  17. // This class exposes the AES-128-CTR-HMAC-SHA256 and AES_256_GCM AEAD. Note
  18. // that there are two versions of most methods: an historical version based
  19. // around |StringPiece| and a more modern version that takes |base::span|.
  20. // Prefer the latter in new code.
  21. class CRYPTO_EXPORT Aead {
  22. public:
  23. enum AeadAlgorithm {
  24. AES_128_CTR_HMAC_SHA256,
  25. AES_256_GCM,
  26. AES_256_GCM_SIV,
  27. CHACHA20_POLY1305
  28. };
  29. explicit Aead(AeadAlgorithm algorithm);
  30. Aead(const Aead&) = delete;
  31. Aead& operator=(const Aead&) = delete;
  32. ~Aead();
  33. // Note that Init keeps a reference to the data pointed to by |key| thus that
  34. // data must outlive this object.
  35. void Init(base::span<const uint8_t> key);
  36. // Note that Init keeps a reference to the data pointed to by |key| thus that
  37. // data must outlive this object.
  38. void Init(const std::string* key);
  39. std::vector<uint8_t> Seal(base::span<const uint8_t> plaintext,
  40. base::span<const uint8_t> nonce,
  41. base::span<const uint8_t> additional_data) const;
  42. bool Seal(base::StringPiece plaintext,
  43. base::StringPiece nonce,
  44. base::StringPiece additional_data,
  45. std::string* ciphertext) const;
  46. absl::optional<std::vector<uint8_t>> Open(
  47. base::span<const uint8_t> ciphertext,
  48. base::span<const uint8_t> nonce,
  49. base::span<const uint8_t> additional_data) const;
  50. bool Open(base::StringPiece ciphertext,
  51. base::StringPiece nonce,
  52. base::StringPiece additional_data,
  53. std::string* plaintext) const;
  54. size_t KeyLength() const;
  55. size_t NonceLength() const;
  56. private:
  57. bool Seal(base::span<const uint8_t> plaintext,
  58. base::span<const uint8_t> nonce,
  59. base::span<const uint8_t> additional_data,
  60. uint8_t* out,
  61. size_t* output_length,
  62. size_t max_output_length) const;
  63. bool Open(base::span<const uint8_t> ciphertext,
  64. base::span<const uint8_t> nonce,
  65. base::span<const uint8_t> additional_data,
  66. uint8_t* out,
  67. size_t* output_length,
  68. size_t max_output_length) const;
  69. absl::optional<base::span<const uint8_t>> key_;
  70. raw_ptr<const evp_aead_st> aead_;
  71. };
  72. } // namespace crypto
  73. #endif // CRYPTO_AEAD_H_