auth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVMe over Fabrics DH-HMAC-CHAP authentication.
  4. * Copyright (c) 2020 Hannes Reinecke, SUSE Software Solutions.
  5. * All rights reserved.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/err.h>
  12. #include <crypto/hash.h>
  13. #include <linux/crc32.h>
  14. #include <linux/base64.h>
  15. #include <linux/ctype.h>
  16. #include <linux/random.h>
  17. #include <linux/nvme-auth.h>
  18. #include <asm/unaligned.h>
  19. #include "nvmet.h"
  20. int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
  21. bool set_ctrl)
  22. {
  23. unsigned char key_hash;
  24. char *dhchap_secret;
  25. if (sscanf(secret, "DHHC-1:%hhd:%*s", &key_hash) != 1)
  26. return -EINVAL;
  27. if (key_hash > 3) {
  28. pr_warn("Invalid DH-HMAC-CHAP hash id %d\n",
  29. key_hash);
  30. return -EINVAL;
  31. }
  32. if (key_hash > 0) {
  33. /* Validate selected hash algorithm */
  34. const char *hmac = nvme_auth_hmac_name(key_hash);
  35. if (!crypto_has_shash(hmac, 0, 0)) {
  36. pr_err("DH-HMAC-CHAP hash %s unsupported\n", hmac);
  37. return -ENOTSUPP;
  38. }
  39. }
  40. dhchap_secret = kstrdup(secret, GFP_KERNEL);
  41. if (!dhchap_secret)
  42. return -ENOMEM;
  43. if (set_ctrl) {
  44. kfree(host->dhchap_ctrl_secret);
  45. host->dhchap_ctrl_secret = strim(dhchap_secret);
  46. host->dhchap_ctrl_key_hash = key_hash;
  47. } else {
  48. kfree(host->dhchap_secret);
  49. host->dhchap_secret = strim(dhchap_secret);
  50. host->dhchap_key_hash = key_hash;
  51. }
  52. return 0;
  53. }
  54. int nvmet_setup_dhgroup(struct nvmet_ctrl *ctrl, u8 dhgroup_id)
  55. {
  56. const char *dhgroup_kpp;
  57. int ret = 0;
  58. pr_debug("%s: ctrl %d selecting dhgroup %d\n",
  59. __func__, ctrl->cntlid, dhgroup_id);
  60. if (ctrl->dh_tfm) {
  61. if (ctrl->dh_gid == dhgroup_id) {
  62. pr_debug("%s: ctrl %d reuse existing DH group %d\n",
  63. __func__, ctrl->cntlid, dhgroup_id);
  64. return 0;
  65. }
  66. crypto_free_kpp(ctrl->dh_tfm);
  67. ctrl->dh_tfm = NULL;
  68. ctrl->dh_gid = 0;
  69. }
  70. if (dhgroup_id == NVME_AUTH_DHGROUP_NULL)
  71. return 0;
  72. dhgroup_kpp = nvme_auth_dhgroup_kpp(dhgroup_id);
  73. if (!dhgroup_kpp) {
  74. pr_debug("%s: ctrl %d invalid DH group %d\n",
  75. __func__, ctrl->cntlid, dhgroup_id);
  76. return -EINVAL;
  77. }
  78. ctrl->dh_tfm = crypto_alloc_kpp(dhgroup_kpp, 0, 0);
  79. if (IS_ERR(ctrl->dh_tfm)) {
  80. pr_debug("%s: ctrl %d failed to setup DH group %d, err %ld\n",
  81. __func__, ctrl->cntlid, dhgroup_id,
  82. PTR_ERR(ctrl->dh_tfm));
  83. ret = PTR_ERR(ctrl->dh_tfm);
  84. ctrl->dh_tfm = NULL;
  85. ctrl->dh_gid = 0;
  86. } else {
  87. ctrl->dh_gid = dhgroup_id;
  88. pr_debug("%s: ctrl %d setup DH group %d\n",
  89. __func__, ctrl->cntlid, ctrl->dh_gid);
  90. ret = nvme_auth_gen_privkey(ctrl->dh_tfm, ctrl->dh_gid);
  91. if (ret < 0) {
  92. pr_debug("%s: ctrl %d failed to generate private key, err %d\n",
  93. __func__, ctrl->cntlid, ret);
  94. kfree_sensitive(ctrl->dh_key);
  95. return ret;
  96. }
  97. ctrl->dh_keysize = crypto_kpp_maxsize(ctrl->dh_tfm);
  98. kfree_sensitive(ctrl->dh_key);
  99. ctrl->dh_key = kzalloc(ctrl->dh_keysize, GFP_KERNEL);
  100. if (!ctrl->dh_key) {
  101. pr_warn("ctrl %d failed to allocate public key\n",
  102. ctrl->cntlid);
  103. return -ENOMEM;
  104. }
  105. ret = nvme_auth_gen_pubkey(ctrl->dh_tfm, ctrl->dh_key,
  106. ctrl->dh_keysize);
  107. if (ret < 0) {
  108. pr_warn("ctrl %d failed to generate public key\n",
  109. ctrl->cntlid);
  110. kfree(ctrl->dh_key);
  111. ctrl->dh_key = NULL;
  112. }
  113. }
  114. return ret;
  115. }
  116. int nvmet_setup_auth(struct nvmet_ctrl *ctrl)
  117. {
  118. int ret = 0;
  119. struct nvmet_host_link *p;
  120. struct nvmet_host *host = NULL;
  121. const char *hash_name;
  122. down_read(&nvmet_config_sem);
  123. if (nvmet_is_disc_subsys(ctrl->subsys))
  124. goto out_unlock;
  125. if (ctrl->subsys->allow_any_host)
  126. goto out_unlock;
  127. list_for_each_entry(p, &ctrl->subsys->hosts, entry) {
  128. pr_debug("check %s\n", nvmet_host_name(p->host));
  129. if (strcmp(nvmet_host_name(p->host), ctrl->hostnqn))
  130. continue;
  131. host = p->host;
  132. break;
  133. }
  134. if (!host) {
  135. pr_debug("host %s not found\n", ctrl->hostnqn);
  136. ret = -EPERM;
  137. goto out_unlock;
  138. }
  139. ret = nvmet_setup_dhgroup(ctrl, host->dhchap_dhgroup_id);
  140. if (ret < 0)
  141. pr_warn("Failed to setup DH group");
  142. if (!host->dhchap_secret) {
  143. pr_debug("No authentication provided\n");
  144. goto out_unlock;
  145. }
  146. if (host->dhchap_hash_id == ctrl->shash_id) {
  147. pr_debug("Re-use existing hash ID %d\n",
  148. ctrl->shash_id);
  149. } else {
  150. hash_name = nvme_auth_hmac_name(host->dhchap_hash_id);
  151. if (!hash_name) {
  152. pr_warn("Hash ID %d invalid\n", host->dhchap_hash_id);
  153. ret = -EINVAL;
  154. goto out_unlock;
  155. }
  156. ctrl->shash_id = host->dhchap_hash_id;
  157. }
  158. /* Skip the 'DHHC-1:XX:' prefix */
  159. nvme_auth_free_key(ctrl->host_key);
  160. ctrl->host_key = nvme_auth_extract_key(host->dhchap_secret + 10,
  161. host->dhchap_key_hash);
  162. if (IS_ERR(ctrl->host_key)) {
  163. ret = PTR_ERR(ctrl->host_key);
  164. ctrl->host_key = NULL;
  165. goto out_free_hash;
  166. }
  167. pr_debug("%s: using hash %s key %*ph\n", __func__,
  168. ctrl->host_key->hash > 0 ?
  169. nvme_auth_hmac_name(ctrl->host_key->hash) : "none",
  170. (int)ctrl->host_key->len, ctrl->host_key->key);
  171. nvme_auth_free_key(ctrl->ctrl_key);
  172. if (!host->dhchap_ctrl_secret) {
  173. ctrl->ctrl_key = NULL;
  174. goto out_unlock;
  175. }
  176. ctrl->ctrl_key = nvme_auth_extract_key(host->dhchap_ctrl_secret + 10,
  177. host->dhchap_ctrl_key_hash);
  178. if (IS_ERR(ctrl->ctrl_key)) {
  179. ret = PTR_ERR(ctrl->ctrl_key);
  180. ctrl->ctrl_key = NULL;
  181. goto out_free_hash;
  182. }
  183. pr_debug("%s: using ctrl hash %s key %*ph\n", __func__,
  184. ctrl->ctrl_key->hash > 0 ?
  185. nvme_auth_hmac_name(ctrl->ctrl_key->hash) : "none",
  186. (int)ctrl->ctrl_key->len, ctrl->ctrl_key->key);
  187. out_free_hash:
  188. if (ret) {
  189. if (ctrl->host_key) {
  190. nvme_auth_free_key(ctrl->host_key);
  191. ctrl->host_key = NULL;
  192. }
  193. ctrl->shash_id = 0;
  194. }
  195. out_unlock:
  196. up_read(&nvmet_config_sem);
  197. return ret;
  198. }
  199. void nvmet_auth_sq_free(struct nvmet_sq *sq)
  200. {
  201. cancel_delayed_work(&sq->auth_expired_work);
  202. kfree(sq->dhchap_c1);
  203. sq->dhchap_c1 = NULL;
  204. kfree(sq->dhchap_c2);
  205. sq->dhchap_c2 = NULL;
  206. kfree(sq->dhchap_skey);
  207. sq->dhchap_skey = NULL;
  208. }
  209. void nvmet_destroy_auth(struct nvmet_ctrl *ctrl)
  210. {
  211. ctrl->shash_id = 0;
  212. if (ctrl->dh_tfm) {
  213. crypto_free_kpp(ctrl->dh_tfm);
  214. ctrl->dh_tfm = NULL;
  215. ctrl->dh_gid = 0;
  216. }
  217. kfree_sensitive(ctrl->dh_key);
  218. ctrl->dh_key = NULL;
  219. if (ctrl->host_key) {
  220. nvme_auth_free_key(ctrl->host_key);
  221. ctrl->host_key = NULL;
  222. }
  223. if (ctrl->ctrl_key) {
  224. nvme_auth_free_key(ctrl->ctrl_key);
  225. ctrl->ctrl_key = NULL;
  226. }
  227. }
  228. bool nvmet_check_auth_status(struct nvmet_req *req)
  229. {
  230. if (req->sq->ctrl->host_key &&
  231. !req->sq->authenticated)
  232. return false;
  233. return true;
  234. }
  235. int nvmet_auth_host_hash(struct nvmet_req *req, u8 *response,
  236. unsigned int shash_len)
  237. {
  238. struct crypto_shash *shash_tfm;
  239. struct shash_desc *shash;
  240. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  241. const char *hash_name;
  242. u8 *challenge = req->sq->dhchap_c1, *host_response;
  243. u8 buf[4];
  244. int ret;
  245. hash_name = nvme_auth_hmac_name(ctrl->shash_id);
  246. if (!hash_name) {
  247. pr_warn("Hash ID %d invalid\n", ctrl->shash_id);
  248. return -EINVAL;
  249. }
  250. shash_tfm = crypto_alloc_shash(hash_name, 0, 0);
  251. if (IS_ERR(shash_tfm)) {
  252. pr_err("failed to allocate shash %s\n", hash_name);
  253. return PTR_ERR(shash_tfm);
  254. }
  255. if (shash_len != crypto_shash_digestsize(shash_tfm)) {
  256. pr_debug("%s: hash len mismatch (len %d digest %d)\n",
  257. __func__, shash_len,
  258. crypto_shash_digestsize(shash_tfm));
  259. ret = -EINVAL;
  260. goto out_free_tfm;
  261. }
  262. host_response = nvme_auth_transform_key(ctrl->host_key, ctrl->hostnqn);
  263. if (IS_ERR(host_response)) {
  264. ret = PTR_ERR(host_response);
  265. goto out_free_tfm;
  266. }
  267. ret = crypto_shash_setkey(shash_tfm, host_response,
  268. ctrl->host_key->len);
  269. if (ret)
  270. goto out_free_response;
  271. if (ctrl->dh_gid != NVME_AUTH_DHGROUP_NULL) {
  272. challenge = kmalloc(shash_len, GFP_KERNEL);
  273. if (!challenge) {
  274. ret = -ENOMEM;
  275. goto out_free_response;
  276. }
  277. ret = nvme_auth_augmented_challenge(ctrl->shash_id,
  278. req->sq->dhchap_skey,
  279. req->sq->dhchap_skey_len,
  280. req->sq->dhchap_c1,
  281. challenge, shash_len);
  282. if (ret)
  283. goto out_free_response;
  284. }
  285. pr_debug("ctrl %d qid %d host response seq %u transaction %d\n",
  286. ctrl->cntlid, req->sq->qid, req->sq->dhchap_s1,
  287. req->sq->dhchap_tid);
  288. shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(shash_tfm),
  289. GFP_KERNEL);
  290. if (!shash) {
  291. ret = -ENOMEM;
  292. goto out_free_response;
  293. }
  294. shash->tfm = shash_tfm;
  295. ret = crypto_shash_init(shash);
  296. if (ret)
  297. goto out;
  298. ret = crypto_shash_update(shash, challenge, shash_len);
  299. if (ret)
  300. goto out;
  301. put_unaligned_le32(req->sq->dhchap_s1, buf);
  302. ret = crypto_shash_update(shash, buf, 4);
  303. if (ret)
  304. goto out;
  305. put_unaligned_le16(req->sq->dhchap_tid, buf);
  306. ret = crypto_shash_update(shash, buf, 2);
  307. if (ret)
  308. goto out;
  309. memset(buf, 0, 4);
  310. ret = crypto_shash_update(shash, buf, 1);
  311. if (ret)
  312. goto out;
  313. ret = crypto_shash_update(shash, "HostHost", 8);
  314. if (ret)
  315. goto out;
  316. ret = crypto_shash_update(shash, ctrl->hostnqn, strlen(ctrl->hostnqn));
  317. if (ret)
  318. goto out;
  319. ret = crypto_shash_update(shash, buf, 1);
  320. if (ret)
  321. goto out;
  322. ret = crypto_shash_update(shash, ctrl->subsysnqn,
  323. strlen(ctrl->subsysnqn));
  324. if (ret)
  325. goto out;
  326. ret = crypto_shash_final(shash, response);
  327. out:
  328. if (challenge != req->sq->dhchap_c1)
  329. kfree(challenge);
  330. kfree(shash);
  331. out_free_response:
  332. kfree_sensitive(host_response);
  333. out_free_tfm:
  334. crypto_free_shash(shash_tfm);
  335. return 0;
  336. }
  337. int nvmet_auth_ctrl_hash(struct nvmet_req *req, u8 *response,
  338. unsigned int shash_len)
  339. {
  340. struct crypto_shash *shash_tfm;
  341. struct shash_desc *shash;
  342. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  343. const char *hash_name;
  344. u8 *challenge = req->sq->dhchap_c2, *ctrl_response;
  345. u8 buf[4];
  346. int ret;
  347. hash_name = nvme_auth_hmac_name(ctrl->shash_id);
  348. if (!hash_name) {
  349. pr_warn("Hash ID %d invalid\n", ctrl->shash_id);
  350. return -EINVAL;
  351. }
  352. shash_tfm = crypto_alloc_shash(hash_name, 0, 0);
  353. if (IS_ERR(shash_tfm)) {
  354. pr_err("failed to allocate shash %s\n", hash_name);
  355. return PTR_ERR(shash_tfm);
  356. }
  357. if (shash_len != crypto_shash_digestsize(shash_tfm)) {
  358. pr_debug("%s: hash len mismatch (len %d digest %d)\n",
  359. __func__, shash_len,
  360. crypto_shash_digestsize(shash_tfm));
  361. ret = -EINVAL;
  362. goto out_free_tfm;
  363. }
  364. ctrl_response = nvme_auth_transform_key(ctrl->ctrl_key,
  365. ctrl->subsysnqn);
  366. if (IS_ERR(ctrl_response)) {
  367. ret = PTR_ERR(ctrl_response);
  368. goto out_free_tfm;
  369. }
  370. ret = crypto_shash_setkey(shash_tfm, ctrl_response,
  371. ctrl->ctrl_key->len);
  372. if (ret)
  373. goto out_free_response;
  374. if (ctrl->dh_gid != NVME_AUTH_DHGROUP_NULL) {
  375. challenge = kmalloc(shash_len, GFP_KERNEL);
  376. if (!challenge) {
  377. ret = -ENOMEM;
  378. goto out_free_response;
  379. }
  380. ret = nvme_auth_augmented_challenge(ctrl->shash_id,
  381. req->sq->dhchap_skey,
  382. req->sq->dhchap_skey_len,
  383. req->sq->dhchap_c2,
  384. challenge, shash_len);
  385. if (ret)
  386. goto out_free_response;
  387. }
  388. shash = kzalloc(sizeof(*shash) + crypto_shash_descsize(shash_tfm),
  389. GFP_KERNEL);
  390. if (!shash) {
  391. ret = -ENOMEM;
  392. goto out_free_response;
  393. }
  394. shash->tfm = shash_tfm;
  395. ret = crypto_shash_init(shash);
  396. if (ret)
  397. goto out;
  398. ret = crypto_shash_update(shash, challenge, shash_len);
  399. if (ret)
  400. goto out;
  401. put_unaligned_le32(req->sq->dhchap_s2, buf);
  402. ret = crypto_shash_update(shash, buf, 4);
  403. if (ret)
  404. goto out;
  405. put_unaligned_le16(req->sq->dhchap_tid, buf);
  406. ret = crypto_shash_update(shash, buf, 2);
  407. if (ret)
  408. goto out;
  409. memset(buf, 0, 4);
  410. ret = crypto_shash_update(shash, buf, 1);
  411. if (ret)
  412. goto out;
  413. ret = crypto_shash_update(shash, "Controller", 10);
  414. if (ret)
  415. goto out;
  416. ret = crypto_shash_update(shash, ctrl->subsysnqn,
  417. strlen(ctrl->subsysnqn));
  418. if (ret)
  419. goto out;
  420. ret = crypto_shash_update(shash, buf, 1);
  421. if (ret)
  422. goto out;
  423. ret = crypto_shash_update(shash, ctrl->hostnqn, strlen(ctrl->hostnqn));
  424. if (ret)
  425. goto out;
  426. ret = crypto_shash_final(shash, response);
  427. out:
  428. if (challenge != req->sq->dhchap_c2)
  429. kfree(challenge);
  430. kfree(shash);
  431. out_free_response:
  432. kfree_sensitive(ctrl_response);
  433. out_free_tfm:
  434. crypto_free_shash(shash_tfm);
  435. return 0;
  436. }
  437. int nvmet_auth_ctrl_exponential(struct nvmet_req *req,
  438. u8 *buf, int buf_size)
  439. {
  440. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  441. int ret = 0;
  442. if (!ctrl->dh_key) {
  443. pr_warn("ctrl %d no DH public key!\n", ctrl->cntlid);
  444. return -ENOKEY;
  445. }
  446. if (buf_size != ctrl->dh_keysize) {
  447. pr_warn("ctrl %d DH public key size mismatch, need %zu is %d\n",
  448. ctrl->cntlid, ctrl->dh_keysize, buf_size);
  449. ret = -EINVAL;
  450. } else {
  451. memcpy(buf, ctrl->dh_key, buf_size);
  452. pr_debug("%s: ctrl %d public key %*ph\n", __func__,
  453. ctrl->cntlid, (int)buf_size, buf);
  454. }
  455. return ret;
  456. }
  457. int nvmet_auth_ctrl_sesskey(struct nvmet_req *req,
  458. u8 *pkey, int pkey_size)
  459. {
  460. struct nvmet_ctrl *ctrl = req->sq->ctrl;
  461. int ret;
  462. req->sq->dhchap_skey_len = ctrl->dh_keysize;
  463. req->sq->dhchap_skey = kzalloc(req->sq->dhchap_skey_len, GFP_KERNEL);
  464. if (!req->sq->dhchap_skey)
  465. return -ENOMEM;
  466. ret = nvme_auth_gen_shared_secret(ctrl->dh_tfm,
  467. pkey, pkey_size,
  468. req->sq->dhchap_skey,
  469. req->sq->dhchap_skey_len);
  470. if (ret)
  471. pr_debug("failed to compute shared secret, err %d\n", ret);
  472. else
  473. pr_debug("%s: shared secret %*ph\n", __func__,
  474. (int)req->sq->dhchap_skey_len,
  475. req->sq->dhchap_skey);
  476. return ret;
  477. }