Version 0.5.0 of tinydtls

Dependents:   tinydtls_test_cellular tinydtls_test_ethernet tiny-dtls

Committer:
ashleymills
Date:
Fri Oct 18 13:18:30 2013 +0000
Revision:
0:ff9ebe0cf0e9
Upgraded to tinydtls 0.5.0

Who changed what in which revision?

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