1 /* dtls -- a very basic DTLS implementation
3 * Copyright (C) 2011--2012 Olaf Bergmann <bergmann@tzi.org>
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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
30 #include "dtls_config.h"
41 /* use malloc()/free() on platforms other than Contiki */
45 INLINE_API dtls_hmac_context_t *
46 dtls_hmac_context_new() {
47 return (dtls_hmac_context_t *)malloc(sizeof(dtls_hmac_context_t));
51 dtls_hmac_context_free(dtls_hmac_context_t *ctx) {
55 #else /* WITH_CONTIKI */
57 MEMB(hmac_context_storage, dtls_hmac_context_t, DTLS_HASH_MAX);
59 INLINE_API dtls_hmac_context_t *
60 dtls_hmac_context_new() {
61 return (dtls_hmac_context_t *)memb_alloc(&hmac_context_storage);
65 dtls_hmac_context_free(dtls_hmac_context_t *ctx) {
66 memb_free(&hmac_context_storage, ctx);
70 dtls_hmac_storage_init() {
71 memb_init(&hmac_context_storage);
73 #endif /* WITH_CONTIKI */
76 dtls_hmac_update(dtls_hmac_context_t *ctx,
77 const unsigned char *input, size_t ilen) {
79 dtls_hash_update(&ctx->data, input, ilen);
83 dtls_hmac_new(const unsigned char *key, size_t klen) {
84 dtls_hmac_context_t *ctx;
86 ctx = dtls_hmac_context_new();
88 dtls_hmac_init(ctx, key, klen);
94 dtls_hmac_init(dtls_hmac_context_t *ctx, const unsigned char *key, size_t klen) {
99 memset(ctx, 0, sizeof(dtls_hmac_context_t));
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);
106 memcpy(ctx->pad, key, klen);
109 for (i=0; i < DTLS_HMAC_BLOCKSIZE; ++i)
112 dtls_hash_init(&ctx->data);
113 dtls_hmac_update(ctx, ctx->pad, DTLS_HMAC_BLOCKSIZE);
115 /* create opad by xor-ing pad[i] with 0x36 ^ 0x5C: */
116 for (i=0; i < DTLS_HMAC_BLOCKSIZE; ++i)
121 dtls_hmac_free(dtls_hmac_context_t *ctx) {
123 dtls_hmac_context_free(ctx);
127 dtls_hmac_finalize(dtls_hmac_context_t *ctx, unsigned char *result) {
128 unsigned char buf[DTLS_HMAC_DIGEST_SIZE];
134 len = dtls_hash_finalize(buf, &ctx->data);
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);
140 len = dtls_hash_finalize(result, &ctx->data);
148 int main(int argc, char **argv) {
149 static unsigned char buf[DTLS_HMAC_DIGEST_SIZE];
151 dtls_hmac_context_t *ctx;
154 fprintf(stderr, "usage: %s key text", argv[0]);
158 dtls_hmac_storage_init();
159 ctx = dtls_hmac_new(argv[1], strlen(argv[1]));
161 dtls_hmac_update(ctx, argv[2], strlen(argv[2]));
163 len = dtls_hmac_finalize(ctx, buf);
165 for(i = 0; i < len; i++)
166 printf("%02x", buf[i]);