Rough and ready port of axTLS

Committer:
ashleymills
Date:
Mon May 13 18:15:18 2013 +0000
Revision:
0:5a29fd060ac8
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 0:5a29fd060ac8 1 /*
ashleymills 0:5a29fd060ac8 2 * Copyright (c) 2007, Cameron Rich
ashleymills 0:5a29fd060ac8 3 *
ashleymills 0:5a29fd060ac8 4 * All rights reserved.
ashleymills 0:5a29fd060ac8 5 *
ashleymills 0:5a29fd060ac8 6 * Redistribution and use in source and binary forms, with or without
ashleymills 0:5a29fd060ac8 7 * modification, are permitted provided that the following conditions are met:
ashleymills 0:5a29fd060ac8 8 *
ashleymills 0:5a29fd060ac8 9 * * Redistributions of source code must retain the above copyright notice,
ashleymills 0:5a29fd060ac8 10 * this list of conditions and the following disclaimer.
ashleymills 0:5a29fd060ac8 11 * * Redistributions in binary form must reproduce the above copyright notice,
ashleymills 0:5a29fd060ac8 12 * this list of conditions and the following disclaimer in the documentation
ashleymills 0:5a29fd060ac8 13 * and/or other materials provided with the distribution.
ashleymills 0:5a29fd060ac8 14 * * Neither the name of the axTLS project nor the names of its contributors
ashleymills 0:5a29fd060ac8 15 * may be used to endorse or promote products derived from this software
ashleymills 0:5a29fd060ac8 16 * without specific prior written permission.
ashleymills 0:5a29fd060ac8 17 *
ashleymills 0:5a29fd060ac8 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
ashleymills 0:5a29fd060ac8 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
ashleymills 0:5a29fd060ac8 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
ashleymills 0:5a29fd060ac8 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
ashleymills 0:5a29fd060ac8 22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
ashleymills 0:5a29fd060ac8 23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
ashleymills 0:5a29fd060ac8 24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
ashleymills 0:5a29fd060ac8 25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
ashleymills 0:5a29fd060ac8 26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
ashleymills 0:5a29fd060ac8 27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
ashleymills 0:5a29fd060ac8 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ashleymills 0:5a29fd060ac8 29 */
ashleymills 0:5a29fd060ac8 30
ashleymills 0:5a29fd060ac8 31 /**
ashleymills 0:5a29fd060ac8 32 * This file implements the MD5 algorithm as defined in RFC1321
ashleymills 0:5a29fd060ac8 33 */
ashleymills 0:5a29fd060ac8 34
ashleymills 0:5a29fd060ac8 35 #include <string.h>
ashleymills 0:5a29fd060ac8 36 #include "os_port.h"
ashleymills 0:5a29fd060ac8 37 #include "crypto.h"
ashleymills 0:5a29fd060ac8 38
ashleymills 0:5a29fd060ac8 39 /* Constants for MD5Transform routine.
ashleymills 0:5a29fd060ac8 40 */
ashleymills 0:5a29fd060ac8 41 #define S11 7
ashleymills 0:5a29fd060ac8 42 #define S12 12
ashleymills 0:5a29fd060ac8 43 #define S13 17
ashleymills 0:5a29fd060ac8 44 #define S14 22
ashleymills 0:5a29fd060ac8 45 #define S21 5
ashleymills 0:5a29fd060ac8 46 #define S22 9
ashleymills 0:5a29fd060ac8 47 #define S23 14
ashleymills 0:5a29fd060ac8 48 #define S24 20
ashleymills 0:5a29fd060ac8 49 #define S31 4
ashleymills 0:5a29fd060ac8 50 #define S32 11
ashleymills 0:5a29fd060ac8 51 #define S33 16
ashleymills 0:5a29fd060ac8 52 #define S34 23
ashleymills 0:5a29fd060ac8 53 #define S41 6
ashleymills 0:5a29fd060ac8 54 #define S42 10
ashleymills 0:5a29fd060ac8 55 #define S43 15
ashleymills 0:5a29fd060ac8 56 #define S44 21
ashleymills 0:5a29fd060ac8 57
ashleymills 0:5a29fd060ac8 58 /* ----- static functions ----- */
ashleymills 0:5a29fd060ac8 59 static void MD5Transform(uint32_t state[4], const uint8_t block[64]);
ashleymills 0:5a29fd060ac8 60 static void Encode(uint8_t *output, uint32_t *input, uint32_t len);
ashleymills 0:5a29fd060ac8 61 static void Decode(uint32_t *output, const uint8_t *input, uint32_t len);
ashleymills 0:5a29fd060ac8 62
ashleymills 0:5a29fd060ac8 63 static const uint8_t PADDING[64] =
ashleymills 0:5a29fd060ac8 64 {
ashleymills 0:5a29fd060ac8 65 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ashleymills 0:5a29fd060ac8 66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
ashleymills 0:5a29fd060ac8 67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
ashleymills 0:5a29fd060ac8 68 };
ashleymills 0:5a29fd060ac8 69
ashleymills 0:5a29fd060ac8 70 /* F, G, H and I are basic MD5 functions.
ashleymills 0:5a29fd060ac8 71 */
ashleymills 0:5a29fd060ac8 72 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
ashleymills 0:5a29fd060ac8 73 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
ashleymills 0:5a29fd060ac8 74 #define H(x, y, z) ((x) ^ (y) ^ (z))
ashleymills 0:5a29fd060ac8 75 #define I(x, y, z) ((y) ^ ((x) | (~z)))
ashleymills 0:5a29fd060ac8 76
ashleymills 0:5a29fd060ac8 77 /* ROTATE_LEFT rotates x left n bits. */
ashleymills 0:5a29fd060ac8 78 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
ashleymills 0:5a29fd060ac8 79
ashleymills 0:5a29fd060ac8 80 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
ashleymills 0:5a29fd060ac8 81 Rotation is separate from addition to prevent recomputation. */
ashleymills 0:5a29fd060ac8 82 #define FF(a, b, c, d, x, s, ac) { \
ashleymills 0:5a29fd060ac8 83 (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
ashleymills 0:5a29fd060ac8 84 (a) = ROTATE_LEFT ((a), (s)); \
ashleymills 0:5a29fd060ac8 85 (a) += (b); \
ashleymills 0:5a29fd060ac8 86 }
ashleymills 0:5a29fd060ac8 87 #define GG(a, b, c, d, x, s, ac) { \
ashleymills 0:5a29fd060ac8 88 (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
ashleymills 0:5a29fd060ac8 89 (a) = ROTATE_LEFT ((a), (s)); \
ashleymills 0:5a29fd060ac8 90 (a) += (b); \
ashleymills 0:5a29fd060ac8 91 }
ashleymills 0:5a29fd060ac8 92 #define HH(a, b, c, d, x, s, ac) { \
ashleymills 0:5a29fd060ac8 93 (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
ashleymills 0:5a29fd060ac8 94 (a) = ROTATE_LEFT ((a), (s)); \
ashleymills 0:5a29fd060ac8 95 (a) += (b); \
ashleymills 0:5a29fd060ac8 96 }
ashleymills 0:5a29fd060ac8 97 #define II(a, b, c, d, x, s, ac) { \
ashleymills 0:5a29fd060ac8 98 (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
ashleymills 0:5a29fd060ac8 99 (a) = ROTATE_LEFT ((a), (s)); \
ashleymills 0:5a29fd060ac8 100 (a) += (b); \
ashleymills 0:5a29fd060ac8 101 }
ashleymills 0:5a29fd060ac8 102
ashleymills 0:5a29fd060ac8 103 /**
ashleymills 0:5a29fd060ac8 104 * MD5 initialization - begins an MD5 operation, writing a new ctx.
ashleymills 0:5a29fd060ac8 105 */
ashleymills 0:5a29fd060ac8 106 EXP_FUNC void STDCALL MD5_Init(MD5_CTX *ctx)
ashleymills 0:5a29fd060ac8 107 {
ashleymills 0:5a29fd060ac8 108 ctx->count[0] = ctx->count[1] = 0;
ashleymills 0:5a29fd060ac8 109
ashleymills 0:5a29fd060ac8 110 /* Load magic initialization constants.
ashleymills 0:5a29fd060ac8 111 */
ashleymills 0:5a29fd060ac8 112 ctx->state[0] = 0x67452301;
ashleymills 0:5a29fd060ac8 113 ctx->state[1] = 0xefcdab89;
ashleymills 0:5a29fd060ac8 114 ctx->state[2] = 0x98badcfe;
ashleymills 0:5a29fd060ac8 115 ctx->state[3] = 0x10325476;
ashleymills 0:5a29fd060ac8 116 }
ashleymills 0:5a29fd060ac8 117
ashleymills 0:5a29fd060ac8 118 /**
ashleymills 0:5a29fd060ac8 119 * Accepts an array of octets as the next portion of the message.
ashleymills 0:5a29fd060ac8 120 */
ashleymills 0:5a29fd060ac8 121 EXP_FUNC void STDCALL MD5_Update(MD5_CTX *ctx, const uint8_t * msg, int len)
ashleymills 0:5a29fd060ac8 122 {
ashleymills 0:5a29fd060ac8 123 uint32_t x;
ashleymills 0:5a29fd060ac8 124 int i, partLen;
ashleymills 0:5a29fd060ac8 125
ashleymills 0:5a29fd060ac8 126 /* Compute number of bytes mod 64 */
ashleymills 0:5a29fd060ac8 127 x = (uint32_t)((ctx->count[0] >> 3) & 0x3F);
ashleymills 0:5a29fd060ac8 128
ashleymills 0:5a29fd060ac8 129 /* Update number of bits */
ashleymills 0:5a29fd060ac8 130 if ((ctx->count[0] += ((uint32_t)len << 3)) < ((uint32_t)len << 3))
ashleymills 0:5a29fd060ac8 131 ctx->count[1]++;
ashleymills 0:5a29fd060ac8 132 ctx->count[1] += ((uint32_t)len >> 29);
ashleymills 0:5a29fd060ac8 133
ashleymills 0:5a29fd060ac8 134 partLen = 64 - x;
ashleymills 0:5a29fd060ac8 135
ashleymills 0:5a29fd060ac8 136 /* Transform as many times as possible. */
ashleymills 0:5a29fd060ac8 137 if (len >= partLen)
ashleymills 0:5a29fd060ac8 138 {
ashleymills 0:5a29fd060ac8 139 memcpy(&ctx->buffer[x], msg, partLen);
ashleymills 0:5a29fd060ac8 140 MD5Transform(ctx->state, ctx->buffer);
ashleymills 0:5a29fd060ac8 141
ashleymills 0:5a29fd060ac8 142 for (i = partLen; i + 63 < len; i += 64)
ashleymills 0:5a29fd060ac8 143 MD5Transform(ctx->state, &msg[i]);
ashleymills 0:5a29fd060ac8 144
ashleymills 0:5a29fd060ac8 145 x = 0;
ashleymills 0:5a29fd060ac8 146 }
ashleymills 0:5a29fd060ac8 147 else
ashleymills 0:5a29fd060ac8 148 i = 0;
ashleymills 0:5a29fd060ac8 149
ashleymills 0:5a29fd060ac8 150 /* Buffer remaining input */
ashleymills 0:5a29fd060ac8 151 memcpy(&ctx->buffer[x], &msg[i], len-i);
ashleymills 0:5a29fd060ac8 152 }
ashleymills 0:5a29fd060ac8 153
ashleymills 0:5a29fd060ac8 154 /**
ashleymills 0:5a29fd060ac8 155 * Return the 128-bit message digest into the user's array
ashleymills 0:5a29fd060ac8 156 */
ashleymills 0:5a29fd060ac8 157 EXP_FUNC void STDCALL MD5_Final(uint8_t *digest, MD5_CTX *ctx)
ashleymills 0:5a29fd060ac8 158 {
ashleymills 0:5a29fd060ac8 159 uint8_t bits[8];
ashleymills 0:5a29fd060ac8 160 uint32_t x, padLen;
ashleymills 0:5a29fd060ac8 161
ashleymills 0:5a29fd060ac8 162 /* Save number of bits */
ashleymills 0:5a29fd060ac8 163 Encode(bits, ctx->count, 8);
ashleymills 0:5a29fd060ac8 164
ashleymills 0:5a29fd060ac8 165 /* Pad out to 56 mod 64.
ashleymills 0:5a29fd060ac8 166 */
ashleymills 0:5a29fd060ac8 167 x = (uint32_t)((ctx->count[0] >> 3) & 0x3f);
ashleymills 0:5a29fd060ac8 168 padLen = (x < 56) ? (56 - x) : (120 - x);
ashleymills 0:5a29fd060ac8 169 MD5_Update(ctx, PADDING, padLen);
ashleymills 0:5a29fd060ac8 170
ashleymills 0:5a29fd060ac8 171 /* Append length (before padding) */
ashleymills 0:5a29fd060ac8 172 MD5_Update(ctx, bits, 8);
ashleymills 0:5a29fd060ac8 173
ashleymills 0:5a29fd060ac8 174 /* Store state in digest */
ashleymills 0:5a29fd060ac8 175 Encode(digest, ctx->state, MD5_SIZE);
ashleymills 0:5a29fd060ac8 176 }
ashleymills 0:5a29fd060ac8 177
ashleymills 0:5a29fd060ac8 178 /**
ashleymills 0:5a29fd060ac8 179 * MD5 basic transformation. Transforms state based on block.
ashleymills 0:5a29fd060ac8 180 */
ashleymills 0:5a29fd060ac8 181 static void MD5Transform(uint32_t state[4], const uint8_t block[64])
ashleymills 0:5a29fd060ac8 182 {
ashleymills 0:5a29fd060ac8 183 uint32_t a = state[0], b = state[1], c = state[2],
ashleymills 0:5a29fd060ac8 184 d = state[3], x[MD5_SIZE];
ashleymills 0:5a29fd060ac8 185
ashleymills 0:5a29fd060ac8 186 Decode(x, block, 64);
ashleymills 0:5a29fd060ac8 187
ashleymills 0:5a29fd060ac8 188 /* Round 1 */
ashleymills 0:5a29fd060ac8 189 FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
ashleymills 0:5a29fd060ac8 190 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
ashleymills 0:5a29fd060ac8 191 FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
ashleymills 0:5a29fd060ac8 192 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
ashleymills 0:5a29fd060ac8 193 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
ashleymills 0:5a29fd060ac8 194 FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
ashleymills 0:5a29fd060ac8 195 FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
ashleymills 0:5a29fd060ac8 196 FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
ashleymills 0:5a29fd060ac8 197 FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
ashleymills 0:5a29fd060ac8 198 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
ashleymills 0:5a29fd060ac8 199 FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
ashleymills 0:5a29fd060ac8 200 FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
ashleymills 0:5a29fd060ac8 201 FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
ashleymills 0:5a29fd060ac8 202 FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
ashleymills 0:5a29fd060ac8 203 FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
ashleymills 0:5a29fd060ac8 204 FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
ashleymills 0:5a29fd060ac8 205
ashleymills 0:5a29fd060ac8 206 /* Round 2 */
ashleymills 0:5a29fd060ac8 207 GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
ashleymills 0:5a29fd060ac8 208 GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
ashleymills 0:5a29fd060ac8 209 GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
ashleymills 0:5a29fd060ac8 210 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
ashleymills 0:5a29fd060ac8 211 GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
ashleymills 0:5a29fd060ac8 212 GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
ashleymills 0:5a29fd060ac8 213 GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
ashleymills 0:5a29fd060ac8 214 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
ashleymills 0:5a29fd060ac8 215 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
ashleymills 0:5a29fd060ac8 216 GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
ashleymills 0:5a29fd060ac8 217 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
ashleymills 0:5a29fd060ac8 218 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
ashleymills 0:5a29fd060ac8 219 GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
ashleymills 0:5a29fd060ac8 220 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
ashleymills 0:5a29fd060ac8 221 GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
ashleymills 0:5a29fd060ac8 222 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
ashleymills 0:5a29fd060ac8 223
ashleymills 0:5a29fd060ac8 224 /* Round 3 */
ashleymills 0:5a29fd060ac8 225 HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
ashleymills 0:5a29fd060ac8 226 HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
ashleymills 0:5a29fd060ac8 227 HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
ashleymills 0:5a29fd060ac8 228 HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
ashleymills 0:5a29fd060ac8 229 HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
ashleymills 0:5a29fd060ac8 230 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
ashleymills 0:5a29fd060ac8 231 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
ashleymills 0:5a29fd060ac8 232 HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
ashleymills 0:5a29fd060ac8 233 HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
ashleymills 0:5a29fd060ac8 234 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
ashleymills 0:5a29fd060ac8 235 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
ashleymills 0:5a29fd060ac8 236 HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
ashleymills 0:5a29fd060ac8 237 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
ashleymills 0:5a29fd060ac8 238 HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
ashleymills 0:5a29fd060ac8 239 HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
ashleymills 0:5a29fd060ac8 240 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
ashleymills 0:5a29fd060ac8 241
ashleymills 0:5a29fd060ac8 242 /* Round 4 */
ashleymills 0:5a29fd060ac8 243 II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
ashleymills 0:5a29fd060ac8 244 II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
ashleymills 0:5a29fd060ac8 245 II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
ashleymills 0:5a29fd060ac8 246 II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
ashleymills 0:5a29fd060ac8 247 II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
ashleymills 0:5a29fd060ac8 248 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
ashleymills 0:5a29fd060ac8 249 II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
ashleymills 0:5a29fd060ac8 250 II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
ashleymills 0:5a29fd060ac8 251 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
ashleymills 0:5a29fd060ac8 252 II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
ashleymills 0:5a29fd060ac8 253 II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
ashleymills 0:5a29fd060ac8 254 II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
ashleymills 0:5a29fd060ac8 255 II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
ashleymills 0:5a29fd060ac8 256 II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
ashleymills 0:5a29fd060ac8 257 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
ashleymills 0:5a29fd060ac8 258 II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
ashleymills 0:5a29fd060ac8 259
ashleymills 0:5a29fd060ac8 260 state[0] += a;
ashleymills 0:5a29fd060ac8 261 state[1] += b;
ashleymills 0:5a29fd060ac8 262 state[2] += c;
ashleymills 0:5a29fd060ac8 263 state[3] += d;
ashleymills 0:5a29fd060ac8 264 }
ashleymills 0:5a29fd060ac8 265
ashleymills 0:5a29fd060ac8 266 /**
ashleymills 0:5a29fd060ac8 267 * Encodes input (uint32_t) into output (uint8_t). Assumes len is
ashleymills 0:5a29fd060ac8 268 * a multiple of 4.
ashleymills 0:5a29fd060ac8 269 */
ashleymills 0:5a29fd060ac8 270 static void Encode(uint8_t *output, uint32_t *input, uint32_t len)
ashleymills 0:5a29fd060ac8 271 {
ashleymills 0:5a29fd060ac8 272 uint32_t i, j;
ashleymills 0:5a29fd060ac8 273
ashleymills 0:5a29fd060ac8 274 for (i = 0, j = 0; j < len; i++, j += 4)
ashleymills 0:5a29fd060ac8 275 {
ashleymills 0:5a29fd060ac8 276 output[j] = (uint8_t)(input[i] & 0xff);
ashleymills 0:5a29fd060ac8 277 output[j+1] = (uint8_t)((input[i] >> 8) & 0xff);
ashleymills 0:5a29fd060ac8 278 output[j+2] = (uint8_t)((input[i] >> 16) & 0xff);
ashleymills 0:5a29fd060ac8 279 output[j+3] = (uint8_t)((input[i] >> 24) & 0xff);
ashleymills 0:5a29fd060ac8 280 }
ashleymills 0:5a29fd060ac8 281 }
ashleymills 0:5a29fd060ac8 282
ashleymills 0:5a29fd060ac8 283 /**
ashleymills 0:5a29fd060ac8 284 * Decodes input (uint8_t) into output (uint32_t). Assumes len is
ashleymills 0:5a29fd060ac8 285 * a multiple of 4.
ashleymills 0:5a29fd060ac8 286 */
ashleymills 0:5a29fd060ac8 287 static void Decode(uint32_t *output, const uint8_t *input, uint32_t len)
ashleymills 0:5a29fd060ac8 288 {
ashleymills 0:5a29fd060ac8 289 uint32_t i, j;
ashleymills 0:5a29fd060ac8 290
ashleymills 0:5a29fd060ac8 291 for (i = 0, j = 0; j < len; i++, j += 4)
ashleymills 0:5a29fd060ac8 292 output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) |
ashleymills 0:5a29fd060ac8 293 (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
ashleymills 0:5a29fd060ac8 294 }