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