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