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