tinydtls  0.8.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
hmac.c
Go to the documentation of this file.
1 /* dtls -- a very basic DTLS implementation
2  *
3  * Copyright (C) 2011--2012 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "dtls_config.h"
31 
32 #ifdef HAVE_ASSERT_H
33 #include <assert.h>
34 #else
35 #define assert(x)
36 #endif
37 
38 #include "debug.h"
39 #include "hmac.h"
40 
41 /* use malloc()/free() on platforms other than Contiki */
42 #ifndef WITH_CONTIKI
43 #include <stdlib.h>
44 
45 static inline dtls_hmac_context_t *
47  return (dtls_hmac_context_t *)malloc(sizeof(dtls_hmac_context_t));
48 }
49 
50 static inline void
52  free(ctx);
53 }
54 
55 #else /* WITH_CONTIKI */
56 #include "memb.h"
57 MEMB(hmac_context_storage, dtls_hmac_context_t, DTLS_HASH_MAX);
58 
59 static inline dtls_hmac_context_t *
61  return (dtls_hmac_context_t *)memb_alloc(&hmac_context_storage);
62 }
63 
64 static inline void
66  memb_free(&hmac_context_storage, ctx);
67 }
68 
69 void
71  memb_init(&hmac_context_storage);
72 }
73 #endif /* WITH_CONTIKI */
74 
75 void
77  const unsigned char *input, size_t ilen) {
78  assert(ctx);
79  dtls_hash_update(&ctx->data, input, ilen);
80 }
81 
83 dtls_hmac_new(const unsigned char *key, size_t klen) {
85 
86  ctx = dtls_hmac_context_new();
87  if (ctx)
88  dtls_hmac_init(ctx, key, klen);
89 
90  return ctx;
91 }
92 
93 void
94 dtls_hmac_init(dtls_hmac_context_t *ctx, const unsigned char *key, size_t klen) {
95  int i;
96 
97  assert(ctx);
98 
99  memset(ctx, 0, sizeof(dtls_hmac_context_t));
100 
101  if (klen > DTLS_HMAC_BLOCKSIZE) {
102  dtls_hash_init(&ctx->data);
103  dtls_hash_update(&ctx->data, key, klen);
104  dtls_hash_finalize(ctx->pad, &ctx->data);
105  } else
106  memcpy(ctx->pad, key, klen);
107 
108  /* create ipad: */
109  for (i=0; i < DTLS_HMAC_BLOCKSIZE; ++i)
110  ctx->pad[i] ^= 0x36;
111 
112  dtls_hash_init(&ctx->data);
113  dtls_hmac_update(ctx, ctx->pad, DTLS_HMAC_BLOCKSIZE);
114 
115  /* create opad by xor-ing pad[i] with 0x36 ^ 0x5C: */
116  for (i=0; i < DTLS_HMAC_BLOCKSIZE; ++i)
117  ctx->pad[i] ^= 0x6A;
118 }
119 
120 void
122  if (ctx)
124 }
125 
126 int
127 dtls_hmac_finalize(dtls_hmac_context_t *ctx, unsigned char *result) {
128  unsigned char buf[DTLS_HMAC_DIGEST_SIZE];
129  size_t len;
130 
131  assert(ctx);
132  assert(result);
133 
134  len = dtls_hash_finalize(buf, &ctx->data);
135 
136  dtls_hash_init(&ctx->data);
138  dtls_hash_update(&ctx->data, buf, len);
139 
140  len = dtls_hash_finalize(result, &ctx->data);
141 
142  return len;
143 }
144 
145 #ifdef HMAC_TEST
146 #include <stdio.h>
147 
148 int main(int argc, char **argv) {
149  static unsigned char buf[DTLS_HMAC_DIGEST_SIZE];
150  size_t len, i;
151  dtls_hmac_context_t *ctx;
152 
153  if (argc < 3) {
154  fprintf(stderr, "usage: %s key text", argv[0]);
155  return -1;
156  }
157 
159  ctx = dtls_hmac_new(argv[1], strlen(argv[1]));
160  assert(ctx);
161  dtls_hmac_update(ctx, argv[2], strlen(argv[2]));
162 
163  len = dtls_hmac_finalize(ctx, buf);
164 
165  for(i = 0; i < len; i++)
166  printf("%02x", buf[i]);
167  printf("\n");
168 
169  dtls_hmac_free(ctx);
170 
171  return 0;
172 }
173 #endif
static dtls_hmac_context_t * dtls_hmac_context_new()
Definition: hmac.c:46
void dtls_hmac_update(dtls_hmac_context_t *ctx, const unsigned char *input, size_t ilen)
Definition: hmac.c:76
void dtls_hmac_free(dtls_hmac_context_t *ctx)
Definition: hmac.c:121
#define DTLS_HMAC_DIGEST_SIZE
Definition: hmac.h:74
void dtls_hmac_init(dtls_hmac_context_t *ctx, const unsigned char *key, size_t klen)
Definition: hmac.c:94
dtls_hmac_context_t * dtls_hmac_new(const unsigned char *key, size_t klen)
Definition: hmac.c:83
static void dtls_hash_init(dtls_hash_t ctx)
Definition: hmac.h:43
unsigned char pad[DTLS_HMAC_BLOCKSIZE]
Definition: hmac.h:96
static void dtls_hash_update(dtls_hash_t ctx, const unsigned char *input, size_t len)
Definition: hmac.h:48
int dtls_hmac_finalize(dtls_hmac_context_t *ctx, unsigned char *result)
Definition: hmac.c:127
static void dtls_hmac_context_free(dtls_hmac_context_t *ctx)
Definition: hmac.c:51
#define DTLS_HMAC_BLOCKSIZE
Definition: hmac.h:73
static size_t dtls_hash_finalize(unsigned char *buf, dtls_hash_t ctx)
Definition: hmac.h:53
static void dtls_hmac_storage_init()
Definition: hmac.h:60
dtls_hash_ctx data
Definition: hmac.h:97