1 /******************************************************************************
2 *
3 * Copyright 2008-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This file contains the implementation of the AES128 and AES CMAC algorithm.
22 *
23 ******************************************************************************/
24
25 #include "crypto_toolbox/aes.h"
26 #include "crypto_toolbox/crypto_toolbox.h"
27
28 namespace bluetooth {
29 namespace crypto_toolbox {
30
31 namespace {
32
33 typedef struct {
34 uint8_t* text;
35 uint16_t len;
36 uint16_t round;
37 } tCMAC_CB;
38
39 thread_local tCMAC_CB cmac_cb;
40
41 /* Rb for AES-128 as block cipher, LSB as [0] */
42 Octet16 const_Rb{0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
43
44 /** utility function to do an biteise exclusive-OR of two bit strings of the
45 * length of OCTET16_LEN. Result is stored in first argument.
46 */
xor_128(Octet16 * a,const Octet16 & b)47 static void xor_128(Octet16* a, const Octet16& b) {
48 // CHECK(a);
49 uint8_t i, *aa = a->data();
50 const uint8_t* bb = b.data();
51
52 for (i = 0; i < OCTET16_LEN; i++) {
53 aa[i] = aa[i] ^ bb[i];
54 }
55 }
56 } // namespace
57
58 /* This function computes AES_128(key, message) */
aes_128(const Octet16 & key,const Octet16 & message)59 Octet16 aes_128(const Octet16& key, const Octet16& message) {
60 Octet16 key_reversed;
61 Octet16 message_reversed;
62 Octet16 output;
63
64 std::reverse_copy(key.begin(), key.end(), key_reversed.begin());
65 std::reverse_copy(message.begin(), message.end(), message_reversed.begin());
66
67 aes_context ctx;
68 aes_set_key(key_reversed.data(), key_reversed.size(), &ctx);
69 aes_encrypt(message_reversed.data(), output.data(), &ctx);
70
71 std::reverse(output.begin(), output.end());
72 return output;
73 }
74
75 /** utility function to padding the given text to be a 128 bits data. The
76 * parameter dest is input and output parameter, it must point to a
77 * OCTET16_LEN memory space; where include length bytes valid data. */
padding(Octet16 * dest,uint8_t length)78 static void padding(Octet16* dest, uint8_t length) {
79 uint8_t i, *p = dest->data();
80 /* original last block */
81 for (i = length; i < OCTET16_LEN; i++) p[OCTET16_LEN - i - 1] = (i == length) ? 0x80 : 0;
82 }
83
84 /** utility function to left shift one bit for a 128 bits value. */
leftshift_onebit(uint8_t * input,uint8_t * output)85 static void leftshift_onebit(uint8_t* input, uint8_t* output) {
86 uint8_t i, overflow = 0, next_overflow = 0;
87 /* input[0] is LSB */
88 for (i = 0; i < OCTET16_LEN; i++) {
89 next_overflow = (input[i] & 0x80) ? 1 : 0;
90 output[i] = (input[i] << 1) | overflow;
91 overflow = next_overflow;
92 }
93 return;
94 }
95
96 /** This function is the calculation of block cipher using AES-128. */
cmac_aes_k_calculate(const Octet16 & key)97 static Octet16 cmac_aes_k_calculate(const Octet16& key) {
98 Octet16 output;
99 Octet16 x{0}; // zero initialized
100
101 uint8_t i = 1;
102 while (i <= cmac_cb.round) {
103 /* Mi' := Mi (+) X */
104 xor_128((Octet16*)&cmac_cb.text[(cmac_cb.round - i) * OCTET16_LEN], x);
105
106 output = aes_128(key, &cmac_cb.text[(cmac_cb.round - i) * OCTET16_LEN], OCTET16_LEN);
107 x = output;
108 i++;
109 }
110
111 return output;
112 }
113
114 /** This function proceeed to prepare the last block of message Mn depending on
115 * the size of the message.
116 */
cmac_prepare_last_block(const Octet16 & k1,const Octet16 & k2)117 static void cmac_prepare_last_block(const Octet16& k1, const Octet16& k2) {
118 // uint8_t x[16] = {0};
119 bool flag;
120
121 /* last block is a complete block set flag to 1 */
122 flag = ((cmac_cb.len % OCTET16_LEN) == 0 && cmac_cb.len != 0) ? true : false;
123
124 if (flag) { /* last block is complete block */
125 xor_128((Octet16*)&cmac_cb.text[0], k1);
126 } else /* padding then xor with k2 */
127 {
128 padding((Octet16*)&cmac_cb.text[0], (uint8_t)(cmac_cb.len % 16));
129
130 xor_128((Octet16*)&cmac_cb.text[0], k2);
131 }
132 }
133
134 /** This is the function to generate the two subkeys.
135 * |key| is CMAC key, expect SRK when used by SMP.
136 */
cmac_generate_subkey(const Octet16 & key)137 static void cmac_generate_subkey(const Octet16& key) {
138 Octet16 zero{};
139 Octet16 p = aes_128(key, zero.data(), OCTET16_LEN);
140
141 Octet16 k1, k2;
142 uint8_t* pp = p.data();
143
144 /* If MSB(L) = 0, then K1 = L << 1 */
145 if ((pp[OCTET16_LEN - 1] & 0x80) != 0) {
146 /* Else K1 = ( L << 1 ) (+) Rb */
147 leftshift_onebit(pp, k1.data());
148 xor_128(&k1, const_Rb);
149 } else {
150 leftshift_onebit(pp, k1.data());
151 }
152
153 if ((k1[OCTET16_LEN - 1] & 0x80) != 0) {
154 /* K2 = (K1 << 1) (+) Rb */
155 leftshift_onebit(k1.data(), k2.data());
156 xor_128(&k2, const_Rb);
157 } else {
158 /* If MSB(K1) = 0, then K2 = K1 << 1 */
159 leftshift_onebit(k1.data(), k2.data());
160 }
161
162 cmac_prepare_last_block(k1, k2);
163 }
164
165 /** key - CMAC key in little endian order
166 * input - text to be signed in little endian byte order.
167 * length - length of the input in byte.
168 */
aes_cmac(const Octet16 & key,const uint8_t * input,uint16_t length)169 Octet16 aes_cmac(const Octet16& key, const uint8_t* input, uint16_t length) {
170 uint32_t len;
171 uint16_t diff;
172 /* n is number of rounds */
173 uint16_t n = (length + OCTET16_LEN - 1) / OCTET16_LEN;
174
175 if (n == 0) n = 1;
176 len = n * OCTET16_LEN;
177
178 /* allocate a memory space of multiple of 16 bytes to hold text */
179 cmac_cb.text = (uint8_t*)alloca(len);
180 cmac_cb.round = n;
181 diff = len - length;
182
183 if (input != NULL && length > 0) {
184 memcpy(&cmac_cb.text[diff], input, (int)length);
185 cmac_cb.len = length;
186 } else {
187 cmac_cb.len = 0;
188 }
189
190 /* prepare calculation for subkey s and last block of data */
191 cmac_generate_subkey(key);
192 /* start calculation */
193 Octet16 signature = cmac_aes_k_calculate(key);
194
195 /* clean up */
196 memset(&cmac_cb, 0, sizeof(tCMAC_CB));
197 // cmac_cb.text is auto-freed by alloca
198
199 return signature;
200 }
201
202 } // namespace crypto_toolbox
203 } // namespace bluetooth
204