Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "includes.h"
00017
00018 #include "common.h"
00019 #include "aes.h"
00020 #include "aes_wrap.h"
00021
00030 int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
00031 {
00032 void *ctx;
00033 u8 cbc[AES_BLOCK_SIZE];
00034 u8 *pos = data;
00035 int i, j, blocks;
00036
00037 ctx = aes_encrypt_init(key, 16);
00038 if (ctx == NULL)
00039 return -1;
00040 os_memcpy(cbc, iv, AES_BLOCK_SIZE);
00041
00042 blocks = data_len / AES_BLOCK_SIZE;
00043 for (i = 0; i < blocks; i++) {
00044 for (j = 0; j < AES_BLOCK_SIZE; j++)
00045 cbc[j] ^= pos[j];
00046 aes_encrypt(ctx, cbc, cbc);
00047 os_memcpy(pos, cbc, AES_BLOCK_SIZE);
00048 pos += AES_BLOCK_SIZE;
00049 }
00050 aes_encrypt_deinit(ctx);
00051 return 0;
00052 }
00053
00054
00063 int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
00064 {
00065 void *ctx;
00066 u8 cbc[AES_BLOCK_SIZE], tmp[AES_BLOCK_SIZE];
00067 u8 *pos = data;
00068 int i, j, blocks;
00069
00070 ctx = aes_decrypt_init(key, 16);
00071 if (ctx == NULL)
00072 return -1;
00073 os_memcpy(cbc, iv, AES_BLOCK_SIZE);
00074
00075 blocks = data_len / AES_BLOCK_SIZE;
00076 for (i = 0; i < blocks; i++) {
00077 os_memcpy(tmp, pos, AES_BLOCK_SIZE);
00078 aes_decrypt(ctx, pos, pos);
00079 for (j = 0; j < AES_BLOCK_SIZE; j++)
00080 pos[j] ^= cbc[j];
00081 os_memcpy(cbc, tmp, AES_BLOCK_SIZE);
00082 pos += AES_BLOCK_SIZE;
00083 }
00084 aes_decrypt_deinit(ctx);
00085 return 0;
00086 }