-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCrypto.h
More file actions
114 lines (105 loc) · 2.94 KB
/
Crypto.h
File metadata and controls
114 lines (105 loc) · 2.94 KB
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
/**
* An extremely minimal crypto library for Arduino devices.
*
* The SHA256 and AES implementations are derived from axTLS
* (http://axtls.sourceforge.net/), Copyright (c) 2008, Cameron Rich.
*
* Ported and refactored by Chris Ellis 2016.
*
*/
#ifndef CRYPTO_h
#define CRYPTO_h
#include <Arduino.h>
#include <osapi.h>
#define SHA256_SIZE 32
#define SHA256HMAC_SIZE 32
#define SHA256HMAC_BLOCKSIZE 64
#define AES_MAXROUNDS 14
#define AES_BLOCKSIZE 16
#define AES_IV_SIZE 16
#define AES_IV_LENGTH 16
#define AES_128_KEY_LENGTH 16
#define AES_256_KEY_LENGTH 16
/**
* Compute a SHA256 hash
*/
class SHA256
{
public:
SHA256();
/**
* Update the hash with new data
*/
void doUpdate(const byte *msg, int len);
void doUpdate(const char *msg, unsigned int len) { doUpdate((byte*) msg, len); }
void doUpdate(const char *msg) { doUpdate((byte*) msg, strlen(msg)); }
/**
* Compute the final hash and store it in [digest], digest must be
* at least 32 bytes
*/
void doFinal(byte *digest);
/**
* Compute the final hash and check it matches this given expected hash
*/
bool matches(const byte *expected);
private:
void SHA256_Process(const byte digest[64]);
uint32_t total[2];
uint32_t state[8];
uint8_t buffer[64];
};
#define HMAC_OPAD 0x5C
#define HMAC_IPAD 0x36
/**
* Compute a HMAC using SHA256
*/
class SHA256HMAC
{
public:
/**
* Compute a SHA256 HMAC with the given [key] key of [length] bytes
* for authenticity
*/
SHA256HMAC(const byte *key, unsigned int keyLen);
/**
* Update the hash with new data
*/
void doUpdate(const byte *msg, unsigned int len);
void doUpdate(const char *msg, unsigned int len) { doUpdate((byte*) msg, len); }
void doUpdate(const char *msg) { doUpdate((byte*) msg, strlen(msg)); }
/**
* Compute the final hash and store it in [digest], digest must be
* at least 32 bytes
*/
void doFinal(byte *digest);
/**
* Compute the final hash and check it matches this given expected hash
*/
bool matches(const byte *expected);
private:
void blockXor(const byte *in, byte *out, byte val, byte len);
SHA256 _hash;
byte _innerKey[SHA256HMAC_BLOCKSIZE];
byte _outerKey[SHA256HMAC_BLOCKSIZE];
};
/**
* AES 128 and 256, based on code from axTLS
*/
class RNG
{
public:
/**
* Fill the [dst] array with [length] random bytes
*/
static void fill(uint8_t *dst, unsigned int length);
/**
* Get a random byte
*/
static byte get();
/**
* Get a 32bit random number
*/
static uint32_t getLong();
private:
};
#endif