3 * The aes/rijndael block cipher.
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2001 Niels Möller
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 #ifndef NETTLE_AES_INTERNAL_H_INCLUDED
27 #define NETTLE_AES_INTERNAL_H_INCLUDED
32 #define _aes_encrypt _nettle_aes_encrypt
33 #define _aes_decrypt _nettle_aes_decrypt
34 #define _aes_encrypt_table _nettle_aes_encrypt_table
36 /* Define to use only small tables. */
42 # define AES_TABLE_SIZE 1
44 # define AES_TABLE_SIZE 4
50 uint32_t table
[AES_TABLE_SIZE
][0x100];
54 _aes_encrypt(const struct aes_ctx
*ctx
,
55 const struct aes_table
*T
,
56 unsigned length
, uint8_t *dst
,
60 _aes_decrypt(const struct aes_ctx
*ctx
,
61 const struct aes_table
*T
,
62 unsigned length
, uint8_t *dst
,
66 /* Get the byte with index 0, 1, 2 and 3 */
67 #define B0(x) ((x) & 0xff)
68 #define B1(x) (((x) >> 8) & 0xff)
69 #define B2(x) (((x) >> 16) & 0xff)
70 #define B3(x) (((x) >> 24) & 0xff)
72 #define SUBBYTE(x, box) ((uint32_t)(box)[B0(x)] \
73 | ((uint32_t)(box)[B1(x)] << 8) \
74 | ((uint32_t)(box)[B2(x)] << 16) \
75 | ((uint32_t)(box)[B3(x)] << 24))
77 #define AES_ROUND(T, w0, w1, w2, w3, k) \
78 (( T->table[0][ B0(w0) ] \
79 ^ T->table[1][ B1(w1) ] \
80 ^ T->table[2][ B2(w2) ] \
81 ^ T->table[3][ B3(w3) ]) ^ (k))
83 #define AES_FINAL_ROUND(T, w0, w1, w2, w3, k) \
84 (( (uint32_t) T->sbox[ B0(w0) ] \
85 | ((uint32_t) T->sbox[ B1(w1) ] << 8) \
86 | ((uint32_t) T->sbox[ B2(w2) ] << 16) \
87 | ((uint32_t) T->sbox[ B3(w3) ] << 24)) ^ (k))
89 /* Globally visible so that the same sbox table can be used by aes_set_encrypt_key */
91 extern const struct aes_table _aes_encrypt_table
;
92 #define aes_sbox (_aes_encrypt_table.sbox)
94 #endif /* NETTLE_AES_INTERNAL_H_INCLUDED */