/src/nettle-with-libgmp/aes-set-key-internal.c
Line  | Count  | Source  | 
1  |  | /* aes-set-key-internal.c  | 
2  |  |    | 
3  |  |    Key setup for the aes/rijndael block cipher.  | 
4  |  |  | 
5  |  |    Copyright (C) 2000, 2001, 2002 Rafael R. Sevilla, Niels Möller  | 
6  |  |    Copyright (C) 2013 Niels Möller  | 
7  |  |  | 
8  |  |    This file is part of GNU Nettle.  | 
9  |  |  | 
10  |  |    GNU Nettle is free software: you can redistribute it and/or  | 
11  |  |    modify it under the terms of either:  | 
12  |  |  | 
13  |  |      * the GNU Lesser General Public License as published by the Free  | 
14  |  |        Software Foundation; either version 3 of the License, or (at your  | 
15  |  |        option) any later version.  | 
16  |  |  | 
17  |  |    or  | 
18  |  |  | 
19  |  |      * the GNU General Public License as published by the Free  | 
20  |  |        Software Foundation; either version 2 of the License, or (at your  | 
21  |  |        option) any later version.  | 
22  |  |  | 
23  |  |    or both in parallel, as here.  | 
24  |  |  | 
25  |  |    GNU Nettle is distributed in the hope that it will be useful,  | 
26  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
27  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
28  |  |    General Public License for more details.  | 
29  |  |  | 
30  |  |    You should have received copies of the GNU General Public License and  | 
31  |  |    the GNU Lesser General Public License along with this program.  If  | 
32  |  |    not, see http://www.gnu.org/licenses/.  | 
33  |  | */  | 
34  |  |  | 
35  |  | /* Originally written by Rafael R. Sevilla <dido@pacific.net.ph> */  | 
36  |  |  | 
37  |  | #if HAVE_CONFIG_H  | 
38  |  | # include "config.h"  | 
39  |  | #endif  | 
40  |  |  | 
41  |  | #include "aes-internal.h"  | 
42  |  | #include <assert.h>  | 
43  |  | #include "macros.h"  | 
44  |  |  | 
45  |  | void  | 
46  |  | _nettle_aes_set_key(unsigned nr, unsigned nk,  | 
47  |  |         uint32_t *subkeys, const uint8_t *key)  | 
48  | 7.30k  | { | 
49  | 7.30k  |   static const uint8_t rcon[10] = { | 
50  | 7.30k  |     0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36,  | 
51  | 7.30k  |   };  | 
52  | 7.30k  |   const uint8_t *rp;  | 
53  | 7.30k  |   unsigned lastkey, i;  | 
54  | 7.30k  |   uint32_t t;  | 
55  |  |  | 
56  | 7.30k  |   assert(nk != 0);  | 
57  | 7.30k  |   lastkey = (AES_BLOCK_SIZE/4) * (nr + 1);  | 
58  |  |     | 
59  | 42.6k  |   for (i=0, rp = rcon; i<nk; i++)  | 
60  | 35.3k  |     subkeys[i] = LE_READ_UINT32(key + i*4);  | 
61  |  |  | 
62  | 317k  |   for (i=nk; i<lastkey; i++)  | 
63  | 310k  |     { | 
64  | 310k  |       t = subkeys[i-1];  | 
65  | 310k  |       if (i % nk == 0)  | 
66  | 68.0k  |   t = SUBBYTE(ROTL32(24, t), aes_sbox) ^ *rp++;  | 
67  |  |  | 
68  | 242k  |       else if (nk > 6 && (i%nk) == 4)  | 
69  | 6.93k  |   t = SUBBYTE(t, aes_sbox);  | 
70  |  |  | 
71  | 310k  |       subkeys[i] = subkeys[i-nk] ^ t;  | 
72  | 310k  |     }    | 
73  | 7.30k  | }  |