File: hmac-md5.c

package info (click to toggle)
bglibs 2.04%2Bdfsg-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 3,368 kB
  • sloc: ansic: 15,820; perl: 674; sh: 64; makefile: 26
file content (92 lines) | stat: -rw-r--r-- 2,299 bytes parent folder | download | duplicates (4)
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
/* $Id: md5.c 646 2005-12-01 22:07:11Z bruce $ */

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>

#include "sysdeps.h"
#include "hmac.h"
#include "md5.h"

struct midstate
{
  uint32 A, B, C, D;
};

static void extract(const void* statep, void* midstatep)
{
  const struct md5_ctx* ctx = statep;
  struct midstate* mid = midstatep;
  mid->A = ctx->A;
  mid->B = ctx->B;
  mid->C = ctx->C;
  mid->D = ctx->D;
}

static void inject(void* statep, const void* midstatep)
{
  struct md5_ctx* ctx = statep;
  const struct midstate* mid = midstatep;
  ctx->A = mid->A;
  ctx->B = mid->B;
  ctx->C = mid->C;
  ctx->D = mid->D;
  ctx->total[0] = 64;
  ctx->total[1] = 0;
  ctx->buflen = 0;
}

static void update(void* ctx,
		   const unsigned char* bytes,
		   unsigned long len)
{
  md5_process_bytes(bytes, len, ctx);
}

const struct hmac_control_block hmac_md5 = {
  .state_size = sizeof(struct md5_ctx),
  .block_size = 64,
  .digest_size = 128/8,
  .midstate_size = sizeof(struct midstate),
  .init = (hmac_init_fn)md5_init_ctx,
  .update = update,
  .finalize = (hmac_finalize_fn)md5_finish_ctx,
  .extract = extract,
  .inject = inject,
};

#ifdef SELFTEST_MAIN
#include <stdio.h>
#include "str.h"

static void HMACTest(const char* key, const char* data)
{
  const str key_str = { (char*)key, strlen(key), 0 };
  const str data_str = { (char*)data, strlen(data), 0 };
  unsigned char digest[128/8];
  unsigned i;
  hmac(&hmac_md5, &key_str, &data_str, digest);
  printf("HMAC (%d,%d) = ", key_str.len, data_str.len);
  for (i = 0; i < sizeof digest; ++i) printf("%02x", digest[i]);
  printf("\n");
}

MAIN
{
  /* Test vectors from RFC 2104 */
  HMACTest("\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
	   "Hi There");
  HMACTest("Jefe", "what do ya want for nothing?");
  HMACTest("\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa\xaa",
	   "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
	   "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
	   "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
	   "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
	   "\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd");
}
#endif
#ifdef SELFTEST_EXP
HMAC (16,8) = 9294727a3638bb1c13f48ef8158bfc9d
HMAC (4,28) = 750c783e6ab0b503eaa86e310a5db738
HMAC (16,50) = 56be34521d144c88dbb8c733f0e8b3f6
#endif