mbed port of tinydtls

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?

UserRevisionLine numberNew 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 #ifndef _HMAC_H_
ashleymills 0:04990d454f45 27 #define _HMAC_H_
ashleymills 0:04990d454f45 28
ashleymills 0:04990d454f45 29 #ifdef HAVE_SYS_TYPES_H
ashleymills 0:04990d454f45 30 #include <sys/types.h>
ashleymills 0:04990d454f45 31 #endif
ashleymills 0:04990d454f45 32
ashleymills 0:04990d454f45 33 #include "global.h"
ashleymills 0:04990d454f45 34
ashleymills 0:04990d454f45 35 #ifdef WITH_SHA256
ashleymills 0:04990d454f45 36 /** Aaron D. Gifford's implementation of SHA256
ashleymills 0:04990d454f45 37 * see http://www.aarongifford.com/ */
ashleymills 0:04990d454f45 38 #include "sha2/sha2.h"
ashleymills 0:04990d454f45 39
ashleymills 0:04990d454f45 40 typedef SHA256_CTX dtls_hash_ctx;
ashleymills 0:04990d454f45 41 typedef dtls_hash_ctx *dtls_hash_t;
ashleymills 0:04990d454f45 42 #define DTLS_HASH_CTX_SIZE sizeof(SHA256_CTX)
ashleymills 0:04990d454f45 43
ashleymills 0:04990d454f45 44 static inline void
ashleymills 0:04990d454f45 45 dtls_hash_init(dtls_hash_t ctx) {
ashleymills 0:04990d454f45 46 SHA256_Init((SHA256_CTX *)ctx);
ashleymills 0:04990d454f45 47 }
ashleymills 0:04990d454f45 48
ashleymills 0:04990d454f45 49 static inline void
ashleymills 0:04990d454f45 50 dtls_hash_update(dtls_hash_t ctx, const unsigned char *input, size_t len) {
ashleymills 0:04990d454f45 51 SHA256_Update((SHA256_CTX *)ctx, input, len);
ashleymills 0:04990d454f45 52 }
ashleymills 0:04990d454f45 53
ashleymills 0:04990d454f45 54 static inline size_t
ashleymills 0:04990d454f45 55 dtls_hash_finalize(unsigned char *buf, dtls_hash_t ctx) {
ashleymills 0:04990d454f45 56 SHA256_Final(buf, (SHA256_CTX *)ctx);
ashleymills 0:04990d454f45 57 return SHA256_DIGEST_LENGTH;
ashleymills 0:04990d454f45 58 }
ashleymills 0:04990d454f45 59 #endif /* WITH_SHA256 */
ashleymills 0:04990d454f45 60
ashleymills 0:04990d454f45 61 /**
ashleymills 0:04990d454f45 62 * \defgroup HMAC Keyed-Hash Message Authentication Code (HMAC)
ashleymills 0:04990d454f45 63 * NIST Standard FIPS 198 describes the Keyed-Hash Message Authentication
ashleymills 0:04990d454f45 64 * Code (HMAC) which is used as hash function for the DTLS PRF.
ashleymills 0:04990d454f45 65 * @{
ashleymills 0:04990d454f45 66 */
ashleymills 0:04990d454f45 67
ashleymills 0:04990d454f45 68 #define DTLS_HMAC_BLOCKSIZE 64 /**< size of hmac blocks */
ashleymills 0:04990d454f45 69 #define DTLS_HMAC_DIGEST_SIZE 32 /**< digest size (for SHA-256) */
ashleymills 0:04990d454f45 70 #define DTLS_HMAC_MAX 64 /**< max number of bytes in digest */
ashleymills 0:04990d454f45 71
ashleymills 0:04990d454f45 72 /**
ashleymills 0:04990d454f45 73 * List of known hash functions for use in dtls_hmac_init(). The
ashleymills 0:04990d454f45 74 * identifiers are the same as the HashAlgorithm defined in
ashleymills 0:04990d454f45 75 * <a href="http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1"
ashleymills 0:04990d454f45 76 * >Section 7.4.1.4.1 of RFC 5246</a>.
ashleymills 0:04990d454f45 77 */
ashleymills 0:04990d454f45 78 typedef enum {
ashleymills 0:04990d454f45 79 HASH_NONE=0, HASH_MD5=1, HASH_SHA1=2, HASH_SHA224=3,
ashleymills 0:04990d454f45 80 HASH_SHA256=4, HASH_SHA384=5, HASH_SHA512=6
ashleymills 0:04990d454f45 81 } dtls_hashfunc_t;
ashleymills 0:04990d454f45 82
ashleymills 0:04990d454f45 83 /**
ashleymills 0:04990d454f45 84 * Context for HMAC generation. This object is initialized with
ashleymills 0:04990d454f45 85 * dtls_hmac_init() and must be passed to dtls_hmac_update() and
ashleymills 0:04990d454f45 86 * dtls_hmac_finalize(). Once, finalized, the component \c H is
ashleymills 0:04990d454f45 87 * invalid and must be initialized again with dtls_hmac_init() before
ashleymills 0:04990d454f45 88 * the structure can be used again.
ashleymills 0:04990d454f45 89 */
ashleymills 0:04990d454f45 90 typedef struct {
ashleymills 0:04990d454f45 91 unsigned char pad[DTLS_HMAC_BLOCKSIZE]; /**< ipad and opad storage */
ashleymills 0:04990d454f45 92 dtls_hash_ctx data; /**< context for hash function */
ashleymills 0:04990d454f45 93 } dtls_hmac_context_t;
ashleymills 0:04990d454f45 94
ashleymills 0:04990d454f45 95 /**
ashleymills 0:04990d454f45 96 * Initializes an existing HMAC context.
ashleymills 0:04990d454f45 97 *
ashleymills 0:04990d454f45 98 * @param ctx The HMAC context to initialize.
ashleymills 0:04990d454f45 99 * @param key The secret key.
ashleymills 0:04990d454f45 100 * @param klen The length of @p key.
ashleymills 0:04990d454f45 101 */
ashleymills 0:04990d454f45 102 void dtls_hmac_init(dtls_hmac_context_t *ctx, const unsigned char *key, size_t klen);
ashleymills 0:04990d454f45 103
ashleymills 0:04990d454f45 104 /**
ashleymills 0:04990d454f45 105 * Allocates a new HMAC context \p ctx with the given secret key.
ashleymills 0:04990d454f45 106 * This function returns \c 1 if \c ctx has been set correctly, or \c
ashleymills 0:04990d454f45 107 * 0 or \c -1 otherwise. Note that this function allocates new storage
ashleymills 0:04990d454f45 108 * that must be released by dtls_hmac_free().
ashleymills 0:04990d454f45 109 *
ashleymills 0:04990d454f45 110 * \param key The secret key.
ashleymills 0:04990d454f45 111 * \param klen The length of \p key.
ashleymills 0:04990d454f45 112 * \return A new dtls_hmac_context_t object or @c NULL on error
ashleymills 0:04990d454f45 113 */
ashleymills 0:04990d454f45 114 dtls_hmac_context_t *dtls_hmac_new(const unsigned char *key, size_t klen);
ashleymills 0:04990d454f45 115
ashleymills 0:04990d454f45 116 /**
ashleymills 0:04990d454f45 117 * Releases the storage for @p ctx that has been allocated by
ashleymills 0:04990d454f45 118 * dtls_hmac_new().
ashleymills 0:04990d454f45 119 *
ashleymills 0:04990d454f45 120 * @param ctx The dtls_hmac_context_t to free.
ashleymills 0:04990d454f45 121 */
ashleymills 0:04990d454f45 122 void dtls_hmac_free(dtls_hmac_context_t *ctx);
ashleymills 0:04990d454f45 123
ashleymills 0:04990d454f45 124 /**
ashleymills 0:04990d454f45 125 * Updates the HMAC context with data from \p input.
ashleymills 0:04990d454f45 126 *
ashleymills 0:04990d454f45 127 * \param ctx The HMAC context.
ashleymills 0:04990d454f45 128 * \param input The input data.
ashleymills 0:04990d454f45 129 * \param ilen Size of \p input.
ashleymills 0:04990d454f45 130 */
ashleymills 0:04990d454f45 131 void dtls_hmac_update(dtls_hmac_context_t *ctx,
ashleymills 0:04990d454f45 132 const unsigned char *input, size_t ilen);
ashleymills 0:04990d454f45 133
ashleymills 0:04990d454f45 134 /**
ashleymills 0:04990d454f45 135 * Completes the HMAC generation and writes the result to the given
ashleymills 0:04990d454f45 136 * output parameter \c result. The buffer must be large enough to hold
ashleymills 0:04990d454f45 137 * the message digest created by the actual hash function. If in
ashleymills 0:04990d454f45 138 * doubt, use \c DTLS_HMAC_MAX. The function returns the number of
ashleymills 0:04990d454f45 139 * bytes written to \c result.
ashleymills 0:04990d454f45 140 *
ashleymills 0:04990d454f45 141 * \param ctx The HMAC context.
ashleymills 0:04990d454f45 142 * \param result Output parameter where the MAC is written to.
ashleymills 0:04990d454f45 143 * \return Length of the MAC written to \p result.
ashleymills 0:04990d454f45 144 */
ashleymills 0:04990d454f45 145 int dtls_hmac_finalize(dtls_hmac_context_t *ctx, unsigned char *result);
ashleymills 0:04990d454f45 146
ashleymills 0:04990d454f45 147 /**@}*/
ashleymills 0:04990d454f45 148
ashleymills 0:04990d454f45 149 #endif /* _HMAC_H_ */