1 /* aes-decrypt-internal.c
3 Decryption function for the aes/rijndael block cipher.
5 Copyright 2002, 2013 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
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.
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.
22 or both in parallel, as here.
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.
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/.
40 #include "aes-internal.h"
44 _nettle_aes_decrypt(unsigned rounds, const uint32_t *keys,
45 const struct aes_table *T,
46 size_t length, uint8_t *dst,
49 FOR_BLOCKS(length, dst, src, AES_BLOCK_SIZE)
51 uint32_t w0, w1, w2, w3; /* working ciphertext */
52 uint32_t t0, t1, t2, t3;
55 /* Get clear text, using little-endian byte order.
56 * Also XOR with the first subkey. */
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];
63 for (i = 1; i < rounds; i++)
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]);
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. */
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]);
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);