Add missing includes of stdlib.h.
[gd/nettle] / aes-set-encrypt-key.c
1 /* aes-set-encrypt-key.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 #if HAVE_CONFIG_H
36 # include "config.h"
37 #endif
38
39 #include <assert.h>
40 #include <stdlib.h>
41
42 #include "aes-internal.h"
43
44 void
45 aes_set_encrypt_key(struct aes_ctx *ctx,
46                     size_t keysize, const uint8_t *key)
47 {
48   unsigned nk, nr;
49
50   assert(keysize >= AES_MIN_KEY_SIZE);
51   assert(keysize <= AES_MAX_KEY_SIZE);
52   
53   /* Truncate keysizes to the valid key sizes provided by Rijndael */
54   if (keysize == AES256_KEY_SIZE) {
55     nk = 8;
56     nr = _AES256_ROUNDS;
57   } else if (keysize >= AES192_KEY_SIZE) {
58     nk = 6;
59     nr = _AES192_ROUNDS;
60   } else { /* must be 16 or more */
61     nk = 4;
62     nr = _AES128_ROUNDS;
63   }
64
65   ctx->rounds = nr;
66   _aes_set_key (nr, nk, ctx->keys, key);
67 }