Rishin Amin / Mbed 2 deprecated Dinghy_RaceTrak_Node_GPS_with_LoRa

Dependencies:   mbed LoRaWAN-lib_gps_lora SingleFrequencyLora

Committer:
Rishin
Date:
Wed Nov 29 15:01:09 2017 +0000
Revision:
15:b4d11baea8bc
Parent:
13:66d854ad31d8
publishining gps with lora

Who changed what in which revision?

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