HMAC-sha1 加密算法

####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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#include <stdlib.h>
#include <stdio.h>
#include <cstring>
#include "hmac_sha1.h"
#define MAX_MESSAGE_LENGTH 4096
/*****************************/
/**** Function Prototypes ****/
/*****************************/

unsigned long int ft(
int t,
unsigned long int x,
unsigned long int y,
unsigned long int z
);

void sha1 (
unsigned char *message,
int message_length,
unsigned char *digest
);

/**************************/
/* Debug out */
/**************************/

#ifdef HMAC_DEBUG
debug_out(
unsigned char *label,
unsigned char *data,
int data_length
)
{
int i,j;
int num_blocks;
int block_remainder;
num_blocks = data_length / 16;
block_remainder = data_length % 16;

printf("%s\n",label);

for (i=0; i< num_blocks;i++)
{
printf("\t");
for (j=0; j< 16;j++)
{
printf("%02x ", data[j + (i*16)]);
}
printf("\n");
}

if (block_remainder > 0)
{
printf("\t");
for (j=0; j<block_remainder; j++)
{
printf("%02x ", data[j+(num_blocks*16)]);
}
printf("\n");
}
}
#endif

/****************************************/
/* sha1() */
/* Performs the NIST SHA-1 algorithm */
/****************************************/

unsigned long int ft(
int t,
unsigned long int x,
unsigned long int y,
unsigned long int z
)
{
unsigned long int a,b,c;

if (t < 20)
{
a = x & y;
b = (~x) & z;
c = a ^ b;
}
else if (t < 40)
{
c = x ^ y ^ z;
}
else if (t < 60)
{
a = x & y;
b = a ^ (x & z);
c = b ^ (y & z);
}
else if (t < 80)
{
c = (x ^ y) ^ z;
}

return c;
}

unsigned long int k(int t)
{
unsigned long int c;

if (t < 20)
{
c = 0x5a827999;
}
else if (t < 40)
{
c = 0x6ed9eba1;
}
else if (t < 60)
{
c = 0x8f1bbcdc;
}
else if (t < 80)
{
c = 0xca62c1d6;
}

return c;
}

unsigned long int rotr(int bits, unsigned long int a)
{
unsigned long int c,d,e,f,g;
c = (0x0001 << bits)-1;
d = ~c;

e = (a & d) >> bits;
f = (a & c) << (32 - bits);

g = e | f;

return (g & 0xffffffff );

}

unsigned long int rotl(int bits, unsigned long int a)
{
unsigned long int c,d,e,f,g;
c = (0x0001 << (32-bits))-1;
d = ~c;

e = (a & c) << bits;
f = (a & d) >> (32 - bits);

g = e | f;

return (g & 0xffffffff );

}


