1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
12 #include <string_view>
15 #include "base/containers/span.h"
16 #include "base/memory/raw_ptr.h"
17 #include "crypto/crypto_export.h"
18 #include "third_party/abseil-cpp/absl/types/optional.h"
24 // This class exposes the AES-128-CTR-HMAC-SHA256 and AES_256_GCM AEAD. Note
25 // that there are two versions of most methods: an historical version based
26 // around |std::string_view| and a more modern version that takes |base::span|.
27 // Prefer the latter in new code.
28 class CRYPTO_EXPORT Aead {
31 AES_128_CTR_HMAC_SHA256,
37 explicit Aead(AeadAlgorithm algorithm);
38 Aead(const Aead&) = delete;
39 Aead& operator=(const Aead&) = delete;
42 // Note that Init keeps a reference to the data pointed to by |key| thus that
43 // data must outlive this object.
44 void Init(base::span<const uint8_t> key);
46 // Note that Init keeps a reference to the data pointed to by |key| thus that
47 // data must outlive this object.
48 void Init(const std::string* key);
50 std::vector<uint8_t> Seal(base::span<const uint8_t> plaintext,
51 base::span<const uint8_t> nonce,
52 base::span<const uint8_t> additional_data) const;
54 bool Seal(std::string_view plaintext,
55 std::string_view nonce,
56 std::string_view additional_data,
57 std::string* ciphertext) const;
59 absl::optional<std::vector<uint8_t>> Open(
60 base::span<const uint8_t> ciphertext,
61 base::span<const uint8_t> nonce,
62 base::span<const uint8_t> additional_data) const;
64 bool Open(std::string_view ciphertext,
65 std::string_view nonce,
66 std::string_view additional_data,
67 std::string* plaintext) const;
69 size_t KeyLength() const;
71 size_t NonceLength() const;
74 bool Seal(base::span<const uint8_t> plaintext,
75 base::span<const uint8_t> nonce,
76 base::span<const uint8_t> additional_data,
78 size_t* output_length,
79 size_t max_output_length) const;
81 bool Open(base::span<const uint8_t> ciphertext,
82 base::span<const uint8_t> nonce,
83 base::span<const uint8_t> additional_data,
85 size_t* output_length,
86 size_t max_output_length) const;
88 absl::optional<base::span<const uint8_t>> key_;
89 raw_ptr<const evp_aead_st> aead_;
94 #endif // CRYPTO_AEAD_H_