crypto.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2017 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include <sys/socket.h>
  17. #include <linux/if_alg.h>
  18. #include <glib.h>
  19. #ifndef SOL_ALG
  20. #define SOL_ALG 279
  21. #endif
  22. #ifndef ALG_SET_AEAD_AUTHSIZE
  23. #define ALG_SET_AEAD_AUTHSIZE 5
  24. #endif
  25. #include "src/shared/util.h"
  26. #include "tools/mesh-gatt/mesh-net.h"
  27. #include "tools/mesh-gatt/crypto.h"
  28. static int alg_new(int fd, const void *keyval, socklen_t keylen,
  29. size_t mic_size)
  30. {
  31. if (setsockopt(fd, SOL_ALG, ALG_SET_KEY, keyval, keylen) < 0) {
  32. g_printerr("key");
  33. return -1;
  34. }
  35. if (mic_size &&
  36. setsockopt(fd, SOL_ALG,
  37. ALG_SET_AEAD_AUTHSIZE, NULL, mic_size) < 0) {
  38. g_printerr("taglen");
  39. return -1;
  40. }
  41. /* FIXME: This should use accept4() with SOCK_CLOEXEC */
  42. return accept(fd, NULL, 0);
  43. }
  44. static bool alg_encrypt(int fd, const void *inbuf, size_t inlen,
  45. void *outbuf, size_t outlen)
  46. {
  47. __u32 alg_op = ALG_OP_ENCRYPT;
  48. char cbuf[CMSG_SPACE(sizeof(alg_op))];
  49. struct cmsghdr *cmsg;
  50. struct msghdr msg;
  51. struct iovec iov;
  52. ssize_t len;
  53. memset(cbuf, 0, sizeof(cbuf));
  54. memset(&msg, 0, sizeof(msg));
  55. msg.msg_control = cbuf;
  56. msg.msg_controllen = sizeof(cbuf);
  57. cmsg = CMSG_FIRSTHDR(&msg);
  58. cmsg->cmsg_level = SOL_ALG;
  59. cmsg->cmsg_type = ALG_SET_OP;
  60. cmsg->cmsg_len = CMSG_LEN(sizeof(alg_op));
  61. memcpy(CMSG_DATA(cmsg), &alg_op, sizeof(alg_op));
  62. iov.iov_base = (void *) inbuf;
  63. iov.iov_len = inlen;
  64. msg.msg_iov = &iov;
  65. msg.msg_iovlen = 1;
  66. len = sendmsg(fd, &msg, 0);
  67. if (len < 0)
  68. return false;
  69. len = read(fd, outbuf, outlen);
  70. if (len < 0)
  71. return false;
  72. return true;
  73. }
  74. static int aes_ecb_setup(const uint8_t key[16])
  75. {
  76. struct sockaddr_alg salg;
  77. int fd, nfd;
  78. fd = socket(PF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
  79. if (fd < 0)
  80. return -1;
  81. memset(&salg, 0, sizeof(salg));
  82. salg.salg_family = AF_ALG;
  83. strcpy((char *) salg.salg_type, "skcipher");
  84. strcpy((char *) salg.salg_name, "ecb(aes)");
  85. if (bind(fd, (struct sockaddr *) &salg, sizeof(salg)) < 0) {
  86. close(fd);
  87. return -1;
  88. }
  89. nfd = alg_new(fd, key, 16, 0);
  90. close(fd);
  91. return nfd;
  92. }
  93. static bool aes_ecb(int fd, const uint8_t plaintext[16], uint8_t encrypted[16])
  94. {
  95. return alg_encrypt(fd, plaintext, 16, encrypted, 16);
  96. }
  97. static void aes_ecb_destroy(int fd)
  98. {
  99. close(fd);
  100. }
  101. static bool aes_ecb_one(const uint8_t key[16],
  102. const uint8_t plaintext[16], uint8_t encrypted[16])
  103. {
  104. bool result;
  105. int fd;
  106. fd = aes_ecb_setup(key);
  107. if (fd < 0)
  108. return false;
  109. result = aes_ecb(fd, plaintext, encrypted);
  110. aes_ecb_destroy(fd);
  111. return result;
  112. }
  113. /* Maximum message length that can be passed to aes_cmac */
  114. #define CMAC_MSG_MAX (64 + 64 + 17)
  115. static int aes_cmac_setup(const uint8_t key[16])
  116. {
  117. struct sockaddr_alg salg;
  118. int fd, nfd;
  119. fd = socket(PF_ALG, SOCK_SEQPACKET | SOCK_CLOEXEC, 0);
  120. if (fd < 0)
  121. return -1;
  122. memset(&salg, 0, sizeof(salg));
  123. salg.salg_family = AF_ALG;
  124. strcpy((char *) salg.salg_type, "hash");
  125. strcpy((char *) salg.salg_name, "cmac(aes)");
  126. if (bind(fd, (struct sockaddr *) &salg, sizeof(salg)) < 0) {
  127. close(fd);
  128. return -1;
  129. }
  130. nfd = alg_new(fd, key, 16, 0);
  131. close(fd);
  132. return nfd;
  133. }
  134. static bool aes_cmac(int fd, const uint8_t *msg,
  135. size_t msg_len, uint8_t res[16])
  136. {
  137. ssize_t len;
  138. if (msg_len > CMAC_MSG_MAX)
  139. return false;
  140. len = send(fd, msg, msg_len, 0);
  141. if (len < 0)
  142. return false;
  143. len = read(fd, res, 16);
  144. if (len < 0)
  145. return false;
  146. return true;
  147. }
  148. static void aes_cmac_destroy(int fd)
  149. {
  150. close(fd);
  151. }
  152. static int aes_cmac_N_start(const uint8_t N[16])
  153. {
  154. int fd;
  155. fd = aes_cmac_setup(N);
  156. return fd;
  157. }
  158. static bool aes_cmac_one(const uint8_t key[16], const void *msg,
  159. size_t msg_len, uint8_t res[16])
  160. {
  161. bool result;
  162. int fd;
  163. fd = aes_cmac_setup(key);
  164. if (fd < 0)
  165. return false;
  166. result = aes_cmac(fd, msg, msg_len, res);
  167. aes_cmac_destroy(fd);
  168. return result;
  169. }
  170. bool mesh_crypto_aes_cmac(const uint8_t key[16], const uint8_t *msg,
  171. size_t msg_len, uint8_t res[16])
  172. {
  173. return aes_cmac_one(key, msg, msg_len, res);
  174. }
  175. bool mesh_crypto_aes_ccm_encrypt(const uint8_t nonce[13], const uint8_t key[16],
  176. const uint8_t *aad, uint16_t aad_len,
  177. const uint8_t *msg, uint16_t msg_len,
  178. uint8_t *out_msg, void *out_mic,
  179. size_t mic_size)
  180. {
  181. uint8_t pmsg[16], cmic[16], cmsg[16];
  182. uint8_t mic[16], Xn[16];
  183. uint16_t blk_cnt, last_blk;
  184. bool result;
  185. size_t i, j;
  186. int fd;
  187. if (aad_len >= 0xff00) {
  188. g_printerr("Unsupported AAD size");
  189. return false;
  190. }
  191. fd = aes_ecb_setup(key);
  192. if (fd < 0)
  193. return false;
  194. /* C_mic = e(AppKey, 0x01 || nonce || 0x0000) */
  195. pmsg[0] = 0x01;
  196. memcpy(pmsg + 1, nonce, 13);
  197. put_be16(0x0000, pmsg + 14);
  198. result = aes_ecb(fd, pmsg, cmic);
  199. if (!result)
  200. goto done;
  201. /* X_0 = e(AppKey, 0x09 || nonce || length) */
  202. if (mic_size == sizeof(uint64_t))
  203. pmsg[0] = 0x19 | (aad_len ? 0x40 : 0x00);
  204. else
  205. pmsg[0] = 0x09 | (aad_len ? 0x40 : 0x00);
  206. memcpy(pmsg + 1, nonce, 13);
  207. put_be16(msg_len, pmsg + 14);
  208. result = aes_ecb(fd, pmsg, Xn);
  209. if (!result)
  210. goto done;
  211. /* If AAD is being used to authenticate, include it here */
  212. if (aad_len) {
  213. put_be16(aad_len, pmsg);
  214. for (i = 0; i < sizeof(uint16_t); i++)
  215. pmsg[i] = Xn[i] ^ pmsg[i];
  216. j = 0;
  217. aad_len += sizeof(uint16_t);
  218. while (aad_len > 16) {
  219. do {
  220. pmsg[i] = Xn[i] ^ aad[j];
  221. i++, j++;
  222. } while (i < 16);
  223. aad_len -= 16;
  224. i = 0;
  225. result = aes_ecb(fd, pmsg, Xn);
  226. if (!result)
  227. goto done;
  228. }
  229. for (i = 0; i < aad_len; i++, j++)
  230. pmsg[i] = Xn[i] ^ aad[j];
  231. for (i = aad_len; i < 16; i++)
  232. pmsg[i] = Xn[i];
  233. result = aes_ecb(fd, pmsg, Xn);
  234. if (!result)
  235. goto done;
  236. }
  237. last_blk = msg_len % 16;
  238. blk_cnt = (msg_len + 15) / 16;
  239. if (!last_blk)
  240. last_blk = 16;
  241. for (j = 0; j < blk_cnt; j++) {
  242. if (j + 1 == blk_cnt) {
  243. /* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */
  244. for (i = 0; i < last_blk; i++)
  245. pmsg[i] = Xn[i] ^ msg[(j * 16) + i];
  246. for (i = last_blk; i < 16; i++)
  247. pmsg[i] = Xn[i] ^ 0x00;
  248. result = aes_ecb(fd, pmsg, Xn);
  249. if (!result)
  250. goto done;
  251. /* MIC = C_mic ^ X_1 */
  252. for (i = 0; i < sizeof(mic); i++)
  253. mic[i] = cmic[i] ^ Xn[i];
  254. /* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */
  255. pmsg[0] = 0x01;
  256. memcpy(pmsg + 1, nonce, 13);
  257. put_be16(j + 1, pmsg + 14);
  258. result = aes_ecb(fd, pmsg, cmsg);
  259. if (!result)
  260. goto done;
  261. if (out_msg) {
  262. /* Encrypted = Payload[0-15] ^ C_1 */
  263. for (i = 0; i < last_blk; i++)
  264. out_msg[(j * 16) + i] =
  265. msg[(j * 16) + i] ^ cmsg[i];
  266. }
  267. } else {
  268. /* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */
  269. for (i = 0; i < 16; i++)
  270. pmsg[i] = Xn[i] ^ msg[(j * 16) + i];
  271. result = aes_ecb(fd, pmsg, Xn);
  272. if (!result)
  273. goto done;
  274. /* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */
  275. pmsg[0] = 0x01;
  276. memcpy(pmsg + 1, nonce, 13);
  277. put_be16(j + 1, pmsg + 14);
  278. result = aes_ecb(fd, pmsg, cmsg);
  279. if (!result)
  280. goto done;
  281. if (out_msg) {
  282. /* Encrypted = Payload[0-15] ^ C_N */
  283. for (i = 0; i < 16; i++)
  284. out_msg[(j * 16) + i] =
  285. msg[(j * 16) + i] ^ cmsg[i];
  286. }
  287. }
  288. }
  289. if (out_msg)
  290. memcpy(out_msg + msg_len, mic, mic_size);
  291. if (out_mic) {
  292. switch (mic_size) {
  293. case sizeof(uint32_t):
  294. *(uint32_t *)out_mic = get_be32(mic);
  295. break;
  296. case sizeof(uint64_t):
  297. *(uint64_t *)out_mic = get_be64(mic);
  298. break;
  299. default:
  300. g_printerr("Unsupported MIC size");
  301. }
  302. }
  303. done:
  304. aes_ecb_destroy(fd);
  305. return result;
  306. }
  307. bool mesh_crypto_aes_ccm_decrypt(const uint8_t nonce[13], const uint8_t key[16],
  308. const uint8_t *aad, uint16_t aad_len,
  309. const uint8_t *enc_msg, uint16_t enc_msg_len,
  310. uint8_t *out_msg, void *out_mic,
  311. size_t mic_size)
  312. {
  313. uint8_t msg[16], pmsg[16], cmic[16], cmsg[16], Xn[16];
  314. uint8_t mic[16];
  315. uint16_t msg_len = enc_msg_len - mic_size;
  316. uint16_t last_blk, blk_cnt;
  317. bool result;
  318. size_t i, j;
  319. int fd;
  320. if (enc_msg_len < 5 || aad_len >= 0xff00)
  321. return false;
  322. fd = aes_ecb_setup(key);
  323. if (fd < 0)
  324. return false;
  325. /* C_mic = e(AppKey, 0x01 || nonce || 0x0000) */
  326. pmsg[0] = 0x01;
  327. memcpy(pmsg + 1, nonce, 13);
  328. put_be16(0x0000, pmsg + 14);
  329. result = aes_ecb(fd, pmsg, cmic);
  330. if (!result)
  331. goto done;
  332. /* X_0 = e(AppKey, 0x09 || nonce || length) */
  333. if (mic_size == sizeof(uint64_t))
  334. pmsg[0] = 0x19 | (aad_len ? 0x40 : 0x00);
  335. else
  336. pmsg[0] = 0x09 | (aad_len ? 0x40 : 0x00);
  337. memcpy(pmsg + 1, nonce, 13);
  338. put_be16(msg_len, pmsg + 14);
  339. result = aes_ecb(fd, pmsg, Xn);
  340. if (!result)
  341. goto done;
  342. /* If AAD is being used to authenticate, include it here */
  343. if (aad_len) {
  344. put_be16(aad_len, pmsg);
  345. for (i = 0; i < sizeof(uint16_t); i++)
  346. pmsg[i] = Xn[i] ^ pmsg[i];
  347. j = 0;
  348. aad_len += sizeof(uint16_t);
  349. while (aad_len > 16) {
  350. do {
  351. pmsg[i] = Xn[i] ^ aad[j];
  352. i++, j++;
  353. } while (i < 16);
  354. aad_len -= 16;
  355. i = 0;
  356. result = aes_ecb(fd, pmsg, Xn);
  357. if (!result)
  358. goto done;
  359. }
  360. for (i = 0; i < aad_len; i++, j++)
  361. pmsg[i] = Xn[i] ^ aad[j];
  362. for (i = aad_len; i < 16; i++)
  363. pmsg[i] = Xn[i];
  364. result = aes_ecb(fd, pmsg, Xn);
  365. if (!result)
  366. goto done;
  367. }
  368. last_blk = msg_len % 16;
  369. blk_cnt = (msg_len + 15) / 16;
  370. if (!last_blk)
  371. last_blk = 16;
  372. for (j = 0; j < blk_cnt; j++) {
  373. if (j + 1 == blk_cnt) {
  374. /* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */
  375. pmsg[0] = 0x01;
  376. memcpy(pmsg + 1, nonce, 13);
  377. put_be16(j + 1, pmsg + 14);
  378. result = aes_ecb(fd, pmsg, cmsg);
  379. if (!result)
  380. goto done;
  381. /* Encrypted = Payload[0-15] ^ C_1 */
  382. for (i = 0; i < last_blk; i++)
  383. msg[i] = enc_msg[(j * 16) + i] ^ cmsg[i];
  384. if (out_msg)
  385. memcpy(out_msg + (j * 16), msg, last_blk);
  386. /* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */
  387. for (i = 0; i < last_blk; i++)
  388. pmsg[i] = Xn[i] ^ msg[i];
  389. for (i = last_blk; i < 16; i++)
  390. pmsg[i] = Xn[i] ^ 0x00;
  391. result = aes_ecb(fd, pmsg, Xn);
  392. if (!result)
  393. goto done;
  394. /* MIC = C_mic ^ X_1 */
  395. for (i = 0; i < sizeof(mic); i++)
  396. mic[i] = cmic[i] ^ Xn[i];
  397. } else {
  398. /* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */
  399. pmsg[0] = 0x01;
  400. memcpy(pmsg + 1, nonce, 13);
  401. put_be16(j + 1, pmsg + 14);
  402. result = aes_ecb(fd, pmsg, cmsg);
  403. if (!result)
  404. goto done;
  405. /* Encrypted = Payload[0-15] ^ C_1 */
  406. for (i = 0; i < 16; i++)
  407. msg[i] = enc_msg[(j * 16) + i] ^ cmsg[i];
  408. if (out_msg)
  409. memcpy(out_msg + (j * 16), msg, 16);
  410. /* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */
  411. for (i = 0; i < 16; i++)
  412. pmsg[i] = Xn[i] ^ msg[i];
  413. result = aes_ecb(fd, pmsg, Xn);
  414. if (!result)
  415. goto done;
  416. }
  417. }
  418. switch (mic_size) {
  419. case sizeof(uint32_t):
  420. if (out_mic)
  421. *(uint32_t *)out_mic = get_be32(mic);
  422. else if (get_be32(enc_msg + enc_msg_len - mic_size) !=
  423. get_be32(mic))
  424. result = false;
  425. break;
  426. case sizeof(uint64_t):
  427. if (out_mic)
  428. *(uint64_t *)out_mic = get_be64(mic);
  429. else if (get_be64(enc_msg + enc_msg_len - mic_size) !=
  430. get_be64(mic))
  431. result = false;
  432. break;
  433. default:
  434. g_printerr("Unsupported MIC size");
  435. result = false;
  436. }
  437. done:
  438. aes_ecb_destroy(fd);
  439. return result;
  440. }
  441. bool mesh_crypto_k1(const uint8_t ikm[16], const uint8_t salt[16],
  442. const void *info, size_t info_len, uint8_t okm[16])
  443. {
  444. uint8_t res[16];
  445. if (!aes_cmac_one(salt, ikm, 16, res))
  446. return false;
  447. return aes_cmac_one(res, info, info_len, okm);
  448. }
  449. bool mesh_crypto_k2(const uint8_t n[16], const uint8_t *p, size_t p_len,
  450. uint8_t net_id[1],
  451. uint8_t enc_key[16],
  452. uint8_t priv_key[16])
  453. {
  454. int fd;
  455. uint8_t output[16];
  456. uint8_t t[16];
  457. uint8_t *stage;
  458. bool success = false;
  459. stage = g_malloc(sizeof(output) + p_len + 1);
  460. if (stage == NULL)
  461. return false;
  462. if (!mesh_crypto_s1("smk2", 4, stage))
  463. goto fail;
  464. if (!aes_cmac_one(stage, n, 16, t))
  465. goto fail;
  466. fd = aes_cmac_N_start(t);
  467. if (fd < 0)
  468. goto fail;
  469. memcpy(stage, p, p_len);
  470. stage[p_len] = 1;
  471. if(!aes_cmac(fd, stage, p_len + 1, output))
  472. goto done;
  473. net_id[0] = output[15] & 0x7f;
  474. memcpy(stage, output, 16);
  475. memcpy(stage + 16, p, p_len);
  476. stage[p_len + 16] = 2;
  477. if(!aes_cmac(fd, stage, p_len + 16 + 1, output))
  478. goto done;
  479. memcpy(enc_key, output, 16);
  480. memcpy(stage, output, 16);
  481. memcpy(stage + 16, p, p_len);
  482. stage[p_len + 16] = 3;
  483. if(!aes_cmac(fd, stage, p_len + 16 + 1, output))
  484. goto done;
  485. memcpy(priv_key, output, 16);
  486. success = true;
  487. done:
  488. aes_cmac_destroy(fd);
  489. fail:
  490. g_free(stage);
  491. return success;
  492. }
  493. static bool crypto_128(const uint8_t n[16], const char *s, uint8_t out128[16])
  494. {
  495. uint8_t id128[] = { 'i', 'd', '1', '2', '8', 0x01 };
  496. uint8_t salt[16];
  497. if (!mesh_crypto_s1(s, 4, salt))
  498. return false;
  499. return mesh_crypto_k1(n, salt, id128, sizeof(id128), out128);
  500. }
  501. bool mesh_crypto_nkik(const uint8_t n[16], uint8_t identity_key[16])
  502. {
  503. return crypto_128(n, "nkik", identity_key);
  504. }
  505. static bool identity_calc(const uint8_t net_key[16], uint16_t addr,
  506. bool check, uint8_t id[16])
  507. {
  508. uint8_t id_key[16];
  509. uint8_t tmp[16];
  510. if (!mesh_crypto_nkik(net_key, id_key))
  511. return false;
  512. memset(tmp, 0, sizeof(tmp));
  513. put_be16(addr, tmp + 14);
  514. if (check) {
  515. memcpy(tmp + 6, id + 8, 8);
  516. } else {
  517. mesh_get_random_bytes(tmp + 6, 8);
  518. memcpy(id + 8, tmp + 6, 8);
  519. }
  520. if (!aes_ecb_one(id_key, tmp, tmp))
  521. return false;
  522. if (check)
  523. return (memcmp(id, tmp + 8, 8) == 0);
  524. memcpy(id, tmp + 8, 8);
  525. return true;
  526. }
  527. bool mesh_crypto_identity(const uint8_t net_key[16], uint16_t addr,
  528. uint8_t id[16])
  529. {
  530. return identity_calc(net_key, addr, false, id);
  531. }
  532. bool mesh_crypto_identity_check(const uint8_t net_key[16], uint16_t addr,
  533. uint8_t id[16])
  534. {
  535. return identity_calc(net_key, addr, true, id);
  536. }
  537. bool mesh_crypto_nkbk(const uint8_t n[16], uint8_t beacon_key[16])
  538. {
  539. return crypto_128(n, "nkbk", beacon_key);
  540. }
  541. bool mesh_crypto_k3(const uint8_t n[16], uint8_t out64[8])
  542. {
  543. uint8_t tmp[16];
  544. uint8_t t[16];
  545. uint8_t id64[] = { 'i', 'd', '6', '4', 0x01 };
  546. if (!mesh_crypto_s1("smk3", 4, tmp))
  547. return false;
  548. if (!aes_cmac_one(tmp, n, 16, t))
  549. return false;
  550. if (!aes_cmac_one(t, id64, sizeof(id64), tmp))
  551. return false;
  552. memcpy(out64, tmp + 8, 8);
  553. return true;
  554. }
  555. bool mesh_crypto_k4(const uint8_t a[16], uint8_t out6[1])
  556. {
  557. uint8_t tmp[16];
  558. uint8_t t[16];
  559. uint8_t id6[] = { 'i', 'd', '6', 0x01 };
  560. if (!mesh_crypto_s1("smk4", 4, tmp))
  561. return false;
  562. if (!aes_cmac_one(tmp, a, 16, t))
  563. return false;
  564. if (!aes_cmac_one(t, id6, sizeof(id6), tmp))
  565. return false;
  566. out6[0] = tmp[15] & 0x3f;
  567. return true;
  568. }
  569. bool mesh_crypto_beacon_cmac(const uint8_t encryption_key[16],
  570. const uint8_t network_id[8],
  571. uint32_t iv_index, bool kr, bool iu,
  572. uint64_t *cmac)
  573. {
  574. uint8_t msg[13], tmp[16];
  575. if (!cmac)
  576. return false;
  577. msg[0] = kr ? 0x01 : 0x00;
  578. msg[0] |= iu ? 0x02 : 0x00;
  579. memcpy(msg + 1, network_id, 8);
  580. put_be32(iv_index, msg + 9);
  581. if (!aes_cmac_one(encryption_key, msg, 13, tmp))
  582. return false;
  583. *cmac = get_be64(tmp);
  584. return true;
  585. }
  586. bool mesh_crypto_network_nonce(bool ctl, uint8_t ttl, uint32_t seq,
  587. uint16_t src, uint32_t iv_index,
  588. uint8_t nonce[13])
  589. {
  590. nonce[0] = 0;
  591. nonce[1] = (ttl & TTL_MASK) | (ctl ? CTL : 0x00);
  592. nonce[2] = (seq >> 16) & 0xff;
  593. nonce[3] = (seq >> 8) & 0xff;
  594. nonce[4] = seq & 0xff;
  595. /* SRC */
  596. put_be16(src, nonce + 5);
  597. put_be16(0, nonce + 7);
  598. /* IV Index */
  599. put_be32(iv_index, nonce + 9);
  600. return true;
  601. }
  602. bool mesh_crypto_network_encrypt(bool ctl, uint8_t ttl,
  603. uint32_t seq, uint16_t src,
  604. uint32_t iv_index,
  605. const uint8_t net_key[16],
  606. const uint8_t *enc_msg, uint8_t enc_msg_len,
  607. uint8_t *out, void *net_mic)
  608. {
  609. uint8_t nonce[13];
  610. if (!mesh_crypto_network_nonce(ctl, ttl, seq, src, iv_index, nonce))
  611. return false;
  612. return mesh_crypto_aes_ccm_encrypt(nonce, net_key,
  613. NULL, 0, enc_msg,
  614. enc_msg_len, out,
  615. net_mic,
  616. ctl ? sizeof(uint64_t) : sizeof(uint32_t));
  617. }
  618. bool mesh_crypto_network_decrypt(bool ctl, uint8_t ttl,
  619. uint32_t seq, uint16_t src,
  620. uint32_t iv_index,
  621. const uint8_t net_key[16],
  622. const uint8_t *enc_msg, uint8_t enc_msg_len,
  623. uint8_t *out, void *net_mic, size_t mic_size)
  624. {
  625. uint8_t nonce[13];
  626. if (!mesh_crypto_network_nonce(ctl, ttl, seq, src, iv_index, nonce))
  627. return false;
  628. return mesh_crypto_aes_ccm_decrypt(nonce, net_key, NULL, 0,
  629. enc_msg, enc_msg_len, out,
  630. net_mic, mic_size);
  631. }
  632. bool mesh_crypto_application_nonce(uint32_t seq, uint16_t src,
  633. uint16_t dst, uint32_t iv_index,
  634. bool aszmic, uint8_t nonce[13])
  635. {
  636. nonce[0] = 0x01;
  637. nonce[1] = aszmic ? 0x80 : 0x00;
  638. nonce[2] = (seq & 0x00ff0000) >> 16;
  639. nonce[3] = (seq & 0x0000ff00) >> 8;
  640. nonce[4] = (seq & 0x000000ff);
  641. nonce[5] = (src & 0xff00) >> 8;
  642. nonce[6] = (src & 0x00ff);
  643. nonce[7] = (dst & 0xff00) >> 8;
  644. nonce[8] = (dst & 0x00ff);
  645. put_be32(iv_index, nonce + 9);
  646. return true;
  647. }
  648. bool mesh_crypto_device_nonce(uint32_t seq, uint16_t src,
  649. uint16_t dst, uint32_t iv_index,
  650. bool aszmic, uint8_t nonce[13])
  651. {
  652. nonce[0] = 0x02;
  653. nonce[1] = aszmic ? 0x80 : 0x00;
  654. nonce[2] = (seq & 0x00ff0000) >> 16;
  655. nonce[3] = (seq & 0x0000ff00) >> 8;
  656. nonce[4] = (seq & 0x000000ff);
  657. nonce[5] = (src & 0xff00) >> 8;
  658. nonce[6] = (src & 0x00ff);
  659. nonce[7] = (dst & 0xff00) >> 8;
  660. nonce[8] = (dst & 0x00ff);
  661. put_be32(iv_index, nonce + 9);
  662. return true;
  663. }
  664. bool mesh_crypto_application_encrypt(uint8_t key_id, uint32_t seq, uint16_t src,
  665. uint16_t dst, uint32_t iv_index,
  666. const uint8_t app_key[16],
  667. const uint8_t *aad, uint8_t aad_len,
  668. const uint8_t *msg, uint8_t msg_len,
  669. uint8_t *out, void *app_mic,
  670. size_t mic_size)
  671. {
  672. uint8_t nonce[13];
  673. bool aszmic = (mic_size == sizeof(uint64_t)) ? true : false;
  674. if (!key_id && !mesh_crypto_device_nonce(seq, src, dst,
  675. iv_index, aszmic, nonce))
  676. return false;
  677. if (key_id && !mesh_crypto_application_nonce(seq, src, dst,
  678. iv_index, aszmic, nonce))
  679. return false;
  680. return mesh_crypto_aes_ccm_encrypt(nonce, app_key, aad, aad_len,
  681. msg, msg_len,
  682. out, app_mic, mic_size);
  683. }
  684. bool mesh_crypto_application_decrypt(uint8_t key_id, uint32_t seq, uint16_t src,
  685. uint16_t dst, uint32_t iv_index,
  686. const uint8_t app_key[16],
  687. const uint8_t *aad, uint8_t aad_len,
  688. const uint8_t *enc_msg, uint8_t enc_msg_len,
  689. uint8_t *out, void *app_mic, size_t mic_size)
  690. {
  691. uint8_t nonce[13];
  692. bool aszmic = (mic_size == sizeof(uint64_t)) ? true : false;
  693. if (!key_id && !mesh_crypto_device_nonce(seq, src, dst,
  694. iv_index, aszmic, nonce))
  695. return false;
  696. if (key_id && !mesh_crypto_application_nonce(seq, src, dst,
  697. iv_index, aszmic, nonce))
  698. return false;
  699. return mesh_crypto_aes_ccm_decrypt(nonce, app_key,
  700. aad, aad_len, enc_msg,
  701. enc_msg_len, out,
  702. app_mic, mic_size);
  703. }
  704. bool mesh_crypto_session_key(const uint8_t secret[32],
  705. const uint8_t salt[16],
  706. uint8_t session_key[16])
  707. {
  708. const uint8_t prsk[4] = "prsk";
  709. if (!aes_cmac_one(salt, secret, 32, session_key))
  710. return false;
  711. return aes_cmac_one(session_key, prsk, 4, session_key);
  712. }
  713. bool mesh_crypto_nonce(const uint8_t secret[32],
  714. const uint8_t salt[16],
  715. uint8_t nonce[13])
  716. {
  717. const uint8_t prsn[4] = "prsn";
  718. uint8_t tmp[16];
  719. bool result;
  720. if (!aes_cmac_one(salt, secret, 32, tmp))
  721. return false;
  722. result = aes_cmac_one(tmp, prsn, 4, tmp);
  723. if (result)
  724. memcpy(nonce, tmp + 3, 13);
  725. return result;
  726. }
  727. bool mesh_crypto_s1(const void *info, size_t len, uint8_t salt[16])
  728. {
  729. const uint8_t zero[16] = {0};
  730. return aes_cmac_one(zero, info, len, salt);
  731. }
  732. bool mesh_crypto_prov_prov_salt(const uint8_t conf_salt[16],
  733. const uint8_t prov_rand[16],
  734. const uint8_t dev_rand[16],
  735. uint8_t prov_salt[16])
  736. {
  737. const uint8_t zero[16] = {0};
  738. uint8_t tmp[16 * 3];
  739. memcpy(tmp, conf_salt, 16);
  740. memcpy(tmp + 16, prov_rand, 16);
  741. memcpy(tmp + 32, dev_rand, 16);
  742. return aes_cmac_one(zero, tmp, sizeof(tmp), prov_salt);
  743. }
  744. bool mesh_crypto_prov_conf_key(const uint8_t secret[32],
  745. const uint8_t salt[16],
  746. uint8_t conf_key[16])
  747. {
  748. const uint8_t prck[4] = "prck";
  749. if (!aes_cmac_one(salt, secret, 32, conf_key))
  750. return false;
  751. return aes_cmac_one(conf_key, prck, 4, conf_key);
  752. }
  753. bool mesh_crypto_device_key(const uint8_t secret[32],
  754. const uint8_t salt[16],
  755. uint8_t device_key[16])
  756. {
  757. const uint8_t prdk[4] = "prdk";
  758. if (!aes_cmac_one(salt, secret, 32, device_key))
  759. return false;
  760. return aes_cmac_one(device_key, prdk, 4, device_key);
  761. }
  762. bool mesh_crypto_virtual_addr(const uint8_t virtual_label[16],
  763. uint16_t *addr)
  764. {
  765. uint8_t tmp[16];
  766. if (!mesh_crypto_s1("vtad", 4, tmp))
  767. return false;
  768. if (!addr || !aes_cmac_one(tmp, virtual_label, 16, tmp))
  769. return false;
  770. *addr = (get_be16(tmp + 14) & 0x3fff) | 0x8000;
  771. return true;
  772. }
  773. bool mesh_crypto_packet_encode(uint8_t *packet, uint8_t packet_len,
  774. const uint8_t network_key[16],
  775. uint32_t iv_index,
  776. const uint8_t privacy_key[16])
  777. {
  778. uint8_t network_nonce[13] = { 0x00, 0x00 };
  779. uint8_t privacy_counter[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, };
  780. uint8_t tmp[16];
  781. int i;
  782. /* Detect Proxy packet by CTL == true && DST == 0x0000 */
  783. if ((packet[1] & CTL) && get_be16(packet + 7) == 0)
  784. network_nonce[0] = 0x03;
  785. else
  786. /* CTL + TTL */
  787. network_nonce[1] = packet[1];
  788. /* Seq Num */
  789. network_nonce[2] = packet[2];
  790. network_nonce[3] = packet[3];
  791. network_nonce[4] = packet[4];
  792. /* SRC */
  793. network_nonce[5] = packet[5];
  794. network_nonce[6] = packet[6];
  795. /* DST not available */
  796. network_nonce[7] = 0;
  797. network_nonce[8] = 0;
  798. /* IV Index */
  799. put_be32(iv_index, network_nonce + 9);
  800. /* Check for Long net-MIC */
  801. if (packet[1] & CTL) {
  802. if (!mesh_crypto_aes_ccm_encrypt(network_nonce, network_key,
  803. NULL, 0,
  804. packet + 7, packet_len - 7 - 8,
  805. packet + 7, NULL, sizeof(uint64_t)))
  806. return false;
  807. } else {
  808. if (!mesh_crypto_aes_ccm_encrypt(network_nonce, network_key,
  809. NULL, 0,
  810. packet + 7, packet_len - 7 - 4,
  811. packet + 7, NULL, sizeof(uint32_t)))
  812. return false;
  813. }
  814. put_be32(iv_index, privacy_counter + 5);
  815. memcpy(privacy_counter + 9, packet + 7, 7);
  816. if (!aes_ecb_one(privacy_key, privacy_counter, tmp))
  817. return false;
  818. for (i = 0; i < 6; i++)
  819. packet[1 + i] ^= tmp[i];
  820. return true;
  821. }
  822. bool mesh_crypto_packet_decode(const uint8_t *packet, uint8_t packet_len,
  823. bool proxy, uint8_t *out, uint32_t iv_index,
  824. const uint8_t network_key[16],
  825. const uint8_t privacy_key[16])
  826. {
  827. uint8_t privacy_counter[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, };
  828. uint8_t network_nonce[13] = { 0x00, 0x00, };
  829. uint8_t tmp[16];
  830. uint16_t src;
  831. int i;
  832. if (packet_len < 14)
  833. return false;
  834. put_be32(iv_index, privacy_counter + 5);
  835. memcpy(privacy_counter + 9, packet + 7, 7);
  836. if (!aes_ecb_one(privacy_key, privacy_counter, tmp))
  837. return false;
  838. memcpy(out, packet, packet_len);
  839. for (i = 0; i < 6; i++)
  840. out[1 + i] ^= tmp[i];
  841. src = get_be16(out + 5);
  842. /* Pre-check SRC address for illegal values */
  843. if (!src || src >= 0x8000)
  844. return false;
  845. /* Detect Proxy packet by CTL == true && proxy == true */
  846. if ((out[1] & CTL) && proxy)
  847. network_nonce[0] = 0x03;
  848. else
  849. /* CTL + TTL */
  850. network_nonce[1] = out[1];
  851. /* Seq Num */
  852. network_nonce[2] = out[2];
  853. network_nonce[3] = out[3];
  854. network_nonce[4] = out[4];
  855. /* SRC */
  856. network_nonce[5] = out[5];
  857. network_nonce[6] = out[6];
  858. /* DST not available */
  859. network_nonce[7] = 0;
  860. network_nonce[8] = 0;
  861. /* IV Index */
  862. put_be32(iv_index, network_nonce + 9);
  863. /* Check for Long MIC */
  864. if (out[1] & CTL) {
  865. uint64_t mic;
  866. if (!mesh_crypto_aes_ccm_decrypt(network_nonce, network_key,
  867. NULL, 0, packet + 7, packet_len - 7,
  868. out + 7, &mic, sizeof(mic)))
  869. return false;
  870. mic ^= get_be64(out + packet_len - 8);
  871. put_be64(mic, out + packet_len - 8);
  872. if (mic)
  873. return false;
  874. } else {
  875. uint32_t mic;
  876. if (!mesh_crypto_aes_ccm_decrypt(network_nonce, network_key,
  877. NULL, 0, packet + 7, packet_len - 7,
  878. out + 7, &mic, sizeof(mic)))
  879. return false;
  880. mic ^= get_be32(out + packet_len - 4);
  881. put_be32(mic, out + packet_len - 4);
  882. if (mic)
  883. return false;
  884. }
  885. return true;
  886. }
  887. bool mesh_get_random_bytes(void *buf, size_t num_bytes)
  888. {
  889. ssize_t len;
  890. int fd;
  891. fd = open("/dev/urandom", O_RDONLY);
  892. if (fd < 0)
  893. return false;
  894. len = read(fd, buf, num_bytes);
  895. close(fd);
  896. if (len < 0)
  897. return false;
  898. return true;
  899. }