[go: nahoru, domu]

blob: fdf59708ceef0155fe941b2fb3d94c88ca4799df [file] [log] [blame]
estark03206a12015-04-25 04:52:251// 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
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>
Adam Langleyf2149732019-08-09 22:23:3312#include <vector>
davidben6004dc52017-02-03 04:15:2913
Adam Langleyf2149732019-08-09 22:23:3314#include "base/containers/span.h"
estark03206a12015-04-25 04:52:2515#include "base/strings/string_piece.h"
16#include "crypto/crypto_export.h"
Anton Bikineeva3f961db2021-05-15 17:56:1217#include "third_party/abseil-cpp/absl/types/optional.h"
estark03206a12015-04-25 04:52:2518
19struct evp_aead_st;
20
21namespace crypto {
22
Adam Langleyf2149732019-08-09 22:23:3323// This class exposes the AES-128-CTR-HMAC-SHA256 and AES_256_GCM AEAD. Note
24// that there are two versions of most methods: an historical version based
25// around |StringPiece| and a more modern version that takes |base::span|.
26// Prefer the latter in new code.
estark03206a12015-04-25 04:52:2527class CRYPTO_EXPORT Aead {
28 public:
Leonid Baraz07c05772020-08-18 19:55:0929 enum AeadAlgorithm {
30 AES_128_CTR_HMAC_SHA256,
31 AES_256_GCM,
32 AES_256_GCM_SIV,
33 CHACHA20_POLY1305
34 };
estark03206a12015-04-25 04:52:2535
36 explicit Aead(AeadAlgorithm algorithm);
Adam Langleyf2149732019-08-09 22:23:3337 Aead(const Aead&) = delete;
38 Aead& operator=(const Aead&) = delete;
estark03206a12015-04-25 04:52:2539 ~Aead();
40
Adam Langleyf2149732019-08-09 22:23:3341 // Note that Init keeps a reference to the data pointed to by |key| thus that
42 // data must outlive this object.
43 void Init(base::span<const uint8_t> key);
44
45 // Note that Init keeps a reference to the data pointed to by |key| thus that
46 // data must outlive this object.
estark03206a12015-04-25 04:52:2547 void Init(const std::string* key);
48
Adam Langleyf2149732019-08-09 22:23:3349 std::vector<uint8_t> Seal(base::span<const uint8_t> plaintext,
50 base::span<const uint8_t> nonce,
51 base::span<const uint8_t> additional_data) const;
52
David Benjamincda45eb2017-11-06 18:16:5253 bool Seal(base::StringPiece plaintext,
54 base::StringPiece nonce,
55 base::StringPiece additional_data,
estark03206a12015-04-25 04:52:2556 std::string* ciphertext) const;
57
Anton Bikineeva3f961db2021-05-15 17:56:1258 absl::optional<std::vector<uint8_t>> Open(
Adam Langleyf2149732019-08-09 22:23:3359 base::span<const uint8_t> ciphertext,
60 base::span<const uint8_t> nonce,
61 base::span<const uint8_t> additional_data) const;
62
David Benjamincda45eb2017-11-06 18:16:5263 bool Open(base::StringPiece ciphertext,
64 base::StringPiece nonce,
65 base::StringPiece additional_data,
estark03206a12015-04-25 04:52:2566 std::string* plaintext) const;
67
68 size_t KeyLength() const;
69
70 size_t NonceLength() const;
71
72 private:
Adam Langleyf2149732019-08-09 22:23:3373 bool Seal(base::span<const uint8_t> plaintext,
74 base::span<const uint8_t> nonce,
75 base::span<const uint8_t> additional_data,
76 uint8_t* out,
77 size_t* output_length,
78 size_t max_output_length) const;
79
80 bool Open(base::span<const uint8_t> ciphertext,
81 base::span<const uint8_t> nonce,
82 base::span<const uint8_t> additional_data,
83 uint8_t* out,
84 size_t* output_length,
85 size_t max_output_length) const;
86
Anton Bikineeva3f961db2021-05-15 17:56:1287 absl::optional<base::span<const uint8_t>> key_;
estark03206a12015-04-25 04:52:2588 const evp_aead_st* aead_;
89};
90
91} // namespace crypto
92
davidben6004dc52017-02-03 04:15:2993#endif // CRYPTO_AEAD_H_