1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  * Copyright (c) 2017 Intel Corporation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <string.h>
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/sys/byteorder.h>
12 #include <zephyr/bluetooth/crypto.h>
13 
14 #include "common/bt_str.h"
15 
16 #define LOG_LEVEL CONFIG_BT_HCI_CORE_LOG_LEVEL
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_REGISTER(bt_aes_ccm);
19 
xor16(uint8_t * dst,const uint8_t * a,const uint8_t * b)20 static inline void xor16(uint8_t *dst, const uint8_t *a, const uint8_t *b)
21 {
22 	dst[0] = a[0] ^ b[0];
23 	dst[1] = a[1] ^ b[1];
24 	dst[2] = a[2] ^ b[2];
25 	dst[3] = a[3] ^ b[3];
26 	dst[4] = a[4] ^ b[4];
27 	dst[5] = a[5] ^ b[5];
28 	dst[6] = a[6] ^ b[6];
29 	dst[7] = a[7] ^ b[7];
30 	dst[8] = a[8] ^ b[8];
31 	dst[9] = a[9] ^ b[9];
32 	dst[10] = a[10] ^ b[10];
33 	dst[11] = a[11] ^ b[11];
34 	dst[12] = a[12] ^ b[12];
35 	dst[13] = a[13] ^ b[13];
36 	dst[14] = a[14] ^ b[14];
37 	dst[15] = a[15] ^ b[15];
38 }
39 
40 /* b field is assumed to have the nonce already present in bytes 1-13 */
ccm_calculate_X0(const uint8_t key[16],const uint8_t * aad,uint8_t aad_len,size_t mic_size,uint16_t msg_len,uint8_t b[16],uint8_t X0[16])41 static int ccm_calculate_X0(const uint8_t key[16], const uint8_t *aad, uint8_t aad_len,
42 			    size_t mic_size, uint16_t msg_len, uint8_t b[16],
43 			    uint8_t X0[16])
44 {
45 	int i, j, err;
46 
47 	/* X_0 = e(AppKey, flags || nonce || length) */
48 	b[0] = (((mic_size - 2) / 2) << 3) | ((!!aad_len) << 6) | 0x01;
49 
50 	sys_put_be16(msg_len, b + 14);
51 
52 	err = bt_encrypt_be(key, b, X0);
53 	if (err) {
54 		return err;
55 	}
56 
57 	/* If AAD is being used to authenticate, include it here */
58 	if (aad_len) {
59 		sys_put_be16(aad_len, b);
60 
61 		for (i = 0; i < sizeof(uint16_t); i++) {
62 			b[i] = X0[i] ^ b[i];
63 		}
64 
65 		j = 0;
66 		aad_len += sizeof(uint16_t);
67 		while (aad_len > 16) {
68 			do {
69 				b[i] = X0[i] ^ aad[j];
70 				i++, j++;
71 			} while (i < 16);
72 
73 			aad_len -= 16;
74 			i = 0;
75 
76 			err = bt_encrypt_be(key, b, X0);
77 			if (err) {
78 				return err;
79 			}
80 		}
81 
82 		for (; i < aad_len; i++, j++) {
83 			b[i] = X0[i] ^ aad[j];
84 		}
85 
86 		for (i = aad_len; i < 16; i++) {
87 			b[i] = X0[i];
88 		}
89 
90 		err = bt_encrypt_be(key, b, X0);
91 		if (err) {
92 			return err;
93 		}
94 	}
95 
96 	return 0;
97 }
98 
ccm_auth(const uint8_t key[16],uint8_t nonce[13],const uint8_t * cleartext_msg,uint16_t msg_len,const uint8_t * aad,size_t aad_len,uint8_t * mic,size_t mic_size)99 static int ccm_auth(const uint8_t key[16], uint8_t nonce[13],
100 		    const uint8_t *cleartext_msg, uint16_t msg_len, const uint8_t *aad,
101 		    size_t aad_len, uint8_t *mic, size_t mic_size)
102 {
103 	uint8_t b[16], Xn[16], s0[16];
104 	uint16_t blk_cnt, last_blk;
105 	int err, j, i;
106 
107 	last_blk = msg_len % 16;
108 	blk_cnt = (msg_len + 15) / 16;
109 	if (!last_blk) {
110 		last_blk = 16U;
111 	}
112 
113 	b[0] = 0x01;
114 	memcpy(b + 1, nonce, 13);
115 
116 	/* S[0] = e(AppKey, 0x01 || nonce || 0x0000) */
117 	sys_put_be16(0x0000, &b[14]);
118 
119 	err = bt_encrypt_be(key, b, s0);
120 	if (err) {
121 		return err;
122 	}
123 
124 	ccm_calculate_X0(key, aad, aad_len, mic_size, msg_len, b, Xn);
125 
126 	for (j = 0; j < blk_cnt; j++) {
127 		/* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */
128 		if (j + 1 == blk_cnt) {
129 			for (i = 0; i < last_blk; i++) {
130 				b[i] = Xn[i] ^ cleartext_msg[(j * 16) + i];
131 			}
132 
133 			memcpy(&b[i], &Xn[i], 16 - i);
134 		} else {
135 			xor16(b, Xn, &cleartext_msg[j * 16]);
136 		}
137 
138 		err = bt_encrypt_be(key, b, Xn);
139 		if (err) {
140 			return err;
141 		}
142 	}
143 
144 	/* MIC = C_mic ^ X_1 */
145 	for (i = 0; i < mic_size; i++) {
146 		mic[i] = s0[i] ^ Xn[i];
147 	}
148 
149 	return 0;
150 }
151 
ccm_crypt(const uint8_t key[16],const uint8_t nonce[13],const uint8_t * in_msg,uint8_t * out_msg,uint16_t msg_len)152 static int ccm_crypt(const uint8_t key[16], const uint8_t nonce[13],
153 		     const uint8_t *in_msg, uint8_t *out_msg, uint16_t msg_len)
154 {
155 	uint8_t a_i[16], s_i[16];
156 	uint16_t last_blk, blk_cnt;
157 	size_t i, j;
158 	int err;
159 
160 	last_blk = msg_len % 16;
161 	blk_cnt = (msg_len + 15) / 16;
162 	if (!last_blk) {
163 		last_blk = 16U;
164 	}
165 
166 	a_i[0] = 0x01;
167 	memcpy(&a_i[1], nonce, 13);
168 
169 	for (j = 0; j < blk_cnt; j++) {
170 		/* S_1 = e(AppKey, 0x01 || nonce || 0x0001) */
171 		sys_put_be16(j + 1, &a_i[14]);
172 
173 		err = bt_encrypt_be(key, a_i, s_i);
174 		if (err) {
175 			return err;
176 		}
177 
178 		/* Encrypted = Payload[0-15] ^ C_1 */
179 		if (j < blk_cnt - 1) {
180 			xor16(&out_msg[j * 16], s_i, &in_msg[j * 16]);
181 		} else {
182 			for (i = 0; i < last_blk; i++) {
183 				out_msg[(j * 16) + i] =
184 					in_msg[(j * 16) + i] ^ s_i[i];
185 			}
186 		}
187 	}
188 	return 0;
189 }
190 
bt_ccm_decrypt(const uint8_t key[16],uint8_t nonce[13],const uint8_t * enc_data,size_t len,const uint8_t * aad,size_t aad_len,uint8_t * plaintext,size_t mic_size)191 int bt_ccm_decrypt(const uint8_t key[16], uint8_t nonce[13],
192 		   const uint8_t *enc_data, size_t len, const uint8_t *aad,
193 		   size_t aad_len, uint8_t *plaintext, size_t mic_size)
194 {
195 	uint8_t mic[16];
196 
197 	if (aad_len >= 0xff00 || mic_size > sizeof(mic) || len > UINT16_MAX) {
198 		return -EINVAL;
199 	}
200 
201 	ccm_crypt(key, nonce, enc_data, plaintext, len);
202 
203 	ccm_auth(key, nonce, plaintext, len, aad, aad_len, mic, mic_size);
204 
205 	if (memcmp(mic, enc_data + len, mic_size)) {
206 		return -EBADMSG;
207 	}
208 
209 	return 0;
210 }
211 
bt_ccm_encrypt(const uint8_t key[16],uint8_t nonce[13],const uint8_t * plaintext,size_t len,const uint8_t * aad,size_t aad_len,uint8_t * enc_data,size_t mic_size)212 int bt_ccm_encrypt(const uint8_t key[16], uint8_t nonce[13],
213 		   const uint8_t *plaintext, size_t len, const uint8_t *aad,
214 		   size_t aad_len, uint8_t *enc_data, size_t mic_size)
215 {
216 	uint8_t *mic = enc_data + len;
217 
218 	LOG_DBG("key %s", bt_hex(key, 16));
219 	LOG_DBG("nonce %s", bt_hex(nonce, 13));
220 	LOG_DBG("msg (len %zu) %s", len, bt_hex(plaintext, len));
221 	LOG_DBG("aad_len %zu mic_size %zu", aad_len, mic_size);
222 
223 	/* Unsupported AAD size */
224 	if (aad_len >= 0xff00 || mic_size > 16 || len > UINT16_MAX) {
225 		return -EINVAL;
226 	}
227 
228 	ccm_auth(key, nonce, plaintext, len, aad, aad_len, mic, mic_size);
229 
230 	ccm_crypt(key, nonce, plaintext, enc_data, len);
231 
232 	return 0;
233 }
234