mirror of
https://github.com/espressif/esp-idf.git
synced 2025-08-17 15:15:02 +00:00

This commit add following crypto changes 1. Update current crypto code with upstream supplicant code 2. Add a proper porting layer to use mbedtls APIs for all the crypto operations used by supplicant. Internal crypto will be used when USE_MBEDLTS flag is disabled in supplicant's menuconfig. This commit also removes the clutter in crypto files due to partial porting of some APIs to mbedtls, all the code from those files have been removed and rewritten in a generic way, this is inspired from current upstream code. This also reduces the lib size significantly, supplicant's lib size reduces around ~567kb after this change(NB: lib size doesn't indicate reduction in final bin size).
87 lines
1.9 KiB
C
87 lines
1.9 KiB
C
/*
|
|
* AES-128 CBC
|
|
*
|
|
* Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
|
|
*
|
|
* This software may be distributed under the terms of the BSD license.
|
|
* See README for more details.
|
|
*/
|
|
|
|
#include "includes.h"
|
|
|
|
#include "common.h"
|
|
#include "aes.h"
|
|
#include "aes_wrap.h"
|
|
|
|
/**
|
|
* aes_128_cbc_encrypt - AES-128 CBC encryption
|
|
* @key: Encryption key
|
|
* @iv: Encryption IV for CBC mode (16 bytes)
|
|
* @data: Data to encrypt in-place
|
|
* @data_len: Length of data in bytes (must be divisible by 16)
|
|
* Returns: 0 on success, -1 on failure
|
|
*/
|
|
int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
|
|
{
|
|
void *ctx;
|
|
u8 cbc[AES_BLOCK_SIZE];
|
|
u8 *pos = data;
|
|
int i, j, blocks;
|
|
|
|
if (TEST_FAIL())
|
|
return -1;
|
|
|
|
ctx = aes_encrypt_init(key, 16);
|
|
if (ctx == NULL)
|
|
return -1;
|
|
os_memcpy(cbc, iv, AES_BLOCK_SIZE);
|
|
|
|
blocks = data_len / AES_BLOCK_SIZE;
|
|
for (i = 0; i < blocks; i++) {
|
|
for (j = 0; j < AES_BLOCK_SIZE; j++)
|
|
cbc[j] ^= pos[j];
|
|
aes_encrypt(ctx, cbc, cbc);
|
|
os_memcpy(pos, cbc, AES_BLOCK_SIZE);
|
|
pos += AES_BLOCK_SIZE;
|
|
}
|
|
aes_encrypt_deinit(ctx);
|
|
return 0;
|
|
}
|
|
|
|
|
|
/**
|
|
* aes_128_cbc_decrypt - AES-128 CBC decryption
|
|
* @key: Decryption key
|
|
* @iv: Decryption IV for CBC mode (16 bytes)
|
|
* @data: Data to decrypt in-place
|
|
* @data_len: Length of data in bytes (must be divisible by 16)
|
|
* Returns: 0 on success, -1 on failure
|
|
*/
|
|
int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
|
|
{
|
|
void *ctx;
|
|
u8 cbc[AES_BLOCK_SIZE], tmp[AES_BLOCK_SIZE];
|
|
u8 *pos = data;
|
|
int i, j, blocks;
|
|
|
|
if (TEST_FAIL())
|
|
return -1;
|
|
|
|
ctx = aes_decrypt_init(key, 16);
|
|
if (ctx == NULL)
|
|
return -1;
|
|
os_memcpy(cbc, iv, AES_BLOCK_SIZE);
|
|
|
|
blocks = data_len / AES_BLOCK_SIZE;
|
|
for (i = 0; i < blocks; i++) {
|
|
os_memcpy(tmp, pos, AES_BLOCK_SIZE);
|
|
aes_decrypt(ctx, pos, pos);
|
|
for (j = 0; j < AES_BLOCK_SIZE; j++)
|
|
pos[j] ^= cbc[j];
|
|
os_memcpy(cbc, tmp, AES_BLOCK_SIZE);
|
|
pos += AES_BLOCK_SIZE;
|
|
}
|
|
aes_decrypt_deinit(ctx);
|
|
return 0;
|
|
}
|