LCOV - code coverage report
Current view: top level - src/crypto - aes-cbc.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 29 31 93.5 %
Date: 2014-03-02 Functions: 2 2 100.0 %
Branches: 10 12 83.3 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * AES-128 CBC
       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_cbc_encrypt - AES-128 CBC encryption
      18                 :            :  * @key: Encryption key
      19                 :            :  * @iv: Encryption IV for CBC mode (16 bytes)
      20                 :            :  * @data: Data to encrypt in-place
      21                 :            :  * @data_len: Length of data in bytes (must be divisible by 16)
      22                 :            :  * Returns: 0 on success, -1 on failure
      23                 :            :  */
      24                 :        641 : int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
      25                 :            : {
      26                 :            :         void *ctx;
      27                 :            :         u8 cbc[AES_BLOCK_SIZE];
      28                 :        641 :         u8 *pos = data;
      29                 :            :         int i, j, blocks;
      30                 :            : 
      31                 :        641 :         ctx = aes_encrypt_init(key, 16);
      32         [ -  + ]:        641 :         if (ctx == NULL)
      33                 :          0 :                 return -1;
      34                 :        641 :         os_memcpy(cbc, iv, AES_BLOCK_SIZE);
      35                 :            : 
      36                 :        641 :         blocks = data_len / AES_BLOCK_SIZE;
      37         [ +  + ]:       3475 :         for (i = 0; i < blocks; i++) {
      38         [ +  + ]:      48178 :                 for (j = 0; j < AES_BLOCK_SIZE; j++)
      39                 :      45344 :                         cbc[j] ^= pos[j];
      40                 :       2834 :                 aes_encrypt(ctx, cbc, cbc);
      41                 :       2834 :                 os_memcpy(pos, cbc, AES_BLOCK_SIZE);
      42                 :       2834 :                 pos += AES_BLOCK_SIZE;
      43                 :            :         }
      44                 :        641 :         aes_encrypt_deinit(ctx);
      45                 :        641 :         return 0;
      46                 :            : }
      47                 :            : 
      48                 :            : 
      49                 :            : /**
      50                 :            :  * aes_128_cbc_decrypt - AES-128 CBC decryption
      51                 :            :  * @key: Decryption key
      52                 :            :  * @iv: Decryption IV for CBC mode (16 bytes)
      53                 :            :  * @data: Data to decrypt in-place
      54                 :            :  * @data_len: Length of data in bytes (must be divisible by 16)
      55                 :            :  * Returns: 0 on success, -1 on failure
      56                 :            :  */
      57                 :        634 : int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
      58                 :            : {
      59                 :            :         void *ctx;
      60                 :            :         u8 cbc[AES_BLOCK_SIZE], tmp[AES_BLOCK_SIZE];
      61                 :        634 :         u8 *pos = data;
      62                 :            :         int i, j, blocks;
      63                 :            : 
      64                 :        634 :         ctx = aes_decrypt_init(key, 16);
      65         [ -  + ]:        634 :         if (ctx == NULL)
      66                 :          0 :                 return -1;
      67                 :        634 :         os_memcpy(cbc, iv, AES_BLOCK_SIZE);
      68                 :            : 
      69                 :        634 :         blocks = data_len / AES_BLOCK_SIZE;
      70         [ +  + ]:       3439 :         for (i = 0; i < blocks; i++) {
      71                 :       2805 :                 os_memcpy(tmp, pos, AES_BLOCK_SIZE);
      72                 :       2805 :                 aes_decrypt(ctx, pos, pos);
      73         [ +  + ]:      47685 :                 for (j = 0; j < AES_BLOCK_SIZE; j++)
      74                 :      44880 :                         pos[j] ^= cbc[j];
      75                 :       2805 :                 os_memcpy(cbc, tmp, AES_BLOCK_SIZE);
      76                 :       2805 :                 pos += AES_BLOCK_SIZE;
      77                 :            :         }
      78                 :        634 :         aes_decrypt_deinit(ctx);
      79                 :        634 :         return 0;
      80                 :            : }

Generated by: LCOV version 1.9