This is a fork of the mbed port of axTLS

Dependents:   TLS_axTLS-Example HTTPSClientExample

Overview

This library is a fork from the mbed port of axTLS. It attempts to :

  • reduce the usage of dynamic memory
  • verify certificates with key size up to 2048 bits
  • provide a simple interface

Encryption

This library uses either RC4 or AES for encryption.

Memory usage

During the establishment of a connection, about 10KB of memory is allocated dynamically (it depends on certificates). Once the connection is established, the memory consumption is relatively low. This means that your program must not use too much static memory or allocate memory before you establish a TLS connection.

Certificates

Certificates are the major source of problem and will often be the reason why your program will crash. Due to memory constraint, there are some limitations on certificates :

  • Each certificate must not be bigger than 2KB
  • TLS client can only handle a chain of up to three certificates (excluding the root certificate). This means that the server must not send more than three certificates.

Also, this library can only load certificates following these specifications :

  • encoded in binary DER format (PKCS1)
  • The public key must use RSA only

Once the connection is established, you should free all loaded certificates by calling CertificateManager::clear(). This will free a few kilobytes (it depends on your certificates). In addition, to enable certificate verification during the connection, this library has a "precomputed mode". This mode uses much less memory than a normal certificate verification.

Normal mode

You need to copy the root certificate in binary-DER format on the mbed. Then in your code, let's say that your root certificate is saved on the mbed as "root.der", assuming that you include CertificateManager.h and that you created a LocalFileSystem, you can load this certificate as this ;

Load root certificate

CertificateManager::add("/local/root.der");
CertificateManager::load();

Do not forget that this mode takes quite a lot of memory ( the memory peak is high while verifying certificates) and will only work if the key size is not bigger than 1024 bits (otherwise it will crash while verifying certificates).

Precomputed mode

In this mode, you need to save the entire chain of certificates (in binary-DER format) including the root certificate on the mbed. In practice, this means that you must first retrieve all certificates that the server sends during a connection and then find the right root certificate. In your code, you must call CertificateManager::add for each certificate and in the right order : from the server certificate to the root certificate. Here is how you shoud load certificates in this mode :

Loadcertificates in precomputed mode

CertificateManager::add("/local/server1.der");
CertificateManager::add("/local/server2.der");
CertificateManager::add("/local/server3.der");
CertificateManager::add("/local/root.der");
CertificateManager::load(true);

Using this mode, you should be able to verify certificates with key size up to 2048 bits.

How do I find these certificates ?

I posted an entry in my notebook detailing how to get certificates from a server. You should be able to get all certificates you need except the root certificate. Here is a way how to get the root certificate on windows :

  1. Open (double-click) the last certificate sent by the server
  2. Go to details panel and click on the entry called Issuer. The first line gives you the name of this certificate and the second line indicates the company who created this certificate
  3. Open firefox
  4. Go to options, advanced panel and click on View Certificates
  5. Go to Authorities panel
  6. Choose the certificate whose name match the issuer of the last certificate sent by the server
  7. Export this certificate to binary-DER format.

Connect to mbed.org !

Import programTLS_axTLS-Example

Establishing a connection to mbed.org using TLS

Committer:
feb11
Date:
Thu Sep 12 15:18:04 2013 +0000
Revision:
0:85fceccc1a7c
intial import

Who changed what in which revision?

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