4 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
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.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
23 * aes_128_ctr_encrypt - AES-128 CTR mode encryption
24 * @key: Key for encryption (16 bytes)
25 * @nonce: Nonce for counter mode (16 bytes)
26 * @data: Data to encrypt in-place
27 * @data_len: Length of data in bytes
28 * Returns: 0 on success, -1 on failure
30 int aes_128_ctr_encrypt(const u8
*key
, const u8
*nonce
,
31 u8
*data
, size_t data_len
)
34 size_t j
, len
, left
= data_len
;
37 u8 counter
[AES_BLOCK_SIZE
], buf
[AES_BLOCK_SIZE
];
39 ctx
= aes_encrypt_init(key
, 16);
42 os_memcpy(counter
, nonce
, AES_BLOCK_SIZE
);
45 aes_encrypt(ctx
, counter
, buf
);
47 len
= (left
< AES_BLOCK_SIZE
) ? left
: AES_BLOCK_SIZE
;
48 for (j
= 0; j
< len
; j
++)
53 for (i
= AES_BLOCK_SIZE
- 1; i
>= 0; i
--) {
59 aes_encrypt_deinit(ctx
);