aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohan Hedberg <johan.hedberg@intel.com>2014-12-02 06:39:23 -0500
committerMarcel Holtmann <marcel@holtmann.org>2014-12-03 10:51:22 -0500
commit06edf8deb55dbdcda2177da31d75ac79ccdc5841 (patch)
tree88eb45af289da23d62d277f595e6aee45190dee9
parentcd0827976205f842cc722f48a1427c9b77c3ca28 (diff)
Bluetooth: Organize SMP crypto functions to logical sections
This patch organizes the various SMP crypto functions so that the LE SC functions appear in one section and the legacy SMP functions in a separate one. Signed-off-by: Johan Hedberg <johan.hedberg@intel.com> Signed-off-by: Marcel Holtmann <marcel@holtmann.org>
-rw-r--r--net/bluetooth/smp.c134
1 files changed, 71 insertions, 63 deletions
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index f845dbf2e677..1d1c33d5d1dc 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -141,6 +141,10 @@ static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
141 dst[len - 1 - i] = src[i]; 141 dst[len - 1 - i] = src[i];
142} 142}
143 143
144/* The following functions map to the LE SC SMP crypto functions
145 * AES-CMAC, f4, f5, f6, g2 and h6.
146 */
147
144static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m, 148static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
145 size_t len, u8 mac[16]) 149 size_t len, u8 mac[16])
146{ 150{
@@ -325,6 +329,26 @@ static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
325 return 0; 329 return 0;
326} 330}
327 331
332static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
333 const u8 key_id[4], u8 res[16])
334{
335 int err;
336
337 SMP_DBG("w %16phN key_id %4phN", w, key_id);
338
339 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
340 if (err)
341 return err;
342
343 SMP_DBG("res %16phN", res);
344
345 return err;
346}
347
348/* The following functions map to the legacy SMP crypto functions e, c1,
349 * s1 and ah.
350 */
351
328static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r) 352static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
329{ 353{
330 struct blkcipher_desc desc; 354 struct blkcipher_desc desc;
@@ -364,18 +388,59 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
364 return err; 388 return err;
365} 389}
366 390
367static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16], 391static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
368 const u8 key_id[4], u8 res[16]) 392 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
393 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
369{ 394{
395 u8 p1[16], p2[16];
370 int err; 396 int err;
371 397
372 SMP_DBG("w %16phN key_id %4phN", w, key_id); 398 memset(p1, 0, 16);
373 399
374 err = aes_cmac(tfm_cmac, w, key_id, 4, res); 400 /* p1 = pres || preq || _rat || _iat */
375 if (err) 401 p1[0] = _iat;
402 p1[1] = _rat;
403 memcpy(p1 + 2, preq, 7);
404 memcpy(p1 + 9, pres, 7);
405
406 /* p2 = padding || ia || ra */
407 memcpy(p2, ra, 6);
408 memcpy(p2 + 6, ia, 6);
409 memset(p2 + 12, 0, 4);
410
411 /* res = r XOR p1 */
412 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
413
414 /* res = e(k, res) */
415 err = smp_e(tfm_aes, k, res);
416 if (err) {
417 BT_ERR("Encrypt data error");
376 return err; 418 return err;
419 }
377 420
378 SMP_DBG("res %16phN", res); 421 /* res = res XOR p2 */
422 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
423
424 /* res = e(k, res) */
425 err = smp_e(tfm_aes, k, res);
426 if (err)
427 BT_ERR("Encrypt data error");
428
429 return err;
430}
431
432static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
433 const u8 r1[16], const u8 r2[16], u8 _r[16])
434{
435 int err;
436
437 /* Just least significant octets from r1 and r2 are considered */
438 memcpy(_r, r2, 8);
439 memcpy(_r + 8, r1, 8);
440
441 err = smp_e(tfm_aes, k, _r);
442 if (err)
443 BT_ERR("Encrypt data error");
379 444
380 return err; 445 return err;
381} 446}
@@ -454,63 +519,6 @@ int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
454 return 0; 519 return 0;
455} 520}
456 521
457static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
458 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
459 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
460{
461 u8 p1[16], p2[16];
462 int err;
463
464 memset(p1, 0, 16);
465
466 /* p1 = pres || preq || _rat || _iat */
467 p1[0] = _iat;
468 p1[1] = _rat;
469 memcpy(p1 + 2, preq, 7);
470 memcpy(p1 + 9, pres, 7);
471
472 /* p2 = padding || ia || ra */
473 memcpy(p2, ra, 6);
474 memcpy(p2 + 6, ia, 6);
475 memset(p2 + 12, 0, 4);
476
477 /* res = r XOR p1 */
478 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
479
480 /* res = e(k, res) */
481 err = smp_e(tfm_aes, k, res);
482 if (err) {
483 BT_ERR("Encrypt data error");
484 return err;
485 }
486
487 /* res = res XOR p2 */
488 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
489
490 /* res = e(k, res) */
491 err = smp_e(tfm_aes, k, res);
492 if (err)
493 BT_ERR("Encrypt data error");
494
495 return err;
496}
497
498static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
499 const u8 r1[16], const u8 r2[16], u8 _r[16])
500{
501 int err;
502
503 /* Just least significant octets from r1 and r2 are considered */
504 memcpy(_r, r2, 8);
505 memcpy(_r + 8, r1, 8);
506
507 err = smp_e(tfm_aes, k, _r);
508 if (err)
509 BT_ERR("Encrypt data error");
510
511 return err;
512}
513
514static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) 522static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
515{ 523{
516 struct l2cap_chan *chan = conn->smp; 524 struct l2cap_chan *chan = conn->smp;