xref: /OK3568_Linux_fs/external/rkwifibt/drivers/rtl8821cs/core/crypto/aes-ccm.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Counter with CBC-MAC (CCM) with AES
3  *
4  * Copyright (c) 2010-2012, 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 "rtw_crypto_wrap.h"
11 
12 #include "aes.h"
13 #include "aes_wrap.h"
14 
15 
xor_aes_block(u8 * dst,const u8 * src)16 static void xor_aes_block(u8 *dst, const u8 *src)
17 {
18 	u32 *d = (u32 *) dst;
19 	u32 *s = (u32 *) src;
20 	*d++ ^= *s++;
21 	*d++ ^= *s++;
22 	*d++ ^= *s++;
23 	*d++ ^= *s++;
24 }
25 
26 
aes_ccm_auth_start(void * aes,size_t M,size_t L,const u8 * nonce,const u8 * aad,size_t aad_len,size_t plain_len,u8 * x)27 static void aes_ccm_auth_start(void *aes, size_t M, size_t L, const u8 *nonce,
28 			       const u8 *aad, size_t aad_len, size_t plain_len,
29 			       u8 *x)
30 {
31 	u8 aad_buf[2 * AES_BLOCK_SIZE];
32 	u8 b[AES_BLOCK_SIZE];
33 
34 	/* Authentication */
35 	/* B_0: Flags | Nonce N | l(m) */
36 	b[0] = aad_len ? 0x40 : 0 /* Adata */;
37 	b[0] |= (((M - 2) / 2) /* M' */ << 3);
38 	b[0] |= (L - 1) /* L' */;
39 	os_memcpy(&b[1], nonce, 15 - L);
40 	WPA_PUT_BE16(&b[AES_BLOCK_SIZE - L], plain_len);
41 
42 	wpa_hexdump_key(_MSG_EXCESSIVE_, "CCM B_0", b, AES_BLOCK_SIZE);
43 	aes_encrypt(aes, b, x); /* X_1 = E(K, B_0) */
44 
45 	if (!aad_len)
46 		return;
47 
48 	WPA_PUT_BE16(aad_buf, aad_len);
49 	os_memcpy(aad_buf + 2, aad, aad_len);
50 	os_memset(aad_buf + 2 + aad_len, 0, sizeof(aad_buf) - 2 - aad_len);
51 
52 	xor_aes_block(aad_buf, x);
53 	aes_encrypt(aes, aad_buf, x); /* X_2 = E(K, X_1 XOR B_1) */
54 
55 	if (aad_len > AES_BLOCK_SIZE - 2) {
56 		xor_aes_block(&aad_buf[AES_BLOCK_SIZE], x);
57 		/* X_3 = E(K, X_2 XOR B_2) */
58 		aes_encrypt(aes, &aad_buf[AES_BLOCK_SIZE], x);
59 	}
60 }
61 
62 
aes_ccm_auth(void * aes,const u8 * data,size_t len,u8 * x)63 static void aes_ccm_auth(void *aes, const u8 *data, size_t len, u8 *x)
64 {
65 	size_t last = len % AES_BLOCK_SIZE;
66 	size_t i;
67 
68 	for (i = 0; i < len / AES_BLOCK_SIZE; i++) {
69 		/* X_i+1 = E(K, X_i XOR B_i) */
70 		xor_aes_block(x, data);
71 		data += AES_BLOCK_SIZE;
72 		aes_encrypt(aes, x, x);
73 	}
74 	if (last) {
75 		/* XOR zero-padded last block */
76 		for (i = 0; i < last; i++)
77 			x[i] ^= *data++;
78 		aes_encrypt(aes, x, x);
79 	}
80 }
81 
82 
aes_ccm_encr_start(size_t L,const u8 * nonce,u8 * a)83 static void aes_ccm_encr_start(size_t L, const u8 *nonce, u8 *a)
84 {
85 	/* A_i = Flags | Nonce N | Counter i */
86 	a[0] = L - 1; /* Flags = L' */
87 	os_memcpy(&a[1], nonce, 15 - L);
88 }
89 
90 
aes_ccm_encr(void * aes,size_t L,const u8 * in,size_t len,u8 * out,u8 * a)91 static void aes_ccm_encr(void *aes, size_t L, const u8 *in, size_t len, u8 *out,
92 			 u8 *a)
93 {
94 	size_t last = len % AES_BLOCK_SIZE;
95 	size_t i;
96 
97 	/* crypt = msg XOR (S_1 | S_2 | ... | S_n) */
98 	for (i = 1; i <= len / AES_BLOCK_SIZE; i++) {
99 		WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], i);
100 		/* S_i = E(K, A_i) */
101 		aes_encrypt(aes, a, out);
102 		xor_aes_block(out, in);
103 		out += AES_BLOCK_SIZE;
104 		in += AES_BLOCK_SIZE;
105 	}
106 	if (last) {
107 		WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], i);
108 		aes_encrypt(aes, a, out);
109 		/* XOR zero-padded last block */
110 		for (i = 0; i < last; i++)
111 			*out++ ^= *in++;
112 	}
113 }
114 
115 
aes_ccm_encr_auth(void * aes,size_t M,u8 * x,u8 * a,u8 * auth)116 static void aes_ccm_encr_auth(void *aes, size_t M, u8 *x, u8 *a, u8 *auth)
117 {
118 	size_t i;
119 	u8 tmp[AES_BLOCK_SIZE];
120 
121 	wpa_hexdump_key(_MSG_EXCESSIVE_, "CCM T", x, M);
122 	/* U = T XOR S_0; S_0 = E(K, A_0) */
123 	WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], 0);
124 	aes_encrypt(aes, a, tmp);
125 	for (i = 0; i < M; i++)
126 		auth[i] = x[i] ^ tmp[i];
127 	wpa_hexdump_key(_MSG_EXCESSIVE_, "CCM U", auth, M);
128 }
129 
130 
aes_ccm_decr_auth(void * aes,size_t M,u8 * a,const u8 * auth,u8 * t)131 static void aes_ccm_decr_auth(void *aes, size_t M, u8 *a, const u8 *auth, u8 *t)
132 {
133 	size_t i;
134 	u8 tmp[AES_BLOCK_SIZE];
135 
136 	wpa_hexdump_key(_MSG_EXCESSIVE_, "CCM U", auth, M);
137 	/* U = T XOR S_0; S_0 = E(K, A_0) */
138 	WPA_PUT_BE16(&a[AES_BLOCK_SIZE - 2], 0);
139 	aes_encrypt(aes, a, tmp);
140 	for (i = 0; i < M; i++)
141 		t[i] = auth[i] ^ tmp[i];
142 	wpa_hexdump_key(_MSG_EXCESSIVE_, "CCM T", t, M);
143 }
144 
145 
146 /* AES-CCM with fixed L=2 and aad_len <= 30 assumption */
aes_ccm_ae(const u8 * key,size_t key_len,const u8 * nonce,size_t M,const u8 * plain,size_t plain_len,const u8 * aad,size_t aad_len,u8 * crypt,u8 * auth)147 int aes_ccm_ae(const u8 *key, size_t key_len, const u8 *nonce,
148 	       size_t M, const u8 *plain, size_t plain_len,
149 	       const u8 *aad, size_t aad_len, u8 *crypt, u8 *auth)
150 {
151 	const size_t L = 2;
152 	void *aes;
153 	u8 x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
154 
155 	if (aad_len > 30 || M > AES_BLOCK_SIZE)
156 		return -1;
157 
158 	aes = aes_encrypt_init(key, key_len);
159 	if (aes == NULL)
160 		return -1;
161 
162 	aes_ccm_auth_start(aes, M, L, nonce, aad, aad_len, plain_len, x);
163 	aes_ccm_auth(aes, plain, plain_len, x);
164 
165 	/* Encryption */
166 	aes_ccm_encr_start(L, nonce, a);
167 	aes_ccm_encr(aes, L, plain, plain_len, crypt, a);
168 	aes_ccm_encr_auth(aes, M, x, a, auth);
169 
170 	aes_encrypt_deinit(aes);
171 
172 	return 0;
173 }
174 
175 
176 /* AES-CCM with fixed L=2 and aad_len <= 30 assumption */
aes_ccm_ad(const u8 * key,size_t key_len,const u8 * nonce,size_t M,const u8 * crypt,size_t crypt_len,const u8 * aad,size_t aad_len,const u8 * auth,u8 * plain)177 int aes_ccm_ad(const u8 *key, size_t key_len, const u8 *nonce,
178 	       size_t M, const u8 *crypt, size_t crypt_len,
179 	       const u8 *aad, size_t aad_len, const u8 *auth, u8 *plain)
180 {
181 	const size_t L = 2;
182 	void *aes;
183 	u8 x[AES_BLOCK_SIZE], a[AES_BLOCK_SIZE];
184 	u8 t[AES_BLOCK_SIZE];
185 
186 	if (aad_len > 30 || M > AES_BLOCK_SIZE)
187 		return -1;
188 
189 	aes = aes_encrypt_init(key, key_len);
190 	if (aes == NULL)
191 		return -1;
192 
193 	/* Decryption */
194 	aes_ccm_encr_start(L, nonce, a);
195 	aes_ccm_decr_auth(aes, M, a, auth, t);
196 
197 	/* plaintext = msg XOR (S_1 | S_2 | ... | S_n) */
198 	aes_ccm_encr(aes, L, crypt, crypt_len, plain, a);
199 
200 	aes_ccm_auth_start(aes, M, L, nonce, aad, aad_len, crypt_len, x);
201 	aes_ccm_auth(aes, plain, crypt_len, x);
202 
203 	aes_encrypt_deinit(aes);
204 
205 	if (os_memcmp_const(x, t, M) != 0) {
206 		wpa_printf(_MSG_EXCESSIVE_, "CCM: Auth mismatch");
207 		return -1;
208 	}
209 
210 	return 0;
211 }
212