void sha1 (
unsigned char *message,
int message_length,
unsigned char *digest
)
{
int i;
int num_blocks;
int block_remainder;
int padded_length;

unsigned long int l;
unsigned long int t;
unsigned long int h[5];
unsigned long int a,b,c,d,e;
unsigned long int w[80];
unsigned long int temp;

#ifdef SHA1_DEBUG
int x,y;
#endif

/* Calculate the number of 512 bit blocks */

padded_length = message_length + 8; /* Add length for l */
padded_length = padded_length + 1; /* Add the 0x01 bit postfix */

l = message_length * 8;

num_blocks = padded_length / 64;
block_remainder = padded_length % 64;


if (block_remainder > 0)
{
num_blocks++;
}

padded_length = padded_length + (64 - block_remainder);

/* clear the padding field */
for (i = message_length; i < (num_blocks * 64); i++)
{
message[i] = 0x00;
}

/* insert b1 padding bit */
message[message_length] = 0x80;

/* Insert l */
message[(num_blocks*64)-1] = (unsigned char)( l & 0xff);
message[(num_blocks*64)-2] = (unsigned char)((l >> 8) & 0xff);
message[(num_blocks*64)-3] = (unsigned char)((l >> 16) & 0xff);
message[(num_blocks*64)-4] = (unsigned char)((l >> 24) & 0xff);

/* Set initial hash state */
h[0] = 0x67452301;
h[1] = 0xefcdab89;
h[2] = 0x98badcfe;
h[3] = 0x10325476;
h[4] = 0xc3d2e1f0;

#ifdef SHA1_DEBUG
printf("INITIAL message_length = %d\n", message_length);
printf("INITIAL padded_length = %d\n", padded_length);
printf("INITIAL num_blocks = %d\n", num_blocks);

for (x=0;x<num_blocks; x++)
{
printf("\t\t");
for (y=0; y<16;y++)
{
printf("%02x ",message[y + (x*64)]);
}
printf("\n");
printf("\t\t");
for (y=0; y<16;y++)
{
printf("%02x ",message[16 + y + (x*64)]);
}
printf("\n");
printf("\t\t");
for (y=0; y<16;y++)
{
printf("%02x ",message[32 + y + (x*64)]);
}
printf("\n");
printf("\t\t");
for (y=0; y<16;y++)
{
printf("%02x ",message[48 + y + (x*64)]);
}
printf("\n");
}

#endif

for (i = 0; i < num_blocks; i++)
{
/* Prepare the message schedule */
for (t=0; t < 80; t++)
{
if (t < 16)
{
w[t] = (256*256*256) * message[(i*64)+(t*4)];
w[t] += (256*256 ) * message[(i*64)+(t*4) + 1];
w[t] += (256 ) * message[(i*64)+(t*4) + 2];
w[t] += message[(i*64)+(t*4) + 3];
}
else if (t < 80)
{
w[t] = rotl(1,(w[t-3] ^ w[t-8] ^ w[t-14] ^ w[t-16]));
}
}

#ifdef SHA1_DEBUG
printf("\tW(0) = %08lX \t W(9) = %08lX \n", w[0], w[8]);
printf("\tW(1) = %08lX \t W(10) = %08lX \n", w[1], w[9]);
printf("\tW(2) = %08lX \t W(11) = %08lX \n", w[2], w[10]);
printf("\tW(3) = %08lX \t W(12) = %08lX \n", w[3], w[11]);
printf("\tW(4) = %08lX \t W(13) = %08lX \n", w[4], w[12]);
printf("\tW(5) = %08lX \t W(14) = %08lX \n", w[5], w[13]);
printf("\tW(6) = %08lX \t W(15) = %08lX \n", w[6], w[14]);
printf("\tW(7) = %08lX \t W(16) = %08lX \n\n", w[7], w[15]);

#endif
/* Initialize the five working variables */
a = h[0];
b = h[1];
c = h[2];
d = h[3];
e = h[4];

/* iterate a-e 80 times */

for (t = 0; t < 80; t++)
{
temp = (rotl(5,a) + ft(t,b,c,d)) & 0xffffffff;
temp = (temp + e) & 0xffffffff;
temp = (temp + k(t)) & 0xffffffff;
temp = (temp + w[t]) & 0xffffffff;
e = d;
d = c;
c = rotl(30,b);
b = a;
a = temp;
#ifdef SHA1_DEBUG
printf("t = %2ld\t %08lx, %08lx, %08lx, %08lx, %08lx\n", t,a,b,c,d,e);
#endif

}

/* compute the ith intermediate hash value */
#ifdef SHA1_DEBUG
printf(" + \t %08lx, %08lx, %08lx, %08lx, %08lx\n", h[0],h[1],h[2],h[3],h[4]);
#endif
h[0] = (a + h[0]) & 0xffffffff;
h[1] = (b + h[1]) & 0xffffffff;
h[2] = (c + h[2]) & 0xffffffff;
h[3] = (d + h[3]) & 0xffffffff;
h[4] = (e + h[4]) & 0xffffffff;

#ifdef SHA1_DEBUG
printf(" = \t %08lx, %08lx, %08lx, %08lx, %08lx\n", h[0],h[1],h[2],h[3],h[4]);
#endif

}

digest[3] = (unsigned char) ( h[0] & 0xff);
digest[2] = (unsigned char) ((h[0] >> 8) & 0xff);
digest[1] = (unsigned char) ((h[0] >> 16) & 0xff);
digest[0] = (unsigned char) ((h[0] >> 24) & 0xff);

digest[7] = (unsigned char) ( h[1] & 0xff);
digest[6] = (unsigned char) ((h[1] >> 8) & 0xff);
digest[5] = (unsigned char) ((h[1] >> 16) & 0xff);
digest[4] = (unsigned char) ((h[1] >> 24) & 0xff);

digest[11] = (unsigned char) ( h[2] & 0xff);
digest[10] = (unsigned char) ((h[2] >> 8) & 0xff);
digest[9] = (unsigned char) ((h[2] >> 16) & 0xff);
digest[8] = (unsigned char) ((h[2] >> 24) & 0xff);

digest[15] = (unsigned char) ( h[3] & 0xff);
digest[14] = (unsigned char) ((h[3] >> 8) & 0xff);
digest[13] = (unsigned char) ((h[3] >> 16) & 0xff);
digest[12] = (unsigned char) ((h[3] >> 24) & 0xff);

digest[19] = (unsigned char) ( h[4] & 0xff);
digest[18] = (unsigned char) ((h[4] >> 8) & 0xff);
digest[17] = (unsigned char) ((h[4] >> 16) & 0xff);
digest[16] = (unsigned char) ((h[4] >> 24) & 0xff);

}

