Update copyright headers for dual licensing.
[gd/nettle] / aes-decrypt-internal.c
1 /* aes-decrypt-internal.c
2
3    Decryption function for the aes/rijndael block cipher.
4
5    Copyright 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_decrypt(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, w3, w2, w1, keys[4*i]);
66           t1 = AES_ROUND(T, w1, w0, w3, w2, keys[4*i + 1]);
67           t2 = AES_ROUND(T, w2, w1, w0, w3, keys[4*i + 2]);
68           t3 = AES_ROUND(T, w3, w2, w1, w0, 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, w3, w2, w1, keys[4*i]);
82       t1 = AES_FINAL_ROUND(T, w1, w0, w3, w2, keys[4*i + 1]);
83       t2 = AES_FINAL_ROUND(T, w2, w1, w0, w3, keys[4*i + 2]);
84       t3 = AES_FINAL_ROUND(T, w3, w2, w1, w0, 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 }