proxygen
Aead.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #pragma once
10 
11 #include <folly/Optional.h>
12 #include <folly/io/IOBuf.h>
13 
14 namespace fizz {
15 
16 struct TrafficKey {
17  std::unique_ptr<folly::IOBuf> key;
18  std::unique_ptr<folly::IOBuf> iv;
19 };
20 
24 class Aead {
25  public:
26  virtual ~Aead() = default;
27 
31  virtual size_t keyLength() const = 0;
32 
36  virtual size_t ivLength() const = 0;
37 
42  virtual void setKey(TrafficKey key) = 0;
43 
47  virtual std::unique_ptr<folly::IOBuf> encrypt(
48  std::unique_ptr<folly::IOBuf>&& plaintext,
49  const folly::IOBuf* associatedData,
50  uint64_t seqNum) const = 0;
51 
57  virtual void setEncryptedBufferHeadroom(size_t headroom) = 0;
58 
63  virtual std::unique_ptr<folly::IOBuf> decrypt(
64  std::unique_ptr<folly::IOBuf>&& ciphertext,
65  const folly::IOBuf* associatedData,
66  uint64_t seqNum) const {
67  auto plaintext = tryDecrypt(
68  std::forward<std::unique_ptr<folly::IOBuf>>(ciphertext),
69  associatedData,
70  seqNum);
71  if (!plaintext) {
72  throw std::runtime_error("decryption failed");
73  }
74  return std::move(*plaintext);
75  }
76 
82  std::unique_ptr<folly::IOBuf>&& ciphertext,
83  const folly::IOBuf* associatedData,
84  uint64_t seqNum) const = 0;
85 
90  virtual size_t getCipherOverhead() const = 0;
91 };
92 } // namespace fizz
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< folly::IOBuf > key
Definition: Aead.h:17
std::unique_ptr< folly::IOBuf > iv
Definition: Aead.h:18
Definition: Actions.h:16
virtual std::unique_ptr< folly::IOBuf > decrypt(std::unique_ptr< folly::IOBuf > &&ciphertext, const folly::IOBuf *associatedData, uint64_t seqNum) const
Definition: Aead.h:63