crypto.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. #include <string.h>
  9. #include <stdbool.h>
  10. #include <errno.h>
  11. #include <tinycrypt/aes.h>
  12. #include <tinycrypt/constants.h>
  13. #include <tinycrypt/cmac_mode.h>
  14. #include <tinycrypt/hmac.h>
  15. #include <tinycrypt/sha256.h>
  16. #include "crypto.h"
  17. #include "mesh/config.h"
  18. #include "mesh/common.h"
  19. #include "mesh/adapter.h"
  20. #include "mesh_v1.1/utils.h"
  21. #define NET_MIC_LEN(pdu) (((pdu)[1] & 0x80) ? 8 : 4)
  22. #define APP_MIC_LEN(aszmic) ((aszmic) ? 8 : 4)
  23. int bt_mesh_aes_cmac(const uint8_t key[16], struct bt_mesh_sg *sg,
  24. size_t sg_len, uint8_t mac[16])
  25. {
  26. struct tc_aes_key_sched_struct sched = {0};
  27. struct tc_cmac_struct state = {0};
  28. if (tc_cmac_setup(&state, key, &sched) == TC_CRYPTO_FAIL) {
  29. return -EIO;
  30. }
  31. for (; sg_len; sg_len--, sg++) {
  32. if (tc_cmac_update(&state, sg->data,
  33. sg->len) == TC_CRYPTO_FAIL) {
  34. return -EIO;
  35. }
  36. }
  37. if (tc_cmac_final(mac, &state) == TC_CRYPTO_FAIL) {
  38. return -EIO;
  39. }
  40. return 0;
  41. }
  42. int bt_mesh_k1(const uint8_t *ikm, size_t ikm_len, const uint8_t salt[16],
  43. const char *info, uint8_t okm[16])
  44. {
  45. int err = 0;
  46. err = bt_mesh_aes_cmac_one(salt, ikm, ikm_len, okm);
  47. if (err < 0) {
  48. return err;
  49. }
  50. return bt_mesh_aes_cmac_one(okm, info, strlen(info), okm);
  51. }
  52. int bt_mesh_k2(const uint8_t n[16], const uint8_t *p, size_t p_len,
  53. uint8_t net_id[1], uint8_t enc_key[16], uint8_t priv_key[16])
  54. {
  55. struct bt_mesh_sg sg[3] = {0};
  56. uint8_t salt[16] = {0};
  57. uint8_t out[16] = {0};
  58. uint8_t t[16] = {0};
  59. uint8_t pad = 0U;
  60. int err = 0;
  61. BT_DBG("n %s", bt_hex(n, 16));
  62. BT_DBG("p %s", bt_hex(p, p_len));
  63. err = bt_mesh_s1("smk2", salt);
  64. if (err) {
  65. return err;
  66. }
  67. err = bt_mesh_aes_cmac_one(salt, n, 16, t);
  68. if (err) {
  69. return err;
  70. }
  71. pad = 0x01;
  72. sg[0].data = NULL;
  73. sg[0].len = 0;
  74. sg[1].data = p;
  75. sg[1].len = p_len;
  76. sg[2].data = &pad;
  77. sg[2].len = sizeof(pad);
  78. err = bt_mesh_aes_cmac(t, sg, ARRAY_SIZE(sg), out);
  79. if (err) {
  80. return err;
  81. }
  82. net_id[0] = out[15] & 0x7f;
  83. sg[0].data = out;
  84. sg[0].len = sizeof(out);
  85. pad = 0x02;
  86. err = bt_mesh_aes_cmac(t, sg, ARRAY_SIZE(sg), out);
  87. if (err) {
  88. return err;
  89. }
  90. memcpy(enc_key, out, 16);
  91. pad = 0x03;
  92. err = bt_mesh_aes_cmac(t, sg, ARRAY_SIZE(sg), out);
  93. if (err) {
  94. return err;
  95. }
  96. memcpy(priv_key, out, 16);
  97. BT_DBG("NID 0x%02x enc_key %s", net_id[0], bt_hex(enc_key, 16));
  98. BT_DBG("priv_key %s", bt_hex(priv_key, 16));
  99. return 0;
  100. }
  101. int bt_mesh_k3(const uint8_t n[16], uint8_t out[8])
  102. {
  103. uint8_t id64[] = { 'i', 'd', '6', '4', 0x01 };
  104. uint8_t tmp[16] = {0};
  105. uint8_t t[16] = {0};
  106. int err = 0;
  107. err = bt_mesh_s1("smk3", tmp);
  108. if (err) {
  109. return err;
  110. }
  111. err = bt_mesh_aes_cmac_one(tmp, n, 16, t);
  112. if (err) {
  113. return err;
  114. }
  115. err = bt_mesh_aes_cmac_one(t, id64, sizeof(id64), tmp);
  116. if (err) {
  117. return err;
  118. }
  119. memcpy(out, tmp + 8, 8);
  120. return 0;
  121. }
  122. int bt_mesh_k4(const uint8_t n[16], uint8_t out[1])
  123. {
  124. uint8_t id6[] = { 'i', 'd', '6', 0x01 };
  125. uint8_t tmp[16] = {0};
  126. uint8_t t[16] = {0};
  127. int err = 0;
  128. err = bt_mesh_s1("smk4", tmp);
  129. if (err) {
  130. return err;
  131. }
  132. err = bt_mesh_aes_cmac_one(tmp, n, 16, t);
  133. if (err) {
  134. return err;
  135. }
  136. err = bt_mesh_aes_cmac_one(t, id6, sizeof(id6), tmp);
  137. if (err) {
  138. return err;
  139. }
  140. out[0] = tmp[15] & BIT_MASK(6);
  141. return 0;
  142. }
  143. int bt_mesh_id128(const uint8_t n[16], const char *s, uint8_t out[16])
  144. {
  145. const char *id128 = "id128\x01";
  146. uint8_t salt[16] = {0};
  147. int err = 0;
  148. err = bt_mesh_s1(s, salt);
  149. if (err) {
  150. return err;
  151. }
  152. return bt_mesh_k1(n, 16, salt, id128, out);
  153. }
  154. static int bt_mesh_ccm_decrypt(const uint8_t key[16], uint8_t nonce[13],
  155. const uint8_t *enc_msg, size_t msg_len,
  156. const uint8_t *aad, size_t aad_len,
  157. uint8_t *out_msg, size_t mic_size)
  158. {
  159. uint8_t msg[16] = {0}, pmsg[16] = {0}, cmic[16] = {0},
  160. cmsg[16] = {0}, Xn[16] = {0}, mic[16] = {0};
  161. uint16_t last_blk = 0U, blk_cnt = 0U;
  162. size_t i = 0U, j = 0U;
  163. int err = 0;
  164. if (msg_len < 1 || aad_len >= 0xff00) {
  165. return -EINVAL;
  166. }
  167. /* C_mic = e(AppKey, 0x01 || nonce || 0x0000) */
  168. pmsg[0] = 0x01;
  169. memcpy(pmsg + 1, nonce, 13);
  170. sys_put_be16(0x0000, pmsg + 14);
  171. err = bt_mesh_encrypt_be(key, pmsg, cmic);
  172. if (err) {
  173. return err;
  174. }
  175. /* X_0 = e(AppKey, 0x09 || nonce || length) */
  176. if (mic_size == sizeof(uint64_t)) {
  177. pmsg[0] = 0x19 | (aad_len ? 0x40 : 0x00);
  178. } else {
  179. pmsg[0] = 0x09 | (aad_len ? 0x40 : 0x00);
  180. }
  181. memcpy(pmsg + 1, nonce, 13);
  182. sys_put_be16(msg_len, pmsg + 14);
  183. err = bt_mesh_encrypt_be(key, pmsg, Xn);
  184. if (err) {
  185. return err;
  186. }
  187. /* If AAD is being used to authenticate, include it here */
  188. if (aad_len) {
  189. sys_put_be16(aad_len, pmsg);
  190. for (i = 0; i < sizeof(uint16_t); i++) {
  191. pmsg[i] = Xn[i] ^ pmsg[i];
  192. }
  193. j = 0;
  194. aad_len += sizeof(uint16_t);
  195. while (aad_len > 16) {
  196. do {
  197. pmsg[i] = Xn[i] ^ aad[j];
  198. i++, j++;
  199. } while (i < 16);
  200. aad_len -= 16;
  201. i = 0;
  202. err = bt_mesh_encrypt_be(key, pmsg, Xn);
  203. if (err) {
  204. return err;
  205. }
  206. }
  207. for (; i < aad_len; i++, j++) {
  208. pmsg[i] = Xn[i] ^ aad[j];
  209. }
  210. for (i = aad_len; i < 16; i++) {
  211. pmsg[i] = Xn[i];
  212. }
  213. err = bt_mesh_encrypt_be(key, pmsg, Xn);
  214. if (err) {
  215. return err;
  216. }
  217. }
  218. last_blk = msg_len % 16;
  219. blk_cnt = (msg_len + 15) / 16;
  220. if (!last_blk) {
  221. last_blk = 16U;
  222. }
  223. for (j = 0; j < blk_cnt; j++) {
  224. if (j + 1 == blk_cnt) {
  225. /* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */
  226. pmsg[0] = 0x01;
  227. memcpy(pmsg + 1, nonce, 13);
  228. sys_put_be16(j + 1, pmsg + 14);
  229. err = bt_mesh_encrypt_be(key, pmsg, cmsg);
  230. if (err) {
  231. return err;
  232. }
  233. /* Encrypted = Payload[0-15] ^ C_1 */
  234. for (i = 0; i < last_blk; i++) {
  235. msg[i] = enc_msg[(j * 16) + i] ^ cmsg[i];
  236. }
  237. memcpy(out_msg + (j * 16), msg, last_blk);
  238. /* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */
  239. for (i = 0; i < last_blk; i++) {
  240. pmsg[i] = Xn[i] ^ msg[i];
  241. }
  242. for (i = last_blk; i < 16; i++) {
  243. pmsg[i] = Xn[i] ^ 0x00;
  244. }
  245. err = bt_mesh_encrypt_be(key, pmsg, Xn);
  246. if (err) {
  247. return err;
  248. }
  249. /* MIC = C_mic ^ X_1 */
  250. for (i = 0; i < sizeof(mic); i++) {
  251. mic[i] = cmic[i] ^ Xn[i];
  252. }
  253. } else {
  254. /* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */
  255. pmsg[0] = 0x01;
  256. memcpy(pmsg + 1, nonce, 13);
  257. sys_put_be16(j + 1, pmsg + 14);
  258. err = bt_mesh_encrypt_be(key, pmsg, cmsg);
  259. if (err) {
  260. return err;
  261. }
  262. /* Encrypted = Payload[0-15] ^ C_1 */
  263. for (i = 0; i < 16; i++) {
  264. msg[i] = enc_msg[(j * 16) + i] ^ cmsg[i];
  265. }
  266. memcpy(out_msg + (j * 16), msg, 16);
  267. /* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */
  268. for (i = 0; i < 16; i++) {
  269. pmsg[i] = Xn[i] ^ msg[i];
  270. }
  271. err = bt_mesh_encrypt_be(key, pmsg, Xn);
  272. if (err) {
  273. return err;
  274. }
  275. }
  276. }
  277. if (memcmp(mic, enc_msg + msg_len, mic_size)) {
  278. return -EBADMSG;
  279. }
  280. return 0;
  281. }
  282. static int bt_mesh_ccm_encrypt(const uint8_t key[16], uint8_t nonce[13],
  283. const uint8_t *msg, size_t msg_len,
  284. const uint8_t *aad, size_t aad_len,
  285. uint8_t *out_msg, size_t mic_size)
  286. {
  287. uint8_t pmsg[16] = {0}, cmic[16] = {0}, cmsg[16] = {0},
  288. mic[16] = {0}, Xn[16] = {0};
  289. uint16_t blk_cnt = 0U, last_blk = 0U;
  290. size_t i = 0U, j = 0U;
  291. int err = 0;
  292. BT_DBG("key %s", bt_hex(key, 16));
  293. BT_DBG("nonce %s", bt_hex(nonce, 13));
  294. BT_DBG("msg (len %u) %s", msg_len, bt_hex(msg, msg_len));
  295. BT_DBG("aad_len %u mic_size %u", aad_len, mic_size);
  296. /* Unsupported AAD size */
  297. if (aad_len >= 0xff00) {
  298. return -EINVAL;
  299. }
  300. /* C_mic = e(AppKey, 0x01 || nonce || 0x0000) */
  301. pmsg[0] = 0x01;
  302. memcpy(pmsg + 1, nonce, 13);
  303. sys_put_be16(0x0000, pmsg + 14);
  304. err = bt_mesh_encrypt_be(key, pmsg, cmic);
  305. if (err) {
  306. return err;
  307. }
  308. /* X_0 = e(AppKey, 0x09 || nonce || length) */
  309. if (mic_size == sizeof(uint64_t)) {
  310. pmsg[0] = 0x19 | (aad_len ? 0x40 : 0x00);
  311. } else {
  312. pmsg[0] = 0x09 | (aad_len ? 0x40 : 0x00);
  313. }
  314. memcpy(pmsg + 1, nonce, 13);
  315. sys_put_be16(msg_len, pmsg + 14);
  316. err = bt_mesh_encrypt_be(key, pmsg, Xn);
  317. if (err) {
  318. return err;
  319. }
  320. /* If AAD is being used to authenticate, include it here */
  321. if (aad_len) {
  322. sys_put_be16(aad_len, pmsg);
  323. for (i = 0; i < sizeof(uint16_t); i++) {
  324. pmsg[i] = Xn[i] ^ pmsg[i];
  325. }
  326. j = 0;
  327. aad_len += sizeof(uint16_t);
  328. while (aad_len > 16) {
  329. do {
  330. pmsg[i] = Xn[i] ^ aad[j];
  331. i++, j++;
  332. } while (i < 16);
  333. aad_len -= 16;
  334. i = 0;
  335. err = bt_mesh_encrypt_be(key, pmsg, Xn);
  336. if (err) {
  337. return err;
  338. }
  339. }
  340. for (; i < aad_len; i++, j++) {
  341. pmsg[i] = Xn[i] ^ aad[j];
  342. }
  343. for (i = aad_len; i < 16; i++) {
  344. pmsg[i] = Xn[i];
  345. }
  346. err = bt_mesh_encrypt_be(key, pmsg, Xn);
  347. if (err) {
  348. return err;
  349. }
  350. }
  351. last_blk = msg_len % 16;
  352. blk_cnt = (msg_len + 15) / 16;
  353. if (!last_blk) {
  354. last_blk = 16U;
  355. }
  356. for (j = 0; j < blk_cnt; j++) {
  357. if (j + 1 == blk_cnt) {
  358. /* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */
  359. for (i = 0; i < last_blk; i++) {
  360. pmsg[i] = Xn[i] ^ msg[(j * 16) + i];
  361. }
  362. for (i = last_blk; i < 16; i++) {
  363. pmsg[i] = Xn[i] ^ 0x00;
  364. }
  365. err = bt_mesh_encrypt_be(key, pmsg, Xn);
  366. if (err) {
  367. return err;
  368. }
  369. /* MIC = C_mic ^ X_1 */
  370. for (i = 0; i < sizeof(mic); i++) {
  371. mic[i] = cmic[i] ^ Xn[i];
  372. }
  373. /* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */
  374. pmsg[0] = 0x01;
  375. memcpy(pmsg + 1, nonce, 13);
  376. sys_put_be16(j + 1, pmsg + 14);
  377. err = bt_mesh_encrypt_be(key, pmsg, cmsg);
  378. if (err) {
  379. return err;
  380. }
  381. /* Encrypted = Payload[0-15] ^ C_1 */
  382. for (i = 0; i < last_blk; i++) {
  383. out_msg[(j * 16) + i] =
  384. msg[(j * 16) + i] ^ cmsg[i];
  385. }
  386. } else {
  387. /* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */
  388. for (i = 0; i < 16; i++) {
  389. pmsg[i] = Xn[i] ^ msg[(j * 16) + i];
  390. }
  391. err = bt_mesh_encrypt_be(key, pmsg, Xn);
  392. if (err) {
  393. return err;
  394. }
  395. /* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */
  396. pmsg[0] = 0x01;
  397. memcpy(pmsg + 1, nonce, 13);
  398. sys_put_be16(j + 1, pmsg + 14);
  399. err = bt_mesh_encrypt_be(key, pmsg, cmsg);
  400. if (err) {
  401. return err;
  402. }
  403. /* Encrypted = Payload[0-15] ^ C_N */
  404. for (i = 0; i < 16; i++) {
  405. out_msg[(j * 16) + i] =
  406. msg[(j * 16) + i] ^ cmsg[i];
  407. }
  408. }
  409. }
  410. memcpy(out_msg + msg_len, mic, mic_size);
  411. return 0;
  412. }
  413. #if CONFIG_BLE_MESH_PROXY
  414. static void create_proxy_nonce(uint8_t nonce[13], const uint8_t *pdu,
  415. uint32_t iv_index)
  416. {
  417. /* Nonce Type */
  418. nonce[0] = 0x03;
  419. /* Pad */
  420. nonce[1] = 0x00;
  421. /* Sequence Number */
  422. nonce[2] = pdu[2];
  423. nonce[3] = pdu[3];
  424. nonce[4] = pdu[4];
  425. /* Source Address */
  426. nonce[5] = pdu[5];
  427. nonce[6] = pdu[6];
  428. /* Pad */
  429. nonce[7] = 0U;
  430. nonce[8] = 0U;
  431. /* IV Index */
  432. sys_put_be32(iv_index, &nonce[9]);
  433. }
  434. #endif /* CONFIG_BLE_MESH_PROXY */
  435. static void create_net_nonce(uint8_t nonce[13], const uint8_t *pdu,
  436. uint32_t iv_index)
  437. {
  438. /* Nonce Type */
  439. nonce[0] = 0x00;
  440. /* FRND + TTL */
  441. nonce[1] = pdu[1];
  442. /* Sequence Number */
  443. nonce[2] = pdu[2];
  444. nonce[3] = pdu[3];
  445. nonce[4] = pdu[4];
  446. /* Source Address */
  447. nonce[5] = pdu[5];
  448. nonce[6] = pdu[6];
  449. /* Pad */
  450. nonce[7] = 0U;
  451. nonce[8] = 0U;
  452. /* IV Index */
  453. sys_put_be32(iv_index, &nonce[9]);
  454. }
  455. int bt_mesh_net_obfuscate(uint8_t *pdu, uint32_t iv_index,
  456. const uint8_t privacy_key[16])
  457. {
  458. uint8_t priv_rand[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, };
  459. uint8_t tmp[16] = {0};
  460. int err = 0, i;
  461. BT_DBG("IVIndex %u, PrivacyKey %s", iv_index, bt_hex(privacy_key, 16));
  462. sys_put_be32(iv_index, &priv_rand[5]);
  463. memcpy(&priv_rand[9], &pdu[7], 7);
  464. BT_DBG("PrivacyRandom %s", bt_hex(priv_rand, 16));
  465. err = bt_mesh_encrypt_be(privacy_key, priv_rand, tmp);
  466. if (err) {
  467. return err;
  468. }
  469. for (i = 0; i < 6; i++) {
  470. pdu[1 + i] ^= tmp[i];
  471. }
  472. return 0;
  473. }
  474. int bt_mesh_net_encrypt(const uint8_t key[16], struct net_buf_simple *buf,
  475. uint32_t iv_index, bool proxy, bool proxy_solic)
  476. {
  477. uint8_t mic_len = NET_MIC_LEN(buf->data);
  478. uint8_t nonce[13] = {0};
  479. int err = 0;
  480. BT_DBG("IVIndex %u EncKey %s mic_len %u", iv_index, bt_hex(key, 16),
  481. mic_len);
  482. BT_DBG("PDU (len %u) %s", buf->len, bt_hex(buf->data, buf->len));
  483. #if CONFIG_BLE_MESH_PROXY
  484. if (proxy) {
  485. #if CONFIG_BLE_MESH_PROXY_SOLIC
  486. if (proxy_solic) {
  487. bt_mesh_create_proxy_solic_nonce(nonce, buf->data, iv_index);
  488. } else
  489. #endif /* CONFIG_BLE_MESH_PROXY_SOLIC */
  490. {
  491. create_proxy_nonce(nonce, buf->data, iv_index);
  492. }
  493. } else {
  494. create_net_nonce(nonce, buf->data, iv_index);
  495. }
  496. #else /* CONFIG_BLE_MESH_PROXY */
  497. create_net_nonce(nonce, buf->data, iv_index);
  498. #endif /* CONFIG_BLE_MESH_PROXY */
  499. BT_DBG("Nonce %s", bt_hex(nonce, 13));
  500. err = bt_mesh_ccm_encrypt(key, nonce, &buf->data[7], buf->len - 7,
  501. NULL, 0, &buf->data[7], mic_len);
  502. if (!err) {
  503. net_buf_simple_add(buf, mic_len);
  504. }
  505. return err;
  506. }
  507. int bt_mesh_net_decrypt(const uint8_t key[16], struct net_buf_simple *buf,
  508. uint32_t iv_index, bool proxy, bool proxy_solic)
  509. {
  510. uint8_t mic_len = NET_MIC_LEN(buf->data);
  511. uint8_t nonce[13] = {0};
  512. BT_DBG("PDU (%u bytes) %s", buf->len, bt_hex(buf->data, buf->len));
  513. BT_DBG("iv_index %u, key %s mic_len %u", iv_index, bt_hex(key, 16),
  514. mic_len);
  515. #if CONFIG_BLE_MESH_PROXY
  516. if (proxy) {
  517. #if CONFIG_BLE_MESH_PROXY_SOLIC
  518. if (proxy_solic) {
  519. bt_mesh_create_proxy_solic_nonce(nonce, buf->data, iv_index);
  520. } else
  521. #endif /* CONFIG_BLE_MESH_PROXY_SOLIC */
  522. {
  523. create_proxy_nonce(nonce, buf->data, iv_index);
  524. }
  525. } else {
  526. create_net_nonce(nonce, buf->data, iv_index);
  527. }
  528. #else /* CONFIG_BLE_MESH_PROXY */
  529. create_net_nonce(nonce, buf->data, iv_index);
  530. #endif /* CONFIG_BLE_MESH_PROXY */
  531. BT_DBG("Nonce %s", bt_hex(nonce, 13));
  532. buf->len -= mic_len;
  533. return bt_mesh_ccm_decrypt(key, nonce, &buf->data[7], buf->len - 7,
  534. NULL, 0, &buf->data[7], mic_len);
  535. }
  536. static void create_app_nonce(uint8_t nonce[13], bool dev_key, uint8_t aszmic,
  537. uint16_t src, uint16_t dst, uint32_t seq_num,
  538. uint32_t iv_index)
  539. {
  540. if (dev_key) {
  541. nonce[0] = 0x02;
  542. } else {
  543. nonce[0] = 0x01;
  544. }
  545. sys_put_be32((seq_num | ((uint32_t)aszmic << 31)), &nonce[1]);
  546. sys_put_be16(src, &nonce[5]);
  547. sys_put_be16(dst, &nonce[7]);
  548. sys_put_be32(iv_index, &nonce[9]);
  549. }
  550. int bt_mesh_app_encrypt(const uint8_t key[16], bool dev_key, uint8_t aszmic,
  551. struct net_buf_simple *buf, const uint8_t *ad,
  552. uint16_t src, uint16_t dst, uint32_t seq_num, uint32_t iv_index)
  553. {
  554. uint8_t nonce[13] = {0};
  555. int err = 0;
  556. BT_DBG("AppKey %s", bt_hex(key, 16));
  557. BT_DBG("dev_key %u src 0x%04x dst 0x%04x", dev_key, src, dst);
  558. BT_DBG("seq_num 0x%08x iv_index 0x%08x", seq_num, iv_index);
  559. BT_DBG("Clear: %s", bt_hex(buf->data, buf->len));
  560. create_app_nonce(nonce, dev_key, aszmic, src, dst, seq_num, iv_index);
  561. BT_DBG("Nonce %s", bt_hex(nonce, 13));
  562. err = bt_mesh_ccm_encrypt(key, nonce, buf->data, buf->len, ad,
  563. ad ? 16 : 0, buf->data, APP_MIC_LEN(aszmic));
  564. if (!err) {
  565. net_buf_simple_add(buf, APP_MIC_LEN(aszmic));
  566. BT_DBG("Encr: %s", bt_hex(buf->data, buf->len));
  567. }
  568. return err;
  569. }
  570. int bt_mesh_app_decrypt(const uint8_t key[16], bool dev_key, uint8_t aszmic,
  571. struct net_buf_simple *buf, struct net_buf_simple *out,
  572. const uint8_t *ad, uint16_t src, uint16_t dst, uint32_t seq_num,
  573. uint32_t iv_index)
  574. {
  575. uint8_t nonce[13] = {0};
  576. int err = 0;
  577. BT_DBG("EncData (len %u) %s", buf->len, bt_hex(buf->data, buf->len));
  578. create_app_nonce(nonce, dev_key, aszmic, src, dst, seq_num, iv_index);
  579. BT_DBG("AppKey %s", bt_hex(key, 16));
  580. BT_DBG("Nonce %s", bt_hex(nonce, 13));
  581. err = bt_mesh_ccm_decrypt(key, nonce, buf->data, buf->len, ad,
  582. ad ? 16 : 0, out->data, APP_MIC_LEN(aszmic));
  583. if (!err) {
  584. net_buf_simple_add(out, buf->len);
  585. }
  586. return err;
  587. }
  588. /* reversed, 8-bit, poly=0x07 */
  589. static const uint8_t crc_table[256] = {
  590. 0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75,
  591. 0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b,
  592. 0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69,
  593. 0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67,
  594. 0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d,
  595. 0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43,
  596. 0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51,
  597. 0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f,
  598. 0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05,
  599. 0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b,
  600. 0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19,
  601. 0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17,
  602. 0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d,
  603. 0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33,
  604. 0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21,
  605. 0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f,
  606. 0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95,
  607. 0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b,
  608. 0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89,
  609. 0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87,
  610. 0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad,
  611. 0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3,
  612. 0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1,
  613. 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf,
  614. 0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5,
  615. 0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb,
  616. 0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9,
  617. 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7,
  618. 0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd,
  619. 0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3,
  620. 0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1,
  621. 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf
  622. };
  623. uint8_t bt_mesh_fcs_calc(const uint8_t *data, uint8_t data_len)
  624. {
  625. uint8_t fcs = 0xff;
  626. while (data_len--) {
  627. fcs = crc_table[fcs ^ *data++];
  628. }
  629. BT_DBG("fcs 0x%02x", 0xff - fcs);
  630. return 0xff - fcs;
  631. }
  632. bool bt_mesh_fcs_check(struct net_buf_simple *buf, uint8_t received_fcs)
  633. {
  634. const uint8_t *data = buf->data;
  635. uint16_t data_len = buf->len;
  636. uint8_t fcs = 0xff;
  637. while (data_len--) {
  638. fcs = crc_table[fcs ^ *data++];
  639. }
  640. return crc_table[fcs ^ received_fcs] == 0xcf;
  641. }
  642. int bt_mesh_virtual_addr(const uint8_t virtual_label[16], uint16_t *addr)
  643. {
  644. uint8_t salt[16] = {0};
  645. uint8_t tmp[16] = {0};
  646. int err = 0;
  647. err = bt_mesh_s1("vtad", salt);
  648. if (err) {
  649. return err;
  650. }
  651. err = bt_mesh_aes_cmac_one(salt, virtual_label, 16, tmp);
  652. if (err) {
  653. return err;
  654. }
  655. *addr = (sys_get_be16(&tmp[14]) & 0x3fff) | 0x8000;
  656. return 0;
  657. }
  658. int bt_mesh_prov_conf_salt(const uint8_t conf_inputs[145], uint8_t salt[16])
  659. {
  660. const uint8_t conf_salt_key[16] = { 0 };
  661. return bt_mesh_aes_cmac_one(conf_salt_key, conf_inputs, 145, salt);
  662. }
  663. int bt_mesh_prov_conf_key(const uint8_t dhkey[32], const uint8_t conf_salt[16],
  664. uint8_t conf_key[16])
  665. {
  666. return bt_mesh_k1(dhkey, 32, conf_salt, "prck", conf_key);
  667. }
  668. int bt_mesh_prov_conf(const uint8_t conf_key[16], const uint8_t rand[16],
  669. const uint8_t auth[16], uint8_t conf[16])
  670. {
  671. struct bt_mesh_sg sg[] = { { rand, 16 }, { auth, 16 } };
  672. BT_DBG("ConfirmationKey %s", bt_hex(conf_key, 16));
  673. BT_DBG("RandomDevice %s", bt_hex(rand, 16));
  674. BT_DBG("AuthValue %s", bt_hex(auth, 16));
  675. return bt_mesh_aes_cmac(conf_key, sg, ARRAY_SIZE(sg), conf);
  676. }
  677. int bt_mesh_prov_decrypt(const uint8_t key[16], uint8_t nonce[13],
  678. const uint8_t data[25 + 8], uint8_t out[25])
  679. {
  680. return bt_mesh_ccm_decrypt(key, nonce, data, 25, NULL, 0, out, 8);
  681. }
  682. #if CONFIG_BLE_MESH_PROVISIONER
  683. int bt_mesh_prov_encrypt(const uint8_t key[16], uint8_t nonce[13],
  684. const uint8_t data[25], uint8_t out[33])
  685. {
  686. return bt_mesh_ccm_encrypt(key, nonce, data, 25, NULL, 0, out, 8);
  687. }
  688. #endif
  689. int bt_mesh_secure_beacon_auth(const uint8_t beacon_key[16], uint8_t flags,
  690. const uint8_t net_id[8], uint32_t iv_index,
  691. uint8_t auth[8])
  692. {
  693. uint8_t msg[13] = {0}, tmp[16] = {0};
  694. int err = 0;
  695. BT_DBG("BeaconKey %s", bt_hex(beacon_key, 16));
  696. BT_DBG("NetId %s", bt_hex(net_id, 8));
  697. BT_DBG("IV Index 0x%08x", iv_index);
  698. msg[0] = flags;
  699. memcpy(&msg[1], net_id, 8);
  700. sys_put_be32(iv_index, &msg[9]);
  701. BT_DBG("BeaconMsg %s", bt_hex(msg, sizeof(msg)));
  702. err = bt_mesh_aes_cmac_one(beacon_key, msg, sizeof(msg), tmp);
  703. if (!err) {
  704. memcpy(auth, tmp, 8);
  705. }
  706. return err;
  707. }