EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
cryptModule.h
Go to the documentation of this file.
1 
11 #ifndef _CRYPTMODULE_h
12 #define _CRYPTMODULE_h
13 
14 #if defined(ARDUINO) && ARDUINO >= 100
15 #include "Arduino.h"
16 #else
17 #include "WProgram.h"
18 #endif
19 #include "EnigmaIoTconfig.h"
20 
21 #define CRYPTMODULE_DEBUG_TAG "CryptModule"
22 
23 #ifdef ESP8266
24 #define RANDOM_32 0x3FF20E44
25 #endif
26 
27 const uint8_t RANDOM_LENGTH = sizeof (uint32_t);
28 const uint8_t CRC_LENGTH = sizeof (uint32_t);
29 
35 class CryptModule {
36 public:
41  static uint32_t random ();
42 
43  static uint32_t random (uint32_t max, uint32_t min = 0) {
44  uint32_t _max, _min;
45 
46  if (max > min) {
47  _max = max;
48  _min = min;
49  } else {
50  _max = min;
51  _min = max;
52  }
53  if (_max != _min) {
54  return _min + (random () % (_max - _min));
55  } else {
56  return _min;
57  }
58  }
59 
66  static uint8_t* random (const uint8_t* buf, size_t len);
67 
82  static bool decryptBuffer (const uint8_t* data, size_t length,
83  const uint8_t* iv, uint8_t ivlen, const uint8_t* key, uint8_t keylen,
84  const uint8_t* aad, uint8_t aadLen, const uint8_t* tag, uint8_t tagLen);
85 
92  static uint8_t* getSHA256 (uint8_t* buffer, uint8_t length);
93 
108  static bool encryptBuffer (const uint8_t* data, size_t length,
109  const uint8_t* iv, uint8_t ivlen, const uint8_t* key, uint8_t keylen,
110  const uint8_t* aad, uint8_t aadLen, const uint8_t* tag, uint8_t tagLen);
111 
115  void getDH1 ();
116 
122  bool getDH2 (const uint8_t* remotePubKey);
123 
128  uint8_t* getPrivDHKey () {
129  return privateDHKey;
130  }
131 
136  uint8_t* getPubDHKey () {
137  return publicDHKey;
138  }
139 
140 protected:
143 };
144 
145 extern CryptModule Crypto;
146 
147 #endif
148 
CryptModule::decryptBuffer
static bool decryptBuffer(const uint8_t *data, size_t length, const uint8_t *iv, uint8_t ivlen, const uint8_t *key, uint8_t keylen, const uint8_t *aad, uint8_t aadLen, const uint8_t *tag, uint8_t tagLen)
Decrypts a buffer using a shared key.
Definition: cryptModule.cpp:52
CryptModule::getPubDHKey
uint8_t * getPubDHKey()
Gets own public key used on Diffie Hellman algorithm.
Definition: cryptModule.h:136
CryptModule::getDH1
void getDH1()
Starts first stage of Diffie Hellman key agreement algorithm.
Definition: cryptModule.cpp:141
CryptModule::privateDHKey
uint8_t privateDHKey[KEY_LENGTH]
Temporary private key store used during key agreement.
Definition: cryptModule.h:141
CryptModule::encryptBuffer
static bool encryptBuffer(const uint8_t *data, size_t length, const uint8_t *iv, uint8_t ivlen, const uint8_t *key, uint8_t keylen, const uint8_t *aad, uint8_t aadLen, const uint8_t *tag, uint8_t tagLen)
Decrypts a buffer using a shared key.
Definition: cryptModule.cpp:86
EnigmaIoTconfig.h
Parameter configuration.
CryptModule
EnigmaIoT Crypto module. Wraps Arduino CryptoLib classes and methods.
Definition: cryptModule.h:35
Crypto
CryptModule Crypto
Singleton Crypto class instance.
Definition: cryptModule.cpp:167
CryptModule::getSHA256
static uint8_t * getSHA256(uint8_t *buffer, uint8_t length)
Generates a SHA256 hash from input.
Definition: cryptModule.cpp:20
CryptModule::random
static uint32_t random(uint32_t max, uint32_t min=0)
Definition: cryptModule.h:43
CryptModule::getPrivDHKey
uint8_t * getPrivDHKey()
Gets own private key used on Diffie Hellman algorithm.
Definition: cryptModule.h:128
CryptModule::random
static uint32_t random()
Gets a random number.
Definition: cryptModule.cpp:119
RANDOM_LENGTH
const uint8_t RANDOM_LENGTH
Length of random number generator values.
Definition: cryptModule.h:27
KEY_LENGTH
const uint8_t KEY_LENGTH
Key length used by selected crypto algorythm. The only tested value is 32. Change it only if you know...
Definition: EnigmaIoTconfigAdvanced.h:70
data
@ data
Definition: GwOutput_generic.h:23
CryptModule::getDH2
bool getDH2(const uint8_t *remotePubKey)
Starts second stage of Diffie Hellman key agreement algorithm and calculate shares key.
Definition: cryptModule.cpp:148
CRC_LENGTH
const uint8_t CRC_LENGTH
Length of CRC.
Definition: cryptModule.h:28
CryptModule::publicDHKey
uint8_t publicDHKey[KEY_LENGTH]
Temporary public key store used during key agreement.
Definition: cryptModule.h:142