lora experiments

Dependencies:   BLE_API LoRaWAN-lib SX1276Lib mbed nRF51822 HCSR04Lib

Fork of LoRa by Olav Nymoen

Committer:
haaspors
Date:
Thu Jun 09 14:42:23 2016 +0000
Revision:
4:63d6744a61b6
Parent:
0:4c1fcbfcc7bf
Add 'device joined lora network' gatt char.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
olav 0:4c1fcbfcc7bf 1 /*
olav 0:4c1fcbfcc7bf 2 ---------------------------------------------------------------------------
olav 0:4c1fcbfcc7bf 3 Copyright (c) 1998-2008, Brian Gladman, Worcester, UK. All rights reserved.
olav 0:4c1fcbfcc7bf 4
olav 0:4c1fcbfcc7bf 5 LICENSE TERMS
olav 0:4c1fcbfcc7bf 6
olav 0:4c1fcbfcc7bf 7 The redistribution and use of this software (with or without changes)
olav 0:4c1fcbfcc7bf 8 is allowed without the payment of fees or royalties provided that:
olav 0:4c1fcbfcc7bf 9
olav 0:4c1fcbfcc7bf 10 1. source code distributions include the above copyright notice, this
olav 0:4c1fcbfcc7bf 11 list of conditions and the following disclaimer;
olav 0:4c1fcbfcc7bf 12
olav 0:4c1fcbfcc7bf 13 2. binary distributions include the above copyright notice, this list
olav 0:4c1fcbfcc7bf 14 of conditions and the following disclaimer in their documentation;
olav 0:4c1fcbfcc7bf 15
olav 0:4c1fcbfcc7bf 16 3. the name of the copyright holder is not used to endorse products
olav 0:4c1fcbfcc7bf 17 built using this software without specific written permission.
olav 0:4c1fcbfcc7bf 18
olav 0:4c1fcbfcc7bf 19 DISCLAIMER
olav 0:4c1fcbfcc7bf 20
olav 0:4c1fcbfcc7bf 21 This software is provided 'as is' with no explicit or implied warranties
olav 0:4c1fcbfcc7bf 22 in respect of its properties, including, but not limited to, correctness
olav 0:4c1fcbfcc7bf 23 and/or fitness for purpose.
olav 0:4c1fcbfcc7bf 24 ---------------------------------------------------------------------------
olav 0:4c1fcbfcc7bf 25 Issue 09/09/2006
olav 0:4c1fcbfcc7bf 26
olav 0:4c1fcbfcc7bf 27 This is an AES implementation that uses only 8-bit byte operations on the
olav 0:4c1fcbfcc7bf 28 cipher state (there are options to use 32-bit types if available).
olav 0:4c1fcbfcc7bf 29
olav 0:4c1fcbfcc7bf 30 The combination of mix columns and byte substitution used here is based on
olav 0:4c1fcbfcc7bf 31 that developed by Karl Malbrain. His contribution is acknowledged.
olav 0:4c1fcbfcc7bf 32 */
olav 0:4c1fcbfcc7bf 33
olav 0:4c1fcbfcc7bf 34 /* define if you have a fast memcpy function on your system */
olav 0:4c1fcbfcc7bf 35 #if 0
olav 0:4c1fcbfcc7bf 36 # define HAVE_MEMCPY
olav 0:4c1fcbfcc7bf 37 # include <string.h>
olav 0:4c1fcbfcc7bf 38 # if defined( _MSC_VER )
olav 0:4c1fcbfcc7bf 39 # include <intrin.h>
olav 0:4c1fcbfcc7bf 40 # pragma intrinsic( memcpy )
olav 0:4c1fcbfcc7bf 41 # endif
olav 0:4c1fcbfcc7bf 42 #endif
olav 0:4c1fcbfcc7bf 43
olav 0:4c1fcbfcc7bf 44 #include <stdio.h>
olav 0:4c1fcbfcc7bf 45 #include <stdlib.h>
olav 0:4c1fcbfcc7bf 46 #include <stdint.h>
olav 0:4c1fcbfcc7bf 47
olav 0:4c1fcbfcc7bf 48 /* define if you have fast 32-bit types on your system */
olav 0:4c1fcbfcc7bf 49 #if 0
olav 0:4c1fcbfcc7bf 50 # define HAVE_UINT_32T
olav 0:4c1fcbfcc7bf 51 #endif
olav 0:4c1fcbfcc7bf 52
olav 0:4c1fcbfcc7bf 53 /* define if you don't want any tables */
olav 0:4c1fcbfcc7bf 54 #if 1
olav 0:4c1fcbfcc7bf 55 # define USE_TABLES
olav 0:4c1fcbfcc7bf 56 #endif
olav 0:4c1fcbfcc7bf 57
olav 0:4c1fcbfcc7bf 58 /* On Intel Core 2 duo VERSION_1 is faster */
olav 0:4c1fcbfcc7bf 59
olav 0:4c1fcbfcc7bf 60 /* alternative versions (test for performance on your system) */
olav 0:4c1fcbfcc7bf 61 #if 1
olav 0:4c1fcbfcc7bf 62 # define VERSION_1
olav 0:4c1fcbfcc7bf 63 #endif
olav 0:4c1fcbfcc7bf 64
olav 0:4c1fcbfcc7bf 65 #include "aes.h"
olav 0:4c1fcbfcc7bf 66
olav 0:4c1fcbfcc7bf 67 //#if defined( HAVE_UINT_32T )
olav 0:4c1fcbfcc7bf 68 // typedef unsigned long uint32_t;
olav 0:4c1fcbfcc7bf 69 //#endif
olav 0:4c1fcbfcc7bf 70
olav 0:4c1fcbfcc7bf 71 /* functions for finite field multiplication in the AES Galois field */
olav 0:4c1fcbfcc7bf 72
olav 0:4c1fcbfcc7bf 73 #define WPOLY 0x011b
olav 0:4c1fcbfcc7bf 74 #define BPOLY 0x1b
olav 0:4c1fcbfcc7bf 75 #define DPOLY 0x008d
olav 0:4c1fcbfcc7bf 76
olav 0:4c1fcbfcc7bf 77 #define f1(x) (x)
olav 0:4c1fcbfcc7bf 78 #define f2(x) ((x << 1) ^ (((x >> 7) & 1) * WPOLY))
olav 0:4c1fcbfcc7bf 79 #define f4(x) ((x << 2) ^ (((x >> 6) & 1) * WPOLY) ^ (((x >> 6) & 2) * WPOLY))
olav 0:4c1fcbfcc7bf 80 #define f8(x) ((x << 3) ^ (((x >> 5) & 1) * WPOLY) ^ (((x >> 5) & 2) * WPOLY) \
olav 0:4c1fcbfcc7bf 81 ^ (((x >> 5) & 4) * WPOLY))
olav 0:4c1fcbfcc7bf 82 #define d2(x) (((x) >> 1) ^ ((x) & 1 ? DPOLY : 0))
olav 0:4c1fcbfcc7bf 83
olav 0:4c1fcbfcc7bf 84 #define f3(x) (f2(x) ^ x)
olav 0:4c1fcbfcc7bf 85 #define f9(x) (f8(x) ^ x)
olav 0:4c1fcbfcc7bf 86 #define fb(x) (f8(x) ^ f2(x) ^ x)
olav 0:4c1fcbfcc7bf 87 #define fd(x) (f8(x) ^ f4(x) ^ x)
olav 0:4c1fcbfcc7bf 88 #define fe(x) (f8(x) ^ f4(x) ^ f2(x))
olav 0:4c1fcbfcc7bf 89
olav 0:4c1fcbfcc7bf 90 #if defined( USE_TABLES )
olav 0:4c1fcbfcc7bf 91
olav 0:4c1fcbfcc7bf 92 #define sb_data(w) { /* S Box data values */ \
olav 0:4c1fcbfcc7bf 93 w(0x63), w(0x7c), w(0x77), w(0x7b), w(0xf2), w(0x6b), w(0x6f), w(0xc5),\
olav 0:4c1fcbfcc7bf 94 w(0x30), w(0x01), w(0x67), w(0x2b), w(0xfe), w(0xd7), w(0xab), w(0x76),\
olav 0:4c1fcbfcc7bf 95 w(0xca), w(0x82), w(0xc9), w(0x7d), w(0xfa), w(0x59), w(0x47), w(0xf0),\
olav 0:4c1fcbfcc7bf 96 w(0xad), w(0xd4), w(0xa2), w(0xaf), w(0x9c), w(0xa4), w(0x72), w(0xc0),\
olav 0:4c1fcbfcc7bf 97 w(0xb7), w(0xfd), w(0x93), w(0x26), w(0x36), w(0x3f), w(0xf7), w(0xcc),\
olav 0:4c1fcbfcc7bf 98 w(0x34), w(0xa5), w(0xe5), w(0xf1), w(0x71), w(0xd8), w(0x31), w(0x15),\
olav 0:4c1fcbfcc7bf 99 w(0x04), w(0xc7), w(0x23), w(0xc3), w(0x18), w(0x96), w(0x05), w(0x9a),\
olav 0:4c1fcbfcc7bf 100 w(0x07), w(0x12), w(0x80), w(0xe2), w(0xeb), w(0x27), w(0xb2), w(0x75),\
olav 0:4c1fcbfcc7bf 101 w(0x09), w(0x83), w(0x2c), w(0x1a), w(0x1b), w(0x6e), w(0x5a), w(0xa0),\
olav 0:4c1fcbfcc7bf 102 w(0x52), w(0x3b), w(0xd6), w(0xb3), w(0x29), w(0xe3), w(0x2f), w(0x84),\
olav 0:4c1fcbfcc7bf 103 w(0x53), w(0xd1), w(0x00), w(0xed), w(0x20), w(0xfc), w(0xb1), w(0x5b),\
olav 0:4c1fcbfcc7bf 104 w(0x6a), w(0xcb), w(0xbe), w(0x39), w(0x4a), w(0x4c), w(0x58), w(0xcf),\
olav 0:4c1fcbfcc7bf 105 w(0xd0), w(0xef), w(0xaa), w(0xfb), w(0x43), w(0x4d), w(0x33), w(0x85),\
olav 0:4c1fcbfcc7bf 106 w(0x45), w(0xf9), w(0x02), w(0x7f), w(0x50), w(0x3c), w(0x9f), w(0xa8),\
olav 0:4c1fcbfcc7bf 107 w(0x51), w(0xa3), w(0x40), w(0x8f), w(0x92), w(0x9d), w(0x38), w(0xf5),\
olav 0:4c1fcbfcc7bf 108 w(0xbc), w(0xb6), w(0xda), w(0x21), w(0x10), w(0xff), w(0xf3), w(0xd2),\
olav 0:4c1fcbfcc7bf 109 w(0xcd), w(0x0c), w(0x13), w(0xec), w(0x5f), w(0x97), w(0x44), w(0x17),\
olav 0:4c1fcbfcc7bf 110 w(0xc4), w(0xa7), w(0x7e), w(0x3d), w(0x64), w(0x5d), w(0x19), w(0x73),\
olav 0:4c1fcbfcc7bf 111 w(0x60), w(0x81), w(0x4f), w(0xdc), w(0x22), w(0x2a), w(0x90), w(0x88),\
olav 0:4c1fcbfcc7bf 112 w(0x46), w(0xee), w(0xb8), w(0x14), w(0xde), w(0x5e), w(0x0b), w(0xdb),\
olav 0:4c1fcbfcc7bf 113 w(0xe0), w(0x32), w(0x3a), w(0x0a), w(0x49), w(0x06), w(0x24), w(0x5c),\
olav 0:4c1fcbfcc7bf 114 w(0xc2), w(0xd3), w(0xac), w(0x62), w(0x91), w(0x95), w(0xe4), w(0x79),\
olav 0:4c1fcbfcc7bf 115 w(0xe7), w(0xc8), w(0x37), w(0x6d), w(0x8d), w(0xd5), w(0x4e), w(0xa9),\
olav 0:4c1fcbfcc7bf 116 w(0x6c), w(0x56), w(0xf4), w(0xea), w(0x65), w(0x7a), w(0xae), w(0x08),\
olav 0:4c1fcbfcc7bf 117 w(0xba), w(0x78), w(0x25), w(0x2e), w(0x1c), w(0xa6), w(0xb4), w(0xc6),\
olav 0:4c1fcbfcc7bf 118 w(0xe8), w(0xdd), w(0x74), w(0x1f), w(0x4b), w(0xbd), w(0x8b), w(0x8a),\
olav 0:4c1fcbfcc7bf 119 w(0x70), w(0x3e), w(0xb5), w(0x66), w(0x48), w(0x03), w(0xf6), w(0x0e),\
olav 0:4c1fcbfcc7bf 120 w(0x61), w(0x35), w(0x57), w(0xb9), w(0x86), w(0xc1), w(0x1d), w(0x9e),\
olav 0:4c1fcbfcc7bf 121 w(0xe1), w(0xf8), w(0x98), w(0x11), w(0x69), w(0xd9), w(0x8e), w(0x94),\
olav 0:4c1fcbfcc7bf 122 w(0x9b), w(0x1e), w(0x87), w(0xe9), w(0xce), w(0x55), w(0x28), w(0xdf),\
olav 0:4c1fcbfcc7bf 123 w(0x8c), w(0xa1), w(0x89), w(0x0d), w(0xbf), w(0xe6), w(0x42), w(0x68),\
olav 0:4c1fcbfcc7bf 124 w(0x41), w(0x99), w(0x2d), w(0x0f), w(0xb0), w(0x54), w(0xbb), w(0x16) }
olav 0:4c1fcbfcc7bf 125
olav 0:4c1fcbfcc7bf 126 #define isb_data(w) { /* inverse S Box data values */ \
olav 0:4c1fcbfcc7bf 127 w(0x52), w(0x09), w(0x6a), w(0xd5), w(0x30), w(0x36), w(0xa5), w(0x38),\
olav 0:4c1fcbfcc7bf 128 w(0xbf), w(0x40), w(0xa3), w(0x9e), w(0x81), w(0xf3), w(0xd7), w(0xfb),\
olav 0:4c1fcbfcc7bf 129 w(0x7c), w(0xe3), w(0x39), w(0x82), w(0x9b), w(0x2f), w(0xff), w(0x87),\
olav 0:4c1fcbfcc7bf 130 w(0x34), w(0x8e), w(0x43), w(0x44), w(0xc4), w(0xde), w(0xe9), w(0xcb),\
olav 0:4c1fcbfcc7bf 131 w(0x54), w(0x7b), w(0x94), w(0x32), w(0xa6), w(0xc2), w(0x23), w(0x3d),\
olav 0:4c1fcbfcc7bf 132 w(0xee), w(0x4c), w(0x95), w(0x0b), w(0x42), w(0xfa), w(0xc3), w(0x4e),\
olav 0:4c1fcbfcc7bf 133 w(0x08), w(0x2e), w(0xa1), w(0x66), w(0x28), w(0xd9), w(0x24), w(0xb2),\
olav 0:4c1fcbfcc7bf 134 w(0x76), w(0x5b), w(0xa2), w(0x49), w(0x6d), w(0x8b), w(0xd1), w(0x25),\
olav 0:4c1fcbfcc7bf 135 w(0x72), w(0xf8), w(0xf6), w(0x64), w(0x86), w(0x68), w(0x98), w(0x16),\
olav 0:4c1fcbfcc7bf 136 w(0xd4), w(0xa4), w(0x5c), w(0xcc), w(0x5d), w(0x65), w(0xb6), w(0x92),\
olav 0:4c1fcbfcc7bf 137 w(0x6c), w(0x70), w(0x48), w(0x50), w(0xfd), w(0xed), w(0xb9), w(0xda),\
olav 0:4c1fcbfcc7bf 138 w(0x5e), w(0x15), w(0x46), w(0x57), w(0xa7), w(0x8d), w(0x9d), w(0x84),\
olav 0:4c1fcbfcc7bf 139 w(0x90), w(0xd8), w(0xab), w(0x00), w(0x8c), w(0xbc), w(0xd3), w(0x0a),\
olav 0:4c1fcbfcc7bf 140 w(0xf7), w(0xe4), w(0x58), w(0x05), w(0xb8), w(0xb3), w(0x45), w(0x06),\
olav 0:4c1fcbfcc7bf 141 w(0xd0), w(0x2c), w(0x1e), w(0x8f), w(0xca), w(0x3f), w(0x0f), w(0x02),\
olav 0:4c1fcbfcc7bf 142 w(0xc1), w(0xaf), w(0xbd), w(0x03), w(0x01), w(0x13), w(0x8a), w(0x6b),\
olav 0:4c1fcbfcc7bf 143 w(0x3a), w(0x91), w(0x11), w(0x41), w(0x4f), w(0x67), w(0xdc), w(0xea),\
olav 0:4c1fcbfcc7bf 144 w(0x97), w(0xf2), w(0xcf), w(0xce), w(0xf0), w(0xb4), w(0xe6), w(0x73),\
olav 0:4c1fcbfcc7bf 145 w(0x96), w(0xac), w(0x74), w(0x22), w(0xe7), w(0xad), w(0x35), w(0x85),\
olav 0:4c1fcbfcc7bf 146 w(0xe2), w(0xf9), w(0x37), w(0xe8), w(0x1c), w(0x75), w(0xdf), w(0x6e),\
olav 0:4c1fcbfcc7bf 147 w(0x47), w(0xf1), w(0x1a), w(0x71), w(0x1d), w(0x29), w(0xc5), w(0x89),\
olav 0:4c1fcbfcc7bf 148 w(0x6f), w(0xb7), w(0x62), w(0x0e), w(0xaa), w(0x18), w(0xbe), w(0x1b),\
olav 0:4c1fcbfcc7bf 149 w(0xfc), w(0x56), w(0x3e), w(0x4b), w(0xc6), w(0xd2), w(0x79), w(0x20),\
olav 0:4c1fcbfcc7bf 150 w(0x9a), w(0xdb), w(0xc0), w(0xfe), w(0x78), w(0xcd), w(0x5a), w(0xf4),\
olav 0:4c1fcbfcc7bf 151 w(0x1f), w(0xdd), w(0xa8), w(0x33), w(0x88), w(0x07), w(0xc7), w(0x31),\
olav 0:4c1fcbfcc7bf 152 w(0xb1), w(0x12), w(0x10), w(0x59), w(0x27), w(0x80), w(0xec), w(0x5f),\
olav 0:4c1fcbfcc7bf 153 w(0x60), w(0x51), w(0x7f), w(0xa9), w(0x19), w(0xb5), w(0x4a), w(0x0d),\
olav 0:4c1fcbfcc7bf 154 w(0x2d), w(0xe5), w(0x7a), w(0x9f), w(0x93), w(0xc9), w(0x9c), w(0xef),\
olav 0:4c1fcbfcc7bf 155 w(0xa0), w(0xe0), w(0x3b), w(0x4d), w(0xae), w(0x2a), w(0xf5), w(0xb0),\
olav 0:4c1fcbfcc7bf 156 w(0xc8), w(0xeb), w(0xbb), w(0x3c), w(0x83), w(0x53), w(0x99), w(0x61),\
olav 0:4c1fcbfcc7bf 157 w(0x17), w(0x2b), w(0x04), w(0x7e), w(0xba), w(0x77), w(0xd6), w(0x26),\
olav 0:4c1fcbfcc7bf 158 w(0xe1), w(0x69), w(0x14), w(0x63), w(0x55), w(0x21), w(0x0c), w(0x7d) }
olav 0:4c1fcbfcc7bf 159
olav 0:4c1fcbfcc7bf 160 #define mm_data(w) { /* basic data for forming finite field tables */ \
olav 0:4c1fcbfcc7bf 161 w(0x00), w(0x01), w(0x02), w(0x03), w(0x04), w(0x05), w(0x06), w(0x07),\
olav 0:4c1fcbfcc7bf 162 w(0x08), w(0x09), w(0x0a), w(0x0b), w(0x0c), w(0x0d), w(0x0e), w(0x0f),\
olav 0:4c1fcbfcc7bf 163 w(0x10), w(0x11), w(0x12), w(0x13), w(0x14), w(0x15), w(0x16), w(0x17),\
olav 0:4c1fcbfcc7bf 164 w(0x18), w(0x19), w(0x1a), w(0x1b), w(0x1c), w(0x1d), w(0x1e), w(0x1f),\
olav 0:4c1fcbfcc7bf 165 w(0x20), w(0x21), w(0x22), w(0x23), w(0x24), w(0x25), w(0x26), w(0x27),\
olav 0:4c1fcbfcc7bf 166 w(0x28), w(0x29), w(0x2a), w(0x2b), w(0x2c), w(0x2d), w(0x2e), w(0x2f),\
olav 0:4c1fcbfcc7bf 167 w(0x30), w(0x31), w(0x32), w(0x33), w(0x34), w(0x35), w(0x36), w(0x37),\
olav 0:4c1fcbfcc7bf 168 w(0x38), w(0x39), w(0x3a), w(0x3b), w(0x3c), w(0x3d), w(0x3e), w(0x3f),\
olav 0:4c1fcbfcc7bf 169 w(0x40), w(0x41), w(0x42), w(0x43), w(0x44), w(0x45), w(0x46), w(0x47),\
olav 0:4c1fcbfcc7bf 170 w(0x48), w(0x49), w(0x4a), w(0x4b), w(0x4c), w(0x4d), w(0x4e), w(0x4f),\
olav 0:4c1fcbfcc7bf 171 w(0x50), w(0x51), w(0x52), w(0x53), w(0x54), w(0x55), w(0x56), w(0x57),\
olav 0:4c1fcbfcc7bf 172 w(0x58), w(0x59), w(0x5a), w(0x5b), w(0x5c), w(0x5d), w(0x5e), w(0x5f),\
olav 0:4c1fcbfcc7bf 173 w(0x60), w(0x61), w(0x62), w(0x63), w(0x64), w(0x65), w(0x66), w(0x67),\
olav 0:4c1fcbfcc7bf 174 w(0x68), w(0x69), w(0x6a), w(0x6b), w(0x6c), w(0x6d), w(0x6e), w(0x6f),\
olav 0:4c1fcbfcc7bf 175 w(0x70), w(0x71), w(0x72), w(0x73), w(0x74), w(0x75), w(0x76), w(0x77),\
olav 0:4c1fcbfcc7bf 176 w(0x78), w(0x79), w(0x7a), w(0x7b), w(0x7c), w(0x7d), w(0x7e), w(0x7f),\
olav 0:4c1fcbfcc7bf 177 w(0x80), w(0x81), w(0x82), w(0x83), w(0x84), w(0x85), w(0x86), w(0x87),\
olav 0:4c1fcbfcc7bf 178 w(0x88), w(0x89), w(0x8a), w(0x8b), w(0x8c), w(0x8d), w(0x8e), w(0x8f),\
olav 0:4c1fcbfcc7bf 179 w(0x90), w(0x91), w(0x92), w(0x93), w(0x94), w(0x95), w(0x96), w(0x97),\
olav 0:4c1fcbfcc7bf 180 w(0x98), w(0x99), w(0x9a), w(0x9b), w(0x9c), w(0x9d), w(0x9e), w(0x9f),\
olav 0:4c1fcbfcc7bf 181 w(0xa0), w(0xa1), w(0xa2), w(0xa3), w(0xa4), w(0xa5), w(0xa6), w(0xa7),\
olav 0:4c1fcbfcc7bf 182 w(0xa8), w(0xa9), w(0xaa), w(0xab), w(0xac), w(0xad), w(0xae), w(0xaf),\
olav 0:4c1fcbfcc7bf 183 w(0xb0), w(0xb1), w(0xb2), w(0xb3), w(0xb4), w(0xb5), w(0xb6), w(0xb7),\
olav 0:4c1fcbfcc7bf 184 w(0xb8), w(0xb9), w(0xba), w(0xbb), w(0xbc), w(0xbd), w(0xbe), w(0xbf),\
olav 0:4c1fcbfcc7bf 185 w(0xc0), w(0xc1), w(0xc2), w(0xc3), w(0xc4), w(0xc5), w(0xc6), w(0xc7),\
olav 0:4c1fcbfcc7bf 186 w(0xc8), w(0xc9), w(0xca), w(0xcb), w(0xcc), w(0xcd), w(0xce), w(0xcf),\
olav 0:4c1fcbfcc7bf 187 w(0xd0), w(0xd1), w(0xd2), w(0xd3), w(0xd4), w(0xd5), w(0xd6), w(0xd7),\
olav 0:4c1fcbfcc7bf 188 w(0xd8), w(0xd9), w(0xda), w(0xdb), w(0xdc), w(0xdd), w(0xde), w(0xdf),\
olav 0:4c1fcbfcc7bf 189 w(0xe0), w(0xe1), w(0xe2), w(0xe3), w(0xe4), w(0xe5), w(0xe6), w(0xe7),\
olav 0:4c1fcbfcc7bf 190 w(0xe8), w(0xe9), w(0xea), w(0xeb), w(0xec), w(0xed), w(0xee), w(0xef),\
olav 0:4c1fcbfcc7bf 191 w(0xf0), w(0xf1), w(0xf2), w(0xf3), w(0xf4), w(0xf5), w(0xf6), w(0xf7),\
olav 0:4c1fcbfcc7bf 192 w(0xf8), w(0xf9), w(0xfa), w(0xfb), w(0xfc), w(0xfd), w(0xfe), w(0xff) }
olav 0:4c1fcbfcc7bf 193
olav 0:4c1fcbfcc7bf 194 static const uint8_t sbox[256] = sb_data(f1);
olav 0:4c1fcbfcc7bf 195
olav 0:4c1fcbfcc7bf 196 #if defined( AES_DEC_PREKEYED )
olav 0:4c1fcbfcc7bf 197 static const uint8_t isbox[256] = isb_data(f1);
olav 0:4c1fcbfcc7bf 198 #endif
olav 0:4c1fcbfcc7bf 199
olav 0:4c1fcbfcc7bf 200 static const uint8_t gfm2_sbox[256] = sb_data(f2);
olav 0:4c1fcbfcc7bf 201 static const uint8_t gfm3_sbox[256] = sb_data(f3);
olav 0:4c1fcbfcc7bf 202
olav 0:4c1fcbfcc7bf 203 #if defined( AES_DEC_PREKEYED )
olav 0:4c1fcbfcc7bf 204 static const uint8_t gfmul_9[256] = mm_data(f9);
olav 0:4c1fcbfcc7bf 205 static const uint8_t gfmul_b[256] = mm_data(fb);
olav 0:4c1fcbfcc7bf 206 static const uint8_t gfmul_d[256] = mm_data(fd);
olav 0:4c1fcbfcc7bf 207 static const uint8_t gfmul_e[256] = mm_data(fe);
olav 0:4c1fcbfcc7bf 208 #endif
olav 0:4c1fcbfcc7bf 209
olav 0:4c1fcbfcc7bf 210 #define s_box(x) sbox[(x)]
olav 0:4c1fcbfcc7bf 211 #if defined( AES_DEC_PREKEYED )
olav 0:4c1fcbfcc7bf 212 #define is_box(x) isbox[(x)]
olav 0:4c1fcbfcc7bf 213 #endif
olav 0:4c1fcbfcc7bf 214 #define gfm2_sb(x) gfm2_sbox[(x)]
olav 0:4c1fcbfcc7bf 215 #define gfm3_sb(x) gfm3_sbox[(x)]
olav 0:4c1fcbfcc7bf 216 #if defined( AES_DEC_PREKEYED )
olav 0:4c1fcbfcc7bf 217 #define gfm_9(x) gfmul_9[(x)]
olav 0:4c1fcbfcc7bf 218 #define gfm_b(x) gfmul_b[(x)]
olav 0:4c1fcbfcc7bf 219 #define gfm_d(x) gfmul_d[(x)]
olav 0:4c1fcbfcc7bf 220 #define gfm_e(x) gfmul_e[(x)]
olav 0:4c1fcbfcc7bf 221 #endif
olav 0:4c1fcbfcc7bf 222 #else
olav 0:4c1fcbfcc7bf 223
olav 0:4c1fcbfcc7bf 224 /* this is the high bit of x right shifted by 1 */
olav 0:4c1fcbfcc7bf 225 /* position. Since the starting polynomial has */
olav 0:4c1fcbfcc7bf 226 /* 9 bits (0x11b), this right shift keeps the */
olav 0:4c1fcbfcc7bf 227 /* values of all top bits within a byte */
olav 0:4c1fcbfcc7bf 228
olav 0:4c1fcbfcc7bf 229 static uint8_t hibit(const uint8_t x)
olav 0:4c1fcbfcc7bf 230 { uint8_t r = (uint8_t)((x >> 1) | (x >> 2));
olav 0:4c1fcbfcc7bf 231
olav 0:4c1fcbfcc7bf 232 r |= (r >> 2);
olav 0:4c1fcbfcc7bf 233 r |= (r >> 4);
olav 0:4c1fcbfcc7bf 234 return (r + 1) >> 1;
olav 0:4c1fcbfcc7bf 235 }
olav 0:4c1fcbfcc7bf 236
olav 0:4c1fcbfcc7bf 237 /* return the inverse of the finite field element x */
olav 0:4c1fcbfcc7bf 238
olav 0:4c1fcbfcc7bf 239 static uint8_t gf_inv(const uint8_t x)
olav 0:4c1fcbfcc7bf 240 { uint8_t p1 = x, p2 = BPOLY, n1 = hibit(x), n2 = 0x80, v1 = 1, v2 = 0;
olav 0:4c1fcbfcc7bf 241
olav 0:4c1fcbfcc7bf 242 if(x < 2)
olav 0:4c1fcbfcc7bf 243 return x;
olav 0:4c1fcbfcc7bf 244
olav 0:4c1fcbfcc7bf 245 for( ; ; )
olav 0:4c1fcbfcc7bf 246 {
olav 0:4c1fcbfcc7bf 247 if(n1)
olav 0:4c1fcbfcc7bf 248 while(n2 >= n1) /* divide polynomial p2 by p1 */
olav 0:4c1fcbfcc7bf 249 {
olav 0:4c1fcbfcc7bf 250 n2 /= n1; /* shift smaller polynomial left */
olav 0:4c1fcbfcc7bf 251 p2 ^= (p1 * n2) & 0xff; /* and remove from larger one */
olav 0:4c1fcbfcc7bf 252 v2 ^= (v1 * n2); /* shift accumulated value and */
olav 0:4c1fcbfcc7bf 253 n2 = hibit(p2); /* add into result */
olav 0:4c1fcbfcc7bf 254 }
olav 0:4c1fcbfcc7bf 255 else
olav 0:4c1fcbfcc7bf 256 return v1;
olav 0:4c1fcbfcc7bf 257
olav 0:4c1fcbfcc7bf 258 if(n2) /* repeat with values swapped */
olav 0:4c1fcbfcc7bf 259 while(n1 >= n2)
olav 0:4c1fcbfcc7bf 260 {
olav 0:4c1fcbfcc7bf 261 n1 /= n2;
olav 0:4c1fcbfcc7bf 262 p1 ^= p2 * n1;
olav 0:4c1fcbfcc7bf 263 v1 ^= v2 * n1;
olav 0:4c1fcbfcc7bf 264 n1 = hibit(p1);
olav 0:4c1fcbfcc7bf 265 }
olav 0:4c1fcbfcc7bf 266 else
olav 0:4c1fcbfcc7bf 267 return v2;
olav 0:4c1fcbfcc7bf 268 }
olav 0:4c1fcbfcc7bf 269 }
olav 0:4c1fcbfcc7bf 270
olav 0:4c1fcbfcc7bf 271 /* The forward and inverse affine transformations used in the S-box */
olav 0:4c1fcbfcc7bf 272 uint8_t fwd_affine(const uint8_t x)
olav 0:4c1fcbfcc7bf 273 {
olav 0:4c1fcbfcc7bf 274 #if defined( HAVE_UINT_32T )
olav 0:4c1fcbfcc7bf 275 uint32_t w = x;
olav 0:4c1fcbfcc7bf 276 w ^= (w << 1) ^ (w << 2) ^ (w << 3) ^ (w << 4);
olav 0:4c1fcbfcc7bf 277 return 0x63 ^ ((w ^ (w >> 8)) & 0xff);
olav 0:4c1fcbfcc7bf 278 #else
olav 0:4c1fcbfcc7bf 279 return 0x63 ^ x ^ (x << 1) ^ (x << 2) ^ (x << 3) ^ (x << 4)
olav 0:4c1fcbfcc7bf 280 ^ (x >> 7) ^ (x >> 6) ^ (x >> 5) ^ (x >> 4);
olav 0:4c1fcbfcc7bf 281 #endif
olav 0:4c1fcbfcc7bf 282 }
olav 0:4c1fcbfcc7bf 283
olav 0:4c1fcbfcc7bf 284 uint8_t inv_affine(const uint8_t x)
olav 0:4c1fcbfcc7bf 285 {
olav 0:4c1fcbfcc7bf 286 #if defined( HAVE_UINT_32T )
olav 0:4c1fcbfcc7bf 287 uint32_t w = x;
olav 0:4c1fcbfcc7bf 288 w = (w << 1) ^ (w << 3) ^ (w << 6);
olav 0:4c1fcbfcc7bf 289 return 0x05 ^ ((w ^ (w >> 8)) & 0xff);
olav 0:4c1fcbfcc7bf 290 #else
olav 0:4c1fcbfcc7bf 291 return 0x05 ^ (x << 1) ^ (x << 3) ^ (x << 6)
olav 0:4c1fcbfcc7bf 292 ^ (x >> 7) ^ (x >> 5) ^ (x >> 2);
olav 0:4c1fcbfcc7bf 293 #endif
olav 0:4c1fcbfcc7bf 294 }
olav 0:4c1fcbfcc7bf 295
olav 0:4c1fcbfcc7bf 296 #define s_box(x) fwd_affine(gf_inv(x))
olav 0:4c1fcbfcc7bf 297 #define is_box(x) gf_inv(inv_affine(x))
olav 0:4c1fcbfcc7bf 298 #define gfm2_sb(x) f2(s_box(x))
olav 0:4c1fcbfcc7bf 299 #define gfm3_sb(x) f3(s_box(x))
olav 0:4c1fcbfcc7bf 300 #define gfm_9(x) f9(x)
olav 0:4c1fcbfcc7bf 301 #define gfm_b(x) fb(x)
olav 0:4c1fcbfcc7bf 302 #define gfm_d(x) fd(x)
olav 0:4c1fcbfcc7bf 303 #define gfm_e(x) fe(x)
olav 0:4c1fcbfcc7bf 304
olav 0:4c1fcbfcc7bf 305 #endif
olav 0:4c1fcbfcc7bf 306
olav 0:4c1fcbfcc7bf 307 #if defined( HAVE_MEMCPY )
olav 0:4c1fcbfcc7bf 308 # define block_copy_nn(d, s, l) memcpy(d, s, l)
olav 0:4c1fcbfcc7bf 309 # define block_copy(d, s) memcpy(d, s, N_BLOCK)
olav 0:4c1fcbfcc7bf 310 #else
olav 0:4c1fcbfcc7bf 311 # define block_copy_nn(d, s, l) copy_block_nn(d, s, l)
olav 0:4c1fcbfcc7bf 312 # define block_copy(d, s) copy_block(d, s)
olav 0:4c1fcbfcc7bf 313 #endif
olav 0:4c1fcbfcc7bf 314
olav 0:4c1fcbfcc7bf 315 static void copy_block( void *d, const void *s )
olav 0:4c1fcbfcc7bf 316 {
olav 0:4c1fcbfcc7bf 317 #if defined( HAVE_UINT_32T )
olav 0:4c1fcbfcc7bf 318 ((uint32_t*)d)[ 0] = ((uint32_t*)s)[ 0];
olav 0:4c1fcbfcc7bf 319 ((uint32_t*)d)[ 1] = ((uint32_t*)s)[ 1];
olav 0:4c1fcbfcc7bf 320 ((uint32_t*)d)[ 2] = ((uint32_t*)s)[ 2];
olav 0:4c1fcbfcc7bf 321 ((uint32_t*)d)[ 3] = ((uint32_t*)s)[ 3];
olav 0:4c1fcbfcc7bf 322 #else
olav 0:4c1fcbfcc7bf 323 ((uint8_t*)d)[ 0] = ((uint8_t*)s)[ 0];
olav 0:4c1fcbfcc7bf 324 ((uint8_t*)d)[ 1] = ((uint8_t*)s)[ 1];
olav 0:4c1fcbfcc7bf 325 ((uint8_t*)d)[ 2] = ((uint8_t*)s)[ 2];
olav 0:4c1fcbfcc7bf 326 ((uint8_t*)d)[ 3] = ((uint8_t*)s)[ 3];
olav 0:4c1fcbfcc7bf 327 ((uint8_t*)d)[ 4] = ((uint8_t*)s)[ 4];
olav 0:4c1fcbfcc7bf 328 ((uint8_t*)d)[ 5] = ((uint8_t*)s)[ 5];
olav 0:4c1fcbfcc7bf 329 ((uint8_t*)d)[ 6] = ((uint8_t*)s)[ 6];
olav 0:4c1fcbfcc7bf 330 ((uint8_t*)d)[ 7] = ((uint8_t*)s)[ 7];
olav 0:4c1fcbfcc7bf 331 ((uint8_t*)d)[ 8] = ((uint8_t*)s)[ 8];
olav 0:4c1fcbfcc7bf 332 ((uint8_t*)d)[ 9] = ((uint8_t*)s)[ 9];
olav 0:4c1fcbfcc7bf 333 ((uint8_t*)d)[10] = ((uint8_t*)s)[10];
olav 0:4c1fcbfcc7bf 334 ((uint8_t*)d)[11] = ((uint8_t*)s)[11];
olav 0:4c1fcbfcc7bf 335 ((uint8_t*)d)[12] = ((uint8_t*)s)[12];
olav 0:4c1fcbfcc7bf 336 ((uint8_t*)d)[13] = ((uint8_t*)s)[13];
olav 0:4c1fcbfcc7bf 337 ((uint8_t*)d)[14] = ((uint8_t*)s)[14];
olav 0:4c1fcbfcc7bf 338 ((uint8_t*)d)[15] = ((uint8_t*)s)[15];
olav 0:4c1fcbfcc7bf 339 #endif
olav 0:4c1fcbfcc7bf 340 }
olav 0:4c1fcbfcc7bf 341
olav 0:4c1fcbfcc7bf 342 static void copy_block_nn( uint8_t * d, const uint8_t *s, uint8_t nn )
olav 0:4c1fcbfcc7bf 343 {
olav 0:4c1fcbfcc7bf 344 while( nn-- )
olav 0:4c1fcbfcc7bf 345 //*((uint8_t*)d)++ = *((uint8_t*)s)++;
olav 0:4c1fcbfcc7bf 346 *d++ = *s++;
olav 0:4c1fcbfcc7bf 347 }
olav 0:4c1fcbfcc7bf 348
olav 0:4c1fcbfcc7bf 349 static void xor_block( void *d, const void *s )
olav 0:4c1fcbfcc7bf 350 {
olav 0:4c1fcbfcc7bf 351 #if defined( HAVE_UINT_32T )
olav 0:4c1fcbfcc7bf 352 ((uint32_t*)d)[ 0] ^= ((uint32_t*)s)[ 0];
olav 0:4c1fcbfcc7bf 353 ((uint32_t*)d)[ 1] ^= ((uint32_t*)s)[ 1];
olav 0:4c1fcbfcc7bf 354 ((uint32_t*)d)[ 2] ^= ((uint32_t*)s)[ 2];
olav 0:4c1fcbfcc7bf 355 ((uint32_t*)d)[ 3] ^= ((uint32_t*)s)[ 3];
olav 0:4c1fcbfcc7bf 356 #else
olav 0:4c1fcbfcc7bf 357 ((uint8_t*)d)[ 0] ^= ((uint8_t*)s)[ 0];
olav 0:4c1fcbfcc7bf 358 ((uint8_t*)d)[ 1] ^= ((uint8_t*)s)[ 1];
olav 0:4c1fcbfcc7bf 359 ((uint8_t*)d)[ 2] ^= ((uint8_t*)s)[ 2];
olav 0:4c1fcbfcc7bf 360 ((uint8_t*)d)[ 3] ^= ((uint8_t*)s)[ 3];
olav 0:4c1fcbfcc7bf 361 ((uint8_t*)d)[ 4] ^= ((uint8_t*)s)[ 4];
olav 0:4c1fcbfcc7bf 362 ((uint8_t*)d)[ 5] ^= ((uint8_t*)s)[ 5];
olav 0:4c1fcbfcc7bf 363 ((uint8_t*)d)[ 6] ^= ((uint8_t*)s)[ 6];
olav 0:4c1fcbfcc7bf 364 ((uint8_t*)d)[ 7] ^= ((uint8_t*)s)[ 7];
olav 0:4c1fcbfcc7bf 365 ((uint8_t*)d)[ 8] ^= ((uint8_t*)s)[ 8];
olav 0:4c1fcbfcc7bf 366 ((uint8_t*)d)[ 9] ^= ((uint8_t*)s)[ 9];
olav 0:4c1fcbfcc7bf 367 ((uint8_t*)d)[10] ^= ((uint8_t*)s)[10];
olav 0:4c1fcbfcc7bf 368 ((uint8_t*)d)[11] ^= ((uint8_t*)s)[11];
olav 0:4c1fcbfcc7bf 369 ((uint8_t*)d)[12] ^= ((uint8_t*)s)[12];
olav 0:4c1fcbfcc7bf 370 ((uint8_t*)d)[13] ^= ((uint8_t*)s)[13];
olav 0:4c1fcbfcc7bf 371 ((uint8_t*)d)[14] ^= ((uint8_t*)s)[14];
olav 0:4c1fcbfcc7bf 372 ((uint8_t*)d)[15] ^= ((uint8_t*)s)[15];
olav 0:4c1fcbfcc7bf 373 #endif
olav 0:4c1fcbfcc7bf 374 }
olav 0:4c1fcbfcc7bf 375
olav 0:4c1fcbfcc7bf 376 static void copy_and_key( void *d, const void *s, const void *k )
olav 0:4c1fcbfcc7bf 377 {
olav 0:4c1fcbfcc7bf 378 #if defined( HAVE_UINT_32T )
olav 0:4c1fcbfcc7bf 379 ((uint32_t*)d)[ 0] = ((uint32_t*)s)[ 0] ^ ((uint32_t*)k)[ 0];
olav 0:4c1fcbfcc7bf 380 ((uint32_t*)d)[ 1] = ((uint32_t*)s)[ 1] ^ ((uint32_t*)k)[ 1];
olav 0:4c1fcbfcc7bf 381 ((uint32_t*)d)[ 2] = ((uint32_t*)s)[ 2] ^ ((uint32_t*)k)[ 2];
olav 0:4c1fcbfcc7bf 382 ((uint32_t*)d)[ 3] = ((uint32_t*)s)[ 3] ^ ((uint32_t*)k)[ 3];
olav 0:4c1fcbfcc7bf 383 #elif 1
olav 0:4c1fcbfcc7bf 384 ((uint8_t*)d)[ 0] = ((uint8_t*)s)[ 0] ^ ((uint8_t*)k)[ 0];
olav 0:4c1fcbfcc7bf 385 ((uint8_t*)d)[ 1] = ((uint8_t*)s)[ 1] ^ ((uint8_t*)k)[ 1];
olav 0:4c1fcbfcc7bf 386 ((uint8_t*)d)[ 2] = ((uint8_t*)s)[ 2] ^ ((uint8_t*)k)[ 2];
olav 0:4c1fcbfcc7bf 387 ((uint8_t*)d)[ 3] = ((uint8_t*)s)[ 3] ^ ((uint8_t*)k)[ 3];
olav 0:4c1fcbfcc7bf 388 ((uint8_t*)d)[ 4] = ((uint8_t*)s)[ 4] ^ ((uint8_t*)k)[ 4];
olav 0:4c1fcbfcc7bf 389 ((uint8_t*)d)[ 5] = ((uint8_t*)s)[ 5] ^ ((uint8_t*)k)[ 5];
olav 0:4c1fcbfcc7bf 390 ((uint8_t*)d)[ 6] = ((uint8_t*)s)[ 6] ^ ((uint8_t*)k)[ 6];
olav 0:4c1fcbfcc7bf 391 ((uint8_t*)d)[ 7] = ((uint8_t*)s)[ 7] ^ ((uint8_t*)k)[ 7];
olav 0:4c1fcbfcc7bf 392 ((uint8_t*)d)[ 8] = ((uint8_t*)s)[ 8] ^ ((uint8_t*)k)[ 8];
olav 0:4c1fcbfcc7bf 393 ((uint8_t*)d)[ 9] = ((uint8_t*)s)[ 9] ^ ((uint8_t*)k)[ 9];
olav 0:4c1fcbfcc7bf 394 ((uint8_t*)d)[10] = ((uint8_t*)s)[10] ^ ((uint8_t*)k)[10];
olav 0:4c1fcbfcc7bf 395 ((uint8_t*)d)[11] = ((uint8_t*)s)[11] ^ ((uint8_t*)k)[11];
olav 0:4c1fcbfcc7bf 396 ((uint8_t*)d)[12] = ((uint8_t*)s)[12] ^ ((uint8_t*)k)[12];
olav 0:4c1fcbfcc7bf 397 ((uint8_t*)d)[13] = ((uint8_t*)s)[13] ^ ((uint8_t*)k)[13];
olav 0:4c1fcbfcc7bf 398 ((uint8_t*)d)[14] = ((uint8_t*)s)[14] ^ ((uint8_t*)k)[14];
olav 0:4c1fcbfcc7bf 399 ((uint8_t*)d)[15] = ((uint8_t*)s)[15] ^ ((uint8_t*)k)[15];
olav 0:4c1fcbfcc7bf 400 #else
olav 0:4c1fcbfcc7bf 401 block_copy(d, s);
olav 0:4c1fcbfcc7bf 402 xor_block(d, k);
olav 0:4c1fcbfcc7bf 403 #endif
olav 0:4c1fcbfcc7bf 404 }
olav 0:4c1fcbfcc7bf 405
olav 0:4c1fcbfcc7bf 406 static void add_round_key( uint8_t d[N_BLOCK], const uint8_t k[N_BLOCK] )
olav 0:4c1fcbfcc7bf 407 {
olav 0:4c1fcbfcc7bf 408 xor_block(d, k);
olav 0:4c1fcbfcc7bf 409 }
olav 0:4c1fcbfcc7bf 410
olav 0:4c1fcbfcc7bf 411 static void shift_sub_rows( uint8_t st[N_BLOCK] )
olav 0:4c1fcbfcc7bf 412 { uint8_t tt;
olav 0:4c1fcbfcc7bf 413
olav 0:4c1fcbfcc7bf 414 st[ 0] = s_box(st[ 0]); st[ 4] = s_box(st[ 4]);
olav 0:4c1fcbfcc7bf 415 st[ 8] = s_box(st[ 8]); st[12] = s_box(st[12]);
olav 0:4c1fcbfcc7bf 416
olav 0:4c1fcbfcc7bf 417 tt = st[1]; st[ 1] = s_box(st[ 5]); st[ 5] = s_box(st[ 9]);
olav 0:4c1fcbfcc7bf 418 st[ 9] = s_box(st[13]); st[13] = s_box( tt );
olav 0:4c1fcbfcc7bf 419
olav 0:4c1fcbfcc7bf 420 tt = st[2]; st[ 2] = s_box(st[10]); st[10] = s_box( tt );
olav 0:4c1fcbfcc7bf 421 tt = st[6]; st[ 6] = s_box(st[14]); st[14] = s_box( tt );
olav 0:4c1fcbfcc7bf 422
olav 0:4c1fcbfcc7bf 423 tt = st[15]; st[15] = s_box(st[11]); st[11] = s_box(st[ 7]);
olav 0:4c1fcbfcc7bf 424 st[ 7] = s_box(st[ 3]); st[ 3] = s_box( tt );
olav 0:4c1fcbfcc7bf 425 }
olav 0:4c1fcbfcc7bf 426
olav 0:4c1fcbfcc7bf 427 #if defined( AES_DEC_PREKEYED )
olav 0:4c1fcbfcc7bf 428
olav 0:4c1fcbfcc7bf 429 static void inv_shift_sub_rows( uint8_t st[N_BLOCK] )
olav 0:4c1fcbfcc7bf 430 { uint8_t tt;
olav 0:4c1fcbfcc7bf 431
olav 0:4c1fcbfcc7bf 432 st[ 0] = is_box(st[ 0]); st[ 4] = is_box(st[ 4]);
olav 0:4c1fcbfcc7bf 433 st[ 8] = is_box(st[ 8]); st[12] = is_box(st[12]);
olav 0:4c1fcbfcc7bf 434
olav 0:4c1fcbfcc7bf 435 tt = st[13]; st[13] = is_box(st[9]); st[ 9] = is_box(st[5]);
olav 0:4c1fcbfcc7bf 436 st[ 5] = is_box(st[1]); st[ 1] = is_box( tt );
olav 0:4c1fcbfcc7bf 437
olav 0:4c1fcbfcc7bf 438 tt = st[2]; st[ 2] = is_box(st[10]); st[10] = is_box( tt );
olav 0:4c1fcbfcc7bf 439 tt = st[6]; st[ 6] = is_box(st[14]); st[14] = is_box( tt );
olav 0:4c1fcbfcc7bf 440
olav 0:4c1fcbfcc7bf 441 tt = st[3]; st[ 3] = is_box(st[ 7]); st[ 7] = is_box(st[11]);
olav 0:4c1fcbfcc7bf 442 st[11] = is_box(st[15]); st[15] = is_box( tt );
olav 0:4c1fcbfcc7bf 443 }
olav 0:4c1fcbfcc7bf 444
olav 0:4c1fcbfcc7bf 445 #endif
olav 0:4c1fcbfcc7bf 446
olav 0:4c1fcbfcc7bf 447 #if defined( VERSION_1 )
olav 0:4c1fcbfcc7bf 448 static void mix_sub_columns( uint8_t dt[N_BLOCK] )
olav 0:4c1fcbfcc7bf 449 { uint8_t st[N_BLOCK];
olav 0:4c1fcbfcc7bf 450 block_copy(st, dt);
olav 0:4c1fcbfcc7bf 451 #else
olav 0:4c1fcbfcc7bf 452 static void mix_sub_columns( uint8_t dt[N_BLOCK], uint8_t st[N_BLOCK] )
olav 0:4c1fcbfcc7bf 453 {
olav 0:4c1fcbfcc7bf 454 #endif
olav 0:4c1fcbfcc7bf 455 dt[ 0] = gfm2_sb(st[0]) ^ gfm3_sb(st[5]) ^ s_box(st[10]) ^ s_box(st[15]);
olav 0:4c1fcbfcc7bf 456 dt[ 1] = s_box(st[0]) ^ gfm2_sb(st[5]) ^ gfm3_sb(st[10]) ^ s_box(st[15]);
olav 0:4c1fcbfcc7bf 457 dt[ 2] = s_box(st[0]) ^ s_box(st[5]) ^ gfm2_sb(st[10]) ^ gfm3_sb(st[15]);
olav 0:4c1fcbfcc7bf 458 dt[ 3] = gfm3_sb(st[0]) ^ s_box(st[5]) ^ s_box(st[10]) ^ gfm2_sb(st[15]);
olav 0:4c1fcbfcc7bf 459
olav 0:4c1fcbfcc7bf 460 dt[ 4] = gfm2_sb(st[4]) ^ gfm3_sb(st[9]) ^ s_box(st[14]) ^ s_box(st[3]);
olav 0:4c1fcbfcc7bf 461 dt[ 5] = s_box(st[4]) ^ gfm2_sb(st[9]) ^ gfm3_sb(st[14]) ^ s_box(st[3]);
olav 0:4c1fcbfcc7bf 462 dt[ 6] = s_box(st[4]) ^ s_box(st[9]) ^ gfm2_sb(st[14]) ^ gfm3_sb(st[3]);
olav 0:4c1fcbfcc7bf 463 dt[ 7] = gfm3_sb(st[4]) ^ s_box(st[9]) ^ s_box(st[14]) ^ gfm2_sb(st[3]);
olav 0:4c1fcbfcc7bf 464
olav 0:4c1fcbfcc7bf 465 dt[ 8] = gfm2_sb(st[8]) ^ gfm3_sb(st[13]) ^ s_box(st[2]) ^ s_box(st[7]);
olav 0:4c1fcbfcc7bf 466 dt[ 9] = s_box(st[8]) ^ gfm2_sb(st[13]) ^ gfm3_sb(st[2]) ^ s_box(st[7]);
olav 0:4c1fcbfcc7bf 467 dt[10] = s_box(st[8]) ^ s_box(st[13]) ^ gfm2_sb(st[2]) ^ gfm3_sb(st[7]);
olav 0:4c1fcbfcc7bf 468 dt[11] = gfm3_sb(st[8]) ^ s_box(st[13]) ^ s_box(st[2]) ^ gfm2_sb(st[7]);
olav 0:4c1fcbfcc7bf 469
olav 0:4c1fcbfcc7bf 470 dt[12] = gfm2_sb(st[12]) ^ gfm3_sb(st[1]) ^ s_box(st[6]) ^ s_box(st[11]);
olav 0:4c1fcbfcc7bf 471 dt[13] = s_box(st[12]) ^ gfm2_sb(st[1]) ^ gfm3_sb(st[6]) ^ s_box(st[11]);
olav 0:4c1fcbfcc7bf 472 dt[14] = s_box(st[12]) ^ s_box(st[1]) ^ gfm2_sb(st[6]) ^ gfm3_sb(st[11]);
olav 0:4c1fcbfcc7bf 473 dt[15] = gfm3_sb(st[12]) ^ s_box(st[1]) ^ s_box(st[6]) ^ gfm2_sb(st[11]);
olav 0:4c1fcbfcc7bf 474 }
olav 0:4c1fcbfcc7bf 475
olav 0:4c1fcbfcc7bf 476 #if defined( AES_DEC_PREKEYED )
olav 0:4c1fcbfcc7bf 477
olav 0:4c1fcbfcc7bf 478 #if defined( VERSION_1 )
olav 0:4c1fcbfcc7bf 479 static void inv_mix_sub_columns( uint8_t dt[N_BLOCK] )
olav 0:4c1fcbfcc7bf 480 { uint8_t st[N_BLOCK];
olav 0:4c1fcbfcc7bf 481 block_copy(st, dt);
olav 0:4c1fcbfcc7bf 482 #else
olav 0:4c1fcbfcc7bf 483 static void inv_mix_sub_columns( uint8_t dt[N_BLOCK], uint8_t st[N_BLOCK] )
olav 0:4c1fcbfcc7bf 484 {
olav 0:4c1fcbfcc7bf 485 #endif
olav 0:4c1fcbfcc7bf 486 dt[ 0] = is_box(gfm_e(st[ 0]) ^ gfm_b(st[ 1]) ^ gfm_d(st[ 2]) ^ gfm_9(st[ 3]));
olav 0:4c1fcbfcc7bf 487 dt[ 5] = is_box(gfm_9(st[ 0]) ^ gfm_e(st[ 1]) ^ gfm_b(st[ 2]) ^ gfm_d(st[ 3]));
olav 0:4c1fcbfcc7bf 488 dt[10] = is_box(gfm_d(st[ 0]) ^ gfm_9(st[ 1]) ^ gfm_e(st[ 2]) ^ gfm_b(st[ 3]));
olav 0:4c1fcbfcc7bf 489 dt[15] = is_box(gfm_b(st[ 0]) ^ gfm_d(st[ 1]) ^ gfm_9(st[ 2]) ^ gfm_e(st[ 3]));
olav 0:4c1fcbfcc7bf 490
olav 0:4c1fcbfcc7bf 491 dt[ 4] = is_box(gfm_e(st[ 4]) ^ gfm_b(st[ 5]) ^ gfm_d(st[ 6]) ^ gfm_9(st[ 7]));
olav 0:4c1fcbfcc7bf 492 dt[ 9] = is_box(gfm_9(st[ 4]) ^ gfm_e(st[ 5]) ^ gfm_b(st[ 6]) ^ gfm_d(st[ 7]));
olav 0:4c1fcbfcc7bf 493 dt[14] = is_box(gfm_d(st[ 4]) ^ gfm_9(st[ 5]) ^ gfm_e(st[ 6]) ^ gfm_b(st[ 7]));
olav 0:4c1fcbfcc7bf 494 dt[ 3] = is_box(gfm_b(st[ 4]) ^ gfm_d(st[ 5]) ^ gfm_9(st[ 6]) ^ gfm_e(st[ 7]));
olav 0:4c1fcbfcc7bf 495
olav 0:4c1fcbfcc7bf 496 dt[ 8] = is_box(gfm_e(st[ 8]) ^ gfm_b(st[ 9]) ^ gfm_d(st[10]) ^ gfm_9(st[11]));
olav 0:4c1fcbfcc7bf 497 dt[13] = is_box(gfm_9(st[ 8]) ^ gfm_e(st[ 9]) ^ gfm_b(st[10]) ^ gfm_d(st[11]));
olav 0:4c1fcbfcc7bf 498 dt[ 2] = is_box(gfm_d(st[ 8]) ^ gfm_9(st[ 9]) ^ gfm_e(st[10]) ^ gfm_b(st[11]));
olav 0:4c1fcbfcc7bf 499 dt[ 7] = is_box(gfm_b(st[ 8]) ^ gfm_d(st[ 9]) ^ gfm_9(st[10]) ^ gfm_e(st[11]));
olav 0:4c1fcbfcc7bf 500
olav 0:4c1fcbfcc7bf 501 dt[12] = is_box(gfm_e(st[12]) ^ gfm_b(st[13]) ^ gfm_d(st[14]) ^ gfm_9(st[15]));
olav 0:4c1fcbfcc7bf 502 dt[ 1] = is_box(gfm_9(st[12]) ^ gfm_e(st[13]) ^ gfm_b(st[14]) ^ gfm_d(st[15]));
olav 0:4c1fcbfcc7bf 503 dt[ 6] = is_box(gfm_d(st[12]) ^ gfm_9(st[13]) ^ gfm_e(st[14]) ^ gfm_b(st[15]));
olav 0:4c1fcbfcc7bf 504 dt[11] = is_box(gfm_b(st[12]) ^ gfm_d(st[13]) ^ gfm_9(st[14]) ^ gfm_e(st[15]));
olav 0:4c1fcbfcc7bf 505 }
olav 0:4c1fcbfcc7bf 506
olav 0:4c1fcbfcc7bf 507 #endif
olav 0:4c1fcbfcc7bf 508
olav 0:4c1fcbfcc7bf 509 #if defined( AES_ENC_PREKEYED ) || defined( AES_DEC_PREKEYED )
olav 0:4c1fcbfcc7bf 510
olav 0:4c1fcbfcc7bf 511 /* Set the cipher key for the pre-keyed version */
olav 0:4c1fcbfcc7bf 512
olav 0:4c1fcbfcc7bf 513 return_type aes_set_key( const uint8_t key[], length_type keylen, aes_context ctx[1] )
olav 0:4c1fcbfcc7bf 514 {
olav 0:4c1fcbfcc7bf 515 uint8_t cc, rc, hi;
olav 0:4c1fcbfcc7bf 516
olav 0:4c1fcbfcc7bf 517 switch( keylen )
olav 0:4c1fcbfcc7bf 518 {
olav 0:4c1fcbfcc7bf 519 case 16:
olav 0:4c1fcbfcc7bf 520 case 24:
olav 0:4c1fcbfcc7bf 521 case 32:
olav 0:4c1fcbfcc7bf 522 break;
olav 0:4c1fcbfcc7bf 523 default:
olav 0:4c1fcbfcc7bf 524 ctx->rnd = 0;
olav 0:4c1fcbfcc7bf 525 return ( uint8_t )-1;
olav 0:4c1fcbfcc7bf 526 }
olav 0:4c1fcbfcc7bf 527 block_copy_nn(ctx->ksch, key, keylen);
olav 0:4c1fcbfcc7bf 528 hi = (keylen + 28) << 2;
olav 0:4c1fcbfcc7bf 529 ctx->rnd = (hi >> 4) - 1;
olav 0:4c1fcbfcc7bf 530 for( cc = keylen, rc = 1; cc < hi; cc += 4 )
olav 0:4c1fcbfcc7bf 531 { uint8_t tt, t0, t1, t2, t3;
olav 0:4c1fcbfcc7bf 532
olav 0:4c1fcbfcc7bf 533 t0 = ctx->ksch[cc - 4];
olav 0:4c1fcbfcc7bf 534 t1 = ctx->ksch[cc - 3];
olav 0:4c1fcbfcc7bf 535 t2 = ctx->ksch[cc - 2];
olav 0:4c1fcbfcc7bf 536 t3 = ctx->ksch[cc - 1];
olav 0:4c1fcbfcc7bf 537 if( cc % keylen == 0 )
olav 0:4c1fcbfcc7bf 538 {
olav 0:4c1fcbfcc7bf 539 tt = t0;
olav 0:4c1fcbfcc7bf 540 t0 = s_box(t1) ^ rc;
olav 0:4c1fcbfcc7bf 541 t1 = s_box(t2);
olav 0:4c1fcbfcc7bf 542 t2 = s_box(t3);
olav 0:4c1fcbfcc7bf 543 t3 = s_box(tt);
olav 0:4c1fcbfcc7bf 544 rc = f2(rc);
olav 0:4c1fcbfcc7bf 545 }
olav 0:4c1fcbfcc7bf 546 else if( keylen > 24 && cc % keylen == 16 )
olav 0:4c1fcbfcc7bf 547 {
olav 0:4c1fcbfcc7bf 548 t0 = s_box(t0);
olav 0:4c1fcbfcc7bf 549 t1 = s_box(t1);
olav 0:4c1fcbfcc7bf 550 t2 = s_box(t2);
olav 0:4c1fcbfcc7bf 551 t3 = s_box(t3);
olav 0:4c1fcbfcc7bf 552 }
olav 0:4c1fcbfcc7bf 553 tt = cc - keylen;
olav 0:4c1fcbfcc7bf 554 ctx->ksch[cc + 0] = ctx->ksch[tt + 0] ^ t0;
olav 0:4c1fcbfcc7bf 555 ctx->ksch[cc + 1] = ctx->ksch[tt + 1] ^ t1;
olav 0:4c1fcbfcc7bf 556 ctx->ksch[cc + 2] = ctx->ksch[tt + 2] ^ t2;
olav 0:4c1fcbfcc7bf 557 ctx->ksch[cc + 3] = ctx->ksch[tt + 3] ^ t3;
olav 0:4c1fcbfcc7bf 558 }
olav 0:4c1fcbfcc7bf 559 return 0;
olav 0:4c1fcbfcc7bf 560 }
olav 0:4c1fcbfcc7bf 561
olav 0:4c1fcbfcc7bf 562 #endif
olav 0:4c1fcbfcc7bf 563
olav 0:4c1fcbfcc7bf 564 #if defined( AES_ENC_PREKEYED )
olav 0:4c1fcbfcc7bf 565
olav 0:4c1fcbfcc7bf 566 /* Encrypt a single block of 16 bytes */
olav 0:4c1fcbfcc7bf 567
olav 0:4c1fcbfcc7bf 568 return_type aes_encrypt( const uint8_t in[N_BLOCK], uint8_t out[N_BLOCK], const aes_context ctx[1] )
olav 0:4c1fcbfcc7bf 569 {
olav 0:4c1fcbfcc7bf 570 if( ctx->rnd )
olav 0:4c1fcbfcc7bf 571 {
olav 0:4c1fcbfcc7bf 572 uint8_t s1[N_BLOCK], r;
olav 0:4c1fcbfcc7bf 573 copy_and_key( s1, in, ctx->ksch );
olav 0:4c1fcbfcc7bf 574 for( r = 1 ; r < ctx->rnd ; ++r )
olav 0:4c1fcbfcc7bf 575 #if defined( VERSION_1 )
olav 0:4c1fcbfcc7bf 576 {
olav 0:4c1fcbfcc7bf 577 mix_sub_columns( s1 );
olav 0:4c1fcbfcc7bf 578 add_round_key( s1, ctx->ksch + r * N_BLOCK);
olav 0:4c1fcbfcc7bf 579 }
olav 0:4c1fcbfcc7bf 580 #else
olav 0:4c1fcbfcc7bf 581 { uint8_t s2[N_BLOCK];
olav 0:4c1fcbfcc7bf 582 mix_sub_columns( s2, s1 );
olav 0:4c1fcbfcc7bf 583 copy_and_key( s1, s2, ctx->ksch + r * N_BLOCK);
olav 0:4c1fcbfcc7bf 584 }
olav 0:4c1fcbfcc7bf 585 #endif
olav 0:4c1fcbfcc7bf 586 shift_sub_rows( s1 );
olav 0:4c1fcbfcc7bf 587 copy_and_key( out, s1, ctx->ksch + r * N_BLOCK );
olav 0:4c1fcbfcc7bf 588 }
olav 0:4c1fcbfcc7bf 589 else {
olav 0:4c1fcbfcc7bf 590 return ( uint8_t )-1;
olav 0:4c1fcbfcc7bf 591 }
olav 0:4c1fcbfcc7bf 592 return 0;
olav 0:4c1fcbfcc7bf 593 }
olav 0:4c1fcbfcc7bf 594
olav 0:4c1fcbfcc7bf 595 /* CBC encrypt a number of blocks (input and return an IV) */
olav 0:4c1fcbfcc7bf 596
olav 0:4c1fcbfcc7bf 597 return_type aes_cbc_encrypt( const uint8_t *in, uint8_t *out,
olav 0:4c1fcbfcc7bf 598 int32_t n_block, uint8_t iv[N_BLOCK], const aes_context ctx[1] )
olav 0:4c1fcbfcc7bf 599 {
olav 0:4c1fcbfcc7bf 600
olav 0:4c1fcbfcc7bf 601 while(n_block--)
olav 0:4c1fcbfcc7bf 602 {
olav 0:4c1fcbfcc7bf 603 xor_block(iv, in);
olav 0:4c1fcbfcc7bf 604 if(aes_encrypt(iv, iv, ctx) != EXIT_SUCCESS)
olav 0:4c1fcbfcc7bf 605 return EXIT_FAILURE;
olav 0:4c1fcbfcc7bf 606 //memcpy(out, iv, N_BLOCK);
olav 0:4c1fcbfcc7bf 607 block_copy(out, iv);
olav 0:4c1fcbfcc7bf 608 in += N_BLOCK;
olav 0:4c1fcbfcc7bf 609 out += N_BLOCK;
olav 0:4c1fcbfcc7bf 610 }
olav 0:4c1fcbfcc7bf 611 return EXIT_SUCCESS;
olav 0:4c1fcbfcc7bf 612 }
olav 0:4c1fcbfcc7bf 613
olav 0:4c1fcbfcc7bf 614 #endif
olav 0:4c1fcbfcc7bf 615
olav 0:4c1fcbfcc7bf 616 #if defined( AES_DEC_PREKEYED )
olav 0:4c1fcbfcc7bf 617
olav 0:4c1fcbfcc7bf 618 /* Decrypt a single block of 16 bytes */
olav 0:4c1fcbfcc7bf 619
olav 0:4c1fcbfcc7bf 620 return_type aes_decrypt( const uint8_t in[N_BLOCK], uint8_t out[N_BLOCK], const aes_context ctx[1] )
olav 0:4c1fcbfcc7bf 621 {
olav 0:4c1fcbfcc7bf 622 if( ctx->rnd )
olav 0:4c1fcbfcc7bf 623 {
olav 0:4c1fcbfcc7bf 624 uint8_t s1[N_BLOCK], r;
olav 0:4c1fcbfcc7bf 625 copy_and_key( s1, in, ctx->ksch + ctx->rnd * N_BLOCK );
olav 0:4c1fcbfcc7bf 626 inv_shift_sub_rows( s1 );
olav 0:4c1fcbfcc7bf 627
olav 0:4c1fcbfcc7bf 628 for( r = ctx->rnd ; --r ; )
olav 0:4c1fcbfcc7bf 629 #if defined( VERSION_1 )
olav 0:4c1fcbfcc7bf 630 {
olav 0:4c1fcbfcc7bf 631 add_round_key( s1, ctx->ksch + r * N_BLOCK );
olav 0:4c1fcbfcc7bf 632 inv_mix_sub_columns( s1 );
olav 0:4c1fcbfcc7bf 633 }
olav 0:4c1fcbfcc7bf 634 #else
olav 0:4c1fcbfcc7bf 635 { uint8_t s2[N_BLOCK];
olav 0:4c1fcbfcc7bf 636 copy_and_key( s2, s1, ctx->ksch + r * N_BLOCK );
olav 0:4c1fcbfcc7bf 637 inv_mix_sub_columns( s1, s2 );
olav 0:4c1fcbfcc7bf 638 }
olav 0:4c1fcbfcc7bf 639 #endif
olav 0:4c1fcbfcc7bf 640 copy_and_key( out, s1, ctx->ksch );
olav 0:4c1fcbfcc7bf 641 }
olav 0:4c1fcbfcc7bf 642 else
olav 0:4c1fcbfcc7bf 643 return -1;
olav 0:4c1fcbfcc7bf 644 return 0;
olav 0:4c1fcbfcc7bf 645 }
olav 0:4c1fcbfcc7bf 646
olav 0:4c1fcbfcc7bf 647 /* CBC decrypt a number of blocks (input and return an IV) */
olav 0:4c1fcbfcc7bf 648
olav 0:4c1fcbfcc7bf 649 return_type aes_cbc_decrypt( const uint8_t *in, uint8_t *out,
olav 0:4c1fcbfcc7bf 650 int32_t n_block, uint8_t iv[N_BLOCK], const aes_context ctx[1] )
olav 0:4c1fcbfcc7bf 651 {
olav 0:4c1fcbfcc7bf 652 while(n_block--)
olav 0:4c1fcbfcc7bf 653 { uint8_t tmp[N_BLOCK];
olav 0:4c1fcbfcc7bf 654
olav 0:4c1fcbfcc7bf 655 //memcpy(tmp, in, N_BLOCK);
olav 0:4c1fcbfcc7bf 656 block_copy(tmp, in);
olav 0:4c1fcbfcc7bf 657 if(aes_decrypt(in, out, ctx) != EXIT_SUCCESS)
olav 0:4c1fcbfcc7bf 658 return EXIT_FAILURE;
olav 0:4c1fcbfcc7bf 659 xor_block(out, iv);
olav 0:4c1fcbfcc7bf 660 //memcpy(iv, tmp, N_BLOCK);
olav 0:4c1fcbfcc7bf 661 block_copy(iv, tmp);
olav 0:4c1fcbfcc7bf 662 in += N_BLOCK;
olav 0:4c1fcbfcc7bf 663 out += N_BLOCK;
olav 0:4c1fcbfcc7bf 664 }
olav 0:4c1fcbfcc7bf 665 return EXIT_SUCCESS;
olav 0:4c1fcbfcc7bf 666 }
olav 0:4c1fcbfcc7bf 667
olav 0:4c1fcbfcc7bf 668 #endif
olav 0:4c1fcbfcc7bf 669
olav 0:4c1fcbfcc7bf 670 #if defined( AES_ENC_128_OTFK )
olav 0:4c1fcbfcc7bf 671
olav 0:4c1fcbfcc7bf 672 /* The 'on the fly' encryption key update for for 128 bit keys */
olav 0:4c1fcbfcc7bf 673
olav 0:4c1fcbfcc7bf 674 static void update_encrypt_key_128( uint8_t k[N_BLOCK], uint8_t *rc )
olav 0:4c1fcbfcc7bf 675 { uint8_t cc;
olav 0:4c1fcbfcc7bf 676
olav 0:4c1fcbfcc7bf 677 k[0] ^= s_box(k[13]) ^ *rc;
olav 0:4c1fcbfcc7bf 678 k[1] ^= s_box(k[14]);
olav 0:4c1fcbfcc7bf 679 k[2] ^= s_box(k[15]);
olav 0:4c1fcbfcc7bf 680 k[3] ^= s_box(k[12]);
olav 0:4c1fcbfcc7bf 681 *rc = f2( *rc );
olav 0:4c1fcbfcc7bf 682
olav 0:4c1fcbfcc7bf 683 for(cc = 4; cc < 16; cc += 4 )
olav 0:4c1fcbfcc7bf 684 {
olav 0:4c1fcbfcc7bf 685 k[cc + 0] ^= k[cc - 4];
olav 0:4c1fcbfcc7bf 686 k[cc + 1] ^= k[cc - 3];
olav 0:4c1fcbfcc7bf 687 k[cc + 2] ^= k[cc - 2];
olav 0:4c1fcbfcc7bf 688 k[cc + 3] ^= k[cc - 1];
olav 0:4c1fcbfcc7bf 689 }
olav 0:4c1fcbfcc7bf 690 }
olav 0:4c1fcbfcc7bf 691
olav 0:4c1fcbfcc7bf 692 /* Encrypt a single block of 16 bytes with 'on the fly' 128 bit keying */
olav 0:4c1fcbfcc7bf 693
olav 0:4c1fcbfcc7bf 694 void aes_encrypt_128( const uint8_t in[N_BLOCK], uint8_t out[N_BLOCK],
olav 0:4c1fcbfcc7bf 695 const uint8_t key[N_BLOCK], uint8_t o_key[N_BLOCK] )
olav 0:4c1fcbfcc7bf 696 { uint8_t s1[N_BLOCK], r, rc = 1;
olav 0:4c1fcbfcc7bf 697
olav 0:4c1fcbfcc7bf 698 if(o_key != key)
olav 0:4c1fcbfcc7bf 699 block_copy( o_key, key );
olav 0:4c1fcbfcc7bf 700 copy_and_key( s1, in, o_key );
olav 0:4c1fcbfcc7bf 701
olav 0:4c1fcbfcc7bf 702 for( r = 1 ; r < 10 ; ++r )
olav 0:4c1fcbfcc7bf 703 #if defined( VERSION_1 )
olav 0:4c1fcbfcc7bf 704 {
olav 0:4c1fcbfcc7bf 705 mix_sub_columns( s1 );
olav 0:4c1fcbfcc7bf 706 update_encrypt_key_128( o_key, &rc );
olav 0:4c1fcbfcc7bf 707 add_round_key( s1, o_key );
olav 0:4c1fcbfcc7bf 708 }
olav 0:4c1fcbfcc7bf 709 #else
olav 0:4c1fcbfcc7bf 710 { uint8_t s2[N_BLOCK];
olav 0:4c1fcbfcc7bf 711 mix_sub_columns( s2, s1 );
olav 0:4c1fcbfcc7bf 712 update_encrypt_key_128( o_key, &rc );
olav 0:4c1fcbfcc7bf 713 copy_and_key( s1, s2, o_key );
olav 0:4c1fcbfcc7bf 714 }
olav 0:4c1fcbfcc7bf 715 #endif
olav 0:4c1fcbfcc7bf 716
olav 0:4c1fcbfcc7bf 717 shift_sub_rows( s1 );
olav 0:4c1fcbfcc7bf 718 update_encrypt_key_128( o_key, &rc );
olav 0:4c1fcbfcc7bf 719 copy_and_key( out, s1, o_key );
olav 0:4c1fcbfcc7bf 720 }
olav 0:4c1fcbfcc7bf 721
olav 0:4c1fcbfcc7bf 722 #endif
olav 0:4c1fcbfcc7bf 723
olav 0:4c1fcbfcc7bf 724 #if defined( AES_DEC_128_OTFK )
olav 0:4c1fcbfcc7bf 725
olav 0:4c1fcbfcc7bf 726 /* The 'on the fly' decryption key update for for 128 bit keys */
olav 0:4c1fcbfcc7bf 727
olav 0:4c1fcbfcc7bf 728 static void update_decrypt_key_128( uint8_t k[N_BLOCK], uint8_t *rc )
olav 0:4c1fcbfcc7bf 729 { uint8_t cc;
olav 0:4c1fcbfcc7bf 730
olav 0:4c1fcbfcc7bf 731 for( cc = 12; cc > 0; cc -= 4 )
olav 0:4c1fcbfcc7bf 732 {
olav 0:4c1fcbfcc7bf 733 k[cc + 0] ^= k[cc - 4];
olav 0:4c1fcbfcc7bf 734 k[cc + 1] ^= k[cc - 3];
olav 0:4c1fcbfcc7bf 735 k[cc + 2] ^= k[cc - 2];
olav 0:4c1fcbfcc7bf 736 k[cc + 3] ^= k[cc - 1];
olav 0:4c1fcbfcc7bf 737 }
olav 0:4c1fcbfcc7bf 738 *rc = d2(*rc);
olav 0:4c1fcbfcc7bf 739 k[0] ^= s_box(k[13]) ^ *rc;
olav 0:4c1fcbfcc7bf 740 k[1] ^= s_box(k[14]);
olav 0:4c1fcbfcc7bf 741 k[2] ^= s_box(k[15]);
olav 0:4c1fcbfcc7bf 742 k[3] ^= s_box(k[12]);
olav 0:4c1fcbfcc7bf 743 }
olav 0:4c1fcbfcc7bf 744
olav 0:4c1fcbfcc7bf 745 /* Decrypt a single block of 16 bytes with 'on the fly' 128 bit keying */
olav 0:4c1fcbfcc7bf 746
olav 0:4c1fcbfcc7bf 747 void aes_decrypt_128( const uint8_t in[N_BLOCK], uint8_t out[N_BLOCK],
olav 0:4c1fcbfcc7bf 748 const uint8_t key[N_BLOCK], uint8_t o_key[N_BLOCK] )
olav 0:4c1fcbfcc7bf 749 {
olav 0:4c1fcbfcc7bf 750 uint8_t s1[N_BLOCK], r, rc = 0x6c;
olav 0:4c1fcbfcc7bf 751 if(o_key != key)
olav 0:4c1fcbfcc7bf 752 block_copy( o_key, key );
olav 0:4c1fcbfcc7bf 753
olav 0:4c1fcbfcc7bf 754 copy_and_key( s1, in, o_key );
olav 0:4c1fcbfcc7bf 755 inv_shift_sub_rows( s1 );
olav 0:4c1fcbfcc7bf 756
olav 0:4c1fcbfcc7bf 757 for( r = 10 ; --r ; )
olav 0:4c1fcbfcc7bf 758 #if defined( VERSION_1 )
olav 0:4c1fcbfcc7bf 759 {
olav 0:4c1fcbfcc7bf 760 update_decrypt_key_128( o_key, &rc );
olav 0:4c1fcbfcc7bf 761 add_round_key( s1, o_key );
olav 0:4c1fcbfcc7bf 762 inv_mix_sub_columns( s1 );
olav 0:4c1fcbfcc7bf 763 }
olav 0:4c1fcbfcc7bf 764 #else
olav 0:4c1fcbfcc7bf 765 { uint8_t s2[N_BLOCK];
olav 0:4c1fcbfcc7bf 766 update_decrypt_key_128( o_key, &rc );
olav 0:4c1fcbfcc7bf 767 copy_and_key( s2, s1, o_key );
olav 0:4c1fcbfcc7bf 768 inv_mix_sub_columns( s1, s2 );
olav 0:4c1fcbfcc7bf 769 }
olav 0:4c1fcbfcc7bf 770 #endif
olav 0:4c1fcbfcc7bf 771 update_decrypt_key_128( o_key, &rc );
olav 0:4c1fcbfcc7bf 772 copy_and_key( out, s1, o_key );
olav 0:4c1fcbfcc7bf 773 }
olav 0:4c1fcbfcc7bf 774
olav 0:4c1fcbfcc7bf 775 #endif
olav 0:4c1fcbfcc7bf 776
olav 0:4c1fcbfcc7bf 777 #if defined( AES_ENC_256_OTFK )
olav 0:4c1fcbfcc7bf 778
olav 0:4c1fcbfcc7bf 779 /* The 'on the fly' encryption key update for for 256 bit keys */
olav 0:4c1fcbfcc7bf 780
olav 0:4c1fcbfcc7bf 781 static void update_encrypt_key_256( uint8_t k[2 * N_BLOCK], uint8_t *rc )
olav 0:4c1fcbfcc7bf 782 { uint8_t cc;
olav 0:4c1fcbfcc7bf 783
olav 0:4c1fcbfcc7bf 784 k[0] ^= s_box(k[29]) ^ *rc;
olav 0:4c1fcbfcc7bf 785 k[1] ^= s_box(k[30]);
olav 0:4c1fcbfcc7bf 786 k[2] ^= s_box(k[31]);
olav 0:4c1fcbfcc7bf 787 k[3] ^= s_box(k[28]);
olav 0:4c1fcbfcc7bf 788 *rc = f2( *rc );
olav 0:4c1fcbfcc7bf 789
olav 0:4c1fcbfcc7bf 790 for(cc = 4; cc < 16; cc += 4)
olav 0:4c1fcbfcc7bf 791 {
olav 0:4c1fcbfcc7bf 792 k[cc + 0] ^= k[cc - 4];
olav 0:4c1fcbfcc7bf 793 k[cc + 1] ^= k[cc - 3];
olav 0:4c1fcbfcc7bf 794 k[cc + 2] ^= k[cc - 2];
olav 0:4c1fcbfcc7bf 795 k[cc + 3] ^= k[cc - 1];
olav 0:4c1fcbfcc7bf 796 }
olav 0:4c1fcbfcc7bf 797
olav 0:4c1fcbfcc7bf 798 k[16] ^= s_box(k[12]);
olav 0:4c1fcbfcc7bf 799 k[17] ^= s_box(k[13]);
olav 0:4c1fcbfcc7bf 800 k[18] ^= s_box(k[14]);
olav 0:4c1fcbfcc7bf 801 k[19] ^= s_box(k[15]);
olav 0:4c1fcbfcc7bf 802
olav 0:4c1fcbfcc7bf 803 for( cc = 20; cc < 32; cc += 4 )
olav 0:4c1fcbfcc7bf 804 {
olav 0:4c1fcbfcc7bf 805 k[cc + 0] ^= k[cc - 4];
olav 0:4c1fcbfcc7bf 806 k[cc + 1] ^= k[cc - 3];
olav 0:4c1fcbfcc7bf 807 k[cc + 2] ^= k[cc - 2];
olav 0:4c1fcbfcc7bf 808 k[cc + 3] ^= k[cc - 1];
olav 0:4c1fcbfcc7bf 809 }
olav 0:4c1fcbfcc7bf 810 }
olav 0:4c1fcbfcc7bf 811
olav 0:4c1fcbfcc7bf 812 /* Encrypt a single block of 16 bytes with 'on the fly' 256 bit keying */
olav 0:4c1fcbfcc7bf 813
olav 0:4c1fcbfcc7bf 814 void aes_encrypt_256( const uint8_t in[N_BLOCK], uint8_t out[N_BLOCK],
olav 0:4c1fcbfcc7bf 815 const uint8_t key[2 * N_BLOCK], uint8_t o_key[2 * N_BLOCK] )
olav 0:4c1fcbfcc7bf 816 {
olav 0:4c1fcbfcc7bf 817 uint8_t s1[N_BLOCK], r, rc = 1;
olav 0:4c1fcbfcc7bf 818 if(o_key != key)
olav 0:4c1fcbfcc7bf 819 {
olav 0:4c1fcbfcc7bf 820 block_copy( o_key, key );
olav 0:4c1fcbfcc7bf 821 block_copy( o_key + 16, key + 16 );
olav 0:4c1fcbfcc7bf 822 }
olav 0:4c1fcbfcc7bf 823 copy_and_key( s1, in, o_key );
olav 0:4c1fcbfcc7bf 824
olav 0:4c1fcbfcc7bf 825 for( r = 1 ; r < 14 ; ++r )
olav 0:4c1fcbfcc7bf 826 #if defined( VERSION_1 )
olav 0:4c1fcbfcc7bf 827 {
olav 0:4c1fcbfcc7bf 828 mix_sub_columns(s1);
olav 0:4c1fcbfcc7bf 829 if( r & 1 )
olav 0:4c1fcbfcc7bf 830 add_round_key( s1, o_key + 16 );
olav 0:4c1fcbfcc7bf 831 else
olav 0:4c1fcbfcc7bf 832 {
olav 0:4c1fcbfcc7bf 833 update_encrypt_key_256( o_key, &rc );
olav 0:4c1fcbfcc7bf 834 add_round_key( s1, o_key );
olav 0:4c1fcbfcc7bf 835 }
olav 0:4c1fcbfcc7bf 836 }
olav 0:4c1fcbfcc7bf 837 #else
olav 0:4c1fcbfcc7bf 838 { uint8_t s2[N_BLOCK];
olav 0:4c1fcbfcc7bf 839 mix_sub_columns( s2, s1 );
olav 0:4c1fcbfcc7bf 840 if( r & 1 )
olav 0:4c1fcbfcc7bf 841 copy_and_key( s1, s2, o_key + 16 );
olav 0:4c1fcbfcc7bf 842 else
olav 0:4c1fcbfcc7bf 843 {
olav 0:4c1fcbfcc7bf 844 update_encrypt_key_256( o_key, &rc );
olav 0:4c1fcbfcc7bf 845 copy_and_key( s1, s2, o_key );
olav 0:4c1fcbfcc7bf 846 }
olav 0:4c1fcbfcc7bf 847 }
olav 0:4c1fcbfcc7bf 848 #endif
olav 0:4c1fcbfcc7bf 849
olav 0:4c1fcbfcc7bf 850 shift_sub_rows( s1 );
olav 0:4c1fcbfcc7bf 851 update_encrypt_key_256( o_key, &rc );
olav 0:4c1fcbfcc7bf 852 copy_and_key( out, s1, o_key );
olav 0:4c1fcbfcc7bf 853 }
olav 0:4c1fcbfcc7bf 854
olav 0:4c1fcbfcc7bf 855 #endif
olav 0:4c1fcbfcc7bf 856
olav 0:4c1fcbfcc7bf 857 #if defined( AES_DEC_256_OTFK )
olav 0:4c1fcbfcc7bf 858
olav 0:4c1fcbfcc7bf 859 /* The 'on the fly' encryption key update for for 256 bit keys */
olav 0:4c1fcbfcc7bf 860
olav 0:4c1fcbfcc7bf 861 static void update_decrypt_key_256( uint8_t k[2 * N_BLOCK], uint8_t *rc )
olav 0:4c1fcbfcc7bf 862 { uint8_t cc;
olav 0:4c1fcbfcc7bf 863
olav 0:4c1fcbfcc7bf 864 for(cc = 28; cc > 16; cc -= 4)
olav 0:4c1fcbfcc7bf 865 {
olav 0:4c1fcbfcc7bf 866 k[cc + 0] ^= k[cc - 4];
olav 0:4c1fcbfcc7bf 867 k[cc + 1] ^= k[cc - 3];
olav 0:4c1fcbfcc7bf 868 k[cc + 2] ^= k[cc - 2];
olav 0:4c1fcbfcc7bf 869 k[cc + 3] ^= k[cc - 1];
olav 0:4c1fcbfcc7bf 870 }
olav 0:4c1fcbfcc7bf 871
olav 0:4c1fcbfcc7bf 872 k[16] ^= s_box(k[12]);
olav 0:4c1fcbfcc7bf 873 k[17] ^= s_box(k[13]);
olav 0:4c1fcbfcc7bf 874 k[18] ^= s_box(k[14]);
olav 0:4c1fcbfcc7bf 875 k[19] ^= s_box(k[15]);
olav 0:4c1fcbfcc7bf 876
olav 0:4c1fcbfcc7bf 877 for(cc = 12; cc > 0; cc -= 4)
olav 0:4c1fcbfcc7bf 878 {
olav 0:4c1fcbfcc7bf 879 k[cc + 0] ^= k[cc - 4];
olav 0:4c1fcbfcc7bf 880 k[cc + 1] ^= k[cc - 3];
olav 0:4c1fcbfcc7bf 881 k[cc + 2] ^= k[cc - 2];
olav 0:4c1fcbfcc7bf 882 k[cc + 3] ^= k[cc - 1];
olav 0:4c1fcbfcc7bf 883 }
olav 0:4c1fcbfcc7bf 884
olav 0:4c1fcbfcc7bf 885 *rc = d2(*rc);
olav 0:4c1fcbfcc7bf 886 k[0] ^= s_box(k[29]) ^ *rc;
olav 0:4c1fcbfcc7bf 887 k[1] ^= s_box(k[30]);
olav 0:4c1fcbfcc7bf 888 k[2] ^= s_box(k[31]);
olav 0:4c1fcbfcc7bf 889 k[3] ^= s_box(k[28]);
olav 0:4c1fcbfcc7bf 890 }
olav 0:4c1fcbfcc7bf 891
olav 0:4c1fcbfcc7bf 892 /* Decrypt a single block of 16 bytes with 'on the fly'
olav 0:4c1fcbfcc7bf 893 256 bit keying
olav 0:4c1fcbfcc7bf 894 */
olav 0:4c1fcbfcc7bf 895 void aes_decrypt_256( const uint8_t in[N_BLOCK], uint8_t out[N_BLOCK],
olav 0:4c1fcbfcc7bf 896 const uint8_t key[2 * N_BLOCK], uint8_t o_key[2 * N_BLOCK] )
olav 0:4c1fcbfcc7bf 897 {
olav 0:4c1fcbfcc7bf 898 uint8_t s1[N_BLOCK], r, rc = 0x80;
olav 0:4c1fcbfcc7bf 899
olav 0:4c1fcbfcc7bf 900 if(o_key != key)
olav 0:4c1fcbfcc7bf 901 {
olav 0:4c1fcbfcc7bf 902 block_copy( o_key, key );
olav 0:4c1fcbfcc7bf 903 block_copy( o_key + 16, key + 16 );
olav 0:4c1fcbfcc7bf 904 }
olav 0:4c1fcbfcc7bf 905
olav 0:4c1fcbfcc7bf 906 copy_and_key( s1, in, o_key );
olav 0:4c1fcbfcc7bf 907 inv_shift_sub_rows( s1 );
olav 0:4c1fcbfcc7bf 908
olav 0:4c1fcbfcc7bf 909 for( r = 14 ; --r ; )
olav 0:4c1fcbfcc7bf 910 #if defined( VERSION_1 )
olav 0:4c1fcbfcc7bf 911 {
olav 0:4c1fcbfcc7bf 912 if( ( r & 1 ) )
olav 0:4c1fcbfcc7bf 913 {
olav 0:4c1fcbfcc7bf 914 update_decrypt_key_256( o_key, &rc );
olav 0:4c1fcbfcc7bf 915 add_round_key( s1, o_key + 16 );
olav 0:4c1fcbfcc7bf 916 }
olav 0:4c1fcbfcc7bf 917 else
olav 0:4c1fcbfcc7bf 918 add_round_key( s1, o_key );
olav 0:4c1fcbfcc7bf 919 inv_mix_sub_columns( s1 );
olav 0:4c1fcbfcc7bf 920 }
olav 0:4c1fcbfcc7bf 921 #else
olav 0:4c1fcbfcc7bf 922 { uint8_t s2[N_BLOCK];
olav 0:4c1fcbfcc7bf 923 if( ( r & 1 ) )
olav 0:4c1fcbfcc7bf 924 {
olav 0:4c1fcbfcc7bf 925 update_decrypt_key_256( o_key, &rc );
olav 0:4c1fcbfcc7bf 926 copy_and_key( s2, s1, o_key + 16 );
olav 0:4c1fcbfcc7bf 927 }
olav 0:4c1fcbfcc7bf 928 else
olav 0:4c1fcbfcc7bf 929 copy_and_key( s2, s1, o_key );
olav 0:4c1fcbfcc7bf 930 inv_mix_sub_columns( s1, s2 );
olav 0:4c1fcbfcc7bf 931 }
olav 0:4c1fcbfcc7bf 932 #endif
olav 0:4c1fcbfcc7bf 933 copy_and_key( out, s1, o_key );
olav 0:4c1fcbfcc7bf 934 }
olav 0:4c1fcbfcc7bf 935
olav 0:4c1fcbfcc7bf 936 #endif