aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/kdf/sp800_108/sp800_108.cpp
blob: 8b1af2ea7e4d69f16eb419e499813b20bcfadf4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
* KDFs defined in NIST SP 800-108
* (C) 2016 Kai Michaelis
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/sp800_108.h>
#include <botan/hmac.h>

#include <iterator>

namespace Botan {

size_t SP800_108_Counter::kdf(uint8_t key[], size_t key_len,
                              const uint8_t secret[], size_t secret_len,
                              const uint8_t salt[], size_t salt_len,
                              const uint8_t label[], size_t label_len) const
   {
      const std::size_t prf_len =  m_prf->output_length();
      const uint8_t delim = 0;
      uint8_t *p = key;
      uint32_t counter = 1;
      uint32_t length = key_len * 8;
      uint8_t be_len[4] = { 0 };
      secure_vector<uint8_t> tmp;

      store_be(length, be_len);
      m_prf->set_key(secret, secret_len);

      while(p < key + key_len && counter != 0)
         {
         const std::size_t to_copy = std::min< std::size_t >(key + key_len - p, prf_len);
         uint8_t be_cnt[4] = { 0 };

         store_be(counter, be_cnt);

         m_prf->update(be_cnt,4);
         m_prf->update(label,label_len);
         m_prf->update(delim);
         m_prf->update(salt,salt_len);
         m_prf->update(be_len,4);
         m_prf->final(tmp);

         std::move(tmp.begin(), tmp.begin() + to_copy, p);
         ++counter;

         if (counter == 0)
            throw Invalid_Argument("Can't process more than 4GB");

         p += to_copy;
         }

   return key_len;
   }

size_t SP800_108_Feedback::kdf(uint8_t key[], size_t key_len,
                               const uint8_t secret[], size_t secret_len,
                               const uint8_t salt[], size_t salt_len,
                               const uint8_t label[], size_t label_len) const
   {
      const std::size_t prf_len =  m_prf->output_length();
      const std::size_t iv_len = (salt_len >= prf_len ? prf_len : 0);
      const uint8_t delim = 0;

      uint8_t *p = key;
      uint32_t counter = 1;
      uint32_t length = key_len * 8;
      uint8_t be_len[4] = { 0 };
      secure_vector< uint8_t > prev(salt, salt + iv_len);
      secure_vector< uint8_t > ctx(salt + iv_len, salt + salt_len);

      store_be(length, be_len);
      m_prf->set_key(secret, secret_len);

      while(p < key + key_len && counter != 0)
         {
         const std::size_t to_copy = std::min< std::size_t >(key + key_len - p, prf_len);
         uint8_t be_cnt[4] = { 0 };

         store_be(counter, be_cnt);

         m_prf->update(prev);
         m_prf->update(be_cnt,4);
         m_prf->update(label,label_len);
         m_prf->update(delim);
         m_prf->update(ctx);
         m_prf->update(be_len,4);
         m_prf->final(prev);

         std::copy(prev.begin(), prev.begin() + to_copy, p);
         ++counter;

         if (counter == 0)
            throw Invalid_Argument("Can't process more than 4GB");

         p += to_copy;
         }

   return key_len;
   }

size_t SP800_108_Pipeline::kdf(uint8_t key[], size_t key_len,
                    const uint8_t secret[], size_t secret_len,
                    const uint8_t salt[], size_t salt_len,
                    const uint8_t label[], size_t label_len) const
   {
      const std::size_t prf_len =  m_prf->output_length();
      const uint8_t delim = 0;

      uint8_t *p = key;
      uint32_t counter = 1;
      uint32_t length = key_len * 8;
      uint8_t be_len[4] = { 0 };
      secure_vector<uint8_t> ai, ki;

      store_be(length, be_len);
      m_prf->set_key(secret,secret_len);

      // A(0)
      std::copy(label,label + label_len,std::back_inserter(ai));
      ai.emplace_back(delim);
      std::copy(salt,salt + salt_len,std::back_inserter(ai));
      std::copy(be_len,be_len + 4,std::back_inserter(ai));

      while(p < key + key_len && counter != 0)
         {
         // A(i)
         m_prf->update(ai);
         m_prf->final(ai);

         // K(i)
         const std::size_t to_copy = std::min< std::size_t >(key + key_len - p, prf_len);
         uint8_t be_cnt[4] = { 0 };

         store_be(counter, be_cnt);

         m_prf->update(ai);
         m_prf->update(be_cnt,4);
         m_prf->update(label, label_len);
         m_prf->update(delim);
         m_prf->update(salt, salt_len);
         m_prf->update(be_len,4);
         m_prf->final(ki);

         std::copy(ki.begin(), ki.begin() + to_copy, p);
         ++counter;

         if (counter == 0)
            throw Invalid_Argument("Can't process more than 4GB");

         p += to_copy;
         }

   return key_len;
   }
}