Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / aes-decrypt-internal.c
blob7236208dca3b935b377211b921759585be1190c3
1 /* aes-decrypt-internal.c
3 * Decryption function for the aes/rijndael block cipher.
4 */
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2002 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.
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.
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., 51 Franklin Street, Fifth Floor, Boston,
23 * MA 02111-1301, USA.
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <assert.h>
32 #include "aes-internal.h"
33 #include "macros.h"
35 void
36 _nettle_aes_decrypt(const struct aes_ctx *ctx,
37 const struct aes_table *T,
38 unsigned length, uint8_t *dst,
39 const uint8_t *src)
41 FOR_BLOCKS(length, dst, src, AES_BLOCK_SIZE)
43 uint32_t w0, w1, w2, w3; /* working ciphertext */
44 uint32_t t0, t1, t2, t3;
45 unsigned round;
47 /* Get clear text, using little-endian byte order.
48 * Also XOR with the first subkey. */
50 w0 = LE_READ_UINT32(src) ^ ctx->keys[0];
51 w1 = LE_READ_UINT32(src + 4) ^ ctx->keys[1];
52 w2 = LE_READ_UINT32(src + 8) ^ ctx->keys[2];
53 w3 = LE_READ_UINT32(src + 12) ^ ctx->keys[3];
55 for (round = 1; round < ctx->nrounds; round++)
57 t0 = AES_ROUND(T, w0, w3, w2, w1, ctx->keys[4*round]);
58 t1 = AES_ROUND(T, w1, w0, w3, w2, ctx->keys[4*round + 1]);
59 t2 = AES_ROUND(T, w2, w1, w0, w3, ctx->keys[4*round + 2]);
60 t3 = AES_ROUND(T, w3, w2, w1, w0, ctx->keys[4*round + 3]);
62 /* We could unroll the loop twice, to avoid these
63 assignments. If all eight variables fit in registers,
64 that should give a slight speedup. */
65 w0 = t0;
66 w1 = t1;
67 w2 = t2;
68 w3 = t3;
71 /* Final round */
73 t0 = AES_FINAL_ROUND(T, w0, w3, w2, w1, ctx->keys[4*round]);
74 t1 = AES_FINAL_ROUND(T, w1, w0, w3, w2, ctx->keys[4*round + 1]);
75 t2 = AES_FINAL_ROUND(T, w2, w1, w0, w3, ctx->keys[4*round + 2]);
76 t3 = AES_FINAL_ROUND(T, w3, w2, w1, w0, ctx->keys[4*round + 3]);
78 LE_WRITE_UINT32(dst, t0);
79 LE_WRITE_UINT32(dst + 8, t2);
80 LE_WRITE_UINT32(dst + 4, t1);
81 LE_WRITE_UINT32(dst + 12, t3);