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