/******************************************************/
/* hmac-sha1() */
/* Performs the hmac-sha1 keyed secure hash algorithm */
/******************************************************/

void hmac_sha1(
unsigned char *key,
int key_length,
unsigned char *data,
int data_length,
unsigned char *digest
)

{
int b = 64; /* blocksize */
unsigned char ipad = 0x36;

unsigned char opad = 0x5c;

unsigned char k0[64];
unsigned char k0xorIpad[64];
unsigned char step7data[64];
unsigned char step5data[MAX_MESSAGE_LENGTH+128];
unsigned char step8data[64+20];
int i;

for (i=0; i<64; i++)
{
k0[i] = 0x00;
}



if (key_length != b) /* Step 1 */
{
/* Step 2 */
if (key_length > b)
{
sha1(key, key_length, digest);
for (i=0;i<20;i++)
{
k0[i]=digest[i];
}
}
else if (key_length < b) /* Step 3 */
{
for (i=0; i<key_length; i++)
{
k0[i] = key[i];
}
}
}
else
{
for (i=0;i<b;i++)
{
k0[i] = key[i];
}
}
#ifdef HMAC_DEBUG
debug_out("k0",k0,64);
#endif
/* Step 4 */
for (i=0; i<64; i++)
{
k0xorIpad[i] = k0[i] ^ ipad;
}
#ifdef HMAC_DEBUG
debug_out("k0 xor ipad",k0xorIpad,64);
#endif
/* Step 5 */
for (i=0; i<64; i++)
{
step5data[i] = k0xorIpad[i];
}
for (i=0;i<data_length;i++)
{
step5data[i+64] = data[i];
}
#ifdef HMAC_DEBUG
debug_out("(k0 xor ipad) || text",step5data,data_length+64);
#endif

/* Step 6 */
sha1(step5data, data_length+b, digest);

#ifdef HMAC_DEBUG
debug_out("Hash((k0 xor ipad) || text)",digest,20);
#endif

/* Step 7 */
for (i=0; i<64; i++)
{
step7data[i] = k0[i] ^ opad;
}

#ifdef HMAC_DEBUG
debug_out("(k0 xor opad)",step7data,64);
#endif

/* Step 8 */
for (i=0;i<64;i++)
{
step8data[i] = step7data[i];
}
for (i=0;i<20;i++)
{
step8data[i+64] = digest[i];
}

#ifdef HMAC_DEBUG
debug_out("(k0 xor opad) || Hash((k0 xor ipad) || text)",step8data,20+64);
#endif

/* Step 9 */
sha1(step8data, b+20, digest);

#ifdef HMAC_DEBUG
debug_out("HASH((k0 xor opad) || Hash((k0 xor ipad) || text))",digest,20);
#endif
}

####原理

#####hmac(hashing message authentication code)信息认证码,一种计算认证码的算法,

  • 输入:secret,message
  • 输出:byte[];
  • 简单步骤:hash(secret ^ out , hash(serret ^ in , message )) ;
    • new char in 0x36 ;new char out 0x5c
    • 将secret跟in异或之后加入message,结果保存为message1
    • 将message1做hash运算(sha1),结果保存为item1
    • 将secret跟out异或之后加入item1,结果保存为message2;
    • 将message2做hash运算(sha1),结果return;

#####sha1 一种hash算法,在hmac算法过程中会用到

ps:hmac-sha1之后获取的20位byte

####作用

​ hmac主要应用在身份验证中,它的使用方法是这样的:  

  1. 客户端发出登录请求(假设是浏览器的GET请求)   

  2. 服务器返回一个随机值,并在会话中记录这个随机值   

  3. 客户端将该随机值作为密钥,用户密码进行hmac运算,然后提交给服务器   

  4. 服务器读取用户数据库中的用户密码和步骤2中发送的随机值做与客户端一样的hmac运算,然后与用户发送的结果比较,如果结果一致则验证用户合法 。  

在这个过程中,可能遭到安全攻击的是服务器发送的随机值和用户发送的hmac结果,而对于截获 了这两个值的黑客而言这两个值是没有意义的,绝无获取用户密码的可能性,随机值的引入使hmac只在当前会话中有效,大大增强了安全性和实用性。