[go: nahoru, domu]

blob: 4deff590baa00de42d8de23a55704514e179fced [file] [log] [blame]
Avi Drissman201a9a832022-09-13 19:39:251// Copyright 2015 The Chromium Authors
estark03206a12015-04-25 04:52:252// 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
avidd373b82015-12-21 21:34:438#include <stddef.h>
Adam Langleyf2149732019-08-09 22:23:339#include <stdint.h>
avidd373b82015-12-21 21:34:4310
davidben6004dc52017-02-03 04:15:2911#include <string>
David Benjamin3bfdc522023-09-01 21:13:4512#include <string_view>
Adam Langleyf2149732019-08-09 22:23:3313#include <vector>
davidben6004dc52017-02-03 04:15:2914
Arthur Sonzogni59ac8222023-11-10 09:46:5415#include <optional>
Adam Langleyf2149732019-08-09 22:23:3316#include "base/containers/span.h"
Keishi Hattori0e45c022021-11-27 09:25:5217#include "base/memory/raw_ptr.h"
estark03206a12015-04-25 04:52:2518#include "crypto/crypto_export.h"
19
20struct evp_aead_st;
21
22namespace crypto {
23
Adam Langleyf2149732019-08-09 22:23:3324// 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 Benjamin3bfdc522023-09-01 21:13:4526// around |std::string_view| and a more modern version that takes |base::span|.
Adam Langleyf2149732019-08-09 22:23:3327// Prefer the latter in new code.
estark03206a12015-04-25 04:52:2528class CRYPTO_EXPORT Aead {
29 public:
Leonid Baraz07c05772020-08-18 19:55:0930 enum AeadAlgorithm {
31 AES_128_CTR_HMAC_SHA256,
32 AES_256_GCM,
33 AES_256_GCM_SIV,
34 CHACHA20_POLY1305
35 };
estark03206a12015-04-25 04:52:2536
37 explicit Aead(AeadAlgorithm algorithm);
Adam Langleyf2149732019-08-09 22:23:3338 Aead(const Aead&) = delete;
39 Aead& operator=(const Aead&) = delete;
estark03206a12015-04-25 04:52:2540 ~Aead();
41
Adam Langleyf2149732019-08-09 22:23:3342 // 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.
estark03206a12015-04-25 04:52:2548 void Init(const std::string* key);
49
Adam Langleyf2149732019-08-09 22:23:3350 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 Benjamin3bfdc522023-09-01 21:13:4554 bool Seal(std::string_view plaintext,
55 std::string_view nonce,
56 std::string_view additional_data,
estark03206a12015-04-25 04:52:2557 std::string* ciphertext) const;
58
Arthur Sonzogni59ac8222023-11-10 09:46:5459 std::optional<std::vector<uint8_t>> Open(
Adam Langleyf2149732019-08-09 22:23:3360 base::span<const uint8_t> ciphertext,
61 base::span<const uint8_t> nonce,
62 base::span<const uint8_t> additional_data) const;
63
David Benjamin3bfdc522023-09-01 21:13:4564 bool Open(std::string_view ciphertext,
65 std::string_view nonce,
66 std::string_view additional_data,
estark03206a12015-04-25 04:52:2567 std::string* plaintext) const;
68
69 size_t KeyLength() const;
70
71 size_t NonceLength() const;
72
73 private:
David Benjamin78bbd022024-02-09 01:02:4974 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 Langleyf2149732019-08-09 22:23:3378
David Benjamin78bbd022024-02-09 01:02:4979 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 Langleyf2149732019-08-09 22:23:3383
Arthur Sonzogni59ac8222023-11-10 09:46:5484 std::optional<base::span<const uint8_t>> key_;
Keishi Hattori0e45c022021-11-27 09:25:5285 raw_ptr<const evp_aead_st> aead_;
estark03206a12015-04-25 04:52:2586};
87
88} // namespace crypto
89
davidben6004dc52017-02-03 04:15:2990#endif // CRYPTO_AEAD_H_