36 template <
typename AeadType,
typename HkdfType>
38 const std::vector<folly::ByteRange>& tokenSecrets) {
39 VLOG(3) <<
"Updating token secrets";
41 for (
const auto& tokenSecret : tokenSecrets) {
42 if (tokenSecret.size() < kMinTokenSecretLength) {
43 LOG(ERROR) <<
"Token cipher secret too small - not updating.";
48 VLOG(4) <<
"Updating token secrets, num=" << tokenSecrets.size();
50 for (
const auto& tokenSecret : tokenSecrets) {
51 Secret extracted(tokenSecret.begin(), tokenSecret.end());
52 for (
const auto& contextString : contextStrings_) {
53 extracted = HkdfType().extract(
61 template <
typename AeadType,
typename HkdfType>
63 Buf plaintext)
const {
64 if (secrets_.empty()) {
76 appender.writeBE(seqNum);
77 token->prependChain(aead.encrypt(
std::move(plaintext),
nullptr, seqNum));
82 template <
typename AeadType,
typename HkdfType>
86 if (secrets_.empty() || !cursor.canAdvance(kTokenHeaderLength)) {
91 cursor.pull(salt.data(), salt.size());
92 auto seqNum = cursor.readBE<
SeqNum>();
94 cursor.clone(ciphertext, cursor.totalLength());
96 for (
const auto&
secret : secrets_) {
98 auto result = aead.tryDecrypt(ciphertext->clone(),
nullptr, seqNum);
104 VLOG(6) <<
"Failed to decrypt token.";
108 template <
typename AeadType,
typename HkdfType>
115 HkdfType().expand(secret, *info, aead.keyLength() + aead.ivLength());
118 cursor.clone(key.key, aead.keyLength());
119 cursor.clone(key.iv, aead.ivLength());
124 template <
typename AeadType,
typename HkdfType>
126 for (
auto&
secret : secrets_) {
static std::unique_ptr< IOBuf > create(std::size_t capacity)
static std::unique_ptr< IOBuf > wrapBuffer(const void *buf, std::size_t capacity)
static void clean(folly::MutableByteRange range)
constexpr detail::Map< Move > move
std::vector< uint8_t > Secret
void push(const uint8_t *buf, size_t len)
std::array< uint8_t, kSaltLength > Salt
folly::Optional< Buf > encrypt(Buf plaintext) const
constexpr Range< Iter > range(Iter first, Iter last)
std::unique_ptr< folly::IOBuf > Buf
static constexpr StringPiece secret
bool setSecrets(const std::vector< folly::ByteRange > &tokenSecrets)
folly::Optional< Buf > decrypt(Buf) const
AeadType createAead(folly::ByteRange secret, folly::ByteRange salt) const