aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/rng/hmac_rng/hmac_rng.h
blob: d6e9b48965be0ebf99e0b4bdf29679ea0e68df7c (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
/*
* HMAC RNG
* (C) 2008,2013,2016 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#ifndef BOTAN_HMAC_RNG_H__
#define BOTAN_HMAC_RNG_H__

#include <botan/stateful_rng.h>
#include <botan/mac.h>

namespace Botan {

/**
* HMAC_RNG - based on the design described in "On Extract-then-Expand
* Key Derivation Functions and an HMAC-based KDF" by Hugo Krawczyk
* (henceforce, 'E-t-E')
*
* However it actually could be parameterized with any two MAC functions,
* not restricted to HMAC (this variation is also described in
* Krawczyk's paper), for instance one could use HMAC(SHA-512) as the
* extractor and CMAC(AES-256) as the PRF.
*/
class BOTAN_DLL HMAC_RNG final : public Stateful_RNG
   {
   public:
      /**
      * Initialize an HMAC_RNG instance with the given MAC as PRF (normally HMAC)
      * @param underlying_rng is a reference to some RNG which will be used
      * to perform the periodic reseeding.
      * @param entropy_sources will be polled to perform reseeding periodically
      * @param reseed_interval specifies a limit of how many times
      * the RNG will be called before automatic reseeding is performed.
      */
      HMAC_RNG(std::unique_ptr<MessageAuthenticationCode> prf,
               RandomNumberGenerator& underlying_rng,
               Entropy_Sources& entropy_sources,
               size_t reseed_interval = BOTAN_RNG_DEFAULT_RESEED_INTERVAL);

      /**
      * Initialize an HMAC_RNG instance with the given MAC as PRF (normally HMAC)
      * @param underlying_rng is a reference to some RNG which will be used
      * to perform the periodic reseeding.
      * @param reseed_interval specifies a limit of how many times
      * the RNG will be called before automatic reseeding is performed.
      */
      HMAC_RNG(std::unique_ptr<MessageAuthenticationCode> prf,
               RandomNumberGenerator& underlying_rng,
               size_t reseed_interval = BOTAN_RNG_DEFAULT_RESEED_INTERVAL);

      /*
      * Initialize an HMAC_RNG instance with the given MAC as PRF (normally HMAC)
      * @param entropy_sources will be polled to perform reseeding periodically
      * @param reseed_interval specifies a limit of how many times
      * the RNG will be called before automatic reseeding is performed.
      */
      HMAC_RNG(std::unique_ptr<MessageAuthenticationCode> prf,
               Entropy_Sources& entropy_sources,
               size_t reseed_interval = BOTAN_RNG_DEFAULT_RESEED_INTERVAL);

      /**
      * Initialize an HMAC_RNG instance with the given MAC as PRF (normally HMAC)
      * Automatic reseeding is disabled completely.
      */
      HMAC_RNG(std::unique_ptr<MessageAuthenticationCode> prf);

      void randomize(byte buf[], size_t len) override;
      void clear() override;
      std::string name() const override;

      size_t reseed(Entropy_Sources& srcs,
                    size_t poll_bits,
                    std::chrono::milliseconds poll_timeout) override;

      void add_entropy(const byte[], size_t) override;

      size_t security_level() const override { return m_prf->output_length() * 8 / 2; }

   private:
      std::unique_ptr<MessageAuthenticationCode> m_prf;
      std::unique_ptr<MessageAuthenticationCode> m_extractor;

      enum HMAC_PRF_Label {
         Running,
         BlockFinished,
         Reseed,
         ExtractorSeed,
      };
      void new_K_value(byte label);

      secure_vector<byte> m_K;
      u32bit m_counter = 0;
   };

}

#endif