crypto.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2018-2019 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <unistd.h>
  15. #include <sys/socket.h>
  16. #include <ell/ell.h>
  17. #include "mesh/mesh-defs.h"
  18. #include "mesh/net.h"
  19. #include "mesh/crypto.h"
  20. /* Multiply used Zero array */
  21. static const uint8_t zero[16] = { 0, };
  22. static bool aes_ecb_one(const uint8_t key[16], const uint8_t in[16],
  23. uint8_t out[16])
  24. {
  25. void *cipher;
  26. bool result = false;
  27. cipher = l_cipher_new(L_CIPHER_AES, key, 16);
  28. if (cipher) {
  29. result = l_cipher_encrypt(cipher, in, out, 16);
  30. l_cipher_free(cipher);
  31. }
  32. return result;
  33. }
  34. static bool aes_cmac(void *checksum, const uint8_t *msg,
  35. size_t msg_len, uint8_t res[16])
  36. {
  37. if (!l_checksum_update(checksum, msg, msg_len))
  38. return false;
  39. if (16 == l_checksum_get_digest(checksum, res, 16))
  40. return true;
  41. return false;
  42. }
  43. static bool aes_cmac_one(const uint8_t key[16], const void *msg,
  44. size_t msg_len, uint8_t res[16])
  45. {
  46. void *checksum;
  47. bool result;
  48. checksum = l_checksum_new_cmac_aes(key, 16);
  49. if (!checksum)
  50. return false;
  51. result = l_checksum_update(checksum, msg, msg_len);
  52. if (result) {
  53. ssize_t len = l_checksum_get_digest(checksum, res, 16);
  54. result = !!(len == 16);
  55. }
  56. l_checksum_free(checksum);
  57. return result;
  58. }
  59. bool mesh_crypto_aes_cmac(const uint8_t key[16], const uint8_t *msg,
  60. size_t msg_len, uint8_t res[16])
  61. {
  62. return aes_cmac_one(key, msg, msg_len, res);
  63. }
  64. bool mesh_crypto_aes_ccm_encrypt(const uint8_t nonce[13], const uint8_t key[16],
  65. const uint8_t *aad, uint16_t aad_len,
  66. const void *msg, uint16_t msg_len,
  67. void *out_msg,
  68. void *out_mic, size_t mic_size)
  69. {
  70. void *cipher;
  71. bool result;
  72. cipher = l_aead_cipher_new(L_AEAD_CIPHER_AES_CCM, key, 16, mic_size);
  73. result = l_aead_cipher_encrypt(cipher, msg, msg_len, aad, aad_len,
  74. nonce, 13, out_msg, msg_len + mic_size);
  75. if (result && out_mic) {
  76. if (mic_size == 4)
  77. *(uint32_t *)out_mic = l_get_be32(out_msg + msg_len);
  78. else
  79. *(uint64_t *)out_mic = l_get_be64(out_msg + msg_len);
  80. }
  81. l_aead_cipher_free(cipher);
  82. return result;
  83. }
  84. bool mesh_crypto_aes_ccm_decrypt(const uint8_t nonce[13], const uint8_t key[16],
  85. const uint8_t *aad, uint16_t aad_len,
  86. const void *enc_msg, uint16_t enc_msg_len,
  87. void *out_msg,
  88. void *out_mic, size_t mic_size)
  89. {
  90. void *cipher;
  91. bool result;
  92. size_t out_msg_len = enc_msg_len - mic_size;
  93. cipher = l_aead_cipher_new(L_AEAD_CIPHER_AES_CCM, key, 16, mic_size);
  94. result = l_aead_cipher_decrypt(cipher, enc_msg, enc_msg_len,
  95. aad, aad_len, nonce, 13,
  96. out_msg, out_msg_len);
  97. if (result && out_mic) {
  98. if (mic_size == 4)
  99. *(uint32_t *)out_mic =
  100. l_get_be32(enc_msg + enc_msg_len - mic_size);
  101. else
  102. *(uint64_t *)out_mic =
  103. l_get_be64(enc_msg + enc_msg_len - mic_size);
  104. }
  105. l_aead_cipher_free(cipher);
  106. return result;
  107. }
  108. bool mesh_crypto_k1(const uint8_t ikm[16], const uint8_t salt[16],
  109. const void *info, size_t info_len, uint8_t okm[16])
  110. {
  111. uint8_t res[16];
  112. if (!aes_cmac_one(salt, ikm, 16, res))
  113. return false;
  114. return aes_cmac_one(res, info, info_len, okm);
  115. }
  116. bool mesh_crypto_k2(const uint8_t n[16], const uint8_t *p, size_t p_len,
  117. uint8_t net_id[1],
  118. uint8_t enc_key[16],
  119. uint8_t priv_key[16])
  120. {
  121. void *checksum;
  122. uint8_t output[16];
  123. uint8_t t[16];
  124. uint8_t *stage;
  125. bool success = false;
  126. stage = l_malloc(sizeof(output) + p_len + 1);
  127. if (!stage)
  128. return false;
  129. if (!mesh_crypto_s1("smk2", 4, stage))
  130. goto fail;
  131. if (!aes_cmac_one(stage, n, 16, t))
  132. goto fail;
  133. checksum = l_checksum_new_cmac_aes(t, 16);
  134. if (!checksum)
  135. goto fail;
  136. memcpy(stage, p, p_len);
  137. stage[p_len] = 1;
  138. if (!aes_cmac(checksum, stage, p_len + 1, output))
  139. goto done;
  140. net_id[0] = output[15] & 0x7f;
  141. memcpy(stage, output, 16);
  142. memcpy(stage + 16, p, p_len);
  143. stage[p_len + 16] = 2;
  144. if (!aes_cmac(checksum, stage, p_len + 16 + 1, output))
  145. goto done;
  146. memcpy(enc_key, output, 16);
  147. memcpy(stage, output, 16);
  148. memcpy(stage + 16, p, p_len);
  149. stage[p_len + 16] = 3;
  150. if (!aes_cmac(checksum, stage, p_len + 16 + 1, output))
  151. goto done;
  152. memcpy(priv_key, output, 16);
  153. success = true;
  154. done:
  155. l_checksum_free(checksum);
  156. fail:
  157. l_free(stage);
  158. return success;
  159. }
  160. static bool crypto_128(const uint8_t n[16], const char *s, uint8_t out128[16])
  161. {
  162. const uint8_t id128[] = { 'i', 'd', '1', '2', '8', 0x01 };
  163. uint8_t salt[16];
  164. if (!mesh_crypto_s1(s, 4, salt))
  165. return false;
  166. return mesh_crypto_k1(n, salt, id128, sizeof(id128), out128);
  167. }
  168. bool mesh_crypto_nkik(const uint8_t n[16], uint8_t identity_key[16])
  169. {
  170. return crypto_128(n, "nkik", identity_key);
  171. }
  172. bool mesh_crypto_identity(const uint8_t net_key[16], uint16_t addr,
  173. uint8_t id[16])
  174. {
  175. uint8_t id_key[16];
  176. uint8_t tmp[16];
  177. if (!mesh_crypto_nkik(net_key, id_key))
  178. return false;
  179. if (!l_get_be64(id + 8))
  180. l_getrandom(id + 8, 8);
  181. memset(tmp, 0, sizeof(tmp));
  182. memcpy(tmp + 6, id + 8, 8);
  183. l_put_be16(addr, tmp + 14);
  184. if (!aes_ecb_one(id_key, tmp, tmp))
  185. return false;
  186. memcpy(id, tmp + 8, 8);
  187. return true;
  188. }
  189. bool mesh_crypto_nkbk(const uint8_t n[16], uint8_t beacon_key[16])
  190. {
  191. return crypto_128(n, "nkbk", beacon_key);
  192. }
  193. bool mesh_crypto_nkpk(const uint8_t n[16], uint8_t proxy_key[16])
  194. {
  195. return crypto_128(n, "nkpk", proxy_key);
  196. }
  197. bool mesh_crypto_k3(const uint8_t n[16], uint8_t out64[8])
  198. {
  199. const uint8_t id64[] = { 'i', 'd', '6', '4', 0x01 };
  200. uint8_t tmp[16];
  201. uint8_t t[16];
  202. if (!mesh_crypto_s1("smk3", 4, tmp))
  203. return false;
  204. if (!aes_cmac_one(tmp, n, 16, t))
  205. return false;
  206. if (!aes_cmac_one(t, id64, sizeof(id64), tmp))
  207. return false;
  208. memcpy(out64, tmp + 8, 8);
  209. return true;
  210. }
  211. bool mesh_crypto_k4(const uint8_t a[16], uint8_t out6[1])
  212. {
  213. const uint8_t id6[] = { 'i', 'd', '6', 0x01 };
  214. uint8_t tmp[16];
  215. uint8_t t[16];
  216. if (!mesh_crypto_s1("smk4", 4, tmp))
  217. return false;
  218. if (!aes_cmac_one(tmp, a, 16, t))
  219. return false;
  220. if (!aes_cmac_one(t, id6, sizeof(id6), tmp))
  221. return false;
  222. out6[0] = tmp[15] & 0x3f;
  223. return true;
  224. }
  225. bool mesh_crypto_beacon_cmac(const uint8_t encryption_key[16],
  226. const uint8_t network_id[8],
  227. uint32_t iv_index, bool kr, bool iu,
  228. uint64_t *cmac)
  229. {
  230. uint8_t msg[13], tmp[16];
  231. if (!cmac)
  232. return false;
  233. msg[0] = kr ? 0x01 : 0x00;
  234. msg[0] |= iu ? 0x02 : 0x00;
  235. memcpy(msg + 1, network_id, 8);
  236. l_put_be32(iv_index, msg + 9);
  237. if (!aes_cmac_one(encryption_key, msg, 13, tmp))
  238. return false;
  239. *cmac = l_get_be64(tmp);
  240. return true;
  241. }
  242. static void mesh_crypto_network_nonce(bool ctl, uint8_t ttl,
  243. uint32_t seq, uint16_t src,
  244. uint32_t iv_index, uint8_t nonce[13])
  245. {
  246. nonce[0] = 0x00;
  247. nonce[1] = (ttl & TTL_MASK) | (ctl ? CTL : 0x00);
  248. nonce[2] = (seq >> 16) & 0xff;
  249. nonce[3] = (seq >> 8) & 0xff;
  250. nonce[4] = seq & 0xff;
  251. l_put_be16(src, nonce + 5);
  252. l_put_be16(0, nonce + 7);
  253. l_put_be32(iv_index, nonce + 9);
  254. }
  255. static void mesh_crypto_application_nonce(uint32_t seq, uint16_t src,
  256. uint16_t dst, uint32_t iv_index,
  257. bool aszmic, uint8_t nonce[13])
  258. {
  259. nonce[0] = 0x01;
  260. nonce[1] = aszmic ? 0x80 : 0x00;
  261. nonce[2] = (seq >> 16) & 0xff;
  262. nonce[3] = (seq >> 8) & 0xff;
  263. nonce[4] = seq & 0xff;
  264. l_put_be16(src, nonce + 5);
  265. l_put_be16(dst, nonce + 7);
  266. l_put_be32(iv_index, nonce + 9);
  267. }
  268. static void mesh_crypto_device_nonce(uint32_t seq, uint16_t src,
  269. uint16_t dst, uint32_t iv_index,
  270. bool aszmic, uint8_t nonce[13])
  271. {
  272. nonce[0] = 0x02;
  273. nonce[1] = aszmic ? 0x80 : 0x00;
  274. nonce[2] = (seq >> 16) & 0xff;
  275. nonce[3] = (seq >> 8) & 0xff;
  276. nonce[4] = seq & 0xff;
  277. l_put_be16(src, nonce + 5);
  278. l_put_be16(dst, nonce + 7);
  279. l_put_be32(iv_index, nonce + 9);
  280. }
  281. static void mesh_crypto_proxy_nonce(uint32_t seq, uint16_t src,
  282. uint32_t iv_index, uint8_t nonce[13])
  283. {
  284. nonce[0] = 0x03;
  285. nonce[1] = 0;
  286. nonce[2] = (seq >> 16) & 0xff;
  287. nonce[3] = (seq >> 8) & 0xff;
  288. nonce[4] = seq & 0xff;
  289. l_put_be16(src, nonce + 5);
  290. l_put_be16(0, nonce + 7);
  291. l_put_be32(iv_index, nonce + 9);
  292. }
  293. bool mesh_crypto_session_key(const uint8_t secret[32],
  294. const uint8_t salt[16],
  295. uint8_t session_key[16])
  296. {
  297. const uint8_t prsk[4] = "prsk";
  298. if (!aes_cmac_one(salt, secret, 32, session_key))
  299. return false;
  300. return aes_cmac_one(session_key, prsk, 4, session_key);
  301. }
  302. bool mesh_crypto_nonce(const uint8_t secret[32],
  303. const uint8_t salt[16],
  304. uint8_t nonce[13])
  305. {
  306. const uint8_t prsn[4] = "prsn";
  307. uint8_t tmp[16];
  308. bool result;
  309. if (!aes_cmac_one(salt, secret, 32, tmp))
  310. return false;
  311. result = aes_cmac_one(tmp, prsn, 4, tmp);
  312. if (result)
  313. memcpy(nonce, tmp + 3, 13);
  314. return result;
  315. }
  316. bool mesh_crypto_s1(const void *info, size_t len, uint8_t salt[16])
  317. {
  318. return aes_cmac_one(zero, info, len, salt);
  319. }
  320. bool mesh_crypto_prov_prov_salt(const uint8_t conf_salt[16],
  321. const uint8_t prov_rand[16],
  322. const uint8_t dev_rand[16],
  323. uint8_t prov_salt[16])
  324. {
  325. uint8_t tmp[16 * 3];
  326. memcpy(tmp, conf_salt, 16);
  327. memcpy(tmp + 16, prov_rand, 16);
  328. memcpy(tmp + 32, dev_rand, 16);
  329. return aes_cmac_one(zero, tmp, sizeof(tmp), prov_salt);
  330. }
  331. bool mesh_crypto_prov_conf_key(const uint8_t secret[32],
  332. const uint8_t salt[16],
  333. uint8_t conf_key[16])
  334. {
  335. const uint8_t prck[4] = "prck";
  336. if (!aes_cmac_one(salt, secret, 32, conf_key))
  337. return false;
  338. return aes_cmac_one(conf_key, prck, 4, conf_key);
  339. }
  340. bool mesh_crypto_device_key(const uint8_t secret[32],
  341. const uint8_t salt[16],
  342. uint8_t device_key[16])
  343. {
  344. const uint8_t prdk[4] = "prdk";
  345. if (!aes_cmac_one(salt, secret, 32, device_key))
  346. return false;
  347. return aes_cmac_one(device_key, prdk, 4, device_key);
  348. }
  349. bool mesh_crypto_virtual_addr(const uint8_t virtual_label[16],
  350. uint16_t *addr)
  351. {
  352. uint8_t tmp[16];
  353. if (!mesh_crypto_s1("vtad", 4, tmp))
  354. return false;
  355. if (!addr || !aes_cmac_one(tmp, virtual_label, 16, tmp))
  356. return false;
  357. *addr = (l_get_be16(tmp + 14) & 0x3fff) | 0x8000;
  358. return true;
  359. }
  360. static void mesh_crypto_privacy_counter(uint32_t iv_index,
  361. const uint8_t *payload,
  362. uint8_t privacy_counter[16])
  363. {
  364. memset(privacy_counter, 0, 5);
  365. l_put_be32(iv_index, privacy_counter + 5);
  366. memcpy(privacy_counter + 9, payload, 7);
  367. }
  368. static bool mesh_crypto_pecb(const uint8_t privacy_key[16],
  369. uint32_t iv_index,
  370. const uint8_t *payload,
  371. uint8_t pecb[16])
  372. {
  373. mesh_crypto_privacy_counter(iv_index, payload, pecb);
  374. return aes_ecb_one(privacy_key, pecb, pecb);
  375. }
  376. static bool mesh_crypto_network_obfuscate(uint8_t *packet,
  377. const uint8_t privacy_key[16],
  378. uint32_t iv_index,
  379. bool ctl, uint8_t ttl,
  380. uint32_t seq, uint16_t src)
  381. {
  382. uint8_t pecb[16];
  383. uint8_t *net_hdr = packet + 1;
  384. int i;
  385. if (!mesh_crypto_pecb(privacy_key, iv_index, packet + 7, pecb))
  386. return false;
  387. l_put_be16(src, net_hdr + 4);
  388. l_put_be32(seq & SEQ_MASK, net_hdr);
  389. net_hdr[0] = ((!!ctl) << 7) | (ttl & TTL_MASK);
  390. for (i = 0; i < 6; i++)
  391. net_hdr[i] = pecb[i] ^ net_hdr[i];
  392. return true;
  393. }
  394. static bool mesh_crypto_network_clarify(uint8_t *packet,
  395. const uint8_t privacy_key[16],
  396. uint32_t iv_index,
  397. bool *ctl, uint8_t *ttl,
  398. uint32_t *seq, uint16_t *src)
  399. {
  400. uint8_t pecb[16];
  401. uint8_t *net_hdr = packet + 1;
  402. int i;
  403. if (!mesh_crypto_pecb(privacy_key, iv_index, packet + 7, pecb))
  404. return false;
  405. for (i = 0; i < 6; i++)
  406. net_hdr[i] = pecb[i] ^ net_hdr[i];
  407. *src = l_get_be16(net_hdr + 4);
  408. *seq = l_get_be32(net_hdr) & SEQ_MASK;
  409. *ttl = net_hdr[0] & TTL_MASK;
  410. *ctl = !!(net_hdr[0] & CTL);
  411. return true;
  412. }
  413. bool mesh_crypto_packet_build(bool ctl, uint8_t ttl,
  414. uint32_t seq,
  415. uint16_t src, uint16_t dst,
  416. uint8_t opcode,
  417. bool segmented, uint8_t key_aid,
  418. bool szmic, bool relay, uint16_t seqZero,
  419. uint8_t segO, uint8_t segN,
  420. const uint8_t *payload, uint8_t payload_len,
  421. uint8_t *packet, uint8_t *packet_len)
  422. {
  423. uint32_t hdr;
  424. size_t n;
  425. if (seq > SEQ_MASK)
  426. return false;
  427. packet[0] = 0;
  428. l_put_be32(seq, packet + 1);
  429. packet[1] = (ctl ? CTL : 0) | (ttl & TTL_MASK);
  430. l_put_be16(src, packet + 5);
  431. l_put_be16(dst, packet + 7);
  432. n = 9;
  433. if (!ctl) {
  434. hdr = segmented << SEG_HDR_SHIFT;
  435. hdr |= (key_aid & KEY_ID_MASK) << KEY_HDR_SHIFT;
  436. if (segmented) {
  437. hdr |= szmic << SZMIC_HDR_SHIFT;
  438. hdr |= (seqZero & SEQ_ZERO_MASK) << SEQ_ZERO_HDR_SHIFT;
  439. hdr |= (segO & SEG_MASK) << SEGO_HDR_SHIFT;
  440. hdr |= (segN & SEG_MASK) << SEGN_HDR_SHIFT;
  441. }
  442. l_put_be32(hdr, packet + n);
  443. /* Only first octet is valid for unsegmented messages */
  444. if (segmented)
  445. n += 4;
  446. else
  447. n += 1;
  448. memcpy(packet + n, payload, payload_len);
  449. l_put_be32(0x00000000, packet + payload_len + n);
  450. if (packet_len)
  451. *packet_len = payload_len + n + 4;
  452. } else {
  453. if ((opcode & OPCODE_MASK) != opcode)
  454. return false;
  455. hdr = opcode << KEY_HDR_SHIFT;
  456. l_put_be32(hdr, packet + n);
  457. n += 1;
  458. memcpy(packet + n, payload, payload_len);
  459. n += payload_len;
  460. l_put_be64(0x0000000000000000, packet + n);
  461. if (packet_len)
  462. *packet_len = n + 8;
  463. }
  464. return true;
  465. }
  466. static bool network_header_parse(const uint8_t *packet, uint8_t packet_len,
  467. bool *ctl, uint8_t *ttl, uint32_t *seq,
  468. uint16_t *src, uint16_t *dst)
  469. {
  470. if (packet_len < 10)
  471. return false;
  472. /* Try to keep bits in the order they exist within the packet */
  473. if (ctl)
  474. *ctl = !!(packet[1] & CTL);
  475. if (ttl)
  476. *ttl = packet[1] & TTL_MASK;
  477. if (seq)
  478. *seq = l_get_be32(packet + 1) & SEQ_MASK;
  479. if (src)
  480. *src = l_get_be16(packet + 5);
  481. if (dst)
  482. *dst = l_get_be16(packet + 7);
  483. return true;
  484. }
  485. bool mesh_crypto_packet_parse(const uint8_t *packet, uint8_t packet_len,
  486. bool *ctl, uint8_t *ttl, uint32_t *seq,
  487. uint16_t *src, uint16_t *dst,
  488. uint32_t *cookie, uint8_t *opcode,
  489. bool *segmented, uint8_t *key_aid,
  490. bool *szmic, bool *relay, uint16_t *seqZero,
  491. uint8_t *segO, uint8_t *segN,
  492. const uint8_t **payload, uint8_t *payload_len)
  493. {
  494. uint32_t hdr;
  495. uint16_t this_dst;
  496. bool is_segmented;
  497. if (!network_header_parse(packet, packet_len,
  498. ctl, ttl, seq, src, &this_dst))
  499. return false;
  500. if (dst)
  501. *dst = this_dst;
  502. hdr = l_get_be32(packet + 9);
  503. is_segmented = !!((hdr >> SEG_HDR_SHIFT) & true);
  504. if (segmented)
  505. *segmented = is_segmented;
  506. if (packet[1] & CTL) {
  507. uint8_t this_opcode = packet[9] & OPCODE_MASK;
  508. if (cookie)
  509. *cookie = l_get_be32(packet + 2) ^ packet[6];
  510. if (opcode)
  511. *opcode = this_opcode;
  512. if (this_dst && this_opcode == NET_OP_SEG_ACKNOWLEDGE) {
  513. if (relay)
  514. *relay = !!((hdr >> RELAY_HDR_SHIFT) & true);
  515. if (seqZero)
  516. *seqZero = (hdr >> SEQ_ZERO_HDR_SHIFT) &
  517. SEQ_ZERO_MASK;
  518. if (payload)
  519. *payload = packet + 9;
  520. if (payload_len)
  521. *payload_len = packet_len - 9;
  522. } else {
  523. if (payload)
  524. *payload = packet + 10;
  525. if (payload_len)
  526. *payload_len = packet_len - 10;
  527. }
  528. } else {
  529. if (cookie)
  530. *cookie = l_get_be32(packet + packet_len - 8);
  531. if (key_aid)
  532. *key_aid = (hdr >> KEY_HDR_SHIFT) & KEY_ID_MASK;
  533. if (is_segmented) {
  534. if (szmic)
  535. *szmic = !!((hdr >> SZMIC_HDR_SHIFT) & true);
  536. if (seqZero)
  537. *seqZero = (hdr >> SEQ_ZERO_HDR_SHIFT) &
  538. SEQ_ZERO_MASK;
  539. if (segO)
  540. *segO = (hdr >> SEGO_HDR_SHIFT) & SEG_MASK;
  541. if (segN)
  542. *segN = (hdr >> SEGN_HDR_SHIFT) & SEG_MASK;
  543. if (payload)
  544. *payload = packet + 13;
  545. if (payload_len)
  546. *payload_len = packet_len - 13;
  547. } else {
  548. if (payload)
  549. *payload = packet + 10;
  550. if (payload_len)
  551. *payload_len = packet_len - 10;
  552. }
  553. }
  554. return true;
  555. }
  556. bool mesh_crypto_payload_encrypt(uint8_t *aad, const uint8_t *payload,
  557. uint8_t *out, uint16_t payload_len,
  558. uint16_t src, uint16_t dst, uint8_t key_aid,
  559. uint32_t seq, uint32_t iv_index,
  560. bool aszmic,
  561. const uint8_t app_key[16])
  562. {
  563. uint8_t nonce[13];
  564. if (payload_len < 1)
  565. return false;
  566. if (key_aid == APP_AID_DEV)
  567. mesh_crypto_device_nonce(seq, src, dst, iv_index, aszmic,
  568. nonce);
  569. else
  570. mesh_crypto_application_nonce(seq, src, dst, iv_index, aszmic,
  571. nonce);
  572. if (!mesh_crypto_aes_ccm_encrypt(nonce, app_key,
  573. aad, aad ? 16 : 0,
  574. payload, payload_len,
  575. out, NULL,
  576. aszmic ? 8 : 4))
  577. return false;
  578. return true;
  579. }
  580. bool mesh_crypto_payload_decrypt(uint8_t *aad, uint16_t aad_len,
  581. const uint8_t *payload, uint16_t payload_len,
  582. bool aszmic,
  583. uint16_t src, uint16_t dst,
  584. uint8_t key_aid, uint32_t seq,
  585. uint32_t iv_index, uint8_t *out,
  586. const uint8_t app_key[16])
  587. {
  588. uint8_t nonce[13];
  589. uint32_t mic32;
  590. uint64_t mic64;
  591. if (payload_len < 5 || !out)
  592. return false;
  593. if (key_aid == APP_AID_DEV)
  594. mesh_crypto_device_nonce(seq, src, dst, iv_index, aszmic,
  595. nonce);
  596. else
  597. mesh_crypto_application_nonce(seq, src, dst, iv_index, aszmic,
  598. nonce);
  599. memcpy(out, payload, payload_len);
  600. if (aszmic) {
  601. if (!mesh_crypto_aes_ccm_decrypt(nonce, app_key,
  602. aad, aad_len,
  603. payload, payload_len,
  604. out, &mic64, sizeof(mic64)))
  605. return false;
  606. mic64 ^= l_get_be64(out + payload_len - 8);
  607. l_put_be64(mic64, out + payload_len - 8);
  608. if (mic64)
  609. return false;
  610. } else {
  611. if (!mesh_crypto_aes_ccm_decrypt(nonce, app_key,
  612. aad, aad_len,
  613. payload, payload_len,
  614. out, &mic32, sizeof(mic32)))
  615. return false;
  616. mic32 ^= l_get_be32(out + payload_len - 4);
  617. l_put_be32(mic32, out + payload_len - 4);
  618. if (mic32)
  619. return false;
  620. }
  621. return true;
  622. }
  623. static bool mesh_crypto_packet_encrypt(uint8_t *packet, uint8_t packet_len,
  624. const uint8_t network_key[16],
  625. uint32_t iv_index, bool proxy,
  626. bool ctl, uint8_t ttl, uint32_t seq,
  627. uint16_t src)
  628. {
  629. uint8_t nonce[13];
  630. /* Detect Proxy packet by CTL == true && DST == 0x0000 */
  631. if (ctl && proxy)
  632. mesh_crypto_proxy_nonce(seq, src, iv_index, nonce);
  633. else
  634. mesh_crypto_network_nonce(ctl, ttl, seq, src, iv_index, nonce);
  635. /* Check for Long net-MIC */
  636. if (ctl) {
  637. if (!mesh_crypto_aes_ccm_encrypt(nonce, network_key,
  638. NULL, 0,
  639. packet + 7, packet_len - 7 - 8,
  640. packet + 7, NULL, 8))
  641. return false;
  642. } else {
  643. if (!mesh_crypto_aes_ccm_encrypt(nonce, network_key,
  644. NULL, 0,
  645. packet + 7, packet_len - 7 - 4,
  646. packet + 7, NULL, 4))
  647. return false;
  648. }
  649. return true;
  650. }
  651. bool mesh_crypto_packet_encode(uint8_t *packet, uint8_t packet_len,
  652. uint32_t iv_index,
  653. const uint8_t network_key[16],
  654. const uint8_t privacy_key[16])
  655. {
  656. bool ctl;
  657. uint8_t ttl;
  658. uint32_t seq;
  659. uint16_t src;
  660. uint16_t dst;
  661. if (!network_header_parse(packet, packet_len,
  662. &ctl, &ttl, &seq, &src, &dst))
  663. return false;
  664. if (!mesh_crypto_packet_encrypt(packet, packet_len, network_key,
  665. iv_index, !dst,
  666. ctl, ttl, seq, src))
  667. return false;
  668. return mesh_crypto_network_obfuscate(packet, privacy_key, iv_index,
  669. ctl, ttl, seq, src);
  670. }
  671. static bool mesh_crypto_packet_decrypt(uint8_t *packet, uint8_t packet_len,
  672. const uint8_t network_key[16],
  673. uint32_t iv_index, bool proxy,
  674. bool ctl, uint8_t ttl, uint32_t seq,
  675. uint16_t src)
  676. {
  677. uint8_t nonce[13];
  678. /* Pre-check SRC address for illegal values */
  679. if (!IS_UNICAST(src))
  680. return false;
  681. /* Detect Proxy packet by CTL == true && proxy == true */
  682. if (ctl & proxy)
  683. mesh_crypto_proxy_nonce(seq, src, iv_index, nonce);
  684. else
  685. mesh_crypto_network_nonce(ctl, ttl, seq, src, iv_index, nonce);
  686. /* Check for Long MIC */
  687. if (ctl) {
  688. uint64_t mic;
  689. if (!mesh_crypto_aes_ccm_decrypt(nonce, network_key,
  690. NULL, 0,
  691. packet + 7, packet_len - 7,
  692. packet + 7, &mic, sizeof(mic)))
  693. return false;
  694. mic ^= l_get_be64(packet + packet_len - 8);
  695. l_put_be64(mic, packet + packet_len - 8);
  696. if (mic)
  697. return false;
  698. } else {
  699. uint32_t mic;
  700. if (!mesh_crypto_aes_ccm_decrypt(nonce, network_key,
  701. NULL, 0,
  702. packet + 7, packet_len - 7,
  703. packet + 7, &mic, sizeof(mic)))
  704. return false;
  705. mic ^= l_get_be32(packet + packet_len - 4);
  706. l_put_be32(mic, packet + packet_len - 4);
  707. if (mic)
  708. return false;
  709. }
  710. return true;
  711. }
  712. bool mesh_crypto_packet_decode(const uint8_t *packet, uint8_t packet_len,
  713. bool proxy, uint8_t *out, uint32_t iv_index,
  714. const uint8_t network_key[16],
  715. const uint8_t privacy_key[16])
  716. {
  717. bool ctl;
  718. uint8_t ttl;
  719. uint32_t seq;
  720. uint16_t src;
  721. if (packet_len < 14)
  722. return false;
  723. memcpy(out, packet, packet_len);
  724. if (!mesh_crypto_network_clarify(out, privacy_key, iv_index,
  725. &ctl, &ttl, &seq, &src))
  726. return false;
  727. return mesh_crypto_packet_decrypt(out, packet_len, network_key,
  728. iv_index, proxy,
  729. ctl, ttl, seq, src);
  730. }
  731. bool mesh_crypto_packet_label(uint8_t *packet, uint8_t packet_len,
  732. uint16_t iv_index, uint8_t network_id)
  733. {
  734. packet[0] = (iv_index & 0x0001) << 7 | (network_id & 0x7f);
  735. return true;
  736. }
  737. /* reversed, 8-bit, poly=0x07 */
  738. static const uint8_t crc_table[256] = {
  739. 0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75,
  740. 0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b,
  741. 0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69,
  742. 0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67,
  743. 0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d,
  744. 0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43,
  745. 0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51,
  746. 0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f,
  747. 0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05,
  748. 0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b,
  749. 0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19,
  750. 0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17,
  751. 0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d,
  752. 0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33,
  753. 0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21,
  754. 0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f,
  755. 0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95,
  756. 0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b,
  757. 0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89,
  758. 0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87,
  759. 0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad,
  760. 0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3,
  761. 0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1,
  762. 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf,
  763. 0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5,
  764. 0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb,
  765. 0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9,
  766. 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7,
  767. 0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd,
  768. 0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3,
  769. 0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1,
  770. 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf
  771. };
  772. uint8_t mesh_crypto_compute_fcs(const uint8_t *packet, uint8_t packet_len)
  773. {
  774. uint8_t fcs = 0xff;
  775. int i;
  776. for (i = 0; i < packet_len; i++)
  777. fcs = crc_table[fcs ^ packet[i]];
  778. return 0xff - fcs;
  779. }
  780. bool mesh_crypto_check_fcs(const uint8_t *packet, uint8_t packet_len,
  781. uint8_t received_fcs)
  782. {
  783. uint8_t fcs = 0xff;
  784. int i;
  785. for (i = 0; i < packet_len; i++)
  786. fcs = crc_table[fcs ^ packet[i]];
  787. fcs = crc_table[fcs ^ received_fcs];
  788. return fcs == 0xcf;
  789. }
  790. /* This function performs a quick-check of ELL and Kernel AEAD encryption.
  791. * Some kernel versions before v4.9 have a known AEAD bug. If the system
  792. * running this test is using a v4.8 or earlier kernel, a failure here is
  793. * likely unless AEAD encryption has been backported.
  794. */
  795. static const uint8_t crypto_test_result[] = {
  796. 0x75, 0x03, 0x7e, 0xe2, 0x89, 0x81, 0xbe, 0x59,
  797. 0xbc, 0xe6, 0xdd, 0x23, 0x63, 0x5b, 0x16, 0x61,
  798. 0xb7, 0x23, 0x92, 0xd4, 0x86, 0xee, 0x84, 0x29,
  799. 0x9a, 0x2a, 0xbf, 0x96
  800. };
  801. bool mesh_crypto_check_avail()
  802. {
  803. void *cipher;
  804. bool result;
  805. uint8_t i;
  806. union {
  807. struct {
  808. uint8_t key[16];
  809. uint8_t aad[16];
  810. uint8_t nonce[13];
  811. uint8_t data[20];
  812. uint8_t mic[8];
  813. } crypto;
  814. uint8_t bytes[73];
  815. } u;
  816. uint8_t out_msg[sizeof(u.crypto.data) + sizeof(u.crypto.mic)];
  817. l_debug("Testing Crypto");
  818. for (i = 0; i < sizeof(u); i++) {
  819. u.bytes[i] = 0x60 + i;
  820. }
  821. cipher = l_aead_cipher_new(L_AEAD_CIPHER_AES_CCM, u.crypto.key,
  822. sizeof(u.crypto.key), sizeof(u.crypto.mic));
  823. if (!cipher)
  824. return false;
  825. result = l_aead_cipher_encrypt(cipher,
  826. u.crypto.data, sizeof(u.crypto.data),
  827. u.crypto.aad, sizeof(u.crypto.aad),
  828. u.crypto.nonce, sizeof(u.crypto.nonce),
  829. out_msg, sizeof(out_msg));
  830. if (result)
  831. result = !memcmp(out_msg, crypto_test_result, sizeof(out_msg));
  832. l_aead_cipher_free(cipher);
  833. return result;
  834. }