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 /*
ashleymills 0:04990d454f45 2 * FILE: sha2.h
ashleymills 0:04990d454f45 3 * AUTHOR: Aaron D. Gifford - http://www.aarongifford.com/
ashleymills 0:04990d454f45 4 *
ashleymills 0:04990d454f45 5 * Copyright (c) 2000-2001, Aaron D. Gifford
ashleymills 0:04990d454f45 6 * All rights reserved.
ashleymills 0:04990d454f45 7 *
ashleymills 0:04990d454f45 8 * Redistribution and use in source and binary forms, with or without
ashleymills 0:04990d454f45 9 * modification, are permitted provided that the following conditions
ashleymills 0:04990d454f45 10 * are met:
ashleymills 0:04990d454f45 11 * 1. Redistributions of source code must retain the above copyright
ashleymills 0:04990d454f45 12 * notice, this list of conditions and the following disclaimer.
ashleymills 0:04990d454f45 13 * 2. Redistributions in binary form must reproduce the above copyright
ashleymills 0:04990d454f45 14 * notice, this list of conditions and the following disclaimer in the
ashleymills 0:04990d454f45 15 * documentation and/or other materials provided with the distribution.
ashleymills 0:04990d454f45 16 * 3. Neither the name of the copyright holder nor the names of contributors
ashleymills 0:04990d454f45 17 * may be used to endorse or promote products derived from this software
ashleymills 0:04990d454f45 18 * without specific prior written permission.
ashleymills 0:04990d454f45 19 *
ashleymills 0:04990d454f45 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
ashleymills 0:04990d454f45 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ashleymills 0:04990d454f45 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ashleymills 0:04990d454f45 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
ashleymills 0:04990d454f45 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ashleymills 0:04990d454f45 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
ashleymills 0:04990d454f45 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ashleymills 0:04990d454f45 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
ashleymills 0:04990d454f45 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
ashleymills 0:04990d454f45 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ashleymills 0:04990d454f45 30 * SUCH DAMAGE.
ashleymills 0:04990d454f45 31 *
ashleymills 0:04990d454f45 32 * $Id: sha2.h,v 1.1 2001/11/08 00:02:01 adg Exp adg $
ashleymills 0:04990d454f45 33 */
ashleymills 0:04990d454f45 34
ashleymills 0:04990d454f45 35 #ifndef __SHA2_H__
ashleymills 0:04990d454f45 36 #define __SHA2_H__
ashleymills 0:04990d454f45 37
ashleymills 0:04990d454f45 38 #ifdef __cplusplus
ashleymills 0:04990d454f45 39 extern "C" {
ashleymills 0:04990d454f45 40 #endif
ashleymills 0:04990d454f45 41
ashleymills 0:04990d454f45 42 #define WITH_SHA256 1
ashleymills 0:04990d454f45 43 /*
ashleymills 0:04990d454f45 44 * Import u_intXX_t size_t type definitions from system headers. You
ashleymills 0:04990d454f45 45 * may need to change this, or define these things yourself in this
ashleymills 0:04990d454f45 46 * file.
ashleymills 0:04990d454f45 47 */
ashleymills 0:04990d454f45 48 #ifdef HAVE_SYS_TYPES_H
ashleymills 0:04990d454f45 49 #include <sys/types.h>
ashleymills 0:04990d454f45 50 #endif
ashleymills 0:04990d454f45 51 #define SHA2_USE_INTTYPES_H
ashleymills 0:04990d454f45 52
ashleymills 0:04990d454f45 53 #ifdef SHA2_USE_INTTYPES_H
ashleymills 0:04990d454f45 54
ashleymills 0:04990d454f45 55 #include <inttypes.h>
ashleymills 0:04990d454f45 56
ashleymills 0:04990d454f45 57 #endif /* SHA2_USE_INTTYPES_H */
ashleymills 0:04990d454f45 58
ashleymills 0:04990d454f45 59
ashleymills 0:04990d454f45 60 /*** SHA-256/384/512 Various Length Definitions ***********************/
ashleymills 0:04990d454f45 61 #define SHA256_BLOCK_LENGTH 64
ashleymills 0:04990d454f45 62 #define SHA256_DIGEST_LENGTH 32
ashleymills 0:04990d454f45 63 #define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
ashleymills 0:04990d454f45 64 #define SHA384_BLOCK_LENGTH 128
ashleymills 0:04990d454f45 65 #define SHA384_DIGEST_LENGTH 48
ashleymills 0:04990d454f45 66 #define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
ashleymills 0:04990d454f45 67 #define SHA512_BLOCK_LENGTH 128
ashleymills 0:04990d454f45 68 #define SHA512_DIGEST_LENGTH 64
ashleymills 0:04990d454f45 69 #define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
ashleymills 0:04990d454f45 70
ashleymills 0:04990d454f45 71
ashleymills 0:04990d454f45 72 /*** SHA-256/384/512 Context Structures *******************************/
ashleymills 0:04990d454f45 73 /* NOTE: If your architecture does not define either u_intXX_t types or
ashleymills 0:04990d454f45 74 * uintXX_t (from inttypes.h), you may need to define things by hand
ashleymills 0:04990d454f45 75 * for your system:
ashleymills 0:04990d454f45 76 */
ashleymills 0:04990d454f45 77 #if 0
ashleymills 0:04990d454f45 78 typedef unsigned char u_int8_t; /* 1-byte (8-bits) */
ashleymills 0:04990d454f45 79 typedef unsigned int u_int32_t; /* 4-bytes (32-bits) */
ashleymills 0:04990d454f45 80 typedef unsigned long long u_int64_t; /* 8-bytes (64-bits) */
ashleymills 0:04990d454f45 81 #endif
ashleymills 0:04990d454f45 82 /*
ashleymills 0:04990d454f45 83 * Most BSD systems already define u_intXX_t types, as does Linux.
ashleymills 0:04990d454f45 84 * Some systems, however, like Compaq's Tru64 Unix instead can use
ashleymills 0:04990d454f45 85 * uintXX_t types defined by very recent ANSI C standards and included
ashleymills 0:04990d454f45 86 * in the file:
ashleymills 0:04990d454f45 87 *
ashleymills 0:04990d454f45 88 * #include <inttypes.h>
ashleymills 0:04990d454f45 89 *
ashleymills 0:04990d454f45 90 * If you choose to use <inttypes.h> then please define:
ashleymills 0:04990d454f45 91 *
ashleymills 0:04990d454f45 92 * #define SHA2_USE_INTTYPES_H
ashleymills 0:04990d454f45 93 *
ashleymills 0:04990d454f45 94 * Or on the command line during compile:
ashleymills 0:04990d454f45 95 *
ashleymills 0:04990d454f45 96 * cc -DSHA2_USE_INTTYPES_H ...
ashleymills 0:04990d454f45 97 */
ashleymills 0:04990d454f45 98 #ifdef SHA2_USE_INTTYPES_H
ashleymills 0:04990d454f45 99
ashleymills 0:04990d454f45 100 typedef struct _SHA256_CTX {
ashleymills 0:04990d454f45 101 uint32_t state[8];
ashleymills 0:04990d454f45 102 uint64_t bitcount;
ashleymills 0:04990d454f45 103 uint8_t buffer[SHA256_BLOCK_LENGTH];
ashleymills 0:04990d454f45 104 } SHA256_CTX;
ashleymills 0:04990d454f45 105 typedef struct _SHA512_CTX {
ashleymills 0:04990d454f45 106 uint64_t state[8];
ashleymills 0:04990d454f45 107 uint64_t bitcount[2];
ashleymills 0:04990d454f45 108 uint8_t buffer[SHA512_BLOCK_LENGTH];
ashleymills 0:04990d454f45 109 } SHA512_CTX;
ashleymills 0:04990d454f45 110
ashleymills 0:04990d454f45 111 #else /* SHA2_USE_INTTYPES_H */
ashleymills 0:04990d454f45 112
ashleymills 0:04990d454f45 113 typedef struct _SHA256_CTX {
ashleymills 0:04990d454f45 114 u_int32_t state[8];
ashleymills 0:04990d454f45 115 u_int64_t bitcount;
ashleymills 0:04990d454f45 116 u_int8_t buffer[SHA256_BLOCK_LENGTH];
ashleymills 0:04990d454f45 117 } SHA256_CTX;
ashleymills 0:04990d454f45 118 typedef struct _SHA512_CTX {
ashleymills 0:04990d454f45 119 u_int64_t state[8];
ashleymills 0:04990d454f45 120 u_int64_t bitcount[2];
ashleymills 0:04990d454f45 121 u_int8_t buffer[SHA512_BLOCK_LENGTH];
ashleymills 0:04990d454f45 122 } SHA512_CTX;
ashleymills 0:04990d454f45 123
ashleymills 0:04990d454f45 124 #endif /* SHA2_USE_INTTYPES_H */
ashleymills 0:04990d454f45 125
ashleymills 0:04990d454f45 126 typedef SHA512_CTX SHA384_CTX;
ashleymills 0:04990d454f45 127
ashleymills 0:04990d454f45 128
ashleymills 0:04990d454f45 129 /*** SHA-256/384/512 Function Prototypes ******************************/
ashleymills 0:04990d454f45 130 #ifndef NOPROTO
ashleymills 0:04990d454f45 131 #ifdef SHA2_USE_INTTYPES_H
ashleymills 0:04990d454f45 132
ashleymills 0:04990d454f45 133 #ifdef WITH_SHA256
ashleymills 0:04990d454f45 134 void SHA256_Init(SHA256_CTX *);
ashleymills 0:04990d454f45 135 void SHA256_Update(SHA256_CTX*, const uint8_t*, size_t);
ashleymills 0:04990d454f45 136 void SHA256_Final(uint8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
ashleymills 0:04990d454f45 137 char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
ashleymills 0:04990d454f45 138 char* SHA256_Data(const uint8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
ashleymills 0:04990d454f45 139 #endif
ashleymills 0:04990d454f45 140
ashleymills 0:04990d454f45 141 #ifdef WITH_SHA384
ashleymills 0:04990d454f45 142 void SHA384_Init(SHA384_CTX*);
ashleymills 0:04990d454f45 143 void SHA384_Update(SHA384_CTX*, const uint8_t*, size_t);
ashleymills 0:04990d454f45 144 void SHA384_Final(uint8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
ashleymills 0:04990d454f45 145 char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
ashleymills 0:04990d454f45 146 char* SHA384_Data(const uint8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
ashleymills 0:04990d454f45 147 #endif
ashleymills 0:04990d454f45 148
ashleymills 0:04990d454f45 149 #ifdef WITH_SHA512
ashleymills 0:04990d454f45 150 void SHA512_Init(SHA512_CTX*);
ashleymills 0:04990d454f45 151 void SHA512_Update(SHA512_CTX*, const uint8_t*, size_t);
ashleymills 0:04990d454f45 152 void SHA512_Final(uint8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
ashleymills 0:04990d454f45 153 char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
ashleymills 0:04990d454f45 154 char* SHA512_Data(const uint8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
ashleymills 0:04990d454f45 155 #endif
ashleymills 0:04990d454f45 156
ashleymills 0:04990d454f45 157 #else /* SHA2_USE_INTTYPES_H */
ashleymills 0:04990d454f45 158
ashleymills 0:04990d454f45 159 #ifdef WITH_SHA256
ashleymills 0:04990d454f45 160 void SHA256_Init(SHA256_CTX *);
ashleymills 0:04990d454f45 161 void SHA256_Update(SHA256_CTX*, const u_int8_t*, size_t);
ashleymills 0:04990d454f45 162 void SHA256_Final(u_int8_t[SHA256_DIGEST_LENGTH], SHA256_CTX*);
ashleymills 0:04990d454f45 163 char* SHA256_End(SHA256_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
ashleymills 0:04990d454f45 164 char* SHA256_Data(const u_int8_t*, size_t, char[SHA256_DIGEST_STRING_LENGTH]);
ashleymills 0:04990d454f45 165 #endif
ashleymills 0:04990d454f45 166
ashleymills 0:04990d454f45 167 #ifdef WITH_SHA384
ashleymills 0:04990d454f45 168 void SHA384_Init(SHA384_CTX*);
ashleymills 0:04990d454f45 169 void SHA384_Update(SHA384_CTX*, const u_int8_t*, size_t);
ashleymills 0:04990d454f45 170 void SHA384_Final(u_int8_t[SHA384_DIGEST_LENGTH], SHA384_CTX*);
ashleymills 0:04990d454f45 171 char* SHA384_End(SHA384_CTX*, char[SHA384_DIGEST_STRING_LENGTH]);
ashleymills 0:04990d454f45 172 char* SHA384_Data(const u_int8_t*, size_t, char[SHA384_DIGEST_STRING_LENGTH]);
ashleymills 0:04990d454f45 173 #endif
ashleymills 0:04990d454f45 174
ashleymills 0:04990d454f45 175 #ifdef WITH_SHA512
ashleymills 0:04990d454f45 176 void SHA512_Init(SHA512_CTX*);
ashleymills 0:04990d454f45 177 void SHA512_Update(SHA512_CTX*, const u_int8_t*, size_t);
ashleymills 0:04990d454f45 178 void SHA512_Final(u_int8_t[SHA512_DIGEST_LENGTH], SHA512_CTX*);
ashleymills 0:04990d454f45 179 char* SHA512_End(SHA512_CTX*, char[SHA512_DIGEST_STRING_LENGTH]);
ashleymills 0:04990d454f45 180 char* SHA512_Data(const u_int8_t*, size_t, char[SHA512_DIGEST_STRING_LENGTH]);
ashleymills 0:04990d454f45 181 #endif
ashleymills 0:04990d454f45 182
ashleymills 0:04990d454f45 183 #endif /* SHA2_USE_INTTYPES_H */
ashleymills 0:04990d454f45 184
ashleymills 0:04990d454f45 185 #else /* NOPROTO */
ashleymills 0:04990d454f45 186
ashleymills 0:04990d454f45 187 #ifdef WITH_SHA256
ashleymills 0:04990d454f45 188 void SHA256_Init();
ashleymills 0:04990d454f45 189 void SHA256_Update();
ashleymills 0:04990d454f45 190 void SHA256_Final();
ashleymills 0:04990d454f45 191 char* SHA256_End();
ashleymills 0:04990d454f45 192 char* SHA256_Data();
ashleymills 0:04990d454f45 193 #endif
ashleymills 0:04990d454f45 194
ashleymills 0:04990d454f45 195 #ifdef WITH_SHA384
ashleymills 0:04990d454f45 196 void SHA384_Init();
ashleymills 0:04990d454f45 197 void SHA384_Update();
ashleymills 0:04990d454f45 198 void SHA384_Final();
ashleymills 0:04990d454f45 199 char* SHA384_End();
ashleymills 0:04990d454f45 200 char* SHA384_Data();
ashleymills 0:04990d454f45 201 #endif
ashleymills 0:04990d454f45 202
ashleymills 0:04990d454f45 203 #ifdef WITH_SHA512
ashleymills 0:04990d454f45 204 void SHA512_Init();
ashleymills 0:04990d454f45 205 void SHA512_Update();
ashleymills 0:04990d454f45 206 void SHA512_Final();
ashleymills 0:04990d454f45 207 char* SHA512_End();
ashleymills 0:04990d454f45 208 char* SHA512_Data();
ashleymills 0:04990d454f45 209 #endif
ashleymills 0:04990d454f45 210
ashleymills 0:04990d454f45 211 #endif /* NOPROTO */
ashleymills 0:04990d454f45 212
ashleymills 0:04990d454f45 213 #ifdef __cplusplus
ashleymills 0:04990d454f45 214 }
ashleymills 0:04990d454f45 215 #endif /* __cplusplus */
ashleymills 0:04990d454f45 216
ashleymills 0:04990d454f45 217 #endif /* __SHA2_H__ */
ashleymills 0:04990d454f45 218