1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2020 Hannes Reinecke, SUSE Linux 4 */ 5 6 #include <linux/module.h> 7 #include <linux/crc32.h> 8 #include <linux/base64.h> 9 #include <linux/prandom.h> 10 #include <linux/scatterlist.h> 11 #include <linux/unaligned.h> 12 #include <crypto/hash.h> 13 #include <crypto/dh.h> 14 #include <crypto/hkdf.h> 15 #include <crypto/sha2.h> 16 #include <linux/nvme.h> 17 #include <linux/nvme-auth.h> 18 19 static u32 nvme_dhchap_seqnum; 20 static DEFINE_MUTEX(nvme_dhchap_mutex); 21 22 u32 nvme_auth_get_seqnum(void) 23 { 24 u32 seqnum; 25 26 mutex_lock(&nvme_dhchap_mutex); 27 if (!nvme_dhchap_seqnum) 28 nvme_dhchap_seqnum = get_random_u32(); 29 else { 30 nvme_dhchap_seqnum++; 31 if (!nvme_dhchap_seqnum) 32 nvme_dhchap_seqnum++; 33 } 34 seqnum = nvme_dhchap_seqnum; 35 mutex_unlock(&nvme_dhchap_mutex); 36 return seqnum; 37 } 38 EXPORT_SYMBOL_GPL(nvme_auth_get_seqnum); 39 40 static const struct nvme_auth_dhgroup_map { 41 char name[16]; 42 char kpp[16]; 43 } dhgroup_map[] = { 44 [NVME_AUTH_DHGROUP_NULL] = { 45 .name = "null", .kpp = "null" }, 46 [NVME_AUTH_DHGROUP_2048] = { 47 .name = "ffdhe2048", .kpp = "ffdhe2048(dh)" }, 48 [NVME_AUTH_DHGROUP_3072] = { 49 .name = "ffdhe3072", .kpp = "ffdhe3072(dh)" }, 50 [NVME_AUTH_DHGROUP_4096] = { 51 .name = "ffdhe4096", .kpp = "ffdhe4096(dh)" }, 52 [NVME_AUTH_DHGROUP_6144] = { 53 .name = "ffdhe6144", .kpp = "ffdhe6144(dh)" }, 54 [NVME_AUTH_DHGROUP_8192] = { 55 .name = "ffdhe8192", .kpp = "ffdhe8192(dh)" }, 56 }; 57 58 const char *nvme_auth_dhgroup_name(u8 dhgroup_id) 59 { 60 if (dhgroup_id >= ARRAY_SIZE(dhgroup_map)) 61 return NULL; 62 return dhgroup_map[dhgroup_id].name; 63 } 64 EXPORT_SYMBOL_GPL(nvme_auth_dhgroup_name); 65 66 const char *nvme_auth_dhgroup_kpp(u8 dhgroup_id) 67 { 68 if (dhgroup_id >= ARRAY_SIZE(dhgroup_map)) 69 return NULL; 70 return dhgroup_map[dhgroup_id].kpp; 71 } 72 EXPORT_SYMBOL_GPL(nvme_auth_dhgroup_kpp); 73 74 u8 nvme_auth_dhgroup_id(const char *dhgroup_name) 75 { 76 int i; 77 78 if (!dhgroup_name || !strlen(dhgroup_name)) 79 return NVME_AUTH_DHGROUP_INVALID; 80 for (i = 0; i < ARRAY_SIZE(dhgroup_map); i++) { 81 if (!strlen(dhgroup_map[i].name)) 82 continue; 83 if (!strncmp(dhgroup_map[i].name, dhgroup_name, 84 strlen(dhgroup_map[i].name))) 85 return i; 86 } 87 return NVME_AUTH_DHGROUP_INVALID; 88 } 89 EXPORT_SYMBOL_GPL(nvme_auth_dhgroup_id); 90 91 static const struct nvme_dhchap_hash_map { 92 int len; 93 char hmac[15]; 94 char digest[8]; 95 } hash_map[] = { 96 [NVME_AUTH_HASH_SHA256] = { 97 .len = 32, 98 .hmac = "hmac(sha256)", 99 .digest = "sha256", 100 }, 101 [NVME_AUTH_HASH_SHA384] = { 102 .len = 48, 103 .hmac = "hmac(sha384)", 104 .digest = "sha384", 105 }, 106 [NVME_AUTH_HASH_SHA512] = { 107 .len = 64, 108 .hmac = "hmac(sha512)", 109 .digest = "sha512", 110 }, 111 }; 112 113 const char *nvme_auth_hmac_name(u8 hmac_id) 114 { 115 if (hmac_id >= ARRAY_SIZE(hash_map)) 116 return NULL; 117 return hash_map[hmac_id].hmac; 118 } 119 EXPORT_SYMBOL_GPL(nvme_auth_hmac_name); 120 121 const char *nvme_auth_digest_name(u8 hmac_id) 122 { 123 if (hmac_id >= ARRAY_SIZE(hash_map)) 124 return NULL; 125 return hash_map[hmac_id].digest; 126 } 127 EXPORT_SYMBOL_GPL(nvme_auth_digest_name); 128 129 u8 nvme_auth_hmac_id(const char *hmac_name) 130 { 131 int i; 132 133 if (!hmac_name || !strlen(hmac_name)) 134 return NVME_AUTH_HASH_INVALID; 135 136 for (i = 0; i < ARRAY_SIZE(hash_map); i++) { 137 if (!strlen(hash_map[i].hmac)) 138 continue; 139 if (!strncmp(hash_map[i].hmac, hmac_name, 140 strlen(hash_map[i].hmac))) 141 return i; 142 } 143 return NVME_AUTH_HASH_INVALID; 144 } 145 EXPORT_SYMBOL_GPL(nvme_auth_hmac_id); 146 147 size_t nvme_auth_hmac_hash_len(u8 hmac_id) 148 { 149 if (hmac_id >= ARRAY_SIZE(hash_map)) 150 return 0; 151 return hash_map[hmac_id].len; 152 } 153 EXPORT_SYMBOL_GPL(nvme_auth_hmac_hash_len); 154 155 u32 nvme_auth_key_struct_size(u32 key_len) 156 { 157 struct nvme_dhchap_key key; 158 159 return struct_size(&key, key, key_len); 160 } 161 EXPORT_SYMBOL_GPL(nvme_auth_key_struct_size); 162 163 struct nvme_dhchap_key *nvme_auth_extract_key(const char *secret, u8 key_hash) 164 { 165 struct nvme_dhchap_key *key; 166 const char *p; 167 u32 crc; 168 int ret, key_len; 169 size_t allocated_len = strlen(secret); 170 171 /* Secret might be affixed with a ':' */ 172 p = strrchr(secret, ':'); 173 if (p) 174 allocated_len = p - secret; 175 key = nvme_auth_alloc_key(allocated_len, 0); 176 if (!key) 177 return ERR_PTR(-ENOMEM); 178 179 key_len = base64_decode(secret, allocated_len, key->key, true, BASE64_STD); 180 if (key_len < 0) { 181 pr_debug("base64 key decoding error %d\n", 182 key_len); 183 ret = key_len; 184 goto out_free_key; 185 } 186 187 if (key_len != 36 && key_len != 52 && 188 key_len != 68) { 189 pr_err("Invalid key len %d\n", key_len); 190 ret = -EINVAL; 191 goto out_free_key; 192 } 193 194 /* The last four bytes is the CRC in little-endian format */ 195 key_len -= 4; 196 /* 197 * The linux implementation doesn't do pre- and post-increments, 198 * so we have to do it manually. 199 */ 200 crc = ~crc32(~0, key->key, key_len); 201 202 if (get_unaligned_le32(key->key + key_len) != crc) { 203 pr_err("key crc mismatch (key %08x, crc %08x)\n", 204 get_unaligned_le32(key->key + key_len), crc); 205 ret = -EKEYREJECTED; 206 goto out_free_key; 207 } 208 key->len = key_len; 209 key->hash = key_hash; 210 return key; 211 out_free_key: 212 nvme_auth_free_key(key); 213 return ERR_PTR(ret); 214 } 215 EXPORT_SYMBOL_GPL(nvme_auth_extract_key); 216 217 struct nvme_dhchap_key *nvme_auth_alloc_key(u32 len, u8 hash) 218 { 219 u32 num_bytes = nvme_auth_key_struct_size(len); 220 struct nvme_dhchap_key *key = kzalloc(num_bytes, GFP_KERNEL); 221 222 if (key) { 223 key->len = len; 224 key->hash = hash; 225 } 226 return key; 227 } 228 EXPORT_SYMBOL_GPL(nvme_auth_alloc_key); 229 230 void nvme_auth_free_key(struct nvme_dhchap_key *key) 231 { 232 if (!key) 233 return; 234 kfree_sensitive(key); 235 } 236 EXPORT_SYMBOL_GPL(nvme_auth_free_key); 237 238 /* 239 * Start computing an HMAC value, given the algorithm ID and raw key. 240 * 241 * The context should be zeroized at the end of its lifetime. The caller can do 242 * that implicitly by calling nvme_auth_hmac_final(), or explicitly (needed when 243 * a context is abandoned without finalizing it) by calling memzero_explicit(). 244 */ 245 int nvme_auth_hmac_init(struct nvme_auth_hmac_ctx *hmac, u8 hmac_id, 246 const u8 *key, size_t key_len) 247 { 248 hmac->hmac_id = hmac_id; 249 switch (hmac_id) { 250 case NVME_AUTH_HASH_SHA256: 251 hmac_sha256_init_usingrawkey(&hmac->sha256, key, key_len); 252 return 0; 253 case NVME_AUTH_HASH_SHA384: 254 hmac_sha384_init_usingrawkey(&hmac->sha384, key, key_len); 255 return 0; 256 case NVME_AUTH_HASH_SHA512: 257 hmac_sha512_init_usingrawkey(&hmac->sha512, key, key_len); 258 return 0; 259 } 260 pr_warn("%s: invalid hash algorithm %d\n", __func__, hmac_id); 261 return -EINVAL; 262 } 263 EXPORT_SYMBOL_GPL(nvme_auth_hmac_init); 264 265 void nvme_auth_hmac_update(struct nvme_auth_hmac_ctx *hmac, const u8 *data, 266 size_t data_len) 267 { 268 switch (hmac->hmac_id) { 269 case NVME_AUTH_HASH_SHA256: 270 hmac_sha256_update(&hmac->sha256, data, data_len); 271 return; 272 case NVME_AUTH_HASH_SHA384: 273 hmac_sha384_update(&hmac->sha384, data, data_len); 274 return; 275 case NVME_AUTH_HASH_SHA512: 276 hmac_sha512_update(&hmac->sha512, data, data_len); 277 return; 278 } 279 /* Unreachable because nvme_auth_hmac_init() validated hmac_id */ 280 WARN_ON_ONCE(1); 281 } 282 EXPORT_SYMBOL_GPL(nvme_auth_hmac_update); 283 284 /* Finish computing an HMAC value. Note that this zeroizes the HMAC context. */ 285 void nvme_auth_hmac_final(struct nvme_auth_hmac_ctx *hmac, u8 *out) 286 { 287 switch (hmac->hmac_id) { 288 case NVME_AUTH_HASH_SHA256: 289 hmac_sha256_final(&hmac->sha256, out); 290 return; 291 case NVME_AUTH_HASH_SHA384: 292 hmac_sha384_final(&hmac->sha384, out); 293 return; 294 case NVME_AUTH_HASH_SHA512: 295 hmac_sha512_final(&hmac->sha512, out); 296 return; 297 } 298 /* Unreachable because nvme_auth_hmac_init() validated hmac_id */ 299 WARN_ON_ONCE(1); 300 } 301 EXPORT_SYMBOL_GPL(nvme_auth_hmac_final); 302 303 struct nvme_dhchap_key *nvme_auth_transform_key( 304 const struct nvme_dhchap_key *key, const char *nqn) 305 { 306 struct nvme_auth_hmac_ctx hmac; 307 struct nvme_dhchap_key *transformed_key; 308 int ret, key_len; 309 310 if (!key) { 311 pr_warn("No key specified\n"); 312 return ERR_PTR(-ENOKEY); 313 } 314 if (key->hash == 0) { 315 key_len = nvme_auth_key_struct_size(key->len); 316 transformed_key = kmemdup(key, key_len, GFP_KERNEL); 317 if (!transformed_key) 318 return ERR_PTR(-ENOMEM); 319 return transformed_key; 320 } 321 ret = nvme_auth_hmac_init(&hmac, key->hash, key->key, key->len); 322 if (ret) 323 return ERR_PTR(ret); 324 key_len = nvme_auth_hmac_hash_len(key->hash); 325 transformed_key = nvme_auth_alloc_key(key_len, key->hash); 326 if (!transformed_key) { 327 memzero_explicit(&hmac, sizeof(hmac)); 328 return ERR_PTR(-ENOMEM); 329 } 330 nvme_auth_hmac_update(&hmac, nqn, strlen(nqn)); 331 nvme_auth_hmac_update(&hmac, "NVMe-over-Fabrics", 17); 332 nvme_auth_hmac_final(&hmac, transformed_key->key); 333 return transformed_key; 334 } 335 EXPORT_SYMBOL_GPL(nvme_auth_transform_key); 336 337 static int nvme_auth_hash_skey(int hmac_id, const u8 *skey, size_t skey_len, 338 u8 *hkey) 339 { 340 const char *digest_name; 341 struct crypto_shash *tfm; 342 int ret; 343 344 digest_name = nvme_auth_digest_name(hmac_id); 345 if (!digest_name) { 346 pr_debug("%s: failed to get digest for %d\n", __func__, 347 hmac_id); 348 return -EINVAL; 349 } 350 tfm = crypto_alloc_shash(digest_name, 0, 0); 351 if (IS_ERR(tfm)) 352 return -ENOMEM; 353 354 ret = crypto_shash_tfm_digest(tfm, skey, skey_len, hkey); 355 if (ret < 0) 356 pr_debug("%s: Failed to hash digest len %zu\n", __func__, 357 skey_len); 358 359 crypto_free_shash(tfm); 360 return ret; 361 } 362 363 int nvme_auth_augmented_challenge(u8 hmac_id, const u8 *skey, size_t skey_len, 364 const u8 *challenge, u8 *aug, size_t hlen) 365 { 366 struct crypto_shash *tfm; 367 u8 *hashed_key; 368 const char *hmac_name; 369 int ret; 370 371 hashed_key = kmalloc(hlen, GFP_KERNEL); 372 if (!hashed_key) 373 return -ENOMEM; 374 375 ret = nvme_auth_hash_skey(hmac_id, skey, 376 skey_len, hashed_key); 377 if (ret < 0) 378 goto out_free_key; 379 380 hmac_name = nvme_auth_hmac_name(hmac_id); 381 if (!hmac_name) { 382 pr_warn("%s: invalid hash algorithm %d\n", 383 __func__, hmac_id); 384 ret = -EINVAL; 385 goto out_free_key; 386 } 387 388 tfm = crypto_alloc_shash(hmac_name, 0, 0); 389 if (IS_ERR(tfm)) { 390 ret = PTR_ERR(tfm); 391 goto out_free_key; 392 } 393 394 ret = crypto_shash_setkey(tfm, hashed_key, hlen); 395 if (ret) 396 goto out_free_hash; 397 398 ret = crypto_shash_tfm_digest(tfm, challenge, hlen, aug); 399 out_free_hash: 400 crypto_free_shash(tfm); 401 out_free_key: 402 kfree_sensitive(hashed_key); 403 return ret; 404 } 405 EXPORT_SYMBOL_GPL(nvme_auth_augmented_challenge); 406 407 int nvme_auth_gen_privkey(struct crypto_kpp *dh_tfm, u8 dh_gid) 408 { 409 int ret; 410 411 ret = crypto_kpp_set_secret(dh_tfm, NULL, 0); 412 if (ret) 413 pr_debug("failed to set private key, error %d\n", ret); 414 415 return ret; 416 } 417 EXPORT_SYMBOL_GPL(nvme_auth_gen_privkey); 418 419 int nvme_auth_gen_pubkey(struct crypto_kpp *dh_tfm, 420 u8 *host_key, size_t host_key_len) 421 { 422 struct kpp_request *req; 423 struct crypto_wait wait; 424 struct scatterlist dst; 425 int ret; 426 427 req = kpp_request_alloc(dh_tfm, GFP_KERNEL); 428 if (!req) 429 return -ENOMEM; 430 431 crypto_init_wait(&wait); 432 kpp_request_set_input(req, NULL, 0); 433 sg_init_one(&dst, host_key, host_key_len); 434 kpp_request_set_output(req, &dst, host_key_len); 435 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 436 crypto_req_done, &wait); 437 438 ret = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait); 439 kpp_request_free(req); 440 return ret; 441 } 442 EXPORT_SYMBOL_GPL(nvme_auth_gen_pubkey); 443 444 int nvme_auth_gen_shared_secret(struct crypto_kpp *dh_tfm, 445 const u8 *ctrl_key, size_t ctrl_key_len, 446 u8 *sess_key, size_t sess_key_len) 447 { 448 struct kpp_request *req; 449 struct crypto_wait wait; 450 struct scatterlist src, dst; 451 int ret; 452 453 req = kpp_request_alloc(dh_tfm, GFP_KERNEL); 454 if (!req) 455 return -ENOMEM; 456 457 crypto_init_wait(&wait); 458 sg_init_one(&src, ctrl_key, ctrl_key_len); 459 kpp_request_set_input(req, &src, ctrl_key_len); 460 sg_init_one(&dst, sess_key, sess_key_len); 461 kpp_request_set_output(req, &dst, sess_key_len); 462 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 463 crypto_req_done, &wait); 464 465 ret = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait); 466 467 kpp_request_free(req); 468 return ret; 469 } 470 EXPORT_SYMBOL_GPL(nvme_auth_gen_shared_secret); 471 472 int nvme_auth_parse_key(const char *secret, struct nvme_dhchap_key **ret_key) 473 { 474 struct nvme_dhchap_key *key; 475 u8 key_hash; 476 477 if (!secret) { 478 *ret_key = NULL; 479 return 0; 480 } 481 482 if (sscanf(secret, "DHHC-1:%hhd:%*s:", &key_hash) != 1) 483 return -EINVAL; 484 485 /* Pass in the secret without the 'DHHC-1:XX:' prefix */ 486 key = nvme_auth_extract_key(secret + 10, key_hash); 487 if (IS_ERR(key)) { 488 *ret_key = NULL; 489 return PTR_ERR(key); 490 } 491 492 *ret_key = key; 493 return 0; 494 } 495 EXPORT_SYMBOL_GPL(nvme_auth_parse_key); 496 497 /** 498 * nvme_auth_generate_psk - Generate a PSK for TLS 499 * @hmac_id: Hash function identifier 500 * @skey: Session key 501 * @skey_len: Length of @skey 502 * @c1: Value of challenge C1 503 * @c2: Value of challenge C2 504 * @hash_len: Hash length of the hash algorithm 505 * @ret_psk: Pointer to the resulting generated PSK 506 * @ret_len: length of @ret_psk 507 * 508 * Generate a PSK for TLS as specified in NVMe base specification, section 509 * 8.13.5.9: Generated PSK for TLS 510 * 511 * The generated PSK for TLS shall be computed applying the HMAC function 512 * using the hash function H( ) selected by the HashID parameter in the 513 * DH-HMAC-CHAP_Challenge message with the session key KS as key to the 514 * concatenation of the two challenges C1 and C2 (i.e., generated 515 * PSK = HMAC(KS, C1 || C2)). 516 * 517 * Returns 0 on success with a valid generated PSK pointer in @ret_psk and 518 * the length of @ret_psk in @ret_len, or a negative error number otherwise. 519 */ 520 int nvme_auth_generate_psk(u8 hmac_id, const u8 *skey, size_t skey_len, 521 const u8 *c1, const u8 *c2, size_t hash_len, 522 u8 **ret_psk, size_t *ret_len) 523 { 524 struct crypto_shash *tfm; 525 SHASH_DESC_ON_STACK(shash, tfm); 526 u8 *psk; 527 const char *hmac_name; 528 int ret, psk_len; 529 530 if (!c1 || !c2) 531 return -EINVAL; 532 533 hmac_name = nvme_auth_hmac_name(hmac_id); 534 if (!hmac_name) { 535 pr_warn("%s: invalid hash algorithm %d\n", 536 __func__, hmac_id); 537 return -EINVAL; 538 } 539 540 tfm = crypto_alloc_shash(hmac_name, 0, 0); 541 if (IS_ERR(tfm)) 542 return PTR_ERR(tfm); 543 544 psk_len = crypto_shash_digestsize(tfm); 545 psk = kzalloc(psk_len, GFP_KERNEL); 546 if (!psk) { 547 ret = -ENOMEM; 548 goto out_free_tfm; 549 } 550 551 shash->tfm = tfm; 552 ret = crypto_shash_setkey(tfm, skey, skey_len); 553 if (ret) 554 goto out_free_psk; 555 556 ret = crypto_shash_init(shash); 557 if (ret) 558 goto out_free_psk; 559 560 ret = crypto_shash_update(shash, c1, hash_len); 561 if (ret) 562 goto out_free_psk; 563 564 ret = crypto_shash_update(shash, c2, hash_len); 565 if (ret) 566 goto out_free_psk; 567 568 ret = crypto_shash_final(shash, psk); 569 if (!ret) { 570 *ret_psk = psk; 571 *ret_len = psk_len; 572 } 573 574 out_free_psk: 575 if (ret) 576 kfree_sensitive(psk); 577 out_free_tfm: 578 crypto_free_shash(tfm); 579 580 return ret; 581 } 582 EXPORT_SYMBOL_GPL(nvme_auth_generate_psk); 583 584 /** 585 * nvme_auth_generate_digest - Generate TLS PSK digest 586 * @hmac_id: Hash function identifier 587 * @psk: Generated input PSK 588 * @psk_len: Length of @psk 589 * @subsysnqn: NQN of the subsystem 590 * @hostnqn: NQN of the host 591 * @ret_digest: Pointer to the returned digest 592 * 593 * Generate a TLS PSK digest as specified in TP8018 Section 3.6.1.3: 594 * TLS PSK and PSK identity Derivation 595 * 596 * The PSK digest shall be computed by encoding in Base64 (refer to RFC 597 * 4648) the result of the application of the HMAC function using the hash 598 * function specified in item 4 above (ie the hash function of the cipher 599 * suite associated with the PSK identity) with the PSK as HMAC key to the 600 * concatenation of: 601 * - the NQN of the host (i.e., NQNh) not including the null terminator; 602 * - a space character; 603 * - the NQN of the NVM subsystem (i.e., NQNc) not including the null 604 * terminator; 605 * - a space character; and 606 * - the seventeen ASCII characters "NVMe-over-Fabrics" 607 * (i.e., <PSK digest> = Base64(HMAC(PSK, NQNh || " " || NQNc || " " || 608 * "NVMe-over-Fabrics"))). 609 * The length of the PSK digest depends on the hash function used to compute 610 * it as follows: 611 * - If the SHA-256 hash function is used, the resulting PSK digest is 44 612 * characters long; or 613 * - If the SHA-384 hash function is used, the resulting PSK digest is 64 614 * characters long. 615 * 616 * Returns 0 on success with a valid digest pointer in @ret_digest, or a 617 * negative error number on failure. 618 */ 619 int nvme_auth_generate_digest(u8 hmac_id, const u8 *psk, size_t psk_len, 620 const char *subsysnqn, const char *hostnqn, 621 char **ret_digest) 622 { 623 struct crypto_shash *tfm; 624 SHASH_DESC_ON_STACK(shash, tfm); 625 u8 *digest; 626 char *enc; 627 const char *hmac_name; 628 size_t digest_len, hmac_len; 629 int ret; 630 631 if (WARN_ON(!subsysnqn || !hostnqn)) 632 return -EINVAL; 633 634 hmac_name = nvme_auth_hmac_name(hmac_id); 635 if (!hmac_name) { 636 pr_warn("%s: invalid hash algorithm %d\n", 637 __func__, hmac_id); 638 return -EINVAL; 639 } 640 641 switch (nvme_auth_hmac_hash_len(hmac_id)) { 642 case 32: 643 hmac_len = 44; 644 break; 645 case 48: 646 hmac_len = 64; 647 break; 648 default: 649 pr_warn("%s: invalid hash algorithm '%s'\n", 650 __func__, hmac_name); 651 return -EINVAL; 652 } 653 654 enc = kzalloc(hmac_len + 1, GFP_KERNEL); 655 if (!enc) 656 return -ENOMEM; 657 658 tfm = crypto_alloc_shash(hmac_name, 0, 0); 659 if (IS_ERR(tfm)) { 660 ret = PTR_ERR(tfm); 661 goto out_free_enc; 662 } 663 664 digest_len = crypto_shash_digestsize(tfm); 665 digest = kzalloc(digest_len, GFP_KERNEL); 666 if (!digest) { 667 ret = -ENOMEM; 668 goto out_free_tfm; 669 } 670 671 shash->tfm = tfm; 672 ret = crypto_shash_setkey(tfm, psk, psk_len); 673 if (ret) 674 goto out_free_digest; 675 676 ret = crypto_shash_init(shash); 677 if (ret) 678 goto out_free_digest; 679 680 ret = crypto_shash_update(shash, hostnqn, strlen(hostnqn)); 681 if (ret) 682 goto out_free_digest; 683 684 ret = crypto_shash_update(shash, " ", 1); 685 if (ret) 686 goto out_free_digest; 687 688 ret = crypto_shash_update(shash, subsysnqn, strlen(subsysnqn)); 689 if (ret) 690 goto out_free_digest; 691 692 ret = crypto_shash_update(shash, " NVMe-over-Fabrics", 18); 693 if (ret) 694 goto out_free_digest; 695 696 ret = crypto_shash_final(shash, digest); 697 if (ret) 698 goto out_free_digest; 699 700 ret = base64_encode(digest, digest_len, enc, true, BASE64_STD); 701 if (ret < hmac_len) { 702 ret = -ENOKEY; 703 goto out_free_digest; 704 } 705 *ret_digest = enc; 706 ret = 0; 707 708 out_free_digest: 709 kfree_sensitive(digest); 710 out_free_tfm: 711 crypto_free_shash(tfm); 712 out_free_enc: 713 if (ret) 714 kfree_sensitive(enc); 715 716 return ret; 717 } 718 EXPORT_SYMBOL_GPL(nvme_auth_generate_digest); 719 720 /** 721 * hkdf_expand_label - HKDF-Expand-Label (RFC 8846 section 7.1) 722 * @hmac_tfm: hash context keyed with pseudorandom key 723 * @label: ASCII label without "tls13 " prefix 724 * @labellen: length of @label 725 * @context: context bytes 726 * @contextlen: length of @context 727 * @okm: output keying material 728 * @okmlen: length of @okm 729 * 730 * Build the TLS 1.3 HkdfLabel structure and invoke hkdf_expand(). 731 * 732 * Returns 0 on success with output keying material stored in @okm, 733 * or a negative errno value otherwise. 734 */ 735 static int hkdf_expand_label(struct crypto_shash *hmac_tfm, 736 const u8 *label, unsigned int labellen, 737 const u8 *context, unsigned int contextlen, 738 u8 *okm, unsigned int okmlen) 739 { 740 int err; 741 u8 *info; 742 unsigned int infolen; 743 const char *tls13_prefix = "tls13 "; 744 unsigned int prefixlen = strlen(tls13_prefix); 745 746 if (WARN_ON(labellen > (255 - prefixlen))) 747 return -EINVAL; 748 if (WARN_ON(contextlen > 255)) 749 return -EINVAL; 750 751 infolen = 2 + (1 + prefixlen + labellen) + (1 + contextlen); 752 info = kzalloc(infolen, GFP_KERNEL); 753 if (!info) 754 return -ENOMEM; 755 756 /* HkdfLabel.Length */ 757 put_unaligned_be16(okmlen, info); 758 759 /* HkdfLabel.Label */ 760 info[2] = prefixlen + labellen; 761 memcpy(info + 3, tls13_prefix, prefixlen); 762 memcpy(info + 3 + prefixlen, label, labellen); 763 764 /* HkdfLabel.Context */ 765 info[3 + prefixlen + labellen] = contextlen; 766 memcpy(info + 4 + prefixlen + labellen, context, contextlen); 767 768 err = hkdf_expand(hmac_tfm, info, infolen, okm, okmlen); 769 kfree_sensitive(info); 770 return err; 771 } 772 773 /** 774 * nvme_auth_derive_tls_psk - Derive TLS PSK 775 * @hmac_id: Hash function identifier 776 * @psk: generated input PSK 777 * @psk_len: size of @psk 778 * @psk_digest: TLS PSK digest 779 * @ret_psk: Pointer to the resulting TLS PSK 780 * 781 * Derive a TLS PSK as specified in TP8018 Section 3.6.1.3: 782 * TLS PSK and PSK identity Derivation 783 * 784 * The TLS PSK shall be derived as follows from an input PSK 785 * (i.e., either a retained PSK or a generated PSK) and a PSK 786 * identity using the HKDF-Extract and HKDF-Expand-Label operations 787 * (refer to RFC 5869 and RFC 8446) where the hash function is the 788 * one specified by the hash specifier of the PSK identity: 789 * 1. PRK = HKDF-Extract(0, Input PSK); and 790 * 2. TLS PSK = HKDF-Expand-Label(PRK, "nvme-tls-psk", PskIdentityContext, L), 791 * where PskIdentityContext is the hash identifier indicated in 792 * the PSK identity concatenated to a space character and to the 793 * Base64 PSK digest (i.e., "<hash> <PSK digest>") and L is the 794 * output size in bytes of the hash function (i.e., 32 for SHA-256 795 * and 48 for SHA-384). 796 * 797 * Returns 0 on success with a valid psk pointer in @ret_psk or a negative 798 * error number otherwise. 799 */ 800 int nvme_auth_derive_tls_psk(int hmac_id, const u8 *psk, size_t psk_len, 801 const char *psk_digest, u8 **ret_psk) 802 { 803 struct crypto_shash *hmac_tfm; 804 const char *hmac_name; 805 const char *label = "nvme-tls-psk"; 806 static const u8 default_salt[NVME_AUTH_MAX_DIGEST_SIZE]; 807 size_t prk_len; 808 const char *ctx; 809 u8 *prk, *tls_key; 810 int ret; 811 812 hmac_name = nvme_auth_hmac_name(hmac_id); 813 if (!hmac_name) { 814 pr_warn("%s: invalid hash algorithm %d\n", 815 __func__, hmac_id); 816 return -EINVAL; 817 } 818 if (hmac_id == NVME_AUTH_HASH_SHA512) { 819 pr_warn("%s: unsupported hash algorithm %s\n", 820 __func__, hmac_name); 821 return -EINVAL; 822 } 823 824 if (psk_len != nvme_auth_hmac_hash_len(hmac_id)) { 825 pr_warn("%s: unexpected psk_len %zu\n", __func__, psk_len); 826 return -EINVAL; 827 } 828 829 hmac_tfm = crypto_alloc_shash(hmac_name, 0, 0); 830 if (IS_ERR(hmac_tfm)) 831 return PTR_ERR(hmac_tfm); 832 833 prk_len = crypto_shash_digestsize(hmac_tfm); 834 prk = kzalloc(prk_len, GFP_KERNEL); 835 if (!prk) { 836 ret = -ENOMEM; 837 goto out_free_shash; 838 } 839 840 if (WARN_ON(prk_len > NVME_AUTH_MAX_DIGEST_SIZE)) { 841 ret = -EINVAL; 842 goto out_free_prk; 843 } 844 ret = hkdf_extract(hmac_tfm, psk, psk_len, 845 default_salt, prk_len, prk); 846 if (ret) 847 goto out_free_prk; 848 849 ret = crypto_shash_setkey(hmac_tfm, prk, prk_len); 850 if (ret) 851 goto out_free_prk; 852 853 ctx = kasprintf(GFP_KERNEL, "%02d %s", hmac_id, psk_digest); 854 if (!ctx) { 855 ret = -ENOMEM; 856 goto out_free_prk; 857 } 858 859 tls_key = kzalloc(psk_len, GFP_KERNEL); 860 if (!tls_key) { 861 ret = -ENOMEM; 862 goto out_free_ctx; 863 } 864 ret = hkdf_expand_label(hmac_tfm, 865 label, strlen(label), 866 ctx, strlen(ctx), 867 tls_key, psk_len); 868 if (ret) { 869 kfree(tls_key); 870 goto out_free_ctx; 871 } 872 *ret_psk = tls_key; 873 874 out_free_ctx: 875 kfree(ctx); 876 out_free_prk: 877 kfree(prk); 878 out_free_shash: 879 crypto_free_shash(hmac_tfm); 880 881 return ret; 882 } 883 EXPORT_SYMBOL_GPL(nvme_auth_derive_tls_psk); 884 885 MODULE_DESCRIPTION("NVMe Authentication framework"); 886 MODULE_LICENSE("GPL v2"); 887