9f61386db94a932edbbeabb650c3503d9d34113b
[platform/upstream/nettle.git] / aes-encrypt-internal.c
1 /* aes-encrypt-internal.c
2
3    Encryption function for the aes/rijndael block cipher.
4
5    Copyright (C) 2002, 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 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <assert.h>
39
40 #include "aes-internal.h"
41 #include "macros.h"
42
43 void
44 _nettle_aes_encrypt(unsigned rounds, const uint32_t *keys,
45                     const struct aes_table *T,
46                     size_t length, uint8_t *dst,
47                     const uint8_t *src)
48 {
49   FOR_BLOCKS(length, dst, src, AES_BLOCK_SIZE)
50     {
51       uint32_t w0, w1, w2, w3;          /* working ciphertext */
52       uint32_t t0, t1, t2, t3;
53       unsigned i;
54       
55       /* Get clear text, using little-endian byte order.
56        * Also XOR with the first subkey. */
57
58       w0 = LE_READ_UINT32(src)      ^ keys[0];
59       w1 = LE_READ_UINT32(src + 4)  ^ keys[1];
60       w2 = LE_READ_UINT32(src + 8)  ^ keys[2];
61       w3 = LE_READ_UINT32(src + 12) ^ keys[3];
62
63       for (i = 1; i < rounds; i++)
64         {
65           t0 = AES_ROUND(T, w0, w1, w2, w3, keys[4*i]);
66           t1 = AES_ROUND(T, w1, w2, w3, w0, keys[4*i + 1]);
67           t2 = AES_ROUND(T, w2, w3, w0, w1, keys[4*i + 2]);
68           t3 = AES_ROUND(T, w3, w0, w1, w2, keys[4*i + 3]);
69
70           /* We could unroll the loop twice, to avoid these
71              assignments. If all eight variables fit in registers,
72              that should give a slight speedup. */
73           w0 = t0;
74           w1 = t1;
75           w2 = t2;
76           w3 = t3;
77         }
78
79       /* Final round */
80
81       t0 = AES_FINAL_ROUND(T, w0, w1, w2, w3, keys[4*i]);
82       t1 = AES_FINAL_ROUND(T, w1, w2, w3, w0, keys[4*i + 1]);
83       t2 = AES_FINAL_ROUND(T, w2, w3, w0, w1, keys[4*i + 2]);
84       t3 = AES_FINAL_ROUND(T, w3, w0, w1, w2, keys[4*i + 3]);
85
86       LE_WRITE_UINT32(dst, t0);
87       LE_WRITE_UINT32(dst + 4, t1);
88       LE_WRITE_UINT32(dst + 8, t2);
89       LE_WRITE_UINT32(dst + 12, t3);
90     }
91 }
92
93 /* Some stats, all for AES 128:
94
95    A. Table-driven indexing (the approach of the old unified
96       _aes_crypt function).
97    B. Unrolling the j-loop.
98
99    C. Eliminated the use of IDXk(j) in the main loop.
100
101    D. Put wtxt in four scalar variables.
102
103    E. Also put t in four scalar variables.
104
105        P4 2.2 GHz         AMD Duron 1.4GHz
106        
107        MB/s  code size
108    A   35.9  0x202        17 MB/s
109    B   37.3  0x334
110    C   33.0  0x2a7
111    D   40.7  0x3f9
112    E   42.9  0x44a        26 MB/s
113  */