rpa.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * @file rpa.c
  3. * Resolvable Private Address Generation and Resolution
  4. */
  5. /*
  6. * Copyright (c) 2017 Nordic Semiconductor ASA
  7. * Copyright (c) 2015-2016 Intel Corporation
  8. *
  9. * SPDX-License-Identifier: Apache-2.0
  10. */
  11. #include <zephyr.h>
  12. #include <stddef.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_RPA)
  16. #define LOG_MODULE_NAME bt_rpa
  17. #include "common/log.h"
  18. #include <acts_bluetooth/crypto.h>
  19. #if defined(CONFIG_BT_CTLR) && defined(CONFIG_BT_HOST_CRYPTO)
  20. #include "../controller/util/util.h"
  21. #include "../controller/hal/ecb.h"
  22. #endif /* defined(CONFIG_BT_CTLR) && defined(CONFIG_BT_HOST_CRYPTO) */
  23. #if defined(CONFIG_BT_PRIVACY) || defined(CONFIG_BT_CTLR_PRIVACY)
  24. static int internal_rand(void *buf, size_t len)
  25. {
  26. /* Force using controller rand function. */
  27. #if defined(CONFIG_BT_CTLR) && defined(CONFIG_BT_HOST_CRYPTO)
  28. return lll_csrand_get(buf, len);
  29. #else
  30. return bt_rand(buf, len);
  31. #endif
  32. }
  33. #endif /* defined(CONFIG_BT_PRIVACY) || defined(CONFIG_BT_CTLR_PRIVACY) */
  34. static int internal_encrypt_le(const uint8_t key[16], const uint8_t plaintext[16],
  35. uint8_t enc_data[16])
  36. {
  37. /* Force using controller encrypt function if supported. */
  38. #if defined(CONFIG_BT_CTLR) && defined(CONFIG_BT_HOST_CRYPTO) && \
  39. defined(CONFIG_BT_CTLR_LE_ENC)
  40. ecb_encrypt(key, plaintext, enc_data, NULL);
  41. return 0;
  42. #else
  43. return bt_encrypt_le(key, plaintext, enc_data);
  44. #endif
  45. }
  46. static int ah(const uint8_t irk[16], const uint8_t r[3], uint8_t out[3])
  47. {
  48. uint8_t res[16];
  49. int err;
  50. BT_DBG("irk %s", bt_hex(irk, 16));
  51. BT_DBG("r %s", bt_hex(r, 3));
  52. /* r' = padding || r */
  53. memcpy(res, r, 3);
  54. (void)memset(res + 3, 0, 13);
  55. err = internal_encrypt_le(irk, res, res);
  56. if (err) {
  57. return err;
  58. }
  59. /* The output of the random address function ah is:
  60. * ah(h, r) = e(k, r') mod 2^24
  61. * The output of the security function e is then truncated to 24 bits
  62. * by taking the least significant 24 bits of the output of e as the
  63. * result of ah.
  64. */
  65. memcpy(out, res, 3);
  66. return 0;
  67. }
  68. #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_CTLR_PRIVACY)
  69. bool bt_rpa_irk_matches(const uint8_t irk[16], const bt_addr_t *addr)
  70. {
  71. uint8_t hash[3];
  72. int err;
  73. BT_DBG("IRK %s bdaddr %s", bt_hex(irk, 16), bt_addr_str(addr));
  74. err = ah(irk, addr->val + 3, hash);
  75. if (err) {
  76. return false;
  77. }
  78. return !memcmp(addr->val, hash, 3);
  79. }
  80. #endif
  81. #if defined(CONFIG_BT_PRIVACY) || defined(CONFIG_BT_CTLR_PRIVACY)
  82. int bt_rpa_create(const uint8_t irk[16], bt_addr_t *rpa)
  83. {
  84. int err;
  85. err = internal_rand(rpa->val + 3, 3);
  86. if (err) {
  87. return err;
  88. }
  89. BT_ADDR_SET_RPA(rpa);
  90. err = ah(irk, rpa->val + 3, rpa->val);
  91. if (err) {
  92. return err;
  93. }
  94. BT_DBG("Created RPA %s", bt_addr_str((bt_addr_t *)rpa->val));
  95. return 0;
  96. }
  97. #else
  98. int bt_rpa_create(const uint8_t irk[16], bt_addr_t *rpa)
  99. {
  100. return -ENOTSUP;
  101. }
  102. #endif /* CONFIG_BT_PRIVACY */