$search
00001 /* 00002 * AES-128 CTR 00003 * 00004 * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License version 2 as 00008 * published by the Free Software Foundation. 00009 * 00010 * Alternatively, this software may be distributed under the terms of BSD 00011 * license. 00012 * 00013 * See README and COPYING for more details. 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_ctr_encrypt(const u8 *key, const u8 *nonce, 00031 u8 *data, size_t data_len) 00032 { 00033 void *ctx; 00034 size_t j, len, left = data_len; 00035 int i; 00036 u8 *pos = data; 00037 u8 counter[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE]; 00038 00039 ctx = aes_encrypt_init(key, 16); 00040 if (ctx == NULL) 00041 return -1; 00042 os_memcpy(counter, nonce, AES_BLOCK_SIZE); 00043 00044 while (left > 0) { 00045 aes_encrypt(ctx, counter, buf); 00046 00047 len = (left < AES_BLOCK_SIZE) ? left : AES_BLOCK_SIZE; 00048 for (j = 0; j < len; j++) 00049 pos[j] ^= buf[j]; 00050 pos += len; 00051 left -= len; 00052 00053 for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) { 00054 counter[i]++; 00055 if (counter[i]) 00056 break; 00057 } 00058 } 00059 aes_encrypt_deinit(ctx); 00060 return 0; 00061 }