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 static int nvme_auth_hmac(u8 hmac_id, const u8 *key, size_t key_len, 304 const u8 *data, size_t data_len, u8 *out) 305 { 306 struct nvme_auth_hmac_ctx hmac; 307 int ret; 308 309 ret = nvme_auth_hmac_init(&hmac, hmac_id, key, key_len); 310 if (ret == 0) { 311 nvme_auth_hmac_update(&hmac, data, data_len); 312 nvme_auth_hmac_final(&hmac, out); 313 } 314 return ret; 315 } 316 317 static int nvme_auth_hash(u8 hmac_id, const u8 *data, size_t data_len, u8 *out) 318 { 319 switch (hmac_id) { 320 case NVME_AUTH_HASH_SHA256: 321 sha256(data, data_len, out); 322 return 0; 323 case NVME_AUTH_HASH_SHA384: 324 sha384(data, data_len, out); 325 return 0; 326 case NVME_AUTH_HASH_SHA512: 327 sha512(data, data_len, out); 328 return 0; 329 } 330 pr_warn("%s: invalid hash algorithm %d\n", __func__, hmac_id); 331 return -EINVAL; 332 } 333 334 struct nvme_dhchap_key *nvme_auth_transform_key( 335 const struct nvme_dhchap_key *key, const char *nqn) 336 { 337 struct nvme_auth_hmac_ctx hmac; 338 struct nvme_dhchap_key *transformed_key; 339 int ret, key_len; 340 341 if (!key) { 342 pr_warn("No key specified\n"); 343 return ERR_PTR(-ENOKEY); 344 } 345 if (key->hash == 0) { 346 key_len = nvme_auth_key_struct_size(key->len); 347 transformed_key = kmemdup(key, key_len, GFP_KERNEL); 348 if (!transformed_key) 349 return ERR_PTR(-ENOMEM); 350 return transformed_key; 351 } 352 ret = nvme_auth_hmac_init(&hmac, key->hash, key->key, key->len); 353 if (ret) 354 return ERR_PTR(ret); 355 key_len = nvme_auth_hmac_hash_len(key->hash); 356 transformed_key = nvme_auth_alloc_key(key_len, key->hash); 357 if (!transformed_key) { 358 memzero_explicit(&hmac, sizeof(hmac)); 359 return ERR_PTR(-ENOMEM); 360 } 361 nvme_auth_hmac_update(&hmac, nqn, strlen(nqn)); 362 nvme_auth_hmac_update(&hmac, "NVMe-over-Fabrics", 17); 363 nvme_auth_hmac_final(&hmac, transformed_key->key); 364 return transformed_key; 365 } 366 EXPORT_SYMBOL_GPL(nvme_auth_transform_key); 367 368 int nvme_auth_augmented_challenge(u8 hmac_id, const u8 *skey, size_t skey_len, 369 const u8 *challenge, u8 *aug, size_t hlen) 370 { 371 u8 hashed_key[NVME_AUTH_MAX_DIGEST_SIZE]; 372 int ret; 373 374 ret = nvme_auth_hash(hmac_id, skey, skey_len, hashed_key); 375 if (ret) 376 return ret; 377 ret = nvme_auth_hmac(hmac_id, hashed_key, hlen, challenge, hlen, aug); 378 memzero_explicit(hashed_key, sizeof(hashed_key)); 379 return ret; 380 } 381 EXPORT_SYMBOL_GPL(nvme_auth_augmented_challenge); 382 383 int nvme_auth_gen_privkey(struct crypto_kpp *dh_tfm, u8 dh_gid) 384 { 385 int ret; 386 387 ret = crypto_kpp_set_secret(dh_tfm, NULL, 0); 388 if (ret) 389 pr_debug("failed to set private key, error %d\n", ret); 390 391 return ret; 392 } 393 EXPORT_SYMBOL_GPL(nvme_auth_gen_privkey); 394 395 int nvme_auth_gen_pubkey(struct crypto_kpp *dh_tfm, 396 u8 *host_key, size_t host_key_len) 397 { 398 struct kpp_request *req; 399 struct crypto_wait wait; 400 struct scatterlist dst; 401 int ret; 402 403 req = kpp_request_alloc(dh_tfm, GFP_KERNEL); 404 if (!req) 405 return -ENOMEM; 406 407 crypto_init_wait(&wait); 408 kpp_request_set_input(req, NULL, 0); 409 sg_init_one(&dst, host_key, host_key_len); 410 kpp_request_set_output(req, &dst, host_key_len); 411 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 412 crypto_req_done, &wait); 413 414 ret = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait); 415 kpp_request_free(req); 416 return ret; 417 } 418 EXPORT_SYMBOL_GPL(nvme_auth_gen_pubkey); 419 420 int nvme_auth_gen_shared_secret(struct crypto_kpp *dh_tfm, 421 const u8 *ctrl_key, size_t ctrl_key_len, 422 u8 *sess_key, size_t sess_key_len) 423 { 424 struct kpp_request *req; 425 struct crypto_wait wait; 426 struct scatterlist src, dst; 427 int ret; 428 429 req = kpp_request_alloc(dh_tfm, GFP_KERNEL); 430 if (!req) 431 return -ENOMEM; 432 433 crypto_init_wait(&wait); 434 sg_init_one(&src, ctrl_key, ctrl_key_len); 435 kpp_request_set_input(req, &src, ctrl_key_len); 436 sg_init_one(&dst, sess_key, sess_key_len); 437 kpp_request_set_output(req, &dst, sess_key_len); 438 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, 439 crypto_req_done, &wait); 440 441 ret = crypto_wait_req(crypto_kpp_compute_shared_secret(req), &wait); 442 443 kpp_request_free(req); 444 return ret; 445 } 446 EXPORT_SYMBOL_GPL(nvme_auth_gen_shared_secret); 447 448 int nvme_auth_parse_key(const char *secret, struct nvme_dhchap_key **ret_key) 449 { 450 struct nvme_dhchap_key *key; 451 u8 key_hash; 452 453 if (!secret) { 454 *ret_key = NULL; 455 return 0; 456 } 457 458 if (sscanf(secret, "DHHC-1:%hhd:%*s:", &key_hash) != 1) 459 return -EINVAL; 460 461 /* Pass in the secret without the 'DHHC-1:XX:' prefix */ 462 key = nvme_auth_extract_key(secret + 10, key_hash); 463 if (IS_ERR(key)) { 464 *ret_key = NULL; 465 return PTR_ERR(key); 466 } 467 468 *ret_key = key; 469 return 0; 470 } 471 EXPORT_SYMBOL_GPL(nvme_auth_parse_key); 472 473 /** 474 * nvme_auth_generate_psk - Generate a PSK for TLS 475 * @hmac_id: Hash function identifier 476 * @skey: Session key 477 * @skey_len: Length of @skey 478 * @c1: Value of challenge C1 479 * @c2: Value of challenge C2 480 * @hash_len: Hash length of the hash algorithm 481 * @ret_psk: Pointer to the resulting generated PSK 482 * @ret_len: length of @ret_psk 483 * 484 * Generate a PSK for TLS as specified in NVMe base specification, section 485 * 8.13.5.9: Generated PSK for TLS 486 * 487 * The generated PSK for TLS shall be computed applying the HMAC function 488 * using the hash function H( ) selected by the HashID parameter in the 489 * DH-HMAC-CHAP_Challenge message with the session key KS as key to the 490 * concatenation of the two challenges C1 and C2 (i.e., generated 491 * PSK = HMAC(KS, C1 || C2)). 492 * 493 * Returns 0 on success with a valid generated PSK pointer in @ret_psk and 494 * the length of @ret_psk in @ret_len, or a negative error number otherwise. 495 */ 496 int nvme_auth_generate_psk(u8 hmac_id, const u8 *skey, size_t skey_len, 497 const u8 *c1, const u8 *c2, size_t hash_len, 498 u8 **ret_psk, size_t *ret_len) 499 { 500 struct crypto_shash *tfm; 501 SHASH_DESC_ON_STACK(shash, tfm); 502 u8 *psk; 503 const char *hmac_name; 504 int ret, psk_len; 505 506 if (!c1 || !c2) 507 return -EINVAL; 508 509 hmac_name = nvme_auth_hmac_name(hmac_id); 510 if (!hmac_name) { 511 pr_warn("%s: invalid hash algorithm %d\n", 512 __func__, hmac_id); 513 return -EINVAL; 514 } 515 516 tfm = crypto_alloc_shash(hmac_name, 0, 0); 517 if (IS_ERR(tfm)) 518 return PTR_ERR(tfm); 519 520 psk_len = crypto_shash_digestsize(tfm); 521 psk = kzalloc(psk_len, GFP_KERNEL); 522 if (!psk) { 523 ret = -ENOMEM; 524 goto out_free_tfm; 525 } 526 527 shash->tfm = tfm; 528 ret = crypto_shash_setkey(tfm, skey, skey_len); 529 if (ret) 530 goto out_free_psk; 531 532 ret = crypto_shash_init(shash); 533 if (ret) 534 goto out_free_psk; 535 536 ret = crypto_shash_update(shash, c1, hash_len); 537 if (ret) 538 goto out_free_psk; 539 540 ret = crypto_shash_update(shash, c2, hash_len); 541 if (ret) 542 goto out_free_psk; 543 544 ret = crypto_shash_final(shash, psk); 545 if (!ret) { 546 *ret_psk = psk; 547 *ret_len = psk_len; 548 } 549 550 out_free_psk: 551 if (ret) 552 kfree_sensitive(psk); 553 out_free_tfm: 554 crypto_free_shash(tfm); 555 556 return ret; 557 } 558 EXPORT_SYMBOL_GPL(nvme_auth_generate_psk); 559 560 /** 561 * nvme_auth_generate_digest - Generate TLS PSK digest 562 * @hmac_id: Hash function identifier 563 * @psk: Generated input PSK 564 * @psk_len: Length of @psk 565 * @subsysnqn: NQN of the subsystem 566 * @hostnqn: NQN of the host 567 * @ret_digest: Pointer to the returned digest 568 * 569 * Generate a TLS PSK digest as specified in TP8018 Section 3.6.1.3: 570 * TLS PSK and PSK identity Derivation 571 * 572 * The PSK digest shall be computed by encoding in Base64 (refer to RFC 573 * 4648) the result of the application of the HMAC function using the hash 574 * function specified in item 4 above (ie the hash function of the cipher 575 * suite associated with the PSK identity) with the PSK as HMAC key to the 576 * concatenation of: 577 * - the NQN of the host (i.e., NQNh) not including the null terminator; 578 * - a space character; 579 * - the NQN of the NVM subsystem (i.e., NQNc) not including the null 580 * terminator; 581 * - a space character; and 582 * - the seventeen ASCII characters "NVMe-over-Fabrics" 583 * (i.e., <PSK digest> = Base64(HMAC(PSK, NQNh || " " || NQNc || " " || 584 * "NVMe-over-Fabrics"))). 585 * The length of the PSK digest depends on the hash function used to compute 586 * it as follows: 587 * - If the SHA-256 hash function is used, the resulting PSK digest is 44 588 * characters long; or 589 * - If the SHA-384 hash function is used, the resulting PSK digest is 64 590 * characters long. 591 * 592 * Returns 0 on success with a valid digest pointer in @ret_digest, or a 593 * negative error number on failure. 594 */ 595 int nvme_auth_generate_digest(u8 hmac_id, const u8 *psk, size_t psk_len, 596 const char *subsysnqn, const char *hostnqn, 597 char **ret_digest) 598 { 599 struct crypto_shash *tfm; 600 SHASH_DESC_ON_STACK(shash, tfm); 601 u8 *digest; 602 char *enc; 603 const char *hmac_name; 604 size_t digest_len, hmac_len; 605 int ret; 606 607 if (WARN_ON(!subsysnqn || !hostnqn)) 608 return -EINVAL; 609 610 hmac_name = nvme_auth_hmac_name(hmac_id); 611 if (!hmac_name) { 612 pr_warn("%s: invalid hash algorithm %d\n", 613 __func__, hmac_id); 614 return -EINVAL; 615 } 616 617 switch (nvme_auth_hmac_hash_len(hmac_id)) { 618 case 32: 619 hmac_len = 44; 620 break; 621 case 48: 622 hmac_len = 64; 623 break; 624 default: 625 pr_warn("%s: invalid hash algorithm '%s'\n", 626 __func__, hmac_name); 627 return -EINVAL; 628 } 629 630 enc = kzalloc(hmac_len + 1, GFP_KERNEL); 631 if (!enc) 632 return -ENOMEM; 633 634 tfm = crypto_alloc_shash(hmac_name, 0, 0); 635 if (IS_ERR(tfm)) { 636 ret = PTR_ERR(tfm); 637 goto out_free_enc; 638 } 639 640 digest_len = crypto_shash_digestsize(tfm); 641 digest = kzalloc(digest_len, GFP_KERNEL); 642 if (!digest) { 643 ret = -ENOMEM; 644 goto out_free_tfm; 645 } 646 647 shash->tfm = tfm; 648 ret = crypto_shash_setkey(tfm, psk, psk_len); 649 if (ret) 650 goto out_free_digest; 651 652 ret = crypto_shash_init(shash); 653 if (ret) 654 goto out_free_digest; 655 656 ret = crypto_shash_update(shash, hostnqn, strlen(hostnqn)); 657 if (ret) 658 goto out_free_digest; 659 660 ret = crypto_shash_update(shash, " ", 1); 661 if (ret) 662 goto out_free_digest; 663 664 ret = crypto_shash_update(shash, subsysnqn, strlen(subsysnqn)); 665 if (ret) 666 goto out_free_digest; 667 668 ret = crypto_shash_update(shash, " NVMe-over-Fabrics", 18); 669 if (ret) 670 goto out_free_digest; 671 672 ret = crypto_shash_final(shash, digest); 673 if (ret) 674 goto out_free_digest; 675 676 ret = base64_encode(digest, digest_len, enc, true, BASE64_STD); 677 if (ret < hmac_len) { 678 ret = -ENOKEY; 679 goto out_free_digest; 680 } 681 *ret_digest = enc; 682 ret = 0; 683 684 out_free_digest: 685 kfree_sensitive(digest); 686 out_free_tfm: 687 crypto_free_shash(tfm); 688 out_free_enc: 689 if (ret) 690 kfree_sensitive(enc); 691 692 return ret; 693 } 694 EXPORT_SYMBOL_GPL(nvme_auth_generate_digest); 695 696 /** 697 * hkdf_expand_label - HKDF-Expand-Label (RFC 8846 section 7.1) 698 * @hmac_tfm: hash context keyed with pseudorandom key 699 * @label: ASCII label without "tls13 " prefix 700 * @labellen: length of @label 701 * @context: context bytes 702 * @contextlen: length of @context 703 * @okm: output keying material 704 * @okmlen: length of @okm 705 * 706 * Build the TLS 1.3 HkdfLabel structure and invoke hkdf_expand(). 707 * 708 * Returns 0 on success with output keying material stored in @okm, 709 * or a negative errno value otherwise. 710 */ 711 static int hkdf_expand_label(struct crypto_shash *hmac_tfm, 712 const u8 *label, unsigned int labellen, 713 const u8 *context, unsigned int contextlen, 714 u8 *okm, unsigned int okmlen) 715 { 716 int err; 717 u8 *info; 718 unsigned int infolen; 719 const char *tls13_prefix = "tls13 "; 720 unsigned int prefixlen = strlen(tls13_prefix); 721 722 if (WARN_ON(labellen > (255 - prefixlen))) 723 return -EINVAL; 724 if (WARN_ON(contextlen > 255)) 725 return -EINVAL; 726 727 infolen = 2 + (1 + prefixlen + labellen) + (1 + contextlen); 728 info = kzalloc(infolen, GFP_KERNEL); 729 if (!info) 730 return -ENOMEM; 731 732 /* HkdfLabel.Length */ 733 put_unaligned_be16(okmlen, info); 734 735 /* HkdfLabel.Label */ 736 info[2] = prefixlen + labellen; 737 memcpy(info + 3, tls13_prefix, prefixlen); 738 memcpy(info + 3 + prefixlen, label, labellen); 739 740 /* HkdfLabel.Context */ 741 info[3 + prefixlen + labellen] = contextlen; 742 memcpy(info + 4 + prefixlen + labellen, context, contextlen); 743 744 err = hkdf_expand(hmac_tfm, info, infolen, okm, okmlen); 745 kfree_sensitive(info); 746 return err; 747 } 748 749 /** 750 * nvme_auth_derive_tls_psk - Derive TLS PSK 751 * @hmac_id: Hash function identifier 752 * @psk: generated input PSK 753 * @psk_len: size of @psk 754 * @psk_digest: TLS PSK digest 755 * @ret_psk: Pointer to the resulting TLS PSK 756 * 757 * Derive a TLS PSK as specified in TP8018 Section 3.6.1.3: 758 * TLS PSK and PSK identity Derivation 759 * 760 * The TLS PSK shall be derived as follows from an input PSK 761 * (i.e., either a retained PSK or a generated PSK) and a PSK 762 * identity using the HKDF-Extract and HKDF-Expand-Label operations 763 * (refer to RFC 5869 and RFC 8446) where the hash function is the 764 * one specified by the hash specifier of the PSK identity: 765 * 1. PRK = HKDF-Extract(0, Input PSK); and 766 * 2. TLS PSK = HKDF-Expand-Label(PRK, "nvme-tls-psk", PskIdentityContext, L), 767 * where PskIdentityContext is the hash identifier indicated in 768 * the PSK identity concatenated to a space character and to the 769 * Base64 PSK digest (i.e., "<hash> <PSK digest>") and L is the 770 * output size in bytes of the hash function (i.e., 32 for SHA-256 771 * and 48 for SHA-384). 772 * 773 * Returns 0 on success with a valid psk pointer in @ret_psk or a negative 774 * error number otherwise. 775 */ 776 int nvme_auth_derive_tls_psk(int hmac_id, const u8 *psk, size_t psk_len, 777 const char *psk_digest, u8 **ret_psk) 778 { 779 struct crypto_shash *hmac_tfm; 780 const char *hmac_name; 781 const char *label = "nvme-tls-psk"; 782 static const u8 default_salt[NVME_AUTH_MAX_DIGEST_SIZE]; 783 size_t prk_len; 784 const char *ctx; 785 u8 *prk, *tls_key; 786 int ret; 787 788 hmac_name = nvme_auth_hmac_name(hmac_id); 789 if (!hmac_name) { 790 pr_warn("%s: invalid hash algorithm %d\n", 791 __func__, hmac_id); 792 return -EINVAL; 793 } 794 if (hmac_id == NVME_AUTH_HASH_SHA512) { 795 pr_warn("%s: unsupported hash algorithm %s\n", 796 __func__, hmac_name); 797 return -EINVAL; 798 } 799 800 if (psk_len != nvme_auth_hmac_hash_len(hmac_id)) { 801 pr_warn("%s: unexpected psk_len %zu\n", __func__, psk_len); 802 return -EINVAL; 803 } 804 805 hmac_tfm = crypto_alloc_shash(hmac_name, 0, 0); 806 if (IS_ERR(hmac_tfm)) 807 return PTR_ERR(hmac_tfm); 808 809 prk_len = crypto_shash_digestsize(hmac_tfm); 810 prk = kzalloc(prk_len, GFP_KERNEL); 811 if (!prk) { 812 ret = -ENOMEM; 813 goto out_free_shash; 814 } 815 816 if (WARN_ON(prk_len > NVME_AUTH_MAX_DIGEST_SIZE)) { 817 ret = -EINVAL; 818 goto out_free_prk; 819 } 820 ret = hkdf_extract(hmac_tfm, psk, psk_len, 821 default_salt, prk_len, prk); 822 if (ret) 823 goto out_free_prk; 824 825 ret = crypto_shash_setkey(hmac_tfm, prk, prk_len); 826 if (ret) 827 goto out_free_prk; 828 829 ctx = kasprintf(GFP_KERNEL, "%02d %s", hmac_id, psk_digest); 830 if (!ctx) { 831 ret = -ENOMEM; 832 goto out_free_prk; 833 } 834 835 tls_key = kzalloc(psk_len, GFP_KERNEL); 836 if (!tls_key) { 837 ret = -ENOMEM; 838 goto out_free_ctx; 839 } 840 ret = hkdf_expand_label(hmac_tfm, 841 label, strlen(label), 842 ctx, strlen(ctx), 843 tls_key, psk_len); 844 if (ret) { 845 kfree(tls_key); 846 goto out_free_ctx; 847 } 848 *ret_psk = tls_key; 849 850 out_free_ctx: 851 kfree(ctx); 852 out_free_prk: 853 kfree(prk); 854 out_free_shash: 855 crypto_free_shash(hmac_tfm); 856 857 return ret; 858 } 859 EXPORT_SYMBOL_GPL(nvme_auth_derive_tls_psk); 860 861 MODULE_DESCRIPTION("NVMe Authentication framework"); 862 MODULE_LICENSE("GPL v2"); 863