-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCryptoinoHMAC.cpp
167 lines (137 loc) · 4.18 KB
/
CryptoinoHMAC.cpp
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
158
159
160
161
162
163
164
165
166
167
/*
* CryptoinoHMAC.cpp - This file is part of Cryptoino
* Copyright (C) 2014 Matthias Kruk
*
* Cryptoino is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 3, or (at your
* option) any later version.
*
* Cryptoino is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cryptoino; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/* !!! HERE BE DRAGONS !!!
*
* Cryptoino is a library that implements some cryptographic primitives
* and mechanisms for use with Arduino boards. IT HAS NEVER BEEN AUDITED
* OR OTHERWISE SCRUTINISED. YOU SHOULD CONSIDER IT UNSAFE AND PRONE TO
* ANY KIND OF SIDE CHANNEL ATTACKS AND ABSOLUTELY NOT USE IT IN ANY KIND
* OF ADVERSARIAL ENVIRONMENT. FURTHERMORE, IT HAS NEVER BEEN OPTIMISED
* FOR PERFORMANCE. USE IT AT YOUR OWN RISK AND FOR TESTING/EDUCATIONAL
* PURPOSES ONLY! YOU HAVE BEEN WARNED.
*/
#include <CryptoinoSHA256.h>
#include <CryptoinoHMAC.h>
#include <CryptoinoErrno.h>
#include <string.h>
#include <stdint.h>
#include <Arduino.h>
/*
* FIXME: These values have been deliberately choosen
* without of any further considerations
*/
#define HMAC_OUTER_PAD 0x5c5c5c5c
#define HMAC_INNER_PAD 0x36363636
HMAC::HMAC(void)
{
this->zero();
return;
}
HMAC::~HMAC(void)
{
this->zero();
return;
}
void HMAC::zero(void)
{
this->hm_errno = 0;
memset(this->hm_opad, 0, sizeof(this->hm_opad));
memset(this->hm_ipad, 0, sizeof(this->hm_ipad));
this->__init = 0;
return;
}
int HMAC::init(const void *key, const int len)
{
uint32_t *k;
if(len != HMAC_KEY_SIZE) {
this->hm_errno = CEINVALKEYLEN;
return(-1);
}
k = (uint32_t*)key;
this->hm_opad[0] = k[0] ^ HMAC_OUTER_PAD;
this->hm_opad[1] = k[1] ^ HMAC_OUTER_PAD;
this->hm_opad[2] = k[2] ^ HMAC_OUTER_PAD;
this->hm_opad[3] = k[3] ^ HMAC_OUTER_PAD;
this->hm_opad[4] = k[4] ^ HMAC_OUTER_PAD;
this->hm_opad[5] = k[5] ^ HMAC_OUTER_PAD;
this->hm_opad[6] = k[6] ^ HMAC_OUTER_PAD;
this->hm_opad[7] = k[7] ^ HMAC_OUTER_PAD;
this->hm_ipad[0] = k[0] ^ HMAC_INNER_PAD;
this->hm_ipad[1] = k[1] ^ HMAC_INNER_PAD;
this->hm_ipad[2] = k[2] ^ HMAC_INNER_PAD;
this->hm_ipad[3] = k[3] ^ HMAC_INNER_PAD;
this->hm_ipad[4] = k[4] ^ HMAC_INNER_PAD;
this->hm_ipad[5] = k[5] ^ HMAC_INNER_PAD;
this->hm_ipad[6] = k[6] ^ HMAC_INNER_PAD;
this->hm_ipad[7] = k[7] ^ HMAC_INNER_PAD;
this->__init = 1;
return(0);
}
int HMAC::authenticate(const void *buf, const size_t len, void *hash, const size_t size)
{
SHA256 ctx;
char ihash[HMAC_OUTPUT_SIZE];
if(!this->__init) {
this->hm_errno = CEINVALSTATE;
return(-1);
}
if(size < HMAC_KEY_SIZE) {
this->hm_errno = CENOSPC;
return(-1);
}
/* the calculation of the hmac works roughly like this:
*
* sha256((k ^ a) ## sha256((k ^ b) ## m))
*
* where k is the secret key used for authentication, ^ means XOR,
* a and b are constants defined in this file (look at the top) and
* ## is the concatenation operation */
ctx.zero();
ctx.feed((const char*)this->hm_ipad, sizeof(this->hm_ipad));
ctx.feed((const char*)buf, len);
ctx.digest(ihash);
/* zero() is called at the end of digest(), so no need to call it manually */
ctx.feed((const char*)this->hm_opad, sizeof(this->hm_opad));
ctx.feed(ihash, sizeof(ihash));
ctx.digest((char*)hash);
this->hm_errno = 0;
return(0);
}
int HMAC::verify(const void *msg, const size_t msg_len, const void *mac, const size_t mac_len)
{
char expectation[HMAC_OUTPUT_SIZE];
if(mac_len != HMAC_OUTPUT_SIZE) {
this->hm_errno = CEINVALMACLEN;
return(-1);
}
if(this->authenticate(msg, msg_len, expectation, sizeof(expectation)) < 0) {
return(-1);
}
if(memcmp(mac, expectation, mac_len) != 0) {
this->hm_errno = CEINVALMAC;
return(-1);
}
this->hm_errno = 0;
return(0);
}
const char* HMAC::strerror(void)
{
return(strcerror(this->hm_errno));
}