Coverage Report

Created: 2021-05-04 09:02

/src/botan/build/include/botan/internal/hkdf.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* HKDF
3
* (C) 2013,2015 Jack Lloyd
4
* (C) 2016 René Korthaus, Rohde & Schwarz Cybersecurity
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8
9
#ifndef BOTAN_HKDF_H_
10
#define BOTAN_HKDF_H_
11
12
#include <botan/mac.h>
13
#include <botan/kdf.h>
14
15
namespace Botan {
16
17
/**
18
* HKDF from RFC 5869.
19
*/
20
class HKDF final : public KDF
21
   {
22
   public:
23
      /**
24
      * @param prf MAC algorithm to use
25
      */
26
      explicit HKDF(std::unique_ptr<MessageAuthenticationCode> prf) :
27
0
         m_prf(std::move(prf)) {}
28
29
      std::unique_ptr<KDF> new_object() const override
30
0
         {
31
0
         return std::make_unique<HKDF>(m_prf->new_object());
32
0
         }
33
34
0
      std::string name() const override { return "HKDF(" + m_prf->name() + ")"; }
35
36
      void kdf(uint8_t key[], size_t key_len,
37
               const uint8_t secret[], size_t secret_len,
38
               const uint8_t salt[], size_t salt_len,
39
               const uint8_t label[], size_t label_len) const override;
40
41
   private:
42
      std::unique_ptr<MessageAuthenticationCode> m_prf;
43
   };
44
45
/**
46
* HKDF Extraction Step from RFC 5869.
47
*/
48
class HKDF_Extract final : public KDF
49
   {
50
   public:
51
      /**
52
      * @param prf MAC algorithm to use
53
      */
54
      explicit HKDF_Extract(std::unique_ptr<MessageAuthenticationCode> prf) :
55
0
         m_prf(std::move(prf)) {}
56
57
      std::unique_ptr<KDF> new_object() const override
58
0
         {
59
0
         return std::make_unique<HKDF_Extract>(m_prf->new_object());
60
0
         }
61
62
0
      std::string name() const override { return "HKDF-Extract(" + m_prf->name() + ")"; }
63
64
      void kdf(uint8_t key[], size_t key_len,
65
               const uint8_t secret[], size_t secret_len,
66
               const uint8_t salt[], size_t salt_len,
67
               const uint8_t label[], size_t label_len) const override;
68
69
   private:
70
      std::unique_ptr<MessageAuthenticationCode> m_prf;
71
   };
72
73
/**
74
* HKDF Expansion Step from RFC 5869.
75
*/
76
class HKDF_Expand final : public KDF
77
   {
78
   public:
79
      /**
80
      * @param prf MAC algorithm to use
81
      */
82
      explicit HKDF_Expand(std::unique_ptr<MessageAuthenticationCode> prf) :
83
0
         m_prf(std::move(prf)) {}
84
85
      std::unique_ptr<KDF> new_object() const override
86
0
         {
87
0
         return std::make_unique<HKDF_Expand>(m_prf->new_object());
88
0
         }
89
90
0
      std::string name() const override { return "HKDF-Expand(" + m_prf->name() + ")"; }
91
92
      void kdf(uint8_t key[], size_t key_len,
93
               const uint8_t secret[], size_t secret_len,
94
               const uint8_t salt[], size_t salt_len,
95
               const uint8_t label[], size_t label_len) const override;
96
97
   private:
98
      std::unique_ptr<MessageAuthenticationCode> m_prf;
99
   };
100
101
/**
102
* HKDF-Expand-Label from TLS 1.3/QUIC
103
* @param hash_fn the hash to use
104
* @param secret the secret bits
105
* @param secret_len the length of secret
106
* @param label the full label (no "TLS 1.3, " or "tls13 " prefix
107
*  is applied)
108
* @param hash_val the previous hash value (used for chaining, may be empty)
109
* @param hash_val_len the length of hash_val
110
* @param length the desired output length
111
*/
112
secure_vector<uint8_t>
113
BOTAN_TEST_API hkdf_expand_label(
114
   const std::string& hash_fn,
115
   const uint8_t secret[], size_t secret_len,
116
   const std::string& label,
117
   const uint8_t hash_val[], size_t hash_val_len,
118
   size_t length);
119
120
121
}
122
123
#endif