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 *
dtls_hmac_context_new()46 dtls_hmac_context_new() {
47   return (dtls_hmac_context_t *)malloc(sizeof(dtls_hmac_context_t));
48 }
49 
50 static inline void
dtls_hmac_context_free(dtls_hmac_context_t * ctx)51 dtls_hmac_context_free(dtls_hmac_context_t *ctx) {
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 *
dtls_hmac_context_new()60 dtls_hmac_context_new() {
61   return (dtls_hmac_context_t *)memb_alloc(&hmac_context_storage);
62 }
63 
64 static inline void
dtls_hmac_context_free(dtls_hmac_context_t * ctx)65 dtls_hmac_context_free(dtls_hmac_context_t *ctx) {
66   memb_free(&hmac_context_storage, ctx);
67 }
68 
69 void
dtls_hmac_storage_init()70 dtls_hmac_storage_init() {
71   memb_init(&hmac_context_storage);
72 }
73 #endif /* WITH_CONTIKI */
74 
75 void
dtls_hmac_update(dtls_hmac_context_t * ctx,const unsigned char * input,size_t ilen)76 dtls_hmac_update(dtls_hmac_context_t *ctx,
77 		 const unsigned char *input, size_t ilen) {
78   assert(ctx);
79   dtls_hash_update(&ctx->data, input, ilen);
80 }
81 
82 dtls_hmac_context_t *
dtls_hmac_new(const unsigned char * key,size_t klen)83 dtls_hmac_new(const unsigned char *key, size_t klen) {
84   dtls_hmac_context_t *ctx;
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
dtls_hmac_init(dtls_hmac_context_t * ctx,const unsigned char * key,size_t klen)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
dtls_hmac_free(dtls_hmac_context_t * ctx)121 dtls_hmac_free(dtls_hmac_context_t *ctx) {
122   if (ctx)
123     dtls_hmac_context_free(ctx);
124 }
125 
126 int
dtls_hmac_finalize(dtls_hmac_context_t * ctx,unsigned char * result)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);
137   dtls_hash_update(&ctx->data, ctx->pad, DTLS_HMAC_BLOCKSIZE);
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 
main(int argc,char ** argv)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 
158   dtls_hmac_storage_init();
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
174