Arduino Cryptography Library
RNG.h
1 /*
2  * Copyright (C) 2015 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef CRYPTO_RNG_h
24 #define CRYPTO_RNG_h
25 
26 #include <inttypes.h>
27 #include <stddef.h>
28 
29 class NoiseSource;
30 
31 class RNGClass
32 {
33 public:
34  RNGClass();
35  ~RNGClass();
36 
37  void begin(const char *tag);
38  void addNoiseSource(NoiseSource &source);
39 
40  void setAutoSaveTime(uint16_t minutes);
41 
42  void rand(uint8_t *data, size_t len);
43  bool available(size_t len) const;
44 
45  void stir(const uint8_t *data, size_t len, unsigned int credit = 0);
46 
47  void save();
48 
49  void loop();
50 
51  void destroy();
52 
53  static const int SEED_SIZE = 48;
54 
55 private:
56  uint32_t block[16];
57  uint32_t stream[16];
58  uint16_t credits : 13;
59  uint16_t firstSave : 1;
60  uint16_t initialized : 1;
61  uint16_t trngPending : 1;
62  unsigned long timer;
63  unsigned long timeout;
64  NoiseSource *noiseSources[4];
65  uint8_t count;
66  uint8_t trngPosn;
67 
68  void rekey();
69  void mixTRNG();
70 };
71 
72 extern RNGClass RNG;
73 
74 #endif
Abstract base class for random noise sources.
Definition: NoiseSource.h:30
Pseudo random number generator suitable for cryptography.
Definition: RNG.h:32
void save()
Saves the random seed to EEPROM.
Definition: RNG.cpp:856
void rand(uint8_t *data, size_t len)
Generates random bytes into a caller-supplied buffer.
Definition: RNG.cpp:660
void setAutoSaveTime(uint16_t minutes)
Sets the amount of time between automatic seed saves.
Definition: RNG.cpp:636
void begin(const char *tag)
Initializes the random number generator.
Definition: RNG.cpp:438
void loop()
Run periodic housekeeping tasks on the random number generator.
Definition: RNG.cpp:898
void destroy()
Destroys the data in the random number pool and the saved seed in EEPROM.
Definition: RNG.cpp:1035
void addNoiseSource(NoiseSource &source)
Adds a noise source to the random number generator.
Definition: RNG.cpp:611
bool available(size_t len) const
Determine if there is sufficient entropy available for a specific request size.
Definition: RNG.cpp:754
RNGClass()
Constructs a new random number generator instance.
Definition: RNG.cpp:311
void stir(const uint8_t *data, size_t len, unsigned int credit=0)
Stirs additional entropy data into the random pool.
Definition: RNG.cpp:787
static const int SEED_SIZE
Size of a saved random number seed in EEPROM space.
Definition: RNG.h:53
~RNGClass()
Destroys this random number generator instance.
Definition: RNG.cpp:326