auth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2020 Hannes Reinecke, SUSE Linux
  4. */
  5. #include <linux/module.h>
  6. #include <linux/crc32.h>
  7. #include <linux/base64.h>
  8. #include <linux/prandom.h>
  9. #include <linux/scatterlist.h>
  10. #include <asm/unaligned.h>
  11. #include <crypto/hash.h>
  12. #include <crypto/dh.h>
  13. #include <linux/nvme.h>
  14. #include <linux/nvme-auth.h>
  15. static u32 nvme_dhchap_seqnum;
  16. static DEFINE_MUTEX(nvme_dhchap_mutex);
  17. u32 nvme_auth_get_seqnum(void)
  18. {
  19. u32 seqnum;
  20. mutex_lock(&nvme_dhchap_mutex);
  21. if (!nvme_dhchap_seqnum)
  22. nvme_dhchap_seqnum = get_random_u32();
  23. else {
  24. nvme_dhchap_seqnum++;
  25. if (!nvme_dhchap_seqnum)
  26. nvme_dhchap_seqnum++;
  27. }
  28. seqnum = nvme_dhchap_seqnum;
  29. mutex_unlock(&nvme_dhchap_mutex);
  30. return seqnum;
  31. }
  32. EXPORT_SYMBOL_GPL(nvme_auth_get_seqnum);
  33. static struct nvme_auth_dhgroup_map {
  34. const char name[16];
  35. const char kpp[16];
  36. } dhgroup_map[] = {
  37. [NVME_AUTH_DHGROUP_NULL] = {
  38. .name = "null", .kpp = "null" },
  39. [NVME_AUTH_DHGROUP_2048] = {
  40. .name = "ffdhe2048", .kpp = "ffdhe2048(dh)" },
  41. [NVME_AUTH_DHGROUP_3072] = {
  42. .name = "ffdhe3072", .kpp = "ffdhe3072(dh)" },
  43. [NVME_AUTH_DHGROUP_4096] = {
  44. .name = "ffdhe4096", .kpp = "ffdhe4096(dh)" },
  45. [NVME_AUTH_DHGROUP_6144] = {
  46. .name = "ffdhe6144", .kpp = "ffdhe6144(dh)" },
  47. [NVME_AUTH_DHGROUP_8192] = {
  48. .name = "ffdhe8192", .kpp = "ffdhe8192(dh)" },
  49. };
  50. const char *nvme_auth_dhgroup_name(u8 dhgroup_id)
  51. {
  52. if (dhgroup_id >= ARRAY_SIZE(dhgroup_map))
  53. return NULL;
  54. return dhgroup_map[dhgroup_id].name;
  55. }
  56. EXPORT_SYMBOL_GPL(nvme_auth_dhgroup_name);
  57. const char *nvme_auth_dhgroup_kpp(u8 dhgroup_id)
  58. {
  59. if (dhgroup_id >= ARRAY_SIZE(dhgroup_map))
  60. return NULL;
  61. return dhgroup_map[dhgroup_id].kpp;
  62. }
  63. EXPORT_SYMBOL_GPL(nvme_auth_dhgroup_kpp);
  64. u8 nvme_auth_dhgroup_id(const char *dhgroup_name)
  65. {
  66. int i;
  67. if (!dhgroup_name || !strlen(dhgroup_name))
  68. return NVME_AUTH_DHGROUP_INVALID;
  69. for (i = 0; i < ARRAY_SIZE(dhgroup_map); i++) {
  70. if (!strlen(dhgroup_map[i].name))
  71. continue;
  72. if (!strncmp(dhgroup_map[i].name, dhgroup_name,
  73. strlen(dhgroup_map[i].name)))
  74. return i;
  75. }
  76. return NVME_AUTH_DHGROUP_INVALID;
  77. }
  78. EXPORT_SYMBOL_GPL(nvme_auth_dhgroup_id);
  79. static struct nvme_dhchap_hash_map {
  80. int len;
  81. const char hmac[15];
  82. const char digest[8];
  83. } hash_map[] = {
  84. [NVME_AUTH_HASH_SHA256] = {
  85. .len = 32,
  86. .hmac = "hmac(sha256)",
  87. .digest = "sha256",
  88. },
  89. [NVME_AUTH_HASH_SHA384] = {
  90. .len = 48,
  91. .hmac = "hmac(sha384)",
  92. .digest = "sha384",
  93. },
  94. [NVME_AUTH_HASH_SHA512] = {
  95. .len = 64,
  96. .hmac = "hmac(sha512)",
  97. .digest = "sha512",
  98. },
  99. };
  100. const char *nvme_auth_hmac_name(u8 hmac_id)
  101. {
  102. if (hmac_id >= ARRAY_SIZE(hash_map))
  103. return NULL;
  104. return hash_map[hmac_id].hmac;
  105. }
  106. EXPORT_SYMBOL_GPL(nvme_auth_hmac_name);
  107. const char *nvme_auth_digest_name(u8 hmac_id)
  108. {
  109. if (hmac_id >= ARRAY_SIZE(hash_map))
  110. return NULL;
  111. return hash_map[hmac_id].digest;
  112. }
  113. EXPORT_SYMBOL_GPL(nvme_auth_digest_name);
  114. u8 nvme_auth_hmac_id(const char *hmac_name)
  115. {
  116. int i;
  117. if (!hmac_name || !strlen(hmac_name))
  118. return NVME_AUTH_HASH_INVALID;
  119. for (i = 0; i < ARRAY_SIZE(hash_map); i++) {
  120. if (!strlen(hash_map[i].hmac))
  121. continue;
  122. if (!strncmp(hash_map[i].hmac, hmac_name,
  123. strlen(hash_map[i].hmac)))
  124. return i;
  125. }
  126. return NVME_AUTH_HASH_INVALID;
  127. }
  128. EXPORT_SYMBOL_GPL(nvme_auth_hmac_id);
  129. size_t nvme_auth_hmac_hash_len(u8 hmac_id)
  130. {
  131. if (hmac_id >= ARRAY_SIZE(hash_map))
  132. return 0;
  133. return hash_map[hmac_id].len;
  134. }
  135. EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len);
  136. struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret,
  137. u8 key_hash)
  138. {
  139. struct nvme_dhchap_key *key;
  140. unsigned char *p;
  141. u32 crc;
  142. int ret, key_len;
  143. size_t allocated_len = strlen(secret);
  144. /* Secret might be affixed with a ':' */
  145. p = strrchr(secret, ':');
  146. if (p)
  147. allocated_len = p - secret;
  148. key = kzalloc(sizeof(*key), GFP_KERNEL);
  149. if (!key)
  150. return ERR_PTR(-ENOMEM);
  151. key->key = kzalloc(allocated_len, GFP_KERNEL);
  152. if (!key->key) {
  153. ret = -ENOMEM;
  154. goto out_free_key;
  155. }
  156. key_len = base64_decode(secret, allocated_len, key->key);
  157. if (key_len < 0) {
  158. pr_debug("base64 key decoding error %d\n",
  159. key_len);
  160. ret = key_len;
  161. goto out_free_secret;
  162. }
  163. if (key_len != 36 && key_len != 52 &&
  164. key_len != 68) {
  165. pr_err("Invalid key len %d\n", key_len);
  166. ret = -EINVAL;
  167. goto out_free_secret;
  168. }
  169. if (key_hash > 0 &&
  170. (key_len - 4) != nvme_auth_hmac_hash_len(key_hash)) {
  171. pr_err("Mismatched key len %d for %s\n", key_len,
  172. nvme_auth_hmac_name(key_hash));
  173. ret = -EINVAL;
  174. goto out_free_secret;
  175. }
  176. /* The last four bytes is the CRC in little-endian format */
  177. key_len -= 4;
  178. /*
  179. * The linux implementation doesn't do pre- and post-increments,
  180. * so we have to do it manually.
  181. */
  182. crc = ~crc32(~0, key->key, key_len);
  183. if (get_unaligned_le32(key->key + key_len) != crc) {
  184. pr_err("key crc mismatch (key %08x, crc %08x)\n",
  185. get_unaligned_le32(key->key + key_len), crc);
  186. ret = -EKEYREJECTED;
  187. goto out_free_secret;
  188. }
  189. key->len = key_len;
  190. key->hash = key_hash;
  191. return key;
  192. out_free_secret:
  193. kfree_sensitive(key->key);
  194. out_free_key:
  195. kfree(key);
  196. return ERR_PTR(ret);
  197. }
  198. EXPORT_SYMBOL_GPL(nvme_auth_extract_key);
  199. void nvme_auth_free_key(struct nvme_dhchap_key *key)
  200. {
  201. if (!key)
  202. return;
  203. kfree_sensitive(key->key);
  204. kfree(key);
  205. }
  206. EXPORT_SYMBOL_GPL(nvme_auth_free_key);
  207. u8 *nvme_auth_transform_key(struct nvme_dhchap_key *key, char *nqn)
  208. {
  209. const char *hmac_name;
  210. struct crypto_shash *key_tfm;
  211. struct shash_desc *shash;
  212. u8 *transformed_key;
  213. int ret;
  214. if (!key || !key->key) {
  215. pr_warn("No key specified\n");
  216. return ERR_PTR(-ENOKEY);
  217. }
  218. if (key->hash == 0) {
  219. transformed_key = kmemdup(key->key, key->len, GFP_KERNEL);
  220. return transformed_key ? transformed_key : ERR_PTR(-ENOMEM);
  221. }
  222. hmac_name = nvme_auth_hmac_name(key->hash);
  223. if (!hmac_name) {
  224. pr_warn("Invalid key hash id %d\n", key->hash);
  225. return ERR_PTR(-EINVAL);
  226. }
  227. key_tfm = crypto_alloc_shash(hmac_name, 0, 0);
  228. if (IS_ERR(key_tfm))
  229. return (u8 *)key_tfm;
  230. shash = kmalloc(sizeof(struct shash_desc) +
  231. crypto_shash_descsize(key_tfm),
  232. GFP_KERNEL);
  233. if (!shash) {
  234. ret = -ENOMEM;
  235. goto out_free_key;
  236. }
  237. transformed_key = kzalloc(crypto_shash_digestsize(key_tfm), GFP_KERNEL);
  238. if (!transformed_key) {
  239. ret = -ENOMEM;
  240. goto out_free_shash;
  241. }
  242. shash->tfm = key_tfm;
  243. ret = crypto_shash_setkey(key_tfm, key->key, key->len);
  244. if (ret < 0)
  245. goto out_free_transformed_key;
  246. ret = crypto_shash_init(shash);
  247. if (ret < 0)
  248. goto out_free_transformed_key;
  249. ret = crypto_shash_update(shash, nqn, strlen(nqn));
  250. if (ret < 0)
  251. goto out_free_transformed_key;
  252. ret = crypto_shash_update(shash, "NVMe-over-Fabrics", 17);
  253. if (ret < 0)
  254. goto out_free_transformed_key;
  255. ret = crypto_shash_final(shash, transformed_key);
  256. if (ret < 0)
  257. goto out_free_transformed_key;
  258. kfree(shash);
  259. crypto_free_shash(key_tfm);
  260. return transformed_key;
  261. out_free_transformed_key:
  262. kfree_sensitive(transformed_key);
  263. out_free_shash:
  264. kfree(shash);
  265. out_free_key:
  266. crypto_free_shash(key_tfm);
  267. return ERR_PTR(ret);
  268. }
  269. EXPORT_SYMBOL_GPL(nvme_auth_transform_key);
  270. static int nvme_auth_hash_skey(int hmac_id, u8 *skey, size_t skey_len, u8 *hkey)
  271. {
  272. const char *digest_name;
  273. struct crypto_shash *tfm;
  274. int ret;
  275. digest_name = nvme_auth_digest_name(hmac_id);
  276. if (!digest_name) {
  277. pr_debug("%s: failed to get digest for %d\n", __func__,
  278. hmac_id);
  279. return -EINVAL;
  280. }
  281. tfm = crypto_alloc_shash(digest_name, 0, 0);
  282. if (IS_ERR(tfm))
  283. return -ENOMEM;
  284. ret = crypto_shash_tfm_digest(tfm, skey, skey_len, hkey);
  285. if (ret < 0)
  286. pr_debug("%s: Failed to hash digest len %zu\n", __func__,
  287. skey_len);
  288. crypto_free_shash(tfm);
  289. return ret;
  290. }
  291. int nvme_auth_augmented_challenge(u8 hmac_id, u8 *skey, size_t skey_len,
  292. u8 *challenge, u8 *aug, size_t hlen)
  293. {
  294. struct crypto_shash *tfm;
  295. struct shash_desc *desc;
  296. u8 *hashed_key;
  297. const char *hmac_name;
  298. int ret;
  299. hashed_key = kmalloc(hlen, GFP_KERNEL);
  300. if (!hashed_key)
  301. return -ENOMEM;
  302. ret = nvme_auth_hash_skey(hmac_id, skey,
  303. skey_len, hashed_key);
  304. if (ret < 0)
  305. goto out_free_key;
  306. hmac_name = nvme_auth_hmac_name(hmac_id);
  307. if (!hmac_name) {
  308. pr_warn("%s: invalid hash algorithm %d\n",
  309. __func__, hmac_id);
  310. ret = -EINVAL;
  311. goto out_free_key;
  312. }
  313. tfm = crypto_alloc_shash(hmac_name, 0, 0);
  314. if (IS_ERR(tfm)) {
  315. ret = PTR_ERR(tfm);
  316. goto out_free_key;
  317. }
  318. desc = kmalloc(sizeof(struct shash_desc) + crypto_shash_descsize(tfm),
  319. GFP_KERNEL);
  320. if (!desc) {
  321. ret = -ENOMEM;
  322. goto out_free_hash;
  323. }
  324. desc->tfm = tfm;
  325. ret = crypto_shash_setkey(tfm, hashed_key, hlen);
  326. if (ret)
  327. goto out_free_desc;
  328. ret = crypto_shash_init(desc);
  329. if (ret)
  330. goto out_free_desc;
  331. ret = crypto_shash_update(desc, challenge, hlen);
  332. if (ret)
  333. goto out_free_desc;
  334. ret = crypto_shash_final(desc, aug);
  335. out_free_desc:
  336. kfree_sensitive(desc);
  337. out_free_hash:
  338. crypto_free_shash(tfm);
  339. out_free_key:
  340. kfree_sensitive(hashed_key);
  341. return ret;
  342. }
  343. EXPORT_SYMBOL_GPL(nvme_auth_augmented_challenge);
  344. int nvme_auth_gen_privkey(struct crypto_kpp *dh_tfm, u8 dh_gid)
  345. {
  346. int ret;
  347. ret = crypto_kpp_set_secret(dh_tfm, NULL, 0);
  348. if (ret)
  349. pr_debug("failed to set private key, error %d\n", ret);
  350. return ret;
  351. }
  352. EXPORT_SYMBOL_GPL(nvme_auth_gen_privkey);
  353. int nvme_auth_gen_pubkey(struct crypto_kpp *dh_tfm,
  354. u8 *host_key, size_t host_key_len)
  355. {
  356. struct kpp_request *req;
  357. struct crypto_wait wait;
  358. struct scatterlist dst;
  359. int ret;
  360. req = kpp_request_alloc(dh_tfm, GFP_KERNEL);
  361. if (!req)
  362. return -ENOMEM;
  363. crypto_init_wait(&wait);
  364. kpp_request_set_input(req, NULL, 0);
  365. sg_init_one(&dst, host_key, host_key_len);
  366. kpp_request_set_output(req, &dst, host_key_len);
  367. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  368. crypto_req_done, &wait);
  369. ret = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
  370. kpp_request_free(req);
  371. return ret;
  372. }
  373. EXPORT_SYMBOL_GPL(nvme_auth_gen_pubkey);
  374. int nvme_auth_gen_shared_secret(struct crypto_kpp *dh_tfm,
  375. u8 *ctrl_key, size_t ctrl_key_len,
  376. u8 *sess_key, size_t sess_key_len)
  377. {
  378. struct kpp_request *req;
  379. struct crypto_wait wait;
  380. struct scatterlist src, dst;
  381. int ret;
  382. req = kpp_request_alloc(dh_tfm, GFP_KERNEL);
  383. if (!req)
  384. return -ENOMEM;
  385. crypto_init_wait(&wait);
  386. sg_init_one(&src, ctrl_key, ctrl_key_len);
  387. kpp_request_set_input(req, &src, ctrl_key_len);
  388. sg_init_one(&dst, sess_key, sess_key_len);
  389. kpp_request_set_output(req, &dst, sess_key_len);
  390. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  391. crypto_req_done, &wait);
  392. ret = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
  393. kpp_request_free(req);
  394. return ret;
  395. }
  396. EXPORT_SYMBOL_GPL(nvme_auth_gen_shared_secret);
  397. int nvme_auth_generate_key(u8 *secret, struct nvme_dhchap_key **ret_key)
  398. {
  399. struct nvme_dhchap_key *key;
  400. u8 key_hash;
  401. if (!secret) {
  402. *ret_key = NULL;
  403. return 0;
  404. }
  405. if (sscanf(secret, "DHHC-1:%hhd:%*s:", &key_hash) != 1)
  406. return -EINVAL;
  407. /* Pass in the secret without the 'DHHC-1:XX:' prefix */
  408. key = nvme_auth_extract_key(secret + 10, key_hash);
  409. if (IS_ERR(key)) {
  410. *ret_key = NULL;
  411. return PTR_ERR(key);
  412. }
  413. *ret_key = key;
  414. return 0;
  415. }
  416. EXPORT_SYMBOL_GPL(nvme_auth_generate_key);
  417. MODULE_LICENSE("GPL v2");