crypto.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* Bluetooth Mesh */
  2. /*
  3. * SPDX-FileCopyrightText: 2017 Intel Corporation
  4. * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. */
  8. #ifndef _CRYPTO_H_
  9. #define _CRYPTO_H_
  10. #include <string.h>
  11. #include "mesh/buf.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. struct bt_mesh_sg {
  16. const void *data;
  17. size_t len;
  18. };
  19. int bt_mesh_aes_cmac(const uint8_t key[16], struct bt_mesh_sg *sg,
  20. size_t sg_len, uint8_t mac[16]);
  21. static inline int bt_mesh_aes_cmac_one(const uint8_t key[16], const void *m,
  22. size_t len, uint8_t mac[16])
  23. {
  24. struct bt_mesh_sg sg = { m, len };
  25. return bt_mesh_aes_cmac(key, &sg, 1, mac);
  26. }
  27. static inline bool bt_mesh_s1(const char *m, uint8_t salt[16])
  28. {
  29. const uint8_t zero[16] = { 0 };
  30. return bt_mesh_aes_cmac_one(zero, m, strlen(m), salt);
  31. }
  32. int bt_mesh_k1(const uint8_t *ikm, size_t ikm_len, const uint8_t salt[16],
  33. const char *info, uint8_t okm[16]);
  34. #define bt_mesh_k1_str(ikm, ikm_len, salt_str, info, okm) \
  35. ({ \
  36. const uint8_t salt[16] = salt_str; \
  37. bt_mesh_k1(ikm, ikm_len, salt, info, okm); \
  38. })
  39. int bt_mesh_k2(const uint8_t n[16], const uint8_t *p, size_t p_len,
  40. uint8_t net_id[1], uint8_t enc_key[16], uint8_t priv_key[16]);
  41. int bt_mesh_k3(const uint8_t n[16], uint8_t out[8]);
  42. int bt_mesh_k4(const uint8_t n[16], uint8_t out[1]);
  43. int bt_mesh_id128(const uint8_t n[16], const char *s, uint8_t out[16]);
  44. static inline int bt_mesh_id_resolving_key(const uint8_t net_key[16],
  45. uint8_t resolving_key[16])
  46. {
  47. return bt_mesh_k1_str(net_key, 16, "smbt", "smbi", resolving_key);
  48. }
  49. static inline int bt_mesh_identity_key(const uint8_t net_key[16],
  50. uint8_t identity_key[16])
  51. {
  52. return bt_mesh_id128(net_key, "nkik", identity_key);
  53. }
  54. static inline int bt_mesh_secure_beacon_key(const uint8_t net_key[16],
  55. uint8_t beacon_key[16])
  56. {
  57. return bt_mesh_id128(net_key, "nkbk", beacon_key);
  58. }
  59. int bt_mesh_beacon_auth(const uint8_t beacon_key[16], uint8_t flags,
  60. const uint8_t net_id[8], uint32_t iv_index,
  61. uint8_t auth[8]);
  62. int bt_mesh_secure_beacon_auth(const uint8_t beacon_key[16], uint8_t flags,
  63. const uint8_t net_id[8], uint32_t iv_index,
  64. uint8_t auth[8]);
  65. static inline int bt_mesh_app_id(const uint8_t app_key[16], uint8_t app_id[1])
  66. {
  67. return bt_mesh_k4(app_key, app_id);
  68. }
  69. static inline int bt_mesh_session_key(const uint8_t dhkey[32],
  70. const uint8_t prov_salt[16],
  71. uint8_t session_key[16])
  72. {
  73. return bt_mesh_k1(dhkey, 32, prov_salt, "prsk", session_key);
  74. }
  75. static inline int bt_mesh_prov_nonce(const uint8_t dhkey[32],
  76. const uint8_t prov_salt[16],
  77. uint8_t nonce[13])
  78. {
  79. uint8_t tmp[16];
  80. int err;
  81. err = bt_mesh_k1(dhkey, 32, prov_salt, "prsn", tmp);
  82. if (!err) {
  83. memcpy(nonce, tmp + 3, 13);
  84. }
  85. return err;
  86. }
  87. static inline int bt_mesh_dev_key(const uint8_t dhkey[32],
  88. const uint8_t prov_salt[16],
  89. uint8_t dev_key[16])
  90. {
  91. return bt_mesh_k1(dhkey, 32, prov_salt, "prdk", dev_key);
  92. }
  93. static inline int bt_mesh_prov_salt(const uint8_t conf_salt[16],
  94. const uint8_t prov_rand[16],
  95. const uint8_t dev_rand[16],
  96. uint8_t prov_salt[16])
  97. {
  98. const uint8_t prov_salt_key[16] = { 0 };
  99. struct bt_mesh_sg sg[] = {
  100. { conf_salt, 16 },
  101. { prov_rand, 16 },
  102. { dev_rand, 16 },
  103. };
  104. return bt_mesh_aes_cmac(prov_salt_key, sg, ARRAY_SIZE(sg), prov_salt);
  105. }
  106. int bt_mesh_net_obfuscate(uint8_t *pdu, uint32_t iv_index,
  107. const uint8_t privacy_key[16]);
  108. int bt_mesh_net_encrypt(const uint8_t key[16], struct net_buf_simple *buf,
  109. uint32_t iv_index, bool proxy, bool proxy_solic);
  110. int bt_mesh_net_decrypt(const uint8_t key[16], struct net_buf_simple *buf,
  111. uint32_t iv_index, bool proxy, bool proxy_solic);
  112. int bt_mesh_app_encrypt(const uint8_t key[16], bool dev_key, uint8_t aszmic,
  113. struct net_buf_simple *buf, const uint8_t *ad,
  114. uint16_t src, uint16_t dst, uint32_t seq_num, uint32_t iv_index);
  115. int bt_mesh_app_decrypt(const uint8_t key[16], bool dev_key, uint8_t aszmic,
  116. struct net_buf_simple *buf, struct net_buf_simple *out,
  117. const uint8_t *ad, uint16_t src, uint16_t dst, uint32_t seq_num,
  118. uint32_t iv_index);
  119. uint8_t bt_mesh_fcs_calc(const uint8_t *data, uint8_t data_len);
  120. bool bt_mesh_fcs_check(struct net_buf_simple *buf, uint8_t received_fcs);
  121. int bt_mesh_virtual_addr(const uint8_t virtual_label[16], uint16_t *addr);
  122. int bt_mesh_prov_conf_salt(const uint8_t conf_inputs[145], uint8_t salt[16]);
  123. int bt_mesh_prov_conf_key(const uint8_t dhkey[32], const uint8_t conf_salt[16],
  124. uint8_t conf_key[16]);
  125. int bt_mesh_prov_conf(const uint8_t conf_key[16], const uint8_t rand[16],
  126. const uint8_t auth[16], uint8_t conf[16]);
  127. int bt_mesh_prov_decrypt(const uint8_t key[16], uint8_t nonce[13],
  128. const uint8_t data[25 + 8], uint8_t out[25]);
  129. int bt_mesh_prov_encrypt(const uint8_t key[16], uint8_t nonce[13],
  130. const uint8_t data[25], uint8_t out[33]);
  131. #ifdef __cplusplus
  132. }
  133. #endif
  134. #endif /* _CRYPTO_H_ */