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