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