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