-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhmac.c
278 lines (232 loc) · 8.03 KB
/
hmac.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* Copyright Rusty Russell (Blockstream) 2015.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <string.h>
#include "hmac.h"
#define IPAD 0x3636363636363636ULL
#define OPAD 0x5C5C5C5C5C5C5C5CULL
#define BLOCK_256_U64S (HMAC_SHA256_BLOCKSIZE / sizeof(uint64_t))
#define BLOCK_512_U64S (HMAC_SHA512_BLOCKSIZE / sizeof(uint64_t))
static inline void xor_block_256(uint64_t block[BLOCK_256_U64S], uint64_t pad)
{
size_t i;
for (i = 0; i < BLOCK_256_U64S; i++)
block[i] ^= pad;
}
static inline void xor_block_512(uint64_t block[BLOCK_512_U64S], uint64_t pad)
{
size_t i;
for (i = 0; i < BLOCK_512_U64S; i++)
block[i] ^= pad;
}
void hmac_sha256_init(struct hmac_sha256_ctx *ctx,
const void *k, size_t ksize)
{
struct sha256 hashed_key;
/* We use k_opad as k_ipad temporarily. */
uint64_t *k_ipad = ctx->k_opad;
/* (keys longer than B bytes are first hashed using H) */
if (ksize > HMAC_SHA256_BLOCKSIZE) {
sha256(&hashed_key, k, ksize);
k = &hashed_key;
ksize = sizeof(hashed_key);
}
/* From RFC2104:
*
* (1) append zeros to the end of K to create a B byte string
* (e.g., if K is of length 20 bytes and B=64, then K will be
* appended with 44 zero bytes 0x00)
*/
memcpy(k_ipad, k, ksize);
memset((char *)k_ipad + ksize, 0, HMAC_SHA256_BLOCKSIZE - ksize);
/*
* (2) XOR (bitwise exclusive-OR) the B byte string computed
* in step (1) with ipad
*/
xor_block_256(k_ipad, IPAD);
/*
* We start (4) here, appending text later:
*
* (3) append the stream of data 'text' to the B byte string resulting
* from step (2)
* (4) apply H to the stream generated in step (3)
*/
sha256_init(&ctx->sha);
sha256_update(&ctx->sha, k_ipad, HMAC_SHA256_BLOCKSIZE);
/*
* (5) XOR (bitwise exclusive-OR) the B byte string computed in
* step (1) with opad
*/
xor_block_256(ctx->k_opad, IPAD^OPAD);
}
void hmac_sha512_init(struct hmac_sha512_ctx *ctx,
const void *k, size_t ksize)
{
struct sha512 hashed_key;
/* We use k_opad as k_ipad temporarily. */
uint64_t *k_ipad = ctx->k_opad;
/* (keys longer than B bytes are first hashed using H) */
if (ksize > HMAC_SHA512_BLOCKSIZE) {
sha512(&hashed_key, k, ksize);
k = &hashed_key;
ksize = sizeof(hashed_key);
}
/* From RFC2104:
*
* (1) append zeros to the end of K to create a B byte string
* (e.g., if K is of length 20 bytes and B=64, then K will be
* appended with 44 zero bytes 0x00)
*/
memcpy(k_ipad, k, ksize);
memset((char *)k_ipad + ksize, 0, HMAC_SHA512_BLOCKSIZE - ksize);
/*
* (2) XOR (bitwise exclusive-OR) the B byte string computed
* in step (1) with ipad
*/
xor_block_512(k_ipad, IPAD);
/*
* We start (4) here, appending text later:
*
* (3) append the stream of data 'text' to the B byte string resulting
* from step (2)
* (4) apply H to the stream generated in step (3)
*/
sha512_init(&ctx->sha);
sha512_update(&ctx->sha, k_ipad, HMAC_SHA512_BLOCKSIZE);
/*
* (5) XOR (bitwise exclusive-OR) the B byte string computed in
* step (1) with opad
*/
xor_block_512(ctx->k_opad, IPAD^OPAD);
}
void hmac_sha256_update(struct hmac_sha256_ctx *ctx, const void *p, size_t size)
{
/* This is the appending-text part of this:
*
* (3) append the stream of data 'text' to the B byte string resulting
* from step (2)
* (4) apply H to the stream generated in step (3)
*/
sha256_update(&ctx->sha, p, size);
}
void hmac_sha512_update(struct hmac_sha512_ctx *ctx, const void *p, size_t size)
{
sha512_update(&ctx->sha, p, size);
}
void hmac_sha256_done(struct hmac_sha256_ctx *ctx,
struct hmac_sha256 *hmac)
{
/* (4) apply H to the stream generated in step (3) */
sha256_done(&ctx->sha, &hmac->sha);
/*
* (6) append the H result from step (4) to the B byte string
* resulting from step (5)
* (7) apply H to the stream generated in step (6) and output
* the result
*/
sha256_init(&ctx->sha);
sha256_update(&ctx->sha, ctx->k_opad, sizeof(ctx->k_opad));
sha256_update(&ctx->sha, &hmac->sha, sizeof(hmac->sha));
sha256_done(&ctx->sha, &hmac->sha);
}
void hmac_sha512_done(struct hmac_sha512_ctx *ctx,
struct hmac_sha512 *hmac)
{
/* (4) apply H to the stream generated in step (3) */
sha512_done(&ctx->sha, &hmac->sha);
/*
* (6) append the H result from step (4) to the B byte string
* resulting from step (5)
* (7) apply H to the stream generated in step (6) and output
* the result
*/
sha512_init(&ctx->sha);
sha512_update(&ctx->sha, ctx->k_opad, sizeof(ctx->k_opad));
sha512_update(&ctx->sha, &hmac->sha, sizeof(hmac->sha));
sha512_done(&ctx->sha, &hmac->sha);
}
#if 1
void hmac_sha256(struct hmac_sha256 *hmac,
const void *k, size_t ksize,
const void *d, size_t dsize)
{
struct hmac_sha256_ctx ctx;
hmac_sha256_init(&ctx, k, ksize);
hmac_sha256_update(&ctx, d, dsize);
hmac_sha256_done(&ctx, hmac);
}
void hmac_sha512(struct hmac_sha512 *hmac,
const void *k, size_t ksize,
const void *d, size_t dsize)
{
struct hmac_sha512_ctx ctx;
hmac_sha512_init(&ctx, k, ksize);
hmac_sha512_update(&ctx, d, dsize);
hmac_sha512_done(&ctx, hmac);
}
#else
/* Direct mapping from MD5 example in RFC2104 */
void hmac_sha256(struct hmac_sha256 *hmac,
const void *key, size_t key_len,
const void *text, size_t text_len)
{
struct sha256_ctx context;
unsigned char k_ipad[65]; /* inner padding -
* key XORd with ipad
*/
unsigned char k_opad[65]; /* outer padding -
* key XORd with opad
*//* start out by storing key in pads */
unsigned char tk[32];
int i;
/* if key is longer than 64 bytes reset it to key=MD5(key) */
if (key_len > 64) {
struct sha256_ctx tctx;
sha256_init(&tctx);
sha256_update(&tctx, key, key_len);
sha256_done(&tctx, tk);
key = tk;
key_len = 32;
}
bzero( k_ipad, sizeof k_ipad);
bzero( k_opad, sizeof k_opad);
bcopy( key, k_ipad, key_len);
bcopy( key, k_opad, key_len);
/* XOR key with ipad and opad values */
for (i=0; i<64; i++) {
k_ipad[i] ^= 0x36;
k_opad[i] ^= 0x5c;
}
/*
* perform inner MD5
*/
sha256_init(&context); /* init context for 1st
* pass */
sha256_update(&context, k_ipad, 64); /* start with inner pad */
sha256_update(&context, text, text_len); /* then text of datagram */
sha256_done(&context, &hmac->sha); /* finish up 1st pass */
/*
* perform outer MD5
*/
sha256_init(&context); /* init context for 2nd
* pass */
sha256_update(&context, k_opad, 64); /* start with outer pad */
sha256_update(&context, &hmac->sha, 32); /* then results of 1st
* hash */
sha256_done(&context, &hmac->sha); /* finish up 2nd pass */
}
#endif