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 * AES implementation - this is a small code version. There are much faster
feb11 0:85fceccc1a7c 33 * versions around but they are much larger in size (i.e. they use large
feb11 0:85fceccc1a7c 34 * submix tables).
feb11 0:85fceccc1a7c 35 */
feb11 0:85fceccc1a7c 36
feb11 0:85fceccc1a7c 37 #include <string.h>
feb11 0:85fceccc1a7c 38 //#include "os_port.h"
feb11 0:85fceccc1a7c 39 #include "crypto.h"
feb11 0:85fceccc1a7c 40 #include <lwip/def.h>
feb11 0:85fceccc1a7c 41
feb11 0:85fceccc1a7c 42 /* all commented out in skeleton mode */
feb11 0:85fceccc1a7c 43 #ifndef CONFIG_SSL_SKELETON_MODE
feb11 0:85fceccc1a7c 44
feb11 0:85fceccc1a7c 45 #define rot1(x) (((x) << 24) | ((x) >> 8))
feb11 0:85fceccc1a7c 46 #define rot2(x) (((x) << 16) | ((x) >> 16))
feb11 0:85fceccc1a7c 47 #define rot3(x) (((x) << 8) | ((x) >> 24))
feb11 0:85fceccc1a7c 48
feb11 0:85fceccc1a7c 49 /*
feb11 0:85fceccc1a7c 50 * This cute trick does 4 'mul by two' at once. Stolen from
feb11 0:85fceccc1a7c 51 * Dr B. R. Gladman <brg@gladman.uk.net> but I'm sure the u-(u>>7) is
feb11 0:85fceccc1a7c 52 * a standard graphics trick
feb11 0:85fceccc1a7c 53 * The key to this is that we need to xor with 0x1b if the top bit is set.
feb11 0:85fceccc1a7c 54 * a 1xxx xxxx 0xxx 0xxx First we mask the 7bit,
feb11 0:85fceccc1a7c 55 * b 1000 0000 0000 0000 then we shift right by 7 putting the 7bit in 0bit,
feb11 0:85fceccc1a7c 56 * c 0000 0001 0000 0000 we then subtract (c) from (b)
feb11 0:85fceccc1a7c 57 * d 0111 1111 0000 0000 and now we and with our mask
feb11 0:85fceccc1a7c 58 * e 0001 1011 0000 0000
feb11 0:85fceccc1a7c 59 */
feb11 0:85fceccc1a7c 60 #define mt 0x80808080
feb11 0:85fceccc1a7c 61 #define ml 0x7f7f7f7f
feb11 0:85fceccc1a7c 62 #define mh 0xfefefefe
feb11 0:85fceccc1a7c 63 #define mm 0x1b1b1b1b
feb11 0:85fceccc1a7c 64 #define mul2(x,t) ((t)=((x)&mt), \
feb11 0:85fceccc1a7c 65 ((((x)+(x))&mh)^(((t)-((t)>>7))&mm)))
feb11 0:85fceccc1a7c 66
feb11 0:85fceccc1a7c 67 #define inv_mix_col(x,f2,f4,f8,f9) (\
feb11 0:85fceccc1a7c 68 (f2)=mul2(x,f2), \
feb11 0:85fceccc1a7c 69 (f4)=mul2(f2,f4), \
feb11 0:85fceccc1a7c 70 (f8)=mul2(f4,f8), \
feb11 0:85fceccc1a7c 71 (f9)=(x)^(f8), \
feb11 0:85fceccc1a7c 72 (f8)=((f2)^(f4)^(f8)), \
feb11 0:85fceccc1a7c 73 (f2)^=(f9), \
feb11 0:85fceccc1a7c 74 (f4)^=(f9), \
feb11 0:85fceccc1a7c 75 (f8)^=rot3(f2), \
feb11 0:85fceccc1a7c 76 (f8)^=rot2(f4), \
feb11 0:85fceccc1a7c 77 (f8)^rot1(f9))
feb11 0:85fceccc1a7c 78
feb11 0:85fceccc1a7c 79 /*
feb11 0:85fceccc1a7c 80 * AES S-box
feb11 0:85fceccc1a7c 81 */
feb11 0:85fceccc1a7c 82 static const uint8_t aes_sbox[256] =
feb11 0:85fceccc1a7c 83 {
feb11 0:85fceccc1a7c 84 0x63,0x7C,0x77,0x7B,0xF2,0x6B,0x6F,0xC5,
feb11 0:85fceccc1a7c 85 0x30,0x01,0x67,0x2B,0xFE,0xD7,0xAB,0x76,
feb11 0:85fceccc1a7c 86 0xCA,0x82,0xC9,0x7D,0xFA,0x59,0x47,0xF0,
feb11 0:85fceccc1a7c 87 0xAD,0xD4,0xA2,0xAF,0x9C,0xA4,0x72,0xC0,
feb11 0:85fceccc1a7c 88 0xB7,0xFD,0x93,0x26,0x36,0x3F,0xF7,0xCC,
feb11 0:85fceccc1a7c 89 0x34,0xA5,0xE5,0xF1,0x71,0xD8,0x31,0x15,
feb11 0:85fceccc1a7c 90 0x04,0xC7,0x23,0xC3,0x18,0x96,0x05,0x9A,
feb11 0:85fceccc1a7c 91 0x07,0x12,0x80,0xE2,0xEB,0x27,0xB2,0x75,
feb11 0:85fceccc1a7c 92 0x09,0x83,0x2C,0x1A,0x1B,0x6E,0x5A,0xA0,
feb11 0:85fceccc1a7c 93 0x52,0x3B,0xD6,0xB3,0x29,0xE3,0x2F,0x84,
feb11 0:85fceccc1a7c 94 0x53,0xD1,0x00,0xED,0x20,0xFC,0xB1,0x5B,
feb11 0:85fceccc1a7c 95 0x6A,0xCB,0xBE,0x39,0x4A,0x4C,0x58,0xCF,
feb11 0:85fceccc1a7c 96 0xD0,0xEF,0xAA,0xFB,0x43,0x4D,0x33,0x85,
feb11 0:85fceccc1a7c 97 0x45,0xF9,0x02,0x7F,0x50,0x3C,0x9F,0xA8,
feb11 0:85fceccc1a7c 98 0x51,0xA3,0x40,0x8F,0x92,0x9D,0x38,0xF5,
feb11 0:85fceccc1a7c 99 0xBC,0xB6,0xDA,0x21,0x10,0xFF,0xF3,0xD2,
feb11 0:85fceccc1a7c 100 0xCD,0x0C,0x13,0xEC,0x5F,0x97,0x44,0x17,
feb11 0:85fceccc1a7c 101 0xC4,0xA7,0x7E,0x3D,0x64,0x5D,0x19,0x73,
feb11 0:85fceccc1a7c 102 0x60,0x81,0x4F,0xDC,0x22,0x2A,0x90,0x88,
feb11 0:85fceccc1a7c 103 0x46,0xEE,0xB8,0x14,0xDE,0x5E,0x0B,0xDB,
feb11 0:85fceccc1a7c 104 0xE0,0x32,0x3A,0x0A,0x49,0x06,0x24,0x5C,
feb11 0:85fceccc1a7c 105 0xC2,0xD3,0xAC,0x62,0x91,0x95,0xE4,0x79,
feb11 0:85fceccc1a7c 106 0xE7,0xC8,0x37,0x6D,0x8D,0xD5,0x4E,0xA9,
feb11 0:85fceccc1a7c 107 0x6C,0x56,0xF4,0xEA,0x65,0x7A,0xAE,0x08,
feb11 0:85fceccc1a7c 108 0xBA,0x78,0x25,0x2E,0x1C,0xA6,0xB4,0xC6,
feb11 0:85fceccc1a7c 109 0xE8,0xDD,0x74,0x1F,0x4B,0xBD,0x8B,0x8A,
feb11 0:85fceccc1a7c 110 0x70,0x3E,0xB5,0x66,0x48,0x03,0xF6,0x0E,
feb11 0:85fceccc1a7c 111 0x61,0x35,0x57,0xB9,0x86,0xC1,0x1D,0x9E,
feb11 0:85fceccc1a7c 112 0xE1,0xF8,0x98,0x11,0x69,0xD9,0x8E,0x94,
feb11 0:85fceccc1a7c 113 0x9B,0x1E,0x87,0xE9,0xCE,0x55,0x28,0xDF,
feb11 0:85fceccc1a7c 114 0x8C,0xA1,0x89,0x0D,0xBF,0xE6,0x42,0x68,
feb11 0:85fceccc1a7c 115 0x41,0x99,0x2D,0x0F,0xB0,0x54,0xBB,0x16,
feb11 0:85fceccc1a7c 116 };
feb11 0:85fceccc1a7c 117
feb11 0:85fceccc1a7c 118 /*
feb11 0:85fceccc1a7c 119 * AES is-box
feb11 0:85fceccc1a7c 120 */
feb11 0:85fceccc1a7c 121 static const uint8_t aes_isbox[256] =
feb11 0:85fceccc1a7c 122 {
feb11 0:85fceccc1a7c 123 0x52,0x09,0x6a,0xd5,0x30,0x36,0xa5,0x38,
feb11 0:85fceccc1a7c 124 0xbf,0x40,0xa3,0x9e,0x81,0xf3,0xd7,0xfb,
feb11 0:85fceccc1a7c 125 0x7c,0xe3,0x39,0x82,0x9b,0x2f,0xff,0x87,
feb11 0:85fceccc1a7c 126 0x34,0x8e,0x43,0x44,0xc4,0xde,0xe9,0xcb,
feb11 0:85fceccc1a7c 127 0x54,0x7b,0x94,0x32,0xa6,0xc2,0x23,0x3d,
feb11 0:85fceccc1a7c 128 0xee,0x4c,0x95,0x0b,0x42,0xfa,0xc3,0x4e,
feb11 0:85fceccc1a7c 129 0x08,0x2e,0xa1,0x66,0x28,0xd9,0x24,0xb2,
feb11 0:85fceccc1a7c 130 0x76,0x5b,0xa2,0x49,0x6d,0x8b,0xd1,0x25,
feb11 0:85fceccc1a7c 131 0x72,0xf8,0xf6,0x64,0x86,0x68,0x98,0x16,
feb11 0:85fceccc1a7c 132 0xd4,0xa4,0x5c,0xcc,0x5d,0x65,0xb6,0x92,
feb11 0:85fceccc1a7c 133 0x6c,0x70,0x48,0x50,0xfd,0xed,0xb9,0xda,
feb11 0:85fceccc1a7c 134 0x5e,0x15,0x46,0x57,0xa7,0x8d,0x9d,0x84,
feb11 0:85fceccc1a7c 135 0x90,0xd8,0xab,0x00,0x8c,0xbc,0xd3,0x0a,
feb11 0:85fceccc1a7c 136 0xf7,0xe4,0x58,0x05,0xb8,0xb3,0x45,0x06,
feb11 0:85fceccc1a7c 137 0xd0,0x2c,0x1e,0x8f,0xca,0x3f,0x0f,0x02,
feb11 0:85fceccc1a7c 138 0xc1,0xaf,0xbd,0x03,0x01,0x13,0x8a,0x6b,
feb11 0:85fceccc1a7c 139 0x3a,0x91,0x11,0x41,0x4f,0x67,0xdc,0xea,
feb11 0:85fceccc1a7c 140 0x97,0xf2,0xcf,0xce,0xf0,0xb4,0xe6,0x73,
feb11 0:85fceccc1a7c 141 0x96,0xac,0x74,0x22,0xe7,0xad,0x35,0x85,
feb11 0:85fceccc1a7c 142 0xe2,0xf9,0x37,0xe8,0x1c,0x75,0xdf,0x6e,
feb11 0:85fceccc1a7c 143 0x47,0xf1,0x1a,0x71,0x1d,0x29,0xc5,0x89,
feb11 0:85fceccc1a7c 144 0x6f,0xb7,0x62,0x0e,0xaa,0x18,0xbe,0x1b,
feb11 0:85fceccc1a7c 145 0xfc,0x56,0x3e,0x4b,0xc6,0xd2,0x79,0x20,
feb11 0:85fceccc1a7c 146 0x9a,0xdb,0xc0,0xfe,0x78,0xcd,0x5a,0xf4,
feb11 0:85fceccc1a7c 147 0x1f,0xdd,0xa8,0x33,0x88,0x07,0xc7,0x31,
feb11 0:85fceccc1a7c 148 0xb1,0x12,0x10,0x59,0x27,0x80,0xec,0x5f,
feb11 0:85fceccc1a7c 149 0x60,0x51,0x7f,0xa9,0x19,0xb5,0x4a,0x0d,
feb11 0:85fceccc1a7c 150 0x2d,0xe5,0x7a,0x9f,0x93,0xc9,0x9c,0xef,
feb11 0:85fceccc1a7c 151 0xa0,0xe0,0x3b,0x4d,0xae,0x2a,0xf5,0xb0,
feb11 0:85fceccc1a7c 152 0xc8,0xeb,0xbb,0x3c,0x83,0x53,0x99,0x61,
feb11 0:85fceccc1a7c 153 0x17,0x2b,0x04,0x7e,0xba,0x77,0xd6,0x26,
feb11 0:85fceccc1a7c 154 0xe1,0x69,0x14,0x63,0x55,0x21,0x0c,0x7d
feb11 0:85fceccc1a7c 155 };
feb11 0:85fceccc1a7c 156
feb11 0:85fceccc1a7c 157 static const unsigned char Rcon[30]=
feb11 0:85fceccc1a7c 158 {
feb11 0:85fceccc1a7c 159 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,
feb11 0:85fceccc1a7c 160 0x1b,0x36,0x6c,0xd8,0xab,0x4d,0x9a,0x2f,
feb11 0:85fceccc1a7c 161 0x5e,0xbc,0x63,0xc6,0x97,0x35,0x6a,0xd4,
feb11 0:85fceccc1a7c 162 0xb3,0x7d,0xfa,0xef,0xc5,0x91,
feb11 0:85fceccc1a7c 163 };
feb11 0:85fceccc1a7c 164
feb11 0:85fceccc1a7c 165 /* ----- static functions ----- */
feb11 0:85fceccc1a7c 166 static void AES_encrypt(const AES_CTX *ctx, uint32_t *data);
feb11 0:85fceccc1a7c 167 static void AES_decrypt(const AES_CTX *ctx, uint32_t *data);
feb11 0:85fceccc1a7c 168
feb11 0:85fceccc1a7c 169 /* Perform doubling in Galois Field GF(2^8) using the irreducible polynomial
feb11 0:85fceccc1a7c 170 x^8+x^4+x^3+x+1 */
feb11 0:85fceccc1a7c 171 static unsigned char AES_xtime(uint32_t x)
feb11 0:85fceccc1a7c 172 {
feb11 0:85fceccc1a7c 173 return (x&0x80) ? (x<<1)^0x1b : x<<1;
feb11 0:85fceccc1a7c 174 }
feb11 0:85fceccc1a7c 175
feb11 0:85fceccc1a7c 176 /**
feb11 0:85fceccc1a7c 177 * Set up AES with the key/iv and cipher size.
feb11 0:85fceccc1a7c 178 */
feb11 0:85fceccc1a7c 179 void AES_set_key(AES_CTX *ctx, const uint8_t *key,
feb11 0:85fceccc1a7c 180 const uint8_t *iv, AES_MODE mode)
feb11 0:85fceccc1a7c 181 {
feb11 0:85fceccc1a7c 182 int i, ii;
feb11 0:85fceccc1a7c 183 uint32_t *W, tmp, tmp2;
feb11 0:85fceccc1a7c 184 const unsigned char *ip;
feb11 0:85fceccc1a7c 185 int words;
feb11 0:85fceccc1a7c 186
feb11 0:85fceccc1a7c 187 switch (mode)
feb11 0:85fceccc1a7c 188 {
feb11 0:85fceccc1a7c 189 case AES_MODE_128:
feb11 0:85fceccc1a7c 190 i = 10;
feb11 0:85fceccc1a7c 191 words = 4;
feb11 0:85fceccc1a7c 192 break;
feb11 0:85fceccc1a7c 193
feb11 0:85fceccc1a7c 194 case AES_MODE_256:
feb11 0:85fceccc1a7c 195 i = 14;
feb11 0:85fceccc1a7c 196 words = 8;
feb11 0:85fceccc1a7c 197 break;
feb11 0:85fceccc1a7c 198
feb11 0:85fceccc1a7c 199 default: /* fail silently */
feb11 0:85fceccc1a7c 200 return;
feb11 0:85fceccc1a7c 201 }
feb11 0:85fceccc1a7c 202
feb11 0:85fceccc1a7c 203 ctx->rounds = i;
feb11 0:85fceccc1a7c 204 ctx->key_size = words;
feb11 0:85fceccc1a7c 205 W = ctx->ks;
feb11 0:85fceccc1a7c 206 for (i = 0; i < words; i+=2)
feb11 0:85fceccc1a7c 207 {
feb11 0:85fceccc1a7c 208 W[i+0]= ((uint32_t)key[ 0]<<24)|
feb11 0:85fceccc1a7c 209 ((uint32_t)key[ 1]<<16)|
feb11 0:85fceccc1a7c 210 ((uint32_t)key[ 2]<< 8)|
feb11 0:85fceccc1a7c 211 ((uint32_t)key[ 3] );
feb11 0:85fceccc1a7c 212 W[i+1]= ((uint32_t)key[ 4]<<24)|
feb11 0:85fceccc1a7c 213 ((uint32_t)key[ 5]<<16)|
feb11 0:85fceccc1a7c 214 ((uint32_t)key[ 6]<< 8)|
feb11 0:85fceccc1a7c 215 ((uint32_t)key[ 7] );
feb11 0:85fceccc1a7c 216 key += 8;
feb11 0:85fceccc1a7c 217 }
feb11 0:85fceccc1a7c 218
feb11 0:85fceccc1a7c 219 ip = Rcon;
feb11 0:85fceccc1a7c 220 ii = 4 * (ctx->rounds+1);
feb11 0:85fceccc1a7c 221 for (i = words; i<ii; i++)
feb11 0:85fceccc1a7c 222 {
feb11 0:85fceccc1a7c 223 tmp = W[i-1];
feb11 0:85fceccc1a7c 224
feb11 0:85fceccc1a7c 225 if ((i % words) == 0)
feb11 0:85fceccc1a7c 226 {
feb11 0:85fceccc1a7c 227 tmp2 =(uint32_t)aes_sbox[(tmp )&0xff]<< 8;
feb11 0:85fceccc1a7c 228 tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<<16;
feb11 0:85fceccc1a7c 229 tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<24;
feb11 0:85fceccc1a7c 230 tmp2|=(uint32_t)aes_sbox[(tmp>>24) ];
feb11 0:85fceccc1a7c 231 tmp=tmp2^(((unsigned int)*ip)<<24);
feb11 0:85fceccc1a7c 232 ip++;
feb11 0:85fceccc1a7c 233 }
feb11 0:85fceccc1a7c 234
feb11 0:85fceccc1a7c 235 if ((words == 8) && ((i % words) == 4))
feb11 0:85fceccc1a7c 236 {
feb11 0:85fceccc1a7c 237 tmp2 =(uint32_t)aes_sbox[(tmp )&0xff] ;
feb11 0:85fceccc1a7c 238 tmp2|=(uint32_t)aes_sbox[(tmp>> 8)&0xff]<< 8;
feb11 0:85fceccc1a7c 239 tmp2|=(uint32_t)aes_sbox[(tmp>>16)&0xff]<<16;
feb11 0:85fceccc1a7c 240 tmp2|=(uint32_t)aes_sbox[(tmp>>24) ]<<24;
feb11 0:85fceccc1a7c 241 tmp=tmp2;
feb11 0:85fceccc1a7c 242 }
feb11 0:85fceccc1a7c 243
feb11 0:85fceccc1a7c 244 W[i]=W[i-words]^tmp;
feb11 0:85fceccc1a7c 245 }
feb11 0:85fceccc1a7c 246
feb11 0:85fceccc1a7c 247 /* copy the iv across */
feb11 0:85fceccc1a7c 248 memcpy(ctx->iv, iv, 16);
feb11 0:85fceccc1a7c 249 }
feb11 0:85fceccc1a7c 250
feb11 0:85fceccc1a7c 251 /**
feb11 0:85fceccc1a7c 252 * Change a key for decryption.
feb11 0:85fceccc1a7c 253 */
feb11 0:85fceccc1a7c 254 void AES_convert_key(AES_CTX *ctx)
feb11 0:85fceccc1a7c 255 {
feb11 0:85fceccc1a7c 256 int i;
feb11 0:85fceccc1a7c 257 uint32_t *k,w,t1,t2,t3,t4;
feb11 0:85fceccc1a7c 258
feb11 0:85fceccc1a7c 259 k = ctx->ks;
feb11 0:85fceccc1a7c 260 k += 4;
feb11 0:85fceccc1a7c 261
feb11 0:85fceccc1a7c 262 for (i= ctx->rounds*4; i > 4; i--)
feb11 0:85fceccc1a7c 263 {
feb11 0:85fceccc1a7c 264 w= *k;
feb11 0:85fceccc1a7c 265 w = inv_mix_col(w,t1,t2,t3,t4);
feb11 0:85fceccc1a7c 266 *k++ =w;
feb11 0:85fceccc1a7c 267 }
feb11 0:85fceccc1a7c 268 }
feb11 0:85fceccc1a7c 269
feb11 0:85fceccc1a7c 270 /**
feb11 0:85fceccc1a7c 271 * Encrypt a byte sequence (with a block size 16) using the AES cipher.
feb11 0:85fceccc1a7c 272 */
feb11 0:85fceccc1a7c 273 void AES_cbc_encrypt(AES_CTX *ctx, const uint8_t *msg, uint8_t *out, int length)
feb11 0:85fceccc1a7c 274 {
feb11 0:85fceccc1a7c 275 int i;
feb11 0:85fceccc1a7c 276 uint32_t tin[4], tout[4], iv[4];
feb11 0:85fceccc1a7c 277
feb11 0:85fceccc1a7c 278 memcpy(iv, ctx->iv, AES_IV_SIZE);
feb11 0:85fceccc1a7c 279 for (i = 0; i < 4; i++)
feb11 0:85fceccc1a7c 280 tout[i] = ntohl(iv[i]);
feb11 0:85fceccc1a7c 281
feb11 0:85fceccc1a7c 282 for (length -= AES_BLOCKSIZE; length >= 0; length -= AES_BLOCKSIZE)
feb11 0:85fceccc1a7c 283 {
feb11 0:85fceccc1a7c 284 uint32_t msg_32[4];
feb11 0:85fceccc1a7c 285 uint32_t out_32[4];
feb11 0:85fceccc1a7c 286 memcpy(msg_32, msg, AES_BLOCKSIZE);
feb11 0:85fceccc1a7c 287 msg += AES_BLOCKSIZE;
feb11 0:85fceccc1a7c 288
feb11 0:85fceccc1a7c 289 for (i = 0; i < 4; i++)
feb11 0:85fceccc1a7c 290 tin[i] = ntohl(msg_32[i])^tout[i];
feb11 0:85fceccc1a7c 291
feb11 0:85fceccc1a7c 292 AES_encrypt(ctx, tin);
feb11 0:85fceccc1a7c 293
feb11 0:85fceccc1a7c 294 for (i = 0; i < 4; i++)
feb11 0:85fceccc1a7c 295 {
feb11 0:85fceccc1a7c 296 tout[i] = tin[i];
feb11 0:85fceccc1a7c 297 out_32[i] = htonl(tout[i]);
feb11 0:85fceccc1a7c 298 }
feb11 0:85fceccc1a7c 299
feb11 0:85fceccc1a7c 300 memcpy(out, out_32, AES_BLOCKSIZE);
feb11 0:85fceccc1a7c 301 out += AES_BLOCKSIZE;
feb11 0:85fceccc1a7c 302 }
feb11 0:85fceccc1a7c 303
feb11 0:85fceccc1a7c 304 for (i = 0; i < 4; i++)
feb11 0:85fceccc1a7c 305 iv[i] = htonl(tout[i]);
feb11 0:85fceccc1a7c 306 memcpy(ctx->iv, iv, AES_IV_SIZE);
feb11 0:85fceccc1a7c 307 }
feb11 0:85fceccc1a7c 308
feb11 0:85fceccc1a7c 309 /**
feb11 0:85fceccc1a7c 310 * Decrypt a byte sequence (with a block size 16) using the AES cipher.
feb11 0:85fceccc1a7c 311 */
feb11 0:85fceccc1a7c 312 void AES_cbc_decrypt(AES_CTX *ctx, const uint8_t *msg, uint8_t *out, int length)
feb11 0:85fceccc1a7c 313 {
feb11 0:85fceccc1a7c 314 int i;
feb11 0:85fceccc1a7c 315 uint32_t tin[4], xor[4], tout[4], data[4], iv[4];
feb11 0:85fceccc1a7c 316
feb11 0:85fceccc1a7c 317 memcpy(iv, ctx->iv, AES_IV_SIZE);
feb11 0:85fceccc1a7c 318 for (i = 0; i < 4; i++)
feb11 0:85fceccc1a7c 319 xor[i] = ntohl(iv[i]);
feb11 0:85fceccc1a7c 320 for (length -= 16; length >= 0; length -= 16)
feb11 0:85fceccc1a7c 321 {
feb11 0:85fceccc1a7c 322 uint32_t msg_32[4];
feb11 0:85fceccc1a7c 323 uint32_t out_32[4];
feb11 0:85fceccc1a7c 324 memcpy(msg_32, msg, AES_BLOCKSIZE);
feb11 0:85fceccc1a7c 325 msg += AES_BLOCKSIZE;
feb11 0:85fceccc1a7c 326
feb11 0:85fceccc1a7c 327 for (i = 0; i < 4; i++)
feb11 0:85fceccc1a7c 328 {
feb11 0:85fceccc1a7c 329 tin[i] = ntohl(msg_32[i]);
feb11 0:85fceccc1a7c 330 data[i] = tin[i];
feb11 0:85fceccc1a7c 331 }
feb11 0:85fceccc1a7c 332
feb11 0:85fceccc1a7c 333 AES_decrypt(ctx, data);
feb11 0:85fceccc1a7c 334
feb11 0:85fceccc1a7c 335 for (i = 0; i < 4; i++)
feb11 0:85fceccc1a7c 336 {
feb11 0:85fceccc1a7c 337 tout[i] = data[i]^xor[i];
feb11 0:85fceccc1a7c 338 xor[i] = tin[i];
feb11 0:85fceccc1a7c 339 out_32[i] = htonl(tout[i]);
feb11 0:85fceccc1a7c 340 }
feb11 0:85fceccc1a7c 341
feb11 0:85fceccc1a7c 342 memcpy(out, out_32, AES_BLOCKSIZE);
feb11 0:85fceccc1a7c 343 out += AES_BLOCKSIZE;
feb11 0:85fceccc1a7c 344
feb11 0:85fceccc1a7c 345 }
feb11 0:85fceccc1a7c 346
feb11 0:85fceccc1a7c 347 for (i = 0; i < 4; i++)
feb11 0:85fceccc1a7c 348 iv[i] = htonl(xor[i]);
feb11 0:85fceccc1a7c 349 memcpy(ctx->iv, iv, AES_IV_SIZE);
feb11 0:85fceccc1a7c 350
feb11 0:85fceccc1a7c 351 }
feb11 0:85fceccc1a7c 352
feb11 0:85fceccc1a7c 353 /**
feb11 0:85fceccc1a7c 354 * Encrypt a single block (16 bytes) of data
feb11 0:85fceccc1a7c 355 */
feb11 0:85fceccc1a7c 356 static void AES_encrypt(const AES_CTX *ctx, uint32_t *data)
feb11 0:85fceccc1a7c 357 {
feb11 0:85fceccc1a7c 358 /* To make this code smaller, generate the sbox entries on the fly.
feb11 0:85fceccc1a7c 359 * This will have a really heavy effect upon performance.
feb11 0:85fceccc1a7c 360 */
feb11 0:85fceccc1a7c 361 uint32_t tmp[4];
feb11 0:85fceccc1a7c 362 uint32_t tmp1, old_a0, a0, a1, a2, a3, row;
feb11 0:85fceccc1a7c 363 int curr_rnd;
feb11 0:85fceccc1a7c 364 int rounds = ctx->rounds;
feb11 0:85fceccc1a7c 365 const uint32_t *k = ctx->ks;
feb11 0:85fceccc1a7c 366
feb11 0:85fceccc1a7c 367 /* Pre-round key addition */
feb11 0:85fceccc1a7c 368 for (row = 0; row < 4; row++)
feb11 0:85fceccc1a7c 369 data[row] ^= *(k++);
feb11 0:85fceccc1a7c 370
feb11 0:85fceccc1a7c 371 /* Encrypt one block. */
feb11 0:85fceccc1a7c 372 for (curr_rnd = 0; curr_rnd < rounds; curr_rnd++)
feb11 0:85fceccc1a7c 373 {
feb11 0:85fceccc1a7c 374 /* Perform ByteSub and ShiftRow operations together */
feb11 0:85fceccc1a7c 375 for (row = 0; row < 4; row++)
feb11 0:85fceccc1a7c 376 {
feb11 0:85fceccc1a7c 377 a0 = (uint32_t)aes_sbox[(data[row%4]>>24)&0xFF];
feb11 0:85fceccc1a7c 378 a1 = (uint32_t)aes_sbox[(data[(row+1)%4]>>16)&0xFF];
feb11 0:85fceccc1a7c 379 a2 = (uint32_t)aes_sbox[(data[(row+2)%4]>>8)&0xFF];
feb11 0:85fceccc1a7c 380 a3 = (uint32_t)aes_sbox[(data[(row+3)%4])&0xFF];
feb11 0:85fceccc1a7c 381
feb11 0:85fceccc1a7c 382 /* Perform MixColumn iff not last round */
feb11 0:85fceccc1a7c 383 if (curr_rnd < (rounds - 1))
feb11 0:85fceccc1a7c 384 {
feb11 0:85fceccc1a7c 385 tmp1 = a0 ^ a1 ^ a2 ^ a3;
feb11 0:85fceccc1a7c 386 old_a0 = a0;
feb11 0:85fceccc1a7c 387 a0 ^= tmp1 ^ AES_xtime(a0 ^ a1);
feb11 0:85fceccc1a7c 388 a1 ^= tmp1 ^ AES_xtime(a1 ^ a2);
feb11 0:85fceccc1a7c 389 a2 ^= tmp1 ^ AES_xtime(a2 ^ a3);
feb11 0:85fceccc1a7c 390 a3 ^= tmp1 ^ AES_xtime(a3 ^ old_a0);
feb11 0:85fceccc1a7c 391 }
feb11 0:85fceccc1a7c 392
feb11 0:85fceccc1a7c 393 tmp[row] = ((a0 << 24) | (a1 << 16) | (a2 << 8) | a3);
feb11 0:85fceccc1a7c 394 }
feb11 0:85fceccc1a7c 395
feb11 0:85fceccc1a7c 396 /* KeyAddition - note that it is vital that this loop is separate from
feb11 0:85fceccc1a7c 397 the MixColumn operation, which must be atomic...*/
feb11 0:85fceccc1a7c 398 for (row = 0; row < 4; row++)
feb11 0:85fceccc1a7c 399 data[row] = tmp[row] ^ *(k++);
feb11 0:85fceccc1a7c 400 }
feb11 0:85fceccc1a7c 401 }
feb11 0:85fceccc1a7c 402
feb11 0:85fceccc1a7c 403 /**
feb11 0:85fceccc1a7c 404 * Decrypt a single block (16 bytes) of data
feb11 0:85fceccc1a7c 405 */
feb11 0:85fceccc1a7c 406 static void AES_decrypt(const AES_CTX *ctx, uint32_t *data)
feb11 0:85fceccc1a7c 407 {
feb11 0:85fceccc1a7c 408 uint32_t tmp[4];
feb11 0:85fceccc1a7c 409 uint32_t xt0,xt1,xt2,xt3,xt4,xt5,xt6;
feb11 0:85fceccc1a7c 410 uint32_t a0, a1, a2, a3, row;
feb11 0:85fceccc1a7c 411 int curr_rnd;
feb11 0:85fceccc1a7c 412 int rounds = ctx->rounds;
feb11 0:85fceccc1a7c 413 const uint32_t *k = ctx->ks + ((rounds+1)*4);
feb11 0:85fceccc1a7c 414 /* pre-round key addition */
feb11 0:85fceccc1a7c 415 for (row=4; row > 0;row--)
feb11 0:85fceccc1a7c 416 data[row-1] ^= *(--k);
feb11 0:85fceccc1a7c 417
feb11 0:85fceccc1a7c 418 /* Decrypt one block */
feb11 0:85fceccc1a7c 419 for (curr_rnd = 0; curr_rnd < rounds; curr_rnd++)
feb11 0:85fceccc1a7c 420 {
feb11 0:85fceccc1a7c 421
feb11 0:85fceccc1a7c 422 /* Perform ByteSub and ShiftRow operations together */
feb11 0:85fceccc1a7c 423 for (row = 4; row > 0; row--)
feb11 0:85fceccc1a7c 424 {
feb11 0:85fceccc1a7c 425
feb11 0:85fceccc1a7c 426 a0 = aes_isbox[(data[(row+3)%4]>>24)&0xFF];
feb11 0:85fceccc1a7c 427 a1 = aes_isbox[(data[(row+2)%4]>>16)&0xFF];
feb11 0:85fceccc1a7c 428 a2 = aes_isbox[(data[(row+1)%4]>>8)&0xFF];
feb11 0:85fceccc1a7c 429 a3 = aes_isbox[(data[row%4])&0xFF];
feb11 0:85fceccc1a7c 430
feb11 0:85fceccc1a7c 431 /* Perform MixColumn iff not last round */
feb11 0:85fceccc1a7c 432 if (curr_rnd<(rounds-1))
feb11 0:85fceccc1a7c 433 {
feb11 0:85fceccc1a7c 434 /* The MDS cofefficients (0x09, 0x0B, 0x0D, 0x0E)
feb11 0:85fceccc1a7c 435 are quite large compared to encryption; this
feb11 0:85fceccc1a7c 436 operation slows decryption down noticeably. */
feb11 0:85fceccc1a7c 437 xt0 = AES_xtime(a0^a1);
feb11 0:85fceccc1a7c 438 xt1 = AES_xtime(a1^a2);
feb11 0:85fceccc1a7c 439 xt2 = AES_xtime(a2^a3);
feb11 0:85fceccc1a7c 440 xt3 = AES_xtime(a3^a0);
feb11 0:85fceccc1a7c 441 xt4 = AES_xtime(xt0^xt1);
feb11 0:85fceccc1a7c 442 xt5 = AES_xtime(xt1^xt2);
feb11 0:85fceccc1a7c 443 xt6 = AES_xtime(xt4^xt5);
feb11 0:85fceccc1a7c 444
feb11 0:85fceccc1a7c 445 xt0 ^= a1^a2^a3^xt4^xt6;
feb11 0:85fceccc1a7c 446 xt1 ^= a0^a2^a3^xt5^xt6;
feb11 0:85fceccc1a7c 447 xt2 ^= a0^a1^a3^xt4^xt6;
feb11 0:85fceccc1a7c 448 xt3 ^= a0^a1^a2^xt5^xt6;
feb11 0:85fceccc1a7c 449 tmp[row-1] = ((xt0<<24)|(xt1<<16)|(xt2<<8)|xt3);
feb11 0:85fceccc1a7c 450 }
feb11 0:85fceccc1a7c 451 else
feb11 0:85fceccc1a7c 452 tmp[row-1] = ((a0<<24)|(a1<<16)|(a2<<8)|a3);
feb11 0:85fceccc1a7c 453 }
feb11 0:85fceccc1a7c 454
feb11 0:85fceccc1a7c 455 for (row = 4; row > 0; row--)
feb11 0:85fceccc1a7c 456 data[row-1] = tmp[row-1] ^ *(--k);
feb11 0:85fceccc1a7c 457 }
feb11 0:85fceccc1a7c 458 }
feb11 0:85fceccc1a7c 459
feb11 0:85fceccc1a7c 460 #endif
feb11 0:85fceccc1a7c 461
feb11 0:85fceccc1a7c 462