Fix build error and set version 2.7.1
[platform/upstream/nettle.git] / aes-internal.h
1 /* aes-internal.h
2  *
3  * The aes/rijndael block cipher.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2001 Niels Möller
9  *  
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.
14  * 
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.
19  * 
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,
23  * MA 02111-1301, USA.
24  */
25
26 #ifndef NETTLE_AES_INTERNAL_H_INCLUDED
27 #define NETTLE_AES_INTERNAL_H_INCLUDED
28
29 #include "aes.h"
30
31 /* Name mangling */
32 #define _aes_encrypt _nettle_aes_encrypt
33 #define _aes_decrypt _nettle_aes_decrypt
34 #define _aes_encrypt_table _nettle_aes_encrypt_table
35
36 /* Define to use only small tables. */
37 #ifndef AES_SMALL
38 # define AES_SMALL 0
39 #endif
40
41 #if AES_SMALL
42 # define AES_TABLE_SIZE 1
43 #else
44 # define AES_TABLE_SIZE 4
45 #endif
46
47 struct aes_table
48 {
49   uint8_t sbox[0x100];
50   uint32_t table[AES_TABLE_SIZE][0x100];
51 };
52
53 void
54 _aes_encrypt(const struct aes_ctx *ctx,
55              const struct aes_table *T,
56              unsigned length, uint8_t *dst,
57              const uint8_t *src);
58
59 void
60 _aes_decrypt(const struct aes_ctx *ctx,
61              const struct aes_table *T,
62              unsigned length, uint8_t *dst,
63              const uint8_t *src);
64
65 /* Macros */
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)
71
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))
76
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))
82
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))
88      
89 /* Globally visible so that the same sbox table can be used by aes_set_encrypt_key */
90
91 extern const struct aes_table _aes_encrypt_table;
92 #define aes_sbox (_aes_encrypt_table.sbox)
93
94 #endif /* NETTLE_AES_INTERNAL_H_INCLUDED */