Zheng Ping / LoRaMacLib

Dependents:   LoRaWAN_MBED

Fork of LoRaMacLib by LoRa All

Committer:
GregCr
Date:
Wed Aug 12 14:08:29 2015 +0000
Revision:
0:9be122c18509
First Implementation

Who changed what in which revision?

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