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
_aes_set_key(unsigned nr,unsigned nk,uint32_t * subkeys,const uint8_t * key)46 _aes_set_key(unsigned nr, unsigned nk,
47 	     uint32_t *subkeys, const uint8_t *key)
48 {
49   static const uint8_t rcon[10] = {
50     0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36,
51   };
52   const uint8_t *rp;
53   unsigned lastkey, i;
54   uint32_t t;
55 
56   assert(nk != 0);
57   lastkey = (AES_BLOCK_SIZE/4) * (nr + 1);
58 
59   for (i=0, rp = rcon; i<nk; i++)
60     subkeys[i] = LE_READ_UINT32(key + i*4);
61 
62   for (i=nk; i<lastkey; i++)
63     {
64       t = subkeys[i-1];
65       if (i % nk == 0)
66 	t = SUBBYTE(ROTL32(24, t), aes_sbox) ^ *rp++;
67 
68       else if (nk > 6 && (i%nk) == 4)
69 	t = SUBBYTE(t, aes_sbox);
70 
71       subkeys[i] = subkeys[i-nk] ^ t;
72     }
73 }
74