]>
git.ipfire.org Git - thirdparty/hostap.git/blob - src/crypto/aes-cbc.c
4 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
17 * aes_128_cbc_encrypt - AES-128 CBC encryption
18 * @key: Encryption key
19 * @iv: Encryption IV for CBC mode (16 bytes)
20 * @data: Data to encrypt in-place
21 * @data_len: Length of data in bytes (must be divisible by 16)
22 * Returns: 0 on success, -1 on failure
24 int aes_128_cbc_encrypt(const u8
*key
, const u8
*iv
, u8
*data
, size_t data_len
)
27 u8 cbc
[AES_BLOCK_SIZE
];
34 ctx
= aes_encrypt_init(key
, 16);
37 os_memcpy(cbc
, iv
, AES_BLOCK_SIZE
);
39 blocks
= data_len
/ AES_BLOCK_SIZE
;
40 for (i
= 0; i
< blocks
; i
++) {
41 for (j
= 0; j
< AES_BLOCK_SIZE
; j
++)
43 aes_encrypt(ctx
, cbc
, cbc
);
44 os_memcpy(pos
, cbc
, AES_BLOCK_SIZE
);
45 pos
+= AES_BLOCK_SIZE
;
47 aes_encrypt_deinit(ctx
);
53 * aes_128_cbc_decrypt - AES-128 CBC decryption
54 * @key: Decryption key
55 * @iv: Decryption IV for CBC mode (16 bytes)
56 * @data: Data to decrypt in-place
57 * @data_len: Length of data in bytes (must be divisible by 16)
58 * Returns: 0 on success, -1 on failure
60 int aes_128_cbc_decrypt(const u8
*key
, const u8
*iv
, u8
*data
, size_t data_len
)
63 u8 cbc
[AES_BLOCK_SIZE
], tmp
[AES_BLOCK_SIZE
];
70 ctx
= aes_decrypt_init(key
, 16);
73 os_memcpy(cbc
, iv
, AES_BLOCK_SIZE
);
75 blocks
= data_len
/ AES_BLOCK_SIZE
;
76 for (i
= 0; i
< blocks
; i
++) {
77 os_memcpy(tmp
, pos
, AES_BLOCK_SIZE
);
78 aes_decrypt(ctx
, pos
, pos
);
79 for (j
= 0; j
< AES_BLOCK_SIZE
; j
++)
81 os_memcpy(cbc
, tmp
, AES_BLOCK_SIZE
);
82 pos
+= AES_BLOCK_SIZE
;
84 aes_decrypt_deinit(ctx
);