Coverage Report

Created: 2025-07-18 06:03

/src/hostap/src/crypto/aes-cbc.c
Line
Count
Source (jump to first uncovered line)
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
int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
25
0
{
26
0
  void *ctx;
27
0
  u8 cbc[AES_BLOCK_SIZE];
28
0
  u8 *pos = data;
29
0
  int i, j, blocks;
30
31
0
  if (TEST_FAIL())
32
0
    return -1;
33
34
0
  ctx = aes_encrypt_init(key, 16);
35
0
  if (ctx == NULL)
36
0
    return -1;
37
0
  os_memcpy(cbc, iv, AES_BLOCK_SIZE);
38
39
0
  blocks = data_len / AES_BLOCK_SIZE;
40
0
  for (i = 0; i < blocks; i++) {
41
0
    for (j = 0; j < AES_BLOCK_SIZE; j++)
42
0
      cbc[j] ^= pos[j];
43
0
    aes_encrypt(ctx, cbc, cbc);
44
0
    os_memcpy(pos, cbc, AES_BLOCK_SIZE);
45
0
    pos += AES_BLOCK_SIZE;
46
0
  }
47
0
  aes_encrypt_deinit(ctx);
48
0
  return 0;
49
0
}
50
51
52
/**
53
 * aes_128_cbc_decrypt - AES-128 CBC decryption
54
 * @key: Decryption key
55
 * @iv: Decryption IV for CBC mode (16 bytes)
56
 * @data: Data to decrypt in-place
57
 * @data_len: Length of data in bytes (must be divisible by 16)
58
 * Returns: 0 on success, -1 on failure
59
 */
60
int aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
61
0
{
62
0
  void *ctx;
63
0
  u8 cbc[AES_BLOCK_SIZE], tmp[AES_BLOCK_SIZE];
64
0
  u8 *pos = data;
65
0
  int i, j, blocks;
66
67
0
  if (TEST_FAIL())
68
0
    return -1;
69
70
0
  ctx = aes_decrypt_init(key, 16);
71
0
  if (ctx == NULL)
72
0
    return -1;
73
0
  os_memcpy(cbc, iv, AES_BLOCK_SIZE);
74
75
0
  blocks = data_len / AES_BLOCK_SIZE;
76
0
  for (i = 0; i < blocks; i++) {
77
0
    os_memcpy(tmp, pos, AES_BLOCK_SIZE);
78
0
    aes_decrypt(ctx, pos, pos);
79
0
    for (j = 0; j < AES_BLOCK_SIZE; j++)
80
0
      pos[j] ^= cbc[j];
81
0
    os_memcpy(cbc, tmp, AES_BLOCK_SIZE);
82
0
    pos += AES_BLOCK_SIZE;
83
0
  }
84
0
  aes_decrypt_deinit(ctx);
85
0
  return 0;
86
0
}