summaryrefslogtreecommitdiff
path: root/tools/hmac-sha1.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/hmac-sha1.c')
-rw-r--r--tools/hmac-sha1.c48
1 files changed, 19 insertions, 29 deletions
diff --git a/tools/hmac-sha1.c b/tools/hmac-sha1.c
index 1b4b3b5abd..8882b10b9a 100644
--- a/tools/hmac-sha1.c
+++ b/tools/hmac-sha1.c
@@ -29,6 +29,8 @@
29 * 29 *
30 */ 30 */
31 31
32#include <string.h>
33
32#include "hmac-sha1.h" 34#include "hmac-sha1.h"
33 35
34/* 36/*
@@ -386,35 +388,20 @@ void SHA1PadMessage(SHA1Context *context)
386 388
387#define SHA_BLOCKSIZE 64 389#define SHA_BLOCKSIZE 64
388 390
389static void truncate
390(
391 char* d1, /* data to be truncated */
392 char* d2, /* truncated data */
393 int len /* length in bytes to keep */
394)
395{
396 int i ;
397 for (i = 0 ; i < len ; i++) d2[i] = d1[i];
398}
399
400
401/* Function to compute the digest */ 391/* Function to compute the digest */
402void 392void
403hmac_sha 393hmac_sha(unsigned char* k, /* secret key */
404( 394 int lk, /* length of the key in bytes */
405 char* k, /* secret key */ 395 unsigned char* d, /* data */
406 int lk, /* length of the key in bytes */ 396 int ld, /* length of data in bytes */
407 char* d, /* data */ 397 unsigned char* out, /* output buffer, at least "t" bytes */
408 int ld, /* length of data in bytes */ 398 int t)
409 char* out, /* output buffer, at least "t" bytes */
410 int t
411)
412{ 399{
413 SHA1Context ictx, octx ; 400 SHA1Context ictx, octx ;
414 char isha[SHA_DIGESTSIZE], osha[SHA_DIGESTSIZE] ; 401 unsigned char isha[SHA_DIGESTSIZE], osha[SHA_DIGESTSIZE] ;
415 char key[SHA_DIGESTSIZE] ; 402 unsigned char key[SHA_DIGESTSIZE] ;
416 char buf[SHA_BLOCKSIZE] ; 403 unsigned char buf[SHA_BLOCKSIZE] ;
417 int i ; 404 int i ;
418 405
419 if (lk > SHA_BLOCKSIZE) { 406 if (lk > SHA_BLOCKSIZE) {
420 407
@@ -433,8 +420,11 @@ hmac_sha
433 SHA1Reset(&ictx) ; 420 SHA1Reset(&ictx) ;
434 421
435 /* Pad the key for inner digest */ 422 /* Pad the key for inner digest */
436 for (i = 0 ; i < lk ; ++i) buf[i] = k[i] ^ 0x36 ; 423 for (i = 0 ; i < lk ; ++i)
437 for (i = lk ; i < SHA_BLOCKSIZE ; ++i) buf[i] = 0x36 ; 424 buf[i] = k[i] ^ 0x36 ;
425
426 for (i = lk ; i < SHA_BLOCKSIZE ; ++i)
427 buf[i] = 0x36 ;
438 428
439 SHA1Input(&ictx, buf, SHA_BLOCKSIZE) ; 429 SHA1Input(&ictx, buf, SHA_BLOCKSIZE) ;
440 SHA1Input(&ictx, d, ld) ; 430 SHA1Input(&ictx, d, ld) ;
@@ -456,8 +446,8 @@ hmac_sha
456 446
457 SHA1Result(&octx, osha) ; 447 SHA1Result(&octx, osha) ;
458 448
459 /* truncate and print the results */ 449 /* truncate the results */
460 t = t > SHA_DIGESTSIZE ? SHA_DIGESTSIZE : t ; 450 t = t > SHA_DIGESTSIZE ? SHA_DIGESTSIZE : t ;
461 truncate(osha, out, t) ; 451 memcpy(out, osha, t);
462 452
463} 453}