A simple library to support serving https.
Dependents: oldheating gps motorhome heating
aes-cbc/aes128cbc.c@20:197c3e6e8b8d, 2019-10-16 (annotated)
- Committer:
- andrewboyson
- Date:
- Wed Oct 16 11:11:03 2019 +0000
- Revision:
- 20:197c3e6e8b8d
- Parent:
- aes/aes128cbc.c@19:f22327e8be7b
Updated aes128cbc to remove some hard to follow casting between a 1d buffer and a 2d array of rows and columns.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 19:f22327e8be7b | 1 | /* |
andrewboyson | 19:f22327e8be7b | 2 | This is an implementation of the AES algorithm, specifically ECB, CTR and CBC mode. |
andrewboyson | 19:f22327e8be7b | 3 | Block size can be chosen in aes.h - available choices are AES128, AES192, AES256. |
andrewboyson | 19:f22327e8be7b | 4 | The implementation is verified against the test vectors in: |
andrewboyson | 19:f22327e8be7b | 5 | National Institute of Standards and Technology Special Publication 800-38A 2001 ED |
andrewboyson | 19:f22327e8be7b | 6 | ECB-AES128 |
andrewboyson | 19:f22327e8be7b | 7 | ---------- |
andrewboyson | 19:f22327e8be7b | 8 | plain-text: |
andrewboyson | 19:f22327e8be7b | 9 | 6bc1bee22e409f96e93d7e117393172a |
andrewboyson | 19:f22327e8be7b | 10 | ae2d8a571e03ac9c9eb76fac45af8e51 |
andrewboyson | 19:f22327e8be7b | 11 | 30c81c46a35ce411e5fbc1191a0a52ef |
andrewboyson | 19:f22327e8be7b | 12 | f69f2445df4f9b17ad2b417be66c3710 |
andrewboyson | 19:f22327e8be7b | 13 | key: |
andrewboyson | 19:f22327e8be7b | 14 | 2b7e151628aed2a6abf7158809cf4f3c |
andrewboyson | 19:f22327e8be7b | 15 | resulting cipher |
andrewboyson | 19:f22327e8be7b | 16 | 3ad77bb40d7a3660a89ecaf32466ef97 |
andrewboyson | 19:f22327e8be7b | 17 | f5d3d58503b9699de785895a96fdbaaf |
andrewboyson | 19:f22327e8be7b | 18 | 43b1cd7f598ece23881b00e3ed030688 |
andrewboyson | 19:f22327e8be7b | 19 | 7b0c785e27e8ad3f8223207104725dd4 |
andrewboyson | 19:f22327e8be7b | 20 | NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0) |
andrewboyson | 19:f22327e8be7b | 21 | You should pad the end of the string with zeros if this is not the case. |
andrewboyson | 19:f22327e8be7b | 22 | For AES192/256 the key size is proportionally larger. |
andrewboyson | 19:f22327e8be7b | 23 | */ |
andrewboyson | 19:f22327e8be7b | 24 | |
andrewboyson | 19:f22327e8be7b | 25 | #include <stdint.h> |
andrewboyson | 19:f22327e8be7b | 26 | #include <string.h> // CBC mode, for memset |
andrewboyson | 19:f22327e8be7b | 27 | #include "aes128cbc.h" |
andrewboyson | 19:f22327e8be7b | 28 | |
andrewboyson | 20:197c3e6e8b8d | 29 | #define NB 4 // The number of columns comprising a state in AES. This is a constant in AES. Value=4 |
andrewboyson | 20:197c3e6e8b8d | 30 | #define NK 4 // The number of 32 bit words in a key. |
andrewboyson | 20:197c3e6e8b8d | 31 | #define NR 10 // The number of rounds in AES Cipher. |
andrewboyson | 19:f22327e8be7b | 32 | |
andrewboyson | 20:197c3e6e8b8d | 33 | /* |
andrewboyson | 20:197c3e6e8b8d | 34 | union state_u |
andrewboyson | 19:f22327e8be7b | 35 | { |
andrewboyson | 20:197c3e6e8b8d | 36 | uint8_t d1[16]; |
andrewboyson | 20:197c3e6e8b8d | 37 | uint8_t d2[4][4]; |
andrewboyson | 19:f22327e8be7b | 38 | }; |
andrewboyson | 20:197c3e6e8b8d | 39 | typedef union state_u state_t; // state - array holding the intermediate results during decryption. |
andrewboyson | 20:197c3e6e8b8d | 40 | */ |
andrewboyson | 19:f22327e8be7b | 41 | static const uint8_t sbox[256] = { |
andrewboyson | 19:f22327e8be7b | 42 | //0 1 2 3 4 5 6 7 8 9 A B C D E F |
andrewboyson | 19:f22327e8be7b | 43 | 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
andrewboyson | 19:f22327e8be7b | 44 | 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
andrewboyson | 19:f22327e8be7b | 45 | 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
andrewboyson | 19:f22327e8be7b | 46 | 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
andrewboyson | 19:f22327e8be7b | 47 | 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
andrewboyson | 19:f22327e8be7b | 48 | 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
andrewboyson | 19:f22327e8be7b | 49 | 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
andrewboyson | 19:f22327e8be7b | 50 | 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
andrewboyson | 19:f22327e8be7b | 51 | 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
andrewboyson | 19:f22327e8be7b | 52 | 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
andrewboyson | 19:f22327e8be7b | 53 | 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
andrewboyson | 19:f22327e8be7b | 54 | 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
andrewboyson | 19:f22327e8be7b | 55 | 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
andrewboyson | 19:f22327e8be7b | 56 | 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
andrewboyson | 19:f22327e8be7b | 57 | 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
andrewboyson | 19:f22327e8be7b | 58 | 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; |
andrewboyson | 19:f22327e8be7b | 59 | |
andrewboyson | 19:f22327e8be7b | 60 | static const uint8_t rsbox[256] = { |
andrewboyson | 19:f22327e8be7b | 61 | 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, |
andrewboyson | 19:f22327e8be7b | 62 | 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, |
andrewboyson | 19:f22327e8be7b | 63 | 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, |
andrewboyson | 19:f22327e8be7b | 64 | 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, |
andrewboyson | 19:f22327e8be7b | 65 | 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, |
andrewboyson | 19:f22327e8be7b | 66 | 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, |
andrewboyson | 19:f22327e8be7b | 67 | 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, |
andrewboyson | 19:f22327e8be7b | 68 | 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, |
andrewboyson | 19:f22327e8be7b | 69 | 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, |
andrewboyson | 19:f22327e8be7b | 70 | 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, |
andrewboyson | 19:f22327e8be7b | 71 | 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, |
andrewboyson | 19:f22327e8be7b | 72 | 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, |
andrewboyson | 19:f22327e8be7b | 73 | 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, |
andrewboyson | 19:f22327e8be7b | 74 | 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, |
andrewboyson | 19:f22327e8be7b | 75 | 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, |
andrewboyson | 19:f22327e8be7b | 76 | 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; |
andrewboyson | 19:f22327e8be7b | 77 | |
andrewboyson | 19:f22327e8be7b | 78 | // The round constant word array, Rcon[i], contains the values given by |
andrewboyson | 19:f22327e8be7b | 79 | // x to the power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8) |
andrewboyson | 19:f22327e8be7b | 80 | static const uint8_t Rcon[11] = { 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; |
andrewboyson | 19:f22327e8be7b | 81 | |
andrewboyson | 19:f22327e8be7b | 82 | // This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states. |
andrewboyson | 20:197c3e6e8b8d | 83 | static void keyExpansion(uint8_t* roundKey, const uint8_t* key) |
andrewboyson | 19:f22327e8be7b | 84 | { |
andrewboyson | 19:f22327e8be7b | 85 | unsigned i, j, k; |
andrewboyson | 19:f22327e8be7b | 86 | uint8_t tempa[4]; // Used for the column/row operations |
andrewboyson | 19:f22327e8be7b | 87 | |
andrewboyson | 19:f22327e8be7b | 88 | // The first round key is the key itself. |
andrewboyson | 20:197c3e6e8b8d | 89 | for (i = 0; i < NK; ++i) |
andrewboyson | 19:f22327e8be7b | 90 | { |
andrewboyson | 20:197c3e6e8b8d | 91 | roundKey[i * 4 + 0] = key[i * 4 + 0]; |
andrewboyson | 20:197c3e6e8b8d | 92 | roundKey[i * 4 + 1] = key[i * 4 + 1]; |
andrewboyson | 20:197c3e6e8b8d | 93 | roundKey[i * 4 + 2] = key[i * 4 + 2]; |
andrewboyson | 20:197c3e6e8b8d | 94 | roundKey[i * 4 + 3] = key[i * 4 + 3]; |
andrewboyson | 19:f22327e8be7b | 95 | } |
andrewboyson | 19:f22327e8be7b | 96 | |
andrewboyson | 19:f22327e8be7b | 97 | // All other round keys are found from the previous round keys. |
andrewboyson | 20:197c3e6e8b8d | 98 | for (i = NK; i < NB * (NR + 1); ++i) |
andrewboyson | 19:f22327e8be7b | 99 | { |
andrewboyson | 19:f22327e8be7b | 100 | { |
andrewboyson | 19:f22327e8be7b | 101 | k = (i - 1) * 4; |
andrewboyson | 20:197c3e6e8b8d | 102 | tempa[0] = roundKey[k + 0]; |
andrewboyson | 20:197c3e6e8b8d | 103 | tempa[1] = roundKey[k + 1]; |
andrewboyson | 20:197c3e6e8b8d | 104 | tempa[2] = roundKey[k + 2]; |
andrewboyson | 20:197c3e6e8b8d | 105 | tempa[3] = roundKey[k + 3]; |
andrewboyson | 19:f22327e8be7b | 106 | |
andrewboyson | 19:f22327e8be7b | 107 | } |
andrewboyson | 19:f22327e8be7b | 108 | |
andrewboyson | 20:197c3e6e8b8d | 109 | if (i % NK == 0) |
andrewboyson | 19:f22327e8be7b | 110 | { |
andrewboyson | 19:f22327e8be7b | 111 | // This function shifts the 4 bytes in a word to the left once. |
andrewboyson | 19:f22327e8be7b | 112 | // [a0,a1,a2,a3] becomes [a1,a2,a3,a0] |
andrewboyson | 19:f22327e8be7b | 113 | |
andrewboyson | 19:f22327e8be7b | 114 | // Function RotWord() |
andrewboyson | 19:f22327e8be7b | 115 | { |
andrewboyson | 19:f22327e8be7b | 116 | const uint8_t u8tmp = tempa[0]; |
andrewboyson | 19:f22327e8be7b | 117 | tempa[0] = tempa[1]; |
andrewboyson | 19:f22327e8be7b | 118 | tempa[1] = tempa[2]; |
andrewboyson | 19:f22327e8be7b | 119 | tempa[2] = tempa[3]; |
andrewboyson | 19:f22327e8be7b | 120 | tempa[3] = u8tmp; |
andrewboyson | 19:f22327e8be7b | 121 | } |
andrewboyson | 19:f22327e8be7b | 122 | |
andrewboyson | 19:f22327e8be7b | 123 | // SubWord() is a function that takes a four-byte input word and |
andrewboyson | 19:f22327e8be7b | 124 | // applies the S-box to each of the four bytes to produce an output word. |
andrewboyson | 19:f22327e8be7b | 125 | |
andrewboyson | 19:f22327e8be7b | 126 | // Function Subword() |
andrewboyson | 19:f22327e8be7b | 127 | { |
andrewboyson | 20:197c3e6e8b8d | 128 | tempa[0] = sbox[tempa[0]]; |
andrewboyson | 20:197c3e6e8b8d | 129 | tempa[1] = sbox[tempa[1]]; |
andrewboyson | 20:197c3e6e8b8d | 130 | tempa[2] = sbox[tempa[2]]; |
andrewboyson | 20:197c3e6e8b8d | 131 | tempa[3] = sbox[tempa[3]]; |
andrewboyson | 19:f22327e8be7b | 132 | } |
andrewboyson | 19:f22327e8be7b | 133 | |
andrewboyson | 20:197c3e6e8b8d | 134 | tempa[0] = tempa[0] ^ Rcon[i/NK]; |
andrewboyson | 19:f22327e8be7b | 135 | } |
andrewboyson | 20:197c3e6e8b8d | 136 | j = i * 4; k=(i - NK) * 4; |
andrewboyson | 20:197c3e6e8b8d | 137 | roundKey[j + 0] = roundKey[k + 0] ^ tempa[0]; |
andrewboyson | 20:197c3e6e8b8d | 138 | roundKey[j + 1] = roundKey[k + 1] ^ tempa[1]; |
andrewboyson | 20:197c3e6e8b8d | 139 | roundKey[j + 2] = roundKey[k + 2] ^ tempa[2]; |
andrewboyson | 20:197c3e6e8b8d | 140 | roundKey[j + 3] = roundKey[k + 3] ^ tempa[3]; |
andrewboyson | 19:f22327e8be7b | 141 | } |
andrewboyson | 19:f22327e8be7b | 142 | } |
andrewboyson | 19:f22327e8be7b | 143 | |
andrewboyson | 19:f22327e8be7b | 144 | // This function adds the round key to state. |
andrewboyson | 19:f22327e8be7b | 145 | // The round key is added to the state by an XOR function. |
andrewboyson | 20:197c3e6e8b8d | 146 | static void addRoundKey(uint8_t round, uint8_t* state, const uint8_t* roundKey) |
andrewboyson | 19:f22327e8be7b | 147 | { |
andrewboyson | 20:197c3e6e8b8d | 148 | uint8_t i,j; |
andrewboyson | 20:197c3e6e8b8d | 149 | for (i = 0; i < 4; ++i) |
andrewboyson | 19:f22327e8be7b | 150 | { |
andrewboyson | 20:197c3e6e8b8d | 151 | for (j = 0; j < 4; ++j) |
andrewboyson | 20:197c3e6e8b8d | 152 | { |
andrewboyson | 20:197c3e6e8b8d | 153 | *(state + i*4 + j) ^= roundKey[(round * NB * 4) + (i * NB) + j]; |
andrewboyson | 20:197c3e6e8b8d | 154 | } |
andrewboyson | 19:f22327e8be7b | 155 | } |
andrewboyson | 19:f22327e8be7b | 156 | } |
andrewboyson | 19:f22327e8be7b | 157 | |
andrewboyson | 19:f22327e8be7b | 158 | // The SubBytes Function Substitutes the values in the |
andrewboyson | 19:f22327e8be7b | 159 | // state matrix with values in an S-box. |
andrewboyson | 20:197c3e6e8b8d | 160 | static void subBytes(uint8_t* state) |
andrewboyson | 19:f22327e8be7b | 161 | { |
andrewboyson | 20:197c3e6e8b8d | 162 | uint8_t i, j; |
andrewboyson | 20:197c3e6e8b8d | 163 | for (i = 0; i < 4; ++i) |
andrewboyson | 19:f22327e8be7b | 164 | { |
andrewboyson | 20:197c3e6e8b8d | 165 | for (j = 0; j < 4; ++j) |
andrewboyson | 20:197c3e6e8b8d | 166 | { |
andrewboyson | 20:197c3e6e8b8d | 167 | *(state + j*4 + i) = sbox[*(state + j*4 + i)]; |
andrewboyson | 20:197c3e6e8b8d | 168 | } |
andrewboyson | 19:f22327e8be7b | 169 | } |
andrewboyson | 19:f22327e8be7b | 170 | } |
andrewboyson | 19:f22327e8be7b | 171 | |
andrewboyson | 19:f22327e8be7b | 172 | // The ShiftRows() function shifts the rows in the state to the left. |
andrewboyson | 19:f22327e8be7b | 173 | // Each row is shifted with different offset. |
andrewboyson | 19:f22327e8be7b | 174 | // Offset = Row number. So the first row is not shifted. |
andrewboyson | 20:197c3e6e8b8d | 175 | static void shiftRows(uint8_t* state) |
andrewboyson | 19:f22327e8be7b | 176 | { |
andrewboyson | 20:197c3e6e8b8d | 177 | uint8_t temp; |
andrewboyson | 20:197c3e6e8b8d | 178 | |
andrewboyson | 20:197c3e6e8b8d | 179 | // Rotate first row 1 columns to left |
andrewboyson | 20:197c3e6e8b8d | 180 | temp = *(state + 4*0 + 1); |
andrewboyson | 20:197c3e6e8b8d | 181 | *(state + 4*0 + 1) = *(state + 4*1 + 1); |
andrewboyson | 20:197c3e6e8b8d | 182 | *(state + 4*1 + 1) = *(state + 4*2 + 1); |
andrewboyson | 20:197c3e6e8b8d | 183 | *(state + 4*2 + 1) = *(state + 4*3 + 1); |
andrewboyson | 20:197c3e6e8b8d | 184 | *(state + 4*3 + 1) = temp; |
andrewboyson | 20:197c3e6e8b8d | 185 | |
andrewboyson | 20:197c3e6e8b8d | 186 | // Rotate second row 2 columns to left |
andrewboyson | 20:197c3e6e8b8d | 187 | temp = *(state + 4*0 + 2); |
andrewboyson | 20:197c3e6e8b8d | 188 | *(state + 4*0 + 2) = *(state + 4*2 + 2); |
andrewboyson | 20:197c3e6e8b8d | 189 | *(state + 4*2 + 2) = temp; |
andrewboyson | 20:197c3e6e8b8d | 190 | |
andrewboyson | 20:197c3e6e8b8d | 191 | temp = *(state + 4*1 + 2); |
andrewboyson | 20:197c3e6e8b8d | 192 | *(state + 4*1 + 2) = *(state + 4*3 + 2); |
andrewboyson | 20:197c3e6e8b8d | 193 | *(state + 4*3 + 2) = temp; |
andrewboyson | 20:197c3e6e8b8d | 194 | |
andrewboyson | 20:197c3e6e8b8d | 195 | // Rotate third row 3 columns to left |
andrewboyson | 20:197c3e6e8b8d | 196 | temp = *(state + 4*0 + 3); |
andrewboyson | 20:197c3e6e8b8d | 197 | *(state + 4*0 + 3) = *(state + 4*3 + 3); |
andrewboyson | 20:197c3e6e8b8d | 198 | *(state + 4*3 + 3) = *(state + 4*2 + 3); |
andrewboyson | 20:197c3e6e8b8d | 199 | *(state + 4*2 + 3) = *(state + 4*1 + 3); |
andrewboyson | 20:197c3e6e8b8d | 200 | *(state + 4*1 + 3) = temp; |
andrewboyson | 19:f22327e8be7b | 201 | } |
andrewboyson | 19:f22327e8be7b | 202 | |
andrewboyson | 19:f22327e8be7b | 203 | static uint8_t xtime(uint8_t x) |
andrewboyson | 19:f22327e8be7b | 204 | { |
andrewboyson | 20:197c3e6e8b8d | 205 | return ((x<<1) ^ (((x>>7) & 1) * 0x1b)); |
andrewboyson | 19:f22327e8be7b | 206 | } |
andrewboyson | 19:f22327e8be7b | 207 | |
andrewboyson | 19:f22327e8be7b | 208 | // MixColumns function mixes the columns of the state matrix |
andrewboyson | 20:197c3e6e8b8d | 209 | static void mixColumns(uint8_t* state) |
andrewboyson | 19:f22327e8be7b | 210 | { |
andrewboyson | 20:197c3e6e8b8d | 211 | uint8_t i; |
andrewboyson | 20:197c3e6e8b8d | 212 | uint8_t tmp, tm, t; |
andrewboyson | 20:197c3e6e8b8d | 213 | for (i = 0; i < 4; ++i) |
andrewboyson | 20:197c3e6e8b8d | 214 | { |
andrewboyson | 20:197c3e6e8b8d | 215 | t = *(state + 4*i + 0); |
andrewboyson | 20:197c3e6e8b8d | 216 | tmp = *(state + 4*i + 0) ^ *(state + 4*i + 1) ^ *(state + 4*i + 2) ^ *(state + 4*i + 3); |
andrewboyson | 20:197c3e6e8b8d | 217 | tm = *(state + 4*i + 0) ^ *(state + 4*i + 1) ; tm = xtime(tm); *(state + 4*i + 0) ^= tm ^ tmp ; |
andrewboyson | 20:197c3e6e8b8d | 218 | tm = *(state + 4*i + 1) ^ *(state + 4*i + 2) ; tm = xtime(tm); *(state + 4*i + 1) ^= tm ^ tmp ; |
andrewboyson | 20:197c3e6e8b8d | 219 | tm = *(state + 4*i + 2) ^ *(state + 4*i + 3) ; tm = xtime(tm); *(state + 4*i + 2) ^= tm ^ tmp ; |
andrewboyson | 20:197c3e6e8b8d | 220 | tm = *(state + 4*i + 3) ^ t ; tm = xtime(tm); *(state + 4*i + 3) ^= tm ^ tmp ; |
andrewboyson | 20:197c3e6e8b8d | 221 | } |
andrewboyson | 19:f22327e8be7b | 222 | } |
andrewboyson | 19:f22327e8be7b | 223 | |
andrewboyson | 20:197c3e6e8b8d | 224 | static uint8_t multiply(uint8_t x, uint8_t y) |
andrewboyson | 19:f22327e8be7b | 225 | { |
andrewboyson | 20:197c3e6e8b8d | 226 | return (((y>>0 & 1) * x) ^ |
andrewboyson | 20:197c3e6e8b8d | 227 | ((y>>1 & 1) * xtime(x)) ^ |
andrewboyson | 20:197c3e6e8b8d | 228 | ((y>>2 & 1) * xtime(xtime(x))) ^ |
andrewboyson | 20:197c3e6e8b8d | 229 | ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ |
andrewboyson | 20:197c3e6e8b8d | 230 | ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))); /* this last call to xtime() can be omitted */ |
andrewboyson | 20:197c3e6e8b8d | 231 | } |
andrewboyson | 19:f22327e8be7b | 232 | |
andrewboyson | 19:f22327e8be7b | 233 | // MixColumns function mixes the columns of the state matrix. |
andrewboyson | 19:f22327e8be7b | 234 | // The method used to multiply may be difficult to understand for the inexperienced. |
andrewboyson | 19:f22327e8be7b | 235 | // Please use the references to gain more information. |
andrewboyson | 20:197c3e6e8b8d | 236 | static void invMixColumns(uint8_t* state) |
andrewboyson | 19:f22327e8be7b | 237 | { |
andrewboyson | 20:197c3e6e8b8d | 238 | int i; |
andrewboyson | 20:197c3e6e8b8d | 239 | uint8_t a, b, c, d; |
andrewboyson | 20:197c3e6e8b8d | 240 | for (i = 0; i < 4; ++i) |
andrewboyson | 20:197c3e6e8b8d | 241 | { |
andrewboyson | 20:197c3e6e8b8d | 242 | a = *(state + 4*i + 0); |
andrewboyson | 20:197c3e6e8b8d | 243 | b = *(state + 4*i + 1); |
andrewboyson | 20:197c3e6e8b8d | 244 | c = *(state + 4*i + 2); |
andrewboyson | 20:197c3e6e8b8d | 245 | d = *(state + 4*i + 3); |
andrewboyson | 19:f22327e8be7b | 246 | |
andrewboyson | 20:197c3e6e8b8d | 247 | *(state + 4*i + 0) = multiply(a, 0x0e) ^ multiply(b, 0x0b) ^ multiply(c, 0x0d) ^ multiply(d, 0x09); |
andrewboyson | 20:197c3e6e8b8d | 248 | *(state + 4*i + 1) = multiply(a, 0x09) ^ multiply(b, 0x0e) ^ multiply(c, 0x0b) ^ multiply(d, 0x0d); |
andrewboyson | 20:197c3e6e8b8d | 249 | *(state + 4*i + 2) = multiply(a, 0x0d) ^ multiply(b, 0x09) ^ multiply(c, 0x0e) ^ multiply(d, 0x0b); |
andrewboyson | 20:197c3e6e8b8d | 250 | *(state + 4*i + 3) = multiply(a, 0x0b) ^ multiply(b, 0x0d) ^ multiply(c, 0x09) ^ multiply(d, 0x0e); |
andrewboyson | 20:197c3e6e8b8d | 251 | } |
andrewboyson | 19:f22327e8be7b | 252 | } |
andrewboyson | 19:f22327e8be7b | 253 | |
andrewboyson | 19:f22327e8be7b | 254 | |
andrewboyson | 19:f22327e8be7b | 255 | // The SubBytes Function Substitutes the values in the |
andrewboyson | 19:f22327e8be7b | 256 | // state matrix with values in an S-box. |
andrewboyson | 20:197c3e6e8b8d | 257 | static void invSubBytes(uint8_t* state) |
andrewboyson | 19:f22327e8be7b | 258 | { |
andrewboyson | 20:197c3e6e8b8d | 259 | uint8_t i, j; |
andrewboyson | 20:197c3e6e8b8d | 260 | for (i = 0; i < 4; ++i) |
andrewboyson | 19:f22327e8be7b | 261 | { |
andrewboyson | 20:197c3e6e8b8d | 262 | for (j = 0; j < 4; ++j) |
andrewboyson | 20:197c3e6e8b8d | 263 | { |
andrewboyson | 20:197c3e6e8b8d | 264 | *(state + 4*j + i) = rsbox[*(state + 4*j + i)]; |
andrewboyson | 20:197c3e6e8b8d | 265 | } |
andrewboyson | 19:f22327e8be7b | 266 | } |
andrewboyson | 19:f22327e8be7b | 267 | } |
andrewboyson | 19:f22327e8be7b | 268 | |
andrewboyson | 20:197c3e6e8b8d | 269 | static void invShiftRows(uint8_t* state) |
andrewboyson | 19:f22327e8be7b | 270 | { |
andrewboyson | 20:197c3e6e8b8d | 271 | uint8_t temp; |
andrewboyson | 20:197c3e6e8b8d | 272 | |
andrewboyson | 20:197c3e6e8b8d | 273 | // Rotate first row 1 columns to right |
andrewboyson | 20:197c3e6e8b8d | 274 | temp = *(state + 4*3 + 1); |
andrewboyson | 20:197c3e6e8b8d | 275 | *(state + 4*3 + 1) = *(state + 4*2 + 1); |
andrewboyson | 20:197c3e6e8b8d | 276 | *(state + 4*2 + 1) = *(state + 4*1 + 1); |
andrewboyson | 20:197c3e6e8b8d | 277 | *(state + 4*1 + 1) = *(state + 4*0 + 1); |
andrewboyson | 20:197c3e6e8b8d | 278 | *(state + 4*0 + 1) = temp; |
andrewboyson | 20:197c3e6e8b8d | 279 | |
andrewboyson | 20:197c3e6e8b8d | 280 | // Rotate second row 2 columns to right |
andrewboyson | 20:197c3e6e8b8d | 281 | temp = *(state + 4*0 + 2); |
andrewboyson | 20:197c3e6e8b8d | 282 | *(state + 4*0 + 2) = *(state + 4*2 + 2); |
andrewboyson | 20:197c3e6e8b8d | 283 | *(state + 4*2 + 2) = temp; |
andrewboyson | 20:197c3e6e8b8d | 284 | |
andrewboyson | 20:197c3e6e8b8d | 285 | temp = *(state + 4*1 + 2); |
andrewboyson | 20:197c3e6e8b8d | 286 | *(state + 4*1 + 2) = *(state + 4*3 + 2); |
andrewboyson | 20:197c3e6e8b8d | 287 | *(state + 4*3 + 2) = temp; |
andrewboyson | 20:197c3e6e8b8d | 288 | |
andrewboyson | 20:197c3e6e8b8d | 289 | // Rotate third row 3 columns to right |
andrewboyson | 20:197c3e6e8b8d | 290 | temp = *(state + 4*0 + 3); |
andrewboyson | 20:197c3e6e8b8d | 291 | *(state + 4*0 + 3) = *(state + 4*1 + 3); |
andrewboyson | 20:197c3e6e8b8d | 292 | *(state + 4*1 + 3) = *(state + 4*2 + 3); |
andrewboyson | 20:197c3e6e8b8d | 293 | *(state + 4*2 + 3) = *(state + 4*3 + 3); |
andrewboyson | 20:197c3e6e8b8d | 294 | *(state + 4*3 + 3) = temp; |
andrewboyson | 19:f22327e8be7b | 295 | } |
andrewboyson | 19:f22327e8be7b | 296 | |
andrewboyson | 19:f22327e8be7b | 297 | // Cipher is the main function that encrypts the PlainText. |
andrewboyson | 20:197c3e6e8b8d | 298 | static void cipher(uint8_t* state, const uint8_t* roundKey) |
andrewboyson | 19:f22327e8be7b | 299 | { |
andrewboyson | 20:197c3e6e8b8d | 300 | uint8_t round = 0; |
andrewboyson | 20:197c3e6e8b8d | 301 | |
andrewboyson | 20:197c3e6e8b8d | 302 | // Add the First round key to the state before starting the rounds. |
andrewboyson | 20:197c3e6e8b8d | 303 | addRoundKey(0, state, roundKey); |
andrewboyson | 20:197c3e6e8b8d | 304 | |
andrewboyson | 20:197c3e6e8b8d | 305 | // There will be Nr rounds. |
andrewboyson | 20:197c3e6e8b8d | 306 | // The first Nr-1 rounds are identical. |
andrewboyson | 20:197c3e6e8b8d | 307 | // These Nr-1 rounds are executed in the loop below. |
andrewboyson | 20:197c3e6e8b8d | 308 | for (round = 1; round < NR; ++round) |
andrewboyson | 20:197c3e6e8b8d | 309 | { |
andrewboyson | 20:197c3e6e8b8d | 310 | subBytes(state); |
andrewboyson | 20:197c3e6e8b8d | 311 | shiftRows(state); |
andrewboyson | 20:197c3e6e8b8d | 312 | mixColumns(state); |
andrewboyson | 20:197c3e6e8b8d | 313 | addRoundKey(round, state, roundKey); |
andrewboyson | 20:197c3e6e8b8d | 314 | } |
andrewboyson | 20:197c3e6e8b8d | 315 | |
andrewboyson | 20:197c3e6e8b8d | 316 | // The last round is given below. |
andrewboyson | 20:197c3e6e8b8d | 317 | // The MixColumns function is not here in the last round. |
andrewboyson | 20:197c3e6e8b8d | 318 | subBytes(state); |
andrewboyson | 20:197c3e6e8b8d | 319 | shiftRows(state); |
andrewboyson | 20:197c3e6e8b8d | 320 | addRoundKey(NR, state, roundKey); |
andrewboyson | 19:f22327e8be7b | 321 | } |
andrewboyson | 19:f22327e8be7b | 322 | |
andrewboyson | 20:197c3e6e8b8d | 323 | static void invCipher(uint8_t* state, const uint8_t* roundKey) |
andrewboyson | 19:f22327e8be7b | 324 | { |
andrewboyson | 20:197c3e6e8b8d | 325 | uint8_t round = 0; |
andrewboyson | 20:197c3e6e8b8d | 326 | |
andrewboyson | 20:197c3e6e8b8d | 327 | // Add the First round key to the state before starting the rounds. |
andrewboyson | 20:197c3e6e8b8d | 328 | addRoundKey(NR, state, roundKey); |
andrewboyson | 20:197c3e6e8b8d | 329 | |
andrewboyson | 20:197c3e6e8b8d | 330 | // There will be Nr rounds. |
andrewboyson | 20:197c3e6e8b8d | 331 | // The first Nr-1 rounds are identical. |
andrewboyson | 20:197c3e6e8b8d | 332 | // These Nr-1 rounds are executed in the loop below. |
andrewboyson | 20:197c3e6e8b8d | 333 | for (round = (NR - 1); round > 0; --round) |
andrewboyson | 20:197c3e6e8b8d | 334 | { |
andrewboyson | 20:197c3e6e8b8d | 335 | invShiftRows(state); |
andrewboyson | 20:197c3e6e8b8d | 336 | invSubBytes(state); |
andrewboyson | 20:197c3e6e8b8d | 337 | addRoundKey(round, state, roundKey); |
andrewboyson | 20:197c3e6e8b8d | 338 | invMixColumns(state); |
andrewboyson | 20:197c3e6e8b8d | 339 | } |
andrewboyson | 20:197c3e6e8b8d | 340 | |
andrewboyson | 20:197c3e6e8b8d | 341 | // The last round is given below. |
andrewboyson | 20:197c3e6e8b8d | 342 | // The MixColumns function is not here in the last round. |
andrewboyson | 20:197c3e6e8b8d | 343 | invShiftRows(state); |
andrewboyson | 20:197c3e6e8b8d | 344 | invSubBytes(state); |
andrewboyson | 20:197c3e6e8b8d | 345 | addRoundKey(0, state, roundKey); |
andrewboyson | 19:f22327e8be7b | 346 | } |
andrewboyson | 19:f22327e8be7b | 347 | |
andrewboyson | 19:f22327e8be7b | 348 | /*****************************************************************************/ |
andrewboyson | 19:f22327e8be7b | 349 | /* Public functions: */ |
andrewboyson | 19:f22327e8be7b | 350 | /*****************************************************************************/ |
andrewboyson | 19:f22327e8be7b | 351 | |
andrewboyson | 19:f22327e8be7b | 352 | void Aes128CbcEncrypt(const uint8_t* key, const uint8_t* iv, uint8_t* buf, uint32_t length) |
andrewboyson | 19:f22327e8be7b | 353 | { |
andrewboyson | 20:197c3e6e8b8d | 354 | uint8_t ctxRoundKey[176]; |
andrewboyson | 20:197c3e6e8b8d | 355 | keyExpansion(ctxRoundKey, key); |
andrewboyson | 20:197c3e6e8b8d | 356 | |
andrewboyson | 20:197c3e6e8b8d | 357 | const uint8_t* prevIv = iv; |
andrewboyson | 20:197c3e6e8b8d | 358 | for (int i = 0; i < length; i += AES128CBC_BLOCK_SIZE) |
andrewboyson | 20:197c3e6e8b8d | 359 | { |
andrewboyson | 20:197c3e6e8b8d | 360 | for (int j = 0; j < AES128CBC_BLOCK_SIZE; ++j) buf[j] ^= prevIv[j]; |
andrewboyson | 20:197c3e6e8b8d | 361 | cipher(buf, ctxRoundKey); |
andrewboyson | 20:197c3e6e8b8d | 362 | prevIv = buf; |
andrewboyson | 20:197c3e6e8b8d | 363 | buf += AES128CBC_BLOCK_SIZE; |
andrewboyson | 20:197c3e6e8b8d | 364 | } |
andrewboyson | 19:f22327e8be7b | 365 | } |
andrewboyson | 19:f22327e8be7b | 366 | void Aes128CbcDecrypt(const uint8_t* key, const uint8_t* iv, uint8_t* buf, uint32_t length) |
andrewboyson | 19:f22327e8be7b | 367 | { |
andrewboyson | 20:197c3e6e8b8d | 368 | uint8_t ctxRoundKey[176]; |
andrewboyson | 20:197c3e6e8b8d | 369 | keyExpansion(ctxRoundKey, key); |
andrewboyson | 20:197c3e6e8b8d | 370 | |
andrewboyson | 20:197c3e6e8b8d | 371 | uint8_t ctxIv[AES128CBC_BLOCK_SIZE]; |
andrewboyson | 20:197c3e6e8b8d | 372 | memcpy (ctxIv, iv, AES128CBC_BLOCK_SIZE); |
andrewboyson | 20:197c3e6e8b8d | 373 | |
andrewboyson | 20:197c3e6e8b8d | 374 | uint8_t storeNextIv[AES128CBC_BLOCK_SIZE]; |
andrewboyson | 20:197c3e6e8b8d | 375 | for (int i = 0; i < length; i += AES128CBC_BLOCK_SIZE) |
andrewboyson | 20:197c3e6e8b8d | 376 | { |
andrewboyson | 20:197c3e6e8b8d | 377 | memcpy(storeNextIv, buf, AES128CBC_BLOCK_SIZE); |
andrewboyson | 20:197c3e6e8b8d | 378 | invCipher(buf, ctxRoundKey); |
andrewboyson | 20:197c3e6e8b8d | 379 | for (int j = 0; j < AES128CBC_BLOCK_SIZE; ++j) buf[j] ^= ctxIv[j]; |
andrewboyson | 20:197c3e6e8b8d | 380 | memcpy(ctxIv, storeNextIv, AES128CBC_BLOCK_SIZE); |
andrewboyson | 20:197c3e6e8b8d | 381 | buf += AES128CBC_BLOCK_SIZE; |
andrewboyson | 20:197c3e6e8b8d | 382 | } |
andrewboyson | 19:f22327e8be7b | 383 | } |