auth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 <linux/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. u32 nvme_auth_key_struct_size(u32 key_len)
  137. {
  138. struct nvme_dhchap_key key;
  139. return struct_size(&key, key, key_len);
  140. }
  141. EXPORT_SYMBOL_GPL(nvme_auth_key_struct_size);
  142. struct nvme_dhchap_key *nvme_auth_extract_key(unsigned char *secret,
  143. u8 key_hash)
  144. {
  145. struct nvme_dhchap_key *key;
  146. unsigned char *p;
  147. u32 crc;
  148. int ret, key_len;
  149. size_t allocated_len = strlen(secret);
  150. /* Secret might be affixed with a ':' */
  151. p = strrchr(secret, ':');
  152. if (p)
  153. allocated_len = p - secret;
  154. key = nvme_auth_alloc_key(allocated_len, 0);
  155. if (!key)
  156. return ERR_PTR(-ENOMEM);
  157. key_len = base64_decode(secret, allocated_len, key->key);
  158. if (key_len < 0) {
  159. pr_debug("base64 key decoding error %d\n",
  160. key_len);
  161. ret = key_len;
  162. goto out_free_secret;
  163. }
  164. if (key_len != 36 && key_len != 52 &&
  165. key_len != 68) {
  166. pr_err("Invalid key len %d\n", key_len);
  167. ret = -EINVAL;
  168. goto out_free_secret;
  169. }
  170. /* The last four bytes is the CRC in little-endian format */
  171. key_len -= 4;
  172. /*
  173. * The linux implementation doesn't do pre- and post-increments,
  174. * so we have to do it manually.
  175. */
  176. crc = ~crc32(~0, key->key, key_len);
  177. if (get_unaligned_le32(key->key + key_len) != crc) {
  178. pr_err("key crc mismatch (key %08x, crc %08x)\n",
  179. get_unaligned_le32(key->key + key_len), crc);
  180. ret = -EKEYREJECTED;
  181. goto out_free_secret;
  182. }
  183. key->len = key_len;
  184. key->hash = key_hash;
  185. return key;
  186. out_free_secret:
  187. nvme_auth_free_key(key);
  188. return ERR_PTR(ret);
  189. }
  190. EXPORT_SYMBOL_GPL(nvme_auth_extract_key);
  191. struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash)
  192. {
  193. u32 num_bytes = nvme_auth_key_struct_size(len);
  194. struct nvme_dhchap_key *key = kzalloc(num_bytes, GFP_KERNEL);
  195. if (key) {
  196. key->len = len;
  197. key->hash = hash;
  198. }
  199. return key;
  200. }
  201. EXPORT_SYMBOL_GPL(nvme_auth_alloc_key);
  202. void nvme_auth_free_key(struct nvme_dhchap_key *key)
  203. {
  204. if (!key)
  205. return;
  206. kfree_sensitive(key);
  207. }
  208. EXPORT_SYMBOL_GPL(nvme_auth_free_key);
  209. struct nvme_dhchap_key *nvme_auth_transform_key(
  210. struct nvme_dhchap_key *key, char *nqn)
  211. {
  212. const char *hmac_name;
  213. struct crypto_shash *key_tfm;
  214. struct shash_desc *shash;
  215. struct nvme_dhchap_key *transformed_key;
  216. int ret, key_len;
  217. if (!key) {
  218. pr_warn("No key specified\n");
  219. return ERR_PTR(-ENOKEY);
  220. }
  221. if (key->hash == 0) {
  222. key_len = nvme_auth_key_struct_size(key->len);
  223. transformed_key = kmemdup(key, key_len, GFP_KERNEL);
  224. if (!transformed_key)
  225. return ERR_PTR(-ENOMEM);
  226. return transformed_key;
  227. }
  228. hmac_name = nvme_auth_hmac_name(key->hash);
  229. if (!hmac_name) {
  230. pr_warn("Invalid key hash id %d\n", key->hash);
  231. return ERR_PTR(-EINVAL);
  232. }
  233. key_tfm = crypto_alloc_shash(hmac_name, 0, 0);
  234. if (IS_ERR(key_tfm))
  235. return ERR_CAST(key_tfm);
  236. shash = kmalloc(sizeof(struct shash_desc) +
  237. crypto_shash_descsize(key_tfm),
  238. GFP_KERNEL);
  239. if (!shash) {
  240. ret = -ENOMEM;
  241. goto out_free_key;
  242. }
  243. key_len = crypto_shash_digestsize(key_tfm);
  244. transformed_key = nvme_auth_alloc_key(key_len, key->hash);
  245. if (!transformed_key) {
  246. ret = -ENOMEM;
  247. goto out_free_shash;
  248. }
  249. shash->tfm = key_tfm;
  250. ret = crypto_shash_setkey(key_tfm, key->key, key->len);
  251. if (ret < 0)
  252. goto out_free_transformed_key;
  253. ret = crypto_shash_init(shash);
  254. if (ret < 0)
  255. goto out_free_transformed_key;
  256. ret = crypto_shash_update(shash, nqn, strlen(nqn));
  257. if (ret < 0)
  258. goto out_free_transformed_key;
  259. ret = crypto_shash_update(shash, "NVMe-over-Fabrics", 17);
  260. if (ret < 0)
  261. goto out_free_transformed_key;
  262. ret = crypto_shash_final(shash, transformed_key->key);
  263. if (ret < 0)
  264. goto out_free_transformed_key;
  265. kfree(shash);
  266. crypto_free_shash(key_tfm);
  267. return transformed_key;
  268. out_free_transformed_key:
  269. nvme_auth_free_key(transformed_key);
  270. out_free_shash:
  271. kfree(shash);
  272. out_free_key:
  273. crypto_free_shash(key_tfm);
  274. return ERR_PTR(ret);
  275. }
  276. EXPORT_SYMBOL_GPL(nvme_auth_transform_key);
  277. static int nvme_auth_hash_skey(int hmac_id, u8 *skey, size_t skey_len, u8 *hkey)
  278. {
  279. const char *digest_name;
  280. struct crypto_shash *tfm;
  281. int ret;
  282. digest_name = nvme_auth_digest_name(hmac_id);
  283. if (!digest_name) {
  284. pr_debug("%s: failed to get digest for %d\n", __func__,
  285. hmac_id);
  286. return -EINVAL;
  287. }
  288. tfm = crypto_alloc_shash(digest_name, 0, 0);
  289. if (IS_ERR(tfm))
  290. return -ENOMEM;
  291. ret = crypto_shash_tfm_digest(tfm, skey, skey_len, hkey);
  292. if (ret < 0)
  293. pr_debug("%s: Failed to hash digest len %zu\n", __func__,
  294. skey_len);
  295. crypto_free_shash(tfm);
  296. return ret;
  297. }
  298. int nvme_auth_augmented_challenge(u8 hmac_id, u8 *skey, size_t skey_len,
  299. u8 *challenge, u8 *aug, size_t hlen)
  300. {
  301. struct crypto_shash *tfm;
  302. u8 *hashed_key;
  303. const char *hmac_name;
  304. int ret;
  305. hashed_key = kmalloc(hlen, GFP_KERNEL);
  306. if (!hashed_key)
  307. return -ENOMEM;
  308. ret = nvme_auth_hash_skey(hmac_id, skey,
  309. skey_len, hashed_key);
  310. if (ret < 0)
  311. goto out_free_key;
  312. hmac_name = nvme_auth_hmac_name(hmac_id);
  313. if (!hmac_name) {
  314. pr_warn("%s: invalid hash algorithm %d\n",
  315. __func__, hmac_id);
  316. ret = -EINVAL;
  317. goto out_free_key;
  318. }
  319. tfm = crypto_alloc_shash(hmac_name, 0, 0);
  320. if (IS_ERR(tfm)) {
  321. ret = PTR_ERR(tfm);
  322. goto out_free_key;
  323. }
  324. ret = crypto_shash_setkey(tfm, hashed_key, hlen);
  325. if (ret)
  326. goto out_free_hash;
  327. ret = crypto_shash_tfm_digest(tfm, challenge, hlen, aug);
  328. out_free_hash:
  329. crypto_free_shash(tfm);
  330. out_free_key:
  331. kfree_sensitive(hashed_key);
  332. return ret;
  333. }
  334. EXPORT_SYMBOL_GPL(nvme_auth_augmented_challenge);
  335. int nvme_auth_gen_privkey(struct crypto_kpp *dh_tfm, u8 dh_gid)
  336. {
  337. int ret;
  338. ret = crypto_kpp_set_secret(dh_tfm, NULL, 0);
  339. if (ret)
  340. pr_debug("failed to set private key, error %d\n", ret);
  341. return ret;
  342. }
  343. EXPORT_SYMBOL_GPL(nvme_auth_gen_privkey);
  344. int nvme_auth_gen_pubkey(struct crypto_kpp *dh_tfm,
  345. u8 *host_key, size_t host_key_len)
  346. {
  347. struct kpp_request *req;
  348. struct crypto_wait wait;
  349. struct scatterlist dst;
  350. int ret;
  351. req = kpp_request_alloc(dh_tfm, GFP_KERNEL);
  352. if (!req)
  353. return -ENOMEM;
  354. crypto_init_wait(&wait);
  355. kpp_request_set_input(req, NULL, 0);
  356. sg_init_one(&dst, host_key, host_key_len);
  357. kpp_request_set_output(req, &dst, host_key_len);
  358. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  359. crypto_req_done, &wait);
  360. ret = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
  361. kpp_request_free(req);
  362. return ret;
  363. }
  364. EXPORT_SYMBOL_GPL(nvme_auth_gen_pubkey);
  365. int nvme_auth_gen_shared_secret(struct crypto_kpp *dh_tfm,
  366. u8 *ctrl_key, size_t ctrl_key_len,
  367. u8 *sess_key, size_t sess_key_len)
  368. {
  369. struct kpp_request *req;
  370. struct crypto_wait wait;
  371. struct scatterlist src, dst;
  372. int ret;
  373. req = kpp_request_alloc(dh_tfm, GFP_KERNEL);
  374. if (!req)
  375. return -ENOMEM;
  376. crypto_init_wait(&wait);
  377. sg_init_one(&src, ctrl_key, ctrl_key_len);
  378. kpp_request_set_input(req, &src, ctrl_key_len);
  379. sg_init_one(&dst, sess_key, sess_key_len);
  380. kpp_request_set_output(req, &dst, sess_key_len);
  381. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  382. crypto_req_done, &wait);
  383. ret = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait);
  384. kpp_request_free(req);
  385. return ret;
  386. }
  387. EXPORT_SYMBOL_GPL(nvme_auth_gen_shared_secret);
  388. int nvme_auth_generate_key(u8 *secret, struct nvme_dhchap_key **ret_key)
  389. {
  390. struct nvme_dhchap_key *key;
  391. u8 key_hash;
  392. if (!secret) {
  393. *ret_key = NULL;
  394. return 0;
  395. }
  396. if (sscanf(secret, "DHHC-1:%hhd:%*s:", &key_hash) != 1)
  397. return -EINVAL;
  398. /* Pass in the secret without the 'DHHC-1:XX:' prefix */
  399. key = nvme_auth_extract_key(secret + 10, key_hash);
  400. if (IS_ERR(key)) {
  401. *ret_key = NULL;
  402. return PTR_ERR(key);
  403. }
  404. *ret_key = key;
  405. return 0;
  406. }
  407. EXPORT_SYMBOL_GPL(nvme_auth_generate_key);
  408. MODULE_DESCRIPTION("NVMe Authentication framework");
  409. MODULE_LICENSE("GPL v2");