ble_gatt_client
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
crypto.c
Go to the documentation of this file.
1 
10 /*
11  *
12  * BlueZ - Bluetooth protocol stack for Linux
13  *
14  * Copyright (C) 2012-2014 Intel Corporation. All rights reserved.
15  *
16  *
17  * This library is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU Lesser General Public
19  * License as published by the Free Software Foundation; either
20  * version 2.1 of the License, or (at your option) any later version.
21  *
22  * This library is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25  * Lesser General Public License for more details.
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30  *
31  */
32 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <sys/socket.h>
41 
42 #include "util.h"
43 #include "crypto.h"
44 
45 #ifndef HAVE_LINUX_IF_ALG_H
46 #ifndef HAVE_LINUX_TYPES_H
47 typedef uint8_t __u8;
48 typedef uint16_t __u16;
49 typedef uint32_t __u32;
50 #else
51 #include <linux/types.h>
52 #endif
53 
54 struct sockaddr_alg {
60 };
61 
62 struct af_alg_iv {
64  __u8 iv[0];
65 };
66 
67 #define ALG_SET_KEY 1
68 #define ALG_SET_IV 2
69 #define ALG_SET_OP 3
70 
71 #define ALG_OP_DECRYPT 0
72 #define ALG_OP_ENCRYPT 1
73 
74 #define PF_ALG 38 /* Algorithm sockets. */
75 #define AF_ALG PF_ALG
76 #else
77 #include <linux/if_alg.h>
78 #endif
79 
80 #ifndef SOL_ALG
81 #define SOL_ALG 279
82 #endif
83 
84 /* Maximum message length that can be passed to aes_cmac */
85 #define CMAC_MSG_MAX 80
86 
87 struct bt_crypto {
88  int ref_count;
89  int ecb_aes;
90  int urandom;
91  int cmac_aes;
92 };
93 
99 static int urandom_setup(void)
100 {
101  int fd;
102 
103  fd = open("/dev/urandom", O_RDONLY);
104  if (fd < 0)
105  return -1;
106 
107  return fd;
108 }
109 
114 static int ecb_aes_setup(void)
115 {
116  struct sockaddr_alg salg;
117  int fd;
118 
119  fd = socket(PF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
120  if (fd < 0)
121  return -1;
122 
123  memset(&salg, 0, sizeof(salg));
124  salg.salg_family = AF_ALG;
125  strcpy((char *) salg.salg_type, "skcipher");
126  strcpy((char *) salg.salg_name, "ecb(aes)");
127 
128  if (bind(fd, (struct sockaddr *) &salg, sizeof(salg)) < 0) {
129  close(fd);
130  return -1;
131  }
132 
133  return fd;
134 }
135 
136 static int cmac_aes_setup(void)
137 {
138  struct sockaddr_alg salg;
139  int fd;
140 
141  fd = socket(PF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
142  if (fd < 0)
143  return -1;
144 
145  memset(&salg, 0, sizeof(salg));
146  salg.salg_family = AF_ALG;
147  strcpy((char *) salg.salg_type, "hash");
148  strcpy((char *) salg.salg_name, "cmac(aes)");
149 
150  if (bind(fd, (struct sockaddr *) &salg, sizeof(salg)) < 0) {
151  close(fd);
152  return -1;
153  }
154 
155  return fd;
156 }
157 
159 {
160  struct bt_crypto *crypto;
161 
162  crypto = new0(struct bt_crypto, 1);
163  if (!crypto)
164  return NULL;
165 
166  crypto->ecb_aes = ecb_aes_setup();
167  if (crypto->ecb_aes < 0) {
168  free(crypto);
169  return NULL;
170  }
171 
172  crypto->urandom = urandom_setup();
173  if (crypto->urandom < 0) {
174  close(crypto->ecb_aes);
175  free(crypto);
176  return NULL;
177  }
178 
179  crypto->cmac_aes = cmac_aes_setup();
180  if (crypto->cmac_aes < 0) {
181  close(crypto->urandom);
182  close(crypto->ecb_aes);
183  free(crypto);
184  return NULL;
185  }
186 
187  return bt_crypto_ref(crypto);
188 }
189 
190 struct bt_crypto *bt_crypto_ref(struct bt_crypto *crypto)
191 {
192  if (!crypto)
193  return NULL;
194 
195  __sync_fetch_and_add(&crypto->ref_count, 1);
196 
197  return crypto;
198 }
199 
200 void bt_crypto_unref(struct bt_crypto *crypto)
201 {
202  if (!crypto)
203  return;
204 
205  if (__sync_sub_and_fetch(&crypto->ref_count, 1))
206  return;
207 
208  close(crypto->urandom);
209  close(crypto->ecb_aes);
210  close(crypto->cmac_aes);
211 
212  free(crypto);
213 }
214 
215 bool bt_crypto_random_bytes(struct bt_crypto *crypto,
216  uint8_t *buf, uint8_t num_bytes)
217 {
218  ssize_t len;
219 
220  if (!crypto)
221  return false;
222 
223  len = read(crypto->urandom, buf, num_bytes);
224  if (len < num_bytes)
225  return false;
226 
227  return true;
228 }
229 
230 static int alg_new(int fd, const void *keyval, socklen_t keylen)
231 {
232  if (setsockopt(fd, SOL_ALG, ALG_SET_KEY, keyval, keylen) < 0)
233  return -1;
234 
235  /* FIXME: This should use accept4() with SOCK_CLOEXEC */
236  return accept(fd, NULL, 0);
237 }
238 
239 static bool alg_encrypt(int fd, const void *inbuf, size_t inlen,
240  void *outbuf, size_t outlen)
241 {
242  __u32 alg_op = ALG_OP_ENCRYPT;
243  char cbuf[CMSG_SPACE(sizeof(alg_op))];
244  struct cmsghdr *cmsg;
245  struct msghdr msg;
246  struct iovec iov;
247  ssize_t len;
248 
249  memset(cbuf, 0, sizeof(cbuf));
250  memset(&msg, 0, sizeof(msg));
251 
252  msg.msg_control = cbuf;
253  msg.msg_controllen = sizeof(cbuf);
254 
255  cmsg = CMSG_FIRSTHDR(&msg);
256  cmsg->cmsg_level = SOL_ALG;
257  cmsg->cmsg_type = ALG_SET_OP;
258  cmsg->cmsg_len = CMSG_LEN(sizeof(alg_op));
259  memcpy(CMSG_DATA(cmsg), &alg_op, sizeof(alg_op));
260 
261  iov.iov_base = (void *) inbuf;
262  iov.iov_len = inlen;
263 
264  msg.msg_iov = &iov;
265  msg.msg_iovlen = 1;
266 
267  len = sendmsg(fd, &msg, 0);
268  if (len < 0)
269  return false;
270 
271  len = read(fd, outbuf, outlen);
272  if (len < 0)
273  return false;
274 
275  return true;
276 }
277 
278 static inline void swap_buf(const uint8_t *src, uint8_t *dst, uint16_t len)
279 {
280  int i;
281 
282  for (i = 0; i < len; i++)
283  dst[len - 1 - i] = src[i];
284 }
285 
286 bool bt_crypto_sign_att(struct bt_crypto *crypto, const uint8_t key[16],
287  const uint8_t *m, uint16_t m_len,
288  uint32_t sign_cnt, uint8_t signature[12])
289 {
290  int fd;
291  int len;
292  uint8_t tmp[16], out[16];
293  uint16_t msg_len = m_len + sizeof(uint32_t);
294  uint8_t msg[msg_len];
295  uint8_t msg_s[msg_len];
296 
297  if (!crypto)
298  return false;
299 
300  memset(msg, 0, msg_len);
301  memcpy(msg, m, m_len);
302 
303  /* Add sign_counter to the message */
304  put_le32(sign_cnt, msg + m_len);
305 
306  /* The most significant octet of key corresponds to key[0] */
307  swap_buf(key, tmp, 16);
308 
309  fd = alg_new(crypto->cmac_aes, tmp, 16);
310  if (fd < 0)
311  return false;
312 
313  /* Swap msg before signing */
314  swap_buf(msg, msg_s, msg_len);
315 
316  len = send(fd, msg_s, msg_len, 0);
317  if (len < 0) {
318  close(fd);
319  return false;
320  }
321 
322  len = read(fd, out, 16);
323  if (len < 0) {
324  close(fd);
325  return false;
326  }
327 
328  close(fd);
329 
330  /*
331  * As to BT spec. 4.1 Vol[3], Part C, chapter 10.4.1 sign counter should
332  * be placed in the signature
333  */
334  put_be32(sign_cnt, out + 8);
335 
336  /*
337  * The most significant octet of hash corresponds to out[0] - swap it.
338  * Then truncate in most significant bit first order to a length of
339  * 12 octets
340  */
341  swap_buf(out, tmp, 16);
342  memcpy(signature, tmp + 4, 12);
343 
344  return true;
345 }
359 bool bt_crypto_e(struct bt_crypto *crypto, const uint8_t key[16],
360  const uint8_t plaintext[16], uint8_t encrypted[16])
361 {
362  uint8_t tmp[16], in[16], out[16];
363  int fd;
364 
365  if (!crypto)
366  return false;
367 
368  /* The most significant octet of key corresponds to key[0] */
369  swap_buf(key, tmp, 16);
370 
371  fd = alg_new(crypto->ecb_aes, tmp, 16);
372  if (fd < 0)
373  return false;
374 
375 
376  /* Most significant octet of plaintextData corresponds to in[0] */
377  swap_buf(plaintext, in, 16);
378 
379  if (!alg_encrypt(fd, in, 16, out, 16)) {
380  close(fd);
381  return false;
382  }
383 
384  /* Most significant octet of encryptedData corresponds to out[0] */
385  swap_buf(out, encrypted, 16);
386 
387  close(fd);
388 
389  return true;
390 }
391 
424 bool bt_crypto_ah(struct bt_crypto *crypto, const uint8_t k[16],
425  const uint8_t r[3], uint8_t hash[3])
426 {
427  uint8_t rp[16];
428  uint8_t encrypted[16];
429 
430  if (!crypto)
431  return false;
432 
433  /* r' = padding || r */
434  memcpy(rp, r, 3);
435  memset(rp + 3, 0, 13);
436 
437  /* e(k, r') */
438  if (!bt_crypto_e(crypto, k, rp, encrypted))
439  return false;
440 
441  /* ah(k, r) = e(k, r') mod 2^24 */
442  memcpy(hash, encrypted, 3);
443 
444  return true;
445 }
446 
447 typedef struct {
448  uint64_t a, b;
449 } u128;
450 
451 static inline void u128_xor(const uint8_t p[16], const uint8_t q[16],
452  uint8_t r[16])
453 {
454  u128 pp, qq, rr;
455 
456  memcpy(&pp, p, 16);
457  memcpy(&qq, q, 16);
458 
459  rr.a = pp.a ^ qq.a;
460  rr.b = pp.b ^ qq.b;
461 
462  memcpy(r, &rr, 16);
463 }
464 
517 bool bt_crypto_c1(struct bt_crypto *crypto, const uint8_t k[16],
518  const uint8_t r[16], const uint8_t pres[7],
519  const uint8_t preq[7], uint8_t iat,
520  const uint8_t ia[6], uint8_t rat,
521  const uint8_t ra[6], uint8_t res[16])
522 {
523  uint8_t p1[16], p2[16];
524 
525  /* p1 = pres || preq || _rat || _iat */
526  p1[0] = iat;
527  p1[1] = rat;
528  memcpy(p1 + 2, preq, 7);
529  memcpy(p1 + 9, pres, 7);
530 
531  /* p2 = padding || ia || ra */
532  memcpy(p2, ra, 6);
533  memcpy(p2 + 6, ia, 6);
534  memset(p2 + 12, 0, 4);
535 
536  /* res = r XOR p1 */
537  u128_xor(r, p1, res);
538 
539  /* res = e(k, res) */
540  if (!bt_crypto_e(crypto, k, res, res))
541  return false;
542 
543  /* res = res XOR p2 */
544  u128_xor(res, p2, res);
545 
546  /* res = e(k, res) */
547  return bt_crypto_e(crypto, k, res, res);
548 }
549 
581 bool bt_crypto_s1(struct bt_crypto *crypto, const uint8_t k[16],
582  const uint8_t r1[16], const uint8_t r2[16],
583  uint8_t res[16])
584 {
585  memcpy(res, r2, 8);
586  memcpy(res + 8, r1, 8);
587 
588  return bt_crypto_e(crypto, k, res, res);
589 }
590 
591 static bool aes_cmac(struct bt_crypto *crypto, uint8_t key[16], uint8_t *msg,
592  size_t msg_len, uint8_t res[16])
593 {
594  uint8_t key_msb[16], out[16], msg_msb[CMAC_MSG_MAX];
595  ssize_t len;
596  int fd;
597 
598  if (msg_len > CMAC_MSG_MAX)
599  return false;
600 
601  swap_buf(key, key_msb, 16);
602  fd = alg_new(crypto->cmac_aes, key_msb, 16);
603  if (fd < 0)
604  return false;
605 
606  swap_buf(msg, msg_msb, msg_len);
607  len = send(fd, msg_msb, msg_len, 0);
608  if (len < 0) {
609  close(fd);
610  return false;
611  }
612 
613  len = read(fd, out, 16);
614  if (len < 0) {
615  close(fd);
616  return false;
617  }
618 
619  swap_buf(out, res, 16);
620 
621  close(fd);
622 
623  return true;
624 }
625 
626 bool bt_crypto_f4(struct bt_crypto *crypto, uint8_t u[32], uint8_t v[32],
627  uint8_t x[16], uint8_t z, uint8_t res[16])
628 {
629  uint8_t m[65];
630 
631  if (!crypto)
632  return false;
633 
634  m[0] = z;
635  memcpy(&m[1], v, 32);
636  memcpy(&m[33], u, 32);
637 
638  return aes_cmac(crypto, x, m, sizeof(m), res);
639 }
640 
641 bool bt_crypto_f5(struct bt_crypto *crypto, uint8_t w[32], uint8_t n1[16],
642  uint8_t n2[16], uint8_t a1[7], uint8_t a2[7],
643  uint8_t mackey[16], uint8_t ltk[16])
644 {
645  uint8_t btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
646  uint8_t salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
647  0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
648  uint8_t length[2] = { 0x00, 0x01 };
649  uint8_t m[53], t[16];
650 
651  if (!aes_cmac(crypto, salt, w, 32, t))
652  return false;
653 
654  memcpy(&m[0], length, 2);
655  memcpy(&m[2], a2, 7);
656  memcpy(&m[9], a1, 7);
657  memcpy(&m[16], n2, 16);
658  memcpy(&m[32], n1, 16);
659  memcpy(&m[48], btle, 4);
660 
661  m[52] = 0; /* Counter */
662  if (!aes_cmac(crypto, t, m, sizeof(m), mackey))
663  return false;
664 
665  m[52] = 1; /* Counter */
666  return aes_cmac(crypto, t, m, sizeof(m), ltk);
667 }
668 
669 bool bt_crypto_f6(struct bt_crypto *crypto, uint8_t w[16], uint8_t n1[16],
670  uint8_t n2[16], uint8_t r[16], uint8_t io_cap[3],
671  uint8_t a1[7], uint8_t a2[7], uint8_t res[16])
672 {
673  uint8_t m[65];
674 
675  memcpy(&m[0], a2, 7);
676  memcpy(&m[7], a1, 7);
677  memcpy(&m[14], io_cap, 3);
678  memcpy(&m[17], r, 16);
679  memcpy(&m[33], n2, 16);
680  memcpy(&m[49], n1, 16);
681 
682  return aes_cmac(crypto, w, m, sizeof(m), res);
683 }
684 
685 bool bt_crypto_g2(struct bt_crypto *crypto, uint8_t u[32], uint8_t v[32],
686  uint8_t x[16], uint8_t y[16], uint32_t *val)
687 {
688  uint8_t m[80], tmp[16];
689 
690  memcpy(&m[0], y, 16);
691  memcpy(&m[16], v, 32);
692  memcpy(&m[48], u, 32);
693 
694  if (!aes_cmac(crypto, x, m, sizeof(m), tmp))
695  return false;
696 
697  *val = get_le32(tmp);
698  *val %= 1000000;
699 
700  return true;
701 }
__u32 salg_mask
Definition: crypto.c:58
bool bt_crypto_c1(struct bt_crypto *crypto, const uint8_t k[16], const uint8_t r[16], const uint8_t pres[7], const uint8_t preq[7], uint8_t iat, const uint8_t ia[6], uint8_t rat, const uint8_t ra[6], uint8_t res[16])
Definition: crypto.c:517
#define CMAC_MSG_MAX
Definition: crypto.c:85
static bool aes_cmac(struct bt_crypto *crypto, uint8_t key[16], uint8_t *msg, size_t msg_len, uint8_t res[16])
Definition: crypto.c:591
int cmac_aes
Definition: crypto.c:91
bool bt_crypto_s1(struct bt_crypto *crypto, const uint8_t k[16], const uint8_t r1[16], const uint8_t r2[16], uint8_t res[16])
Definition: crypto.c:581
#define ALG_OP_ENCRYPT
Definition: crypto.c:72
__u32 salg_feat
Definition: crypto.c:57
static int alg_new(int fd, const void *keyval, socklen_t keylen)
Definition: crypto.c:230
static void u128_xor(const uint8_t p[16], const uint8_t q[16], uint8_t r[16])
Definition: crypto.c:451
#define ALG_SET_KEY
Definition: crypto.c:67
static void put_be32(uint32_t val, void *dst)
Definition: util.h:145
int ref_count
Definition: crypto.c:88
static bool alg_encrypt(int fd, const void *inbuf, size_t inlen, void *outbuf, size_t outlen)
Definition: crypto.c:239
__u8 salg_name[64]
Definition: crypto.c:59
uint64_t a
Definition: crypto.c:448
__u16 salg_family
Definition: crypto.c:55
__u32 ivlen
Definition: crypto.c:63
bool bt_crypto_f4(struct bt_crypto *crypto, uint8_t u[32], uint8_t v[32], uint8_t x[16], uint8_t z, uint8_t res[16])
Definition: crypto.c:626
struct bt_crypto * bt_crypto_new(void)
Definition: crypto.c:158
static uint32_t get_le32(const void *ptr)
Definition: util.h:110
uint32_t __u32
Definition: crypto.c:49
static int ecb_aes_setup(void)
Definition: crypto.c:114
bool bt_crypto_g2(struct bt_crypto *crypto, uint8_t u[32], uint8_t v[32], uint8_t x[16], uint8_t y[16], uint32_t *val)
Definition: crypto.c:685
int ecb_aes
Definition: crypto.c:89
#define ALG_SET_OP
Definition: crypto.c:69
static void put_le32(uint32_t val, void *dst)
Definition: util.h:140
bool bt_crypto_f5(struct bt_crypto *crypto, uint8_t w[32], uint8_t n1[16], uint8_t n2[16], uint8_t a1[7], uint8_t a2[7], uint8_t mackey[16], uint8_t ltk[16])
Definition: crypto.c:641
__u8 salg_type[14]
Definition: crypto.c:56
struct bt_crypto * bt_crypto_ref(struct bt_crypto *crypto)
Definition: crypto.c:190
#define PF_ALG
Definition: crypto.c:74
#define AF_ALG
Definition: crypto.c:75
bool bt_crypto_sign_att(struct bt_crypto *crypto, const uint8_t key[16], const uint8_t *m, uint16_t m_len, uint32_t sign_cnt, uint8_t signature[12])
Definition: crypto.c:286
uint8_t __u8
Definition: crypto.c:47
bool bt_crypto_random_bytes(struct bt_crypto *crypto, uint8_t *buf, uint8_t num_bytes)
Definition: crypto.c:215
bool bt_crypto_e(struct bt_crypto *crypto, const uint8_t key[16], const uint8_t plaintext[16], uint8_t encrypted[16])
Definition: crypto.c:359
static void swap_buf(const uint8_t *src, uint8_t *dst, uint16_t len)
Definition: crypto.c:278
#define SOL_ALG
Definition: crypto.c:81
void bt_crypto_unref(struct bt_crypto *crypto)
Definition: crypto.c:200
Definition: crypto.c:447
bool bt_crypto_f6(struct bt_crypto *crypto, uint8_t w[16], uint8_t n1[16], uint8_t n2[16], uint8_t r[16], uint8_t io_cap[3], uint8_t a1[7], uint8_t a2[7], uint8_t res[16])
Definition: crypto.c:669
__u8 iv[0]
Definition: crypto.c:64
static int cmac_aes_setup(void)
Definition: crypto.c:136
uint16_t __u16
Definition: crypto.c:48
int urandom
Definition: crypto.c:90
#define new0(t, n)
Definition: util.h:82
uint64_t b
Definition: crypto.c:448
static int urandom_setup(void)
Definition: crypto.c:99
bool bt_crypto_ah(struct bt_crypto *crypto, const uint8_t k[16], const uint8_t r[3], uint8_t hash[3])
Definition: crypto.c:424