tinydtls  0.8.6
hmac.c
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (c) 2011, 2012, 2013, 2014, 2015 Olaf Bergmann (TZI) and others.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
7  *
8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
9  * and the Eclipse Distribution License is available at
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  *
12  * Contributors:
13  * Olaf Bergmann - initial API and implementation
14  * Hauke Mehrtens - memory optimization, ECC integration
15  *
16  *******************************************************************************/
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #ifdef HAVE_ASSERT_H
23 #include <assert.h>
24 #else
25 #define assert(x)
26 #endif
27 
28 #include "dtls_debug.h"
29 #include "hmac.h"
30 
31 /* use malloc()/free() on platforms other than Contiki */
32 #ifndef WITH_CONTIKI
33 #include <stdlib.h>
34 
35 static inline dtls_hmac_context_t *
37  return (dtls_hmac_context_t *)malloc(sizeof(dtls_hmac_context_t));
38 }
39 
40 static inline void
42  free(ctx);
43 }
44 
45 void
47 }
48 
49 #else /* WITH_CONTIKI */
50 #include "memb.h"
51 MEMB(hmac_context_storage, dtls_hmac_context_t, DTLS_HASH_MAX);
52 
53 static inline dtls_hmac_context_t *
55  return (dtls_hmac_context_t *)memb_alloc(&hmac_context_storage);
56 }
57 
58 static inline void
60  memb_free(&hmac_context_storage, ctx);
61 }
62 
63 void
65  memb_init(&hmac_context_storage);
66 }
67 #endif /* WITH_CONTIKI */
68 
69 void
71  const unsigned char *input, size_t ilen) {
72  assert(ctx);
73  dtls_hash_update(&ctx->data, input, ilen);
74 }
75 
77 dtls_hmac_new(const unsigned char *key, size_t klen) {
79 
80  ctx = dtls_hmac_context_new();
81  if (ctx)
82  dtls_hmac_init(ctx, key, klen);
83 
84  return ctx;
85 }
86 
87 void
88 dtls_hmac_init(dtls_hmac_context_t *ctx, const unsigned char *key, size_t klen) {
89  int i;
90 
91  assert(ctx);
92 
93  memset(ctx, 0, sizeof(dtls_hmac_context_t));
94 
95  if (klen > DTLS_HMAC_BLOCKSIZE) {
96  dtls_hash_init(&ctx->data);
97  dtls_hash_update(&ctx->data, key, klen);
98  dtls_hash_finalize(ctx->pad, &ctx->data);
99  } else
100  memcpy(ctx->pad, key, klen);
101 
102  /* create ipad: */
103  for (i=0; i < DTLS_HMAC_BLOCKSIZE; ++i)
104  ctx->pad[i] ^= 0x36;
105 
106  dtls_hash_init(&ctx->data);
107  dtls_hmac_update(ctx, ctx->pad, DTLS_HMAC_BLOCKSIZE);
108 
109  /* create opad by xor-ing pad[i] with 0x36 ^ 0x5C: */
110  for (i=0; i < DTLS_HMAC_BLOCKSIZE; ++i)
111  ctx->pad[i] ^= 0x6A;
112 }
113 
114 void
116  if (ctx)
118 }
119 
120 int
121 dtls_hmac_finalize(dtls_hmac_context_t *ctx, unsigned char *result) {
122  unsigned char buf[DTLS_HMAC_DIGEST_SIZE];
123  size_t len;
124 
125  assert(ctx);
126  assert(result);
127 
128  len = dtls_hash_finalize(buf, &ctx->data);
129 
130  dtls_hash_init(&ctx->data);
132  dtls_hash_update(&ctx->data, buf, len);
133 
134  len = dtls_hash_finalize(result, &ctx->data);
135 
136  return len;
137 }
138 
139 #ifdef HMAC_TEST
140 #include <stdio.h>
141 
142 int main(int argc, char **argv) {
143  static unsigned char buf[DTLS_HMAC_DIGEST_SIZE];
144  size_t len, i;
145  dtls_hmac_context_t *ctx;
146 
147  if (argc < 3) {
148  fprintf(stderr, "usage: %s key text", argv[0]);
149  return -1;
150  }
151 
153  ctx = dtls_hmac_new(argv[1], strlen(argv[1]));
154  assert(ctx);
155  dtls_hmac_update(ctx, argv[2], strlen(argv[2]));
156 
157  len = dtls_hmac_finalize(ctx, buf);
158 
159  for(i = 0; i < len; i++)
160  printf("%02x", buf[i]);
161  printf("\n");
162 
163  dtls_hmac_free(ctx);
164 
165  return 0;
166 }
167 #endif
void dtls_hmac_update(dtls_hmac_context_t *ctx, const unsigned char *input, size_t ilen)
Definition: hmac.c:70
void dtls_hmac_free(dtls_hmac_context_t *ctx)
Definition: hmac.c:115
#define assert(x)
Definition: hmac.c:25
#define DTLS_HMAC_DIGEST_SIZE
Definition: hmac.h:62
void dtls_hmac_init(dtls_hmac_context_t *ctx, const unsigned char *key, size_t klen)
Definition: hmac.c:88
dtls_hmac_context_t * dtls_hmac_new(const unsigned char *key, size_t klen)
Definition: hmac.c:77
static void dtls_hash_init(dtls_hash_t ctx)
Definition: hmac.h:36
unsigned char pad[DTLS_HMAC_BLOCKSIZE]
Definition: hmac.h:84
static void dtls_hash_update(dtls_hash_t ctx, const unsigned char *input, size_t len)
Definition: hmac.h:41
int dtls_hmac_finalize(dtls_hmac_context_t *ctx, unsigned char *result)
Definition: hmac.c:121
static dtls_hmac_context_t * dtls_hmac_context_new(void)
Definition: hmac.c:36
static void dtls_hmac_context_free(dtls_hmac_context_t *ctx)
Definition: hmac.c:41
void dtls_hmac_storage_init(void)
Definition: hmac.c:46
#define DTLS_HMAC_BLOCKSIZE
Definition: hmac.h:61
static size_t dtls_hash_finalize(unsigned char *buf, dtls_hash_t ctx)
Definition: hmac.h:46
dtls_hash_ctx data
Definition: hmac.h:85