proxygen
AeadTokenCipher.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 <fizz/record/Types.h>
12 #include <folly/Optional.h>
13 #include <folly/io/IOBuf.h>
14 
15 namespace fizz {
16 namespace server {
17 
21 template <typename AeadType, typename HkdfType>
23  public:
24  static constexpr size_t kMinTokenSecretLength = 32;
25 
32  explicit AeadTokenCipher(std::vector<std::string> contextStrings)
33  : contextStrings_(std::move(contextStrings)) {}
34 
36  clearSecrets();
37  }
38 
44  bool setSecrets(const std::vector<folly::ByteRange>& tokenSecrets);
45 
46  folly::Optional<Buf> encrypt(Buf plaintext) const;
47 
49 
50  private:
51  using Secret = std::vector<uint8_t>;
52  static constexpr size_t kSaltLength = HkdfType::HashLen;
53  using Salt = std::array<uint8_t, kSaltLength>;
54  using SeqNum = uint32_t;
55  static constexpr size_t kTokenHeaderLength = kSaltLength + sizeof(SeqNum);
56 
57  AeadType createAead(folly::ByteRange secret, folly::ByteRange salt) const;
58 
59  void clearSecrets();
60 
61  // First secret is the one used to encrypt.
62  std::vector<Secret> secrets_;
63 
64  std::vector<std::string> contextStrings_;
65 };
66 } // namespace server
67 } // namespace fizz
68 
AeadTokenCipher(std::vector< std::string > contextStrings)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::vector< uint8_t > Secret
STL namespace.
std::array< uint8_t, kSaltLength > Salt
static constexpr size_t kMinTokenSecretLength
folly::Optional< Buf > encrypt(Buf plaintext) const
Definition: Actions.h:16
static constexpr size_t kTokenHeaderLength
std::vector< Secret > secrets_
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
static constexpr size_t kSaltLength
static constexpr StringPiece secret
bool setSecrets(const std::vector< folly::ByteRange > &tokenSecrets)
std::vector< std::string > contextStrings_
folly::Optional< Buf > decrypt(Buf) const
AeadType createAead(folly::ByteRange secret, folly::ByteRange salt) const