3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2013-2014 Intel Corporation
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "monitor/bt.h"
31 #include "src/shared/util.h"
32 #include "src/shared/hci.h"
33 #include "src/shared/hci-crypto.h"
37 bt_hci_crypto_func_t callback;
41 static void le_encrypt_callback(const void *response, uint8_t size,
44 struct crypto_data *data = user_data;
45 const struct bt_hci_rsp_le_encrypt *rsp = response;
48 data->callback(NULL, 0, data->user_data);
52 data->callback(rsp->data, data->size, data->user_data);
55 static bool le_encrypt(struct bt_hci *hci, uint8_t size,
56 const uint8_t key[16], const uint8_t plaintext[16],
57 bt_hci_crypto_func_t callback, void *user_data)
59 struct crypto_data *data;
60 struct bt_hci_cmd_le_encrypt cmd;
62 if (!callback || !size || size > 16)
65 memcpy(cmd.key, key, 16);
66 memcpy(cmd.plaintext, plaintext, 16);
68 data = new0(struct crypto_data, 1);
70 data->callback = callback;
71 data->user_data = user_data;
73 if (!bt_hci_send(hci, BT_HCI_CMD_LE_ENCRYPT, &cmd, sizeof(cmd),
74 le_encrypt_callback, data, free)) {
82 static void prand_callback(const void *response, uint8_t size,
85 struct crypto_data *data = user_data;
86 const struct bt_hci_rsp_le_rand *rsp = response;
90 data->callback(NULL, 0, data->user_data);
94 prand[0] = (rsp->number & 0xff0000) >> 16;
95 prand[1] = (rsp->number & 0x00ff00) >> 8;
96 prand[2] = (rsp->number & 0x00003f) | 0x40;
98 data->callback(prand, 3, data->user_data);
101 bool bt_hci_crypto_prand(struct bt_hci *hci,
102 bt_hci_crypto_func_t callback, void *user_data)
104 struct crypto_data *data;
109 data = new0(struct crypto_data, 1);
110 data->callback = callback;
111 data->user_data = user_data;
113 if (!bt_hci_send(hci, BT_HCI_CMD_LE_RAND, NULL, 0,
114 prand_callback, data, free)) {
122 bool bt_hci_crypto_e(struct bt_hci *hci,
123 const uint8_t key[16], const uint8_t plaintext[16],
124 bt_hci_crypto_func_t callback, void *user_data)
126 return le_encrypt(hci, 16, key, plaintext, callback, user_data);
129 bool bt_hci_crypto_d1(struct bt_hci *hci,
130 const uint8_t k[16], uint16_t d, uint16_t r,
131 bt_hci_crypto_func_t callback, void *user_data)
135 /* d' = padding || r || d */
140 memset(dp + 4, 0, 12);
142 /* d1(k, d, r) = e(k, d') */
143 return le_encrypt(hci, 16, k, dp, callback, user_data);
146 bool bt_hci_crypto_dm(struct bt_hci *hci,
147 const uint8_t k[16], const uint8_t r[8],
148 bt_hci_crypto_func_t callback, void *user_data)
152 /* r' = padding || r */
154 memset(rp + 8, 0, 8);
156 /* dm(k, r) = e(k, r') mod 2^16 */
157 return le_encrypt(hci, 8, k, rp, callback, user_data);
160 bool bt_hci_crypto_ah(struct bt_hci *hci,
161 const uint8_t k[16], const uint8_t r[3],
162 bt_hci_crypto_func_t callback, void *user_data)
166 /* r' = padding || r */
168 memset(rp + 3, 0, 13);
170 /* ah(k, r) = e(k, r') mod 2^24 */
171 return le_encrypt(hci, 3, k, rp, callback, user_data);