LCOV - code coverage report
Current view: top level - src/crypto - aes-ctr.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 19 20 95.0 %
Date: 2014-03-02 Functions: 1 1 100.0 %
Branches: 7 10 70.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * AES-128 CTR
       3                 :            :  *
       4                 :            :  * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
       5                 :            :  *
       6                 :            :  * This software may be distributed under the terms of the BSD license.
       7                 :            :  * See README for more details.
       8                 :            :  */
       9                 :            : 
      10                 :            : #include "includes.h"
      11                 :            : 
      12                 :            : #include "common.h"
      13                 :            : #include "aes.h"
      14                 :            : #include "aes_wrap.h"
      15                 :            : 
      16                 :            : /**
      17                 :            :  * aes_128_ctr_encrypt - AES-128 CTR mode encryption
      18                 :            :  * @key: Key for encryption (16 bytes)
      19                 :            :  * @nonce: Nonce for counter mode (16 bytes)
      20                 :            :  * @data: Data to encrypt in-place
      21                 :            :  * @data_len: Length of data in bytes
      22                 :            :  * Returns: 0 on success, -1 on failure
      23                 :            :  */
      24                 :         60 : int aes_128_ctr_encrypt(const u8 *key, const u8 *nonce,
      25                 :            :                         u8 *data, size_t data_len)
      26                 :            : {
      27                 :            :         void *ctx;
      28                 :         60 :         size_t j, len, left = data_len;
      29                 :            :         int i;
      30                 :         60 :         u8 *pos = data;
      31                 :            :         u8 counter[AES_BLOCK_SIZE], buf[AES_BLOCK_SIZE];
      32                 :            : 
      33                 :         60 :         ctx = aes_encrypt_init(key, 16);
      34         [ -  + ]:         60 :         if (ctx == NULL)
      35                 :          0 :                 return -1;
      36                 :         60 :         os_memcpy(counter, nonce, AES_BLOCK_SIZE);
      37                 :            : 
      38         [ +  + ]:        120 :         while (left > 0) {
      39                 :         60 :                 aes_encrypt(ctx, counter, buf);
      40                 :            : 
      41                 :         60 :                 len = (left < AES_BLOCK_SIZE) ? left : AES_BLOCK_SIZE;
      42         [ +  + ]:        120 :                 for (j = 0; j < len; j++)
      43                 :         60 :                         pos[j] ^= buf[j];
      44                 :         60 :                 pos += len;
      45                 :         60 :                 left -= len;
      46                 :            : 
      47         [ +  - ]:         60 :                 for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
      48                 :         60 :                         counter[i]++;
      49         [ +  - ]:         60 :                         if (counter[i])
      50                 :         60 :                                 break;
      51                 :            :                 }
      52                 :            :         }
      53                 :         60 :         aes_encrypt_deinit(ctx);
      54                 :         60 :         return 0;
      55                 :            : }

Generated by: LCOV version 1.9