A Port of TI's Webserver for the CC3000

Dependencies:   mbed

Committer:
dflet
Date:
Mon Sep 16 18:37:14 2013 +0000
Revision:
2:e6a185df9e4c
Parent:
0:6ad60d78b315
ADC and Leds now work on board and config.html page.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dflet 0:6ad60d78b315 1 /*
dflet 0:6ad60d78b315 2 * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
dflet 0:6ad60d78b315 3 * MD5 Message-Digest Algorithm (RFC 1321).
dflet 0:6ad60d78b315 4 *
dflet 0:6ad60d78b315 5 * Homepage:
dflet 0:6ad60d78b315 6 * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
dflet 0:6ad60d78b315 7 *
dflet 0:6ad60d78b315 8 * Author:
dflet 0:6ad60d78b315 9 * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
dflet 0:6ad60d78b315 10 *
dflet 0:6ad60d78b315 11 * This software was written by Alexander Peslyak in 2001. No copyright is
dflet 0:6ad60d78b315 12 * claimed, and the software is hereby placed in the public domain.
dflet 0:6ad60d78b315 13 * In case this attempt to disclaim copyright and place the software in the
dflet 0:6ad60d78b315 14 * public domain is deemed null and void, then the software is
dflet 0:6ad60d78b315 15 * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the
dflet 0:6ad60d78b315 16 * general public under the following terms:
dflet 0:6ad60d78b315 17 *
dflet 0:6ad60d78b315 18 * Redistribution and use in source and binary forms, with or without
dflet 0:6ad60d78b315 19 * modification, are permitted.
dflet 0:6ad60d78b315 20 *
dflet 0:6ad60d78b315 21 * There's ABSOLUTELY NO WARRANTY, express or implied.
dflet 0:6ad60d78b315 22 *
dflet 0:6ad60d78b315 23 * (This is a heavily cut-down "BSD license".)
dflet 0:6ad60d78b315 24 *
dflet 0:6ad60d78b315 25 * This differs from Colin Plumb's older public domain implementation in that
dflet 0:6ad60d78b315 26 * no exactly 32-bit integer data type is required (any 32-bit or wider
dflet 0:6ad60d78b315 27 * unsigned integer data type will do), there's no compile-time endianness
dflet 0:6ad60d78b315 28 * configuration, and the function prototypes match OpenSSL's. No code from
dflet 0:6ad60d78b315 29 * Colin Plumb's implementation has been reused; this comment merely compares
dflet 0:6ad60d78b315 30 * the properties of the two independent implementations.
dflet 0:6ad60d78b315 31 *
dflet 0:6ad60d78b315 32 * The primary goals of this implementation are portability and ease of use.
dflet 0:6ad60d78b315 33 * It is meant to be fast, but not as fast as possible. Some known
dflet 0:6ad60d78b315 34 * optimizations are not included to reduce source code size and avoid
dflet 0:6ad60d78b315 35 * compile-time configuration.
dflet 0:6ad60d78b315 36 */
dflet 0:6ad60d78b315 37
dflet 0:6ad60d78b315 38 #ifndef HAVE_OPENSSL
dflet 0:6ad60d78b315 39
dflet 0:6ad60d78b315 40 #include "mbed.h"
dflet 0:6ad60d78b315 41 #include <string.h>
dflet 0:6ad60d78b315 42
dflet 0:6ad60d78b315 43 #include "md5.h"
dflet 0:6ad60d78b315 44
dflet 0:6ad60d78b315 45 /*
dflet 0:6ad60d78b315 46 * The basic MD5 functions.
dflet 0:6ad60d78b315 47 *
dflet 0:6ad60d78b315 48 * F and G are optimized compared to their RFC 1321 definitions for
dflet 0:6ad60d78b315 49 * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
dflet 0:6ad60d78b315 50 * implementation.
dflet 0:6ad60d78b315 51 */
dflet 0:6ad60d78b315 52 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
dflet 0:6ad60d78b315 53 #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
dflet 0:6ad60d78b315 54 #define H(x, y, z) ((x) ^ (y) ^ (z))
dflet 0:6ad60d78b315 55 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
dflet 0:6ad60d78b315 56
dflet 0:6ad60d78b315 57 /*
dflet 0:6ad60d78b315 58 * The MD5 transformation for all four rounds.
dflet 0:6ad60d78b315 59 */
dflet 0:6ad60d78b315 60 #define STEP(f, a, b, c, d, x, t, s) \
dflet 0:6ad60d78b315 61 (a) += f((b), (c), (d)) + (x) + (t); \
dflet 0:6ad60d78b315 62 (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
dflet 0:6ad60d78b315 63 (a) += (b);
dflet 0:6ad60d78b315 64
dflet 0:6ad60d78b315 65 /*
dflet 0:6ad60d78b315 66 * SET reads 4 input bytes in little-endian byte order and stores them
dflet 0:6ad60d78b315 67 * in a properly aligned word in host byte order.
dflet 0:6ad60d78b315 68 *
dflet 0:6ad60d78b315 69 * The check for little-endian architectures that tolerate unaligned
dflet 0:6ad60d78b315 70 * memory accesses is just an optimization. Nothing will break if it
dflet 0:6ad60d78b315 71 * doesn't work.
dflet 0:6ad60d78b315 72 */
dflet 0:6ad60d78b315 73 #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
dflet 0:6ad60d78b315 74 #define SET(n) \
dflet 0:6ad60d78b315 75 (*(MD5_u32plus *)&ptr[(n) * 4])
dflet 0:6ad60d78b315 76 #define GET(n) \
dflet 0:6ad60d78b315 77 SET(n)
dflet 0:6ad60d78b315 78 #else
dflet 0:6ad60d78b315 79 #define SET(n) \
dflet 0:6ad60d78b315 80 (ctx->block[(n)] = \
dflet 0:6ad60d78b315 81 (MD5_u32plus)ptr[(n) * 4] | \
dflet 0:6ad60d78b315 82 ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
dflet 0:6ad60d78b315 83 ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
dflet 0:6ad60d78b315 84 ((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
dflet 0:6ad60d78b315 85 #define GET(n) \
dflet 0:6ad60d78b315 86 (ctx->block[(n)])
dflet 0:6ad60d78b315 87 #endif
dflet 0:6ad60d78b315 88
dflet 0:6ad60d78b315 89 /*
dflet 0:6ad60d78b315 90 * This processes one or more 64-byte data blocks, but does NOT update
dflet 0:6ad60d78b315 91 * the bit counters. There are no alignment requirements.
dflet 0:6ad60d78b315 92 */
dflet 0:6ad60d78b315 93 static void *body(MD5_CTX *ctx, void *data, unsigned long size)
dflet 0:6ad60d78b315 94 {
dflet 0:6ad60d78b315 95 unsigned char *ptr;
dflet 0:6ad60d78b315 96 MD5_u32plus a, b, c, d;
dflet 0:6ad60d78b315 97 MD5_u32plus saved_a, saved_b, saved_c, saved_d;
dflet 0:6ad60d78b315 98
dflet 0:6ad60d78b315 99 ptr = (unsigned char*)data;
dflet 0:6ad60d78b315 100
dflet 0:6ad60d78b315 101 a = ctx->a;
dflet 0:6ad60d78b315 102 b = ctx->b;
dflet 0:6ad60d78b315 103 c = ctx->c;
dflet 0:6ad60d78b315 104 d = ctx->d;
dflet 0:6ad60d78b315 105
dflet 0:6ad60d78b315 106 do {
dflet 0:6ad60d78b315 107 saved_a = a;
dflet 0:6ad60d78b315 108 saved_b = b;
dflet 0:6ad60d78b315 109 saved_c = c;
dflet 0:6ad60d78b315 110 saved_d = d;
dflet 0:6ad60d78b315 111
dflet 0:6ad60d78b315 112 /* Round 1 */
dflet 0:6ad60d78b315 113 STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
dflet 0:6ad60d78b315 114 STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
dflet 0:6ad60d78b315 115 STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
dflet 0:6ad60d78b315 116 STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
dflet 0:6ad60d78b315 117 STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
dflet 0:6ad60d78b315 118 STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
dflet 0:6ad60d78b315 119 STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
dflet 0:6ad60d78b315 120 STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
dflet 0:6ad60d78b315 121 STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
dflet 0:6ad60d78b315 122 STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
dflet 0:6ad60d78b315 123 STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
dflet 0:6ad60d78b315 124 STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
dflet 0:6ad60d78b315 125 STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
dflet 0:6ad60d78b315 126 STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
dflet 0:6ad60d78b315 127 STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
dflet 0:6ad60d78b315 128 STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
dflet 0:6ad60d78b315 129
dflet 0:6ad60d78b315 130 /* Round 2 */
dflet 0:6ad60d78b315 131 STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
dflet 0:6ad60d78b315 132 STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
dflet 0:6ad60d78b315 133 STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
dflet 0:6ad60d78b315 134 STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
dflet 0:6ad60d78b315 135 STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
dflet 0:6ad60d78b315 136 STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
dflet 0:6ad60d78b315 137 STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
dflet 0:6ad60d78b315 138 STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
dflet 0:6ad60d78b315 139 STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
dflet 0:6ad60d78b315 140 STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
dflet 0:6ad60d78b315 141 STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
dflet 0:6ad60d78b315 142 STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
dflet 0:6ad60d78b315 143 STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
dflet 0:6ad60d78b315 144 STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
dflet 0:6ad60d78b315 145 STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
dflet 0:6ad60d78b315 146 STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
dflet 0:6ad60d78b315 147
dflet 0:6ad60d78b315 148 /* Round 3 */
dflet 0:6ad60d78b315 149 STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
dflet 0:6ad60d78b315 150 STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
dflet 0:6ad60d78b315 151 STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
dflet 0:6ad60d78b315 152 STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
dflet 0:6ad60d78b315 153 STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
dflet 0:6ad60d78b315 154 STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
dflet 0:6ad60d78b315 155 STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
dflet 0:6ad60d78b315 156 STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
dflet 0:6ad60d78b315 157 STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
dflet 0:6ad60d78b315 158 STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
dflet 0:6ad60d78b315 159 STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
dflet 0:6ad60d78b315 160 STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
dflet 0:6ad60d78b315 161 STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
dflet 0:6ad60d78b315 162 STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
dflet 0:6ad60d78b315 163 STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
dflet 0:6ad60d78b315 164 STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
dflet 0:6ad60d78b315 165
dflet 0:6ad60d78b315 166 /* Round 4 */
dflet 0:6ad60d78b315 167 STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
dflet 0:6ad60d78b315 168 STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
dflet 0:6ad60d78b315 169 STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
dflet 0:6ad60d78b315 170 STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
dflet 0:6ad60d78b315 171 STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
dflet 0:6ad60d78b315 172 STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
dflet 0:6ad60d78b315 173 STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
dflet 0:6ad60d78b315 174 STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
dflet 0:6ad60d78b315 175 STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
dflet 0:6ad60d78b315 176 STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
dflet 0:6ad60d78b315 177 STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
dflet 0:6ad60d78b315 178 STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
dflet 0:6ad60d78b315 179 STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
dflet 0:6ad60d78b315 180 STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
dflet 0:6ad60d78b315 181 STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
dflet 0:6ad60d78b315 182 STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
dflet 0:6ad60d78b315 183
dflet 0:6ad60d78b315 184 a += saved_a;
dflet 0:6ad60d78b315 185 b += saved_b;
dflet 0:6ad60d78b315 186 c += saved_c;
dflet 0:6ad60d78b315 187 d += saved_d;
dflet 0:6ad60d78b315 188
dflet 0:6ad60d78b315 189 ptr += 64;
dflet 0:6ad60d78b315 190 } while (size -= 64);
dflet 0:6ad60d78b315 191
dflet 0:6ad60d78b315 192 ctx->a = a;
dflet 0:6ad60d78b315 193 ctx->b = b;
dflet 0:6ad60d78b315 194 ctx->c = c;
dflet 0:6ad60d78b315 195 ctx->d = d;
dflet 0:6ad60d78b315 196
dflet 0:6ad60d78b315 197 return ptr;
dflet 0:6ad60d78b315 198 }
dflet 0:6ad60d78b315 199
dflet 0:6ad60d78b315 200 void MD5_Init(MD5_CTX *ctx)
dflet 0:6ad60d78b315 201 {
dflet 0:6ad60d78b315 202 ctx->a = 0x67452301;
dflet 0:6ad60d78b315 203 ctx->b = 0xefcdab89;
dflet 0:6ad60d78b315 204 ctx->c = 0x98badcfe;
dflet 0:6ad60d78b315 205 ctx->d = 0x10325476;
dflet 0:6ad60d78b315 206
dflet 0:6ad60d78b315 207 ctx->lo = 0;
dflet 0:6ad60d78b315 208 ctx->hi = 0;
dflet 0:6ad60d78b315 209 }
dflet 0:6ad60d78b315 210
dflet 0:6ad60d78b315 211 void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
dflet 0:6ad60d78b315 212 {
dflet 0:6ad60d78b315 213 MD5_u32plus saved_lo;
dflet 0:6ad60d78b315 214 unsigned long used, free;
dflet 0:6ad60d78b315 215
dflet 0:6ad60d78b315 216 saved_lo = ctx->lo;
dflet 0:6ad60d78b315 217 if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
dflet 0:6ad60d78b315 218 ctx->hi++;
dflet 0:6ad60d78b315 219 ctx->hi += size >> 29;
dflet 0:6ad60d78b315 220
dflet 0:6ad60d78b315 221 used = saved_lo & 0x3f;
dflet 0:6ad60d78b315 222
dflet 0:6ad60d78b315 223 if (used) {
dflet 0:6ad60d78b315 224 free = 64 - used;
dflet 0:6ad60d78b315 225
dflet 0:6ad60d78b315 226 if (size < free) {
dflet 0:6ad60d78b315 227 memcpy(&ctx->buffer[used], data, size);
dflet 0:6ad60d78b315 228 return;
dflet 0:6ad60d78b315 229 }
dflet 0:6ad60d78b315 230
dflet 0:6ad60d78b315 231 memcpy(&ctx->buffer[used], data, free);
dflet 0:6ad60d78b315 232 data = (unsigned char *)data + free;
dflet 0:6ad60d78b315 233 size -= free;
dflet 0:6ad60d78b315 234 body(ctx, ctx->buffer, 64);
dflet 0:6ad60d78b315 235 }
dflet 0:6ad60d78b315 236
dflet 0:6ad60d78b315 237 if (size >= 64) {
dflet 0:6ad60d78b315 238 data = body(ctx, data, size & ~(unsigned long)0x3f);
dflet 0:6ad60d78b315 239 size &= 0x3f;
dflet 0:6ad60d78b315 240 }
dflet 0:6ad60d78b315 241
dflet 0:6ad60d78b315 242 memcpy(ctx->buffer, data, size);
dflet 0:6ad60d78b315 243 }
dflet 0:6ad60d78b315 244
dflet 0:6ad60d78b315 245 void MD5_Final(unsigned char *result, MD5_CTX *ctx)
dflet 0:6ad60d78b315 246 {
dflet 0:6ad60d78b315 247 unsigned long used, free;
dflet 0:6ad60d78b315 248
dflet 0:6ad60d78b315 249 used = ctx->lo & 0x3f;
dflet 0:6ad60d78b315 250
dflet 0:6ad60d78b315 251 ctx->buffer[used++] = 0x80;
dflet 0:6ad60d78b315 252
dflet 0:6ad60d78b315 253 free = 64 - used;
dflet 0:6ad60d78b315 254
dflet 0:6ad60d78b315 255 if (free < 8) {
dflet 0:6ad60d78b315 256 memset(&ctx->buffer[used], 0, free);
dflet 0:6ad60d78b315 257 body(ctx, ctx->buffer, 64);
dflet 0:6ad60d78b315 258 used = 0;
dflet 0:6ad60d78b315 259 free = 64;
dflet 0:6ad60d78b315 260 }
dflet 0:6ad60d78b315 261
dflet 0:6ad60d78b315 262 memset(&ctx->buffer[used], 0, free - 8);
dflet 0:6ad60d78b315 263
dflet 0:6ad60d78b315 264 ctx->lo <<= 3;
dflet 0:6ad60d78b315 265 ctx->buffer[56] = ctx->lo;
dflet 0:6ad60d78b315 266 ctx->buffer[57] = ctx->lo >> 8;
dflet 0:6ad60d78b315 267 ctx->buffer[58] = ctx->lo >> 16;
dflet 0:6ad60d78b315 268 ctx->buffer[59] = ctx->lo >> 24;
dflet 0:6ad60d78b315 269 ctx->buffer[60] = ctx->hi;
dflet 0:6ad60d78b315 270 ctx->buffer[61] = ctx->hi >> 8;
dflet 0:6ad60d78b315 271 ctx->buffer[62] = ctx->hi >> 16;
dflet 0:6ad60d78b315 272 ctx->buffer[63] = ctx->hi >> 24;
dflet 0:6ad60d78b315 273
dflet 0:6ad60d78b315 274 body(ctx, ctx->buffer, 64);
dflet 0:6ad60d78b315 275
dflet 0:6ad60d78b315 276 result[0] = ctx->a;
dflet 0:6ad60d78b315 277 result[1] = ctx->a >> 8;
dflet 0:6ad60d78b315 278 result[2] = ctx->a >> 16;
dflet 0:6ad60d78b315 279 result[3] = ctx->a >> 24;
dflet 0:6ad60d78b315 280 result[4] = ctx->b;
dflet 0:6ad60d78b315 281 result[5] = ctx->b >> 8;
dflet 0:6ad60d78b315 282 result[6] = ctx->b >> 16;
dflet 0:6ad60d78b315 283 result[7] = ctx->b >> 24;
dflet 0:6ad60d78b315 284 result[8] = ctx->c;
dflet 0:6ad60d78b315 285 result[9] = ctx->c >> 8;
dflet 0:6ad60d78b315 286 result[10] = ctx->c >> 16;
dflet 0:6ad60d78b315 287 result[11] = ctx->c >> 24;
dflet 0:6ad60d78b315 288 result[12] = ctx->d;
dflet 0:6ad60d78b315 289 result[13] = ctx->d >> 8;
dflet 0:6ad60d78b315 290 result[14] = ctx->d >> 16;
dflet 0:6ad60d78b315 291 result[15] = ctx->d >> 24;
dflet 0:6ad60d78b315 292
dflet 0:6ad60d78b315 293 memset(ctx, 0, sizeof(*ctx));
dflet 0:6ad60d78b315 294 }
dflet 0:6ad60d78b315 295
dflet 0:6ad60d78b315 296 #endif
dflet 0:6ad60d78b315 297