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.
22 * aes_128_cbc_encrypt - AES-128 CBC encryption
23 * @key: Encryption key
24 * @iv: Encryption IV for CBC mode (16 bytes)
25 * @data: Data to encrypt in-place
26 * @data_len: Length of data in bytes (must be divisible by 16)
27 * Returns: 0 on success, -1 on failure
29 int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
36 ctx = aes_encrypt_init(key, 16);
39 os_memcpy(cbc, iv, BLOCK_SIZE);
41 blocks = data_len / BLOCK_SIZE;
42 for (i = 0; i < blocks; i++) {
43 for (j = 0; j < BLOCK_SIZE; j++)
45 aes_encrypt(ctx, cbc, cbc);
46 os_memcpy(pos, cbc, BLOCK_SIZE);
49 aes_encrypt_deinit(ctx);
55 * aes_128_cbc_decrypt - AES-128 CBC decryption
56 * @key: Decryption key
57 * @iv: Decryption IV for CBC mode (16 bytes)
58 * @data: Data to decrypt in-place
59 * @data_len: Length of data in bytes (must be divisible by 16)
60 * Returns: 0 on success, -1 on failure
62 int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
65 u8 cbc[BLOCK_SIZE], tmp[BLOCK_SIZE];
69 ctx = aes_decrypt_init(key, 16);
72 os_memcpy(cbc, iv, BLOCK_SIZE);
74 blocks = data_len / BLOCK_SIZE;
75 for (i = 0; i < blocks; i++) {
76 os_memcpy(tmp, pos, BLOCK_SIZE);
77 aes_decrypt(ctx, pos, pos);
78 for (j = 0; j < BLOCK_SIZE; j++)
80 os_memcpy(cbc, tmp, BLOCK_SIZE);
83 aes_decrypt_deinit(ctx);