proxygen
fizz::HkdfImpl< Hash > Class Template Reference

#include <Hkdf.h>

Inheritance diagram for fizz::HkdfImpl< Hash >:
fizz::Hkdf

Public Member Functions

std::vector< uint8_textract (folly::ByteRange salt, folly::ByteRange ikm) const override
 
std::unique_ptr< folly::IOBufexpand (folly::ByteRange extractedKey, const folly::IOBuf &info, size_t outputBytes) const override
 
std::unique_ptr< folly::IOBufhkdf (folly::ByteRange ikm, folly::ByteRange salt, const folly::IOBuf &info, size_t outputBytes) const override
 
size_t hashLength () const override
 
- Public Member Functions inherited from fizz::Hkdf
virtual ~Hkdf ()=default
 

Static Public Attributes

static constexpr size_t HashLen = Hash::HashLen
 

Detailed Description

template<typename Hash>
class fizz::HkdfImpl< Hash >

HKDF implementation using a templated HMAC implementation.

The template struct requires the following parameters:

  • HashLen: length of the hash digest
  • hmac(ByteRange key, const IOBuf& in, MutableByteRange out)

Definition at line 54 of file Hkdf.h.

Member Function Documentation

template<typename Hash >
std::unique_ptr< folly::IOBuf > fizz::HkdfImpl< Hash >::expand ( folly::ByteRange  extractedKey,
const folly::IOBuf info,
size_t  outputBytes 
) const
inlineoverridevirtual

Implements fizz::Hkdf.

Definition at line 25 of file Hkdf-inl.h.

References folly::IOBuf::clone(), folly::IOBuf::create(), folly::gen::move, folly::range(), folly::chrono::round(), folly::Range< Iter >::size(), and UNLIKELY.

28  {
29  CHECK_EQ(extractedKey.size(), Hash::HashLen);
30  if (UNLIKELY(outputBytes > 255 * Hash::HashLen)) {
31  throw std::runtime_error("Output too long");
32  }
33  // HDKF expansion step.
34  size_t numRounds = (outputBytes + Hash::HashLen - 1) / Hash::HashLen;
35  auto expanded = folly::IOBuf::create(numRounds * Hash::HashLen);
36 
37  auto in = folly::IOBuf::create(0);
38  for (size_t round = 1; round <= numRounds; ++round) {
39  in->prependChain(info.clone());
40  // We're guaranteed that the round num will fit in
41  // one byte because of the check at the beginning of
42  // the method.
43  auto roundNum = folly::IOBuf::create(1);
44  roundNum->append(1);
45  roundNum->writableData()[0] = round;
46  in->prependChain(std::move(roundNum));
47 
48  size_t outputStartIdx = (round - 1) * Hash::HashLen;
49  Hash::hmac(
50  folly::range(extractedKey),
51  *in,
52  {expanded->writableData() + outputStartIdx, Hash::HashLen});
53  expanded->append(Hash::HashLen);
54 
55  in = expanded->clone();
56  in->trimStart(outputStartIdx);
57  }
58  expanded->trimEnd(numRounds * Hash::HashLen - outputBytes);
59  return expanded;
60 }
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
constexpr To round(std::chrono::duration< Rep, Period > const &d)
Definition: Chrono.h:139
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
constexpr size_type size() const
Definition: Range.h:431
std::unique_ptr< IOBuf > clone() const
Definition: IOBuf.cpp:527
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
#define UNLIKELY(x)
Definition: Likely.h:48
template<typename Hash >
std::vector< uint8_t > fizz::HkdfImpl< Hash >::extract ( folly::ByteRange  salt,
folly::ByteRange  ikm 
) const
inlineoverridevirtual

Implements fizz::Hkdf.

Definition at line 12 of file Hkdf-inl.h.

References folly::Range< Iter >::empty(), folly::range(), and folly::IOBuf::wrapBufferAsValue().

14  {
15  auto zeros = std::vector<uint8_t>(Hash::HashLen, 0);
16  // Extraction step HMAC-HASH(salt, IKM)
17  std::vector<uint8_t> extractedKey(Hash::HashLen);
18  salt = salt.empty() ? folly::range(zeros) : salt;
19  Hash::hmac(
20  salt, folly::IOBuf::wrapBufferAsValue(ikm), folly::range(extractedKey));
21  return extractedKey;
22 }
constexpr bool empty() const
Definition: Range.h:443
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
static IOBuf wrapBufferAsValue(const void *buf, std::size_t capacity) noexcept
Definition: IOBuf.cpp:357
template<typename Hash>
size_t fizz::HkdfImpl< Hash >::hashLength ( ) const
inlineoverridevirtual

Implements fizz::Hkdf.

Definition at line 72 of file Hkdf.h.

72  {
73  return HashLen;
74  }
static constexpr size_t HashLen
Definition: Hkdf.h:56
template<typename Hash >
std::unique_ptr< folly::IOBuf > fizz::HkdfImpl< Hash >::hkdf ( folly::ByteRange  ikm,
folly::ByteRange  salt,
const folly::IOBuf info,
size_t  outputBytes 
) const
inlineoverridevirtual

Implements fizz::Hkdf.

Definition at line 63 of file Hkdf-inl.h.

References folly::range().

67  {
68  return expand(folly::range(extract(salt, ikm)), info, outputBytes);
69 }
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
std::unique_ptr< folly::IOBuf > expand(folly::ByteRange extractedKey, const folly::IOBuf &info, size_t outputBytes) const override
Definition: Hkdf-inl.h:25
std::vector< uint8_t > extract(folly::ByteRange salt, folly::ByteRange ikm) const override
Definition: Hkdf-inl.h:12

Member Data Documentation

template<typename Hash>
constexpr size_t fizz::HkdfImpl< Hash >::HashLen = Hash::HashLen
static

Definition at line 56 of file Hkdf.h.


The documentation for this class was generated from the following files: