RadioShuttle Lib for the STM32 L4 Heltec Board
Dependents: Turtle_RadioShuttle
util/aes.c@13:591254bed18b, 2019-04-14 (annotated)
- Committer:
- Helmut Tschemernjak
- Date:
- Sun Apr 14 18:35:26 2019 +0200
- Revision:
- 13:591254bed18b
- Parent:
- 0:0c31756924a2
Updated RadioStatus to be in common with mbed and Arduino
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Helmut64 | 0:0c31756924a2 | 1 | /* |
Helmut64 | 0:0c31756924a2 | 2 | Downloaded from here: https://github.com/kokke/tiny-AES128-C |
Helmut64 | 0:0c31756924a2 | 3 | This is an implementation of the AES128 algorithm, specifically ECB and CBC mode. |
Helmut64 | 0:0c31756924a2 | 4 | |
Helmut64 | 0:0c31756924a2 | 5 | The implementation is verified against the test vectors in: |
Helmut64 | 0:0c31756924a2 | 6 | National Institute of Standards and Technology Special Publication 800-38A 2001 ED |
Helmut64 | 0:0c31756924a2 | 7 | |
Helmut64 | 0:0c31756924a2 | 8 | ECB-AES128 |
Helmut64 | 0:0c31756924a2 | 9 | ---------- |
Helmut64 | 0:0c31756924a2 | 10 | |
Helmut64 | 0:0c31756924a2 | 11 | plain-text: |
Helmut64 | 0:0c31756924a2 | 12 | 6bc1bee22e409f96e93d7e117393172a |
Helmut64 | 0:0c31756924a2 | 13 | ae2d8a571e03ac9c9eb76fac45af8e51 |
Helmut64 | 0:0c31756924a2 | 14 | 30c81c46a35ce411e5fbc1191a0a52ef |
Helmut64 | 0:0c31756924a2 | 15 | f69f2445df4f9b17ad2b417be66c3710 |
Helmut64 | 0:0c31756924a2 | 16 | |
Helmut64 | 0:0c31756924a2 | 17 | key: |
Helmut64 | 0:0c31756924a2 | 18 | 2b7e151628aed2a6abf7158809cf4f3c |
Helmut64 | 0:0c31756924a2 | 19 | |
Helmut64 | 0:0c31756924a2 | 20 | resulting cipher |
Helmut64 | 0:0c31756924a2 | 21 | 3ad77bb40d7a3660a89ecaf32466ef97 |
Helmut64 | 0:0c31756924a2 | 22 | f5d3d58503b9699de785895a96fdbaaf |
Helmut64 | 0:0c31756924a2 | 23 | 43b1cd7f598ece23881b00e3ed030688 |
Helmut64 | 0:0c31756924a2 | 24 | 7b0c785e27e8ad3f8223207104725dd4 |
Helmut64 | 0:0c31756924a2 | 25 | |
Helmut64 | 0:0c31756924a2 | 26 | |
Helmut64 | 0:0c31756924a2 | 27 | NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0) |
Helmut64 | 0:0c31756924a2 | 28 | You should pad the end of the string with zeros if this is not the case. |
Helmut64 | 0:0c31756924a2 | 29 | |
Helmut64 | 0:0c31756924a2 | 30 | */ |
Helmut64 | 0:0c31756924a2 | 31 | |
Helmut64 | 0:0c31756924a2 | 32 | |
Helmut64 | 0:0c31756924a2 | 33 | /*****************************************************************************/ |
Helmut64 | 0:0c31756924a2 | 34 | /* Includes: */ |
Helmut64 | 0:0c31756924a2 | 35 | /*****************************************************************************/ |
Helmut64 | 0:0c31756924a2 | 36 | #include <stdint.h> |
Helmut64 | 0:0c31756924a2 | 37 | #include <string.h> // CBC mode, for memset |
Helmut64 | 0:0c31756924a2 | 38 | #include "aes.h" |
Helmut64 | 0:0c31756924a2 | 39 | |
Helmut64 | 0:0c31756924a2 | 40 | /*****************************************************************************/ |
Helmut64 | 0:0c31756924a2 | 41 | /* Defines: */ |
Helmut64 | 0:0c31756924a2 | 42 | /*****************************************************************************/ |
Helmut64 | 0:0c31756924a2 | 43 | // The number of columns comprising a state in AES. This is a constant in AES. Value=4 |
Helmut64 | 0:0c31756924a2 | 44 | #define Nb 4 |
Helmut64 | 0:0c31756924a2 | 45 | // The number of 32 bit words in a key. |
Helmut64 | 0:0c31756924a2 | 46 | #define Nk 4 |
Helmut64 | 0:0c31756924a2 | 47 | |
Helmut64 | 0:0c31756924a2 | 48 | // jcallan@github points out that declaring Multiply as a function |
Helmut64 | 0:0c31756924a2 | 49 | // reduces code size considerably with the Keil ARM compiler. |
Helmut64 | 0:0c31756924a2 | 50 | // See this link for more information: https://github.com/kokke/tiny-AES128-C/pull/3 |
Helmut64 | 0:0c31756924a2 | 51 | #ifndef MULTIPLY_AS_A_FUNCTION |
Helmut64 | 0:0c31756924a2 | 52 | #define MULTIPLY_AS_A_FUNCTION 1 |
Helmut64 | 0:0c31756924a2 | 53 | #endif |
Helmut64 | 0:0c31756924a2 | 54 | |
Helmut64 | 0:0c31756924a2 | 55 | /* |
Helmut64 | 0:0c31756924a2 | 56 | * simple re-defines do use the existing code with a context |
Helmut64 | 0:0c31756924a2 | 57 | */ |
Helmut64 | 0:0c31756924a2 | 58 | #define state ctx->state |
Helmut64 | 0:0c31756924a2 | 59 | #define RoundKey ctx->RoundKey |
Helmut64 | 0:0c31756924a2 | 60 | #define Iv ctx->InitialVector |
Helmut64 | 0:0c31756924a2 | 61 | #define KEYLEN AES128_KEYLEN |
Helmut64 | 0:0c31756924a2 | 62 | |
Helmut64 | 0:0c31756924a2 | 63 | // The lookup-tables are marked const so they can be placed in read-only storage instead of RAM |
Helmut64 | 0:0c31756924a2 | 64 | // The numbers below can be computed dynamically trading ROM for RAM - |
Helmut64 | 0:0c31756924a2 | 65 | // This can be useful in (embedded) bootloader applications, where ROM is often limited. |
Helmut64 | 0:0c31756924a2 | 66 | static const uint8_t sbox[256] = { |
Helmut64 | 0:0c31756924a2 | 67 | //0 1 2 3 4 5 6 7 8 9 A B C D E F |
Helmut64 | 0:0c31756924a2 | 68 | 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
Helmut64 | 0:0c31756924a2 | 69 | 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
Helmut64 | 0:0c31756924a2 | 70 | 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
Helmut64 | 0:0c31756924a2 | 71 | 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
Helmut64 | 0:0c31756924a2 | 72 | 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
Helmut64 | 0:0c31756924a2 | 73 | 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
Helmut64 | 0:0c31756924a2 | 74 | 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
Helmut64 | 0:0c31756924a2 | 75 | 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
Helmut64 | 0:0c31756924a2 | 76 | 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
Helmut64 | 0:0c31756924a2 | 77 | 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
Helmut64 | 0:0c31756924a2 | 78 | 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
Helmut64 | 0:0c31756924a2 | 79 | 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
Helmut64 | 0:0c31756924a2 | 80 | 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
Helmut64 | 0:0c31756924a2 | 81 | 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
Helmut64 | 0:0c31756924a2 | 82 | 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
Helmut64 | 0:0c31756924a2 | 83 | 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; |
Helmut64 | 0:0c31756924a2 | 84 | |
Helmut64 | 0:0c31756924a2 | 85 | static const uint8_t rsbox[256] = |
Helmut64 | 0:0c31756924a2 | 86 | { 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, |
Helmut64 | 0:0c31756924a2 | 87 | 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, |
Helmut64 | 0:0c31756924a2 | 88 | 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, |
Helmut64 | 0:0c31756924a2 | 89 | 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, |
Helmut64 | 0:0c31756924a2 | 90 | 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, |
Helmut64 | 0:0c31756924a2 | 91 | 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, |
Helmut64 | 0:0c31756924a2 | 92 | 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, |
Helmut64 | 0:0c31756924a2 | 93 | 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, |
Helmut64 | 0:0c31756924a2 | 94 | 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, |
Helmut64 | 0:0c31756924a2 | 95 | 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, |
Helmut64 | 0:0c31756924a2 | 96 | 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, |
Helmut64 | 0:0c31756924a2 | 97 | 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, |
Helmut64 | 0:0c31756924a2 | 98 | 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, |
Helmut64 | 0:0c31756924a2 | 99 | 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, |
Helmut64 | 0:0c31756924a2 | 100 | 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, |
Helmut64 | 0:0c31756924a2 | 101 | 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; |
Helmut64 | 0:0c31756924a2 | 102 | |
Helmut64 | 0:0c31756924a2 | 103 | |
Helmut64 | 0:0c31756924a2 | 104 | // The round constant word array, Rcon[i], contains the values given by |
Helmut64 | 0:0c31756924a2 | 105 | // x to th e power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8) |
Helmut64 | 0:0c31756924a2 | 106 | // Note that i starts at 1, not 0). |
Helmut64 | 0:0c31756924a2 | 107 | static const uint8_t Rcon[10] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; |
Helmut64 | 0:0c31756924a2 | 108 | |
Helmut64 | 0:0c31756924a2 | 109 | |
Helmut64 | 0:0c31756924a2 | 110 | /*****************************************************************************/ |
Helmut64 | 0:0c31756924a2 | 111 | /* Private functions: */ |
Helmut64 | 0:0c31756924a2 | 112 | /*****************************************************************************/ |
Helmut64 | 0:0c31756924a2 | 113 | static uint8_t getSBoxValue(uint8_t num) |
Helmut64 | 0:0c31756924a2 | 114 | { |
Helmut64 | 0:0c31756924a2 | 115 | return sbox[num]; |
Helmut64 | 0:0c31756924a2 | 116 | } |
Helmut64 | 0:0c31756924a2 | 117 | |
Helmut64 | 0:0c31756924a2 | 118 | static uint8_t getSBoxInvert(uint8_t num) |
Helmut64 | 0:0c31756924a2 | 119 | { |
Helmut64 | 0:0c31756924a2 | 120 | return rsbox[num]; |
Helmut64 | 0:0c31756924a2 | 121 | } |
Helmut64 | 0:0c31756924a2 | 122 | |
Helmut64 | 0:0c31756924a2 | 123 | // This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states. |
Helmut64 | 0:0c31756924a2 | 124 | static void KeyExpansion(AES_CTX *ctx, const uint8_t* Key) |
Helmut64 | 0:0c31756924a2 | 125 | { |
Helmut64 | 0:0c31756924a2 | 126 | uint32_t i, j, k; |
Helmut64 | 0:0c31756924a2 | 127 | uint8_t tempa[4]; // Used for the column/row operations |
Helmut64 | 0:0c31756924a2 | 128 | |
Helmut64 | 0:0c31756924a2 | 129 | // The first round key is the key itself. |
Helmut64 | 0:0c31756924a2 | 130 | for(i = 0; i < Nk; ++i) |
Helmut64 | 0:0c31756924a2 | 131 | { |
Helmut64 | 0:0c31756924a2 | 132 | RoundKey[(i * 4) + 0] = Key[(i * 4) + 0]; |
Helmut64 | 0:0c31756924a2 | 133 | RoundKey[(i * 4) + 1] = Key[(i * 4) + 1]; |
Helmut64 | 0:0c31756924a2 | 134 | RoundKey[(i * 4) + 2] = Key[(i * 4) + 2]; |
Helmut64 | 0:0c31756924a2 | 135 | RoundKey[(i * 4) + 3] = Key[(i * 4) + 3]; |
Helmut64 | 0:0c31756924a2 | 136 | } |
Helmut64 | 0:0c31756924a2 | 137 | |
Helmut64 | 0:0c31756924a2 | 138 | // All other round keys are found from the previous round keys. |
Helmut64 | 0:0c31756924a2 | 139 | for(; (i < (Nb * (Nr + 1))); ++i) |
Helmut64 | 0:0c31756924a2 | 140 | { |
Helmut64 | 0:0c31756924a2 | 141 | for(j = 0; j < 4; ++j) |
Helmut64 | 0:0c31756924a2 | 142 | { |
Helmut64 | 0:0c31756924a2 | 143 | tempa[j]=RoundKey[(i-1) * 4 + j]; |
Helmut64 | 0:0c31756924a2 | 144 | } |
Helmut64 | 0:0c31756924a2 | 145 | if (i % Nk == 0) |
Helmut64 | 0:0c31756924a2 | 146 | { |
Helmut64 | 0:0c31756924a2 | 147 | // This function rotates the 4 bytes in a word to the left once. |
Helmut64 | 0:0c31756924a2 | 148 | // [a0,a1,a2,a3] becomes [a1,a2,a3,a0] |
Helmut64 | 0:0c31756924a2 | 149 | |
Helmut64 | 0:0c31756924a2 | 150 | // Function RotWord() |
Helmut64 | 0:0c31756924a2 | 151 | { |
Helmut64 | 0:0c31756924a2 | 152 | k = tempa[0]; |
Helmut64 | 0:0c31756924a2 | 153 | tempa[0] = tempa[1]; |
Helmut64 | 0:0c31756924a2 | 154 | tempa[1] = tempa[2]; |
Helmut64 | 0:0c31756924a2 | 155 | tempa[2] = tempa[3]; |
Helmut64 | 0:0c31756924a2 | 156 | tempa[3] = k; |
Helmut64 | 0:0c31756924a2 | 157 | } |
Helmut64 | 0:0c31756924a2 | 158 | |
Helmut64 | 0:0c31756924a2 | 159 | // SubWord() is a function that takes a four-byte input word and |
Helmut64 | 0:0c31756924a2 | 160 | // applies the S-box to each of the four bytes to produce an output word. |
Helmut64 | 0:0c31756924a2 | 161 | |
Helmut64 | 0:0c31756924a2 | 162 | // Function Subword() |
Helmut64 | 0:0c31756924a2 | 163 | { |
Helmut64 | 0:0c31756924a2 | 164 | tempa[0] = getSBoxValue(tempa[0]); |
Helmut64 | 0:0c31756924a2 | 165 | tempa[1] = getSBoxValue(tempa[1]); |
Helmut64 | 0:0c31756924a2 | 166 | tempa[2] = getSBoxValue(tempa[2]); |
Helmut64 | 0:0c31756924a2 | 167 | tempa[3] = getSBoxValue(tempa[3]); |
Helmut64 | 0:0c31756924a2 | 168 | } |
Helmut64 | 0:0c31756924a2 | 169 | |
Helmut64 | 0:0c31756924a2 | 170 | tempa[0] = tempa[0] ^ Rcon[(i/Nk)-1]; |
Helmut64 | 0:0c31756924a2 | 171 | } |
Helmut64 | 0:0c31756924a2 | 172 | else if (Nk > 6 && i % Nk == 4) |
Helmut64 | 0:0c31756924a2 | 173 | { |
Helmut64 | 0:0c31756924a2 | 174 | // Function Subword() |
Helmut64 | 0:0c31756924a2 | 175 | { |
Helmut64 | 0:0c31756924a2 | 176 | tempa[0] = getSBoxValue(tempa[0]); |
Helmut64 | 0:0c31756924a2 | 177 | tempa[1] = getSBoxValue(tempa[1]); |
Helmut64 | 0:0c31756924a2 | 178 | tempa[2] = getSBoxValue(tempa[2]); |
Helmut64 | 0:0c31756924a2 | 179 | tempa[3] = getSBoxValue(tempa[3]); |
Helmut64 | 0:0c31756924a2 | 180 | } |
Helmut64 | 0:0c31756924a2 | 181 | } |
Helmut64 | 0:0c31756924a2 | 182 | RoundKey[i * 4 + 0] = RoundKey[(i - Nk) * 4 + 0] ^ tempa[0]; |
Helmut64 | 0:0c31756924a2 | 183 | RoundKey[i * 4 + 1] = RoundKey[(i - Nk) * 4 + 1] ^ tempa[1]; |
Helmut64 | 0:0c31756924a2 | 184 | RoundKey[i * 4 + 2] = RoundKey[(i - Nk) * 4 + 2] ^ tempa[2]; |
Helmut64 | 0:0c31756924a2 | 185 | RoundKey[i * 4 + 3] = RoundKey[(i - Nk) * 4 + 3] ^ tempa[3]; |
Helmut64 | 0:0c31756924a2 | 186 | } |
Helmut64 | 0:0c31756924a2 | 187 | } |
Helmut64 | 0:0c31756924a2 | 188 | |
Helmut64 | 0:0c31756924a2 | 189 | // This function adds the round key to state. |
Helmut64 | 0:0c31756924a2 | 190 | // The round key is added to the state by an XOR function. |
Helmut64 | 0:0c31756924a2 | 191 | static void AddRoundKey(AES_CTX *ctx, uint8_t round) |
Helmut64 | 0:0c31756924a2 | 192 | { |
Helmut64 | 0:0c31756924a2 | 193 | uint8_t i,j; |
Helmut64 | 0:0c31756924a2 | 194 | for(i=0;i<4;++i) |
Helmut64 | 0:0c31756924a2 | 195 | { |
Helmut64 | 0:0c31756924a2 | 196 | for(j = 0; j < 4; ++j) |
Helmut64 | 0:0c31756924a2 | 197 | { |
Helmut64 | 0:0c31756924a2 | 198 | (*state)[i][j] ^= RoundKey[round * Nb * 4 + i * Nb + j]; |
Helmut64 | 0:0c31756924a2 | 199 | } |
Helmut64 | 0:0c31756924a2 | 200 | } |
Helmut64 | 0:0c31756924a2 | 201 | } |
Helmut64 | 0:0c31756924a2 | 202 | |
Helmut64 | 0:0c31756924a2 | 203 | // The SubBytes Function Substitutes the values in the |
Helmut64 | 0:0c31756924a2 | 204 | // state matrix with values in an S-box. |
Helmut64 | 0:0c31756924a2 | 205 | static void SubBytes(AES_CTX *ctx) |
Helmut64 | 0:0c31756924a2 | 206 | { |
Helmut64 | 0:0c31756924a2 | 207 | uint8_t i, j; |
Helmut64 | 0:0c31756924a2 | 208 | for(i = 0; i < 4; ++i) |
Helmut64 | 0:0c31756924a2 | 209 | { |
Helmut64 | 0:0c31756924a2 | 210 | for(j = 0; j < 4; ++j) |
Helmut64 | 0:0c31756924a2 | 211 | { |
Helmut64 | 0:0c31756924a2 | 212 | (*state)[j][i] = getSBoxValue((*state)[j][i]); |
Helmut64 | 0:0c31756924a2 | 213 | } |
Helmut64 | 0:0c31756924a2 | 214 | } |
Helmut64 | 0:0c31756924a2 | 215 | } |
Helmut64 | 0:0c31756924a2 | 216 | |
Helmut64 | 0:0c31756924a2 | 217 | // The ShiftRows() function shifts the rows in the state to the left. |
Helmut64 | 0:0c31756924a2 | 218 | // Each row is shifted with different offset. |
Helmut64 | 0:0c31756924a2 | 219 | // Offset = Row number. So the first row is not shifted. |
Helmut64 | 0:0c31756924a2 | 220 | static void ShiftRows(AES_CTX *ctx) |
Helmut64 | 0:0c31756924a2 | 221 | { |
Helmut64 | 0:0c31756924a2 | 222 | uint8_t temp; |
Helmut64 | 0:0c31756924a2 | 223 | |
Helmut64 | 0:0c31756924a2 | 224 | // Rotate first row 1 columns to left |
Helmut64 | 0:0c31756924a2 | 225 | temp = (*state)[0][1]; |
Helmut64 | 0:0c31756924a2 | 226 | (*state)[0][1] = (*state)[1][1]; |
Helmut64 | 0:0c31756924a2 | 227 | (*state)[1][1] = (*state)[2][1]; |
Helmut64 | 0:0c31756924a2 | 228 | (*state)[2][1] = (*state)[3][1]; |
Helmut64 | 0:0c31756924a2 | 229 | (*state)[3][1] = temp; |
Helmut64 | 0:0c31756924a2 | 230 | |
Helmut64 | 0:0c31756924a2 | 231 | // Rotate second row 2 columns to left |
Helmut64 | 0:0c31756924a2 | 232 | temp = (*state)[0][2]; |
Helmut64 | 0:0c31756924a2 | 233 | (*state)[0][2] = (*state)[2][2]; |
Helmut64 | 0:0c31756924a2 | 234 | (*state)[2][2] = temp; |
Helmut64 | 0:0c31756924a2 | 235 | |
Helmut64 | 0:0c31756924a2 | 236 | temp = (*state)[1][2]; |
Helmut64 | 0:0c31756924a2 | 237 | (*state)[1][2] = (*state)[3][2]; |
Helmut64 | 0:0c31756924a2 | 238 | (*state)[3][2] = temp; |
Helmut64 | 0:0c31756924a2 | 239 | |
Helmut64 | 0:0c31756924a2 | 240 | // Rotate third row 3 columns to left |
Helmut64 | 0:0c31756924a2 | 241 | temp = (*state)[0][3]; |
Helmut64 | 0:0c31756924a2 | 242 | (*state)[0][3] = (*state)[3][3]; |
Helmut64 | 0:0c31756924a2 | 243 | (*state)[3][3] = (*state)[2][3]; |
Helmut64 | 0:0c31756924a2 | 244 | (*state)[2][3] = (*state)[1][3]; |
Helmut64 | 0:0c31756924a2 | 245 | (*state)[1][3] = temp; |
Helmut64 | 0:0c31756924a2 | 246 | } |
Helmut64 | 0:0c31756924a2 | 247 | |
Helmut64 | 0:0c31756924a2 | 248 | static uint8_t xtime(uint8_t x) |
Helmut64 | 0:0c31756924a2 | 249 | { |
Helmut64 | 0:0c31756924a2 | 250 | return ((x<<1) ^ (((x>>7) & 1) * 0x1b)); |
Helmut64 | 0:0c31756924a2 | 251 | } |
Helmut64 | 0:0c31756924a2 | 252 | |
Helmut64 | 0:0c31756924a2 | 253 | // MixColumns function mixes the columns of the state matrix |
Helmut64 | 0:0c31756924a2 | 254 | static void MixColumns(AES_CTX *ctx) |
Helmut64 | 0:0c31756924a2 | 255 | { |
Helmut64 | 0:0c31756924a2 | 256 | uint8_t i; |
Helmut64 | 0:0c31756924a2 | 257 | uint8_t Tmp,Tm,t; |
Helmut64 | 0:0c31756924a2 | 258 | for(i = 0; i < 4; ++i) |
Helmut64 | 0:0c31756924a2 | 259 | { |
Helmut64 | 0:0c31756924a2 | 260 | t = (*state)[i][0]; |
Helmut64 | 0:0c31756924a2 | 261 | Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ; |
Helmut64 | 0:0c31756924a2 | 262 | Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ; |
Helmut64 | 0:0c31756924a2 | 263 | Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ; |
Helmut64 | 0:0c31756924a2 | 264 | Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ; |
Helmut64 | 0:0c31756924a2 | 265 | Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ; |
Helmut64 | 0:0c31756924a2 | 266 | } |
Helmut64 | 0:0c31756924a2 | 267 | } |
Helmut64 | 0:0c31756924a2 | 268 | |
Helmut64 | 0:0c31756924a2 | 269 | // Multiply is used to multiply numbers in the field GF(2^8) |
Helmut64 | 0:0c31756924a2 | 270 | #if MULTIPLY_AS_A_FUNCTION |
Helmut64 | 0:0c31756924a2 | 271 | static uint8_t Multiply(uint8_t x, uint8_t y) |
Helmut64 | 0:0c31756924a2 | 272 | { |
Helmut64 | 0:0c31756924a2 | 273 | return (((y & 1) * x) ^ |
Helmut64 | 0:0c31756924a2 | 274 | ((y>>1 & 1) * xtime(x)) ^ |
Helmut64 | 0:0c31756924a2 | 275 | ((y>>2 & 1) * xtime(xtime(x))) ^ |
Helmut64 | 0:0c31756924a2 | 276 | ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ |
Helmut64 | 0:0c31756924a2 | 277 | ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))); |
Helmut64 | 0:0c31756924a2 | 278 | } |
Helmut64 | 0:0c31756924a2 | 279 | #else |
Helmut64 | 0:0c31756924a2 | 280 | #define Multiply(x, y) \ |
Helmut64 | 0:0c31756924a2 | 281 | ( ((y & 1) * x) ^ \ |
Helmut64 | 0:0c31756924a2 | 282 | ((y>>1 & 1) * xtime(x)) ^ \ |
Helmut64 | 0:0c31756924a2 | 283 | ((y>>2 & 1) * xtime(xtime(x))) ^ \ |
Helmut64 | 0:0c31756924a2 | 284 | ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \ |
Helmut64 | 0:0c31756924a2 | 285 | ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \ |
Helmut64 | 0:0c31756924a2 | 286 | |
Helmut64 | 0:0c31756924a2 | 287 | #endif |
Helmut64 | 0:0c31756924a2 | 288 | |
Helmut64 | 0:0c31756924a2 | 289 | // MixColumns function mixes the columns of the state matrix. |
Helmut64 | 0:0c31756924a2 | 290 | // The method used to multiply may be difficult to understand for the inexperienced. |
Helmut64 | 0:0c31756924a2 | 291 | // Please use the references to gain more information. |
Helmut64 | 0:0c31756924a2 | 292 | static void InvMixColumns(AES_CTX *ctx) |
Helmut64 | 0:0c31756924a2 | 293 | { |
Helmut64 | 0:0c31756924a2 | 294 | int i; |
Helmut64 | 0:0c31756924a2 | 295 | uint8_t a,b,c,d; |
Helmut64 | 0:0c31756924a2 | 296 | for(i=0;i<4;++i) |
Helmut64 | 0:0c31756924a2 | 297 | { |
Helmut64 | 0:0c31756924a2 | 298 | a = (*state)[i][0]; |
Helmut64 | 0:0c31756924a2 | 299 | b = (*state)[i][1]; |
Helmut64 | 0:0c31756924a2 | 300 | c = (*state)[i][2]; |
Helmut64 | 0:0c31756924a2 | 301 | d = (*state)[i][3]; |
Helmut64 | 0:0c31756924a2 | 302 | |
Helmut64 | 0:0c31756924a2 | 303 | (*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09); |
Helmut64 | 0:0c31756924a2 | 304 | (*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d); |
Helmut64 | 0:0c31756924a2 | 305 | (*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b); |
Helmut64 | 0:0c31756924a2 | 306 | (*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e); |
Helmut64 | 0:0c31756924a2 | 307 | } |
Helmut64 | 0:0c31756924a2 | 308 | } |
Helmut64 | 0:0c31756924a2 | 309 | |
Helmut64 | 0:0c31756924a2 | 310 | |
Helmut64 | 0:0c31756924a2 | 311 | // The SubBytes Function Substitutes the values in the |
Helmut64 | 0:0c31756924a2 | 312 | // state matrix with values in an S-box. |
Helmut64 | 0:0c31756924a2 | 313 | static void InvSubBytes(AES_CTX *ctx) |
Helmut64 | 0:0c31756924a2 | 314 | { |
Helmut64 | 0:0c31756924a2 | 315 | uint8_t i,j; |
Helmut64 | 0:0c31756924a2 | 316 | for(i=0;i<4;++i) |
Helmut64 | 0:0c31756924a2 | 317 | { |
Helmut64 | 0:0c31756924a2 | 318 | for(j=0;j<4;++j) |
Helmut64 | 0:0c31756924a2 | 319 | { |
Helmut64 | 0:0c31756924a2 | 320 | (*state)[j][i] = getSBoxInvert((*state)[j][i]); |
Helmut64 | 0:0c31756924a2 | 321 | } |
Helmut64 | 0:0c31756924a2 | 322 | } |
Helmut64 | 0:0c31756924a2 | 323 | } |
Helmut64 | 0:0c31756924a2 | 324 | |
Helmut64 | 0:0c31756924a2 | 325 | static void InvShiftRows(AES_CTX *ctx) |
Helmut64 | 0:0c31756924a2 | 326 | { |
Helmut64 | 0:0c31756924a2 | 327 | uint8_t temp; |
Helmut64 | 0:0c31756924a2 | 328 | |
Helmut64 | 0:0c31756924a2 | 329 | // Rotate first row 1 columns to right |
Helmut64 | 0:0c31756924a2 | 330 | temp=(*state)[3][1]; |
Helmut64 | 0:0c31756924a2 | 331 | (*state)[3][1]=(*state)[2][1]; |
Helmut64 | 0:0c31756924a2 | 332 | (*state)[2][1]=(*state)[1][1]; |
Helmut64 | 0:0c31756924a2 | 333 | (*state)[1][1]=(*state)[0][1]; |
Helmut64 | 0:0c31756924a2 | 334 | (*state)[0][1]=temp; |
Helmut64 | 0:0c31756924a2 | 335 | |
Helmut64 | 0:0c31756924a2 | 336 | // Rotate second row 2 columns to right |
Helmut64 | 0:0c31756924a2 | 337 | temp=(*state)[0][2]; |
Helmut64 | 0:0c31756924a2 | 338 | (*state)[0][2]=(*state)[2][2]; |
Helmut64 | 0:0c31756924a2 | 339 | (*state)[2][2]=temp; |
Helmut64 | 0:0c31756924a2 | 340 | |
Helmut64 | 0:0c31756924a2 | 341 | temp=(*state)[1][2]; |
Helmut64 | 0:0c31756924a2 | 342 | (*state)[1][2]=(*state)[3][2]; |
Helmut64 | 0:0c31756924a2 | 343 | (*state)[3][2]=temp; |
Helmut64 | 0:0c31756924a2 | 344 | |
Helmut64 | 0:0c31756924a2 | 345 | // Rotate third row 3 columns to right |
Helmut64 | 0:0c31756924a2 | 346 | temp=(*state)[0][3]; |
Helmut64 | 0:0c31756924a2 | 347 | (*state)[0][3]=(*state)[1][3]; |
Helmut64 | 0:0c31756924a2 | 348 | (*state)[1][3]=(*state)[2][3]; |
Helmut64 | 0:0c31756924a2 | 349 | (*state)[2][3]=(*state)[3][3]; |
Helmut64 | 0:0c31756924a2 | 350 | (*state)[3][3]=temp; |
Helmut64 | 0:0c31756924a2 | 351 | } |
Helmut64 | 0:0c31756924a2 | 352 | |
Helmut64 | 0:0c31756924a2 | 353 | |
Helmut64 | 0:0c31756924a2 | 354 | // Cipher is the main function that encrypts the PlainText. |
Helmut64 | 0:0c31756924a2 | 355 | static void Cipher(AES_CTX *ctx) |
Helmut64 | 0:0c31756924a2 | 356 | { |
Helmut64 | 0:0c31756924a2 | 357 | uint8_t round = 0; |
Helmut64 | 0:0c31756924a2 | 358 | |
Helmut64 | 0:0c31756924a2 | 359 | // Add the First round key to the state before starting the rounds. |
Helmut64 | 0:0c31756924a2 | 360 | AddRoundKey(ctx, 0); |
Helmut64 | 0:0c31756924a2 | 361 | |
Helmut64 | 0:0c31756924a2 | 362 | // There will be Nr rounds. |
Helmut64 | 0:0c31756924a2 | 363 | // The first Nr-1 rounds are identical. |
Helmut64 | 0:0c31756924a2 | 364 | // These Nr-1 rounds are executed in the loop below. |
Helmut64 | 0:0c31756924a2 | 365 | for(round = 1; round < Nr; ++round) |
Helmut64 | 0:0c31756924a2 | 366 | { |
Helmut64 | 0:0c31756924a2 | 367 | SubBytes(ctx); |
Helmut64 | 0:0c31756924a2 | 368 | ShiftRows(ctx); |
Helmut64 | 0:0c31756924a2 | 369 | MixColumns(ctx); |
Helmut64 | 0:0c31756924a2 | 370 | AddRoundKey(ctx, round); |
Helmut64 | 0:0c31756924a2 | 371 | } |
Helmut64 | 0:0c31756924a2 | 372 | |
Helmut64 | 0:0c31756924a2 | 373 | // The last round is given below. |
Helmut64 | 0:0c31756924a2 | 374 | // The MixColumns function is not here in the last round. |
Helmut64 | 0:0c31756924a2 | 375 | SubBytes(ctx); |
Helmut64 | 0:0c31756924a2 | 376 | ShiftRows(ctx); |
Helmut64 | 0:0c31756924a2 | 377 | AddRoundKey(ctx, Nr); |
Helmut64 | 0:0c31756924a2 | 378 | } |
Helmut64 | 0:0c31756924a2 | 379 | |
Helmut64 | 0:0c31756924a2 | 380 | static void InvCipher(AES_CTX *ctx) |
Helmut64 | 0:0c31756924a2 | 381 | { |
Helmut64 | 0:0c31756924a2 | 382 | uint8_t round=0; |
Helmut64 | 0:0c31756924a2 | 383 | |
Helmut64 | 0:0c31756924a2 | 384 | // Add the First round key to the state before starting the rounds. |
Helmut64 | 0:0c31756924a2 | 385 | AddRoundKey(ctx, Nr); |
Helmut64 | 0:0c31756924a2 | 386 | |
Helmut64 | 0:0c31756924a2 | 387 | // There will be Nr rounds. |
Helmut64 | 0:0c31756924a2 | 388 | // The first Nr-1 rounds are identical. |
Helmut64 | 0:0c31756924a2 | 389 | // These Nr-1 rounds are executed in the loop below. |
Helmut64 | 0:0c31756924a2 | 390 | for(round=Nr-1;round>0;round--) |
Helmut64 | 0:0c31756924a2 | 391 | { |
Helmut64 | 0:0c31756924a2 | 392 | InvShiftRows(ctx); |
Helmut64 | 0:0c31756924a2 | 393 | InvSubBytes(ctx); |
Helmut64 | 0:0c31756924a2 | 394 | AddRoundKey(ctx, round); |
Helmut64 | 0:0c31756924a2 | 395 | InvMixColumns(ctx); |
Helmut64 | 0:0c31756924a2 | 396 | } |
Helmut64 | 0:0c31756924a2 | 397 | |
Helmut64 | 0:0c31756924a2 | 398 | // The last round is given below. |
Helmut64 | 0:0c31756924a2 | 399 | // The MixColumns function is not here in the last round. |
Helmut64 | 0:0c31756924a2 | 400 | InvShiftRows(ctx); |
Helmut64 | 0:0c31756924a2 | 401 | InvSubBytes(ctx); |
Helmut64 | 0:0c31756924a2 | 402 | AddRoundKey(ctx, 0); |
Helmut64 | 0:0c31756924a2 | 403 | } |
Helmut64 | 0:0c31756924a2 | 404 | |
Helmut64 | 0:0c31756924a2 | 405 | |
Helmut64 | 0:0c31756924a2 | 406 | /*****************************************************************************/ |
Helmut64 | 0:0c31756924a2 | 407 | /* Public functions: */ |
Helmut64 | 0:0c31756924a2 | 408 | /*****************************************************************************/ |
Helmut64 | 0:0c31756924a2 | 409 | |
Helmut64 | 0:0c31756924a2 | 410 | |
Helmut64 | 0:0c31756924a2 | 411 | void AES128_InitContext(AES_CTX *ctx, const uint8_t* key, const uint8_t* initialVector) |
Helmut64 | 0:0c31756924a2 | 412 | { |
Helmut64 | 0:0c31756924a2 | 413 | // The KeyExpansion routine must be called before encryption. |
Helmut64 | 0:0c31756924a2 | 414 | KeyExpansion(ctx, key); |
Helmut64 | 0:0c31756924a2 | 415 | if (initialVector) |
Helmut64 | 0:0c31756924a2 | 416 | memcpy(ctx->InitialVector, initialVector, KEYLEN); |
Helmut64 | 0:0c31756924a2 | 417 | |
Helmut64 | 0:0c31756924a2 | 418 | } |
Helmut64 | 0:0c31756924a2 | 419 | #ifdef ECB_MODE |
Helmut64 | 0:0c31756924a2 | 420 | |
Helmut64 | 0:0c31756924a2 | 421 | |
Helmut64 | 0:0c31756924a2 | 422 | void AES128_ECB_encrypt(AES_CTX *ctx, const uint8_t* input, uint8_t* output) |
Helmut64 | 0:0c31756924a2 | 423 | { |
Helmut64 | 0:0c31756924a2 | 424 | // Copy input to output, and work in-memory on output |
Helmut64 | 0:0c31756924a2 | 425 | memcpy(output, input, KEYLEN); |
Helmut64 | 0:0c31756924a2 | 426 | state = (state_t*)output; |
Helmut64 | 0:0c31756924a2 | 427 | |
Helmut64 | 0:0c31756924a2 | 428 | // The next function call encrypts the PlainText with the Key using AES algorithm. |
Helmut64 | 0:0c31756924a2 | 429 | Cipher(ctx); |
Helmut64 | 0:0c31756924a2 | 430 | } |
Helmut64 | 0:0c31756924a2 | 431 | |
Helmut64 | 0:0c31756924a2 | 432 | void AES128_ECB_decrypt(AES_CTX *ctx, const uint8_t* input, uint8_t *output) |
Helmut64 | 0:0c31756924a2 | 433 | { |
Helmut64 | 0:0c31756924a2 | 434 | // Copy input to output, and work in-memory on output |
Helmut64 | 0:0c31756924a2 | 435 | memcpy(output, input, KEYLEN); |
Helmut64 | 0:0c31756924a2 | 436 | state = (state_t*)output; |
Helmut64 | 0:0c31756924a2 | 437 | |
Helmut64 | 0:0c31756924a2 | 438 | InvCipher(ctx); |
Helmut64 | 0:0c31756924a2 | 439 | } |
Helmut64 | 0:0c31756924a2 | 440 | |
Helmut64 | 0:0c31756924a2 | 441 | |
Helmut64 | 0:0c31756924a2 | 442 | #endif // ECB_MODE |
Helmut64 | 0:0c31756924a2 | 443 | |
Helmut64 | 0:0c31756924a2 | 444 | |
Helmut64 | 0:0c31756924a2 | 445 | |
Helmut64 | 0:0c31756924a2 | 446 | |
Helmut64 | 0:0c31756924a2 | 447 | #ifdef CBC_MODE |
Helmut64 | 0:0c31756924a2 | 448 | |
Helmut64 | 0:0c31756924a2 | 449 | |
Helmut64 | 0:0c31756924a2 | 450 | static void XorWithIv(AES_CTX *ctx, uint8_t* buf) |
Helmut64 | 0:0c31756924a2 | 451 | { |
Helmut64 | 0:0c31756924a2 | 452 | if (sizeof(int) == 4) { |
Helmut64 | 0:0c31756924a2 | 453 | uint32_t *data = (uint32_t *)buf; |
Helmut64 | 0:0c31756924a2 | 454 | uint32_t *iVec = (uint32_t *)Iv; |
Helmut64 | 0:0c31756924a2 | 455 | |
Helmut64 | 0:0c31756924a2 | 456 | *data++ ^= *iVec++; |
Helmut64 | 0:0c31756924a2 | 457 | *data++ ^= *iVec++; |
Helmut64 | 0:0c31756924a2 | 458 | *data++ ^= *iVec++; |
Helmut64 | 0:0c31756924a2 | 459 | *data++ ^= *iVec++; |
Helmut64 | 0:0c31756924a2 | 460 | } else { |
Helmut64 | 0:0c31756924a2 | 461 | uint8_t i; |
Helmut64 | 0:0c31756924a2 | 462 | for(i = 0; i < KEYLEN; ++i) |
Helmut64 | 0:0c31756924a2 | 463 | { |
Helmut64 | 0:0c31756924a2 | 464 | buf[i] ^= Iv[i]; |
Helmut64 | 0:0c31756924a2 | 465 | } |
Helmut64 | 0:0c31756924a2 | 466 | } |
Helmut64 | 0:0c31756924a2 | 467 | } |
Helmut64 | 0:0c31756924a2 | 468 | |
Helmut64 | 0:0c31756924a2 | 469 | void AES128_CBC_encrypt_buffer(AES_CTX *ctx, uint8_t* output, const uint8_t* input, uint32_t length) |
Helmut64 | 0:0c31756924a2 | 470 | { |
Helmut64 | 0:0c31756924a2 | 471 | uintptr_t i; |
Helmut64 | 0:0c31756924a2 | 472 | uint8_t remainders = length % KEYLEN; /* Remaining bytes in the last non-full block */ |
Helmut64 | 0:0c31756924a2 | 473 | |
Helmut64 | 0:0c31756924a2 | 474 | |
Helmut64 | 0:0c31756924a2 | 475 | for(i = 0; i < length; i += KEYLEN) |
Helmut64 | 0:0c31756924a2 | 476 | { |
Helmut64 | 0:0c31756924a2 | 477 | memcpy(output, input, KEYLEN); |
Helmut64 | 0:0c31756924a2 | 478 | XorWithIv(ctx, output); |
Helmut64 | 0:0c31756924a2 | 479 | state = (state_t*)output; |
Helmut64 | 0:0c31756924a2 | 480 | Cipher(ctx); |
Helmut64 | 0:0c31756924a2 | 481 | memcpy(ctx->InitialVector, output, KEYLEN); |
Helmut64 | 0:0c31756924a2 | 482 | input += KEYLEN; |
Helmut64 | 0:0c31756924a2 | 483 | output += KEYLEN; |
Helmut64 | 0:0c31756924a2 | 484 | } |
Helmut64 | 0:0c31756924a2 | 485 | |
Helmut64 | 0:0c31756924a2 | 486 | if(remainders) |
Helmut64 | 0:0c31756924a2 | 487 | { |
Helmut64 | 0:0c31756924a2 | 488 | memcpy(output, input, KEYLEN); |
Helmut64 | 0:0c31756924a2 | 489 | memset(output + remainders, 0, KEYLEN - remainders); /* add 0-padding */ |
Helmut64 | 0:0c31756924a2 | 490 | state = (state_t*)output; |
Helmut64 | 0:0c31756924a2 | 491 | Cipher(ctx); |
Helmut64 | 0:0c31756924a2 | 492 | } |
Helmut64 | 0:0c31756924a2 | 493 | } |
Helmut64 | 0:0c31756924a2 | 494 | |
Helmut64 | 0:0c31756924a2 | 495 | void AES128_CBC_decrypt_buffer(AES_CTX *ctx, uint8_t* output, const uint8_t* input, uint32_t length) |
Helmut64 | 0:0c31756924a2 | 496 | { |
Helmut64 | 0:0c31756924a2 | 497 | uintptr_t i; |
Helmut64 | 0:0c31756924a2 | 498 | uint8_t remainders = length % KEYLEN; /* Remaining bytes in the last non-full block */ |
Helmut64 | 0:0c31756924a2 | 499 | |
Helmut64 | 0:0c31756924a2 | 500 | |
Helmut64 | 0:0c31756924a2 | 501 | for(i = 0; i < length; i += KEYLEN) |
Helmut64 | 0:0c31756924a2 | 502 | { |
Helmut64 | 0:0c31756924a2 | 503 | memcpy(output, input, KEYLEN); |
Helmut64 | 0:0c31756924a2 | 504 | state = (state_t*)output; |
Helmut64 | 0:0c31756924a2 | 505 | InvCipher(ctx); |
Helmut64 | 0:0c31756924a2 | 506 | XorWithIv(ctx, output); |
Helmut64 | 0:0c31756924a2 | 507 | memcpy(ctx->InitialVector, input, KEYLEN); |
Helmut64 | 0:0c31756924a2 | 508 | input += KEYLEN; |
Helmut64 | 0:0c31756924a2 | 509 | output += KEYLEN; |
Helmut64 | 0:0c31756924a2 | 510 | } |
Helmut64 | 0:0c31756924a2 | 511 | |
Helmut64 | 0:0c31756924a2 | 512 | if(remainders) |
Helmut64 | 0:0c31756924a2 | 513 | { |
Helmut64 | 0:0c31756924a2 | 514 | memcpy(output, input, KEYLEN); |
Helmut64 | 0:0c31756924a2 | 515 | memset(output+remainders, 0, KEYLEN - remainders); /* add 0-padding */ |
Helmut64 | 0:0c31756924a2 | 516 | state = (state_t*)output; |
Helmut64 | 0:0c31756924a2 | 517 | InvCipher(ctx); |
Helmut64 | 0:0c31756924a2 | 518 | } |
Helmut64 | 0:0c31756924a2 | 519 | } |
Helmut64 | 0:0c31756924a2 | 520 | |
Helmut64 | 0:0c31756924a2 | 521 | |
Helmut64 | 0:0c31756924a2 | 522 | #endif // CBC_MODE |
Helmut64 | 0:0c31756924a2 | 523 | |
Helmut64 | 0:0c31756924a2 | 524 |