3 The aes/rijndael block cipher.
5 Copyright (C) 2001, 2013 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
34 #ifndef NETTLE_AES_INTERNAL_H_INCLUDED
35 #define NETTLE_AES_INTERNAL_H_INCLUDED
40 #define _aes_set_key _nettle_aes_set_key
41 #define _aes_invert _nettle_aes_invert
42 #define _aes_encrypt _nettle_aes_encrypt
43 #define _aes_decrypt _nettle_aes_decrypt
44 #define _aes_encrypt_table _nettle_aes_encrypt_table
46 /* Define to use only small tables. */
52 # define AES_TABLE_SIZE 1
54 # define AES_TABLE_SIZE 4
60 uint32_t table[AES_TABLE_SIZE][0x100];
64 _aes_set_key(unsigned nr, unsigned nk,
65 uint32_t *subkeys, const uint8_t *key);
68 _aes_invert(unsigned rounds, uint32_t *dst, const uint32_t *src);
71 _aes_encrypt(unsigned rounds, const uint32_t *keys,
72 const struct aes_table *T,
73 size_t length, uint8_t *dst,
77 _aes_decrypt(unsigned rounds, const uint32_t *keys,
78 const struct aes_table *T,
79 size_t length, uint8_t *dst,
83 /* Get the byte with index 0, 1, 2 and 3 */
84 #define B0(x) ((x) & 0xff)
85 #define B1(x) (((x) >> 8) & 0xff)
86 #define B2(x) (((x) >> 16) & 0xff)
87 #define B3(x) (((x) >> 24) & 0xff)
89 #define SUBBYTE(x, box) ((uint32_t)(box)[B0(x)] \
90 | ((uint32_t)(box)[B1(x)] << 8) \
91 | ((uint32_t)(box)[B2(x)] << 16) \
92 | ((uint32_t)(box)[B3(x)] << 24))
94 #define AES_ROUND(T, w0, w1, w2, w3, k) \
95 (( T->table[0][ B0(w0) ] \
96 ^ T->table[1][ B1(w1) ] \
97 ^ T->table[2][ B2(w2) ] \
98 ^ T->table[3][ B3(w3) ]) ^ (k))
100 #define AES_FINAL_ROUND(T, w0, w1, w2, w3, k) \
101 (( (uint32_t) T->sbox[ B0(w0) ] \
102 | ((uint32_t) T->sbox[ B1(w1) ] << 8) \
103 | ((uint32_t) T->sbox[ B2(w2) ] << 16) \
104 | ((uint32_t) T->sbox[ B3(w3) ] << 24)) ^ (k))
106 /* Globally visible so that the same sbox table can be used by aes_set_encrypt_key */
108 extern const struct aes_table _aes_encrypt_table;
109 #define aes_sbox (_aes_encrypt_table.sbox)
111 #endif /* NETTLE_AES_INTERNAL_H_INCLUDED */