Rizin
unix-like reverse engineering framework and cli tools
aes-set-key-internal.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2000, 2001, 2002 Rafael R. Sevilla, Niels Möller
2 // SPDX-FileCopyrightText: 2013 Niels Möller
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 /* aes-set-key-internal.c
6 
7  Key setup for the aes/rijndael block cipher.
8 
9  Copyright (C) 2000, 2001, 2002 Rafael R. Sevilla, Niels Möller
10  Copyright (C) 2013 Niels Möller
11 
12  This file is part of GNU Nettle.
13 
14  GNU Nettle is free software: you can redistribute it and/or
15  modify it under the terms of either:
16 
17  * the GNU Lesser General Public License as published by the Free
18  Software Foundation; either version 3 of the License, or (at your
19  option) any later version.
20 
21  or
22 
23  * the GNU General Public License as published by the Free
24  Software Foundation; either version 2 of the License, or (at your
25  option) any later version.
26 
27  or both in parallel, as here.
28 
29  GNU Nettle is distributed in the hope that it will be useful,
30  but WITHOUT ANY WARRANTY; without even the implied warranty of
31  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32  General Public License for more details.
33 
34  You should have received copies of the GNU General Public License and
35  the GNU Lesser General Public License along with this program. If
36  not, see http://www.gnu.org/licenses/.
37 */
38 
39 /* Originally written by Rafael R. Sevilla <dido@pacific.net.ph> */
40 
41 #if HAVE_CONFIG_H
42 # include "config.h"
43 #endif
44 
45 #include "aes-internal.h"
46 #include <assert.h>
47 #include "macros.h"
48 
49 void
50 _nettle_aes_set_key(unsigned nr, unsigned nk,
51  uint32_t *subkeys, const uint8_t *key)
52 {
53  static const uint8_t rcon[10] = {
54  0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1b,0x36,
55  };
56  const uint8_t *rp;
57  unsigned lastkey, i;
58  uint32_t t;
59 
60  assert(nk != 0);
61  lastkey = (AES_BLOCK_SIZE/4) * (nr + 1);
62 
63  for (i=0, rp = rcon; i<nk; i++)
64  subkeys[i] = LE_READ_UINT32(key + i*4);
65 
66  for (i=nk; i<lastkey; i++)
67  {
68  t = subkeys[i-1];
69  if (i % nk == 0)
70  t = SUBBYTE(ROTL32(24, t), aes_sbox) ^ *rp++;
71 
72  else if (nk > 6 && (i%nk) == 4)
73  t = SUBBYTE(t, aes_sbox);
74 
75  subkeys[i] = subkeys[i-nk] ^ t;
76  }
77 }
#define SUBBYTE(x, box)
Definition: aes-internal.h:85
#define aes_sbox
Definition: aes-internal.h:103
void _nettle_aes_set_key(unsigned nr, unsigned nk, uint32_t *subkeys, const uint8_t *key)
lzma_index ** i
Definition: index.h:629
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
static char * rp[]
Definition: i8080dis.c:36
assert(limit<=UINT32_MAX/2)
#define ROTL32(n, x)
Definition: macros.h:157
#define LE_READ_UINT32(p)
Definition: macros.h:122
unsigned int uint32_t
Definition: sftypes.h:29
unsigned char uint8_t
Definition: sftypes.h:31
#define AES_BLOCK_SIZE
Definition: zipint.h:77