Merge branch 'upstream' into tizen
[platform/upstream/nettle.git] / aes-internal.h
1 /* aes-internal.h
2
3    The aes/rijndael block cipher.
4
5    Copyright (C) 2001, 2013 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
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.
15
16    or
17
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.
21
22    or both in parallel, as here.
23
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.
28
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/.
32 */
33
34 #ifndef NETTLE_AES_INTERNAL_H_INCLUDED
35 #define NETTLE_AES_INTERNAL_H_INCLUDED
36
37 #include "aes.h"
38
39 /* Name mangling */
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
45
46 /* Define to use only small tables. */
47 #ifndef AES_SMALL
48 # define AES_SMALL 0
49 #endif
50
51 #if AES_SMALL
52 # define AES_TABLE_SIZE 1
53 #else
54 # define AES_TABLE_SIZE 4
55 #endif
56
57 struct aes_table
58 {
59   uint8_t sbox[0x100];
60   uint32_t table[AES_TABLE_SIZE][0x100];
61 };
62
63 void
64 _aes_set_key(unsigned nr, unsigned nk,
65              uint32_t *subkeys, const uint8_t *key);
66
67 void
68 _aes_invert(unsigned rounds, uint32_t *dst, const uint32_t *src);
69
70 void
71 _aes_encrypt(unsigned rounds, const uint32_t *keys,
72              const struct aes_table *T,
73              size_t length, uint8_t *dst,
74              const uint8_t *src);
75
76 void
77 _aes_decrypt(unsigned rounds, const uint32_t *keys,
78              const struct aes_table *T,
79              size_t length, uint8_t *dst,
80              const uint8_t *src);
81
82 /* Macros */
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)
88
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))
93
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))
99
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))
105      
106 /* Globally visible so that the same sbox table can be used by aes_set_encrypt_key */
107
108 extern const struct aes_table _aes_encrypt_table;
109 #define aes_sbox (_aes_encrypt_table.sbox)
110
111 #endif /* NETTLE_AES_INTERNAL_H_INCLUDED */