aes-ctr.c
Go to the documentation of this file.00001
00017 #include "includes.h"
00018
00019 #include "common.h"
00020 #include "aes.h"
00021
00031 int aes_128_ctr_encrypt(const u8 *key, const u8 *nonce,
00032 u8 *data, size_t data_len)
00033 {
00034 void *ctx;
00035 size_t j, len, left = data_len;
00036 int i;
00037 u8 *pos = data;
00038 u8 counter[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
00039
00040 ctx = aes_encrypt_init(key, 16);
00041 if (ctx == NULL)
00042 return -1;
00043 os_memcpy(counter, nonce, AES_BLOCK_SIZE);
00044
00045 while (left > 0) {
00046 aes_encrypt(ctx, counter, buf);
00047
00048 len = (left < AES_BLOCK_SIZE) ? left : AES_BLOCK_SIZE;
00049 for (j = 0; j < len; j++)
00050 pos[j] ^= buf[j];
00051 pos += len;
00052 left -= len;
00053
00054 for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
00055 counter[i]++;
00056 if (counter[i])
00057 break;
00058 }
00059 }
00060 aes_encrypt_deinit(ctx);
00061 return 0;
00062 }
00063