/src/nettle/aes-decrypt-internal.c
Line  | Count  | Source (jump to first uncovered line)  | 
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  |  | /* For fat builds */  | 
44  |  | #if HAVE_NATIVE_aes_decrypt  | 
45  |  | void  | 
46  |  | _nettle_aes_decrypt_c(unsigned rounds, const uint32_t *keys,  | 
47  |  |     const struct aes_table *T,  | 
48  |  |     size_t length, uint8_t *dst,  | 
49  |  |     const uint8_t *src);  | 
50  |  | #define _nettle_aes_decrypt _nettle_aes_decrypt_c  | 
51  |  | #endif  | 
52  |  |  | 
53  |  | void  | 
54  |  | _nettle_aes_decrypt(unsigned rounds, const uint32_t *keys,  | 
55  |  |         const struct aes_table *T,  | 
56  |  |         size_t length, uint8_t *dst,  | 
57  |  |         const uint8_t *src)  | 
58  | 0  | { | 
59  | 0  |   FOR_BLOCKS(length, dst, src, AES_BLOCK_SIZE)  | 
60  | 0  |     { | 
61  | 0  |       uint32_t w0, w1, w2, w3;    /* working ciphertext */  | 
62  | 0  |       uint32_t t0, t1, t2, t3;  | 
63  | 0  |       const uint32_t *p;  | 
64  | 0  |       unsigned i;  | 
65  |  |         | 
66  |  |       /* Get clear text, using little-endian byte order.  | 
67  |  |        * Also XOR with the first subkey. */  | 
68  |  | 
  | 
69  | 0  |       w0 = LE_READ_UINT32(src)      ^ keys[0];  | 
70  | 0  |       w1 = LE_READ_UINT32(src + 4)  ^ keys[1];  | 
71  | 0  |       w2 = LE_READ_UINT32(src + 8)  ^ keys[2];  | 
72  | 0  |       w3 = LE_READ_UINT32(src + 12) ^ keys[3];  | 
73  |  | 
  | 
74  | 0  |       for (i = 1, p = keys - 4; i < rounds; i++, p -= 4)  | 
75  | 0  |   { | 
76  | 0  |     t0 = AES_ROUND(T, w0, w3, w2, w1, p[0]);  | 
77  | 0  |     t1 = AES_ROUND(T, w1, w0, w3, w2, p[1]);  | 
78  | 0  |     t2 = AES_ROUND(T, w2, w1, w0, w3, p[2]);  | 
79  | 0  |     t3 = AES_ROUND(T, w3, w2, w1, w0, p[3]);  | 
80  |  |  | 
81  |  |     /* We could unroll the loop twice, to avoid these  | 
82  |  |        assignments. If all eight variables fit in registers,  | 
83  |  |        that should give a slight speedup. */  | 
84  | 0  |     w0 = t0;  | 
85  | 0  |     w1 = t1;  | 
86  | 0  |     w2 = t2;  | 
87  | 0  |     w3 = t3;  | 
88  | 0  |   }  | 
89  |  |  | 
90  |  |       /* Final round */  | 
91  |  | 
  | 
92  | 0  |       t0 = AES_FINAL_ROUND(T, w0, w3, w2, w1, p[0]);  | 
93  | 0  |       t1 = AES_FINAL_ROUND(T, w1, w0, w3, w2, p[1]);  | 
94  | 0  |       t2 = AES_FINAL_ROUND(T, w2, w1, w0, w3, p[2]);  | 
95  | 0  |       t3 = AES_FINAL_ROUND(T, w3, w2, w1, w0, p[3]);  | 
96  |  | 
  | 
97  | 0  |       LE_WRITE_UINT32(dst, t0);  | 
98  | 0  |       LE_WRITE_UINT32(dst + 4, t1);  | 
99  | 0  |       LE_WRITE_UINT32(dst + 8, t2);  | 
100  | 0  |       LE_WRITE_UINT32(dst + 12, t3);  | 
101  | 0  |     }  | 
102  | 0  | }  |