Remove unneeded aes_i.h inclusion from number of places
[mech_eap.orig] / src / crypto / aes-ctr.c
1 /*
2  * AES-128 CTR
3  *
4  * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  */
15
16 #include "includes.h"
17
18 #include "common.h"
19 #include "aes.h"
20
21 /**
22  * aes_128_ctr_encrypt - AES-128 CTR mode encryption
23  * @key: Key for encryption (16 bytes)
24  * @nonce: Nonce for counter mode (16 bytes)
25  * @data: Data to encrypt in-place
26  * @data_len: Length of data in bytes
27  * Returns: 0 on success, -1 on failure
28  */
29 int aes_128_ctr_encrypt(const u8 *key, const u8 *nonce,
30                         u8 *data, size_t data_len)
31 {
32         void *ctx;
33         size_t j, len, left = data_len;
34         int i;
35         u8 *pos = data;
36         u8 counter[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
37
38         ctx = aes_encrypt_init(key, 16);
39         if (ctx == NULL)
40                 return -1;
41         os_memcpy(counter, nonce, AES_BLOCK_SIZE);
42
43         while (left > 0) {
44                 aes_encrypt(ctx, counter, buf);
45
46                 len = (left < AES_BLOCK_SIZE) ? left : AES_BLOCK_SIZE;
47                 for (j = 0; j < len; j++)
48                         pos[j] ^= buf[j];
49                 pos += len;
50                 left -= len;
51
52                 for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
53                         counter[i]++;
54                         if (counter[i])
55                                 break;
56                 }
57         }
58         aes_encrypt_deinit(ctx);
59         return 0;
60 }