Avi Drissman | 201a9a83 | 2022-09-13 19:39:25 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
estark | 03206a1 | 2015-04-25 04:52:25 | [diff] [blame] | 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 | |
avi | dd373b8 | 2015-12-21 21:34:43 | [diff] [blame] | 8 | #include <stddef.h> |
Adam Langley | f214973 | 2019-08-09 22:23:33 | [diff] [blame] | 9 | #include <stdint.h> |
avi | dd373b8 | 2015-12-21 21:34:43 | [diff] [blame] | 10 | |
davidben | 6004dc5 | 2017-02-03 04:15:29 | [diff] [blame] | 11 | #include <string> |
David Benjamin | 3bfdc52 | 2023-09-01 21:13:45 | [diff] [blame] | 12 | #include <string_view> |
Adam Langley | f214973 | 2019-08-09 22:23:33 | [diff] [blame] | 13 | #include <vector> |
davidben | 6004dc5 | 2017-02-03 04:15:29 | [diff] [blame] | 14 | |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 15 | #include <optional> |
Adam Langley | f214973 | 2019-08-09 22:23:33 | [diff] [blame] | 16 | #include "base/containers/span.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 17 | #include "base/memory/raw_ptr.h" |
estark | 03206a1 | 2015-04-25 04:52:25 | [diff] [blame] | 18 | #include "crypto/crypto_export.h" |
| 19 | |
| 20 | struct evp_aead_st; |
| 21 | |
| 22 | namespace crypto { |
| 23 | |
Adam Langley | f214973 | 2019-08-09 22:23:33 | [diff] [blame] | 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 |
David Benjamin | 3bfdc52 | 2023-09-01 21:13:45 | [diff] [blame] | 26 | // around |std::string_view| and a more modern version that takes |base::span|. |
Adam Langley | f214973 | 2019-08-09 22:23:33 | [diff] [blame] | 27 | // Prefer the latter in new code. |
estark | 03206a1 | 2015-04-25 04:52:25 | [diff] [blame] | 28 | class CRYPTO_EXPORT Aead { |
| 29 | public: |
Leonid Baraz | 07c0577 | 2020-08-18 19:55:09 | [diff] [blame] | 30 | enum AeadAlgorithm { |
| 31 | AES_128_CTR_HMAC_SHA256, |
| 32 | AES_256_GCM, |
| 33 | AES_256_GCM_SIV, |
| 34 | CHACHA20_POLY1305 |
| 35 | }; |
estark | 03206a1 | 2015-04-25 04:52:25 | [diff] [blame] | 36 | |
| 37 | explicit Aead(AeadAlgorithm algorithm); |
Adam Langley | f214973 | 2019-08-09 22:23:33 | [diff] [blame] | 38 | Aead(const Aead&) = delete; |
| 39 | Aead& operator=(const Aead&) = delete; |
estark | 03206a1 | 2015-04-25 04:52:25 | [diff] [blame] | 40 | ~Aead(); |
| 41 | |
Adam Langley | f214973 | 2019-08-09 22:23:33 | [diff] [blame] | 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); |
| 45 | |
| 46 | // Note that Init keeps a reference to the data pointed to by |key| thus that |
| 47 | // data must outlive this object. |
estark | 03206a1 | 2015-04-25 04:52:25 | [diff] [blame] | 48 | void Init(const std::string* key); |
| 49 | |
Adam Langley | f214973 | 2019-08-09 22:23:33 | [diff] [blame] | 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; |
| 53 | |
David Benjamin | 3bfdc52 | 2023-09-01 21:13:45 | [diff] [blame] | 54 | bool Seal(std::string_view plaintext, |
| 55 | std::string_view nonce, |
| 56 | std::string_view additional_data, |
estark | 03206a1 | 2015-04-25 04:52:25 | [diff] [blame] | 57 | std::string* ciphertext) const; |
| 58 | |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 59 | std::optional<std::vector<uint8_t>> Open( |
Adam Langley | f214973 | 2019-08-09 22:23:33 | [diff] [blame] | 60 | base::span<const uint8_t> ciphertext, |
| 61 | base::span<const uint8_t> nonce, |
| 62 | base::span<const uint8_t> additional_data) const; |
| 63 | |
David Benjamin | 3bfdc52 | 2023-09-01 21:13:45 | [diff] [blame] | 64 | bool Open(std::string_view ciphertext, |
| 65 | std::string_view nonce, |
| 66 | std::string_view additional_data, |
estark | 03206a1 | 2015-04-25 04:52:25 | [diff] [blame] | 67 | std::string* plaintext) const; |
| 68 | |
| 69 | size_t KeyLength() const; |
| 70 | |
| 71 | size_t NonceLength() const; |
| 72 | |
| 73 | private: |
David Benjamin | 78bbd02 | 2024-02-09 01:02:49 | [diff] [blame] | 74 | std::optional<size_t> Seal(base::span<const uint8_t> plaintext, |
| 75 | base::span<const uint8_t> nonce, |
| 76 | base::span<const uint8_t> additional_data, |
| 77 | base::span<uint8_t> out) const; |
Adam Langley | f214973 | 2019-08-09 22:23:33 | [diff] [blame] | 78 | |
David Benjamin | 78bbd02 | 2024-02-09 01:02:49 | [diff] [blame] | 79 | std::optional<size_t> Open(base::span<const uint8_t> ciphertext, |
| 80 | base::span<const uint8_t> nonce, |
| 81 | base::span<const uint8_t> additional_data, |
| 82 | base::span<uint8_t> out) const; |
Adam Langley | f214973 | 2019-08-09 22:23:33 | [diff] [blame] | 83 | |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 84 | std::optional<base::span<const uint8_t>> key_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 85 | raw_ptr<const evp_aead_st> aead_; |
estark | 03206a1 | 2015-04-25 04:52:25 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace crypto |
| 89 | |
davidben | 6004dc5 | 2017-02-03 04:15:29 | [diff] [blame] | 90 | #endif // CRYPTO_AEAD_H_ |