Imported Upstream version 2.4
[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., 59 Temple Place - Suite 330, Boston,
23  * MA 02111-1307, 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 #define ROTBYTE(x) (((x) >> 8) | (((x) & 0xff) << 24))
67 #define ROTRBYTE(x) (((x) << 8) | (((x) >> 24) & 0xff))
68 #define SUBBYTE(x, box) (((box)[((x) & 0xff)]) | \
69                         ((box)[(((x) >> 8) & 0xff)] << 8) | \
70                         ((box)[(((x) >> 16) & 0xff)] << 16) | \
71                         ((box)[(((x) >> 24) & 0xff)] << 24))
72
73 /* Get the byte with index 0, 1, 2 and 3 */
74 #define B0(x) ((x) & 0xff)
75 #define B1(x) (((x) >> 8) & 0xff)
76 #define B2(x) (((x) >> 16) & 0xff)
77 #define B3(x) (((x) >> 24) & 0xff)
78
79 #define AES_ROUND(T, w0, w1, w2, w3, k)         \
80 ((  T->table[0][ B0(w0) ]                       \
81   ^ T->table[1][ B1(w1) ]                       \
82   ^ T->table[2][ B2(w2) ]                       \
83   ^ T->table[3][ B3(w3) ]) ^ (k))
84
85 #define AES_FINAL_ROUND(T, w0, w1, w2, w3, k)           \
86 ((   (uint32_t) T->sbox[ B0(w0) ]                       \
87   | ((uint32_t) T->sbox[ B1(w1) ] << 8)                 \
88   | ((uint32_t) T->sbox[ B2(w2) ] << 16)                \
89   | ((uint32_t) T->sbox[ B3(w3) ] << 24)) ^ (k))
90      
91 /* Globally visible so that the same sbox table can be used by aes_set_encrypt_key */
92
93 extern const struct aes_table _aes_encrypt_table;
94 #define aes_sbox (_aes_encrypt_table.sbox)
95
96 #endif /* NETTLE_AES_INTERNAL_H_INCLUDED */